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