xref: /freebsd/sys/compat/linuxkpi/common/include/linux/device.h (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
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-2016 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/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>
46 
47 #include <sys/bus.h>
48 
49 enum irqreturn	{ IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
50 typedef enum irqreturn	irqreturn_t;
51 
52 struct class {
53 	const char	*name;
54 	struct module	*owner;
55 	struct kobject	kobj;
56 	devclass_t	bsdclass;
57 	void		(*class_release)(struct class *class);
58 	void		(*dev_release)(struct device *dev);
59 	char *		(*devnode)(struct device *dev, umode_t *mode);
60 };
61 
62 struct device {
63 	struct device	*parent;
64 	struct list_head irqents;
65 	device_t	bsddev;
66 	dev_t		devt;
67 	struct class	*class;
68 	void		(*release)(struct device *dev);
69 	struct kobject	kobj;
70 	uint64_t	*dma_mask;
71 	void		*driver_data;
72 	unsigned int	irq;
73 	unsigned int	msix;
74 	unsigned int	msix_max;
75 	const struct attribute_group **groups;
76 };
77 
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;
82 
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 *);
88 };
89 
90 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
91 	struct class_attribute class_attr_##_name =			\
92 	    { { #_name, NULL, _mode }, _show, _store }
93 
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 *,
100 					size_t);
101 };
102 
103 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
104 	struct device_attribute dev_attr_##_name =			\
105 	    { { #_name, NULL, _mode }, _show, _store }
106 
107 /* Simple class attribute that is just a static string */
108 struct class_attribute_string {
109 	struct class_attribute attr;
110 	char *str;
111 };
112 
113 static inline ssize_t
114 show_class_attr_string(struct class *class,
115 				struct class_attribute *attr, char *buf)
116 {
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);
120 }
121 
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)
128 
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__)
135 
136 static inline void *
137 dev_get_drvdata(const struct device *dev)
138 {
139 
140 	return dev->driver_data;
141 }
142 
143 static inline void
144 dev_set_drvdata(struct device *dev, void *data)
145 {
146 
147 	dev->driver_data = data;
148 }
149 
150 static inline struct device *
151 get_device(struct device *dev)
152 {
153 
154 	if (dev)
155 		kobject_get(&dev->kobj);
156 
157 	return (dev);
158 }
159 
160 static inline char *
161 dev_name(const struct device *dev)
162 {
163 
164  	return kobject_name(&dev->kobj);
165 }
166 
167 #define	dev_set_name(_dev, _fmt, ...)					\
168 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
169 
170 static inline void
171 put_device(struct device *dev)
172 {
173 
174 	if (dev)
175 		kobject_put(&dev->kobj);
176 }
177 
178 static inline int
179 class_register(struct class *class)
180 {
181 
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);
186 
187 	return (0);
188 }
189 
190 static inline void
191 class_unregister(struct class *class)
192 {
193 
194 	kobject_put(&class->kobj);
195 }
196 
197 static inline struct device *kobj_to_dev(struct kobject *kobj)
198 {
199 	return container_of(kobj, struct device, kobj);
200 }
201 
202 /*
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.
206  */
207 static inline void
208 device_initialize(struct device *dev)
209 {
210 	device_t bsddev;
211 
212 	bsddev = NULL;
213 	if (dev->devt) {
214 		int unit = MINOR(dev->devt);
215 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
216 	}
217 	if (bsddev != NULL)
218 		device_set_softc(bsddev, dev);
219 
220 	dev->bsddev = bsddev;
221 	kobject_init(&dev->kobj, &linux_dev_ktype);
222 }
223 
224 static inline int
225 device_add(struct device *dev)
226 {
227 	if (dev->bsddev != NULL) {
228 		if (dev->devt == 0)
229 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
230 	}
231 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
232 	return (0);
233 }
234 
235 static inline void
236 device_create_release(struct device *dev)
237 {
238 	kfree(dev);
239 }
240 
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)
245 {
246 	struct device *dev = NULL;
247 	int retval = -ENODEV;
248 
249 	if (class == NULL || IS_ERR(class))
250 		goto error;
251 
252 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
253 	if (!dev) {
254 		retval = -ENOMEM;
255 		goto error;
256 	}
257 
258 	device_initialize(dev);
259 	dev->devt = devt;
260 	dev->class = class;
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);
266 
267 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
268 	if (retval)
269 		goto error;
270 
271 	retval = device_add(dev);
272 	if (retval)
273 		goto error;
274 
275 	return dev;
276 
277 error:
278 	put_device(dev);
279 	return ERR_PTR(retval);
280 }
281 
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, ...)
286 {
287 	va_list vargs;
288 	struct device *dev;
289 
290 	va_start(vargs, fmt);
291 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
292 	    groups, fmt, vargs);
293 	va_end(vargs);
294 	return dev;
295 }
296 
297 static inline int
298 device_register(struct device *dev)
299 {
300 	device_t bsddev;
301 	int unit;
302 
303 	bsddev = NULL;
304 	unit = -1;
305 
306 	if (dev->devt) {
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);
311 	}
312 	if (bsddev == NULL && dev->parent != NULL) {
313 		bsddev = device_add_child(dev->parent->bsddev,
314 		    dev->class->kobj.name, unit);
315 	}
316 	if (bsddev != NULL) {
317 		if (dev->devt == 0)
318 			dev->devt = makedev(0, device_get_unit(bsddev));
319 		device_set_softc(bsddev, dev);
320 	}
321 	dev->bsddev = bsddev;
322 	kobject_init(&dev->kobj, &linux_dev_ktype);
323 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
324 
325 	return (0);
326 }
327 
328 static inline void
329 device_unregister(struct device *dev)
330 {
331 	device_t bsddev;
332 
333 	bsddev = dev->bsddev;
334 	dev->bsddev = NULL;
335 
336 	if (bsddev != NULL) {
337 		mtx_lock(&Giant);
338 		device_delete_child(device_get_parent(bsddev), bsddev);
339 		mtx_unlock(&Giant);
340 	}
341 	put_device(dev);
342 }
343 
344 static inline void
345 device_del(struct device *dev)
346 {
347 	device_t bsddev;
348 
349 	bsddev = dev->bsddev;
350 	dev->bsddev = NULL;
351 
352 	if (bsddev != NULL) {
353 		mtx_lock(&Giant);
354 		device_delete_child(device_get_parent(bsddev), bsddev);
355 		mtx_unlock(&Giant);
356 	}
357 }
358 
359 struct device *device_create(struct class *class, struct device *parent,
360 	    dev_t devt, void *drvdata, const char *fmt, ...);
361 
362 static inline void
363 device_destroy(struct class *class, dev_t devt)
364 {
365 	device_t bsddev;
366 	int unit;
367 
368 	unit = MINOR(devt);
369 	bsddev = devclass_get_device(class->bsdclass, unit);
370 	if (bsddev != NULL)
371 		device_unregister(device_get_softc(bsddev));
372 }
373 
374 static inline void
375 linux_class_kfree(struct class *class)
376 {
377 
378 	kfree(class);
379 }
380 
381 static inline struct class *
382 class_create(struct module *owner, const char *name)
383 {
384 	struct class *class;
385 	int error;
386 
387 	class = kzalloc(sizeof(*class), M_WAITOK);
388 	class->owner = owner;
389 	class->name= name;
390 	class->class_release = linux_class_kfree;
391 	error = class_register(class);
392 	if (error) {
393 		kfree(class);
394 		return (NULL);
395 	}
396 
397 	return (class);
398 }
399 
400 static inline void
401 class_destroy(struct class *class)
402 {
403 
404 	if (class == NULL)
405 		return;
406 	class_unregister(class);
407 }
408 
409 static inline int
410 device_create_file(struct device *dev, const struct device_attribute *attr)
411 {
412 
413 	if (dev)
414 		return sysfs_create_file(&dev->kobj, &attr->attr);
415 	return -EINVAL;
416 }
417 
418 static inline void
419 device_remove_file(struct device *dev, const struct device_attribute *attr)
420 {
421 
422 	if (dev)
423 		sysfs_remove_file(&dev->kobj, &attr->attr);
424 }
425 
426 static inline int
427 class_create_file(struct class *class, const struct class_attribute *attr)
428 {
429 
430 	if (class)
431 		return sysfs_create_file(&class->kobj, &attr->attr);
432 	return -EINVAL;
433 }
434 
435 static inline void
436 class_remove_file(struct class *class, const struct class_attribute *attr)
437 {
438 
439 	if (class)
440 		sysfs_remove_file(&class->kobj, &attr->attr);
441 }
442 
443 static inline int
444 dev_to_node(struct device *dev)
445 {
446                 return -1;
447 }
448 
449 char *kvasprintf(gfp_t, const char *, va_list);
450 char *kasprintf(gfp_t, const char *, ...);
451 
452 #endif	/* _LINUX_DEVICE_H_ */
453