blob: ba99a7a3f81aedf5a2c4dc004fa83d7cdee78e8d [file] [log] [blame]
David Vrabel7e6133a2008-09-17 16:34:28 +01001/*
2 * Wireless Host Controller (WHC) asynchronous schedule management.
3 *
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include <linux/kernel.h>
19#include <linux/dma-mapping.h>
20#include <linux/uwb/umc.h>
21#include <linux/usb.h>
22#define D_LOCAL 0
23#include <linux/uwb/debug.h>
24
25#include "../../wusbcore/wusbhc.h"
26
27#include "whcd.h"
28
29#if D_LOCAL >= 4
30static void dump_asl(struct whc *whc, const char *tag)
31{
32 struct device *dev = &whc->umc->dev;
33 struct whc_qset *qset;
34
35 d_printf(4, dev, "ASL %s\n", tag);
36
37 list_for_each_entry(qset, &whc->async_list, list_node) {
38 dump_qset(qset, dev);
39 }
40}
41#else
42static inline void dump_asl(struct whc *whc, const char *tag)
43{
44}
45#endif
46
47
48static void qset_get_next_prev(struct whc *whc, struct whc_qset *qset,
49 struct whc_qset **next, struct whc_qset **prev)
50{
51 struct list_head *n, *p;
52
53 BUG_ON(list_empty(&whc->async_list));
54
55 n = qset->list_node.next;
56 if (n == &whc->async_list)
57 n = n->next;
58 p = qset->list_node.prev;
59 if (p == &whc->async_list)
60 p = p->prev;
61
62 *next = container_of(n, struct whc_qset, list_node);
63 *prev = container_of(p, struct whc_qset, list_node);
64
65}
66
67static void asl_qset_insert_begin(struct whc *whc, struct whc_qset *qset)
68{
69 list_move(&qset->list_node, &whc->async_list);
70 qset->in_sw_list = true;
71}
72
73static void asl_qset_insert(struct whc *whc, struct whc_qset *qset)
74{
75 struct whc_qset *next, *prev;
76
77 qset_clear(whc, qset);
78
79 /* Link into ASL. */
80 qset_get_next_prev(whc, qset, &next, &prev);
81 whc_qset_set_link_ptr(&qset->qh.link, next->qset_dma);
82 whc_qset_set_link_ptr(&prev->qh.link, qset->qset_dma);
83 qset->in_hw_list = true;
84}
85
86static void asl_qset_remove(struct whc *whc, struct whc_qset *qset)
87{
88 struct whc_qset *prev, *next;
89
90 qset_get_next_prev(whc, qset, &next, &prev);
91
92 list_move(&qset->list_node, &whc->async_removed_list);
93 qset->in_sw_list = false;
94
95 /*
96 * No more qsets in the ASL? The caller must stop the ASL as
97 * it's no longer valid.
98 */
99 if (list_empty(&whc->async_list))
100 return;
101
102 /* Remove from ASL. */
103 whc_qset_set_link_ptr(&prev->qh.link, next->qset_dma);
104 qset->in_hw_list = false;
105}
106
107/**
108 * process_qset - process any recently inactivated or halted qTDs in a
109 * qset.
110 *
111 * After inactive qTDs are removed, new qTDs can be added if the
112 * urb queue still contains URBs.
113 *
114 * Returns any additional WUSBCMD bits for the ASL sync command (i.e.,
115 * WUSBCMD_ASYNC_QSET_RM if a halted qset was removed).
116 */
117static uint32_t process_qset(struct whc *whc, struct whc_qset *qset)
118{
119 enum whc_update update = 0;
120 uint32_t status = 0;
121
122 while (qset->ntds) {
123 struct whc_qtd *td;
124 int t;
125
126 t = qset->td_start;
127 td = &qset->qtd[qset->td_start];
128 status = le32_to_cpu(td->status);
129
130 /*
131 * Nothing to do with a still active qTD.
132 */
133 if (status & QTD_STS_ACTIVE)
134 break;
135
136 if (status & QTD_STS_HALTED) {
137 /* Ug, an error. */
138 process_halted_qtd(whc, qset, td);
139 goto done;
140 }
141
142 /* Mmm, a completed qTD. */
143 process_inactive_qtd(whc, qset, td);
144 }
145
146 update |= qset_add_qtds(whc, qset);
147
148done:
149 /*
150 * Remove this qset from the ASL if requested, but only if has
151 * no qTDs.
152 */
153 if (qset->remove && qset->ntds == 0) {
154 asl_qset_remove(whc, qset);
155 update |= WHC_UPDATE_REMOVED;
156 }
157 return update;
158}
159
160void asl_start(struct whc *whc)
161{
162 struct whc_qset *qset;
163
164 qset = list_first_entry(&whc->async_list, struct whc_qset, list_node);
165
166 le_writeq(qset->qset_dma | QH_LINK_NTDS(8), whc->base + WUSBASYNCLISTADDR);
167
168 whc_write_wusbcmd(whc, WUSBCMD_ASYNC_EN, WUSBCMD_ASYNC_EN);
169 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
170 WUSBSTS_ASYNC_SCHED, WUSBSTS_ASYNC_SCHED,
171 1000, "start ASL");
172}
173
174void asl_stop(struct whc *whc)
175{
176 whc_write_wusbcmd(whc, WUSBCMD_ASYNC_EN, 0);
177 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
178 WUSBSTS_ASYNC_SCHED, 0,
179 1000, "stop ASL");
180}
181
David Vrabel56968d02008-11-25 14:23:40 +0000182/**
183 * asl_update - request an ASL update and wait for the hardware to be synced
184 * @whc: the WHCI HC
185 * @wusbcmd: WUSBCMD value to start the update.
186 *
187 * If the WUSB HC is inactive (i.e., the ASL is stopped) then the
188 * update must be skipped as the hardware may not respond to update
189 * requests.
190 */
David Vrabel7e6133a2008-09-17 16:34:28 +0100191void asl_update(struct whc *whc, uint32_t wusbcmd)
192{
David Vrabel56968d02008-11-25 14:23:40 +0000193 struct wusbhc *wusbhc = &whc->wusbhc;
194
195 mutex_lock(&wusbhc->mutex);
196 if (wusbhc->active) {
197 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
198 wait_event(whc->async_list_wq,
199 (le_readl(whc->base + WUSBCMD) & WUSBCMD_ASYNC_UPDATED) == 0);
200 }
201 mutex_unlock(&wusbhc->mutex);
David Vrabel7e6133a2008-09-17 16:34:28 +0100202}
203
204/**
205 * scan_async_work - scan the ASL for qsets to process.
206 *
207 * Process each qset in the ASL in turn and then signal the WHC that
208 * the ASL has been updated.
209 *
210 * Then start, stop or update the asynchronous schedule as required.
211 */
212void scan_async_work(struct work_struct *work)
213{
214 struct whc *whc = container_of(work, struct whc, async_work);
215 struct whc_qset *qset, *t;
216 enum whc_update update = 0;
217
218 spin_lock_irq(&whc->lock);
219
220 dump_asl(whc, "before processing");
221
222 /*
223 * Transerve the software list backwards so new qsets can be
224 * safely inserted into the ASL without making it non-circular.
225 */
226 list_for_each_entry_safe_reverse(qset, t, &whc->async_list, list_node) {
227 if (!qset->in_hw_list) {
228 asl_qset_insert(whc, qset);
229 update |= WHC_UPDATE_ADDED;
230 }
231
232 update |= process_qset(whc, qset);
233 }
234
235 dump_asl(whc, "after processing");
236
237 spin_unlock_irq(&whc->lock);
238
239 if (update) {
240 uint32_t wusbcmd = WUSBCMD_ASYNC_UPDATED | WUSBCMD_ASYNC_SYNCED_DB;
241 if (update & WHC_UPDATE_REMOVED)
242 wusbcmd |= WUSBCMD_ASYNC_QSET_RM;
243 asl_update(whc, wusbcmd);
244 }
245
246 /*
247 * Now that the ASL is updated, complete the removal of any
248 * removed qsets.
249 */
250 spin_lock(&whc->lock);
251
252 list_for_each_entry_safe(qset, t, &whc->async_removed_list, list_node) {
253 qset_remove_complete(whc, qset);
254 }
255
256 spin_unlock(&whc->lock);
257}
258
259/**
260 * asl_urb_enqueue - queue an URB onto the asynchronous list (ASL).
261 * @whc: the WHCI host controller
262 * @urb: the URB to enqueue
263 * @mem_flags: flags for any memory allocations
264 *
265 * The qset for the endpoint is obtained and the urb queued on to it.
266 *
267 * Work is scheduled to update the hardware's view of the ASL.
268 */
269int asl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
270{
271 struct whc_qset *qset;
272 int err;
273 unsigned long flags;
274
275 spin_lock_irqsave(&whc->lock, flags);
276
277 qset = get_qset(whc, urb, GFP_ATOMIC);
278 if (qset == NULL)
279 err = -ENOMEM;
280 else
281 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
282 if (!err) {
283 usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
284 if (!qset->in_sw_list)
285 asl_qset_insert_begin(whc, qset);
286 }
287
288 spin_unlock_irqrestore(&whc->lock, flags);
289
290 if (!err)
291 queue_work(whc->workqueue, &whc->async_work);
292
293 return 0;
294}
295
296/**
297 * asl_urb_dequeue - remove an URB (qset) from the async list.
298 * @whc: the WHCI host controller
299 * @urb: the URB to dequeue
300 * @status: the current status of the URB
301 *
302 * URBs that do yet have qTDs can simply be removed from the software
303 * queue, otherwise the qset must be removed from the ASL so the qTDs
304 * can be removed.
305 */
306int asl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
307{
308 struct whc_urb *wurb = urb->hcpriv;
309 struct whc_qset *qset = wurb->qset;
310 struct whc_std *std, *t;
311 int ret;
312 unsigned long flags;
313
314 spin_lock_irqsave(&whc->lock, flags);
315
316 ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
317 if (ret < 0)
318 goto out;
319
320 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
321 if (std->urb == urb)
322 qset_free_std(whc, std);
323 else
324 std->qtd = NULL; /* so this std is re-added when the qset is */
325 }
326
327 asl_qset_remove(whc, qset);
328 wurb->status = status;
329 wurb->is_async = true;
330 queue_work(whc->workqueue, &wurb->dequeue_work);
331
332out:
333 spin_unlock_irqrestore(&whc->lock, flags);
334
335 return ret;
336}
337
338/**
339 * asl_qset_delete - delete a qset from the ASL
340 */
341void asl_qset_delete(struct whc *whc, struct whc_qset *qset)
342{
343 qset->remove = 1;
344 queue_work(whc->workqueue, &whc->async_work);
345 qset_delete(whc, qset);
346}
347
348/**
349 * asl_init - initialize the asynchronous schedule list
350 *
351 * A dummy qset with no qTDs is added to the ASL to simplify removing
352 * qsets (no need to stop the ASL when the last qset is removed).
353 */
354int asl_init(struct whc *whc)
355{
356 struct whc_qset *qset;
357
358 qset = qset_alloc(whc, GFP_KERNEL);
359 if (qset == NULL)
360 return -ENOMEM;
361
362 asl_qset_insert_begin(whc, qset);
363 asl_qset_insert(whc, qset);
364
365 return 0;
366}
367
368/**
369 * asl_clean_up - free ASL resources
370 *
371 * The ASL is stopped and empty except for the dummy qset.
372 */
373void asl_clean_up(struct whc *whc)
374{
375 struct whc_qset *qset;
376
377 if (!list_empty(&whc->async_list)) {
378 qset = list_first_entry(&whc->async_list, struct whc_qset, list_node);
379 list_del(&qset->list_node);
380 qset_free(whc, qset);
381 }
382}