diff mbox series

[v1,2/3] usb: dwc3: gadget: Add support for snps,reserved-endpoints property

Message ID 20250116154117.148915-3-andriy.shevchenko@linux.intel.com (mailing list archive)
State New
Headers show
Series usb: dwc3: Avoid using reserved EPs | expand

Commit Message

Andy Shevchenko Jan. 16, 2025, 3:40 p.m. UTC
The snps,reserved-endpoints property lists the reserved endpoints
that shouldn't be used for normal transfers. Add support for that
to the driver.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/usb/dwc3/gadget.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

Comments

Thinh Nguyen Jan. 16, 2025, 11:35 p.m. UTC | #1
On Thu, Jan 16, 2025, Andy Shevchenko wrote:
> The snps,reserved-endpoints property lists the reserved endpoints
> that shouldn't be used for normal transfers. Add support for that
> to the driver.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/usb/dwc3/gadget.c | 38 +++++++++++++++++++++++++++++++++++++-
>  1 file changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index 31a654c6f15b..3f806fb8b61c 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -3349,14 +3349,50 @@ static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
>  	return 0;
>  }
>  
> +static int dwc3_gadget_parse_reserved_endpoints(struct dwc3 *dwc, u8 *eps, size_t count)
> +{
> +	const char *propname = "snps,reserved-endpoints";
> +	int ret;
> +
> +	ret = device_property_count_u8(dwc->dev, propname);
> +	if (ret < 0)
> +		return 0;
> +	if (ret == 0)
> +		return 0;

Just use if (ret <= 0) return 0.

> +	if (ret > count) {
> +		dev_err(dwc->dev, "too many entries in %s\n", propname);
> +		return -EINVAL;
> +	}
> +
> +	count = ret;
> +	ret = device_property_read_u8_array(dwc->dev, propname, eps, count);
> +	if (ret)
> +		dev_err(dwc->dev, "failed to read %s\n", propname);
> +
> +	return ret;
> +}
> +
>  static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
>  {
>  	u8				epnum;
> +	u8 eps[DWC3_ENDPOINTS_NUM];

Please keep consistent alignment.

> +	u8 count, num;

Please keep declaration in separate lines here.

> +	int ret;
>  
>  	INIT_LIST_HEAD(&dwc->gadget->ep_list);
>  
> +	ret = dwc3_gadget_parse_reserved_endpoints(dwc, eps, ARRAY_SIZE(eps));
> +	if (ret < 0)
> +		return ret;
> +	count = ret;
> +
>  	for (epnum = 0; epnum < total; epnum++) {
> -		int			ret;
> +		for (num = 0; num < count; num++) {
> +			if (epnum == eps[num])
> +				break;
> +		}
> +		if (num < count)
> +			continue;

You can probably rewrite this logic better.

>  
>  		ret = dwc3_gadget_init_endpoint(dwc, epnum);
>  		if (ret)
> -- 
> 2.43.0.rc1.1336.g36b5255a03ac
> 

BR,
Thinh
Andy Shevchenko Jan. 17, 2025, 1:32 p.m. UTC | #2
On Thu, Jan 16, 2025 at 11:35:19PM +0000, Thinh Nguyen wrote:
> On Thu, Jan 16, 2025, Andy Shevchenko wrote:

...

> >  	for (epnum = 0; epnum < total; epnum++) {
> > -		int			ret;
> > +		for (num = 0; num < count; num++) {
> > +			if (epnum == eps[num])
> > +				break;
> > +		}
> > +		if (num < count)
> > +			continue;
> 
> You can probably rewrite this logic better.

Any suggestions?

Thanks for the review!
diff mbox series

Patch

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 31a654c6f15b..3f806fb8b61c 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -3349,14 +3349,50 @@  static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
 	return 0;
 }
 
+static int dwc3_gadget_parse_reserved_endpoints(struct dwc3 *dwc, u8 *eps, size_t count)
+{
+	const char *propname = "snps,reserved-endpoints";
+	int ret;
+
+	ret = device_property_count_u8(dwc->dev, propname);
+	if (ret < 0)
+		return 0;
+	if (ret == 0)
+		return 0;
+	if (ret > count) {
+		dev_err(dwc->dev, "too many entries in %s\n", propname);
+		return -EINVAL;
+	}
+
+	count = ret;
+	ret = device_property_read_u8_array(dwc->dev, propname, eps, count);
+	if (ret)
+		dev_err(dwc->dev, "failed to read %s\n", propname);
+
+	return ret;
+}
+
 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
 {
 	u8				epnum;
+	u8 eps[DWC3_ENDPOINTS_NUM];
+	u8 count, num;
+	int ret;
 
 	INIT_LIST_HEAD(&dwc->gadget->ep_list);
 
+	ret = dwc3_gadget_parse_reserved_endpoints(dwc, eps, ARRAY_SIZE(eps));
+	if (ret < 0)
+		return ret;
+	count = ret;
+
 	for (epnum = 0; epnum < total; epnum++) {
-		int			ret;
+		for (num = 0; num < count; num++) {
+			if (epnum == eps[num])
+				break;
+		}
+		if (num < count)
+			continue;
 
 		ret = dwc3_gadget_init_endpoint(dwc, epnum);
 		if (ret)