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.
29 #ifndef _LINUX_DEVICE_H_
30 #define _LINUX_DEVICE_H_
32 #include <linux/types.h>
33 #include <linux/kobject.h>
34 #include <linux/list.h>
35 #include <linux/compiler.h>
36 #include <linux/types.h>
37 #include <linux/module.h>
38 #include <linux/workqueue.h>
39 #include <linux/sysfs.h>
40 #include <linux/kdev_t.h>
41 #include <asm/atomic.h>
45 enum irqreturn { IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
46 typedef enum irqreturn irqreturn_t;
53 void (*class_release)(struct class *class);
54 void (*dev_release)(struct device *dev);
55 char * (*devnode)(struct device *dev, umode_t *mode);
59 struct device *parent;
60 struct list_head irqents;
64 void (*release)(struct device *dev);
70 unsigned int msix_max;
73 extern struct device linux_rootdev;
74 extern struct kobject class_root;
76 struct class_attribute {
77 struct attribute attr;
78 ssize_t (*show)(struct class *, struct class_attribute *, char *);
79 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
80 const void *(*namespace)(struct class *, const struct class_attribute *);
83 #define CLASS_ATTR(_name, _mode, _show, _store) \
84 struct class_attribute class_attr_##_name = \
85 { { #_name, NULL, _mode }, _show, _store }
87 struct device_attribute {
88 struct attribute attr;
89 ssize_t (*show)(struct device *,
90 struct device_attribute *, char *);
91 ssize_t (*store)(struct device *,
92 struct device_attribute *, const char *,
96 #define DEVICE_ATTR(_name, _mode, _show, _store) \
97 struct device_attribute dev_attr_##_name = \
98 { { #_name, NULL, _mode }, _show, _store }
100 /* Simple class attribute that is just a static string */
101 struct class_attribute_string {
102 struct class_attribute attr;
106 static inline ssize_t
107 show_class_attr_string(struct class *class,
108 struct class_attribute *attr, char *buf)
110 struct class_attribute_string *cs;
111 cs = container_of(attr, struct class_attribute_string, attr);
112 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
115 /* Currently read-only only */
116 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
117 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
118 #define CLASS_ATTR_STRING(_name, _mode, _str) \
119 struct class_attribute_string class_attr_##_name = \
120 _CLASS_ATTR_STRING(_name, _mode, _str)
122 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
123 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
124 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
125 #define dev_printk(lvl, dev, fmt, ...) \
126 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
129 dev_get_drvdata(struct device *dev)
132 return dev->driver_data;
136 dev_set_drvdata(struct device *dev, void *data)
139 dev->driver_data = data;
142 static inline struct device *
143 get_device(struct device *dev)
147 kobject_get(&dev->kobj);
153 dev_name(const struct device *dev)
156 return kobject_name(&dev->kobj);
159 #define dev_set_name(_dev, _fmt, ...) \
160 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
163 put_device(struct device *dev)
167 kobject_put(&dev->kobj);
170 static inline ssize_t
171 class_show(struct kobject *kobj, struct attribute *attr, char *buf)
173 struct class_attribute *dattr;
176 dattr = container_of(attr, struct class_attribute, attr);
179 error = dattr->show(container_of(kobj, struct class, kobj),
184 static inline ssize_t
185 class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
188 struct class_attribute *dattr;
191 dattr = container_of(attr, struct class_attribute, attr);
194 error = dattr->store(container_of(kobj, struct class, kobj),
200 class_release(struct kobject *kobj)
204 class = container_of(kobj, struct class, kobj);
205 if (class->class_release)
206 class->class_release(class);
209 static struct sysfs_ops class_sysfs = {
211 .store = class_store,
213 static struct kobj_type class_ktype = {
214 .release = class_release,
215 .sysfs_ops = &class_sysfs
219 class_register(struct class *class)
222 class->bsdclass = devclass_create(class->name);
223 kobject_init(&class->kobj, &class_ktype);
224 kobject_set_name(&class->kobj, class->name);
225 kobject_add(&class->kobj, &class_root, class->name);
231 class_unregister(struct class *class)
234 kobject_put(&class->kobj);
238 device_release(struct kobject *kobj)
242 dev = container_of(kobj, struct device, kobj);
243 /* This is the precedence defined by linux. */
246 else if (dev->class && dev->class->dev_release)
247 dev->class->dev_release(dev);
250 static inline ssize_t
251 dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
253 struct device_attribute *dattr;
256 dattr = container_of(attr, struct device_attribute, attr);
259 error = dattr->show(container_of(kobj, struct device, kobj),
264 static inline ssize_t
265 dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
268 struct device_attribute *dattr;
271 dattr = container_of(attr, struct device_attribute, attr);
274 error = dattr->store(container_of(kobj, struct device, kobj),
279 static struct sysfs_ops dev_sysfs = { .show = dev_show, .store = dev_store, };
280 static struct kobj_type dev_ktype = {
281 .release = device_release,
282 .sysfs_ops = &dev_sysfs
286 * Devices are registered and created for exporting to sysfs. create
287 * implies register and register assumes the device fields have been
288 * setup appropriately before being called.
291 device_register(struct device *dev)
298 unit = MINOR(dev->devt);
299 bsddev = devclass_get_device(dev->class->bsdclass, unit);
303 bsddev = device_add_child(dev->parent->bsddev,
304 dev->class->kobj.name, unit);
307 dev->devt = makedev(0, device_get_unit(bsddev));
308 device_set_softc(bsddev, dev);
310 dev->bsddev = bsddev;
311 kobject_init(&dev->kobj, &dev_ktype);
312 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
318 device_unregister(struct device *dev)
322 bsddev = dev->bsddev;
325 device_delete_child(device_get_parent(bsddev), bsddev);
330 struct device *device_create(struct class *class, struct device *parent,
331 dev_t devt, void *drvdata, const char *fmt, ...);
334 device_destroy(struct class *class, dev_t devt)
340 bsddev = devclass_get_device(class->bsdclass, unit);
342 device_unregister(device_get_softc(bsddev));
346 class_kfree(struct class *class)
352 static inline struct class *
353 class_create(struct module *owner, const char *name)
358 class = kzalloc(sizeof(*class), M_WAITOK);
359 class->owner = owner;
361 class->class_release = class_kfree;
362 error = class_register(class);
372 class_destroy(struct class *class)
377 class_unregister(class);
381 device_create_file(struct device *dev, const struct device_attribute *attr)
385 return sysfs_create_file(&dev->kobj, &attr->attr);
390 device_remove_file(struct device *dev, const struct device_attribute *attr)
394 sysfs_remove_file(&dev->kobj, &attr->attr);
398 class_create_file(struct class *class, const struct class_attribute *attr)
402 return sysfs_create_file(&class->kobj, &attr->attr);
407 class_remove_file(struct class *class, const struct class_attribute *attr)
411 sysfs_remove_file(&class->kobj, &attr->attr);
414 static inline int dev_to_node(struct device *dev)
419 static inline char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
426 len = vsnprintf(NULL, 0, fmt, aq);
429 vsnprintf(p, len+1, fmt, ap);
434 char *kasprintf(gfp_t, const char *, ...);
436 #endif /* _LINUX_DEVICE_H_ */