1
2
3
4
5
6
7#include <linux/kernel.h>
8#include <linux/gfp.h>
9#include <linux/dma-mapping.h>
10#include <linux/uwb/umc.h>
11#include <linux/usb.h>
12
13#include "../../wusbcore/wusbhc.h"
14
15#include "whcd.h"
16
17static void update_pzl_pointers(struct whc *whc, int period, u64 addr)
18{
19 switch (period) {
20 case 0:
21 whc_qset_set_link_ptr(&whc->pz_list[0], addr);
22 whc_qset_set_link_ptr(&whc->pz_list[2], addr);
23 whc_qset_set_link_ptr(&whc->pz_list[4], addr);
24 whc_qset_set_link_ptr(&whc->pz_list[6], addr);
25 whc_qset_set_link_ptr(&whc->pz_list[8], addr);
26 whc_qset_set_link_ptr(&whc->pz_list[10], addr);
27 whc_qset_set_link_ptr(&whc->pz_list[12], addr);
28 whc_qset_set_link_ptr(&whc->pz_list[14], addr);
29 break;
30 case 1:
31 whc_qset_set_link_ptr(&whc->pz_list[1], addr);
32 whc_qset_set_link_ptr(&whc->pz_list[5], addr);
33 whc_qset_set_link_ptr(&whc->pz_list[9], addr);
34 whc_qset_set_link_ptr(&whc->pz_list[13], addr);
35 break;
36 case 2:
37 whc_qset_set_link_ptr(&whc->pz_list[3], addr);
38 whc_qset_set_link_ptr(&whc->pz_list[11], addr);
39 break;
40 case 3:
41 whc_qset_set_link_ptr(&whc->pz_list[7], addr);
42 break;
43 case 4:
44 whc_qset_set_link_ptr(&whc->pz_list[15], addr);
45 break;
46 }
47}
48
49
50
51
52
53
54static int qset_get_period(struct whc *whc, struct whc_qset *qset)
55{
56 uint8_t bInterval = qset->ep->desc.bInterval;
57
58 if (bInterval < 6)
59 bInterval = 6;
60 if (bInterval > 10)
61 bInterval = 10;
62 return bInterval - 6;
63}
64
65static void qset_insert_in_sw_list(struct whc *whc, struct whc_qset *qset)
66{
67 int period;
68
69 period = qset_get_period(whc, qset);
70
71 qset_clear(whc, qset);
72 list_move(&qset->list_node, &whc->periodic_list[period]);
73 qset->in_sw_list = true;
74}
75
76static void pzl_qset_remove(struct whc *whc, struct whc_qset *qset)
77{
78 list_move(&qset->list_node, &whc->periodic_removed_list);
79 qset->in_hw_list = false;
80 qset->in_sw_list = false;
81}
82
83
84
85
86
87
88
89
90
91
92static enum whc_update pzl_process_qset(struct whc *whc, struct whc_qset *qset)
93{
94 enum whc_update update = 0;
95 uint32_t status = 0;
96
97 while (qset->ntds) {
98 struct whc_qtd *td;
99
100 td = &qset->qtd[qset->td_start];
101 status = le32_to_cpu(td->status);
102
103
104
105
106 if (status & QTD_STS_ACTIVE)
107 break;
108
109 if (status & QTD_STS_HALTED) {
110
111 process_halted_qtd(whc, qset, td);
112
113
114
115 update |= WHC_UPDATE_UPDATED;
116 goto done;
117 }
118
119
120 process_inactive_qtd(whc, qset, td);
121 }
122
123 if (!qset->remove)
124 update |= qset_add_qtds(whc, qset);
125
126done:
127
128
129
130 if (qset->remove && qset->ntds == 0) {
131 pzl_qset_remove(whc, qset);
132 update |= WHC_UPDATE_REMOVED;
133 }
134
135 return update;
136}
137
138
139
140
141
142
143
144
145void pzl_start(struct whc *whc)
146{
147 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
148
149 whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, WUSBCMD_PERIODIC_EN);
150 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
151 WUSBSTS_PERIODIC_SCHED, WUSBSTS_PERIODIC_SCHED,
152 1000, "start PZL");
153}
154
155
156
157
158
159void pzl_stop(struct whc *whc)
160{
161 whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, 0);
162 whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
163 WUSBSTS_PERIODIC_SCHED, 0,
164 1000, "stop PZL");
165}
166
167
168
169
170
171
172
173
174
175
176void pzl_update(struct whc *whc, uint32_t wusbcmd)
177{
178 struct wusbhc *wusbhc = &whc->wusbhc;
179 long t;
180
181 mutex_lock(&wusbhc->mutex);
182 if (wusbhc->active) {
183 whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
184 t = wait_event_timeout(
185 whc->periodic_list_wq,
186 (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0,
187 msecs_to_jiffies(1000));
188 if (t == 0)
189 whc_hw_error(whc, "PZL update timeout");
190 }
191 mutex_unlock(&wusbhc->mutex);
192}
193
194static void update_pzl_hw_view(struct whc *whc)
195{
196 struct whc_qset *qset, *t;
197 int period;
198 u64 tmp_qh = 0;
199
200 for (period = 0; period < 5; period++) {
201 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
202 whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
203 tmp_qh = qset->qset_dma;
204 qset->in_hw_list = true;
205 }
206 update_pzl_pointers(whc, period, tmp_qh);
207 }
208}
209
210
211
212
213
214
215
216
217
218void scan_periodic_work(struct work_struct *work)
219{
220 struct whc *whc = container_of(work, struct whc, periodic_work);
221 struct whc_qset *qset, *t;
222 enum whc_update update = 0;
223 int period;
224
225 spin_lock_irq(&whc->lock);
226
227 for (period = 4; period >= 0; period--) {
228 list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
229 if (!qset->in_hw_list)
230 update |= WHC_UPDATE_ADDED;
231 update |= pzl_process_qset(whc, qset);
232 }
233 }
234
235 if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
236 update_pzl_hw_view(whc);
237
238 spin_unlock_irq(&whc->lock);
239
240 if (update) {
241 uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
242 if (update & WHC_UPDATE_REMOVED)
243 wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
244 pzl_update(whc, wusbcmd);
245 }
246
247
248
249
250
251
252
253
254 spin_lock_irq(&whc->lock);
255
256 list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
257 qset_remove_complete(whc, qset);
258 if (qset->reset) {
259 qset_reset(whc, qset);
260 if (!list_empty(&qset->stds)) {
261 qset_insert_in_sw_list(whc, qset);
262 queue_work(whc->workqueue, &whc->periodic_work);
263 }
264 }
265 }
266
267 spin_unlock_irq(&whc->lock);
268}
269
270
271
272
273
274
275
276
277
278
279
280int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
281{
282 struct whc_qset *qset;
283 int err;
284 unsigned long flags;
285
286 spin_lock_irqsave(&whc->lock, flags);
287
288 err = usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
289 if (err < 0) {
290 spin_unlock_irqrestore(&whc->lock, flags);
291 return err;
292 }
293
294 qset = get_qset(whc, urb, GFP_ATOMIC);
295 if (qset == NULL)
296 err = -ENOMEM;
297 else
298 err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
299 if (!err) {
300 if (!qset->in_sw_list && !qset->remove)
301 qset_insert_in_sw_list(whc, qset);
302 } else
303 usb_hcd_unlink_urb_from_ep(&whc->wusbhc.usb_hcd, urb);
304
305 spin_unlock_irqrestore(&whc->lock, flags);
306
307 if (!err)
308 queue_work(whc->workqueue, &whc->periodic_work);
309
310 return err;
311}
312
313
314
315
316
317
318
319
320
321
322
323int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
324{
325 struct whc_urb *wurb = urb->hcpriv;
326 struct whc_qset *qset = wurb->qset;
327 struct whc_std *std, *t;
328 bool has_qtd = false;
329 int ret;
330 unsigned long flags;
331
332 spin_lock_irqsave(&whc->lock, flags);
333
334 ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
335 if (ret < 0)
336 goto out;
337
338 list_for_each_entry_safe(std, t, &qset->stds, list_node) {
339 if (std->urb == urb) {
340 if (std->qtd)
341 has_qtd = true;
342 qset_free_std(whc, std);
343 } else
344 std->qtd = NULL;
345 }
346
347 if (has_qtd) {
348 pzl_qset_remove(whc, qset);
349 update_pzl_hw_view(whc);
350 wurb->status = status;
351 wurb->is_async = false;
352 queue_work(whc->workqueue, &wurb->dequeue_work);
353 } else
354 qset_remove_urb(whc, qset, urb, status);
355out:
356 spin_unlock_irqrestore(&whc->lock, flags);
357
358 return ret;
359}
360
361
362
363
364void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
365{
366 qset->remove = 1;
367 queue_work(whc->workqueue, &whc->periodic_work);
368 qset_delete(whc, qset);
369}
370
371
372
373
374
375int pzl_init(struct whc *whc)
376{
377 int i;
378
379 whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
380 &whc->pz_list_dma, GFP_KERNEL);
381 if (whc->pz_list == NULL)
382 return -ENOMEM;
383
384
385 for (i = 0; i < 16; i++)
386 whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
387
388 le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
389
390 return 0;
391}
392
393
394
395
396
397
398
399void pzl_clean_up(struct whc *whc)
400{
401 if (whc->pz_list)
402 dma_free_coherent(&whc->umc->dev, sizeof(u64) * 16, whc->pz_list,
403 whc->pz_list_dma);
404}
405