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