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/sysfs.h>
37 #include <linux/list.h>
38 #include <linux/compiler.h>
39 #include <linux/types.h>
40 #include <linux/module.h>
41 #include <linux/workqueue.h>
42 #include <linux/sysfs.h>
43 #include <linux/kdev_t.h>
44 #include <asm/atomic.h>
48 enum irqreturn { IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
49 typedef enum irqreturn irqreturn_t;
56 void (*class_release)(struct class *class);
57 void (*dev_release)(struct device *dev);
58 char * (*devnode)(struct device *dev, umode_t *mode);
62 struct device *parent;
63 struct list_head irqents;
67 void (*release)(struct device *dev);
73 unsigned int msix_max;
76 extern struct device linux_root_device;
77 extern struct kobject linux_class_root;
78 extern const struct kobj_type linux_dev_ktype;
79 extern const struct kobj_type linux_class_ktype;
81 struct class_attribute {
82 struct attribute attr;
83 ssize_t (*show)(struct class *, struct class_attribute *, char *);
84 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
85 const void *(*namespace)(struct class *, const struct class_attribute *);
88 #define CLASS_ATTR(_name, _mode, _show, _store) \
89 struct class_attribute class_attr_##_name = \
90 { { #_name, NULL, _mode }, _show, _store }
92 struct device_attribute {
93 struct attribute attr;
94 ssize_t (*show)(struct device *,
95 struct device_attribute *, char *);
96 ssize_t (*store)(struct device *,
97 struct device_attribute *, const char *,
101 #define DEVICE_ATTR(_name, _mode, _show, _store) \
102 struct device_attribute dev_attr_##_name = \
103 { { #_name, NULL, _mode }, _show, _store }
105 /* Simple class attribute that is just a static string */
106 struct class_attribute_string {
107 struct class_attribute attr;
111 static inline ssize_t
112 show_class_attr_string(struct class *class,
113 struct class_attribute *attr, char *buf)
115 struct class_attribute_string *cs;
116 cs = container_of(attr, struct class_attribute_string, attr);
117 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
120 /* Currently read-only only */
121 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
122 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
123 #define CLASS_ATTR_STRING(_name, _mode, _str) \
124 struct class_attribute_string class_attr_##_name = \
125 _CLASS_ATTR_STRING(_name, _mode, _str)
127 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
128 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
129 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
130 #define dev_printk(lvl, dev, fmt, ...) \
131 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
134 dev_get_drvdata(struct device *dev)
137 return dev->driver_data;
141 dev_set_drvdata(struct device *dev, void *data)
144 dev->driver_data = data;
147 static inline struct device *
148 get_device(struct device *dev)
152 kobject_get(&dev->kobj);
158 dev_name(const struct device *dev)
161 return kobject_name(&dev->kobj);
164 #define dev_set_name(_dev, _fmt, ...) \
165 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
168 put_device(struct device *dev)
172 kobject_put(&dev->kobj);
176 class_register(struct class *class)
179 class->bsdclass = devclass_create(class->name);
180 kobject_init(&class->kobj, &linux_class_ktype);
181 kobject_set_name(&class->kobj, class->name);
182 kobject_add(&class->kobj, &linux_class_root, class->name);
188 class_unregister(struct class *class)
191 kobject_put(&class->kobj);
195 * Devices are registered and created for exporting to sysfs. create
196 * implies register and register assumes the device fields have been
197 * setup appropriately before being called.
200 device_register(struct device *dev)
207 unit = MINOR(dev->devt);
208 bsddev = devclass_get_device(dev->class->bsdclass, unit);
212 bsddev = device_add_child(dev->parent->bsddev,
213 dev->class->kobj.name, unit);
216 dev->devt = makedev(0, device_get_unit(bsddev));
217 device_set_softc(bsddev, dev);
219 dev->bsddev = bsddev;
220 kobject_init(&dev->kobj, &linux_dev_ktype);
221 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
227 device_unregister(struct device *dev)
231 bsddev = dev->bsddev;
234 device_delete_child(device_get_parent(bsddev), bsddev);
239 struct device *device_create(struct class *class, struct device *parent,
240 dev_t devt, void *drvdata, const char *fmt, ...);
243 device_destroy(struct class *class, dev_t devt)
249 bsddev = devclass_get_device(class->bsdclass, unit);
251 device_unregister(device_get_softc(bsddev));
255 linux_class_kfree(struct class *class)
261 static inline struct class *
262 class_create(struct module *owner, const char *name)
267 class = kzalloc(sizeof(*class), M_WAITOK);
268 class->owner = owner;
270 class->class_release = linux_class_kfree;
271 error = class_register(class);
281 class_destroy(struct class *class)
286 class_unregister(class);
290 device_create_file(struct device *dev, const struct device_attribute *attr)
294 return sysfs_create_file(&dev->kobj, &attr->attr);
299 device_remove_file(struct device *dev, const struct device_attribute *attr)
303 sysfs_remove_file(&dev->kobj, &attr->attr);
307 class_create_file(struct class *class, const struct class_attribute *attr)
311 return sysfs_create_file(&class->kobj, &attr->attr);
316 class_remove_file(struct class *class, const struct class_attribute *attr)
320 sysfs_remove_file(&class->kobj, &attr->attr);
324 dev_to_node(struct device *dev)
329 char *kvasprintf(gfp_t, const char *, va_list);
330 char *kasprintf(gfp_t, const char *, ...);
332 #endif /* _LINUX_DEVICE_H_ */