Skip to contentSkip to conversation

usb: gadget: f_mass_storage: Disable eps during disconnect

PATCH In review Mattijs Korpershoek 2 participants 3 messages Jun 17 – Jun 26 USB

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: gadget: f_mass_storage: Disable eps during disconnect

When trying two ums commands in a row, the second one no longer enumerates properly from the host.

This happens since commit
59310d1ecb9f ("usb: gadget: introduce 'enabled' flag in struct usb_ep") causing usb_ep_enable() to return early when ep->enabled is already set.

Gadget function drivers (such as f_fastboot or f_mass_storage) implement a disable() function which is called whenever we are done using the gadget.

Because f_mass_storage driver does not disable the endpoints, ep->enabled will never be set to false again.

This can be reproduced on the STM32MP157C-DK2 or the Khadas VIM3 boards.

Add calls to usb_ep_disable() as done in linux [1] to fix this.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=9fff139aeb11186fd8e75860c959c86cb43ab2f6

Fixes 59310d1ecb9f ("usb: gadget: introduce 'enabled' flag in struct usb_ep")

---
This was initially reported by Patrice Chotard at:
https://lore.kernel.org/all/[email protected]/

That thread contains reproduction steps.
---
Changes in v2:
- Add missing Reported-by to commit message
- Link to v1: https://patch.msgid.link/[email protected]

---
 drivers/usb/gadget/f_mass_storage.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/usb/gadget/f_mass_storage.c +11 -0
index 71dc58da3f03..87ed25e8bb3a 100644
@@ -2275,6 +2275,17 @@ static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 static void fsg_disable(struct usb_function *f)
 {
 	struct fsg_dev *fsg = fsg_from_func(f);
+
+	/* Disable the endpoints */
+	if (fsg->bulk_in_enabled) {
+		usb_ep_disable(fsg->bulk_in);
+		fsg->bulk_in_enabled = 0;
+	}
+	if (fsg->bulk_out_enabled) {
+		usb_ep_disable(fsg->bulk_out);
+		fsg->bulk_out_enabled = 0;
+	}
+
 	fsg->common->new_fsg = NULL;
 	raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
 }

---
base-commit: a7830e87555abfb81cc69275cecb2bc0fbde5b28
change-id: 20260617-ums-disconnect-d839bc17a47d

Best regards,

Signature · 1 line
Mattijs Korpershoek <[email protected]>
Reviewed-by on v2 1/1
#1 lore raw
Quoted 63 lines · Mattijs Korpershoek

When trying two ums commands in a row, the second one no longer enumerates properly from the host.

This happens since commit
59310d1ecb9f ("usb: gadget: introduce 'enabled' flag in struct usb_ep") causing usb_ep_enable() to return early when ep->enabled is already set.

Gadget function drivers (such as f_fastboot or f_mass_storage) implement a disable() function which is called whenever we are done using the gadget.

Because f_mass_storage driver does not disable the endpoints, ep->enabled will never be set to false again.

This can be reproduced on the STM32MP157C-DK2 or the Khadas VIM3 boards.

Add calls to usb_ep_disable() as done in linux [1] to fix this.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=9fff139aeb11186fd8e75860c959c86cb43ab2f6

Fixes 59310d1ecb9f ("usb: gadget: introduce 'enabled' flag in struct usb_ep")

---
This was initially reported by Patrice Chotard at:
https://lore.kernel.org/all/[email protected]/

That thread contains reproduction steps.
---
Changes in v2:
- Add missing Reported-by to commit message
- Link to v1: https://patch.msgid.link/[email protected]

---
 drivers/usb/gadget/f_mass_storage.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/usb/gadget/f_mass_storage.c +11 -1
index 71dc58da3f03..87ed25e8bb3a 100644
@@ -2275,6 +2275,17 @@ static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 static void fsg_disable(struct usb_function *f)
 {
 	struct fsg_dev *fsg = fsg_from_func(f);
+
+	/* Disable the endpoints */
+	if (fsg->bulk_in_enabled) {
+		usb_ep_disable(fsg->bulk_in);
+		fsg->bulk_in_enabled = 0;
+	}
+	if (fsg->bulk_out_enabled) {
+		usb_ep_disable(fsg->bulk_out);
+		fsg->bulk_out_enabled = 0;
+	}
+
 	fsg->common->new_fsg = NULL;
 	raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
 }
 
---

base-commit: a7830e87555abfb81cc69275cecb2bc0fbde5b28
change-id: 20260617-ums-disconnect-d839bc17a47d

Quoted 4 lines

Best regards,

Signature · 1 line
Mattijs Korpershoek <[email protected]>

Hi Mattijs

Tested on STM32MP157c-dk2 board.

Thanks

#2 lore raw
In reply to Mattijs Korpershoek

Hi,

Quoted 8 lines · Mattijs Korpershoek

When trying two ums commands in a row, the second one no longer enumerates properly from the host.

This happens since commit
59310d1ecb9f ("usb: gadget: introduce 'enabled' flag in struct usb_ep") causing usb_ep_enable() to return early when ep->enabled is already set.

[...]

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

[1/1] usb: gadget: f_mass_storage: Disable eps during disconnect

      https://source.denx.de/u-boot/custodians/u-boot-dfu/-/commit/1c758ce38caa783c85129753c1ecc9d14a203d8e

--
Mattijs