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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
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.
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.
31 #ifndef _LINUX_DEVICE_H_
32 #define _LINUX_DEVICE_H_
34 #include <linux/err.h>
35 #include <linux/types.h>
36 #include <linux/kobject.h>
37 #include <linux/sysfs.h>
38 #include <linux/list.h>
39 #include <linux/compiler.h>
40 #include <linux/types.h>
41 #include <linux/module.h>
42 #include <linux/workqueue.h>
43 #include <linux/sysfs.h>
44 #include <linux/kdev_t.h>
45 #include <asm/atomic.h>
49 enum irqreturn { IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
50 typedef enum irqreturn irqreturn_t;
59 void (*class_release)(struct class *class);
60 void (*dev_release)(struct device *dev);
61 char * (*devnode)(struct device *dev, umode_t *mode);
65 int (*suspend)(struct device *dev);
66 int (*suspend_late)(struct device *dev);
67 int (*resume)(struct device *dev);
68 int (*resume_early)(struct device *dev);
69 int (*freeze)(struct device *dev);
70 int (*freeze_late)(struct device *dev);
71 int (*thaw)(struct device *dev);
72 int (*thaw_early)(struct device *dev);
73 int (*poweroff)(struct device *dev);
74 int (*poweroff_late)(struct device *dev);
75 int (*restore)(struct device *dev);
76 int (*restore_early)(struct device *dev);
77 int (*runtime_suspend)(struct device *dev);
78 int (*runtime_resume)(struct device *dev);
79 int (*runtime_idle)(struct device *dev);
82 struct device_driver {
84 const struct dev_pm_ops *pm;
92 struct device *parent;
93 struct list_head irqents;
96 * The following flag is used to determine if the LinuxKPI is
97 * responsible for detaching the BSD device or not. If the
98 * LinuxKPI got the BSD device using devclass_get_device(), it
99 * must not try to detach or delete it, because it's already
100 * done somewhere else.
102 bool bsddev_attached_here;
103 struct device_driver *driver;
104 struct device_type *type;
107 void (*release)(struct device *dev);
112 #define LINUX_IRQ_INVALID 65535
114 unsigned int msix_max;
115 const struct attribute_group **groups;
118 extern struct device linux_root_device;
119 extern struct kobject linux_class_root;
120 extern const struct kobj_type linux_dev_ktype;
121 extern const struct kobj_type linux_class_ktype;
123 struct class_attribute {
124 struct attribute attr;
125 ssize_t (*show)(struct class *, struct class_attribute *, char *);
126 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
127 const void *(*namespace)(struct class *, const struct class_attribute *);
130 #define CLASS_ATTR(_name, _mode, _show, _store) \
131 struct class_attribute class_attr_##_name = \
132 { { #_name, NULL, _mode }, _show, _store }
134 struct device_attribute {
135 struct attribute attr;
136 ssize_t (*show)(struct device *,
137 struct device_attribute *, char *);
138 ssize_t (*store)(struct device *,
139 struct device_attribute *, const char *,
143 #define DEVICE_ATTR(_name, _mode, _show, _store) \
144 struct device_attribute dev_attr_##_name = \
145 __ATTR(_name, _mode, _show, _store)
146 #define DEVICE_ATTR_RO(_name) \
147 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
148 #define DEVICE_ATTR_WO(_name) \
149 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
150 #define DEVICE_ATTR_RW(_name) \
151 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
153 /* Simple class attribute that is just a static string */
154 struct class_attribute_string {
155 struct class_attribute attr;
159 static inline ssize_t
160 show_class_attr_string(struct class *class,
161 struct class_attribute *attr, char *buf)
163 struct class_attribute_string *cs;
164 cs = container_of(attr, struct class_attribute_string, attr);
165 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
168 /* Currently read-only only */
169 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
170 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
171 #define CLASS_ATTR_STRING(_name, _mode, _str) \
172 struct class_attribute_string class_attr_##_name = \
173 _CLASS_ATTR_STRING(_name, _mode, _str)
175 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
176 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
177 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
178 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
179 #define dev_printk(lvl, dev, fmt, ...) \
180 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
182 #define dev_err_ratelimited(dev, ...) do { \
183 static linux_ratelimit_t __ratelimited; \
184 if (linux_ratelimited(&__ratelimited)) \
185 dev_err(dev, __VA_ARGS__); \
188 #define dev_warn_ratelimited(dev, ...) do { \
189 static linux_ratelimit_t __ratelimited; \
190 if (linux_ratelimited(&__ratelimited)) \
191 dev_warn(dev, __VA_ARGS__); \
195 dev_get_drvdata(const struct device *dev)
198 return dev->driver_data;
202 dev_set_drvdata(struct device *dev, void *data)
205 dev->driver_data = data;
208 static inline struct device *
209 get_device(struct device *dev)
213 kobject_get(&dev->kobj);
219 dev_name(const struct device *dev)
222 return kobject_name(&dev->kobj);
225 #define dev_set_name(_dev, _fmt, ...) \
226 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
229 put_device(struct device *dev)
233 kobject_put(&dev->kobj);
237 class_register(struct class *class)
240 class->bsdclass = devclass_create(class->name);
241 kobject_init(&class->kobj, &linux_class_ktype);
242 kobject_set_name(&class->kobj, class->name);
243 kobject_add(&class->kobj, &linux_class_root, class->name);
249 class_unregister(struct class *class)
252 kobject_put(&class->kobj);
255 static inline struct device *kobj_to_dev(struct kobject *kobj)
257 return container_of(kobj, struct device, kobj);
261 * Devices are registered and created for exporting to sysfs. Create
262 * implies register and register assumes the device fields have been
263 * setup appropriately before being called.
266 device_initialize(struct device *dev)
268 device_t bsddev = NULL;
272 unit = MINOR(dev->devt);
273 bsddev = devclass_get_device(dev->class->bsdclass, unit);
274 dev->bsddev_attached_here = false;
275 } else if (dev->parent == NULL) {
276 bsddev = devclass_get_device(dev->class->bsdclass, 0);
277 dev->bsddev_attached_here = false;
279 dev->bsddev_attached_here = true;
282 if (bsddev == NULL && dev->parent != NULL) {
283 bsddev = device_add_child(dev->parent->bsddev,
284 dev->class->kobj.name, unit);
288 device_set_softc(bsddev, dev);
290 dev->bsddev = bsddev;
291 MPASS(dev->bsddev != NULL);
292 kobject_init(&dev->kobj, &linux_dev_ktype);
296 device_add(struct device *dev)
298 if (dev->bsddev != NULL) {
300 dev->devt = makedev(0, device_get_unit(dev->bsddev));
302 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
307 device_create_release(struct device *dev)
312 static inline struct device *
313 device_create_groups_vargs(struct class *class, struct device *parent,
314 dev_t devt, void *drvdata, const struct attribute_group **groups,
315 const char *fmt, va_list args)
317 struct device *dev = NULL;
318 int retval = -ENODEV;
320 if (class == NULL || IS_ERR(class))
323 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
331 dev->parent = parent;
332 dev->groups = groups;
333 dev->release = device_create_release;
334 /* device_initialize() needs the class and parent to be set */
335 device_initialize(dev);
336 dev_set_drvdata(dev, drvdata);
338 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
342 retval = device_add(dev);
350 return ERR_PTR(retval);
353 static inline struct device *
354 device_create_with_groups(struct class *class,
355 struct device *parent, dev_t devt, void *drvdata,
356 const struct attribute_group **groups, const char *fmt, ...)
361 va_start(vargs, fmt);
362 dev = device_create_groups_vargs(class, parent, devt, drvdata,
369 device_is_registered(struct device *dev)
372 return (dev->bsddev != NULL);
376 device_register(struct device *dev)
378 device_t bsddev = NULL;
381 if (device_is_registered(dev))
385 unit = MINOR(dev->devt);
386 bsddev = devclass_get_device(dev->class->bsdclass, unit);
387 dev->bsddev_attached_here = false;
388 } else if (dev->parent == NULL) {
389 bsddev = devclass_get_device(dev->class->bsdclass, 0);
390 dev->bsddev_attached_here = false;
392 dev->bsddev_attached_here = true;
394 if (bsddev == NULL && dev->parent != NULL) {
395 bsddev = device_add_child(dev->parent->bsddev,
396 dev->class->kobj.name, unit);
398 if (bsddev != NULL) {
400 dev->devt = makedev(0, device_get_unit(bsddev));
401 device_set_softc(bsddev, dev);
403 dev->bsddev = bsddev;
405 kobject_init(&dev->kobj, &linux_dev_ktype);
406 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
412 device_unregister(struct device *dev)
416 bsddev = dev->bsddev;
419 if (bsddev != NULL && dev->bsddev_attached_here) {
421 device_delete_child(device_get_parent(bsddev), bsddev);
428 device_del(struct device *dev)
432 bsddev = dev->bsddev;
435 if (bsddev != NULL && dev->bsddev_attached_here) {
437 device_delete_child(device_get_parent(bsddev), bsddev);
442 struct device *device_create(struct class *class, struct device *parent,
443 dev_t devt, void *drvdata, const char *fmt, ...);
446 device_destroy(struct class *class, dev_t devt)
452 bsddev = devclass_get_device(class->bsdclass, unit);
454 device_unregister(device_get_softc(bsddev));
458 linux_class_kfree(struct class *class)
464 static inline struct class *
465 class_create(struct module *owner, const char *name)
470 class = kzalloc(sizeof(*class), M_WAITOK);
471 class->owner = owner;
473 class->class_release = linux_class_kfree;
474 error = class_register(class);
484 class_destroy(struct class *class)
489 class_unregister(class);
493 device_create_file(struct device *dev, const struct device_attribute *attr)
497 return sysfs_create_file(&dev->kobj, &attr->attr);
502 device_remove_file(struct device *dev, const struct device_attribute *attr)
506 sysfs_remove_file(&dev->kobj, &attr->attr);
510 class_create_file(struct class *class, const struct class_attribute *attr)
514 return sysfs_create_file(&class->kobj, &attr->attr);
519 class_remove_file(struct class *class, const struct class_attribute *attr)
523 sysfs_remove_file(&class->kobj, &attr->attr);
527 dev_to_node(struct device *dev)
532 char *kvasprintf(gfp_t, const char *, va_list);
533 char *kasprintf(gfp_t, const char *, ...);
535 #endif /* _LINUX_DEVICE_H_ */