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;
57 void (*class_release)(struct class *class);
58 void (*dev_release)(struct device *dev);
59 char * (*devnode)(struct device *dev, umode_t *mode);
63 struct device *parent;
64 struct list_head irqents;
67 * The following flag is used to determine if the LinuxKPI is
68 * responsible for detaching the BSD device or not. If the
69 * LinuxKPI got the BSD device using devclass_get_device(), it
70 * must not try to detach or delete it, because it's already
71 * done somewhere else.
73 bool bsddev_attached_here;
76 void (*release)(struct device *dev);
81 #define LINUX_IRQ_INVALID 65535
83 unsigned int msix_max;
84 const struct attribute_group **groups;
87 extern struct device linux_root_device;
88 extern struct kobject linux_class_root;
89 extern const struct kobj_type linux_dev_ktype;
90 extern const struct kobj_type linux_class_ktype;
92 struct class_attribute {
93 struct attribute attr;
94 ssize_t (*show)(struct class *, struct class_attribute *, char *);
95 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
96 const void *(*namespace)(struct class *, const struct class_attribute *);
99 #define CLASS_ATTR(_name, _mode, _show, _store) \
100 struct class_attribute class_attr_##_name = \
101 { { #_name, NULL, _mode }, _show, _store }
103 struct device_attribute {
104 struct attribute attr;
105 ssize_t (*show)(struct device *,
106 struct device_attribute *, char *);
107 ssize_t (*store)(struct device *,
108 struct device_attribute *, const char *,
112 #define DEVICE_ATTR(_name, _mode, _show, _store) \
113 struct device_attribute dev_attr_##_name = \
114 { { #_name, NULL, _mode }, _show, _store }
116 /* Simple class attribute that is just a static string */
117 struct class_attribute_string {
118 struct class_attribute attr;
122 static inline ssize_t
123 show_class_attr_string(struct class *class,
124 struct class_attribute *attr, char *buf)
126 struct class_attribute_string *cs;
127 cs = container_of(attr, struct class_attribute_string, attr);
128 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
131 /* Currently read-only only */
132 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
133 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
134 #define CLASS_ATTR_STRING(_name, _mode, _str) \
135 struct class_attribute_string class_attr_##_name = \
136 _CLASS_ATTR_STRING(_name, _mode, _str)
138 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
139 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
140 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
141 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
142 #define dev_printk(lvl, dev, fmt, ...) \
143 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
146 dev_get_drvdata(const struct device *dev)
149 return dev->driver_data;
153 dev_set_drvdata(struct device *dev, void *data)
156 dev->driver_data = data;
159 static inline struct device *
160 get_device(struct device *dev)
164 kobject_get(&dev->kobj);
170 dev_name(const struct device *dev)
173 return kobject_name(&dev->kobj);
176 #define dev_set_name(_dev, _fmt, ...) \
177 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
180 put_device(struct device *dev)
184 kobject_put(&dev->kobj);
188 class_register(struct class *class)
191 class->bsdclass = devclass_create(class->name);
192 kobject_init(&class->kobj, &linux_class_ktype);
193 kobject_set_name(&class->kobj, class->name);
194 kobject_add(&class->kobj, &linux_class_root, class->name);
200 class_unregister(struct class *class)
203 kobject_put(&class->kobj);
206 static inline struct device *kobj_to_dev(struct kobject *kobj)
208 return container_of(kobj, struct device, kobj);
212 * Devices are registered and created for exporting to sysfs. Create
213 * implies register and register assumes the device fields have been
214 * setup appropriately before being called.
217 device_initialize(struct device *dev)
219 device_t bsddev = NULL;
223 unit = MINOR(dev->devt);
224 bsddev = devclass_get_device(dev->class->bsdclass, unit);
225 dev->bsddev_attached_here = false;
226 } else if (dev->parent == NULL) {
227 bsddev = devclass_get_device(dev->class->bsdclass, 0);
228 dev->bsddev_attached_here = false;
230 dev->bsddev_attached_here = true;
233 if (bsddev == NULL && dev->parent != NULL) {
234 bsddev = device_add_child(dev->parent->bsddev,
235 dev->class->kobj.name, unit);
239 device_set_softc(bsddev, dev);
241 dev->bsddev = bsddev;
242 MPASS(dev->bsddev != NULL);
243 kobject_init(&dev->kobj, &linux_dev_ktype);
247 device_add(struct device *dev)
249 if (dev->bsddev != NULL) {
251 dev->devt = makedev(0, device_get_unit(dev->bsddev));
253 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
258 device_create_release(struct device *dev)
263 static inline struct device *
264 device_create_groups_vargs(struct class *class, struct device *parent,
265 dev_t devt, void *drvdata, const struct attribute_group **groups,
266 const char *fmt, va_list args)
268 struct device *dev = NULL;
269 int retval = -ENODEV;
271 if (class == NULL || IS_ERR(class))
274 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
282 dev->parent = parent;
283 dev->groups = groups;
284 dev->release = device_create_release;
285 /* device_initialize() needs the class and parent to be set */
286 device_initialize(dev);
287 dev_set_drvdata(dev, drvdata);
289 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
293 retval = device_add(dev);
301 return ERR_PTR(retval);
304 static inline struct device *
305 device_create_with_groups(struct class *class,
306 struct device *parent, dev_t devt, void *drvdata,
307 const struct attribute_group **groups, const char *fmt, ...)
312 va_start(vargs, fmt);
313 dev = device_create_groups_vargs(class, parent, devt, drvdata,
320 device_register(struct device *dev)
322 device_t bsddev = NULL;
325 if (dev->bsddev != NULL)
329 unit = MINOR(dev->devt);
330 bsddev = devclass_get_device(dev->class->bsdclass, unit);
331 dev->bsddev_attached_here = false;
332 } else if (dev->parent == NULL) {
333 bsddev = devclass_get_device(dev->class->bsdclass, 0);
334 dev->bsddev_attached_here = false;
336 dev->bsddev_attached_here = true;
338 if (bsddev == NULL && dev->parent != NULL) {
339 bsddev = device_add_child(dev->parent->bsddev,
340 dev->class->kobj.name, unit);
342 if (bsddev != NULL) {
344 dev->devt = makedev(0, device_get_unit(bsddev));
345 device_set_softc(bsddev, dev);
347 dev->bsddev = bsddev;
349 kobject_init(&dev->kobj, &linux_dev_ktype);
350 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
356 device_unregister(struct device *dev)
360 bsddev = dev->bsddev;
363 if (bsddev != NULL && dev->bsddev_attached_here) {
365 device_delete_child(device_get_parent(bsddev), bsddev);
372 device_del(struct device *dev)
376 bsddev = dev->bsddev;
379 if (bsddev != NULL && dev->bsddev_attached_here) {
381 device_delete_child(device_get_parent(bsddev), bsddev);
386 struct device *device_create(struct class *class, struct device *parent,
387 dev_t devt, void *drvdata, const char *fmt, ...);
390 device_destroy(struct class *class, dev_t devt)
396 bsddev = devclass_get_device(class->bsdclass, unit);
398 device_unregister(device_get_softc(bsddev));
402 linux_class_kfree(struct class *class)
408 static inline struct class *
409 class_create(struct module *owner, const char *name)
414 class = kzalloc(sizeof(*class), M_WAITOK);
415 class->owner = owner;
417 class->class_release = linux_class_kfree;
418 error = class_register(class);
428 class_destroy(struct class *class)
433 class_unregister(class);
437 device_create_file(struct device *dev, const struct device_attribute *attr)
441 return sysfs_create_file(&dev->kobj, &attr->attr);
446 device_remove_file(struct device *dev, const struct device_attribute *attr)
450 sysfs_remove_file(&dev->kobj, &attr->attr);
454 class_create_file(struct class *class, const struct class_attribute *attr)
458 return sysfs_create_file(&class->kobj, &attr->attr);
463 class_remove_file(struct class *class, const struct class_attribute *attr)
467 sysfs_remove_file(&class->kobj, &attr->attr);
471 dev_to_node(struct device *dev)
476 char *kvasprintf(gfp_t, const char *, va_list);
477 char *kasprintf(gfp_t, const char *, ...);
479 #endif /* _LINUX_DEVICE_H_ */