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/kdev_t.h>
44 #include <linux/backlight.h>
45 #include <asm/atomic.h>
48 #include <sys/backlight.h>
58 const struct dev_pm_ops *pm;
59 const struct attribute_group **dev_groups;
60 void (*class_release)(struct class *class);
61 void (*dev_release)(struct device *dev);
62 char * (*devnode)(struct device *dev, umode_t *mode);
66 int (*prepare)(struct device *dev);
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
115 unsigned int irq_start;
116 unsigned int irq_end;
117 const struct attribute_group **groups;
118 struct fwnode_handle *fwnode;
119 struct cdev *backlight_dev;
120 struct backlight_device *bd;
122 spinlock_t devres_lock;
123 struct list_head devres_head;
126 extern struct device linux_root_device;
127 extern struct kobject linux_class_root;
128 extern const struct kobj_type linux_dev_ktype;
129 extern const struct kobj_type linux_class_ktype;
131 struct class_attribute {
132 struct attribute attr;
133 ssize_t (*show)(struct class *, struct class_attribute *, char *);
134 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
135 const void *(*namespace)(struct class *, const struct class_attribute *);
138 #define CLASS_ATTR(_name, _mode, _show, _store) \
139 struct class_attribute class_attr_##_name = \
140 { { #_name, NULL, _mode }, _show, _store }
142 struct device_attribute {
143 struct attribute attr;
144 ssize_t (*show)(struct device *,
145 struct device_attribute *, char *);
146 ssize_t (*store)(struct device *,
147 struct device_attribute *, const char *,
151 #define DEVICE_ATTR(_name, _mode, _show, _store) \
152 struct device_attribute dev_attr_##_name = \
153 __ATTR(_name, _mode, _show, _store)
154 #define DEVICE_ATTR_RO(_name) \
155 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
156 #define DEVICE_ATTR_WO(_name) \
157 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
158 #define DEVICE_ATTR_RW(_name) \
159 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
161 /* Simple class attribute that is just a static string */
162 struct class_attribute_string {
163 struct class_attribute attr;
167 static inline ssize_t
168 show_class_attr_string(struct class *class,
169 struct class_attribute *attr, char *buf)
171 struct class_attribute_string *cs;
172 cs = container_of(attr, struct class_attribute_string, attr);
173 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
176 /* Currently read-only only */
177 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
178 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
179 #define CLASS_ATTR_STRING(_name, _mode, _str) \
180 struct class_attribute_string class_attr_##_name = \
181 _CLASS_ATTR_STRING(_name, _mode, _str)
183 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
184 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
185 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
186 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
187 #define dev_dbg(dev, fmt, ...) do { } while (0)
188 #define dev_printk(lvl, dev, fmt, ...) \
189 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
191 #define dev_err_once(dev, ...) do { \
192 static bool __dev_err_once; \
193 if (!__dev_err_once) { \
194 __dev_err_once = 1; \
195 dev_err(dev, __VA_ARGS__); \
199 #define dev_err_ratelimited(dev, ...) do { \
200 static linux_ratelimit_t __ratelimited; \
201 if (linux_ratelimited(&__ratelimited)) \
202 dev_err(dev, __VA_ARGS__); \
205 #define dev_warn_ratelimited(dev, ...) do { \
206 static linux_ratelimit_t __ratelimited; \
207 if (linux_ratelimited(&__ratelimited)) \
208 dev_warn(dev, __VA_ARGS__); \
212 dev_get_drvdata(const struct device *dev)
215 return dev->driver_data;
219 dev_set_drvdata(struct device *dev, void *data)
222 dev->driver_data = data;
225 static inline struct device *
226 get_device(struct device *dev)
230 kobject_get(&dev->kobj);
236 dev_name(const struct device *dev)
239 return kobject_name(&dev->kobj);
242 #define dev_set_name(_dev, _fmt, ...) \
243 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
246 put_device(struct device *dev)
250 kobject_put(&dev->kobj);
254 class_register(struct class *class)
257 class->bsdclass = devclass_create(class->name);
258 kobject_init(&class->kobj, &linux_class_ktype);
259 kobject_set_name(&class->kobj, class->name);
260 kobject_add(&class->kobj, &linux_class_root, class->name);
266 class_unregister(struct class *class)
269 kobject_put(&class->kobj);
272 static inline struct device *kobj_to_dev(struct kobject *kobj)
274 return container_of(kobj, struct device, kobj);
278 * Devices are registered and created for exporting to sysfs. Create
279 * implies register and register assumes the device fields have been
280 * setup appropriately before being called.
283 device_initialize(struct device *dev)
285 device_t bsddev = NULL;
289 unit = MINOR(dev->devt);
290 bsddev = devclass_get_device(dev->class->bsdclass, unit);
291 dev->bsddev_attached_here = false;
292 } else if (dev->parent == NULL) {
293 bsddev = devclass_get_device(dev->class->bsdclass, 0);
294 dev->bsddev_attached_here = false;
296 dev->bsddev_attached_here = true;
299 if (bsddev == NULL && dev->parent != NULL) {
300 bsddev = device_add_child(dev->parent->bsddev,
301 dev->class->kobj.name, unit);
305 device_set_softc(bsddev, dev);
307 dev->bsddev = bsddev;
308 MPASS(dev->bsddev != NULL);
309 kobject_init(&dev->kobj, &linux_dev_ktype);
311 spin_lock_init(&dev->devres_lock);
312 INIT_LIST_HEAD(&dev->devres_head);
316 device_add(struct device *dev)
318 if (dev->bsddev != NULL) {
320 dev->devt = makedev(0, device_get_unit(dev->bsddev));
322 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
325 return (sysfs_create_groups(&dev->kobj, dev->groups));
331 device_create_release(struct device *dev)
336 static inline struct device *
337 device_create_groups_vargs(struct class *class, struct device *parent,
338 dev_t devt, void *drvdata, const struct attribute_group **groups,
339 const char *fmt, va_list args)
341 struct device *dev = NULL;
342 int retval = -ENODEV;
344 if (class == NULL || IS_ERR(class))
347 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
355 dev->parent = parent;
356 dev->groups = groups;
357 dev->release = device_create_release;
358 /* device_initialize() needs the class and parent to be set */
359 device_initialize(dev);
360 dev_set_drvdata(dev, drvdata);
362 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
366 retval = device_add(dev);
374 return ERR_PTR(retval);
377 static inline struct device *
378 device_create_with_groups(struct class *class,
379 struct device *parent, dev_t devt, void *drvdata,
380 const struct attribute_group **groups, const char *fmt, ...)
385 va_start(vargs, fmt);
386 dev = device_create_groups_vargs(class, parent, devt, drvdata,
393 device_is_registered(struct device *dev)
396 return (dev->bsddev != NULL);
400 device_register(struct device *dev)
402 device_t bsddev = NULL;
405 if (device_is_registered(dev))
409 unit = MINOR(dev->devt);
410 bsddev = devclass_get_device(dev->class->bsdclass, unit);
411 dev->bsddev_attached_here = false;
412 } else if (dev->parent == NULL) {
413 bsddev = devclass_get_device(dev->class->bsdclass, 0);
414 dev->bsddev_attached_here = false;
416 dev->bsddev_attached_here = true;
418 if (bsddev == NULL && dev->parent != NULL) {
419 bsddev = device_add_child(dev->parent->bsddev,
420 dev->class->kobj.name, unit);
422 if (bsddev != NULL) {
424 dev->devt = makedev(0, device_get_unit(bsddev));
425 device_set_softc(bsddev, dev);
427 dev->bsddev = bsddev;
429 kobject_init(&dev->kobj, &linux_dev_ktype);
430 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
432 sysfs_create_groups(&dev->kobj, dev->class->dev_groups);
438 device_unregister(struct device *dev)
442 sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);
444 bsddev = dev->bsddev;
447 if (bsddev != NULL && dev->bsddev_attached_here) {
449 device_delete_child(device_get_parent(bsddev), bsddev);
456 device_del(struct device *dev)
460 bsddev = dev->bsddev;
463 if (bsddev != NULL && dev->bsddev_attached_here) {
465 device_delete_child(device_get_parent(bsddev), bsddev);
470 struct device *device_create(struct class *class, struct device *parent,
471 dev_t devt, void *drvdata, const char *fmt, ...);
474 device_destroy(struct class *class, dev_t devt)
480 bsddev = devclass_get_device(class->bsdclass, unit);
482 device_unregister(device_get_softc(bsddev));
485 #define dev_pm_set_driver_flags(dev, flags) do { \
489 linux_class_kfree(struct class *class)
495 static inline struct class *
496 class_create(struct module *owner, const char *name)
501 class = kzalloc(sizeof(*class), M_WAITOK);
502 class->owner = owner;
504 class->class_release = linux_class_kfree;
505 error = class_register(class);
515 class_destroy(struct class *class)
520 class_unregister(class);
524 device_create_file(struct device *dev, const struct device_attribute *attr)
528 return sysfs_create_file(&dev->kobj, &attr->attr);
533 device_remove_file(struct device *dev, const struct device_attribute *attr)
537 sysfs_remove_file(&dev->kobj, &attr->attr);
541 class_create_file(struct class *class, const struct class_attribute *attr)
545 return sysfs_create_file(&class->kobj, &attr->attr);
550 class_remove_file(struct class *class, const struct class_attribute *attr)
554 sysfs_remove_file(&class->kobj, &attr->attr);
558 dev_to_node(struct device *dev)
563 char *kvasprintf(gfp_t, const char *, va_list);
564 char *kasprintf(gfp_t, const char *, ...);
566 #endif /* _LINUX_DEVICE_H_ */