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