Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1230458 > unrolled thread

[PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances

Started byKrzysztof Opasiak <k.opasiak@samsung.com>
First post2015-09-22 18:50 +0200
Last post2015-09-22 19:30 +0200
Articles 5 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen  between instances Krzysztof Opasiak <k.opasiak@samsung.com> - 2015-09-22 18:50 +0200
    [PATCH 2/2] usb: gadget: loopback: Fix looping back logic  implementation Krzysztof Opasiak <k.opasiak@samsung.com> - 2015-09-22 18:50 +0200
    Re: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and  buflen between instances Felipe Balbi <balbi@ti.com> - 2015-09-22 19:00 +0200
      Re: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and  buflen between instances Krzysztof Opasiak <k.opasiak@samsung.com> - 2015-09-22 19:00 +0200
        Re: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and  buflen between instances Felipe Balbi <balbi@ti.com> - 2015-09-22 19:30 +0200

#1230458 — [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances

FromKrzysztof Opasiak <k.opasiak@samsung.com>
Date2015-09-22 18:50 +0200
Subject[PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
Message-ID<qbyVs-2z7-9@gated-at.bofh.it>
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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1230464 — [PATCH 2/2] usb: gadget: loopback: Fix looping back logic implementation

FromKrzysztof Opasiak <k.opasiak@samsung.com>
Date2015-09-22 18:50 +0200
Subject[PATCH 2/2] usb: gadget: loopback: Fix looping back logic implementation
Message-ID<qbyVs-2z7-31@gated-at.bofh.it>
In reply to#1230458
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>
---
 drivers/usb/gadget/function/f_loopback.c |  128 +++++++++++++++++++++---------
 1 file changed, 89 insertions(+), 39 deletions(-)

diff --git a/drivers/usb/gadget/function/f_loopback.c b/drivers/usb/gadget/function/f_loopback.c
index e4bfed4..e96589b 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,7 @@ 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:
 		free_ep_req(ep, req);
 		return;
 	}
@@ -295,50 +312,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 = out_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 +388,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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1230485 — Re: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances

FromFelipe Balbi <balbi@ti.com>
Date2015-09-22 19:00 +0200
SubjectRe: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
Message-ID<qbz5a-2KT-55@gated-at.bofh.it>
In reply to#1230458

[Multipart message — attachments visible in raw view] — view raw

On Tue, Sep 22, 2015 at 06:39:26PM +0200, 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>

looks like you missed linux-usb on both patches. Care to resend ?

-- 
balbi

[toc] | [prev] | [next] | [standalone]


#1230494 — Re: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances

FromKrzysztof Opasiak <k.opasiak@samsung.com>
Date2015-09-22 19:00 +0200
SubjectRe: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
Message-ID<qbz5c-2KT-93@gated-at.bofh.it>
In reply to#1230485

On 09/22/2015 06:53 PM, Felipe Balbi wrote:
> On Tue, Sep 22, 2015 at 06:39:26PM +0200, 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>
>
> looks like you missed linux-usb on both patches. Care to resend ?
>

Sorry, just send v2 without reading your email because I have found some 
mem leak. I will resend v2 with cc to linux-usb.

Best regards,
-- 
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1230527 — Re: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances

FromFelipe Balbi <balbi@ti.com>
Date2015-09-22 19:30 +0200
SubjectRe: [PATCH 1/2] usb: gadget: loopback: fix: Don't share qlen and buflen between instances
Message-ID<qbzya-3xX-15@gated-at.bofh.it>
In reply to#1230494

[Multipart message — attachments visible in raw view] — view raw

On Tue, Sep 22, 2015 at 06:58:13PM +0200, Krzysztof Opasiak wrote:
> 
> 
> On 09/22/2015 06:53 PM, Felipe Balbi wrote:
> >On Tue, Sep 22, 2015 at 06:39:26PM +0200, 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>
> >
> >looks like you missed linux-usb on both patches. Care to resend ?
> >
> 
> Sorry, just send v2 without reading your email because I have found some mem
> leak. I will resend v2 with cc to linux-usb.

Thanks :-)

-- 
balbi

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web