Skip to contentSkip to conversation

usb: gadget: f_midi: Fixed a bug when buflen was smaller than wMaxPacketSize

PATCH Superseded Felipe F. Tonello 5 participants 14 messages Mar 9, 2016 – Apr 4, 2016 USB

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: f_midi: Fixed a bug when buflen was smaller than wMaxPacketSize

buflen by default (256) is smaller than wMaxPacketSize (512) in high-speed devices.

That caused the OUT endpoint to freeze if the host send any data packet of length greater than 256 bytes.

This is an example dump of what happended on that enpoint:

HOST:   [DATA][Length=260][...]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
...
HOST:   [PING]
DEVICE: [NAK]

This patch fixes this problem by setting the minimum usb_request's buffer size for the OUT endpoint as its wMaxPacketSize.

---
 drivers/usb/gadget/function/f_midi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_midi.c +3 -1
index 8475e3dc82d4..826ba641f29d 100644
@@ -366,7 +366,9 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 	/* allocate a bunch of read buffers and queue them all at once. */
 	for (i = 0; i < midi->qlen && err == 0; i++) {
 		struct usb_request *req =
-			midi_alloc_ep_req(midi->out_ep, midi->buflen);
+			midi_alloc_ep_req(midi->out_ep,
+				max_t(unsigned, midi->buflen,
+					bulk_out_desc.wMaxPacketSize));
 		if (req == NULL)
 			return -ENOMEM;
 
Signature · 1 line
2.7.2
#1 lore raw
Quoted 30 lines · Felipe F. Tonello

buflen by default (256) is smaller than wMaxPacketSize (512) in high-speed devices.

That caused the OUT endpoint to freeze if the host send any data packet of length greater than 256 bytes.

This is an example dump of what happended on that enpoint:

HOST:   [DATA][Length=260][...]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
...
HOST:   [PING]
DEVICE: [NAK]

This patch fixes this problem by setting the minimum usb_request's buffer size for the OUT endpoint as its wMaxPacketSize.

---
 drivers/usb/gadget/function/f_midi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_midi.c +0 -0
index 8475e3dc82d4..826ba641f29d 100644

drivers/usb/gadget/function/f_midi.c static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) · line 374

Quoted 10 lines
drivers/usb/gadget/function/f_midi.c +3 -1
@@ -366,7 +366,9 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
        /* allocate a bunch of read buffers and queue them all at once. */
        for (i = 0; i < midi->qlen && err == 0; i++) {
                struct usb_request *req =
-                       midi_alloc_ep_req(midi->out_ep, midi->buflen);
+                       midi_alloc_ep_req(midi->out_ep,
+                               max_t(unsigned, midi->buflen,
+                                       bulk_out_desc.wMaxPacketSize));
                if (req == NULL)
                        return -ENOMEM;
 

Won't you get a buffer overrun if midi->buflen is less than wMaxPacketSize?

Regards, Steve

#2 lore raw

Hi Steve,

Quoted 26 lines · Steve Calfee
Quoted 25 lines · Felipe F. Tonello

buflen by default (256) is smaller than wMaxPacketSize (512) in high-speed devices.

That caused the OUT endpoint to freeze if the host send any data packet of length greater than 256 bytes.

This is an example dump of what happended on that enpoint:

HOST:   [DATA][Length=260][...]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
...
HOST:   [PING]
DEVICE: [NAK]

This patch fixes this problem by setting the minimum usb_request's buffer size for the OUT endpoint as its wMaxPacketSize.

---
 drivers/usb/gadget/function/f_midi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 17 lines
Quoted 5 lines
drivers/usb/gadget/function/f_midi.c +0 -0
index 8475e3dc82d4..826ba641f29d 100644

drivers/usb/gadget/function/f_midi.c static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) · line 374

Quoted 10 lines
drivers/usb/gadget/function/f_midi.c +3 -1
@@ -366,7 +366,9 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
        /* allocate a bunch of read buffers and queue them all at once. */
        for (i = 0; i < midi->qlen && err == 0; i++) {
                struct usb_request *req =
-                       midi_alloc_ep_req(midi->out_ep, midi->buflen);
+                       midi_alloc_ep_req(midi->out_ep,
+                               max_t(unsigned, midi->buflen,
+                                       bulk_out_desc.wMaxPacketSize));
                if (req == NULL)
                        return -ENOMEM;
 

Won't you get a buffer overrun if midi->buflen is less than wMaxPacketSize?

No, because of the *max_t(unsigned, midi->buflen,
bulk_out_desc.wMaxPacketSize)*.

Maybe that's not the most clear indentation but I had to break it in order to avoid passing 80 columns.

Signature · 1 line
Felipe
#3 lore raw
In reply to Steve Calfee
Quoted 5 lines · Steve Calfee
Quoted 4 lines · Felipe F. Tonello

[...]
This patch fixes this problem by setting the minimum usb_request's buffer size for the OUT endpoint as its wMaxPacketSize.

Quoted 10 lines

drivers/usb/gadget/function/f_midi.c static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) · line 369

Quoted 8 lines
drivers/usb/gadget/function/f_midi.c +3 -1
@@ -366,7 +366,9 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
                struct usb_request *req =
-                       midi_alloc_ep_req(midi->out_ep, midi->buflen);
+                       midi_alloc_ep_req(midi->out_ep,
+                               max_t(unsigned, midi->buflen,
+                                       bulk_out_desc.wMaxPacketSize));

Won't you get a buffer overrun if midi->buflen is less than wMaxPacketSize?

No. f_midi_handle_out_data() uses only the request buffer.

Regards,
Clemens

Acked-by on v1 1/1
#4 lore raw
In reply to Felipe F. Tonello
Quoted 17 lines · Felipe F. Tonello

buflen by default (256) is smaller than wMaxPacketSize (512) in high-speed devices.

That caused the OUT endpoint to freeze if the host send any data packet of length greater than 256 bytes.

This is an example dump of what happended on that enpoint:

HOST:   [DATA][Length=260][...]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
...
HOST:   [PING]
DEVICE: [NAK]
Quoted 4 lines

This patch fixes this problem by setting the minimum usb_request's buffer size for the OUT endpoint as its wMaxPacketSize.

But see comment below:

Quoted 9 lines
---
 drivers/usb/gadget/function/f_midi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_midi.c +0 -0
index 8475e3dc82d4..826ba641f29d 100644

drivers/usb/gadget/function/f_midi.c static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) · line 371

Quoted 7 lines
drivers/usb/gadget/function/f_midi.c +3 -1
@@ -366,7 +366,9 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 	/* allocate a bunch of read buffers and queue them all at once. */
 	for (i = 0; i < midi->qlen && err == 0; i++) {
 		struct usb_request *req =
-			midi_alloc_ep_req(midi->out_ep, midi->buflen);
+			midi_alloc_ep_req(midi->out_ep,
+				max_t(unsigned, midi->buflen,
+					bulk_out_desc.wMaxPacketSize));

Or, just:

+ midi_alloc_ep_req(midi->out_ep,
+ bulk_out_desc.wMaxPacketSize);

Packet cannot be greater than wMaxPacketSize so there is no need to allocate more (if buflen > wMaxPacketSize).

drivers/usb/gadget/function/f_midi.c static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) · line 374

Quoted 3 lines
drivers/usb/gadget/function/f_midi.c +0 -0
 		if (req == NULL)
 			return -ENOMEM;
 

I’m also wondering whether it would be beneficial to get rid of buflen all together and use wMaxPacketSie for in endpoints as well? Is that feasible?

Signature · 3 lines
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ
«If at first you don’t succeed, give up skydiving»
#5 lore raw

Hi Michal,

Quoted 49 lines · Michal Nazarewicz
Quoted 17 lines · Felipe F. Tonello

buflen by default (256) is smaller than wMaxPacketSize (512) in high-speed devices.

That caused the OUT endpoint to freeze if the host send any data packet of length greater than 256 bytes.

This is an example dump of what happended on that enpoint:

HOST:   [DATA][Length=260][...]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
...
HOST:   [PING]
DEVICE: [NAK]
Quoted 4 lines

This patch fixes this problem by setting the minimum usb_request's buffer size for the OUT endpoint as its wMaxPacketSize.

But see comment below:

Quoted 9 lines
---
 drivers/usb/gadget/function/f_midi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_midi.c +0 -0
index 8475e3dc82d4..826ba641f29d 100644

drivers/usb/gadget/function/f_midi.c static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) · line 371

Quoted 7 lines
drivers/usb/gadget/function/f_midi.c +3 -1
@@ -366,7 +366,9 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 	/* allocate a bunch of read buffers and queue them all at once. */
 	for (i = 0; i < midi->qlen && err == 0; i++) {
 		struct usb_request *req =
-			midi_alloc_ep_req(midi->out_ep, midi->buflen);
+			midi_alloc_ep_req(midi->out_ep,
+				max_t(unsigned, midi->buflen,
+					bulk_out_desc.wMaxPacketSize));

Or, just:

(quoted diff) +2 -0
+			midi_alloc_ep_req(midi->out_ep,
+					  bulk_out_desc.wMaxPacketSize);
Quoted 2 lines

Packet cannot be greater than wMaxPacketSize so there is no need to allocate more (if buflen > wMaxPacketSize).

I didn't know that was a constraint. If so, I agree with you.

Quoted 8 lines

drivers/usb/gadget/function/f_midi.c static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) · line 374

Quoted 4 lines
drivers/usb/gadget/function/f_midi.c +0 -0
 		if (req == NULL)
 			return -ENOMEM;
 

I’m also wondering whether it would be beneficial to get rid of buflen all together and use wMaxPacketSie for in endpoints as well? Is that feasible?

Yes, we could just remove the buflen parameter.

The only scenario where I can see buflen been "useful" is if the user wants to have a smaller buffer size for the OUT endpoint. Should we support this case or not?

I can rework this patch for any case we agree on.

Signature · 1 line
Felipe
#6 lore raw
Quoted 10 lines · Felipe Ferreri Tonello
Quoted 3 lines · Michal Nazarewicz

I’m also wondering whether it would be beneficial to get rid of buflen all together and use wMaxPacketSie for in endpoints as well? Is that feasible?

Yes, we could just remove the buflen parameter.

The only scenario where I can see buflen been "useful" is if the user wants to have a smaller buffer size for the OUT endpoint. Should we support this case or not?

Splitting data into multiple packets would not make sense from a performance perspective. The only possible reason would be to work around a (theoretical) bug in some host software that does not handle larger buffers, but there aren't that many host implementations, and I am not aware of any with such a bug.

Regards,
Clemens

#7 lore raw
In reply to Michal Nazarewicz
Quoted 50 lines · Michal Nazarewicz

[ text/plain ]

Quoted 17 lines · Felipe F. Tonello

buflen by default (256) is smaller than wMaxPacketSize (512) in high-speed devices.

That caused the OUT endpoint to freeze if the host send any data packet of length greater than 256 bytes.

This is an example dump of what happended on that enpoint:

HOST:   [DATA][Length=260][...]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
HOST:   [PING]
DEVICE: [NAK]
...
HOST:   [PING]
DEVICE: [NAK]
Quoted 4 lines

This patch fixes this problem by setting the minimum usb_request's buffer size for the OUT endpoint as its wMaxPacketSize.

But see comment below:

Quoted 9 lines
---
 drivers/usb/gadget/function/f_midi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_midi.c +0 -0
index 8475e3dc82d4..826ba641f29d 100644

drivers/usb/gadget/function/f_midi.c static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) · line 371

Quoted 7 lines
drivers/usb/gadget/function/f_midi.c +3 -1
@@ -366,7 +366,9 @@ static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
 	/* allocate a bunch of read buffers and queue them all at once. */
 	for (i = 0; i < midi->qlen && err == 0; i++) {
 		struct usb_request *req =
-			midi_alloc_ep_req(midi->out_ep, midi->buflen);
+			midi_alloc_ep_req(midi->out_ep,
+				max_t(unsigned, midi->buflen,
+					bulk_out_desc.wMaxPacketSize));

Or, just:

(quoted diff) +2 -0
+			midi_alloc_ep_req(midi->out_ep,
+					  bulk_out_desc.wMaxPacketSize);
Quoted 2 lines

Packet cannot be greater than wMaxPacketSize so there is no need to allocate more (if buflen > wMaxPacketSize).

a USB packet, right. that's correct. But a struct usb_request can point to whatever size buffer it wants and UDC is required to split that into wMaxPacketSize transfers.

Signature · 1 line
balbi
#8 lore raw
Quoted 3 lines · Felipe Balbi

a USB packet, right. that's correct. But a struct usb_request can point to whatever size buffer it wants and UDC is required to split that into wMaxPacketSize transfers.

D’oh. Of course. Disregard all my comments on the patch (except for Ack).

Signature · 3 lines
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ
«If at first you don’t succeed, give up skydiving»
#9 lore raw

Hi Balbi and Mina,

Quoted 8 lines · Michal Nazarewicz
Quoted 3 lines · Felipe Balbi

a USB packet, right. that's correct. But a struct usb_request can point to whatever size buffer it wants and UDC is required to split that into wMaxPacketSize transfers.

D’oh. Of course. Disregard all my comments on the patch (except for Ack).

I didn't really get it. Does that mean that if buflen is multiple of wMaxPacketSize, the UDC driver should fit as many [DATA] packets into one usb_request and call complete() or it will always call complete() on each [DATA] packet, thus not requiring buflen at all?

Does that mean that we can still use buflen and this patch is still valid? (besides the endianess issue that was addressed on v2)

Signature · 1 line
Felipe
#10 lore raw

Hi,

Quoted 12 lines · Felipe Ferreri Tonello

Hi Balbi and Mina,

Quoted 8 lines · Michal Nazarewicz
Quoted 3 lines · Felipe Balbi

a USB packet, right. that's correct. But a struct usb_request can point to whatever size buffer it wants and UDC is required to split that into wMaxPacketSize transfers.

D’oh. Of course. Disregard all my comments on the patch (except for Ack).

Quoted 7 lines

I didn't really get it. Does that mean that if buflen is multiple of wMaxPacketSize, the UDC driver should fit as many [DATA] packets into one usb_request and call complete() or it will always call complete() on each [DATA] packet, thus not requiring buflen at all?

Does that mean that we can still use buflen and this patch is still valid? (besides the endianess issue that was addressed on v2)

if you have e.g. 2048 bytes of data to transfer and wMaxPacketSize is e.g. 256 bytes, the UDC controller is required to do whatever it needs to do to transfer 2048 bytes (or less if there's a short packet).

You don't need to break these 2048 bytes into several requests yourself, the UDC is required to do that for the gadget.

Signature · 1 line
balbi
#11 lore raw

Hi Balbi,

Quoted 24 lines · Felipe Balbi

Hi,

Quoted 12 lines · Felipe Ferreri Tonello

Hi Balbi and Mina,

Quoted 8 lines · Michal Nazarewicz
Quoted 3 lines · Felipe Balbi

a USB packet, right. that's correct. But a struct usb_request can point to whatever size buffer it wants and UDC is required to split that into wMaxPacketSize transfers.

D’oh. Of course. Disregard all my comments on the patch (except for Ack).

Quoted 7 lines

I didn't really get it. Does that mean that if buflen is multiple of wMaxPacketSize, the UDC driver should fit as many [DATA] packets into one usb_request and call complete() or it will always call complete() on each [DATA] packet, thus not requiring buflen at all?

Does that mean that we can still use buflen and this patch is still valid? (besides the endianess issue that was addressed on v2)

Quoted 6 lines

if you have e.g. 2048 bytes of data to transfer and wMaxPacketSize is e.g. 256 bytes, the UDC controller is required to do whatever it needs to do to transfer 2048 bytes (or less if there's a short packet).

You don't need to break these 2048 bytes into several requests yourself, the UDC is required to do that for the gadget.

Right, what about OUT endpoints?

So that means that buflen is still usable, at least on IN endpoints.

Signature · 1 line
Felipe
#12 lore raw

Hi,

Quoted 18 lines · Felipe Ferreri Tonello
Quoted 18 lines
Quoted 17 lines · Michal Nazarewicz
Quoted 8 lines · Felipe Balbi
Quoted 3 lines

a USB packet, right. that's correct. But a struct usb_request can point to whatever size buffer it wants and UDC is required to split that into wMaxPacketSize transfers.

D’oh. Of course. Disregard all my comments on the patch (except for Ack).

I didn't really get it. Does that mean that if buflen is multiple of wMaxPacketSize, the UDC driver should fit as many [DATA] packets into one usb_request and call complete() or it will always call complete() on each [DATA] packet, thus not requiring buflen at all?

Does that mean that we can still use buflen and this patch is still valid? (besides the endianess issue that was addressed on v2)

Quoted 8 lines
Quoted 6 lines

if you have e.g. 2048 bytes of data to transfer and wMaxPacketSize is e.g. 256 bytes, the UDC controller is required to do whatever it needs to do to transfer 2048 bytes (or less if there's a short packet).

You don't need to break these 2048 bytes into several requests yourself, the UDC is required to do that for the gadget.

Right, what about OUT endpoints?

also applicable

Signature · 1 line
balbi
#13 lore raw

Hi Balbi,

Quoted 26 lines · Felipe Balbi

Hi,

Quoted 22 lines · Felipe Ferreri Tonello
Quoted 22 lines
Quoted 10 lines · Michal Nazarewicz
Quoted 8 lines · Felipe Balbi
Quoted 3 lines

a USB packet, right. that's correct. But a struct usb_request can point to whatever size buffer it wants and UDC is required to split that into wMaxPacketSize transfers.

D’oh. Of course. Disregard all my comments on the patch (except for Ack).

Quoted 7 lines

I didn't really get it. Does that mean that if buflen is multiple of wMaxPacketSize, the UDC driver should fit as many [DATA] packets into one usb_request and call complete() or it will always call complete() on each [DATA] packet, thus not requiring buflen at all?

Does that mean that we can still use buflen and this patch is still valid? (besides the endianess issue that was addressed on v2)

if you have e.g. 2048 bytes of data to transfer and wMaxPacketSize is e.g. 256 bytes, the UDC controller is required to do whatever it needs to do to transfer 2048 bytes (or less if there's a short packet).

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

You don't need to break these 2048 bytes into several requests yourself, the UDC is required to do that for the gadget.

Right, what about OUT endpoints?

also applicable

Ok, so I will make few tests here and resend a v3 probably with buflen still.

Signature · 1 line
Felipe