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 struct device *parent;
66 struct list_head irqents;
69 * The following flag is used to determine if the LinuxKPI is
70 * responsible for detaching the BSD device or not. If the
71 * LinuxKPI got the BSD device using devclass_get_device(), it
72 * must not try to detach or delete it, because it's already
73 * done somewhere else.
75 bool bsddev_attached_here;
78 void (*release)(struct device *dev);
83 #define LINUX_IRQ_INVALID 65535
85 unsigned int msix_max;
86 const struct attribute_group **groups;
89 extern struct device linux_root_device;
90 extern struct kobject linux_class_root;
91 extern const struct kobj_type linux_dev_ktype;
92 extern const struct kobj_type linux_class_ktype;
94 struct class_attribute {
95 struct attribute attr;
96 ssize_t (*show)(struct class *, struct class_attribute *, char *);
97 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
98 const void *(*namespace)(struct class *, const struct class_attribute *);
101 #define CLASS_ATTR(_name, _mode, _show, _store) \
102 struct class_attribute class_attr_##_name = \
103 { { #_name, NULL, _mode }, _show, _store }
105 struct device_attribute {
106 struct attribute attr;
107 ssize_t (*show)(struct device *,
108 struct device_attribute *, char *);
109 ssize_t (*store)(struct device *,
110 struct device_attribute *, const char *,
114 #define DEVICE_ATTR(_name, _mode, _show, _store) \
115 struct device_attribute dev_attr_##_name = \
116 { { #_name, NULL, _mode }, _show, _store }
118 /* Simple class attribute that is just a static string */
119 struct class_attribute_string {
120 struct class_attribute attr;
124 static inline ssize_t
125 show_class_attr_string(struct class *class,
126 struct class_attribute *attr, char *buf)
128 struct class_attribute_string *cs;
129 cs = container_of(attr, struct class_attribute_string, attr);
130 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
133 /* Currently read-only only */
134 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
135 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
136 #define CLASS_ATTR_STRING(_name, _mode, _str) \
137 struct class_attribute_string class_attr_##_name = \
138 _CLASS_ATTR_STRING(_name, _mode, _str)
140 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
141 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
142 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
143 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
144 #define dev_printk(lvl, dev, fmt, ...) \
145 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
147 #define dev_err_ratelimited(dev, ...) do { \
148 static linux_ratelimit_t __ratelimited; \
149 if (linux_ratelimited(&__ratelimited)) \
150 dev_err(dev, __VA_ARGS__); \
153 #define dev_warn_ratelimited(dev, ...) do { \
154 static linux_ratelimit_t __ratelimited; \
155 if (linux_ratelimited(&__ratelimited)) \
156 dev_warn(dev, __VA_ARGS__); \
160 dev_get_drvdata(const struct device *dev)
163 return dev->driver_data;
167 dev_set_drvdata(struct device *dev, void *data)
170 dev->driver_data = data;
173 static inline struct device *
174 get_device(struct device *dev)
178 kobject_get(&dev->kobj);
184 dev_name(const struct device *dev)
187 return kobject_name(&dev->kobj);
190 #define dev_set_name(_dev, _fmt, ...) \
191 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
194 put_device(struct device *dev)
198 kobject_put(&dev->kobj);
202 class_register(struct class *class)
205 class->bsdclass = devclass_create(class->name);
206 kobject_init(&class->kobj, &linux_class_ktype);
207 kobject_set_name(&class->kobj, class->name);
208 kobject_add(&class->kobj, &linux_class_root, class->name);
214 class_unregister(struct class *class)
217 kobject_put(&class->kobj);
220 static inline struct device *kobj_to_dev(struct kobject *kobj)
222 return container_of(kobj, struct device, kobj);
226 * Devices are registered and created for exporting to sysfs. Create
227 * implies register and register assumes the device fields have been
228 * setup appropriately before being called.
231 device_initialize(struct device *dev)
233 device_t bsddev = NULL;
237 unit = MINOR(dev->devt);
238 bsddev = devclass_get_device(dev->class->bsdclass, unit);
239 dev->bsddev_attached_here = false;
240 } else if (dev->parent == NULL) {
241 bsddev = devclass_get_device(dev->class->bsdclass, 0);
242 dev->bsddev_attached_here = false;
244 dev->bsddev_attached_here = true;
247 if (bsddev == NULL && dev->parent != NULL) {
248 bsddev = device_add_child(dev->parent->bsddev,
249 dev->class->kobj.name, unit);
253 device_set_softc(bsddev, dev);
255 dev->bsddev = bsddev;
256 MPASS(dev->bsddev != NULL);
257 kobject_init(&dev->kobj, &linux_dev_ktype);
261 device_add(struct device *dev)
263 if (dev->bsddev != NULL) {
265 dev->devt = makedev(0, device_get_unit(dev->bsddev));
267 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
272 device_create_release(struct device *dev)
277 static inline struct device *
278 device_create_groups_vargs(struct class *class, struct device *parent,
279 dev_t devt, void *drvdata, const struct attribute_group **groups,
280 const char *fmt, va_list args)
282 struct device *dev = NULL;
283 int retval = -ENODEV;
285 if (class == NULL || IS_ERR(class))
288 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
296 dev->parent = parent;
297 dev->groups = groups;
298 dev->release = device_create_release;
299 /* device_initialize() needs the class and parent to be set */
300 device_initialize(dev);
301 dev_set_drvdata(dev, drvdata);
303 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
307 retval = device_add(dev);
315 return ERR_PTR(retval);
318 static inline struct device *
319 device_create_with_groups(struct class *class,
320 struct device *parent, dev_t devt, void *drvdata,
321 const struct attribute_group **groups, const char *fmt, ...)
326 va_start(vargs, fmt);
327 dev = device_create_groups_vargs(class, parent, devt, drvdata,
334 device_register(struct device *dev)
336 device_t bsddev = NULL;
339 if (dev->bsddev != NULL)
343 unit = MINOR(dev->devt);
344 bsddev = devclass_get_device(dev->class->bsdclass, unit);
345 dev->bsddev_attached_here = false;
346 } else if (dev->parent == NULL) {
347 bsddev = devclass_get_device(dev->class->bsdclass, 0);
348 dev->bsddev_attached_here = false;
350 dev->bsddev_attached_here = true;
352 if (bsddev == NULL && dev->parent != NULL) {
353 bsddev = device_add_child(dev->parent->bsddev,
354 dev->class->kobj.name, unit);
356 if (bsddev != NULL) {
358 dev->devt = makedev(0, device_get_unit(bsddev));
359 device_set_softc(bsddev, dev);
361 dev->bsddev = bsddev;
363 kobject_init(&dev->kobj, &linux_dev_ktype);
364 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
370 device_unregister(struct device *dev)
374 bsddev = dev->bsddev;
377 if (bsddev != NULL && dev->bsddev_attached_here) {
379 device_delete_child(device_get_parent(bsddev), bsddev);
386 device_del(struct device *dev)
390 bsddev = dev->bsddev;
393 if (bsddev != NULL && dev->bsddev_attached_here) {
395 device_delete_child(device_get_parent(bsddev), bsddev);
400 struct device *device_create(struct class *class, struct device *parent,
401 dev_t devt, void *drvdata, const char *fmt, ...);
404 device_destroy(struct class *class, dev_t devt)
410 bsddev = devclass_get_device(class->bsdclass, unit);
412 device_unregister(device_get_softc(bsddev));
416 linux_class_kfree(struct class *class)
422 static inline struct class *
423 class_create(struct module *owner, const char *name)
428 class = kzalloc(sizeof(*class), M_WAITOK);
429 class->owner = owner;
431 class->class_release = linux_class_kfree;
432 error = class_register(class);
442 class_destroy(struct class *class)
447 class_unregister(class);
451 device_create_file(struct device *dev, const struct device_attribute *attr)
455 return sysfs_create_file(&dev->kobj, &attr->attr);
460 device_remove_file(struct device *dev, const struct device_attribute *attr)
464 sysfs_remove_file(&dev->kobj, &attr->attr);
468 class_create_file(struct class *class, const struct class_attribute *attr)
472 return sysfs_create_file(&class->kobj, &attr->attr);
477 class_remove_file(struct class *class, const struct class_attribute *attr)
481 sysfs_remove_file(&class->kobj, &attr->attr);
485 dev_to_node(struct device *dev)
490 char *kvasprintf(gfp_t, const char *, va_list);
491 char *kasprintf(gfp_t, const char *, ...);
493 #endif /* _LINUX_DEVICE_H_ */