We do not have Linux kobject with attributes in barebox, but we do
have something roughly equivalent: struct device and device parameters,
which enables us to do things like eth0.mode=dhcp on the shell.

The parameter API is fairly expressive and would be very useful for
transferring data between commands. In preparation for doing that,
introduce struct bobject, which will allow device-less parameters in
the future. We keep it in a union with the name, so we do not have to
patch all instances of dev->name across the tree.

Signed-off-by: Ahmad Fatoum <a.fat...@barebox.org>
---
 drivers/base/driver.c | 32 --------------------------------
 include/bobject.h     | 26 ++++++++++++++++++++++++++
 include/device.h      |  9 +++++++--
 lib/Makefile          |  1 +
 lib/bobject.c         | 36 ++++++++++++++++++++++++++++++++++++
 5 files changed, 70 insertions(+), 34 deletions(-)
 create mode 100644 include/bobject.h
 create mode 100644 lib/bobject.c

diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 21bf88a74e50..4f3ae4909e07 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -621,38 +621,6 @@ int generic_memmap_ro(struct cdev *cdev, void **map, int 
flags)
        return generic_memmap_rw(cdev, map, flags);
 }
 
-/**
- * dev_set_name - set a device name
- * @dev: device
- * @fmt: format string for the device's name
- *
- * NOTE: This function expects dev->name to be free()-able, so extra
- * precautions needs to be taken when mixing its usage with manual
- * assignement of device.name.
- */
-int dev_set_name(struct device *dev, const char *fmt, ...)
-{
-       va_list vargs;
-       int err;
-       /*
-        * Save old pointer in case we are overriding already set name
-        */
-       char *oldname = dev->name;
-
-       va_start(vargs, fmt);
-       err = vasprintf(&dev->name, fmt, vargs);
-       va_end(vargs);
-
-       /*
-        * Free old pointer, we do this after vasprintf call in case
-        * old device name was in one of vargs
-        */
-       free(oldname);
-
-       return WARN_ON(err < 0) ? err : 0;
-}
-EXPORT_SYMBOL_GPL(dev_set_name);
-
 /**
  * dev_add_alias - add alias for device
  * @dev: device
diff --git a/include/bobject.h b/include/bobject.h
new file mode 100644
index 000000000000..783ab64569dc
--- /dev/null
+++ b/include/bobject.h
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __BOBJECT_H_
+#define __BOBJECT_H_
+
+#include <linux/compiler.h>
+
+/**
+ * struct bobject - barebox object
+ * @name: name of object (must be first member)
+ */
+struct bobject {
+       char                    *name;
+};
+
+struct device;
+
+typedef union {
+       struct bobject *bobj;
+       struct device *dev;
+} bobject_t __attribute__((transparent_union));
+
+
+__printf(2, 3) int bobject_set_name(bobject_t bobj, const char *name, ...);
+
+#endif
diff --git a/include/device.h b/include/device.h
index 7ef807f7d6b1..8b65693d9672 100644
--- a/include/device.h
+++ b/include/device.h
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 #include <linux/list.h>
 #include <init.h>
+#include <bobject.h>
 
 enum dev_dma_coherence {
        DEV_DMA_COHERENCE_DEFAULT = 0,
@@ -26,6 +27,7 @@ struct of_device_id;
 
 /**
  * struct device - The basic device structure
+ * @bobject: base class barebox object
  * @name: This member is used to match with a driver. This is a
  *        descriptive name and could be MPC5XXX_ether or imx_serial.
  *        Unless absolutely necessary, should not be modified
@@ -63,7 +65,10 @@ struct of_device_id;
  * @deferred_probe_reason: If a driver probe is deferred, this stores the last 
error.
  */
 struct device {
-       char *name;
+       union {
+               struct bobject bobject;
+               char *name;
+       };
 
        char *unique_name;
        int id;
@@ -187,7 +192,7 @@ static inline const char *dev_name(const struct device *dev)
        return dev_id(dev) ?: dev->name;
 }
 
-int dev_set_name(struct device *dev, const char *fmt, ...) __printf(2, 3);
+#define dev_set_name bobject_set_name
 
 static inline bool dev_is_dma_coherent(struct device *dev)
 {
diff --git a/lib/Makefile b/lib/Makefile
index 9592ccb68312..13f1de142008 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
+obj-y                  += bobject.o
 obj-y                  += bcd.o
 obj-y                  += term.o
 obj-$(CONFIG_BOOTSTRAP)        += bootstrap/
diff --git a/lib/bobject.c b/lib/bobject.c
new file mode 100644
index 000000000000..a770993b90bd
--- /dev/null
+++ b/lib/bobject.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <bobject.h>
+#include <stdio.h>
+
+/**
+ * bobject_set_name - set a barebox object's name
+ * @bobj: barebox object or device
+ * @fmt: format string for the object's name
+ *
+ * NOTE: This function expects bobj->name to be free()-able, so extra
+ * precautions needs to be taken when mixing its usage with manual
+ * assignement of bobject.name.
+ */
+int bobject_set_name(bobject_t bobj, const char *fmt, ...)
+{
+       va_list vargs;
+       int err;
+       /*
+        * Save old pointer in case we are overriding already set name
+        */
+       char *oldname = bobj.bobj->name;
+
+       va_start(vargs, fmt);
+       err = vasprintf(&bobj.bobj->name, fmt, vargs);
+       va_end(vargs);
+
+       /*
+        * Free old pointer, we do this after vasprintf call in case
+        * old device name was in one of vargs
+        */
+       free(oldname);
+
+       return WARN_ON(err < 0) ? err : 0;
+}
+EXPORT_SYMBOL_GPL(bobject_set_name);
-- 
2.39.5


Reply via email to