Skip to contentSkip to conversation

usb: gadget: call usb_gadget_check_config() to verify UDC capability

PATCH Superseded Frank Li 3 participants 7 messages Jun 28, 2023 – Jun 30, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Frank Li

author maintainer
#0 lore raw

PATCH 1/1 usb: gadget: call usb_gadget_check_config() to verify UDC capability

The legacy gadget driver omitted calling usb_gadget_check_config() to ensure that the USB device controller (UDC) has adequate resources, including sufficient endpoint numbers and types, to support the given configuration.

Previously, usb_add_config() was solely invoked by the legacy gadget driver. Adds the necessary usb_gadget_check_config() after the bind() operation to fix the issue.

Fixes dce49449e04f ("usb: cdns3: allocate TX FIFO size according to composite EP number")
---
 drivers/usb/gadget/composite.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/gadget/composite.c +4 -0
index 1b3489149e5e..dd9b90481b4c 100644
@@ -1125,6 +1125,10 @@ int usb_add_config(struct usb_composite_dev *cdev,
 		goto done;
 
 	status = bind(config);
+
+	if (status == 0)
+		status = usb_gadget_check_config(cdev->gadget);
+
 	if (status < 0) {
 		while (!list_empty(&config->functions)) {
 			struct usb_function		*f;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Frank Li

The legacy gadget driver omitted calling usb_gadget_check_config() to ensure that the USB device controller (UDC) has adequate resources, including sufficient endpoint numbers and types, to support the given configuration.

Previously, usb_add_config() was solely invoked by the legacy gadget driver. Adds the necessary usb_gadget_check_config() after the bind() operation to fix the issue.

You have only fixed composite.c. Not all gadget drivers use composite.c so it will be still broken for them.

Please also add default sane configuration in CDNS3 so it works even if usb_gadget_check_config() is not invoked.

Quoted 23 lines
Fixes dce49449e04f ("usb: cdns3: allocate TX FIFO size according to composite EP number")
---
 drivers/usb/gadget/composite.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/gadget/composite.c +4 -0
index 1b3489149e5e..dd9b90481b4c 100644
@@ -1125,6 +1125,10 @@ int usb_add_config(struct usb_composite_dev *cdev,
 		goto done;
 
 	status = bind(config);
+
+	if (status == 0)
+		status = usb_gadget_check_config(cdev->gadget);
+
 	if (status < 0) {
 		while (!list_empty(&config->functions)) {
 			struct usb_function		*f;
Signature · 2 lines
cheers,
-roger
author maintainer
#2 lore raw
Quoted 32 lines

-----Original Message-----
From: Roger Quadros <[email protected]>
Sent: Wednesday, June 28, 2023 10:23 PM
To: Frank Li <[email protected]>; [email protected]; [email protected]; Jun Li <[email protected]>; Greg Kroah-Hartman <[email protected]>; Elson Roy Serrao <[email protected]>; Thinh Nguyen <[email protected]>; Andy Shevchenko <[email protected]>; Jó Ágila Bitsch <[email protected]>; Prashanth K <[email protected]>; Peter Chen <[email protected]>; open list:USB SUBSYSTEM <linux- [email protected]>; open list <[email protected]>
Subject: [EXT] Re: [PATCH 1/1] usb: gadget: call usb_gadget_check_config() to verify UDC capability

Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button

Quoted 8 lines · Frank Li

The legacy gadget driver omitted calling usb_gadget_check_config() to ensure that the USB device controller (UDC) has adequate resources, including sufficient endpoint numbers and types, to support the given configuration.

Previously, usb_add_config() was solely invoked by the legacy gadget driver. Adds the necessary usb_gadget_check_config() after the bind() operation to fix the issue.

Quoted 5 lines

You have only fixed composite.c. Not all gadget drivers use composite.c so it will be still broken for them.

Please also add default sane configuration in CDNS3 so it works even if usb_gadget_check_config() is not invoked.

Which one was not call usb_add_config()?
DWC3 also use
.check_config = dwc3_gadget_check_config,
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/dwc3/gadget.c

I think it is not correct by assume UDC can support specific config at gadget function driver.
Add default value of CDNS3 actually hidden potential problem.

I don't suggest it before fixed hidden potential problem.

Frank

Quoted 31 lines
Quoted 3 lines
Fixes dce49449e04f ("usb: cdns3: allocate TX FIFO size according to

composite EP number")

Review

Quoted 7 lines
---
 drivers/usb/gadget/composite.c | 4 ++++
 1 file changed, 4 insertions(+)
(quoted diff) +0 -0

b/drivers/usb/gadget/composite.c

drivers/usb/gadget/composite.c int usb_add_config(struct usb_composite_dev

Quoted 4 lines

index 1b3489149e5e..dd9b90481b4c 100644

drivers/usb/gadget/composite.c +0 -0
@@ -1125,6 +1125,10 @@ int usb_add_config(struct usb_composite_dev

*cdev,

drivers/usb/gadget/composite.c int usb_add_config(struct usb_composite_dev *cdev, · line 1134

Quoted 10 lines
drivers/usb/gadget/composite.c +4 -0
              goto done;
 
      status = bind(config);
+
+     if (status == 0)
+             status = usb_gadget_check_config(cdev->gadget);
+
      if (status < 0) {
              while (!list_empty(&config->functions)) {
                      struct usb_function             *f;

--
cheers,
-roger

#5 lore raw
In reply to Frank Li
Quoted 21 lines · Frank Li

The legacy gadget driver omitted calling usb_gadget_check_config() to ensure that the USB device controller (UDC) has adequate resources, including sufficient endpoint numbers and types, to support the given configuration.

Previously, usb_add_config() was solely invoked by the legacy gadget driver. Adds the necessary usb_gadget_check_config() after the bind() operation to fix the issue.

Fixes dce49449e04f ("usb: cdns3: allocate TX FIFO size according to composite EP number")
---
 drivers/usb/gadget/composite.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/gadget/composite.c +0 -0
index 1b3489149e5e..dd9b90481b4c 100644

drivers/usb/gadget/composite.c int usb_add_config(struct usb_composite_dev *cdev, · line 1130

Quoted 6 lines
drivers/usb/gadget/composite.c +3 -0
@@ -1125,6 +1125,10 @@ int usb_add_config(struct usb_composite_dev *cdev,
 		goto done;
 
 	status = bind(config);
+
+	if (status == 0)
+		status = usb_gadget_check_config(cdev->gadget);

This will work for legacy gadgets that support only one configurations. Take for example g_ether. It has two configurations when RNDIS is enabled. And usb_add_config() is invoked for each configuration.

cdns3_gadget_check_config() calculates the total IN end-points based on the ep->claimed flag.

        list_for_each_entry(ep, &gadget->ep_list, ep_list) {                
                if (ep->claimed && (ep->address & USB_DIR_IN))              
                        n_in++;                                             
        }  

This ep->claimed flag is later cleared by usb_ep_autoconfig_reset() which is invoked in usb_add_config(). So for multi-configurations, actual total in end points is not taken into consideration thus resulting in incorrect fifo size allocation/check.

g_ffs is another gadget which has multiple configurations.

Quoted 4 lines
drivers/usb/gadget/composite.c +1 -0
+
 	if (status < 0) {
 		while (!list_empty(&config->functions)) {
 			struct usb_function		*f;
Signature · 2 lines
Regards,
Ravi
author maintainer
#6 lore raw
Quoted 66 lines

-----Original Message-----
From: Ravi Gunasekaran <[email protected]>
Sent: Thursday, June 29, 2023 12:11 AM
To: Frank Li <[email protected]>; [email protected]; [email protected]; Jun Li <[email protected]>; Greg Kroah-Hartman <[email protected]>; Elson Roy Serrao <[email protected]>; Thinh Nguyen <[email protected]>; Andy Shevchenko <[email protected]>; Jó Ágila Bitsch <[email protected]>; Prashanth K <[email protected]>; Peter Chen <[email protected]>; open list:USB SUBSYSTEM <linux- [email protected]>; open list <[email protected]>
Subject: [EXT] Re: [PATCH 1/1] usb: gadget: call usb_gadget_check_config() to verify UDC capability

Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button

Quoted 10 lines · Frank Li

The legacy gadget driver omitted calling usb_gadget_check_config() to ensure that the USB device controller (UDC) has adequate resources, including sufficient endpoint numbers and types, to support the given configuration.

Previously, usb_add_config() was solely invoked by the legacy gadget driver. Adds the necessary usb_gadget_check_config() after the bind() operation to fix the issue.

Fixes dce49449e04f ("usb: cdns3: allocate TX FIFO size according to

composite EP number")

Review

Quoted 7 lines
---
 drivers/usb/gadget/composite.c | 4 ++++
 1 file changed, 4 insertions(+)
(quoted diff) +0 -0

b/drivers/usb/gadget/composite.c

drivers/usb/gadget/composite.c int usb_add_config(struct usb_composite_dev

Quoted 4 lines

index 1b3489149e5e..dd9b90481b4c 100644

drivers/usb/gadget/composite.c +0 -0
@@ -1125,6 +1125,10 @@ int usb_add_config(struct usb_composite_dev

*cdev,

drivers/usb/gadget/composite.c int usb_add_config(struct usb_composite_dev *cdev, · line 1130

Quoted 6 lines
drivers/usb/gadget/composite.c +3 -0
              goto done;
 
      status = bind(config);
+
+     if (status == 0)
+             status = usb_gadget_check_config(cdev->gadget);

This will work for legacy gadgets that support only one configurations. Take for example g_ether. It has two configurations when RNDIS is enabled. And usb_add_config() is invoked for each configuration.

cdns3_gadget_check_config() calculates the total IN end-points based on the ep->claimed flag.

        list_for_each_entry(ep, &gadget->ep_list, ep_list) {
                if (ep->claimed && (ep->address & USB_DIR_IN))
                        n_in++;
        }
Quoted 7 lines

This ep->claimed flag is later cleared by usb_ep_autoconfig_reset() which is invoked in usb_add_config(). So for multi-configurations, actual total in end points
is not taken into consideration thus resulting in incorrect fifo size allocation/check.

g_ffs is another gadget which has multiple configurations.

Actually it is another problem that I am trying to figure out. Configfs should have
similar Problem. We never test multi config case in configfs also. Generally,
composite device only have multi interfaces.

cdns3_gadget_match_ep wrong cache all endpoints for all configs, but actually only one config can be active.

Frank

Quoted 10 lines

drivers/usb/gadget/composite.c int usb_add_config(struct usb_composite_dev *cdev, · line 1134

Quoted 6 lines
drivers/usb/gadget/composite.c +1 -0
+
      if (status < 0) {
              while (!list_empty(&config->functions)) {
                      struct usb_function             *f;

--
Regards,
Ravi

#3 lore raw
In reply to Frank Li
Quoted 35 lines · Frank Li
Quoted 35 lines

-----Original Message-----
From: Roger Quadros <[email protected]>
Sent: Wednesday, June 28, 2023 10:23 PM
To: Frank Li <[email protected]>; [email protected]; [email protected]; Jun Li <[email protected]>; Greg Kroah-Hartman <[email protected]>; Elson Roy Serrao <[email protected]>; Thinh Nguyen <[email protected]>; Andy Shevchenko <[email protected]>; Jó Ágila Bitsch <[email protected]>; Prashanth K <[email protected]>; Peter Chen <[email protected]>; open list:USB SUBSYSTEM <linux- [email protected]>; open list <[email protected]>
Subject: [EXT] Re: [PATCH 1/1] usb: gadget: call usb_gadget_check_config() to verify UDC capability

Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button

Quoted 8 lines · Frank Li

The legacy gadget driver omitted calling usb_gadget_check_config() to ensure that the USB device controller (UDC) has adequate resources, including sufficient endpoint numbers and types, to support the given configuration.

Previously, usb_add_config() was solely invoked by the legacy gadget driver. Adds the necessary usb_gadget_check_config() after the bind() operation to fix the issue.

You have only fixed composite.c. Not all gadget drivers use composite.c so it will be still broken for them.

Quoted 7 lines
Quoted 2 lines

Please also add default sane configuration in CDNS3 so it works even if usb_gadget_check_config() is not invoked.

Which one was not call usb_add_config()?
DWC3 also use
.check_config = dwc3_gadget_check_config,
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/dwc3/gadget.c

I meant gadget drivers not UDC drivers.

e.g.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/gadget/legacy/dbgp.c

Quoted 40 lines

I think it is not correct by assume UDC can support specific config at gadget function driver.
Add default value of CDNS3 actually hidden potential problem.

I don't suggest it before fixed hidden potential problem.

Frank

Quoted 31 lines
Quoted 3 lines
Fixes dce49449e04f ("usb: cdns3: allocate TX FIFO size according to

composite EP number")

Review

Quoted 7 lines
---
 drivers/usb/gadget/composite.c | 4 ++++
 1 file changed, 4 insertions(+)
(quoted diff) +0 -0

b/drivers/usb/gadget/composite.c

drivers/usb/gadget/composite.c int usb_add_config(struct usb_composite_dev

Quoted 4 lines

index 1b3489149e5e..dd9b90481b4c 100644

drivers/usb/gadget/composite.c +0 -0
@@ -1125,6 +1125,10 @@ int usb_add_config(struct usb_composite_dev

*cdev,

drivers/usb/gadget/composite.c int usb_add_config(struct usb_composite_dev *cdev, · line 1134

Quoted 10 lines
drivers/usb/gadget/composite.c +4 -0
              goto done;
 
      status = bind(config);
+
+     if (status == 0)
+             status = usb_gadget_check_config(cdev->gadget);
+
      if (status < 0) {
              while (!list_empty(&config->functions)) {
                      struct usb_function             *f;

--
cheers,
-roger

Signature · 2 lines
cheers,
-roger
author maintainer
#4 lore raw
Quoted 11 lines · Roger Quadros
Quoted 4 lines

.check_config = dwc3_gadget_check_config,
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/dwc3/gadget.c

I meant gadget drivers not UDC drivers.

e.g.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/gadget/legacy/dbgp.c

This is for EHCI host controller debug port, specially implement simple tty gadget devices. Generally used for PC. This driver suppose will not use UDC at all.