1 /* Faraday FOTG210 EHCI-like driver
3 * Copyright (c) 2013 Faraday Technology Corporation
5 * Author: Yuan-Hsin Chen <yhchen@faraday-tech.com>
6 * Feng-Hsin Chiang <john453@faraday-tech.com>
7 * Po-Yu Chuang <ratbert.chuang@gmail.com>
9 * Most of code borrowed from the Linux-3.7 EHCI driver
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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>
48 #include <asm/byteorder.h>
50 #include <asm/unaligned.h>
52 #define DRIVER_AUTHOR "Yuan-Hsin Chen"
53 #define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
54 static const char hcd_name[] = "fotg210_hcd";
56 #undef FOTG210_URB_TRACE
59 /* magic numbers that can affect system performance */
60 #define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
61 #define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
62 #define FOTG210_TUNE_RL_TT 0
63 #define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
64 #define FOTG210_TUNE_MULT_TT 1
66 /* Some drivers think it's safe to schedule isochronous transfers more than 256
67 * ms into the future (partly as a result of an old bug in the scheduling
68 * code). In an attempt to avoid trouble, we will use a minimum scheduling
69 * length of 512 frames instead of 256.
71 #define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
73 /* Initial IRQ latency: faster than hw default */
74 static int log2_irq_thresh; /* 0 to 6 */
75 module_param(log2_irq_thresh, int, S_IRUGO);
76 MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
78 /* initial park setting: slower than hw default */
80 module_param(park, uint, S_IRUGO);
81 MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
83 /* for link power management(LPM) feature */
84 static unsigned int hird;
85 module_param(hird, int, S_IRUGO);
86 MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
88 #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
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)
101 /* check the values in the HCSPARAMS register (host controller _Structural_
102 * parameters) see EHCI spec, Table 2-4 for each value
104 static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
106 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
108 fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
109 HCS_N_PORTS(params));
112 /* check the values in the HCCPARAMS register (host controller _Capability_
113 * parameters) see EHCI Spec, Table 2-5 for each value
115 static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
117 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
119 fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label,
121 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
122 HCC_CANPARK(params) ? " park" : "");
125 static void __maybe_unused
126 dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
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]));
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]));
141 static void __maybe_unused
142 dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
144 struct fotg210_qh_hw *hw = qh->hw;
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,
150 dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
153 static void __maybe_unused
154 dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
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),
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]));
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]));
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]);
187 static int __maybe_unused
188 dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
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" : "");
204 static int __maybe_unused
205 dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
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" : "");
217 static const char *const fls_strings[] = { "1024", "512", "256", "??" };
219 static int dbg_command_buf(char *buf, unsigned len, const char *label,
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");
236 static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
241 /* signaling state */
242 switch (status & (3 << 10)) {
248 break; /* low speed */
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, /*device address */
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" : "");
272 /* functions have the "wrong" filename when they're output... */
273 #define dbg_status(fotg210, label, status) { \
275 dbg_status_buf(_buf, sizeof(_buf), label, status); \
276 fotg210_dbg(fotg210, "%s\n", _buf); \
279 #define dbg_cmd(fotg210, label, command) { \
281 dbg_command_buf(_buf, sizeof(_buf), label, command); \
282 fotg210_dbg(fotg210, "%s\n", _buf); \
285 #define dbg_port(fotg210, label, port, status) { \
287 fotg210_dbg(fotg210, "%s\n", \
288 dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
291 /* troubleshooting help: expose state in debugfs */
292 static int debug_async_open(struct inode *, struct file *);
293 static int debug_periodic_open(struct inode *, struct file *);
294 static int debug_registers_open(struct inode *, struct file *);
295 static int debug_async_open(struct inode *, struct file *);
297 static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
298 static int debug_close(struct inode *, struct file *);
300 static 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,
307 static 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,
314 static 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,
322 static struct dentry *fotg210_debug_root;
324 struct debug_buffer {
325 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
327 struct mutex mutex; /* protect filling of buffer */
328 size_t count; /* number of characters filled into buffer */
333 static inline char speed_char(u32 scratch)
335 switch (scratch & (3 << 12)) {
350 static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
352 __u32 v = hc32_to_cpu(fotg210, token);
354 if (v & QTD_STS_ACTIVE)
356 if (v & QTD_STS_HALT)
358 if (!IS_SHORT_READ(v))
360 /* tries to advance through hw_alt_next */
364 static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
365 char **nextp, unsigned *sizep)
369 struct fotg210_qtd *td;
371 unsigned size = *sizep;
374 __le32 list_end = FOTG210_LIST_END(fotg210);
375 struct fotg210_qh_hw *hw = qh->hw;
377 if (hw->hw_qtd_next == list_end) /* NEC does this */
380 mark = token_mark(fotg210, hw->hw_token);
381 if (mark == '/') { /* qh_alt_next controls qh advance? */
382 if ((hw->hw_alt_next & QTD_MASK(fotg210)) ==
383 fotg210->async->hw->hw_alt_next)
384 mark = '#'; /* blocked */
385 else if (hw->hw_alt_next == list_end)
386 mark = '.'; /* use hw_qtd_next */
387 /* else alt_next points to some other qtd */
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,
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)
400 (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
404 /* hc may be modifying the list as we read it ... */
405 list_for_each_entry(td, &qh->qtd_list, qtd_list) {
406 scratch = hc32_to_cpup(fotg210, &td->hw_token);
408 if (hw_curr == td->qtd_dma)
410 else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
412 else if (QTD_LENGTH(scratch)) {
413 if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
415 else if (td->hw_alt_next != list_end)
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) {
435 (scratch >> 16) & 0x7fff,
446 temp = snprintf(next, size, "\n");
458 static ssize_t fill_async_buffer(struct debug_buffer *buf)
461 struct fotg210_hcd *fotg210;
465 struct fotg210_qh *qh;
467 hcd = bus_to_hcd(buf->bus);
468 fotg210 = hcd_to_fotg210(hcd);
469 next = buf->output_buf;
470 size = buf->alloc_size;
474 /* dumps a snapshot of the async schedule.
475 * usually empty except for long-term bulk reads, or head.
476 * one QH per line, and TDs we know about
478 spin_lock_irqsave(&fotg210->lock, flags);
479 for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
481 qh_lines(fotg210, qh, &next, &size);
482 if (fotg210->async_unlink && size > 0) {
483 temp = scnprintf(next, size, "\nunlink =\n");
487 for (qh = fotg210->async_unlink; size > 0 && qh;
488 qh = qh->unlink_next)
489 qh_lines(fotg210, qh, &next, &size);
491 spin_unlock_irqrestore(&fotg210->lock, flags);
493 return strlen(buf->output_buf);
496 /* count tds, get ep direction */
497 static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
498 struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
500 u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
501 struct fotg210_qtd *qtd;
505 /* count tds, get ep direction */
506 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
508 switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
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);
524 #define DBG_SCHED_LIMIT 64
525 static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
528 struct fotg210_hcd *fotg210;
530 union fotg210_shadow p, *seen;
531 unsigned temp, size, seen_count;
536 seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
542 hcd = bus_to_hcd(buf->bus);
543 fotg210 = hcd_to_fotg210(hcd);
544 next = buf->output_buf;
545 size = buf->alloc_size;
547 temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
551 /* dump a snapshot of the periodic schedule.
552 * iso changes, interrupt usually doesn't.
554 spin_lock_irqsave(&fotg210->lock, flags);
555 for (i = 0; i < fotg210->periodic_size; i++) {
556 p = fotg210->pshadow[i];
560 tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);
562 temp = scnprintf(next, size, "%4d: ", i);
567 struct fotg210_qh_hw *hw;
569 switch (hc32_to_cpu(fotg210, tag)) {
572 temp = scnprintf(next, size, " qh%d-%04x/%p",
574 hc32_to_cpup(fotg210,
577 & (QH_CMASK | QH_SMASK),
581 /* don't repeat what follows this qh */
582 for (temp = 0; temp < seen_count; temp++) {
583 if (seen[temp].ptr != p.ptr)
585 if (p.qh->qh_next.ptr) {
586 temp = scnprintf(next, size,
593 /* show more info the first time around */
594 if (temp == seen_count) {
595 temp = output_buf_tds_dir(next,
599 if (seen_count < DBG_SCHED_LIMIT)
600 seen[seen_count++].qh = p.qh;
603 tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
607 temp = scnprintf(next, size,
609 p.fstn->hw_prev, p.fstn);
610 tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
611 p = p.fstn->fstn_next;
614 temp = scnprintf(next, size,
616 tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
624 temp = scnprintf(next, size, "\n");
628 spin_unlock_irqrestore(&fotg210->lock, flags);
631 return buf->alloc_size - size;
633 #undef DBG_SCHED_LIMIT
635 static const char *rh_state_string(struct fotg210_hcd *fotg210)
637 switch (fotg210->rh_state) {
638 case FOTG210_RH_HALTED:
640 case FOTG210_RH_SUSPENDED:
642 case FOTG210_RH_RUNNING:
644 case FOTG210_RH_STOPPING:
650 static ssize_t fill_registers_buffer(struct debug_buffer *buf)
653 struct fotg210_hcd *fotg210;
655 unsigned temp, size, i;
656 char *next, scratch[80];
657 static const char fmt[] = "%*s\n";
658 static const char label[] = "";
660 hcd = bus_to_hcd(buf->bus);
661 fotg210 = hcd_to_fotg210(hcd);
662 next = buf->output_buf;
663 size = buf->alloc_size;
665 spin_lock_irqsave(&fotg210->lock, flags);
667 if (!HCD_HW_ACCESSIBLE(hcd)) {
668 size = scnprintf(next, size,
669 "bus %s, device %s\n"
671 "SUSPENDED(no register access)\n",
672 hcd->self.controller->bus->name,
673 dev_name(hcd->self.controller),
678 /* Capability Registers */
679 i = HC_VERSION(fotg210, fotg210_readl(fotg210,
680 &fotg210->caps->hc_capbase));
681 temp = scnprintf(next, size,
682 "bus %s, device %s\n"
684 "EHCI %x.%02x, rh state %s\n",
685 hcd->self.controller->bus->name,
686 dev_name(hcd->self.controller),
688 i >> 8, i & 0x0ff, rh_state_string(fotg210));
692 /* FIXME interpret both types of params */
693 i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
694 temp = scnprintf(next, size, "structural params 0x%08x\n", i);
698 i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
699 temp = scnprintf(next, size, "capability params 0x%08x\n", i);
703 /* Operational Registers */
704 temp = dbg_status_buf(scratch, sizeof(scratch), label,
705 fotg210_readl(fotg210, &fotg210->regs->status));
706 temp = scnprintf(next, size, fmt, temp, scratch);
710 temp = dbg_command_buf(scratch, sizeof(scratch), label,
711 fotg210_readl(fotg210, &fotg210->regs->command));
712 temp = scnprintf(next, size, fmt, temp, scratch);
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);
722 temp = scnprintf(next, size, "uframe %04x\n",
723 fotg210_read_frame_index(fotg210));
727 if (fotg210->async_unlink) {
728 temp = scnprintf(next, size, "async unlink qh %p\n",
729 fotg210->async_unlink);
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);
742 temp = scnprintf(next, size, "complete %ld unlink %ld\n",
743 fotg210->stats.complete, fotg210->stats.unlink);
749 spin_unlock_irqrestore(&fotg210->lock, flags);
751 return buf->alloc_size - size;
754 static struct debug_buffer
755 *alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
757 struct debug_buffer *buf;
759 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
763 buf->fill_func = fill_func;
764 mutex_init(&buf->mutex);
765 buf->alloc_size = PAGE_SIZE;
771 static int fill_buffer(struct debug_buffer *buf)
775 if (!buf->output_buf)
776 buf->output_buf = vmalloc(buf->alloc_size);
778 if (!buf->output_buf) {
783 ret = buf->fill_func(buf);
794 static ssize_t debug_output(struct file *file, char __user *user_buf,
795 size_t len, loff_t *offset)
797 struct debug_buffer *buf = file->private_data;
800 mutex_lock(&buf->mutex);
801 if (buf->count == 0) {
802 ret = fill_buffer(buf);
804 mutex_unlock(&buf->mutex);
808 mutex_unlock(&buf->mutex);
810 ret = simple_read_from_buffer(user_buf, len, offset,
811 buf->output_buf, buf->count);
818 static int debug_close(struct inode *inode, struct file *file)
820 struct debug_buffer *buf = file->private_data;
823 vfree(buf->output_buf);
829 static int debug_async_open(struct inode *inode, struct file *file)
831 file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
833 return file->private_data ? 0 : -ENOMEM;
836 static int debug_periodic_open(struct inode *inode, struct file *file)
838 struct debug_buffer *buf;
840 buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
844 buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
845 file->private_data = buf;
849 static int debug_registers_open(struct inode *inode, struct file *file)
851 file->private_data = alloc_buffer(inode->i_private,
852 fill_registers_buffer);
854 return file->private_data ? 0 : -ENOMEM;
857 static inline void create_debug_files(struct fotg210_hcd *fotg210)
859 struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
861 fotg210->debug_dir = debugfs_create_dir(bus->bus_name,
863 if (!fotg210->debug_dir)
866 if (!debugfs_create_file("async", S_IRUGO, fotg210->debug_dir, bus,
870 if (!debugfs_create_file("periodic", S_IRUGO, fotg210->debug_dir, bus,
871 &debug_periodic_fops))
874 if (!debugfs_create_file("registers", S_IRUGO, fotg210->debug_dir, bus,
875 &debug_registers_fops))
881 debugfs_remove_recursive(fotg210->debug_dir);
884 static inline void remove_debug_files(struct fotg210_hcd *fotg210)
886 debugfs_remove_recursive(fotg210->debug_dir);
889 /* handshake - spin reading hc until handshake completes or fails
890 * @ptr: address of hc register to be read
891 * @mask: bits to look at in result of read
892 * @done: value of those bits when handshake succeeds
893 * @usec: timeout in microseconds
895 * Returns negative errno, or zero on success
897 * Success happens when the "mask" bits have the specified value (hardware
898 * handshake done). There are two failure modes: "usec" have passed (major
899 * hardware flakeout), or the register reads as all-ones (hardware removed).
901 * That last failure should_only happen in cases like physical cardbus eject
902 * before driver shutdown. But it also seems to be caused by bugs in cardbus
903 * bridge shutdown: shutting down the bridge before the devices using it.
905 static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
906 u32 mask, u32 done, int usec)
911 result = fotg210_readl(fotg210, ptr);
912 if (result == ~(u32)0) /* card removed */
923 /* Force HC to halt state from unknown (EHCI spec section 2.3).
924 * Must be called with interrupts enabled and the lock not held.
926 static int fotg210_halt(struct fotg210_hcd *fotg210)
930 spin_lock_irq(&fotg210->lock);
932 /* disable any irqs left enabled by previous code */
933 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
936 * This routine gets called during probe before fotg210->command
937 * has been initialized, so we can't rely on its value.
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);
944 spin_unlock_irq(&fotg210->lock);
945 synchronize_irq(fotg210_to_hcd(fotg210)->irq);
947 return handshake(fotg210, &fotg210->regs->status,
948 STS_HALT, STS_HALT, 16 * 125);
951 /* Reset a non-running (STS_HALT == 1) controller.
952 * Must be called with interrupts enabled and the lock not held.
954 static int fotg210_reset(struct fotg210_hcd *fotg210)
957 u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
959 /* If the EHCI debug controller is active, special care must be
960 * taken before and after a host controller reset
962 if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
963 fotg210->debug = NULL;
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);
977 dbgp_external_startup(fotg210_to_hcd(fotg210));
979 fotg210->port_c_suspend = fotg210->suspended_ports =
980 fotg210->resuming_ports = 0;
984 /* Idle the controller (turn off the schedules).
985 * Must be called with interrupts enabled and the lock not held.
987 static void fotg210_quiesce(struct fotg210_hcd *fotg210)
991 if (fotg210->rh_state != FOTG210_RH_RUNNING)
994 /* wait for any schedule enables/disables to take effect */
995 temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
996 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
999 /* then disable anything that's still active */
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);
1005 /* hardware can take 16 microframes to turn off ... */
1006 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
1010 static void end_unlink_async(struct fotg210_hcd *fotg210);
1011 static void unlink_empty_async(struct fotg210_hcd *fotg210);
1012 static void fotg210_work(struct fotg210_hcd *fotg210);
1013 static void start_unlink_intr(struct fotg210_hcd *fotg210,
1014 struct fotg210_qh *qh);
1015 static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
1017 /* Set a bit in the USBCMD register */
1018 static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
1020 fotg210->command |= bit;
1021 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1023 /* unblock posted write */
1024 fotg210_readl(fotg210, &fotg210->regs->command);
1027 /* Clear a bit in the USBCMD register */
1028 static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
1030 fotg210->command &= ~bit;
1031 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1033 /* unblock posted write */
1034 fotg210_readl(fotg210, &fotg210->regs->command);
1037 /* EHCI timer support... Now using hrtimers.
1039 * Lots of different events are triggered from fotg210->hrtimer. Whenever
1040 * the timer routine runs, it checks each possible event; events that are
1041 * currently enabled and whose expiration time has passed get handled.
1042 * The set of enabled events is stored as a collection of bitflags in
1043 * fotg210->enabled_hrtimer_events, and they are numbered in order of
1044 * increasing delay values (ranging between 1 ms and 100 ms).
1046 * Rather than implementing a sorted list or tree of all pending events,
1047 * we keep track only of the lowest-numbered pending event, in
1048 * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
1049 * expiration time is set to the timeout value for this event.
1051 * As a result, events might not get handled right away; the actual delay
1052 * could be anywhere up to twice the requested delay. This doesn't
1053 * matter, because none of the events are especially time-critical. The
1054 * ones that matter most all have a delay of 1 ms, so they will be
1055 * handled after 2 ms at most, which is okay. In addition to this, we
1056 * allow for an expiration range of 1 ms.
1059 /* Delay lengths for the hrtimer event types.
1060 * Keep this list sorted by delay length, in the same order as
1061 * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
1063 static unsigned event_delays_ns[] = {
1064 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */
1065 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */
1066 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */
1067 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */
1068 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */
1069 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1070 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1071 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1072 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1073 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */
1076 /* Enable a pending hrtimer event */
1077 static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
1080 ktime_t *timeout = &fotg210->hr_timeouts[event];
1083 *timeout = ktime_add(ktime_get(), event_delays_ns[event]);
1084 fotg210->enabled_hrtimer_events |= (1 << event);
1086 /* Track only the lowest-numbered pending event */
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);
1095 /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
1096 static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
1098 unsigned actual, want;
1100 /* Don't enable anything if the controller isn't running (e.g., died) */
1101 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1104 want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
1105 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;
1107 if (want != actual) {
1109 /* Poll again later, but give up after about 20 ms */
1110 if (fotg210->ASS_poll_count++ < 20) {
1111 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
1115 fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
1118 fotg210->ASS_poll_count = 0;
1120 /* The status is up-to-date; restart or stop the schedule as needed */
1121 if (want == 0) { /* Stopped */
1122 if (fotg210->async_count > 0)
1123 fotg210_set_command_bit(fotg210, CMD_ASE);
1125 } else { /* Running */
1126 if (fotg210->async_count == 0) {
1128 /* Turn off the schedule after a while */
1129 fotg210_enable_event(fotg210,
1130 FOTG210_HRTIMER_DISABLE_ASYNC,
1136 /* Turn off the async schedule after a brief delay */
1137 static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
1139 fotg210_clear_command_bit(fotg210, CMD_ASE);
1143 /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
1144 static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
1146 unsigned actual, want;
1148 /* Don't do anything if the controller isn't running (e.g., died) */
1149 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1152 want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
1153 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;
1155 if (want != actual) {
1157 /* Poll again later, but give up after about 20 ms */
1158 if (fotg210->PSS_poll_count++ < 20) {
1159 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
1163 fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
1166 fotg210->PSS_poll_count = 0;
1168 /* The status is up-to-date; restart or stop the schedule as needed */
1169 if (want == 0) { /* Stopped */
1170 if (fotg210->periodic_count > 0)
1171 fotg210_set_command_bit(fotg210, CMD_PSE);
1173 } else { /* Running */
1174 if (fotg210->periodic_count == 0) {
1176 /* Turn off the schedule after a while */
1177 fotg210_enable_event(fotg210,
1178 FOTG210_HRTIMER_DISABLE_PERIODIC,
1184 /* Turn off the periodic schedule after a brief delay */
1185 static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
1187 fotg210_clear_command_bit(fotg210, CMD_PSE);
1191 /* Poll the STS_HALT status bit; see when a dead controller stops */
1192 static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
1194 if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
1196 /* Give up after a few milliseconds */
1197 if (fotg210->died_poll_count++ < 5) {
1198 /* Try again later */
1199 fotg210_enable_event(fotg210,
1200 FOTG210_HRTIMER_POLL_DEAD, true);
1203 fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
1206 /* Clean up the mess */
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);
1212 /* Not in process context, so don't try to reset the controller */
1216 /* Handle unlinked interrupt QHs once they are gone from the hardware */
1217 static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
1219 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
1222 * Process all the QHs on the intr_unlink list that were added
1223 * before the current unlink cycle began. The list is in
1224 * temporal order, so stop when we reach the first entry in the
1225 * current cycle. But if the root hub isn't running then
1226 * process all the QHs on the list.
1228 fotg210->intr_unlinking = true;
1229 while (fotg210->intr_unlink) {
1230 struct fotg210_qh *qh = fotg210->intr_unlink;
1232 if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
1234 fotg210->intr_unlink = qh->unlink_next;
1235 qh->unlink_next = NULL;
1236 end_unlink_intr(fotg210, qh);
1239 /* Handle remaining entries later */
1240 if (fotg210->intr_unlink) {
1241 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
1243 ++fotg210->intr_unlink_cycle;
1245 fotg210->intr_unlinking = false;
1249 /* Start another free-iTDs/siTDs cycle */
1250 static void start_free_itds(struct fotg210_hcd *fotg210)
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);
1261 /* Wait for controller to stop using old iTDs and siTDs */
1262 static void end_free_itds(struct fotg210_hcd *fotg210)
1264 struct fotg210_itd *itd, *n;
1266 if (fotg210->rh_state < FOTG210_RH_RUNNING)
1267 fotg210->last_itd_to_free = NULL;
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)
1276 if (!list_empty(&fotg210->cached_itd_list))
1277 start_free_itds(fotg210);
1281 /* Handle lost (or very late) IAA interrupts */
1282 static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
1284 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1288 * Lost IAA irqs wedge things badly; seen first with a vt8235.
1289 * So we need this watchdog, but must protect it against both
1290 * (a) SMP races against real IAA firing and retriggering, and
1291 * (b) clean HC shutdown, when IAA watchdog was pending.
1293 if (fotg210->async_iaa) {
1296 /* If we get here, IAA is *REALLY* late. It's barely
1297 * conceivable that the system is so busy that CMD_IAAD
1298 * is still legitimately set, so let's be sure it's
1299 * clear before we read STS_IAA. (The HC should clear
1300 * CMD_IAAD when it sets STS_IAA.)
1302 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
1305 * If IAA is set here it either legitimately triggered
1306 * after the watchdog timer expired (_way_ late, so we'll
1307 * still count it as lost) ... or a silicon erratum:
1308 * - VIA seems to set IAA without triggering the IRQ;
1309 * - IAAD potentially cleared without setting IAA.
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);
1318 fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
1320 end_unlink_async(fotg210);
1325 /* Enable the I/O watchdog, if appropriate */
1326 static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
1328 /* Not needed if the controller isn't running or it's already enabled */
1329 if (fotg210->rh_state != FOTG210_RH_RUNNING ||
1330 (fotg210->enabled_hrtimer_events &
1331 BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
1335 * Isochronous transfers always need the watchdog.
1336 * For other sorts we use it only if the flag is set.
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,
1345 /* Handler functions for the hrtimer event types.
1346 * Keep this array in the same order as the event types indexed by
1347 * enum fotg210_hrtimer_event in fotg210.h.
1349 static void (*event_handlers[])(struct fotg210_hcd *) = {
1350 fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */
1351 fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */
1352 fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */
1353 fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */
1354 end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */
1355 unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1356 fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1357 fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1358 fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1359 fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */
1362 static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
1364 struct fotg210_hcd *fotg210 =
1365 container_of(t, struct fotg210_hcd, hrtimer);
1367 unsigned long events;
1368 unsigned long flags;
1371 spin_lock_irqsave(&fotg210->lock, flags);
1373 events = fotg210->enabled_hrtimer_events;
1374 fotg210->enabled_hrtimer_events = 0;
1375 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
1378 * Check each pending event. If its time has expired, handle
1379 * the event; otherwise re-enable it.
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);
1386 fotg210_enable_event(fotg210, e, false);
1389 spin_unlock_irqrestore(&fotg210->lock, flags);
1390 return HRTIMER_NORESTART;
1393 #define fotg210_bus_suspend NULL
1394 #define fotg210_bus_resume NULL
1396 static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
1397 u32 __iomem *status_reg, int port_status)
1399 if (!(port_status & PORT_CONNECT))
1402 /* if reset finished and it's still not enabled -- handoff */
1403 if (!(port_status & PORT_PE))
1404 /* with integrated TT, there's nobody to hand it to! */
1405 fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
1408 fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
1415 /* build "status change" packet (one or two bytes) from HC registers */
1417 static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
1419 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
1423 unsigned long flags;
1425 /* init status to no-changes */
1428 /* Inform the core about resumes-in-progress by returning
1429 * a non-zero value even if there are no status changes.
1431 status = fotg210->resuming_ports;
1433 mask = PORT_CSC | PORT_PEC;
1434 /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
1436 /* no hub change reports (bit 0) for now (power, ...) */
1438 /* port N changes (bit N)? */
1439 spin_lock_irqsave(&fotg210->lock, flags);
1441 temp = fotg210_readl(fotg210, &fotg210->regs->port_status);
1444 * Return status information even for ports with OWNER set.
1445 * Otherwise hub_wq wouldn't see the disconnect event when a
1446 * high-speed device is switched over to the companion
1447 * controller by the user.
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]))) {
1456 /* FIXME autosuspend idle root hubs */
1457 spin_unlock_irqrestore(&fotg210->lock, flags);
1458 return status ? retval : 0;
1461 static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210,
1462 struct usb_hub_descriptor *desc)
1464 int ports = HCS_N_PORTS(fotg210->hcs_params);
1467 desc->bDescriptorType = USB_DT_HUB;
1468 desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */
1469 desc->bHubContrCurrent = 0;
1471 desc->bNbrPorts = ports;
1472 temp = 1 + (ports / 8);
1473 desc->bDescLength = 7 + 2 * temp;
1475 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1476 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1477 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
1479 temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
1480 temp |= HUB_CHAR_NO_LPSM; /* no power switching */
1481 desc->wHubCharacteristics = cpu_to_le16(temp);
1484 static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1485 u16 wIndex, char *buf, u16 wLength)
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;
1496 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1497 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1498 * (track current state ourselves) ... blink for diagnostics,
1499 * power, "this is the one", etc. EHCI spec supports this.
1502 spin_lock_irqsave(&fotg210->lock, flags);
1504 case ClearHubFeature:
1506 case C_HUB_LOCAL_POWER:
1507 case C_HUB_OVER_CURRENT:
1508 /* no hub-wide feature/status flags */
1514 case ClearPortFeature:
1515 if (!wIndex || wIndex > ports)
1518 temp = fotg210_readl(fotg210, status_reg);
1519 temp &= ~PORT_RWC_BITS;
1522 * Even if OWNER is set, so the port is owned by the
1523 * companion controller, hub_wq needs to be able to clear
1524 * the port-change status bits (especially
1525 * USB_PORT_STAT_C_CONNECTION).
1529 case USB_PORT_FEAT_ENABLE:
1530 fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
1532 case USB_PORT_FEAT_C_ENABLE:
1533 fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
1535 case USB_PORT_FEAT_SUSPEND:
1536 if (temp & PORT_RESET)
1538 if (!(temp & PORT_SUSPEND))
1540 if ((temp & PORT_PE) == 0)
1543 /* resume signaling for 20 msec */
1544 fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
1545 fotg210->reset_done[wIndex] = jiffies
1546 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
1548 case USB_PORT_FEAT_C_SUSPEND:
1549 clear_bit(wIndex, &fotg210->port_c_suspend);
1551 case USB_PORT_FEAT_C_CONNECTION:
1552 fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
1554 case USB_PORT_FEAT_C_OVER_CURRENT:
1555 fotg210_writel(fotg210, temp | OTGISR_OVC,
1556 &fotg210->regs->otgisr);
1558 case USB_PORT_FEAT_C_RESET:
1559 /* GetPortStatus clears reset */
1564 fotg210_readl(fotg210, &fotg210->regs->command);
1566 case GetHubDescriptor:
1567 fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
1571 /* no hub-wide feature/status flags */
1573 /*cpu_to_le32s ((u32 *) buf); */
1576 if (!wIndex || wIndex > ports)
1580 temp = fotg210_readl(fotg210, status_reg);
1582 /* wPortChange bits */
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;
1588 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
1589 if (temp1 & OTGISR_OVC)
1590 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1592 /* whoever resumes must GetPortStatus to complete it!! */
1593 if (temp & PORT_RESUME) {
1595 /* Remote Wakeup received? */
1596 if (!fotg210->reset_done[wIndex]) {
1597 /* resume signaling for 20 msec */
1598 fotg210->reset_done[wIndex] = jiffies
1599 + msecs_to_jiffies(20);
1600 /* check the port again */
1601 mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
1602 fotg210->reset_done[wIndex]);
1605 /* resume completed? */
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;
1612 /* stop resume signaling */
1613 temp = fotg210_readl(fotg210, status_reg);
1614 fotg210_writel(fotg210, temp &
1615 ~(PORT_RWC_BITS | PORT_RESUME),
1617 clear_bit(wIndex, &fotg210->resuming_ports);
1618 retval = handshake(fotg210, status_reg,
1619 PORT_RESUME, 0, 2000);/* 2ms */
1621 fotg210_err(fotg210,
1622 "port %d resume error %d\n",
1623 wIndex + 1, retval);
1626 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1630 /* whoever resets must GetPortStatus to complete it!! */
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);
1637 /* force reset to complete */
1638 fotg210_writel(fotg210,
1639 temp & ~(PORT_RWC_BITS | PORT_RESET),
1641 /* REVISIT: some hardware needs 550+ usec to clear
1642 * this bit; seems too long to spin routinely...
1644 retval = handshake(fotg210, status_reg,
1645 PORT_RESET, 0, 1000);
1647 fotg210_err(fotg210, "port %d reset error %d\n",
1648 wIndex + 1, retval);
1652 /* see what we found out */
1653 temp = check_reset_complete(fotg210, wIndex, status_reg,
1654 fotg210_readl(fotg210, status_reg));
1657 if (!(temp & (PORT_RESUME|PORT_RESET))) {
1658 fotg210->reset_done[wIndex] = 0;
1659 clear_bit(wIndex, &fotg210->resuming_ports);
1662 /* transfer dedicated ports to the companion hc */
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",
1669 temp = fotg210_readl(fotg210, status_reg);
1673 * Even if OWNER is set, there's no harm letting hub_wq
1674 * see the wPortStatus values (they should all be 0 except
1675 * for PORT_POWER anyway).
1678 if (temp & PORT_CONNECT) {
1679 status |= USB_PORT_STAT_CONNECTION;
1680 status |= fotg210_port_speed(fotg210, temp);
1683 status |= USB_PORT_STAT_ENABLE;
1685 /* maybe the port was unsuspended without our knowledge */
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;
1693 set_bit(wIndex, &fotg210->port_c_suspend);
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;
1704 if (status & ~0xffff) /* only if wPortChange is interesting */
1705 dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
1706 put_unaligned_le32(status, buf);
1710 case C_HUB_LOCAL_POWER:
1711 case C_HUB_OVER_CURRENT:
1712 /* no hub-wide feature/status flags */
1718 case SetPortFeature:
1719 selector = wIndex >> 8;
1722 if (!wIndex || wIndex > ports)
1725 temp = fotg210_readl(fotg210, status_reg);
1726 temp &= ~PORT_RWC_BITS;
1728 case USB_PORT_FEAT_SUSPEND:
1729 if ((temp & PORT_PE) == 0
1730 || (temp & PORT_RESET) != 0)
1733 /* After above check the port must be connected.
1734 * Set appropriate bit thus could put phy into low power
1735 * mode if we have hostpc feature
1737 fotg210_writel(fotg210, temp | PORT_SUSPEND,
1739 set_bit(wIndex, &fotg210->suspended_ports);
1741 case USB_PORT_FEAT_RESET:
1742 if (temp & PORT_RESUME)
1744 /* line status bits may report this as low speed,
1745 * which can be fine if this root hub has a
1746 * transaction translator built in.
1748 fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
1753 * caller must wait, then call GetPortStatus
1754 * usb 2.0 spec says 50 ms resets on root
1756 fotg210->reset_done[wIndex] = jiffies
1757 + msecs_to_jiffies(50);
1758 fotg210_writel(fotg210, temp, status_reg);
1761 /* For downstream facing ports (these): one hub port is put
1762 * into test mode according to USB2 11.24.2.13, then the hub
1763 * must be reset (which for root hub now means rmmod+modprobe,
1764 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1765 * about the EHCI-specific stuff.
1767 case USB_PORT_FEAT_TEST:
1768 if (!selector || selector > 5)
1770 spin_unlock_irqrestore(&fotg210->lock, flags);
1771 fotg210_quiesce(fotg210);
1772 spin_lock_irqsave(&fotg210->lock, flags);
1774 /* Put all enabled ports into suspend */
1775 temp = fotg210_readl(fotg210, status_reg) &
1778 fotg210_writel(fotg210, temp | PORT_SUSPEND,
1781 spin_unlock_irqrestore(&fotg210->lock, flags);
1782 fotg210_halt(fotg210);
1783 spin_lock_irqsave(&fotg210->lock, flags);
1785 temp = fotg210_readl(fotg210, status_reg);
1786 temp |= selector << 16;
1787 fotg210_writel(fotg210, temp, status_reg);
1793 fotg210_readl(fotg210, &fotg210->regs->command);
1798 /* "stall" on error */
1801 spin_unlock_irqrestore(&fotg210->lock, flags);
1805 static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
1811 static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
1817 /* There's basically three types of memory:
1818 * - data used only by the HCD ... kmalloc is fine
1819 * - async and periodic schedules, shared by HC and HCD ... these
1820 * need to use dma_pool or dma_alloc_coherent
1821 * - driver buffers, read/written by HC ... single shot DMA mapped
1823 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
1824 * No memory seen by this driver is pageable.
1827 /* Allocate the key transfer structures from the previously allocated pool */
1828 static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
1829 struct fotg210_qtd *qtd, dma_addr_t dma)
1831 memset(qtd, 0, sizeof(*qtd));
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);
1839 static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
1842 struct fotg210_qtd *qtd;
1845 qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
1847 fotg210_qtd_init(fotg210, qtd, dma);
1852 static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
1853 struct fotg210_qtd *qtd)
1855 dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
1859 static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
1861 /* clean qtds first, and know this is not linked */
1862 if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
1863 fotg210_dbg(fotg210, "unused qh not empty!\n");
1867 fotg210_qtd_free(fotg210, qh->dummy);
1868 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1872 static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
1875 struct fotg210_qh *qh;
1878 qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
1881 qh->hw = (struct fotg210_qh_hw *)
1882 dma_pool_alloc(fotg210->qh_pool, flags, &dma);
1885 memset(qh->hw, 0, sizeof(*qh->hw));
1887 INIT_LIST_HEAD(&qh->qtd_list);
1889 /* dummy td enables safe urb queuing */
1890 qh->dummy = fotg210_qtd_alloc(fotg210, flags);
1891 if (qh->dummy == NULL) {
1892 fotg210_dbg(fotg210, "no dummy td\n");
1898 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1904 /* The queue heads and transfer descriptors are managed from pools tied
1905 * to each of the "per device" structures.
1906 * This is the initialisation and cleanup code.
1909 static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
1912 qh_destroy(fotg210, fotg210->async);
1913 fotg210->async = NULL;
1916 qh_destroy(fotg210, fotg210->dummy);
1917 fotg210->dummy = NULL;
1919 /* DMA consistent memory and pools */
1920 dma_pool_destroy(fotg210->qtd_pool);
1921 fotg210->qtd_pool = NULL;
1923 dma_pool_destroy(fotg210->qh_pool);
1924 fotg210->qh_pool = NULL;
1926 dma_pool_destroy(fotg210->itd_pool);
1927 fotg210->itd_pool = NULL;
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;
1935 /* shadow periodic table */
1936 kfree(fotg210->pshadow);
1937 fotg210->pshadow = NULL;
1940 /* remember to add cleanup code (above) if you add anything here */
1941 static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
1945 /* QTDs for control/bulk/intr transfers */
1946 fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
1947 fotg210_to_hcd(fotg210)->self.controller,
1948 sizeof(struct fotg210_qtd),
1949 32 /* byte alignment (for hw parts) */,
1950 4096 /* can't cross 4K */);
1951 if (!fotg210->qtd_pool)
1954 /* QHs for control/bulk/intr transfers */
1955 fotg210->qh_pool = dma_pool_create("fotg210_qh",
1956 fotg210_to_hcd(fotg210)->self.controller,
1957 sizeof(struct fotg210_qh_hw),
1958 32 /* byte alignment (for hw parts) */,
1959 4096 /* can't cross 4K */);
1960 if (!fotg210->qh_pool)
1963 fotg210->async = fotg210_qh_alloc(fotg210, flags);
1964 if (!fotg210->async)
1967 /* ITD for high speed ISO transfers */
1968 fotg210->itd_pool = dma_pool_create("fotg210_itd",
1969 fotg210_to_hcd(fotg210)->self.controller,
1970 sizeof(struct fotg210_itd),
1971 64 /* byte alignment (for hw parts) */,
1972 4096 /* can't cross 4K */);
1973 if (!fotg210->itd_pool)
1976 /* Hardware periodic table */
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)
1984 for (i = 0; i < fotg210->periodic_size; i++)
1985 fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
1987 /* software shadow of hardware table */
1988 fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
1990 if (fotg210->pshadow != NULL)
1994 fotg210_dbg(fotg210, "couldn't init memory\n");
1995 fotg210_mem_cleanup(fotg210);
1998 /* EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
2000 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
2001 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
2002 * buffers needed for the larger number). We use one QH per endpoint, queue
2003 * multiple urbs (all three types) per endpoint. URBs may need several qtds.
2005 * ISO traffic uses "ISO TD" (itd) records, and (along with
2006 * interrupts) needs careful scheduling. Performance improvements can be
2007 * an ongoing challenge. That's in "ehci-sched.c".
2009 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
2010 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
2011 * (b) special fields in qh entries or (c) split iso entries. TTs will
2012 * buffer low/full speed data so the host collects it at high speed.
2015 /* fill a qtd, returning how much of the buffer we were able to queue up */
2016 static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd,
2017 dma_addr_t buf, size_t len, int token, int maxpacket)
2022 /* one buffer entry per 4K ... first might be short or unaligned */
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); /* rest of that page */
2026 if (likely(len < count)) /* ... iff needed */
2032 /* per-qtd limit: from 16K to 20K (best alignment) */
2033 for (i = 1; count < len && i < 5; i++) {
2035 qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
2036 qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
2039 if ((count + 0x1000) < len)
2045 /* short packets may only terminate transfers */
2047 count -= (count % maxpacket);
2049 qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
2050 qtd->length = count;
2055 static inline void qh_update(struct fotg210_hcd *fotg210,
2056 struct fotg210_qh *qh, struct fotg210_qtd *qtd)
2058 struct fotg210_qh_hw *hw = qh->hw;
2060 /* writes to an active overlay are unsafe */
2061 BUG_ON(qh->qh_state != QH_STATE_IDLE);
2063 hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2064 hw->hw_alt_next = FOTG210_LIST_END(fotg210);
2066 /* Except for control endpoints, we make hardware maintain data
2067 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
2068 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
2071 if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
2072 unsigned is_out, epnum;
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);
2082 hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
2085 /* if it weren't for a common silicon quirk (writing the dummy into the qh
2086 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
2087 * recovery (including urb dequeue) would need software changes to a QH...
2089 static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
2091 struct fotg210_qtd *qtd;
2093 if (list_empty(&qh->qtd_list))
2096 qtd = list_entry(qh->qtd_list.next,
2097 struct fotg210_qtd, qtd_list);
2099 * first qtd may already be partially processed.
2100 * If we come here during unlink, the QH overlay region
2101 * might have reference to the just unlinked qtd. The
2102 * qtd is updated in qh_completions(). Update the QH
2105 if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
2106 qh->hw->hw_qtd_next = qtd->hw_next;
2112 qh_update(fotg210, qh, qtd);
2115 static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2117 static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
2118 struct usb_host_endpoint *ep)
2120 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
2121 struct fotg210_qh *qh = ep->hcpriv;
2122 unsigned long flags;
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);
2132 static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
2133 struct fotg210_qh *qh, struct urb *urb, u32 token)
2136 /* If an async split transaction gets an error or is unlinked,
2137 * the TT buffer may be left in an indeterminate state. We
2138 * have to clear the TT buffer.
2140 * Note: this routine is never called for Isochronous transfers.
2142 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
2143 struct usb_device *tt = urb->dev->tt->hub;
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);
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;
2158 static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb,
2159 size_t length, u32 token)
2161 int status = -EINPROGRESS;
2163 /* count IN/OUT bytes, not SETUP (even short packets) */
2164 if (likely(QTD_PID(token) != 2))
2165 urb->actual_length += length - QTD_LENGTH(token);
2167 /* don't modify error codes */
2168 if (unlikely(urb->unlinked))
2171 /* force cleanup after short read; not always an error */
2172 if (unlikely(IS_SHORT_READ(token)))
2173 status = -EREMOTEIO;
2175 /* serious "can't proceed" faults reported by the hardware */
2176 if (token & QTD_STS_HALT) {
2177 if (token & QTD_STS_BABBLE) {
2178 /* FIXME "must" disable babbling device's port too */
2179 status = -EOVERFLOW;
2180 /* CERR nonzero + halt --> stall */
2181 } else if (QTD_CERR(token)) {
2184 /* In theory, more than one of the following bits can be set
2185 * since they are sticky and the transaction is retried.
2186 * Which to test first is rather arbitrary.
2188 } else if (token & QTD_STS_MMF) {
2189 /* fs/ls interrupt xfer missed the complete-split */
2191 } else if (token & QTD_STS_DBE) {
2192 status = (QTD_PID(token) == 1) /* IN ? */
2193 ? -ENOSR /* hc couldn't read data */
2194 : -ECOMM; /* hc couldn't write data */
2195 } else if (token & QTD_STS_XACT) {
2196 /* timeout, bad CRC, wrong PID, etc */
2197 fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
2199 usb_pipeendpoint(urb->pipe),
2200 usb_pipein(urb->pipe) ? "in" : "out");
2202 } else { /* unknown */
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",
2217 static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb,
2219 __releases(fotg210->lock)
2220 __acquires(fotg210->lock)
2222 if (likely(urb->hcpriv != NULL)) {
2223 struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
2225 /* S-mask in a QH means it's an interrupt urb */
2226 if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
2228 /* ... update hc-wide periodic stats (for usbfs) */
2229 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
2233 if (unlikely(urb->unlinked)) {
2234 COUNT(fotg210->stats.unlink);
2236 /* report non-error and short read status as zero */
2237 if (status == -EINPROGRESS || status == -EREMOTEIO)
2239 COUNT(fotg210->stats.complete);
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",
2249 urb->actual_length, urb->transfer_buffer_length);
2252 /* complete() can reenter this HCD */
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);
2259 static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2261 /* Process and free completed qtds for a qh, returning URBs to drivers.
2262 * Chases up to qh->hw_current. Returns number of completions called,
2263 * indicating how much "real" work we did.
2265 static unsigned qh_completions(struct fotg210_hcd *fotg210,
2266 struct fotg210_qh *qh)
2268 struct fotg210_qtd *last, *end = qh->dummy;
2269 struct fotg210_qtd *qtd, *tmp;
2274 struct fotg210_qh_hw *hw = qh->hw;
2276 if (unlikely(list_empty(&qh->qtd_list)))
2279 /* completions (or tasks on other cpus) must never clobber HALT
2280 * till we've gone through and cleaned everything up, even when
2281 * they add urbs to this qh's queue or mark them for unlinking.
2283 * NOTE: unlinking expects to be done in queue order.
2285 * It's a bug for qh->qh_state to be anything other than
2286 * QH_STATE_IDLE, unless our caller is scan_async() or
2289 state = qh->qh_state;
2290 qh->qh_state = QH_STATE_COMPLETING;
2291 stopped = (state == QH_STATE_IDLE);
2295 last_status = -EINPROGRESS;
2296 qh->needs_rescan = 0;
2298 /* remove de-activated QTDs from front of queue.
2299 * after faults (including short reads), cleanup this urb
2300 * then let the queue advance.
2301 * if queue is stopped, handles unlinks.
2303 list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
2309 /* clean up any state from previous QTD ...*/
2311 if (likely(last->urb != urb)) {
2312 fotg210_urb_done(fotg210, last->urb,
2315 last_status = -EINPROGRESS;
2317 fotg210_qtd_free(fotg210, last);
2321 /* ignore urbs submitted during completions we reported */
2325 /* hardware copies qtd out of qh overlay */
2327 token = hc32_to_cpu(fotg210, qtd->hw_token);
2329 /* always clean up qtds the hc de-activated */
2331 if ((token & QTD_STS_ACTIVE) == 0) {
2333 /* Report Data Buffer Error: non-fatal but useful */
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)
2340 urb->transfer_buffer_length, qtd, qh);
2342 /* on STALL, error, and short reads this urb must
2343 * complete and all its qtds must be recycled.
2345 if ((token & QTD_STS_HALT) != 0) {
2347 /* retry transaction errors until we
2348 * reach the software xacterr limit
2350 if ((token & QTD_STS_XACT) &&
2351 QTD_CERR(token) == 0 &&
2352 ++qh->xacterrs < QH_XACTERR_MAX &&
2354 fotg210_dbg(fotg210,
2355 "detected XactErr len %zu/%zu retry %d\n",
2356 qtd->length - QTD_LENGTH(token),
2360 /* reset the token in the qtd and the
2361 * qh overlay (which still contains
2362 * the qtd) so that we pick up from
2365 token &= ~QTD_STS_HALT;
2366 token |= QTD_STS_ACTIVE |
2367 (FOTG210_TUNE_CERR << 10);
2368 qtd->hw_token = cpu_to_hc32(fotg210,
2371 hw->hw_token = cpu_to_hc32(fotg210,
2377 /* magic dummy for some short reads; qh won't advance.
2378 * that silicon quirk can kick in with this dummy too.
2380 * other short reads won't stop the queue, including
2381 * control transfers (status stage handles that) or
2382 * most other single-qtd reads ... the queue stops if
2383 * URB_SHORT_NOT_OK was set so the driver submitting
2384 * the urbs could clean it up.
2386 } else if (IS_SHORT_READ(token) &&
2387 !(qtd->hw_alt_next &
2388 FOTG210_LIST_END(fotg210))) {
2392 /* stop scanning when we reach qtds the hc is using */
2393 } else if (likely(!stopped
2394 && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
2397 /* scan the whole queue for unlinks whenever it stops */
2401 /* cancel everything if we halt, suspend, etc */
2402 if (fotg210->rh_state < FOTG210_RH_RUNNING)
2403 last_status = -ESHUTDOWN;
2405 /* this qtd is active; skip it unless a previous qtd
2406 * for its urb faulted, or its urb was canceled.
2408 else if (last_status == -EINPROGRESS && !urb->unlinked)
2411 /* qh unlinked; token in overlay may be most current */
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);
2417 /* An unlink may leave an incomplete
2418 * async transaction in the TT buffer.
2419 * We have to clear it.
2421 fotg210_clear_tt_buffer(fotg210, qh, urb,
2426 /* unless we already know the urb's status, collect qtd status
2427 * and update count of bytes transferred. in common short read
2428 * cases with only one data qtd (including control transfers),
2429 * queue processing won't halt. but with two or more qtds (for
2430 * example, with a 32 KB transfer), when the first qtd gets a
2431 * short read the second must be removed by hand.
2433 if (last_status == -EINPROGRESS) {
2434 last_status = qtd_copy_status(fotg210, urb,
2435 qtd->length, token);
2436 if (last_status == -EREMOTEIO &&
2438 FOTG210_LIST_END(fotg210)))
2439 last_status = -EINPROGRESS;
2441 /* As part of low/full-speed endpoint-halt processing
2442 * we must clear the TT buffer (11.17.5).
2444 if (unlikely(last_status != -EINPROGRESS &&
2445 last_status != -EREMOTEIO)) {
2446 /* The TT's in some hubs malfunction when they
2447 * receive this request following a STALL (they
2448 * stop sending isochronous packets). Since a
2449 * STALL can't leave the TT buffer in a busy
2450 * state (if you believe Figures 11-48 - 11-51
2451 * in the USB 2.0 spec), we won't clear the TT
2452 * buffer in this case. Strictly speaking this
2453 * is a violation of the spec.
2455 if (last_status != -EPIPE)
2456 fotg210_clear_tt_buffer(fotg210, qh,
2461 /* if we're removing something not at the queue head,
2462 * patch the hardware queue pointer.
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;
2470 /* remove qtd; it's recycled after possible urb completion */
2471 list_del(&qtd->qtd_list);
2474 /* reinit the xacterr counter for the next qtd */
2478 /* last urb's completion might still need calling */
2479 if (likely(last != NULL)) {
2480 fotg210_urb_done(fotg210, last->urb, last_status);
2482 fotg210_qtd_free(fotg210, last);
2485 /* Do we need to rescan for URBs dequeued during a giveback? */
2486 if (unlikely(qh->needs_rescan)) {
2487 /* If the QH is already unlinked, do the rescan now. */
2488 if (state == QH_STATE_IDLE)
2491 /* Otherwise we have to wait until the QH is fully unlinked.
2492 * Our caller will start an unlink if qh->needs_rescan is
2493 * set. But if an unlink has already started, nothing needs
2496 if (state != QH_STATE_LINKED)
2497 qh->needs_rescan = 0;
2500 /* restore original state; caller must unlink or relink */
2501 qh->qh_state = state;
2503 /* be sure the hardware's done with the qh before refreshing
2504 * it after fault cleanup, or recovering from silicon wrongly
2505 * overlaying the dummy qtd (which reduces DMA chatter).
2507 if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
2510 qh_refresh(fotg210, qh);
2512 case QH_STATE_LINKED:
2513 /* We won't refresh a QH that's linked (after the HC
2514 * stopped the queue). That avoids a race:
2515 * - HC reads first part of QH;
2516 * - CPU updates that first part and the token;
2517 * - HC reads rest of that QH, including token
2518 * Result: HC gets an inconsistent image, and then
2519 * DMAs to/from the wrong memory (corrupting it).
2521 * That should be rare for interrupt transfers,
2522 * except maybe high bandwidth ...
2525 /* Tell the caller to start an unlink */
2526 qh->needs_rescan = 1;
2528 /* otherwise, unlink already started */
2535 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
2536 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
2537 /* ... and packet size, for any kind of endpoint descriptor */
2538 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
2540 /* reverse of qh_urb_transaction: free a list of TDs.
2541 * used for cleanup after errors, before HC sees an URB's TDs.
2543 static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb,
2544 struct list_head *head)
2546 struct fotg210_qtd *qtd, *temp;
2548 list_for_each_entry_safe(qtd, temp, head, qtd_list) {
2549 list_del(&qtd->qtd_list);
2550 fotg210_qtd_free(fotg210, qtd);
2554 /* create a list of filled qtds for this URB; won't link into qh.
2556 static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210,
2557 struct urb *urb, struct list_head *head, gfp_t flags)
2559 struct fotg210_qtd *qtd, *qtd_prev;
2561 int len, this_sg_len, maxpacket;
2565 struct scatterlist *sg;
2568 * URBs map to sequences of QTDs: one logical transaction
2570 qtd = fotg210_qtd_alloc(fotg210, flags);
2573 list_add_tail(&qtd->qtd_list, head);
2576 token = QTD_STS_ACTIVE;
2577 token |= (FOTG210_TUNE_CERR << 10);
2578 /* for split transactions, SplitXState initialized to zero */
2580 len = urb->transfer_buffer_length;
2581 is_input = usb_pipein(urb->pipe);
2582 if (usb_pipecontrol(urb->pipe)) {
2584 qtd_fill(fotg210, qtd, urb->setup_dma,
2585 sizeof(struct usb_ctrlrequest),
2586 token | (2 /* "setup" */ << 8), 8);
2588 /* ... and always at least one more pid */
2589 token ^= QTD_TOGGLE;
2591 qtd = fotg210_qtd_alloc(fotg210, flags);
2595 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2596 list_add_tail(&qtd->qtd_list, head);
2598 /* for zero length DATA stages, STATUS is always IN */
2600 token |= (1 /* "in" */ << 8);
2604 * data transfer stage: buffer setup
2606 i = urb->num_mapped_sgs;
2607 if (len > 0 && i > 0) {
2609 buf = sg_dma_address(sg);
2611 /* urb->transfer_buffer_length may be smaller than the
2612 * size of the scatterlist (or vice versa)
2614 this_sg_len = min_t(int, sg_dma_len(sg), len);
2617 buf = urb->transfer_dma;
2622 token |= (1 /* "in" */ << 8);
2623 /* else it's already initted to "out" pid (0 << 8) */
2625 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
2628 * buffer gets wrapped in one or more qtds;
2629 * last one may be "short" (including zero len)
2630 * and may serve as a control status ack
2635 this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
2637 this_sg_len -= this_qtd_len;
2638 len -= this_qtd_len;
2639 buf += this_qtd_len;
2642 * short reads advance to a "magic" dummy instead of the next
2643 * qtd ... that forces the queue to stop, for manual cleanup.
2644 * (this will usually be overridden later.)
2647 qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
2649 /* qh makes control packets use qtd toggle; maybe switch it */
2650 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
2651 token ^= QTD_TOGGLE;
2653 if (likely(this_sg_len <= 0)) {
2654 if (--i <= 0 || len <= 0)
2657 buf = sg_dma_address(sg);
2658 this_sg_len = min_t(int, sg_dma_len(sg), len);
2662 qtd = fotg210_qtd_alloc(fotg210, flags);
2666 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2667 list_add_tail(&qtd->qtd_list, head);
2671 * unless the caller requires manual cleanup after short reads,
2672 * have the alt_next mechanism keep the queue running after the
2673 * last data qtd (the only one, for control and most other cases).
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);
2680 * control requests may need a terminating data "status" ack;
2681 * other OUT ones may need a terminating short packet
2684 if (likely(urb->transfer_buffer_length != 0)) {
2687 if (usb_pipecontrol(urb->pipe)) {
2689 token ^= 0x0100; /* "in" <--> "out" */
2690 token |= QTD_TOGGLE; /* force DATA1 */
2691 } else if (usb_pipeout(urb->pipe)
2692 && (urb->transfer_flags & URB_ZERO_PACKET)
2693 && !(urb->transfer_buffer_length % maxpacket)) {
2698 qtd = fotg210_qtd_alloc(fotg210, flags);
2702 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2703 list_add_tail(&qtd->qtd_list, head);
2705 /* never any data in such packets */
2706 qtd_fill(fotg210, qtd, 0, 0, token, 0);
2710 /* by default, enable interrupt on urb completion */
2711 if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
2712 qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
2716 qtd_list_free(fotg210, urb, head);
2720 /* Would be best to create all qh's from config descriptors,
2721 * when each interface/altsetting is established. Unlink
2722 * any previous qh and cancel its urbs first; endpoints are
2723 * implicitly reset then (data toggle too).
2724 * That'd mean updating how usbcore talks to HCDs. (2.7?)
2728 /* Each QH holds a qtd list; a QH is used for everything except iso.
2730 * For interrupt urbs, the scheduler must set the microframe scheduling
2731 * mask(s) each time the QH gets scheduled. For highspeed, that's
2732 * just one microframe in the s-mask. For split interrupt transactions
2733 * there are additional complications: c-mask, maybe FSTNs.
2735 static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb,
2738 struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags);
2739 u32 info1 = 0, info2 = 0;
2742 struct usb_tt *tt = urb->dev->tt;
2743 struct fotg210_qh_hw *hw;
2749 * init endpoint/device data for this QH
2751 info1 |= usb_pipeendpoint(urb->pipe) << 8;
2752 info1 |= usb_pipedevice(urb->pipe) << 0;
2754 is_input = usb_pipein(urb->pipe);
2755 type = usb_pipetype(urb->pipe);
2756 maxp = usb_maxpacket(urb->dev, urb->pipe, !is_input);
2758 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
2759 * acts like up to 3KB, but is built from smaller packets.
2761 if (max_packet(maxp) > 1024) {
2762 fotg210_dbg(fotg210, "bogus qh maxpacket %d\n",
2767 /* Compute interrupt scheduling parameters just once, and save.
2768 * - allowing for high bandwidth, how many nsec/uframe are used?
2769 * - split transactions need a second CSPLIT uframe; same question
2770 * - splits also need a schedule gap (for full/low speed I/O)
2771 * - qh has a polling interval
2773 * For control/bulk requests, the HC or TT handles these.
2775 if (type == PIPE_INTERRUPT) {
2776 qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
2778 hb_mult(maxp) * max_packet(maxp)));
2779 qh->start = NO_FRAME;
2781 if (urb->dev->speed == USB_SPEED_HIGH) {
2785 qh->period = urb->interval >> 3;
2786 if (qh->period == 0 && urb->interval != 1) {
2787 /* NOTE interval 2 or 4 uframes could work.
2788 * But interval 1 scheduling is simpler, and
2789 * includes high bandwidth.
2792 } else if (qh->period > fotg210->periodic_size) {
2793 qh->period = fotg210->periodic_size;
2794 urb->interval = qh->period << 3;
2799 /* gap is f(FS/LS transfer times) */
2800 qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
2801 is_input, 0, maxp) / (125 * 1000);
2803 /* FIXME this just approximates SPLIT/CSPLIT times */
2804 if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
2805 qh->c_usecs = qh->usecs + HS_USECS(0);
2806 qh->usecs = HS_USECS(1);
2807 } else { /* SPLIT+DATA, gap, CSPLIT */
2808 qh->usecs += HS_USECS(1);
2809 qh->c_usecs = HS_USECS(0);
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;
2824 /* support for tt scheduling, and access to toggles */
2828 switch (urb->dev->speed) {
2830 info1 |= QH_LOW_SPEED;
2833 case USB_SPEED_FULL:
2834 /* EPS 0 means "full" */
2835 if (type != PIPE_INTERRUPT)
2836 info1 |= (FOTG210_TUNE_RL_TT << 28);
2837 if (type == PIPE_CONTROL) {
2838 info1 |= QH_CONTROL_EP; /* for TT */
2839 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2841 info1 |= maxp << 16;
2843 info2 |= (FOTG210_TUNE_MULT_TT << 30);
2845 /* Some Freescale processors have an erratum in which the
2846 * port number in the queue head was 0..N-1 instead of 1..N.
2848 if (fotg210_has_fsl_portno_bug(fotg210))
2849 info2 |= (urb->dev->ttport-1) << 23;
2851 info2 |= urb->dev->ttport << 23;
2853 /* set the address of the TT; for TDI's integrated
2854 * root hub tt, leave it zeroed.
2856 if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
2857 info2 |= tt->hub->devnum << 16;
2859 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
2863 case USB_SPEED_HIGH: /* no TT involved */
2864 info1 |= QH_HIGH_SPEED;
2865 if (type == PIPE_CONTROL) {
2866 info1 |= (FOTG210_TUNE_RL_HS << 28);
2867 info1 |= 64 << 16; /* usb2 fixed maxpacket */
2868 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2869 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2870 } else if (type == PIPE_BULK) {
2871 info1 |= (FOTG210_TUNE_RL_HS << 28);
2872 /* The USB spec says that high speed bulk endpoints
2873 * always use 512 byte maxpacket. But some device
2874 * vendors decided to ignore that, and MSFT is happy
2875 * to help them do so. So now people expect to use
2876 * such nonconformant devices with Linux too; sigh.
2878 info1 |= max_packet(maxp) << 16;
2879 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2880 } else { /* PIPE_INTERRUPT */
2881 info1 |= max_packet(maxp) << 16;
2882 info2 |= hb_mult(maxp) << 30;
2886 fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
2889 qh_destroy(fotg210, qh);
2893 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
2895 /* init as live, toggle clear, advance to dummy */
2896 qh->qh_state = QH_STATE_IDLE;
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);
2906 static void enable_async(struct fotg210_hcd *fotg210)
2908 if (fotg210->async_count++)
2911 /* Stop waiting to turn off the async schedule */
2912 fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
2914 /* Don't start the schedule until ASS is 0 */
2915 fotg210_poll_ASS(fotg210);
2916 turn_on_io_watchdog(fotg210);
2919 static void disable_async(struct fotg210_hcd *fotg210)
2921 if (--fotg210->async_count)
2924 /* The async schedule and async_unlink list are supposed to be empty */
2925 WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
2927 /* Don't turn off the schedule until ASS is 1 */
2928 fotg210_poll_ASS(fotg210);
2931 /* move qh (and its qtds) onto async queue; maybe enable queue. */
2933 static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
2935 __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
2936 struct fotg210_qh *head;
2938 /* Don't link a QH if there's a Clear-TT-Buffer pending */
2939 if (unlikely(qh->clearing_tt))
2942 WARN_ON(qh->qh_state != QH_STATE_IDLE);
2944 /* clear halt and/or toggle; and maybe recover from silicon quirk */
2945 qh_refresh(fotg210, qh);
2947 /* splice right after start */
2948 head = fotg210->async;
2949 qh->qh_next = head->qh_next;
2950 qh->hw->hw_next = head->hw->hw_next;
2953 head->qh_next.qh = qh;
2954 head->hw->hw_next = dma;
2957 qh->qh_state = QH_STATE_LINKED;
2958 /* qtd completions reported later by interrupt */
2960 enable_async(fotg210);
2963 /* For control/bulk/interrupt, return QH with these TDs appended.
2964 * Allocates and initializes the QH if necessary.
2965 * Returns null if it can't allocate a QH it needs to.
2966 * If the QH has TDs (urbs) already, that's great.
2968 static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210,
2969 struct urb *urb, struct list_head *qtd_list,
2970 int epnum, void **ptr)
2972 struct fotg210_qh *qh = NULL;
2973 __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
2975 qh = (struct fotg210_qh *) *ptr;
2976 if (unlikely(qh == NULL)) {
2977 /* can't sleep here, we have fotg210->lock... */
2978 qh = qh_make(fotg210, urb, GFP_ATOMIC);
2981 if (likely(qh != NULL)) {
2982 struct fotg210_qtd *qtd;
2984 if (unlikely(list_empty(qtd_list)))
2987 qtd = list_entry(qtd_list->next, struct fotg210_qtd,
2990 /* control qh may need patching ... */
2991 if (unlikely(epnum == 0)) {
2992 /* usb_reset_device() briefly reverts to address 0 */
2993 if (usb_pipedevice(urb->pipe) == 0)
2994 qh->hw->hw_info1 &= ~qh_addr_mask;
2997 /* just one way to queue requests: swap with the dummy qtd.
2998 * only hc or qh_refresh() ever modify the overlay.
3000 if (likely(qtd != NULL)) {
3001 struct fotg210_qtd *dummy;
3005 /* to avoid racing the HC, use the dummy td instead of
3006 * the first td of our list (becomes new dummy). both
3007 * tds stay deactivated until we're done, when the
3008 * HC is allowed to fetch the old dummy (4.10.2).
3010 token = qtd->hw_token;
3011 qtd->hw_token = HALT_BIT(fotg210);
3015 dma = dummy->qtd_dma;
3017 dummy->qtd_dma = dma;
3019 list_del(&qtd->qtd_list);
3020 list_add(&dummy->qtd_list, qtd_list);
3021 list_splice_tail(qtd_list, &qh->qtd_list);
3023 fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
3026 /* hc must see the new dummy at list end */
3028 qtd = list_entry(qh->qtd_list.prev,
3029 struct fotg210_qtd, qtd_list);
3030 qtd->hw_next = QTD_NEXT(fotg210, dma);
3032 /* let the hc process these next qtds */
3034 dummy->hw_token = token;
3042 static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb,
3043 struct list_head *qtd_list, gfp_t mem_flags)
3046 unsigned long flags;
3047 struct fotg210_qh *qh = NULL;
3050 epnum = urb->ep->desc.bEndpointAddress;
3052 #ifdef FOTG210_URB_TRACE
3054 struct fotg210_qtd *qtd;
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)
3062 urb->transfer_buffer_length,
3063 qtd, urb->ep->hcpriv);
3067 spin_lock_irqsave(&fotg210->lock, flags);
3068 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3072 rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
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);
3083 /* Control/bulk operations through TTs don't need scheduling,
3084 * the HC and TT handle it when the TT has a buffer ready.
3086 if (likely(qh->qh_state == QH_STATE_IDLE))
3087 qh_link_async(fotg210, qh);
3089 spin_unlock_irqrestore(&fotg210->lock, flags);
3090 if (unlikely(qh == NULL))
3091 qtd_list_free(fotg210, urb, qtd_list);
3095 static void single_unlink_async(struct fotg210_hcd *fotg210,
3096 struct fotg210_qh *qh)
3098 struct fotg210_qh *prev;
3100 /* Add to the end of the list of QHs waiting for the next IAAD */
3101 qh->qh_state = QH_STATE_UNLINK;
3102 if (fotg210->async_unlink)
3103 fotg210->async_unlink_last->unlink_next = qh;
3105 fotg210->async_unlink = qh;
3106 fotg210->async_unlink_last = qh;
3108 /* Unlink it from the schedule */
3109 prev = fotg210->async;
3110 while (prev->qh_next.qh != qh)
3111 prev = prev->qh_next.qh;
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;
3119 static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
3122 * Do nothing if an IAA cycle is already running or
3123 * if one will be started shortly.
3125 if (fotg210->async_iaa || fotg210->async_unlinking)
3128 /* Do all the waiting QHs at once */
3129 fotg210->async_iaa = fotg210->async_unlink;
3130 fotg210->async_unlink = NULL;
3132 /* If the controller isn't running, we don't have to wait for it */
3133 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
3134 if (!nested) /* Avoid recursion */
3135 end_unlink_async(fotg210);
3137 /* Otherwise start a new IAA cycle */
3138 } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
3139 /* Make sure the unlinks are all visible to the hardware */
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,
3150 /* the async qh for the qtds being unlinked are now gone from the HC */
3152 static void end_unlink_async(struct fotg210_hcd *fotg210)
3154 struct fotg210_qh *qh;
3156 /* Process the idle QHs */
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;
3164 qh->qh_state = QH_STATE_IDLE;
3165 qh->qh_next.qh = NULL;
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);
3173 fotg210->async_unlinking = false;
3175 /* Start a new IAA cycle if any QHs are waiting for it */
3176 if (fotg210->async_unlink) {
3177 start_iaa_cycle(fotg210, true);
3178 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
3183 static void unlink_empty_async(struct fotg210_hcd *fotg210)
3185 struct fotg210_qh *qh, *next;
3186 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
3187 bool check_unlinks_later = false;
3189 /* Unlink all the async QHs that have been empty for a timer cycle */
3190 next = fotg210->async->qh_next.qh;
3193 next = qh->qh_next.qh;
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;
3201 single_unlink_async(fotg210, qh);
3205 /* Start a new IAA cycle if any QHs are waiting for it */
3206 if (fotg210->async_unlink)
3207 start_iaa_cycle(fotg210, false);
3209 /* QHs that haven't been empty for long enough will be handled later */
3210 if (check_unlinks_later) {
3211 fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
3213 ++fotg210->async_unlink_cycle;
3217 /* makes sure the async qh will become idle */
3218 /* caller must own fotg210->lock */
3220 static void start_unlink_async(struct fotg210_hcd *fotg210,
3221 struct fotg210_qh *qh)
3224 * If the QH isn't linked then there's nothing we can do
3225 * unless we were called during a giveback, in which case
3226 * qh_completions() has to deal with it.
3228 if (qh->qh_state != QH_STATE_LINKED) {
3229 if (qh->qh_state == QH_STATE_COMPLETING)
3230 qh->needs_rescan = 1;
3234 single_unlink_async(fotg210, qh);
3235 start_iaa_cycle(fotg210, false);
3238 static void scan_async(struct fotg210_hcd *fotg210)
3240 struct fotg210_qh *qh;
3241 bool check_unlinks_later = false;
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;
3248 /* clean any finished work for this qh */
3249 if (!list_empty(&qh->qtd_list)) {
3253 * Unlinks could happen here; completion reporting
3254 * drops the lock. That's why fotg210->qh_scan_next
3255 * always holds the next qh to scan; if the next qh
3256 * gets unlinked then fotg210->qh_scan_next is adjusted
3257 * in single_unlink_async().
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)
3272 * Unlink empty entries, reducing DMA usage as well
3273 * as HCD schedule-scanning costs. Delay for any qh
3274 * we just scanned, there's a not-unusual case that it
3275 * doesn't stay idle for long.
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;
3285 /* EHCI scheduled transaction support: interrupt, iso, split iso
3286 * These are called "periodic" transactions in the EHCI spec.
3288 * Note that for interrupt transfers, the QH/QTD manipulation is shared
3289 * with the "asynchronous" transaction support (control/bulk transfers).
3290 * The only real difference is in how interrupt transfers are scheduled.
3292 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
3293 * It keeps track of every ITD (or SITD) that's linked, and holds enough
3294 * pre-calculated schedule data to make appending to the queue be quick.
3296 static int fotg210_get_frame(struct usb_hcd *hcd);
3298 /* periodic_next_shadow - return "next" pointer on shadow list
3299 * @periodic: host pointer to qh/itd
3300 * @tag: hardware tag for type of this record
3302 static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210,
3303 union fotg210_shadow *periodic, __hc32 tag)
3305 switch (hc32_to_cpu(fotg210, tag)) {
3307 return &periodic->qh->qh_next;
3309 return &periodic->fstn->fstn_next;
3311 return &periodic->itd->itd_next;
3315 static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210,
3316 union fotg210_shadow *periodic, __hc32 tag)
3318 switch (hc32_to_cpu(fotg210, tag)) {
3319 /* our fotg210_shadow.qh is actually software part */
3321 return &periodic->qh->hw->hw_next;
3322 /* others are hw parts */
3324 return periodic->hw_next;
3328 /* caller must hold fotg210->lock */
3329 static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
3332 union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
3333 __hc32 *hw_p = &fotg210->periodic[frame];
3334 union fotg210_shadow here = *prev_p;
3336 /* find predecessor of "ptr"; hw and shadow lists are in sync */
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));
3344 /* an interrupt entry (at list end) could have been shared */
3348 /* update shadow and hardware lists ... the old "next" pointers
3349 * from ptr may still be in use, the caller updates them.
3351 *prev_p = *periodic_next_shadow(fotg210, &here,
3352 Q_NEXT_TYPE(fotg210, *hw_p));
3354 *hw_p = *shadow_next_periodic(fotg210, &here,
3355 Q_NEXT_TYPE(fotg210, *hw_p));
3358 /* how many of the uframe's 125 usecs are allocated? */
3359 static unsigned short periodic_usecs(struct fotg210_hcd *fotg210,
3360 unsigned frame, unsigned uframe)
3362 __hc32 *hw_p = &fotg210->periodic[frame];
3363 union fotg210_shadow *q = &fotg210->pshadow[frame];
3365 struct fotg210_qh_hw *hw;
3368 switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
3371 /* is it in the S-mask? */
3372 if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
3373 usecs += q->qh->usecs;
3374 /* ... or C-mask? */
3375 if (hw->hw_info2 & cpu_to_hc32(fotg210,
3377 usecs += q->qh->c_usecs;
3378 hw_p = &hw->hw_next;
3379 q = &q->qh->qh_next;
3381 /* case Q_TYPE_FSTN: */
3383 /* for "save place" FSTNs, count the relevant INTR
3384 * bandwidth from the previous frame
3386 if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
3387 fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
3389 hw_p = &q->fstn->hw_next;
3390 q = &q->fstn->fstn_next;
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;
3400 if (usecs > fotg210->uframe_periodic_max)
3401 fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
3402 frame * 8 + uframe, usecs);
3406 static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
3408 if (!dev1->tt || !dev2->tt)
3410 if (dev1->tt != dev2->tt)
3412 if (dev1->tt->multi)
3413 return dev1->ttport == dev2->ttport;
3418 /* return true iff the device's transaction translator is available
3419 * for a periodic transfer starting at the specified frame, using
3420 * all the uframes in the mask.
3422 static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period,
3423 struct usb_device *dev, unsigned frame, u32 uf_mask)
3425 if (period == 0) /* error */
3428 /* note bandwidth wastage: split never follows csplit
3429 * (different dev or endpoint) until the next uframe.
3430 * calling convention doesn't make that distinction.
3432 for (; frame < fotg210->periodic_size; frame += period) {
3433 union fotg210_shadow here;
3435 struct fotg210_qh_hw *hw;
3437 here = fotg210->pshadow[frame];
3438 type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
3440 switch (hc32_to_cpu(fotg210, type)) {
3442 type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
3443 here = here.itd->itd_next;
3447 if (same_tt(dev, here.qh->dev)) {
3450 mask = hc32_to_cpu(fotg210,
3452 /* "knows" no gap is needed */
3457 type = Q_NEXT_TYPE(fotg210, hw->hw_next);
3458 here = here.qh->qh_next;
3460 /* case Q_TYPE_FSTN: */
3462 fotg210_dbg(fotg210,
3463 "periodic frame %d bogus type %d\n",
3467 /* collision or error */
3476 static void enable_periodic(struct fotg210_hcd *fotg210)
3478 if (fotg210->periodic_count++)
3481 /* Stop waiting to turn off the periodic schedule */
3482 fotg210->enabled_hrtimer_events &=
3483 ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
3485 /* Don't start the schedule until PSS is 0 */
3486 fotg210_poll_PSS(fotg210);
3487 turn_on_io_watchdog(fotg210);
3490 static void disable_periodic(struct fotg210_hcd *fotg210)
3492 if (--fotg210->periodic_count)
3495 /* Don't turn off the schedule until PSS is 1 */
3496 fotg210_poll_PSS(fotg210);
3499 /* periodic schedule slots have iso tds (normal or split) first, then a
3500 * sparse tree for active interrupt transfers.
3502 * this just links in a qh; caller guarantees uframe masks are set right.
3503 * no FSTN support (yet; fotg210 0.96+)
3505 static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3508 unsigned period = qh->period;
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,
3516 /* high bandwidth, or otherwise every microframe */
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;
3526 /* skip the iso nodes at list head */
3528 type = Q_NEXT_TYPE(fotg210, *hw_p);
3529 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
3531 prev = periodic_next_shadow(fotg210, prev, type);
3532 hw_p = shadow_next_periodic(fotg210, &here, type);
3536 /* sorting each branch by period (slow-->fast)
3537 * enables sharing interior tree nodes
3539 while (here.ptr && qh != here.qh) {
3540 if (qh->period > here.qh->period)
3542 prev = &here.qh->qh_next;
3543 hw_p = &here.qh->hw->hw_next;
3546 /* link in this qh, unless some earlier pass did that */
3547 if (qh != here.qh) {
3550 qh->hw->hw_next = *hw_p;
3553 *hw_p = QH_NEXT(fotg210, qh->qh_dma);
3556 qh->qh_state = QH_STATE_LINKED;
3559 /* update per-qh bandwidth for usbfs */
3560 fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
3561 ? ((qh->usecs + qh->c_usecs) / qh->period)
3564 list_add(&qh->intr_node, &fotg210->intr_qh_list);
3566 /* maybe enable periodic schedule processing */
3567 ++fotg210->intr_count;
3568 enable_periodic(fotg210);
3571 static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
3572 struct fotg210_qh *qh)
3578 * If qh is for a low/full-speed device, simply unlinking it
3579 * could interfere with an ongoing split transaction. To unlink
3580 * it safely would require setting the QH_INACTIVATE bit and
3581 * waiting at least one frame, as described in EHCI 4.12.2.5.
3583 * We won't bother with any of this. Instead, we assume that the
3584 * only reason for unlinking an interrupt QH while the current URB
3585 * is still active is to dequeue all the URBs (flush the whole
3588 * If rebalancing the periodic schedule is ever implemented, this
3589 * approach will no longer be valid.
3592 /* high bandwidth, or otherwise part of every microframe */
3593 period = qh->period;
3597 for (i = qh->start; i < fotg210->periodic_size; i += period)
3598 periodic_unlink(fotg210, i, qh);
3600 /* update per-qh bandwidth for usbfs */
3601 fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
3602 ? ((qh->usecs + qh->c_usecs) / qh->period)
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,
3611 /* qh->qh_next still "live" to HC */
3612 qh->qh_state = QH_STATE_UNLINK;
3613 qh->qh_next.ptr = NULL;
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);
3621 static void start_unlink_intr(struct fotg210_hcd *fotg210,
3622 struct fotg210_qh *qh)
3624 /* If the QH isn't linked then there's nothing we can do
3625 * unless we were called during a giveback, in which case
3626 * qh_completions() has to deal with it.
3628 if (qh->qh_state != QH_STATE_LINKED) {
3629 if (qh->qh_state == QH_STATE_COMPLETING)
3630 qh->needs_rescan = 1;
3634 qh_unlink_periodic(fotg210, qh);
3636 /* Make sure the unlinks are visible before starting the timer */
3640 * The EHCI spec doesn't say how long it takes the controller to
3641 * stop accessing an unlinked interrupt QH. The timer delay is
3642 * 9 uframes; presumably that will be long enough.
3644 qh->unlink_cycle = fotg210->intr_unlink_cycle;
3646 /* New entries go at the end of the intr_unlink list */
3647 if (fotg210->intr_unlink)
3648 fotg210->intr_unlink_last->unlink_next = qh;
3650 fotg210->intr_unlink = qh;
3651 fotg210->intr_unlink_last = qh;
3653 if (fotg210->intr_unlinking)
3654 ; /* Avoid recursive calls */
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,
3660 ++fotg210->intr_unlink_cycle;
3664 static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3666 struct fotg210_qh_hw *hw = qh->hw;
3669 qh->qh_state = QH_STATE_IDLE;
3670 hw->hw_next = FOTG210_LIST_END(fotg210);
3672 qh_completions(fotg210, qh);
3674 /* reschedule QH iff another request is queued */
3675 if (!list_empty(&qh->qtd_list) &&
3676 fotg210->rh_state == FOTG210_RH_RUNNING) {
3677 rc = qh_schedule(fotg210, qh);
3679 /* An error here likely indicates handshake failure
3680 * or no space left in the schedule. Neither fault
3681 * should happen often ...
3683 * FIXME kill the now-dysfunctional queued urbs
3686 fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
3690 /* maybe turn off periodic schedule */
3691 --fotg210->intr_count;
3692 disable_periodic(fotg210);
3695 static int check_period(struct fotg210_hcd *fotg210, unsigned frame,
3696 unsigned uframe, unsigned period, unsigned usecs)
3700 /* complete split running into next frame?
3701 * given FSTN support, we could sometimes check...
3706 /* convert "usecs we need" to "max already claimed" */
3707 usecs = fotg210->uframe_periodic_max - usecs;
3709 /* we "know" 2 and 4 uframe intervals were rejected; so
3710 * for period 0, check _every_ microframe in the schedule.
3712 if (unlikely(period == 0)) {
3714 for (uframe = 0; uframe < 7; uframe++) {
3715 claimed = periodic_usecs(fotg210, frame,
3717 if (claimed > usecs)
3720 } while ((frame += 1) < fotg210->periodic_size);
3722 /* just check the specified uframe, at that period */
3725 claimed = periodic_usecs(fotg210, frame, uframe);
3726 if (claimed > usecs)
3728 } while ((frame += period) < fotg210->periodic_size);
3735 static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame,
3736 unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp)
3738 int retval = -ENOSPC;
3741 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
3744 if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
3752 /* Make sure this tt's buffer is also available for CSPLITs.
3753 * We pessimize a bit; probably the typical full speed case
3754 * doesn't need the second CSPLIT.
3756 * NOTE: both SPLIT and CSPLIT could be checked in just
3759 mask = 0x03 << (uframe + qh->gap_uf);
3760 *c_maskp = cpu_to_hc32(fotg210, mask << 8);
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))
3767 if (!check_period(fotg210, frame, uframe + qh->gap_uf,
3768 qh->period, qh->c_usecs))
3776 /* "first fit" scheduling policy used the first time through,
3777 * or when the previous schedule slot can't be re-used.
3779 static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3784 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
3785 struct fotg210_qh_hw *hw = qh->hw;
3787 qh_refresh(fotg210, qh);
3788 hw->hw_next = FOTG210_LIST_END(fotg210);
3791 /* reuse the previous schedule slots, if we can */
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,
3802 /* else scan the schedule to find a group of slots such that all
3803 * uframes have enough periodic bandwidth available.
3806 /* "normal" case, uframing flexible except with splits */
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,
3821 /* qh->period == 0 means every uframe */
3824 status = check_intr_schedule(fotg210, 0, 0, qh,
3831 /* reset S-frame and (maybe) C-frame masks */
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;
3838 fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
3840 /* stuff into the periodic schedule */
3841 qh_link_periodic(fotg210, qh);
3846 static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb,
3847 struct list_head *qtd_list, gfp_t mem_flags)
3850 unsigned long flags;
3851 struct fotg210_qh *qh;
3853 struct list_head empty;
3855 /* get endpoint and transfer/schedule data */
3856 epnum = urb->ep->desc.bEndpointAddress;
3858 spin_lock_irqsave(&fotg210->lock, flags);
3860 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3861 status = -ESHUTDOWN;
3862 goto done_not_linked;
3864 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
3865 if (unlikely(status))
3866 goto done_not_linked;
3868 /* get qh and force any scheduling errors */
3869 INIT_LIST_HEAD(&empty);
3870 qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
3875 if (qh->qh_state == QH_STATE_IDLE) {
3876 status = qh_schedule(fotg210, qh);
3881 /* then queue the urb's tds to the qh */
3882 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
3885 /* ... update usbfs periodic stats */
3886 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
3889 if (unlikely(status))
3890 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
3892 spin_unlock_irqrestore(&fotg210->lock, flags);
3894 qtd_list_free(fotg210, urb, qtd_list);
3899 static void scan_intr(struct fotg210_hcd *fotg210)
3901 struct fotg210_qh *qh;
3903 list_for_each_entry_safe(qh, fotg210->qh_scan_next,
3904 &fotg210->intr_qh_list, intr_node) {
3906 /* clean any finished work for this qh */
3907 if (!list_empty(&qh->qtd_list)) {
3911 * Unlinks could happen here; completion reporting
3912 * drops the lock. That's why fotg210->qh_scan_next
3913 * always holds the next qh to scan; if the next qh
3914 * gets unlinked then fotg210->qh_scan_next is adjusted
3915 * in qh_unlink_periodic().
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);
3928 /* fotg210_iso_stream ops work with both ITD and SITD */
3930 static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags)
3932 struct fotg210_iso_stream *stream;
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;
3943 static void iso_stream_init(struct fotg210_hcd *fotg210,
3944 struct fotg210_iso_stream *stream, struct usb_device *dev,
3945 int pipe, unsigned interval)
3948 unsigned epnum, maxp;
3954 * this might be a "high bandwidth" highspeed endpoint,
3955 * as encoded in the ep descriptor's wMaxPacket field
3957 epnum = usb_pipeendpoint(pipe);
3958 is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
3959 maxp = usb_maxpacket(dev, pipe, !is_input);
3965 maxp = max_packet(maxp);
3966 multi = hb_mult(maxp);
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);
3974 /* usbfs wants to report the average usecs per frame tied up
3975 * when transfers on this endpoint are scheduled ...
3977 if (dev->speed == USB_SPEED_FULL) {
3979 stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
3980 is_input, 1, maxp));
3983 stream->highspeed = 1;
3984 stream->usecs = HS_USECS_ISO(maxp);
3986 bandwidth = stream->usecs * 8;
3987 bandwidth /= interval;
3989 stream->bandwidth = bandwidth;
3991 stream->bEndpointAddress = is_input | epnum;
3992 stream->interval = interval;
3993 stream->maxp = maxp;
3996 static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210,
4000 struct fotg210_iso_stream *stream;
4001 struct usb_host_endpoint *ep;
4002 unsigned long flags;
4004 epnum = usb_pipeendpoint(urb->pipe);
4005 if (usb_pipein(urb->pipe))
4006 ep = urb->dev->ep_in[epnum];
4008 ep = urb->dev->ep_out[epnum];
4010 spin_lock_irqsave(&fotg210->lock, flags);
4011 stream = ep->hcpriv;
4013 if (unlikely(stream == NULL)) {
4014 stream = iso_stream_alloc(GFP_ATOMIC);
4015 if (likely(stream != NULL)) {
4016 ep->hcpriv = stream;
4018 iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
4022 /* if dev->ep[epnum] is a QH, hw is set */
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");
4030 spin_unlock_irqrestore(&fotg210->lock, flags);
4034 /* fotg210_iso_sched ops can be ITD-only or SITD-only */
4036 static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
4039 struct fotg210_iso_sched *iso_sched;
4040 int size = sizeof(*iso_sched);
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);
4050 static 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)
4055 dma_addr_t dma = urb->transfer_dma;
4057 /* how many uframes are needed for these transfers */
4058 iso_sched->span = urb->number_of_packets * stream->interval;
4060 /* figure out per-uframe itd fields that we'll need later
4061 * when we fit new itds into the schedule.
4063 for (i = 0; i < urb->number_of_packets; i++) {
4064 struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
4069 length = urb->iso_frame_desc[i].length;
4070 buf = dma + urb->iso_frame_desc[i].offset;
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);
4080 /* might need to cross a buffer page within a uframe */
4081 uframe->bufp = (buf & ~(u64)0x0fff);
4083 if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
4088 static void iso_sched_free(struct fotg210_iso_stream *stream,
4089 struct fotg210_iso_sched *iso_sched)
4093 /* caller must hold fotg210->lock!*/
4094 list_splice(&iso_sched->td_list, &stream->free_list);
4098 static int itd_urb_transaction(struct fotg210_iso_stream *stream,
4099 struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags)
4101 struct fotg210_itd *itd;
4105 struct fotg210_iso_sched *sched;
4106 unsigned long flags;
4108 sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
4109 if (unlikely(sched == NULL))
4112 itd_sched_init(fotg210, sched, stream, urb);
4114 if (urb->interval < 8)
4115 num_itds = 1 + (sched->span + 7) / 8;
4117 num_itds = urb->number_of_packets;
4119 /* allocate/init ITDs */
4120 spin_lock_irqsave(&fotg210->lock, flags);
4121 for (i = 0; i < num_itds; i++) {
4124 * Use iTDs from the free list, but not iTDs that may
4125 * still be in use by the hardware.
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)
4132 list_del(&itd->itd_list);
4133 itd_dma = itd->itd_dma;
4136 spin_unlock_irqrestore(&fotg210->lock, flags);
4137 itd = dma_pool_alloc(fotg210->itd_pool, mem_flags,
4139 spin_lock_irqsave(&fotg210->lock, flags);
4141 iso_sched_free(stream, sched);
4142 spin_unlock_irqrestore(&fotg210->lock, flags);
4147 memset(itd, 0, sizeof(*itd));
4148 itd->itd_dma = itd_dma;
4149 list_add(&itd->itd_list, &sched->td_list);
4151 spin_unlock_irqrestore(&fotg210->lock, flags);
4153 /* temporarily store schedule info in hcpriv */
4154 urb->hcpriv = sched;
4155 urb->error_count = 0;
4159 static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe,
4160 u8 usecs, u32 period)
4164 /* can't commit more than uframe_periodic_max usec */
4165 if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
4166 > (fotg210->uframe_periodic_max - usecs))
4169 /* we know urb->interval is 2^N uframes */
4171 } while (uframe < mod);
4175 /* This scheduler plans almost as far into the future as it has actual
4176 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
4177 * "as small as possible" to be cache-friendlier.) That limits the size
4178 * transfers you can stream reliably; avoid more than 64 msec per urb.
4179 * Also avoid queue depths of less than fotg210's worst irq latency (affected
4180 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
4181 * and other factors); or more than about 230 msec total (for portability,
4182 * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
4185 #define SCHEDULE_SLOP 80 /* microframes */
4187 static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb,
4188 struct fotg210_iso_stream *stream)
4190 u32 now, next, start, period, span;
4192 unsigned mod = fotg210->periodic_size << 3;
4193 struct fotg210_iso_sched *sched = urb->hcpriv;
4195 period = urb->interval;
4198 if (span > mod - SCHEDULE_SLOP) {
4199 fotg210_dbg(fotg210, "iso request %p too long\n", urb);
4204 now = fotg210_read_frame_index(fotg210) & (mod - 1);
4206 /* Typical case: reuse current schedule, stream is still active.
4207 * Hopefully there are no gaps from the host falling behind
4208 * (irq delays etc), but if there are we'll take the next
4209 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
4211 if (likely(!list_empty(&stream->td_list))) {
4214 /* For high speed devices, allow scheduling within the
4215 * isochronous scheduling threshold. For full speed devices
4216 * and Intel PCI-based controllers, don't (work around for
4219 if (!stream->highspeed && fotg210->fs_i_thresh)
4220 next = now + fotg210->i_thresh;
4224 /* Fell behind (by up to twice the slop amount)?
4225 * We decide based on the time of the last currently-scheduled
4226 * slot, not the time of the next available slot.
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);
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,
4243 /* need to schedule; when's the next (u)frame we could start?
4244 * this is bigger than fotg210->i_thresh allows; scheduling itself
4245 * isn't free, the slop should handle reasonably slow cpus. it
4246 * can also help high bandwidth if the dma and irq loads don't
4247 * jump until after the queue is primed.
4252 start = SCHEDULE_SLOP + (now & ~0x07);
4254 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
4256 /* find a uframe slot with enough bandwidth.
4257 * Early uframes are more precious because full-speed
4258 * iso IN transfers can't use late uframes,
4259 * and therefore they should be allocated last.
4265 /* check schedule: enough space? */
4266 if (itd_slot_ok(fotg210, mod, start,
4267 stream->usecs, period))
4269 } while (start > next && !done);
4271 /* no room in the schedule */
4273 fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
4274 urb, now, now + mod);
4280 /* Tried to schedule too far into the future? */
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);
4290 stream->next_uframe = start & (mod - 1);
4292 /* report high speed start in uframes; full speed, in frames */
4293 urb->start_frame = stream->next_uframe;
4294 if (!stream->highspeed)
4295 urb->start_frame >>= 3;
4297 /* Make sure scan_isoc() sees these */
4298 if (fotg210->isoc_count == 0)
4299 fotg210->next_frame = now >> 3;
4303 iso_sched_free(stream, sched);
4308 static inline void itd_init(struct fotg210_hcd *fotg210,
4309 struct fotg210_iso_stream *stream, struct fotg210_itd *itd)
4313 /* it's been recently zeroed */
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;
4319 for (i = 0; i < 8; i++)
4322 /* All other fields are filled when scheduling */
4325 static inline void itd_patch(struct fotg210_hcd *fotg210,
4326 struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched,
4327 unsigned index, u16 uframe)
4329 struct fotg210_iso_packet *uf = &iso_sched->packet[index];
4330 unsigned pg = itd->pg;
4333 itd->index[uframe] = index;
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));
4340 /* iso_frame_desc[].offset must be strictly increasing */
4341 if (unlikely(uf->cross)) {
4342 u64 bufp = uf->bufp + 4096;
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));
4350 static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame,
4351 struct fotg210_itd *itd)
4353 union fotg210_shadow *prev = &fotg210->pshadow[frame];
4354 __hc32 *hw_p = &fotg210->periodic[frame];
4355 union fotg210_shadow here = *prev;
4358 /* skip any iso nodes which might belong to previous microframes */
4360 type = Q_NEXT_TYPE(fotg210, *hw_p);
4361 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
4363 prev = periodic_next_shadow(fotg210, prev, type);
4364 hw_p = shadow_next_periodic(fotg210, &here, type);
4368 itd->itd_next = here;
4369 itd->hw_next = *hw_p;
4373 *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
4376 /* fit urb's itds into the selected schedule slot; activate as needed */
4377 static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb,
4378 unsigned mod, struct fotg210_iso_stream *stream)
4381 unsigned next_uframe, uframe, frame;
4382 struct fotg210_iso_sched *iso_sched = urb->hcpriv;
4383 struct fotg210_itd *itd;
4385 next_uframe = stream->next_uframe & (mod - 1);
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",
4395 next_uframe >> 3, next_uframe & 0x7);
4398 /* fill iTDs uframe by uframe */
4399 for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
4401 /* ASSERT: we have all necessary itds */
4403 /* ASSERT: no itds for this endpoint in this uframe */
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;
4410 itd_init(fotg210, stream, itd);
4413 uframe = next_uframe & 0x07;
4414 frame = next_uframe >> 3;
4416 itd_patch(fotg210, itd, iso_sched, packet, uframe);
4418 next_uframe += stream->interval;
4419 next_uframe &= mod - 1;
4422 /* link completed itds into the schedule */
4423 if (((next_uframe >> 3) != frame)
4424 || packet == urb->number_of_packets) {
4425 itd_link(fotg210, frame & (fotg210->periodic_size - 1),
4430 stream->next_uframe = next_uframe;
4432 /* don't need that schedule data any more */
4433 iso_sched_free(stream, iso_sched);
4436 ++fotg210->isoc_count;
4437 enable_periodic(fotg210);
4440 #define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
4441 FOTG210_ISOC_XACTERR)
4443 /* Process and recycle a completed ITD. Return true iff its urb completed,
4444 * and hence its completion callback probably added things to the hardware
4447 * Note that we carefully avoid recycling this descriptor until after any
4448 * completion callback runs, so that it won't be reused quickly. That is,
4449 * assuming (a) no more than two urbs per frame on this endpoint, and also
4450 * (b) only this endpoint's completions submit URBs. It seems some silicon
4451 * corrupts things if you reuse completed descriptors very quickly...
4453 static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
4455 struct urb *urb = itd->urb;
4456 struct usb_iso_packet_descriptor *desc;
4460 struct fotg210_iso_stream *stream = itd->stream;
4461 struct usb_device *dev;
4462 bool retval = false;
4464 /* for each uframe with a packet */
4465 for (uframe = 0; uframe < 8; uframe++) {
4466 if (likely(itd->index[uframe] == -1))
4468 urb_index = itd->index[uframe];
4469 desc = &urb->iso_frame_desc[urb_index];
4471 t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
4472 itd->hw_transaction[uframe] = 0;
4474 /* report transfer status */
4475 if (unlikely(t & ISO_ERRS)) {
4477 if (t & FOTG210_ISOC_BUF_ERR)
4478 desc->status = usb_pipein(urb->pipe)
4479 ? -ENOSR /* hc couldn't read */
4480 : -ECOMM; /* hc couldn't write */
4481 else if (t & FOTG210_ISOC_BABBLE)
4482 desc->status = -EOVERFLOW;
4483 else /* (t & FOTG210_ISOC_XACTERR) */
4484 desc->status = -EPROTO;
4486 /* HC need not update length with this error */
4487 if (!(t & FOTG210_ISOC_BABBLE)) {
4488 desc->actual_length =
4489 fotg210_itdlen(urb, desc, t);
4490 urb->actual_length += desc->actual_length;
4492 } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
4494 desc->actual_length = fotg210_itdlen(urb, desc, t);
4495 urb->actual_length += desc->actual_length;
4497 /* URB was too late */
4498 desc->status = -EXDEV;
4502 /* handle completion now? */
4503 if (likely((urb_index + 1) != urb->number_of_packets))
4506 /* ASSERT: it's really the last itd for this urb
4507 * list_for_each_entry (itd, &stream->td_list, itd_list)
4508 * BUG_ON (itd->urb == urb);
4511 /* give urb back to the driver; completion often (re)submits */
4513 fotg210_urb_done(fotg210, urb, 0);
4517 --fotg210->isoc_count;
4518 disable_periodic(fotg210);
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");
4532 /* Add to the end of the free list for later reuse */
4533 list_move_tail(&itd->itd_list, &stream->free_list);
4535 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
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);
4545 static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
4548 int status = -EINVAL;
4549 unsigned long flags;
4550 struct fotg210_iso_stream *stream;
4552 /* Get iso_stream head */
4553 stream = iso_stream_find(fotg210, urb);
4554 if (unlikely(stream == NULL)) {
4555 fotg210_dbg(fotg210, "can't get iso stream\n");
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);
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,
4577 /* allocate ITDs w/o locking anything */
4578 status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
4579 if (unlikely(status < 0)) {
4580 fotg210_dbg(fotg210, "can't init itds\n");
4584 /* schedule ... need to lock */
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;
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);
4597 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
4599 spin_unlock_irqrestore(&fotg210->lock, flags);
4604 static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame,
4605 unsigned now_frame, bool live)
4609 union fotg210_shadow q, *q_p;
4612 /* scan each element in frame's queue for completions */
4613 q_p = &fotg210->pshadow[frame];
4614 hw_p = &fotg210->periodic[frame];
4616 type = Q_NEXT_TYPE(fotg210, *hw_p);
4620 switch (hc32_to_cpu(fotg210, type)) {
4622 /* If this ITD is still active, leave it for
4623 * later processing ... check the next entry.
4624 * No need to check for activity unless the
4627 if (frame == now_frame && live) {
4629 for (uf = 0; uf < 8; uf++) {
4630 if (q.itd->hw_transaction[uf] &
4631 ITD_ACTIVE(fotg210))
4635 q_p = &q.itd->itd_next;
4636 hw_p = &q.itd->hw_next;
4637 type = Q_NEXT_TYPE(fotg210,
4644 /* Take finished ITDs out of the schedule
4645 * and process them: recycle, maybe report
4646 * URB completion. HC won't cache the
4647 * pointer for much longer, if at all.
4649 *q_p = q.itd->itd_next;
4650 *hw_p = q.itd->hw_next;
4651 type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
4653 modified = itd_complete(fotg210, q.itd);
4657 fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
4658 type, frame, q.ptr);
4662 /* End of the iTDs and siTDs */
4667 /* assume completion callbacks modify the queue */
4668 if (unlikely(modified && fotg210->isoc_count > 0))
4674 static void scan_isoc(struct fotg210_hcd *fotg210)
4676 unsigned uf, now_frame, frame, ret;
4677 unsigned fmask = fotg210->periodic_size - 1;
4681 * When running, scan from last scan point up to "now"
4682 * else clean up by scanning everything that's left.
4683 * Touches as few pages as possible: cache-friendly.
4685 if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
4686 uf = fotg210_read_frame_index(fotg210);
4687 now_frame = (uf >> 3) & fmask;
4690 now_frame = (fotg210->next_frame - 1) & fmask;
4693 fotg210->now_frame = now_frame;
4695 frame = fotg210->next_frame;
4699 ret = scan_frame_queue(fotg210, frame,
4702 /* Stop when we have reached the current frame */
4703 if (frame == now_frame)
4705 frame = (frame + 1) & fmask;
4707 fotg210->next_frame = now_frame;
4710 /* Display / Set uframe_periodic_max
4712 static ssize_t show_uframe_periodic_max(struct device *dev,
4713 struct device_attribute *attr, char *buf)
4715 struct fotg210_hcd *fotg210;
4718 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4719 n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
4724 static ssize_t store_uframe_periodic_max(struct device *dev,
4725 struct device_attribute *attr, const char *buf, size_t count)
4727 struct fotg210_hcd *fotg210;
4728 unsigned uframe_periodic_max;
4729 unsigned frame, uframe;
4730 unsigned short allocated_max;
4731 unsigned long flags;
4734 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4735 if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
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);
4747 * lock, so that our checking does not race with possible periodic
4748 * bandwidth allocation through submitting new urbs.
4750 spin_lock_irqsave(&fotg210->lock, flags);
4753 * for request to decrease max periodic bandwidth, we have to check
4754 * every microframe in the schedule to see whether the decrease is
4757 if (uframe_periodic_max < fotg210->uframe_periodic_max) {
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,
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);
4774 /* increasing is always ok */
4776 fotg210_info(fotg210,
4777 "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
4778 100 * uframe_periodic_max/125, uframe_periodic_max);
4780 if (uframe_periodic_max != 100)
4781 fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
4783 fotg210->uframe_periodic_max = uframe_periodic_max;
4787 spin_unlock_irqrestore(&fotg210->lock, flags);
4791 static DEVICE_ATTR(uframe_periodic_max, 0644, show_uframe_periodic_max,
4792 store_uframe_periodic_max);
4794 static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
4796 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
4798 return device_create_file(controller, &dev_attr_uframe_periodic_max);
4801 static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
4803 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
4805 device_remove_file(controller, &dev_attr_uframe_periodic_max);
4807 /* On some systems, leaving remote wakeup enabled prevents system shutdown.
4808 * The firmware seems to think that powering off is a wakeup event!
4809 * This routine turns off remote wakeup and everything else, on all ports.
4811 static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
4813 u32 __iomem *status_reg = &fotg210->regs->port_status;
4815 fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
4818 /* Halt HC, turn off all ports, and let the BIOS use the companion controllers.
4819 * Must be called with interrupts enabled and the lock not held.
4821 static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
4823 fotg210_halt(fotg210);
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);
4831 /* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
4832 * This forcibly disables dma and IRQs, helping kexec and other cases
4833 * where the next system software may expect clean state.
4835 static void fotg210_shutdown(struct usb_hcd *hcd)
4837 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
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);
4845 fotg210_silence_controller(fotg210);
4847 hrtimer_cancel(&fotg210->hrtimer);
4850 /* fotg210_work is called from some interrupts, timers, and so on.
4851 * it calls driver completion functions, after dropping fotg210->lock.
4853 static void fotg210_work(struct fotg210_hcd *fotg210)
4855 /* another CPU may drop fotg210->lock during a schedule scan while
4856 * it reports urb completions. this flag guards against bogus
4857 * attempts at re-entrant schedule scanning.
4859 if (fotg210->scanning) {
4860 fotg210->need_rescan = true;
4863 fotg210->scanning = true;
4866 fotg210->need_rescan = false;
4867 if (fotg210->async_count)
4868 scan_async(fotg210);
4869 if (fotg210->intr_count > 0)
4871 if (fotg210->isoc_count > 0)
4873 if (fotg210->need_rescan)
4875 fotg210->scanning = false;
4877 /* the IO watchdog guards against hardware or driver bugs that
4878 * misplace IRQs, and should let us run completely without IRQs.
4879 * such lossage has been observed on both VT6202 and VT8235.
4881 turn_on_io_watchdog(fotg210);
4884 /* Called when the fotg210_hcd module is removed.
4886 static void fotg210_stop(struct usb_hcd *hcd)
4888 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
4890 fotg210_dbg(fotg210, "stop\n");
4892 /* no more interrupts ... */
4894 spin_lock_irq(&fotg210->lock);
4895 fotg210->enabled_hrtimer_events = 0;
4896 spin_unlock_irq(&fotg210->lock);
4898 fotg210_quiesce(fotg210);
4899 fotg210_silence_controller(fotg210);
4900 fotg210_reset(fotg210);
4902 hrtimer_cancel(&fotg210->hrtimer);
4903 remove_sysfs_files(fotg210);
4904 remove_debug_files(fotg210);
4906 /* root hub is shut down separately (first, when possible) */
4907 spin_lock_irq(&fotg210->lock);
4908 end_free_itds(fotg210);
4909 spin_unlock_irq(&fotg210->lock);
4910 fotg210_mem_cleanup(fotg210);
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);
4920 dbg_status(fotg210, "fotg210_stop completed",
4921 fotg210_readl(fotg210, &fotg210->regs->status));
4924 /* one-time init, only for memory state */
4925 static int hcd_fotg210_init(struct usb_hcd *hcd)
4927 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
4931 struct fotg210_qh_hw *hw;
4933 spin_lock_init(&fotg210->lock);
4936 * keep io watchdog by default, those good HCDs could turn off it later
4938 fotg210->need_io_watchdog = 1;
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;
4944 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
4947 * by default set standard 80% (== 100 usec/uframe) max periodic
4948 * bandwidth as required by USB 2.0
4950 fotg210->uframe_periodic_max = 100;
4953 * hw default: 1K periodic list heads, one per frame.
4954 * periodic_size can shrink by USBCMD update if hcc_params allows.
4956 fotg210->periodic_size = DEFAULT_I_TDPS;
4957 INIT_LIST_HEAD(&fotg210->intr_qh_list);
4958 INIT_LIST_HEAD(&fotg210->cached_itd_list);
4960 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
4961 /* periodic schedule size can be smaller than default */
4962 switch (FOTG210_TUNE_FLS) {
4964 fotg210->periodic_size = 1024;
4967 fotg210->periodic_size = 512;
4970 fotg210->periodic_size = 256;
4976 retval = fotg210_mem_init(fotg210, GFP_KERNEL);
4980 /* controllers may cache some of the periodic schedule ... */
4981 fotg210->i_thresh = 2;
4984 * dedicate a qh for the async ring head, since we couldn't unlink
4985 * a 'real' qh without stopping the async schedule [4.8]. use it
4986 * as the 'reclamation list head' too.
4987 * its dummy is used in hw_alt_next of many tds, to prevent the qh
4988 * from automatically advancing to the next td after short reads.
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);
4999 /* clear interrupt enables, set irq latency */
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 /* HW default park == 3, on hardware that supports it (like
5005 * NVidia and ALI silicon), maximizes throughput on the async
5006 * schedule by avoiding QH fetches between transfers.
5008 * With fast usb storage devices and NForce2, "park" seems to
5009 * make problems: throughput reduction (!), data errors...
5012 park = min_t(unsigned, park, 3);
5016 fotg210_dbg(fotg210, "park %d\n", park);
5018 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5019 /* periodic schedule size can be smaller than default */
5021 temp |= (FOTG210_TUNE_FLS << 2);
5023 fotg210->command = temp;
5025 /* Accept arbitrarily long scatter-gather lists */
5026 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
5027 hcd->self.sg_tablesize = ~0;
5031 /* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
5032 static int fotg210_run(struct usb_hcd *hcd)
5034 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5038 hcd->uses_new_polling = 1;
5040 /* EHCI spec section 4.1 */
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);
5048 * hcc_params controls whether fotg210->regs->segment must (!!!)
5049 * be used; it constrains QH/ITD/SITD and QTD locations.
5050 * dma_pool consistent memory always uses segment zero.
5051 * streaming mappings for I/O buffers, like pci_map_single(),
5052 * can return segments above 4GB, if the device allows.
5054 * NOTE: the dma mask is visible through dev->dma_mask, so
5055 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
5056 * Scsi_Host.highmem_io, and so forth. It's readonly to all
5057 * host side drivers though.
5059 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
5062 * Philips, Intel, and maybe others need CMD_RUN before the
5063 * root hub will detect new devices (why?); NEC doesn't
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);
5071 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5072 * are explicitly handed to companion controller(s), so no TT is
5073 * involved with the root hub. (Except where one is integrated,
5074 * and there's no companion controller unless maybe for USB OTG.)
5076 * Turning on the CF flag will transfer ownership of all ports
5077 * from the companions to the EHCI controller. If any of the
5078 * companions are in the middle of a port reset at the time, it
5079 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
5080 * guarantees that no resets are in progress. After we set CF,
5081 * a short delay lets the hardware catch up; new resets shouldn't
5082 * be started before the port switching actions could complete.
5084 down_write(&ehci_cf_port_reset_rwsem);
5085 fotg210->rh_state = FOTG210_RH_RUNNING;
5086 /* unblock posted writes */
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();
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);
5099 fotg210_writel(fotg210, INTR_MASK,
5100 &fotg210->regs->intr_enable); /* Turn On Interrupts */
5102 /* GRR this is run-once init(), being done every time the HC starts.
5103 * So long as they're part of class devices, we can't do it init()
5104 * since the class device isn't created that early.
5106 create_debug_files(fotg210);
5107 create_sysfs_files(fotg210);
5112 static int fotg210_setup(struct usb_hcd *hcd)
5114 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5117 fotg210->regs = (void __iomem *)fotg210->caps +
5119 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
5120 dbg_hcs_params(fotg210, "reset");
5121 dbg_hcc_params(fotg210, "reset");
5123 /* cache this readonly data; minimize chip reads */
5124 fotg210->hcs_params = fotg210_readl(fotg210,
5125 &fotg210->caps->hcs_params);
5127 fotg210->sbrn = HCD_USB2;
5129 /* data structure init */
5130 retval = hcd_fotg210_init(hcd);
5134 retval = fotg210_halt(fotg210);
5138 fotg210_reset(fotg210);
5143 static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
5145 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5146 u32 status, masked_status, pcd_status = 0, cmd;
5149 spin_lock(&fotg210->lock);
5151 status = fotg210_readl(fotg210, &fotg210->regs->status);
5153 /* e.g. cardbus physical eject */
5154 if (status == ~(u32) 0) {
5155 fotg210_dbg(fotg210, "device removed\n");
5160 * We don't use STS_FLR, but some controllers don't like it to
5161 * remain on, so mask it out along with the other status bits.
5163 masked_status = status & (INTR_MASK | STS_FLR);
5166 if (!masked_status ||
5167 unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
5168 spin_unlock(&fotg210->lock);
5172 /* clear (just) interrupts */
5173 fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
5174 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
5177 /* unrequested/ignored: Frame List Rollover */
5178 dbg_status(fotg210, "irq", status);
5180 /* INT, ERR, and IAA interrupt rates can be throttled */
5182 /* normal [4.15.1.2] or error [4.15.1.1] completion */
5183 if (likely((status & (STS_INT|STS_ERR)) != 0)) {
5184 if (likely((status & STS_ERR) == 0))
5185 COUNT(fotg210->stats.normal);
5187 COUNT(fotg210->stats.error);
5191 /* complete the unlinking of some qh [4.15.2.3] */
5192 if (status & STS_IAA) {
5194 /* Turn off the IAA watchdog */
5195 fotg210->enabled_hrtimer_events &=
5196 ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
5199 * Mild optimization: Allow another IAAD to reset the
5200 * hrtimer, if one occurs before the next expiration.
5201 * In theory we could always cancel the hrtimer, but
5202 * tests show that about half the time it will be reset
5203 * for some other event anyway.
5205 if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
5206 ++fotg210->next_hrtimer_event;
5208 /* guard against (alleged) silicon errata */
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);
5215 fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
5218 /* remote wakeup [4.3.1] */
5219 if (status & STS_PCD) {
5221 u32 __iomem *status_reg = &fotg210->regs->port_status;
5223 /* kick root hub later */
5224 pcd_status = status;
5226 /* resume root hub? */
5227 if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
5228 usb_hcd_resume_root_hub(hcd);
5230 pstatus = fotg210_readl(fotg210, status_reg);
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) {
5238 /* start 20 msec resume signaling from this port,
5239 * and make hub_wq collect PORT_STAT_C_SUSPEND to
5240 * stop that signaling. Use 5 ms extra for safety,
5241 * like usb_port_resume() does.
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]);
5250 /* PCI errors [4.15.2.4] */
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);
5258 /* Don't let the controller do anything more */
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);
5267 /* Handle completions when the controller stops */
5272 fotg210_work(fotg210);
5273 spin_unlock(&fotg210->lock);
5275 usb_hcd_poll_rh_status(hcd);
5279 /* non-error returns are a promise to giveback() the urb later
5280 * we drop ownership so next owner (or urb unlink) can get it
5282 * urb + dev is in hcd.self.controller.urb_list
5283 * we're queueing TDs onto software and hardware lists
5285 * hcd-specific init for hcpriv hasn't been done yet
5287 * NOTE: control, bulk, and interrupt share the same code to append TDs
5288 * to a (possibly active) QH, and the same QH scanning code.
5290 static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
5293 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5294 struct list_head qtd_list;
5296 INIT_LIST_HEAD(&qtd_list);
5298 switch (usb_pipetype(urb->pipe)) {
5300 /* qh_completions() code doesn't handle all the fault cases
5301 * in multi-TD control transfers. Even 1KB is rare anyway.
5303 if (urb->transfer_buffer_length > (16 * 1024))
5306 /* case PIPE_BULK: */
5308 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5310 return submit_async(fotg210, urb, &qtd_list, mem_flags);
5312 case PIPE_INTERRUPT:
5313 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5315 return intr_submit(fotg210, urb, &qtd_list, mem_flags);
5317 case PIPE_ISOCHRONOUS:
5318 return itd_submit(fotg210, urb, mem_flags);
5322 /* remove from hardware lists
5323 * completions normally happen asynchronously
5326 static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
5328 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5329 struct fotg210_qh *qh;
5330 unsigned long flags;
5333 spin_lock_irqsave(&fotg210->lock, flags);
5334 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
5338 switch (usb_pipetype(urb->pipe)) {
5339 /* case PIPE_CONTROL: */
5340 /* case PIPE_BULK:*/
5342 qh = (struct fotg210_qh *) urb->hcpriv;
5345 switch (qh->qh_state) {
5346 case QH_STATE_LINKED:
5347 case QH_STATE_COMPLETING:
5348 start_unlink_async(fotg210, qh);
5350 case QH_STATE_UNLINK:
5351 case QH_STATE_UNLINK_WAIT:
5352 /* already started */
5355 /* QH might be waiting for a Clear-TT-Buffer */
5356 qh_completions(fotg210, qh);
5361 case PIPE_INTERRUPT:
5362 qh = (struct fotg210_qh *) urb->hcpriv;
5365 switch (qh->qh_state) {
5366 case QH_STATE_LINKED:
5367 case QH_STATE_COMPLETING:
5368 start_unlink_intr(fotg210, qh);
5371 qh_completions(fotg210, qh);
5374 fotg210_dbg(fotg210, "bogus qh %p state %d\n",
5380 case PIPE_ISOCHRONOUS:
5383 /* wait till next completion, do it then. */
5384 /* completion irqs can wait up to 1024 msec, */
5388 spin_unlock_irqrestore(&fotg210->lock, flags);
5392 /* bulk qh holds the data toggle */
5394 static void fotg210_endpoint_disable(struct usb_hcd *hcd,
5395 struct usb_host_endpoint *ep)
5397 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5398 unsigned long flags;
5399 struct fotg210_qh *qh, *tmp;
5401 /* ASSERT: any requests/urbs are being unlinked */
5402 /* ASSERT: nobody can be submitting urbs for this any more */
5405 spin_lock_irqsave(&fotg210->lock, flags);
5410 /* endpoints can be iso streams. for now, we don't
5411 * accelerate iso completions ... so spin a while.
5413 if (qh->hw == NULL) {
5414 struct fotg210_iso_stream *stream = ep->hcpriv;
5416 if (!list_empty(&stream->td_list))
5419 /* BUG_ON(!list_empty(&stream->free_list)); */
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;
5431 tmp = tmp->qh_next.qh)
5433 /* periodic qh self-unlinks on empty, and a COMPLETING qh
5434 * may already be unlinked.
5437 start_unlink_async(fotg210, qh);
5439 case QH_STATE_UNLINK: /* wait for hw to finish? */
5440 case QH_STATE_UNLINK_WAIT:
5442 spin_unlock_irqrestore(&fotg210->lock, flags);
5443 schedule_timeout_uninterruptible(1);
5445 case QH_STATE_IDLE: /* fully unlinked */
5446 if (qh->clearing_tt)
5448 if (list_empty(&qh->qtd_list)) {
5449 qh_destroy(fotg210, qh);
5452 /* else FALL THROUGH */
5454 /* caller was supposed to have unlinked any requests;
5455 * that's not our job. just leak this memory.
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)");
5464 spin_unlock_irqrestore(&fotg210->lock, flags);
5467 static void fotg210_endpoint_reset(struct usb_hcd *hcd,
5468 struct usb_host_endpoint *ep)
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;
5477 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
5480 spin_lock_irqsave(&fotg210->lock, flags);
5483 /* For Bulk and Interrupt endpoints we maintain the toggle state
5484 * in the hardware; the toggle bits in udev aren't used at all.
5485 * When an endpoint is reset by usb_clear_halt() we must reset
5486 * the toggle bit in the 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) {
5495 /* The toggle value in the QH can't be updated
5496 * while the QH is active. Unlink it now;
5497 * re-linking will call qh_refresh().
5499 if (eptype == USB_ENDPOINT_XFER_BULK)
5500 start_unlink_async(fotg210, qh);
5502 start_unlink_intr(fotg210, qh);
5505 spin_unlock_irqrestore(&fotg210->lock, flags);
5508 static int fotg210_get_frame(struct usb_hcd *hcd)
5510 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5512 return (fotg210_read_frame_index(fotg210) >> 3) %
5513 fotg210->periodic_size;
5516 /* The EHCI in ChipIdea HDRC cannot be a separate module or device,
5517 * because its registers (and irq) are shared between host/gadget/otg
5518 * functions and in order to facilitate role switching we cannot
5519 * give the fotg210 driver exclusive access to those.
5521 MODULE_DESCRIPTION(DRIVER_DESC);
5522 MODULE_AUTHOR(DRIVER_AUTHOR);
5523 MODULE_LICENSE("GPL");
5525 static 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),
5531 * generic hardware linkage
5534 .flags = HCD_MEMORY | HCD_USB2,
5537 * basic lifecycle operations
5539 .reset = hcd_fotg210_init,
5540 .start = fotg210_run,
5541 .stop = fotg210_stop,
5542 .shutdown = fotg210_shutdown,
5545 * managing i/o requests and associated device resources
5547 .urb_enqueue = fotg210_urb_enqueue,
5548 .urb_dequeue = fotg210_urb_dequeue,
5549 .endpoint_disable = fotg210_endpoint_disable,
5550 .endpoint_reset = fotg210_endpoint_reset,
5553 * scheduling support
5555 .get_frame_number = fotg210_get_frame,
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,
5565 .relinquish_port = fotg210_relinquish_port,
5566 .port_handed_over = fotg210_port_handed_over,
5568 .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
5571 static void fotg210_init(struct fotg210_hcd *fotg210)
5575 iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
5576 &fotg210->regs->gmir);
5578 value = ioread32(&fotg210->regs->otgcsr);
5579 value &= ~OTGCSR_A_BUS_DROP;
5580 value |= OTGCSR_A_BUS_REQ;
5581 iowrite32(value, &fotg210->regs->otgcsr);
5585 * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
5587 * Allocates basic resources for this USB host controller, and
5588 * then invokes the start() method for the HCD associated with it
5589 * through the hotplug entry's driver_data.
5591 static int fotg210_hcd_probe(struct platform_device *pdev)
5593 struct device *dev = &pdev->dev;
5594 struct usb_hcd *hcd;
5595 struct resource *res;
5597 int retval = -ENODEV;
5598 struct fotg210_hcd *fotg210;
5603 pdev->dev.power.power_state = PMSG_ON;
5605 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
5607 dev_err(dev, "Found HC with no IRQ. Check %s setup!\n",
5614 hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
5617 dev_err(dev, "failed to create hcd with err %d\n", retval);
5619 goto fail_create_hcd;
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);
5631 hcd->rsrc_start = res->start;
5632 hcd->rsrc_len = resource_size(res);
5634 fotg210 = hcd_to_fotg210(hcd);
5636 fotg210->caps = hcd->regs;
5638 retval = fotg210_setup(hcd);
5642 fotg210_init(fotg210);
5644 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
5646 dev_err(dev, "failed to add hcd with err %d\n", retval);
5649 device_wakeup_enable(hcd->self.controller);
5656 dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
5661 * fotg210_hcd_remove - shutdown processing for EHCI HCDs
5662 * @dev: USB Host Controller being removed
5665 static int fotg210_hcd_remove(struct platform_device *pdev)
5667 struct device *dev = &pdev->dev;
5668 struct usb_hcd *hcd = dev_get_drvdata(dev);
5673 usb_remove_hcd(hcd);
5679 static struct platform_driver fotg210_hcd_driver = {
5681 .name = "fotg210-hcd",
5683 .probe = fotg210_hcd_probe,
5684 .remove = fotg210_hcd_remove,
5687 static int __init fotg210_hcd_init(void)
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");
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));
5705 fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
5706 if (!fotg210_debug_root) {
5711 retval = platform_driver_register(&fotg210_hcd_driver);
5717 debugfs_remove(fotg210_debug_root);
5718 fotg210_debug_root = NULL;
5720 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5723 module_init(fotg210_hcd_init);
5725 static void __exit fotg210_hcd_cleanup(void)
5727 platform_driver_unregister(&fotg210_hcd_driver);
5728 debugfs_remove(fotg210_debug_root);
5729 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5731 module_exit(fotg210_hcd_cleanup);