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