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