All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: dwc2: Fix gadget DMA unmap direction
@ 2021-05-06 11:22 Phil Elwell
  2021-05-08  6:10 ` Minas Harutyunyan
  0 siblings, 1 reply; 2+ messages in thread
From: Phil Elwell @ 2021-05-06 11:22 UTC (permalink / raw)
  To: Minas Harutyunyan, Greg Kroah-Hartman, Mian Yousaf Kaukab,
	Felipe Balbi, Paul Zimmerman, Nicolas Saenz Julienne, linux-usb,
	linux-rpi-kernel
  Cc: Phil Elwell

The dwc2 gadget support maps and unmaps DMA buffers as necessary. When
mapping and unmapping it uses the direction of the endpoint to select
the direction of the DMA transfer, but this fails for Control OUT
transfers because the unmap occurs after the endpoint direction has
been reversed for the status phase.

A possible solution would be to unmap the buffer before the direction
is changed, but a safer, less invasive fix is to remember the buffer
direction independently of the endpoint direction.

Fixes: fe0b94abcdf6 ("usb: dwc2: gadget: manage ep0 state in software")
Signed-off-by: Phil Elwell <phil@raspberrypi.com>
---
 drivers/usb/dwc2/core.h   | 2 ++
 drivers/usb/dwc2/gadget.c | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index da5ac4a4595b..ab6b815e0089 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -113,6 +113,7 @@ struct dwc2_hsotg_req;
  * @debugfs: File entry for debugfs file for this endpoint.
  * @dir_in: Set to true if this endpoint is of the IN direction, which
  *          means that it is sending data to the Host.
+ * @map_dir: Set to the value of dir_in when the DMA buffer is mapped.
  * @index: The index for the endpoint registers.
  * @mc: Multi Count - number of transactions per microframe
  * @interval: Interval for periodic endpoints, in frames or microframes.
@@ -162,6 +163,7 @@ struct dwc2_hsotg_ep {
 	unsigned short		fifo_index;
 
 	unsigned char           dir_in;
+	unsigned char           map_dir;
 	unsigned char           index;
 	unsigned char           mc;
 	u16                     interval;
diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index e6bb1bdb2760..184964174dc0 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -422,7 +422,7 @@ static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
 {
 	struct usb_request *req = &hs_req->req;
 
-	usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
+	usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->map_dir);
 }
 
 /*
@@ -1242,6 +1242,7 @@ static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
 {
 	int ret;
 
+	hs_ep->map_dir = hs_ep->dir_in;
 	ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
 	if (ret)
 		goto dma_error;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] usb: dwc2: Fix gadget DMA unmap direction
  2021-05-06 11:22 [PATCH] usb: dwc2: Fix gadget DMA unmap direction Phil Elwell
@ 2021-05-08  6:10 ` Minas Harutyunyan
  0 siblings, 0 replies; 2+ messages in thread
From: Minas Harutyunyan @ 2021-05-08  6:10 UTC (permalink / raw)
  To: Phil Elwell, Greg Kroah-Hartman, Mian Yousaf Kaukab,
	Felipe Balbi, Paul Zimmerman, Nicolas Saenz Julienne, linux-usb,
	linux-rpi-kernel

On 5/6/2021 3:22 PM, Phil Elwell wrote:
> The dwc2 gadget support maps and unmaps DMA buffers as necessary. When
> mapping and unmapping it uses the direction of the endpoint to select
> the direction of the DMA transfer, but this fails for Control OUT
> transfers because the unmap occurs after the endpoint direction has
> been reversed for the status phase.
> 
> A possible solution would be to unmap the buffer before the direction
> is changed, but a safer, less invasive fix is to remember the buffer
> direction independently of the endpoint direction.
> 
> Fixes: fe0b94abcdf6 ("usb: dwc2: gadget: manage ep0 state in software")
> Signed-off-by: Phil Elwell <phil@raspberrypi.com>

Acked-by: Minas Harutyunyan <Minas.Harutyunyan@synopsys.com>

> ---
>   drivers/usb/dwc2/core.h   | 2 ++
>   drivers/usb/dwc2/gadget.c | 3 ++-
>   2 files changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
> index da5ac4a4595b..ab6b815e0089 100644
> --- a/drivers/usb/dwc2/core.h
> +++ b/drivers/usb/dwc2/core.h
> @@ -113,6 +113,7 @@ struct dwc2_hsotg_req;
>    * @debugfs: File entry for debugfs file for this endpoint.
>    * @dir_in: Set to true if this endpoint is of the IN direction, which
>    *          means that it is sending data to the Host.
> + * @map_dir: Set to the value of dir_in when the DMA buffer is mapped.
>    * @index: The index for the endpoint registers.
>    * @mc: Multi Count - number of transactions per microframe
>    * @interval: Interval for periodic endpoints, in frames or microframes.
> @@ -162,6 +163,7 @@ struct dwc2_hsotg_ep {
>   	unsigned short		fifo_index;
>   
>   	unsigned char           dir_in;
> +	unsigned char           map_dir;
>   	unsigned char           index;
>   	unsigned char           mc;
>   	u16                     interval;
> diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
> index e6bb1bdb2760..184964174dc0 100644
> --- a/drivers/usb/dwc2/gadget.c
> +++ b/drivers/usb/dwc2/gadget.c
> @@ -422,7 +422,7 @@ static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
>   {
>   	struct usb_request *req = &hs_req->req;
>   
> -	usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
> +	usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->map_dir);
>   }
>   
>   /*
> @@ -1242,6 +1242,7 @@ static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
>   {
>   	int ret;
>   
> +	hs_ep->map_dir = hs_ep->dir_in;
>   	ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
>   	if (ret)
>   		goto dma_error;
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-08  6:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-06 11:22 [PATCH] usb: dwc2: Fix gadget DMA unmap direction Phil Elwell
2021-05-08  6:10 ` Minas Harutyunyan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.