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