add gadget quirk to adapt f_fs for DWC3
Subsystems (2)
v7 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.
This new patch set was tested and validated in my environment:
- Intel Merrifield was able to use DWC3/f_fs with adbd service (it wasn't before).
Changes from v6 to v7:
- Downgraded patch set version to v5 as per Felipe Balbi request, with some changes:
- usb_ep_align_maxpacketsize() macro was change to usp_ep_align_maybe()
in order to check gadget->quirk_ep_out_aligned_size internally.
- updated patch 4/5 to use new macro usp_ep_align_maybe() and to fix
following compilation warning:
$ drivers/usb/gadget/f_fs.c: In function 'ffs_epfile_io':
$ drivers/usb/gadget/f_fs.c:857:124: warning: comparison of distinct pointer types lacks a cast [enabled by default]
---
David Cohen (3):
usb: gadget: move bitflags to the end of usb_gadget struct
usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
usb: dwc3: set gadget's quirk ep_out_align_size
Michal Nazarewicz (2):
usb: gadget: f_fs: remove loop from I/O function
usb: f_fs: check quirk to pad epout buf size when not aligned to
maxpacketsize
drivers/usb/dwc3/gadget.c | 6 +++ drivers/usb/gadget/f_fs.c | 115 +++++++++++++++++++++++---------------------- include/linux/usb/gadget.h | 39 +++++++++++---- 3 files changed, 94 insertions(+), 66 deletions(-)
Signature · 1 line
1.8.4.2
PATCH v7 1/5 usb: gadget: move bitflags to the end of usb_gadget struct
This patch moves all bitflags to the end of usb_gadget struct in order to improve readability.
--- include/linux/usb/gadget.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
include/linux/usb/gadget.h +10 -9
@@ -485,6 +485,11 @@ struct usb_gadget_ops {* @max_speed: Maximal speed the UDC can handle. UDC must support this * and all slower speeds. * @state: the state we are now (attached, suspended, configured, etc) +* @name: Identifies the controller hardware type. Used in diagnostics +* and sometimes configuration. +* @dev: Driver model state for this abstract device. +* @out_epnum: last used out ep number +* @in_epnum: last used in ep number * @sg_supported: true if we can handle scatter-gather * @is_otg: True if the USB device port uses a Mini-AB jack, so that the * gadget driver must provide a USB OTG descriptor.
@@ -497,11 +502,6 @@ struct usb_gadget_ops {* only supports HNP on a different root port. * @b_hnp_enable: OTG device feature flag, indicating that the A-Host * enabled HNP support. -* @name: Identifies the controller hardware type. Used in diagnostics -* and sometimes configuration. -* @dev: Driver model state for this abstract device. -* @out_epnum: last used out ep number -* @in_epnum: last used in ep number * * Gadgets have a mostly-portable "gadget driver" implementing device * functions, handling all usb configurations and interfaces. Gadget
@@ -530,16 +530,17 @@ struct usb_gadget {enum usb_device_speed speed;enum usb_device_speed max_speed;enum usb_device_state state; +const char *name; +struct device dev; +unsigned out_epnum; +unsigned in_epnum; + unsigned sg_supported:1; unsigned is_otg:1; unsigned is_a_peripheral:1; unsigned b_hnp_enable:1; unsigned a_hnp_support:1; unsigned a_alt_hnp_support:1; -const char *name; -struct device dev; -unsigned out_epnum; -unsigned in_epnum; };#define work_to_gadget (w) (container_of((w), struct usb_gadget, work))
Signature · 1 line
1.8.4.2
PATCH v7 2/5 usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
In reply to David CohenDue 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. A helper is also provided to align buffer size when necessary.
--- include/linux/usb/gadget.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
include/linux/usb/gadget.h +20 -0
@@ -502,6 +502,8 @@ struct usb_gadget_ops {* only supports HNP on a different root port. * @b_hnp_enable: OTG device feature flag, indicating that the A-Host * enabled HNP support. +* @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to +* MaxPacketSize. * * Gadgets have a mostly-portable "gadget driver" implementing device * functions, handling all usb configurations and interfaces. Gadget
@@ -541,6 +543,7 @@ struct usb_gadget { unsigned b_hnp_enable:1; unsigned a_hnp_support:1; unsigned a_alt_hnp_support:1; + unsigned quirk_ep_out_aligned_size:1; };#define work_to_gadget (w) (container_of((w), struct usb_gadget, work))
@@ -559,6 +562,23 @@ static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)/** +* usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget +* requires quirk_ep_out_aligned_size, otherwise reguens len. +* @g: controller to check for quirk +* @ep: the endpoint whose maxpacketsize is used to align @len +* @len: buffer size's length to align to @ep's maxpacketsize +* +* This helper is used in case it's required for any reason to check and maybe +* align buffer's size to an ep's maxpacketsize. +*/ +static inline size_t +usb_ep_align_maybe (struct usb_gadget *g,struct usb_ep *ep,size_t len) +{ +return !g->quirk_ep_out_aligned_size ? len : +round_up (len, (size_t )ep->desc->wMaxPacketSize); +} + +/** * gadget_is_dualspeed - return true iff the hardware handles high speed * @g: controller that might support both high and full speeds */
Signature · 1 line
1.8.4.2
Re: [PATCH v7 2/5] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
Quoted 4 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.
Quoted 8 lines
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. A helper is also provided to align buffer size when necessary.Signed-off-by David Cohen <[email protected]>Cc Alan Stern <[email protected]>Cc Michal Nazarewicz <[email protected]>
Quoted 26 lines
--- include/linux/usb/gadget.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)include/linux/usb/gadget.h +3
-0@@ -502,6 +502,8 @@ struct usb_gadget_ops {* only supports HNP on a different root port. * @b_hnp_enable: OTG device feature flag, indicating that the A-Host * enabled HNP support. +* @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to +* MaxPacketSize. * * Gadgets have a mostly-portable "gadget driver" implementing device * functions, handling all usb configurations and interfaces. Gadget @@ -541,6 +543,7 @@ struct usb_gadget { unsigned b_hnp_enable:1; unsigned a_hnp_support:1; unsigned a_alt_hnp_support:1; + unsigned quirk_ep_out_aligned_size:1; };#define work_to_gadget (w) (container_of((w), struct usb_gadget, work))
include/linux/usb/gadget.h static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev) · line 566
Quoted 5 lines
include/linux/usb/gadget.h +2
-0@@ -559,6 +562,23 @@ static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)/** +* usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget +* requires quirk_ep_out_aligned_size, otherwise reguens len.
“returns”
Quoted 18 lines
include/linux/usb/gadget.h +15
-0+* @g: controller to check for quirk +* @ep: the endpoint whose maxpacketsize is used to align @len +* @len: buffer size's length to align to @ep's maxpacketsize +* +* This helper is used in case it's required for any reason to check and maybe +* align buffer's size to an ep's maxpacketsize. +*/ +static inline size_t +usb_ep_align_maybe (struct usb_gadget *g,struct usb_ep *ep,size_t len) +{ +return !g->quirk_ep_out_aligned_size ? len : +round_up (len, (size_t )ep->desc->wMaxPacketSize); +} + +/** * gadget_is_dualspeed - return true iff the hardware handles high speed * @g: controller that might support both high and full speeds */
Signature · 4 lines
Best regards, _ _ .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michał “mina86” Nazarewicz (o o) ooo +--<[email protected]>--<xmpp:[email protected]>--ooO--(_)--Ooo--
Re: [PATCH v7 2/5] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
Quoted 41 lines · Michal Nazarewicz
Quoted 4 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.
Quoted 8 lines
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. A helper is also provided to align buffer size when necessary.Signed-off-by David Cohen <[email protected]>Cc Alan Stern <[email protected]>Cc Michal Nazarewicz <[email protected]>Quoted 25 lines
--- include/linux/usb/gadget.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)include/linux/usb/gadget.h +3
-0@@ -502,6 +502,8 @@ struct usb_gadget_ops {* only supports HNP on a different root port. * @b_hnp_enable: OTG device feature flag, indicating that the A-Host * enabled HNP support. +* @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to +* MaxPacketSize. * * Gadgets have a mostly-portable "gadget driver" implementing device * functions, handling all usb configurations and interfaces. Gadget @@ -541,6 +543,7 @@ struct usb_gadget { unsigned b_hnp_enable:1; unsigned a_hnp_support:1; unsigned a_alt_hnp_support:1; + unsigned quirk_ep_out_aligned_size:1; };#define work_to_gadget (w) (container_of((w), struct usb_gadget, work))
Quoted 8 lines
include/linux/usb/gadget.h static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev) · line 566
Quoted 6 lines
include/linux/usb/gadget.h +2
-0@@ -559,6 +562,23 @@ static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)/** +* usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget +* requires quirk_ep_out_aligned_size, otherwise reguens len. “returns”
I've got no idea how returns became reguens :)
But maybe Felipe can fix it when applying?
Br, David
Re: [PATCH v7 2/5] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
Quoted 45 lines · David Cohen
Quoted 44 lines · Michal Nazarewicz
Quoted 4 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.
Quoted 8 lines
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. A helper is also provided to align buffer size when necessary.Signed-off-by David Cohen <[email protected]>Cc Alan Stern <[email protected]>Cc Michal Nazarewicz <[email protected]>Quoted 28 lines
--- include/linux/usb/gadget.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)include/linux/usb/gadget.h +3
-0@@ -502,6 +502,8 @@ struct usb_gadget_ops {* only supports HNP on a different root port. * @b_hnp_enable: OTG device feature flag, indicating that the A-Host * enabled HNP support. +* @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to +* MaxPacketSize. * * Gadgets have a mostly-portable "gadget driver" implementing device * functions, handling all usb configurations and interfaces. Gadget @@ -541,6 +543,7 @@ struct usb_gadget { unsigned b_hnp_enable:1; unsigned a_hnp_support:1; unsigned a_alt_hnp_support:1; + unsigned quirk_ep_out_aligned_size:1; };#define work_to_gadget (w) (container_of((w), struct usb_gadget, work))@@ -559,6 +562,23 @@ static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
Quoted 8 lines
Quoted 5 lines
include/linux/usb/gadget.h static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev) · line 566
Quoted 3 lines
include/linux/usb/gadget.h +2
-0/** +* usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget +* requires quirk_ep_out_aligned_size, otherwise reguens len. “returns”
I've got no idea how returns became reguens :)
But maybe Felipe can fix it when applying?
Sure, but you owe me a beer hehe :-)
Signature · 1 line
balbi
Re: [PATCH v7 2/5] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
/**
+ *usb_ep_align_maybe- returns @len aligned to ep's maxpacketsize if gadget
+ * requiresquirk_ep_out_aligned_size, otherwise reguens len.“returns”
I've got no idea how returns became reguens :)
But maybe Felipe can fix it when applying?Sure, but you owe me a beer hehe :-)
Just print this e-mail and you can redeem one as soon as you visit Portland :P
Br, David
Re: [PATCH v7 2/5] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
In reply to David CohenQuoted 1 line · David Cohen
I've got no idea how returns became reguens :)
The keys are like right next to each other <http://bash.org/?5300>. ;)
Quoted 1 line
But maybe Felipe can fix it when applying?
Signature · 4 lines
Best regards, _ _ .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michał “mina86” Nazarewicz (o o) ooo +--<[email protected]>--<xmpp:[email protected]>--ooO--(_)--Ooo--
Re: [PATCH v7 2/5] usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
Quoted 4 lines · Michal Nazarewicz
Quoted 1 line · David Cohen
I've got no idea how returns became reguens :)
The keys are like right next to each other <http://bash.org/?5300>. ;)
I'll remember this excuse if I need some day :)
Quoted 5 lines
Quoted 2 lines
But maybe Felipe can fix it when applying?
PATCH v7 3/5 usb: gadget: f_fs: remove loop from I/O function
In reply to David CohenFrom: Michal Nazarewicz <[email protected]>
When endpoint changes (due to it being disabled or alt setting changed), mimic the action as if the change happened after the request has been queued, instead of retrying with the new endpoint.
--- drivers/usb/gadget/f_fs.c | 94 ++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 54 deletions(-)
drivers/usb/gadget/f_fs.c +40 -54
@@ -760,73 +760,59 @@ static ssize_t ffs_epfile_io(struct file *file,ssize_t ret;int halt; - goto first_try; -do { -spin_unlock_irq (&epfile->ffs->eps_lock); -mutex_unlock (&epfile->mutex); +/* Are we still active? */ +if (WARN_ON (epfile->ffs->state !=FFS_ACTIVE )) { + ret = -ENODEV ; + goto error; + } -first_try: -/* Are we still active? */ -if (WARN_ON (epfile->ffs->state !=FFS_ACTIVE )) { - ret = -ENODEV ; +/* Wait for endpoint to be enabled */ + ep = epfile->ep; +if (!ep) { +if (file->f_flags &O_NONBLOCK ) { + ret = -EAGAIN ; goto error; } -/* Wait for endpoint to be enabled */ - ep = epfile->ep; -if (!ep) { -if (file->f_flags &O_NONBLOCK ) { - ret = -EAGAIN ; - goto error; - } - -if (wait_event_interruptible (epfile->wait, - (ep = epfile->ep))) { - ret = -EINTR ; - goto error; - } - } - -/* Do we halt? */ - halt = !read == !epfile->in; -if (halt && epfile->isoc) { - ret = -EINVAL ; + ret =wait_event_interruptible (epfile->wait, (ep = epfile->ep)); +if (ret) { + ret = -EINTR ; goto error; } + } -/* Allocate & copy */ -if (!halt && !data) { - data =kzalloc (len,GFP_KERNEL ); -if (unlikely (!data)) -return -ENOMEM ; +/* Do we halt? */ + halt = !read == !epfile->in; +if (halt && epfile->isoc) { + ret = -EINVAL ; + goto error; + } -if (!read && -unlikely (__copy_from_user (data, buf, len))) { - ret = -EFAULT ; - goto error; - } - } +/* Allocate & copy */ +if (!halt) { + data =kmalloc (len,GFP_KERNEL ); +if (unlikely (!data)) +return -ENOMEM ; -/* We will be using request */ - ret =ffs_mutex_lock (&epfile->mutex, - file->f_flags &O_NONBLOCK ); -if (unlikely (ret)) +if (!read &&unlikely (copy_from_user (data, buf, len))) { + ret = -EFAULT ; goto error; + } + } -/* -* We're called from user space, we can use _irq rather then -* _irqsave -*/ -spin_lock_irq (&epfile->ffs->eps_lock); +/* We will be using request */ + ret =ffs_mutex_lock (&epfile->mutex, file->f_flags &O_NONBLOCK ); +if (unlikely (ret)) + goto error; -/* -* While we were acquiring mutex endpoint got disabled -* or changed? -*/ - }while (unlikely (epfile->ep != ep)); +spin_lock_irq (&epfile->ffs->eps_lock); -/* Halt */ -if (unlikely (halt)) { +if (epfile->ep != ep) { +/* In the meantime, endpoint got disabled or changed. */ + ret = -ESHUTDOWN ; +spin_unlock_irq (&epfile->ffs->eps_lock); + }else if (halt) { +/* Halt */ if (likely (epfile->ep == ep) && !WARN_ON (!ep->ep))usb_ep_set_halt (ep->ep);spin_unlock_irq (&epfile->ffs->eps_lock);
Signature · 1 line
1.8.4.2
PATCH v7 4/5 usb: f_fs: check quirk to pad epout buf size when not aligned to maxpacketsize
In reply to David CohenFrom: Michal Nazarewicz <[email protected]>
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.
--- drivers/usb/gadget/f_fs.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
drivers/usb/gadget/f_fs.c +19 -4
@@ -755,9 +755,10 @@ 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; +ssize_t ret, data_len;int halt;/* Are we still active? */
@@ -790,7 +791,13 @@ static ssize_t ffs_epfile_io(struct file *file,/* Allocate & copy */ if (!halt) { - data =kmalloc (len,GFP_KERNEL ); +/* +* Controller may require buffer size to be aligned to +* maxpacketsize of an out endpoint. +*/ + data_len = read ?usb_ep_align_maybe (gadget, ep->ep, len) : len; + + data =kmalloc (data_len,GFP_KERNEL );if (unlikely (!data))return -ENOMEM ;
@@ -825,7 +832,7 @@ static ssize_t ffs_epfile_io(struct file *file, req->context = &done; req->complete = ffs_epfile_io_complete; req->buf = data; - req->length = len; + req->length = data_len; ret =usb_ep_queue (ep->ep, req,GFP_ATOMIC );
@@ -837,9 +844,17 @@ static ssize_t ffs_epfile_io(struct file *file, ret = -EINTR ;usb_ep_dequeue (ep->ep, req); }else { +/* +* XXX We may end up silently droping data here. +* Since data_len (i.e. req->length) may be bigger +* than len (after being rounded up to maxpacketsize), +* we may end up with more data then user space has +* space for. +*/ ret = ep->status;if (read && ret >0 && -unlikely (copy_to_user (buf, data, ret))) +unlikely (copy_to_user (buf, data, +min_t (size_t, ret, len)))) ret = -EFAULT ; } }
Signature · 1 line
1.8.4.2
PATCH v7 5/5 usb: dwc3: set gadget's quirk ep_out_align_size
In reply to David CohenDWC3 requires epout to have buffer size aligned to MaxPacketSize value. This patch sets 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 = true; + +/* * 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.2