2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 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/types.h>
35 #include <linux/kobject.h>
36 #include <linux/list.h>
37 #include <linux/compiler.h>
38 #include <linux/types.h>
39 #include <linux/module.h>
40 #include <linux/workqueue.h>
41 #include <linux/sysfs.h>
42 #include <linux/kdev_t.h>
43 #include <asm/atomic.h>
47 enum irqreturn { IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
48 typedef enum irqreturn irqreturn_t;
55 void (*class_release)(struct class *class);
56 void (*dev_release)(struct device *dev);
57 char * (*devnode)(struct device *dev, umode_t *mode);
61 struct device *parent;
62 struct list_head irqents;
66 void (*release)(struct device *dev);
72 unsigned int msix_max;
75 extern struct device linux_rootdev;
76 extern struct kobject class_root;
78 struct class_attribute {
79 struct attribute attr;
80 ssize_t (*show)(struct class *, struct class_attribute *, char *);
81 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
82 const void *(*namespace)(struct class *, const struct class_attribute *);
85 #define CLASS_ATTR(_name, _mode, _show, _store) \
86 struct class_attribute class_attr_##_name = \
87 { { #_name, NULL, _mode }, _show, _store }
89 struct device_attribute {
90 struct attribute attr;
91 ssize_t (*show)(struct device *,
92 struct device_attribute *, char *);
93 ssize_t (*store)(struct device *,
94 struct device_attribute *, const char *,
98 #define DEVICE_ATTR(_name, _mode, _show, _store) \
99 struct device_attribute dev_attr_##_name = \
100 { { #_name, NULL, _mode }, _show, _store }
102 /* Simple class attribute that is just a static string */
103 struct class_attribute_string {
104 struct class_attribute attr;
108 static inline ssize_t
109 show_class_attr_string(struct class *class,
110 struct class_attribute *attr, char *buf)
112 struct class_attribute_string *cs;
113 cs = container_of(attr, struct class_attribute_string, attr);
114 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
117 /* Currently read-only only */
118 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
119 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
120 #define CLASS_ATTR_STRING(_name, _mode, _str) \
121 struct class_attribute_string class_attr_##_name = \
122 _CLASS_ATTR_STRING(_name, _mode, _str)
124 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
125 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
126 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
127 #define dev_printk(lvl, dev, fmt, ...) \
128 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
131 dev_get_drvdata(struct device *dev)
134 return dev->driver_data;
138 dev_set_drvdata(struct device *dev, void *data)
141 dev->driver_data = data;
144 static inline struct device *
145 get_device(struct device *dev)
149 kobject_get(&dev->kobj);
155 dev_name(const struct device *dev)
158 return kobject_name(&dev->kobj);
161 #define dev_set_name(_dev, _fmt, ...) \
162 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
165 put_device(struct device *dev)
169 kobject_put(&dev->kobj);
172 static inline ssize_t
173 class_show(struct kobject *kobj, struct attribute *attr, char *buf)
175 struct class_attribute *dattr;
178 dattr = container_of(attr, struct class_attribute, attr);
181 error = dattr->show(container_of(kobj, struct class, kobj),
186 static inline ssize_t
187 class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
190 struct class_attribute *dattr;
193 dattr = container_of(attr, struct class_attribute, attr);
196 error = dattr->store(container_of(kobj, struct class, kobj),
202 class_release(struct kobject *kobj)
206 class = container_of(kobj, struct class, kobj);
207 if (class->class_release)
208 class->class_release(class);
211 static struct sysfs_ops class_sysfs = {
213 .store = class_store,
215 static struct kobj_type class_ktype = {
216 .release = class_release,
217 .sysfs_ops = &class_sysfs
221 class_register(struct class *class)
224 class->bsdclass = devclass_create(class->name);
225 kobject_init(&class->kobj, &class_ktype);
226 kobject_set_name(&class->kobj, class->name);
227 kobject_add(&class->kobj, &class_root, class->name);
233 class_unregister(struct class *class)
236 kobject_put(&class->kobj);
240 device_release(struct kobject *kobj)
244 dev = container_of(kobj, struct device, kobj);
245 /* This is the precedence defined by linux. */
248 else if (dev->class && dev->class->dev_release)
249 dev->class->dev_release(dev);
252 static inline ssize_t
253 dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
255 struct device_attribute *dattr;
258 dattr = container_of(attr, struct device_attribute, attr);
261 error = dattr->show(container_of(kobj, struct device, kobj),
266 static inline ssize_t
267 dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
270 struct device_attribute *dattr;
273 dattr = container_of(attr, struct device_attribute, attr);
276 error = dattr->store(container_of(kobj, struct device, kobj),
281 static struct sysfs_ops dev_sysfs = { .show = dev_show, .store = dev_store, };
282 static struct kobj_type dev_ktype = {
283 .release = device_release,
284 .sysfs_ops = &dev_sysfs
288 * Devices are registered and created for exporting to sysfs. create
289 * implies register and register assumes the device fields have been
290 * setup appropriately before being called.
293 device_register(struct device *dev)
300 unit = MINOR(dev->devt);
301 bsddev = devclass_get_device(dev->class->bsdclass, unit);
305 bsddev = device_add_child(dev->parent->bsddev,
306 dev->class->kobj.name, unit);
309 dev->devt = makedev(0, device_get_unit(bsddev));
310 device_set_softc(bsddev, dev);
312 dev->bsddev = bsddev;
313 kobject_init(&dev->kobj, &dev_ktype);
314 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
320 device_unregister(struct device *dev)
324 bsddev = dev->bsddev;
327 device_delete_child(device_get_parent(bsddev), bsddev);
332 struct device *device_create(struct class *class, struct device *parent,
333 dev_t devt, void *drvdata, const char *fmt, ...);
336 device_destroy(struct class *class, dev_t devt)
342 bsddev = devclass_get_device(class->bsdclass, unit);
344 device_unregister(device_get_softc(bsddev));
348 class_kfree(struct class *class)
354 static inline struct class *
355 class_create(struct module *owner, const char *name)
360 class = kzalloc(sizeof(*class), M_WAITOK);
361 class->owner = owner;
363 class->class_release = class_kfree;
364 error = class_register(class);
374 class_destroy(struct class *class)
379 class_unregister(class);
383 device_create_file(struct device *dev, const struct device_attribute *attr)
387 return sysfs_create_file(&dev->kobj, &attr->attr);
392 device_remove_file(struct device *dev, const struct device_attribute *attr)
396 sysfs_remove_file(&dev->kobj, &attr->attr);
400 class_create_file(struct class *class, const struct class_attribute *attr)
404 return sysfs_create_file(&class->kobj, &attr->attr);
409 class_remove_file(struct class *class, const struct class_attribute *attr)
413 sysfs_remove_file(&class->kobj, &attr->attr);
416 static inline int dev_to_node(struct device *dev)
421 char *kvasprintf(gfp_t, const char *, va_list);
422 char *kasprintf(gfp_t, const char *, ...);
424 #endif /* _LINUX_DEVICE_H_ */