2 * Wireless Host Controller (WHC) asynchronous 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 <linux/uwb/debug.h>
25 #include "../../wusbcore/wusbhc.h"
30 static void dump_asl(struct whc
*whc
, const char *tag
)
32 struct device
*dev
= &whc
->umc
->dev
;
33 struct whc_qset
*qset
;
35 d_printf(4, dev
, "ASL %s\n", tag
);
37 list_for_each_entry(qset
, &whc
->async_list
, list_node
) {
42 static inline void dump_asl(struct whc
*whc
, const char *tag
)
48 static void qset_get_next_prev(struct whc
*whc
, struct whc_qset
*qset
,
49 struct whc_qset
**next
, struct whc_qset
**prev
)
51 struct list_head
*n
, *p
;
53 BUG_ON(list_empty(&whc
->async_list
));
55 n
= qset
->list_node
.next
;
56 if (n
== &whc
->async_list
)
58 p
= qset
->list_node
.prev
;
59 if (p
== &whc
->async_list
)
62 *next
= container_of(n
, struct whc_qset
, list_node
);
63 *prev
= container_of(p
, struct whc_qset
, list_node
);
67 static void asl_qset_insert_begin(struct whc
*whc
, struct whc_qset
*qset
)
69 list_move(&qset
->list_node
, &whc
->async_list
);
70 qset
->in_sw_list
= true;
73 static void asl_qset_insert(struct whc
*whc
, struct whc_qset
*qset
)
75 struct whc_qset
*next
, *prev
;
77 qset_clear(whc
, qset
);
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;
86 static void asl_qset_remove(struct whc
*whc
, struct whc_qset
*qset
)
88 struct whc_qset
*prev
, *next
;
90 qset_get_next_prev(whc
, qset
, &next
, &prev
);
92 list_move(&qset
->list_node
, &whc
->async_removed_list
);
93 qset
->in_sw_list
= false;
96 * No more qsets in the ASL? The caller must stop the ASL as
97 * it's no longer valid.
99 if (list_empty(&whc
->async_list
))
102 /* Remove from ASL. */
103 whc_qset_set_link_ptr(&prev
->qh
.link
, next
->qset_dma
);
104 qset
->in_hw_list
= false;
108 * process_qset - process any recently inactivated or halted qTDs in a
111 * After inactive qTDs are removed, new qTDs can be added if the
112 * urb queue still contains URBs.
114 * Returns any additional WUSBCMD bits for the ASL sync command (i.e.,
115 * WUSBCMD_ASYNC_QSET_RM if a halted qset was removed).
117 static uint32_t process_qset(struct whc
*whc
, struct whc_qset
*qset
)
119 enum whc_update update
= 0;
127 td
= &qset
->qtd
[qset
->td_start
];
128 status
= le32_to_cpu(td
->status
);
131 * Nothing to do with a still active qTD.
133 if (status
& QTD_STS_ACTIVE
)
136 if (status
& QTD_STS_HALTED
) {
138 process_halted_qtd(whc
, qset
, td
);
142 /* Mmm, a completed qTD. */
143 process_inactive_qtd(whc
, qset
, td
);
146 update
|= qset_add_qtds(whc
, qset
);
150 * Remove this qset from the ASL if requested, but only if has
153 if (qset
->remove
&& qset
->ntds
== 0) {
154 asl_qset_remove(whc
, qset
);
155 update
|= WHC_UPDATE_REMOVED
;
160 void asl_start(struct whc
*whc
)
162 struct whc_qset
*qset
;
164 qset
= list_first_entry(&whc
->async_list
, struct whc_qset
, list_node
);
166 le_writeq(qset
->qset_dma
| QH_LINK_NTDS(8), whc
->base
+ WUSBASYNCLISTADDR
);
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
,
174 void asl_stop(struct whc
*whc
)
176 whc_write_wusbcmd(whc
, WUSBCMD_ASYNC_EN
, 0);
177 whci_wait_for(&whc
->umc
->dev
, whc
->base
+ WUSBSTS
,
178 WUSBSTS_ASYNC_SCHED
, 0,
182 void asl_update(struct whc
*whc
, uint32_t wusbcmd
)
184 whc_write_wusbcmd(whc
, wusbcmd
, wusbcmd
);
185 wait_event(whc
->async_list_wq
,
186 (le_readl(whc
->base
+ WUSBCMD
) & WUSBCMD_ASYNC_UPDATED
) == 0);
190 * scan_async_work - scan the ASL for qsets to process.
192 * Process each qset in the ASL in turn and then signal the WHC that
193 * the ASL has been updated.
195 * Then start, stop or update the asynchronous schedule as required.
197 void scan_async_work(struct work_struct
*work
)
199 struct whc
*whc
= container_of(work
, struct whc
, async_work
);
200 struct whc_qset
*qset
, *t
;
201 enum whc_update update
= 0;
203 spin_lock_irq(&whc
->lock
);
205 dump_asl(whc
, "before processing");
208 * Transerve the software list backwards so new qsets can be
209 * safely inserted into the ASL without making it non-circular.
211 list_for_each_entry_safe_reverse(qset
, t
, &whc
->async_list
, list_node
) {
212 if (!qset
->in_hw_list
) {
213 asl_qset_insert(whc
, qset
);
214 update
|= WHC_UPDATE_ADDED
;
217 update
|= process_qset(whc
, qset
);
220 dump_asl(whc
, "after processing");
222 spin_unlock_irq(&whc
->lock
);
225 uint32_t wusbcmd
= WUSBCMD_ASYNC_UPDATED
| WUSBCMD_ASYNC_SYNCED_DB
;
226 if (update
& WHC_UPDATE_REMOVED
)
227 wusbcmd
|= WUSBCMD_ASYNC_QSET_RM
;
228 asl_update(whc
, wusbcmd
);
232 * Now that the ASL is updated, complete the removal of any
235 spin_lock(&whc
->lock
);
237 list_for_each_entry_safe(qset
, t
, &whc
->async_removed_list
, list_node
) {
238 qset_remove_complete(whc
, qset
);
241 spin_unlock(&whc
->lock
);
245 * asl_urb_enqueue - queue an URB onto the asynchronous list (ASL).
246 * @whc: the WHCI host controller
247 * @urb: the URB to enqueue
248 * @mem_flags: flags for any memory allocations
250 * The qset for the endpoint is obtained and the urb queued on to it.
252 * Work is scheduled to update the hardware's view of the ASL.
254 int asl_urb_enqueue(struct whc
*whc
, struct urb
*urb
, gfp_t mem_flags
)
256 struct whc_qset
*qset
;
260 spin_lock_irqsave(&whc
->lock
, flags
);
262 qset
= get_qset(whc
, urb
, GFP_ATOMIC
);
266 err
= qset_add_urb(whc
, qset
, urb
, GFP_ATOMIC
);
268 usb_hcd_link_urb_to_ep(&whc
->wusbhc
.usb_hcd
, urb
);
269 if (!qset
->in_sw_list
)
270 asl_qset_insert_begin(whc
, qset
);
273 spin_unlock_irqrestore(&whc
->lock
, flags
);
276 queue_work(whc
->workqueue
, &whc
->async_work
);
282 * asl_urb_dequeue - remove an URB (qset) from the async list.
283 * @whc: the WHCI host controller
284 * @urb: the URB to dequeue
285 * @status: the current status of the URB
287 * URBs that do yet have qTDs can simply be removed from the software
288 * queue, otherwise the qset must be removed from the ASL so the qTDs
291 int asl_urb_dequeue(struct whc
*whc
, struct urb
*urb
, int status
)
293 struct whc_urb
*wurb
= urb
->hcpriv
;
294 struct whc_qset
*qset
= wurb
->qset
;
295 struct whc_std
*std
, *t
;
299 spin_lock_irqsave(&whc
->lock
, flags
);
301 ret
= usb_hcd_check_unlink_urb(&whc
->wusbhc
.usb_hcd
, urb
, status
);
305 list_for_each_entry_safe(std
, t
, &qset
->stds
, list_node
) {
307 qset_free_std(whc
, std
);
309 std
->qtd
= NULL
; /* so this std is re-added when the qset is */
312 asl_qset_remove(whc
, qset
);
313 wurb
->status
= status
;
314 wurb
->is_async
= true;
315 queue_work(whc
->workqueue
, &wurb
->dequeue_work
);
318 spin_unlock_irqrestore(&whc
->lock
, flags
);
324 * asl_qset_delete - delete a qset from the ASL
326 void asl_qset_delete(struct whc
*whc
, struct whc_qset
*qset
)
329 queue_work(whc
->workqueue
, &whc
->async_work
);
330 qset_delete(whc
, qset
);
334 * asl_init - initialize the asynchronous schedule list
336 * A dummy qset with no qTDs is added to the ASL to simplify removing
337 * qsets (no need to stop the ASL when the last qset is removed).
339 int asl_init(struct whc
*whc
)
341 struct whc_qset
*qset
;
343 qset
= qset_alloc(whc
, GFP_KERNEL
);
347 asl_qset_insert_begin(whc
, qset
);
348 asl_qset_insert(whc
, qset
);
354 * asl_clean_up - free ASL resources
356 * The ASL is stopped and empty except for the dummy qset.
358 void asl_clean_up(struct whc
*whc
)
360 struct whc_qset
*qset
;
362 if (!list_empty(&whc
->async_list
)) {
363 qset
= list_first_entry(&whc
->async_list
, struct whc_qset
, list_node
);
364 list_del(&qset
->list_node
);
365 qset_free(whc
, qset
);