Merge branch 'master' into percpu
[pandora-kernel.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
23 #include "../../wusbcore/wusbhc.h"
24
25 #include "whcd.h"
26
27 static 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  */
64 static 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
75 static 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
86 static 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  */
102 static 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                         /* A halted qTD always triggers an update
125                            because the qset was either removed or
126                            reactivated. */
127                         update |= WHC_UPDATE_UPDATED;
128                         goto done;
129                 }
130
131                 /* Mmm, a completed qTD. */
132                 process_inactive_qtd(whc, qset, td);
133         }
134
135         if (!qset->remove)
136                 update |= qset_add_qtds(whc, qset);
137
138 done:
139         /*
140          * If there are no qTDs in this qset, remove it from the PZL.
141          */
142         if (qset->remove && qset->ntds == 0) {
143                 pzl_qset_remove(whc, qset);
144                 update |= WHC_UPDATE_REMOVED;
145         }
146
147         return update;
148 }
149
150 /**
151  * pzl_start - start the periodic schedule
152  * @whc: the WHCI host controller
153  *
154  * The PZL must be valid (e.g., all entries in the list should have
155  * the T bit set).
156  */
157 void pzl_start(struct whc *whc)
158 {
159         le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
160
161         whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, WUSBCMD_PERIODIC_EN);
162         whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
163                       WUSBSTS_PERIODIC_SCHED, WUSBSTS_PERIODIC_SCHED,
164                       1000, "start PZL");
165 }
166
167 /**
168  * pzl_stop - stop the periodic schedule
169  * @whc: the WHCI host controller
170  */
171 void pzl_stop(struct whc *whc)
172 {
173         whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, 0);
174         whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
175                       WUSBSTS_PERIODIC_SCHED, 0,
176                       1000, "stop PZL");
177 }
178
179 /**
180  * pzl_update - request a PZL update and wait for the hardware to be synced
181  * @whc: the WHCI HC
182  * @wusbcmd: WUSBCMD value to start the update.
183  *
184  * If the WUSB HC is inactive (i.e., the PZL is stopped) then the
185  * update must be skipped as the hardware may not respond to update
186  * requests.
187  */
188 void pzl_update(struct whc *whc, uint32_t wusbcmd)
189 {
190         struct wusbhc *wusbhc = &whc->wusbhc;
191         long t;
192
193         mutex_lock(&wusbhc->mutex);
194         if (wusbhc->active) {
195                 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
196                 t = wait_event_timeout(
197                         whc->periodic_list_wq,
198                         (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0,
199                         msecs_to_jiffies(1000));
200                 if (t == 0)
201                         whc_hw_error(whc, "PZL update timeout");
202         }
203         mutex_unlock(&wusbhc->mutex);
204 }
205
206 static void update_pzl_hw_view(struct whc *whc)
207 {
208         struct whc_qset *qset, *t;
209         int period;
210         u64 tmp_qh = 0;
211
212         for (period = 0; period < 5; period++) {
213                 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
214                         whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
215                         tmp_qh = qset->qset_dma;
216                         qset->in_hw_list = true;
217                 }
218                 update_pzl_pointers(whc, period, tmp_qh);
219         }
220 }
221
222 /**
223  * scan_periodic_work - scan the PZL for qsets to process.
224  *
225  * Process each qset in the PZL in turn and then signal the WHC that
226  * the PZL has been updated.
227  *
228  * Then start, stop or update the periodic schedule as required.
229  */
230 void scan_periodic_work(struct work_struct *work)
231 {
232         struct whc *whc = container_of(work, struct whc, periodic_work);
233         struct whc_qset *qset, *t;
234         enum whc_update update = 0;
235         int period;
236
237         spin_lock_irq(&whc->lock);
238
239         for (period = 4; period >= 0; period--) {
240                 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
241                         if (!qset->in_hw_list)
242                                 update |= WHC_UPDATE_ADDED;
243                         update |= pzl_process_qset(whc, qset);
244                 }
245         }
246
247         if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
248                 update_pzl_hw_view(whc);
249
250         spin_unlock_irq(&whc->lock);
251
252         if (update) {
253                 uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
254                 if (update & WHC_UPDATE_REMOVED)
255                         wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
256                 pzl_update(whc, wusbcmd);
257         }
258
259         /*
260          * Now that the PZL is updated, complete the removal of any
261          * removed qsets.
262          *
263          * If the qset was to be reset, do so and reinsert it into the
264          * PZL if it has pending transfers.
265          */
266         spin_lock_irq(&whc->lock);
267
268         list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
269                 qset_remove_complete(whc, qset);
270                 if (qset->reset) {
271                         qset_reset(whc, qset);
272                         if (!list_empty(&qset->stds)) {
273                                 qset_insert_in_sw_list(whc, qset);
274                                 queue_work(whc->workqueue, &whc->periodic_work);
275                         }
276                 }
277         }
278
279         spin_unlock_irq(&whc->lock);
280 }
281
282 /**
283  * pzl_urb_enqueue - queue an URB onto the periodic list (PZL)
284  * @whc: the WHCI host controller
285  * @urb: the URB to enqueue
286  * @mem_flags: flags for any memory allocations
287  *
288  * The qset for the endpoint is obtained and the urb queued on to it.
289  *
290  * Work is scheduled to update the hardware's view of the PZL.
291  */
292 int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
293 {
294         struct whc_qset *qset;
295         int err;
296         unsigned long flags;
297
298         spin_lock_irqsave(&whc->lock, flags);
299
300         err = usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
301         if (err < 0) {
302                 spin_unlock_irqrestore(&whc->lock, flags);
303                 return err;
304         }
305
306         qset = get_qset(whc, urb, GFP_ATOMIC);
307         if (qset == NULL)
308                 err = -ENOMEM;
309         else
310                 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
311         if (!err) {
312                 if (!qset->in_sw_list && !qset->remove)
313                         qset_insert_in_sw_list(whc, qset);
314         } else
315                 usb_hcd_unlink_urb_from_ep(&whc->wusbhc.usb_hcd, urb);
316
317         spin_unlock_irqrestore(&whc->lock, flags);
318
319         if (!err)
320                 queue_work(whc->workqueue, &whc->periodic_work);
321
322         return err;
323 }
324
325 /**
326  * pzl_urb_dequeue - remove an URB (qset) from the periodic list
327  * @whc: the WHCI host controller
328  * @urb: the URB to dequeue
329  * @status: the current status of the URB
330  *
331  * URBs that do yet have qTDs can simply be removed from the software
332  * queue, otherwise the qset must be removed so the qTDs can be safely
333  * removed.
334  */
335 int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
336 {
337         struct whc_urb *wurb = urb->hcpriv;
338         struct whc_qset *qset = wurb->qset;
339         struct whc_std *std, *t;
340         bool has_qtd = false;
341         int ret;
342         unsigned long flags;
343
344         spin_lock_irqsave(&whc->lock, flags);
345
346         ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
347         if (ret < 0)
348                 goto out;
349
350         list_for_each_entry_safe(std, t, &qset->stds, list_node) {
351                 if (std->urb == urb) {
352                         if (std->qtd)
353                                 has_qtd = true;
354                         qset_free_std(whc, std);
355                 } else
356                         std->qtd = NULL; /* so this std is re-added when the qset is */
357         }
358
359         if (has_qtd) {
360                 pzl_qset_remove(whc, qset);
361                 update_pzl_hw_view(whc);
362                 wurb->status = status;
363                 wurb->is_async = false;
364                 queue_work(whc->workqueue, &wurb->dequeue_work);
365         } else
366                 qset_remove_urb(whc, qset, urb, status);
367 out:
368         spin_unlock_irqrestore(&whc->lock, flags);
369
370         return ret;
371 }
372
373 /**
374  * pzl_qset_delete - delete a qset from the PZL
375  */
376 void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
377 {
378         qset->remove = 1;
379         queue_work(whc->workqueue, &whc->periodic_work);
380         qset_delete(whc, qset);
381 }
382
383 /**
384  * pzl_init - initialize the periodic zone list
385  * @whc: the WHCI host controller
386  */
387 int pzl_init(struct whc *whc)
388 {
389         int i;
390
391         whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
392                                           &whc->pz_list_dma, GFP_KERNEL);
393         if (whc->pz_list == NULL)
394                 return -ENOMEM;
395
396         /* Set T bit on all elements in PZL. */
397         for (i = 0; i < 16; i++)
398                 whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
399
400         le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
401
402         return 0;
403 }
404
405 /**
406  * pzl_clean_up - free PZL resources
407  * @whc: the WHCI host controller
408  *
409  * The PZL is stopped and empty.
410  */
411 void pzl_clean_up(struct whc *whc)
412 {
413         if (whc->pz_list)
414                 dma_free_coherent(&whc->umc->dev,  sizeof(u64) * 16, whc->pz_list,
415                                   whc->pz_list_dma);
416 }