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 #define	dev_dbg_ratelimited(dev, ...) do {	\
239 	static linux_ratelimit_t __ratelimited;	\
240 	if (linux_ratelimited(&__ratelimited))	\
241 		dev_dbg(dev, __VA_ARGS__);	\
242 } while (0)
243 
244 /* Public and LinuxKPI internal devres functions. */
245 void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t);
246 void lkpi_devres_add(struct device *, void *);
247 void lkpi_devres_free(void *);
248 void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *),
249     int (*match)(struct device *, void *, void *), void *);
250 int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *),
251     int (*match)(struct device *, void *, void *), void *);
252 #define	devres_alloc(_r, _s, _g)	lkpi_devres_alloc(_r, _s, _g)
253 #define	devres_add(_d, _p)		lkpi_devres_add(_d, _p)
254 #define	devres_free(_p)			lkpi_devres_free(_p)
255 #define	devres_find(_d, _rfn, _mfn, _mp) \
256 					lkpi_devres_find(_d, _rfn, _mfn, _mp)
257 #define	devres_destroy(_d, _rfn, _mfn, _mp) \
258 					lkpi_devres_destroy(_d, _rfn, _mfn, _mp)
259 void lkpi_devres_release_free_list(struct device *);
260 void lkpi_devres_unlink(struct device *, void *);
261 void lkpi_devm_kmalloc_release(struct device *, void *);
262 
263 static inline const char *
264 dev_driver_string(const struct device *dev)
265 {
266 	driver_t *drv;
267 	const char *str = "";
268 
269 	if (dev->bsddev != NULL) {
270 		drv = device_get_driver(dev->bsddev);
271 		if (drv != NULL)
272 			str = drv->name;
273 	}
274 
275 	return (str);
276 }
277 
278 static inline void *
279 dev_get_drvdata(const struct device *dev)
280 {
281 
282 	return dev->driver_data;
283 }
284 
285 static inline void
286 dev_set_drvdata(struct device *dev, void *data)
287 {
288 
289 	dev->driver_data = data;
290 }
291 
292 static inline struct device *
293 get_device(struct device *dev)
294 {
295 
296 	if (dev)
297 		kobject_get(&dev->kobj);
298 
299 	return (dev);
300 }
301 
302 static inline char *
303 dev_name(const struct device *dev)
304 {
305 
306 	return kobject_name(&dev->kobj);
307 }
308 
309 #define	dev_set_name(_dev, _fmt, ...)					\
310 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
311 
312 static inline void
313 put_device(struct device *dev)
314 {
315 
316 	if (dev)
317 		kobject_put(&dev->kobj);
318 }
319 
320 struct class *class_create(struct module *owner, const char *name);
321 
322 static inline int
323 class_register(struct class *class)
324 {
325 
326 	class->bsdclass = devclass_create(class->name);
327 	kobject_init(&class->kobj, &linux_class_ktype);
328 	kobject_set_name(&class->kobj, class->name);
329 	kobject_add(&class->kobj, &linux_class_root, class->name);
330 
331 	return (0);
332 }
333 
334 static inline void
335 class_unregister(struct class *class)
336 {
337 
338 	kobject_put(&class->kobj);
339 }
340 
341 static inline struct device *kobj_to_dev(struct kobject *kobj)
342 {
343 	return container_of(kobj, struct device, kobj);
344 }
345 
346 struct device *device_create(struct class *class, struct device *parent,
347 	    dev_t devt, void *drvdata, const char *fmt, ...);
348 struct device *device_create_groups_vargs(struct class *class, struct device *parent,
349     dev_t devt, void *drvdata, const struct attribute_group **groups,
350     const char *fmt, va_list args);
351 
352 /*
353  * Devices are registered and created for exporting to sysfs. Create
354  * implies register and register assumes the device fields have been
355  * setup appropriately before being called.
356  */
357 static inline void
358 device_initialize(struct device *dev)
359 {
360 	device_t bsddev = NULL;
361 	int unit = -1;
362 
363 	if (dev->devt) {
364 		unit = MINOR(dev->devt);
365 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
366 		dev->bsddev_attached_here = false;
367 	} else if (dev->parent == NULL) {
368 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
369 		dev->bsddev_attached_here = false;
370 	} else {
371 		dev->bsddev_attached_here = true;
372 	}
373 
374 	if (bsddev == NULL && dev->parent != NULL) {
375 		bsddev = device_add_child(dev->parent->bsddev,
376 		    dev->class->kobj.name, unit);
377 	}
378 
379 	if (bsddev != NULL)
380 		device_set_softc(bsddev, dev);
381 
382 	dev->bsddev = bsddev;
383 	MPASS(dev->bsddev != NULL);
384 	kobject_init(&dev->kobj, &linux_dev_ktype);
385 
386 	spin_lock_init(&dev->devres_lock);
387 	INIT_LIST_HEAD(&dev->devres_head);
388 }
389 
390 static inline int
391 device_add(struct device *dev)
392 {
393 	if (dev->bsddev != NULL) {
394 		if (dev->devt == 0)
395 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
396 	}
397 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
398 
399 	if (dev->groups)
400 		return (sysfs_create_groups(&dev->kobj, dev->groups));
401 
402 	return (0);
403 }
404 
405 static inline void
406 device_create_release(struct device *dev)
407 {
408 	kfree(dev);
409 }
410 
411 static inline struct device *
412 device_create_with_groups(struct class *class,
413     struct device *parent, dev_t devt, void *drvdata,
414     const struct attribute_group **groups, const char *fmt, ...)
415 {
416 	va_list vargs;
417 	struct device *dev;
418 
419 	va_start(vargs, fmt);
420 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
421 	    groups, fmt, vargs);
422 	va_end(vargs);
423 	return dev;
424 }
425 
426 static inline bool
427 device_is_registered(struct device *dev)
428 {
429 
430 	return (dev->bsddev != NULL);
431 }
432 
433 static inline int
434 device_register(struct device *dev)
435 {
436 	device_t bsddev = NULL;
437 	int unit = -1;
438 
439 	if (device_is_registered(dev))
440 		goto done;
441 
442 	if (dev->devt) {
443 		unit = MINOR(dev->devt);
444 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
445 		dev->bsddev_attached_here = false;
446 	} else if (dev->parent == NULL) {
447 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
448 		dev->bsddev_attached_here = false;
449 	} else {
450 		dev->bsddev_attached_here = true;
451 	}
452 	if (bsddev == NULL && dev->parent != NULL) {
453 		bsddev = device_add_child(dev->parent->bsddev,
454 		    dev->class->kobj.name, unit);
455 	}
456 	if (bsddev != NULL) {
457 		if (dev->devt == 0)
458 			dev->devt = makedev(0, device_get_unit(bsddev));
459 		device_set_softc(bsddev, dev);
460 	}
461 	dev->bsddev = bsddev;
462 done:
463 	kobject_init(&dev->kobj, &linux_dev_ktype);
464 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
465 
466 	sysfs_create_groups(&dev->kobj, dev->class->dev_groups);
467 
468 	return (0);
469 }
470 
471 static inline void
472 device_unregister(struct device *dev)
473 {
474 	device_t bsddev;
475 
476 	sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);
477 
478 	bsddev = dev->bsddev;
479 	dev->bsddev = NULL;
480 
481 	if (bsddev != NULL && dev->bsddev_attached_here) {
482 		bus_topo_lock();
483 		device_delete_child(device_get_parent(bsddev), bsddev);
484 		bus_topo_unlock();
485 	}
486 	put_device(dev);
487 }
488 
489 static inline void
490 device_del(struct device *dev)
491 {
492 	device_t bsddev;
493 
494 	bsddev = dev->bsddev;
495 	dev->bsddev = NULL;
496 
497 	if (bsddev != NULL && dev->bsddev_attached_here) {
498 		bus_topo_lock();
499 		device_delete_child(device_get_parent(bsddev), bsddev);
500 		bus_topo_unlock();
501 	}
502 }
503 
504 static inline void
505 device_destroy(struct class *class, dev_t devt)
506 {
507 	device_t bsddev;
508 	int unit;
509 
510 	unit = MINOR(devt);
511 	bsddev = devclass_get_device(class->bsdclass, unit);
512 	if (bsddev != NULL)
513 		device_unregister(device_get_softc(bsddev));
514 }
515 
516 static inline void
517 device_release_driver(struct device *dev)
518 {
519 
520 #if 0
521 	/* This leads to panics. Disable temporarily. Keep to rework. */
522 
523 	/* We also need to cleanup LinuxKPI bits. What else? */
524 	lkpi_devres_release_free_list(dev);
525 	dev_set_drvdata(dev, NULL);
526 	/* Do not call dev->release! */
527 
528 	bus_topo_lock();
529 	if (device_is_attached(dev->bsddev))
530 		device_detach(dev->bsddev);
531 	bus_topo_unlock();
532 #endif
533 }
534 
535 static inline int
536 device_reprobe(struct device *dev)
537 {
538 	int error;
539 
540 	device_release_driver(dev);
541 	bus_topo_lock();
542 	error = device_probe_and_attach(dev->bsddev);
543 	bus_topo_unlock();
544 
545 	return (-error);
546 }
547 
548 static inline void
549 device_set_wakeup_enable(struct device *dev __unused, bool enable __unused)
550 {
551 
552 	/*
553 	 * XXX-BZ TODO This is used by wireless drivers supporting WoWLAN which
554 	 * we currently do not support.
555 	 */
556 }
557 
558 static inline int
559 device_wakeup_enable(struct device *dev)
560 {
561 
562 	device_set_wakeup_enable(dev, true);
563 	return (0);
564 }
565 
566 #define	dev_pm_set_driver_flags(dev, flags) do { \
567 } while (0)
568 
569 static inline void
570 linux_class_kfree(struct class *class)
571 {
572 
573 	kfree(class);
574 }
575 
576 static inline void
577 class_destroy(struct class *class)
578 {
579 
580 	if (class == NULL)
581 		return;
582 	class_unregister(class);
583 }
584 
585 static inline int
586 device_create_file(struct device *dev, const struct device_attribute *attr)
587 {
588 
589 	if (dev)
590 		return sysfs_create_file(&dev->kobj, &attr->attr);
591 	return -EINVAL;
592 }
593 
594 static inline void
595 device_remove_file(struct device *dev, const struct device_attribute *attr)
596 {
597 
598 	if (dev)
599 		sysfs_remove_file(&dev->kobj, &attr->attr);
600 }
601 
602 static inline int
603 class_create_file(struct class *class, const struct class_attribute *attr)
604 {
605 
606 	if (class)
607 		return sysfs_create_file(&class->kobj, &attr->attr);
608 	return -EINVAL;
609 }
610 
611 static inline void
612 class_remove_file(struct class *class, const struct class_attribute *attr)
613 {
614 
615 	if (class)
616 		sysfs_remove_file(&class->kobj, &attr->attr);
617 }
618 
619 #define	dev_to_node(dev) linux_dev_to_node(dev)
620 #define	of_node_to_nid(node) -1
621 int linux_dev_to_node(struct device *);
622 
623 char *kvasprintf(gfp_t, const char *, va_list);
624 char *kasprintf(gfp_t, const char *, ...);
625 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...);
626 
627 #define	devm_kasprintf(_dev, _gfp, _fmt, ...)			\
628     lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__)
629 
630 static __inline void *
631 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
632 {
633 	void *p;
634 
635 	p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp);
636 	if (p != NULL)
637 		lkpi_devres_add(dev, p);
638 
639 	return (p);
640 }
641 
642 static inline void *
643 devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
644 {
645 	void *dst;
646 
647 	if (len == 0)
648 		return (NULL);
649 
650 	dst = devm_kmalloc(dev, len, gfp);
651 	if (dst != NULL)
652 		memcpy(dst, src, len);
653 
654 	return (dst);
655 }
656 
657 #define	devm_kzalloc(_dev, _size, _gfp)				\
658     devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO)
659 
660 #define	devm_kcalloc(_dev, _sizen, _size, _gfp)			\
661     devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO)
662 
663 #endif	/* _LINUXKPI_LINUX_DEVICE_H_ */
664