Skip to contentSkip to conversation

usb: dwc3: gadget: Fix .udc_set_speed()

PATCH Superseded Roger Quadros 3 participants 11 messages Oct 31, 2017 – Nov 2, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: dwc3: gadget: Fix .udc_set_speed()

UDC core calls .udc_set_speed() with the speed parameter containing the maximum speed supported by the gadget function driver. This might very well be more or less than that supported by the dwc3 controller driver.

Select the lesser of the 2 speeds so both are operating within limits.

This fixes PHY Erratic errors and 2 second enumeration delay on TI's AM437x platforms.

Fixes 7d8d0639565f ("usb: dwc3: gadget: implement ->udc_set_speed()")
Cc <[email protected]> # v4.13+
---
 drivers/usb/dwc3/gadget.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/dwc3/gadget.c +2 -0
index f064f15..9f27ec0 100644
@@ -2008,6 +2008,8 @@ static void dwc3_gadget_set_speed(struct usb_gadget *g,
 	unsigned long		flags;
 	u32			reg;
 
+	speed = min(g->max_speed, speed);
+
 	spin_lock_irqsave(&dwc->lock, flags);
 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
 	reg &= ~(DWC3_DCFG_SPEED_MASK);
Signature · 4 lines
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#1 lore raw
Quoted 24 lines · Roger Quadros

UDC core calls .udc_set_speed() with the speed parameter containing the maximum speed supported by the gadget function driver. This might very well be more or less than that supported by the dwc3 controller driver.

Select the lesser of the 2 speeds so both are operating within limits.

This fixes PHY Erratic errors and 2 second enumeration delay on TI's AM437x platforms.

Fixes 7d8d0639565f ("usb: dwc3: gadget: implement ->udc_set_speed()")
Cc <[email protected]> # v4.13+
---
 drivers/usb/dwc3/gadget.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/dwc3/gadget.c +0 -0
index f064f15..9f27ec0 100644

drivers/usb/dwc3/gadget.c static void dwc3_gadget_set_speed(struct usb_gadget *g, · line 2011

Quoted 4 lines
drivers/usb/dwc3/gadget.c +1 -0
@@ -2008,6 +2008,8 @@ static void dwc3_gadget_set_speed(struct usb_gadget *g,
 	unsigned long		flags;
 	u32			reg;
 
+	speed = min(g->max_speed, speed);

should we do that in udc core itself?

@@ -1080,8 +1080,12 @@ static inline void usb_gadget_udc_stop(struct usb_udc *udc)
static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
enum usb_device_speed speed)
{
- if (udc->gadget->ops->udc_set_speed)
- udc->gadget->ops->udc_set_speed(udc->gadget, speed);
+ if (udc->gadget->ops->udc_set_speed) {
+ enum usb_device_speed s;
+
+ s = min(speed, udc->gadget->max_speed);
+ udc->gadget->ops->udc_set_speed(udc->gadget, s);
+ }
}

/**

then the fix applies for all UDCs.

Signature · 1 line
balbi
#2 lore raw


Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

Quoted 49 lines · Felipe Balbi
Quoted 24 lines · Roger Quadros

UDC core calls .udc_set_speed() with the speed parameter containing the maximum speed supported by the gadget function driver. This might very well be more or less than that supported by the dwc3 controller driver.

Select the lesser of the 2 speeds so both are operating within limits.

This fixes PHY Erratic errors and 2 second enumeration delay on TI's AM437x platforms.

Fixes 7d8d0639565f ("usb: dwc3: gadget: implement ->udc_set_speed()")
Cc <[email protected]> # v4.13+
---
 drivers/usb/dwc3/gadget.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/dwc3/gadget.c +0 -0
index f064f15..9f27ec0 100644

drivers/usb/dwc3/gadget.c static void dwc3_gadget_set_speed(struct usb_gadget *g, · line 2011

Quoted 4 lines
drivers/usb/dwc3/gadget.c +1 -0
@@ -2008,6 +2008,8 @@ static void dwc3_gadget_set_speed(struct usb_gadget *g,
 	unsigned long		flags;
 	u32			reg;
 
+	speed = min(g->max_speed, speed);

should we do that in udc core itself?

(quoted diff) +6 -2
@@ -1080,8 +1080,12 @@ static inline void usb_gadget_udc_stop(struct usb_udc *udc)
 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
 					    enum usb_device_speed speed)
 {
-	if (udc->gadget->ops->udc_set_speed)
-		udc->gadget->ops->udc_set_speed(udc->gadget, speed);
+	if (udc->gadget->ops->udc_set_speed) {
+		enum usb_device_speed s;
+
+		s = min(speed, udc->gadget->max_speed);
+		udc->gadget->ops->udc_set_speed(udc->gadget, s);
+	}
 }
 
 /**
Quoted 2 lines

then the fix applies for all UDCs.

Sounds like a good idea. Will re-spin this patch.

Signature · 4 lines
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#3 lore raw

PATCH v2 1/1 usb: dwc3: gadget: Fix .udc_set_speed()

In reply to Roger Quadros

Currently UDC core calls ->udc_set_speed() with the speed parameter containing the maximum speed supported by the gadget function driver. This might very well be more than that supported by the UDC controller driver.

Select the lesser of the 2 speeds so both UDC and gadget function driver are operating within limits.

This fixes PHY Erratic errors and 2 second enumeration delay on TI's AM437x platforms.

Fixes 6099eca796ae ("usb: gadget: core: introduce ->udc_set_speed() method")
Cc <[email protected]> # v4.13+

---
v2:
- Move speed limiting code to UDC core.

 drivers/usb/gadget/udc/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/udc/core.c +6 -2
index d41d07a..def1b05 100644
@@ -1080,8 +1080,12 @@ static inline void usb_gadget_udc_stop(struct usb_udc *udc)
 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
 					    enum usb_device_speed speed)
 {
-	if (udc->gadget->ops->udc_set_speed)
-		udc->gadget->ops->udc_set_speed(udc->gadget, speed);
+	if (udc->gadget->ops->udc_set_speed) {
+		enum usb_device_speed s;
+
+		s = min(speed, udc->gadget->max_speed);
+		udc->gadget->ops->udc_set_speed(udc->gadget, s);
+	}
 }
 
 /**
Signature · 4 lines
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#4 lore raw

Felipe,

Forgot to update subject :P.
Will resend.

Quoted 42 lines · Roger Quadros

Currently UDC core calls ->udc_set_speed() with the speed parameter containing the maximum speed supported by the gadget function driver. This might very well be more than that supported by the UDC controller driver.

Select the lesser of the 2 speeds so both UDC and gadget function driver are operating within limits.

This fixes PHY Erratic errors and 2 second enumeration delay on TI's AM437x platforms.

Fixes 6099eca796ae ("usb: gadget: core: introduce ->udc_set_speed() method")
Cc <[email protected]> # v4.13+

---
v2:
- Move speed limiting code to UDC core.

 drivers/usb/gadget/udc/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/udc/core.c +6 -2
index d41d07a..def1b05 100644
@@ -1080,8 +1080,12 @@ static inline void usb_gadget_udc_stop(struct usb_udc *udc)
 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
 					    enum usb_device_speed speed)
 {
-	if (udc->gadget->ops->udc_set_speed)
-		udc->gadget->ops->udc_set_speed(udc->gadget, speed);
+	if (udc->gadget->ops->udc_set_speed) {
+		enum usb_device_speed s;
+
+		s = min(speed, udc->gadget->max_speed);
+		udc->gadget->ops->udc_set_speed(udc->gadget, s);
+	}
 }
 
 /**
Signature · 4 lines
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#5 lore raw

PATCH RESEND v2 1/1 usb: gadget: core: Fix ->udc_set_speed() speed handling

In reply to Roger Quadros

Currently UDC core calls ->udc_set_speed() with the speed parameter containing the maximum speed supported by the gadget function driver. This might very well be more than that supported by the UDC controller driver.

Select the lesser of the 2 speeds so both UDC and gadget function driver are operating within limits.

This fixes PHY Erratic errors and 2 second enumeration delay on TI's AM437x platforms.

Fixes 6099eca796ae ("usb: gadget: core: introduce ->udc_set_speed() method")
Cc <[email protected]> # v4.13+

---
v2:
- Move speed limiting code to UDC core.

 drivers/usb/gadget/udc/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/udc/core.c +6 -2
index d41d07a..def1b05 100644
@@ -1080,8 +1080,12 @@ static inline void usb_gadget_udc_stop(struct usb_udc *udc)
 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
 					    enum usb_device_speed speed)
 {
-	if (udc->gadget->ops->udc_set_speed)
-		udc->gadget->ops->udc_set_speed(udc->gadget, speed);
+	if (udc->gadget->ops->udc_set_speed) {
+		enum usb_device_speed s;
+
+		s = min(speed, udc->gadget->max_speed);
+		udc->gadget->ops->udc_set_speed(udc->gadget, s);
+	}
 }
 
 /**
Signature · 4 lines
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#6 lore raw

Re: [PATCH v2][RESEND] usb: gadget: core: Fix ->udc_set_speed() speed handling

Quoted 31 lines · Roger Quadros

Currently UDC core calls ->udc_set_speed() with the speed parameter containing the maximum speed supported by the gadget function driver. This might very well be more than that supported by the UDC controller driver.

Select the lesser of the 2 speeds so both UDC and gadget function driver are operating within limits.

This fixes PHY Erratic errors and 2 second enumeration delay on TI's AM437x platforms.

Fixes 6099eca796ae ("usb: gadget: core: introduce ->udc_set_speed() method")
Cc <[email protected]> # v4.13+

---
v2:
- Move speed limiting code to UDC core.

 drivers/usb/gadget/udc/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/udc/core.c +0 -1
index d41d07a..def1b05 100644
@@ -1080,8 +1080,12 @@ static inline void usb_gadget_udc_stop(struct usb_udc *udc)
 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
 					    enum usb_device_speed speed)
 {
-	if (udc->gadget->ops->udc_set_speed)

drivers/usb/gadget/udc/core.c static inline void usb_gadget_udc_stop(struct usb_udc *udc) · line 1091

Quoted 10 lines
drivers/usb/gadget/udc/core.c +6 -1
-		udc->gadget->ops->udc_set_speed(udc->gadget, speed);
+	if (udc->gadget->ops->udc_set_speed) {
+		enum usb_device_speed s;
+
+		s = min(speed, udc->gadget->max_speed);
+		udc->gadget->ops->udc_set_speed(udc->gadget, s);
+	}
 }
 
 /**

Roger, would you like to send in a follow-up patch that removes the now unnecessary code from dummy_udc_set_speed() in dummy_hcd.c?

Alan Stern

#7 lore raw

Re: [PATCH v2][RESEND] usb: gadget: core: Fix ->udc_set_speed() speed handling

Hi Alan,

Quoted 42 lines · Alan Stern
Quoted 40 lines · Roger Quadros

Currently UDC core calls ->udc_set_speed() with the speed parameter containing the maximum speed supported by the gadget function driver. This might very well be more than that supported by the UDC controller driver.

Select the lesser of the 2 speeds so both UDC and gadget function driver are operating within limits.

This fixes PHY Erratic errors and 2 second enumeration delay on TI's AM437x platforms.

Fixes 6099eca796ae ("usb: gadget: core: introduce ->udc_set_speed() method")
Cc <[email protected]> # v4.13+

---
v2:
- Move speed limiting code to UDC core.

 drivers/usb/gadget/udc/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/udc/core.c +6 -2
index d41d07a..def1b05 100644
@@ -1080,8 +1080,12 @@ static inline void usb_gadget_udc_stop(struct usb_udc *udc)
 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
 					    enum usb_device_speed speed)
 {
-	if (udc->gadget->ops->udc_set_speed)
-		udc->gadget->ops->udc_set_speed(udc->gadget, speed);
+	if (udc->gadget->ops->udc_set_speed) {
+		enum usb_device_speed s;
+
+		s = min(speed, udc->gadget->max_speed);
+		udc->gadget->ops->udc_set_speed(udc->gadget, s);
+	}
 }
 
Quoted 5 lines

drivers/usb/gadget/udc/core.c static inline void usb_gadget_udc_stop(struct usb_udc *udc) · line 1091

Quoted 1 line
drivers/usb/gadget/udc/core.c +0 -0
 /**

Roger, would you like to send in a follow-up patch that removes the now unnecessary code from dummy_udc_set_speed() in dummy_hcd.c?

Sure. I have a question though.

In dummy_udc_set_speed() we should just set gadget.speed to whatever is passed in speed. This will be the maximum supported speed by the gadget function driver.

However, is it possible that the gadget will be connected at a lower speed than that?

I see gadget.speed being updated in dummy_hub_control() GetPortStatus: case, but not in case of USB_SPEED_HIGH and USB_SPEED_LOW.

Signature · 4 lines
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#8 lore raw

Re: [PATCH v2][RESEND] usb: gadget: core: Fix ->udc_set_speed() speed handling

Quoted 8 lines · Roger Quadros
Quoted 3 lines

Roger, would you like to send in a follow-up patch that removes the now unnecessary code from dummy_udc_set_speed() in dummy_hcd.c?

Sure. I have a question though.

In dummy_udc_set_speed() we should just set gadget.speed to whatever is passed in speed. This will be the maximum supported speed by the gadget function driver.

Following your patch, it will be the maximum speed supported by the gadget function driver or the maximum speed supported by the UDC driver, whichever is lower. That's the whole point of your patch, right?

Quoted 1 line

However, is it possible that the gadget will be connected at a lower speed than that?

Yes, it is possible to connect at a speed lower than the function driver's maximum speed. The connection speed for dummy-hcd is controlled by a pair of module parameters: is_super_speed and is_high_speed.

Quoted 2 lines

I see gadget.speed being updated in dummy_hub_control() GetPortStatus: case, but not in case of USB_SPEED_HIGH and USB_SPEED_LOW.

Here's what the code does, in outline form:

	switch (dum_hcd->dum->gadget.speed) {
	case USB_SPEED_HIGH:
		...
		break;
	case USB_SPEED_LOW:
		...
		break;
	default:
		dum_hcd->dum->gadget.speed = USB_SPEED_FULL;
		break;
	}

In other words, if gadget.speed isn't USB_SPEED_HIGH or USB_SPEED_LOW, set it to USB_SPEED_FULL. And this is all under the case where the connection is USB-2, not USB-3 or higher.

How is this related to the requested change to dummy_udc_set_speed()?

Alan Stern

#9 lore raw

Re: [PATCH v2][RESEND] usb: gadget: core: Fix ->udc_set_speed() speed handling

Quoted 8 lines · Alan Stern
Quoted 6 lines · Roger Quadros
Quoted 3 lines

Roger, would you like to send in a follow-up patch that removes the now unnecessary code from dummy_udc_set_speed() in dummy_hcd.c?

Sure. I have a question though.

Quoted 7 lines
Quoted 2 lines

In dummy_udc_set_speed() we should just set gadget.speed to whatever is passed in speed. This will be the maximum supported speed by the gadget function driver.

Following your patch, it will be the maximum speed supported by the gadget function driver or the maximum speed supported by the UDC driver, whichever is lower. That's the whole point of your patch, right?

Yes.

Quoted 25 lines
Quoted 2 lines

However, is it possible that the gadget will be connected at a lower speed than that?

Yes, it is possible to connect at a speed lower than the function driver's maximum speed. The connection speed for dummy-hcd is controlled by a pair of module parameters: is_super_speed and is_high_speed.

Quoted 2 lines

I see gadget.speed being updated in dummy_hub_control() GetPortStatus: case, but not in case of USB_SPEED_HIGH and USB_SPEED_LOW.

Here's what the code does, in outline form:

	switch (dum_hcd->dum->gadget.speed) {
	case USB_SPEED_HIGH:
		...
		break;
	case USB_SPEED_LOW:
		...
		break;
	default:
		dum_hcd->dum->gadget.speed = USB_SPEED_FULL;
		break;
	}
Quoted 5 lines

In other words, if gadget.speed isn't USB_SPEED_HIGH or USB_SPEED_LOW, set it to USB_SPEED_FULL. And this is all under the case where the connection is USB-2, not USB-3 or higher.

How is this related to the requested change to dummy_udc_set_speed()?

With or without the requested change to dummy_udc_set_speed() there will never be a case when gadget.speed isn't already USB_SPEED_FULL
if connection is USB-2 and gadget.speed is not USB_SPEED_HIGH or USB_SPEED_LOW.

So in the above code snippet, default: portion is redundant.

But that can be fixed in a separate patch.

Signature · 4 lines
cheers,
-roger

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
#10 lore raw

Re: [PATCH v2][RESEND] usb: gadget: core: Fix ->udc_set_speed() speed handling

Quoted 50 lines · Roger Quadros
Quoted 8 lines · Alan Stern
Quoted 6 lines · Roger Quadros
Quoted 3 lines

Roger, would you like to send in a follow-up patch that removes the now unnecessary code from dummy_udc_set_speed() in dummy_hcd.c?

Sure. I have a question though.

Quoted 7 lines
Quoted 2 lines

In dummy_udc_set_speed() we should just set gadget.speed to whatever is passed in speed. This will be the maximum supported speed by the gadget function driver.

Following your patch, it will be the maximum speed supported by the gadget function driver or the maximum speed supported by the UDC driver, whichever is lower. That's the whole point of your patch, right?

Yes.

Quoted 25 lines
Quoted 2 lines

However, is it possible that the gadget will be connected at a lower speed than that?

Yes, it is possible to connect at a speed lower than the function driver's maximum speed. The connection speed for dummy-hcd is controlled by a pair of module parameters: is_super_speed and is_high_speed.

Quoted 2 lines

I see gadget.speed being updated in dummy_hub_control() GetPortStatus: case, but not in case of USB_SPEED_HIGH and USB_SPEED_LOW.

Here's what the code does, in outline form:

	switch (dum_hcd->dum->gadget.speed) {
	case USB_SPEED_HIGH:
		...
		break;
	case USB_SPEED_LOW:
		...
		break;
	default:
		dum_hcd->dum->gadget.speed = USB_SPEED_FULL;
		break;
	}
Quoted 5 lines

In other words, if gadget.speed isn't USB_SPEED_HIGH or USB_SPEED_LOW, set it to USB_SPEED_FULL. And this is all under the case where the connection is USB-2, not USB-3 or higher.

How is this related to the requested change to dummy_udc_set_speed()?

Quoted 7 lines

With or without the requested change to dummy_udc_set_speed() there will never be a case when gadget.speed isn't already USB_SPEED_FULL
if connection is USB-2 and gadget.speed is not USB_SPEED_HIGH or USB_SPEED_LOW.

So in the above code snippet, default: portion is redundant.

But that can be fixed in a separate patch.

Ah, I see. Yes, you can change this too if you want.

Alan Stern