1 /*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
13 * disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 #ifndef _LINUX_DEVICE_H_
30 #define _LINUX_DEVICE_H_
31
32 #include <linux/types.h>
33 #include <linux/kobject.h>
34 #include <linux/list.h>
35 #include <linux/compiler.h>
36 #include <linux/types.h>
37 #include <linux/module.h>
38 #include <linux/workqueue.h>
39 #include <linux/sysfs.h>
40 #include <linux/kdev_t.h>
41 #include <asm/atomic.h>
42
43 #include <sys/bus.h>
44
45 enum irqreturn { IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
46 typedef enum irqreturn irqreturn_t;
47
48 struct class {
49 const char *name;
50 struct module *owner;
51 struct kobject kobj;
52 devclass_t bsdclass;
53 void (*class_release)(struct class *class);
54 void (*dev_release)(struct device *dev);
55 char * (*devnode)(struct device *dev, umode_t *mode);
56 };
57
58 struct device {
59 struct device *parent;
60 struct list_head irqents;
61 device_t bsddev;
62 dev_t devt;
63 struct class *class;
64 void (*release)(struct device *dev);
65 struct kobject kobj;
66 uint64_t *dma_mask;
67 void *driver_data;
68 unsigned int irq;
69 unsigned int msix;
70 unsigned int msix_max;
71 };
72
73 extern struct device linux_rootdev;
74 extern struct kobject class_root;
75
76 struct class_attribute {
77 struct attribute attr;
78 ssize_t (*show)(struct class *, struct class_attribute *, char *);
79 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
80 const void *(*namespace)(struct class *, const struct class_attribute *);
81 };
82
83 #define CLASS_ATTR(_name, _mode, _show, _store) \
84 struct class_attribute class_attr_##_name = \
85 { { #_name, NULL, _mode }, _show, _store }
86
87 struct device_attribute {
88 struct attribute attr;
89 ssize_t (*show)(struct device *,
90 struct device_attribute *, char *);
91 ssize_t (*store)(struct device *,
92 struct device_attribute *, const char *,
93 size_t);
94 };
95
96 #define DEVICE_ATTR(_name, _mode, _show, _store) \
97 struct device_attribute dev_attr_##_name = \
98 { { #_name, NULL, _mode }, _show, _store }
99
100 /* Simple class attribute that is just a static string */
101 struct class_attribute_string {
102 struct class_attribute attr;
103 char *str;
104 };
105
106 static inline ssize_t
show_class_attr_string(struct class * class,struct class_attribute * attr,char * buf)107 show_class_attr_string(struct class *class,
108 struct class_attribute *attr, char *buf)
109 {
110 struct class_attribute_string *cs;
111 cs = container_of(attr, struct class_attribute_string, attr);
112 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
113 }
114
115 /* Currently read-only only */
116 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
117 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
118 #define CLASS_ATTR_STRING(_name, _mode, _str) \
119 struct class_attribute_string class_attr_##_name = \
120 _CLASS_ATTR_STRING(_name, _mode, _str)
121
122 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
123 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
124 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
125 #define dev_printk(lvl, dev, fmt, ...) \
126 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
127
128 static inline void *
dev_get_drvdata(struct device * dev)129 dev_get_drvdata(struct device *dev)
130 {
131
132 return dev->driver_data;
133 }
134
135 static inline void
dev_set_drvdata(struct device * dev,void * data)136 dev_set_drvdata(struct device *dev, void *data)
137 {
138
139 dev->driver_data = data;
140 }
141
142 static inline struct device *
get_device(struct device * dev)143 get_device(struct device *dev)
144 {
145
146 if (dev)
147 kobject_get(&dev->kobj);
148
149 return (dev);
150 }
151
152 static inline char *
dev_name(const struct device * dev)153 dev_name(const struct device *dev)
154 {
155
156 return kobject_name(&dev->kobj);
157 }
158
159 #define dev_set_name(_dev, _fmt, ...) \
160 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
161
162 static inline void
put_device(struct device * dev)163 put_device(struct device *dev)
164 {
165
166 if (dev)
167 kobject_put(&dev->kobj);
168 }
169
170 static inline ssize_t
class_show(struct kobject * kobj,struct attribute * attr,char * buf)171 class_show(struct kobject *kobj, struct attribute *attr, char *buf)
172 {
173 struct class_attribute *dattr;
174 ssize_t error;
175
176 dattr = container_of(attr, struct class_attribute, attr);
177 error = -EIO;
178 if (dattr->show)
179 error = dattr->show(container_of(kobj, struct class, kobj),
180 dattr, buf);
181 return (error);
182 }
183
184 static inline ssize_t
class_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)185 class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
186 size_t count)
187 {
188 struct class_attribute *dattr;
189 ssize_t error;
190
191 dattr = container_of(attr, struct class_attribute, attr);
192 error = -EIO;
193 if (dattr->store)
194 error = dattr->store(container_of(kobj, struct class, kobj),
195 dattr, buf, count);
196 return (error);
197 }
198
199 static inline void
class_release(struct kobject * kobj)200 class_release(struct kobject *kobj)
201 {
202 struct class *class;
203
204 class = container_of(kobj, struct class, kobj);
205 if (class->class_release)
206 class->class_release(class);
207 }
208
209 static struct sysfs_ops class_sysfs = {
210 .show = class_show,
211 .store = class_store,
212 };
213 static struct kobj_type class_ktype = {
214 .release = class_release,
215 .sysfs_ops = &class_sysfs
216 };
217
218 static inline int
class_register(struct class * class)219 class_register(struct class *class)
220 {
221
222 class->bsdclass = devclass_create(class->name);
223 kobject_init(&class->kobj, &class_ktype);
224 kobject_set_name(&class->kobj, class->name);
225 kobject_add(&class->kobj, &class_root, class->name);
226
227 return (0);
228 }
229
230 static inline void
class_unregister(struct class * class)231 class_unregister(struct class *class)
232 {
233
234 kobject_put(&class->kobj);
235 }
236
237 static inline void
device_release(struct kobject * kobj)238 device_release(struct kobject *kobj)
239 {
240 struct device *dev;
241
242 dev = container_of(kobj, struct device, kobj);
243 /* This is the precedence defined by linux. */
244 if (dev->release)
245 dev->release(dev);
246 else if (dev->class && dev->class->dev_release)
247 dev->class->dev_release(dev);
248 }
249
250 static inline ssize_t
dev_show(struct kobject * kobj,struct attribute * attr,char * buf)251 dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
252 {
253 struct device_attribute *dattr;
254 ssize_t error;
255
256 dattr = container_of(attr, struct device_attribute, attr);
257 error = -EIO;
258 if (dattr->show)
259 error = dattr->show(container_of(kobj, struct device, kobj),
260 dattr, buf);
261 return (error);
262 }
263
264 static inline ssize_t
dev_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)265 dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
266 size_t count)
267 {
268 struct device_attribute *dattr;
269 ssize_t error;
270
271 dattr = container_of(attr, struct device_attribute, attr);
272 error = -EIO;
273 if (dattr->store)
274 error = dattr->store(container_of(kobj, struct device, kobj),
275 dattr, buf, count);
276 return (error);
277 }
278
279 static struct sysfs_ops dev_sysfs = { .show = dev_show, .store = dev_store, };
280 static struct kobj_type dev_ktype = {
281 .release = device_release,
282 .sysfs_ops = &dev_sysfs
283 };
284
285 /*
286 * Devices are registered and created for exporting to sysfs. create
287 * implies register and register assumes the device fields have been
288 * setup appropriately before being called.
289 */
290 static inline int
device_register(struct device * dev)291 device_register(struct device *dev)
292 {
293 device_t bsddev;
294 int unit;
295
296 bsddev = NULL;
297 if (dev->devt) {
298 unit = MINOR(dev->devt);
299 bsddev = devclass_get_device(dev->class->bsdclass, unit);
300 } else
301 unit = -1;
302 if (bsddev == NULL)
303 bsddev = device_add_child(dev->parent->bsddev,
304 dev->class->kobj.name, unit);
305 if (bsddev) {
306 if (dev->devt == 0)
307 dev->devt = makedev(0, device_get_unit(bsddev));
308 device_set_softc(bsddev, dev);
309 }
310 dev->bsddev = bsddev;
311 kobject_init(&dev->kobj, &dev_ktype);
312 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
313
314 return (0);
315 }
316
317 static inline void
device_unregister(struct device * dev)318 device_unregister(struct device *dev)
319 {
320 device_t bsddev;
321
322 bsddev = dev->bsddev;
323 mtx_lock(&Giant);
324 if (bsddev)
325 device_delete_child(device_get_parent(bsddev), bsddev);
326 mtx_unlock(&Giant);
327 put_device(dev);
328 }
329
330 struct device *device_create(struct class *class, struct device *parent,
331 dev_t devt, void *drvdata, const char *fmt, ...);
332
333 static inline void
device_destroy(struct class * class,dev_t devt)334 device_destroy(struct class *class, dev_t devt)
335 {
336 device_t bsddev;
337 int unit;
338
339 unit = MINOR(devt);
340 bsddev = devclass_get_device(class->bsdclass, unit);
341 if (bsddev)
342 device_unregister(device_get_softc(bsddev));
343 }
344
345 static inline void
class_kfree(struct class * class)346 class_kfree(struct class *class)
347 {
348
349 kfree(class);
350 }
351
352 static inline struct class *
class_create(struct module * owner,const char * name)353 class_create(struct module *owner, const char *name)
354 {
355 struct class *class;
356 int error;
357
358 class = kzalloc(sizeof(*class), M_WAITOK);
359 class->owner = owner;
360 class->name= name;
361 class->class_release = class_kfree;
362 error = class_register(class);
363 if (error) {
364 kfree(class);
365 return (NULL);
366 }
367
368 return (class);
369 }
370
371 static inline void
class_destroy(struct class * class)372 class_destroy(struct class *class)
373 {
374
375 if (class == NULL)
376 return;
377 class_unregister(class);
378 }
379
380 static inline int
device_create_file(struct device * dev,const struct device_attribute * attr)381 device_create_file(struct device *dev, const struct device_attribute *attr)
382 {
383
384 if (dev)
385 return sysfs_create_file(&dev->kobj, &attr->attr);
386 return -EINVAL;
387 }
388
389 static inline void
device_remove_file(struct device * dev,const struct device_attribute * attr)390 device_remove_file(struct device *dev, const struct device_attribute *attr)
391 {
392
393 if (dev)
394 sysfs_remove_file(&dev->kobj, &attr->attr);
395 }
396
397 static inline int
class_create_file(struct class * class,const struct class_attribute * attr)398 class_create_file(struct class *class, const struct class_attribute *attr)
399 {
400
401 if (class)
402 return sysfs_create_file(&class->kobj, &attr->attr);
403 return -EINVAL;
404 }
405
406 static inline void
class_remove_file(struct class * class,const struct class_attribute * attr)407 class_remove_file(struct class *class, const struct class_attribute *attr)
408 {
409
410 if (class)
411 sysfs_remove_file(&class->kobj, &attr->attr);
412 }
413
dev_to_node(struct device * dev)414 static inline int dev_to_node(struct device *dev)
415 {
416 return -1;
417 }
418
kvasprintf(gfp_t gfp,const char * fmt,va_list ap)419 static inline char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
420 {
421 unsigned int len;
422 char *p = NULL;
423 va_list aq;
424
425 va_copy(aq, ap);
426 len = vsnprintf(NULL, 0, fmt, aq);
427 va_end(aq);
428
429 vsnprintf(p, len+1, fmt, ap);
430
431 return p;
432 }
433
434 char *kasprintf(gfp_t, const char *, ...);
435
436 #endif /* _LINUX_DEVICE_H_ */
437