usb: gadget: f_midi: Fixed a bug when buflen was smaller than wMaxPacketSize
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
@@ -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
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.
Signed-off-by Felipe F. Tonello <[email protected]>--- drivers/usb/gadget/function/f_midi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/usb/gadget/function/f_midi.c +0
-0
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
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.
Signed-off-by Felipe F. Tonello <[email protected]>--- 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
-0drivers/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->buflenis 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
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->buflenis less than wMaxPacketSize?
No. f_midi_handle_out_data() uses only the request buffer.
Regards,
Clemens
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.
Signed-off-by Felipe F. Tonello <[email protected]>
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
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
-0if (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»
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.
Signed-off-by Felipe F. Tonello <[email protected]>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
-0drivers/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
-0if (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
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
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.
Signed-off-by Felipe F. Tonello <[email protected]>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
-0drivers/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
Quoted 3 lines · Felipe Balbi
a USB packet, right. that's correct. But a struct
usb_requestcan 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»
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_requestcan 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
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_requestcan 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_requestand callcomplete()or it will always callcomplete()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
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_requestcan 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_requestand callcomplete()or it will always callcomplete()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
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_requestcan 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_requestand callcomplete()or it will always callcomplete()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
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_requestcan 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_requestand callcomplete()or it will always callcomplete()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