[PATCH v2 20/20] include: add linux/device.h wrapper around driver.h

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Nov 22 09:29:51 PST 2023


We don't have device-scoped resource control and in practice memory
allocated in device functions is only freed when barebox terminates, so
let's accept the fact that most code is leaky and implement devm_
as literal leaky abstractions.

While at it provide some macros to translate Linux API into equivalent
barebox API.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
v1 -> v2:
  - unchanged
---
 include/linux/device.h | 66 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)
 create mode 100644 include/linux/device.h

diff --git a/include/linux/device.h b/include/linux/device.h
new file mode 100644
index 000000000000..0bd61154ac30
--- /dev/null
+++ b/include/linux/device.h
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef LINUX_DRIVER_H_
+#define LINUX_DRIVER_H_
+
+#include <driver.h>
+#include <linux/slab.h>
+#include <linux/bug.h>
+#include <mmu.h>
+
+#define device_driver driver
+
+#define __devm_wrapper(fn, dev, ...) ({ BUG_ON(!dev); fn(__VA_ARGS__); })
+
+#define devm_kmalloc(...)		__devm_wrapper(kmalloc, __VA_ARGS__)
+#define devm_krealloc(...)		__devm_wrapper(krealloc, __VA_ARGS__)
+#define devm_kvasprintf(...)		__devm_wrapper(kvasprintf, __VA_ARGS__)
+#define devm_kasprintf(...)		__devm_wrapper(kasprintf, __VA_ARGS__)
+#define devm_kzalloc(...)		__devm_wrapper(kzalloc, __VA_ARGS__)
+#define devm_kmalloc_array(...)		__devm_wrapper(kmalloc_array, __VA_ARGS__)
+#define devm_kcalloc(...)		__devm_wrapper(kcalloc, __VA_ARGS__)
+#define devm_krealloc_array(...)	__devm_wrapper(krealloc_array, __VA_ARGS__)
+#define devm_kfree(...)			__devm_wrapper(kfree, __VA_ARGS__)
+#define devm_kstrdup(...)		__devm_wrapper(kstrdup, __VA_ARGS__)
+#define devm_kstrdup_const(...)		__devm_wrapper(kstrdup_const, __VA_ARGS__)
+#define devm_kmemdup(...)		__devm_wrapper(kmemdup, __VA_ARGS__)
+#define devm_bitmap_zalloc(dev, nbits, gfp)	\
+	__devm_wrapper(bitmap_zalloc, dev, nbits)
+
+#define device_register register_device
+#define device_unregister unregister_device
+
+#define driver_register register_driver
+#define driver_unregister unregister_driver
+
+static inline void __iomem *devm_ioremap(struct device *dev,
+					 resource_size_t start,
+					 resource_size_t size)
+{
+	if (start)
+		remap_range((void *)start, size, MAP_UNCACHED);
+
+	return IOMEM(start);
+}
+
+static inline int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data,
+				   int (*fn)(struct device *dev, void *data))
+{
+	struct device *dev;
+	int ret;
+
+	bus_for_each_device(bus, dev) {
+		if (start) {
+			if (dev == start)
+				start = NULL;
+			continue;
+		}
+
+		ret = fn(dev, data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+#endif
-- 
2.39.2




More information about the barebox mailing list