xref: /freebsd/sys/compat/linuxkpi/common/include/linux/device.h (revision 0554abf0e036ce0c68591fae8b52b0118fd6378f)
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 device;
53 
54 struct class {
55 	const char	*name;
56 	struct module	*owner;
57 	struct kobject	kobj;
58 	devclass_t	bsdclass;
59 	void		(*class_release)(struct class *class);
60 	void		(*dev_release)(struct device *dev);
61 	char *		(*devnode)(struct device *dev, umode_t *mode);
62 };
63 
64 struct dev_pm_ops {
65 	int (*suspend)(struct device *dev);
66 	int (*suspend_late)(struct device *dev);
67 	int (*resume)(struct device *dev);
68 	int (*resume_early)(struct device *dev);
69 	int (*freeze)(struct device *dev);
70 	int (*freeze_late)(struct device *dev);
71 	int (*thaw)(struct device *dev);
72 	int (*thaw_early)(struct device *dev);
73 	int (*poweroff)(struct device *dev);
74 	int (*poweroff_late)(struct device *dev);
75 	int (*restore)(struct device *dev);
76 	int (*restore_early)(struct device *dev);
77 	int (*runtime_suspend)(struct device *dev);
78 	int (*runtime_resume)(struct device *dev);
79 	int (*runtime_idle)(struct device *dev);
80 };
81 
82 struct device_driver {
83 	const char	*name;
84 	const struct dev_pm_ops *pm;
85 };
86 
87 struct device_type {
88 	const char	*name;
89 };
90 
91 struct device {
92 	struct device	*parent;
93 	struct list_head irqents;
94 	device_t	bsddev;
95 	/*
96 	 * The following flag is used to determine if the LinuxKPI is
97 	 * responsible for detaching the BSD device or not. If the
98 	 * LinuxKPI got the BSD device using devclass_get_device(), it
99 	 * must not try to detach or delete it, because it's already
100 	 * done somewhere else.
101 	 */
102 	bool		bsddev_attached_here;
103 	struct device_driver *driver;
104 	struct device_type *type;
105 	dev_t		devt;
106 	struct class	*class;
107 	void		(*release)(struct device *dev);
108 	struct kobject	kobj;
109 	uint64_t	*dma_mask;
110 	void		*driver_data;
111 	unsigned int	irq;
112 #define	LINUX_IRQ_INVALID	65535
113 	unsigned int	msix;
114 	unsigned int	msix_max;
115 	const struct attribute_group **groups;
116 
117 	spinlock_t	devres_lock;
118 	struct list_head devres_head;
119 };
120 
121 extern struct device linux_root_device;
122 extern struct kobject linux_class_root;
123 extern const struct kobj_type linux_dev_ktype;
124 extern const struct kobj_type linux_class_ktype;
125 
126 struct class_attribute {
127         struct attribute attr;
128         ssize_t (*show)(struct class *, struct class_attribute *, char *);
129         ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
130         const void *(*namespace)(struct class *, const struct class_attribute *);
131 };
132 
133 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
134 	struct class_attribute class_attr_##_name =			\
135 	    { { #_name, NULL, _mode }, _show, _store }
136 
137 struct device_attribute {
138 	struct attribute	attr;
139 	ssize_t			(*show)(struct device *,
140 					struct device_attribute *, char *);
141 	ssize_t			(*store)(struct device *,
142 					struct device_attribute *, const char *,
143 					size_t);
144 };
145 
146 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
147 	struct device_attribute dev_attr_##_name =			\
148 	    __ATTR(_name, _mode, _show, _store)
149 #define	DEVICE_ATTR_RO(_name)						\
150 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
151 #define	DEVICE_ATTR_WO(_name)						\
152 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
153 #define	DEVICE_ATTR_RW(_name)						\
154 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
155 
156 /* Simple class attribute that is just a static string */
157 struct class_attribute_string {
158 	struct class_attribute attr;
159 	char *str;
160 };
161 
162 static inline ssize_t
163 show_class_attr_string(struct class *class,
164 				struct class_attribute *attr, char *buf)
165 {
166 	struct class_attribute_string *cs;
167 	cs = container_of(attr, struct class_attribute_string, attr);
168 	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
169 }
170 
171 /* Currently read-only only */
172 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
173 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
174 #define CLASS_ATTR_STRING(_name, _mode, _str) \
175 	struct class_attribute_string class_attr_##_name = \
176 		_CLASS_ATTR_STRING(_name, _mode, _str)
177 
178 #define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
179 #define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
180 #define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
181 #define	dev_notice(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
182 #define	dev_printk(lvl, dev, fmt, ...)					\
183 	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
184 
185 #define	dev_err_ratelimited(dev, ...) do {	\
186 	static linux_ratelimit_t __ratelimited;	\
187 	if (linux_ratelimited(&__ratelimited))	\
188 		dev_err(dev, __VA_ARGS__);	\
189 } while (0)
190 
191 #define	dev_warn_ratelimited(dev, ...) do {	\
192 	static linux_ratelimit_t __ratelimited;	\
193 	if (linux_ratelimited(&__ratelimited))	\
194 		dev_warn(dev, __VA_ARGS__);	\
195 } while (0)
196 
197 static inline void *
198 dev_get_drvdata(const struct device *dev)
199 {
200 
201 	return dev->driver_data;
202 }
203 
204 static inline void
205 dev_set_drvdata(struct device *dev, void *data)
206 {
207 
208 	dev->driver_data = data;
209 }
210 
211 static inline struct device *
212 get_device(struct device *dev)
213 {
214 
215 	if (dev)
216 		kobject_get(&dev->kobj);
217 
218 	return (dev);
219 }
220 
221 static inline char *
222 dev_name(const struct device *dev)
223 {
224 
225  	return kobject_name(&dev->kobj);
226 }
227 
228 #define	dev_set_name(_dev, _fmt, ...)					\
229 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
230 
231 static inline void
232 put_device(struct device *dev)
233 {
234 
235 	if (dev)
236 		kobject_put(&dev->kobj);
237 }
238 
239 static inline int
240 class_register(struct class *class)
241 {
242 
243 	class->bsdclass = devclass_create(class->name);
244 	kobject_init(&class->kobj, &linux_class_ktype);
245 	kobject_set_name(&class->kobj, class->name);
246 	kobject_add(&class->kobj, &linux_class_root, class->name);
247 
248 	return (0);
249 }
250 
251 static inline void
252 class_unregister(struct class *class)
253 {
254 
255 	kobject_put(&class->kobj);
256 }
257 
258 static inline struct device *kobj_to_dev(struct kobject *kobj)
259 {
260 	return container_of(kobj, struct device, kobj);
261 }
262 
263 /*
264  * Devices are registered and created for exporting to sysfs. Create
265  * implies register and register assumes the device fields have been
266  * setup appropriately before being called.
267  */
268 static inline void
269 device_initialize(struct device *dev)
270 {
271 	device_t bsddev = NULL;
272 	int unit = -1;
273 
274 	if (dev->devt) {
275 		unit = MINOR(dev->devt);
276 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
277 		dev->bsddev_attached_here = false;
278 	} else if (dev->parent == NULL) {
279 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
280 		dev->bsddev_attached_here = false;
281 	} else {
282 		dev->bsddev_attached_here = true;
283 	}
284 
285 	if (bsddev == NULL && dev->parent != NULL) {
286 		bsddev = device_add_child(dev->parent->bsddev,
287 		    dev->class->kobj.name, unit);
288 	}
289 
290 	if (bsddev != NULL)
291 		device_set_softc(bsddev, dev);
292 
293 	dev->bsddev = bsddev;
294 	MPASS(dev->bsddev != NULL);
295 	kobject_init(&dev->kobj, &linux_dev_ktype);
296 
297 	spin_lock_init(&dev->devres_lock);
298 	INIT_LIST_HEAD(&dev->devres_head);
299 }
300 
301 static inline int
302 device_add(struct device *dev)
303 {
304 	if (dev->bsddev != NULL) {
305 		if (dev->devt == 0)
306 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
307 	}
308 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
309 	return (0);
310 }
311 
312 static inline void
313 device_create_release(struct device *dev)
314 {
315 	kfree(dev);
316 }
317 
318 static inline struct device *
319 device_create_groups_vargs(struct class *class, struct device *parent,
320     dev_t devt, void *drvdata, const struct attribute_group **groups,
321     const char *fmt, va_list args)
322 {
323 	struct device *dev = NULL;
324 	int retval = -ENODEV;
325 
326 	if (class == NULL || IS_ERR(class))
327 		goto error;
328 
329 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
330 	if (!dev) {
331 		retval = -ENOMEM;
332 		goto error;
333 	}
334 
335 	dev->devt = devt;
336 	dev->class = class;
337 	dev->parent = parent;
338 	dev->groups = groups;
339 	dev->release = device_create_release;
340 	/* device_initialize() needs the class and parent to be set */
341 	device_initialize(dev);
342 	dev_set_drvdata(dev, drvdata);
343 
344 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
345 	if (retval)
346 		goto error;
347 
348 	retval = device_add(dev);
349 	if (retval)
350 		goto error;
351 
352 	return dev;
353 
354 error:
355 	put_device(dev);
356 	return ERR_PTR(retval);
357 }
358 
359 static inline struct device *
360 device_create_with_groups(struct class *class,
361     struct device *parent, dev_t devt, void *drvdata,
362     const struct attribute_group **groups, const char *fmt, ...)
363 {
364 	va_list vargs;
365 	struct device *dev;
366 
367 	va_start(vargs, fmt);
368 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
369 	    groups, fmt, vargs);
370 	va_end(vargs);
371 	return dev;
372 }
373 
374 static inline bool
375 device_is_registered(struct device *dev)
376 {
377 
378 	return (dev->bsddev != NULL);
379 }
380 
381 static inline int
382 device_register(struct device *dev)
383 {
384 	device_t bsddev = NULL;
385 	int unit = -1;
386 
387 	if (device_is_registered(dev))
388 		goto done;
389 
390 	if (dev->devt) {
391 		unit = MINOR(dev->devt);
392 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
393 		dev->bsddev_attached_here = false;
394 	} else if (dev->parent == NULL) {
395 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
396 		dev->bsddev_attached_here = false;
397 	} else {
398 		dev->bsddev_attached_here = true;
399 	}
400 	if (bsddev == NULL && dev->parent != NULL) {
401 		bsddev = device_add_child(dev->parent->bsddev,
402 		    dev->class->kobj.name, unit);
403 	}
404 	if (bsddev != NULL) {
405 		if (dev->devt == 0)
406 			dev->devt = makedev(0, device_get_unit(bsddev));
407 		device_set_softc(bsddev, dev);
408 	}
409 	dev->bsddev = bsddev;
410 done:
411 	kobject_init(&dev->kobj, &linux_dev_ktype);
412 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
413 
414 	return (0);
415 }
416 
417 static inline void
418 device_unregister(struct device *dev)
419 {
420 	device_t bsddev;
421 
422 	bsddev = dev->bsddev;
423 	dev->bsddev = NULL;
424 
425 	if (bsddev != NULL && dev->bsddev_attached_here) {
426 		mtx_lock(&Giant);
427 		device_delete_child(device_get_parent(bsddev), bsddev);
428 		mtx_unlock(&Giant);
429 	}
430 	put_device(dev);
431 }
432 
433 static inline void
434 device_del(struct device *dev)
435 {
436 	device_t bsddev;
437 
438 	bsddev = dev->bsddev;
439 	dev->bsddev = NULL;
440 
441 	if (bsddev != NULL && dev->bsddev_attached_here) {
442 		mtx_lock(&Giant);
443 		device_delete_child(device_get_parent(bsddev), bsddev);
444 		mtx_unlock(&Giant);
445 	}
446 }
447 
448 struct device *device_create(struct class *class, struct device *parent,
449 	    dev_t devt, void *drvdata, const char *fmt, ...);
450 
451 static inline void
452 device_destroy(struct class *class, dev_t devt)
453 {
454 	device_t bsddev;
455 	int unit;
456 
457 	unit = MINOR(devt);
458 	bsddev = devclass_get_device(class->bsdclass, unit);
459 	if (bsddev != NULL)
460 		device_unregister(device_get_softc(bsddev));
461 }
462 
463 static inline void
464 linux_class_kfree(struct class *class)
465 {
466 
467 	kfree(class);
468 }
469 
470 static inline struct class *
471 class_create(struct module *owner, const char *name)
472 {
473 	struct class *class;
474 	int error;
475 
476 	class = kzalloc(sizeof(*class), M_WAITOK);
477 	class->owner = owner;
478 	class->name = name;
479 	class->class_release = linux_class_kfree;
480 	error = class_register(class);
481 	if (error) {
482 		kfree(class);
483 		return (NULL);
484 	}
485 
486 	return (class);
487 }
488 
489 static inline void
490 class_destroy(struct class *class)
491 {
492 
493 	if (class == NULL)
494 		return;
495 	class_unregister(class);
496 }
497 
498 static inline int
499 device_create_file(struct device *dev, const struct device_attribute *attr)
500 {
501 
502 	if (dev)
503 		return sysfs_create_file(&dev->kobj, &attr->attr);
504 	return -EINVAL;
505 }
506 
507 static inline void
508 device_remove_file(struct device *dev, const struct device_attribute *attr)
509 {
510 
511 	if (dev)
512 		sysfs_remove_file(&dev->kobj, &attr->attr);
513 }
514 
515 static inline int
516 class_create_file(struct class *class, const struct class_attribute *attr)
517 {
518 
519 	if (class)
520 		return sysfs_create_file(&class->kobj, &attr->attr);
521 	return -EINVAL;
522 }
523 
524 static inline void
525 class_remove_file(struct class *class, const struct class_attribute *attr)
526 {
527 
528 	if (class)
529 		sysfs_remove_file(&class->kobj, &attr->attr);
530 }
531 
532 static inline int
533 dev_to_node(struct device *dev)
534 {
535                 return -1;
536 }
537 
538 char *kvasprintf(gfp_t, const char *, va_list);
539 char *kasprintf(gfp_t, const char *, ...);
540 
541 #endif	/* _LINUX_DEVICE_H_ */
542