1 From 71109f7d314b6205259d5cecf8314708f84fb9e6 Mon Sep 17 00:00:00 2001
2 From: P33M <P33M@github.com>
3 Date: Thu, 21 Mar 2013 19:36:17 +0000
4 Subject: [PATCH 057/174] dwc_otg: implement tasklet for returning URBs to
7 The dwc_otg driver interrupt handler for transfer completion will spend
8 a very long time with interrupts disabled when a URB is completed -
9 this is because usb_hcd_giveback_urb is called from within the handler
10 which for a USB device driver with complicated processing (e.g. webcam)
11 will take an exorbitant amount of time to complete. This results in
12 missed completion interrupts for other USB packets which lead to them
13 being dropped due to microframe overruns.
15 This patch splits returning the URB to the usb hcd layer into a
16 high-priority tasklet. This will have most benefit for isochronous IN
17 transfers but will also have incidental benefit where multiple periodic
18 devices are active at once.
20 .../usb/host/dwc_common_port/dwc_common_linux.c | 5 ++++
21 drivers/usb/host/dwc_common_port/dwc_list.h | 14 ++++-----
22 drivers/usb/host/dwc_common_port/dwc_os.h | 2 ++
23 drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 34 +++++++++++++++++++++-
24 drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 10 +++++++
25 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 25 ++++++++++------
26 6 files changed, 73 insertions(+), 17 deletions(-)
28 --- a/drivers/usb/host/dwc_common_port/dwc_common_linux.c
29 +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
30 @@ -991,6 +991,11 @@ void DWC_TASK_SCHEDULE(dwc_tasklet_t *ta
31 tasklet_schedule(&task->t);
34 +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
36 + tasklet_hi_schedule(&task->t);
41 - run in process context (can sleep)
42 --- a/drivers/usb/host/dwc_common_port/dwc_list.h
43 +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
44 @@ -384,17 +384,17 @@ struct { \
45 #define DWC_TAILQ_PREV(elm, headname, field) \
46 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
47 #define DWC_TAILQ_EMPTY(head) \
48 - (TAILQ_FIRST(head) == TAILQ_END(head))
49 + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
51 #define DWC_TAILQ_FOREACH(var, head, field) \
52 - for((var) = TAILQ_FIRST(head); \
53 - (var) != TAILQ_END(head); \
54 - (var) = TAILQ_NEXT(var, field))
55 + for ((var) = DWC_TAILQ_FIRST(head); \
56 + (var) != DWC_TAILQ_END(head); \
57 + (var) = DWC_TAILQ_NEXT(var, field))
59 #define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
60 - for((var) = TAILQ_LAST(head, headname); \
61 - (var) != TAILQ_END(head); \
62 - (var) = TAILQ_PREV(var, headname, field))
63 + for ((var) = DWC_TAILQ_LAST(head, headname); \
64 + (var) != DWC_TAILQ_END(head); \
65 + (var) = DWC_TAILQ_PREV(var, headname, field))
68 * Tail queue functions.
69 --- a/drivers/usb/host/dwc_common_port/dwc_os.h
70 +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
71 @@ -981,6 +981,8 @@ extern void DWC_TASK_FREE(dwc_tasklet_t
72 extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
73 #define dwc_task_schedule DWC_TASK_SCHEDULE
75 +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
76 +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
80 --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
81 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
86 +#include <linux/usb.h>
87 +#include <linux/usb/hcd.h>
89 #include "dwc_otg_hcd.h"
90 #include "dwc_otg_regs.h"
92 @@ -694,6 +697,31 @@ static void reset_tasklet_func(void *dat
93 dwc_otg_hcd->flags.b.port_reset_change = 1;
96 +static void completion_tasklet_func(void *ptr)
98 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
100 + urb_tq_entry_t *item;
101 + dwc_irqflags_t flags;
103 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
104 + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
105 + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
107 + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
109 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
112 + usb_hcd_unlink_urb_from_ep(hcd->priv, urb);
113 + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
115 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
117 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
121 static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
123 dwc_list_link_t *item;
124 @@ -833,6 +861,7 @@ static void dwc_otg_hcd_free(dwc_otg_hcd
126 DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
127 DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
128 + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
130 #ifdef DWC_DEV_SRPCAP
131 if (dwc_otg_hcd->core_if->power_down == 2 &&
132 @@ -877,7 +906,7 @@ int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd
133 DWC_LIST_INIT(&hcd->periodic_sched_ready);
134 DWC_LIST_INIT(&hcd->periodic_sched_assigned);
135 DWC_LIST_INIT(&hcd->periodic_sched_queued);
137 + DWC_TAILQ_INIT(&hcd->completed_urb_list);
139 * Create a host channel descriptor for each host channel implemented
140 * in the controller. Initialize the channel descriptor array.
141 @@ -915,6 +944,9 @@ int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd
143 /* Initialize reset tasklet. */
144 hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
146 + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
147 + completion_tasklet_func, hcd);
148 #ifdef DWC_DEV_SRPCAP
149 if (hcd->core_if->power_down == 2) {
150 /* Initialize Power on timer for Host power up in case hibernation */
151 --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
152 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
153 @@ -374,6 +374,13 @@ typedef struct dwc_otg_qh {
155 DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
157 +typedef struct urb_tq_entry {
159 + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
162 +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
165 * This structure holds the state of the HCD, including the non-periodic and
166 * periodic schedules.
167 @@ -551,6 +558,9 @@ struct dwc_otg_hcd {
168 /* Tasket to do a reset */
169 dwc_tasklet_t *reset_tasklet;
171 + dwc_tasklet_t *completion_tasklet;
172 + struct urb_list completed_urb_list;
175 dwc_spinlock_t *lock;
176 dwc_spinlock_t *channel_lock;
177 --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
178 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
179 @@ -271,7 +271,7 @@ static int _complete(dwc_otg_hcd_t * hcd
180 dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
182 struct urb *urb = (struct urb *)urb_handle;
184 + urb_tq_entry_t *new_entry;
185 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
186 DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
187 __func__, urb, usb_pipedevice(urb->pipe),
188 @@ -285,7 +285,7 @@ static int _complete(dwc_otg_hcd_t * hcd
193 + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
194 urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
195 /* Convert status value. */
197 @@ -348,18 +348,25 @@ static int _complete(dwc_otg_hcd_t * hcd
200 DWC_FREE(dwc_otg_urb);
203 + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
204 + urb->status = -EPROTO;
205 + /* don't schedule the tasklet -
206 + * directly return the packet here with error. */
207 #if USB_URB_EP_LINKING
208 - usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
209 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
211 - DWC_SPINUNLOCK(hcd->lock);
212 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
213 - usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
214 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
216 - usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, status);
217 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
219 - DWC_SPINLOCK(hcd->lock);
222 + new_entry->urb = urb;
223 + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
225 + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);