blob: 2ae5abf69a6a0693dfea85e3be1f861874efec78 [file] [log] [blame]
David Vrabel7e6133a2008-09-17 16:34:28 +01001/*
2 * Wireless Host Controller (WHC) periodic 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>
David Vrabel7e6133a2008-09-17 16:34:28 +010022
23#include "../../wusbcore/wusbhc.h"
24
25#include "whcd.h"
26
David Vrabel7e6133a2008-09-17 16:34:28 +010027static void update_pzl_pointers(struct whc *whc, int period, u64 addr)
28{
29 switch (period) {
30 case 0:
31 whc_qset_set_link_ptr(&whc->pz_list[0], addr);
32 whc_qset_set_link_ptr(&whc->pz_list[2], addr);
33 whc_qset_set_link_ptr(&whc->pz_list[4], addr);
34 whc_qset_set_link_ptr(&whc->pz_list[6], addr);
35 whc_qset_set_link_ptr(&whc->pz_list[8], addr);
36 whc_qset_set_link_ptr(&whc->pz_list[10], addr);
37 whc_qset_set_link_ptr(&whc->pz_list[12], addr);
38 whc_qset_set_link_ptr(&whc->pz_list[14], addr);
39 break;
40 case 1:
41 whc_qset_set_link_ptr(&whc->pz_list[1], addr);
42 whc_qset_set_link_ptr(&whc->pz_list[5], addr);
43 whc_qset_set_link_ptr(&whc->pz_list[9], addr);
44 whc_qset_set_link_ptr(&whc->pz_list[13], addr);
45 break;
46 case 2:
47 whc_qset_set_link_ptr(&whc->pz_list[3], addr);
48 whc_qset_set_link_ptr(&whc->pz_list[11], addr);
49 break;
50 case 3:
51 whc_qset_set_link_ptr(&whc->pz_list[7], addr);
52 break;
53 case 4:
54 whc_qset_set_link_ptr(&whc->pz_list[15], addr);
55 break;
56 }
57}
58
59/*
60 * Return the 'period' to use for this qset. The minimum interval for
61 * the endpoint is used so whatever urbs are submitted the device is
62 * polled often enough.
63 */
64static int qset_get_period(struct whc *whc, struct whc_qset *qset)
65{
66 uint8_t bInterval = qset->ep->desc.bInterval;
67
68 if (bInterval < 6)
69 bInterval = 6;
70 if (bInterval > 10)
71 bInterval = 10;
72 return bInterval - 6;
73}
74
75static void qset_insert_in_sw_list(struct whc *whc, struct whc_qset *qset)
76{
77 int period;
78
79 period = qset_get_period(whc, qset);
80
81 qset_clear(whc, qset);
82 list_move(&qset->list_node, &whc->periodic_list[period]);
83 qset->in_sw_list = true;
84}
85
86static void pzl_qset_remove(struct whc *whc, struct whc_qset *qset)
87{
88 list_move(&qset->list_node, &whc->periodic_removed_list);
89 qset->in_hw_list = false;
90 qset->in_sw_list = false;
91}
92
93/**
94 * pzl_process_qset - process any recently inactivated or halted qTDs
95 * in a qset.
96 *
97 * After inactive qTDs are removed, new qTDs can be added if the
98 * urb queue still contains URBs.
99 *
100 * Returns the schedule updates required.
101 */
102static enum whc_update pzl_process_qset(struct whc *whc, struct whc_qset *qset)
103{
104 enum whc_update update = 0;
105 uint32_t status = 0;
106
107 while (qset->ntds) {
108 struct whc_qtd *td;
109 int t;
110
111 t = qset->td_start;
112 td = &qset->qtd[qset->td_start];
113 status = le32_to_cpu(td->status);
114
115 /*
116 * Nothing to do with a still active qTD.
117 */
118 if (status & QTD_STS_ACTIVE)
119 break;
120
121 if (status & QTD_STS_HALTED) {
122 /* Ug, an error. */
123 process_halted_qtd(whc, qset, td);
124 goto done;
125 }
126
127 /* Mmm, a completed qTD. */
128 process_inactive_qtd(whc, qset, td);
129 }
130
131 update |= qset_add_qtds(whc, qset);
132
133done:
134 /*
135 * If there are no qTDs in this qset, remove it from the PZL.
136 */
137 if (qset->remove && qset->ntds == 0) {
138 pzl_qset_remove(whc, qset);
139 update |= WHC_UPDATE_REMOVED;
140 }
141
142 return update;
143}
144
145/**
146 * pzl_start - start the periodic schedule
147 * @whc: the WHCI host controller
148 *
149 * The PZL must be valid (e.g., all entries in the list should have
150 * the T bit set).
151 */
152void pzl_start(struct whc *whc)
153{
154 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
155
156 whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, WUSBCMD_PERIODIC_EN);
157 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
158 WUSBSTS_PERIODIC_SCHED, WUSBSTS_PERIODIC_SCHED,
159 1000, "start PZL");
160}
161
162/**
163 * pzl_stop - stop the periodic schedule
164 * @whc: the WHCI host controller
165 */
166void pzl_stop(struct whc *whc)
167{
168 whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, 0);
169 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
170 WUSBSTS_PERIODIC_SCHED, 0,
171 1000, "stop PZL");
172}
173
David Vrabel56968d02008-11-25 14:23:40 +0000174/**
175 * pzl_update - request a PZL update and wait for the hardware to be synced
176 * @whc: the WHCI HC
177 * @wusbcmd: WUSBCMD value to start the update.
178 *
179 * If the WUSB HC is inactive (i.e., the PZL is stopped) then the
180 * update must be skipped as the hardware may not respond to update
181 * requests.
182 */
David Vrabel7e6133a2008-09-17 16:34:28 +0100183void pzl_update(struct whc *whc, uint32_t wusbcmd)
184{
David Vrabel56968d02008-11-25 14:23:40 +0000185 struct wusbhc *wusbhc = &whc->wusbhc;
186
187 mutex_lock(&wusbhc->mutex);
188 if (wusbhc->active) {
189 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
190 wait_event(whc->periodic_list_wq,
191 (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0);
192 }
193 mutex_unlock(&wusbhc->mutex);
David Vrabel7e6133a2008-09-17 16:34:28 +0100194}
195
196static void update_pzl_hw_view(struct whc *whc)
197{
198 struct whc_qset *qset, *t;
199 int period;
200 u64 tmp_qh = 0;
201
202 for (period = 0; period < 5; period++) {
203 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
204 whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
205 tmp_qh = qset->qset_dma;
206 qset->in_hw_list = true;
207 }
208 update_pzl_pointers(whc, period, tmp_qh);
209 }
210}
211
212/**
213 * scan_periodic_work - scan the PZL for qsets to process.
214 *
215 * Process each qset in the PZL in turn and then signal the WHC that
216 * the PZL has been updated.
217 *
218 * Then start, stop or update the periodic schedule as required.
219 */
220void scan_periodic_work(struct work_struct *work)
221{
222 struct whc *whc = container_of(work, struct whc, periodic_work);
223 struct whc_qset *qset, *t;
224 enum whc_update update = 0;
225 int period;
226
227 spin_lock_irq(&whc->lock);
228
David Vrabel7e6133a2008-09-17 16:34:28 +0100229 for (period = 4; period >= 0; period--) {
230 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
231 if (!qset->in_hw_list)
232 update |= WHC_UPDATE_ADDED;
233 update |= pzl_process_qset(whc, qset);
234 }
235 }
236
237 if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
238 update_pzl_hw_view(whc);
239
David Vrabel7e6133a2008-09-17 16:34:28 +0100240 spin_unlock_irq(&whc->lock);
241
242 if (update) {
243 uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
244 if (update & WHC_UPDATE_REMOVED)
245 wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
246 pzl_update(whc, wusbcmd);
247 }
248
249 /*
250 * Now that the PZL is updated, complete the removal of any
251 * removed qsets.
252 */
253 spin_lock(&whc->lock);
254
255 list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
256 qset_remove_complete(whc, qset);
257 }
258
259 spin_unlock(&whc->lock);
260}
261
262/**
263 * pzl_urb_enqueue - queue an URB onto the periodic list (PZL)
264 * @whc: the WHCI host controller
265 * @urb: the URB to enqueue
266 * @mem_flags: flags for any memory allocations
267 *
268 * The qset for the endpoint is obtained and the urb queued on to it.
269 *
270 * Work is scheduled to update the hardware's view of the PZL.
271 */
272int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
273{
274 struct whc_qset *qset;
275 int err;
276 unsigned long flags;
277
278 spin_lock_irqsave(&whc->lock, flags);
279
280 qset = get_qset(whc, urb, GFP_ATOMIC);
281 if (qset == NULL)
282 err = -ENOMEM;
283 else
284 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
285 if (!err) {
286 usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
287 if (!qset->in_sw_list)
288 qset_insert_in_sw_list(whc, qset);
289 }
290
291 spin_unlock_irqrestore(&whc->lock, flags);
292
293 if (!err)
294 queue_work(whc->workqueue, &whc->periodic_work);
295
296 return 0;
297}
298
299/**
300 * pzl_urb_dequeue - remove an URB (qset) from the periodic list
301 * @whc: the WHCI host controller
302 * @urb: the URB to dequeue
303 * @status: the current status of the URB
304 *
305 * URBs that do yet have qTDs can simply be removed from the software
306 * queue, otherwise the qset must be removed so the qTDs can be safely
307 * removed.
308 */
309int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
310{
311 struct whc_urb *wurb = urb->hcpriv;
312 struct whc_qset *qset = wurb->qset;
313 struct whc_std *std, *t;
314 int ret;
315 unsigned long flags;
316
317 spin_lock_irqsave(&whc->lock, flags);
318
319 ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
320 if (ret < 0)
321 goto out;
322
323 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
324 if (std->urb == urb)
325 qset_free_std(whc, std);
326 else
327 std->qtd = NULL; /* so this std is re-added when the qset is */
328 }
329
330 pzl_qset_remove(whc, qset);
331 wurb->status = status;
332 wurb->is_async = false;
333 queue_work(whc->workqueue, &wurb->dequeue_work);
334
335out:
336 spin_unlock_irqrestore(&whc->lock, flags);
337
338 return ret;
339}
340
341/**
342 * pzl_qset_delete - delete a qset from the PZL
343 */
344void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
345{
346 qset->remove = 1;
347 queue_work(whc->workqueue, &whc->periodic_work);
348 qset_delete(whc, qset);
349}
350
351
352/**
353 * pzl_init - initialize the periodic zone list
354 * @whc: the WHCI host controller
355 */
356int pzl_init(struct whc *whc)
357{
358 int i;
359
360 whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
361 &whc->pz_list_dma, GFP_KERNEL);
362 if (whc->pz_list == NULL)
363 return -ENOMEM;
364
365 /* Set T bit on all elements in PZL. */
366 for (i = 0; i < 16; i++)
367 whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
368
369 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
370
371 return 0;
372}
373
374/**
375 * pzl_clean_up - free PZL resources
376 * @whc: the WHCI host controller
377 *
378 * The PZL is stopped and empty.
379 */
380void pzl_clean_up(struct whc *whc)
381{
382 if (whc->pz_list)
383 dma_free_coherent(&whc->umc->dev, sizeof(u64) * 16, whc->pz_list,
384 whc->pz_list_dma);
385}