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>
57 const struct dev_pm_ops *pm;
58 void (*class_release)(struct class *class);
59 void (*dev_release)(struct device *dev);
60 char * (*devnode)(struct device *dev, umode_t *mode);
64 int (*suspend)(struct device *dev);
65 int (*suspend_late)(struct device *dev);
66 int (*resume)(struct device *dev);
67 int (*resume_early)(struct device *dev);
68 int (*freeze)(struct device *dev);
69 int (*freeze_late)(struct device *dev);
70 int (*thaw)(struct device *dev);
71 int (*thaw_early)(struct device *dev);
72 int (*poweroff)(struct device *dev);
73 int (*poweroff_late)(struct device *dev);
74 int (*restore)(struct device *dev);
75 int (*restore_early)(struct device *dev);
76 int (*runtime_suspend)(struct device *dev);
77 int (*runtime_resume)(struct device *dev);
78 int (*runtime_idle)(struct device *dev);
81 struct device_driver {
83 const struct dev_pm_ops *pm;
91 struct device *parent;
92 struct list_head irqents;
95 * The following flag is used to determine if the LinuxKPI is
96 * responsible for detaching the BSD device or not. If the
97 * LinuxKPI got the BSD device using devclass_get_device(), it
98 * must not try to detach or delete it, because it's already
99 * done somewhere else.
101 bool bsddev_attached_here;
102 struct device_driver *driver;
103 struct device_type *type;
106 void (*release)(struct device *dev);
111 #define LINUX_IRQ_INVALID 65535
113 unsigned int msix_max;
114 const struct attribute_group **groups;
115 struct fwnode_handle *fwnode;
117 spinlock_t devres_lock;
118 struct list_head devres_head;
121 extern struct device linux_root_device;
122 extern struct kobject linux_class_root;
123 extern const struct kobj_type linux_dev_ktype;
124 extern const struct kobj_type linux_class_ktype;
126 struct class_attribute {
127 struct attribute attr;
128 ssize_t (*show)(struct class *, struct class_attribute *, char *);
129 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
130 const void *(*namespace)(struct class *, const struct class_attribute *);
133 #define CLASS_ATTR(_name, _mode, _show, _store) \
134 struct class_attribute class_attr_##_name = \
135 { { #_name, NULL, _mode }, _show, _store }
137 struct device_attribute {
138 struct attribute attr;
139 ssize_t (*show)(struct device *,
140 struct device_attribute *, char *);
141 ssize_t (*store)(struct device *,
142 struct device_attribute *, const char *,
146 #define DEVICE_ATTR(_name, _mode, _show, _store) \
147 struct device_attribute dev_attr_##_name = \
148 __ATTR(_name, _mode, _show, _store)
149 #define DEVICE_ATTR_RO(_name) \
150 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
151 #define DEVICE_ATTR_WO(_name) \
152 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
153 #define DEVICE_ATTR_RW(_name) \
154 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
156 /* Simple class attribute that is just a static string */
157 struct class_attribute_string {
158 struct class_attribute attr;
162 static inline ssize_t
163 show_class_attr_string(struct class *class,
164 struct class_attribute *attr, char *buf)
166 struct class_attribute_string *cs;
167 cs = container_of(attr, struct class_attribute_string, attr);
168 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
171 /* Currently read-only only */
172 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
173 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
174 #define CLASS_ATTR_STRING(_name, _mode, _str) \
175 struct class_attribute_string class_attr_##_name = \
176 _CLASS_ATTR_STRING(_name, _mode, _str)
178 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
179 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
180 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
181 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
182 #define dev_dbg(dev, fmt, ...) do { } while (0)
183 #define dev_printk(lvl, dev, fmt, ...) \
184 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
186 #define dev_err_once(dev, ...) do { \
187 static bool __dev_err_once; \
188 if (!__dev_err_once) { \
189 __dev_err_once = 1; \
190 dev_err(dev, __VA_ARGS__); \
194 #define dev_err_ratelimited(dev, ...) do { \
195 static linux_ratelimit_t __ratelimited; \
196 if (linux_ratelimited(&__ratelimited)) \
197 dev_err(dev, __VA_ARGS__); \
200 #define dev_warn_ratelimited(dev, ...) do { \
201 static linux_ratelimit_t __ratelimited; \
202 if (linux_ratelimited(&__ratelimited)) \
203 dev_warn(dev, __VA_ARGS__); \
207 dev_get_drvdata(const struct device *dev)
210 return dev->driver_data;
214 dev_set_drvdata(struct device *dev, void *data)
217 dev->driver_data = data;
220 static inline struct device *
221 get_device(struct device *dev)
225 kobject_get(&dev->kobj);
231 dev_name(const struct device *dev)
234 return kobject_name(&dev->kobj);
237 #define dev_set_name(_dev, _fmt, ...) \
238 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
241 put_device(struct device *dev)
245 kobject_put(&dev->kobj);
249 class_register(struct class *class)
252 class->bsdclass = devclass_create(class->name);
253 kobject_init(&class->kobj, &linux_class_ktype);
254 kobject_set_name(&class->kobj, class->name);
255 kobject_add(&class->kobj, &linux_class_root, class->name);
261 class_unregister(struct class *class)
264 kobject_put(&class->kobj);
267 static inline struct device *kobj_to_dev(struct kobject *kobj)
269 return container_of(kobj, struct device, kobj);
273 * Devices are registered and created for exporting to sysfs. Create
274 * implies register and register assumes the device fields have been
275 * setup appropriately before being called.
278 device_initialize(struct device *dev)
280 device_t bsddev = NULL;
284 unit = MINOR(dev->devt);
285 bsddev = devclass_get_device(dev->class->bsdclass, unit);
286 dev->bsddev_attached_here = false;
287 } else if (dev->parent == NULL) {
288 bsddev = devclass_get_device(dev->class->bsdclass, 0);
289 dev->bsddev_attached_here = false;
291 dev->bsddev_attached_here = true;
294 if (bsddev == NULL && dev->parent != NULL) {
295 bsddev = device_add_child(dev->parent->bsddev,
296 dev->class->kobj.name, unit);
300 device_set_softc(bsddev, dev);
302 dev->bsddev = bsddev;
303 MPASS(dev->bsddev != NULL);
304 kobject_init(&dev->kobj, &linux_dev_ktype);
306 spin_lock_init(&dev->devres_lock);
307 INIT_LIST_HEAD(&dev->devres_head);
311 device_add(struct device *dev)
313 if (dev->bsddev != NULL) {
315 dev->devt = makedev(0, device_get_unit(dev->bsddev));
317 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
322 device_create_release(struct device *dev)
327 static inline struct device *
328 device_create_groups_vargs(struct class *class, struct device *parent,
329 dev_t devt, void *drvdata, const struct attribute_group **groups,
330 const char *fmt, va_list args)
332 struct device *dev = NULL;
333 int retval = -ENODEV;
335 if (class == NULL || IS_ERR(class))
338 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
346 dev->parent = parent;
347 dev->groups = groups;
348 dev->release = device_create_release;
349 /* device_initialize() needs the class and parent to be set */
350 device_initialize(dev);
351 dev_set_drvdata(dev, drvdata);
353 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
357 retval = device_add(dev);
365 return ERR_PTR(retval);
368 static inline struct device *
369 device_create_with_groups(struct class *class,
370 struct device *parent, dev_t devt, void *drvdata,
371 const struct attribute_group **groups, const char *fmt, ...)
376 va_start(vargs, fmt);
377 dev = device_create_groups_vargs(class, parent, devt, drvdata,
384 device_is_registered(struct device *dev)
387 return (dev->bsddev != NULL);
391 device_register(struct device *dev)
393 device_t bsddev = NULL;
396 if (device_is_registered(dev))
400 unit = MINOR(dev->devt);
401 bsddev = devclass_get_device(dev->class->bsdclass, unit);
402 dev->bsddev_attached_here = false;
403 } else if (dev->parent == NULL) {
404 bsddev = devclass_get_device(dev->class->bsdclass, 0);
405 dev->bsddev_attached_here = false;
407 dev->bsddev_attached_here = true;
409 if (bsddev == NULL && dev->parent != NULL) {
410 bsddev = device_add_child(dev->parent->bsddev,
411 dev->class->kobj.name, unit);
413 if (bsddev != NULL) {
415 dev->devt = makedev(0, device_get_unit(bsddev));
416 device_set_softc(bsddev, dev);
418 dev->bsddev = bsddev;
420 kobject_init(&dev->kobj, &linux_dev_ktype);
421 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
427 device_unregister(struct device *dev)
431 bsddev = dev->bsddev;
434 if (bsddev != NULL && dev->bsddev_attached_here) {
436 device_delete_child(device_get_parent(bsddev), bsddev);
443 device_del(struct device *dev)
447 bsddev = dev->bsddev;
450 if (bsddev != NULL && dev->bsddev_attached_here) {
452 device_delete_child(device_get_parent(bsddev), bsddev);
457 struct device *device_create(struct class *class, struct device *parent,
458 dev_t devt, void *drvdata, const char *fmt, ...);
461 device_destroy(struct class *class, dev_t devt)
467 bsddev = devclass_get_device(class->bsdclass, unit);
469 device_unregister(device_get_softc(bsddev));
472 #define dev_pm_set_driver_flags(dev, flags) do { \
476 linux_class_kfree(struct class *class)
482 static inline struct class *
483 class_create(struct module *owner, const char *name)
488 class = kzalloc(sizeof(*class), M_WAITOK);
489 class->owner = owner;
491 class->class_release = linux_class_kfree;
492 error = class_register(class);
502 class_destroy(struct class *class)
507 class_unregister(class);
511 device_create_file(struct device *dev, const struct device_attribute *attr)
515 return sysfs_create_file(&dev->kobj, &attr->attr);
520 device_remove_file(struct device *dev, const struct device_attribute *attr)
524 sysfs_remove_file(&dev->kobj, &attr->attr);
528 class_create_file(struct class *class, const struct class_attribute *attr)
532 return sysfs_create_file(&class->kobj, &attr->attr);
537 class_remove_file(struct class *class, const struct class_attribute *attr)
541 sysfs_remove_file(&class->kobj, &attr->attr);
545 dev_to_node(struct device *dev)
550 char *kvasprintf(gfp_t, const char *, va_list);
551 char *kasprintf(gfp_t, const char *, ...);
553 #endif /* _LINUX_DEVICE_H_ */