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