brcm2708: switch to 3.14
[15.05/openwrt.git] / target / linux / brcm2708 / patches-3.10 / 0057-dwc_otg-implement-tasklet-for-returning-URBs-to-usbc.patch
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
5  usbcore hcd layer
6
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.
14
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.
19 ---
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(-)
27
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);
34  }
35  
36 +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
37 +{
38 +       tasklet_hi_schedule(&task->t);
39 +}
40 +
41  
42  /* workqueues
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))
54  
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))
62  
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))
70  
71  /*
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
80  
81 +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
82 +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
83  
84  /** @name Timer
85   *
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
90 @@ -40,6 +40,9 @@
91   * header file.
92   */
93  
94 +#include <linux/usb.h>
95 +#include <linux/usb/hcd.h>
96 +
97  #include "dwc_otg_hcd.h"
98  #include "dwc_otg_regs.h"
99  
100 @@ -694,6 +697,31 @@ static void reset_tasklet_func(void *data)
101         dwc_otg_hcd->flags.b.port_reset_change = 1;
102  }
103  
104 +static void completion_tasklet_func(void *ptr)
105 +{
106 +       dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
107 +       struct urb *urb;
108 +       urb_tq_entry_t *item;
109 +       dwc_irqflags_t flags;
110 +
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);
114 +               urb = item->urb;
115 +               DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
116 +                               urb_tq_entries);
117 +               DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
118 +               DWC_FREE(item);
119 +
120 +               usb_hcd_unlink_urb_from_ep(hcd->priv, urb);
121 +               usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
122 +
123 +               DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
124 +       }
125 +       DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
126 +       return;
127 +}
128 +
129  static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
130  {
131         dwc_list_link_t *item;
132 @@ -833,6 +861,7 @@ static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
133  
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);
137  
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);
144 -
145 +       DWC_TAILQ_INIT(&hcd->completed_urb_list);
146         /*
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)
150  
151         /* Initialize reset tasklet. */
152         hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
153 +
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 {
164  
165  DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
166  
167 +typedef struct urb_tq_entry {
168 +       struct urb *urb;
169 +       DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
170 +} urb_tq_entry_t;
171 +
172 +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
173 +
174  /**
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;
180  
181 +       dwc_tasklet_t *completion_tasklet;
182 +       struct urb_list completed_urb_list;
183 +
184         /*  */
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)
193  {
194         struct urb *urb = (struct urb *)urb_handle;
195 -
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,
201                         }
202                 }
203         }
204 -
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. */
208         switch (status) {
209 @@ -348,18 +348,25 @@ static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
210         }
211  
212         DWC_FREE(dwc_otg_urb);
213 -
214 +       if (!new_entry) {
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);
222  #endif
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);
227  #else
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);
230  #endif
231 -       DWC_SPINLOCK(hcd->lock);
232 -
233 +       } else {
234 +               new_entry->urb = urb;
235 +               DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
236 +                                       urb_tq_entries);
237 +               DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
238 +       }
239         return 0;
240  }
241  
242 -- 
243 1.9.1
244