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