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