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