Skip to contentSkip to conversation

Improve the experience with USB gadgets

PATCH In review Miquel Raynal 2 participants 6 messages Oct 10, 2023 – Nov 21, 2023 USB

lore
Share & tools
lore
#0 lore raw

v3 cover Improve the experience with USB gadgets

Try to ease the use of USB gadgets for people not fully aware of all the terms and constraints. With more helpful error messages a little bit of guidance anyone should be able to adapt to the bind/unbind game that is now necessary with USB gadgets.

The idea is to eventually get proper USB composite gadget support, but that is a longer term goal.

Changes in v3:
* First patch is new.
* Collected the tags received.
* Imply CMD_DM from CMD_BIND.

Miquel Raynal (3):
cmd: Change the dependencies between CMD_BIND and USB_GADGET
cmd: bind: Try to improve the (un)bind help
usb: udc: Try to clarify an error message

 cmd/Kconfig                       |  2 +-
 cmd/bind.c                        |  4 ++++
 drivers/usb/gadget/Kconfig        |  1 +
 drivers/usb/gadget/udc/udc-core.c | 13 ++++++++++++-
 4 files changed, 18 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 cmd: Change the dependencies between CMD_BIND and USB_GADGET

Today CMD_BIND defaults to 'y' when USB_ETHER is enabled. In practice, CMD_BIND should default to 'y' when any USB gadget is enabled not only USB_ETHER. Let's invert the logic of the dependency and use the weak 'imply' keyword to enforce this.

---
 cmd/Kconfig                | 1 -
 drivers/usb/gadget/Kconfig | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
cmd/Kconfig +0 -1
index 43ca10f69cc..6cc3bf6c2d0 100644
@@ -996,7 +996,6 @@ config CMD_BCB
 config CMD_BIND
 	bool "bind/unbind - Bind or unbind a device to/from a driver"
 	depends on DM
-	default y if USB_ETHER
 	help
 	  Bind or unbind a device to/from a driver from the command line.
 	  This is useful in situations where a device may be handled by several
drivers/usb/gadget/Kconfig +1 -0
index 1cfe6022842..44f47a07207 100644
@@ -17,6 +17,7 @@ menuconfig USB_GADGET
 	bool "USB Gadget Support"
 	depends on DM
 	select DM_USB
+	imply CMD_BIND
 	help
 	   USB is a master/slave protocol, organized with one master
 	   host (such as a PC) controlling up to 127 peripheral devices.
Signature · 1 line
2.34.1
Reviewed-by Tested-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] cmd: Change the dependencies between CMD_BIND and USB_GADGET

Quoted 6 lines · Miquel Raynal

Today CMD_BIND defaults to 'y' when USB_ETHER is enabled. In practice, CMD_BIND should default to 'y' when any USB gadget is enabled not only USB_ETHER. Let's invert the logic of the dependency and use the weak 'imply' keyword to enforce this.

Tested that the bind command exists when building with:
configs/khadas-vim3_android_defconfig

Quoted 31 lines
---
 cmd/Kconfig                | 1 -
 drivers/usb/gadget/Kconfig | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
cmd/Kconfig +0 -1
index 43ca10f69cc..6cc3bf6c2d0 100644
@@ -996,7 +996,6 @@ config CMD_BCB
 config CMD_BIND
 	bool "bind/unbind - Bind or unbind a device to/from a driver"
 	depends on DM
-	default y if USB_ETHER
 	help
 	  Bind or unbind a device to/from a driver from the command line.
 	  This is useful in situations where a device may be handled by several
drivers/usb/gadget/Kconfig +1 -0
index 1cfe6022842..44f47a07207 100644
@@ -17,6 +17,7 @@ menuconfig USB_GADGET
 	bool "USB Gadget Support"
 	depends on DM
 	select DM_USB
+	imply CMD_BIND
 	help
 	   USB is a master/slave protocol, organized with one master
 	   host (such as a PC) controlling up to 127 peripheral devices.
Signature · 1 line
2.34.1
#3 lore raw

PATCH v3 2/3 cmd: bind: Try to improve the (un)bind help

In reply to Miquel Raynal

While it may sound totally obvious for the regular U-Boot developer to get the parameters of the bind/unbind commands from the output of 'dm tree', it did not felt straightforward to me until I was explicitly told to look there. And even when I knew the command, I did not make a direct link between the arguments of this command and the columns returned by 'dm tree'.

Several of us lost a lot of time because of that, I would like to kindly help other users by slightly improving this textual line. Unfortunately, because of how this string is used (like within the 'help' command) I cannot detail much more, but at least the pointer is there.

While we add this message, we can also imply CMD_DM when we enable CMD_BIND so the debug message does not lead to an unknown command. This way the 'dm' command will likely be there unless explicitly disabled.

---
 cmd/Kconfig | 1 +
 cmd/bind.c  | 4 ++++
 2 files changed, 5 insertions(+)
cmd/Kconfig +1 -0
index 6cc3bf6c2d0..668411fd87c 100644
@@ -996,6 +996,7 @@ config CMD_BCB
 config CMD_BIND
 	bool "bind/unbind - Bind or unbind a device to/from a driver"
 	depends on DM
+	imply CMD_DM
 	help
 	  Bind or unbind a device to/from a driver from the command line.
 	  This is useful in situations where a device may be handled by several
cmd/bind.c +4 -0
index 4d1b7885e60..be0d4d2a711 100644
@@ -246,6 +246,8 @@ U_BOOT_CMD(
 	"Bind a device to a driver",
 	"<node path> <driver>\n"
 	"bind <class> <index> <driver>\n"
+	"Use 'dm tree' to list all devices registered in the driver model,\n"
+	"their path, class, index and current driver.\n"
 );
 
 U_BOOT_CMD(
@@ -254,4 +256,6 @@ U_BOOT_CMD(
 	"<node path>\n"
 	"unbind <class> <index>\n"
 	"unbind <class> <index> <driver>\n"
+	"Use 'dm tree' to list all devices registered in the driver model,\n"
+	"their path, class, index and current driver.\n"
 );
Signature · 1 line
2.34.1
#4 lore raw

PATCH v3 3/3 usb: udc: Try to clarify an error message

In reply to Miquel Raynal

At some point when trying to use USB gadgets, two situations may arise and lead to a failure. Either the UDC (USB Device Controller) is not available at all (not described or not probed) or the UDC is already in use. For instance, as the USB Ethernet gadget remains bound to the UDC, the use of any other USB gadget (fastboot, dfu, etc) *after* will always fail with the "couldn't find an available UDC" error.

Let's give a more helpful message by making a difference between the two cases. Let's also hint people who would get this error and grep it into the sources a better explanation of what's wrong with their workflow.

---

While doing this I really wanted to add "much more" error comments but I faced another reality: often the messages are there but use pr_err/log_err which is actually silenced by default with LOGLEVEL=3, so I consider this unnecessary, as decreasing the loglevel will make these messages appear. I would have expected errors to be displayed, but I understand it makes the binaries even bigger.

---
 drivers/usb/gadget/udc/udc-core.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/usb/gadget/udc/udc-core.c +12 -1
index 7f73926cb3e..8405b03462e 100644
@@ -323,6 +323,7 @@ err1:
 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
 {
 	struct usb_udc		*udc = NULL;
+	unsigned int		udc_count = 0;
 	int			ret;
 
 	if (!driver || !driver->bind || !driver->setup)
@@ -330,12 +331,22 @@ int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
 
 	mutex_lock(&udc_lock);
 	list_for_each_entry(udc, &udc_list, list) {
+		udc_count++;
+
 		/* For now we take the first one */
 		if (!udc->driver)
 			goto found;
 	}
 
-	printf("couldn't find an available UDC\n");
+	if (!udc_count)
+		printf("No UDC available in the system\n");
+	else
+		/* When this happens, users should 'unbind <class> <index>'
+		 * using the output of 'dm tree' and looking at the line right
+		 * after the USB peripheral/device controller.
+		 */
+		printf("All UDCs in use (%d available), use the unbind command\n",
+		       udc_count);
 	mutex_unlock(&udc_lock);
 	return -ENODEV;
 found:
Signature · 1 line
2.34.1
#5 lore raw
In reply to Miquel Raynal

Hi,

Quoted 9 lines · Miquel Raynal

Try to ease the use of USB gadgets for people not fully aware of all the terms and constraints. With more helpful error messages a little bit of guidance anyone should be able to adapt to the bind/unbind game that is now necessary with USB gadgets.

The idea is to eventually get proper USB composite gadget support, but that is a longer term goal.

[...]

Thanks, Applied to https://source.denx.de/u-boot/custodians/u-boot-dfu (u-boot-dfu-next)

[1/3] cmd: Change the dependencies between CMD_BIND and USB_GADGET

      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/9b63fcaec664780545318d923063bedd898a629c
[2/3] cmd: bind: Try to improve the (un)bind help
      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/249a75d8e82b422639beedca3d7d945cd78869ba
[3/3] usb: udc: Try to clarify an error message
      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/8a0d07807abb5370fe879321c7f1d22fdda3255f

--
Mattijs