1 // SPDX-License-Identifier: GPL-2.0+
2 /* Faraday FOTG210 EHCI-like driver
4 * Copyright (c) 2013 Faraday Technology Corporation
6 * Author: Yuan-Hsin Chen <yhchen@faraday-tech.com>
7 * Feng-Hsin Chiang <john453@faraday-tech.com>
8 * Po-Yu Chuang <ratbert.chuang@gmail.com>
10 * Most of code borrowed from the Linux-3.7 EHCI driver
12 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/dmapool.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/ioport.h>
19 #include <linux/sched.h>
20 #include <linux/vmalloc.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/hrtimer.h>
24 #include <linux/list.h>
25 #include <linux/interrupt.h>
26 #include <linux/usb.h>
27 #include <linux/usb/hcd.h>
28 #include <linux/moduleparam.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/debugfs.h>
31 #include <linux/slab.h>
32 #include <linux/uaccess.h>
33 #include <linux/platform_device.h>
35 #include <linux/clk.h>
37 #include <asm/byteorder.h>
39 #include <asm/unaligned.h>
41 #define DRIVER_AUTHOR "Yuan-Hsin Chen"
42 #define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
43 static const char hcd_name
[] = "fotg210_hcd";
45 #undef FOTG210_URB_TRACE
48 /* magic numbers that can affect system performance */
49 #define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
50 #define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
51 #define FOTG210_TUNE_RL_TT 0
52 #define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
53 #define FOTG210_TUNE_MULT_TT 1
55 /* Some drivers think it's safe to schedule isochronous transfers more than 256
56 * ms into the future (partly as a result of an old bug in the scheduling
57 * code). In an attempt to avoid trouble, we will use a minimum scheduling
58 * length of 512 frames instead of 256.
60 #define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
62 /* Initial IRQ latency: faster than hw default */
63 static int log2_irq_thresh
; /* 0 to 6 */
64 module_param(log2_irq_thresh
, int, S_IRUGO
);
65 MODULE_PARM_DESC(log2_irq_thresh
, "log2 IRQ latency, 1-64 microframes");
67 /* initial park setting: slower than hw default */
69 module_param(park
, uint
, S_IRUGO
);
70 MODULE_PARM_DESC(park
, "park setting; 1-3 back-to-back async packets");
72 /* for link power management(LPM) feature */
73 static unsigned int hird
;
74 module_param(hird
, int, S_IRUGO
);
75 MODULE_PARM_DESC(hird
, "host initiated resume duration, +1 for each 75us");
77 #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
81 #define fotg210_dbg(fotg210, fmt, args...) \
82 dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
83 #define fotg210_err(fotg210, fmt, args...) \
84 dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
85 #define fotg210_info(fotg210, fmt, args...) \
86 dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
87 #define fotg210_warn(fotg210, fmt, args...) \
88 dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
90 /* check the values in the HCSPARAMS register (host controller _Structural_
91 * parameters) see EHCI spec, Table 2-4 for each value
93 static void dbg_hcs_params(struct fotg210_hcd
*fotg210
, char *label
)
95 u32 params
= fotg210_readl(fotg210
, &fotg210
->caps
->hcs_params
);
97 fotg210_dbg(fotg210
, "%s hcs_params 0x%x ports=%d\n", label
, params
,
101 /* check the values in the HCCPARAMS register (host controller _Capability_
102 * parameters) see EHCI Spec, Table 2-5 for each value
104 static void dbg_hcc_params(struct fotg210_hcd
*fotg210
, char *label
)
106 u32 params
= fotg210_readl(fotg210
, &fotg210
->caps
->hcc_params
);
108 fotg210_dbg(fotg210
, "%s hcc_params %04x uframes %s%s\n", label
,
110 HCC_PGM_FRAMELISTLEN(params
) ? "256/512/1024" : "1024",
111 HCC_CANPARK(params
) ? " park" : "");
114 static void __maybe_unused
115 dbg_qtd(const char *label
, struct fotg210_hcd
*fotg210
, struct fotg210_qtd
*qtd
)
117 fotg210_dbg(fotg210
, "%s td %p n%08x %08x t%08x p0=%08x\n", label
, qtd
,
118 hc32_to_cpup(fotg210
, &qtd
->hw_next
),
119 hc32_to_cpup(fotg210
, &qtd
->hw_alt_next
),
120 hc32_to_cpup(fotg210
, &qtd
->hw_token
),
121 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[0]));
123 fotg210_dbg(fotg210
, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
124 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[1]),
125 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[2]),
126 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[3]),
127 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[4]));
130 static void __maybe_unused
131 dbg_qh(const char *label
, struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
133 struct fotg210_qh_hw
*hw
= qh
->hw
;
135 fotg210_dbg(fotg210
, "%s qh %p n%08x info %x %x qtd %x\n", label
, qh
,
136 hw
->hw_next
, hw
->hw_info1
, hw
->hw_info2
,
139 dbg_qtd("overlay", fotg210
, (struct fotg210_qtd
*) &hw
->hw_qtd_next
);
142 static void __maybe_unused
143 dbg_itd(const char *label
, struct fotg210_hcd
*fotg210
, struct fotg210_itd
*itd
)
145 fotg210_dbg(fotg210
, "%s[%d] itd %p, next %08x, urb %p\n", label
,
146 itd
->frame
, itd
, hc32_to_cpu(fotg210
, itd
->hw_next
),
150 " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
151 hc32_to_cpu(fotg210
, itd
->hw_transaction
[0]),
152 hc32_to_cpu(fotg210
, itd
->hw_transaction
[1]),
153 hc32_to_cpu(fotg210
, itd
->hw_transaction
[2]),
154 hc32_to_cpu(fotg210
, itd
->hw_transaction
[3]),
155 hc32_to_cpu(fotg210
, itd
->hw_transaction
[4]),
156 hc32_to_cpu(fotg210
, itd
->hw_transaction
[5]),
157 hc32_to_cpu(fotg210
, itd
->hw_transaction
[6]),
158 hc32_to_cpu(fotg210
, itd
->hw_transaction
[7]));
161 " buf: %08x %08x %08x %08x %08x %08x %08x\n",
162 hc32_to_cpu(fotg210
, itd
->hw_bufp
[0]),
163 hc32_to_cpu(fotg210
, itd
->hw_bufp
[1]),
164 hc32_to_cpu(fotg210
, itd
->hw_bufp
[2]),
165 hc32_to_cpu(fotg210
, itd
->hw_bufp
[3]),
166 hc32_to_cpu(fotg210
, itd
->hw_bufp
[4]),
167 hc32_to_cpu(fotg210
, itd
->hw_bufp
[5]),
168 hc32_to_cpu(fotg210
, itd
->hw_bufp
[6]));
170 fotg210_dbg(fotg210
, " index: %d %d %d %d %d %d %d %d\n",
171 itd
->index
[0], itd
->index
[1], itd
->index
[2],
172 itd
->index
[3], itd
->index
[4], itd
->index
[5],
173 itd
->index
[6], itd
->index
[7]);
176 static int __maybe_unused
177 dbg_status_buf(char *buf
, unsigned len
, const char *label
, u32 status
)
179 return scnprintf(buf
, len
, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
180 label
, label
[0] ? " " : "", status
,
181 (status
& STS_ASS
) ? " Async" : "",
182 (status
& STS_PSS
) ? " Periodic" : "",
183 (status
& STS_RECL
) ? " Recl" : "",
184 (status
& STS_HALT
) ? " Halt" : "",
185 (status
& STS_IAA
) ? " IAA" : "",
186 (status
& STS_FATAL
) ? " FATAL" : "",
187 (status
& STS_FLR
) ? " FLR" : "",
188 (status
& STS_PCD
) ? " PCD" : "",
189 (status
& STS_ERR
) ? " ERR" : "",
190 (status
& STS_INT
) ? " INT" : "");
193 static int __maybe_unused
194 dbg_intr_buf(char *buf
, unsigned len
, const char *label
, u32 enable
)
196 return scnprintf(buf
, len
, "%s%sintrenable %02x%s%s%s%s%s%s",
197 label
, label
[0] ? " " : "", enable
,
198 (enable
& STS_IAA
) ? " IAA" : "",
199 (enable
& STS_FATAL
) ? " FATAL" : "",
200 (enable
& STS_FLR
) ? " FLR" : "",
201 (enable
& STS_PCD
) ? " PCD" : "",
202 (enable
& STS_ERR
) ? " ERR" : "",
203 (enable
& STS_INT
) ? " INT" : "");
206 static const char *const fls_strings
[] = { "1024", "512", "256", "??" };
208 static int dbg_command_buf(char *buf
, unsigned len
, const char *label
,
211 return scnprintf(buf
, len
,
212 "%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s",
213 label
, label
[0] ? " " : "", command
,
214 (command
& CMD_PARK
) ? " park" : "(park)",
215 CMD_PARK_CNT(command
),
216 (command
>> 16) & 0x3f,
217 (command
& CMD_IAAD
) ? " IAAD" : "",
218 (command
& CMD_ASE
) ? " Async" : "",
219 (command
& CMD_PSE
) ? " Periodic" : "",
220 fls_strings
[(command
>> 2) & 0x3],
221 (command
& CMD_RESET
) ? " Reset" : "",
222 (command
& CMD_RUN
) ? "RUN" : "HALT");
225 static char *dbg_port_buf(char *buf
, unsigned len
, const char *label
, int port
,
230 /* signaling state */
231 switch (status
& (3 << 10)) {
237 break; /* low speed */
246 scnprintf(buf
, len
, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s",
247 label
, label
[0] ? " " : "", port
, status
,
248 status
>> 25, /*device address */
250 (status
& PORT_RESET
) ? " RESET" : "",
251 (status
& PORT_SUSPEND
) ? " SUSPEND" : "",
252 (status
& PORT_RESUME
) ? " RESUME" : "",
253 (status
& PORT_PEC
) ? " PEC" : "",
254 (status
& PORT_PE
) ? " PE" : "",
255 (status
& PORT_CSC
) ? " CSC" : "",
256 (status
& PORT_CONNECT
) ? " CONNECT" : "");
261 /* functions have the "wrong" filename when they're output... */
262 #define dbg_status(fotg210, label, status) { \
264 dbg_status_buf(_buf, sizeof(_buf), label, status); \
265 fotg210_dbg(fotg210, "%s\n", _buf); \
268 #define dbg_cmd(fotg210, label, command) { \
270 dbg_command_buf(_buf, sizeof(_buf), label, command); \
271 fotg210_dbg(fotg210, "%s\n", _buf); \
274 #define dbg_port(fotg210, label, port, status) { \
276 fotg210_dbg(fotg210, "%s\n", \
277 dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
280 /* troubleshooting help: expose state in debugfs */
281 static int debug_async_open(struct inode
*, struct file
*);
282 static int debug_periodic_open(struct inode
*, struct file
*);
283 static int debug_registers_open(struct inode
*, struct file
*);
284 static int debug_async_open(struct inode
*, struct file
*);
286 static ssize_t
debug_output(struct file
*, char __user
*, size_t, loff_t
*);
287 static int debug_close(struct inode
*, struct file
*);
289 static const struct file_operations debug_async_fops
= {
290 .owner
= THIS_MODULE
,
291 .open
= debug_async_open
,
292 .read
= debug_output
,
293 .release
= debug_close
,
294 .llseek
= default_llseek
,
296 static const struct file_operations debug_periodic_fops
= {
297 .owner
= THIS_MODULE
,
298 .open
= debug_periodic_open
,
299 .read
= debug_output
,
300 .release
= debug_close
,
301 .llseek
= default_llseek
,
303 static const struct file_operations debug_registers_fops
= {
304 .owner
= THIS_MODULE
,
305 .open
= debug_registers_open
,
306 .read
= debug_output
,
307 .release
= debug_close
,
308 .llseek
= default_llseek
,
311 static struct dentry
*fotg210_debug_root
;
313 struct debug_buffer
{
314 ssize_t (*fill_func
)(struct debug_buffer
*); /* fill method */
316 struct mutex mutex
; /* protect filling of buffer */
317 size_t count
; /* number of characters filled into buffer */
322 static inline char speed_char(u32 scratch
)
324 switch (scratch
& (3 << 12)) {
339 static inline char token_mark(struct fotg210_hcd
*fotg210
, __hc32 token
)
341 __u32 v
= hc32_to_cpu(fotg210
, token
);
343 if (v
& QTD_STS_ACTIVE
)
345 if (v
& QTD_STS_HALT
)
347 if (!IS_SHORT_READ(v
))
349 /* tries to advance through hw_alt_next */
353 static void qh_lines(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
,
354 char **nextp
, unsigned *sizep
)
358 struct fotg210_qtd
*td
;
360 unsigned size
= *sizep
;
363 __le32 list_end
= FOTG210_LIST_END(fotg210
);
364 struct fotg210_qh_hw
*hw
= qh
->hw
;
366 if (hw
->hw_qtd_next
== list_end
) /* NEC does this */
369 mark
= token_mark(fotg210
, hw
->hw_token
);
370 if (mark
== '/') { /* qh_alt_next controls qh advance? */
371 if ((hw
->hw_alt_next
& QTD_MASK(fotg210
)) ==
372 fotg210
->async
->hw
->hw_alt_next
)
373 mark
= '#'; /* blocked */
374 else if (hw
->hw_alt_next
== list_end
)
375 mark
= '.'; /* use hw_qtd_next */
376 /* else alt_next points to some other qtd */
378 scratch
= hc32_to_cpup(fotg210
, &hw
->hw_info1
);
379 hw_curr
= (mark
== '*') ? hc32_to_cpup(fotg210
, &hw
->hw_current
) : 0;
380 temp
= scnprintf(next
, size
,
381 "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
382 qh
, scratch
& 0x007f,
384 (scratch
>> 8) & 0x000f,
385 scratch
, hc32_to_cpup(fotg210
, &hw
->hw_info2
),
386 hc32_to_cpup(fotg210
, &hw
->hw_token
), mark
,
387 (cpu_to_hc32(fotg210
, QTD_TOGGLE
) & hw
->hw_token
)
389 (hc32_to_cpup(fotg210
, &hw
->hw_alt_next
) >> 1) & 0x0f);
393 /* hc may be modifying the list as we read it ... */
394 list_for_each_entry(td
, &qh
->qtd_list
, qtd_list
) {
395 scratch
= hc32_to_cpup(fotg210
, &td
->hw_token
);
397 if (hw_curr
== td
->qtd_dma
)
399 else if (hw
->hw_qtd_next
== cpu_to_hc32(fotg210
, td
->qtd_dma
))
401 else if (QTD_LENGTH(scratch
)) {
402 if (td
->hw_alt_next
== fotg210
->async
->hw
->hw_alt_next
)
404 else if (td
->hw_alt_next
!= list_end
)
407 temp
= snprintf(next
, size
,
408 "\n\t%p%c%s len=%d %08x urb %p",
409 td
, mark
, ({ char *tmp
;
410 switch ((scratch
>>8)&0x03) {
424 (scratch
>> 16) & 0x7fff,
435 temp
= snprintf(next
, size
, "\n");
447 static ssize_t
fill_async_buffer(struct debug_buffer
*buf
)
450 struct fotg210_hcd
*fotg210
;
454 struct fotg210_qh
*qh
;
456 hcd
= bus_to_hcd(buf
->bus
);
457 fotg210
= hcd_to_fotg210(hcd
);
458 next
= buf
->output_buf
;
459 size
= buf
->alloc_size
;
463 /* dumps a snapshot of the async schedule.
464 * usually empty except for long-term bulk reads, or head.
465 * one QH per line, and TDs we know about
467 spin_lock_irqsave(&fotg210
->lock
, flags
);
468 for (qh
= fotg210
->async
->qh_next
.qh
; size
> 0 && qh
;
470 qh_lines(fotg210
, qh
, &next
, &size
);
471 if (fotg210
->async_unlink
&& size
> 0) {
472 temp
= scnprintf(next
, size
, "\nunlink =\n");
476 for (qh
= fotg210
->async_unlink
; size
> 0 && qh
;
477 qh
= qh
->unlink_next
)
478 qh_lines(fotg210
, qh
, &next
, &size
);
480 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
482 return strlen(buf
->output_buf
);
485 /* count tds, get ep direction */
486 static unsigned output_buf_tds_dir(char *buf
, struct fotg210_hcd
*fotg210
,
487 struct fotg210_qh_hw
*hw
, struct fotg210_qh
*qh
, unsigned size
)
489 u32 scratch
= hc32_to_cpup(fotg210
, &hw
->hw_info1
);
490 struct fotg210_qtd
*qtd
;
494 /* count tds, get ep direction */
495 list_for_each_entry(qtd
, &qh
->qtd_list
, qtd_list
) {
497 switch ((hc32_to_cpu(fotg210
, qtd
->hw_token
) >> 8) & 0x03) {
507 return scnprintf(buf
, size
, "(%c%d ep%d%s [%d/%d] q%d p%d)",
508 speed_char(scratch
), scratch
& 0x007f,
509 (scratch
>> 8) & 0x000f, type
, qh
->usecs
,
510 qh
->c_usecs
, temp
, (scratch
>> 16) & 0x7ff);
513 #define DBG_SCHED_LIMIT 64
514 static ssize_t
fill_periodic_buffer(struct debug_buffer
*buf
)
517 struct fotg210_hcd
*fotg210
;
519 union fotg210_shadow p
, *seen
;
520 unsigned temp
, size
, seen_count
;
525 seen
= kmalloc_array(DBG_SCHED_LIMIT
, sizeof(*seen
), GFP_ATOMIC
);
531 hcd
= bus_to_hcd(buf
->bus
);
532 fotg210
= hcd_to_fotg210(hcd
);
533 next
= buf
->output_buf
;
534 size
= buf
->alloc_size
;
536 temp
= scnprintf(next
, size
, "size = %d\n", fotg210
->periodic_size
);
540 /* dump a snapshot of the periodic schedule.
541 * iso changes, interrupt usually doesn't.
543 spin_lock_irqsave(&fotg210
->lock
, flags
);
544 for (i
= 0; i
< fotg210
->periodic_size
; i
++) {
545 p
= fotg210
->pshadow
[i
];
549 tag
= Q_NEXT_TYPE(fotg210
, fotg210
->periodic
[i
]);
551 temp
= scnprintf(next
, size
, "%4d: ", i
);
556 struct fotg210_qh_hw
*hw
;
558 switch (hc32_to_cpu(fotg210
, tag
)) {
561 temp
= scnprintf(next
, size
, " qh%d-%04x/%p",
563 hc32_to_cpup(fotg210
,
566 & (QH_CMASK
| QH_SMASK
),
570 /* don't repeat what follows this qh */
571 for (temp
= 0; temp
< seen_count
; temp
++) {
572 if (seen
[temp
].ptr
!= p
.ptr
)
574 if (p
.qh
->qh_next
.ptr
) {
575 temp
= scnprintf(next
, size
,
582 /* show more info the first time around */
583 if (temp
== seen_count
) {
584 temp
= output_buf_tds_dir(next
,
588 if (seen_count
< DBG_SCHED_LIMIT
)
589 seen
[seen_count
++].qh
= p
.qh
;
592 tag
= Q_NEXT_TYPE(fotg210
, hw
->hw_next
);
596 temp
= scnprintf(next
, size
,
598 p
.fstn
->hw_prev
, p
.fstn
);
599 tag
= Q_NEXT_TYPE(fotg210
, p
.fstn
->hw_next
);
600 p
= p
.fstn
->fstn_next
;
603 temp
= scnprintf(next
, size
,
605 tag
= Q_NEXT_TYPE(fotg210
, p
.itd
->hw_next
);
613 temp
= scnprintf(next
, size
, "\n");
617 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
620 return buf
->alloc_size
- size
;
622 #undef DBG_SCHED_LIMIT
624 static const char *rh_state_string(struct fotg210_hcd
*fotg210
)
626 switch (fotg210
->rh_state
) {
627 case FOTG210_RH_HALTED
:
629 case FOTG210_RH_SUSPENDED
:
631 case FOTG210_RH_RUNNING
:
633 case FOTG210_RH_STOPPING
:
639 static ssize_t
fill_registers_buffer(struct debug_buffer
*buf
)
642 struct fotg210_hcd
*fotg210
;
644 unsigned temp
, size
, i
;
645 char *next
, scratch
[80];
646 static const char fmt
[] = "%*s\n";
647 static const char label
[] = "";
649 hcd
= bus_to_hcd(buf
->bus
);
650 fotg210
= hcd_to_fotg210(hcd
);
651 next
= buf
->output_buf
;
652 size
= buf
->alloc_size
;
654 spin_lock_irqsave(&fotg210
->lock
, flags
);
656 if (!HCD_HW_ACCESSIBLE(hcd
)) {
657 size
= scnprintf(next
, size
,
658 "bus %s, device %s\n"
660 "SUSPENDED(no register access)\n",
661 hcd
->self
.controller
->bus
->name
,
662 dev_name(hcd
->self
.controller
),
667 /* Capability Registers */
668 i
= HC_VERSION(fotg210
, fotg210_readl(fotg210
,
669 &fotg210
->caps
->hc_capbase
));
670 temp
= scnprintf(next
, size
,
671 "bus %s, device %s\n"
673 "EHCI %x.%02x, rh state %s\n",
674 hcd
->self
.controller
->bus
->name
,
675 dev_name(hcd
->self
.controller
),
677 i
>> 8, i
& 0x0ff, rh_state_string(fotg210
));
681 /* FIXME interpret both types of params */
682 i
= fotg210_readl(fotg210
, &fotg210
->caps
->hcs_params
);
683 temp
= scnprintf(next
, size
, "structural params 0x%08x\n", i
);
687 i
= fotg210_readl(fotg210
, &fotg210
->caps
->hcc_params
);
688 temp
= scnprintf(next
, size
, "capability params 0x%08x\n", i
);
692 /* Operational Registers */
693 temp
= dbg_status_buf(scratch
, sizeof(scratch
), label
,
694 fotg210_readl(fotg210
, &fotg210
->regs
->status
));
695 temp
= scnprintf(next
, size
, fmt
, temp
, scratch
);
699 temp
= dbg_command_buf(scratch
, sizeof(scratch
), label
,
700 fotg210_readl(fotg210
, &fotg210
->regs
->command
));
701 temp
= scnprintf(next
, size
, fmt
, temp
, scratch
);
705 temp
= dbg_intr_buf(scratch
, sizeof(scratch
), label
,
706 fotg210_readl(fotg210
, &fotg210
->regs
->intr_enable
));
707 temp
= scnprintf(next
, size
, fmt
, temp
, scratch
);
711 temp
= scnprintf(next
, size
, "uframe %04x\n",
712 fotg210_read_frame_index(fotg210
));
716 if (fotg210
->async_unlink
) {
717 temp
= scnprintf(next
, size
, "async unlink qh %p\n",
718 fotg210
->async_unlink
);
724 temp
= scnprintf(next
, size
,
725 "irq normal %ld err %ld iaa %ld(lost %ld)\n",
726 fotg210
->stats
.normal
, fotg210
->stats
.error
,
727 fotg210
->stats
.iaa
, fotg210
->stats
.lost_iaa
);
731 temp
= scnprintf(next
, size
, "complete %ld unlink %ld\n",
732 fotg210
->stats
.complete
, fotg210
->stats
.unlink
);
738 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
740 return buf
->alloc_size
- size
;
743 static struct debug_buffer
744 *alloc_buffer(struct usb_bus
*bus
, ssize_t (*fill_func
)(struct debug_buffer
*))
746 struct debug_buffer
*buf
;
748 buf
= kzalloc(sizeof(struct debug_buffer
), GFP_KERNEL
);
752 buf
->fill_func
= fill_func
;
753 mutex_init(&buf
->mutex
);
754 buf
->alloc_size
= PAGE_SIZE
;
760 static int fill_buffer(struct debug_buffer
*buf
)
764 if (!buf
->output_buf
)
765 buf
->output_buf
= vmalloc(buf
->alloc_size
);
767 if (!buf
->output_buf
) {
772 ret
= buf
->fill_func(buf
);
783 static ssize_t
debug_output(struct file
*file
, char __user
*user_buf
,
784 size_t len
, loff_t
*offset
)
786 struct debug_buffer
*buf
= file
->private_data
;
789 mutex_lock(&buf
->mutex
);
790 if (buf
->count
== 0) {
791 ret
= fill_buffer(buf
);
793 mutex_unlock(&buf
->mutex
);
797 mutex_unlock(&buf
->mutex
);
799 ret
= simple_read_from_buffer(user_buf
, len
, offset
,
800 buf
->output_buf
, buf
->count
);
807 static int debug_close(struct inode
*inode
, struct file
*file
)
809 struct debug_buffer
*buf
= file
->private_data
;
812 vfree(buf
->output_buf
);
818 static int debug_async_open(struct inode
*inode
, struct file
*file
)
820 file
->private_data
= alloc_buffer(inode
->i_private
, fill_async_buffer
);
822 return file
->private_data
? 0 : -ENOMEM
;
825 static int debug_periodic_open(struct inode
*inode
, struct file
*file
)
827 struct debug_buffer
*buf
;
829 buf
= alloc_buffer(inode
->i_private
, fill_periodic_buffer
);
833 buf
->alloc_size
= (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE
;
834 file
->private_data
= buf
;
838 static int debug_registers_open(struct inode
*inode
, struct file
*file
)
840 file
->private_data
= alloc_buffer(inode
->i_private
,
841 fill_registers_buffer
);
843 return file
->private_data
? 0 : -ENOMEM
;
846 static inline void create_debug_files(struct fotg210_hcd
*fotg210
)
848 struct usb_bus
*bus
= &fotg210_to_hcd(fotg210
)->self
;
851 root
= debugfs_create_dir(bus
->bus_name
, fotg210_debug_root
);
852 fotg210
->debug_dir
= root
;
854 debugfs_create_file("async", S_IRUGO
, root
, bus
, &debug_async_fops
);
855 debugfs_create_file("periodic", S_IRUGO
, root
, bus
,
856 &debug_periodic_fops
);
857 debugfs_create_file("registers", S_IRUGO
, root
, bus
,
858 &debug_registers_fops
);
861 static inline void remove_debug_files(struct fotg210_hcd
*fotg210
)
863 debugfs_remove_recursive(fotg210
->debug_dir
);
866 /* handshake - spin reading hc until handshake completes or fails
867 * @ptr: address of hc register to be read
868 * @mask: bits to look at in result of read
869 * @done: value of those bits when handshake succeeds
870 * @usec: timeout in microseconds
872 * Returns negative errno, or zero on success
874 * Success happens when the "mask" bits have the specified value (hardware
875 * handshake done). There are two failure modes: "usec" have passed (major
876 * hardware flakeout), or the register reads as all-ones (hardware removed).
878 * That last failure should_only happen in cases like physical cardbus eject
879 * before driver shutdown. But it also seems to be caused by bugs in cardbus
880 * bridge shutdown: shutting down the bridge before the devices using it.
882 static int handshake(struct fotg210_hcd
*fotg210
, void __iomem
*ptr
,
883 u32 mask
, u32 done
, int usec
)
888 result
= fotg210_readl(fotg210
, ptr
);
889 if (result
== ~(u32
)0) /* card removed */
900 /* Force HC to halt state from unknown (EHCI spec section 2.3).
901 * Must be called with interrupts enabled and the lock not held.
903 static int fotg210_halt(struct fotg210_hcd
*fotg210
)
907 spin_lock_irq(&fotg210
->lock
);
909 /* disable any irqs left enabled by previous code */
910 fotg210_writel(fotg210
, 0, &fotg210
->regs
->intr_enable
);
913 * This routine gets called during probe before fotg210->command
914 * has been initialized, so we can't rely on its value.
916 fotg210
->command
&= ~CMD_RUN
;
917 temp
= fotg210_readl(fotg210
, &fotg210
->regs
->command
);
918 temp
&= ~(CMD_RUN
| CMD_IAAD
);
919 fotg210_writel(fotg210
, temp
, &fotg210
->regs
->command
);
921 spin_unlock_irq(&fotg210
->lock
);
922 synchronize_irq(fotg210_to_hcd(fotg210
)->irq
);
924 return handshake(fotg210
, &fotg210
->regs
->status
,
925 STS_HALT
, STS_HALT
, 16 * 125);
928 /* Reset a non-running (STS_HALT == 1) controller.
929 * Must be called with interrupts enabled and the lock not held.
931 static int fotg210_reset(struct fotg210_hcd
*fotg210
)
934 u32 command
= fotg210_readl(fotg210
, &fotg210
->regs
->command
);
936 /* If the EHCI debug controller is active, special care must be
937 * taken before and after a host controller reset
939 if (fotg210
->debug
&& !dbgp_reset_prep(fotg210_to_hcd(fotg210
)))
940 fotg210
->debug
= NULL
;
942 command
|= CMD_RESET
;
943 dbg_cmd(fotg210
, "reset", command
);
944 fotg210_writel(fotg210
, command
, &fotg210
->regs
->command
);
945 fotg210
->rh_state
= FOTG210_RH_HALTED
;
946 fotg210
->next_statechange
= jiffies
;
947 retval
= handshake(fotg210
, &fotg210
->regs
->command
,
948 CMD_RESET
, 0, 250 * 1000);
954 dbgp_external_startup(fotg210_to_hcd(fotg210
));
956 fotg210
->port_c_suspend
= fotg210
->suspended_ports
=
957 fotg210
->resuming_ports
= 0;
961 /* Idle the controller (turn off the schedules).
962 * Must be called with interrupts enabled and the lock not held.
964 static void fotg210_quiesce(struct fotg210_hcd
*fotg210
)
968 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
)
971 /* wait for any schedule enables/disables to take effect */
972 temp
= (fotg210
->command
<< 10) & (STS_ASS
| STS_PSS
);
973 handshake(fotg210
, &fotg210
->regs
->status
, STS_ASS
| STS_PSS
, temp
,
976 /* then disable anything that's still active */
977 spin_lock_irq(&fotg210
->lock
);
978 fotg210
->command
&= ~(CMD_ASE
| CMD_PSE
);
979 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
980 spin_unlock_irq(&fotg210
->lock
);
982 /* hardware can take 16 microframes to turn off ... */
983 handshake(fotg210
, &fotg210
->regs
->status
, STS_ASS
| STS_PSS
, 0,
987 static void end_unlink_async(struct fotg210_hcd
*fotg210
);
988 static void unlink_empty_async(struct fotg210_hcd
*fotg210
);
989 static void fotg210_work(struct fotg210_hcd
*fotg210
);
990 static void start_unlink_intr(struct fotg210_hcd
*fotg210
,
991 struct fotg210_qh
*qh
);
992 static void end_unlink_intr(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
);
994 /* Set a bit in the USBCMD register */
995 static void fotg210_set_command_bit(struct fotg210_hcd
*fotg210
, u32 bit
)
997 fotg210
->command
|= bit
;
998 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
1000 /* unblock posted write */
1001 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1004 /* Clear a bit in the USBCMD register */
1005 static void fotg210_clear_command_bit(struct fotg210_hcd
*fotg210
, u32 bit
)
1007 fotg210
->command
&= ~bit
;
1008 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
1010 /* unblock posted write */
1011 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1014 /* EHCI timer support... Now using hrtimers.
1016 * Lots of different events are triggered from fotg210->hrtimer. Whenever
1017 * the timer routine runs, it checks each possible event; events that are
1018 * currently enabled and whose expiration time has passed get handled.
1019 * The set of enabled events is stored as a collection of bitflags in
1020 * fotg210->enabled_hrtimer_events, and they are numbered in order of
1021 * increasing delay values (ranging between 1 ms and 100 ms).
1023 * Rather than implementing a sorted list or tree of all pending events,
1024 * we keep track only of the lowest-numbered pending event, in
1025 * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
1026 * expiration time is set to the timeout value for this event.
1028 * As a result, events might not get handled right away; the actual delay
1029 * could be anywhere up to twice the requested delay. This doesn't
1030 * matter, because none of the events are especially time-critical. The
1031 * ones that matter most all have a delay of 1 ms, so they will be
1032 * handled after 2 ms at most, which is okay. In addition to this, we
1033 * allow for an expiration range of 1 ms.
1036 /* Delay lengths for the hrtimer event types.
1037 * Keep this list sorted by delay length, in the same order as
1038 * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
1040 static unsigned event_delays_ns
[] = {
1041 1 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_POLL_ASS */
1042 1 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_POLL_PSS */
1043 1 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_POLL_DEAD */
1044 1125 * NSEC_PER_USEC
, /* FOTG210_HRTIMER_UNLINK_INTR */
1045 2 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_FREE_ITDS */
1046 6 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1047 10 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1048 10 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1049 15 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1050 100 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_IO_WATCHDOG */
1053 /* Enable a pending hrtimer event */
1054 static void fotg210_enable_event(struct fotg210_hcd
*fotg210
, unsigned event
,
1057 ktime_t
*timeout
= &fotg210
->hr_timeouts
[event
];
1060 *timeout
= ktime_add(ktime_get(), event_delays_ns
[event
]);
1061 fotg210
->enabled_hrtimer_events
|= (1 << event
);
1063 /* Track only the lowest-numbered pending event */
1064 if (event
< fotg210
->next_hrtimer_event
) {
1065 fotg210
->next_hrtimer_event
= event
;
1066 hrtimer_start_range_ns(&fotg210
->hrtimer
, *timeout
,
1067 NSEC_PER_MSEC
, HRTIMER_MODE_ABS
);
1072 /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
1073 static void fotg210_poll_ASS(struct fotg210_hcd
*fotg210
)
1075 unsigned actual
, want
;
1077 /* Don't enable anything if the controller isn't running (e.g., died) */
1078 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
)
1081 want
= (fotg210
->command
& CMD_ASE
) ? STS_ASS
: 0;
1082 actual
= fotg210_readl(fotg210
, &fotg210
->regs
->status
) & STS_ASS
;
1084 if (want
!= actual
) {
1086 /* Poll again later, but give up after about 20 ms */
1087 if (fotg210
->ASS_poll_count
++ < 20) {
1088 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_POLL_ASS
,
1092 fotg210_dbg(fotg210
, "Waited too long for the async schedule status (%x/%x), giving up\n",
1095 fotg210
->ASS_poll_count
= 0;
1097 /* The status is up-to-date; restart or stop the schedule as needed */
1098 if (want
== 0) { /* Stopped */
1099 if (fotg210
->async_count
> 0)
1100 fotg210_set_command_bit(fotg210
, CMD_ASE
);
1102 } else { /* Running */
1103 if (fotg210
->async_count
== 0) {
1105 /* Turn off the schedule after a while */
1106 fotg210_enable_event(fotg210
,
1107 FOTG210_HRTIMER_DISABLE_ASYNC
,
1113 /* Turn off the async schedule after a brief delay */
1114 static void fotg210_disable_ASE(struct fotg210_hcd
*fotg210
)
1116 fotg210_clear_command_bit(fotg210
, CMD_ASE
);
1120 /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
1121 static void fotg210_poll_PSS(struct fotg210_hcd
*fotg210
)
1123 unsigned actual
, want
;
1125 /* Don't do anything if the controller isn't running (e.g., died) */
1126 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
)
1129 want
= (fotg210
->command
& CMD_PSE
) ? STS_PSS
: 0;
1130 actual
= fotg210_readl(fotg210
, &fotg210
->regs
->status
) & STS_PSS
;
1132 if (want
!= actual
) {
1134 /* Poll again later, but give up after about 20 ms */
1135 if (fotg210
->PSS_poll_count
++ < 20) {
1136 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_POLL_PSS
,
1140 fotg210_dbg(fotg210
, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
1143 fotg210
->PSS_poll_count
= 0;
1145 /* The status is up-to-date; restart or stop the schedule as needed */
1146 if (want
== 0) { /* Stopped */
1147 if (fotg210
->periodic_count
> 0)
1148 fotg210_set_command_bit(fotg210
, CMD_PSE
);
1150 } else { /* Running */
1151 if (fotg210
->periodic_count
== 0) {
1153 /* Turn off the schedule after a while */
1154 fotg210_enable_event(fotg210
,
1155 FOTG210_HRTIMER_DISABLE_PERIODIC
,
1161 /* Turn off the periodic schedule after a brief delay */
1162 static void fotg210_disable_PSE(struct fotg210_hcd
*fotg210
)
1164 fotg210_clear_command_bit(fotg210
, CMD_PSE
);
1168 /* Poll the STS_HALT status bit; see when a dead controller stops */
1169 static void fotg210_handle_controller_death(struct fotg210_hcd
*fotg210
)
1171 if (!(fotg210_readl(fotg210
, &fotg210
->regs
->status
) & STS_HALT
)) {
1173 /* Give up after a few milliseconds */
1174 if (fotg210
->died_poll_count
++ < 5) {
1175 /* Try again later */
1176 fotg210_enable_event(fotg210
,
1177 FOTG210_HRTIMER_POLL_DEAD
, true);
1180 fotg210_warn(fotg210
, "Waited too long for the controller to stop, giving up\n");
1183 /* Clean up the mess */
1184 fotg210
->rh_state
= FOTG210_RH_HALTED
;
1185 fotg210_writel(fotg210
, 0, &fotg210
->regs
->intr_enable
);
1186 fotg210_work(fotg210
);
1187 end_unlink_async(fotg210
);
1189 /* Not in process context, so don't try to reset the controller */
1193 /* Handle unlinked interrupt QHs once they are gone from the hardware */
1194 static void fotg210_handle_intr_unlinks(struct fotg210_hcd
*fotg210
)
1196 bool stopped
= (fotg210
->rh_state
< FOTG210_RH_RUNNING
);
1199 * Process all the QHs on the intr_unlink list that were added
1200 * before the current unlink cycle began. The list is in
1201 * temporal order, so stop when we reach the first entry in the
1202 * current cycle. But if the root hub isn't running then
1203 * process all the QHs on the list.
1205 fotg210
->intr_unlinking
= true;
1206 while (fotg210
->intr_unlink
) {
1207 struct fotg210_qh
*qh
= fotg210
->intr_unlink
;
1209 if (!stopped
&& qh
->unlink_cycle
== fotg210
->intr_unlink_cycle
)
1211 fotg210
->intr_unlink
= qh
->unlink_next
;
1212 qh
->unlink_next
= NULL
;
1213 end_unlink_intr(fotg210
, qh
);
1216 /* Handle remaining entries later */
1217 if (fotg210
->intr_unlink
) {
1218 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_UNLINK_INTR
,
1220 ++fotg210
->intr_unlink_cycle
;
1222 fotg210
->intr_unlinking
= false;
1226 /* Start another free-iTDs/siTDs cycle */
1227 static void start_free_itds(struct fotg210_hcd
*fotg210
)
1229 if (!(fotg210
->enabled_hrtimer_events
&
1230 BIT(FOTG210_HRTIMER_FREE_ITDS
))) {
1231 fotg210
->last_itd_to_free
= list_entry(
1232 fotg210
->cached_itd_list
.prev
,
1233 struct fotg210_itd
, itd_list
);
1234 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_FREE_ITDS
, true);
1238 /* Wait for controller to stop using old iTDs and siTDs */
1239 static void end_free_itds(struct fotg210_hcd
*fotg210
)
1241 struct fotg210_itd
*itd
, *n
;
1243 if (fotg210
->rh_state
< FOTG210_RH_RUNNING
)
1244 fotg210
->last_itd_to_free
= NULL
;
1246 list_for_each_entry_safe(itd
, n
, &fotg210
->cached_itd_list
, itd_list
) {
1247 list_del(&itd
->itd_list
);
1248 dma_pool_free(fotg210
->itd_pool
, itd
, itd
->itd_dma
);
1249 if (itd
== fotg210
->last_itd_to_free
)
1253 if (!list_empty(&fotg210
->cached_itd_list
))
1254 start_free_itds(fotg210
);
1258 /* Handle lost (or very late) IAA interrupts */
1259 static void fotg210_iaa_watchdog(struct fotg210_hcd
*fotg210
)
1261 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
)
1265 * Lost IAA irqs wedge things badly; seen first with a vt8235.
1266 * So we need this watchdog, but must protect it against both
1267 * (a) SMP races against real IAA firing and retriggering, and
1268 * (b) clean HC shutdown, when IAA watchdog was pending.
1270 if (fotg210
->async_iaa
) {
1273 /* If we get here, IAA is *REALLY* late. It's barely
1274 * conceivable that the system is so busy that CMD_IAAD
1275 * is still legitimately set, so let's be sure it's
1276 * clear before we read STS_IAA. (The HC should clear
1277 * CMD_IAAD when it sets STS_IAA.)
1279 cmd
= fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1282 * If IAA is set here it either legitimately triggered
1283 * after the watchdog timer expired (_way_ late, so we'll
1284 * still count it as lost) ... or a silicon erratum:
1285 * - VIA seems to set IAA without triggering the IRQ;
1286 * - IAAD potentially cleared without setting IAA.
1288 status
= fotg210_readl(fotg210
, &fotg210
->regs
->status
);
1289 if ((status
& STS_IAA
) || !(cmd
& CMD_IAAD
)) {
1290 INCR(fotg210
->stats
.lost_iaa
);
1291 fotg210_writel(fotg210
, STS_IAA
,
1292 &fotg210
->regs
->status
);
1295 fotg210_dbg(fotg210
, "IAA watchdog: status %x cmd %x\n",
1297 end_unlink_async(fotg210
);
1302 /* Enable the I/O watchdog, if appropriate */
1303 static void turn_on_io_watchdog(struct fotg210_hcd
*fotg210
)
1305 /* Not needed if the controller isn't running or it's already enabled */
1306 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
||
1307 (fotg210
->enabled_hrtimer_events
&
1308 BIT(FOTG210_HRTIMER_IO_WATCHDOG
)))
1312 * Isochronous transfers always need the watchdog.
1313 * For other sorts we use it only if the flag is set.
1315 if (fotg210
->isoc_count
> 0 || (fotg210
->need_io_watchdog
&&
1316 fotg210
->async_count
+ fotg210
->intr_count
> 0))
1317 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_IO_WATCHDOG
,
1322 /* Handler functions for the hrtimer event types.
1323 * Keep this array in the same order as the event types indexed by
1324 * enum fotg210_hrtimer_event in fotg210.h.
1326 static void (*event_handlers
[])(struct fotg210_hcd
*) = {
1327 fotg210_poll_ASS
, /* FOTG210_HRTIMER_POLL_ASS */
1328 fotg210_poll_PSS
, /* FOTG210_HRTIMER_POLL_PSS */
1329 fotg210_handle_controller_death
, /* FOTG210_HRTIMER_POLL_DEAD */
1330 fotg210_handle_intr_unlinks
, /* FOTG210_HRTIMER_UNLINK_INTR */
1331 end_free_itds
, /* FOTG210_HRTIMER_FREE_ITDS */
1332 unlink_empty_async
, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1333 fotg210_iaa_watchdog
, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1334 fotg210_disable_PSE
, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1335 fotg210_disable_ASE
, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1336 fotg210_work
, /* FOTG210_HRTIMER_IO_WATCHDOG */
1339 static enum hrtimer_restart
fotg210_hrtimer_func(struct hrtimer
*t
)
1341 struct fotg210_hcd
*fotg210
=
1342 container_of(t
, struct fotg210_hcd
, hrtimer
);
1344 unsigned long events
;
1345 unsigned long flags
;
1348 spin_lock_irqsave(&fotg210
->lock
, flags
);
1350 events
= fotg210
->enabled_hrtimer_events
;
1351 fotg210
->enabled_hrtimer_events
= 0;
1352 fotg210
->next_hrtimer_event
= FOTG210_HRTIMER_NO_EVENT
;
1355 * Check each pending event. If its time has expired, handle
1356 * the event; otherwise re-enable it.
1359 for_each_set_bit(e
, &events
, FOTG210_HRTIMER_NUM_EVENTS
) {
1360 if (ktime_compare(now
, fotg210
->hr_timeouts
[e
]) >= 0)
1361 event_handlers
[e
](fotg210
);
1363 fotg210_enable_event(fotg210
, e
, false);
1366 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1367 return HRTIMER_NORESTART
;
1370 #define fotg210_bus_suspend NULL
1371 #define fotg210_bus_resume NULL
1373 static int check_reset_complete(struct fotg210_hcd
*fotg210
, int index
,
1374 u32 __iomem
*status_reg
, int port_status
)
1376 if (!(port_status
& PORT_CONNECT
))
1379 /* if reset finished and it's still not enabled -- handoff */
1380 if (!(port_status
& PORT_PE
))
1381 /* with integrated TT, there's nobody to hand it to! */
1382 fotg210_dbg(fotg210
, "Failed to enable port %d on root hub TT\n",
1385 fotg210_dbg(fotg210
, "port %d reset complete, port enabled\n",
1392 /* build "status change" packet (one or two bytes) from HC registers */
1394 static int fotg210_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1396 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
1400 unsigned long flags
;
1402 /* init status to no-changes */
1405 /* Inform the core about resumes-in-progress by returning
1406 * a non-zero value even if there are no status changes.
1408 status
= fotg210
->resuming_ports
;
1410 mask
= PORT_CSC
| PORT_PEC
;
1411 /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
1413 /* no hub change reports (bit 0) for now (power, ...) */
1415 /* port N changes (bit N)? */
1416 spin_lock_irqsave(&fotg210
->lock
, flags
);
1418 temp
= fotg210_readl(fotg210
, &fotg210
->regs
->port_status
);
1421 * Return status information even for ports with OWNER set.
1422 * Otherwise hub_wq wouldn't see the disconnect event when a
1423 * high-speed device is switched over to the companion
1424 * controller by the user.
1427 if ((temp
& mask
) != 0 || test_bit(0, &fotg210
->port_c_suspend
) ||
1428 (fotg210
->reset_done
[0] &&
1429 time_after_eq(jiffies
, fotg210
->reset_done
[0]))) {
1433 /* FIXME autosuspend idle root hubs */
1434 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1435 return status
? retval
: 0;
1438 static void fotg210_hub_descriptor(struct fotg210_hcd
*fotg210
,
1439 struct usb_hub_descriptor
*desc
)
1441 int ports
= HCS_N_PORTS(fotg210
->hcs_params
);
1444 desc
->bDescriptorType
= USB_DT_HUB
;
1445 desc
->bPwrOn2PwrGood
= 10; /* fotg210 1.0, 2.3.9 says 20ms max */
1446 desc
->bHubContrCurrent
= 0;
1448 desc
->bNbrPorts
= ports
;
1449 temp
= 1 + (ports
/ 8);
1450 desc
->bDescLength
= 7 + 2 * temp
;
1452 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1453 memset(&desc
->u
.hs
.DeviceRemovable
[0], 0, temp
);
1454 memset(&desc
->u
.hs
.DeviceRemovable
[temp
], 0xff, temp
);
1456 temp
= HUB_CHAR_INDV_PORT_OCPM
; /* per-port overcurrent reporting */
1457 temp
|= HUB_CHAR_NO_LPSM
; /* no power switching */
1458 desc
->wHubCharacteristics
= cpu_to_le16(temp
);
1461 static int fotg210_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
1462 u16 wIndex
, char *buf
, u16 wLength
)
1464 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
1465 int ports
= HCS_N_PORTS(fotg210
->hcs_params
);
1466 u32 __iomem
*status_reg
= &fotg210
->regs
->port_status
;
1467 u32 temp
, temp1
, status
;
1468 unsigned long flags
;
1473 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1474 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1475 * (track current state ourselves) ... blink for diagnostics,
1476 * power, "this is the one", etc. EHCI spec supports this.
1479 spin_lock_irqsave(&fotg210
->lock
, flags
);
1481 case ClearHubFeature
:
1483 case C_HUB_LOCAL_POWER
:
1484 case C_HUB_OVER_CURRENT
:
1485 /* no hub-wide feature/status flags */
1491 case ClearPortFeature
:
1492 if (!wIndex
|| wIndex
> ports
)
1495 temp
= fotg210_readl(fotg210
, status_reg
);
1496 temp
&= ~PORT_RWC_BITS
;
1499 * Even if OWNER is set, so the port is owned by the
1500 * companion controller, hub_wq needs to be able to clear
1501 * the port-change status bits (especially
1502 * USB_PORT_STAT_C_CONNECTION).
1506 case USB_PORT_FEAT_ENABLE
:
1507 fotg210_writel(fotg210
, temp
& ~PORT_PE
, status_reg
);
1509 case USB_PORT_FEAT_C_ENABLE
:
1510 fotg210_writel(fotg210
, temp
| PORT_PEC
, status_reg
);
1512 case USB_PORT_FEAT_SUSPEND
:
1513 if (temp
& PORT_RESET
)
1515 if (!(temp
& PORT_SUSPEND
))
1517 if ((temp
& PORT_PE
) == 0)
1520 /* resume signaling for 20 msec */
1521 fotg210_writel(fotg210
, temp
| PORT_RESUME
, status_reg
);
1522 fotg210
->reset_done
[wIndex
] = jiffies
1523 + msecs_to_jiffies(USB_RESUME_TIMEOUT
);
1525 case USB_PORT_FEAT_C_SUSPEND
:
1526 clear_bit(wIndex
, &fotg210
->port_c_suspend
);
1528 case USB_PORT_FEAT_C_CONNECTION
:
1529 fotg210_writel(fotg210
, temp
| PORT_CSC
, status_reg
);
1531 case USB_PORT_FEAT_C_OVER_CURRENT
:
1532 fotg210_writel(fotg210
, temp
| OTGISR_OVC
,
1533 &fotg210
->regs
->otgisr
);
1535 case USB_PORT_FEAT_C_RESET
:
1536 /* GetPortStatus clears reset */
1541 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1543 case GetHubDescriptor
:
1544 fotg210_hub_descriptor(fotg210
, (struct usb_hub_descriptor
*)
1548 /* no hub-wide feature/status flags */
1550 /*cpu_to_le32s ((u32 *) buf); */
1553 if (!wIndex
|| wIndex
> ports
)
1557 temp
= fotg210_readl(fotg210
, status_reg
);
1559 /* wPortChange bits */
1560 if (temp
& PORT_CSC
)
1561 status
|= USB_PORT_STAT_C_CONNECTION
<< 16;
1562 if (temp
& PORT_PEC
)
1563 status
|= USB_PORT_STAT_C_ENABLE
<< 16;
1565 temp1
= fotg210_readl(fotg210
, &fotg210
->regs
->otgisr
);
1566 if (temp1
& OTGISR_OVC
)
1567 status
|= USB_PORT_STAT_C_OVERCURRENT
<< 16;
1569 /* whoever resumes must GetPortStatus to complete it!! */
1570 if (temp
& PORT_RESUME
) {
1572 /* Remote Wakeup received? */
1573 if (!fotg210
->reset_done
[wIndex
]) {
1574 /* resume signaling for 20 msec */
1575 fotg210
->reset_done
[wIndex
] = jiffies
1576 + msecs_to_jiffies(20);
1577 /* check the port again */
1578 mod_timer(&fotg210_to_hcd(fotg210
)->rh_timer
,
1579 fotg210
->reset_done
[wIndex
]);
1582 /* resume completed? */
1583 else if (time_after_eq(jiffies
,
1584 fotg210
->reset_done
[wIndex
])) {
1585 clear_bit(wIndex
, &fotg210
->suspended_ports
);
1586 set_bit(wIndex
, &fotg210
->port_c_suspend
);
1587 fotg210
->reset_done
[wIndex
] = 0;
1589 /* stop resume signaling */
1590 temp
= fotg210_readl(fotg210
, status_reg
);
1591 fotg210_writel(fotg210
, temp
&
1592 ~(PORT_RWC_BITS
| PORT_RESUME
),
1594 clear_bit(wIndex
, &fotg210
->resuming_ports
);
1595 retval
= handshake(fotg210
, status_reg
,
1596 PORT_RESUME
, 0, 2000);/* 2ms */
1598 fotg210_err(fotg210
,
1599 "port %d resume error %d\n",
1600 wIndex
+ 1, retval
);
1603 temp
&= ~(PORT_SUSPEND
|PORT_RESUME
|(3<<10));
1607 /* whoever resets must GetPortStatus to complete it!! */
1608 if ((temp
& PORT_RESET
) && time_after_eq(jiffies
,
1609 fotg210
->reset_done
[wIndex
])) {
1610 status
|= USB_PORT_STAT_C_RESET
<< 16;
1611 fotg210
->reset_done
[wIndex
] = 0;
1612 clear_bit(wIndex
, &fotg210
->resuming_ports
);
1614 /* force reset to complete */
1615 fotg210_writel(fotg210
,
1616 temp
& ~(PORT_RWC_BITS
| PORT_RESET
),
1618 /* REVISIT: some hardware needs 550+ usec to clear
1619 * this bit; seems too long to spin routinely...
1621 retval
= handshake(fotg210
, status_reg
,
1622 PORT_RESET
, 0, 1000);
1624 fotg210_err(fotg210
, "port %d reset error %d\n",
1625 wIndex
+ 1, retval
);
1629 /* see what we found out */
1630 temp
= check_reset_complete(fotg210
, wIndex
, status_reg
,
1631 fotg210_readl(fotg210
, status_reg
));
1633 /* restart schedule */
1634 fotg210
->command
|= CMD_RUN
;
1635 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
1638 if (!(temp
& (PORT_RESUME
|PORT_RESET
))) {
1639 fotg210
->reset_done
[wIndex
] = 0;
1640 clear_bit(wIndex
, &fotg210
->resuming_ports
);
1643 /* transfer dedicated ports to the companion hc */
1644 if ((temp
& PORT_CONNECT
) &&
1645 test_bit(wIndex
, &fotg210
->companion_ports
)) {
1646 temp
&= ~PORT_RWC_BITS
;
1647 fotg210_writel(fotg210
, temp
, status_reg
);
1648 fotg210_dbg(fotg210
, "port %d --> companion\n",
1650 temp
= fotg210_readl(fotg210
, status_reg
);
1654 * Even if OWNER is set, there's no harm letting hub_wq
1655 * see the wPortStatus values (they should all be 0 except
1656 * for PORT_POWER anyway).
1659 if (temp
& PORT_CONNECT
) {
1660 status
|= USB_PORT_STAT_CONNECTION
;
1661 status
|= fotg210_port_speed(fotg210
, temp
);
1664 status
|= USB_PORT_STAT_ENABLE
;
1666 /* maybe the port was unsuspended without our knowledge */
1667 if (temp
& (PORT_SUSPEND
|PORT_RESUME
)) {
1668 status
|= USB_PORT_STAT_SUSPEND
;
1669 } else if (test_bit(wIndex
, &fotg210
->suspended_ports
)) {
1670 clear_bit(wIndex
, &fotg210
->suspended_ports
);
1671 clear_bit(wIndex
, &fotg210
->resuming_ports
);
1672 fotg210
->reset_done
[wIndex
] = 0;
1674 set_bit(wIndex
, &fotg210
->port_c_suspend
);
1677 temp1
= fotg210_readl(fotg210
, &fotg210
->regs
->otgisr
);
1678 if (temp1
& OTGISR_OVC
)
1679 status
|= USB_PORT_STAT_OVERCURRENT
;
1680 if (temp
& PORT_RESET
)
1681 status
|= USB_PORT_STAT_RESET
;
1682 if (test_bit(wIndex
, &fotg210
->port_c_suspend
))
1683 status
|= USB_PORT_STAT_C_SUSPEND
<< 16;
1685 if (status
& ~0xffff) /* only if wPortChange is interesting */
1686 dbg_port(fotg210
, "GetStatus", wIndex
+ 1, temp
);
1687 put_unaligned_le32(status
, buf
);
1691 case C_HUB_LOCAL_POWER
:
1692 case C_HUB_OVER_CURRENT
:
1693 /* no hub-wide feature/status flags */
1699 case SetPortFeature
:
1700 selector
= wIndex
>> 8;
1703 if (!wIndex
|| wIndex
> ports
)
1706 temp
= fotg210_readl(fotg210
, status_reg
);
1707 temp
&= ~PORT_RWC_BITS
;
1709 case USB_PORT_FEAT_SUSPEND
:
1710 if ((temp
& PORT_PE
) == 0
1711 || (temp
& PORT_RESET
) != 0)
1714 /* After above check the port must be connected.
1715 * Set appropriate bit thus could put phy into low power
1716 * mode if we have hostpc feature
1718 fotg210_writel(fotg210
, temp
| PORT_SUSPEND
,
1720 set_bit(wIndex
, &fotg210
->suspended_ports
);
1722 case USB_PORT_FEAT_RESET
:
1723 if (temp
& PORT_RESUME
)
1725 /* line status bits may report this as low speed,
1726 * which can be fine if this root hub has a
1727 * transaction translator built in.
1729 fotg210_dbg(fotg210
, "port %d reset\n", wIndex
+ 1);
1734 * caller must wait, then call GetPortStatus
1735 * usb 2.0 spec says 50 ms resets on root
1737 fotg210
->reset_done
[wIndex
] = jiffies
1738 + msecs_to_jiffies(50);
1739 fotg210_writel(fotg210
, temp
, status_reg
);
1742 /* For downstream facing ports (these): one hub port is put
1743 * into test mode according to USB2 11.24.2.13, then the hub
1744 * must be reset (which for root hub now means rmmod+modprobe,
1745 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1746 * about the EHCI-specific stuff.
1748 case USB_PORT_FEAT_TEST
:
1749 if (!selector
|| selector
> 5)
1751 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1752 fotg210_quiesce(fotg210
);
1753 spin_lock_irqsave(&fotg210
->lock
, flags
);
1755 /* Put all enabled ports into suspend */
1756 temp
= fotg210_readl(fotg210
, status_reg
) &
1759 fotg210_writel(fotg210
, temp
| PORT_SUSPEND
,
1762 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1763 fotg210_halt(fotg210
);
1764 spin_lock_irqsave(&fotg210
->lock
, flags
);
1766 temp
= fotg210_readl(fotg210
, status_reg
);
1767 temp
|= selector
<< 16;
1768 fotg210_writel(fotg210
, temp
, status_reg
);
1774 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1779 /* "stall" on error */
1782 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1786 static void __maybe_unused
fotg210_relinquish_port(struct usb_hcd
*hcd
,
1792 static int __maybe_unused
fotg210_port_handed_over(struct usb_hcd
*hcd
,
1798 /* There's basically three types of memory:
1799 * - data used only by the HCD ... kmalloc is fine
1800 * - async and periodic schedules, shared by HC and HCD ... these
1801 * need to use dma_pool or dma_alloc_coherent
1802 * - driver buffers, read/written by HC ... single shot DMA mapped
1804 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
1805 * No memory seen by this driver is pageable.
1808 /* Allocate the key transfer structures from the previously allocated pool */
1809 static inline void fotg210_qtd_init(struct fotg210_hcd
*fotg210
,
1810 struct fotg210_qtd
*qtd
, dma_addr_t dma
)
1812 memset(qtd
, 0, sizeof(*qtd
));
1814 qtd
->hw_token
= cpu_to_hc32(fotg210
, QTD_STS_HALT
);
1815 qtd
->hw_next
= FOTG210_LIST_END(fotg210
);
1816 qtd
->hw_alt_next
= FOTG210_LIST_END(fotg210
);
1817 INIT_LIST_HEAD(&qtd
->qtd_list
);
1820 static struct fotg210_qtd
*fotg210_qtd_alloc(struct fotg210_hcd
*fotg210
,
1823 struct fotg210_qtd
*qtd
;
1826 qtd
= dma_pool_alloc(fotg210
->qtd_pool
, flags
, &dma
);
1828 fotg210_qtd_init(fotg210
, qtd
, dma
);
1833 static inline void fotg210_qtd_free(struct fotg210_hcd
*fotg210
,
1834 struct fotg210_qtd
*qtd
)
1836 dma_pool_free(fotg210
->qtd_pool
, qtd
, qtd
->qtd_dma
);
1840 static void qh_destroy(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
1842 /* clean qtds first, and know this is not linked */
1843 if (!list_empty(&qh
->qtd_list
) || qh
->qh_next
.ptr
) {
1844 fotg210_dbg(fotg210
, "unused qh not empty!\n");
1848 fotg210_qtd_free(fotg210
, qh
->dummy
);
1849 dma_pool_free(fotg210
->qh_pool
, qh
->hw
, qh
->qh_dma
);
1853 static struct fotg210_qh
*fotg210_qh_alloc(struct fotg210_hcd
*fotg210
,
1856 struct fotg210_qh
*qh
;
1859 qh
= kzalloc(sizeof(*qh
), GFP_ATOMIC
);
1862 qh
->hw
= dma_pool_zalloc(fotg210
->qh_pool
, flags
, &dma
);
1866 INIT_LIST_HEAD(&qh
->qtd_list
);
1868 /* dummy td enables safe urb queuing */
1869 qh
->dummy
= fotg210_qtd_alloc(fotg210
, flags
);
1870 if (qh
->dummy
== NULL
) {
1871 fotg210_dbg(fotg210
, "no dummy td\n");
1877 dma_pool_free(fotg210
->qh_pool
, qh
->hw
, qh
->qh_dma
);
1883 /* The queue heads and transfer descriptors are managed from pools tied
1884 * to each of the "per device" structures.
1885 * This is the initialisation and cleanup code.
1888 static void fotg210_mem_cleanup(struct fotg210_hcd
*fotg210
)
1891 qh_destroy(fotg210
, fotg210
->async
);
1892 fotg210
->async
= NULL
;
1895 qh_destroy(fotg210
, fotg210
->dummy
);
1896 fotg210
->dummy
= NULL
;
1898 /* DMA consistent memory and pools */
1899 dma_pool_destroy(fotg210
->qtd_pool
);
1900 fotg210
->qtd_pool
= NULL
;
1902 dma_pool_destroy(fotg210
->qh_pool
);
1903 fotg210
->qh_pool
= NULL
;
1905 dma_pool_destroy(fotg210
->itd_pool
);
1906 fotg210
->itd_pool
= NULL
;
1908 if (fotg210
->periodic
)
1909 dma_free_coherent(fotg210_to_hcd(fotg210
)->self
.controller
,
1910 fotg210
->periodic_size
* sizeof(u32
),
1911 fotg210
->periodic
, fotg210
->periodic_dma
);
1912 fotg210
->periodic
= NULL
;
1914 /* shadow periodic table */
1915 kfree(fotg210
->pshadow
);
1916 fotg210
->pshadow
= NULL
;
1919 /* remember to add cleanup code (above) if you add anything here */
1920 static int fotg210_mem_init(struct fotg210_hcd
*fotg210
, gfp_t flags
)
1924 /* QTDs for control/bulk/intr transfers */
1925 fotg210
->qtd_pool
= dma_pool_create("fotg210_qtd",
1926 fotg210_to_hcd(fotg210
)->self
.controller
,
1927 sizeof(struct fotg210_qtd
),
1928 32 /* byte alignment (for hw parts) */,
1929 4096 /* can't cross 4K */);
1930 if (!fotg210
->qtd_pool
)
1933 /* QHs for control/bulk/intr transfers */
1934 fotg210
->qh_pool
= dma_pool_create("fotg210_qh",
1935 fotg210_to_hcd(fotg210
)->self
.controller
,
1936 sizeof(struct fotg210_qh_hw
),
1937 32 /* byte alignment (for hw parts) */,
1938 4096 /* can't cross 4K */);
1939 if (!fotg210
->qh_pool
)
1942 fotg210
->async
= fotg210_qh_alloc(fotg210
, flags
);
1943 if (!fotg210
->async
)
1946 /* ITD for high speed ISO transfers */
1947 fotg210
->itd_pool
= dma_pool_create("fotg210_itd",
1948 fotg210_to_hcd(fotg210
)->self
.controller
,
1949 sizeof(struct fotg210_itd
),
1950 64 /* byte alignment (for hw parts) */,
1951 4096 /* can't cross 4K */);
1952 if (!fotg210
->itd_pool
)
1955 /* Hardware periodic table */
1956 fotg210
->periodic
= (__le32
*)
1957 dma_alloc_coherent(fotg210_to_hcd(fotg210
)->self
.controller
,
1958 fotg210
->periodic_size
* sizeof(__le32
),
1959 &fotg210
->periodic_dma
, 0);
1960 if (fotg210
->periodic
== NULL
)
1963 for (i
= 0; i
< fotg210
->periodic_size
; i
++)
1964 fotg210
->periodic
[i
] = FOTG210_LIST_END(fotg210
);
1966 /* software shadow of hardware table */
1967 fotg210
->pshadow
= kcalloc(fotg210
->periodic_size
, sizeof(void *),
1969 if (fotg210
->pshadow
!= NULL
)
1973 fotg210_dbg(fotg210
, "couldn't init memory\n");
1974 fotg210_mem_cleanup(fotg210
);
1977 /* EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
1979 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
1980 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
1981 * buffers needed for the larger number). We use one QH per endpoint, queue
1982 * multiple urbs (all three types) per endpoint. URBs may need several qtds.
1984 * ISO traffic uses "ISO TD" (itd) records, and (along with
1985 * interrupts) needs careful scheduling. Performance improvements can be
1986 * an ongoing challenge. That's in "ehci-sched.c".
1988 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
1989 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
1990 * (b) special fields in qh entries or (c) split iso entries. TTs will
1991 * buffer low/full speed data so the host collects it at high speed.
1994 /* fill a qtd, returning how much of the buffer we were able to queue up */
1995 static int qtd_fill(struct fotg210_hcd
*fotg210
, struct fotg210_qtd
*qtd
,
1996 dma_addr_t buf
, size_t len
, int token
, int maxpacket
)
2001 /* one buffer entry per 4K ... first might be short or unaligned */
2002 qtd
->hw_buf
[0] = cpu_to_hc32(fotg210
, (u32
)addr
);
2003 qtd
->hw_buf_hi
[0] = cpu_to_hc32(fotg210
, (u32
)(addr
>> 32));
2004 count
= 0x1000 - (buf
& 0x0fff); /* rest of that page */
2005 if (likely(len
< count
)) /* ... iff needed */
2011 /* per-qtd limit: from 16K to 20K (best alignment) */
2012 for (i
= 1; count
< len
&& i
< 5; i
++) {
2014 qtd
->hw_buf
[i
] = cpu_to_hc32(fotg210
, (u32
)addr
);
2015 qtd
->hw_buf_hi
[i
] = cpu_to_hc32(fotg210
,
2018 if ((count
+ 0x1000) < len
)
2024 /* short packets may only terminate transfers */
2026 count
-= (count
% maxpacket
);
2028 qtd
->hw_token
= cpu_to_hc32(fotg210
, (count
<< 16) | token
);
2029 qtd
->length
= count
;
2034 static inline void qh_update(struct fotg210_hcd
*fotg210
,
2035 struct fotg210_qh
*qh
, struct fotg210_qtd
*qtd
)
2037 struct fotg210_qh_hw
*hw
= qh
->hw
;
2039 /* writes to an active overlay are unsafe */
2040 BUG_ON(qh
->qh_state
!= QH_STATE_IDLE
);
2042 hw
->hw_qtd_next
= QTD_NEXT(fotg210
, qtd
->qtd_dma
);
2043 hw
->hw_alt_next
= FOTG210_LIST_END(fotg210
);
2045 /* Except for control endpoints, we make hardware maintain data
2046 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
2047 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
2050 if (!(hw
->hw_info1
& cpu_to_hc32(fotg210
, QH_TOGGLE_CTL
))) {
2051 unsigned is_out
, epnum
;
2053 is_out
= qh
->is_out
;
2054 epnum
= (hc32_to_cpup(fotg210
, &hw
->hw_info1
) >> 8) & 0x0f;
2055 if (unlikely(!usb_gettoggle(qh
->dev
, epnum
, is_out
))) {
2056 hw
->hw_token
&= ~cpu_to_hc32(fotg210
, QTD_TOGGLE
);
2057 usb_settoggle(qh
->dev
, epnum
, is_out
, 1);
2061 hw
->hw_token
&= cpu_to_hc32(fotg210
, QTD_TOGGLE
| QTD_STS_PING
);
2064 /* if it weren't for a common silicon quirk (writing the dummy into the qh
2065 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
2066 * recovery (including urb dequeue) would need software changes to a QH...
2068 static void qh_refresh(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
2070 struct fotg210_qtd
*qtd
;
2072 if (list_empty(&qh
->qtd_list
))
2075 qtd
= list_entry(qh
->qtd_list
.next
,
2076 struct fotg210_qtd
, qtd_list
);
2078 * first qtd may already be partially processed.
2079 * If we come here during unlink, the QH overlay region
2080 * might have reference to the just unlinked qtd. The
2081 * qtd is updated in qh_completions(). Update the QH
2084 if (cpu_to_hc32(fotg210
, qtd
->qtd_dma
) == qh
->hw
->hw_current
) {
2085 qh
->hw
->hw_qtd_next
= qtd
->hw_next
;
2091 qh_update(fotg210
, qh
, qtd
);
2094 static void qh_link_async(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
);
2096 static void fotg210_clear_tt_buffer_complete(struct usb_hcd
*hcd
,
2097 struct usb_host_endpoint
*ep
)
2099 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
2100 struct fotg210_qh
*qh
= ep
->hcpriv
;
2101 unsigned long flags
;
2103 spin_lock_irqsave(&fotg210
->lock
, flags
);
2104 qh
->clearing_tt
= 0;
2105 if (qh
->qh_state
== QH_STATE_IDLE
&& !list_empty(&qh
->qtd_list
)
2106 && fotg210
->rh_state
== FOTG210_RH_RUNNING
)
2107 qh_link_async(fotg210
, qh
);
2108 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
2111 static void fotg210_clear_tt_buffer(struct fotg210_hcd
*fotg210
,
2112 struct fotg210_qh
*qh
, struct urb
*urb
, u32 token
)
2115 /* If an async split transaction gets an error or is unlinked,
2116 * the TT buffer may be left in an indeterminate state. We
2117 * have to clear the TT buffer.
2119 * Note: this routine is never called for Isochronous transfers.
2121 if (urb
->dev
->tt
&& !usb_pipeint(urb
->pipe
) && !qh
->clearing_tt
) {
2122 struct usb_device
*tt
= urb
->dev
->tt
->hub
;
2125 "clear tt buffer port %d, a%d ep%d t%08x\n",
2126 urb
->dev
->ttport
, urb
->dev
->devnum
,
2127 usb_pipeendpoint(urb
->pipe
), token
);
2129 if (urb
->dev
->tt
->hub
!=
2130 fotg210_to_hcd(fotg210
)->self
.root_hub
) {
2131 if (usb_hub_clear_tt_buffer(urb
) == 0)
2132 qh
->clearing_tt
= 1;
2137 static int qtd_copy_status(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
2138 size_t length
, u32 token
)
2140 int status
= -EINPROGRESS
;
2142 /* count IN/OUT bytes, not SETUP (even short packets) */
2143 if (likely(QTD_PID(token
) != 2))
2144 urb
->actual_length
+= length
- QTD_LENGTH(token
);
2146 /* don't modify error codes */
2147 if (unlikely(urb
->unlinked
))
2150 /* force cleanup after short read; not always an error */
2151 if (unlikely(IS_SHORT_READ(token
)))
2152 status
= -EREMOTEIO
;
2154 /* serious "can't proceed" faults reported by the hardware */
2155 if (token
& QTD_STS_HALT
) {
2156 if (token
& QTD_STS_BABBLE
) {
2157 /* FIXME "must" disable babbling device's port too */
2158 status
= -EOVERFLOW
;
2159 /* CERR nonzero + halt --> stall */
2160 } else if (QTD_CERR(token
)) {
2163 /* In theory, more than one of the following bits can be set
2164 * since they are sticky and the transaction is retried.
2165 * Which to test first is rather arbitrary.
2167 } else if (token
& QTD_STS_MMF
) {
2168 /* fs/ls interrupt xfer missed the complete-split */
2170 } else if (token
& QTD_STS_DBE
) {
2171 status
= (QTD_PID(token
) == 1) /* IN ? */
2172 ? -ENOSR
/* hc couldn't read data */
2173 : -ECOMM
; /* hc couldn't write data */
2174 } else if (token
& QTD_STS_XACT
) {
2175 /* timeout, bad CRC, wrong PID, etc */
2176 fotg210_dbg(fotg210
, "devpath %s ep%d%s 3strikes\n",
2178 usb_pipeendpoint(urb
->pipe
),
2179 usb_pipein(urb
->pipe
) ? "in" : "out");
2181 } else { /* unknown */
2185 fotg210_dbg(fotg210
,
2186 "dev%d ep%d%s qtd token %08x --> status %d\n",
2187 usb_pipedevice(urb
->pipe
),
2188 usb_pipeendpoint(urb
->pipe
),
2189 usb_pipein(urb
->pipe
) ? "in" : "out",
2196 static void fotg210_urb_done(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
2198 __releases(fotg210
->lock
)
2199 __acquires(fotg210
->lock
)
2201 if (likely(urb
->hcpriv
!= NULL
)) {
2202 struct fotg210_qh
*qh
= (struct fotg210_qh
*) urb
->hcpriv
;
2204 /* S-mask in a QH means it's an interrupt urb */
2205 if ((qh
->hw
->hw_info2
& cpu_to_hc32(fotg210
, QH_SMASK
)) != 0) {
2207 /* ... update hc-wide periodic stats (for usbfs) */
2208 fotg210_to_hcd(fotg210
)->self
.bandwidth_int_reqs
--;
2212 if (unlikely(urb
->unlinked
)) {
2213 INCR(fotg210
->stats
.unlink
);
2215 /* report non-error and short read status as zero */
2216 if (status
== -EINPROGRESS
|| status
== -EREMOTEIO
)
2218 INCR(fotg210
->stats
.complete
);
2221 #ifdef FOTG210_URB_TRACE
2222 fotg210_dbg(fotg210
,
2223 "%s %s urb %p ep%d%s status %d len %d/%d\n",
2224 __func__
, urb
->dev
->devpath
, urb
,
2225 usb_pipeendpoint(urb
->pipe
),
2226 usb_pipein(urb
->pipe
) ? "in" : "out",
2228 urb
->actual_length
, urb
->transfer_buffer_length
);
2231 /* complete() can reenter this HCD */
2232 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210
), urb
);
2233 spin_unlock(&fotg210
->lock
);
2234 usb_hcd_giveback_urb(fotg210_to_hcd(fotg210
), urb
, status
);
2235 spin_lock(&fotg210
->lock
);
2238 static int qh_schedule(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
);
2240 /* Process and free completed qtds for a qh, returning URBs to drivers.
2241 * Chases up to qh->hw_current. Returns number of completions called,
2242 * indicating how much "real" work we did.
2244 static unsigned qh_completions(struct fotg210_hcd
*fotg210
,
2245 struct fotg210_qh
*qh
)
2247 struct fotg210_qtd
*last
, *end
= qh
->dummy
;
2248 struct fotg210_qtd
*qtd
, *tmp
;
2253 struct fotg210_qh_hw
*hw
= qh
->hw
;
2255 if (unlikely(list_empty(&qh
->qtd_list
)))
2258 /* completions (or tasks on other cpus) must never clobber HALT
2259 * till we've gone through and cleaned everything up, even when
2260 * they add urbs to this qh's queue or mark them for unlinking.
2262 * NOTE: unlinking expects to be done in queue order.
2264 * It's a bug for qh->qh_state to be anything other than
2265 * QH_STATE_IDLE, unless our caller is scan_async() or
2268 state
= qh
->qh_state
;
2269 qh
->qh_state
= QH_STATE_COMPLETING
;
2270 stopped
= (state
== QH_STATE_IDLE
);
2274 last_status
= -EINPROGRESS
;
2275 qh
->needs_rescan
= 0;
2277 /* remove de-activated QTDs from front of queue.
2278 * after faults (including short reads), cleanup this urb
2279 * then let the queue advance.
2280 * if queue is stopped, handles unlinks.
2282 list_for_each_entry_safe(qtd
, tmp
, &qh
->qtd_list
, qtd_list
) {
2288 /* clean up any state from previous QTD ...*/
2290 if (likely(last
->urb
!= urb
)) {
2291 fotg210_urb_done(fotg210
, last
->urb
,
2294 last_status
= -EINPROGRESS
;
2296 fotg210_qtd_free(fotg210
, last
);
2300 /* ignore urbs submitted during completions we reported */
2304 /* hardware copies qtd out of qh overlay */
2306 token
= hc32_to_cpu(fotg210
, qtd
->hw_token
);
2308 /* always clean up qtds the hc de-activated */
2310 if ((token
& QTD_STS_ACTIVE
) == 0) {
2312 /* Report Data Buffer Error: non-fatal but useful */
2313 if (token
& QTD_STS_DBE
)
2314 fotg210_dbg(fotg210
,
2315 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
2316 urb
, usb_endpoint_num(&urb
->ep
->desc
),
2317 usb_endpoint_dir_in(&urb
->ep
->desc
)
2319 urb
->transfer_buffer_length
, qtd
, qh
);
2321 /* on STALL, error, and short reads this urb must
2322 * complete and all its qtds must be recycled.
2324 if ((token
& QTD_STS_HALT
) != 0) {
2326 /* retry transaction errors until we
2327 * reach the software xacterr limit
2329 if ((token
& QTD_STS_XACT
) &&
2330 QTD_CERR(token
) == 0 &&
2331 ++qh
->xacterrs
< QH_XACTERR_MAX
&&
2333 fotg210_dbg(fotg210
,
2334 "detected XactErr len %zu/%zu retry %d\n",
2335 qtd
->length
- QTD_LENGTH(token
),
2339 /* reset the token in the qtd and the
2340 * qh overlay (which still contains
2341 * the qtd) so that we pick up from
2344 token
&= ~QTD_STS_HALT
;
2345 token
|= QTD_STS_ACTIVE
|
2346 (FOTG210_TUNE_CERR
<< 10);
2347 qtd
->hw_token
= cpu_to_hc32(fotg210
,
2350 hw
->hw_token
= cpu_to_hc32(fotg210
,
2356 /* magic dummy for some short reads; qh won't advance.
2357 * that silicon quirk can kick in with this dummy too.
2359 * other short reads won't stop the queue, including
2360 * control transfers (status stage handles that) or
2361 * most other single-qtd reads ... the queue stops if
2362 * URB_SHORT_NOT_OK was set so the driver submitting
2363 * the urbs could clean it up.
2365 } else if (IS_SHORT_READ(token
) &&
2366 !(qtd
->hw_alt_next
&
2367 FOTG210_LIST_END(fotg210
))) {
2371 /* stop scanning when we reach qtds the hc is using */
2372 } else if (likely(!stopped
2373 && fotg210
->rh_state
>= FOTG210_RH_RUNNING
)) {
2376 /* scan the whole queue for unlinks whenever it stops */
2380 /* cancel everything if we halt, suspend, etc */
2381 if (fotg210
->rh_state
< FOTG210_RH_RUNNING
)
2382 last_status
= -ESHUTDOWN
;
2384 /* this qtd is active; skip it unless a previous qtd
2385 * for its urb faulted, or its urb was canceled.
2387 else if (last_status
== -EINPROGRESS
&& !urb
->unlinked
)
2390 /* qh unlinked; token in overlay may be most current */
2391 if (state
== QH_STATE_IDLE
&&
2392 cpu_to_hc32(fotg210
, qtd
->qtd_dma
)
2393 == hw
->hw_current
) {
2394 token
= hc32_to_cpu(fotg210
, hw
->hw_token
);
2396 /* An unlink may leave an incomplete
2397 * async transaction in the TT buffer.
2398 * We have to clear it.
2400 fotg210_clear_tt_buffer(fotg210
, qh
, urb
,
2405 /* unless we already know the urb's status, collect qtd status
2406 * and update count of bytes transferred. in common short read
2407 * cases with only one data qtd (including control transfers),
2408 * queue processing won't halt. but with two or more qtds (for
2409 * example, with a 32 KB transfer), when the first qtd gets a
2410 * short read the second must be removed by hand.
2412 if (last_status
== -EINPROGRESS
) {
2413 last_status
= qtd_copy_status(fotg210
, urb
,
2414 qtd
->length
, token
);
2415 if (last_status
== -EREMOTEIO
&&
2417 FOTG210_LIST_END(fotg210
)))
2418 last_status
= -EINPROGRESS
;
2420 /* As part of low/full-speed endpoint-halt processing
2421 * we must clear the TT buffer (11.17.5).
2423 if (unlikely(last_status
!= -EINPROGRESS
&&
2424 last_status
!= -EREMOTEIO
)) {
2425 /* The TT's in some hubs malfunction when they
2426 * receive this request following a STALL (they
2427 * stop sending isochronous packets). Since a
2428 * STALL can't leave the TT buffer in a busy
2429 * state (if you believe Figures 11-48 - 11-51
2430 * in the USB 2.0 spec), we won't clear the TT
2431 * buffer in this case. Strictly speaking this
2432 * is a violation of the spec.
2434 if (last_status
!= -EPIPE
)
2435 fotg210_clear_tt_buffer(fotg210
, qh
,
2440 /* if we're removing something not at the queue head,
2441 * patch the hardware queue pointer.
2443 if (stopped
&& qtd
->qtd_list
.prev
!= &qh
->qtd_list
) {
2444 last
= list_entry(qtd
->qtd_list
.prev
,
2445 struct fotg210_qtd
, qtd_list
);
2446 last
->hw_next
= qtd
->hw_next
;
2449 /* remove qtd; it's recycled after possible urb completion */
2450 list_del(&qtd
->qtd_list
);
2453 /* reinit the xacterr counter for the next qtd */
2457 /* last urb's completion might still need calling */
2458 if (likely(last
!= NULL
)) {
2459 fotg210_urb_done(fotg210
, last
->urb
, last_status
);
2461 fotg210_qtd_free(fotg210
, last
);
2464 /* Do we need to rescan for URBs dequeued during a giveback? */
2465 if (unlikely(qh
->needs_rescan
)) {
2466 /* If the QH is already unlinked, do the rescan now. */
2467 if (state
== QH_STATE_IDLE
)
2470 /* Otherwise we have to wait until the QH is fully unlinked.
2471 * Our caller will start an unlink if qh->needs_rescan is
2472 * set. But if an unlink has already started, nothing needs
2475 if (state
!= QH_STATE_LINKED
)
2476 qh
->needs_rescan
= 0;
2479 /* restore original state; caller must unlink or relink */
2480 qh
->qh_state
= state
;
2482 /* be sure the hardware's done with the qh before refreshing
2483 * it after fault cleanup, or recovering from silicon wrongly
2484 * overlaying the dummy qtd (which reduces DMA chatter).
2486 if (stopped
!= 0 || hw
->hw_qtd_next
== FOTG210_LIST_END(fotg210
)) {
2489 qh_refresh(fotg210
, qh
);
2491 case QH_STATE_LINKED
:
2492 /* We won't refresh a QH that's linked (after the HC
2493 * stopped the queue). That avoids a race:
2494 * - HC reads first part of QH;
2495 * - CPU updates that first part and the token;
2496 * - HC reads rest of that QH, including token
2497 * Result: HC gets an inconsistent image, and then
2498 * DMAs to/from the wrong memory (corrupting it).
2500 * That should be rare for interrupt transfers,
2501 * except maybe high bandwidth ...
2504 /* Tell the caller to start an unlink */
2505 qh
->needs_rescan
= 1;
2507 /* otherwise, unlink already started */
2514 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
2515 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
2516 /* ... and packet size, for any kind of endpoint descriptor */
2517 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
2519 /* reverse of qh_urb_transaction: free a list of TDs.
2520 * used for cleanup after errors, before HC sees an URB's TDs.
2522 static void qtd_list_free(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
2523 struct list_head
*head
)
2525 struct fotg210_qtd
*qtd
, *temp
;
2527 list_for_each_entry_safe(qtd
, temp
, head
, qtd_list
) {
2528 list_del(&qtd
->qtd_list
);
2529 fotg210_qtd_free(fotg210
, qtd
);
2533 /* create a list of filled qtds for this URB; won't link into qh.
2535 static struct list_head
*qh_urb_transaction(struct fotg210_hcd
*fotg210
,
2536 struct urb
*urb
, struct list_head
*head
, gfp_t flags
)
2538 struct fotg210_qtd
*qtd
, *qtd_prev
;
2540 int len
, this_sg_len
, maxpacket
;
2544 struct scatterlist
*sg
;
2547 * URBs map to sequences of QTDs: one logical transaction
2549 qtd
= fotg210_qtd_alloc(fotg210
, flags
);
2552 list_add_tail(&qtd
->qtd_list
, head
);
2555 token
= QTD_STS_ACTIVE
;
2556 token
|= (FOTG210_TUNE_CERR
<< 10);
2557 /* for split transactions, SplitXState initialized to zero */
2559 len
= urb
->transfer_buffer_length
;
2560 is_input
= usb_pipein(urb
->pipe
);
2561 if (usb_pipecontrol(urb
->pipe
)) {
2563 qtd_fill(fotg210
, qtd
, urb
->setup_dma
,
2564 sizeof(struct usb_ctrlrequest
),
2565 token
| (2 /* "setup" */ << 8), 8);
2567 /* ... and always at least one more pid */
2568 token
^= QTD_TOGGLE
;
2570 qtd
= fotg210_qtd_alloc(fotg210
, flags
);
2574 qtd_prev
->hw_next
= QTD_NEXT(fotg210
, qtd
->qtd_dma
);
2575 list_add_tail(&qtd
->qtd_list
, head
);
2577 /* for zero length DATA stages, STATUS is always IN */
2579 token
|= (1 /* "in" */ << 8);
2583 * data transfer stage: buffer setup
2585 i
= urb
->num_mapped_sgs
;
2586 if (len
> 0 && i
> 0) {
2588 buf
= sg_dma_address(sg
);
2590 /* urb->transfer_buffer_length may be smaller than the
2591 * size of the scatterlist (or vice versa)
2593 this_sg_len
= min_t(int, sg_dma_len(sg
), len
);
2596 buf
= urb
->transfer_dma
;
2601 token
|= (1 /* "in" */ << 8);
2602 /* else it's already initted to "out" pid (0 << 8) */
2604 maxpacket
= max_packet(usb_maxpacket(urb
->dev
, urb
->pipe
, !is_input
));
2607 * buffer gets wrapped in one or more qtds;
2608 * last one may be "short" (including zero len)
2609 * and may serve as a control status ack
2614 this_qtd_len
= qtd_fill(fotg210
, qtd
, buf
, this_sg_len
, token
,
2616 this_sg_len
-= this_qtd_len
;
2617 len
-= this_qtd_len
;
2618 buf
+= this_qtd_len
;
2621 * short reads advance to a "magic" dummy instead of the next
2622 * qtd ... that forces the queue to stop, for manual cleanup.
2623 * (this will usually be overridden later.)
2626 qtd
->hw_alt_next
= fotg210
->async
->hw
->hw_alt_next
;
2628 /* qh makes control packets use qtd toggle; maybe switch it */
2629 if ((maxpacket
& (this_qtd_len
+ (maxpacket
- 1))) == 0)
2630 token
^= QTD_TOGGLE
;
2632 if (likely(this_sg_len
<= 0)) {
2633 if (--i
<= 0 || len
<= 0)
2636 buf
= sg_dma_address(sg
);
2637 this_sg_len
= min_t(int, sg_dma_len(sg
), len
);
2641 qtd
= fotg210_qtd_alloc(fotg210
, flags
);
2645 qtd_prev
->hw_next
= QTD_NEXT(fotg210
, qtd
->qtd_dma
);
2646 list_add_tail(&qtd
->qtd_list
, head
);
2650 * unless the caller requires manual cleanup after short reads,
2651 * have the alt_next mechanism keep the queue running after the
2652 * last data qtd (the only one, for control and most other cases).
2654 if (likely((urb
->transfer_flags
& URB_SHORT_NOT_OK
) == 0 ||
2655 usb_pipecontrol(urb
->pipe
)))
2656 qtd
->hw_alt_next
= FOTG210_LIST_END(fotg210
);
2659 * control requests may need a terminating data "status" ack;
2660 * other OUT ones may need a terminating short packet
2663 if (likely(urb
->transfer_buffer_length
!= 0)) {
2666 if (usb_pipecontrol(urb
->pipe
)) {
2668 token
^= 0x0100; /* "in" <--> "out" */
2669 token
|= QTD_TOGGLE
; /* force DATA1 */
2670 } else if (usb_pipeout(urb
->pipe
)
2671 && (urb
->transfer_flags
& URB_ZERO_PACKET
)
2672 && !(urb
->transfer_buffer_length
% maxpacket
)) {
2677 qtd
= fotg210_qtd_alloc(fotg210
, flags
);
2681 qtd_prev
->hw_next
= QTD_NEXT(fotg210
, qtd
->qtd_dma
);
2682 list_add_tail(&qtd
->qtd_list
, head
);
2684 /* never any data in such packets */
2685 qtd_fill(fotg210
, qtd
, 0, 0, token
, 0);
2689 /* by default, enable interrupt on urb completion */
2690 if (likely(!(urb
->transfer_flags
& URB_NO_INTERRUPT
)))
2691 qtd
->hw_token
|= cpu_to_hc32(fotg210
, QTD_IOC
);
2695 qtd_list_free(fotg210
, urb
, head
);
2699 /* Would be best to create all qh's from config descriptors,
2700 * when each interface/altsetting is established. Unlink
2701 * any previous qh and cancel its urbs first; endpoints are
2702 * implicitly reset then (data toggle too).
2703 * That'd mean updating how usbcore talks to HCDs. (2.7?)
2707 /* Each QH holds a qtd list; a QH is used for everything except iso.
2709 * For interrupt urbs, the scheduler must set the microframe scheduling
2710 * mask(s) each time the QH gets scheduled. For highspeed, that's
2711 * just one microframe in the s-mask. For split interrupt transactions
2712 * there are additional complications: c-mask, maybe FSTNs.
2714 static struct fotg210_qh
*qh_make(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
2717 struct fotg210_qh
*qh
= fotg210_qh_alloc(fotg210
, flags
);
2718 u32 info1
= 0, info2
= 0;
2721 struct usb_tt
*tt
= urb
->dev
->tt
;
2722 struct fotg210_qh_hw
*hw
;
2728 * init endpoint/device data for this QH
2730 info1
|= usb_pipeendpoint(urb
->pipe
) << 8;
2731 info1
|= usb_pipedevice(urb
->pipe
) << 0;
2733 is_input
= usb_pipein(urb
->pipe
);
2734 type
= usb_pipetype(urb
->pipe
);
2735 maxp
= usb_maxpacket(urb
->dev
, urb
->pipe
, !is_input
);
2737 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
2738 * acts like up to 3KB, but is built from smaller packets.
2740 if (max_packet(maxp
) > 1024) {
2741 fotg210_dbg(fotg210
, "bogus qh maxpacket %d\n",
2746 /* Compute interrupt scheduling parameters just once, and save.
2747 * - allowing for high bandwidth, how many nsec/uframe are used?
2748 * - split transactions need a second CSPLIT uframe; same question
2749 * - splits also need a schedule gap (for full/low speed I/O)
2750 * - qh has a polling interval
2752 * For control/bulk requests, the HC or TT handles these.
2754 if (type
== PIPE_INTERRUPT
) {
2755 qh
->usecs
= NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH
,
2757 hb_mult(maxp
) * max_packet(maxp
)));
2758 qh
->start
= NO_FRAME
;
2760 if (urb
->dev
->speed
== USB_SPEED_HIGH
) {
2764 qh
->period
= urb
->interval
>> 3;
2765 if (qh
->period
== 0 && urb
->interval
!= 1) {
2766 /* NOTE interval 2 or 4 uframes could work.
2767 * But interval 1 scheduling is simpler, and
2768 * includes high bandwidth.
2771 } else if (qh
->period
> fotg210
->periodic_size
) {
2772 qh
->period
= fotg210
->periodic_size
;
2773 urb
->interval
= qh
->period
<< 3;
2778 /* gap is f(FS/LS transfer times) */
2779 qh
->gap_uf
= 1 + usb_calc_bus_time(urb
->dev
->speed
,
2780 is_input
, 0, maxp
) / (125 * 1000);
2782 /* FIXME this just approximates SPLIT/CSPLIT times */
2783 if (is_input
) { /* SPLIT, gap, CSPLIT+DATA */
2784 qh
->c_usecs
= qh
->usecs
+ HS_USECS(0);
2785 qh
->usecs
= HS_USECS(1);
2786 } else { /* SPLIT+DATA, gap, CSPLIT */
2787 qh
->usecs
+= HS_USECS(1);
2788 qh
->c_usecs
= HS_USECS(0);
2791 think_time
= tt
? tt
->think_time
: 0;
2792 qh
->tt_usecs
= NS_TO_US(think_time
+
2793 usb_calc_bus_time(urb
->dev
->speed
,
2794 is_input
, 0, max_packet(maxp
)));
2795 qh
->period
= urb
->interval
;
2796 if (qh
->period
> fotg210
->periodic_size
) {
2797 qh
->period
= fotg210
->periodic_size
;
2798 urb
->interval
= qh
->period
;
2803 /* support for tt scheduling, and access to toggles */
2807 switch (urb
->dev
->speed
) {
2809 info1
|= QH_LOW_SPEED
;
2812 case USB_SPEED_FULL
:
2813 /* EPS 0 means "full" */
2814 if (type
!= PIPE_INTERRUPT
)
2815 info1
|= (FOTG210_TUNE_RL_TT
<< 28);
2816 if (type
== PIPE_CONTROL
) {
2817 info1
|= QH_CONTROL_EP
; /* for TT */
2818 info1
|= QH_TOGGLE_CTL
; /* toggle from qtd */
2820 info1
|= maxp
<< 16;
2822 info2
|= (FOTG210_TUNE_MULT_TT
<< 30);
2824 /* Some Freescale processors have an erratum in which the
2825 * port number in the queue head was 0..N-1 instead of 1..N.
2827 if (fotg210_has_fsl_portno_bug(fotg210
))
2828 info2
|= (urb
->dev
->ttport
-1) << 23;
2830 info2
|= urb
->dev
->ttport
<< 23;
2832 /* set the address of the TT; for TDI's integrated
2833 * root hub tt, leave it zeroed.
2835 if (tt
&& tt
->hub
!= fotg210_to_hcd(fotg210
)->self
.root_hub
)
2836 info2
|= tt
->hub
->devnum
<< 16;
2838 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
2842 case USB_SPEED_HIGH
: /* no TT involved */
2843 info1
|= QH_HIGH_SPEED
;
2844 if (type
== PIPE_CONTROL
) {
2845 info1
|= (FOTG210_TUNE_RL_HS
<< 28);
2846 info1
|= 64 << 16; /* usb2 fixed maxpacket */
2847 info1
|= QH_TOGGLE_CTL
; /* toggle from qtd */
2848 info2
|= (FOTG210_TUNE_MULT_HS
<< 30);
2849 } else if (type
== PIPE_BULK
) {
2850 info1
|= (FOTG210_TUNE_RL_HS
<< 28);
2851 /* The USB spec says that high speed bulk endpoints
2852 * always use 512 byte maxpacket. But some device
2853 * vendors decided to ignore that, and MSFT is happy
2854 * to help them do so. So now people expect to use
2855 * such nonconformant devices with Linux too; sigh.
2857 info1
|= max_packet(maxp
) << 16;
2858 info2
|= (FOTG210_TUNE_MULT_HS
<< 30);
2859 } else { /* PIPE_INTERRUPT */
2860 info1
|= max_packet(maxp
) << 16;
2861 info2
|= hb_mult(maxp
) << 30;
2865 fotg210_dbg(fotg210
, "bogus dev %p speed %d\n", urb
->dev
,
2868 qh_destroy(fotg210
, qh
);
2872 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
2874 /* init as live, toggle clear, advance to dummy */
2875 qh
->qh_state
= QH_STATE_IDLE
;
2877 hw
->hw_info1
= cpu_to_hc32(fotg210
, info1
);
2878 hw
->hw_info2
= cpu_to_hc32(fotg210
, info2
);
2879 qh
->is_out
= !is_input
;
2880 usb_settoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), !is_input
, 1);
2881 qh_refresh(fotg210
, qh
);
2885 static void enable_async(struct fotg210_hcd
*fotg210
)
2887 if (fotg210
->async_count
++)
2890 /* Stop waiting to turn off the async schedule */
2891 fotg210
->enabled_hrtimer_events
&= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC
);
2893 /* Don't start the schedule until ASS is 0 */
2894 fotg210_poll_ASS(fotg210
);
2895 turn_on_io_watchdog(fotg210
);
2898 static void disable_async(struct fotg210_hcd
*fotg210
)
2900 if (--fotg210
->async_count
)
2903 /* The async schedule and async_unlink list are supposed to be empty */
2904 WARN_ON(fotg210
->async
->qh_next
.qh
|| fotg210
->async_unlink
);
2906 /* Don't turn off the schedule until ASS is 1 */
2907 fotg210_poll_ASS(fotg210
);
2910 /* move qh (and its qtds) onto async queue; maybe enable queue. */
2912 static void qh_link_async(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
2914 __hc32 dma
= QH_NEXT(fotg210
, qh
->qh_dma
);
2915 struct fotg210_qh
*head
;
2917 /* Don't link a QH if there's a Clear-TT-Buffer pending */
2918 if (unlikely(qh
->clearing_tt
))
2921 WARN_ON(qh
->qh_state
!= QH_STATE_IDLE
);
2923 /* clear halt and/or toggle; and maybe recover from silicon quirk */
2924 qh_refresh(fotg210
, qh
);
2926 /* splice right after start */
2927 head
= fotg210
->async
;
2928 qh
->qh_next
= head
->qh_next
;
2929 qh
->hw
->hw_next
= head
->hw
->hw_next
;
2932 head
->qh_next
.qh
= qh
;
2933 head
->hw
->hw_next
= dma
;
2936 qh
->qh_state
= QH_STATE_LINKED
;
2937 /* qtd completions reported later by interrupt */
2939 enable_async(fotg210
);
2942 /* For control/bulk/interrupt, return QH with these TDs appended.
2943 * Allocates and initializes the QH if necessary.
2944 * Returns null if it can't allocate a QH it needs to.
2945 * If the QH has TDs (urbs) already, that's great.
2947 static struct fotg210_qh
*qh_append_tds(struct fotg210_hcd
*fotg210
,
2948 struct urb
*urb
, struct list_head
*qtd_list
,
2949 int epnum
, void **ptr
)
2951 struct fotg210_qh
*qh
= NULL
;
2952 __hc32 qh_addr_mask
= cpu_to_hc32(fotg210
, 0x7f);
2954 qh
= (struct fotg210_qh
*) *ptr
;
2955 if (unlikely(qh
== NULL
)) {
2956 /* can't sleep here, we have fotg210->lock... */
2957 qh
= qh_make(fotg210
, urb
, GFP_ATOMIC
);
2960 if (likely(qh
!= NULL
)) {
2961 struct fotg210_qtd
*qtd
;
2963 if (unlikely(list_empty(qtd_list
)))
2966 qtd
= list_entry(qtd_list
->next
, struct fotg210_qtd
,
2969 /* control qh may need patching ... */
2970 if (unlikely(epnum
== 0)) {
2971 /* usb_reset_device() briefly reverts to address 0 */
2972 if (usb_pipedevice(urb
->pipe
) == 0)
2973 qh
->hw
->hw_info1
&= ~qh_addr_mask
;
2976 /* just one way to queue requests: swap with the dummy qtd.
2977 * only hc or qh_refresh() ever modify the overlay.
2979 if (likely(qtd
!= NULL
)) {
2980 struct fotg210_qtd
*dummy
;
2984 /* to avoid racing the HC, use the dummy td instead of
2985 * the first td of our list (becomes new dummy). both
2986 * tds stay deactivated until we're done, when the
2987 * HC is allowed to fetch the old dummy (4.10.2).
2989 token
= qtd
->hw_token
;
2990 qtd
->hw_token
= HALT_BIT(fotg210
);
2994 dma
= dummy
->qtd_dma
;
2996 dummy
->qtd_dma
= dma
;
2998 list_del(&qtd
->qtd_list
);
2999 list_add(&dummy
->qtd_list
, qtd_list
);
3000 list_splice_tail(qtd_list
, &qh
->qtd_list
);
3002 fotg210_qtd_init(fotg210
, qtd
, qtd
->qtd_dma
);
3005 /* hc must see the new dummy at list end */
3007 qtd
= list_entry(qh
->qtd_list
.prev
,
3008 struct fotg210_qtd
, qtd_list
);
3009 qtd
->hw_next
= QTD_NEXT(fotg210
, dma
);
3011 /* let the hc process these next qtds */
3013 dummy
->hw_token
= token
;
3021 static int submit_async(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
3022 struct list_head
*qtd_list
, gfp_t mem_flags
)
3025 unsigned long flags
;
3026 struct fotg210_qh
*qh
= NULL
;
3029 epnum
= urb
->ep
->desc
.bEndpointAddress
;
3031 #ifdef FOTG210_URB_TRACE
3033 struct fotg210_qtd
*qtd
;
3035 qtd
= list_entry(qtd_list
->next
, struct fotg210_qtd
, qtd_list
);
3036 fotg210_dbg(fotg210
,
3037 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
3038 __func__
, urb
->dev
->devpath
, urb
,
3039 epnum
& 0x0f, (epnum
& USB_DIR_IN
)
3041 urb
->transfer_buffer_length
,
3042 qtd
, urb
->ep
->hcpriv
);
3046 spin_lock_irqsave(&fotg210
->lock
, flags
);
3047 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210
)))) {
3051 rc
= usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210
), urb
);
3055 qh
= qh_append_tds(fotg210
, urb
, qtd_list
, epnum
, &urb
->ep
->hcpriv
);
3056 if (unlikely(qh
== NULL
)) {
3057 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210
), urb
);
3062 /* Control/bulk operations through TTs don't need scheduling,
3063 * the HC and TT handle it when the TT has a buffer ready.
3065 if (likely(qh
->qh_state
== QH_STATE_IDLE
))
3066 qh_link_async(fotg210
, qh
);
3068 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
3069 if (unlikely(qh
== NULL
))
3070 qtd_list_free(fotg210
, urb
, qtd_list
);
3074 static void single_unlink_async(struct fotg210_hcd
*fotg210
,
3075 struct fotg210_qh
*qh
)
3077 struct fotg210_qh
*prev
;
3079 /* Add to the end of the list of QHs waiting for the next IAAD */
3080 qh
->qh_state
= QH_STATE_UNLINK
;
3081 if (fotg210
->async_unlink
)
3082 fotg210
->async_unlink_last
->unlink_next
= qh
;
3084 fotg210
->async_unlink
= qh
;
3085 fotg210
->async_unlink_last
= qh
;
3087 /* Unlink it from the schedule */
3088 prev
= fotg210
->async
;
3089 while (prev
->qh_next
.qh
!= qh
)
3090 prev
= prev
->qh_next
.qh
;
3092 prev
->hw
->hw_next
= qh
->hw
->hw_next
;
3093 prev
->qh_next
= qh
->qh_next
;
3094 if (fotg210
->qh_scan_next
== qh
)
3095 fotg210
->qh_scan_next
= qh
->qh_next
.qh
;
3098 static void start_iaa_cycle(struct fotg210_hcd
*fotg210
, bool nested
)
3101 * Do nothing if an IAA cycle is already running or
3102 * if one will be started shortly.
3104 if (fotg210
->async_iaa
|| fotg210
->async_unlinking
)
3107 /* Do all the waiting QHs at once */
3108 fotg210
->async_iaa
= fotg210
->async_unlink
;
3109 fotg210
->async_unlink
= NULL
;
3111 /* If the controller isn't running, we don't have to wait for it */
3112 if (unlikely(fotg210
->rh_state
< FOTG210_RH_RUNNING
)) {
3113 if (!nested
) /* Avoid recursion */
3114 end_unlink_async(fotg210
);
3116 /* Otherwise start a new IAA cycle */
3117 } else if (likely(fotg210
->rh_state
== FOTG210_RH_RUNNING
)) {
3118 /* Make sure the unlinks are all visible to the hardware */
3121 fotg210_writel(fotg210
, fotg210
->command
| CMD_IAAD
,
3122 &fotg210
->regs
->command
);
3123 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
3124 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_IAA_WATCHDOG
,
3129 /* the async qh for the qtds being unlinked are now gone from the HC */
3131 static void end_unlink_async(struct fotg210_hcd
*fotg210
)
3133 struct fotg210_qh
*qh
;
3135 /* Process the idle QHs */
3137 fotg210
->async_unlinking
= true;
3138 while (fotg210
->async_iaa
) {
3139 qh
= fotg210
->async_iaa
;
3140 fotg210
->async_iaa
= qh
->unlink_next
;
3141 qh
->unlink_next
= NULL
;
3143 qh
->qh_state
= QH_STATE_IDLE
;
3144 qh
->qh_next
.qh
= NULL
;
3146 qh_completions(fotg210
, qh
);
3147 if (!list_empty(&qh
->qtd_list
) &&
3148 fotg210
->rh_state
== FOTG210_RH_RUNNING
)
3149 qh_link_async(fotg210
, qh
);
3150 disable_async(fotg210
);
3152 fotg210
->async_unlinking
= false;
3154 /* Start a new IAA cycle if any QHs are waiting for it */
3155 if (fotg210
->async_unlink
) {
3156 start_iaa_cycle(fotg210
, true);
3157 if (unlikely(fotg210
->rh_state
< FOTG210_RH_RUNNING
))
3162 static void unlink_empty_async(struct fotg210_hcd
*fotg210
)
3164 struct fotg210_qh
*qh
, *next
;
3165 bool stopped
= (fotg210
->rh_state
< FOTG210_RH_RUNNING
);
3166 bool check_unlinks_later
= false;
3168 /* Unlink all the async QHs that have been empty for a timer cycle */
3169 next
= fotg210
->async
->qh_next
.qh
;
3172 next
= qh
->qh_next
.qh
;
3174 if (list_empty(&qh
->qtd_list
) &&
3175 qh
->qh_state
== QH_STATE_LINKED
) {
3176 if (!stopped
&& qh
->unlink_cycle
==
3177 fotg210
->async_unlink_cycle
)
3178 check_unlinks_later
= true;
3180 single_unlink_async(fotg210
, qh
);
3184 /* Start a new IAA cycle if any QHs are waiting for it */
3185 if (fotg210
->async_unlink
)
3186 start_iaa_cycle(fotg210
, false);
3188 /* QHs that haven't been empty for long enough will be handled later */
3189 if (check_unlinks_later
) {
3190 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_ASYNC_UNLINKS
,
3192 ++fotg210
->async_unlink_cycle
;
3196 /* makes sure the async qh will become idle */
3197 /* caller must own fotg210->lock */
3199 static void start_unlink_async(struct fotg210_hcd
*fotg210
,
3200 struct fotg210_qh
*qh
)
3203 * If the QH isn't linked then there's nothing we can do
3204 * unless we were called during a giveback, in which case
3205 * qh_completions() has to deal with it.
3207 if (qh
->qh_state
!= QH_STATE_LINKED
) {
3208 if (qh
->qh_state
== QH_STATE_COMPLETING
)
3209 qh
->needs_rescan
= 1;
3213 single_unlink_async(fotg210
, qh
);
3214 start_iaa_cycle(fotg210
, false);
3217 static void scan_async(struct fotg210_hcd
*fotg210
)
3219 struct fotg210_qh
*qh
;
3220 bool check_unlinks_later
= false;
3222 fotg210
->qh_scan_next
= fotg210
->async
->qh_next
.qh
;
3223 while (fotg210
->qh_scan_next
) {
3224 qh
= fotg210
->qh_scan_next
;
3225 fotg210
->qh_scan_next
= qh
->qh_next
.qh
;
3227 /* clean any finished work for this qh */
3228 if (!list_empty(&qh
->qtd_list
)) {
3232 * Unlinks could happen here; completion reporting
3233 * drops the lock. That's why fotg210->qh_scan_next
3234 * always holds the next qh to scan; if the next qh
3235 * gets unlinked then fotg210->qh_scan_next is adjusted
3236 * in single_unlink_async().
3238 temp
= qh_completions(fotg210
, qh
);
3239 if (qh
->needs_rescan
) {
3240 start_unlink_async(fotg210
, qh
);
3241 } else if (list_empty(&qh
->qtd_list
)
3242 && qh
->qh_state
== QH_STATE_LINKED
) {
3243 qh
->unlink_cycle
= fotg210
->async_unlink_cycle
;
3244 check_unlinks_later
= true;
3245 } else if (temp
!= 0)
3251 * Unlink empty entries, reducing DMA usage as well
3252 * as HCD schedule-scanning costs. Delay for any qh
3253 * we just scanned, there's a not-unusual case that it
3254 * doesn't stay idle for long.
3256 if (check_unlinks_later
&& fotg210
->rh_state
== FOTG210_RH_RUNNING
&&
3257 !(fotg210
->enabled_hrtimer_events
&
3258 BIT(FOTG210_HRTIMER_ASYNC_UNLINKS
))) {
3259 fotg210_enable_event(fotg210
,
3260 FOTG210_HRTIMER_ASYNC_UNLINKS
, true);
3261 ++fotg210
->async_unlink_cycle
;
3264 /* EHCI scheduled transaction support: interrupt, iso, split iso
3265 * These are called "periodic" transactions in the EHCI spec.
3267 * Note that for interrupt transfers, the QH/QTD manipulation is shared
3268 * with the "asynchronous" transaction support (control/bulk transfers).
3269 * The only real difference is in how interrupt transfers are scheduled.
3271 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
3272 * It keeps track of every ITD (or SITD) that's linked, and holds enough
3273 * pre-calculated schedule data to make appending to the queue be quick.
3275 static int fotg210_get_frame(struct usb_hcd
*hcd
);
3277 /* periodic_next_shadow - return "next" pointer on shadow list
3278 * @periodic: host pointer to qh/itd
3279 * @tag: hardware tag for type of this record
3281 static union fotg210_shadow
*periodic_next_shadow(struct fotg210_hcd
*fotg210
,
3282 union fotg210_shadow
*periodic
, __hc32 tag
)
3284 switch (hc32_to_cpu(fotg210
, tag
)) {
3286 return &periodic
->qh
->qh_next
;
3288 return &periodic
->fstn
->fstn_next
;
3290 return &periodic
->itd
->itd_next
;
3294 static __hc32
*shadow_next_periodic(struct fotg210_hcd
*fotg210
,
3295 union fotg210_shadow
*periodic
, __hc32 tag
)
3297 switch (hc32_to_cpu(fotg210
, tag
)) {
3298 /* our fotg210_shadow.qh is actually software part */
3300 return &periodic
->qh
->hw
->hw_next
;
3301 /* others are hw parts */
3303 return periodic
->hw_next
;
3307 /* caller must hold fotg210->lock */
3308 static void periodic_unlink(struct fotg210_hcd
*fotg210
, unsigned frame
,
3311 union fotg210_shadow
*prev_p
= &fotg210
->pshadow
[frame
];
3312 __hc32
*hw_p
= &fotg210
->periodic
[frame
];
3313 union fotg210_shadow here
= *prev_p
;
3315 /* find predecessor of "ptr"; hw and shadow lists are in sync */
3316 while (here
.ptr
&& here
.ptr
!= ptr
) {
3317 prev_p
= periodic_next_shadow(fotg210
, prev_p
,
3318 Q_NEXT_TYPE(fotg210
, *hw_p
));
3319 hw_p
= shadow_next_periodic(fotg210
, &here
,
3320 Q_NEXT_TYPE(fotg210
, *hw_p
));
3323 /* an interrupt entry (at list end) could have been shared */
3327 /* update shadow and hardware lists ... the old "next" pointers
3328 * from ptr may still be in use, the caller updates them.
3330 *prev_p
= *periodic_next_shadow(fotg210
, &here
,
3331 Q_NEXT_TYPE(fotg210
, *hw_p
));
3333 *hw_p
= *shadow_next_periodic(fotg210
, &here
,
3334 Q_NEXT_TYPE(fotg210
, *hw_p
));
3337 /* how many of the uframe's 125 usecs are allocated? */
3338 static unsigned short periodic_usecs(struct fotg210_hcd
*fotg210
,
3339 unsigned frame
, unsigned uframe
)
3341 __hc32
*hw_p
= &fotg210
->periodic
[frame
];
3342 union fotg210_shadow
*q
= &fotg210
->pshadow
[frame
];
3344 struct fotg210_qh_hw
*hw
;
3347 switch (hc32_to_cpu(fotg210
, Q_NEXT_TYPE(fotg210
, *hw_p
))) {
3350 /* is it in the S-mask? */
3351 if (hw
->hw_info2
& cpu_to_hc32(fotg210
, 1 << uframe
))
3352 usecs
+= q
->qh
->usecs
;
3353 /* ... or C-mask? */
3354 if (hw
->hw_info2
& cpu_to_hc32(fotg210
,
3356 usecs
+= q
->qh
->c_usecs
;
3357 hw_p
= &hw
->hw_next
;
3358 q
= &q
->qh
->qh_next
;
3360 /* case Q_TYPE_FSTN: */
3362 /* for "save place" FSTNs, count the relevant INTR
3363 * bandwidth from the previous frame
3365 if (q
->fstn
->hw_prev
!= FOTG210_LIST_END(fotg210
))
3366 fotg210_dbg(fotg210
, "ignoring FSTN cost ...\n");
3368 hw_p
= &q
->fstn
->hw_next
;
3369 q
= &q
->fstn
->fstn_next
;
3372 if (q
->itd
->hw_transaction
[uframe
])
3373 usecs
+= q
->itd
->stream
->usecs
;
3374 hw_p
= &q
->itd
->hw_next
;
3375 q
= &q
->itd
->itd_next
;
3379 if (usecs
> fotg210
->uframe_periodic_max
)
3380 fotg210_err(fotg210
, "uframe %d sched overrun: %d usecs\n",
3381 frame
* 8 + uframe
, usecs
);
3385 static int same_tt(struct usb_device
*dev1
, struct usb_device
*dev2
)
3387 if (!dev1
->tt
|| !dev2
->tt
)
3389 if (dev1
->tt
!= dev2
->tt
)
3391 if (dev1
->tt
->multi
)
3392 return dev1
->ttport
== dev2
->ttport
;
3397 /* return true iff the device's transaction translator is available
3398 * for a periodic transfer starting at the specified frame, using
3399 * all the uframes in the mask.
3401 static int tt_no_collision(struct fotg210_hcd
*fotg210
, unsigned period
,
3402 struct usb_device
*dev
, unsigned frame
, u32 uf_mask
)
3404 if (period
== 0) /* error */
3407 /* note bandwidth wastage: split never follows csplit
3408 * (different dev or endpoint) until the next uframe.
3409 * calling convention doesn't make that distinction.
3411 for (; frame
< fotg210
->periodic_size
; frame
+= period
) {
3412 union fotg210_shadow here
;
3414 struct fotg210_qh_hw
*hw
;
3416 here
= fotg210
->pshadow
[frame
];
3417 type
= Q_NEXT_TYPE(fotg210
, fotg210
->periodic
[frame
]);
3419 switch (hc32_to_cpu(fotg210
, type
)) {
3421 type
= Q_NEXT_TYPE(fotg210
, here
.itd
->hw_next
);
3422 here
= here
.itd
->itd_next
;
3426 if (same_tt(dev
, here
.qh
->dev
)) {
3429 mask
= hc32_to_cpu(fotg210
,
3431 /* "knows" no gap is needed */
3436 type
= Q_NEXT_TYPE(fotg210
, hw
->hw_next
);
3437 here
= here
.qh
->qh_next
;
3439 /* case Q_TYPE_FSTN: */
3441 fotg210_dbg(fotg210
,
3442 "periodic frame %d bogus type %d\n",
3446 /* collision or error */
3455 static void enable_periodic(struct fotg210_hcd
*fotg210
)
3457 if (fotg210
->periodic_count
++)
3460 /* Stop waiting to turn off the periodic schedule */
3461 fotg210
->enabled_hrtimer_events
&=
3462 ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC
);
3464 /* Don't start the schedule until PSS is 0 */
3465 fotg210_poll_PSS(fotg210
);
3466 turn_on_io_watchdog(fotg210
);
3469 static void disable_periodic(struct fotg210_hcd
*fotg210
)
3471 if (--fotg210
->periodic_count
)
3474 /* Don't turn off the schedule until PSS is 1 */
3475 fotg210_poll_PSS(fotg210
);
3478 /* periodic schedule slots have iso tds (normal or split) first, then a
3479 * sparse tree for active interrupt transfers.
3481 * this just links in a qh; caller guarantees uframe masks are set right.
3482 * no FSTN support (yet; fotg210 0.96+)
3484 static void qh_link_periodic(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
3487 unsigned period
= qh
->period
;
3489 dev_dbg(&qh
->dev
->dev
,
3490 "link qh%d-%04x/%p start %d [%d/%d us]\n", period
,
3491 hc32_to_cpup(fotg210
, &qh
->hw
->hw_info2
) &
3492 (QH_CMASK
| QH_SMASK
), qh
, qh
->start
, qh
->usecs
,
3495 /* high bandwidth, or otherwise every microframe */
3499 for (i
= qh
->start
; i
< fotg210
->periodic_size
; i
+= period
) {
3500 union fotg210_shadow
*prev
= &fotg210
->pshadow
[i
];
3501 __hc32
*hw_p
= &fotg210
->periodic
[i
];
3502 union fotg210_shadow here
= *prev
;
3505 /* skip the iso nodes at list head */
3507 type
= Q_NEXT_TYPE(fotg210
, *hw_p
);
3508 if (type
== cpu_to_hc32(fotg210
, Q_TYPE_QH
))
3510 prev
= periodic_next_shadow(fotg210
, prev
, type
);
3511 hw_p
= shadow_next_periodic(fotg210
, &here
, type
);
3515 /* sorting each branch by period (slow-->fast)
3516 * enables sharing interior tree nodes
3518 while (here
.ptr
&& qh
!= here
.qh
) {
3519 if (qh
->period
> here
.qh
->period
)
3521 prev
= &here
.qh
->qh_next
;
3522 hw_p
= &here
.qh
->hw
->hw_next
;
3525 /* link in this qh, unless some earlier pass did that */
3526 if (qh
!= here
.qh
) {
3529 qh
->hw
->hw_next
= *hw_p
;
3532 *hw_p
= QH_NEXT(fotg210
, qh
->qh_dma
);
3535 qh
->qh_state
= QH_STATE_LINKED
;
3538 /* update per-qh bandwidth for usbfs */
3539 fotg210_to_hcd(fotg210
)->self
.bandwidth_allocated
+= qh
->period
3540 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
3543 list_add(&qh
->intr_node
, &fotg210
->intr_qh_list
);
3545 /* maybe enable periodic schedule processing */
3546 ++fotg210
->intr_count
;
3547 enable_periodic(fotg210
);
3550 static void qh_unlink_periodic(struct fotg210_hcd
*fotg210
,
3551 struct fotg210_qh
*qh
)
3557 * If qh is for a low/full-speed device, simply unlinking it
3558 * could interfere with an ongoing split transaction. To unlink
3559 * it safely would require setting the QH_INACTIVATE bit and
3560 * waiting at least one frame, as described in EHCI 4.12.2.5.
3562 * We won't bother with any of this. Instead, we assume that the
3563 * only reason for unlinking an interrupt QH while the current URB
3564 * is still active is to dequeue all the URBs (flush the whole
3567 * If rebalancing the periodic schedule is ever implemented, this
3568 * approach will no longer be valid.
3571 /* high bandwidth, or otherwise part of every microframe */
3572 period
= qh
->period
;
3576 for (i
= qh
->start
; i
< fotg210
->periodic_size
; i
+= period
)
3577 periodic_unlink(fotg210
, i
, qh
);
3579 /* update per-qh bandwidth for usbfs */
3580 fotg210_to_hcd(fotg210
)->self
.bandwidth_allocated
-= qh
->period
3581 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
3584 dev_dbg(&qh
->dev
->dev
,
3585 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
3586 qh
->period
, hc32_to_cpup(fotg210
, &qh
->hw
->hw_info2
) &
3587 (QH_CMASK
| QH_SMASK
), qh
, qh
->start
, qh
->usecs
,
3590 /* qh->qh_next still "live" to HC */
3591 qh
->qh_state
= QH_STATE_UNLINK
;
3592 qh
->qh_next
.ptr
= NULL
;
3594 if (fotg210
->qh_scan_next
== qh
)
3595 fotg210
->qh_scan_next
= list_entry(qh
->intr_node
.next
,
3596 struct fotg210_qh
, intr_node
);
3597 list_del(&qh
->intr_node
);
3600 static void start_unlink_intr(struct fotg210_hcd
*fotg210
,
3601 struct fotg210_qh
*qh
)
3603 /* If the QH isn't linked then there's nothing we can do
3604 * unless we were called during a giveback, in which case
3605 * qh_completions() has to deal with it.
3607 if (qh
->qh_state
!= QH_STATE_LINKED
) {
3608 if (qh
->qh_state
== QH_STATE_COMPLETING
)
3609 qh
->needs_rescan
= 1;
3613 qh_unlink_periodic(fotg210
, qh
);
3615 /* Make sure the unlinks are visible before starting the timer */
3619 * The EHCI spec doesn't say how long it takes the controller to
3620 * stop accessing an unlinked interrupt QH. The timer delay is
3621 * 9 uframes; presumably that will be long enough.
3623 qh
->unlink_cycle
= fotg210
->intr_unlink_cycle
;
3625 /* New entries go at the end of the intr_unlink list */
3626 if (fotg210
->intr_unlink
)
3627 fotg210
->intr_unlink_last
->unlink_next
= qh
;
3629 fotg210
->intr_unlink
= qh
;
3630 fotg210
->intr_unlink_last
= qh
;
3632 if (fotg210
->intr_unlinking
)
3633 ; /* Avoid recursive calls */
3634 else if (fotg210
->rh_state
< FOTG210_RH_RUNNING
)
3635 fotg210_handle_intr_unlinks(fotg210
);
3636 else if (fotg210
->intr_unlink
== qh
) {
3637 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_UNLINK_INTR
,
3639 ++fotg210
->intr_unlink_cycle
;
3643 static void end_unlink_intr(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
3645 struct fotg210_qh_hw
*hw
= qh
->hw
;
3648 qh
->qh_state
= QH_STATE_IDLE
;
3649 hw
->hw_next
= FOTG210_LIST_END(fotg210
);
3651 qh_completions(fotg210
, qh
);
3653 /* reschedule QH iff another request is queued */
3654 if (!list_empty(&qh
->qtd_list
) &&
3655 fotg210
->rh_state
== FOTG210_RH_RUNNING
) {
3656 rc
= qh_schedule(fotg210
, qh
);
3658 /* An error here likely indicates handshake failure
3659 * or no space left in the schedule. Neither fault
3660 * should happen often ...
3662 * FIXME kill the now-dysfunctional queued urbs
3665 fotg210_err(fotg210
, "can't reschedule qh %p, err %d\n",
3669 /* maybe turn off periodic schedule */
3670 --fotg210
->intr_count
;
3671 disable_periodic(fotg210
);
3674 static int check_period(struct fotg210_hcd
*fotg210
, unsigned frame
,
3675 unsigned uframe
, unsigned period
, unsigned usecs
)
3679 /* complete split running into next frame?
3680 * given FSTN support, we could sometimes check...
3685 /* convert "usecs we need" to "max already claimed" */
3686 usecs
= fotg210
->uframe_periodic_max
- usecs
;
3688 /* we "know" 2 and 4 uframe intervals were rejected; so
3689 * for period 0, check _every_ microframe in the schedule.
3691 if (unlikely(period
== 0)) {
3693 for (uframe
= 0; uframe
< 7; uframe
++) {
3694 claimed
= periodic_usecs(fotg210
, frame
,
3696 if (claimed
> usecs
)
3699 } while ((frame
+= 1) < fotg210
->periodic_size
);
3701 /* just check the specified uframe, at that period */
3704 claimed
= periodic_usecs(fotg210
, frame
, uframe
);
3705 if (claimed
> usecs
)
3707 } while ((frame
+= period
) < fotg210
->periodic_size
);
3714 static int check_intr_schedule(struct fotg210_hcd
*fotg210
, unsigned frame
,
3715 unsigned uframe
, const struct fotg210_qh
*qh
, __hc32
*c_maskp
)
3717 int retval
= -ENOSPC
;
3720 if (qh
->c_usecs
&& uframe
>= 6) /* FSTN territory? */
3723 if (!check_period(fotg210
, frame
, uframe
, qh
->period
, qh
->usecs
))
3731 /* Make sure this tt's buffer is also available for CSPLITs.
3732 * We pessimize a bit; probably the typical full speed case
3733 * doesn't need the second CSPLIT.
3735 * NOTE: both SPLIT and CSPLIT could be checked in just
3738 mask
= 0x03 << (uframe
+ qh
->gap_uf
);
3739 *c_maskp
= cpu_to_hc32(fotg210
, mask
<< 8);
3741 mask
|= 1 << uframe
;
3742 if (tt_no_collision(fotg210
, qh
->period
, qh
->dev
, frame
, mask
)) {
3743 if (!check_period(fotg210
, frame
, uframe
+ qh
->gap_uf
+ 1,
3744 qh
->period
, qh
->c_usecs
))
3746 if (!check_period(fotg210
, frame
, uframe
+ qh
->gap_uf
,
3747 qh
->period
, qh
->c_usecs
))
3755 /* "first fit" scheduling policy used the first time through,
3756 * or when the previous schedule slot can't be re-used.
3758 static int qh_schedule(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
3763 unsigned frame
; /* 0..(qh->period - 1), or NO_FRAME */
3764 struct fotg210_qh_hw
*hw
= qh
->hw
;
3766 qh_refresh(fotg210
, qh
);
3767 hw
->hw_next
= FOTG210_LIST_END(fotg210
);
3770 /* reuse the previous schedule slots, if we can */
3771 if (frame
< qh
->period
) {
3772 uframe
= ffs(hc32_to_cpup(fotg210
, &hw
->hw_info2
) & QH_SMASK
);
3773 status
= check_intr_schedule(fotg210
, frame
, --uframe
,
3781 /* else scan the schedule to find a group of slots such that all
3782 * uframes have enough periodic bandwidth available.
3785 /* "normal" case, uframing flexible except with splits */
3789 for (i
= qh
->period
; status
&& i
> 0; --i
) {
3790 frame
= ++fotg210
->random_frame
% qh
->period
;
3791 for (uframe
= 0; uframe
< 8; uframe
++) {
3792 status
= check_intr_schedule(fotg210
,
3800 /* qh->period == 0 means every uframe */
3803 status
= check_intr_schedule(fotg210
, 0, 0, qh
,
3810 /* reset S-frame and (maybe) C-frame masks */
3811 hw
->hw_info2
&= cpu_to_hc32(fotg210
, ~(QH_CMASK
| QH_SMASK
));
3812 hw
->hw_info2
|= qh
->period
3813 ? cpu_to_hc32(fotg210
, 1 << uframe
)
3814 : cpu_to_hc32(fotg210
, QH_SMASK
);
3815 hw
->hw_info2
|= c_mask
;
3817 fotg210_dbg(fotg210
, "reused qh %p schedule\n", qh
);
3819 /* stuff into the periodic schedule */
3820 qh_link_periodic(fotg210
, qh
);
3825 static int intr_submit(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
3826 struct list_head
*qtd_list
, gfp_t mem_flags
)
3829 unsigned long flags
;
3830 struct fotg210_qh
*qh
;
3832 struct list_head empty
;
3834 /* get endpoint and transfer/schedule data */
3835 epnum
= urb
->ep
->desc
.bEndpointAddress
;
3837 spin_lock_irqsave(&fotg210
->lock
, flags
);
3839 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210
)))) {
3840 status
= -ESHUTDOWN
;
3841 goto done_not_linked
;
3843 status
= usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210
), urb
);
3844 if (unlikely(status
))
3845 goto done_not_linked
;
3847 /* get qh and force any scheduling errors */
3848 INIT_LIST_HEAD(&empty
);
3849 qh
= qh_append_tds(fotg210
, urb
, &empty
, epnum
, &urb
->ep
->hcpriv
);
3854 if (qh
->qh_state
== QH_STATE_IDLE
) {
3855 status
= qh_schedule(fotg210
, qh
);
3860 /* then queue the urb's tds to the qh */
3861 qh
= qh_append_tds(fotg210
, urb
, qtd_list
, epnum
, &urb
->ep
->hcpriv
);
3864 /* ... update usbfs periodic stats */
3865 fotg210_to_hcd(fotg210
)->self
.bandwidth_int_reqs
++;
3868 if (unlikely(status
))
3869 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210
), urb
);
3871 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
3873 qtd_list_free(fotg210
, urb
, qtd_list
);
3878 static void scan_intr(struct fotg210_hcd
*fotg210
)
3880 struct fotg210_qh
*qh
;
3882 list_for_each_entry_safe(qh
, fotg210
->qh_scan_next
,
3883 &fotg210
->intr_qh_list
, intr_node
) {
3885 /* clean any finished work for this qh */
3886 if (!list_empty(&qh
->qtd_list
)) {
3890 * Unlinks could happen here; completion reporting
3891 * drops the lock. That's why fotg210->qh_scan_next
3892 * always holds the next qh to scan; if the next qh
3893 * gets unlinked then fotg210->qh_scan_next is adjusted
3894 * in qh_unlink_periodic().
3896 temp
= qh_completions(fotg210
, qh
);
3897 if (unlikely(qh
->needs_rescan
||
3898 (list_empty(&qh
->qtd_list
) &&
3899 qh
->qh_state
== QH_STATE_LINKED
)))
3900 start_unlink_intr(fotg210
, qh
);
3907 /* fotg210_iso_stream ops work with both ITD and SITD */
3909 static struct fotg210_iso_stream
*iso_stream_alloc(gfp_t mem_flags
)
3911 struct fotg210_iso_stream
*stream
;
3913 stream
= kzalloc(sizeof(*stream
), mem_flags
);
3914 if (likely(stream
!= NULL
)) {
3915 INIT_LIST_HEAD(&stream
->td_list
);
3916 INIT_LIST_HEAD(&stream
->free_list
);
3917 stream
->next_uframe
= -1;
3922 static void iso_stream_init(struct fotg210_hcd
*fotg210
,
3923 struct fotg210_iso_stream
*stream
, struct usb_device
*dev
,
3924 int pipe
, unsigned interval
)
3927 unsigned epnum
, maxp
;
3933 * this might be a "high bandwidth" highspeed endpoint,
3934 * as encoded in the ep descriptor's wMaxPacket field
3936 epnum
= usb_pipeendpoint(pipe
);
3937 is_input
= usb_pipein(pipe
) ? USB_DIR_IN
: 0;
3938 maxp
= usb_maxpacket(dev
, pipe
, !is_input
);
3944 maxp
= max_packet(maxp
);
3945 multi
= hb_mult(maxp
);
3949 stream
->buf0
= cpu_to_hc32(fotg210
, (epnum
<< 8) | dev
->devnum
);
3950 stream
->buf1
= cpu_to_hc32(fotg210
, buf1
);
3951 stream
->buf2
= cpu_to_hc32(fotg210
, multi
);
3953 /* usbfs wants to report the average usecs per frame tied up
3954 * when transfers on this endpoint are scheduled ...
3956 if (dev
->speed
== USB_SPEED_FULL
) {
3958 stream
->usecs
= NS_TO_US(usb_calc_bus_time(dev
->speed
,
3959 is_input
, 1, maxp
));
3962 stream
->highspeed
= 1;
3963 stream
->usecs
= HS_USECS_ISO(maxp
);
3965 bandwidth
= stream
->usecs
* 8;
3966 bandwidth
/= interval
;
3968 stream
->bandwidth
= bandwidth
;
3970 stream
->bEndpointAddress
= is_input
| epnum
;
3971 stream
->interval
= interval
;
3972 stream
->maxp
= maxp
;
3975 static struct fotg210_iso_stream
*iso_stream_find(struct fotg210_hcd
*fotg210
,
3979 struct fotg210_iso_stream
*stream
;
3980 struct usb_host_endpoint
*ep
;
3981 unsigned long flags
;
3983 epnum
= usb_pipeendpoint(urb
->pipe
);
3984 if (usb_pipein(urb
->pipe
))
3985 ep
= urb
->dev
->ep_in
[epnum
];
3987 ep
= urb
->dev
->ep_out
[epnum
];
3989 spin_lock_irqsave(&fotg210
->lock
, flags
);
3990 stream
= ep
->hcpriv
;
3992 if (unlikely(stream
== NULL
)) {
3993 stream
= iso_stream_alloc(GFP_ATOMIC
);
3994 if (likely(stream
!= NULL
)) {
3995 ep
->hcpriv
= stream
;
3997 iso_stream_init(fotg210
, stream
, urb
->dev
, urb
->pipe
,
4001 /* if dev->ep[epnum] is a QH, hw is set */
4002 } else if (unlikely(stream
->hw
!= NULL
)) {
4003 fotg210_dbg(fotg210
, "dev %s ep%d%s, not iso??\n",
4004 urb
->dev
->devpath
, epnum
,
4005 usb_pipein(urb
->pipe
) ? "in" : "out");
4009 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4013 /* fotg210_iso_sched ops can be ITD-only or SITD-only */
4015 static struct fotg210_iso_sched
*iso_sched_alloc(unsigned packets
,
4018 struct fotg210_iso_sched
*iso_sched
;
4019 int size
= sizeof(*iso_sched
);
4021 size
+= packets
* sizeof(struct fotg210_iso_packet
);
4022 iso_sched
= kzalloc(size
, mem_flags
);
4023 if (likely(iso_sched
!= NULL
))
4024 INIT_LIST_HEAD(&iso_sched
->td_list
);
4029 static inline void itd_sched_init(struct fotg210_hcd
*fotg210
,
4030 struct fotg210_iso_sched
*iso_sched
,
4031 struct fotg210_iso_stream
*stream
, struct urb
*urb
)
4034 dma_addr_t dma
= urb
->transfer_dma
;
4036 /* how many uframes are needed for these transfers */
4037 iso_sched
->span
= urb
->number_of_packets
* stream
->interval
;
4039 /* figure out per-uframe itd fields that we'll need later
4040 * when we fit new itds into the schedule.
4042 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
4043 struct fotg210_iso_packet
*uframe
= &iso_sched
->packet
[i
];
4048 length
= urb
->iso_frame_desc
[i
].length
;
4049 buf
= dma
+ urb
->iso_frame_desc
[i
].offset
;
4051 trans
= FOTG210_ISOC_ACTIVE
;
4052 trans
|= buf
& 0x0fff;
4053 if (unlikely(((i
+ 1) == urb
->number_of_packets
))
4054 && !(urb
->transfer_flags
& URB_NO_INTERRUPT
))
4055 trans
|= FOTG210_ITD_IOC
;
4056 trans
|= length
<< 16;
4057 uframe
->transaction
= cpu_to_hc32(fotg210
, trans
);
4059 /* might need to cross a buffer page within a uframe */
4060 uframe
->bufp
= (buf
& ~(u64
)0x0fff);
4062 if (unlikely((uframe
->bufp
!= (buf
& ~(u64
)0x0fff))))
4067 static void iso_sched_free(struct fotg210_iso_stream
*stream
,
4068 struct fotg210_iso_sched
*iso_sched
)
4072 /* caller must hold fotg210->lock!*/
4073 list_splice(&iso_sched
->td_list
, &stream
->free_list
);
4077 static int itd_urb_transaction(struct fotg210_iso_stream
*stream
,
4078 struct fotg210_hcd
*fotg210
, struct urb
*urb
, gfp_t mem_flags
)
4080 struct fotg210_itd
*itd
;
4084 struct fotg210_iso_sched
*sched
;
4085 unsigned long flags
;
4087 sched
= iso_sched_alloc(urb
->number_of_packets
, mem_flags
);
4088 if (unlikely(sched
== NULL
))
4091 itd_sched_init(fotg210
, sched
, stream
, urb
);
4093 if (urb
->interval
< 8)
4094 num_itds
= 1 + (sched
->span
+ 7) / 8;
4096 num_itds
= urb
->number_of_packets
;
4098 /* allocate/init ITDs */
4099 spin_lock_irqsave(&fotg210
->lock
, flags
);
4100 for (i
= 0; i
< num_itds
; i
++) {
4103 * Use iTDs from the free list, but not iTDs that may
4104 * still be in use by the hardware.
4106 if (likely(!list_empty(&stream
->free_list
))) {
4107 itd
= list_first_entry(&stream
->free_list
,
4108 struct fotg210_itd
, itd_list
);
4109 if (itd
->frame
== fotg210
->now_frame
)
4111 list_del(&itd
->itd_list
);
4112 itd_dma
= itd
->itd_dma
;
4115 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4116 itd
= dma_pool_zalloc(fotg210
->itd_pool
, mem_flags
,
4118 spin_lock_irqsave(&fotg210
->lock
, flags
);
4120 iso_sched_free(stream
, sched
);
4121 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4126 itd
->itd_dma
= itd_dma
;
4127 list_add(&itd
->itd_list
, &sched
->td_list
);
4129 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4131 /* temporarily store schedule info in hcpriv */
4132 urb
->hcpriv
= sched
;
4133 urb
->error_count
= 0;
4137 static inline int itd_slot_ok(struct fotg210_hcd
*fotg210
, u32 mod
, u32 uframe
,
4138 u8 usecs
, u32 period
)
4142 /* can't commit more than uframe_periodic_max usec */
4143 if (periodic_usecs(fotg210
, uframe
>> 3, uframe
& 0x7)
4144 > (fotg210
->uframe_periodic_max
- usecs
))
4147 /* we know urb->interval is 2^N uframes */
4149 } while (uframe
< mod
);
4153 /* This scheduler plans almost as far into the future as it has actual
4154 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
4155 * "as small as possible" to be cache-friendlier.) That limits the size
4156 * transfers you can stream reliably; avoid more than 64 msec per urb.
4157 * Also avoid queue depths of less than fotg210's worst irq latency (affected
4158 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
4159 * and other factors); or more than about 230 msec total (for portability,
4160 * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
4163 #define SCHEDULE_SLOP 80 /* microframes */
4165 static int iso_stream_schedule(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
4166 struct fotg210_iso_stream
*stream
)
4168 u32 now
, next
, start
, period
, span
;
4170 unsigned mod
= fotg210
->periodic_size
<< 3;
4171 struct fotg210_iso_sched
*sched
= urb
->hcpriv
;
4173 period
= urb
->interval
;
4176 if (span
> mod
- SCHEDULE_SLOP
) {
4177 fotg210_dbg(fotg210
, "iso request %p too long\n", urb
);
4182 now
= fotg210_read_frame_index(fotg210
) & (mod
- 1);
4184 /* Typical case: reuse current schedule, stream is still active.
4185 * Hopefully there are no gaps from the host falling behind
4186 * (irq delays etc), but if there are we'll take the next
4187 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
4189 if (likely(!list_empty(&stream
->td_list
))) {
4192 /* For high speed devices, allow scheduling within the
4193 * isochronous scheduling threshold. For full speed devices
4194 * and Intel PCI-based controllers, don't (work around for
4197 if (!stream
->highspeed
&& fotg210
->fs_i_thresh
)
4198 next
= now
+ fotg210
->i_thresh
;
4202 /* Fell behind (by up to twice the slop amount)?
4203 * We decide based on the time of the last currently-scheduled
4204 * slot, not the time of the next available slot.
4206 excess
= (stream
->next_uframe
- period
- next
) & (mod
- 1);
4207 if (excess
>= mod
- 2 * SCHEDULE_SLOP
)
4208 start
= next
+ excess
- mod
+ period
*
4209 DIV_ROUND_UP(mod
- excess
, period
);
4211 start
= next
+ excess
+ period
;
4212 if (start
- now
>= mod
) {
4213 fotg210_dbg(fotg210
, "request %p would overflow (%d+%d >= %d)\n",
4214 urb
, start
- now
- period
, period
,
4221 /* need to schedule; when's the next (u)frame we could start?
4222 * this is bigger than fotg210->i_thresh allows; scheduling itself
4223 * isn't free, the slop should handle reasonably slow cpus. it
4224 * can also help high bandwidth if the dma and irq loads don't
4225 * jump until after the queue is primed.
4230 start
= SCHEDULE_SLOP
+ (now
& ~0x07);
4232 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
4234 /* find a uframe slot with enough bandwidth.
4235 * Early uframes are more precious because full-speed
4236 * iso IN transfers can't use late uframes,
4237 * and therefore they should be allocated last.
4243 /* check schedule: enough space? */
4244 if (itd_slot_ok(fotg210
, mod
, start
,
4245 stream
->usecs
, period
))
4247 } while (start
> next
&& !done
);
4249 /* no room in the schedule */
4251 fotg210_dbg(fotg210
, "iso resched full %p (now %d max %d)\n",
4252 urb
, now
, now
+ mod
);
4258 /* Tried to schedule too far into the future? */
4259 if (unlikely(start
- now
+ span
- period
>=
4260 mod
- 2 * SCHEDULE_SLOP
)) {
4261 fotg210_dbg(fotg210
, "request %p would overflow (%d+%d >= %d)\n",
4262 urb
, start
- now
, span
- period
,
4263 mod
- 2 * SCHEDULE_SLOP
);
4268 stream
->next_uframe
= start
& (mod
- 1);
4270 /* report high speed start in uframes; full speed, in frames */
4271 urb
->start_frame
= stream
->next_uframe
;
4272 if (!stream
->highspeed
)
4273 urb
->start_frame
>>= 3;
4275 /* Make sure scan_isoc() sees these */
4276 if (fotg210
->isoc_count
== 0)
4277 fotg210
->next_frame
= now
>> 3;
4281 iso_sched_free(stream
, sched
);
4286 static inline void itd_init(struct fotg210_hcd
*fotg210
,
4287 struct fotg210_iso_stream
*stream
, struct fotg210_itd
*itd
)
4291 /* it's been recently zeroed */
4292 itd
->hw_next
= FOTG210_LIST_END(fotg210
);
4293 itd
->hw_bufp
[0] = stream
->buf0
;
4294 itd
->hw_bufp
[1] = stream
->buf1
;
4295 itd
->hw_bufp
[2] = stream
->buf2
;
4297 for (i
= 0; i
< 8; i
++)
4300 /* All other fields are filled when scheduling */
4303 static inline void itd_patch(struct fotg210_hcd
*fotg210
,
4304 struct fotg210_itd
*itd
, struct fotg210_iso_sched
*iso_sched
,
4305 unsigned index
, u16 uframe
)
4307 struct fotg210_iso_packet
*uf
= &iso_sched
->packet
[index
];
4308 unsigned pg
= itd
->pg
;
4311 itd
->index
[uframe
] = index
;
4313 itd
->hw_transaction
[uframe
] = uf
->transaction
;
4314 itd
->hw_transaction
[uframe
] |= cpu_to_hc32(fotg210
, pg
<< 12);
4315 itd
->hw_bufp
[pg
] |= cpu_to_hc32(fotg210
, uf
->bufp
& ~(u32
)0);
4316 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(fotg210
, (u32
)(uf
->bufp
>> 32));
4318 /* iso_frame_desc[].offset must be strictly increasing */
4319 if (unlikely(uf
->cross
)) {
4320 u64 bufp
= uf
->bufp
+ 4096;
4323 itd
->hw_bufp
[pg
] |= cpu_to_hc32(fotg210
, bufp
& ~(u32
)0);
4324 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(fotg210
, (u32
)(bufp
>> 32));
4328 static inline void itd_link(struct fotg210_hcd
*fotg210
, unsigned frame
,
4329 struct fotg210_itd
*itd
)
4331 union fotg210_shadow
*prev
= &fotg210
->pshadow
[frame
];
4332 __hc32
*hw_p
= &fotg210
->periodic
[frame
];
4333 union fotg210_shadow here
= *prev
;
4336 /* skip any iso nodes which might belong to previous microframes */
4338 type
= Q_NEXT_TYPE(fotg210
, *hw_p
);
4339 if (type
== cpu_to_hc32(fotg210
, Q_TYPE_QH
))
4341 prev
= periodic_next_shadow(fotg210
, prev
, type
);
4342 hw_p
= shadow_next_periodic(fotg210
, &here
, type
);
4346 itd
->itd_next
= here
;
4347 itd
->hw_next
= *hw_p
;
4351 *hw_p
= cpu_to_hc32(fotg210
, itd
->itd_dma
| Q_TYPE_ITD
);
4354 /* fit urb's itds into the selected schedule slot; activate as needed */
4355 static void itd_link_urb(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
4356 unsigned mod
, struct fotg210_iso_stream
*stream
)
4359 unsigned next_uframe
, uframe
, frame
;
4360 struct fotg210_iso_sched
*iso_sched
= urb
->hcpriv
;
4361 struct fotg210_itd
*itd
;
4363 next_uframe
= stream
->next_uframe
& (mod
- 1);
4365 if (unlikely(list_empty(&stream
->td_list
))) {
4366 fotg210_to_hcd(fotg210
)->self
.bandwidth_allocated
4367 += stream
->bandwidth
;
4368 fotg210_dbg(fotg210
,
4369 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
4370 urb
->dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
4371 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out",
4373 next_uframe
>> 3, next_uframe
& 0x7);
4376 /* fill iTDs uframe by uframe */
4377 for (packet
= 0, itd
= NULL
; packet
< urb
->number_of_packets
;) {
4379 /* ASSERT: we have all necessary itds */
4381 /* ASSERT: no itds for this endpoint in this uframe */
4383 itd
= list_entry(iso_sched
->td_list
.next
,
4384 struct fotg210_itd
, itd_list
);
4385 list_move_tail(&itd
->itd_list
, &stream
->td_list
);
4386 itd
->stream
= stream
;
4388 itd_init(fotg210
, stream
, itd
);
4391 uframe
= next_uframe
& 0x07;
4392 frame
= next_uframe
>> 3;
4394 itd_patch(fotg210
, itd
, iso_sched
, packet
, uframe
);
4396 next_uframe
+= stream
->interval
;
4397 next_uframe
&= mod
- 1;
4400 /* link completed itds into the schedule */
4401 if (((next_uframe
>> 3) != frame
)
4402 || packet
== urb
->number_of_packets
) {
4403 itd_link(fotg210
, frame
& (fotg210
->periodic_size
- 1),
4408 stream
->next_uframe
= next_uframe
;
4410 /* don't need that schedule data any more */
4411 iso_sched_free(stream
, iso_sched
);
4414 ++fotg210
->isoc_count
;
4415 enable_periodic(fotg210
);
4418 #define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
4419 FOTG210_ISOC_XACTERR)
4421 /* Process and recycle a completed ITD. Return true iff its urb completed,
4422 * and hence its completion callback probably added things to the hardware
4425 * Note that we carefully avoid recycling this descriptor until after any
4426 * completion callback runs, so that it won't be reused quickly. That is,
4427 * assuming (a) no more than two urbs per frame on this endpoint, and also
4428 * (b) only this endpoint's completions submit URBs. It seems some silicon
4429 * corrupts things if you reuse completed descriptors very quickly...
4431 static bool itd_complete(struct fotg210_hcd
*fotg210
, struct fotg210_itd
*itd
)
4433 struct urb
*urb
= itd
->urb
;
4434 struct usb_iso_packet_descriptor
*desc
;
4438 struct fotg210_iso_stream
*stream
= itd
->stream
;
4439 struct usb_device
*dev
;
4440 bool retval
= false;
4442 /* for each uframe with a packet */
4443 for (uframe
= 0; uframe
< 8; uframe
++) {
4444 if (likely(itd
->index
[uframe
] == -1))
4446 urb_index
= itd
->index
[uframe
];
4447 desc
= &urb
->iso_frame_desc
[urb_index
];
4449 t
= hc32_to_cpup(fotg210
, &itd
->hw_transaction
[uframe
]);
4450 itd
->hw_transaction
[uframe
] = 0;
4452 /* report transfer status */
4453 if (unlikely(t
& ISO_ERRS
)) {
4455 if (t
& FOTG210_ISOC_BUF_ERR
)
4456 desc
->status
= usb_pipein(urb
->pipe
)
4457 ? -ENOSR
/* hc couldn't read */
4458 : -ECOMM
; /* hc couldn't write */
4459 else if (t
& FOTG210_ISOC_BABBLE
)
4460 desc
->status
= -EOVERFLOW
;
4461 else /* (t & FOTG210_ISOC_XACTERR) */
4462 desc
->status
= -EPROTO
;
4464 /* HC need not update length with this error */
4465 if (!(t
& FOTG210_ISOC_BABBLE
)) {
4466 desc
->actual_length
=
4467 fotg210_itdlen(urb
, desc
, t
);
4468 urb
->actual_length
+= desc
->actual_length
;
4470 } else if (likely((t
& FOTG210_ISOC_ACTIVE
) == 0)) {
4472 desc
->actual_length
= fotg210_itdlen(urb
, desc
, t
);
4473 urb
->actual_length
+= desc
->actual_length
;
4475 /* URB was too late */
4476 desc
->status
= -EXDEV
;
4480 /* handle completion now? */
4481 if (likely((urb_index
+ 1) != urb
->number_of_packets
))
4484 /* ASSERT: it's really the last itd for this urb
4485 * list_for_each_entry (itd, &stream->td_list, itd_list)
4486 * BUG_ON (itd->urb == urb);
4489 /* give urb back to the driver; completion often (re)submits */
4491 fotg210_urb_done(fotg210
, urb
, 0);
4495 --fotg210
->isoc_count
;
4496 disable_periodic(fotg210
);
4498 if (unlikely(list_is_singular(&stream
->td_list
))) {
4499 fotg210_to_hcd(fotg210
)->self
.bandwidth_allocated
4500 -= stream
->bandwidth
;
4501 fotg210_dbg(fotg210
,
4502 "deschedule devp %s ep%d%s-iso\n",
4503 dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
4504 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out");
4510 /* Add to the end of the free list for later reuse */
4511 list_move_tail(&itd
->itd_list
, &stream
->free_list
);
4513 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
4514 if (list_empty(&stream
->td_list
)) {
4515 list_splice_tail_init(&stream
->free_list
,
4516 &fotg210
->cached_itd_list
);
4517 start_free_itds(fotg210
);
4523 static int itd_submit(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
4526 int status
= -EINVAL
;
4527 unsigned long flags
;
4528 struct fotg210_iso_stream
*stream
;
4530 /* Get iso_stream head */
4531 stream
= iso_stream_find(fotg210
, urb
);
4532 if (unlikely(stream
== NULL
)) {
4533 fotg210_dbg(fotg210
, "can't get iso stream\n");
4536 if (unlikely(urb
->interval
!= stream
->interval
&&
4537 fotg210_port_speed(fotg210
, 0) ==
4538 USB_PORT_STAT_HIGH_SPEED
)) {
4539 fotg210_dbg(fotg210
, "can't change iso interval %d --> %d\n",
4540 stream
->interval
, urb
->interval
);
4544 #ifdef FOTG210_URB_TRACE
4545 fotg210_dbg(fotg210
,
4546 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
4547 __func__
, urb
->dev
->devpath
, urb
,
4548 usb_pipeendpoint(urb
->pipe
),
4549 usb_pipein(urb
->pipe
) ? "in" : "out",
4550 urb
->transfer_buffer_length
,
4551 urb
->number_of_packets
, urb
->interval
,
4555 /* allocate ITDs w/o locking anything */
4556 status
= itd_urb_transaction(stream
, fotg210
, urb
, mem_flags
);
4557 if (unlikely(status
< 0)) {
4558 fotg210_dbg(fotg210
, "can't init itds\n");
4562 /* schedule ... need to lock */
4563 spin_lock_irqsave(&fotg210
->lock
, flags
);
4564 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210
)))) {
4565 status
= -ESHUTDOWN
;
4566 goto done_not_linked
;
4568 status
= usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210
), urb
);
4569 if (unlikely(status
))
4570 goto done_not_linked
;
4571 status
= iso_stream_schedule(fotg210
, urb
, stream
);
4572 if (likely(status
== 0))
4573 itd_link_urb(fotg210
, urb
, fotg210
->periodic_size
<< 3, stream
);
4575 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210
), urb
);
4577 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4582 static inline int scan_frame_queue(struct fotg210_hcd
*fotg210
, unsigned frame
,
4583 unsigned now_frame
, bool live
)
4587 union fotg210_shadow q
, *q_p
;
4590 /* scan each element in frame's queue for completions */
4591 q_p
= &fotg210
->pshadow
[frame
];
4592 hw_p
= &fotg210
->periodic
[frame
];
4594 type
= Q_NEXT_TYPE(fotg210
, *hw_p
);
4598 switch (hc32_to_cpu(fotg210
, type
)) {
4600 /* If this ITD is still active, leave it for
4601 * later processing ... check the next entry.
4602 * No need to check for activity unless the
4605 if (frame
== now_frame
&& live
) {
4607 for (uf
= 0; uf
< 8; uf
++) {
4608 if (q
.itd
->hw_transaction
[uf
] &
4609 ITD_ACTIVE(fotg210
))
4613 q_p
= &q
.itd
->itd_next
;
4614 hw_p
= &q
.itd
->hw_next
;
4615 type
= Q_NEXT_TYPE(fotg210
,
4622 /* Take finished ITDs out of the schedule
4623 * and process them: recycle, maybe report
4624 * URB completion. HC won't cache the
4625 * pointer for much longer, if at all.
4627 *q_p
= q
.itd
->itd_next
;
4628 *hw_p
= q
.itd
->hw_next
;
4629 type
= Q_NEXT_TYPE(fotg210
, q
.itd
->hw_next
);
4631 modified
= itd_complete(fotg210
, q
.itd
);
4635 fotg210_dbg(fotg210
, "corrupt type %d frame %d shadow %p\n",
4636 type
, frame
, q
.ptr
);
4640 /* End of the iTDs and siTDs */
4645 /* assume completion callbacks modify the queue */
4646 if (unlikely(modified
&& fotg210
->isoc_count
> 0))
4652 static void scan_isoc(struct fotg210_hcd
*fotg210
)
4654 unsigned uf
, now_frame
, frame
, ret
;
4655 unsigned fmask
= fotg210
->periodic_size
- 1;
4659 * When running, scan from last scan point up to "now"
4660 * else clean up by scanning everything that's left.
4661 * Touches as few pages as possible: cache-friendly.
4663 if (fotg210
->rh_state
>= FOTG210_RH_RUNNING
) {
4664 uf
= fotg210_read_frame_index(fotg210
);
4665 now_frame
= (uf
>> 3) & fmask
;
4668 now_frame
= (fotg210
->next_frame
- 1) & fmask
;
4671 fotg210
->now_frame
= now_frame
;
4673 frame
= fotg210
->next_frame
;
4677 ret
= scan_frame_queue(fotg210
, frame
,
4680 /* Stop when we have reached the current frame */
4681 if (frame
== now_frame
)
4683 frame
= (frame
+ 1) & fmask
;
4685 fotg210
->next_frame
= now_frame
;
4688 /* Display / Set uframe_periodic_max
4690 static ssize_t
uframe_periodic_max_show(struct device
*dev
,
4691 struct device_attribute
*attr
, char *buf
)
4693 struct fotg210_hcd
*fotg210
;
4696 fotg210
= hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev
)));
4697 n
= scnprintf(buf
, PAGE_SIZE
, "%d\n", fotg210
->uframe_periodic_max
);
4702 static ssize_t
uframe_periodic_max_store(struct device
*dev
,
4703 struct device_attribute
*attr
, const char *buf
, size_t count
)
4705 struct fotg210_hcd
*fotg210
;
4706 unsigned uframe_periodic_max
;
4707 unsigned frame
, uframe
;
4708 unsigned short allocated_max
;
4709 unsigned long flags
;
4712 fotg210
= hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev
)));
4713 if (kstrtouint(buf
, 0, &uframe_periodic_max
) < 0)
4716 if (uframe_periodic_max
< 100 || uframe_periodic_max
>= 125) {
4717 fotg210_info(fotg210
, "rejecting invalid request for uframe_periodic_max=%u\n",
4718 uframe_periodic_max
);
4725 * lock, so that our checking does not race with possible periodic
4726 * bandwidth allocation through submitting new urbs.
4728 spin_lock_irqsave(&fotg210
->lock
, flags
);
4731 * for request to decrease max periodic bandwidth, we have to check
4732 * every microframe in the schedule to see whether the decrease is
4735 if (uframe_periodic_max
< fotg210
->uframe_periodic_max
) {
4738 for (frame
= 0; frame
< fotg210
->periodic_size
; ++frame
)
4739 for (uframe
= 0; uframe
< 7; ++uframe
)
4740 allocated_max
= max(allocated_max
,
4741 periodic_usecs(fotg210
, frame
,
4744 if (allocated_max
> uframe_periodic_max
) {
4745 fotg210_info(fotg210
,
4746 "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n",
4747 allocated_max
, uframe_periodic_max
);
4752 /* increasing is always ok */
4754 fotg210_info(fotg210
,
4755 "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
4756 100 * uframe_periodic_max
/125, uframe_periodic_max
);
4758 if (uframe_periodic_max
!= 100)
4759 fotg210_warn(fotg210
, "max periodic bandwidth set is non-standard\n");
4761 fotg210
->uframe_periodic_max
= uframe_periodic_max
;
4765 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4769 static DEVICE_ATTR_RW(uframe_periodic_max
);
4771 static inline int create_sysfs_files(struct fotg210_hcd
*fotg210
)
4773 struct device
*controller
= fotg210_to_hcd(fotg210
)->self
.controller
;
4775 return device_create_file(controller
, &dev_attr_uframe_periodic_max
);
4778 static inline void remove_sysfs_files(struct fotg210_hcd
*fotg210
)
4780 struct device
*controller
= fotg210_to_hcd(fotg210
)->self
.controller
;
4782 device_remove_file(controller
, &dev_attr_uframe_periodic_max
);
4784 /* On some systems, leaving remote wakeup enabled prevents system shutdown.
4785 * The firmware seems to think that powering off is a wakeup event!
4786 * This routine turns off remote wakeup and everything else, on all ports.
4788 static void fotg210_turn_off_all_ports(struct fotg210_hcd
*fotg210
)
4790 u32 __iomem
*status_reg
= &fotg210
->regs
->port_status
;
4792 fotg210_writel(fotg210
, PORT_RWC_BITS
, status_reg
);
4795 /* Halt HC, turn off all ports, and let the BIOS use the companion controllers.
4796 * Must be called with interrupts enabled and the lock not held.
4798 static void fotg210_silence_controller(struct fotg210_hcd
*fotg210
)
4800 fotg210_halt(fotg210
);
4802 spin_lock_irq(&fotg210
->lock
);
4803 fotg210
->rh_state
= FOTG210_RH_HALTED
;
4804 fotg210_turn_off_all_ports(fotg210
);
4805 spin_unlock_irq(&fotg210
->lock
);
4808 /* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
4809 * This forcibly disables dma and IRQs, helping kexec and other cases
4810 * where the next system software may expect clean state.
4812 static void fotg210_shutdown(struct usb_hcd
*hcd
)
4814 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
4816 spin_lock_irq(&fotg210
->lock
);
4817 fotg210
->shutdown
= true;
4818 fotg210
->rh_state
= FOTG210_RH_STOPPING
;
4819 fotg210
->enabled_hrtimer_events
= 0;
4820 spin_unlock_irq(&fotg210
->lock
);
4822 fotg210_silence_controller(fotg210
);
4824 hrtimer_cancel(&fotg210
->hrtimer
);
4827 /* fotg210_work is called from some interrupts, timers, and so on.
4828 * it calls driver completion functions, after dropping fotg210->lock.
4830 static void fotg210_work(struct fotg210_hcd
*fotg210
)
4832 /* another CPU may drop fotg210->lock during a schedule scan while
4833 * it reports urb completions. this flag guards against bogus
4834 * attempts at re-entrant schedule scanning.
4836 if (fotg210
->scanning
) {
4837 fotg210
->need_rescan
= true;
4840 fotg210
->scanning
= true;
4843 fotg210
->need_rescan
= false;
4844 if (fotg210
->async_count
)
4845 scan_async(fotg210
);
4846 if (fotg210
->intr_count
> 0)
4848 if (fotg210
->isoc_count
> 0)
4850 if (fotg210
->need_rescan
)
4852 fotg210
->scanning
= false;
4854 /* the IO watchdog guards against hardware or driver bugs that
4855 * misplace IRQs, and should let us run completely without IRQs.
4856 * such lossage has been observed on both VT6202 and VT8235.
4858 turn_on_io_watchdog(fotg210
);
4861 /* Called when the fotg210_hcd module is removed.
4863 static void fotg210_stop(struct usb_hcd
*hcd
)
4865 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
4867 fotg210_dbg(fotg210
, "stop\n");
4869 /* no more interrupts ... */
4871 spin_lock_irq(&fotg210
->lock
);
4872 fotg210
->enabled_hrtimer_events
= 0;
4873 spin_unlock_irq(&fotg210
->lock
);
4875 fotg210_quiesce(fotg210
);
4876 fotg210_silence_controller(fotg210
);
4877 fotg210_reset(fotg210
);
4879 hrtimer_cancel(&fotg210
->hrtimer
);
4880 remove_sysfs_files(fotg210
);
4881 remove_debug_files(fotg210
);
4883 /* root hub is shut down separately (first, when possible) */
4884 spin_lock_irq(&fotg210
->lock
);
4885 end_free_itds(fotg210
);
4886 spin_unlock_irq(&fotg210
->lock
);
4887 fotg210_mem_cleanup(fotg210
);
4889 #ifdef FOTG210_STATS
4890 fotg210_dbg(fotg210
, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
4891 fotg210
->stats
.normal
, fotg210
->stats
.error
,
4892 fotg210
->stats
.iaa
, fotg210
->stats
.lost_iaa
);
4893 fotg210_dbg(fotg210
, "complete %ld unlink %ld\n",
4894 fotg210
->stats
.complete
, fotg210
->stats
.unlink
);
4897 dbg_status(fotg210
, "fotg210_stop completed",
4898 fotg210_readl(fotg210
, &fotg210
->regs
->status
));
4901 /* one-time init, only for memory state */
4902 static int hcd_fotg210_init(struct usb_hcd
*hcd
)
4904 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
4908 struct fotg210_qh_hw
*hw
;
4910 spin_lock_init(&fotg210
->lock
);
4913 * keep io watchdog by default, those good HCDs could turn off it later
4915 fotg210
->need_io_watchdog
= 1;
4917 hrtimer_init(&fotg210
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
4918 fotg210
->hrtimer
.function
= fotg210_hrtimer_func
;
4919 fotg210
->next_hrtimer_event
= FOTG210_HRTIMER_NO_EVENT
;
4921 hcc_params
= fotg210_readl(fotg210
, &fotg210
->caps
->hcc_params
);
4924 * by default set standard 80% (== 100 usec/uframe) max periodic
4925 * bandwidth as required by USB 2.0
4927 fotg210
->uframe_periodic_max
= 100;
4930 * hw default: 1K periodic list heads, one per frame.
4931 * periodic_size can shrink by USBCMD update if hcc_params allows.
4933 fotg210
->periodic_size
= DEFAULT_I_TDPS
;
4934 INIT_LIST_HEAD(&fotg210
->intr_qh_list
);
4935 INIT_LIST_HEAD(&fotg210
->cached_itd_list
);
4937 if (HCC_PGM_FRAMELISTLEN(hcc_params
)) {
4938 /* periodic schedule size can be smaller than default */
4939 switch (FOTG210_TUNE_FLS
) {
4941 fotg210
->periodic_size
= 1024;
4944 fotg210
->periodic_size
= 512;
4947 fotg210
->periodic_size
= 256;
4953 retval
= fotg210_mem_init(fotg210
, GFP_KERNEL
);
4957 /* controllers may cache some of the periodic schedule ... */
4958 fotg210
->i_thresh
= 2;
4961 * dedicate a qh for the async ring head, since we couldn't unlink
4962 * a 'real' qh without stopping the async schedule [4.8]. use it
4963 * as the 'reclamation list head' too.
4964 * its dummy is used in hw_alt_next of many tds, to prevent the qh
4965 * from automatically advancing to the next td after short reads.
4967 fotg210
->async
->qh_next
.qh
= NULL
;
4968 hw
= fotg210
->async
->hw
;
4969 hw
->hw_next
= QH_NEXT(fotg210
, fotg210
->async
->qh_dma
);
4970 hw
->hw_info1
= cpu_to_hc32(fotg210
, QH_HEAD
);
4971 hw
->hw_token
= cpu_to_hc32(fotg210
, QTD_STS_HALT
);
4972 hw
->hw_qtd_next
= FOTG210_LIST_END(fotg210
);
4973 fotg210
->async
->qh_state
= QH_STATE_LINKED
;
4974 hw
->hw_alt_next
= QTD_NEXT(fotg210
, fotg210
->async
->dummy
->qtd_dma
);
4976 /* clear interrupt enables, set irq latency */
4977 if (log2_irq_thresh
< 0 || log2_irq_thresh
> 6)
4978 log2_irq_thresh
= 0;
4979 temp
= 1 << (16 + log2_irq_thresh
);
4980 if (HCC_CANPARK(hcc_params
)) {
4981 /* HW default park == 3, on hardware that supports it (like
4982 * NVidia and ALI silicon), maximizes throughput on the async
4983 * schedule by avoiding QH fetches between transfers.
4985 * With fast usb storage devices and NForce2, "park" seems to
4986 * make problems: throughput reduction (!), data errors...
4989 park
= min_t(unsigned, park
, 3);
4993 fotg210_dbg(fotg210
, "park %d\n", park
);
4995 if (HCC_PGM_FRAMELISTLEN(hcc_params
)) {
4996 /* periodic schedule size can be smaller than default */
4998 temp
|= (FOTG210_TUNE_FLS
<< 2);
5000 fotg210
->command
= temp
;
5002 /* Accept arbitrarily long scatter-gather lists */
5003 if (!hcd
->localmem_pool
)
5004 hcd
->self
.sg_tablesize
= ~0;
5008 /* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
5009 static int fotg210_run(struct usb_hcd
*hcd
)
5011 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5015 hcd
->uses_new_polling
= 1;
5017 /* EHCI spec section 4.1 */
5019 fotg210_writel(fotg210
, fotg210
->periodic_dma
,
5020 &fotg210
->regs
->frame_list
);
5021 fotg210_writel(fotg210
, (u32
)fotg210
->async
->qh_dma
,
5022 &fotg210
->regs
->async_next
);
5025 * hcc_params controls whether fotg210->regs->segment must (!!!)
5026 * be used; it constrains QH/ITD/SITD and QTD locations.
5027 * dma_pool consistent memory always uses segment zero.
5028 * streaming mappings for I/O buffers, like pci_map_single(),
5029 * can return segments above 4GB, if the device allows.
5031 * NOTE: the dma mask is visible through dev->dma_mask, so
5032 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
5033 * Scsi_Host.highmem_io, and so forth. It's readonly to all
5034 * host side drivers though.
5036 hcc_params
= fotg210_readl(fotg210
, &fotg210
->caps
->hcc_params
);
5039 * Philips, Intel, and maybe others need CMD_RUN before the
5040 * root hub will detect new devices (why?); NEC doesn't
5042 fotg210
->command
&= ~(CMD_IAAD
|CMD_PSE
|CMD_ASE
|CMD_RESET
);
5043 fotg210
->command
|= CMD_RUN
;
5044 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
5045 dbg_cmd(fotg210
, "init", fotg210
->command
);
5048 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5049 * are explicitly handed to companion controller(s), so no TT is
5050 * involved with the root hub. (Except where one is integrated,
5051 * and there's no companion controller unless maybe for USB OTG.)
5053 * Turning on the CF flag will transfer ownership of all ports
5054 * from the companions to the EHCI controller. If any of the
5055 * companions are in the middle of a port reset at the time, it
5056 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
5057 * guarantees that no resets are in progress. After we set CF,
5058 * a short delay lets the hardware catch up; new resets shouldn't
5059 * be started before the port switching actions could complete.
5061 down_write(&ehci_cf_port_reset_rwsem
);
5062 fotg210
->rh_state
= FOTG210_RH_RUNNING
;
5063 /* unblock posted writes */
5064 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
5065 usleep_range(5000, 10000);
5066 up_write(&ehci_cf_port_reset_rwsem
);
5067 fotg210
->last_periodic_enable
= ktime_get_real();
5069 temp
= HC_VERSION(fotg210
,
5070 fotg210_readl(fotg210
, &fotg210
->caps
->hc_capbase
));
5071 fotg210_info(fotg210
,
5072 "USB %x.%x started, EHCI %x.%02x\n",
5073 ((fotg210
->sbrn
& 0xf0) >> 4), (fotg210
->sbrn
& 0x0f),
5074 temp
>> 8, temp
& 0xff);
5076 fotg210_writel(fotg210
, INTR_MASK
,
5077 &fotg210
->regs
->intr_enable
); /* Turn On Interrupts */
5079 /* GRR this is run-once init(), being done every time the HC starts.
5080 * So long as they're part of class devices, we can't do it init()
5081 * since the class device isn't created that early.
5083 create_debug_files(fotg210
);
5084 create_sysfs_files(fotg210
);
5089 static int fotg210_setup(struct usb_hcd
*hcd
)
5091 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5094 fotg210
->regs
= (void __iomem
*)fotg210
->caps
+
5096 fotg210_readl(fotg210
, &fotg210
->caps
->hc_capbase
));
5097 dbg_hcs_params(fotg210
, "reset");
5098 dbg_hcc_params(fotg210
, "reset");
5100 /* cache this readonly data; minimize chip reads */
5101 fotg210
->hcs_params
= fotg210_readl(fotg210
,
5102 &fotg210
->caps
->hcs_params
);
5104 fotg210
->sbrn
= HCD_USB2
;
5106 /* data structure init */
5107 retval
= hcd_fotg210_init(hcd
);
5111 retval
= fotg210_halt(fotg210
);
5115 fotg210_reset(fotg210
);
5120 static irqreturn_t
fotg210_irq(struct usb_hcd
*hcd
)
5122 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5123 u32 status
, masked_status
, pcd_status
= 0, cmd
;
5126 spin_lock(&fotg210
->lock
);
5128 status
= fotg210_readl(fotg210
, &fotg210
->regs
->status
);
5130 /* e.g. cardbus physical eject */
5131 if (status
== ~(u32
) 0) {
5132 fotg210_dbg(fotg210
, "device removed\n");
5137 * We don't use STS_FLR, but some controllers don't like it to
5138 * remain on, so mask it out along with the other status bits.
5140 masked_status
= status
& (INTR_MASK
| STS_FLR
);
5143 if (!masked_status
||
5144 unlikely(fotg210
->rh_state
== FOTG210_RH_HALTED
)) {
5145 spin_unlock(&fotg210
->lock
);
5149 /* clear (just) interrupts */
5150 fotg210_writel(fotg210
, masked_status
, &fotg210
->regs
->status
);
5151 cmd
= fotg210_readl(fotg210
, &fotg210
->regs
->command
);
5154 /* unrequested/ignored: Frame List Rollover */
5155 dbg_status(fotg210
, "irq", status
);
5157 /* INT, ERR, and IAA interrupt rates can be throttled */
5159 /* normal [4.15.1.2] or error [4.15.1.1] completion */
5160 if (likely((status
& (STS_INT
|STS_ERR
)) != 0)) {
5161 if (likely((status
& STS_ERR
) == 0))
5162 INCR(fotg210
->stats
.normal
);
5164 INCR(fotg210
->stats
.error
);
5168 /* complete the unlinking of some qh [4.15.2.3] */
5169 if (status
& STS_IAA
) {
5171 /* Turn off the IAA watchdog */
5172 fotg210
->enabled_hrtimer_events
&=
5173 ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG
);
5176 * Mild optimization: Allow another IAAD to reset the
5177 * hrtimer, if one occurs before the next expiration.
5178 * In theory we could always cancel the hrtimer, but
5179 * tests show that about half the time it will be reset
5180 * for some other event anyway.
5182 if (fotg210
->next_hrtimer_event
== FOTG210_HRTIMER_IAA_WATCHDOG
)
5183 ++fotg210
->next_hrtimer_event
;
5185 /* guard against (alleged) silicon errata */
5187 fotg210_dbg(fotg210
, "IAA with IAAD still set?\n");
5188 if (fotg210
->async_iaa
) {
5189 INCR(fotg210
->stats
.iaa
);
5190 end_unlink_async(fotg210
);
5192 fotg210_dbg(fotg210
, "IAA with nothing unlinked?\n");
5195 /* remote wakeup [4.3.1] */
5196 if (status
& STS_PCD
) {
5198 u32 __iomem
*status_reg
= &fotg210
->regs
->port_status
;
5200 /* kick root hub later */
5201 pcd_status
= status
;
5203 /* resume root hub? */
5204 if (fotg210
->rh_state
== FOTG210_RH_SUSPENDED
)
5205 usb_hcd_resume_root_hub(hcd
);
5207 pstatus
= fotg210_readl(fotg210
, status_reg
);
5209 if (test_bit(0, &fotg210
->suspended_ports
) &&
5210 ((pstatus
& PORT_RESUME
) ||
5211 !(pstatus
& PORT_SUSPEND
)) &&
5212 (pstatus
& PORT_PE
) &&
5213 fotg210
->reset_done
[0] == 0) {
5215 /* start 20 msec resume signaling from this port,
5216 * and make hub_wq collect PORT_STAT_C_SUSPEND to
5217 * stop that signaling. Use 5 ms extra for safety,
5218 * like usb_port_resume() does.
5220 fotg210
->reset_done
[0] = jiffies
+ msecs_to_jiffies(25);
5221 set_bit(0, &fotg210
->resuming_ports
);
5222 fotg210_dbg(fotg210
, "port 1 remote wakeup\n");
5223 mod_timer(&hcd
->rh_timer
, fotg210
->reset_done
[0]);
5227 /* PCI errors [4.15.2.4] */
5228 if (unlikely((status
& STS_FATAL
) != 0)) {
5229 fotg210_err(fotg210
, "fatal error\n");
5230 dbg_cmd(fotg210
, "fatal", cmd
);
5231 dbg_status(fotg210
, "fatal", status
);
5235 /* Don't let the controller do anything more */
5236 fotg210
->shutdown
= true;
5237 fotg210
->rh_state
= FOTG210_RH_STOPPING
;
5238 fotg210
->command
&= ~(CMD_RUN
| CMD_ASE
| CMD_PSE
);
5239 fotg210_writel(fotg210
, fotg210
->command
,
5240 &fotg210
->regs
->command
);
5241 fotg210_writel(fotg210
, 0, &fotg210
->regs
->intr_enable
);
5242 fotg210_handle_controller_death(fotg210
);
5244 /* Handle completions when the controller stops */
5249 fotg210_work(fotg210
);
5250 spin_unlock(&fotg210
->lock
);
5252 usb_hcd_poll_rh_status(hcd
);
5256 /* non-error returns are a promise to giveback() the urb later
5257 * we drop ownership so next owner (or urb unlink) can get it
5259 * urb + dev is in hcd.self.controller.urb_list
5260 * we're queueing TDs onto software and hardware lists
5262 * hcd-specific init for hcpriv hasn't been done yet
5264 * NOTE: control, bulk, and interrupt share the same code to append TDs
5265 * to a (possibly active) QH, and the same QH scanning code.
5267 static int fotg210_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
5270 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5271 struct list_head qtd_list
;
5273 INIT_LIST_HEAD(&qtd_list
);
5275 switch (usb_pipetype(urb
->pipe
)) {
5277 /* qh_completions() code doesn't handle all the fault cases
5278 * in multi-TD control transfers. Even 1KB is rare anyway.
5280 if (urb
->transfer_buffer_length
> (16 * 1024))
5283 /* case PIPE_BULK: */
5285 if (!qh_urb_transaction(fotg210
, urb
, &qtd_list
, mem_flags
))
5287 return submit_async(fotg210
, urb
, &qtd_list
, mem_flags
);
5289 case PIPE_INTERRUPT
:
5290 if (!qh_urb_transaction(fotg210
, urb
, &qtd_list
, mem_flags
))
5292 return intr_submit(fotg210
, urb
, &qtd_list
, mem_flags
);
5294 case PIPE_ISOCHRONOUS
:
5295 return itd_submit(fotg210
, urb
, mem_flags
);
5299 /* remove from hardware lists
5300 * completions normally happen asynchronously
5303 static int fotg210_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
5305 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5306 struct fotg210_qh
*qh
;
5307 unsigned long flags
;
5310 spin_lock_irqsave(&fotg210
->lock
, flags
);
5311 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
5315 switch (usb_pipetype(urb
->pipe
)) {
5316 /* case PIPE_CONTROL: */
5317 /* case PIPE_BULK:*/
5319 qh
= (struct fotg210_qh
*) urb
->hcpriv
;
5322 switch (qh
->qh_state
) {
5323 case QH_STATE_LINKED
:
5324 case QH_STATE_COMPLETING
:
5325 start_unlink_async(fotg210
, qh
);
5327 case QH_STATE_UNLINK
:
5328 case QH_STATE_UNLINK_WAIT
:
5329 /* already started */
5332 /* QH might be waiting for a Clear-TT-Buffer */
5333 qh_completions(fotg210
, qh
);
5338 case PIPE_INTERRUPT
:
5339 qh
= (struct fotg210_qh
*) urb
->hcpriv
;
5342 switch (qh
->qh_state
) {
5343 case QH_STATE_LINKED
:
5344 case QH_STATE_COMPLETING
:
5345 start_unlink_intr(fotg210
, qh
);
5348 qh_completions(fotg210
, qh
);
5351 fotg210_dbg(fotg210
, "bogus qh %p state %d\n",
5357 case PIPE_ISOCHRONOUS
:
5360 /* wait till next completion, do it then. */
5361 /* completion irqs can wait up to 1024 msec, */
5365 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5369 /* bulk qh holds the data toggle */
5371 static void fotg210_endpoint_disable(struct usb_hcd
*hcd
,
5372 struct usb_host_endpoint
*ep
)
5374 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5375 unsigned long flags
;
5376 struct fotg210_qh
*qh
, *tmp
;
5378 /* ASSERT: any requests/urbs are being unlinked */
5379 /* ASSERT: nobody can be submitting urbs for this any more */
5382 spin_lock_irqsave(&fotg210
->lock
, flags
);
5387 /* endpoints can be iso streams. for now, we don't
5388 * accelerate iso completions ... so spin a while.
5390 if (qh
->hw
== NULL
) {
5391 struct fotg210_iso_stream
*stream
= ep
->hcpriv
;
5393 if (!list_empty(&stream
->td_list
))
5396 /* BUG_ON(!list_empty(&stream->free_list)); */
5401 if (fotg210
->rh_state
< FOTG210_RH_RUNNING
)
5402 qh
->qh_state
= QH_STATE_IDLE
;
5403 switch (qh
->qh_state
) {
5404 case QH_STATE_LINKED
:
5405 case QH_STATE_COMPLETING
:
5406 for (tmp
= fotg210
->async
->qh_next
.qh
;
5408 tmp
= tmp
->qh_next
.qh
)
5410 /* periodic qh self-unlinks on empty, and a COMPLETING qh
5411 * may already be unlinked.
5414 start_unlink_async(fotg210
, qh
);
5416 case QH_STATE_UNLINK
: /* wait for hw to finish? */
5417 case QH_STATE_UNLINK_WAIT
:
5419 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5420 schedule_timeout_uninterruptible(1);
5422 case QH_STATE_IDLE
: /* fully unlinked */
5423 if (qh
->clearing_tt
)
5425 if (list_empty(&qh
->qtd_list
)) {
5426 qh_destroy(fotg210
, qh
);
5431 /* caller was supposed to have unlinked any requests;
5432 * that's not our job. just leak this memory.
5434 fotg210_err(fotg210
, "qh %p (#%02x) state %d%s\n",
5435 qh
, ep
->desc
.bEndpointAddress
, qh
->qh_state
,
5436 list_empty(&qh
->qtd_list
) ? "" : "(has tds)");
5441 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5444 static void fotg210_endpoint_reset(struct usb_hcd
*hcd
,
5445 struct usb_host_endpoint
*ep
)
5447 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5448 struct fotg210_qh
*qh
;
5449 int eptype
= usb_endpoint_type(&ep
->desc
);
5450 int epnum
= usb_endpoint_num(&ep
->desc
);
5451 int is_out
= usb_endpoint_dir_out(&ep
->desc
);
5452 unsigned long flags
;
5454 if (eptype
!= USB_ENDPOINT_XFER_BULK
&& eptype
!= USB_ENDPOINT_XFER_INT
)
5457 spin_lock_irqsave(&fotg210
->lock
, flags
);
5460 /* For Bulk and Interrupt endpoints we maintain the toggle state
5461 * in the hardware; the toggle bits in udev aren't used at all.
5462 * When an endpoint is reset by usb_clear_halt() we must reset
5463 * the toggle bit in the QH.
5466 usb_settoggle(qh
->dev
, epnum
, is_out
, 0);
5467 if (!list_empty(&qh
->qtd_list
)) {
5468 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
5469 } else if (qh
->qh_state
== QH_STATE_LINKED
||
5470 qh
->qh_state
== QH_STATE_COMPLETING
) {
5472 /* The toggle value in the QH can't be updated
5473 * while the QH is active. Unlink it now;
5474 * re-linking will call qh_refresh().
5476 if (eptype
== USB_ENDPOINT_XFER_BULK
)
5477 start_unlink_async(fotg210
, qh
);
5479 start_unlink_intr(fotg210
, qh
);
5482 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5485 static int fotg210_get_frame(struct usb_hcd
*hcd
)
5487 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5489 return (fotg210_read_frame_index(fotg210
) >> 3) %
5490 fotg210
->periodic_size
;
5493 /* The EHCI in ChipIdea HDRC cannot be a separate module or device,
5494 * because its registers (and irq) are shared between host/gadget/otg
5495 * functions and in order to facilitate role switching we cannot
5496 * give the fotg210 driver exclusive access to those.
5498 MODULE_DESCRIPTION(DRIVER_DESC
);
5499 MODULE_AUTHOR(DRIVER_AUTHOR
);
5500 MODULE_LICENSE("GPL");
5502 static const struct hc_driver fotg210_fotg210_hc_driver
= {
5503 .description
= hcd_name
,
5504 .product_desc
= "Faraday USB2.0 Host Controller",
5505 .hcd_priv_size
= sizeof(struct fotg210_hcd
),
5508 * generic hardware linkage
5511 .flags
= HCD_MEMORY
| HCD_DMA
| HCD_USB2
,
5514 * basic lifecycle operations
5516 .reset
= hcd_fotg210_init
,
5517 .start
= fotg210_run
,
5518 .stop
= fotg210_stop
,
5519 .shutdown
= fotg210_shutdown
,
5522 * managing i/o requests and associated device resources
5524 .urb_enqueue
= fotg210_urb_enqueue
,
5525 .urb_dequeue
= fotg210_urb_dequeue
,
5526 .endpoint_disable
= fotg210_endpoint_disable
,
5527 .endpoint_reset
= fotg210_endpoint_reset
,
5530 * scheduling support
5532 .get_frame_number
= fotg210_get_frame
,
5537 .hub_status_data
= fotg210_hub_status_data
,
5538 .hub_control
= fotg210_hub_control
,
5539 .bus_suspend
= fotg210_bus_suspend
,
5540 .bus_resume
= fotg210_bus_resume
,
5542 .relinquish_port
= fotg210_relinquish_port
,
5543 .port_handed_over
= fotg210_port_handed_over
,
5545 .clear_tt_buffer_complete
= fotg210_clear_tt_buffer_complete
,
5548 static void fotg210_init(struct fotg210_hcd
*fotg210
)
5552 iowrite32(GMIR_MDEV_INT
| GMIR_MOTG_INT
| GMIR_INT_POLARITY
,
5553 &fotg210
->regs
->gmir
);
5555 value
= ioread32(&fotg210
->regs
->otgcsr
);
5556 value
&= ~OTGCSR_A_BUS_DROP
;
5557 value
|= OTGCSR_A_BUS_REQ
;
5558 iowrite32(value
, &fotg210
->regs
->otgcsr
);
5562 * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
5564 * Allocates basic resources for this USB host controller, and
5565 * then invokes the start() method for the HCD associated with it
5566 * through the hotplug entry's driver_data.
5568 static int fotg210_hcd_probe(struct platform_device
*pdev
)
5570 struct device
*dev
= &pdev
->dev
;
5571 struct usb_hcd
*hcd
;
5572 struct resource
*res
;
5574 int retval
= -ENODEV
;
5575 struct fotg210_hcd
*fotg210
;
5580 pdev
->dev
.power
.power_state
= PMSG_ON
;
5582 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
5584 dev_err(dev
, "Found HC with no IRQ. Check %s setup!\n",
5591 hcd
= usb_create_hcd(&fotg210_fotg210_hc_driver
, dev
,
5594 dev_err(dev
, "failed to create hcd with err %d\n", retval
);
5596 goto fail_create_hcd
;
5601 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
5602 hcd
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
5603 if (IS_ERR(hcd
->regs
)) {
5604 retval
= PTR_ERR(hcd
->regs
);
5605 goto failed_put_hcd
;
5608 hcd
->rsrc_start
= res
->start
;
5609 hcd
->rsrc_len
= resource_size(res
);
5611 fotg210
= hcd_to_fotg210(hcd
);
5613 fotg210
->caps
= hcd
->regs
;
5615 /* It's OK not to supply this clock */
5616 fotg210
->pclk
= clk_get(dev
, "PCLK");
5617 if (!IS_ERR(fotg210
->pclk
)) {
5618 retval
= clk_prepare_enable(fotg210
->pclk
);
5620 dev_err(dev
, "failed to enable PCLK\n");
5621 goto failed_put_hcd
;
5623 } else if (PTR_ERR(fotg210
->pclk
) == -EPROBE_DEFER
) {
5625 * Percolate deferrals, for anything else,
5626 * just live without the clocking.
5628 retval
= PTR_ERR(fotg210
->pclk
);
5629 goto failed_dis_clk
;
5632 retval
= fotg210_setup(hcd
);
5634 goto failed_dis_clk
;
5636 fotg210_init(fotg210
);
5638 retval
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
5640 dev_err(dev
, "failed to add hcd with err %d\n", retval
);
5641 goto failed_dis_clk
;
5643 device_wakeup_enable(hcd
->self
.controller
);
5644 platform_set_drvdata(pdev
, hcd
);
5649 if (!IS_ERR(fotg210
->pclk
))
5650 clk_disable_unprepare(fotg210
->pclk
);
5654 dev_err(dev
, "init %s fail, %d\n", dev_name(dev
), retval
);
5659 * fotg210_hcd_remove - shutdown processing for EHCI HCDs
5660 * @dev: USB Host Controller being removed
5663 static int fotg210_hcd_remove(struct platform_device
*pdev
)
5665 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
5666 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5668 if (!IS_ERR(fotg210
->pclk
))
5669 clk_disable_unprepare(fotg210
->pclk
);
5671 usb_remove_hcd(hcd
);
5678 static const struct of_device_id fotg210_of_match
[] = {
5679 { .compatible
= "faraday,fotg210" },
5682 MODULE_DEVICE_TABLE(of
, fotg210_of_match
);
5685 static struct platform_driver fotg210_hcd_driver
= {
5687 .name
= "fotg210-hcd",
5688 .of_match_table
= of_match_ptr(fotg210_of_match
),
5690 .probe
= fotg210_hcd_probe
,
5691 .remove
= fotg210_hcd_remove
,
5694 static int __init
fotg210_hcd_init(void)
5701 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
5702 set_bit(USB_EHCI_LOADED
, &usb_hcds_loaded
);
5703 if (test_bit(USB_UHCI_LOADED
, &usb_hcds_loaded
) ||
5704 test_bit(USB_OHCI_LOADED
, &usb_hcds_loaded
))
5705 pr_warn("Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
5707 pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd\n",
5708 hcd_name
, sizeof(struct fotg210_qh
),
5709 sizeof(struct fotg210_qtd
),
5710 sizeof(struct fotg210_itd
));
5712 fotg210_debug_root
= debugfs_create_dir("fotg210", usb_debug_root
);
5714 retval
= platform_driver_register(&fotg210_hcd_driver
);
5720 debugfs_remove(fotg210_debug_root
);
5721 fotg210_debug_root
= NULL
;
5723 clear_bit(USB_EHCI_LOADED
, &usb_hcds_loaded
);
5726 module_init(fotg210_hcd_init
);
5728 static void __exit
fotg210_hcd_cleanup(void)
5730 platform_driver_unregister(&fotg210_hcd_driver
);
5731 debugfs_remove(fotg210_debug_root
);
5732 clear_bit(USB_EHCI_LOADED
, &usb_hcds_loaded
);
5734 module_exit(fotg210_hcd_cleanup
);