Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
@ 2015-09-22 18:40 Krzysztof Opasiak
  2015-09-22 18:40 ` [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation Krzysztof Opasiak
  2015-09-24 15:19 ` [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances Robert Baldyga
  0 siblings, 2 replies; 10+ messages in thread
From: Krzysztof Opasiak @ 2015-09-22 18:40 UTC (permalink / raw)
  To: balbi, gregkh; +Cc: linux-usb, andrzej.p, stable, Krzysztof Opasiak

Each instance of loopback function may have different qlen
and buflen attributes values. When linking function to
configuration those values had been assigned to global
variables. Linking other instance to config overwrites those
values.

This commit moves those values to f_loopback structure
to avoid overwriting. Now each function has its own instance
of those values.

Cc: <stable@vger.kernel.org> # 3.10+
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
---
 drivers/usb/gadget/function/f_loopback.c |   24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
index 6e2fe63..e4bfed4 100644
--- a/drivers/usb/gadget/function/f_loopback.c
+++ b/drivers/usb/gadget/function/f_loopback.c
@@ -34,6 +34,9 @@ struct f_loopback {
 
 	struct usb_ep		*in_ep;
 	struct usb_ep		*out_ep;
+
+	unsigned                qlen;
+	unsigned                buflen;
 };
 
 static inline struct f_loopback *func_to_loop(struct usb_function *f)
@@ -41,13 +44,10 @@ static inline struct f_loopback *func_to_loop(struct usb_function *f)
 	return container_of(f, struct f_loopback, function);
 }
 
-static unsigned qlen;
-static unsigned buflen;
-
 /*-------------------------------------------------------------------------*/
 
 static struct usb_interface_descriptor loopback_intf = {
-	.bLength =		sizeof loopback_intf,
+	.bLength =		sizeof(loopback_intf),
 	.bDescriptorType =	USB_DT_INTERFACE,
 
 	.bNumEndpoints =	2,
@@ -253,7 +253,7 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
 		}
 
 		/* queue the buffer for some later OUT packet */
-		req->length = buflen;
+		req->length = loop->buflen;
 		status = usb_ep_queue(ep, req, GFP_ATOMIC);
 		if (status == 0)
 			return;
@@ -290,7 +290,9 @@ static void disable_loopback(struct f_loopback *loop)
 
 static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
 {
-	return alloc_ep_req(ep, len, buflen);
+	struct f_loopback	*loop = ep->driver_data;
+
+	return alloc_ep_req(ep, len, loop->buflen);
 }
 
 static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
@@ -317,7 +319,7 @@ static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *lo
 	 * we buffer at most 'qlen' transfers; fewer if any need more
 	 * than 'buflen' bytes each.
 	 */
-	for (i = 0; i < qlen && result == 0; i++) {
+	for (i = 0; i < loop->qlen && result == 0; i++) {
 		req = lb_alloc_ep_req(ep, 0);
 		if (!req)
 			goto fail1;
@@ -391,10 +393,10 @@ static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
 	lb_opts->refcnt++;
 	mutex_unlock(&lb_opts->lock);
 
-	buflen = lb_opts->bulk_buflen;
-	qlen = lb_opts->qlen;
-	if (!qlen)
-		qlen = 32;
+	loop->buflen = lb_opts->bulk_buflen;
+	loop->qlen = lb_opts->qlen;
+	if (!loop->qlen)
+		loop->qlen = 32;
 
 	loop->function.name = "loopback";
 	loop->function.bind = loopback_bind;
-- 
1.7.9.5


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

* [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation
  2015-09-22 18:40 [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances Krzysztof Opasiak
@ 2015-09-22 18:40 ` Krzysztof Opasiak
  2015-09-30 16:11   ` Felipe Balbi
  2015-09-24 15:19 ` [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances Robert Baldyga
  1 sibling, 1 reply; 10+ messages in thread
From: Krzysztof Opasiak @ 2015-09-22 18:40 UTC (permalink / raw)
  To: balbi, gregkh; +Cc: linux-usb, andrzej.p, stable, Krzysztof Opasiak

Since:

commit e0857ce58e8658657f5f12fe25272b93cfeb16aa
("usb: gadget: loopback: don't queue requests to bogus endpoints")

Loopback function is not realy working as that commit removed
all looping back logic. After that commit ep-out works like
/dev/null and ep-in works like /dev/zero.

This commit fix this issue by allocating set of out requests
and set of in requests but each out req shares buffer with
one in req:

out_req->buf ---> buf <--- in_req.buf
out_req->context <---> in_req.context

The completion routine simply  enqueue the suitable req in
an oposite direction.

Cc: <stable@vger.kernel.org> # 3.18+
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
---
Changes since v2:
- fix requests context assignment

Changes since v1:
- add missing usb_ep_free_request() in complete() callback
---
 drivers/usb/gadget/function/f_loopback.c |  131 +++++++++++++++++++++---------
 1 file changed, 92 insertions(+), 39 deletions(-)

diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
index e4bfed4..c369554 100644
--- a/drivers/usb/gadget/function/f_loopback.c
+++ b/drivers/usb/gadget/function/f_loopback.c
@@ -245,22 +245,38 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
 	int			status = req->status;
 
 	switch (status) {
-
 	case 0:				/* normal completion? */
 		if (ep == loop->out_ep) {
-			req->zero = (req->actual < req->length);
-			req->length = req->actual;
+			/*
+			 * We received some data from the host so let's
+			 * queue it so host can read the from our in ep
+			 */
+			struct usb_request *in_req = req->context;
+
+			in_req->zero = (req->actual < req->length);
+			in_req->length = req->actual;
+			ep = loop->in_ep;
+			req = in_req;
+		} else {
+			/*
+			 * We have just looped back a bunch of data
+			 * to host. Now let's wait for some more data.
+			 */
+			req = req->context;
+			ep = loop->out_ep;
 		}
 
-		/* queue the buffer for some later OUT packet */
-		req->length = loop->buflen;
+		/* queue the buffer back to host or for next bunch of data */
 		status = usb_ep_queue(ep, req, GFP_ATOMIC);
-		if (status == 0)
+		if (status == 0) {
 			return;
+		} else {
+			ERROR(cdev, "Unable to loop back buffer to %s: %d\n",
+			      ep->name, status);
+			goto free_req;
+		}
 
 		/* "should never get here" */
-		/* FALLTHROUGH */
-
 	default:
 		ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
 				status, req->actual, req->length);
@@ -274,6 +290,10 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
 	case -ECONNABORTED:		/* hardware forced ep reset */
 	case -ECONNRESET:		/* request dequeued */
 	case -ESHUTDOWN:		/* disconnect from host */
+free_req:
+		usb_ep_free_request(ep == loop->in_ep ?
+				    loop->out_ep : loop->in_ep,
+				    req->context);
 		free_ep_req(ep, req);
 		return;
 	}
@@ -295,50 +315,72 @@ static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
 	return alloc_ep_req(ep, len, loop->buflen);
 }
 
-static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
-		struct usb_ep *ep)
+static int alloc_requests(struct usb_composite_dev *cdev,
+			  struct f_loopback *loop)
 {
-	struct usb_request			*req;
-	unsigned				i;
-	int					result;
-
-	/*
-	 * one endpoint writes data back IN to the host while another endpoint
-	 * just reads OUT packets
-	 */
-	result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
-	if (result)
-		goto fail0;
-	result = usb_ep_enable(ep);
-	if (result < 0)
-		goto fail0;
-	ep->driver_data = loop;
+	struct usb_request *in_req, *out_req;
+	int i;
+	int result = 0;
 
 	/*
 	 * allocate a bunch of read buffers and queue them all at once.
-	 * we buffer at most 'qlen' transfers; fewer if any need more
-	 * than 'buflen' bytes each.
+	 * we buffer at most 'qlen' transfers; We allocate buffers only
+	 * for out transfer and reuse them in IN transfers to implement
+	 * our loopback functionality
 	 */
 	for (i = 0; i < loop->qlen && result == 0; i++) {
-		req = lb_alloc_ep_req(ep, 0);
-		if (!req)
-			goto fail1;
+		result = -ENOMEM;
+
+		in_req = usb_ep_alloc_request(loop->in_ep, GFP_KERNEL);
+		if (!in_req)
+			goto fail;
+
+		out_req = lb_alloc_ep_req(loop->out_ep, 0);
+		if (!out_req)
+			goto fail_in;
+
+		in_req->complete = loopback_complete;
+		out_req->complete = loopback_complete;
+
+		in_req->buf = out_req->buf;
+		/* length will be set in complete routine */
+		in_req->context = out_req;
+		out_req->context = in_req;
 
-		req->complete = loopback_complete;
-		result = usb_ep_queue(ep, req, GFP_ATOMIC);
+		result = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
 		if (result) {
 			ERROR(cdev, "%s queue req --> %d\n",
-					ep->name, result);
-			goto fail1;
+					loop->out_ep->name, result);
+			goto fail_out;
 		}
 	}
 
 	return 0;
 
-fail1:
-	usb_ep_disable(ep);
+fail_out:
+	free_ep_req(loop->out_ep, out_req);
+fail_in:
+	usb_ep_free_request(loop->in_ep, in_req);
+fail:
+	return result;
+}
+
+static int enable_endpoint(struct usb_composite_dev *cdev,
+			   struct f_loopback *loop, struct usb_ep *ep)
+{
+	int					result;
+
+	result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
+	if (result)
+		goto out;
 
-fail0:
+	result = usb_ep_enable(ep);
+	if (result < 0)
+		goto out;
+	ep->driver_data = loop;
+	result = 0;
+
+out:
 	return result;
 }
 
@@ -349,13 +391,24 @@ enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
 
 	result = enable_endpoint(cdev, loop, loop->in_ep);
 	if (result)
-		return result;
+		goto out;
 
 	result = enable_endpoint(cdev, loop, loop->out_ep);
 	if (result)
-		return result;
+		goto disable_in;
+
+	result = alloc_requests(cdev, loop);
+	if (result)
+		goto disable_out;
 
 	DBG(cdev, "%s enabled\n", loop->function.name);
+	return 0;
+
+disable_out:
+	usb_ep_disable(loop->out_ep);
+disable_in:
+	usb_ep_disable(loop->in_ep);
+out:
 	return result;
 }
 
-- 
1.7.9.5


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

* Re: [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
  2015-09-22 18:40 [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances Krzysztof Opasiak
  2015-09-22 18:40 ` [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation Krzysztof Opasiak
@ 2015-09-24 15:19 ` Robert Baldyga
  2015-09-24 16:51   ` Felipe Balbi
  1 sibling, 1 reply; 10+ messages in thread
From: Robert Baldyga @ 2015-09-24 15:19 UTC (permalink / raw)
  To: Krzysztof Opasiak, balbi, gregkh; +Cc: linux-usb, andrzej.p, stable

On 09/22/2015 08:40 PM, Krzysztof Opasiak wrote:
> Each instance of loopback function may have different qlen
> and buflen attributes values. When linking function to
> configuration those values had been assigned to global
> variables. Linking other instance to config overwrites those
> values.
> 
> This commit moves those values to f_loopback structure
> to avoid overwriting. Now each function has its own instance
> of those values.
> 
> Cc: <stable@vger.kernel.org> # 3.10+
> Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>

Reviewed-by: Robert Baldyga <r.baldyga@samsung.com>

> ---
>  drivers/usb/gadget/function/f_loopback.c |   24 +++++++++++++-----------
>  1 file changed, 13 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
> index 6e2fe63..e4bfed4 100644
> --- a/drivers/usb/gadget/function/f_loopback.c
> +++ b/drivers/usb/gadget/function/f_loopback.c
> @@ -34,6 +34,9 @@ struct f_loopback {
>  
>  	struct usb_ep		*in_ep;
>  	struct usb_ep		*out_ep;
> +
> +	unsigned                qlen;
> +	unsigned                buflen;
>  };
>  
>  static inline struct f_loopback *func_to_loop(struct usb_function *f)
> @@ -41,13 +44,10 @@ static inline struct f_loopback *func_to_loop(struct usb_function *f)
>  	return container_of(f, struct f_loopback, function);
>  }
>  
> -static unsigned qlen;
> -static unsigned buflen;
> -
>  /*-------------------------------------------------------------------------*/
>  
>  static struct usb_interface_descriptor loopback_intf = {
> -	.bLength =		sizeof loopback_intf,
> +	.bLength =		sizeof(loopback_intf),
>  	.bDescriptorType =	USB_DT_INTERFACE,
>  
>  	.bNumEndpoints =	2,
> @@ -253,7 +253,7 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
>  		}
>  
>  		/* queue the buffer for some later OUT packet */
> -		req->length = buflen;
> +		req->length = loop->buflen;
>  		status = usb_ep_queue(ep, req, GFP_ATOMIC);
>  		if (status == 0)
>  			return;
> @@ -290,7 +290,9 @@ static void disable_loopback(struct f_loopback *loop)
>  
>  static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
>  {
> -	return alloc_ep_req(ep, len, buflen);
> +	struct f_loopback	*loop = ep->driver_data;
> +
> +	return alloc_ep_req(ep, len, loop->buflen);
>  }
>  
>  static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
> @@ -317,7 +319,7 @@ static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *lo
>  	 * we buffer at most 'qlen' transfers; fewer if any need more
>  	 * than 'buflen' bytes each.
>  	 */
> -	for (i = 0; i < qlen && result == 0; i++) {
> +	for (i = 0; i < loop->qlen && result == 0; i++) {
>  		req = lb_alloc_ep_req(ep, 0);
>  		if (!req)
>  			goto fail1;
> @@ -391,10 +393,10 @@ static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
>  	lb_opts->refcnt++;
>  	mutex_unlock(&lb_opts->lock);
>  
> -	buflen = lb_opts->bulk_buflen;
> -	qlen = lb_opts->qlen;
> -	if (!qlen)
> -		qlen = 32;
> +	loop->buflen = lb_opts->bulk_buflen;
> +	loop->qlen = lb_opts->qlen;
> +	if (!loop->qlen)
> +		loop->qlen = 32;
>  
>  	loop->function.name = "loopback";
>  	loop->function.bind = loopback_bind;
> 


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

* Re: [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
  2015-09-24 15:19 ` [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances Robert Baldyga
@ 2015-09-24 16:51   ` Felipe Balbi
  2015-09-24 17:10     ` Krzysztof Opasiak
  0 siblings, 1 reply; 10+ messages in thread
From: Felipe Balbi @ 2015-09-24 16:51 UTC (permalink / raw)
  To: Robert Baldyga
  Cc: Krzysztof Opasiak, balbi, gregkh, linux-usb, andrzej.p, stable

[-- Attachment #1: Type: text/plain, Size: 3737 bytes --]

On Thu, Sep 24, 2015 at 05:19:12PM +0200, Robert Baldyga wrote:
> On 09/22/2015 08:40 PM, Krzysztof Opasiak wrote:
> > Each instance of loopback function may have different qlen
> > and buflen attributes values. When linking function to
> > configuration those values had been assigned to global
> > variables. Linking other instance to config overwrites those
> > values.
> > 
> > This commit moves those values to f_loopback structure
> > to avoid overwriting. Now each function has its own instance
> > of those values.
> > 
> > Cc: <stable@vger.kernel.org> # 3.10+
> > Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
> 
> Reviewed-by: Robert Baldyga <r.baldyga@samsung.com>

doesn't seem to fit stable IMO. Care to explain why you think we need
to backport this to v3.10+ ?

> 
> > ---
> >  drivers/usb/gadget/function/f_loopback.c |   24 +++++++++++++-----------
> >  1 file changed, 13 insertions(+), 11 deletions(-)
> > 
> > diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
> > index 6e2fe63..e4bfed4 100644
> > --- a/drivers/usb/gadget/function/f_loopback.c
> > +++ b/drivers/usb/gadget/function/f_loopback.c
> > @@ -34,6 +34,9 @@ struct f_loopback {
> >  
> >  	struct usb_ep		*in_ep;
> >  	struct usb_ep		*out_ep;
> > +
> > +	unsigned                qlen;
> > +	unsigned                buflen;
> >  };
> >  
> >  static inline struct f_loopback *func_to_loop(struct usb_function *f)
> > @@ -41,13 +44,10 @@ static inline struct f_loopback *func_to_loop(struct usb_function *f)
> >  	return container_of(f, struct f_loopback, function);
> >  }
> >  
> > -static unsigned qlen;
> > -static unsigned buflen;
> > -
> >  /*-------------------------------------------------------------------------*/
> >  
> >  static struct usb_interface_descriptor loopback_intf = {
> > -	.bLength =		sizeof loopback_intf,
> > +	.bLength =		sizeof(loopback_intf),
> >  	.bDescriptorType =	USB_DT_INTERFACE,
> >  
> >  	.bNumEndpoints =	2,
> > @@ -253,7 +253,7 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
> >  		}
> >  
> >  		/* queue the buffer for some later OUT packet */
> > -		req->length = buflen;
> > +		req->length = loop->buflen;
> >  		status = usb_ep_queue(ep, req, GFP_ATOMIC);
> >  		if (status == 0)
> >  			return;
> > @@ -290,7 +290,9 @@ static void disable_loopback(struct f_loopback *loop)
> >  
> >  static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
> >  {
> > -	return alloc_ep_req(ep, len, buflen);
> > +	struct f_loopback	*loop = ep->driver_data;
> > +
> > +	return alloc_ep_req(ep, len, loop->buflen);
> >  }
> >  
> >  static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
> > @@ -317,7 +319,7 @@ static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *lo
> >  	 * we buffer at most 'qlen' transfers; fewer if any need more
> >  	 * than 'buflen' bytes each.
> >  	 */
> > -	for (i = 0; i < qlen && result == 0; i++) {
> > +	for (i = 0; i < loop->qlen && result == 0; i++) {
> >  		req = lb_alloc_ep_req(ep, 0);
> >  		if (!req)
> >  			goto fail1;
> > @@ -391,10 +393,10 @@ static struct usb_function *loopback_alloc(struct usb_function_instance *fi)
> >  	lb_opts->refcnt++;
> >  	mutex_unlock(&lb_opts->lock);
> >  
> > -	buflen = lb_opts->bulk_buflen;
> > -	qlen = lb_opts->qlen;
> > -	if (!qlen)
> > -		qlen = 32;
> > +	loop->buflen = lb_opts->bulk_buflen;
> > +	loop->qlen = lb_opts->qlen;
> > +	if (!loop->qlen)
> > +		loop->qlen = 32;
> >  
> >  	loop->function.name = "loopback";
> >  	loop->function.bind = loopback_bind;
> > 
> 

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
  2015-09-24 16:51   ` Felipe Balbi
@ 2015-09-24 17:10     ` Krzysztof Opasiak
  2015-09-30 16:08       ` Felipe Balbi
  0 siblings, 1 reply; 10+ messages in thread
From: Krzysztof Opasiak @ 2015-09-24 17:10 UTC (permalink / raw)
  To: balbi, Robert Baldyga; +Cc: gregkh, linux-usb, andrzej.p, stable



On 09/24/2015 06:51 PM, Felipe Balbi wrote:
> On Thu, Sep 24, 2015 at 05:19:12PM +0200, Robert Baldyga wrote:
>> On 09/22/2015 08:40 PM, Krzysztof Opasiak wrote:
>>> Each instance of loopback function may have different qlen
>>> and buflen attributes values. When linking function to
>>> configuration those values had been assigned to global
>>> variables. Linking other instance to config overwrites those
>>> values.
>>>
>>> This commit moves those values to f_loopback structure
>>> to avoid overwriting. Now each function has its own instance
>>> of those values.
>>>
>>> Cc: <stable@vger.kernel.org> # 3.10+
>>> Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
>>
>> Reviewed-by: Robert Baldyga <r.baldyga@samsung.com>
>
> doesn't seem to fit stable IMO. Care to explain why you think we need
> to backport this to v3.10+ ?
>

Let's consider following set of commands:

cd $CFS_ROOT/usb_gadget
mkdir g1
cd g1
mkdir functions/SourceSink.1
mkdir functions/SourceSink.2
echo 1024 > functions/SourceSink.1/buflen
echo 512 > functions/SourceSink.2/buflen
mkdir configs/c.1
ln -s functions/SourceSink.1 configs/c.1/
ln -s functions/SourceSink.2 configs/c.1/
echo $UDC > UDC

You assume that after executing this script you will get two instances 
of SourceSink functions and one of them will alloc requests buffers 
which has 1024 and second one which has only 512.

Unfortunately due to using global variables both of them are going to 
alloc only 512 bytes for each request. The same situation is with qlen. 
All instances are going to use attributes values from the last one in 
the system, even if they are in multiple gadgets.

In my opinion second patch in this series is much more important as it 
makes loopback function working again after almost year of being only 
/dev/null and /dev/zero. But that one should to go only to 3.18.

Best regards,
-- 
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics

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

* Re: [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
  2015-09-24 17:10     ` Krzysztof Opasiak
@ 2015-09-30 16:08       ` Felipe Balbi
  0 siblings, 0 replies; 10+ messages in thread
From: Felipe Balbi @ 2015-09-30 16:08 UTC (permalink / raw)
  To: Krzysztof Opasiak
  Cc: balbi, Robert Baldyga, gregkh, linux-usb, andrzej.p, stable

[-- Attachment #1: Type: text/plain, Size: 2306 bytes --]

On Thu, Sep 24, 2015 at 07:10:53PM +0200, Krzysztof Opasiak wrote:
> 
> 
> On 09/24/2015 06:51 PM, Felipe Balbi wrote:
> >On Thu, Sep 24, 2015 at 05:19:12PM +0200, Robert Baldyga wrote:
> >>On 09/22/2015 08:40 PM, Krzysztof Opasiak wrote:
> >>>Each instance of loopback function may have different qlen
> >>>and buflen attributes values. When linking function to
> >>>configuration those values had been assigned to global
> >>>variables. Linking other instance to config overwrites those
> >>>values.
> >>>
> >>>This commit moves those values to f_loopback structure
> >>>to avoid overwriting. Now each function has its own instance
> >>>of those values.
> >>>
> >>>Cc: <stable@vger.kernel.org> # 3.10+
> >>>Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
> >>
> >>Reviewed-by: Robert Baldyga <r.baldyga@samsung.com>
> >
> >doesn't seem to fit stable IMO. Care to explain why you think we need
> >to backport this to v3.10+ ?
> >
> 
> Let's consider following set of commands:
> 
> cd $CFS_ROOT/usb_gadget
> mkdir g1
> cd g1
> mkdir functions/SourceSink.1
> mkdir functions/SourceSink.2
> echo 1024 > functions/SourceSink.1/buflen
> echo 512 > functions/SourceSink.2/buflen
> mkdir configs/c.1
> ln -s functions/SourceSink.1 configs/c.1/
> ln -s functions/SourceSink.2 configs/c.1/
> echo $UDC > UDC
> 
> You assume that after executing this script you will get two instances of
> SourceSink functions and one of them will alloc requests buffers which has
> 1024 and second one which has only 512.
> 
> Unfortunately due to using global variables both of them are going to alloc
> only 512 bytes for each request. The same situation is with qlen. All
> instances are going to use attributes values from the last one in the
> system, even if they are in multiple gadgets.

that's all fine and dandy, but it's still a new requirement. It fits easily
into "has never worked before" category. If it was something which really
regressed, IOW we used to support it but some other commit broke it, then
I'd agree.

> In my opinion second patch in this series is much more important as it makes
> loopback function working again after almost year of being only /dev/null
> and /dev/zero. But that one should to go only to 3.18.

ok

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation
  2015-09-22 18:40 ` [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation Krzysztof Opasiak
@ 2015-09-30 16:11   ` Felipe Balbi
  2015-09-30 16:15     ` Felipe Balbi
  0 siblings, 1 reply; 10+ messages in thread
From: Felipe Balbi @ 2015-09-30 16:11 UTC (permalink / raw)
  To: Krzysztof Opasiak; +Cc: balbi, gregkh, linux-usb, andrzej.p, stable

[-- Attachment #1: Type: text/plain, Size: 6910 bytes --]

On Tue, Sep 22, 2015 at 08:40:23PM +0200, Krzysztof Opasiak wrote:
> Since:
> 
> commit e0857ce58e8658657f5f12fe25272b93cfeb16aa

this should be something like:

Since commit e0857ce58e86 (" .... ")

> ("usb: gadget: loopback: don't queue requests to bogus endpoints")
> 
> Loopback function is not realy working as that commit removed
> all looping back logic. After that commit ep-out works like
> /dev/null and ep-in works like /dev/zero.
> 
> This commit fix this issue by allocating set of out requests
> and set of in requests but each out req shares buffer with
> one in req:
> 
> out_req->buf ---> buf <--- in_req.buf
> out_req->context <---> in_req.context
> 
> The completion routine simply  enqueue the suitable req in
> an oposite direction.
> 
> Cc: <stable@vger.kernel.org> # 3.18+

missing Fixes: e0857ce58e86 ("...")

I'll fix both while applying, but make sure to make this proper
next time.

> Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
> ---
> Changes since v2:
> - fix requests context assignment
> 
> Changes since v1:
> - add missing usb_ep_free_request() in complete() callback
> ---
>  drivers/usb/gadget/function/f_loopback.c |  131 +++++++++++++++++++++---------
>  1 file changed, 92 insertions(+), 39 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
> index e4bfed4..c369554 100644
> --- a/drivers/usb/gadget/function/f_loopback.c
> +++ b/drivers/usb/gadget/function/f_loopback.c
> @@ -245,22 +245,38 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
>  	int			status = req->status;
>  
>  	switch (status) {
> -
>  	case 0:				/* normal completion? */
>  		if (ep == loop->out_ep) {
> -			req->zero = (req->actual < req->length);
> -			req->length = req->actual;
> +			/*
> +			 * We received some data from the host so let's
> +			 * queue it so host can read the from our in ep
> +			 */
> +			struct usb_request *in_req = req->context;
> +
> +			in_req->zero = (req->actual < req->length);
> +			in_req->length = req->actual;
> +			ep = loop->in_ep;
> +			req = in_req;
> +		} else {
> +			/*
> +			 * We have just looped back a bunch of data
> +			 * to host. Now let's wait for some more data.
> +			 */
> +			req = req->context;
> +			ep = loop->out_ep;
>  		}
>  
> -		/* queue the buffer for some later OUT packet */
> -		req->length = loop->buflen;
> +		/* queue the buffer back to host or for next bunch of data */
>  		status = usb_ep_queue(ep, req, GFP_ATOMIC);
> -		if (status == 0)
> +		if (status == 0) {
>  			return;
> +		} else {
> +			ERROR(cdev, "Unable to loop back buffer to %s: %d\n",
> +			      ep->name, status);
> +			goto free_req;
> +		}
>  
>  		/* "should never get here" */
> -		/* FALLTHROUGH */
> -
>  	default:
>  		ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name,
>  				status, req->actual, req->length);
> @@ -274,6 +290,10 @@ static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
>  	case -ECONNABORTED:		/* hardware forced ep reset */
>  	case -ECONNRESET:		/* request dequeued */
>  	case -ESHUTDOWN:		/* disconnect from host */
> +free_req:
> +		usb_ep_free_request(ep == loop->in_ep ?
> +				    loop->out_ep : loop->in_ep,
> +				    req->context);
>  		free_ep_req(ep, req);
>  		return;
>  	}
> @@ -295,50 +315,72 @@ static inline struct usb_request *lb_alloc_ep_req(struct usb_ep *ep, int len)
>  	return alloc_ep_req(ep, len, loop->buflen);
>  }
>  
> -static int enable_endpoint(struct usb_composite_dev *cdev, struct f_loopback *loop,
> -		struct usb_ep *ep)
> +static int alloc_requests(struct usb_composite_dev *cdev,
> +			  struct f_loopback *loop)
>  {
> -	struct usb_request			*req;
> -	unsigned				i;
> -	int					result;
> -
> -	/*
> -	 * one endpoint writes data back IN to the host while another endpoint
> -	 * just reads OUT packets
> -	 */
> -	result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
> -	if (result)
> -		goto fail0;
> -	result = usb_ep_enable(ep);
> -	if (result < 0)
> -		goto fail0;
> -	ep->driver_data = loop;
> +	struct usb_request *in_req, *out_req;
> +	int i;
> +	int result = 0;
>  
>  	/*
>  	 * allocate a bunch of read buffers and queue them all at once.
> -	 * we buffer at most 'qlen' transfers; fewer if any need more
> -	 * than 'buflen' bytes each.
> +	 * we buffer at most 'qlen' transfers; We allocate buffers only
> +	 * for out transfer and reuse them in IN transfers to implement
> +	 * our loopback functionality
>  	 */
>  	for (i = 0; i < loop->qlen && result == 0; i++) {
> -		req = lb_alloc_ep_req(ep, 0);
> -		if (!req)
> -			goto fail1;
> +		result = -ENOMEM;
> +
> +		in_req = usb_ep_alloc_request(loop->in_ep, GFP_KERNEL);
> +		if (!in_req)
> +			goto fail;
> +
> +		out_req = lb_alloc_ep_req(loop->out_ep, 0);
> +		if (!out_req)
> +			goto fail_in;
> +
> +		in_req->complete = loopback_complete;
> +		out_req->complete = loopback_complete;
> +
> +		in_req->buf = out_req->buf;
> +		/* length will be set in complete routine */
> +		in_req->context = out_req;
> +		out_req->context = in_req;
>  
> -		req->complete = loopback_complete;
> -		result = usb_ep_queue(ep, req, GFP_ATOMIC);
> +		result = usb_ep_queue(loop->out_ep, out_req, GFP_ATOMIC);
>  		if (result) {
>  			ERROR(cdev, "%s queue req --> %d\n",
> -					ep->name, result);
> -			goto fail1;
> +					loop->out_ep->name, result);
> +			goto fail_out;
>  		}
>  	}
>  
>  	return 0;
>  
> -fail1:
> -	usb_ep_disable(ep);
> +fail_out:
> +	free_ep_req(loop->out_ep, out_req);
> +fail_in:
> +	usb_ep_free_request(loop->in_ep, in_req);
> +fail:
> +	return result;
> +}
> +
> +static int enable_endpoint(struct usb_composite_dev *cdev,
> +			   struct f_loopback *loop, struct usb_ep *ep)
> +{
> +	int					result;
> +
> +	result = config_ep_by_speed(cdev->gadget, &(loop->function), ep);
> +	if (result)
> +		goto out;
>  
> -fail0:
> +	result = usb_ep_enable(ep);
> +	if (result < 0)
> +		goto out;
> +	ep->driver_data = loop;
> +	result = 0;
> +
> +out:
>  	return result;
>  }
>  
> @@ -349,13 +391,24 @@ enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop)
>  
>  	result = enable_endpoint(cdev, loop, loop->in_ep);
>  	if (result)
> -		return result;
> +		goto out;
>  
>  	result = enable_endpoint(cdev, loop, loop->out_ep);
>  	if (result)
> -		return result;
> +		goto disable_in;
> +
> +	result = alloc_requests(cdev, loop);
> +	if (result)
> +		goto disable_out;
>  
>  	DBG(cdev, "%s enabled\n", loop->function.name);
> +	return 0;
> +
> +disable_out:
> +	usb_ep_disable(loop->out_ep);
> +disable_in:
> +	usb_ep_disable(loop->in_ep);
> +out:
>  	return result;
>  }
>  
> -- 
> 1.7.9.5
> 

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation
  2015-09-30 16:11   ` Felipe Balbi
@ 2015-09-30 16:15     ` Felipe Balbi
  2015-10-13 18:11       ` Krzysztof Opasiak
  0 siblings, 1 reply; 10+ messages in thread
From: Felipe Balbi @ 2015-09-30 16:15 UTC (permalink / raw)
  To: Felipe Balbi; +Cc: Krzysztof Opasiak, gregkh, linux-usb, andrzej.p, stable

[-- Attachment #1: Type: text/plain, Size: 1134 bytes --]

On Wed, Sep 30, 2015 at 11:11:52AM -0500, Felipe Balbi wrote:
> On Tue, Sep 22, 2015 at 08:40:23PM +0200, Krzysztof Opasiak wrote:
> > Since:
> > 
> > commit e0857ce58e8658657f5f12fe25272b93cfeb16aa
> 
> this should be something like:
> 
> Since commit e0857ce58e86 (" .... ")
> 
> > ("usb: gadget: loopback: don't queue requests to bogus endpoints")
> > 
> > Loopback function is not realy working as that commit removed
> > all looping back logic. After that commit ep-out works like
> > /dev/null and ep-in works like /dev/zero.
> > 
> > This commit fix this issue by allocating set of out requests
> > and set of in requests but each out req shares buffer with
> > one in req:
> > 
> > out_req->buf ---> buf <--- in_req.buf
> > out_req->context <---> in_req.context
> > 
> > The completion routine simply  enqueue the suitable req in
> > an oposite direction.
> > 
> > Cc: <stable@vger.kernel.org> # 3.18+
> 
> missing Fixes: e0857ce58e86 ("...")
> 
> I'll fix both while applying, but make sure to make this proper
> next time.

actually fails to apply to v4.3-rc3, please rebase.

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation
  2015-09-30 16:15     ` Felipe Balbi
@ 2015-10-13 18:11       ` Krzysztof Opasiak
  2015-10-13 18:16         ` Felipe Balbi
  0 siblings, 1 reply; 10+ messages in thread
From: Krzysztof Opasiak @ 2015-10-13 18:11 UTC (permalink / raw)
  To: balbi; +Cc: gregkh, linux-usb, andrzej.p, stable

Hi,

On 09/30/2015 06:15 PM, Felipe Balbi wrote:
> On Wed, Sep 30, 2015 at 11:11:52AM -0500, Felipe Balbi wrote:
>> On Tue, Sep 22, 2015 at 08:40:23PM +0200, Krzysztof Opasiak wrote:
>>> Since:
>>>
>>> commit e0857ce58e8658657f5f12fe25272b93cfeb16aa
>>
>> this should be something like:
>>
>> Since commit e0857ce58e86 (" .... ")
>>
>>> ("usb: gadget: loopback: don't queue requests to bogus endpoints")
>>>
>>> Loopback function is not realy working as that commit removed
>>> all looping back logic. After that commit ep-out works like
>>> /dev/null and ep-in works like /dev/zero.
>>>
>>> This commit fix this issue by allocating set of out requests
>>> and set of in requests but each out req shares buffer with
>>> one in req:
>>>
>>> out_req->buf ---> buf <--- in_req.buf
>>> out_req->context <---> in_req.context
>>>
>>> The completion routine simply  enqueue the suitable req in
>>> an oposite direction.
>>>
>>> Cc: <stable@vger.kernel.org> # 3.18+
>>
>> missing Fixes: e0857ce58e86 ("...")
>>
>> I'll fix both while applying, but make sure to make this proper
>> next time.
>
> actually fails to apply to v4.3-rc3, please rebase.
>

Sorry for my late response but I was out of office due to ELCE in Dublin.

I managed to apply this series onto 4.3-rc5 without any conflicts.

Would you like me to split this series into 2 separate one so you can 
apply only this commit and leave the first one for the next merge window?

Best regards,
-- 
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics

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

* Re: [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation
  2015-10-13 18:11       ` Krzysztof Opasiak
@ 2015-10-13 18:16         ` Felipe Balbi
  0 siblings, 0 replies; 10+ messages in thread
From: Felipe Balbi @ 2015-10-13 18:16 UTC (permalink / raw)
  To: Krzysztof Opasiak; +Cc: gregkh, linux-usb, andrzej.p, stable

[-- Attachment #1: Type: text/plain, Size: 1624 bytes --]


Hi,

Krzysztof Opasiak <k.opasiak@samsung.com> writes:
> Hi,
>
> On 09/30/2015 06:15 PM, Felipe Balbi wrote:
>> On Wed, Sep 30, 2015 at 11:11:52AM -0500, Felipe Balbi wrote:
>>> On Tue, Sep 22, 2015 at 08:40:23PM +0200, Krzysztof Opasiak wrote:
>>>> Since:
>>>>
>>>> commit e0857ce58e8658657f5f12fe25272b93cfeb16aa
>>>
>>> this should be something like:
>>>
>>> Since commit e0857ce58e86 (" .... ")
>>>
>>>> ("usb: gadget: loopback: don't queue requests to bogus endpoints")
>>>>
>>>> Loopback function is not realy working as that commit removed
>>>> all looping back logic. After that commit ep-out works like
>>>> /dev/null and ep-in works like /dev/zero.
>>>>
>>>> This commit fix this issue by allocating set of out requests
>>>> and set of in requests but each out req shares buffer with
>>>> one in req:
>>>>
>>>> out_req->buf ---> buf <--- in_req.buf
>>>> out_req->context <---> in_req.context
>>>>
>>>> The completion routine simply  enqueue the suitable req in
>>>> an oposite direction.
>>>>
>>>> Cc: <stable@vger.kernel.org> # 3.18+
>>>
>>> missing Fixes: e0857ce58e86 ("...")
>>>
>>> I'll fix both while applying, but make sure to make this proper
>>> next time.
>>
>> actually fails to apply to v4.3-rc3, please rebase.
>>
>
> Sorry for my late response but I was out of office due to ELCE in Dublin.
>
> I managed to apply this series onto 4.3-rc5 without any conflicts.
>
> Would you like me to split this series into 2 separate one so you can 
> apply only this commit and leave the first one for the next merge window?

yes, please. Thanks

-- 
balbi

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 818 bytes --]

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

end of thread, other threads:[~2015-10-13 18:16 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-22 18:40 [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances Krzysztof Opasiak
2015-09-22 18:40 ` [PATCH v3 2/2] usb: gadget: loopback: Fix looping back logic implementation Krzysztof Opasiak
2015-09-30 16:11   ` Felipe Balbi
2015-09-30 16:15     ` Felipe Balbi
2015-10-13 18:11       ` Krzysztof Opasiak
2015-10-13 18:16         ` Felipe Balbi
2015-09-24 15:19 ` [PATCH v3 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances Robert Baldyga
2015-09-24 16:51   ` Felipe Balbi
2015-09-24 17:10     ` Krzysztof Opasiak
2015-09-30 16:08       ` Felipe Balbi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox