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