2 * Wireless Host Controller (WHC) periodic schedule management.
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
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.
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.
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/>.
18 #include <linux/kernel.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/uwb/umc.h>
21 #include <linux/usb.h>
23 #include "../../wusbcore/wusbhc.h"
27 static void update_pzl_pointers(struct whc *whc, int period, u64 addr)
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);
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);
47 whc_qset_set_link_ptr(&whc->pz_list[3], addr);
48 whc_qset_set_link_ptr(&whc->pz_list[11], addr);
51 whc_qset_set_link_ptr(&whc->pz_list[7], addr);
54 whc_qset_set_link_ptr(&whc->pz_list[15], addr);
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.
64 static int qset_get_period(struct whc *whc, struct whc_qset *qset)
66 uint8_t bInterval = qset->ep->desc.bInterval;
75 static void qset_insert_in_sw_list(struct whc *whc, struct whc_qset *qset)
79 period = qset_get_period(whc, qset);
81 qset_clear(whc, qset);
82 list_move(&qset->list_node, &whc->periodic_list[period]);
83 qset->in_sw_list = true;
86 static void pzl_qset_remove(struct whc *whc, struct whc_qset *qset)
88 list_move(&qset->list_node, &whc->periodic_removed_list);
89 qset->in_hw_list = false;
90 qset->in_sw_list = false;
94 * pzl_process_qset - process any recently inactivated or halted qTDs
97 * After inactive qTDs are removed, new qTDs can be added if the
98 * urb queue still contains URBs.
100 * Returns the schedule updates required.
102 static enum whc_update pzl_process_qset(struct whc *whc, struct whc_qset *qset)
104 enum whc_update update = 0;
112 td = &qset->qtd[qset->td_start];
113 status = le32_to_cpu(td->status);
116 * Nothing to do with a still active qTD.
118 if (status & QTD_STS_ACTIVE)
121 if (status & QTD_STS_HALTED) {
123 process_halted_qtd(whc, qset, td);
127 /* Mmm, a completed qTD. */
128 process_inactive_qtd(whc, qset, td);
131 update |= qset_add_qtds(whc, qset);
135 * If there are no qTDs in this qset, remove it from the PZL.
137 if (qset->remove && qset->ntds == 0) {
138 pzl_qset_remove(whc, qset);
139 update |= WHC_UPDATE_REMOVED;
146 * pzl_start - start the periodic schedule
147 * @whc: the WHCI host controller
149 * The PZL must be valid (e.g., all entries in the list should have
152 void pzl_start(struct whc *whc)
154 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
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,
163 * pzl_stop - stop the periodic schedule
164 * @whc: the WHCI host controller
166 void pzl_stop(struct whc *whc)
168 whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, 0);
169 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
170 WUSBSTS_PERIODIC_SCHED, 0,
175 * pzl_update - request a PZL update and wait for the hardware to be synced
177 * @wusbcmd: WUSBCMD value to start the update.
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
183 void pzl_update(struct whc *whc, uint32_t wusbcmd)
185 struct wusbhc *wusbhc = &whc->wusbhc;
188 mutex_lock(&wusbhc->mutex);
189 if (wusbhc->active) {
190 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
191 t = wait_event_timeout(
192 whc->periodic_list_wq,
193 (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0,
194 msecs_to_jiffies(1000));
196 whc_hw_error(whc, "PZL update timeout");
198 mutex_unlock(&wusbhc->mutex);
201 static void update_pzl_hw_view(struct whc *whc)
203 struct whc_qset *qset, *t;
207 for (period = 0; period < 5; period++) {
208 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
209 whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
210 tmp_qh = qset->qset_dma;
211 qset->in_hw_list = true;
213 update_pzl_pointers(whc, period, tmp_qh);
218 * scan_periodic_work - scan the PZL for qsets to process.
220 * Process each qset in the PZL in turn and then signal the WHC that
221 * the PZL has been updated.
223 * Then start, stop or update the periodic schedule as required.
225 void scan_periodic_work(struct work_struct *work)
227 struct whc *whc = container_of(work, struct whc, periodic_work);
228 struct whc_qset *qset, *t;
229 enum whc_update update = 0;
232 spin_lock_irq(&whc->lock);
234 for (period = 4; period >= 0; period--) {
235 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
236 if (!qset->in_hw_list)
237 update |= WHC_UPDATE_ADDED;
238 update |= pzl_process_qset(whc, qset);
242 if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
243 update_pzl_hw_view(whc);
245 spin_unlock_irq(&whc->lock);
248 uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
249 if (update & WHC_UPDATE_REMOVED)
250 wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
251 pzl_update(whc, wusbcmd);
255 * Now that the PZL is updated, complete the removal of any
258 spin_lock(&whc->lock);
260 list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
261 qset_remove_complete(whc, qset);
264 spin_unlock(&whc->lock);
268 * pzl_urb_enqueue - queue an URB onto the periodic list (PZL)
269 * @whc: the WHCI host controller
270 * @urb: the URB to enqueue
271 * @mem_flags: flags for any memory allocations
273 * The qset for the endpoint is obtained and the urb queued on to it.
275 * Work is scheduled to update the hardware's view of the PZL.
277 int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
279 struct whc_qset *qset;
283 spin_lock_irqsave(&whc->lock, flags);
285 qset = get_qset(whc, urb, GFP_ATOMIC);
289 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
291 usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
292 if (!qset->in_sw_list)
293 qset_insert_in_sw_list(whc, qset);
296 spin_unlock_irqrestore(&whc->lock, flags);
299 queue_work(whc->workqueue, &whc->periodic_work);
305 * pzl_urb_dequeue - remove an URB (qset) from the periodic list
306 * @whc: the WHCI host controller
307 * @urb: the URB to dequeue
308 * @status: the current status of the URB
310 * URBs that do yet have qTDs can simply be removed from the software
311 * queue, otherwise the qset must be removed so the qTDs can be safely
314 int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
316 struct whc_urb *wurb = urb->hcpriv;
317 struct whc_qset *qset = wurb->qset;
318 struct whc_std *std, *t;
322 spin_lock_irqsave(&whc->lock, flags);
324 ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
328 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
330 qset_free_std(whc, std);
332 std->qtd = NULL; /* so this std is re-added when the qset is */
335 pzl_qset_remove(whc, qset);
336 wurb->status = status;
337 wurb->is_async = false;
338 queue_work(whc->workqueue, &wurb->dequeue_work);
341 spin_unlock_irqrestore(&whc->lock, flags);
347 * pzl_qset_delete - delete a qset from the PZL
349 void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
352 queue_work(whc->workqueue, &whc->periodic_work);
353 qset_delete(whc, qset);
358 * pzl_init - initialize the periodic zone list
359 * @whc: the WHCI host controller
361 int pzl_init(struct whc *whc)
365 whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
366 &whc->pz_list_dma, GFP_KERNEL);
367 if (whc->pz_list == NULL)
370 /* Set T bit on all elements in PZL. */
371 for (i = 0; i < 16; i++)
372 whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
374 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
380 * pzl_clean_up - free PZL resources
381 * @whc: the WHCI host controller
383 * The PZL is stopped and empty.
385 void pzl_clean_up(struct whc *whc)
388 dma_free_coherent(&whc->umc->dev, sizeof(u64) * 16, whc->pz_list,