Skip to contentSkip to conversation

cmd: ums: Set Device Descriptor iSerial based on serial# env variable

PATCH In review Marek Vasut 3 participants 4 messages Jun 26 – Jul 22 USB

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cmd: ums: Set Device Descriptor iSerial based on serial# env variable

The DFU code currently sets the content of "serial#" environment variable into Device Descriptor iSerial field. This is useful when there are multiple devices connected to the same host PC and they have to be told apart. Replicate the same behavior in the UMS code.

Example without serial# set, which is the original behavior:
"
u-boot=> ums 0 mmc 1
UMS: LUN 0, dev mmc 1, hwpart 0, sector 0x0, count 0x1d5a000

$ dmesg -w
...
usb 5-2.3.7: New USB device found, idVendor=0525, idProduct=a4a5, bcdDevice=7e.a7
usb 5-2.3.7: New USB device strings: Mfr=1, Product=2, SerialNumber=0
usb 5-2.3.7: Product: USB download gadget
usb 5-2.3.7: Manufacturer: Vendor
                            <------------------------------- NOT HERE
usb-storage 5-2.3.7:1.0: USB Mass Storage device detected
...
$ lsusb -vd 0525:a4a5

Bus 005 Device 051: ID 0525:a4a5 Netchip Technology, Inc. Linux-USB File-backed Storage Gadget Negotiated speed: High Speed (480Mbps)
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00

  bDeviceClass            0 [unknown]
  bDeviceSubClass         0 [unknown]
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0x0525 Netchip Technology, Inc.
  idProduct          0xa4a5 Linux-USB File-backed Storage Gadget
  bcdDevice           7e.a7
  iManufacturer           1 Vendor
  iProduct                2 USB download gadget
  iSerial                 0 <------------------------------- NOT HERE

...
"

Example with serial# set to 1234abcd, which is the optional new behavior:
"
u-boot=> setenv serial# 1234abcd <------------------------------ HERE u-boot=> ums 0 mmc 1
UMS: LUN 0, dev mmc 1, hwpart 0, sector 0x0, count 0x1d5a000

$ dmesg -w
...
usb 5-2.3.7: New USB device found, idVendor=0525, idProduct=a4a5, bcdDevice=7e.a7
usb 5-2.3.7: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 5-2.3.7: Product: USB download gadget
usb 5-2.3.7: Manufacturer: Vendor
usb 5-2.3.7: SerialNumber: 1234abcd <--------------------------- HERE
usb-storage 5-2.3.7:1.0: USB Mass Storage device detected
...
$ lsusb -vd 0525:a4a5

Bus 005 Device 052: ID 0525:a4a5 Netchip Technology, Inc. Linux-USB File-backed Storage Gadget Negotiated speed: High Speed (480Mbps)
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00

  bDeviceClass            0 [unknown]
  bDeviceSubClass         0 [unknown]
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0x0525 Netchip Technology, Inc.
  idProduct          0xa4a5 Linux-USB File-backed Storage Gadget
  bcdDevice           7e.a7
  iManufacturer           1 Vendor
  iProduct                2 USB download gadget
  iSerial                 3 1234abcd <-------------------------- HERE

...
"

---
Cc: Lukasz Majewski <[email protected]>
Cc: Mattijs Korpershoek <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
---
V2: Move to fsg_bind()

---
 drivers/usb/gadget/f_mass_storage.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/usb/gadget/f_mass_storage.c +8 -0
index 71dc58da3f0..f82acd10b45 100644
@@ -241,6 +241,7 @@
 
 #include <config.h>
 #include <div64.h>
+#include <env.h>
 #include <hexdump.h>
 #include <log.h>
 #include <malloc.h>
@@ -2651,8 +2652,15 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
 	struct usb_gadget	*gadget = c->cdev->gadget;
 	int			i;
 	struct usb_ep		*ep;
+	char __maybe_unused	*sn;
 	fsg->gadget = gadget;
 
+	if (CONFIG_IS_ENABLED(ENV_SUPPORT)) {
+		sn = env_get("serial#");
+		if (sn)
+			g_dnl_set_serialnumber(sn);
+	}
+
 	/* New interface */
 	i = usb_interface_id(c, f);
 	if (i < 0)
Signature · 1 line
2.53.0
Reviewed-by on v2 1/1
#1 lore raw

Hi Marek,

Thank you for the patch.

Quoted 75 lines · Marek Vasut

The DFU code currently sets the content of "serial#" environment variable into Device Descriptor iSerial field. This is useful when there are multiple devices connected to the same host PC and they have to be told apart. Replicate the same behavior in the UMS code.

Example without serial# set, which is the original behavior:
"
u-boot=> ums 0 mmc 1
UMS: LUN 0, dev mmc 1, hwpart 0, sector 0x0, count 0x1d5a000

$ dmesg -w
...
usb 5-2.3.7: New USB device found, idVendor=0525, idProduct=a4a5, bcdDevice=7e.a7
usb 5-2.3.7: New USB device strings: Mfr=1, Product=2, SerialNumber=0
usb 5-2.3.7: Product: USB download gadget
usb 5-2.3.7: Manufacturer: Vendor
                            <------------------------------- NOT HERE
usb-storage 5-2.3.7:1.0: USB Mass Storage device detected
...
$ lsusb -vd 0525:a4a5

Bus 005 Device 051: ID 0525:a4a5 Netchip Technology, Inc. Linux-USB File-backed Storage Gadget Negotiated speed: High Speed (480Mbps)
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00

  bDeviceClass            0 [unknown]
  bDeviceSubClass         0 [unknown]
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0x0525 Netchip Technology, Inc.
  idProduct          0xa4a5 Linux-USB File-backed Storage Gadget
  bcdDevice           7e.a7
  iManufacturer           1 Vendor
  iProduct                2 USB download gadget
  iSerial                 0 <------------------------------- NOT HERE

...
"

Example with serial# set to 1234abcd, which is the optional new behavior:
"
u-boot=> setenv serial# 1234abcd <------------------------------ HERE u-boot=> ums 0 mmc 1
UMS: LUN 0, dev mmc 1, hwpart 0, sector 0x0, count 0x1d5a000

$ dmesg -w
...
usb 5-2.3.7: New USB device found, idVendor=0525, idProduct=a4a5, bcdDevice=7e.a7
usb 5-2.3.7: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 5-2.3.7: Product: USB download gadget
usb 5-2.3.7: Manufacturer: Vendor
usb 5-2.3.7: SerialNumber: 1234abcd <--------------------------- HERE
usb-storage 5-2.3.7:1.0: USB Mass Storage device detected
...
$ lsusb -vd 0525:a4a5

Bus 005 Device 052: ID 0525:a4a5 Netchip Technology, Inc. Linux-USB File-backed Storage Gadget Negotiated speed: High Speed (480Mbps)
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00

  bDeviceClass            0 [unknown]
  bDeviceSubClass         0 [unknown]
  bDeviceProtocol         0
  bMaxPacketSize0        64
  idVendor           0x0525 Netchip Technology, Inc.
  idProduct          0xa4a5 Linux-USB File-backed Storage Gadget
  bcdDevice           7e.a7
  iManufacturer           1 Vendor
  iProduct                2 USB download gadget
  iSerial                 3 1234abcd <-------------------------- HERE
Quoted 4 lines

...
"

I looked at the linux implementation and I could not find anything related to the serial number in drivers/usb/gadget/function/f_mass_storage.c.
I think in linux it's handled by drivers/usb/gadget/configfs.c, therefore we can't just do a straight port from linux.

This looks good to me.

#2 lore raw
In reply to Marek Vasut

Hi,

Quoted 11 lines · Marek Vasut

The DFU code currently sets the content of "serial#" environment variable into Device Descriptor iSerial field. This is useful when there are multiple devices connected to the same host PC and they have to be told apart. Replicate the same behavior in the UMS code.

Example without serial# set, which is the original behavior:
"
u-boot=> ums 0 mmc 1
UMS: LUN 0, dev mmc 1, hwpart 0, sector 0x0, count 0x1d5a000

[...]

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

[1/1] cmd: ums: Set Device Descriptor iSerial based on serial# env variable

      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/3caa8f42e1f657177780be5a445502cbb9181e43

--
Mattijs

#3 lore raw
Quoted 13 lines · Mattijs Korpershoek via U-Boot

Hi,

Quoted 10 lines · Marek Vasut

The DFU code currently sets the content of "serial#" environment variable into Device Descriptor iSerial field. This is useful when there are multiple devices connected to the same host PC and they have to be told apart. Replicate the same behavior in the UMS code.

Example without serial# set, which is the original behavior:
"
u-boot=> ums 0 mmc 1
UMS: LUN 0, dev mmc 1, hwpart 0, sector 0x0, count 0x1d5a000

Quoted 6 lines
Quoted 1 line

[...]

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

[1/1] cmd: ums: Set Device Descriptor iSerial based on serial# env variable

      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/3caa8f42e1f657177780be5a445502cbb9181e43

Err, I forgot to update my automation. This patch has been applied to the new source location:
https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu.git

See: https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/3caa8f42e1f657177780be5a445502cbb9181e43

Quoted 3 lines

--
Mattijs