xref: /freebsd/sys/compat/linuxkpi/common/include/linux/device.h (revision 263660c061ac76d449cbca7bdd0db2ecdfad76d9)
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	_LINUXKPI_LINUX_DEVICE_H_
32 #define	_LINUXKPI_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/kdev_t.h>
44 #include <linux/backlight.h>
45 #include <linux/pm.h>
46 #include <linux/idr.h>
47 #include <linux/ratelimit.h>	/* via linux/dev_printk.h */
48 #include <asm/atomic.h>
49 
50 #include <sys/bus.h>
51 #include <sys/backlight.h>
52 
53 struct device;
54 struct fwnode_handle;
55 
56 struct class {
57 	const char	*name;
58 	struct module	*owner;
59 	struct kobject	kobj;
60 	devclass_t	bsdclass;
61 	const struct dev_pm_ops *pm;
62 	const struct attribute_group **dev_groups;
63 	void		(*class_release)(struct class *class);
64 	void		(*dev_release)(struct device *dev);
65 	char *		(*devnode)(struct device *dev, umode_t *mode);
66 };
67 
68 struct dev_pm_ops {
69 	int (*prepare)(struct device *dev);
70 	int (*suspend)(struct device *dev);
71 	int (*suspend_late)(struct device *dev);
72 	int (*resume)(struct device *dev);
73 	int (*resume_early)(struct device *dev);
74 	int (*freeze)(struct device *dev);
75 	int (*freeze_late)(struct device *dev);
76 	int (*thaw)(struct device *dev);
77 	int (*thaw_early)(struct device *dev);
78 	int (*poweroff)(struct device *dev);
79 	int (*poweroff_late)(struct device *dev);
80 	int (*restore)(struct device *dev);
81 	int (*restore_early)(struct device *dev);
82 	int (*runtime_suspend)(struct device *dev);
83 	int (*runtime_resume)(struct device *dev);
84 	int (*runtime_idle)(struct device *dev);
85 };
86 
87 struct device_driver {
88 	const char	*name;
89 	const struct dev_pm_ops *pm;
90 };
91 
92 struct device_type {
93 	const char	*name;
94 };
95 
96 struct device {
97 	struct device	*parent;
98 	struct list_head irqents;
99 	device_t	bsddev;
100 	/*
101 	 * The following flag is used to determine if the LinuxKPI is
102 	 * responsible for detaching the BSD device or not. If the
103 	 * LinuxKPI got the BSD device using devclass_get_device(), it
104 	 * must not try to detach or delete it, because it's already
105 	 * done somewhere else.
106 	 */
107 	bool		bsddev_attached_here;
108 	struct device_driver *driver;
109 	struct device_type *type;
110 	dev_t		devt;
111 	struct class	*class;
112 	void		(*release)(struct device *dev);
113 	struct kobject	kobj;
114 	void		*dma_priv;
115 	void		*driver_data;
116 	unsigned int	irq;
117 #define	LINUX_IRQ_INVALID	65535
118 	unsigned int	irq_start;
119 	unsigned int	irq_end;
120 	const struct attribute_group **groups;
121 	struct fwnode_handle *fwnode;
122 	struct cdev	*backlight_dev;
123 	struct backlight_device	*bd;
124 
125 	spinlock_t	devres_lock;
126 	struct list_head devres_head;
127 };
128 
129 extern struct device linux_root_device;
130 extern struct kobject linux_class_root;
131 extern const struct kobj_type linux_dev_ktype;
132 extern const struct kobj_type linux_class_ktype;
133 
134 struct class_attribute {
135 	struct attribute attr;
136 	ssize_t (*show)(struct class *, struct class_attribute *, char *);
137 	ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
138 	const void *(*namespace)(struct class *, const struct class_attribute *);
139 };
140 
141 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
142 	struct class_attribute class_attr_##_name =			\
143 	    { { #_name, NULL, _mode }, _show, _store }
144 
145 struct device_attribute {
146 	struct attribute	attr;
147 	ssize_t			(*show)(struct device *,
148 					struct device_attribute *, char *);
149 	ssize_t			(*store)(struct device *,
150 					struct device_attribute *, const char *,
151 					size_t);
152 };
153 
154 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
155 	struct device_attribute dev_attr_##_name =			\
156 	    __ATTR(_name, _mode, _show, _store)
157 #define	DEVICE_ATTR_RO(_name)						\
158 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
159 #define	DEVICE_ATTR_WO(_name)						\
160 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
161 #define	DEVICE_ATTR_RW(_name)						\
162 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
163 
164 /* Simple class attribute that is just a static string */
165 struct class_attribute_string {
166 	struct class_attribute attr;
167 	char *str;
168 };
169 
170 static inline ssize_t
171 show_class_attr_string(struct class *class,
172 				struct class_attribute *attr, char *buf)
173 {
174 	struct class_attribute_string *cs;
175 	cs = container_of(attr, struct class_attribute_string, attr);
176 	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
177 }
178 
179 /* Currently read-only only */
180 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
181 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
182 #define CLASS_ATTR_STRING(_name, _mode, _str) \
183 	struct class_attribute_string class_attr_##_name = \
184 		_CLASS_ATTR_STRING(_name, _mode, _str)
185 
186 #define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
187 #define	dev_crit(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
188 #define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
189 #define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
190 #define	dev_notice(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
191 #define	dev_dbg(dev, fmt, ...)	do { } while (0)
192 #define	dev_printk(lvl, dev, fmt, ...)					\
193 	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
194 
195 #define	dev_err_once(dev, ...) do {		\
196 	static bool __dev_err_once;		\
197 	if (!__dev_err_once) {			\
198 		__dev_err_once = 1;		\
199 		dev_err(dev, __VA_ARGS__);	\
200 	}					\
201 } while (0)
202 
203 #define	dev_err_ratelimited(dev, ...) do {	\
204 	static linux_ratelimit_t __ratelimited;	\
205 	if (linux_ratelimited(&__ratelimited))	\
206 		dev_err(dev, __VA_ARGS__);	\
207 } while (0)
208 
209 #define	dev_warn_ratelimited(dev, ...) do {	\
210 	static linux_ratelimit_t __ratelimited;	\
211 	if (linux_ratelimited(&__ratelimited))	\
212 		dev_warn(dev, __VA_ARGS__);	\
213 } while (0)
214 
215 /* Public and LinuxKPI internal devres functions. */
216 void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t);
217 void lkpi_devres_add(struct device *, void *);
218 void lkpi_devres_free(void *);
219 void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *),
220     int (*match)(struct device *, void *, void *), void *);
221 int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *),
222     int (*match)(struct device *, void *, void *), void *);
223 #define	devres_alloc(_r, _s, _g)	lkpi_devres_alloc(_r, _s, _g)
224 #define	devres_add(_d, _p)		lkpi_devres_add(_d, _p)
225 #define	devres_free(_p)			lkpi_devres_free(_p)
226 #define	devres_find(_d, _rfn, _mfn, _mp) \
227 					lkpi_devres_find(_d, _rfn, _mfn, _mp)
228 #define	devres_destroy(_d, _rfn, _mfn, _mp) \
229 					lkpi_devres_destroy(_d, _rfn, _mfn, _mp)
230 void lkpi_devres_release_free_list(struct device *);
231 void lkpi_devres_unlink(struct device *, void *);
232 void lkpi_devm_kmalloc_release(struct device *, void *);
233 
234 static inline const char *
235 dev_driver_string(const struct device *dev)
236 {
237 	driver_t *drv;
238 	const char *str = "";
239 
240 	if (dev->bsddev != NULL) {
241 		drv = device_get_driver(dev->bsddev);
242 		if (drv != NULL)
243 			str = drv->name;
244 	}
245 
246 	return (str);
247 }
248 
249 static inline void *
250 dev_get_drvdata(const struct device *dev)
251 {
252 
253 	return dev->driver_data;
254 }
255 
256 static inline void
257 dev_set_drvdata(struct device *dev, void *data)
258 {
259 
260 	dev->driver_data = data;
261 }
262 
263 static inline struct device *
264 get_device(struct device *dev)
265 {
266 
267 	if (dev)
268 		kobject_get(&dev->kobj);
269 
270 	return (dev);
271 }
272 
273 static inline char *
274 dev_name(const struct device *dev)
275 {
276 
277 	return kobject_name(&dev->kobj);
278 }
279 
280 #define	dev_set_name(_dev, _fmt, ...)					\
281 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
282 
283 static inline void
284 put_device(struct device *dev)
285 {
286 
287 	if (dev)
288 		kobject_put(&dev->kobj);
289 }
290 
291 static inline int
292 class_register(struct class *class)
293 {
294 
295 	class->bsdclass = devclass_create(class->name);
296 	kobject_init(&class->kobj, &linux_class_ktype);
297 	kobject_set_name(&class->kobj, class->name);
298 	kobject_add(&class->kobj, &linux_class_root, class->name);
299 
300 	return (0);
301 }
302 
303 static inline void
304 class_unregister(struct class *class)
305 {
306 
307 	kobject_put(&class->kobj);
308 }
309 
310 static inline struct device *kobj_to_dev(struct kobject *kobj)
311 {
312 	return container_of(kobj, struct device, kobj);
313 }
314 
315 /*
316  * Devices are registered and created for exporting to sysfs. Create
317  * implies register and register assumes the device fields have been
318  * setup appropriately before being called.
319  */
320 static inline void
321 device_initialize(struct device *dev)
322 {
323 	device_t bsddev = NULL;
324 	int unit = -1;
325 
326 	if (dev->devt) {
327 		unit = MINOR(dev->devt);
328 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
329 		dev->bsddev_attached_here = false;
330 	} else if (dev->parent == NULL) {
331 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
332 		dev->bsddev_attached_here = false;
333 	} else {
334 		dev->bsddev_attached_here = true;
335 	}
336 
337 	if (bsddev == NULL && dev->parent != NULL) {
338 		bsddev = device_add_child(dev->parent->bsddev,
339 		    dev->class->kobj.name, unit);
340 	}
341 
342 	if (bsddev != NULL)
343 		device_set_softc(bsddev, dev);
344 
345 	dev->bsddev = bsddev;
346 	MPASS(dev->bsddev != NULL);
347 	kobject_init(&dev->kobj, &linux_dev_ktype);
348 
349 	spin_lock_init(&dev->devres_lock);
350 	INIT_LIST_HEAD(&dev->devres_head);
351 }
352 
353 static inline int
354 device_add(struct device *dev)
355 {
356 	if (dev->bsddev != NULL) {
357 		if (dev->devt == 0)
358 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
359 	}
360 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
361 
362 	if (dev->groups)
363 		return (sysfs_create_groups(&dev->kobj, dev->groups));
364 
365 	return (0);
366 }
367 
368 static inline void
369 device_create_release(struct device *dev)
370 {
371 	kfree(dev);
372 }
373 
374 static inline struct device *
375 device_create_groups_vargs(struct class *class, struct device *parent,
376     dev_t devt, void *drvdata, const struct attribute_group **groups,
377     const char *fmt, va_list args)
378 {
379 	struct device *dev = NULL;
380 	int retval = -ENODEV;
381 
382 	if (class == NULL || IS_ERR(class))
383 		goto error;
384 
385 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
386 	if (!dev) {
387 		retval = -ENOMEM;
388 		goto error;
389 	}
390 
391 	dev->devt = devt;
392 	dev->class = class;
393 	dev->parent = parent;
394 	dev->groups = groups;
395 	dev->release = device_create_release;
396 	/* device_initialize() needs the class and parent to be set */
397 	device_initialize(dev);
398 	dev_set_drvdata(dev, drvdata);
399 
400 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
401 	if (retval)
402 		goto error;
403 
404 	retval = device_add(dev);
405 	if (retval)
406 		goto error;
407 
408 	return dev;
409 
410 error:
411 	put_device(dev);
412 	return ERR_PTR(retval);
413 }
414 
415 static inline struct device *
416 device_create_with_groups(struct class *class,
417     struct device *parent, dev_t devt, void *drvdata,
418     const struct attribute_group **groups, const char *fmt, ...)
419 {
420 	va_list vargs;
421 	struct device *dev;
422 
423 	va_start(vargs, fmt);
424 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
425 	    groups, fmt, vargs);
426 	va_end(vargs);
427 	return dev;
428 }
429 
430 static inline bool
431 device_is_registered(struct device *dev)
432 {
433 
434 	return (dev->bsddev != NULL);
435 }
436 
437 static inline int
438 device_register(struct device *dev)
439 {
440 	device_t bsddev = NULL;
441 	int unit = -1;
442 
443 	if (device_is_registered(dev))
444 		goto done;
445 
446 	if (dev->devt) {
447 		unit = MINOR(dev->devt);
448 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
449 		dev->bsddev_attached_here = false;
450 	} else if (dev->parent == NULL) {
451 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
452 		dev->bsddev_attached_here = false;
453 	} else {
454 		dev->bsddev_attached_here = true;
455 	}
456 	if (bsddev == NULL && dev->parent != NULL) {
457 		bsddev = device_add_child(dev->parent->bsddev,
458 		    dev->class->kobj.name, unit);
459 	}
460 	if (bsddev != NULL) {
461 		if (dev->devt == 0)
462 			dev->devt = makedev(0, device_get_unit(bsddev));
463 		device_set_softc(bsddev, dev);
464 	}
465 	dev->bsddev = bsddev;
466 done:
467 	kobject_init(&dev->kobj, &linux_dev_ktype);
468 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
469 
470 	sysfs_create_groups(&dev->kobj, dev->class->dev_groups);
471 
472 	return (0);
473 }
474 
475 static inline void
476 device_unregister(struct device *dev)
477 {
478 	device_t bsddev;
479 
480 	sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);
481 
482 	bsddev = dev->bsddev;
483 	dev->bsddev = NULL;
484 
485 	if (bsddev != NULL && dev->bsddev_attached_here) {
486 		bus_topo_lock();
487 		device_delete_child(device_get_parent(bsddev), bsddev);
488 		bus_topo_unlock();
489 	}
490 	put_device(dev);
491 }
492 
493 static inline void
494 device_del(struct device *dev)
495 {
496 	device_t bsddev;
497 
498 	bsddev = dev->bsddev;
499 	dev->bsddev = NULL;
500 
501 	if (bsddev != NULL && dev->bsddev_attached_here) {
502 		bus_topo_lock();
503 		device_delete_child(device_get_parent(bsddev), bsddev);
504 		bus_topo_unlock();
505 	}
506 }
507 
508 struct device *device_create(struct class *class, struct device *parent,
509 	    dev_t devt, void *drvdata, const char *fmt, ...);
510 
511 static inline void
512 device_destroy(struct class *class, dev_t devt)
513 {
514 	device_t bsddev;
515 	int unit;
516 
517 	unit = MINOR(devt);
518 	bsddev = devclass_get_device(class->bsdclass, unit);
519 	if (bsddev != NULL)
520 		device_unregister(device_get_softc(bsddev));
521 }
522 
523 static inline void
524 device_release_driver(struct device *dev)
525 {
526 
527 #if 0
528 	/* This leads to panics. Disable temporarily. Keep to rework. */
529 
530 	/* We also need to cleanup LinuxKPI bits. What else? */
531 	lkpi_devres_release_free_list(dev);
532 	dev_set_drvdata(dev, NULL);
533 	/* Do not call dev->release! */
534 
535 	bus_topo_lock();
536 	if (device_is_attached(dev->bsddev))
537 		device_detach(dev->bsddev);
538 	bus_topo_unlock();
539 #endif
540 }
541 
542 static inline int
543 device_reprobe(struct device *dev)
544 {
545 	int error;
546 
547 	device_release_driver(dev);
548 	bus_topo_lock();
549 	error = device_probe_and_attach(dev->bsddev);
550 	bus_topo_unlock();
551 
552 	return (-error);
553 }
554 
555 #define	dev_pm_set_driver_flags(dev, flags) do { \
556 } while (0)
557 
558 static inline void
559 linux_class_kfree(struct class *class)
560 {
561 
562 	kfree(class);
563 }
564 
565 static inline struct class *
566 class_create(struct module *owner, const char *name)
567 {
568 	struct class *class;
569 	int error;
570 
571 	class = kzalloc(sizeof(*class), M_WAITOK);
572 	class->owner = owner;
573 	class->name = name;
574 	class->class_release = linux_class_kfree;
575 	error = class_register(class);
576 	if (error) {
577 		kfree(class);
578 		return (NULL);
579 	}
580 
581 	return (class);
582 }
583 
584 static inline void
585 class_destroy(struct class *class)
586 {
587 
588 	if (class == NULL)
589 		return;
590 	class_unregister(class);
591 }
592 
593 static inline int
594 device_create_file(struct device *dev, const struct device_attribute *attr)
595 {
596 
597 	if (dev)
598 		return sysfs_create_file(&dev->kobj, &attr->attr);
599 	return -EINVAL;
600 }
601 
602 static inline void
603 device_remove_file(struct device *dev, const struct device_attribute *attr)
604 {
605 
606 	if (dev)
607 		sysfs_remove_file(&dev->kobj, &attr->attr);
608 }
609 
610 static inline int
611 class_create_file(struct class *class, const struct class_attribute *attr)
612 {
613 
614 	if (class)
615 		return sysfs_create_file(&class->kobj, &attr->attr);
616 	return -EINVAL;
617 }
618 
619 static inline void
620 class_remove_file(struct class *class, const struct class_attribute *attr)
621 {
622 
623 	if (class)
624 		sysfs_remove_file(&class->kobj, &attr->attr);
625 }
626 
627 #define	dev_to_node(dev) linux_dev_to_node(dev)
628 #define	of_node_to_nid(node) -1
629 int linux_dev_to_node(struct device *);
630 
631 char *kvasprintf(gfp_t, const char *, va_list);
632 char *kasprintf(gfp_t, const char *, ...);
633 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...);
634 
635 #define	devm_kasprintf(_dev, _gfp, _fmt, ...)			\
636     lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__)
637 
638 static __inline void *
639 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
640 {
641 	void *p;
642 
643 	p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp);
644 	if (p != NULL)
645 		lkpi_devres_add(dev, p);
646 
647 	return (p);
648 }
649 
650 #define	devm_kzalloc(_dev, _size, _gfp)				\
651     devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO)
652 
653 #define	devm_kcalloc(_dev, _sizen, _size, _gfp)			\
654     devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO)
655 
656 #endif	/* _LINUXKPI_LINUX_DEVICE_H_ */
657