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