1 From f5908d1de6a9231622ebe7ae174749305a69878f 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/196] 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 diff --git a/drivers/usb/host/dwc_common_port/dwc_common_linux.c b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
29 index f00a9ff..6814e51 100644
30 --- a/drivers/usb/host/dwc_common_port/dwc_common_linux.c
31 +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
32 @@ -991,6 +991,11 @@ void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
33 tasklet_schedule(&task->t);
36 +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
38 + tasklet_hi_schedule(&task->t);
43 - run in process context (can sleep)
44 diff --git a/drivers/usb/host/dwc_common_port/dwc_list.h b/drivers/usb/host/dwc_common_port/dwc_list.h
45 index 89cc325..4ce560d 100644
46 --- a/drivers/usb/host/dwc_common_port/dwc_list.h
47 +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
48 @@ -384,17 +384,17 @@ struct { \
49 #define DWC_TAILQ_PREV(elm, headname, field) \
50 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
51 #define DWC_TAILQ_EMPTY(head) \
52 - (TAILQ_FIRST(head) == TAILQ_END(head))
53 + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
55 #define DWC_TAILQ_FOREACH(var, head, field) \
56 - for((var) = TAILQ_FIRST(head); \
57 - (var) != TAILQ_END(head); \
58 - (var) = TAILQ_NEXT(var, field))
59 + for ((var) = DWC_TAILQ_FIRST(head); \
60 + (var) != DWC_TAILQ_END(head); \
61 + (var) = DWC_TAILQ_NEXT(var, field))
63 #define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
64 - for((var) = TAILQ_LAST(head, headname); \
65 - (var) != TAILQ_END(head); \
66 - (var) = TAILQ_PREV(var, headname, field))
67 + for ((var) = DWC_TAILQ_LAST(head, headname); \
68 + (var) != DWC_TAILQ_END(head); \
69 + (var) = DWC_TAILQ_PREV(var, headname, field))
72 * Tail queue functions.
73 diff --git a/drivers/usb/host/dwc_common_port/dwc_os.h b/drivers/usb/host/dwc_common_port/dwc_os.h
74 index 308ddd5..8eb24ea 100644
75 --- a/drivers/usb/host/dwc_common_port/dwc_os.h
76 +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
77 @@ -981,6 +981,8 @@ extern void DWC_TASK_FREE(dwc_tasklet_t *task);
78 extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
79 #define dwc_task_schedule DWC_TASK_SCHEDULE
81 +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
82 +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
86 diff --git a/drivers/usb/host/dwc_otg/dwc_otg_hcd.c b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
87 index fcec97f..91eefec 100644
88 --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
89 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
94 +#include <linux/usb.h>
95 +#include <linux/usb/hcd.h>
97 #include "dwc_otg_hcd.h"
98 #include "dwc_otg_regs.h"
100 @@ -694,6 +697,31 @@ static void reset_tasklet_func(void *data)
101 dwc_otg_hcd->flags.b.port_reset_change = 1;
104 +static void completion_tasklet_func(void *ptr)
106 + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
108 + urb_tq_entry_t *item;
109 + dwc_irqflags_t flags;
111 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
112 + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
113 + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
115 + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
117 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
120 + usb_hcd_unlink_urb_from_ep(hcd->priv, urb);
121 + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
123 + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
125 + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
129 static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
131 dwc_list_link_t *item;
132 @@ -833,6 +861,7 @@ static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
134 DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
135 DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
136 + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
138 #ifdef DWC_DEV_SRPCAP
139 if (dwc_otg_hcd->core_if->power_down == 2 &&
140 @@ -877,7 +906,7 @@ int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
141 DWC_LIST_INIT(&hcd->periodic_sched_ready);
142 DWC_LIST_INIT(&hcd->periodic_sched_assigned);
143 DWC_LIST_INIT(&hcd->periodic_sched_queued);
145 + DWC_TAILQ_INIT(&hcd->completed_urb_list);
147 * Create a host channel descriptor for each host channel implemented
148 * in the controller. Initialize the channel descriptor array.
149 @@ -915,6 +944,9 @@ int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
151 /* Initialize reset tasklet. */
152 hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
154 + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
155 + completion_tasklet_func, hcd);
156 #ifdef DWC_DEV_SRPCAP
157 if (hcd->core_if->power_down == 2) {
158 /* Initialize Power on timer for Host power up in case hibernation */
159 diff --git a/drivers/usb/host/dwc_otg/dwc_otg_hcd.h b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
160 index 45e44ea..0493dbf 100644
161 --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
162 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
163 @@ -374,6 +374,13 @@ typedef struct dwc_otg_qh {
165 DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
167 +typedef struct urb_tq_entry {
169 + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
172 +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
175 * This structure holds the state of the HCD, including the non-periodic and
176 * periodic schedules.
177 @@ -551,6 +558,9 @@ struct dwc_otg_hcd {
178 /* Tasket to do a reset */
179 dwc_tasklet_t *reset_tasklet;
181 + dwc_tasklet_t *completion_tasklet;
182 + struct urb_list completed_urb_list;
185 dwc_spinlock_t *lock;
186 dwc_spinlock_t *channel_lock;
187 diff --git a/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
188 index 9702f81..7bb133a 100644
189 --- a/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
190 +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
191 @@ -271,7 +271,7 @@ static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
192 dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
194 struct urb *urb = (struct urb *)urb_handle;
196 + urb_tq_entry_t *new_entry;
197 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
198 DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
199 __func__, urb, usb_pipedevice(urb->pipe),
200 @@ -285,7 +285,7 @@ static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
205 + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
206 urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
207 /* Convert status value. */
209 @@ -348,18 +348,25 @@ static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
212 DWC_FREE(dwc_otg_urb);
215 + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
216 + urb->status = -EPROTO;
217 + /* don't schedule the tasklet -
218 + * directly return the packet here with error. */
219 #if USB_URB_EP_LINKING
220 - usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
221 + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
223 - DWC_SPINUNLOCK(hcd->lock);
224 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
225 - usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
226 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
228 - usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, status);
229 + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
231 - DWC_SPINLOCK(hcd->lock);
234 + new_entry->urb = urb;
235 + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
237 + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);