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