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