[PATCH 5/6] usb: otg: turn global otg device into device alias

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Feb 28 08:05:17 PST 2024


barebox supports manual switching of USB OTG ports to either peripheral
or host via the otgX.mode={host,peripheral} device parameter.

When support for configuring multiple OTG ports was added, the old
singleton otg device was kept, as not to break existing scripts.

Since then, barebox has gained support for device aliases, which are a
light weight mechanism to resolve device names and point at a different
device. This is exactly what's required here, so make use of it.

Cc: Jules Maselbas <jmaselbas at zdiv.net>
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 drivers/usb/otg/otgdev.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/otg/otgdev.c b/drivers/usb/otg/otgdev.c
index e035c988af2d..b4315c2d3c28 100644
--- a/drivers/usb/otg/otgdev.c
+++ b/drivers/usb/otg/otgdev.c
@@ -58,11 +58,6 @@ static int register_otg_device(struct device *dev, struct otg_mode *otg)
 	return PTR_ERR_OR_ZERO(param_mode);
 }
 
-static struct device otg_device = {
-	.name = "otg",
-	.id = DEVICE_ID_SINGLE,
-};
-
 struct bus_type otg_bus_type = {
 	.name = "usbotg" /* "otg" is already taken for the alias */
 };
@@ -70,6 +65,7 @@ struct bus_type otg_bus_type = {
 int usb_register_otg_device(struct device *parent,
 			    int (*set_mode)(void *ctx, enum usb_dr_mode mode), void *ctx)
 {
+	bool first_otg = list_empty(&otg_bus_type.device_list);
 	int ret;
 	struct otg_mode *otg;
 
@@ -85,15 +81,15 @@ int usb_register_otg_device(struct device *parent,
 	otg->set_mode_callback = set_mode;
 	otg->ctx = ctx;
 
-	/* register otg.mode as an alias of otg0.mode */
-	if (otg_device.parent == NULL) {
-		otg_device.parent = parent;
-		ret = register_otg_device(&otg_device, otg);
-		if (ret)
-			return ret;
-	}
+	ret = register_otg_device(&otg->dev, otg);
+	if (ret)
+		return ret;
 
-	return register_otg_device(&otg->dev, otg);
+	/* register otg.mode as an alias of otg0.mode */
+	if (first_otg)
+		dev_add_alias(&otg->dev, "otg");
+
+	return 0;
 }
 
 static int otg_bus_init(void)
-- 
2.39.2




More information about the barebox mailing list