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;
68 void (*release)(struct device *dev);
74 unsigned int msix_max;
75 const struct attribute_group **groups;
78 extern struct device linux_root_device;
79 extern struct kobject linux_class_root;
80 extern const struct kobj_type linux_dev_ktype;
81 extern const struct kobj_type linux_class_ktype;
83 struct class_attribute {
84 struct attribute attr;
85 ssize_t (*show)(struct class *, struct class_attribute *, char *);
86 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
87 const void *(*namespace)(struct class *, const struct class_attribute *);
90 #define CLASS_ATTR(_name, _mode, _show, _store) \
91 struct class_attribute class_attr_##_name = \
92 { { #_name, NULL, _mode }, _show, _store }
94 struct device_attribute {
95 struct attribute attr;
96 ssize_t (*show)(struct device *,
97 struct device_attribute *, char *);
98 ssize_t (*store)(struct device *,
99 struct device_attribute *, const char *,
103 #define DEVICE_ATTR(_name, _mode, _show, _store) \
104 struct device_attribute dev_attr_##_name = \
105 { { #_name, NULL, _mode }, _show, _store }
107 /* Simple class attribute that is just a static string */
108 struct class_attribute_string {
109 struct class_attribute attr;
113 static inline ssize_t
114 show_class_attr_string(struct class *class,
115 struct class_attribute *attr, char *buf)
117 struct class_attribute_string *cs;
118 cs = container_of(attr, struct class_attribute_string, attr);
119 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
122 /* Currently read-only only */
123 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
124 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
125 #define CLASS_ATTR_STRING(_name, _mode, _str) \
126 struct class_attribute_string class_attr_##_name = \
127 _CLASS_ATTR_STRING(_name, _mode, _str)
129 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
130 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
131 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
132 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
133 #define dev_printk(lvl, dev, fmt, ...) \
134 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
137 dev_get_drvdata(const struct device *dev)
140 return dev->driver_data;
144 dev_set_drvdata(struct device *dev, void *data)
147 dev->driver_data = data;
150 static inline struct device *
151 get_device(struct device *dev)
155 kobject_get(&dev->kobj);
161 dev_name(const struct device *dev)
164 return kobject_name(&dev->kobj);
167 #define dev_set_name(_dev, _fmt, ...) \
168 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
171 put_device(struct device *dev)
175 kobject_put(&dev->kobj);
179 class_register(struct class *class)
182 class->bsdclass = devclass_create(class->name);
183 kobject_init(&class->kobj, &linux_class_ktype);
184 kobject_set_name(&class->kobj, class->name);
185 kobject_add(&class->kobj, &linux_class_root, class->name);
191 class_unregister(struct class *class)
194 kobject_put(&class->kobj);
197 static inline struct device *kobj_to_dev(struct kobject *kobj)
199 return container_of(kobj, struct device, kobj);
203 * Devices are registered and created for exporting to sysfs. Create
204 * implies register and register assumes the device fields have been
205 * setup appropriately before being called.
208 device_initialize(struct device *dev)
214 int unit = MINOR(dev->devt);
215 bsddev = devclass_get_device(dev->class->bsdclass, unit);
218 device_set_softc(bsddev, dev);
220 dev->bsddev = bsddev;
221 kobject_init(&dev->kobj, &linux_dev_ktype);
225 device_add(struct device *dev)
227 if (dev->bsddev != NULL) {
229 dev->devt = makedev(0, device_get_unit(dev->bsddev));
231 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
236 device_create_release(struct device *dev)
241 static inline struct device *
242 device_create_groups_vargs(struct class *class, struct device *parent,
243 dev_t devt, void *drvdata, const struct attribute_group **groups,
244 const char *fmt, va_list args)
246 struct device *dev = NULL;
247 int retval = -ENODEV;
249 if (class == NULL || IS_ERR(class))
252 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
258 device_initialize(dev);
261 dev->parent = parent;
262 dev->groups = groups;
263 dev->release = device_create_release;
264 dev->bsddev = devclass_get_device(dev->class->bsdclass, MINOR(devt));
265 dev_set_drvdata(dev, drvdata);
267 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
271 retval = device_add(dev);
279 return ERR_PTR(retval);
282 static inline struct device *
283 device_create_with_groups(struct class *class,
284 struct device *parent, dev_t devt, void *drvdata,
285 const struct attribute_group **groups, const char *fmt, ...)
290 va_start(vargs, fmt);
291 dev = device_create_groups_vargs(class, parent, devt, drvdata,
298 device_register(struct device *dev)
307 unit = MINOR(dev->devt);
308 bsddev = devclass_get_device(dev->class->bsdclass, unit);
309 } else if (dev->parent == NULL) {
310 bsddev = devclass_get_device(dev->class->bsdclass, 0);
312 if (bsddev == NULL && dev->parent != NULL) {
313 bsddev = device_add_child(dev->parent->bsddev,
314 dev->class->kobj.name, unit);
316 if (bsddev != NULL) {
318 dev->devt = makedev(0, device_get_unit(bsddev));
319 device_set_softc(bsddev, dev);
321 dev->bsddev = bsddev;
322 kobject_init(&dev->kobj, &linux_dev_ktype);
323 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
329 device_unregister(struct device *dev)
333 bsddev = dev->bsddev;
336 if (bsddev != NULL) {
338 device_delete_child(device_get_parent(bsddev), bsddev);
345 device_del(struct device *dev)
349 bsddev = dev->bsddev;
352 if (bsddev != NULL) {
354 device_delete_child(device_get_parent(bsddev), bsddev);
359 struct device *device_create(struct class *class, struct device *parent,
360 dev_t devt, void *drvdata, const char *fmt, ...);
363 device_destroy(struct class *class, dev_t devt)
369 bsddev = devclass_get_device(class->bsdclass, unit);
371 device_unregister(device_get_softc(bsddev));
375 linux_class_kfree(struct class *class)
381 static inline struct class *
382 class_create(struct module *owner, const char *name)
387 class = kzalloc(sizeof(*class), M_WAITOK);
388 class->owner = owner;
390 class->class_release = linux_class_kfree;
391 error = class_register(class);
401 class_destroy(struct class *class)
406 class_unregister(class);
410 device_create_file(struct device *dev, const struct device_attribute *attr)
414 return sysfs_create_file(&dev->kobj, &attr->attr);
419 device_remove_file(struct device *dev, const struct device_attribute *attr)
423 sysfs_remove_file(&dev->kobj, &attr->attr);
427 class_create_file(struct class *class, const struct class_attribute *attr)
431 return sysfs_create_file(&class->kobj, &attr->attr);
436 class_remove_file(struct class *class, const struct class_attribute *attr)
440 sysfs_remove_file(&class->kobj, &attr->attr);
444 dev_to_node(struct device *dev)
449 char *kvasprintf(gfp_t, const char *, va_list);
450 char *kasprintf(gfp_t, const char *, ...);
452 #endif /* _LINUX_DEVICE_H_ */