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;
60 const struct dev_pm_ops *pm;
61 void (*class_release)(struct class *class);
62 void (*dev_release)(struct device *dev);
63 char * (*devnode)(struct device *dev, umode_t *mode);
67 int (*suspend)(struct device *dev);
68 int (*suspend_late)(struct device *dev);
69 int (*resume)(struct device *dev);
70 int (*resume_early)(struct device *dev);
71 int (*freeze)(struct device *dev);
72 int (*freeze_late)(struct device *dev);
73 int (*thaw)(struct device *dev);
74 int (*thaw_early)(struct device *dev);
75 int (*poweroff)(struct device *dev);
76 int (*poweroff_late)(struct device *dev);
77 int (*restore)(struct device *dev);
78 int (*restore_early)(struct device *dev);
79 int (*runtime_suspend)(struct device *dev);
80 int (*runtime_resume)(struct device *dev);
81 int (*runtime_idle)(struct device *dev);
84 struct device_driver {
86 const struct dev_pm_ops *pm;
94 struct device *parent;
95 struct list_head irqents;
98 * The following flag is used to determine if the LinuxKPI is
99 * responsible for detaching the BSD device or not. If the
100 * LinuxKPI got the BSD device using devclass_get_device(), it
101 * must not try to detach or delete it, because it's already
102 * done somewhere else.
104 bool bsddev_attached_here;
105 struct device_driver *driver;
106 struct device_type *type;
109 void (*release)(struct device *dev);
114 #define LINUX_IRQ_INVALID 65535
116 unsigned int msix_max;
117 const struct attribute_group **groups;
118 struct fwnode_handle *fwnode;
120 spinlock_t devres_lock;
121 struct list_head devres_head;
124 extern struct device linux_root_device;
125 extern struct kobject linux_class_root;
126 extern const struct kobj_type linux_dev_ktype;
127 extern const struct kobj_type linux_class_ktype;
129 struct class_attribute {
130 struct attribute attr;
131 ssize_t (*show)(struct class *, struct class_attribute *, char *);
132 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
133 const void *(*namespace)(struct class *, const struct class_attribute *);
136 #define CLASS_ATTR(_name, _mode, _show, _store) \
137 struct class_attribute class_attr_##_name = \
138 { { #_name, NULL, _mode }, _show, _store }
140 struct device_attribute {
141 struct attribute attr;
142 ssize_t (*show)(struct device *,
143 struct device_attribute *, char *);
144 ssize_t (*store)(struct device *,
145 struct device_attribute *, const char *,
149 #define DEVICE_ATTR(_name, _mode, _show, _store) \
150 struct device_attribute dev_attr_##_name = \
151 __ATTR(_name, _mode, _show, _store)
152 #define DEVICE_ATTR_RO(_name) \
153 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
154 #define DEVICE_ATTR_WO(_name) \
155 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
156 #define DEVICE_ATTR_RW(_name) \
157 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
159 /* Simple class attribute that is just a static string */
160 struct class_attribute_string {
161 struct class_attribute attr;
165 static inline ssize_t
166 show_class_attr_string(struct class *class,
167 struct class_attribute *attr, char *buf)
169 struct class_attribute_string *cs;
170 cs = container_of(attr, struct class_attribute_string, attr);
171 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
174 /* Currently read-only only */
175 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
176 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
177 #define CLASS_ATTR_STRING(_name, _mode, _str) \
178 struct class_attribute_string class_attr_##_name = \
179 _CLASS_ATTR_STRING(_name, _mode, _str)
181 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
182 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
183 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
184 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
185 #define dev_dbg(dev, fmt, ...) do { } while (0)
186 #define dev_printk(lvl, dev, fmt, ...) \
187 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
189 #define dev_err_ratelimited(dev, ...) do { \
190 static linux_ratelimit_t __ratelimited; \
191 if (linux_ratelimited(&__ratelimited)) \
192 dev_err(dev, __VA_ARGS__); \
195 #define dev_warn_ratelimited(dev, ...) do { \
196 static linux_ratelimit_t __ratelimited; \
197 if (linux_ratelimited(&__ratelimited)) \
198 dev_warn(dev, __VA_ARGS__); \
202 dev_get_drvdata(const struct device *dev)
205 return dev->driver_data;
209 dev_set_drvdata(struct device *dev, void *data)
212 dev->driver_data = data;
215 static inline struct device *
216 get_device(struct device *dev)
220 kobject_get(&dev->kobj);
226 dev_name(const struct device *dev)
229 return kobject_name(&dev->kobj);
232 #define dev_set_name(_dev, _fmt, ...) \
233 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
236 put_device(struct device *dev)
240 kobject_put(&dev->kobj);
244 class_register(struct class *class)
247 class->bsdclass = devclass_create(class->name);
248 kobject_init(&class->kobj, &linux_class_ktype);
249 kobject_set_name(&class->kobj, class->name);
250 kobject_add(&class->kobj, &linux_class_root, class->name);
256 class_unregister(struct class *class)
259 kobject_put(&class->kobj);
262 static inline struct device *kobj_to_dev(struct kobject *kobj)
264 return container_of(kobj, struct device, kobj);
268 * Devices are registered and created for exporting to sysfs. Create
269 * implies register and register assumes the device fields have been
270 * setup appropriately before being called.
273 device_initialize(struct device *dev)
275 device_t bsddev = NULL;
279 unit = MINOR(dev->devt);
280 bsddev = devclass_get_device(dev->class->bsdclass, unit);
281 dev->bsddev_attached_here = false;
282 } else if (dev->parent == NULL) {
283 bsddev = devclass_get_device(dev->class->bsdclass, 0);
284 dev->bsddev_attached_here = false;
286 dev->bsddev_attached_here = true;
289 if (bsddev == NULL && dev->parent != NULL) {
290 bsddev = device_add_child(dev->parent->bsddev,
291 dev->class->kobj.name, unit);
295 device_set_softc(bsddev, dev);
297 dev->bsddev = bsddev;
298 MPASS(dev->bsddev != NULL);
299 kobject_init(&dev->kobj, &linux_dev_ktype);
301 spin_lock_init(&dev->devres_lock);
302 INIT_LIST_HEAD(&dev->devres_head);
306 device_add(struct device *dev)
308 if (dev->bsddev != NULL) {
310 dev->devt = makedev(0, device_get_unit(dev->bsddev));
312 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
317 device_create_release(struct device *dev)
322 static inline struct device *
323 device_create_groups_vargs(struct class *class, struct device *parent,
324 dev_t devt, void *drvdata, const struct attribute_group **groups,
325 const char *fmt, va_list args)
327 struct device *dev = NULL;
328 int retval = -ENODEV;
330 if (class == NULL || IS_ERR(class))
333 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
341 dev->parent = parent;
342 dev->groups = groups;
343 dev->release = device_create_release;
344 /* device_initialize() needs the class and parent to be set */
345 device_initialize(dev);
346 dev_set_drvdata(dev, drvdata);
348 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
352 retval = device_add(dev);
360 return ERR_PTR(retval);
363 static inline struct device *
364 device_create_with_groups(struct class *class,
365 struct device *parent, dev_t devt, void *drvdata,
366 const struct attribute_group **groups, const char *fmt, ...)
371 va_start(vargs, fmt);
372 dev = device_create_groups_vargs(class, parent, devt, drvdata,
379 device_is_registered(struct device *dev)
382 return (dev->bsddev != NULL);
386 device_register(struct device *dev)
388 device_t bsddev = NULL;
391 if (device_is_registered(dev))
395 unit = MINOR(dev->devt);
396 bsddev = devclass_get_device(dev->class->bsdclass, unit);
397 dev->bsddev_attached_here = false;
398 } else if (dev->parent == NULL) {
399 bsddev = devclass_get_device(dev->class->bsdclass, 0);
400 dev->bsddev_attached_here = false;
402 dev->bsddev_attached_here = true;
404 if (bsddev == NULL && dev->parent != NULL) {
405 bsddev = device_add_child(dev->parent->bsddev,
406 dev->class->kobj.name, unit);
408 if (bsddev != NULL) {
410 dev->devt = makedev(0, device_get_unit(bsddev));
411 device_set_softc(bsddev, dev);
413 dev->bsddev = bsddev;
415 kobject_init(&dev->kobj, &linux_dev_ktype);
416 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
422 device_unregister(struct device *dev)
426 bsddev = dev->bsddev;
429 if (bsddev != NULL && dev->bsddev_attached_here) {
431 device_delete_child(device_get_parent(bsddev), bsddev);
438 device_del(struct device *dev)
442 bsddev = dev->bsddev;
445 if (bsddev != NULL && dev->bsddev_attached_here) {
447 device_delete_child(device_get_parent(bsddev), bsddev);
452 struct device *device_create(struct class *class, struct device *parent,
453 dev_t devt, void *drvdata, const char *fmt, ...);
456 device_destroy(struct class *class, dev_t devt)
462 bsddev = devclass_get_device(class->bsdclass, unit);
464 device_unregister(device_get_softc(bsddev));
468 linux_class_kfree(struct class *class)
474 static inline struct class *
475 class_create(struct module *owner, const char *name)
480 class = kzalloc(sizeof(*class), M_WAITOK);
481 class->owner = owner;
483 class->class_release = linux_class_kfree;
484 error = class_register(class);
494 class_destroy(struct class *class)
499 class_unregister(class);
503 device_create_file(struct device *dev, const struct device_attribute *attr)
507 return sysfs_create_file(&dev->kobj, &attr->attr);
512 device_remove_file(struct device *dev, const struct device_attribute *attr)
516 sysfs_remove_file(&dev->kobj, &attr->attr);
520 class_create_file(struct class *class, const struct class_attribute *attr)
524 return sysfs_create_file(&class->kobj, &attr->attr);
529 class_remove_file(struct class *class, const struct class_attribute *attr)
533 sysfs_remove_file(&class->kobj, &attr->attr);
537 dev_to_node(struct device *dev)
542 char *kvasprintf(gfp_t, const char *, va_list);
543 char *kasprintf(gfp_t, const char *, ...);
545 #endif /* _LINUX_DEVICE_H_ */