xref: /freebsd/sys/compat/linuxkpi/common/include/linux/device.h (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
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.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
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.
17  *
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.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUX_DEVICE_H_
32 #define	_LINUX_DEVICE_H_
33 
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>
45 
46 #include <sys/bus.h>
47 
48 enum irqreturn	{ IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
49 typedef enum irqreturn	irqreturn_t;
50 
51 struct class {
52 	const char	*name;
53 	struct module	*owner;
54 	struct kobject	kobj;
55 	devclass_t	bsdclass;
56 	void		(*class_release)(struct class *class);
57 	void		(*dev_release)(struct device *dev);
58 	char *		(*devnode)(struct device *dev, umode_t *mode);
59 };
60 
61 struct device {
62 	struct device	*parent;
63 	struct list_head irqents;
64 	device_t	bsddev;
65 	dev_t		devt;
66 	struct class	*class;
67 	void		(*release)(struct device *dev);
68 	struct kobject	kobj;
69 	uint64_t	*dma_mask;
70 	void		*driver_data;
71 	unsigned int	irq;
72 	unsigned int	msix;
73 	unsigned int	msix_max;
74 };
75 
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;
80 
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 *);
86 };
87 
88 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
89 	struct class_attribute class_attr_##_name =			\
90 	    { { #_name, NULL, _mode }, _show, _store }
91 
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 *,
98 					size_t);
99 };
100 
101 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
102 	struct device_attribute dev_attr_##_name =			\
103 	    { { #_name, NULL, _mode }, _show, _store }
104 
105 /* Simple class attribute that is just a static string */
106 struct class_attribute_string {
107 	struct class_attribute attr;
108 	char *str;
109 };
110 
111 static inline ssize_t
112 show_class_attr_string(struct class *class,
113 				struct class_attribute *attr, char *buf)
114 {
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);
118 }
119 
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)
126 
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__)
132 
133 static inline void *
134 dev_get_drvdata(struct device *dev)
135 {
136 
137 	return dev->driver_data;
138 }
139 
140 static inline void
141 dev_set_drvdata(struct device *dev, void *data)
142 {
143 
144 	dev->driver_data = data;
145 }
146 
147 static inline struct device *
148 get_device(struct device *dev)
149 {
150 
151 	if (dev)
152 		kobject_get(&dev->kobj);
153 
154 	return (dev);
155 }
156 
157 static inline char *
158 dev_name(const struct device *dev)
159 {
160 
161  	return kobject_name(&dev->kobj);
162 }
163 
164 #define	dev_set_name(_dev, _fmt, ...)					\
165 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
166 
167 static inline void
168 put_device(struct device *dev)
169 {
170 
171 	if (dev)
172 		kobject_put(&dev->kobj);
173 }
174 
175 static inline int
176 class_register(struct class *class)
177 {
178 
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);
183 
184 	return (0);
185 }
186 
187 static inline void
188 class_unregister(struct class *class)
189 {
190 
191 	kobject_put(&class->kobj);
192 }
193 
194 /*
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.
198  */
199 static inline int
200 device_register(struct device *dev)
201 {
202 	device_t bsddev;
203 	int unit;
204 
205 	bsddev = NULL;
206 	if (dev->devt) {
207 		unit = MINOR(dev->devt);
208 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
209 	} else
210 		unit = -1;
211 	if (bsddev == NULL)
212 		bsddev = device_add_child(dev->parent->bsddev,
213 		    dev->class->kobj.name, unit);
214 	if (bsddev) {
215 		if (dev->devt == 0)
216 			dev->devt = makedev(0, device_get_unit(bsddev));
217 		device_set_softc(bsddev, dev);
218 	}
219 	dev->bsddev = bsddev;
220 	kobject_init(&dev->kobj, &linux_dev_ktype);
221 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
222 
223 	return (0);
224 }
225 
226 static inline void
227 device_unregister(struct device *dev)
228 {
229 	device_t bsddev;
230 
231 	bsddev = dev->bsddev;
232 	mtx_lock(&Giant);
233 	if (bsddev)
234 		device_delete_child(device_get_parent(bsddev), bsddev);
235 	mtx_unlock(&Giant);
236 	put_device(dev);
237 }
238 
239 struct device *device_create(struct class *class, struct device *parent,
240 	    dev_t devt, void *drvdata, const char *fmt, ...);
241 
242 static inline void
243 device_destroy(struct class *class, dev_t devt)
244 {
245 	device_t bsddev;
246 	int unit;
247 
248 	unit = MINOR(devt);
249 	bsddev = devclass_get_device(class->bsdclass, unit);
250 	if (bsddev)
251 		device_unregister(device_get_softc(bsddev));
252 }
253 
254 static inline void
255 linux_class_kfree(struct class *class)
256 {
257 
258 	kfree(class);
259 }
260 
261 static inline struct class *
262 class_create(struct module *owner, const char *name)
263 {
264 	struct class *class;
265 	int error;
266 
267 	class = kzalloc(sizeof(*class), M_WAITOK);
268 	class->owner = owner;
269 	class->name= name;
270 	class->class_release = linux_class_kfree;
271 	error = class_register(class);
272 	if (error) {
273 		kfree(class);
274 		return (NULL);
275 	}
276 
277 	return (class);
278 }
279 
280 static inline void
281 class_destroy(struct class *class)
282 {
283 
284 	if (class == NULL)
285 		return;
286 	class_unregister(class);
287 }
288 
289 static inline int
290 device_create_file(struct device *dev, const struct device_attribute *attr)
291 {
292 
293 	if (dev)
294 		return sysfs_create_file(&dev->kobj, &attr->attr);
295 	return -EINVAL;
296 }
297 
298 static inline void
299 device_remove_file(struct device *dev, const struct device_attribute *attr)
300 {
301 
302 	if (dev)
303 		sysfs_remove_file(&dev->kobj, &attr->attr);
304 }
305 
306 static inline int
307 class_create_file(struct class *class, const struct class_attribute *attr)
308 {
309 
310 	if (class)
311 		return sysfs_create_file(&class->kobj, &attr->attr);
312 	return -EINVAL;
313 }
314 
315 static inline void
316 class_remove_file(struct class *class, const struct class_attribute *attr)
317 {
318 
319 	if (class)
320 		sysfs_remove_file(&class->kobj, &attr->attr);
321 }
322 
323 static inline int
324 dev_to_node(struct device *dev)
325 {
326                 return -1;
327 }
328 
329 char *kvasprintf(gfp_t, const char *, va_list);
330 char *kasprintf(gfp_t, const char *, ...);
331 
332 #endif	/* _LINUX_DEVICE_H_ */
333