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