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-2016 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 unit = -1;
298
299 if (dev->devt) {
300 unit = MINOR(dev->devt);
301 bsddev = devclass_get_device(dev->class->bsdclass, unit);
302 } else if (dev->parent == NULL) {
303 bsddev = devclass_get_device(dev->class->bsdclass, 0);
304 }
305 if (bsddev == NULL && dev->parent != NULL) {
306 bsddev = device_add_child(dev->parent->bsddev,
307 dev->class->kobj.name, unit);
308 }
309 if (bsddev != NULL) {
310 if (dev->devt == 0)
311 dev->devt = makedev(0, device_get_unit(bsddev));
312 device_set_softc(bsddev, dev);
313 }
314 dev->bsddev = bsddev;
315 kobject_init(&dev->kobj, &dev_ktype);
316 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
317
318 return (0);
319 }
320
321 static inline void
device_unregister(struct device * dev)322 device_unregister(struct device *dev)
323 {
324 device_t bsddev;
325
326 bsddev = dev->bsddev;
327 mtx_lock(&Giant);
328 if (bsddev)
329 device_delete_child(device_get_parent(bsddev), bsddev);
330 mtx_unlock(&Giant);
331 put_device(dev);
332 }
333
334 struct device *device_create(struct class *class, struct device *parent,
335 dev_t devt, void *drvdata, const char *fmt, ...);
336
337 static inline void
device_destroy(struct class * class,dev_t devt)338 device_destroy(struct class *class, dev_t devt)
339 {
340 device_t bsddev;
341 int unit;
342
343 unit = MINOR(devt);
344 bsddev = devclass_get_device(class->bsdclass, unit);
345 if (bsddev)
346 device_unregister(device_get_softc(bsddev));
347 }
348
349 static inline void
class_kfree(struct class * class)350 class_kfree(struct class *class)
351 {
352
353 kfree(class);
354 }
355
356 static inline struct class *
class_create(struct module * owner,const char * name)357 class_create(struct module *owner, const char *name)
358 {
359 struct class *class;
360 int error;
361
362 class = kzalloc(sizeof(*class), M_WAITOK);
363 class->owner = owner;
364 class->name= name;
365 class->class_release = class_kfree;
366 error = class_register(class);
367 if (error) {
368 kfree(class);
369 return (NULL);
370 }
371
372 return (class);
373 }
374
375 static inline void
class_destroy(struct class * class)376 class_destroy(struct class *class)
377 {
378
379 if (class == NULL)
380 return;
381 class_unregister(class);
382 }
383
384 static inline int
device_create_file(struct device * dev,const struct device_attribute * attr)385 device_create_file(struct device *dev, const struct device_attribute *attr)
386 {
387
388 if (dev)
389 return sysfs_create_file(&dev->kobj, &attr->attr);
390 return -EINVAL;
391 }
392
393 static inline void
device_remove_file(struct device * dev,const struct device_attribute * attr)394 device_remove_file(struct device *dev, const struct device_attribute *attr)
395 {
396
397 if (dev)
398 sysfs_remove_file(&dev->kobj, &attr->attr);
399 }
400
401 static inline int
class_create_file(struct class * class,const struct class_attribute * attr)402 class_create_file(struct class *class, const struct class_attribute *attr)
403 {
404
405 if (class)
406 return sysfs_create_file(&class->kobj, &attr->attr);
407 return -EINVAL;
408 }
409
410 static inline void
class_remove_file(struct class * class,const struct class_attribute * attr)411 class_remove_file(struct class *class, const struct class_attribute *attr)
412 {
413
414 if (class)
415 sysfs_remove_file(&class->kobj, &attr->attr);
416 }
417
dev_to_node(struct device * dev)418 static inline int dev_to_node(struct device *dev)
419 {
420 return -1;
421 }
422
423 char *kvasprintf(gfp_t, const char *, va_list);
424 char *kasprintf(gfp_t, const char *, ...);
425
426 #endif /* _LINUX_DEVICE_H_ */
427