add gadget quirk to adapt f_fs for DWC3
Subsystems (2)
v3 cover add gadget quirk to adapt f_fs for DWC3
Hi,
These patches are a proposal to add gadget quirks in an immediate objective to adapt f_fs when using DWC3 controller. But the quirk solution is generic and can be used by other controllers to adapt gadget functions to their non-standard restrictions.
This change is necessary to make Android's adbd service to work on Intel Merrifield with f_fs instead of out-of-tree android gadget.
---
David Cohen (3):
usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
usb: ffs: check quirk to pad epout buf size when not aligned to
maxpacketsize
usb: dwc3: add quirk USB_GADGET_QUIRK_EP_OUT_ALIGNED_SIZE to gadget
driver
drivers/usb/dwc3/gadget.c | 6 ++++++ drivers/usb/gadget/f_fs.c | 17 +++++++++++++++++ include/linux/usb/gadget.h | 3 +++ 3 files changed, 26 insertions(+)
Signature · 1 line
1.8.4.rc3
PATCH v3 1/3 usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
Due to USB controllers may have different restrictions, usb gadget layer needs to provide a generic way to inform gadget functions to complain with non-standard requirements.
This patch adds 'quirk_ep_out_aligned_size' field to struct usb_gadget to inform when controller's epout requires buffer size to be aligned to MaxPacketSize.
--- include/linux/usb/gadget.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/usb/gadget.h +3 -0
@@ -540,6 +540,9 @@ struct usb_gadget {struct device dev;unsigned out_epnum;unsigned in_epnum; + +/* epout requires buffer size to be aligned to MaxPacketSize */ + unsigned quirk_ep_out_aligned_size:1; };#define work_to_gadget (w) (container_of((w), struct usb_gadget, work))
Signature · 1 line
1.8.4.rc3
Re: [PATCH v3 1/3] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
Quoted 18 lines · David Cohen
Due to USB controllers may have different restrictions, usb gadget layer needs to provide a generic way to inform gadget functions to complain with non-standard requirements.
This patch adds '
quirk_ep_out_aligned_size' field to structusb_gadgetto inform when controller's epout requires buffer size to be aligned to MaxPacketSize.Signed-off-by David Cohen <[email protected]>--- include/linux/usb/gadget.h | 3 +++ 1 file changed, 3 insertions(+)include/linux/usb/gadget.h +0
-0
include/linux/usb/gadget.h struct usb_gadget { · line 547
Quoted 8 lines
include/linux/usb/gadget.h +3
-0@@ -540,6 +540,9 @@ struct usb_gadget {struct device dev;unsigned out_epnum;unsigned in_epnum; + +/* epout requires buffer size to be aligned to MaxPacketSize */ + unsigned quirk_ep_out_aligned_size:1; };#define work_to_gadget (w) (container_of((w), struct usb_gadget, work))
Instead of adding this new bitflag to the end of the structure, where it will cause the structure to grow by at least 4 bytes, why not add it in the middle, along with all the other bitflags, where it won't cause any change in the structure's size?
If you prefer, you can move the name, dev, out_epnum, and in_epnum fields higher up, so that the bitflags come last.
Alan Stern
Re: [PATCH v3 1/3] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
Quoted 34 lines · Alan Stern
Quoted 18 lines · David Cohen
Due to USB controllers may have different restrictions, usb gadget layer needs to provide a generic way to inform gadget functions to complain with non-standard requirements.
This patch adds '
quirk_ep_out_aligned_size' field to structusb_gadgetto inform when controller's epout requires buffer size to be aligned to MaxPacketSize.Signed-off-by David Cohen <[email protected]>--- include/linux/usb/gadget.h | 3 +++ 1 file changed, 3 insertions(+)include/linux/usb/gadget.h +0
-0include/linux/usb/gadget.h struct usb_gadget { · line 547
Quoted 8 lines
include/linux/usb/gadget.h +3
-0@@ -540,6 +540,9 @@ struct usb_gadget {struct device dev;unsigned out_epnum;unsigned in_epnum; + +/* epout requires buffer size to be aligned to MaxPacketSize */ + unsigned quirk_ep_out_aligned_size:1; };#define work_to_gadget (w) (container_of((w), struct usb_gadget, work))Instead of adding this new bitflag to the end of the structure, where it will cause the structure to grow by at least 4 bytes, why not add it in the middle, along with all the other bitflags, where it won't cause any change in the structure's size?
Quoted 2 lines
If you prefer, you can move the name, dev,
out_epnum, andin_epnumfields higher up, so that the bitflags come last.
I'd prefer moving the fields and having all bit-fields, last. Thanks
Signature · 1 line
balbi
Re: [PATCH v3 1/3] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
In reply to David CohenHi,
Quoted 18 lines · David Cohen
Due to USB controllers may have different restrictions, usb gadget layer needs to provide a generic way to inform gadget functions to complain with non-standard requirements.
This patch adds '
quirk_ep_out_aligned_size' field to structusb_gadgetto inform when controller's epout requires buffer size to be aligned to MaxPacketSize.Signed-off-by David Cohen <[email protected]>--- include/linux/usb/gadget.h | 3 +++ 1 file changed, 3 insertions(+)include/linux/usb/gadget.h +0
-0
include/linux/usb/gadget.h struct usb_gadget { · line 544
Quoted 5 lines
include/linux/usb/gadget.h +2
-0@@ -540,6 +540,9 @@ struct usb_gadget {struct device dev;unsigned out_epnum;unsigned in_epnum; + +/* epout requires buffer size to be aligned to MaxPacketSize */
please document this on the kernel-doc comment above.
Signature · 1 line
balbi
PATCH v3 2/3 usb: ffs: check quirk to pad epout buf size when not aligned to maxpacketsize
In reply to David CohenCheck gadget.quirk_ep_out_aligned_size to decide if buffer size requires to be aligned to maxpacketsize of an out endpoint. ffs_epfile_io() needs to pad epout buffer to match above condition if quirk is found.
--- drivers/usb/gadget/f_fs.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/usb/gadget/f_fs.c +17 -0
@@ -755,10 +755,12 @@ static ssize_t ffs_epfile_io(struct file *file, char __user *buf, size_t len, int read) { struct ffs_epfile *epfile = file->private_data; +struct usb_gadget *gadget = epfile->ffs->gadget;struct ffs_ep *ep;char *data =NULL ;ssize_t ret;int halt; +size_t orig_len = len; goto first_try; do {
@@ -794,6 +796,21 @@ first_try: goto error; } +/* +* Controller requires buffer size to be aligned to +* maxpacketsize of an out endpoint. +*/ +if (gadget->quirk_ep_out_aligned_size && read && + !IS_ALIGNED (len, ep->ep->desc->wMaxPacketSize)) { +size_t old_len = len; + len =roundup (orig_len, + (size_t )ep->ep->desc->wMaxPacketSize); +if (unlikely (data) && len > old_len) { +kfree (data); + data =NULL ; + } + } +/* Allocate & copy */ if (!halt && !data) { data =kzalloc (len,GFP_KERNEL );
Signature · 1 line
1.8.4.rc3
Re: [PATCH v3 2/3] usb: ffs: check quirk to pad epout buf size when not aligned to maxpacketsize
Quoted 27 lines · David Cohen
Check gadget.quirk_ep_out_aligned_size to decide if buffer size requires to be aligned to maxpacketsize of an out endpoint.
ffs_epfile_io()needs to pad epout buffer to match above condition if quirk is found.Signed-off-by David Cohen <[email protected]>--- drivers/usb/gadget/f_fs.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/usb/gadget/f_fs.c +2
-0@@ -755,10 +755,12 @@ static ssize_t ffs_epfile_io(struct file *file, char __user *buf, size_t len, int read) { struct ffs_epfile *epfile = file->private_data; +struct usb_gadget *gadget = epfile->ffs->gadget;struct ffs_ep *ep;char *data =NULL ;ssize_t ret;int halt; +size_t orig_len = len; goto first_try; do {
drivers/usb/gadget/f_fs.c first_try: · line 804
Quoted 9 lines
drivers/usb/gadget/f_fs.c +6
-0@@ -794,6 +796,21 @@ first_try: goto error; } +/* +* Controller requires buffer size to be aligned to +* maxpacketsize of an out endpoint. +*/ +if (gadget->quirk_ep_out_aligned_size && read && + !IS_ALIGNED (len, ep->ep->desc->wMaxPacketSize)) {
IS_ALIGNED works only when the second argument is a power of 2.
Interrupt endpoints are not required to have maxpacket sizes that are powers of 2. Does this code ever get used for an interrupt endpoint?
drivers/usb/gadget/f_fs.c first_try: · line 805
Quoted 1 line
drivers/usb/gadget/f_fs.c +1
-0+size_t old_len = len;
Why add old_len here when you added orig_len above?
drivers/usb/gadget/f_fs.c first_try: · line 808
Quoted 3 lines
drivers/usb/gadget/f_fs.c +3
-0+ len =roundup (orig_len, + (size_t )ep->ep->desc->wMaxPacketSize); +if (unlikely (data) && len > old_len) {
If the original value wasn't aligned, how can len fail to be > old_len?
drivers/usb/gadget/f_fs.c first_try: · line 816
Quoted 8 lines
drivers/usb/gadget/f_fs.c +5
-0+kfree (data); + data =NULL ; + } + } +/* Allocate & copy */ if (!halt && !data) { data =kzalloc (len,GFP_KERNEL );
Alan Stern
Re: [PATCH v3 2/3] usb: ffs: check quirk to pad epout buf size when not aligned to maxpacketsize
Hi Alan,
Appreciate your comments. Please, see my reply.
Quoted 41 lines · Alan Stern
Quoted 27 lines · David Cohen
Check gadget.quirk_ep_out_aligned_size to decide if buffer size requires to be aligned to maxpacketsize of an out endpoint.
ffs_epfile_io()needs to pad epout buffer to match above condition if quirk is found.Signed-off-by David Cohen <[email protected]>--- drivers/usb/gadget/f_fs.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/usb/gadget/f_fs.c +2
-0@@ -755,10 +755,12 @@ static ssize_t ffs_epfile_io(struct file *file, char __user *buf, size_t len, int read) { struct ffs_epfile *epfile = file->private_data; +struct usb_gadget *gadget = epfile->ffs->gadget;struct ffs_ep *ep;char *data =NULL ;ssize_t ret;int halt; +size_t orig_len = len; goto first_try; do {drivers/usb/gadget/f_fs.c first_try: · line 804
Quoted 9 lines
drivers/usb/gadget/f_fs.c +6
-0@@ -794,6 +796,21 @@ first_try: goto error; } +/* +* Controller requires buffer size to be aligned to +* maxpacketsize of an out endpoint. +*/ +if (gadget->quirk_ep_out_aligned_size && read && + !IS_ALIGNED (len, ep->ep->desc->wMaxPacketSize)) {
IS_ALIGNEDworks only when the second argument is a power of 2.
Quoted 2 lines
Interrupt endpoints are not required to have maxpacket sizes that are powers of 2. Does this code ever get used for an interrupt endpoint?
That's a good point. It won't use interrupt ep on the case I am working on, but f_fs allows it. I'll cover both cases in next patchset.
Quoted 4 lines
drivers/usb/gadget/f_fs.c first_try: · line 805
Quoted 2 lines
drivers/usb/gadget/f_fs.c +1
-0+size_t old_len = len;Why add
old_lenhere when you addedorig_lenabove?
See below.
Quoted 6 lines
drivers/usb/gadget/f_fs.c first_try: · line 808
Quoted 4 lines
drivers/usb/gadget/f_fs.c +3
-0+ len =roundup (orig_len, + (size_t )ep->ep->desc->wMaxPacketSize); +if (unlikely (data) && len > old_len) {If the original value wasn't aligned, how can len fail to be >
old_len?
This code is inside a loop. There is an unlikely case explained in the end to loop again if ep got disabled or changed while acquiring mutex. If that happens, I want to avoid to re-allocate buffer if previous size was already enough. Since 'len' gets updated when aligning buf size, we need to keep track of 'orig_len' to do only minimum necessary pad on new alignment. 'old_len' is used to save previous value to decide for new allocation or not. But maybe I should change it to something more accurate like 'buf_size' or 'alloc_size'.
Br, David Cohen
PATCH v3 3/3 usb: dwc3: add quirk USB_GADGET_QUIRK_EP_OUT_ALIGNED_SIZE to gadget driver
In reply to David CohenDWC3 requires epout to have buffer size aligned to MaxPacketSize value. This patch adds necessary quirk for it.
--- drivers/usb/dwc3/gadget.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/usb/dwc3/gadget.c +6 -0
@@ -2600,6 +2600,12 @@ int dwc3_gadget_init(struct dwc3 *dwc) dwc->gadget.name ="dwc3-gadget" ;/* +* Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize +* on ep out. +*/ + dwc->gadget.quirk_ep_out_aligned_size =1 ; + +/* * REVISIT: Here we should clear all pending IRQs to be * sure we're starting from a well known location. */
Signature · 1 line
1.8.4.rc3
Re: [PATCH v3 3/3] usb: dwc3: add quirk USB_GADGET_QUIRK_EP_OUT_ALIGNED_SIZE to gadget driver
Quoted 13 lines · David Cohen
DWC3 requires epout to have buffer size aligned to MaxPacketSize value. This patch adds necessary quirk for it.
Signed-off-by David Cohen <[email protected]>--- drivers/usb/dwc3/gadget.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/usb/dwc3/gadget.c +0
-0
drivers/usb/dwc3/gadget.c int dwc3_gadget_init(struct dwc3 *dwc) · line 2606
Quoted 7 lines
drivers/usb/dwc3/gadget.c +4
-0@@ -2600,6 +2600,12 @@ int dwc3_gadget_init(struct dwc3 *dwc) dwc->gadget.name ="dwc3-gadget" ;/* +* Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize +* on ep out. +*/ + dwc->gadget.quirk_ep_out_aligned_size =1 ;
just to make it look cooler and neater, could you set to 'true' instead :-)
Signature · 1 line
balbi
Re: [PATCH v3 3/3] usb: dwc3: add quirk USB_GADGET_QUIRK_EP_OUT_ALIGNED_SIZE to gadget driver
Quoted 16 lines · Felipe Balbi
Quoted 15 lines · David Cohen
DWC3 requires epout to have buffer size aligned to MaxPacketSize value. This patch adds necessary quirk for it.
Signed-off-by David Cohen <[email protected]>--- drivers/usb/dwc3/gadget.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/usb/dwc3/gadget.c +0
-0@@ -2600,6 +2600,12 @@ int dwc3_gadget_init(struct dwc3 *dwc) dwc->gadget.name ="dwc3-gadget" ;
Quoted 8 lines
drivers/usb/dwc3/gadget.c int dwc3_gadget_init(struct dwc3 *dwc) · line 2606
Quoted 5 lines
drivers/usb/dwc3/gadget.c +4
-0/* +* Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize +* on ep out. +*/ + dwc->gadget.quirk_ep_out_aligned_size =1 ;just to make it look cooler and neater, could you set to 'true' instead :-)
'bool' is an alien in C :)
But I can change to true in next patch set.
Br, David Cohen
Re: [PATCH v3 3/3] usb: dwc3: add quirk USB_GADGET_QUIRK_EP_OUT_ALIGNED_SIZE to gadget driver
Quoted 26 lines · David Cohen
Quoted 16 lines · Felipe Balbi
Quoted 15 lines · David Cohen
DWC3 requires epout to have buffer size aligned to MaxPacketSize value. This patch adds necessary quirk for it.
Signed-off-by David Cohen <[email protected]>--- drivers/usb/dwc3/gadget.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/usb/dwc3/gadget.c +0
-0@@ -2600,6 +2600,12 @@ int dwc3_gadget_init(struct dwc3 *dwc) dwc->gadget.name ="dwc3-gadget" ;Quoted 8 lines
drivers/usb/dwc3/gadget.c int dwc3_gadget_init(struct dwc3 *dwc) · line 2606
Quoted 5 lines
drivers/usb/dwc3/gadget.c +4
-0/* +* Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize +* on ep out. +*/ + dwc->gadget.quirk_ep_out_aligned_size =1 ;just to make it look cooler and neater, could you set to 'true' instead :-)
Quoted 2 lines
'bool' is an alien in C :)
But I can change to true in next patch set.
don't change the type, just assign true instead of 1. true is defined as !0, so it'll be a 1 anywa. Still the rest of the driver uses true/false for one-bit fields.
Signature · 1 line
balbi