1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/device.h>
14#include <linux/dmapool.h>
15#include <linux/kernel.h>
16#include <linux/delay.h>
17#include <linux/ioport.h>
18#include <linux/sched.h>
19#include <linux/vmalloc.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/hrtimer.h>
23#include <linux/list.h>
24#include <linux/interrupt.h>
25#include <linux/usb.h>
26#include <linux/usb/hcd.h>
27#include <linux/moduleparam.h>
28#include <linux/dma-mapping.h>
29#include <linux/debugfs.h>
30#include <linux/slab.h>
31#include <linux/uaccess.h>
32#include <linux/platform_device.h>
33#include <linux/io.h>
34
35#include <asm/byteorder.h>
36#include <asm/irq.h>
37#include <asm/unaligned.h>
38
39#define DRIVER_AUTHOR "Yuan-Hsin Chen"
40#define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
41static const char hcd_name[] = "fotg210_hcd";
42
43#undef FOTG210_URB_TRACE
44#define FOTG210_STATS
45
46
47#define FOTG210_TUNE_CERR 3
48#define FOTG210_TUNE_RL_HS 4
49#define FOTG210_TUNE_RL_TT 0
50#define FOTG210_TUNE_MULT_HS 1
51#define FOTG210_TUNE_MULT_TT 1
52
53
54
55
56
57
58#define FOTG210_TUNE_FLS 1
59
60
61static int log2_irq_thresh;
62module_param(log2_irq_thresh, int, S_IRUGO);
63MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
64
65
66static unsigned park;
67module_param(park, uint, S_IRUGO);
68MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
69
70
71static unsigned int hird;
72module_param(hird, int, S_IRUGO);
73MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
74
75#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
76
77#include "fotg210.h"
78
79#define fotg210_dbg(fotg210, fmt, args...) \
80 dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
81#define fotg210_err(fotg210, fmt, args...) \
82 dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
83#define fotg210_info(fotg210, fmt, args...) \
84 dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
85#define fotg210_warn(fotg210, fmt, args...) \
86 dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
87
88
89
90
91static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
92{
93 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
94
95 fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
96 HCS_N_PORTS(params));
97}
98
99
100
101
102static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
103{
104 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
105
106 fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label,
107 params,
108 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
109 HCC_CANPARK(params) ? " park" : "");
110}
111
112static void __maybe_unused
113dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
114{
115 fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
116 hc32_to_cpup(fotg210, &qtd->hw_next),
117 hc32_to_cpup(fotg210, &qtd->hw_alt_next),
118 hc32_to_cpup(fotg210, &qtd->hw_token),
119 hc32_to_cpup(fotg210, &qtd->hw_buf[0]));
120 if (qtd->hw_buf[1])
121 fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
122 hc32_to_cpup(fotg210, &qtd->hw_buf[1]),
123 hc32_to_cpup(fotg210, &qtd->hw_buf[2]),
124 hc32_to_cpup(fotg210, &qtd->hw_buf[3]),
125 hc32_to_cpup(fotg210, &qtd->hw_buf[4]));
126}
127
128static void __maybe_unused
129dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
130{
131 struct fotg210_qh_hw *hw = qh->hw;
132
133 fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label, qh,
134 hw->hw_next, hw->hw_info1, hw->hw_info2,
135 hw->hw_current);
136
137 dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
138}
139
140static void __maybe_unused
141dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
142{
143 fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n", label,
144 itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next),
145 itd->urb);
146
147 fotg210_dbg(fotg210,
148 " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
149 hc32_to_cpu(fotg210, itd->hw_transaction[0]),
150 hc32_to_cpu(fotg210, itd->hw_transaction[1]),
151 hc32_to_cpu(fotg210, itd->hw_transaction[2]),
152 hc32_to_cpu(fotg210, itd->hw_transaction[3]),
153 hc32_to_cpu(fotg210, itd->hw_transaction[4]),
154 hc32_to_cpu(fotg210, itd->hw_transaction[5]),
155 hc32_to_cpu(fotg210, itd->hw_transaction[6]),
156 hc32_to_cpu(fotg210, itd->hw_transaction[7]));
157
158 fotg210_dbg(fotg210,
159 " buf: %08x %08x %08x %08x %08x %08x %08x\n",
160 hc32_to_cpu(fotg210, itd->hw_bufp[0]),
161 hc32_to_cpu(fotg210, itd->hw_bufp[1]),
162 hc32_to_cpu(fotg210, itd->hw_bufp[2]),
163 hc32_to_cpu(fotg210, itd->hw_bufp[3]),
164 hc32_to_cpu(fotg210, itd->hw_bufp[4]),
165 hc32_to_cpu(fotg210, itd->hw_bufp[5]),
166 hc32_to_cpu(fotg210, itd->hw_bufp[6]));
167
168 fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n",
169 itd->index[0], itd->index[1], itd->index[2],
170 itd->index[3], itd->index[4], itd->index[5],
171 itd->index[6], itd->index[7]);
172}
173
174static int __maybe_unused
175dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
176{
177 return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
178 label, label[0] ? " " : "", status,
179 (status & STS_ASS) ? " Async" : "",
180 (status & STS_PSS) ? " Periodic" : "",
181 (status & STS_RECL) ? " Recl" : "",
182 (status & STS_HALT) ? " Halt" : "",
183 (status & STS_IAA) ? " IAA" : "",
184 (status & STS_FATAL) ? " FATAL" : "",
185 (status & STS_FLR) ? " FLR" : "",
186 (status & STS_PCD) ? " PCD" : "",
187 (status & STS_ERR) ? " ERR" : "",
188 (status & STS_INT) ? " INT" : "");
189}
190
191static int __maybe_unused
192dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
193{
194 return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
195 label, label[0] ? " " : "", enable,
196 (enable & STS_IAA) ? " IAA" : "",
197 (enable & STS_FATAL) ? " FATAL" : "",
198 (enable & STS_FLR) ? " FLR" : "",
199 (enable & STS_PCD) ? " PCD" : "",
200 (enable & STS_ERR) ? " ERR" : "",
201 (enable & STS_INT) ? " INT" : "");
202}
203
204static const char *const fls_strings[] = { "1024", "512", "256", "??" };
205
206static int dbg_command_buf(char *buf, unsigned len, const char *label,
207 u32 command)
208{
209 return scnprintf(buf, len,
210 "%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s",
211 label, label[0] ? " " : "", command,
212 (command & CMD_PARK) ? " park" : "(park)",
213 CMD_PARK_CNT(command),
214 (command >> 16) & 0x3f,
215 (command & CMD_IAAD) ? " IAAD" : "",
216 (command & CMD_ASE) ? " Async" : "",
217 (command & CMD_PSE) ? " Periodic" : "",
218 fls_strings[(command >> 2) & 0x3],
219 (command & CMD_RESET) ? " Reset" : "",
220 (command & CMD_RUN) ? "RUN" : "HALT");
221}
222
223static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
224 u32 status)
225{
226 char *sig;
227
228
229 switch (status & (3 << 10)) {
230 case 0 << 10:
231 sig = "se0";
232 break;
233 case 1 << 10:
234 sig = "k";
235 break;
236 case 2 << 10:
237 sig = "j";
238 break;
239 default:
240 sig = "?";
241 break;
242 }
243
244 scnprintf(buf, len, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s",
245 label, label[0] ? " " : "", port, status,
246 status >> 25,
247 sig,
248 (status & PORT_RESET) ? " RESET" : "",
249 (status & PORT_SUSPEND) ? " SUSPEND" : "",
250 (status & PORT_RESUME) ? " RESUME" : "",
251 (status & PORT_PEC) ? " PEC" : "",
252 (status & PORT_PE) ? " PE" : "",
253 (status & PORT_CSC) ? " CSC" : "",
254 (status & PORT_CONNECT) ? " CONNECT" : "");
255
256 return buf;
257}
258
259
260#define dbg_status(fotg210, label, status) { \
261 char _buf[80]; \
262 dbg_status_buf(_buf, sizeof(_buf), label, status); \
263 fotg210_dbg(fotg210, "%s\n", _buf); \
264}
265
266#define dbg_cmd(fotg210, label, command) { \
267 char _buf[80]; \
268 dbg_command_buf(_buf, sizeof(_buf), label, command); \
269 fotg210_dbg(fotg210, "%s\n", _buf); \
270}
271
272#define dbg_port(fotg210, label, port, status) { \
273 char _buf[80]; \
274 fotg210_dbg(fotg210, "%s\n", \
275 dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
276}
277
278
279static int debug_async_open(struct inode *, struct file *);
280static int debug_periodic_open(struct inode *, struct file *);
281static int debug_registers_open(struct inode *, struct file *);
282static int debug_async_open(struct inode *, struct file *);
283
284static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
285static int debug_close(struct inode *, struct file *);
286
287static const struct file_operations debug_async_fops = {
288 .owner = THIS_MODULE,
289 .open = debug_async_open,
290 .read = debug_output,
291 .release = debug_close,
292 .llseek = default_llseek,
293};
294static const struct file_operations debug_periodic_fops = {
295 .owner = THIS_MODULE,
296 .open = debug_periodic_open,
297 .read = debug_output,
298 .release = debug_close,
299 .llseek = default_llseek,
300};
301static const struct file_operations debug_registers_fops = {
302 .owner = THIS_MODULE,
303 .open = debug_registers_open,
304 .read = debug_output,
305 .release = debug_close,
306 .llseek = default_llseek,
307};
308
309static struct dentry *fotg210_debug_root;
310
311struct debug_buffer {
312 ssize_t (*fill_func)(struct debug_buffer *);
313 struct usb_bus *bus;
314 struct mutex mutex;
315 size_t count;
316 char *output_buf;
317 size_t alloc_size;
318};
319
320static inline char speed_char(u32 scratch)
321{
322 switch (scratch & (3 << 12)) {
323 case QH_FULL_SPEED:
324 return 'f';
325
326 case QH_LOW_SPEED:
327 return 'l';
328
329 case QH_HIGH_SPEED:
330 return 'h';
331
332 default:
333 return '?';
334 }
335}
336
337static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
338{
339 __u32 v = hc32_to_cpu(fotg210, token);
340
341 if (v & QTD_STS_ACTIVE)
342 return '*';
343 if (v & QTD_STS_HALT)
344 return '-';
345 if (!IS_SHORT_READ(v))
346 return ' ';
347
348 return '/';
349}
350
351static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
352 char **nextp, unsigned *sizep)
353{
354 u32 scratch;
355 u32 hw_curr;
356 struct fotg210_qtd *td;
357 unsigned temp;
358 unsigned size = *sizep;
359 char *next = *nextp;
360 char mark;
361 __le32 list_end = FOTG210_LIST_END(fotg210);
362 struct fotg210_qh_hw *hw = qh->hw;
363
364 if (hw->hw_qtd_next == list_end)
365 mark = '@';
366 else
367 mark = token_mark(fotg210, hw->hw_token);
368 if (mark == '/') {
369 if ((hw->hw_alt_next & QTD_MASK(fotg210)) ==
370 fotg210->async->hw->hw_alt_next)
371 mark = '#';
372 else if (hw->hw_alt_next == list_end)
373 mark = '.';
374
375 }
376 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
377 hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0;
378 temp = scnprintf(next, size,
379 "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
380 qh, scratch & 0x007f,
381 speed_char(scratch),
382 (scratch >> 8) & 0x000f,
383 scratch, hc32_to_cpup(fotg210, &hw->hw_info2),
384 hc32_to_cpup(fotg210, &hw->hw_token), mark,
385 (cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token)
386 ? "data1" : "data0",
387 (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
388 size -= temp;
389 next += temp;
390
391
392 list_for_each_entry(td, &qh->qtd_list, qtd_list) {
393 scratch = hc32_to_cpup(fotg210, &td->hw_token);
394 mark = ' ';
395 if (hw_curr == td->qtd_dma)
396 mark = '*';
397 else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
398 mark = '+';
399 else if (QTD_LENGTH(scratch)) {
400 if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
401 mark = '#';
402 else if (td->hw_alt_next != list_end)
403 mark = '/';
404 }
405 temp = snprintf(next, size,
406 "\n\t%p%c%s len=%d %08x urb %p",
407 td, mark, ({ char *tmp;
408 switch ((scratch>>8)&0x03) {
409 case 0:
410 tmp = "out";
411 break;
412 case 1:
413 tmp = "in";
414 break;
415 case 2:
416 tmp = "setup";
417 break;
418 default:
419 tmp = "?";
420 break;
421 } tmp; }),
422 (scratch >> 16) & 0x7fff,
423 scratch,
424 td->urb);
425 if (size < temp)
426 temp = size;
427 size -= temp;
428 next += temp;
429 if (temp == size)
430 goto done;
431 }
432
433 temp = snprintf(next, size, "\n");
434 if (size < temp)
435 temp = size;
436
437 size -= temp;
438 next += temp;
439
440done:
441 *sizep = size;
442 *nextp = next;
443}
444
445static ssize_t fill_async_buffer(struct debug_buffer *buf)
446{
447 struct usb_hcd *hcd;
448 struct fotg210_hcd *fotg210;
449 unsigned long flags;
450 unsigned temp, size;
451 char *next;
452 struct fotg210_qh *qh;
453
454 hcd = bus_to_hcd(buf->bus);
455 fotg210 = hcd_to_fotg210(hcd);
456 next = buf->output_buf;
457 size = buf->alloc_size;
458
459 *next = 0;
460
461
462
463
464
465 spin_lock_irqsave(&fotg210->lock, flags);
466 for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
467 qh = qh->qh_next.qh)
468 qh_lines(fotg210, qh, &next, &size);
469 if (fotg210->async_unlink && size > 0) {
470 temp = scnprintf(next, size, "\nunlink =\n");
471 size -= temp;
472 next += temp;
473
474 for (qh = fotg210->async_unlink; size > 0 && qh;
475 qh = qh->unlink_next)
476 qh_lines(fotg210, qh, &next, &size);
477 }
478 spin_unlock_irqrestore(&fotg210->lock, flags);
479
480 return strlen(buf->output_buf);
481}
482
483
484static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
485 struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
486{
487 u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
488 struct fotg210_qtd *qtd;
489 char *type = "";
490 unsigned temp = 0;
491
492
493 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
494 temp++;
495 switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
496 case 0:
497 type = "out";
498 continue;
499 case 1:
500 type = "in";
501 continue;
502 }
503 }
504
505 return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)",
506 speed_char(scratch), scratch & 0x007f,
507 (scratch >> 8) & 0x000f, type, qh->usecs,
508 qh->c_usecs, temp, (scratch >> 16) & 0x7ff);
509}
510
511#define DBG_SCHED_LIMIT 64
512static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
513{
514 struct usb_hcd *hcd;
515 struct fotg210_hcd *fotg210;
516 unsigned long flags;
517 union fotg210_shadow p, *seen;
518 unsigned temp, size, seen_count;
519 char *next;
520 unsigned i;
521 __hc32 tag;
522
523 seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
524 if (!seen)
525 return 0;
526
527 seen_count = 0;
528
529 hcd = bus_to_hcd(buf->bus);
530 fotg210 = hcd_to_fotg210(hcd);
531 next = buf->output_buf;
532 size = buf->alloc_size;
533
534 temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
535 size -= temp;
536 next += temp;
537
538
539
540
541 spin_lock_irqsave(&fotg210->lock, flags);
542 for (i = 0; i < fotg210->periodic_size; i++) {
543 p = fotg210->pshadow[i];
544 if (likely(!p.ptr))
545 continue;
546
547 tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);
548
549 temp = scnprintf(next, size, "%4d: ", i);
550 size -= temp;
551 next += temp;
552
553 do {
554 struct fotg210_qh_hw *hw;
555
556 switch (hc32_to_cpu(fotg210, tag)) {
557 case Q_TYPE_QH:
558 hw = p.qh->hw;
559 temp = scnprintf(next, size, " qh%d-%04x/%p",
560 p.qh->period,
561 hc32_to_cpup(fotg210,
562 &hw->hw_info2)
563
564 & (QH_CMASK | QH_SMASK),
565 p.qh);
566 size -= temp;
567 next += temp;
568
569 for (temp = 0; temp < seen_count; temp++) {
570 if (seen[temp].ptr != p.ptr)
571 continue;
572 if (p.qh->qh_next.ptr) {
573 temp = scnprintf(next, size,
574 " ...");
575 size -= temp;
576 next += temp;
577 }
578 break;
579 }
580
581 if (temp == seen_count) {
582 temp = output_buf_tds_dir(next,
583 fotg210, hw,
584 p.qh, size);
585
586 if (seen_count < DBG_SCHED_LIMIT)
587 seen[seen_count++].qh = p.qh;
588 } else
589 temp = 0;
590 tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
591 p = p.qh->qh_next;
592 break;
593 case Q_TYPE_FSTN:
594 temp = scnprintf(next, size,
595 " fstn-%8x/%p",
596 p.fstn->hw_prev, p.fstn);
597 tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
598 p = p.fstn->fstn_next;
599 break;
600 case Q_TYPE_ITD:
601 temp = scnprintf(next, size,
602 " itd/%p", p.itd);
603 tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
604 p = p.itd->itd_next;
605 break;
606 }
607 size -= temp;
608 next += temp;
609 } while (p.ptr);
610
611 temp = scnprintf(next, size, "\n");
612 size -= temp;
613 next += temp;
614 }
615 spin_unlock_irqrestore(&fotg210->lock, flags);
616 kfree(seen);
617
618 return buf->alloc_size - size;
619}
620#undef DBG_SCHED_LIMIT
621
622static const char *rh_state_string(struct fotg210_hcd *fotg210)
623{
624 switch (fotg210->rh_state) {
625 case FOTG210_RH_HALTED:
626 return "halted";
627 case FOTG210_RH_SUSPENDED:
628 return "suspended";
629 case FOTG210_RH_RUNNING:
630 return "running";
631 case FOTG210_RH_STOPPING:
632 return "stopping";
633 }
634 return "?";
635}
636
637static ssize_t fill_registers_buffer(struct debug_buffer *buf)
638{
639 struct usb_hcd *hcd;
640 struct fotg210_hcd *fotg210;
641 unsigned long flags;
642 unsigned temp, size, i;
643 char *next, scratch[80];
644 static const char fmt[] = "%*s\n";
645 static const char label[] = "";
646
647 hcd = bus_to_hcd(buf->bus);
648 fotg210 = hcd_to_fotg210(hcd);
649 next = buf->output_buf;
650 size = buf->alloc_size;
651
652 spin_lock_irqsave(&fotg210->lock, flags);
653
654 if (!HCD_HW_ACCESSIBLE(hcd)) {
655 size = scnprintf(next, size,
656 "bus %s, device %s\n"
657 "%s\n"
658 "SUSPENDED(no register access)\n",
659 hcd->self.controller->bus->name,
660 dev_name(hcd->self.controller),
661 hcd->product_desc);
662 goto done;
663 }
664
665
666 i = HC_VERSION(fotg210, fotg210_readl(fotg210,
667 &fotg210->caps->hc_capbase));
668 temp = scnprintf(next, size,
669 "bus %s, device %s\n"
670 "%s\n"
671 "EHCI %x.%02x, rh state %s\n",
672 hcd->self.controller->bus->name,
673 dev_name(hcd->self.controller),
674 hcd->product_desc,
675 i >> 8, i & 0x0ff, rh_state_string(fotg210));
676 size -= temp;
677 next += temp;
678
679
680 i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
681 temp = scnprintf(next, size, "structural params 0x%08x\n", i);
682 size -= temp;
683 next += temp;
684
685 i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
686 temp = scnprintf(next, size, "capability params 0x%08x\n", i);
687 size -= temp;
688 next += temp;
689
690
691 temp = dbg_status_buf(scratch, sizeof(scratch), label,
692 fotg210_readl(fotg210, &fotg210->regs->status));
693 temp = scnprintf(next, size, fmt, temp, scratch);
694 size -= temp;
695 next += temp;
696
697 temp = dbg_command_buf(scratch, sizeof(scratch), label,
698 fotg210_readl(fotg210, &fotg210->regs->command));
699 temp = scnprintf(next, size, fmt, temp, scratch);
700 size -= temp;
701 next += temp;
702
703 temp = dbg_intr_buf(scratch, sizeof(scratch), label,
704 fotg210_readl(fotg210, &fotg210->regs->intr_enable));
705 temp = scnprintf(next, size, fmt, temp, scratch);
706 size -= temp;
707 next += temp;
708
709 temp = scnprintf(next, size, "uframe %04x\n",
710 fotg210_read_frame_index(fotg210));
711 size -= temp;
712 next += temp;
713
714 if (fotg210->async_unlink) {
715 temp = scnprintf(next, size, "async unlink qh %p\n",
716 fotg210->async_unlink);
717 size -= temp;
718 next += temp;
719 }
720
721#ifdef FOTG210_STATS
722 temp = scnprintf(next, size,
723 "irq normal %ld err %ld iaa %ld(lost %ld)\n",
724 fotg210->stats.normal, fotg210->stats.error,
725 fotg210->stats.iaa, fotg210->stats.lost_iaa);
726 size -= temp;
727 next += temp;
728
729 temp = scnprintf(next, size, "complete %ld unlink %ld\n",
730 fotg210->stats.complete, fotg210->stats.unlink);
731 size -= temp;
732 next += temp;
733#endif
734
735done:
736 spin_unlock_irqrestore(&fotg210->lock, flags);
737
738 return buf->alloc_size - size;
739}
740
741static struct debug_buffer
742*alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
743{
744 struct debug_buffer *buf;
745
746 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
747
748 if (buf) {
749 buf->bus = bus;
750 buf->fill_func = fill_func;
751 mutex_init(&buf->mutex);
752 buf->alloc_size = PAGE_SIZE;
753 }
754
755 return buf;
756}
757
758static int fill_buffer(struct debug_buffer *buf)
759{
760 int ret = 0;
761
762 if (!buf->output_buf)
763 buf->output_buf = vmalloc(buf->alloc_size);
764
765 if (!buf->output_buf) {
766 ret = -ENOMEM;
767 goto out;
768 }
769
770 ret = buf->fill_func(buf);
771
772 if (ret >= 0) {
773 buf->count = ret;
774 ret = 0;
775 }
776
777out:
778 return ret;
779}
780
781static ssize_t debug_output(struct file *file, char __user *user_buf,
782 size_t len, loff_t *offset)
783{
784 struct debug_buffer *buf = file->private_data;
785 int ret = 0;
786
787 mutex_lock(&buf->mutex);
788 if (buf->count == 0) {
789 ret = fill_buffer(buf);
790 if (ret != 0) {
791 mutex_unlock(&buf->mutex);
792 goto out;
793 }
794 }
795 mutex_unlock(&buf->mutex);
796
797 ret = simple_read_from_buffer(user_buf, len, offset,
798 buf->output_buf, buf->count);
799
800out:
801 return ret;
802
803}
804
805static int debug_close(struct inode *inode, struct file *file)
806{
807 struct debug_buffer *buf = file->private_data;
808
809 if (buf) {
810 vfree(buf->output_buf);
811 kfree(buf);
812 }
813
814 return 0;
815}
816static int debug_async_open(struct inode *inode, struct file *file)
817{
818 file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
819
820 return file->private_data ? 0 : -ENOMEM;
821}
822
823static int debug_periodic_open(struct inode *inode, struct file *file)
824{
825 struct debug_buffer *buf;
826
827 buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
828 if (!buf)
829 return -ENOMEM;
830
831 buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
832 file->private_data = buf;
833 return 0;
834}
835
836static int debug_registers_open(struct inode *inode, struct file *file)
837{
838 file->private_data = alloc_buffer(inode->i_private,
839 fill_registers_buffer);
840
841 return file->private_data ? 0 : -ENOMEM;
842}
843
844static inline void create_debug_files(struct fotg210_hcd *fotg210)
845{
846 struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
847 struct dentry *root;
848
849 root = debugfs_create_dir(bus->bus_name, fotg210_debug_root);
850 fotg210->debug_dir = root;
851
852 debugfs_create_file("async", S_IRUGO, root, bus, &debug_async_fops);
853 debugfs_create_file("periodic", S_IRUGO, root, bus,
854 &debug_periodic_fops);
855 debugfs_create_file("registers", S_IRUGO, root, bus,
856 &debug_registers_fops);
857}
858
859static inline void remove_debug_files(struct fotg210_hcd *fotg210)
860{
861 debugfs_remove_recursive(fotg210->debug_dir);
862}
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
881 u32 mask, u32 done, int usec)
882{
883 u32 result;
884
885 do {
886 result = fotg210_readl(fotg210, ptr);
887 if (result == ~(u32)0)
888 return -ENODEV;
889 result &= mask;
890 if (result == done)
891 return 0;
892 udelay(1);
893 usec--;
894 } while (usec > 0);
895 return -ETIMEDOUT;
896}
897
898
899
900
901static int fotg210_halt(struct fotg210_hcd *fotg210)
902{
903 u32 temp;
904
905 spin_lock_irq(&fotg210->lock);
906
907
908 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
909
910
911
912
913
914 fotg210->command &= ~CMD_RUN;
915 temp = fotg210_readl(fotg210, &fotg210->regs->command);
916 temp &= ~(CMD_RUN | CMD_IAAD);
917 fotg210_writel(fotg210, temp, &fotg210->regs->command);
918
919 spin_unlock_irq(&fotg210->lock);
920 synchronize_irq(fotg210_to_hcd(fotg210)->irq);
921
922 return handshake(fotg210, &fotg210->regs->status,
923 STS_HALT, STS_HALT, 16 * 125);
924}
925
926
927
928
929static int fotg210_reset(struct fotg210_hcd *fotg210)
930{
931 int retval;
932 u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
933
934
935
936
937 if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
938 fotg210->debug = NULL;
939
940 command |= CMD_RESET;
941 dbg_cmd(fotg210, "reset", command);
942 fotg210_writel(fotg210, command, &fotg210->regs->command);
943 fotg210->rh_state = FOTG210_RH_HALTED;
944 fotg210->next_statechange = jiffies;
945 retval = handshake(fotg210, &fotg210->regs->command,
946 CMD_RESET, 0, 250 * 1000);
947
948 if (retval)
949 return retval;
950
951 if (fotg210->debug)
952 dbgp_external_startup(fotg210_to_hcd(fotg210));
953
954 fotg210->port_c_suspend = fotg210->suspended_ports =
955 fotg210->resuming_ports = 0;
956 return retval;
957}
958
959
960
961
962static void fotg210_quiesce(struct fotg210_hcd *fotg210)
963{
964 u32 temp;
965
966 if (fotg210->rh_state != FOTG210_RH_RUNNING)
967 return;
968
969
970 temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
971 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
972 16 * 125);
973
974
975 spin_lock_irq(&fotg210->lock);
976 fotg210->command &= ~(CMD_ASE | CMD_PSE);
977 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
978 spin_unlock_irq(&fotg210->lock);
979
980
981 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
982 16 * 125);
983}
984
985static void end_unlink_async(struct fotg210_hcd *fotg210);
986static void unlink_empty_async(struct fotg210_hcd *fotg210);
987static void fotg210_work(struct fotg210_hcd *fotg210);
988static void start_unlink_intr(struct fotg210_hcd *fotg210,
989 struct fotg210_qh *qh);
990static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
991
992
993static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
994{
995 fotg210->command |= bit;
996 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
997
998
999 fotg210_readl(fotg210, &fotg210->regs->command);
1000}
1001
1002
1003static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
1004{
1005 fotg210->command &= ~bit;
1006 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1007
1008
1009 fotg210_readl(fotg210, &fotg210->regs->command);
1010}
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038static unsigned event_delays_ns[] = {
1039 1 * NSEC_PER_MSEC,
1040 1 * NSEC_PER_MSEC,
1041 1 * NSEC_PER_MSEC,
1042 1125 * NSEC_PER_USEC,
1043 2 * NSEC_PER_MSEC,
1044 6 * NSEC_PER_MSEC,
1045 10 * NSEC_PER_MSEC,
1046 10 * NSEC_PER_MSEC,
1047 15 * NSEC_PER_MSEC,
1048 100 * NSEC_PER_MSEC,
1049};
1050
1051
1052static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
1053 bool resched)
1054{
1055 ktime_t *timeout = &fotg210->hr_timeouts[event];
1056
1057 if (resched)
1058 *timeout = ktime_add(ktime_get(), event_delays_ns[event]);
1059 fotg210->enabled_hrtimer_events |= (1 << event);
1060
1061
1062 if (event < fotg210->next_hrtimer_event) {
1063 fotg210->next_hrtimer_event = event;
1064 hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
1065 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
1066 }
1067}
1068
1069
1070
1071static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
1072{
1073 unsigned actual, want;
1074
1075
1076 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1077 return;
1078
1079 want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
1080 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;
1081
1082 if (want != actual) {
1083
1084
1085 if (fotg210->ASS_poll_count++ < 20) {
1086 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
1087 true);
1088 return;
1089 }
1090 fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
1091 want, actual);
1092 }
1093 fotg210->ASS_poll_count = 0;
1094
1095
1096 if (want == 0) {
1097 if (fotg210->async_count > 0)
1098 fotg210_set_command_bit(fotg210, CMD_ASE);
1099
1100 } else {
1101 if (fotg210->async_count == 0) {
1102
1103
1104 fotg210_enable_event(fotg210,
1105 FOTG210_HRTIMER_DISABLE_ASYNC,
1106 true);
1107 }
1108 }
1109}
1110
1111
1112static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
1113{
1114 fotg210_clear_command_bit(fotg210, CMD_ASE);
1115}
1116
1117
1118
1119static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
1120{
1121 unsigned actual, want;
1122
1123
1124 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1125 return;
1126
1127 want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
1128 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;
1129
1130 if (want != actual) {
1131
1132
1133 if (fotg210->PSS_poll_count++ < 20) {
1134 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
1135 true);
1136 return;
1137 }
1138 fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
1139 want, actual);
1140 }
1141 fotg210->PSS_poll_count = 0;
1142
1143
1144 if (want == 0) {
1145 if (fotg210->periodic_count > 0)
1146 fotg210_set_command_bit(fotg210, CMD_PSE);
1147
1148 } else {
1149 if (fotg210->periodic_count == 0) {
1150
1151
1152 fotg210_enable_event(fotg210,
1153 FOTG210_HRTIMER_DISABLE_PERIODIC,
1154 true);
1155 }
1156 }
1157}
1158
1159
1160static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
1161{
1162 fotg210_clear_command_bit(fotg210, CMD_PSE);
1163}
1164
1165
1166
1167static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
1168{
1169 if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
1170
1171
1172 if (fotg210->died_poll_count++ < 5) {
1173
1174 fotg210_enable_event(fotg210,
1175 FOTG210_HRTIMER_POLL_DEAD, true);
1176 return;
1177 }
1178 fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
1179 }
1180
1181
1182 fotg210->rh_state = FOTG210_RH_HALTED;
1183 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
1184 fotg210_work(fotg210);
1185 end_unlink_async(fotg210);
1186
1187
1188}
1189
1190
1191
1192static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
1193{
1194 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
1195
1196
1197
1198
1199
1200
1201
1202
1203 fotg210->intr_unlinking = true;
1204 while (fotg210->intr_unlink) {
1205 struct fotg210_qh *qh = fotg210->intr_unlink;
1206
1207 if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
1208 break;
1209 fotg210->intr_unlink = qh->unlink_next;
1210 qh->unlink_next = NULL;
1211 end_unlink_intr(fotg210, qh);
1212 }
1213
1214
1215 if (fotg210->intr_unlink) {
1216 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
1217 true);
1218 ++fotg210->intr_unlink_cycle;
1219 }
1220 fotg210->intr_unlinking = false;
1221}
1222
1223
1224
1225static void start_free_itds(struct fotg210_hcd *fotg210)
1226{
1227 if (!(fotg210->enabled_hrtimer_events &
1228 BIT(FOTG210_HRTIMER_FREE_ITDS))) {
1229 fotg210->last_itd_to_free = list_entry(
1230 fotg210->cached_itd_list.prev,
1231 struct fotg210_itd, itd_list);
1232 fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
1233 }
1234}
1235
1236
1237static void end_free_itds(struct fotg210_hcd *fotg210)
1238{
1239 struct fotg210_itd *itd, *n;
1240
1241 if (fotg210->rh_state < FOTG210_RH_RUNNING)
1242 fotg210->last_itd_to_free = NULL;
1243
1244 list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
1245 list_del(&itd->itd_list);
1246 dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
1247 if (itd == fotg210->last_itd_to_free)
1248 break;
1249 }
1250
1251 if (!list_empty(&fotg210->cached_itd_list))
1252 start_free_itds(fotg210);
1253}
1254
1255
1256
1257static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
1258{
1259 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1260 return;
1261
1262
1263
1264
1265
1266
1267
1268 if (fotg210->async_iaa) {
1269 u32 cmd, status;
1270
1271
1272
1273
1274
1275
1276
1277 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
1278
1279
1280
1281
1282
1283
1284
1285
1286 status = fotg210_readl(fotg210, &fotg210->regs->status);
1287 if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
1288 COUNT(fotg210->stats.lost_iaa);
1289 fotg210_writel(fotg210, STS_IAA,
1290 &fotg210->regs->status);
1291 }
1292
1293 fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
1294 status, cmd);
1295 end_unlink_async(fotg210);
1296 }
1297}
1298
1299
1300
1301static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
1302{
1303
1304 if (fotg210->rh_state != FOTG210_RH_RUNNING ||
1305 (fotg210->enabled_hrtimer_events &
1306 BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
1307 return;
1308
1309
1310
1311
1312
1313 if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog &&
1314 fotg210->async_count + fotg210->intr_count > 0))
1315 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG,
1316 true);
1317}
1318
1319
1320
1321
1322
1323
1324static void (*event_handlers[])(struct fotg210_hcd *) = {
1325 fotg210_poll_ASS,
1326 fotg210_poll_PSS,
1327 fotg210_handle_controller_death,
1328 fotg210_handle_intr_unlinks,
1329 end_free_itds,
1330 unlink_empty_async,
1331 fotg210_iaa_watchdog,
1332 fotg210_disable_PSE,
1333 fotg210_disable_ASE,
1334 fotg210_work,
1335};
1336
1337static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
1338{
1339 struct fotg210_hcd *fotg210 =
1340 container_of(t, struct fotg210_hcd, hrtimer);
1341 ktime_t now;
1342 unsigned long events;
1343 unsigned long flags;
1344 unsigned e;
1345
1346 spin_lock_irqsave(&fotg210->lock, flags);
1347
1348 events = fotg210->enabled_hrtimer_events;
1349 fotg210->enabled_hrtimer_events = 0;
1350 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
1351
1352
1353
1354
1355
1356 now = ktime_get();
1357 for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) {
1358 if (ktime_compare(now, fotg210->hr_timeouts[e]) >= 0)
1359 event_handlers[e](fotg210);
1360 else
1361 fotg210_enable_event(fotg210, e, false);
1362 }
1363
1364 spin_unlock_irqrestore(&fotg210->lock, flags);
1365 return HRTIMER_NORESTART;
1366}
1367
1368#define fotg210_bus_suspend NULL
1369#define fotg210_bus_resume NULL
1370
1371static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
1372 u32 __iomem *status_reg, int port_status)
1373{
1374 if (!(port_status & PORT_CONNECT))
1375 return port_status;
1376
1377
1378 if (!(port_status & PORT_PE))
1379
1380 fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
1381 index + 1);
1382 else
1383 fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
1384 index + 1);
1385
1386 return port_status;
1387}
1388
1389
1390
1391
1392static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
1393{
1394 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
1395 u32 temp, status;
1396 u32 mask;
1397 int retval = 1;
1398 unsigned long flags;
1399
1400
1401 buf[0] = 0;
1402
1403
1404
1405
1406 status = fotg210->resuming_ports;
1407
1408 mask = PORT_CSC | PORT_PEC;
1409
1410
1411
1412
1413
1414 spin_lock_irqsave(&fotg210->lock, flags);
1415
1416 temp = fotg210_readl(fotg210, &fotg210->regs->port_status);
1417
1418
1419
1420
1421
1422
1423
1424
1425 if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend) ||
1426 (fotg210->reset_done[0] &&
1427 time_after_eq(jiffies, fotg210->reset_done[0]))) {
1428 buf[0] |= 1 << 1;
1429 status = STS_PCD;
1430 }
1431
1432 spin_unlock_irqrestore(&fotg210->lock, flags);
1433 return status ? retval : 0;
1434}
1435
1436static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210,
1437 struct usb_hub_descriptor *desc)
1438{
1439 int ports = HCS_N_PORTS(fotg210->hcs_params);
1440 u16 temp;
1441
1442 desc->bDescriptorType = USB_DT_HUB;
1443 desc->bPwrOn2PwrGood = 10;
1444 desc->bHubContrCurrent = 0;
1445
1446 desc->bNbrPorts = ports;
1447 temp = 1 + (ports / 8);
1448 desc->bDescLength = 7 + 2 * temp;
1449
1450
1451 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1452 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
1453
1454 temp = HUB_CHAR_INDV_PORT_OCPM;
1455 temp |= HUB_CHAR_NO_LPSM;
1456 desc->wHubCharacteristics = cpu_to_le16(temp);
1457}
1458
1459static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1460 u16 wIndex, char *buf, u16 wLength)
1461{
1462 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
1463 int ports = HCS_N_PORTS(fotg210->hcs_params);
1464 u32 __iomem *status_reg = &fotg210->regs->port_status;
1465 u32 temp, temp1, status;
1466 unsigned long flags;
1467 int retval = 0;
1468 unsigned selector;
1469
1470
1471
1472
1473
1474
1475
1476
1477 spin_lock_irqsave(&fotg210->lock, flags);
1478 switch (typeReq) {
1479 case ClearHubFeature:
1480 switch (wValue) {
1481 case C_HUB_LOCAL_POWER:
1482 case C_HUB_OVER_CURRENT:
1483
1484 break;
1485 default:
1486 goto error;
1487 }
1488 break;
1489 case ClearPortFeature:
1490 if (!wIndex || wIndex > ports)
1491 goto error;
1492 wIndex--;
1493 temp = fotg210_readl(fotg210, status_reg);
1494 temp &= ~PORT_RWC_BITS;
1495
1496
1497
1498
1499
1500
1501
1502
1503 switch (wValue) {
1504 case USB_PORT_FEAT_ENABLE:
1505 fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
1506 break;
1507 case USB_PORT_FEAT_C_ENABLE:
1508 fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
1509 break;
1510 case USB_PORT_FEAT_SUSPEND:
1511 if (temp & PORT_RESET)
1512 goto error;
1513 if (!(temp & PORT_SUSPEND))
1514 break;
1515 if ((temp & PORT_PE) == 0)
1516 goto error;
1517
1518
1519 fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
1520 fotg210->reset_done[wIndex] = jiffies
1521 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
1522 break;
1523 case USB_PORT_FEAT_C_SUSPEND:
1524 clear_bit(wIndex, &fotg210->port_c_suspend);
1525 break;
1526 case USB_PORT_FEAT_C_CONNECTION:
1527 fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
1528 break;
1529 case USB_PORT_FEAT_C_OVER_CURRENT:
1530 fotg210_writel(fotg210, temp | OTGISR_OVC,
1531 &fotg210->regs->otgisr);
1532 break;
1533 case USB_PORT_FEAT_C_RESET:
1534
1535 break;
1536 default:
1537 goto error;
1538 }
1539 fotg210_readl(fotg210, &fotg210->regs->command);
1540 break;
1541 case GetHubDescriptor:
1542 fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
1543 buf);
1544 break;
1545 case GetHubStatus:
1546
1547 memset(buf, 0, 4);
1548
1549 break;
1550 case GetPortStatus:
1551 if (!wIndex || wIndex > ports)
1552 goto error;
1553 wIndex--;
1554 status = 0;
1555 temp = fotg210_readl(fotg210, status_reg);
1556
1557
1558 if (temp & PORT_CSC)
1559 status |= USB_PORT_STAT_C_CONNECTION << 16;
1560 if (temp & PORT_PEC)
1561 status |= USB_PORT_STAT_C_ENABLE << 16;
1562
1563 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
1564 if (temp1 & OTGISR_OVC)
1565 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1566
1567
1568 if (temp & PORT_RESUME) {
1569
1570
1571 if (!fotg210->reset_done[wIndex]) {
1572
1573 fotg210->reset_done[wIndex] = jiffies
1574 + msecs_to_jiffies(20);
1575
1576 mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
1577 fotg210->reset_done[wIndex]);
1578 }
1579
1580
1581 else if (time_after_eq(jiffies,
1582 fotg210->reset_done[wIndex])) {
1583 clear_bit(wIndex, &fotg210->suspended_ports);
1584 set_bit(wIndex, &fotg210->port_c_suspend);
1585 fotg210->reset_done[wIndex] = 0;
1586
1587
1588 temp = fotg210_readl(fotg210, status_reg);
1589 fotg210_writel(fotg210, temp &
1590 ~(PORT_RWC_BITS | PORT_RESUME),
1591 status_reg);
1592 clear_bit(wIndex, &fotg210->resuming_ports);
1593 retval = handshake(fotg210, status_reg,
1594 PORT_RESUME, 0, 2000);
1595 if (retval != 0) {
1596 fotg210_err(fotg210,
1597 "port %d resume error %d\n",
1598 wIndex + 1, retval);
1599 goto error;
1600 }
1601 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1602 }
1603 }
1604
1605
1606 if ((temp & PORT_RESET) && time_after_eq(jiffies,
1607 fotg210->reset_done[wIndex])) {
1608 status |= USB_PORT_STAT_C_RESET << 16;
1609 fotg210->reset_done[wIndex] = 0;
1610 clear_bit(wIndex, &fotg210->resuming_ports);
1611
1612
1613 fotg210_writel(fotg210,
1614 temp & ~(PORT_RWC_BITS | PORT_RESET),
1615 status_reg);
1616
1617
1618
1619 retval = handshake(fotg210, status_reg,
1620 PORT_RESET, 0, 1000);
1621 if (retval != 0) {
1622 fotg210_err(fotg210, "port %d reset error %d\n",
1623 wIndex + 1, retval);
1624 goto error;
1625 }
1626
1627
1628 temp = check_reset_complete(fotg210, wIndex, status_reg,
1629 fotg210_readl(fotg210, status_reg));
1630 }
1631
1632 if (!(temp & (PORT_RESUME|PORT_RESET))) {
1633 fotg210->reset_done[wIndex] = 0;
1634 clear_bit(wIndex, &fotg210->resuming_ports);
1635 }
1636
1637
1638 if ((temp & PORT_CONNECT) &&
1639 test_bit(wIndex, &fotg210->companion_ports)) {
1640 temp &= ~PORT_RWC_BITS;
1641 fotg210_writel(fotg210, temp, status_reg);
1642 fotg210_dbg(fotg210, "port %d --> companion\n",
1643 wIndex + 1);
1644 temp = fotg210_readl(fotg210, status_reg);
1645 }
1646
1647
1648
1649
1650
1651
1652
1653 if (temp & PORT_CONNECT) {
1654 status |= USB_PORT_STAT_CONNECTION;
1655 status |= fotg210_port_speed(fotg210, temp);
1656 }
1657 if (temp & PORT_PE)
1658 status |= USB_PORT_STAT_ENABLE;
1659
1660
1661 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1662 status |= USB_PORT_STAT_SUSPEND;
1663 } else if (test_bit(wIndex, &fotg210->suspended_ports)) {
1664 clear_bit(wIndex, &fotg210->suspended_ports);
1665 clear_bit(wIndex, &fotg210->resuming_ports);
1666 fotg210->reset_done[wIndex] = 0;
1667 if (temp & PORT_PE)
1668 set_bit(wIndex, &fotg210->port_c_suspend);
1669 }
1670
1671 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
1672 if (temp1 & OTGISR_OVC)
1673 status |= USB_PORT_STAT_OVERCURRENT;
1674 if (temp & PORT_RESET)
1675 status |= USB_PORT_STAT_RESET;
1676 if (test_bit(wIndex, &fotg210->port_c_suspend))
1677 status |= USB_PORT_STAT_C_SUSPEND << 16;
1678
1679 if (status & ~0xffff)
1680 dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
1681 put_unaligned_le32(status, buf);
1682 break;
1683 case SetHubFeature:
1684 switch (wValue) {
1685 case C_HUB_LOCAL_POWER:
1686 case C_HUB_OVER_CURRENT:
1687
1688 break;
1689 default:
1690 goto error;
1691 }
1692 break;
1693 case SetPortFeature:
1694 selector = wIndex >> 8;
1695 wIndex &= 0xff;
1696
1697 if (!wIndex || wIndex > ports)
1698 goto error;
1699 wIndex--;
1700 temp = fotg210_readl(fotg210, status_reg);
1701 temp &= ~PORT_RWC_BITS;
1702 switch (wValue) {
1703 case USB_PORT_FEAT_SUSPEND:
1704 if ((temp & PORT_PE) == 0
1705 || (temp & PORT_RESET) != 0)
1706 goto error;
1707
1708
1709
1710
1711
1712 fotg210_writel(fotg210, temp | PORT_SUSPEND,
1713 status_reg);
1714 set_bit(wIndex, &fotg210->suspended_ports);
1715 break;
1716 case USB_PORT_FEAT_RESET:
1717 if (temp & PORT_RESUME)
1718 goto error;
1719
1720
1721
1722
1723 fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
1724 temp |= PORT_RESET;
1725 temp &= ~PORT_PE;
1726
1727
1728
1729
1730
1731 fotg210->reset_done[wIndex] = jiffies
1732 + msecs_to_jiffies(50);
1733 fotg210_writel(fotg210, temp, status_reg);
1734 break;
1735
1736
1737
1738
1739
1740
1741
1742 case USB_PORT_FEAT_TEST:
1743 if (!selector || selector > 5)
1744 goto error;
1745 spin_unlock_irqrestore(&fotg210->lock, flags);
1746 fotg210_quiesce(fotg210);
1747 spin_lock_irqsave(&fotg210->lock, flags);
1748
1749
1750 temp = fotg210_readl(fotg210, status_reg) &
1751 ~PORT_RWC_BITS;
1752 if (temp & PORT_PE)
1753 fotg210_writel(fotg210, temp | PORT_SUSPEND,
1754 status_reg);
1755
1756 spin_unlock_irqrestore(&fotg210->lock, flags);
1757 fotg210_halt(fotg210);
1758 spin_lock_irqsave(&fotg210->lock, flags);
1759
1760 temp = fotg210_readl(fotg210, status_reg);
1761 temp |= selector << 16;
1762 fotg210_writel(fotg210, temp, status_reg);
1763 break;
1764
1765 default:
1766 goto error;
1767 }
1768 fotg210_readl(fotg210, &fotg210->regs->command);
1769 break;
1770
1771 default:
1772error:
1773
1774 retval = -EPIPE;
1775 }
1776 spin_unlock_irqrestore(&fotg210->lock, flags);
1777 return retval;
1778}
1779
1780static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
1781 int portnum)
1782{
1783 return;
1784}
1785
1786static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
1787 int portnum)
1788{
1789 return 0;
1790}
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
1804 struct fotg210_qtd *qtd, dma_addr_t dma)
1805{
1806 memset(qtd, 0, sizeof(*qtd));
1807 qtd->qtd_dma = dma;
1808 qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
1809 qtd->hw_next = FOTG210_LIST_END(fotg210);
1810 qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
1811 INIT_LIST_HEAD(&qtd->qtd_list);
1812}
1813
1814static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
1815 gfp_t flags)
1816{
1817 struct fotg210_qtd *qtd;
1818 dma_addr_t dma;
1819
1820 qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
1821 if (qtd != NULL)
1822 fotg210_qtd_init(fotg210, qtd, dma);
1823
1824 return qtd;
1825}
1826
1827static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
1828 struct fotg210_qtd *qtd)
1829{
1830 dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
1831}
1832
1833
1834static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
1835{
1836
1837 if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
1838 fotg210_dbg(fotg210, "unused qh not empty!\n");
1839 BUG();
1840 }
1841 if (qh->dummy)
1842 fotg210_qtd_free(fotg210, qh->dummy);
1843 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1844 kfree(qh);
1845}
1846
1847static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
1848 gfp_t flags)
1849{
1850 struct fotg210_qh *qh;
1851 dma_addr_t dma;
1852
1853 qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
1854 if (!qh)
1855 goto done;
1856 qh->hw = dma_pool_zalloc(fotg210->qh_pool, flags, &dma);
1857 if (!qh->hw)
1858 goto fail;
1859 qh->qh_dma = dma;
1860 INIT_LIST_HEAD(&qh->qtd_list);
1861
1862
1863 qh->dummy = fotg210_qtd_alloc(fotg210, flags);
1864 if (qh->dummy == NULL) {
1865 fotg210_dbg(fotg210, "no dummy td\n");
1866 goto fail1;
1867 }
1868done:
1869 return qh;
1870fail1:
1871 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1872fail:
1873 kfree(qh);
1874 return NULL;
1875}
1876
1877
1878
1879
1880
1881
1882static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
1883{
1884 if (fotg210->async)
1885 qh_destroy(fotg210, fotg210->async);
1886 fotg210->async = NULL;
1887
1888 if (fotg210->dummy)
1889 qh_destroy(fotg210, fotg210->dummy);
1890 fotg210->dummy = NULL;
1891
1892
1893 dma_pool_destroy(fotg210->qtd_pool);
1894 fotg210->qtd_pool = NULL;
1895
1896 dma_pool_destroy(fotg210->qh_pool);
1897 fotg210->qh_pool = NULL;
1898
1899 dma_pool_destroy(fotg210->itd_pool);
1900 fotg210->itd_pool = NULL;
1901
1902 if (fotg210->periodic)
1903 dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller,
1904 fotg210->periodic_size * sizeof(u32),
1905 fotg210->periodic, fotg210->periodic_dma);
1906 fotg210->periodic = NULL;
1907
1908
1909 kfree(fotg210->pshadow);
1910 fotg210->pshadow = NULL;
1911}
1912
1913
1914static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
1915{
1916 int i;
1917
1918
1919 fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
1920 fotg210_to_hcd(fotg210)->self.controller,
1921 sizeof(struct fotg210_qtd),
1922 32 ,
1923 4096 );
1924 if (!fotg210->qtd_pool)
1925 goto fail;
1926
1927
1928 fotg210->qh_pool = dma_pool_create("fotg210_qh",
1929 fotg210_to_hcd(fotg210)->self.controller,
1930 sizeof(struct fotg210_qh_hw),
1931 32 ,
1932 4096 );
1933 if (!fotg210->qh_pool)
1934 goto fail;
1935
1936 fotg210->async = fotg210_qh_alloc(fotg210, flags);
1937 if (!fotg210->async)
1938 goto fail;
1939
1940
1941 fotg210->itd_pool = dma_pool_create("fotg210_itd",
1942 fotg210_to_hcd(fotg210)->self.controller,
1943 sizeof(struct fotg210_itd),
1944 64 ,
1945 4096 );
1946 if (!fotg210->itd_pool)
1947 goto fail;
1948
1949
1950 fotg210->periodic = (__le32 *)
1951 dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller,
1952 fotg210->periodic_size * sizeof(__le32),
1953 &fotg210->periodic_dma, 0);
1954 if (fotg210->periodic == NULL)
1955 goto fail;
1956
1957 for (i = 0; i < fotg210->periodic_size; i++)
1958 fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
1959
1960
1961 fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
1962 flags);
1963 if (fotg210->pshadow != NULL)
1964 return 0;
1965
1966fail:
1967 fotg210_dbg(fotg210, "couldn't init memory\n");
1968 fotg210_mem_cleanup(fotg210);
1969 return -ENOMEM;
1970}
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd,
1990 dma_addr_t buf, size_t len, int token, int maxpacket)
1991{
1992 int i, count;
1993 u64 addr = buf;
1994
1995
1996 qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr);
1997 qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32));
1998 count = 0x1000 - (buf & 0x0fff);
1999 if (likely(len < count))
2000 count = len;
2001 else {
2002 buf += 0x1000;
2003 buf &= ~0x0fff;
2004
2005
2006 for (i = 1; count < len && i < 5; i++) {
2007 addr = buf;
2008 qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
2009 qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
2010 (u32)(addr >> 32));
2011 buf += 0x1000;
2012 if ((count + 0x1000) < len)
2013 count += 0x1000;
2014 else
2015 count = len;
2016 }
2017
2018
2019 if (count != len)
2020 count -= (count % maxpacket);
2021 }
2022 qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
2023 qtd->length = count;
2024
2025 return count;
2026}
2027
2028static inline void qh_update(struct fotg210_hcd *fotg210,
2029 struct fotg210_qh *qh, struct fotg210_qtd *qtd)
2030{
2031 struct fotg210_qh_hw *hw = qh->hw;
2032
2033
2034 BUG_ON(qh->qh_state != QH_STATE_IDLE);
2035
2036 hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2037 hw->hw_alt_next = FOTG210_LIST_END(fotg210);
2038
2039
2040
2041
2042
2043
2044 if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
2045 unsigned is_out, epnum;
2046
2047 is_out = qh->is_out;
2048 epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f;
2049 if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
2050 hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE);
2051 usb_settoggle(qh->dev, epnum, is_out, 1);
2052 }
2053 }
2054
2055 hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
2056}
2057
2058
2059
2060
2061
2062static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
2063{
2064 struct fotg210_qtd *qtd;
2065
2066 if (list_empty(&qh->qtd_list))
2067 qtd = qh->dummy;
2068 else {
2069 qtd = list_entry(qh->qtd_list.next,
2070 struct fotg210_qtd, qtd_list);
2071
2072
2073
2074
2075
2076
2077
2078 if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
2079 qh->hw->hw_qtd_next = qtd->hw_next;
2080 qtd = NULL;
2081 }
2082 }
2083
2084 if (qtd)
2085 qh_update(fotg210, qh, qtd);
2086}
2087
2088static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2089
2090static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
2091 struct usb_host_endpoint *ep)
2092{
2093 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
2094 struct fotg210_qh *qh = ep->hcpriv;
2095 unsigned long flags;
2096
2097 spin_lock_irqsave(&fotg210->lock, flags);
2098 qh->clearing_tt = 0;
2099 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
2100 && fotg210->rh_state == FOTG210_RH_RUNNING)
2101 qh_link_async(fotg210, qh);
2102 spin_unlock_irqrestore(&fotg210->lock, flags);
2103}
2104
2105static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
2106 struct fotg210_qh *qh, struct urb *urb, u32 token)
2107{
2108
2109
2110
2111
2112
2113
2114
2115 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
2116 struct usb_device *tt = urb->dev->tt->hub;
2117
2118 dev_dbg(&tt->dev,
2119 "clear tt buffer port %d, a%d ep%d t%08x\n",
2120 urb->dev->ttport, urb->dev->devnum,
2121 usb_pipeendpoint(urb->pipe), token);
2122
2123 if (urb->dev->tt->hub !=
2124 fotg210_to_hcd(fotg210)->self.root_hub) {
2125 if (usb_hub_clear_tt_buffer(urb) == 0)
2126 qh->clearing_tt = 1;
2127 }
2128 }
2129}
2130
2131static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb,
2132 size_t length, u32 token)
2133{
2134 int status = -EINPROGRESS;
2135
2136
2137 if (likely(QTD_PID(token) != 2))
2138 urb->actual_length += length - QTD_LENGTH(token);
2139
2140
2141 if (unlikely(urb->unlinked))
2142 return status;
2143
2144
2145 if (unlikely(IS_SHORT_READ(token)))
2146 status = -EREMOTEIO;
2147
2148
2149 if (token & QTD_STS_HALT) {
2150 if (token & QTD_STS_BABBLE) {
2151
2152 status = -EOVERFLOW;
2153
2154 } else if (QTD_CERR(token)) {
2155 status = -EPIPE;
2156
2157
2158
2159
2160
2161 } else if (token & QTD_STS_MMF) {
2162
2163 status = -EPROTO;
2164 } else if (token & QTD_STS_DBE) {
2165 status = (QTD_PID(token) == 1)
2166 ? -ENOSR
2167 : -ECOMM;
2168 } else if (token & QTD_STS_XACT) {
2169
2170 fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
2171 urb->dev->devpath,
2172 usb_pipeendpoint(urb->pipe),
2173 usb_pipein(urb->pipe) ? "in" : "out");
2174 status = -EPROTO;
2175 } else {
2176 status = -EPROTO;
2177 }
2178
2179 fotg210_dbg(fotg210,
2180 "dev%d ep%d%s qtd token %08x --> status %d\n",
2181 usb_pipedevice(urb->pipe),
2182 usb_pipeendpoint(urb->pipe),
2183 usb_pipein(urb->pipe) ? "in" : "out",
2184 token, status);
2185 }
2186
2187 return status;
2188}
2189
2190static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb,
2191 int status)
2192__releases(fotg210->lock)
2193__acquires(fotg210->lock)
2194{
2195 if (likely(urb->hcpriv != NULL)) {
2196 struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
2197
2198
2199 if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
2200
2201
2202 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
2203 }
2204 }
2205
2206 if (unlikely(urb->unlinked)) {
2207 COUNT(fotg210->stats.unlink);
2208 } else {
2209
2210 if (status == -EINPROGRESS || status == -EREMOTEIO)
2211 status = 0;
2212 COUNT(fotg210->stats.complete);
2213 }
2214
2215#ifdef FOTG210_URB_TRACE
2216 fotg210_dbg(fotg210,
2217 "%s %s urb %p ep%d%s status %d len %d/%d\n",
2218 __func__, urb->dev->devpath, urb,
2219 usb_pipeendpoint(urb->pipe),
2220 usb_pipein(urb->pipe) ? "in" : "out",
2221 status,
2222 urb->actual_length, urb->transfer_buffer_length);
2223#endif
2224
2225
2226 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
2227 spin_unlock(&fotg210->lock);
2228 usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status);
2229 spin_lock(&fotg210->lock);
2230}
2231
2232static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2233
2234
2235
2236
2237
2238static unsigned qh_completions(struct fotg210_hcd *fotg210,
2239 struct fotg210_qh *qh)
2240{
2241 struct fotg210_qtd *last, *end = qh->dummy;
2242 struct fotg210_qtd *qtd, *tmp;
2243 int last_status;
2244 int stopped;
2245 unsigned count = 0;
2246 u8 state;
2247 struct fotg210_qh_hw *hw = qh->hw;
2248
2249 if (unlikely(list_empty(&qh->qtd_list)))
2250 return count;
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262 state = qh->qh_state;
2263 qh->qh_state = QH_STATE_COMPLETING;
2264 stopped = (state == QH_STATE_IDLE);
2265
2266rescan:
2267 last = NULL;
2268 last_status = -EINPROGRESS;
2269 qh->needs_rescan = 0;
2270
2271
2272
2273
2274
2275
2276 list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
2277 struct urb *urb;
2278 u32 token = 0;
2279
2280 urb = qtd->urb;
2281
2282
2283 if (last) {
2284 if (likely(last->urb != urb)) {
2285 fotg210_urb_done(fotg210, last->urb,
2286 last_status);
2287 count++;
2288 last_status = -EINPROGRESS;
2289 }
2290 fotg210_qtd_free(fotg210, last);
2291 last = NULL;
2292 }
2293
2294
2295 if (qtd == end)
2296 break;
2297
2298
2299 rmb();
2300 token = hc32_to_cpu(fotg210, qtd->hw_token);
2301
2302
2303retry_xacterr:
2304 if ((token & QTD_STS_ACTIVE) == 0) {
2305
2306
2307 if (token & QTD_STS_DBE)
2308 fotg210_dbg(fotg210,
2309 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
2310 urb, usb_endpoint_num(&urb->ep->desc),
2311 usb_endpoint_dir_in(&urb->ep->desc)
2312 ? "in" : "out",
2313 urb->transfer_buffer_length, qtd, qh);
2314
2315
2316
2317
2318 if ((token & QTD_STS_HALT) != 0) {
2319
2320
2321
2322
2323 if ((token & QTD_STS_XACT) &&
2324 QTD_CERR(token) == 0 &&
2325 ++qh->xacterrs < QH_XACTERR_MAX &&
2326 !urb->unlinked) {
2327 fotg210_dbg(fotg210,
2328 "detected XactErr len %zu/%zu retry %d\n",
2329 qtd->length - QTD_LENGTH(token),
2330 qtd->length,
2331 qh->xacterrs);
2332
2333
2334
2335
2336
2337
2338 token &= ~QTD_STS_HALT;
2339 token |= QTD_STS_ACTIVE |
2340 (FOTG210_TUNE_CERR << 10);
2341 qtd->hw_token = cpu_to_hc32(fotg210,
2342 token);
2343 wmb();
2344 hw->hw_token = cpu_to_hc32(fotg210,
2345 token);
2346 goto retry_xacterr;
2347 }
2348 stopped = 1;
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359 } else if (IS_SHORT_READ(token) &&
2360 !(qtd->hw_alt_next &
2361 FOTG210_LIST_END(fotg210))) {
2362 stopped = 1;
2363 }
2364
2365
2366 } else if (likely(!stopped
2367 && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
2368 break;
2369
2370
2371 } else {
2372 stopped = 1;
2373
2374
2375 if (fotg210->rh_state < FOTG210_RH_RUNNING)
2376 last_status = -ESHUTDOWN;
2377
2378
2379
2380
2381 else if (last_status == -EINPROGRESS && !urb->unlinked)
2382 continue;
2383
2384
2385 if (state == QH_STATE_IDLE &&
2386 cpu_to_hc32(fotg210, qtd->qtd_dma)
2387 == hw->hw_current) {
2388 token = hc32_to_cpu(fotg210, hw->hw_token);
2389
2390
2391
2392
2393
2394 fotg210_clear_tt_buffer(fotg210, qh, urb,
2395 token);
2396 }
2397 }
2398
2399
2400
2401
2402
2403
2404
2405
2406 if (last_status == -EINPROGRESS) {
2407 last_status = qtd_copy_status(fotg210, urb,
2408 qtd->length, token);
2409 if (last_status == -EREMOTEIO &&
2410 (qtd->hw_alt_next &
2411 FOTG210_LIST_END(fotg210)))
2412 last_status = -EINPROGRESS;
2413
2414
2415
2416
2417 if (unlikely(last_status != -EINPROGRESS &&
2418 last_status != -EREMOTEIO)) {
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428 if (last_status != -EPIPE)
2429 fotg210_clear_tt_buffer(fotg210, qh,
2430 urb, token);
2431 }
2432 }
2433
2434
2435
2436
2437 if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
2438 last = list_entry(qtd->qtd_list.prev,
2439 struct fotg210_qtd, qtd_list);
2440 last->hw_next = qtd->hw_next;
2441 }
2442
2443
2444 list_del(&qtd->qtd_list);
2445 last = qtd;
2446
2447
2448 qh->xacterrs = 0;
2449 }
2450
2451
2452 if (likely(last != NULL)) {
2453 fotg210_urb_done(fotg210, last->urb, last_status);
2454 count++;
2455 fotg210_qtd_free(fotg210, last);
2456 }
2457
2458
2459 if (unlikely(qh->needs_rescan)) {
2460
2461 if (state == QH_STATE_IDLE)
2462 goto rescan;
2463
2464
2465
2466
2467
2468
2469 if (state != QH_STATE_LINKED)
2470 qh->needs_rescan = 0;
2471 }
2472
2473
2474 qh->qh_state = state;
2475
2476
2477
2478
2479
2480 if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
2481 switch (state) {
2482 case QH_STATE_IDLE:
2483 qh_refresh(fotg210, qh);
2484 break;
2485 case QH_STATE_LINKED:
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499 qh->needs_rescan = 1;
2500 break;
2501
2502 }
2503 }
2504
2505 return count;
2506}
2507
2508
2509#define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
2510
2511#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
2512
2513
2514
2515
2516static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb,
2517 struct list_head *head)
2518{
2519 struct fotg210_qtd *qtd, *temp;
2520
2521 list_for_each_entry_safe(qtd, temp, head, qtd_list) {
2522 list_del(&qtd->qtd_list);
2523 fotg210_qtd_free(fotg210, qtd);
2524 }
2525}
2526
2527
2528
2529static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210,
2530 struct urb *urb, struct list_head *head, gfp_t flags)
2531{
2532 struct fotg210_qtd *qtd, *qtd_prev;
2533 dma_addr_t buf;
2534 int len, this_sg_len, maxpacket;
2535 int is_input;
2536 u32 token;
2537 int i;
2538 struct scatterlist *sg;
2539
2540
2541
2542
2543 qtd = fotg210_qtd_alloc(fotg210, flags);
2544 if (unlikely(!qtd))
2545 return NULL;
2546 list_add_tail(&qtd->qtd_list, head);
2547 qtd->urb = urb;
2548
2549 token = QTD_STS_ACTIVE;
2550 token |= (FOTG210_TUNE_CERR << 10);
2551
2552
2553 len = urb->transfer_buffer_length;
2554 is_input = usb_pipein(urb->pipe);
2555 if (usb_pipecontrol(urb->pipe)) {
2556
2557 qtd_fill(fotg210, qtd, urb->setup_dma,
2558 sizeof(struct usb_ctrlrequest),
2559 token | (2 << 8), 8);
2560
2561
2562 token ^= QTD_TOGGLE;
2563 qtd_prev = qtd;
2564 qtd = fotg210_qtd_alloc(fotg210, flags);
2565 if (unlikely(!qtd))
2566 goto cleanup;
2567 qtd->urb = urb;
2568 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2569 list_add_tail(&qtd->qtd_list, head);
2570
2571
2572 if (len == 0)
2573 token |= (1 << 8);
2574 }
2575
2576
2577
2578
2579 i = urb->num_mapped_sgs;
2580 if (len > 0 && i > 0) {
2581 sg = urb->sg;
2582 buf = sg_dma_address(sg);
2583
2584
2585
2586
2587 this_sg_len = min_t(int, sg_dma_len(sg), len);
2588 } else {
2589 sg = NULL;
2590 buf = urb->transfer_dma;
2591 this_sg_len = len;
2592 }
2593
2594 if (is_input)
2595 token |= (1 << 8);
2596
2597
2598 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
2599
2600
2601
2602
2603
2604
2605 for (;;) {
2606 int this_qtd_len;
2607
2608 this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
2609 maxpacket);
2610 this_sg_len -= this_qtd_len;
2611 len -= this_qtd_len;
2612 buf += this_qtd_len;
2613
2614
2615
2616
2617
2618
2619 if (is_input)
2620 qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
2621
2622
2623 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
2624 token ^= QTD_TOGGLE;
2625
2626 if (likely(this_sg_len <= 0)) {
2627 if (--i <= 0 || len <= 0)
2628 break;
2629 sg = sg_next(sg);
2630 buf = sg_dma_address(sg);
2631 this_sg_len = min_t(int, sg_dma_len(sg), len);
2632 }
2633
2634 qtd_prev = qtd;
2635 qtd = fotg210_qtd_alloc(fotg210, flags);
2636 if (unlikely(!qtd))
2637 goto cleanup;
2638 qtd->urb = urb;
2639 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2640 list_add_tail(&qtd->qtd_list, head);
2641 }
2642
2643
2644
2645
2646
2647
2648 if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 ||
2649 usb_pipecontrol(urb->pipe)))
2650 qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
2651
2652
2653
2654
2655
2656
2657 if (likely(urb->transfer_buffer_length != 0)) {
2658 int one_more = 0;
2659
2660 if (usb_pipecontrol(urb->pipe)) {
2661 one_more = 1;
2662 token ^= 0x0100;
2663 token |= QTD_TOGGLE;
2664 } else if (usb_pipeout(urb->pipe)
2665 && (urb->transfer_flags & URB_ZERO_PACKET)
2666 && !(urb->transfer_buffer_length % maxpacket)) {
2667 one_more = 1;
2668 }
2669 if (one_more) {
2670 qtd_prev = qtd;
2671 qtd = fotg210_qtd_alloc(fotg210, flags);
2672 if (unlikely(!qtd))
2673 goto cleanup;
2674 qtd->urb = urb;
2675 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2676 list_add_tail(&qtd->qtd_list, head);
2677
2678
2679 qtd_fill(fotg210, qtd, 0, 0, token, 0);
2680 }
2681 }
2682
2683
2684 if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
2685 qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
2686 return head;
2687
2688cleanup:
2689 qtd_list_free(fotg210, urb, head);
2690 return NULL;
2691}
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb,
2709 gfp_t flags)
2710{
2711 struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags);
2712 u32 info1 = 0, info2 = 0;
2713 int is_input, type;
2714 int maxp = 0;
2715 struct usb_tt *tt = urb->dev->tt;
2716 struct fotg210_qh_hw *hw;
2717
2718 if (!qh)
2719 return qh;
2720
2721
2722
2723
2724 info1 |= usb_pipeendpoint(urb->pipe) << 8;
2725 info1 |= usb_pipedevice(urb->pipe) << 0;
2726
2727 is_input = usb_pipein(urb->pipe);
2728 type = usb_pipetype(urb->pipe);
2729 maxp = usb_maxpacket(urb->dev, urb->pipe, !is_input);
2730
2731
2732
2733
2734 if (max_packet(maxp) > 1024) {
2735 fotg210_dbg(fotg210, "bogus qh maxpacket %d\n",
2736 max_packet(maxp));
2737 goto done;
2738 }
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748 if (type == PIPE_INTERRUPT) {
2749 qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
2750 is_input, 0,
2751 hb_mult(maxp) * max_packet(maxp)));
2752 qh->start = NO_FRAME;
2753
2754 if (urb->dev->speed == USB_SPEED_HIGH) {
2755 qh->c_usecs = 0;
2756 qh->gap_uf = 0;
2757
2758 qh->period = urb->interval >> 3;
2759 if (qh->period == 0 && urb->interval != 1) {
2760
2761
2762
2763
2764 urb->interval = 1;
2765 } else if (qh->period > fotg210->periodic_size) {
2766 qh->period = fotg210->periodic_size;
2767 urb->interval = qh->period << 3;
2768 }
2769 } else {
2770 int think_time;
2771
2772
2773 qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
2774 is_input, 0, maxp) / (125 * 1000);
2775
2776
2777 if (is_input) {
2778 qh->c_usecs = qh->usecs + HS_USECS(0);
2779 qh->usecs = HS_USECS(1);
2780 } else {
2781 qh->usecs += HS_USECS(1);
2782 qh->c_usecs = HS_USECS(0);
2783 }
2784
2785 think_time = tt ? tt->think_time : 0;
2786 qh->tt_usecs = NS_TO_US(think_time +
2787 usb_calc_bus_time(urb->dev->speed,
2788 is_input, 0, max_packet(maxp)));
2789 qh->period = urb->interval;
2790 if (qh->period > fotg210->periodic_size) {
2791 qh->period = fotg210->periodic_size;
2792 urb->interval = qh->period;
2793 }
2794 }
2795 }
2796
2797
2798 qh->dev = urb->dev;
2799
2800
2801 switch (urb->dev->speed) {
2802 case USB_SPEED_LOW:
2803 info1 |= QH_LOW_SPEED;
2804
2805
2806 case USB_SPEED_FULL:
2807
2808 if (type != PIPE_INTERRUPT)
2809 info1 |= (FOTG210_TUNE_RL_TT << 28);
2810 if (type == PIPE_CONTROL) {
2811 info1 |= QH_CONTROL_EP;
2812 info1 |= QH_TOGGLE_CTL;
2813 }
2814 info1 |= maxp << 16;
2815
2816 info2 |= (FOTG210_TUNE_MULT_TT << 30);
2817
2818
2819
2820
2821 if (fotg210_has_fsl_portno_bug(fotg210))
2822 info2 |= (urb->dev->ttport-1) << 23;
2823 else
2824 info2 |= urb->dev->ttport << 23;
2825
2826
2827
2828
2829 if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
2830 info2 |= tt->hub->devnum << 16;
2831
2832
2833
2834 break;
2835
2836 case USB_SPEED_HIGH:
2837 info1 |= QH_HIGH_SPEED;
2838 if (type == PIPE_CONTROL) {
2839 info1 |= (FOTG210_TUNE_RL_HS << 28);
2840 info1 |= 64 << 16;
2841 info1 |= QH_TOGGLE_CTL;
2842 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2843 } else if (type == PIPE_BULK) {
2844 info1 |= (FOTG210_TUNE_RL_HS << 28);
2845
2846
2847
2848
2849
2850
2851 info1 |= max_packet(maxp) << 16;
2852 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2853 } else {
2854 info1 |= max_packet(maxp) << 16;
2855 info2 |= hb_mult(maxp) << 30;
2856 }
2857 break;
2858 default:
2859 fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
2860 urb->dev->speed);
2861done:
2862 qh_destroy(fotg210, qh);
2863 return NULL;
2864 }
2865
2866
2867
2868
2869 qh->qh_state = QH_STATE_IDLE;
2870 hw = qh->hw;
2871 hw->hw_info1 = cpu_to_hc32(fotg210, info1);
2872 hw->hw_info2 = cpu_to_hc32(fotg210, info2);
2873 qh->is_out = !is_input;
2874 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
2875 qh_refresh(fotg210, qh);
2876 return qh;
2877}
2878
2879static void enable_async(struct fotg210_hcd *fotg210)
2880{
2881 if (fotg210->async_count++)
2882 return;
2883
2884
2885 fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
2886
2887
2888 fotg210_poll_ASS(fotg210);
2889 turn_on_io_watchdog(fotg210);
2890}
2891
2892static void disable_async(struct fotg210_hcd *fotg210)
2893{
2894 if (--fotg210->async_count)
2895 return;
2896
2897
2898 WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
2899
2900
2901 fotg210_poll_ASS(fotg210);
2902}
2903
2904
2905
2906static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
2907{
2908 __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
2909 struct fotg210_qh *head;
2910
2911
2912 if (unlikely(qh->clearing_tt))
2913 return;
2914
2915 WARN_ON(qh->qh_state != QH_STATE_IDLE);
2916
2917
2918 qh_refresh(fotg210, qh);
2919
2920
2921 head = fotg210->async;
2922 qh->qh_next = head->qh_next;
2923 qh->hw->hw_next = head->hw->hw_next;
2924 wmb();
2925
2926 head->qh_next.qh = qh;
2927 head->hw->hw_next = dma;
2928
2929 qh->xacterrs = 0;
2930 qh->qh_state = QH_STATE_LINKED;
2931
2932
2933 enable_async(fotg210);
2934}
2935
2936
2937
2938
2939
2940
2941static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210,
2942 struct urb *urb, struct list_head *qtd_list,
2943 int epnum, void **ptr)
2944{
2945 struct fotg210_qh *qh = NULL;
2946 __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
2947
2948 qh = (struct fotg210_qh *) *ptr;
2949 if (unlikely(qh == NULL)) {
2950
2951 qh = qh_make(fotg210, urb, GFP_ATOMIC);
2952 *ptr = qh;
2953 }
2954 if (likely(qh != NULL)) {
2955 struct fotg210_qtd *qtd;
2956
2957 if (unlikely(list_empty(qtd_list)))
2958 qtd = NULL;
2959 else
2960 qtd = list_entry(qtd_list->next, struct fotg210_qtd,
2961 qtd_list);
2962
2963
2964 if (unlikely(epnum == 0)) {
2965
2966 if (usb_pipedevice(urb->pipe) == 0)
2967 qh->hw->hw_info1 &= ~qh_addr_mask;
2968 }
2969
2970
2971
2972
2973 if (likely(qtd != NULL)) {
2974 struct fotg210_qtd *dummy;
2975 dma_addr_t dma;
2976 __hc32 token;
2977
2978
2979
2980
2981
2982
2983 token = qtd->hw_token;
2984 qtd->hw_token = HALT_BIT(fotg210);
2985
2986 dummy = qh->dummy;
2987
2988 dma = dummy->qtd_dma;
2989 *dummy = *qtd;
2990 dummy->qtd_dma = dma;
2991
2992 list_del(&qtd->qtd_list);
2993 list_add(&dummy->qtd_list, qtd_list);
2994 list_splice_tail(qtd_list, &qh->qtd_list);
2995
2996 fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
2997 qh->dummy = qtd;
2998
2999
3000 dma = qtd->qtd_dma;
3001 qtd = list_entry(qh->qtd_list.prev,
3002 struct fotg210_qtd, qtd_list);
3003 qtd->hw_next = QTD_NEXT(fotg210, dma);
3004
3005
3006 wmb();
3007 dummy->hw_token = token;
3008
3009 urb->hcpriv = qh;
3010 }
3011 }
3012 return qh;
3013}
3014
3015static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb,
3016 struct list_head *qtd_list, gfp_t mem_flags)
3017{
3018 int epnum;
3019 unsigned long flags;
3020 struct fotg210_qh *qh = NULL;
3021 int rc;
3022
3023 epnum = urb->ep->desc.bEndpointAddress;
3024
3025#ifdef FOTG210_URB_TRACE
3026 {
3027 struct fotg210_qtd *qtd;
3028
3029 qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list);
3030 fotg210_dbg(fotg210,
3031 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
3032 __func__, urb->dev->devpath, urb,
3033 epnum & 0x0f, (epnum & USB_DIR_IN)
3034 ? "in" : "out",
3035 urb->transfer_buffer_length,
3036 qtd, urb->ep->hcpriv);
3037 }
3038#endif
3039
3040 spin_lock_irqsave(&fotg210->lock, flags);
3041 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3042 rc = -ESHUTDOWN;
3043 goto done;
3044 }
3045 rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
3046 if (unlikely(rc))
3047 goto done;
3048
3049 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
3050 if (unlikely(qh == NULL)) {
3051 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
3052 rc = -ENOMEM;
3053 goto done;
3054 }
3055
3056
3057
3058
3059 if (likely(qh->qh_state == QH_STATE_IDLE))
3060 qh_link_async(fotg210, qh);
3061done:
3062 spin_unlock_irqrestore(&fotg210->lock, flags);
3063 if (unlikely(qh == NULL))
3064 qtd_list_free(fotg210, urb, qtd_list);
3065 return rc;
3066}
3067
3068static void single_unlink_async(struct fotg210_hcd *fotg210,
3069 struct fotg210_qh *qh)
3070{
3071 struct fotg210_qh *prev;
3072
3073
3074 qh->qh_state = QH_STATE_UNLINK;
3075 if (fotg210->async_unlink)
3076 fotg210->async_unlink_last->unlink_next = qh;
3077 else
3078 fotg210->async_unlink = qh;
3079 fotg210->async_unlink_last = qh;
3080
3081
3082 prev = fotg210->async;
3083 while (prev->qh_next.qh != qh)
3084 prev = prev->qh_next.qh;
3085
3086 prev->hw->hw_next = qh->hw->hw_next;
3087 prev->qh_next = qh->qh_next;
3088 if (fotg210->qh_scan_next == qh)
3089 fotg210->qh_scan_next = qh->qh_next.qh;
3090}
3091
3092static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
3093{
3094
3095
3096
3097
3098 if (fotg210->async_iaa || fotg210->async_unlinking)
3099 return;
3100
3101
3102 fotg210->async_iaa = fotg210->async_unlink;
3103 fotg210->async_unlink = NULL;
3104
3105
3106 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
3107 if (!nested)
3108 end_unlink_async(fotg210);
3109
3110
3111 } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
3112
3113 wmb();
3114
3115 fotg210_writel(fotg210, fotg210->command | CMD_IAAD,
3116 &fotg210->regs->command);
3117 fotg210_readl(fotg210, &fotg210->regs->command);
3118 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG,
3119 true);
3120 }
3121}
3122
3123
3124
3125static void end_unlink_async(struct fotg210_hcd *fotg210)
3126{
3127 struct fotg210_qh *qh;
3128
3129
3130restart:
3131 fotg210->async_unlinking = true;
3132 while (fotg210->async_iaa) {
3133 qh = fotg210->async_iaa;
3134 fotg210->async_iaa = qh->unlink_next;
3135 qh->unlink_next = NULL;
3136
3137 qh->qh_state = QH_STATE_IDLE;
3138 qh->qh_next.qh = NULL;
3139
3140 qh_completions(fotg210, qh);
3141 if (!list_empty(&qh->qtd_list) &&
3142 fotg210->rh_state == FOTG210_RH_RUNNING)
3143 qh_link_async(fotg210, qh);
3144 disable_async(fotg210);
3145 }
3146 fotg210->async_unlinking = false;
3147
3148
3149 if (fotg210->async_unlink) {
3150 start_iaa_cycle(fotg210, true);
3151 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
3152 goto restart;
3153 }
3154}
3155
3156static void unlink_empty_async(struct fotg210_hcd *fotg210)
3157{
3158 struct fotg210_qh *qh, *next;
3159 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
3160 bool check_unlinks_later = false;
3161
3162
3163 next = fotg210->async->qh_next.qh;
3164 while (next) {
3165 qh = next;
3166 next = qh->qh_next.qh;
3167
3168 if (list_empty(&qh->qtd_list) &&
3169 qh->qh_state == QH_STATE_LINKED) {
3170 if (!stopped && qh->unlink_cycle ==
3171 fotg210->async_unlink_cycle)
3172 check_unlinks_later = true;
3173 else
3174 single_unlink_async(fotg210, qh);
3175 }
3176 }
3177
3178
3179 if (fotg210->async_unlink)
3180 start_iaa_cycle(fotg210, false);
3181
3182
3183 if (check_unlinks_later) {
3184 fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
3185 true);
3186 ++fotg210->async_unlink_cycle;
3187 }
3188}
3189
3190
3191
3192
3193static void start_unlink_async(struct fotg210_hcd *fotg210,
3194 struct fotg210_qh *qh)
3195{
3196
3197
3198
3199
3200
3201 if (qh->qh_state != QH_STATE_LINKED) {
3202 if (qh->qh_state == QH_STATE_COMPLETING)
3203 qh->needs_rescan = 1;
3204 return;
3205 }
3206
3207 single_unlink_async(fotg210, qh);
3208 start_iaa_cycle(fotg210, false);
3209}
3210
3211static void scan_async(struct fotg210_hcd *fotg210)
3212{
3213 struct fotg210_qh *qh;
3214 bool check_unlinks_later = false;
3215
3216 fotg210->qh_scan_next = fotg210->async->qh_next.qh;
3217 while (fotg210->qh_scan_next) {
3218 qh = fotg210->qh_scan_next;
3219 fotg210->qh_scan_next = qh->qh_next.qh;
3220rescan:
3221
3222 if (!list_empty(&qh->qtd_list)) {
3223 int temp;
3224
3225
3226
3227
3228
3229
3230
3231
3232 temp = qh_completions(fotg210, qh);
3233 if (qh->needs_rescan) {
3234 start_unlink_async(fotg210, qh);
3235 } else if (list_empty(&qh->qtd_list)
3236 && qh->qh_state == QH_STATE_LINKED) {
3237 qh->unlink_cycle = fotg210->async_unlink_cycle;
3238 check_unlinks_later = true;
3239 } else if (temp != 0)
3240 goto rescan;
3241 }
3242 }
3243
3244
3245
3246
3247
3248
3249
3250 if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING &&
3251 !(fotg210->enabled_hrtimer_events &
3252 BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) {
3253 fotg210_enable_event(fotg210,
3254 FOTG210_HRTIMER_ASYNC_UNLINKS, true);
3255 ++fotg210->async_unlink_cycle;
3256 }
3257}
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269static int fotg210_get_frame(struct usb_hcd *hcd);
3270
3271
3272
3273
3274
3275static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210,
3276 union fotg210_shadow *periodic, __hc32 tag)
3277{
3278 switch (hc32_to_cpu(fotg210, tag)) {
3279 case Q_TYPE_QH:
3280 return &periodic->qh->qh_next;
3281 case Q_TYPE_FSTN:
3282 return &periodic->fstn->fstn_next;
3283 default:
3284 return &periodic->itd->itd_next;
3285 }
3286}
3287
3288static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210,
3289 union fotg210_shadow *periodic, __hc32 tag)
3290{
3291 switch (hc32_to_cpu(fotg210, tag)) {
3292
3293 case Q_TYPE_QH:
3294 return &periodic->qh->hw->hw_next;
3295
3296 default:
3297 return periodic->hw_next;
3298 }
3299}
3300
3301
3302static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
3303 void *ptr)
3304{
3305 union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
3306 __hc32 *hw_p = &fotg210->periodic[frame];
3307 union fotg210_shadow here = *prev_p;
3308
3309
3310 while (here.ptr && here.ptr != ptr) {
3311 prev_p = periodic_next_shadow(fotg210, prev_p,
3312 Q_NEXT_TYPE(fotg210, *hw_p));
3313 hw_p = shadow_next_periodic(fotg210, &here,
3314 Q_NEXT_TYPE(fotg210, *hw_p));
3315 here = *prev_p;
3316 }
3317
3318 if (!here.ptr)
3319 return;
3320
3321
3322
3323
3324 *prev_p = *periodic_next_shadow(fotg210, &here,
3325 Q_NEXT_TYPE(fotg210, *hw_p));
3326
3327 *hw_p = *shadow_next_periodic(fotg210, &here,
3328 Q_NEXT_TYPE(fotg210, *hw_p));
3329}
3330
3331
3332static unsigned short periodic_usecs(struct fotg210_hcd *fotg210,
3333 unsigned frame, unsigned uframe)
3334{
3335 __hc32 *hw_p = &fotg210->periodic[frame];
3336 union fotg210_shadow *q = &fotg210->pshadow[frame];
3337 unsigned usecs = 0;
3338 struct fotg210_qh_hw *hw;
3339
3340 while (q->ptr) {
3341 switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
3342 case Q_TYPE_QH:
3343 hw = q->qh->hw;
3344
3345 if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
3346 usecs += q->qh->usecs;
3347
3348 if (hw->hw_info2 & cpu_to_hc32(fotg210,
3349 1 << (8 + uframe)))
3350 usecs += q->qh->c_usecs;
3351 hw_p = &hw->hw_next;
3352 q = &q->qh->qh_next;
3353 break;
3354
3355 default:
3356
3357
3358
3359 if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
3360 fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
3361
3362 hw_p = &q->fstn->hw_next;
3363 q = &q->fstn->fstn_next;
3364 break;
3365 case Q_TYPE_ITD:
3366 if (q->itd->hw_transaction[uframe])
3367 usecs += q->itd->stream->usecs;
3368 hw_p = &q->itd->hw_next;
3369 q = &q->itd->itd_next;
3370 break;
3371 }
3372 }
3373 if (usecs > fotg210->uframe_periodic_max)
3374 fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
3375 frame * 8 + uframe, usecs);
3376 return usecs;
3377}
3378
3379static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
3380{
3381 if (!dev1->tt || !dev2->tt)
3382 return 0;
3383 if (dev1->tt != dev2->tt)
3384 return 0;
3385 if (dev1->tt->multi)
3386 return dev1->ttport == dev2->ttport;
3387 else
3388 return 1;
3389}
3390
3391
3392
3393
3394
3395static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period,
3396 struct usb_device *dev, unsigned frame, u32 uf_mask)
3397{
3398 if (period == 0)
3399 return 0;
3400
3401
3402
3403
3404
3405 for (; frame < fotg210->periodic_size; frame += period) {
3406 union fotg210_shadow here;
3407 __hc32 type;
3408 struct fotg210_qh_hw *hw;
3409
3410 here = fotg210->pshadow[frame];
3411 type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
3412 while (here.ptr) {
3413 switch (hc32_to_cpu(fotg210, type)) {
3414 case Q_TYPE_ITD:
3415 type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
3416 here = here.itd->itd_next;
3417 continue;
3418 case Q_TYPE_QH:
3419 hw = here.qh->hw;
3420 if (same_tt(dev, here.qh->dev)) {
3421 u32 mask;
3422
3423 mask = hc32_to_cpu(fotg210,
3424 hw->hw_info2);
3425
3426 mask |= mask >> 8;
3427 if (mask & uf_mask)
3428 break;
3429 }
3430 type = Q_NEXT_TYPE(fotg210, hw->hw_next);
3431 here = here.qh->qh_next;
3432 continue;
3433
3434 default:
3435 fotg210_dbg(fotg210,
3436 "periodic frame %d bogus type %d\n",
3437 frame, type);
3438 }
3439
3440
3441 return 0;
3442 }
3443 }
3444
3445
3446 return 1;
3447}
3448
3449static void enable_periodic(struct fotg210_hcd *fotg210)
3450{
3451 if (fotg210->periodic_count++)
3452 return;
3453
3454
3455 fotg210->enabled_hrtimer_events &=
3456 ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
3457
3458
3459 fotg210_poll_PSS(fotg210);
3460 turn_on_io_watchdog(fotg210);
3461}
3462
3463static void disable_periodic(struct fotg210_hcd *fotg210)
3464{
3465 if (--fotg210->periodic_count)
3466 return;
3467
3468
3469 fotg210_poll_PSS(fotg210);
3470}
3471
3472
3473
3474
3475
3476
3477
3478static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3479{
3480 unsigned i;
3481 unsigned period = qh->period;
3482
3483 dev_dbg(&qh->dev->dev,
3484 "link qh%d-%04x/%p start %d [%d/%d us]\n", period,
3485 hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
3486 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
3487 qh->c_usecs);
3488
3489
3490 if (period == 0)
3491 period = 1;
3492
3493 for (i = qh->start; i < fotg210->periodic_size; i += period) {
3494 union fotg210_shadow *prev = &fotg210->pshadow[i];
3495 __hc32 *hw_p = &fotg210->periodic[i];
3496 union fotg210_shadow here = *prev;
3497 __hc32 type = 0;
3498
3499
3500 while (here.ptr) {
3501 type = Q_NEXT_TYPE(fotg210, *hw_p);
3502 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
3503 break;
3504 prev = periodic_next_shadow(fotg210, prev, type);
3505 hw_p = shadow_next_periodic(fotg210, &here, type);
3506 here = *prev;
3507 }
3508
3509
3510
3511
3512 while (here.ptr && qh != here.qh) {
3513 if (qh->period > here.qh->period)
3514 break;
3515 prev = &here.qh->qh_next;
3516 hw_p = &here.qh->hw->hw_next;
3517 here = *prev;
3518 }
3519
3520 if (qh != here.qh) {
3521 qh->qh_next = here;
3522 if (here.qh)
3523 qh->hw->hw_next = *hw_p;
3524 wmb();
3525 prev->qh = qh;
3526 *hw_p = QH_NEXT(fotg210, qh->qh_dma);
3527 }
3528 }
3529 qh->qh_state = QH_STATE_LINKED;
3530 qh->xacterrs = 0;
3531
3532
3533 fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
3534 ? ((qh->usecs + qh->c_usecs) / qh->period)
3535 : (qh->usecs * 8);
3536
3537 list_add(&qh->intr_node, &fotg210->intr_qh_list);
3538
3539
3540 ++fotg210->intr_count;
3541 enable_periodic(fotg210);
3542}
3543
3544static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
3545 struct fotg210_qh *qh)
3546{
3547 unsigned i;
3548 unsigned period;
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566 period = qh->period;
3567 if (!period)
3568 period = 1;
3569
3570 for (i = qh->start; i < fotg210->periodic_size; i += period)
3571 periodic_unlink(fotg210, i, qh);
3572
3573
3574 fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
3575 ? ((qh->usecs + qh->c_usecs) / qh->period)
3576 : (qh->usecs * 8);
3577
3578 dev_dbg(&qh->dev->dev,
3579 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
3580 qh->period, hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
3581 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
3582 qh->c_usecs);
3583
3584
3585 qh->qh_state = QH_STATE_UNLINK;
3586 qh->qh_next.ptr = NULL;
3587
3588 if (fotg210->qh_scan_next == qh)
3589 fotg210->qh_scan_next = list_entry(qh->intr_node.next,
3590 struct fotg210_qh, intr_node);
3591 list_del(&qh->intr_node);
3592}
3593
3594static void start_unlink_intr(struct fotg210_hcd *fotg210,
3595 struct fotg210_qh *qh)
3596{
3597
3598
3599
3600
3601 if (qh->qh_state != QH_STATE_LINKED) {
3602 if (qh->qh_state == QH_STATE_COMPLETING)
3603 qh->needs_rescan = 1;
3604 return;
3605 }
3606
3607 qh_unlink_periodic(fotg210, qh);
3608
3609
3610 wmb();
3611
3612
3613
3614
3615
3616
3617 qh->unlink_cycle = fotg210->intr_unlink_cycle;
3618
3619
3620 if (fotg210->intr_unlink)
3621 fotg210->intr_unlink_last->unlink_next = qh;
3622 else
3623 fotg210->intr_unlink = qh;
3624 fotg210->intr_unlink_last = qh;
3625
3626 if (fotg210->intr_unlinking)
3627 ;
3628 else if (fotg210->rh_state < FOTG210_RH_RUNNING)
3629 fotg210_handle_intr_unlinks(fotg210);
3630 else if (fotg210->intr_unlink == qh) {
3631 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
3632 true);
3633 ++fotg210->intr_unlink_cycle;
3634 }
3635}
3636
3637static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3638{
3639 struct fotg210_qh_hw *hw = qh->hw;
3640 int rc;
3641
3642 qh->qh_state = QH_STATE_IDLE;
3643 hw->hw_next = FOTG210_LIST_END(fotg210);
3644
3645 qh_completions(fotg210, qh);
3646
3647
3648 if (!list_empty(&qh->qtd_list) &&
3649 fotg210->rh_state == FOTG210_RH_RUNNING) {
3650 rc = qh_schedule(fotg210, qh);
3651
3652
3653
3654
3655
3656
3657
3658 if (rc != 0)
3659 fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
3660 qh, rc);
3661 }
3662
3663
3664 --fotg210->intr_count;
3665 disable_periodic(fotg210);
3666}
3667
3668static int check_period(struct fotg210_hcd *fotg210, unsigned frame,
3669 unsigned uframe, unsigned period, unsigned usecs)
3670{
3671 int claimed;
3672
3673
3674
3675
3676 if (uframe >= 8)
3677 return 0;
3678
3679
3680 usecs = fotg210->uframe_periodic_max - usecs;
3681
3682
3683
3684
3685 if (unlikely(period == 0)) {
3686 do {
3687 for (uframe = 0; uframe < 7; uframe++) {
3688 claimed = periodic_usecs(fotg210, frame,
3689 uframe);
3690 if (claimed > usecs)
3691 return 0;
3692 }
3693 } while ((frame += 1) < fotg210->periodic_size);
3694
3695
3696 } else {
3697 do {
3698 claimed = periodic_usecs(fotg210, frame, uframe);
3699 if (claimed > usecs)
3700 return 0;
3701 } while ((frame += period) < fotg210->periodic_size);
3702 }
3703
3704
3705 return 1;
3706}
3707
3708static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame,
3709 unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp)
3710{
3711 int retval = -ENOSPC;
3712 u8 mask = 0;
3713
3714 if (qh->c_usecs && uframe >= 6)
3715 goto done;
3716
3717 if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
3718 goto done;
3719 if (!qh->c_usecs) {
3720 retval = 0;
3721 *c_maskp = 0;
3722 goto done;
3723 }
3724
3725
3726
3727
3728
3729
3730
3731
3732 mask = 0x03 << (uframe + qh->gap_uf);
3733 *c_maskp = cpu_to_hc32(fotg210, mask << 8);
3734
3735 mask |= 1 << uframe;
3736 if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) {
3737 if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1,
3738 qh->period, qh->c_usecs))
3739 goto done;
3740 if (!check_period(fotg210, frame, uframe + qh->gap_uf,
3741 qh->period, qh->c_usecs))
3742 goto done;
3743 retval = 0;
3744 }
3745done:
3746 return retval;
3747}
3748
3749
3750
3751
3752static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3753{
3754 int status;
3755 unsigned uframe;
3756 __hc32 c_mask;
3757 unsigned frame;
3758 struct fotg210_qh_hw *hw = qh->hw;
3759
3760 qh_refresh(fotg210, qh);
3761 hw->hw_next = FOTG210_LIST_END(fotg210);
3762 frame = qh->start;
3763
3764
3765 if (frame < qh->period) {
3766 uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK);
3767 status = check_intr_schedule(fotg210, frame, --uframe,
3768 qh, &c_mask);
3769 } else {
3770 uframe = 0;
3771 c_mask = 0;
3772 status = -ENOSPC;
3773 }
3774
3775
3776
3777
3778 if (status) {
3779
3780 if (qh->period) {
3781 int i;
3782
3783 for (i = qh->period; status && i > 0; --i) {
3784 frame = ++fotg210->random_frame % qh->period;
3785 for (uframe = 0; uframe < 8; uframe++) {
3786 status = check_intr_schedule(fotg210,
3787 frame, uframe, qh,
3788 &c_mask);
3789 if (status == 0)
3790 break;
3791 }
3792 }
3793
3794
3795 } else {
3796 frame = 0;
3797 status = check_intr_schedule(fotg210, 0, 0, qh,
3798 &c_mask);
3799 }
3800 if (status)
3801 goto done;
3802 qh->start = frame;
3803
3804
3805 hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK));
3806 hw->hw_info2 |= qh->period
3807 ? cpu_to_hc32(fotg210, 1 << uframe)
3808 : cpu_to_hc32(fotg210, QH_SMASK);
3809 hw->hw_info2 |= c_mask;
3810 } else
3811 fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
3812
3813
3814 qh_link_periodic(fotg210, qh);
3815done:
3816 return status;
3817}
3818
3819static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb,
3820 struct list_head *qtd_list, gfp_t mem_flags)
3821{
3822 unsigned epnum;
3823 unsigned long flags;
3824 struct fotg210_qh *qh;
3825 int status;
3826 struct list_head empty;
3827
3828
3829 epnum = urb->ep->desc.bEndpointAddress;
3830
3831 spin_lock_irqsave(&fotg210->lock, flags);
3832
3833 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3834 status = -ESHUTDOWN;
3835 goto done_not_linked;
3836 }
3837 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
3838 if (unlikely(status))
3839 goto done_not_linked;
3840
3841
3842 INIT_LIST_HEAD(&empty);
3843 qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
3844 if (qh == NULL) {
3845 status = -ENOMEM;
3846 goto done;
3847 }
3848 if (qh->qh_state == QH_STATE_IDLE) {
3849 status = qh_schedule(fotg210, qh);
3850 if (status)
3851 goto done;
3852 }
3853
3854
3855 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
3856 BUG_ON(qh == NULL);
3857
3858
3859 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
3860
3861done:
3862 if (unlikely(status))
3863 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
3864done_not_linked:
3865 spin_unlock_irqrestore(&fotg210->lock, flags);
3866 if (status)
3867 qtd_list_free(fotg210, urb, qtd_list);
3868
3869 return status;
3870}
3871
3872static void scan_intr(struct fotg210_hcd *fotg210)
3873{
3874 struct fotg210_qh *qh;
3875
3876 list_for_each_entry_safe(qh, fotg210->qh_scan_next,
3877 &fotg210->intr_qh_list, intr_node) {
3878rescan:
3879
3880 if (!list_empty(&qh->qtd_list)) {
3881 int temp;
3882
3883
3884
3885
3886
3887
3888
3889
3890 temp = qh_completions(fotg210, qh);
3891 if (unlikely(qh->needs_rescan ||
3892 (list_empty(&qh->qtd_list) &&
3893 qh->qh_state == QH_STATE_LINKED)))
3894 start_unlink_intr(fotg210, qh);
3895 else if (temp != 0)
3896 goto rescan;
3897 }
3898 }
3899}
3900
3901
3902
3903static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags)
3904{
3905 struct fotg210_iso_stream *stream;
3906
3907 stream = kzalloc(sizeof(*stream), mem_flags);
3908 if (likely(stream != NULL)) {
3909 INIT_LIST_HEAD(&stream->td_list);
3910 INIT_LIST_HEAD(&stream->free_list);
3911 stream->next_uframe = -1;
3912 }
3913 return stream;
3914}
3915
3916static void iso_stream_init(struct fotg210_hcd *fotg210,
3917 struct fotg210_iso_stream *stream, struct usb_device *dev,
3918 int pipe, unsigned interval)
3919{
3920 u32 buf1;
3921 unsigned epnum, maxp;
3922 int is_input;
3923 long bandwidth;
3924 unsigned multi;
3925
3926
3927
3928
3929
3930 epnum = usb_pipeendpoint(pipe);
3931 is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
3932 maxp = usb_maxpacket(dev, pipe, !is_input);
3933 if (is_input)
3934 buf1 = (1 << 11);
3935 else
3936 buf1 = 0;
3937
3938 maxp = max_packet(maxp);
3939 multi = hb_mult(maxp);
3940 buf1 |= maxp;
3941 maxp *= multi;
3942
3943 stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum);
3944 stream->buf1 = cpu_to_hc32(fotg210, buf1);
3945 stream->buf2 = cpu_to_hc32(fotg210, multi);
3946
3947
3948
3949
3950 if (dev->speed == USB_SPEED_FULL) {
3951 interval <<= 3;
3952 stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
3953 is_input, 1, maxp));
3954 stream->usecs /= 8;
3955 } else {
3956 stream->highspeed = 1;
3957 stream->usecs = HS_USECS_ISO(maxp);
3958 }
3959 bandwidth = stream->usecs * 8;
3960 bandwidth /= interval;
3961
3962 stream->bandwidth = bandwidth;
3963 stream->udev = dev;
3964 stream->bEndpointAddress = is_input | epnum;
3965 stream->interval = interval;
3966 stream->maxp = maxp;
3967}
3968
3969static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210,
3970 struct urb *urb)
3971{
3972 unsigned epnum;
3973 struct fotg210_iso_stream *stream;
3974 struct usb_host_endpoint *ep;
3975 unsigned long flags;
3976
3977 epnum = usb_pipeendpoint(urb->pipe);
3978 if (usb_pipein(urb->pipe))
3979 ep = urb->dev->ep_in[epnum];
3980 else
3981 ep = urb->dev->ep_out[epnum];
3982
3983 spin_lock_irqsave(&fotg210->lock, flags);
3984 stream = ep->hcpriv;
3985
3986 if (unlikely(stream == NULL)) {
3987 stream = iso_stream_alloc(GFP_ATOMIC);
3988 if (likely(stream != NULL)) {
3989 ep->hcpriv = stream;
3990 stream->ep = ep;
3991 iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
3992 urb->interval);
3993 }
3994
3995
3996 } else if (unlikely(stream->hw != NULL)) {
3997 fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n",
3998 urb->dev->devpath, epnum,
3999 usb_pipein(urb->pipe) ? "in" : "out");
4000 stream = NULL;
4001 }
4002
4003 spin_unlock_irqrestore(&fotg210->lock, flags);
4004 return stream;
4005}
4006
4007
4008
4009static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
4010 gfp_t mem_flags)
4011{
4012 struct fotg210_iso_sched *iso_sched;
4013 int size = sizeof(*iso_sched);
4014
4015 size += packets * sizeof(struct fotg210_iso_packet);
4016 iso_sched = kzalloc(size, mem_flags);
4017 if (likely(iso_sched != NULL))
4018 INIT_LIST_HEAD(&iso_sched->td_list);
4019
4020 return iso_sched;
4021}
4022
4023static inline void itd_sched_init(struct fotg210_hcd *fotg210,
4024 struct fotg210_iso_sched *iso_sched,
4025 struct fotg210_iso_stream *stream, struct urb *urb)
4026{
4027 unsigned i;
4028 dma_addr_t dma = urb->transfer_dma;
4029
4030
4031 iso_sched->span = urb->number_of_packets * stream->interval;
4032
4033
4034
4035
4036 for (i = 0; i < urb->number_of_packets; i++) {
4037 struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
4038 unsigned length;
4039 dma_addr_t buf;
4040 u32 trans;
4041
4042 length = urb->iso_frame_desc[i].length;
4043 buf = dma + urb->iso_frame_desc[i].offset;
4044
4045 trans = FOTG210_ISOC_ACTIVE;
4046 trans |= buf & 0x0fff;
4047 if (unlikely(((i + 1) == urb->number_of_packets))
4048 && !(urb->transfer_flags & URB_NO_INTERRUPT))
4049 trans |= FOTG210_ITD_IOC;
4050 trans |= length << 16;
4051 uframe->transaction = cpu_to_hc32(fotg210, trans);
4052
4053
4054 uframe->bufp = (buf & ~(u64)0x0fff);
4055 buf += length;
4056 if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
4057 uframe->cross = 1;
4058 }
4059}
4060
4061static void iso_sched_free(struct fotg210_iso_stream *stream,
4062 struct fotg210_iso_sched *iso_sched)
4063{
4064 if (!iso_sched)
4065 return;
4066
4067 list_splice(&iso_sched->td_list, &stream->free_list);
4068 kfree(iso_sched);
4069}
4070
4071static int itd_urb_transaction(struct fotg210_iso_stream *stream,
4072 struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags)
4073{
4074 struct fotg210_itd *itd;
4075 dma_addr_t itd_dma;
4076 int i;
4077 unsigned num_itds;
4078 struct fotg210_iso_sched *sched;
4079 unsigned long flags;
4080
4081 sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
4082 if (unlikely(sched == NULL))
4083 return -ENOMEM;
4084
4085 itd_sched_init(fotg210, sched, stream, urb);
4086
4087 if (urb->interval < 8)
4088 num_itds = 1 + (sched->span + 7) / 8;
4089 else
4090 num_itds = urb->number_of_packets;
4091
4092
4093 spin_lock_irqsave(&fotg210->lock, flags);
4094 for (i = 0; i < num_itds; i++) {
4095
4096
4097
4098
4099
4100 if (likely(!list_empty(&stream->free_list))) {
4101 itd = list_first_entry(&stream->free_list,
4102 struct fotg210_itd, itd_list);
4103 if (itd->frame == fotg210->now_frame)
4104 goto alloc_itd;
4105 list_del(&itd->itd_list);
4106 itd_dma = itd->itd_dma;
4107 } else {
4108alloc_itd:
4109 spin_unlock_irqrestore(&fotg210->lock, flags);
4110 itd = dma_pool_zalloc(fotg210->itd_pool, mem_flags,
4111 &itd_dma);
4112 spin_lock_irqsave(&fotg210->lock, flags);
4113 if (!itd) {
4114 iso_sched_free(stream, sched);
4115 spin_unlock_irqrestore(&fotg210->lock, flags);
4116 return -ENOMEM;
4117 }
4118 }
4119
4120 itd->itd_dma = itd_dma;
4121 list_add(&itd->itd_list, &sched->td_list);
4122 }
4123 spin_unlock_irqrestore(&fotg210->lock, flags);
4124
4125
4126 urb->hcpriv = sched;
4127 urb->error_count = 0;
4128 return 0;
4129}
4130
4131static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe,
4132 u8 usecs, u32 period)
4133{
4134 uframe %= period;
4135 do {
4136
4137 if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
4138 > (fotg210->uframe_periodic_max - usecs))
4139 return 0;
4140
4141
4142 uframe += period;
4143 } while (uframe < mod);
4144 return 1;
4145}
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157#define SCHEDULE_SLOP 80
4158
4159static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb,
4160 struct fotg210_iso_stream *stream)
4161{
4162 u32 now, next, start, period, span;
4163 int status;
4164 unsigned mod = fotg210->periodic_size << 3;
4165 struct fotg210_iso_sched *sched = urb->hcpriv;
4166
4167 period = urb->interval;
4168 span = sched->span;
4169
4170 if (span > mod - SCHEDULE_SLOP) {
4171 fotg210_dbg(fotg210, "iso request %p too long\n", urb);
4172 status = -EFBIG;
4173 goto fail;
4174 }
4175
4176 now = fotg210_read_frame_index(fotg210) & (mod - 1);
4177
4178
4179
4180
4181
4182
4183 if (likely(!list_empty(&stream->td_list))) {
4184 u32 excess;
4185
4186
4187
4188
4189
4190
4191 if (!stream->highspeed && fotg210->fs_i_thresh)
4192 next = now + fotg210->i_thresh;
4193 else
4194 next = now;
4195
4196
4197
4198
4199
4200 excess = (stream->next_uframe - period - next) & (mod - 1);
4201 if (excess >= mod - 2 * SCHEDULE_SLOP)
4202 start = next + excess - mod + period *
4203 DIV_ROUND_UP(mod - excess, period);
4204 else
4205 start = next + excess + period;
4206 if (start - now >= mod) {
4207 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
4208 urb, start - now - period, period,
4209 mod);
4210 status = -EFBIG;
4211 goto fail;
4212 }
4213 }
4214
4215
4216
4217
4218
4219
4220
4221 else {
4222 int done = 0;
4223
4224 start = SCHEDULE_SLOP + (now & ~0x07);
4225
4226
4227
4228
4229
4230
4231
4232
4233 next = start;
4234 start += period;
4235 do {
4236 start--;
4237
4238 if (itd_slot_ok(fotg210, mod, start,
4239 stream->usecs, period))
4240 done = 1;
4241 } while (start > next && !done);
4242
4243
4244 if (!done) {
4245 fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
4246 urb, now, now + mod);
4247 status = -ENOSPC;
4248 goto fail;
4249 }
4250 }
4251
4252
4253 if (unlikely(start - now + span - period >=
4254 mod - 2 * SCHEDULE_SLOP)) {
4255 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
4256 urb, start - now, span - period,
4257 mod - 2 * SCHEDULE_SLOP);
4258 status = -EFBIG;
4259 goto fail;
4260 }
4261
4262 stream->next_uframe = start & (mod - 1);
4263
4264
4265 urb->start_frame = stream->next_uframe;
4266 if (!stream->highspeed)
4267 urb->start_frame >>= 3;
4268
4269
4270 if (fotg210->isoc_count == 0)
4271 fotg210->next_frame = now >> 3;
4272 return 0;
4273
4274fail:
4275 iso_sched_free(stream, sched);
4276 urb->hcpriv = NULL;
4277 return status;
4278}
4279
4280static inline void itd_init(struct fotg210_hcd *fotg210,
4281 struct fotg210_iso_stream *stream, struct fotg210_itd *itd)
4282{
4283 int i;
4284
4285
4286 itd->hw_next = FOTG210_LIST_END(fotg210);
4287 itd->hw_bufp[0] = stream->buf0;
4288 itd->hw_bufp[1] = stream->buf1;
4289 itd->hw_bufp[2] = stream->buf2;
4290
4291 for (i = 0; i < 8; i++)
4292 itd->index[i] = -1;
4293
4294
4295}
4296
4297static inline void itd_patch(struct fotg210_hcd *fotg210,
4298 struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched,
4299 unsigned index, u16 uframe)
4300{
4301 struct fotg210_iso_packet *uf = &iso_sched->packet[index];
4302 unsigned pg = itd->pg;
4303
4304 uframe &= 0x07;
4305 itd->index[uframe] = index;
4306
4307 itd->hw_transaction[uframe] = uf->transaction;
4308 itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12);
4309 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0);
4310 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32));
4311
4312
4313 if (unlikely(uf->cross)) {
4314 u64 bufp = uf->bufp + 4096;
4315
4316 itd->pg = ++pg;
4317 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0);
4318 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32));
4319 }
4320}
4321
4322static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame,
4323 struct fotg210_itd *itd)
4324{
4325 union fotg210_shadow *prev = &fotg210->pshadow[frame];
4326 __hc32 *hw_p = &fotg210->periodic[frame];
4327 union fotg210_shadow here = *prev;
4328 __hc32 type = 0;
4329
4330
4331 while (here.ptr) {
4332 type = Q_NEXT_TYPE(fotg210, *hw_p);
4333 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
4334 break;
4335 prev = periodic_next_shadow(fotg210, prev, type);
4336 hw_p = shadow_next_periodic(fotg210, &here, type);
4337 here = *prev;
4338 }
4339
4340 itd->itd_next = here;
4341 itd->hw_next = *hw_p;
4342 prev->itd = itd;
4343 itd->frame = frame;
4344 wmb();
4345 *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
4346}
4347
4348
4349static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb,
4350 unsigned mod, struct fotg210_iso_stream *stream)
4351{
4352 int packet;
4353 unsigned next_uframe, uframe, frame;
4354 struct fotg210_iso_sched *iso_sched = urb->hcpriv;
4355 struct fotg210_itd *itd;
4356
4357 next_uframe = stream->next_uframe & (mod - 1);
4358
4359 if (unlikely(list_empty(&stream->td_list))) {
4360 fotg210_to_hcd(fotg210)->self.bandwidth_allocated
4361 += stream->bandwidth;
4362 fotg210_dbg(fotg210,
4363 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
4364 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
4365 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
4366 urb->interval,
4367 next_uframe >> 3, next_uframe & 0x7);
4368 }
4369
4370
4371 for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
4372 if (itd == NULL) {
4373
4374
4375
4376
4377 itd = list_entry(iso_sched->td_list.next,
4378 struct fotg210_itd, itd_list);
4379 list_move_tail(&itd->itd_list, &stream->td_list);
4380 itd->stream = stream;
4381 itd->urb = urb;
4382 itd_init(fotg210, stream, itd);
4383 }
4384
4385 uframe = next_uframe & 0x07;
4386 frame = next_uframe >> 3;
4387
4388 itd_patch(fotg210, itd, iso_sched, packet, uframe);
4389
4390 next_uframe += stream->interval;
4391 next_uframe &= mod - 1;
4392 packet++;
4393
4394
4395 if (((next_uframe >> 3) != frame)
4396 || packet == urb->number_of_packets) {
4397 itd_link(fotg210, frame & (fotg210->periodic_size - 1),
4398 itd);
4399 itd = NULL;
4400 }
4401 }
4402 stream->next_uframe = next_uframe;
4403
4404
4405 iso_sched_free(stream, iso_sched);
4406 urb->hcpriv = NULL;
4407
4408 ++fotg210->isoc_count;
4409 enable_periodic(fotg210);
4410}
4411
4412#define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
4413 FOTG210_ISOC_XACTERR)
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
4426{
4427 struct urb *urb = itd->urb;
4428 struct usb_iso_packet_descriptor *desc;
4429 u32 t;
4430 unsigned uframe;
4431 int urb_index = -1;
4432 struct fotg210_iso_stream *stream = itd->stream;
4433 struct usb_device *dev;
4434 bool retval = false;
4435
4436
4437 for (uframe = 0; uframe < 8; uframe++) {
4438 if (likely(itd->index[uframe] == -1))
4439 continue;
4440 urb_index = itd->index[uframe];
4441 desc = &urb->iso_frame_desc[urb_index];
4442
4443 t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
4444 itd->hw_transaction[uframe] = 0;
4445
4446
4447 if (unlikely(t & ISO_ERRS)) {
4448 urb->error_count++;
4449 if (t & FOTG210_ISOC_BUF_ERR)
4450 desc->status = usb_pipein(urb->pipe)
4451 ? -ENOSR
4452 : -ECOMM;
4453 else if (t & FOTG210_ISOC_BABBLE)
4454 desc->status = -EOVERFLOW;
4455 else
4456 desc->status = -EPROTO;
4457
4458
4459 if (!(t & FOTG210_ISOC_BABBLE)) {
4460 desc->actual_length =
4461 fotg210_itdlen(urb, desc, t);
4462 urb->actual_length += desc->actual_length;
4463 }
4464 } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
4465 desc->status = 0;
4466 desc->actual_length = fotg210_itdlen(urb, desc, t);
4467 urb->actual_length += desc->actual_length;
4468 } else {
4469
4470 desc->status = -EXDEV;
4471 }
4472 }
4473
4474
4475 if (likely((urb_index + 1) != urb->number_of_packets))
4476 goto done;
4477
4478
4479
4480
4481
4482
4483
4484 dev = urb->dev;
4485 fotg210_urb_done(fotg210, urb, 0);
4486 retval = true;
4487 urb = NULL;
4488
4489 --fotg210->isoc_count;
4490 disable_periodic(fotg210);
4491
4492 if (unlikely(list_is_singular(&stream->td_list))) {
4493 fotg210_to_hcd(fotg210)->self.bandwidth_allocated
4494 -= stream->bandwidth;
4495 fotg210_dbg(fotg210,
4496 "deschedule devp %s ep%d%s-iso\n",
4497 dev->devpath, stream->bEndpointAddress & 0x0f,
4498 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
4499 }
4500
4501done:
4502 itd->urb = NULL;
4503
4504
4505 list_move_tail(&itd->itd_list, &stream->free_list);
4506
4507
4508 if (list_empty(&stream->td_list)) {
4509 list_splice_tail_init(&stream->free_list,
4510 &fotg210->cached_itd_list);
4511 start_free_itds(fotg210);
4512 }
4513
4514 return retval;
4515}
4516
4517static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
4518 gfp_t mem_flags)
4519{
4520 int status = -EINVAL;
4521 unsigned long flags;
4522 struct fotg210_iso_stream *stream;
4523
4524
4525 stream = iso_stream_find(fotg210, urb);
4526 if (unlikely(stream == NULL)) {
4527 fotg210_dbg(fotg210, "can't get iso stream\n");
4528 return -ENOMEM;
4529 }
4530 if (unlikely(urb->interval != stream->interval &&
4531 fotg210_port_speed(fotg210, 0) ==
4532 USB_PORT_STAT_HIGH_SPEED)) {
4533 fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n",
4534 stream->interval, urb->interval);
4535 goto done;
4536 }
4537
4538#ifdef FOTG210_URB_TRACE
4539 fotg210_dbg(fotg210,
4540 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
4541 __func__, urb->dev->devpath, urb,
4542 usb_pipeendpoint(urb->pipe),
4543 usb_pipein(urb->pipe) ? "in" : "out",
4544 urb->transfer_buffer_length,
4545 urb->number_of_packets, urb->interval,
4546 stream);
4547#endif
4548
4549
4550 status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
4551 if (unlikely(status < 0)) {
4552 fotg210_dbg(fotg210, "can't init itds\n");
4553 goto done;
4554 }
4555
4556
4557 spin_lock_irqsave(&fotg210->lock, flags);
4558 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
4559 status = -ESHUTDOWN;
4560 goto done_not_linked;
4561 }
4562 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
4563 if (unlikely(status))
4564 goto done_not_linked;
4565 status = iso_stream_schedule(fotg210, urb, stream);
4566 if (likely(status == 0))
4567 itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream);
4568 else
4569 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
4570done_not_linked:
4571 spin_unlock_irqrestore(&fotg210->lock, flags);
4572done:
4573 return status;
4574}
4575
4576static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame,
4577 unsigned now_frame, bool live)
4578{
4579 unsigned uf;
4580 bool modified;
4581 union fotg210_shadow q, *q_p;
4582 __hc32 type, *hw_p;
4583
4584
4585 q_p = &fotg210->pshadow[frame];
4586 hw_p = &fotg210->periodic[frame];
4587 q.ptr = q_p->ptr;
4588 type = Q_NEXT_TYPE(fotg210, *hw_p);
4589 modified = false;
4590
4591 while (q.ptr) {
4592 switch (hc32_to_cpu(fotg210, type)) {
4593 case Q_TYPE_ITD:
4594
4595
4596
4597
4598
4599 if (frame == now_frame && live) {
4600 rmb();
4601 for (uf = 0; uf < 8; uf++) {
4602 if (q.itd->hw_transaction[uf] &
4603 ITD_ACTIVE(fotg210))
4604 break;
4605 }
4606 if (uf < 8) {
4607 q_p = &q.itd->itd_next;
4608 hw_p = &q.itd->hw_next;
4609 type = Q_NEXT_TYPE(fotg210,
4610 q.itd->hw_next);
4611 q = *q_p;
4612 break;
4613 }
4614 }
4615
4616
4617
4618
4619
4620
4621 *q_p = q.itd->itd_next;
4622 *hw_p = q.itd->hw_next;
4623 type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
4624 wmb();
4625 modified = itd_complete(fotg210, q.itd);
4626 q = *q_p;
4627 break;
4628 default:
4629 fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
4630 type, frame, q.ptr);
4631
4632 case Q_TYPE_QH:
4633 case Q_TYPE_FSTN:
4634
4635 q.ptr = NULL;
4636 break;
4637 }
4638
4639
4640 if (unlikely(modified && fotg210->isoc_count > 0))
4641 return -EINVAL;
4642 }
4643 return 0;
4644}
4645
4646static void scan_isoc(struct fotg210_hcd *fotg210)
4647{
4648 unsigned uf, now_frame, frame, ret;
4649 unsigned fmask = fotg210->periodic_size - 1;
4650 bool live;
4651
4652
4653
4654
4655
4656
4657 if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
4658 uf = fotg210_read_frame_index(fotg210);
4659 now_frame = (uf >> 3) & fmask;
4660 live = true;
4661 } else {
4662 now_frame = (fotg210->next_frame - 1) & fmask;
4663 live = false;
4664 }
4665 fotg210->now_frame = now_frame;
4666
4667 frame = fotg210->next_frame;
4668 for (;;) {
4669 ret = 1;
4670 while (ret != 0)
4671 ret = scan_frame_queue(fotg210, frame,
4672 now_frame, live);
4673
4674
4675 if (frame == now_frame)
4676 break;
4677 frame = (frame + 1) & fmask;
4678 }
4679 fotg210->next_frame = now_frame;
4680}
4681
4682
4683
4684static ssize_t uframe_periodic_max_show(struct device *dev,
4685 struct device_attribute *attr, char *buf)
4686{
4687 struct fotg210_hcd *fotg210;
4688 int n;
4689
4690 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4691 n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
4692 return n;
4693}
4694
4695
4696static ssize_t uframe_periodic_max_store(struct device *dev,
4697 struct device_attribute *attr, const char *buf, size_t count)
4698{
4699 struct fotg210_hcd *fotg210;
4700 unsigned uframe_periodic_max;
4701 unsigned frame, uframe;
4702 unsigned short allocated_max;
4703 unsigned long flags;
4704 ssize_t ret;
4705
4706 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4707 if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
4708 return -EINVAL;
4709
4710 if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
4711 fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n",
4712 uframe_periodic_max);
4713 return -EINVAL;
4714 }
4715
4716 ret = -EINVAL;
4717
4718
4719
4720
4721
4722 spin_lock_irqsave(&fotg210->lock, flags);
4723
4724
4725
4726
4727
4728
4729 if (uframe_periodic_max < fotg210->uframe_periodic_max) {
4730 allocated_max = 0;
4731
4732 for (frame = 0; frame < fotg210->periodic_size; ++frame)
4733 for (uframe = 0; uframe < 7; ++uframe)
4734 allocated_max = max(allocated_max,
4735 periodic_usecs(fotg210, frame,
4736 uframe));
4737
4738 if (allocated_max > uframe_periodic_max) {
4739 fotg210_info(fotg210,
4740 "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n",
4741 allocated_max, uframe_periodic_max);
4742 goto out_unlock;
4743 }
4744 }
4745
4746
4747
4748 fotg210_info(fotg210,
4749 "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
4750 100 * uframe_periodic_max/125, uframe_periodic_max);
4751
4752 if (uframe_periodic_max != 100)
4753 fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
4754
4755 fotg210->uframe_periodic_max = uframe_periodic_max;
4756 ret = count;
4757
4758out_unlock:
4759 spin_unlock_irqrestore(&fotg210->lock, flags);
4760 return ret;
4761}
4762
4763static DEVICE_ATTR_RW(uframe_periodic_max);
4764
4765static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
4766{
4767 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
4768
4769 return device_create_file(controller, &dev_attr_uframe_periodic_max);
4770}
4771
4772static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
4773{
4774 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
4775
4776 device_remove_file(controller, &dev_attr_uframe_periodic_max);
4777}
4778
4779
4780
4781
4782static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
4783{
4784 u32 __iomem *status_reg = &fotg210->regs->port_status;
4785
4786 fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
4787}
4788
4789
4790
4791
4792static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
4793{
4794 fotg210_halt(fotg210);
4795
4796 spin_lock_irq(&fotg210->lock);
4797 fotg210->rh_state = FOTG210_RH_HALTED;
4798 fotg210_turn_off_all_ports(fotg210);
4799 spin_unlock_irq(&fotg210->lock);
4800}
4801
4802
4803
4804
4805
4806static void fotg210_shutdown(struct usb_hcd *hcd)
4807{
4808 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
4809
4810 spin_lock_irq(&fotg210->lock);
4811 fotg210->shutdown = true;
4812 fotg210->rh_state = FOTG210_RH_STOPPING;
4813 fotg210->enabled_hrtimer_events = 0;
4814 spin_unlock_irq(&fotg210->lock);
4815
4816 fotg210_silence_controller(fotg210);
4817
4818 hrtimer_cancel(&fotg210->hrtimer);
4819}
4820
4821
4822
4823
4824static void fotg210_work(struct fotg210_hcd *fotg210)
4825{
4826
4827
4828
4829
4830 if (fotg210->scanning) {
4831 fotg210->need_rescan = true;
4832 return;
4833 }
4834 fotg210->scanning = true;
4835
4836rescan:
4837 fotg210->need_rescan = false;
4838 if (fotg210->async_count)
4839 scan_async(fotg210);
4840 if (fotg210->intr_count > 0)
4841 scan_intr(fotg210);
4842 if (fotg210->isoc_count > 0)
4843 scan_isoc(fotg210);
4844 if (fotg210->need_rescan)
4845 goto rescan;
4846 fotg210->scanning = false;
4847
4848
4849
4850
4851
4852 turn_on_io_watchdog(fotg210);
4853}
4854
4855
4856
4857static void fotg210_stop(struct usb_hcd *hcd)
4858{
4859 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
4860
4861 fotg210_dbg(fotg210, "stop\n");
4862
4863
4864
4865 spin_lock_irq(&fotg210->lock);
4866 fotg210->enabled_hrtimer_events = 0;
4867 spin_unlock_irq(&fotg210->lock);
4868
4869 fotg210_quiesce(fotg210);
4870 fotg210_silence_controller(fotg210);
4871 fotg210_reset(fotg210);
4872
4873 hrtimer_cancel(&fotg210->hrtimer);
4874 remove_sysfs_files(fotg210);
4875 remove_debug_files(fotg210);
4876
4877
4878 spin_lock_irq(&fotg210->lock);
4879 end_free_itds(fotg210);
4880 spin_unlock_irq(&fotg210->lock);
4881 fotg210_mem_cleanup(fotg210);
4882
4883#ifdef FOTG210_STATS
4884 fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
4885 fotg210->stats.normal, fotg210->stats.error,
4886 fotg210->stats.iaa, fotg210->stats.lost_iaa);
4887 fotg210_dbg(fotg210, "complete %ld unlink %ld\n",
4888 fotg210->stats.complete, fotg210->stats.unlink);
4889#endif
4890
4891 dbg_status(fotg210, "fotg210_stop completed",
4892 fotg210_readl(fotg210, &fotg210->regs->status));
4893}
4894
4895
4896static int hcd_fotg210_init(struct usb_hcd *hcd)
4897{
4898 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
4899 u32 temp;
4900 int retval;
4901 u32 hcc_params;
4902 struct fotg210_qh_hw *hw;
4903
4904 spin_lock_init(&fotg210->lock);
4905
4906
4907
4908
4909 fotg210->need_io_watchdog = 1;
4910
4911 hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
4912 fotg210->hrtimer.function = fotg210_hrtimer_func;
4913 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
4914
4915 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
4916
4917
4918
4919
4920
4921 fotg210->uframe_periodic_max = 100;
4922
4923
4924
4925
4926
4927 fotg210->periodic_size = DEFAULT_I_TDPS;
4928 INIT_LIST_HEAD(&fotg210->intr_qh_list);
4929 INIT_LIST_HEAD(&fotg210->cached_itd_list);
4930
4931 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
4932
4933 switch (FOTG210_TUNE_FLS) {
4934 case 0:
4935 fotg210->periodic_size = 1024;
4936 break;
4937 case 1:
4938 fotg210->periodic_size = 512;
4939 break;
4940 case 2:
4941 fotg210->periodic_size = 256;
4942 break;
4943 default:
4944 BUG();
4945 }
4946 }
4947 retval = fotg210_mem_init(fotg210, GFP_KERNEL);
4948 if (retval < 0)
4949 return retval;
4950
4951
4952 fotg210->i_thresh = 2;
4953
4954
4955
4956
4957
4958
4959
4960
4961 fotg210->async->qh_next.qh = NULL;
4962 hw = fotg210->async->hw;
4963 hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma);
4964 hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD);
4965 hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
4966 hw->hw_qtd_next = FOTG210_LIST_END(fotg210);
4967 fotg210->async->qh_state = QH_STATE_LINKED;
4968 hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma);
4969
4970
4971 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
4972 log2_irq_thresh = 0;
4973 temp = 1 << (16 + log2_irq_thresh);
4974 if (HCC_CANPARK(hcc_params)) {
4975
4976
4977
4978
4979
4980
4981
4982 if (park) {
4983 park = min_t(unsigned, park, 3);
4984 temp |= CMD_PARK;
4985 temp |= park << 8;
4986 }
4987 fotg210_dbg(fotg210, "park %d\n", park);
4988 }
4989 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
4990
4991 temp &= ~(3 << 2);
4992 temp |= (FOTG210_TUNE_FLS << 2);
4993 }
4994 fotg210->command = temp;
4995
4996
4997 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
4998 hcd->self.sg_tablesize = ~0;
4999 return 0;
5000}
5001
5002
5003static int fotg210_run(struct usb_hcd *hcd)
5004{
5005 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5006 u32 temp;
5007 u32 hcc_params;
5008
5009 hcd->uses_new_polling = 1;
5010
5011
5012
5013 fotg210_writel(fotg210, fotg210->periodic_dma,
5014 &fotg210->regs->frame_list);
5015 fotg210_writel(fotg210, (u32)fotg210->async->qh_dma,
5016 &fotg210->regs->async_next);
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
5031
5032
5033
5034
5035
5036 fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
5037 fotg210->command |= CMD_RUN;
5038 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
5039 dbg_cmd(fotg210, "init", fotg210->command);
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055 down_write(&ehci_cf_port_reset_rwsem);
5056 fotg210->rh_state = FOTG210_RH_RUNNING;
5057
5058 fotg210_readl(fotg210, &fotg210->regs->command);
5059 usleep_range(5000, 10000);
5060 up_write(&ehci_cf_port_reset_rwsem);
5061 fotg210->last_periodic_enable = ktime_get_real();
5062
5063 temp = HC_VERSION(fotg210,
5064 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
5065 fotg210_info(fotg210,
5066 "USB %x.%x started, EHCI %x.%02x\n",
5067 ((fotg210->sbrn & 0xf0) >> 4), (fotg210->sbrn & 0x0f),
5068 temp >> 8, temp & 0xff);
5069
5070 fotg210_writel(fotg210, INTR_MASK,
5071 &fotg210->regs->intr_enable);
5072
5073
5074
5075
5076
5077 create_debug_files(fotg210);
5078 create_sysfs_files(fotg210);
5079
5080 return 0;
5081}
5082
5083static int fotg210_setup(struct usb_hcd *hcd)
5084{
5085 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5086 int retval;
5087
5088 fotg210->regs = (void __iomem *)fotg210->caps +
5089 HC_LENGTH(fotg210,
5090 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
5091 dbg_hcs_params(fotg210, "reset");
5092 dbg_hcc_params(fotg210, "reset");
5093
5094
5095 fotg210->hcs_params = fotg210_readl(fotg210,
5096 &fotg210->caps->hcs_params);
5097
5098 fotg210->sbrn = HCD_USB2;
5099
5100
5101 retval = hcd_fotg210_init(hcd);
5102 if (retval)
5103 return retval;
5104
5105 retval = fotg210_halt(fotg210);
5106 if (retval)
5107 return retval;
5108
5109 fotg210_reset(fotg210);
5110
5111 return 0;
5112}
5113
5114static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
5115{
5116 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5117 u32 status, masked_status, pcd_status = 0, cmd;
5118 int bh;
5119
5120 spin_lock(&fotg210->lock);
5121
5122 status = fotg210_readl(fotg210, &fotg210->regs->status);
5123
5124
5125 if (status == ~(u32) 0) {
5126 fotg210_dbg(fotg210, "device removed\n");
5127 goto dead;
5128 }
5129
5130
5131
5132
5133
5134 masked_status = status & (INTR_MASK | STS_FLR);
5135
5136
5137 if (!masked_status ||
5138 unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
5139 spin_unlock(&fotg210->lock);
5140 return IRQ_NONE;
5141 }
5142
5143
5144 fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
5145 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
5146 bh = 0;
5147
5148
5149 dbg_status(fotg210, "irq", status);
5150
5151
5152
5153
5154 if (likely((status & (STS_INT|STS_ERR)) != 0)) {
5155 if (likely((status & STS_ERR) == 0))
5156 COUNT(fotg210->stats.normal);
5157 else
5158 COUNT(fotg210->stats.error);
5159 bh = 1;
5160 }
5161
5162
5163 if (status & STS_IAA) {
5164
5165
5166 fotg210->enabled_hrtimer_events &=
5167 ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
5168
5169
5170
5171
5172
5173
5174
5175
5176 if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
5177 ++fotg210->next_hrtimer_event;
5178
5179
5180 if (cmd & CMD_IAAD)
5181 fotg210_dbg(fotg210, "IAA with IAAD still set?\n");
5182 if (fotg210->async_iaa) {
5183 COUNT(fotg210->stats.iaa);
5184 end_unlink_async(fotg210);
5185 } else
5186 fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
5187 }
5188
5189
5190 if (status & STS_PCD) {
5191 int pstatus;
5192 u32 __iomem *status_reg = &fotg210->regs->port_status;
5193
5194
5195 pcd_status = status;
5196
5197
5198 if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
5199 usb_hcd_resume_root_hub(hcd);
5200
5201 pstatus = fotg210_readl(fotg210, status_reg);
5202
5203 if (test_bit(0, &fotg210->suspended_ports) &&
5204 ((pstatus & PORT_RESUME) ||
5205 !(pstatus & PORT_SUSPEND)) &&
5206 (pstatus & PORT_PE) &&
5207 fotg210->reset_done[0] == 0) {
5208
5209
5210
5211
5212
5213
5214 fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25);
5215 set_bit(0, &fotg210->resuming_ports);
5216 fotg210_dbg(fotg210, "port 1 remote wakeup\n");
5217 mod_timer(&hcd->rh_timer, fotg210->reset_done[0]);
5218 }
5219 }
5220
5221
5222 if (unlikely((status & STS_FATAL) != 0)) {
5223 fotg210_err(fotg210, "fatal error\n");
5224 dbg_cmd(fotg210, "fatal", cmd);
5225 dbg_status(fotg210, "fatal", status);
5226dead:
5227 usb_hc_died(hcd);
5228
5229
5230 fotg210->shutdown = true;
5231 fotg210->rh_state = FOTG210_RH_STOPPING;
5232 fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
5233 fotg210_writel(fotg210, fotg210->command,
5234 &fotg210->regs->command);
5235 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
5236 fotg210_handle_controller_death(fotg210);
5237
5238
5239 bh = 0;
5240 }
5241
5242 if (bh)
5243 fotg210_work(fotg210);
5244 spin_unlock(&fotg210->lock);
5245 if (pcd_status)
5246 usb_hcd_poll_rh_status(hcd);
5247 return IRQ_HANDLED;
5248}
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
5262 gfp_t mem_flags)
5263{
5264 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5265 struct list_head qtd_list;
5266
5267 INIT_LIST_HEAD(&qtd_list);
5268
5269 switch (usb_pipetype(urb->pipe)) {
5270 case PIPE_CONTROL:
5271
5272
5273
5274 if (urb->transfer_buffer_length > (16 * 1024))
5275 return -EMSGSIZE;
5276
5277
5278 default:
5279 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5280 return -ENOMEM;
5281 return submit_async(fotg210, urb, &qtd_list, mem_flags);
5282
5283 case PIPE_INTERRUPT:
5284 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5285 return -ENOMEM;
5286 return intr_submit(fotg210, urb, &qtd_list, mem_flags);
5287
5288 case PIPE_ISOCHRONOUS:
5289 return itd_submit(fotg210, urb, mem_flags);
5290 }
5291}
5292
5293
5294
5295
5296
5297static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
5298{
5299 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5300 struct fotg210_qh *qh;
5301 unsigned long flags;
5302 int rc;
5303
5304 spin_lock_irqsave(&fotg210->lock, flags);
5305 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
5306 if (rc)
5307 goto done;
5308
5309 switch (usb_pipetype(urb->pipe)) {
5310
5311
5312 default:
5313 qh = (struct fotg210_qh *) urb->hcpriv;
5314 if (!qh)
5315 break;
5316 switch (qh->qh_state) {
5317 case QH_STATE_LINKED:
5318 case QH_STATE_COMPLETING:
5319 start_unlink_async(fotg210, qh);
5320 break;
5321 case QH_STATE_UNLINK:
5322 case QH_STATE_UNLINK_WAIT:
5323
5324 break;
5325 case QH_STATE_IDLE:
5326
5327 qh_completions(fotg210, qh);
5328 break;
5329 }
5330 break;
5331
5332 case PIPE_INTERRUPT:
5333 qh = (struct fotg210_qh *) urb->hcpriv;
5334 if (!qh)
5335 break;
5336 switch (qh->qh_state) {
5337 case QH_STATE_LINKED:
5338 case QH_STATE_COMPLETING:
5339 start_unlink_intr(fotg210, qh);
5340 break;
5341 case QH_STATE_IDLE:
5342 qh_completions(fotg210, qh);
5343 break;
5344 default:
5345 fotg210_dbg(fotg210, "bogus qh %p state %d\n",
5346 qh, qh->qh_state);
5347 goto done;
5348 }
5349 break;
5350
5351 case PIPE_ISOCHRONOUS:
5352
5353
5354
5355
5356 break;
5357 }
5358done:
5359 spin_unlock_irqrestore(&fotg210->lock, flags);
5360 return rc;
5361}
5362
5363
5364
5365static void fotg210_endpoint_disable(struct usb_hcd *hcd,
5366 struct usb_host_endpoint *ep)
5367{
5368 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5369 unsigned long flags;
5370 struct fotg210_qh *qh, *tmp;
5371
5372
5373
5374
5375rescan:
5376 spin_lock_irqsave(&fotg210->lock, flags);
5377 qh = ep->hcpriv;
5378 if (!qh)
5379 goto done;
5380
5381
5382
5383
5384 if (qh->hw == NULL) {
5385 struct fotg210_iso_stream *stream = ep->hcpriv;
5386
5387 if (!list_empty(&stream->td_list))
5388 goto idle_timeout;
5389
5390
5391 kfree(stream);
5392 goto done;
5393 }
5394
5395 if (fotg210->rh_state < FOTG210_RH_RUNNING)
5396 qh->qh_state = QH_STATE_IDLE;
5397 switch (qh->qh_state) {
5398 case QH_STATE_LINKED:
5399 case QH_STATE_COMPLETING:
5400 for (tmp = fotg210->async->qh_next.qh;
5401 tmp && tmp != qh;
5402 tmp = tmp->qh_next.qh)
5403 continue;
5404
5405
5406
5407 if (tmp)
5408 start_unlink_async(fotg210, qh);
5409
5410 case QH_STATE_UNLINK:
5411 case QH_STATE_UNLINK_WAIT:
5412idle_timeout:
5413 spin_unlock_irqrestore(&fotg210->lock, flags);
5414 schedule_timeout_uninterruptible(1);
5415 goto rescan;
5416 case QH_STATE_IDLE:
5417 if (qh->clearing_tt)
5418 goto idle_timeout;
5419 if (list_empty(&qh->qtd_list)) {
5420 qh_destroy(fotg210, qh);
5421 break;
5422 }
5423
5424 default:
5425
5426
5427
5428 fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n",
5429 qh, ep->desc.bEndpointAddress, qh->qh_state,
5430 list_empty(&qh->qtd_list) ? "" : "(has tds)");
5431 break;
5432 }
5433done:
5434 ep->hcpriv = NULL;
5435 spin_unlock_irqrestore(&fotg210->lock, flags);
5436}
5437
5438static void fotg210_endpoint_reset(struct usb_hcd *hcd,
5439 struct usb_host_endpoint *ep)
5440{
5441 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5442 struct fotg210_qh *qh;
5443 int eptype = usb_endpoint_type(&ep->desc);
5444 int epnum = usb_endpoint_num(&ep->desc);
5445 int is_out = usb_endpoint_dir_out(&ep->desc);
5446 unsigned long flags;
5447
5448 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
5449 return;
5450
5451 spin_lock_irqsave(&fotg210->lock, flags);
5452 qh = ep->hcpriv;
5453
5454
5455
5456
5457
5458
5459 if (qh) {
5460 usb_settoggle(qh->dev, epnum, is_out, 0);
5461 if (!list_empty(&qh->qtd_list)) {
5462 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
5463 } else if (qh->qh_state == QH_STATE_LINKED ||
5464 qh->qh_state == QH_STATE_COMPLETING) {
5465
5466
5467
5468
5469
5470 if (eptype == USB_ENDPOINT_XFER_BULK)
5471 start_unlink_async(fotg210, qh);
5472 else
5473 start_unlink_intr(fotg210, qh);
5474 }
5475 }
5476 spin_unlock_irqrestore(&fotg210->lock, flags);
5477}
5478
5479static int fotg210_get_frame(struct usb_hcd *hcd)
5480{
5481 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5482
5483 return (fotg210_read_frame_index(fotg210) >> 3) %
5484 fotg210->periodic_size;
5485}
5486
5487
5488
5489
5490
5491
5492MODULE_DESCRIPTION(DRIVER_DESC);
5493MODULE_AUTHOR(DRIVER_AUTHOR);
5494MODULE_LICENSE("GPL");
5495
5496static const struct hc_driver fotg210_fotg210_hc_driver = {
5497 .description = hcd_name,
5498 .product_desc = "Faraday USB2.0 Host Controller",
5499 .hcd_priv_size = sizeof(struct fotg210_hcd),
5500
5501
5502
5503
5504 .irq = fotg210_irq,
5505 .flags = HCD_MEMORY | HCD_USB2,
5506
5507
5508
5509
5510 .reset = hcd_fotg210_init,
5511 .start = fotg210_run,
5512 .stop = fotg210_stop,
5513 .shutdown = fotg210_shutdown,
5514
5515
5516
5517
5518 .urb_enqueue = fotg210_urb_enqueue,
5519 .urb_dequeue = fotg210_urb_dequeue,
5520 .endpoint_disable = fotg210_endpoint_disable,
5521 .endpoint_reset = fotg210_endpoint_reset,
5522
5523
5524
5525
5526 .get_frame_number = fotg210_get_frame,
5527
5528
5529
5530
5531 .hub_status_data = fotg210_hub_status_data,
5532 .hub_control = fotg210_hub_control,
5533 .bus_suspend = fotg210_bus_suspend,
5534 .bus_resume = fotg210_bus_resume,
5535
5536 .relinquish_port = fotg210_relinquish_port,
5537 .port_handed_over = fotg210_port_handed_over,
5538
5539 .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
5540};
5541
5542static void fotg210_init(struct fotg210_hcd *fotg210)
5543{
5544 u32 value;
5545
5546 iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
5547 &fotg210->regs->gmir);
5548
5549 value = ioread32(&fotg210->regs->otgcsr);
5550 value &= ~OTGCSR_A_BUS_DROP;
5551 value |= OTGCSR_A_BUS_REQ;
5552 iowrite32(value, &fotg210->regs->otgcsr);
5553}
5554
5555
5556
5557
5558
5559
5560
5561
5562static int fotg210_hcd_probe(struct platform_device *pdev)
5563{
5564 struct device *dev = &pdev->dev;
5565 struct usb_hcd *hcd;
5566 struct resource *res;
5567 int irq;
5568 int retval = -ENODEV;
5569 struct fotg210_hcd *fotg210;
5570
5571 if (usb_disabled())
5572 return -ENODEV;
5573
5574 pdev->dev.power.power_state = PMSG_ON;
5575
5576 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
5577 if (!res) {
5578 dev_err(dev, "Found HC with no IRQ. Check %s setup!\n",
5579 dev_name(dev));
5580 return -ENODEV;
5581 }
5582
5583 irq = res->start;
5584
5585 hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
5586 dev_name(dev));
5587 if (!hcd) {
5588 dev_err(dev, "failed to create hcd with err %d\n", retval);
5589 retval = -ENOMEM;
5590 goto fail_create_hcd;
5591 }
5592
5593 hcd->has_tt = 1;
5594
5595 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5596 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
5597 if (IS_ERR(hcd->regs)) {
5598 retval = PTR_ERR(hcd->regs);
5599 goto failed;
5600 }
5601
5602 hcd->rsrc_start = res->start;
5603 hcd->rsrc_len = resource_size(res);
5604
5605 fotg210 = hcd_to_fotg210(hcd);
5606
5607 fotg210->caps = hcd->regs;
5608
5609 retval = fotg210_setup(hcd);
5610 if (retval)
5611 goto failed;
5612
5613 fotg210_init(fotg210);
5614
5615 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
5616 if (retval) {
5617 dev_err(dev, "failed to add hcd with err %d\n", retval);
5618 goto failed;
5619 }
5620 device_wakeup_enable(hcd->self.controller);
5621
5622 return retval;
5623
5624failed:
5625 usb_put_hcd(hcd);
5626fail_create_hcd:
5627 dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
5628 return retval;
5629}
5630
5631
5632
5633
5634
5635
5636static int fotg210_hcd_remove(struct platform_device *pdev)
5637{
5638 struct device *dev = &pdev->dev;
5639 struct usb_hcd *hcd = dev_get_drvdata(dev);
5640
5641 if (!hcd)
5642 return 0;
5643
5644 usb_remove_hcd(hcd);
5645 usb_put_hcd(hcd);
5646
5647 return 0;
5648}
5649
5650static struct platform_driver fotg210_hcd_driver = {
5651 .driver = {
5652 .name = "fotg210-hcd",
5653 },
5654 .probe = fotg210_hcd_probe,
5655 .remove = fotg210_hcd_remove,
5656};
5657
5658static int __init fotg210_hcd_init(void)
5659{
5660 int retval = 0;
5661
5662 if (usb_disabled())
5663 return -ENODEV;
5664
5665 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
5666 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5667 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
5668 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
5669 pr_warn("Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
5670
5671 pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd\n",
5672 hcd_name, sizeof(struct fotg210_qh),
5673 sizeof(struct fotg210_qtd),
5674 sizeof(struct fotg210_itd));
5675
5676 fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
5677
5678 retval = platform_driver_register(&fotg210_hcd_driver);
5679 if (retval < 0)
5680 goto clean;
5681 return retval;
5682
5683clean:
5684 debugfs_remove(fotg210_debug_root);
5685 fotg210_debug_root = NULL;
5686
5687 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5688 return retval;
5689}
5690module_init(fotg210_hcd_init);
5691
5692static void __exit fotg210_hcd_cleanup(void)
5693{
5694 platform_driver_unregister(&fotg210_hcd_driver);
5695 debugfs_remove(fotg210_debug_root);
5696 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5697}
5698module_exit(fotg210_hcd_cleanup);
5699