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