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 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/dmapool.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/sched.h>
33 #include <linux/vmalloc.h>
34 #include <linux/errno.h>
35 #include <linux/init.h>
36 #include <linux/hrtimer.h>
37 #include <linux/list.h>
38 #include <linux/interrupt.h>
39 #include <linux/usb.h>
40 #include <linux/usb/hcd.h>
41 #include <linux/moduleparam.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/debugfs.h>
44 #include <linux/slab.h>
45 #include <linux/uaccess.h>
46 #include <linux/platform_device.h>
49 #include <asm/byteorder.h>
51 #include <asm/unaligned.h>
53 /*-------------------------------------------------------------------------*/
54 #define DRIVER_AUTHOR "Yuan-Hsin Chen"
55 #define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
57 static const char hcd_name
[] = "fotg210_hcd";
60 #undef FOTG210_URB_TRACE
66 /* magic numbers that can affect system performance */
67 #define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
68 #define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
69 #define FOTG210_TUNE_RL_TT 0
70 #define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
71 #define FOTG210_TUNE_MULT_TT 1
73 * Some drivers think it's safe to schedule isochronous transfers more than
74 * 256 ms into the future (partly as a result of an old bug in the scheduling
75 * code). In an attempt to avoid trouble, we will use a minimum scheduling
76 * length of 512 frames instead of 256.
78 #define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
80 /* Initial IRQ latency: faster than hw default */
81 static int log2_irq_thresh
; /* 0 to 6 */
82 module_param(log2_irq_thresh
, int, S_IRUGO
);
83 MODULE_PARM_DESC(log2_irq_thresh
, "log2 IRQ latency, 1-64 microframes");
85 /* initial park setting: slower than hw default */
87 module_param(park
, uint
, S_IRUGO
);
88 MODULE_PARM_DESC(park
, "park setting; 1-3 back-to-back async packets");
90 /* for link power management(LPM) feature */
91 static unsigned int hird
;
92 module_param(hird
, int, S_IRUGO
);
93 MODULE_PARM_DESC(hird
, "host initiated resume duration, +1 for each 75us");
95 #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
99 /*-------------------------------------------------------------------------*/
101 #define fotg210_dbg(fotg210, fmt, args...) \
102 dev_dbg(fotg210_to_hcd(fotg210)->self.controller , fmt , ## args)
103 #define fotg210_err(fotg210, fmt, args...) \
104 dev_err(fotg210_to_hcd(fotg210)->self.controller , fmt , ## args)
105 #define fotg210_info(fotg210, fmt, args...) \
106 dev_info(fotg210_to_hcd(fotg210)->self.controller , fmt , ## args)
107 #define fotg210_warn(fotg210, fmt, args...) \
108 dev_warn(fotg210_to_hcd(fotg210)->self.controller , fmt , ## args)
111 # define fotg210_vdbg fotg210_dbg
113 static inline void fotg210_vdbg(struct fotg210_hcd
*fotg210
, ...) {}
118 /* check the values in the HCSPARAMS register
119 * (host controller _Structural_ parameters)
120 * see EHCI spec, Table 2-4 for each value
122 static void dbg_hcs_params(struct fotg210_hcd
*fotg210
, char *label
)
124 u32 params
= fotg210_readl(fotg210
, &fotg210
->caps
->hcs_params
);
127 "%s hcs_params 0x%x ports=%d\n",
134 static inline void dbg_hcs_params(struct fotg210_hcd
*fotg210
, char *label
) {}
140 /* check the values in the HCCPARAMS register
141 * (host controller _Capability_ parameters)
142 * see EHCI Spec, Table 2-5 for each value
144 static void dbg_hcc_params(struct fotg210_hcd
*fotg210
, char *label
)
146 u32 params
= fotg210_readl(fotg210
, &fotg210
->caps
->hcc_params
);
149 "%s hcc_params %04x uframes %s%s\n",
152 HCC_PGM_FRAMELISTLEN(params
) ? "256/512/1024" : "1024",
153 HCC_CANPARK(params
) ? " park" : "");
157 static inline void dbg_hcc_params(struct fotg210_hcd
*fotg210
, char *label
) {}
163 static void __maybe_unused
164 dbg_qtd(const char *label
, struct fotg210_hcd
*fotg210
, struct fotg210_qtd
*qtd
)
166 fotg210_dbg(fotg210
, "%s td %p n%08x %08x t%08x p0=%08x\n", label
, qtd
,
167 hc32_to_cpup(fotg210
, &qtd
->hw_next
),
168 hc32_to_cpup(fotg210
, &qtd
->hw_alt_next
),
169 hc32_to_cpup(fotg210
, &qtd
->hw_token
),
170 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[0]));
172 fotg210_dbg(fotg210
, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
173 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[1]),
174 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[2]),
175 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[3]),
176 hc32_to_cpup(fotg210
, &qtd
->hw_buf
[4]));
179 static void __maybe_unused
180 dbg_qh(const char *label
, struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
182 struct fotg210_qh_hw
*hw
= qh
->hw
;
184 fotg210_dbg(fotg210
, "%s qh %p n%08x info %x %x qtd %x\n", label
,
185 qh
, hw
->hw_next
, hw
->hw_info1
, hw
->hw_info2
, hw
->hw_current
);
186 dbg_qtd("overlay", fotg210
, (struct fotg210_qtd
*) &hw
->hw_qtd_next
);
189 static void __maybe_unused
190 dbg_itd(const char *label
, struct fotg210_hcd
*fotg210
, struct fotg210_itd
*itd
)
192 fotg210_dbg(fotg210
, "%s[%d] itd %p, next %08x, urb %p\n",
193 label
, itd
->frame
, itd
, hc32_to_cpu(fotg210
, itd
->hw_next
),
196 " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
197 hc32_to_cpu(fotg210
, itd
->hw_transaction
[0]),
198 hc32_to_cpu(fotg210
, itd
->hw_transaction
[1]),
199 hc32_to_cpu(fotg210
, itd
->hw_transaction
[2]),
200 hc32_to_cpu(fotg210
, itd
->hw_transaction
[3]),
201 hc32_to_cpu(fotg210
, itd
->hw_transaction
[4]),
202 hc32_to_cpu(fotg210
, itd
->hw_transaction
[5]),
203 hc32_to_cpu(fotg210
, itd
->hw_transaction
[6]),
204 hc32_to_cpu(fotg210
, itd
->hw_transaction
[7]));
206 " buf: %08x %08x %08x %08x %08x %08x %08x\n",
207 hc32_to_cpu(fotg210
, itd
->hw_bufp
[0]),
208 hc32_to_cpu(fotg210
, itd
->hw_bufp
[1]),
209 hc32_to_cpu(fotg210
, itd
->hw_bufp
[2]),
210 hc32_to_cpu(fotg210
, itd
->hw_bufp
[3]),
211 hc32_to_cpu(fotg210
, itd
->hw_bufp
[4]),
212 hc32_to_cpu(fotg210
, itd
->hw_bufp
[5]),
213 hc32_to_cpu(fotg210
, itd
->hw_bufp
[6]));
214 fotg210_dbg(fotg210
, " index: %d %d %d %d %d %d %d %d\n",
215 itd
->index
[0], itd
->index
[1], itd
->index
[2],
216 itd
->index
[3], itd
->index
[4], itd
->index
[5],
217 itd
->index
[6], itd
->index
[7]);
220 static int __maybe_unused
221 dbg_status_buf(char *buf
, unsigned len
, const char *label
, u32 status
)
223 return scnprintf(buf
, len
,
224 "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
225 label
, label
[0] ? " " : "", status
,
226 (status
& STS_ASS
) ? " Async" : "",
227 (status
& STS_PSS
) ? " Periodic" : "",
228 (status
& STS_RECL
) ? " Recl" : "",
229 (status
& STS_HALT
) ? " Halt" : "",
230 (status
& STS_IAA
) ? " IAA" : "",
231 (status
& STS_FATAL
) ? " FATAL" : "",
232 (status
& STS_FLR
) ? " FLR" : "",
233 (status
& STS_PCD
) ? " PCD" : "",
234 (status
& STS_ERR
) ? " ERR" : "",
235 (status
& STS_INT
) ? " INT" : ""
239 static int __maybe_unused
240 dbg_intr_buf(char *buf
, unsigned len
, const char *label
, u32 enable
)
242 return scnprintf(buf
, len
,
243 "%s%sintrenable %02x%s%s%s%s%s%s",
244 label
, label
[0] ? " " : "", enable
,
245 (enable
& STS_IAA
) ? " IAA" : "",
246 (enable
& STS_FATAL
) ? " FATAL" : "",
247 (enable
& STS_FLR
) ? " FLR" : "",
248 (enable
& STS_PCD
) ? " PCD" : "",
249 (enable
& STS_ERR
) ? " ERR" : "",
250 (enable
& STS_INT
) ? " INT" : ""
254 static const char *const fls_strings
[] = { "1024", "512", "256", "??" };
257 dbg_command_buf(char *buf
, unsigned len
, const char *label
, u32 command
)
259 return scnprintf(buf
, len
,
260 "%s%scommand %07x %s=%d ithresh=%d%s%s%s "
262 label
, label
[0] ? " " : "", command
,
263 (command
& CMD_PARK
) ? " park" : "(park)",
264 CMD_PARK_CNT(command
),
265 (command
>> 16) & 0x3f,
266 (command
& CMD_IAAD
) ? " IAAD" : "",
267 (command
& CMD_ASE
) ? " Async" : "",
268 (command
& CMD_PSE
) ? " Periodic" : "",
269 fls_strings
[(command
>> 2) & 0x3],
270 (command
& CMD_RESET
) ? " Reset" : "",
271 (command
& CMD_RUN
) ? "RUN" : "HALT"
276 dbg_port_buf(char *buf
, unsigned len
, const char *label
, int port
, u32 status
)
280 /* signaling state */
281 switch (status
& (3 << 10)) {
287 break; /* low speed */
296 return scnprintf(buf
, len
,
297 "%s%sport:%d status %06x %d "
298 "sig=%s%s%s%s%s%s%s%s",
299 label
, label
[0] ? " " : "", port
, status
,
300 status
>>25,/*device address */
302 (status
& PORT_RESET
) ? " RESET" : "",
303 (status
& PORT_SUSPEND
) ? " SUSPEND" : "",
304 (status
& PORT_RESUME
) ? " RESUME" : "",
305 (status
& PORT_PEC
) ? " PEC" : "",
306 (status
& PORT_PE
) ? " PE" : "",
307 (status
& PORT_CSC
) ? " CSC" : "",
308 (status
& PORT_CONNECT
) ? " CONNECT" : "");
312 static inline void __maybe_unused
313 dbg_qh(char *label
, struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
316 static inline int __maybe_unused
317 dbg_status_buf(char *buf
, unsigned len
, const char *label
, u32 status
)
320 static inline int __maybe_unused
321 dbg_command_buf(char *buf
, unsigned len
, const char *label
, u32 command
)
324 static inline int __maybe_unused
325 dbg_intr_buf(char *buf
, unsigned len
, const char *label
, u32 enable
)
328 static inline int __maybe_unused
329 dbg_port_buf(char *buf
, unsigned len
, const char *label
, int port
, u32 status
)
334 /* functions have the "wrong" filename when they're output... */
335 #define dbg_status(fotg210, label, status) { \
337 dbg_status_buf(_buf, sizeof(_buf), label, status); \
338 fotg210_dbg(fotg210, "%s\n", _buf); \
341 #define dbg_cmd(fotg210, label, command) { \
343 dbg_command_buf(_buf, sizeof(_buf), label, command); \
344 fotg210_dbg(fotg210, "%s\n", _buf); \
347 #define dbg_port(fotg210, label, port, status) { \
349 dbg_port_buf(_buf, sizeof(_buf), label, port, status); \
350 fotg210_dbg(fotg210, "%s\n", _buf); \
353 /*-------------------------------------------------------------------------*/
355 #ifdef STUB_DEBUG_FILES
357 static inline void create_debug_files(struct fotg210_hcd
*bus
) { }
358 static inline void remove_debug_files(struct fotg210_hcd
*bus
) { }
362 /* troubleshooting help: expose state in debugfs */
364 static int debug_async_open(struct inode
*, struct file
*);
365 static int debug_periodic_open(struct inode
*, struct file
*);
366 static int debug_registers_open(struct inode
*, struct file
*);
367 static int debug_async_open(struct inode
*, struct file
*);
369 static ssize_t
debug_output(struct file
*, char __user
*, size_t, loff_t
*);
370 static int debug_close(struct inode
*, struct file
*);
372 static const struct file_operations debug_async_fops
= {
373 .owner
= THIS_MODULE
,
374 .open
= debug_async_open
,
375 .read
= debug_output
,
376 .release
= debug_close
,
377 .llseek
= default_llseek
,
379 static const struct file_operations debug_periodic_fops
= {
380 .owner
= THIS_MODULE
,
381 .open
= debug_periodic_open
,
382 .read
= debug_output
,
383 .release
= debug_close
,
384 .llseek
= default_llseek
,
386 static const struct file_operations debug_registers_fops
= {
387 .owner
= THIS_MODULE
,
388 .open
= debug_registers_open
,
389 .read
= debug_output
,
390 .release
= debug_close
,
391 .llseek
= default_llseek
,
394 static struct dentry
*fotg210_debug_root
;
396 struct debug_buffer
{
397 ssize_t (*fill_func
)(struct debug_buffer
*); /* fill method */
399 struct mutex mutex
; /* protect filling of buffer */
400 size_t count
; /* number of characters filled into buffer */
405 #define speed_char(info1)({ char tmp; \
406 switch (info1 & (3 << 12)) { \
407 case QH_FULL_SPEED: \
411 case QH_HIGH_SPEED: \
417 static inline char token_mark(struct fotg210_hcd
*fotg210
, __hc32 token
)
419 __u32 v
= hc32_to_cpu(fotg210
, token
);
421 if (v
& QTD_STS_ACTIVE
)
423 if (v
& QTD_STS_HALT
)
425 if (!IS_SHORT_READ(v
))
427 /* tries to advance through hw_alt_next */
431 static void qh_lines(
432 struct fotg210_hcd
*fotg210
,
433 struct fotg210_qh
*qh
,
440 struct fotg210_qtd
*td
;
442 unsigned size
= *sizep
;
445 __le32 list_end
= FOTG210_LIST_END(fotg210
);
446 struct fotg210_qh_hw
*hw
= qh
->hw
;
448 if (hw
->hw_qtd_next
== list_end
) /* NEC does this */
451 mark
= token_mark(fotg210
, hw
->hw_token
);
452 if (mark
== '/') { /* qh_alt_next controls qh advance? */
453 if ((hw
->hw_alt_next
& QTD_MASK(fotg210
))
454 == fotg210
->async
->hw
->hw_alt_next
)
455 mark
= '#'; /* blocked */
456 else if (hw
->hw_alt_next
== list_end
)
457 mark
= '.'; /* use hw_qtd_next */
458 /* else alt_next points to some other qtd */
460 scratch
= hc32_to_cpup(fotg210
, &hw
->hw_info1
);
461 hw_curr
= (mark
== '*') ? hc32_to_cpup(fotg210
, &hw
->hw_current
) : 0;
462 temp
= scnprintf(next
, size
,
463 "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
464 qh
, scratch
& 0x007f,
466 (scratch
>> 8) & 0x000f,
467 scratch
, hc32_to_cpup(fotg210
, &hw
->hw_info2
),
468 hc32_to_cpup(fotg210
, &hw
->hw_token
), mark
,
469 (cpu_to_hc32(fotg210
, QTD_TOGGLE
) & hw
->hw_token
)
471 (hc32_to_cpup(fotg210
, &hw
->hw_alt_next
) >> 1) & 0x0f);
475 /* hc may be modifying the list as we read it ... */
476 list_for_each_entry(td
, &qh
->qtd_list
, qtd_list
) {
477 scratch
= hc32_to_cpup(fotg210
, &td
->hw_token
);
479 if (hw_curr
== td
->qtd_dma
)
481 else if (hw
->hw_qtd_next
== cpu_to_hc32(fotg210
, td
->qtd_dma
))
483 else if (QTD_LENGTH(scratch
)) {
484 if (td
->hw_alt_next
== fotg210
->async
->hw
->hw_alt_next
)
486 else if (td
->hw_alt_next
!= list_end
)
489 temp
= snprintf(next
, size
,
490 "\n\t%p%c%s len=%d %08x urb %p",
491 td
, mark
, ({ char *tmp
;
492 switch ((scratch
>>8)&0x03) {
506 (scratch
>> 16) & 0x7fff,
517 temp
= snprintf(next
, size
, "\n");
528 static ssize_t
fill_async_buffer(struct debug_buffer
*buf
)
531 struct fotg210_hcd
*fotg210
;
535 struct fotg210_qh
*qh
;
537 hcd
= bus_to_hcd(buf
->bus
);
538 fotg210
= hcd_to_fotg210(hcd
);
539 next
= buf
->output_buf
;
540 size
= buf
->alloc_size
;
544 /* dumps a snapshot of the async schedule.
545 * usually empty except for long-term bulk reads, or head.
546 * one QH per line, and TDs we know about
548 spin_lock_irqsave(&fotg210
->lock
, flags
);
549 for (qh
= fotg210
->async
->qh_next
.qh
; size
> 0 && qh
;
551 qh_lines(fotg210
, qh
, &next
, &size
);
552 if (fotg210
->async_unlink
&& size
> 0) {
553 temp
= scnprintf(next
, size
, "\nunlink =\n");
557 for (qh
= fotg210
->async_unlink
; size
> 0 && qh
;
558 qh
= qh
->unlink_next
)
559 qh_lines(fotg210
, qh
, &next
, &size
);
561 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
563 return strlen(buf
->output_buf
);
566 #define DBG_SCHED_LIMIT 64
567 static ssize_t
fill_periodic_buffer(struct debug_buffer
*buf
)
570 struct fotg210_hcd
*fotg210
;
572 union fotg210_shadow p
, *seen
;
573 unsigned temp
, size
, seen_count
;
578 seen
= kmalloc(DBG_SCHED_LIMIT
* sizeof(*seen
), GFP_ATOMIC
);
583 hcd
= bus_to_hcd(buf
->bus
);
584 fotg210
= hcd_to_fotg210(hcd
);
585 next
= buf
->output_buf
;
586 size
= buf
->alloc_size
;
588 temp
= scnprintf(next
, size
, "size = %d\n", fotg210
->periodic_size
);
592 /* dump a snapshot of the periodic schedule.
593 * iso changes, interrupt usually doesn't.
595 spin_lock_irqsave(&fotg210
->lock
, flags
);
596 for (i
= 0; i
< fotg210
->periodic_size
; i
++) {
597 p
= fotg210
->pshadow
[i
];
600 tag
= Q_NEXT_TYPE(fotg210
, fotg210
->periodic
[i
]);
602 temp
= scnprintf(next
, size
, "%4d: ", i
);
607 struct fotg210_qh_hw
*hw
;
609 switch (hc32_to_cpu(fotg210
, tag
)) {
612 temp
= scnprintf(next
, size
, " qh%d-%04x/%p",
614 hc32_to_cpup(fotg210
,
617 & (QH_CMASK
| QH_SMASK
),
621 /* don't repeat what follows this qh */
622 for (temp
= 0; temp
< seen_count
; temp
++) {
623 if (seen
[temp
].ptr
!= p
.ptr
)
625 if (p
.qh
->qh_next
.ptr
) {
626 temp
= scnprintf(next
, size
,
633 /* show more info the first time around */
634 if (temp
== seen_count
) {
635 u32 scratch
= hc32_to_cpup(fotg210
,
637 struct fotg210_qtd
*qtd
;
640 /* count tds, get ep direction */
642 list_for_each_entry(qtd
,
646 switch (0x03 & (hc32_to_cpu(
648 qtd
->hw_token
) >> 8)) {
658 temp
= scnprintf(next
, size
,
663 (scratch
>> 8) & 0x000f, type
,
664 p
.qh
->usecs
, p
.qh
->c_usecs
,
666 0x7ff & (scratch
>> 16));
668 if (seen_count
< DBG_SCHED_LIMIT
)
669 seen
[seen_count
++].qh
= p
.qh
;
672 tag
= Q_NEXT_TYPE(fotg210
, hw
->hw_next
);
676 temp
= scnprintf(next
, size
,
677 " fstn-%8x/%p", p
.fstn
->hw_prev
,
679 tag
= Q_NEXT_TYPE(fotg210
, p
.fstn
->hw_next
);
680 p
= p
.fstn
->fstn_next
;
683 temp
= scnprintf(next
, size
,
685 tag
= Q_NEXT_TYPE(fotg210
, p
.itd
->hw_next
);
693 temp
= scnprintf(next
, size
, "\n");
697 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
700 return buf
->alloc_size
- size
;
702 #undef DBG_SCHED_LIMIT
704 static const char *rh_state_string(struct fotg210_hcd
*fotg210
)
706 switch (fotg210
->rh_state
) {
707 case FOTG210_RH_HALTED
:
709 case FOTG210_RH_SUSPENDED
:
711 case FOTG210_RH_RUNNING
:
713 case FOTG210_RH_STOPPING
:
719 static ssize_t
fill_registers_buffer(struct debug_buffer
*buf
)
722 struct fotg210_hcd
*fotg210
;
724 unsigned temp
, size
, i
;
725 char *next
, scratch
[80];
726 static const char fmt
[] = "%*s\n";
727 static const char label
[] = "";
729 hcd
= bus_to_hcd(buf
->bus
);
730 fotg210
= hcd_to_fotg210(hcd
);
731 next
= buf
->output_buf
;
732 size
= buf
->alloc_size
;
734 spin_lock_irqsave(&fotg210
->lock
, flags
);
736 if (!HCD_HW_ACCESSIBLE(hcd
)) {
737 size
= scnprintf(next
, size
,
738 "bus %s, device %s\n"
740 "SUSPENDED(no register access)\n",
741 hcd
->self
.controller
->bus
->name
,
742 dev_name(hcd
->self
.controller
),
747 /* Capability Registers */
748 i
= HC_VERSION(fotg210
, fotg210_readl(fotg210
,
749 &fotg210
->caps
->hc_capbase
));
750 temp
= scnprintf(next
, size
,
751 "bus %s, device %s\n"
753 "EHCI %x.%02x, rh state %s\n",
754 hcd
->self
.controller
->bus
->name
,
755 dev_name(hcd
->self
.controller
),
757 i
>> 8, i
& 0x0ff, rh_state_string(fotg210
));
761 /* FIXME interpret both types of params */
762 i
= fotg210_readl(fotg210
, &fotg210
->caps
->hcs_params
);
763 temp
= scnprintf(next
, size
, "structural params 0x%08x\n", i
);
767 i
= fotg210_readl(fotg210
, &fotg210
->caps
->hcc_params
);
768 temp
= scnprintf(next
, size
, "capability params 0x%08x\n", i
);
772 /* Operational Registers */
773 temp
= dbg_status_buf(scratch
, sizeof(scratch
), label
,
774 fotg210_readl(fotg210
, &fotg210
->regs
->status
));
775 temp
= scnprintf(next
, size
, fmt
, temp
, scratch
);
779 temp
= dbg_command_buf(scratch
, sizeof(scratch
), label
,
780 fotg210_readl(fotg210
, &fotg210
->regs
->command
));
781 temp
= scnprintf(next
, size
, fmt
, temp
, scratch
);
785 temp
= dbg_intr_buf(scratch
, sizeof(scratch
), label
,
786 fotg210_readl(fotg210
, &fotg210
->regs
->intr_enable
));
787 temp
= scnprintf(next
, size
, fmt
, temp
, scratch
);
791 temp
= scnprintf(next
, size
, "uframe %04x\n",
792 fotg210_read_frame_index(fotg210
));
796 if (fotg210
->async_unlink
) {
797 temp
= scnprintf(next
, size
, "async unlink qh %p\n",
798 fotg210
->async_unlink
);
804 temp
= scnprintf(next
, size
,
805 "irq normal %ld err %ld iaa %ld(lost %ld)\n",
806 fotg210
->stats
.normal
, fotg210
->stats
.error
, fotg210
->stats
.iaa
,
807 fotg210
->stats
.lost_iaa
);
811 temp
= scnprintf(next
, size
, "complete %ld unlink %ld\n",
812 fotg210
->stats
.complete
, fotg210
->stats
.unlink
);
818 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
820 return buf
->alloc_size
- size
;
823 static struct debug_buffer
*alloc_buffer(struct usb_bus
*bus
,
824 ssize_t (*fill_func
)(struct debug_buffer
*))
826 struct debug_buffer
*buf
;
828 buf
= kzalloc(sizeof(struct debug_buffer
), GFP_KERNEL
);
832 buf
->fill_func
= fill_func
;
833 mutex_init(&buf
->mutex
);
834 buf
->alloc_size
= PAGE_SIZE
;
840 static int fill_buffer(struct debug_buffer
*buf
)
844 if (!buf
->output_buf
)
845 buf
->output_buf
= vmalloc(buf
->alloc_size
);
847 if (!buf
->output_buf
) {
852 ret
= buf
->fill_func(buf
);
863 static ssize_t
debug_output(struct file
*file
, char __user
*user_buf
,
864 size_t len
, loff_t
*offset
)
866 struct debug_buffer
*buf
= file
->private_data
;
869 mutex_lock(&buf
->mutex
);
870 if (buf
->count
== 0) {
871 ret
= fill_buffer(buf
);
873 mutex_unlock(&buf
->mutex
);
877 mutex_unlock(&buf
->mutex
);
879 ret
= simple_read_from_buffer(user_buf
, len
, offset
,
880 buf
->output_buf
, buf
->count
);
887 static int debug_close(struct inode
*inode
, struct file
*file
)
889 struct debug_buffer
*buf
= file
->private_data
;
892 vfree(buf
->output_buf
);
898 static int debug_async_open(struct inode
*inode
, struct file
*file
)
900 file
->private_data
= alloc_buffer(inode
->i_private
, fill_async_buffer
);
902 return file
->private_data
? 0 : -ENOMEM
;
905 static int debug_periodic_open(struct inode
*inode
, struct file
*file
)
907 struct debug_buffer
*buf
;
908 buf
= alloc_buffer(inode
->i_private
, fill_periodic_buffer
);
912 buf
->alloc_size
= (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE
;
913 file
->private_data
= buf
;
917 static int debug_registers_open(struct inode
*inode
, struct file
*file
)
919 file
->private_data
= alloc_buffer(inode
->i_private
,
920 fill_registers_buffer
);
922 return file
->private_data
? 0 : -ENOMEM
;
925 static inline void create_debug_files(struct fotg210_hcd
*fotg210
)
927 struct usb_bus
*bus
= &fotg210_to_hcd(fotg210
)->self
;
929 fotg210
->debug_dir
= debugfs_create_dir(bus
->bus_name
,
931 if (!fotg210
->debug_dir
)
934 if (!debugfs_create_file("async", S_IRUGO
, fotg210
->debug_dir
, bus
,
938 if (!debugfs_create_file("periodic", S_IRUGO
, fotg210
->debug_dir
, bus
,
939 &debug_periodic_fops
))
942 if (!debugfs_create_file("registers", S_IRUGO
, fotg210
->debug_dir
, bus
,
943 &debug_registers_fops
))
949 debugfs_remove_recursive(fotg210
->debug_dir
);
952 static inline void remove_debug_files(struct fotg210_hcd
*fotg210
)
954 debugfs_remove_recursive(fotg210
->debug_dir
);
957 #endif /* STUB_DEBUG_FILES */
958 /*-------------------------------------------------------------------------*/
961 * handshake - spin reading hc until handshake completes or fails
962 * @ptr: address of hc register to be read
963 * @mask: bits to look at in result of read
964 * @done: value of those bits when handshake succeeds
965 * @usec: timeout in microseconds
967 * Returns negative errno, or zero on success
969 * Success happens when the "mask" bits have the specified value (hardware
970 * handshake done). There are two failure modes: "usec" have passed (major
971 * hardware flakeout), or the register reads as all-ones (hardware removed).
973 * That last failure should_only happen in cases like physical cardbus eject
974 * before driver shutdown. But it also seems to be caused by bugs in cardbus
975 * bridge shutdown: shutting down the bridge before the devices using it.
977 static int handshake(struct fotg210_hcd
*fotg210
, void __iomem
*ptr
,
978 u32 mask
, u32 done
, int usec
)
983 result
= fotg210_readl(fotg210
, ptr
);
984 if (result
== ~(u32
)0) /* card removed */
996 * Force HC to halt state from unknown (EHCI spec section 2.3).
997 * Must be called with interrupts enabled and the lock not held.
999 static int fotg210_halt(struct fotg210_hcd
*fotg210
)
1003 spin_lock_irq(&fotg210
->lock
);
1005 /* disable any irqs left enabled by previous code */
1006 fotg210_writel(fotg210
, 0, &fotg210
->regs
->intr_enable
);
1009 * This routine gets called during probe before fotg210->command
1010 * has been initialized, so we can't rely on its value.
1012 fotg210
->command
&= ~CMD_RUN
;
1013 temp
= fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1014 temp
&= ~(CMD_RUN
| CMD_IAAD
);
1015 fotg210_writel(fotg210
, temp
, &fotg210
->regs
->command
);
1017 spin_unlock_irq(&fotg210
->lock
);
1018 synchronize_irq(fotg210_to_hcd(fotg210
)->irq
);
1020 return handshake(fotg210
, &fotg210
->regs
->status
,
1021 STS_HALT
, STS_HALT
, 16 * 125);
1025 * Reset a non-running (STS_HALT == 1) controller.
1026 * Must be called with interrupts enabled and the lock not held.
1028 static int fotg210_reset(struct fotg210_hcd
*fotg210
)
1031 u32 command
= fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1033 /* If the EHCI debug controller is active, special care must be
1034 * taken before and after a host controller reset */
1035 if (fotg210
->debug
&& !dbgp_reset_prep(fotg210_to_hcd(fotg210
)))
1036 fotg210
->debug
= NULL
;
1038 command
|= CMD_RESET
;
1039 dbg_cmd(fotg210
, "reset", command
);
1040 fotg210_writel(fotg210
, command
, &fotg210
->regs
->command
);
1041 fotg210
->rh_state
= FOTG210_RH_HALTED
;
1042 fotg210
->next_statechange
= jiffies
;
1043 retval
= handshake(fotg210
, &fotg210
->regs
->command
,
1044 CMD_RESET
, 0, 250 * 1000);
1050 dbgp_external_startup(fotg210_to_hcd(fotg210
));
1052 fotg210
->port_c_suspend
= fotg210
->suspended_ports
=
1053 fotg210
->resuming_ports
= 0;
1058 * Idle the controller (turn off the schedules).
1059 * Must be called with interrupts enabled and the lock not held.
1061 static void fotg210_quiesce(struct fotg210_hcd
*fotg210
)
1065 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
)
1068 /* wait for any schedule enables/disables to take effect */
1069 temp
= (fotg210
->command
<< 10) & (STS_ASS
| STS_PSS
);
1070 handshake(fotg210
, &fotg210
->regs
->status
, STS_ASS
| STS_PSS
, temp
,
1073 /* then disable anything that's still active */
1074 spin_lock_irq(&fotg210
->lock
);
1075 fotg210
->command
&= ~(CMD_ASE
| CMD_PSE
);
1076 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
1077 spin_unlock_irq(&fotg210
->lock
);
1079 /* hardware can take 16 microframes to turn off ... */
1080 handshake(fotg210
, &fotg210
->regs
->status
, STS_ASS
| STS_PSS
, 0,
1084 /*-------------------------------------------------------------------------*/
1086 static void end_unlink_async(struct fotg210_hcd
*fotg210
);
1087 static void unlink_empty_async(struct fotg210_hcd
*fotg210
);
1088 static void fotg210_work(struct fotg210_hcd
*fotg210
);
1089 static void start_unlink_intr(struct fotg210_hcd
*fotg210
,
1090 struct fotg210_qh
*qh
);
1091 static void end_unlink_intr(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
);
1093 /*-------------------------------------------------------------------------*/
1095 /* Set a bit in the USBCMD register */
1096 static void fotg210_set_command_bit(struct fotg210_hcd
*fotg210
, u32 bit
)
1098 fotg210
->command
|= bit
;
1099 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
1101 /* unblock posted write */
1102 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1105 /* Clear a bit in the USBCMD register */
1106 static void fotg210_clear_command_bit(struct fotg210_hcd
*fotg210
, u32 bit
)
1108 fotg210
->command
&= ~bit
;
1109 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
1111 /* unblock posted write */
1112 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1115 /*-------------------------------------------------------------------------*/
1118 * EHCI timer support... Now using hrtimers.
1120 * Lots of different events are triggered from fotg210->hrtimer. Whenever
1121 * the timer routine runs, it checks each possible event; events that are
1122 * currently enabled and whose expiration time has passed get handled.
1123 * The set of enabled events is stored as a collection of bitflags in
1124 * fotg210->enabled_hrtimer_events, and they are numbered in order of
1125 * increasing delay values (ranging between 1 ms and 100 ms).
1127 * Rather than implementing a sorted list or tree of all pending events,
1128 * we keep track only of the lowest-numbered pending event, in
1129 * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
1130 * expiration time is set to the timeout value for this event.
1132 * As a result, events might not get handled right away; the actual delay
1133 * could be anywhere up to twice the requested delay. This doesn't
1134 * matter, because none of the events are especially time-critical. The
1135 * ones that matter most all have a delay of 1 ms, so they will be
1136 * handled after 2 ms at most, which is okay. In addition to this, we
1137 * allow for an expiration range of 1 ms.
1141 * Delay lengths for the hrtimer event types.
1142 * Keep this list sorted by delay length, in the same order as
1143 * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
1145 static unsigned event_delays_ns
[] = {
1146 1 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_POLL_ASS */
1147 1 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_POLL_PSS */
1148 1 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_POLL_DEAD */
1149 1125 * NSEC_PER_USEC
, /* FOTG210_HRTIMER_UNLINK_INTR */
1150 2 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_FREE_ITDS */
1151 6 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1152 10 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1153 10 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1154 15 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1155 100 * NSEC_PER_MSEC
, /* FOTG210_HRTIMER_IO_WATCHDOG */
1158 /* Enable a pending hrtimer event */
1159 static void fotg210_enable_event(struct fotg210_hcd
*fotg210
, unsigned event
,
1162 ktime_t
*timeout
= &fotg210
->hr_timeouts
[event
];
1165 *timeout
= ktime_add(ktime_get(),
1166 ktime_set(0, event_delays_ns
[event
]));
1167 fotg210
->enabled_hrtimer_events
|= (1 << event
);
1169 /* Track only the lowest-numbered pending event */
1170 if (event
< fotg210
->next_hrtimer_event
) {
1171 fotg210
->next_hrtimer_event
= event
;
1172 hrtimer_start_range_ns(&fotg210
->hrtimer
, *timeout
,
1173 NSEC_PER_MSEC
, HRTIMER_MODE_ABS
);
1178 /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
1179 static void fotg210_poll_ASS(struct fotg210_hcd
*fotg210
)
1181 unsigned actual
, want
;
1183 /* Don't enable anything if the controller isn't running (e.g., died) */
1184 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
)
1187 want
= (fotg210
->command
& CMD_ASE
) ? STS_ASS
: 0;
1188 actual
= fotg210_readl(fotg210
, &fotg210
->regs
->status
) & STS_ASS
;
1190 if (want
!= actual
) {
1192 /* Poll again later, but give up after about 20 ms */
1193 if (fotg210
->ASS_poll_count
++ < 20) {
1194 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_POLL_ASS
,
1198 fotg210_dbg(fotg210
, "Waited too long for the async schedule status (%x/%x), giving up\n",
1201 fotg210
->ASS_poll_count
= 0;
1203 /* The status is up-to-date; restart or stop the schedule as needed */
1204 if (want
== 0) { /* Stopped */
1205 if (fotg210
->async_count
> 0)
1206 fotg210_set_command_bit(fotg210
, CMD_ASE
);
1208 } else { /* Running */
1209 if (fotg210
->async_count
== 0) {
1211 /* Turn off the schedule after a while */
1212 fotg210_enable_event(fotg210
,
1213 FOTG210_HRTIMER_DISABLE_ASYNC
,
1219 /* Turn off the async schedule after a brief delay */
1220 static void fotg210_disable_ASE(struct fotg210_hcd
*fotg210
)
1222 fotg210_clear_command_bit(fotg210
, CMD_ASE
);
1226 /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
1227 static void fotg210_poll_PSS(struct fotg210_hcd
*fotg210
)
1229 unsigned actual
, want
;
1231 /* Don't do anything if the controller isn't running (e.g., died) */
1232 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
)
1235 want
= (fotg210
->command
& CMD_PSE
) ? STS_PSS
: 0;
1236 actual
= fotg210_readl(fotg210
, &fotg210
->regs
->status
) & STS_PSS
;
1238 if (want
!= actual
) {
1240 /* Poll again later, but give up after about 20 ms */
1241 if (fotg210
->PSS_poll_count
++ < 20) {
1242 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_POLL_PSS
,
1246 fotg210_dbg(fotg210
, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
1249 fotg210
->PSS_poll_count
= 0;
1251 /* The status is up-to-date; restart or stop the schedule as needed */
1252 if (want
== 0) { /* Stopped */
1253 if (fotg210
->periodic_count
> 0)
1254 fotg210_set_command_bit(fotg210
, CMD_PSE
);
1256 } else { /* Running */
1257 if (fotg210
->periodic_count
== 0) {
1259 /* Turn off the schedule after a while */
1260 fotg210_enable_event(fotg210
,
1261 FOTG210_HRTIMER_DISABLE_PERIODIC
,
1267 /* Turn off the periodic schedule after a brief delay */
1268 static void fotg210_disable_PSE(struct fotg210_hcd
*fotg210
)
1270 fotg210_clear_command_bit(fotg210
, CMD_PSE
);
1274 /* Poll the STS_HALT status bit; see when a dead controller stops */
1275 static void fotg210_handle_controller_death(struct fotg210_hcd
*fotg210
)
1277 if (!(fotg210_readl(fotg210
, &fotg210
->regs
->status
) & STS_HALT
)) {
1279 /* Give up after a few milliseconds */
1280 if (fotg210
->died_poll_count
++ < 5) {
1281 /* Try again later */
1282 fotg210_enable_event(fotg210
,
1283 FOTG210_HRTIMER_POLL_DEAD
, true);
1286 fotg210_warn(fotg210
, "Waited too long for the controller to stop, giving up\n");
1289 /* Clean up the mess */
1290 fotg210
->rh_state
= FOTG210_RH_HALTED
;
1291 fotg210_writel(fotg210
, 0, &fotg210
->regs
->intr_enable
);
1292 fotg210_work(fotg210
);
1293 end_unlink_async(fotg210
);
1295 /* Not in process context, so don't try to reset the controller */
1299 /* Handle unlinked interrupt QHs once they are gone from the hardware */
1300 static void fotg210_handle_intr_unlinks(struct fotg210_hcd
*fotg210
)
1302 bool stopped
= (fotg210
->rh_state
< FOTG210_RH_RUNNING
);
1305 * Process all the QHs on the intr_unlink list that were added
1306 * before the current unlink cycle began. The list is in
1307 * temporal order, so stop when we reach the first entry in the
1308 * current cycle. But if the root hub isn't running then
1309 * process all the QHs on the list.
1311 fotg210
->intr_unlinking
= true;
1312 while (fotg210
->intr_unlink
) {
1313 struct fotg210_qh
*qh
= fotg210
->intr_unlink
;
1315 if (!stopped
&& qh
->unlink_cycle
== fotg210
->intr_unlink_cycle
)
1317 fotg210
->intr_unlink
= qh
->unlink_next
;
1318 qh
->unlink_next
= NULL
;
1319 end_unlink_intr(fotg210
, qh
);
1322 /* Handle remaining entries later */
1323 if (fotg210
->intr_unlink
) {
1324 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_UNLINK_INTR
,
1326 ++fotg210
->intr_unlink_cycle
;
1328 fotg210
->intr_unlinking
= false;
1332 /* Start another free-iTDs/siTDs cycle */
1333 static void start_free_itds(struct fotg210_hcd
*fotg210
)
1335 if (!(fotg210
->enabled_hrtimer_events
&
1336 BIT(FOTG210_HRTIMER_FREE_ITDS
))) {
1337 fotg210
->last_itd_to_free
= list_entry(
1338 fotg210
->cached_itd_list
.prev
,
1339 struct fotg210_itd
, itd_list
);
1340 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_FREE_ITDS
, true);
1344 /* Wait for controller to stop using old iTDs and siTDs */
1345 static void end_free_itds(struct fotg210_hcd
*fotg210
)
1347 struct fotg210_itd
*itd
, *n
;
1349 if (fotg210
->rh_state
< FOTG210_RH_RUNNING
)
1350 fotg210
->last_itd_to_free
= NULL
;
1352 list_for_each_entry_safe(itd
, n
, &fotg210
->cached_itd_list
, itd_list
) {
1353 list_del(&itd
->itd_list
);
1354 dma_pool_free(fotg210
->itd_pool
, itd
, itd
->itd_dma
);
1355 if (itd
== fotg210
->last_itd_to_free
)
1359 if (!list_empty(&fotg210
->cached_itd_list
))
1360 start_free_itds(fotg210
);
1364 /* Handle lost (or very late) IAA interrupts */
1365 static void fotg210_iaa_watchdog(struct fotg210_hcd
*fotg210
)
1367 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
)
1371 * Lost IAA irqs wedge things badly; seen first with a vt8235.
1372 * So we need this watchdog, but must protect it against both
1373 * (a) SMP races against real IAA firing and retriggering, and
1374 * (b) clean HC shutdown, when IAA watchdog was pending.
1376 if (fotg210
->async_iaa
) {
1379 /* If we get here, IAA is *REALLY* late. It's barely
1380 * conceivable that the system is so busy that CMD_IAAD
1381 * is still legitimately set, so let's be sure it's
1382 * clear before we read STS_IAA. (The HC should clear
1383 * CMD_IAAD when it sets STS_IAA.)
1385 cmd
= fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1388 * If IAA is set here it either legitimately triggered
1389 * after the watchdog timer expired (_way_ late, so we'll
1390 * still count it as lost) ... or a silicon erratum:
1391 * - VIA seems to set IAA without triggering the IRQ;
1392 * - IAAD potentially cleared without setting IAA.
1394 status
= fotg210_readl(fotg210
, &fotg210
->regs
->status
);
1395 if ((status
& STS_IAA
) || !(cmd
& CMD_IAAD
)) {
1396 COUNT(fotg210
->stats
.lost_iaa
);
1397 fotg210_writel(fotg210
, STS_IAA
,
1398 &fotg210
->regs
->status
);
1401 fotg210_vdbg(fotg210
, "IAA watchdog: status %x cmd %x\n",
1403 end_unlink_async(fotg210
);
1408 /* Enable the I/O watchdog, if appropriate */
1409 static void turn_on_io_watchdog(struct fotg210_hcd
*fotg210
)
1411 /* Not needed if the controller isn't running or it's already enabled */
1412 if (fotg210
->rh_state
!= FOTG210_RH_RUNNING
||
1413 (fotg210
->enabled_hrtimer_events
&
1414 BIT(FOTG210_HRTIMER_IO_WATCHDOG
)))
1418 * Isochronous transfers always need the watchdog.
1419 * For other sorts we use it only if the flag is set.
1421 if (fotg210
->isoc_count
> 0 || (fotg210
->need_io_watchdog
&&
1422 fotg210
->async_count
+ fotg210
->intr_count
> 0))
1423 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_IO_WATCHDOG
,
1429 * Handler functions for the hrtimer event types.
1430 * Keep this array in the same order as the event types indexed by
1431 * enum fotg210_hrtimer_event in fotg210.h.
1433 static void (*event_handlers
[])(struct fotg210_hcd
*) = {
1434 fotg210_poll_ASS
, /* FOTG210_HRTIMER_POLL_ASS */
1435 fotg210_poll_PSS
, /* FOTG210_HRTIMER_POLL_PSS */
1436 fotg210_handle_controller_death
, /* FOTG210_HRTIMER_POLL_DEAD */
1437 fotg210_handle_intr_unlinks
, /* FOTG210_HRTIMER_UNLINK_INTR */
1438 end_free_itds
, /* FOTG210_HRTIMER_FREE_ITDS */
1439 unlink_empty_async
, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1440 fotg210_iaa_watchdog
, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1441 fotg210_disable_PSE
, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1442 fotg210_disable_ASE
, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1443 fotg210_work
, /* FOTG210_HRTIMER_IO_WATCHDOG */
1446 static enum hrtimer_restart
fotg210_hrtimer_func(struct hrtimer
*t
)
1448 struct fotg210_hcd
*fotg210
=
1449 container_of(t
, struct fotg210_hcd
, hrtimer
);
1451 unsigned long events
;
1452 unsigned long flags
;
1455 spin_lock_irqsave(&fotg210
->lock
, flags
);
1457 events
= fotg210
->enabled_hrtimer_events
;
1458 fotg210
->enabled_hrtimer_events
= 0;
1459 fotg210
->next_hrtimer_event
= FOTG210_HRTIMER_NO_EVENT
;
1462 * Check each pending event. If its time has expired, handle
1463 * the event; otherwise re-enable it.
1466 for_each_set_bit(e
, &events
, FOTG210_HRTIMER_NUM_EVENTS
) {
1467 if (now
.tv64
>= fotg210
->hr_timeouts
[e
].tv64
)
1468 event_handlers
[e
](fotg210
);
1470 fotg210_enable_event(fotg210
, e
, false);
1473 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1474 return HRTIMER_NORESTART
;
1477 /*-------------------------------------------------------------------------*/
1479 #define fotg210_bus_suspend NULL
1480 #define fotg210_bus_resume NULL
1482 /*-------------------------------------------------------------------------*/
1484 static int check_reset_complete(
1485 struct fotg210_hcd
*fotg210
,
1487 u32 __iomem
*status_reg
,
1490 if (!(port_status
& PORT_CONNECT
))
1493 /* if reset finished and it's still not enabled -- handoff */
1494 if (!(port_status
& PORT_PE
)) {
1495 /* with integrated TT, there's nobody to hand it to! */
1496 fotg210_dbg(fotg210
,
1497 "Failed to enable port %d on root hub TT\n",
1501 fotg210_dbg(fotg210
, "port %d reset complete, port enabled\n",
1508 /*-------------------------------------------------------------------------*/
1511 /* build "status change" packet (one or two bytes) from HC registers */
1514 fotg210_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
1516 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
1520 unsigned long flags
;
1522 /* init status to no-changes */
1525 /* Inform the core about resumes-in-progress by returning
1526 * a non-zero value even if there are no status changes.
1528 status
= fotg210
->resuming_ports
;
1530 mask
= PORT_CSC
| PORT_PEC
;
1531 /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
1533 /* no hub change reports (bit 0) for now (power, ...) */
1535 /* port N changes (bit N)? */
1536 spin_lock_irqsave(&fotg210
->lock
, flags
);
1538 temp
= fotg210_readl(fotg210
, &fotg210
->regs
->port_status
);
1541 * Return status information even for ports with OWNER set.
1542 * Otherwise khubd wouldn't see the disconnect event when a
1543 * high-speed device is switched over to the companion
1544 * controller by the user.
1547 if ((temp
& mask
) != 0 || test_bit(0, &fotg210
->port_c_suspend
)
1548 || (fotg210
->reset_done
[0] && time_after_eq(
1549 jiffies
, fotg210
->reset_done
[0]))) {
1553 /* FIXME autosuspend idle root hubs */
1554 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1555 return status
? retval
: 0;
1558 /*-------------------------------------------------------------------------*/
1561 fotg210_hub_descriptor(
1562 struct fotg210_hcd
*fotg210
,
1563 struct usb_hub_descriptor
*desc
1565 int ports
= HCS_N_PORTS(fotg210
->hcs_params
);
1568 desc
->bDescriptorType
= 0x29;
1569 desc
->bPwrOn2PwrGood
= 10; /* fotg210 1.0, 2.3.9 says 20ms max */
1570 desc
->bHubContrCurrent
= 0;
1572 desc
->bNbrPorts
= ports
;
1573 temp
= 1 + (ports
/ 8);
1574 desc
->bDescLength
= 7 + 2 * temp
;
1576 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1577 memset(&desc
->u
.hs
.DeviceRemovable
[0], 0, temp
);
1578 memset(&desc
->u
.hs
.DeviceRemovable
[temp
], 0xff, temp
);
1580 temp
= 0x0008; /* per-port overcurrent reporting */
1581 temp
|= 0x0002; /* no power switching */
1582 desc
->wHubCharacteristics
= cpu_to_le16(temp
);
1585 /*-------------------------------------------------------------------------*/
1587 static int fotg210_hub_control(
1588 struct usb_hcd
*hcd
,
1595 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
1596 int ports
= HCS_N_PORTS(fotg210
->hcs_params
);
1597 u32 __iomem
*status_reg
= &fotg210
->regs
->port_status
;
1598 u32 temp
, temp1
, status
;
1599 unsigned long flags
;
1604 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1605 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1606 * (track current state ourselves) ... blink for diagnostics,
1607 * power, "this is the one", etc. EHCI spec supports this.
1610 spin_lock_irqsave(&fotg210
->lock
, flags
);
1612 case ClearHubFeature
:
1614 case C_HUB_LOCAL_POWER
:
1615 case C_HUB_OVER_CURRENT
:
1616 /* no hub-wide feature/status flags */
1622 case ClearPortFeature
:
1623 if (!wIndex
|| wIndex
> ports
)
1626 temp
= fotg210_readl(fotg210
, status_reg
);
1627 temp
&= ~PORT_RWC_BITS
;
1630 * Even if OWNER is set, so the port is owned by the
1631 * companion controller, khubd needs to be able to clear
1632 * the port-change status bits (especially
1633 * USB_PORT_STAT_C_CONNECTION).
1637 case USB_PORT_FEAT_ENABLE
:
1638 fotg210_writel(fotg210
, temp
& ~PORT_PE
, status_reg
);
1640 case USB_PORT_FEAT_C_ENABLE
:
1641 fotg210_writel(fotg210
, temp
| PORT_PEC
, status_reg
);
1643 case USB_PORT_FEAT_SUSPEND
:
1644 if (temp
& PORT_RESET
)
1646 if (!(temp
& PORT_SUSPEND
))
1648 if ((temp
& PORT_PE
) == 0)
1651 /* resume signaling for 20 msec */
1652 fotg210_writel(fotg210
, temp
| PORT_RESUME
, status_reg
);
1653 fotg210
->reset_done
[wIndex
] = jiffies
1654 + msecs_to_jiffies(USB_RESUME_TIMEOUT
);
1656 case USB_PORT_FEAT_C_SUSPEND
:
1657 clear_bit(wIndex
, &fotg210
->port_c_suspend
);
1659 case USB_PORT_FEAT_C_CONNECTION
:
1660 fotg210_writel(fotg210
, temp
| PORT_CSC
, status_reg
);
1662 case USB_PORT_FEAT_C_OVER_CURRENT
:
1663 fotg210_writel(fotg210
, temp
| OTGISR_OVC
,
1664 &fotg210
->regs
->otgisr
);
1666 case USB_PORT_FEAT_C_RESET
:
1667 /* GetPortStatus clears reset */
1672 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1674 case GetHubDescriptor
:
1675 fotg210_hub_descriptor(fotg210
, (struct usb_hub_descriptor
*)
1679 /* no hub-wide feature/status flags */
1681 /*cpu_to_le32s ((u32 *) buf); */
1684 if (!wIndex
|| wIndex
> ports
)
1688 temp
= fotg210_readl(fotg210
, status_reg
);
1690 /* wPortChange bits */
1691 if (temp
& PORT_CSC
)
1692 status
|= USB_PORT_STAT_C_CONNECTION
<< 16;
1693 if (temp
& PORT_PEC
)
1694 status
|= USB_PORT_STAT_C_ENABLE
<< 16;
1696 temp1
= fotg210_readl(fotg210
, &fotg210
->regs
->otgisr
);
1697 if (temp1
& OTGISR_OVC
)
1698 status
|= USB_PORT_STAT_C_OVERCURRENT
<< 16;
1700 /* whoever resumes must GetPortStatus to complete it!! */
1701 if (temp
& PORT_RESUME
) {
1703 /* Remote Wakeup received? */
1704 if (!fotg210
->reset_done
[wIndex
]) {
1705 /* resume signaling for 20 msec */
1706 fotg210
->reset_done
[wIndex
] = jiffies
1707 + msecs_to_jiffies(20);
1708 /* check the port again */
1709 mod_timer(&fotg210_to_hcd(fotg210
)->rh_timer
,
1710 fotg210
->reset_done
[wIndex
]);
1713 /* resume completed? */
1714 else if (time_after_eq(jiffies
,
1715 fotg210
->reset_done
[wIndex
])) {
1716 clear_bit(wIndex
, &fotg210
->suspended_ports
);
1717 set_bit(wIndex
, &fotg210
->port_c_suspend
);
1718 fotg210
->reset_done
[wIndex
] = 0;
1720 /* stop resume signaling */
1721 temp
= fotg210_readl(fotg210
, status_reg
);
1722 fotg210_writel(fotg210
,
1723 temp
& ~(PORT_RWC_BITS
| PORT_RESUME
),
1725 clear_bit(wIndex
, &fotg210
->resuming_ports
);
1726 retval
= handshake(fotg210
, status_reg
,
1727 PORT_RESUME
, 0, 2000 /* 2msec */);
1729 fotg210_err(fotg210
,
1730 "port %d resume error %d\n",
1731 wIndex
+ 1, retval
);
1734 temp
&= ~(PORT_SUSPEND
|PORT_RESUME
|(3<<10));
1738 /* whoever resets must GetPortStatus to complete it!! */
1739 if ((temp
& PORT_RESET
)
1740 && time_after_eq(jiffies
,
1741 fotg210
->reset_done
[wIndex
])) {
1742 status
|= USB_PORT_STAT_C_RESET
<< 16;
1743 fotg210
->reset_done
[wIndex
] = 0;
1744 clear_bit(wIndex
, &fotg210
->resuming_ports
);
1746 /* force reset to complete */
1747 fotg210_writel(fotg210
,
1748 temp
& ~(PORT_RWC_BITS
| PORT_RESET
),
1750 /* REVISIT: some hardware needs 550+ usec to clear
1751 * this bit; seems too long to spin routinely...
1753 retval
= handshake(fotg210
, status_reg
,
1754 PORT_RESET
, 0, 1000);
1756 fotg210_err(fotg210
, "port %d reset error %d\n",
1757 wIndex
+ 1, retval
);
1761 /* see what we found out */
1762 temp
= check_reset_complete(fotg210
, wIndex
, status_reg
,
1763 fotg210_readl(fotg210
, status_reg
));
1766 if (!(temp
& (PORT_RESUME
|PORT_RESET
))) {
1767 fotg210
->reset_done
[wIndex
] = 0;
1768 clear_bit(wIndex
, &fotg210
->resuming_ports
);
1771 /* transfer dedicated ports to the companion hc */
1772 if ((temp
& PORT_CONNECT
) &&
1773 test_bit(wIndex
, &fotg210
->companion_ports
)) {
1774 temp
&= ~PORT_RWC_BITS
;
1775 fotg210_writel(fotg210
, temp
, status_reg
);
1776 fotg210_dbg(fotg210
, "port %d --> companion\n",
1778 temp
= fotg210_readl(fotg210
, status_reg
);
1782 * Even if OWNER is set, there's no harm letting khubd
1783 * see the wPortStatus values (they should all be 0 except
1784 * for PORT_POWER anyway).
1787 if (temp
& PORT_CONNECT
) {
1788 status
|= USB_PORT_STAT_CONNECTION
;
1789 status
|= fotg210_port_speed(fotg210
, temp
);
1792 status
|= USB_PORT_STAT_ENABLE
;
1794 /* maybe the port was unsuspended without our knowledge */
1795 if (temp
& (PORT_SUSPEND
|PORT_RESUME
)) {
1796 status
|= USB_PORT_STAT_SUSPEND
;
1797 } else if (test_bit(wIndex
, &fotg210
->suspended_ports
)) {
1798 clear_bit(wIndex
, &fotg210
->suspended_ports
);
1799 clear_bit(wIndex
, &fotg210
->resuming_ports
);
1800 fotg210
->reset_done
[wIndex
] = 0;
1802 set_bit(wIndex
, &fotg210
->port_c_suspend
);
1805 temp1
= fotg210_readl(fotg210
, &fotg210
->regs
->otgisr
);
1806 if (temp1
& OTGISR_OVC
)
1807 status
|= USB_PORT_STAT_OVERCURRENT
;
1808 if (temp
& PORT_RESET
)
1809 status
|= USB_PORT_STAT_RESET
;
1810 if (test_bit(wIndex
, &fotg210
->port_c_suspend
))
1811 status
|= USB_PORT_STAT_C_SUSPEND
<< 16;
1813 #ifndef VERBOSE_DEBUG
1814 if (status
& ~0xffff) /* only if wPortChange is interesting */
1816 dbg_port(fotg210
, "GetStatus", wIndex
+ 1, temp
);
1817 put_unaligned_le32(status
, buf
);
1821 case C_HUB_LOCAL_POWER
:
1822 case C_HUB_OVER_CURRENT
:
1823 /* no hub-wide feature/status flags */
1829 case SetPortFeature
:
1830 selector
= wIndex
>> 8;
1833 if (!wIndex
|| wIndex
> ports
)
1836 temp
= fotg210_readl(fotg210
, status_reg
);
1837 temp
&= ~PORT_RWC_BITS
;
1839 case USB_PORT_FEAT_SUSPEND
:
1840 if ((temp
& PORT_PE
) == 0
1841 || (temp
& PORT_RESET
) != 0)
1844 /* After above check the port must be connected.
1845 * Set appropriate bit thus could put phy into low power
1846 * mode if we have hostpc feature
1848 fotg210_writel(fotg210
, temp
| PORT_SUSPEND
,
1850 set_bit(wIndex
, &fotg210
->suspended_ports
);
1852 case USB_PORT_FEAT_RESET
:
1853 if (temp
& PORT_RESUME
)
1855 /* line status bits may report this as low speed,
1856 * which can be fine if this root hub has a
1857 * transaction translator built in.
1859 fotg210_vdbg(fotg210
, "port %d reset\n", wIndex
+ 1);
1864 * caller must wait, then call GetPortStatus
1865 * usb 2.0 spec says 50 ms resets on root
1867 fotg210
->reset_done
[wIndex
] = jiffies
1868 + msecs_to_jiffies(50);
1869 fotg210_writel(fotg210
, temp
, status_reg
);
1872 /* For downstream facing ports (these): one hub port is put
1873 * into test mode according to USB2 11.24.2.13, then the hub
1874 * must be reset (which for root hub now means rmmod+modprobe,
1875 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1876 * about the EHCI-specific stuff.
1878 case USB_PORT_FEAT_TEST
:
1879 if (!selector
|| selector
> 5)
1881 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1882 fotg210_quiesce(fotg210
);
1883 spin_lock_irqsave(&fotg210
->lock
, flags
);
1885 /* Put all enabled ports into suspend */
1886 temp
= fotg210_readl(fotg210
, status_reg
) &
1889 fotg210_writel(fotg210
, temp
| PORT_SUSPEND
,
1892 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1893 fotg210_halt(fotg210
);
1894 spin_lock_irqsave(&fotg210
->lock
, flags
);
1896 temp
= fotg210_readl(fotg210
, status_reg
);
1897 temp
|= selector
<< 16;
1898 fotg210_writel(fotg210
, temp
, status_reg
);
1904 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
1909 /* "stall" on error */
1912 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1916 static void __maybe_unused
fotg210_relinquish_port(struct usb_hcd
*hcd
,
1922 static int __maybe_unused
fotg210_port_handed_over(struct usb_hcd
*hcd
,
1927 /*-------------------------------------------------------------------------*/
1929 * There's basically three types of memory:
1930 * - data used only by the HCD ... kmalloc is fine
1931 * - async and periodic schedules, shared by HC and HCD ... these
1932 * need to use dma_pool or dma_alloc_coherent
1933 * - driver buffers, read/written by HC ... single shot DMA mapped
1935 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
1936 * No memory seen by this driver is pageable.
1939 /*-------------------------------------------------------------------------*/
1941 /* Allocate the key transfer structures from the previously allocated pool */
1943 static inline void fotg210_qtd_init(struct fotg210_hcd
*fotg210
,
1944 struct fotg210_qtd
*qtd
, dma_addr_t dma
)
1946 memset(qtd
, 0, sizeof(*qtd
));
1948 qtd
->hw_token
= cpu_to_hc32(fotg210
, QTD_STS_HALT
);
1949 qtd
->hw_next
= FOTG210_LIST_END(fotg210
);
1950 qtd
->hw_alt_next
= FOTG210_LIST_END(fotg210
);
1951 INIT_LIST_HEAD(&qtd
->qtd_list
);
1954 static struct fotg210_qtd
*fotg210_qtd_alloc(struct fotg210_hcd
*fotg210
,
1957 struct fotg210_qtd
*qtd
;
1960 qtd
= dma_pool_alloc(fotg210
->qtd_pool
, flags
, &dma
);
1962 fotg210_qtd_init(fotg210
, qtd
, dma
);
1967 static inline void fotg210_qtd_free(struct fotg210_hcd
*fotg210
,
1968 struct fotg210_qtd
*qtd
)
1970 dma_pool_free(fotg210
->qtd_pool
, qtd
, qtd
->qtd_dma
);
1974 static void qh_destroy(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
1976 /* clean qtds first, and know this is not linked */
1977 if (!list_empty(&qh
->qtd_list
) || qh
->qh_next
.ptr
) {
1978 fotg210_dbg(fotg210
, "unused qh not empty!\n");
1982 fotg210_qtd_free(fotg210
, qh
->dummy
);
1983 dma_pool_free(fotg210
->qh_pool
, qh
->hw
, qh
->qh_dma
);
1987 static struct fotg210_qh
*fotg210_qh_alloc(struct fotg210_hcd
*fotg210
,
1990 struct fotg210_qh
*qh
;
1993 qh
= kzalloc(sizeof(*qh
), GFP_ATOMIC
);
1996 qh
->hw
= (struct fotg210_qh_hw
*)
1997 dma_pool_alloc(fotg210
->qh_pool
, flags
, &dma
);
2000 memset(qh
->hw
, 0, sizeof(*qh
->hw
));
2002 INIT_LIST_HEAD(&qh
->qtd_list
);
2004 /* dummy td enables safe urb queuing */
2005 qh
->dummy
= fotg210_qtd_alloc(fotg210
, flags
);
2006 if (qh
->dummy
== NULL
) {
2007 fotg210_dbg(fotg210
, "no dummy td\n");
2013 dma_pool_free(fotg210
->qh_pool
, qh
->hw
, qh
->qh_dma
);
2019 /*-------------------------------------------------------------------------*/
2021 /* The queue heads and transfer descriptors are managed from pools tied
2022 * to each of the "per device" structures.
2023 * This is the initialisation and cleanup code.
2026 static void fotg210_mem_cleanup(struct fotg210_hcd
*fotg210
)
2029 qh_destroy(fotg210
, fotg210
->async
);
2030 fotg210
->async
= NULL
;
2033 qh_destroy(fotg210
, fotg210
->dummy
);
2034 fotg210
->dummy
= NULL
;
2036 /* DMA consistent memory and pools */
2037 if (fotg210
->qtd_pool
)
2038 dma_pool_destroy(fotg210
->qtd_pool
);
2039 fotg210
->qtd_pool
= NULL
;
2041 if (fotg210
->qh_pool
) {
2042 dma_pool_destroy(fotg210
->qh_pool
);
2043 fotg210
->qh_pool
= NULL
;
2046 if (fotg210
->itd_pool
)
2047 dma_pool_destroy(fotg210
->itd_pool
);
2048 fotg210
->itd_pool
= NULL
;
2050 if (fotg210
->periodic
)
2051 dma_free_coherent(fotg210_to_hcd(fotg210
)->self
.controller
,
2052 fotg210
->periodic_size
* sizeof(u32
),
2053 fotg210
->periodic
, fotg210
->periodic_dma
);
2054 fotg210
->periodic
= NULL
;
2056 /* shadow periodic table */
2057 kfree(fotg210
->pshadow
);
2058 fotg210
->pshadow
= NULL
;
2061 /* remember to add cleanup code (above) if you add anything here */
2062 static int fotg210_mem_init(struct fotg210_hcd
*fotg210
, gfp_t flags
)
2066 /* QTDs for control/bulk/intr transfers */
2067 fotg210
->qtd_pool
= dma_pool_create("fotg210_qtd",
2068 fotg210_to_hcd(fotg210
)->self
.controller
,
2069 sizeof(struct fotg210_qtd
),
2070 32 /* byte alignment (for hw parts) */,
2071 4096 /* can't cross 4K */);
2072 if (!fotg210
->qtd_pool
)
2075 /* QHs for control/bulk/intr transfers */
2076 fotg210
->qh_pool
= dma_pool_create("fotg210_qh",
2077 fotg210_to_hcd(fotg210
)->self
.controller
,
2078 sizeof(struct fotg210_qh_hw
),
2079 32 /* byte alignment (for hw parts) */,
2080 4096 /* can't cross 4K */);
2081 if (!fotg210
->qh_pool
)
2084 fotg210
->async
= fotg210_qh_alloc(fotg210
, flags
);
2085 if (!fotg210
->async
)
2088 /* ITD for high speed ISO transfers */
2089 fotg210
->itd_pool
= dma_pool_create("fotg210_itd",
2090 fotg210_to_hcd(fotg210
)->self
.controller
,
2091 sizeof(struct fotg210_itd
),
2092 64 /* byte alignment (for hw parts) */,
2093 4096 /* can't cross 4K */);
2094 if (!fotg210
->itd_pool
)
2097 /* Hardware periodic table */
2098 fotg210
->periodic
= (__le32
*)
2099 dma_alloc_coherent(fotg210_to_hcd(fotg210
)->self
.controller
,
2100 fotg210
->periodic_size
* sizeof(__le32
),
2101 &fotg210
->periodic_dma
, 0);
2102 if (fotg210
->periodic
== NULL
)
2105 for (i
= 0; i
< fotg210
->periodic_size
; i
++)
2106 fotg210
->periodic
[i
] = FOTG210_LIST_END(fotg210
);
2108 /* software shadow of hardware table */
2109 fotg210
->pshadow
= kcalloc(fotg210
->periodic_size
, sizeof(void *),
2111 if (fotg210
->pshadow
!= NULL
)
2115 fotg210_dbg(fotg210
, "couldn't init memory\n");
2116 fotg210_mem_cleanup(fotg210
);
2119 /*-------------------------------------------------------------------------*/
2121 * EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
2123 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
2124 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
2125 * buffers needed for the larger number). We use one QH per endpoint, queue
2126 * multiple urbs (all three types) per endpoint. URBs may need several qtds.
2128 * ISO traffic uses "ISO TD" (itd) records, and (along with
2129 * interrupts) needs careful scheduling. Performance improvements can be
2130 * an ongoing challenge. That's in "ehci-sched.c".
2132 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
2133 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
2134 * (b) special fields in qh entries or (c) split iso entries. TTs will
2135 * buffer low/full speed data so the host collects it at high speed.
2138 /*-------------------------------------------------------------------------*/
2140 /* fill a qtd, returning how much of the buffer we were able to queue up */
2143 qtd_fill(struct fotg210_hcd
*fotg210
, struct fotg210_qtd
*qtd
, dma_addr_t buf
,
2144 size_t len
, int token
, int maxpacket
)
2149 /* one buffer entry per 4K ... first might be short or unaligned */
2150 qtd
->hw_buf
[0] = cpu_to_hc32(fotg210
, (u32
)addr
);
2151 qtd
->hw_buf_hi
[0] = cpu_to_hc32(fotg210
, (u32
)(addr
>> 32));
2152 count
= 0x1000 - (buf
& 0x0fff); /* rest of that page */
2153 if (likely(len
< count
)) /* ... iff needed */
2159 /* per-qtd limit: from 16K to 20K (best alignment) */
2160 for (i
= 1; count
< len
&& i
< 5; i
++) {
2162 qtd
->hw_buf
[i
] = cpu_to_hc32(fotg210
, (u32
)addr
);
2163 qtd
->hw_buf_hi
[i
] = cpu_to_hc32(fotg210
,
2166 if ((count
+ 0x1000) < len
)
2172 /* short packets may only terminate transfers */
2174 count
-= (count
% maxpacket
);
2176 qtd
->hw_token
= cpu_to_hc32(fotg210
, (count
<< 16) | token
);
2177 qtd
->length
= count
;
2182 /*-------------------------------------------------------------------------*/
2185 qh_update(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
,
2186 struct fotg210_qtd
*qtd
)
2188 struct fotg210_qh_hw
*hw
= qh
->hw
;
2190 /* writes to an active overlay are unsafe */
2191 BUG_ON(qh
->qh_state
!= QH_STATE_IDLE
);
2193 hw
->hw_qtd_next
= QTD_NEXT(fotg210
, qtd
->qtd_dma
);
2194 hw
->hw_alt_next
= FOTG210_LIST_END(fotg210
);
2196 /* Except for control endpoints, we make hardware maintain data
2197 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
2198 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
2201 if (!(hw
->hw_info1
& cpu_to_hc32(fotg210
, QH_TOGGLE_CTL
))) {
2202 unsigned is_out
, epnum
;
2204 is_out
= qh
->is_out
;
2205 epnum
= (hc32_to_cpup(fotg210
, &hw
->hw_info1
) >> 8) & 0x0f;
2206 if (unlikely(!usb_gettoggle(qh
->dev
, epnum
, is_out
))) {
2207 hw
->hw_token
&= ~cpu_to_hc32(fotg210
, QTD_TOGGLE
);
2208 usb_settoggle(qh
->dev
, epnum
, is_out
, 1);
2212 hw
->hw_token
&= cpu_to_hc32(fotg210
, QTD_TOGGLE
| QTD_STS_PING
);
2215 /* if it weren't for a common silicon quirk (writing the dummy into the qh
2216 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
2217 * recovery (including urb dequeue) would need software changes to a QH...
2220 qh_refresh(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
2222 struct fotg210_qtd
*qtd
;
2224 if (list_empty(&qh
->qtd_list
))
2227 qtd
= list_entry(qh
->qtd_list
.next
,
2228 struct fotg210_qtd
, qtd_list
);
2230 * first qtd may already be partially processed.
2231 * If we come here during unlink, the QH overlay region
2232 * might have reference to the just unlinked qtd. The
2233 * qtd is updated in qh_completions(). Update the QH
2236 if (cpu_to_hc32(fotg210
, qtd
->qtd_dma
) == qh
->hw
->hw_current
) {
2237 qh
->hw
->hw_qtd_next
= qtd
->hw_next
;
2243 qh_update(fotg210
, qh
, qtd
);
2246 /*-------------------------------------------------------------------------*/
2248 static void qh_link_async(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
);
2250 static void fotg210_clear_tt_buffer_complete(struct usb_hcd
*hcd
,
2251 struct usb_host_endpoint
*ep
)
2253 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
2254 struct fotg210_qh
*qh
= ep
->hcpriv
;
2255 unsigned long flags
;
2257 spin_lock_irqsave(&fotg210
->lock
, flags
);
2258 qh
->clearing_tt
= 0;
2259 if (qh
->qh_state
== QH_STATE_IDLE
&& !list_empty(&qh
->qtd_list
)
2260 && fotg210
->rh_state
== FOTG210_RH_RUNNING
)
2261 qh_link_async(fotg210
, qh
);
2262 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
2265 static void fotg210_clear_tt_buffer(struct fotg210_hcd
*fotg210
,
2266 struct fotg210_qh
*qh
,
2267 struct urb
*urb
, u32 token
)
2270 /* If an async split transaction gets an error or is unlinked,
2271 * the TT buffer may be left in an indeterminate state. We
2272 * have to clear the TT buffer.
2274 * Note: this routine is never called for Isochronous transfers.
2276 if (urb
->dev
->tt
&& !usb_pipeint(urb
->pipe
) && !qh
->clearing_tt
) {
2278 struct usb_device
*tt
= urb
->dev
->tt
->hub
;
2280 "clear tt buffer port %d, a%d ep%d t%08x\n",
2281 urb
->dev
->ttport
, urb
->dev
->devnum
,
2282 usb_pipeendpoint(urb
->pipe
), token
);
2284 if (urb
->dev
->tt
->hub
!=
2285 fotg210_to_hcd(fotg210
)->self
.root_hub
) {
2286 if (usb_hub_clear_tt_buffer(urb
) == 0)
2287 qh
->clearing_tt
= 1;
2292 static int qtd_copy_status(
2293 struct fotg210_hcd
*fotg210
,
2299 int status
= -EINPROGRESS
;
2301 /* count IN/OUT bytes, not SETUP (even short packets) */
2302 if (likely(QTD_PID(token
) != 2))
2303 urb
->actual_length
+= length
- QTD_LENGTH(token
);
2305 /* don't modify error codes */
2306 if (unlikely(urb
->unlinked
))
2309 /* force cleanup after short read; not always an error */
2310 if (unlikely(IS_SHORT_READ(token
)))
2311 status
= -EREMOTEIO
;
2313 /* serious "can't proceed" faults reported by the hardware */
2314 if (token
& QTD_STS_HALT
) {
2315 if (token
& QTD_STS_BABBLE
) {
2316 /* FIXME "must" disable babbling device's port too */
2317 status
= -EOVERFLOW
;
2318 /* CERR nonzero + halt --> stall */
2319 } else if (QTD_CERR(token
)) {
2322 /* In theory, more than one of the following bits can be set
2323 * since they are sticky and the transaction is retried.
2324 * Which to test first is rather arbitrary.
2326 } else if (token
& QTD_STS_MMF
) {
2327 /* fs/ls interrupt xfer missed the complete-split */
2329 } else if (token
& QTD_STS_DBE
) {
2330 status
= (QTD_PID(token
) == 1) /* IN ? */
2331 ? -ENOSR
/* hc couldn't read data */
2332 : -ECOMM
; /* hc couldn't write data */
2333 } else if (token
& QTD_STS_XACT
) {
2334 /* timeout, bad CRC, wrong PID, etc */
2335 fotg210_dbg(fotg210
, "devpath %s ep%d%s 3strikes\n",
2337 usb_pipeendpoint(urb
->pipe
),
2338 usb_pipein(urb
->pipe
) ? "in" : "out");
2340 } else { /* unknown */
2344 fotg210_vdbg(fotg210
,
2345 "dev%d ep%d%s qtd token %08x --> status %d\n",
2346 usb_pipedevice(urb
->pipe
),
2347 usb_pipeendpoint(urb
->pipe
),
2348 usb_pipein(urb
->pipe
) ? "in" : "out",
2356 fotg210_urb_done(struct fotg210_hcd
*fotg210
, struct urb
*urb
, int status
)
2357 __releases(fotg210
->lock
)
2358 __acquires(fotg210
->lock
)
2360 if (likely(urb
->hcpriv
!= NULL
)) {
2361 struct fotg210_qh
*qh
= (struct fotg210_qh
*) urb
->hcpriv
;
2363 /* S-mask in a QH means it's an interrupt urb */
2364 if ((qh
->hw
->hw_info2
& cpu_to_hc32(fotg210
, QH_SMASK
)) != 0) {
2366 /* ... update hc-wide periodic stats (for usbfs) */
2367 fotg210_to_hcd(fotg210
)->self
.bandwidth_int_reqs
--;
2371 if (unlikely(urb
->unlinked
)) {
2372 COUNT(fotg210
->stats
.unlink
);
2374 /* report non-error and short read status as zero */
2375 if (status
== -EINPROGRESS
|| status
== -EREMOTEIO
)
2377 COUNT(fotg210
->stats
.complete
);
2380 #ifdef FOTG210_URB_TRACE
2381 fotg210_dbg(fotg210
,
2382 "%s %s urb %p ep%d%s status %d len %d/%d\n",
2383 __func__
, urb
->dev
->devpath
, urb
,
2384 usb_pipeendpoint(urb
->pipe
),
2385 usb_pipein(urb
->pipe
) ? "in" : "out",
2387 urb
->actual_length
, urb
->transfer_buffer_length
);
2390 /* complete() can reenter this HCD */
2391 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210
), urb
);
2392 spin_unlock(&fotg210
->lock
);
2393 usb_hcd_giveback_urb(fotg210_to_hcd(fotg210
), urb
, status
);
2394 spin_lock(&fotg210
->lock
);
2397 static int qh_schedule(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
);
2400 * Process and free completed qtds for a qh, returning URBs to drivers.
2401 * Chases up to qh->hw_current. Returns number of completions called,
2402 * indicating how much "real" work we did.
2405 qh_completions(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
2407 struct fotg210_qtd
*last
, *end
= qh
->dummy
;
2408 struct list_head
*entry
, *tmp
;
2413 struct fotg210_qh_hw
*hw
= qh
->hw
;
2415 if (unlikely(list_empty(&qh
->qtd_list
)))
2418 /* completions (or tasks on other cpus) must never clobber HALT
2419 * till we've gone through and cleaned everything up, even when
2420 * they add urbs to this qh's queue or mark them for unlinking.
2422 * NOTE: unlinking expects to be done in queue order.
2424 * It's a bug for qh->qh_state to be anything other than
2425 * QH_STATE_IDLE, unless our caller is scan_async() or
2428 state
= qh
->qh_state
;
2429 qh
->qh_state
= QH_STATE_COMPLETING
;
2430 stopped
= (state
== QH_STATE_IDLE
);
2434 last_status
= -EINPROGRESS
;
2435 qh
->needs_rescan
= 0;
2437 /* remove de-activated QTDs from front of queue.
2438 * after faults (including short reads), cleanup this urb
2439 * then let the queue advance.
2440 * if queue is stopped, handles unlinks.
2442 list_for_each_safe(entry
, tmp
, &qh
->qtd_list
) {
2443 struct fotg210_qtd
*qtd
;
2447 qtd
= list_entry(entry
, struct fotg210_qtd
, qtd_list
);
2450 /* clean up any state from previous QTD ...*/
2452 if (likely(last
->urb
!= urb
)) {
2453 fotg210_urb_done(fotg210
, last
->urb
,
2456 last_status
= -EINPROGRESS
;
2458 fotg210_qtd_free(fotg210
, last
);
2462 /* ignore urbs submitted during completions we reported */
2466 /* hardware copies qtd out of qh overlay */
2468 token
= hc32_to_cpu(fotg210
, qtd
->hw_token
);
2470 /* always clean up qtds the hc de-activated */
2472 if ((token
& QTD_STS_ACTIVE
) == 0) {
2474 /* Report Data Buffer Error: non-fatal but useful */
2475 if (token
& QTD_STS_DBE
)
2476 fotg210_dbg(fotg210
,
2477 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
2479 usb_endpoint_num(&urb
->ep
->desc
),
2480 usb_endpoint_dir_in(&urb
->ep
->desc
)
2482 urb
->transfer_buffer_length
,
2486 /* on STALL, error, and short reads this urb must
2487 * complete and all its qtds must be recycled.
2489 if ((token
& QTD_STS_HALT
) != 0) {
2491 /* retry transaction errors until we
2492 * reach the software xacterr limit
2494 if ((token
& QTD_STS_XACT
) &&
2495 QTD_CERR(token
) == 0 &&
2496 ++qh
->xacterrs
< QH_XACTERR_MAX
&&
2498 fotg210_dbg(fotg210
,
2499 "detected XactErr len %zu/%zu retry %d\n",
2500 qtd
->length
- QTD_LENGTH(token
), qtd
->length
, qh
->xacterrs
);
2502 /* reset the token in the qtd and the
2503 * qh overlay (which still contains
2504 * the qtd) so that we pick up from
2507 token
&= ~QTD_STS_HALT
;
2508 token
|= QTD_STS_ACTIVE
|
2509 (FOTG210_TUNE_CERR
<< 10);
2510 qtd
->hw_token
= cpu_to_hc32(fotg210
,
2513 hw
->hw_token
= cpu_to_hc32(fotg210
,
2519 /* magic dummy for some short reads; qh won't advance.
2520 * that silicon quirk can kick in with this dummy too.
2522 * other short reads won't stop the queue, including
2523 * control transfers (status stage handles that) or
2524 * most other single-qtd reads ... the queue stops if
2525 * URB_SHORT_NOT_OK was set so the driver submitting
2526 * the urbs could clean it up.
2528 } else if (IS_SHORT_READ(token
)
2529 && !(qtd
->hw_alt_next
2530 & FOTG210_LIST_END(fotg210
))) {
2534 /* stop scanning when we reach qtds the hc is using */
2535 } else if (likely(!stopped
2536 && fotg210
->rh_state
>= FOTG210_RH_RUNNING
)) {
2539 /* scan the whole queue for unlinks whenever it stops */
2543 /* cancel everything if we halt, suspend, etc */
2544 if (fotg210
->rh_state
< FOTG210_RH_RUNNING
)
2545 last_status
= -ESHUTDOWN
;
2547 /* this qtd is active; skip it unless a previous qtd
2548 * for its urb faulted, or its urb was canceled.
2550 else if (last_status
== -EINPROGRESS
&& !urb
->unlinked
)
2553 /* qh unlinked; token in overlay may be most current */
2554 if (state
== QH_STATE_IDLE
2555 && cpu_to_hc32(fotg210
, qtd
->qtd_dma
)
2556 == hw
->hw_current
) {
2557 token
= hc32_to_cpu(fotg210
, hw
->hw_token
);
2559 /* An unlink may leave an incomplete
2560 * async transaction in the TT buffer.
2561 * We have to clear it.
2563 fotg210_clear_tt_buffer(fotg210
, qh
, urb
,
2568 /* unless we already know the urb's status, collect qtd status
2569 * and update count of bytes transferred. in common short read
2570 * cases with only one data qtd (including control transfers),
2571 * queue processing won't halt. but with two or more qtds (for
2572 * example, with a 32 KB transfer), when the first qtd gets a
2573 * short read the second must be removed by hand.
2575 if (last_status
== -EINPROGRESS
) {
2576 last_status
= qtd_copy_status(fotg210
, urb
,
2577 qtd
->length
, token
);
2578 if (last_status
== -EREMOTEIO
2579 && (qtd
->hw_alt_next
2580 & FOTG210_LIST_END(fotg210
)))
2581 last_status
= -EINPROGRESS
;
2583 /* As part of low/full-speed endpoint-halt processing
2584 * we must clear the TT buffer (11.17.5).
2586 if (unlikely(last_status
!= -EINPROGRESS
&&
2587 last_status
!= -EREMOTEIO
)) {
2588 /* The TT's in some hubs malfunction when they
2589 * receive this request following a STALL (they
2590 * stop sending isochronous packets). Since a
2591 * STALL can't leave the TT buffer in a busy
2592 * state (if you believe Figures 11-48 - 11-51
2593 * in the USB 2.0 spec), we won't clear the TT
2594 * buffer in this case. Strictly speaking this
2595 * is a violation of the spec.
2597 if (last_status
!= -EPIPE
)
2598 fotg210_clear_tt_buffer(fotg210
, qh
,
2603 /* if we're removing something not at the queue head,
2604 * patch the hardware queue pointer.
2606 if (stopped
&& qtd
->qtd_list
.prev
!= &qh
->qtd_list
) {
2607 last
= list_entry(qtd
->qtd_list
.prev
,
2608 struct fotg210_qtd
, qtd_list
);
2609 last
->hw_next
= qtd
->hw_next
;
2612 /* remove qtd; it's recycled after possible urb completion */
2613 list_del(&qtd
->qtd_list
);
2616 /* reinit the xacterr counter for the next qtd */
2620 /* last urb's completion might still need calling */
2621 if (likely(last
!= NULL
)) {
2622 fotg210_urb_done(fotg210
, last
->urb
, last_status
);
2624 fotg210_qtd_free(fotg210
, last
);
2627 /* Do we need to rescan for URBs dequeued during a giveback? */
2628 if (unlikely(qh
->needs_rescan
)) {
2629 /* If the QH is already unlinked, do the rescan now. */
2630 if (state
== QH_STATE_IDLE
)
2633 /* Otherwise we have to wait until the QH is fully unlinked.
2634 * Our caller will start an unlink if qh->needs_rescan is
2635 * set. But if an unlink has already started, nothing needs
2638 if (state
!= QH_STATE_LINKED
)
2639 qh
->needs_rescan
= 0;
2642 /* restore original state; caller must unlink or relink */
2643 qh
->qh_state
= state
;
2645 /* be sure the hardware's done with the qh before refreshing
2646 * it after fault cleanup, or recovering from silicon wrongly
2647 * overlaying the dummy qtd (which reduces DMA chatter).
2649 if (stopped
!= 0 || hw
->hw_qtd_next
== FOTG210_LIST_END(fotg210
)) {
2652 qh_refresh(fotg210
, qh
);
2654 case QH_STATE_LINKED
:
2655 /* We won't refresh a QH that's linked (after the HC
2656 * stopped the queue). That avoids a race:
2657 * - HC reads first part of QH;
2658 * - CPU updates that first part and the token;
2659 * - HC reads rest of that QH, including token
2660 * Result: HC gets an inconsistent image, and then
2661 * DMAs to/from the wrong memory (corrupting it).
2663 * That should be rare for interrupt transfers,
2664 * except maybe high bandwidth ...
2667 /* Tell the caller to start an unlink */
2668 qh
->needs_rescan
= 1;
2670 /* otherwise, unlink already started */
2677 /*-------------------------------------------------------------------------*/
2679 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
2680 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
2681 /* ... and packet size, for any kind of endpoint descriptor */
2682 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
2685 * reverse of qh_urb_transaction: free a list of TDs.
2686 * used for cleanup after errors, before HC sees an URB's TDs.
2688 static void qtd_list_free(
2689 struct fotg210_hcd
*fotg210
,
2691 struct list_head
*qtd_list
2693 struct list_head
*entry
, *temp
;
2695 list_for_each_safe(entry
, temp
, qtd_list
) {
2696 struct fotg210_qtd
*qtd
;
2698 qtd
= list_entry(entry
, struct fotg210_qtd
, qtd_list
);
2699 list_del(&qtd
->qtd_list
);
2700 fotg210_qtd_free(fotg210
, qtd
);
2705 * create a list of filled qtds for this URB; won't link into qh.
2707 static struct list_head
*
2709 struct fotg210_hcd
*fotg210
,
2711 struct list_head
*head
,
2714 struct fotg210_qtd
*qtd
, *qtd_prev
;
2716 int len
, this_sg_len
, maxpacket
;
2720 struct scatterlist
*sg
;
2723 * URBs map to sequences of QTDs: one logical transaction
2725 qtd
= fotg210_qtd_alloc(fotg210
, flags
);
2728 list_add_tail(&qtd
->qtd_list
, head
);
2731 token
= QTD_STS_ACTIVE
;
2732 token
|= (FOTG210_TUNE_CERR
<< 10);
2733 /* for split transactions, SplitXState initialized to zero */
2735 len
= urb
->transfer_buffer_length
;
2736 is_input
= usb_pipein(urb
->pipe
);
2737 if (usb_pipecontrol(urb
->pipe
)) {
2739 qtd_fill(fotg210
, qtd
, urb
->setup_dma
,
2740 sizeof(struct usb_ctrlrequest
),
2741 token
| (2 /* "setup" */ << 8), 8);
2743 /* ... and always at least one more pid */
2744 token
^= QTD_TOGGLE
;
2746 qtd
= fotg210_qtd_alloc(fotg210
, flags
);
2750 qtd_prev
->hw_next
= QTD_NEXT(fotg210
, qtd
->qtd_dma
);
2751 list_add_tail(&qtd
->qtd_list
, head
);
2753 /* for zero length DATA stages, STATUS is always IN */
2755 token
|= (1 /* "in" */ << 8);
2759 * data transfer stage: buffer setup
2761 i
= urb
->num_mapped_sgs
;
2762 if (len
> 0 && i
> 0) {
2764 buf
= sg_dma_address(sg
);
2766 /* urb->transfer_buffer_length may be smaller than the
2767 * size of the scatterlist (or vice versa)
2769 this_sg_len
= min_t(int, sg_dma_len(sg
), len
);
2772 buf
= urb
->transfer_dma
;
2777 token
|= (1 /* "in" */ << 8);
2778 /* else it's already initted to "out" pid (0 << 8) */
2780 maxpacket
= max_packet(usb_maxpacket(urb
->dev
, urb
->pipe
, !is_input
));
2783 * buffer gets wrapped in one or more qtds;
2784 * last one may be "short" (including zero len)
2785 * and may serve as a control status ack
2790 this_qtd_len
= qtd_fill(fotg210
, qtd
, buf
, this_sg_len
, token
,
2792 this_sg_len
-= this_qtd_len
;
2793 len
-= this_qtd_len
;
2794 buf
+= this_qtd_len
;
2797 * short reads advance to a "magic" dummy instead of the next
2798 * qtd ... that forces the queue to stop, for manual cleanup.
2799 * (this will usually be overridden later.)
2802 qtd
->hw_alt_next
= fotg210
->async
->hw
->hw_alt_next
;
2804 /* qh makes control packets use qtd toggle; maybe switch it */
2805 if ((maxpacket
& (this_qtd_len
+ (maxpacket
- 1))) == 0)
2806 token
^= QTD_TOGGLE
;
2808 if (likely(this_sg_len
<= 0)) {
2809 if (--i
<= 0 || len
<= 0)
2812 buf
= sg_dma_address(sg
);
2813 this_sg_len
= min_t(int, sg_dma_len(sg
), len
);
2817 qtd
= fotg210_qtd_alloc(fotg210
, flags
);
2821 qtd_prev
->hw_next
= QTD_NEXT(fotg210
, qtd
->qtd_dma
);
2822 list_add_tail(&qtd
->qtd_list
, head
);
2826 * unless the caller requires manual cleanup after short reads,
2827 * have the alt_next mechanism keep the queue running after the
2828 * last data qtd (the only one, for control and most other cases).
2830 if (likely((urb
->transfer_flags
& URB_SHORT_NOT_OK
) == 0
2831 || usb_pipecontrol(urb
->pipe
)))
2832 qtd
->hw_alt_next
= FOTG210_LIST_END(fotg210
);
2835 * control requests may need a terminating data "status" ack;
2836 * other OUT ones may need a terminating short packet
2839 if (likely(urb
->transfer_buffer_length
!= 0)) {
2842 if (usb_pipecontrol(urb
->pipe
)) {
2844 token
^= 0x0100; /* "in" <--> "out" */
2845 token
|= QTD_TOGGLE
; /* force DATA1 */
2846 } else if (usb_pipeout(urb
->pipe
)
2847 && (urb
->transfer_flags
& URB_ZERO_PACKET
)
2848 && !(urb
->transfer_buffer_length
% maxpacket
)) {
2853 qtd
= fotg210_qtd_alloc(fotg210
, flags
);
2857 qtd_prev
->hw_next
= QTD_NEXT(fotg210
, qtd
->qtd_dma
);
2858 list_add_tail(&qtd
->qtd_list
, head
);
2860 /* never any data in such packets */
2861 qtd_fill(fotg210
, qtd
, 0, 0, token
, 0);
2865 /* by default, enable interrupt on urb completion */
2866 if (likely(!(urb
->transfer_flags
& URB_NO_INTERRUPT
)))
2867 qtd
->hw_token
|= cpu_to_hc32(fotg210
, QTD_IOC
);
2871 qtd_list_free(fotg210
, urb
, head
);
2875 /*-------------------------------------------------------------------------*/
2877 * Would be best to create all qh's from config descriptors,
2878 * when each interface/altsetting is established. Unlink
2879 * any previous qh and cancel its urbs first; endpoints are
2880 * implicitly reset then (data toggle too).
2881 * That'd mean updating how usbcore talks to HCDs. (2.7?)
2886 * Each QH holds a qtd list; a QH is used for everything except iso.
2888 * For interrupt urbs, the scheduler must set the microframe scheduling
2889 * mask(s) each time the QH gets scheduled. For highspeed, that's
2890 * just one microframe in the s-mask. For split interrupt transactions
2891 * there are additional complications: c-mask, maybe FSTNs.
2893 static struct fotg210_qh
*
2895 struct fotg210_hcd
*fotg210
,
2899 struct fotg210_qh
*qh
= fotg210_qh_alloc(fotg210
, flags
);
2900 u32 info1
= 0, info2
= 0;
2903 struct usb_tt
*tt
= urb
->dev
->tt
;
2904 struct fotg210_qh_hw
*hw
;
2910 * init endpoint/device data for this QH
2912 info1
|= usb_pipeendpoint(urb
->pipe
) << 8;
2913 info1
|= usb_pipedevice(urb
->pipe
) << 0;
2915 is_input
= usb_pipein(urb
->pipe
);
2916 type
= usb_pipetype(urb
->pipe
);
2917 maxp
= usb_maxpacket(urb
->dev
, urb
->pipe
, !is_input
);
2919 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
2920 * acts like up to 3KB, but is built from smaller packets.
2922 if (max_packet(maxp
) > 1024) {
2923 fotg210_dbg(fotg210
, "bogus qh maxpacket %d\n",
2928 /* Compute interrupt scheduling parameters just once, and save.
2929 * - allowing for high bandwidth, how many nsec/uframe are used?
2930 * - split transactions need a second CSPLIT uframe; same question
2931 * - splits also need a schedule gap (for full/low speed I/O)
2932 * - qh has a polling interval
2934 * For control/bulk requests, the HC or TT handles these.
2936 if (type
== PIPE_INTERRUPT
) {
2937 qh
->usecs
= NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH
,
2939 hb_mult(maxp
) * max_packet(maxp
)));
2940 qh
->start
= NO_FRAME
;
2942 if (urb
->dev
->speed
== USB_SPEED_HIGH
) {
2946 qh
->period
= urb
->interval
>> 3;
2947 if (qh
->period
== 0 && urb
->interval
!= 1) {
2948 /* NOTE interval 2 or 4 uframes could work.
2949 * But interval 1 scheduling is simpler, and
2950 * includes high bandwidth.
2953 } else if (qh
->period
> fotg210
->periodic_size
) {
2954 qh
->period
= fotg210
->periodic_size
;
2955 urb
->interval
= qh
->period
<< 3;
2960 /* gap is f(FS/LS transfer times) */
2961 qh
->gap_uf
= 1 + usb_calc_bus_time(urb
->dev
->speed
,
2962 is_input
, 0, maxp
) / (125 * 1000);
2964 /* FIXME this just approximates SPLIT/CSPLIT times */
2965 if (is_input
) { /* SPLIT, gap, CSPLIT+DATA */
2966 qh
->c_usecs
= qh
->usecs
+ HS_USECS(0);
2967 qh
->usecs
= HS_USECS(1);
2968 } else { /* SPLIT+DATA, gap, CSPLIT */
2969 qh
->usecs
+= HS_USECS(1);
2970 qh
->c_usecs
= HS_USECS(0);
2973 think_time
= tt
? tt
->think_time
: 0;
2974 qh
->tt_usecs
= NS_TO_US(think_time
+
2975 usb_calc_bus_time(urb
->dev
->speed
,
2976 is_input
, 0, max_packet(maxp
)));
2977 qh
->period
= urb
->interval
;
2978 if (qh
->period
> fotg210
->periodic_size
) {
2979 qh
->period
= fotg210
->periodic_size
;
2980 urb
->interval
= qh
->period
;
2985 /* support for tt scheduling, and access to toggles */
2989 switch (urb
->dev
->speed
) {
2991 info1
|= QH_LOW_SPEED
;
2994 case USB_SPEED_FULL
:
2995 /* EPS 0 means "full" */
2996 if (type
!= PIPE_INTERRUPT
)
2997 info1
|= (FOTG210_TUNE_RL_TT
<< 28);
2998 if (type
== PIPE_CONTROL
) {
2999 info1
|= QH_CONTROL_EP
; /* for TT */
3000 info1
|= QH_TOGGLE_CTL
; /* toggle from qtd */
3002 info1
|= maxp
<< 16;
3004 info2
|= (FOTG210_TUNE_MULT_TT
<< 30);
3006 /* Some Freescale processors have an erratum in which the
3007 * port number in the queue head was 0..N-1 instead of 1..N.
3009 if (fotg210_has_fsl_portno_bug(fotg210
))
3010 info2
|= (urb
->dev
->ttport
-1) << 23;
3012 info2
|= urb
->dev
->ttport
<< 23;
3014 /* set the address of the TT; for TDI's integrated
3015 * root hub tt, leave it zeroed.
3017 if (tt
&& tt
->hub
!= fotg210_to_hcd(fotg210
)->self
.root_hub
)
3018 info2
|= tt
->hub
->devnum
<< 16;
3020 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
3024 case USB_SPEED_HIGH
: /* no TT involved */
3025 info1
|= QH_HIGH_SPEED
;
3026 if (type
== PIPE_CONTROL
) {
3027 info1
|= (FOTG210_TUNE_RL_HS
<< 28);
3028 info1
|= 64 << 16; /* usb2 fixed maxpacket */
3029 info1
|= QH_TOGGLE_CTL
; /* toggle from qtd */
3030 info2
|= (FOTG210_TUNE_MULT_HS
<< 30);
3031 } else if (type
== PIPE_BULK
) {
3032 info1
|= (FOTG210_TUNE_RL_HS
<< 28);
3033 /* The USB spec says that high speed bulk endpoints
3034 * always use 512 byte maxpacket. But some device
3035 * vendors decided to ignore that, and MSFT is happy
3036 * to help them do so. So now people expect to use
3037 * such nonconformant devices with Linux too; sigh.
3039 info1
|= max_packet(maxp
) << 16;
3040 info2
|= (FOTG210_TUNE_MULT_HS
<< 30);
3041 } else { /* PIPE_INTERRUPT */
3042 info1
|= max_packet(maxp
) << 16;
3043 info2
|= hb_mult(maxp
) << 30;
3047 fotg210_dbg(fotg210
, "bogus dev %p speed %d\n", urb
->dev
,
3050 qh_destroy(fotg210
, qh
);
3054 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
3056 /* init as live, toggle clear, advance to dummy */
3057 qh
->qh_state
= QH_STATE_IDLE
;
3059 hw
->hw_info1
= cpu_to_hc32(fotg210
, info1
);
3060 hw
->hw_info2
= cpu_to_hc32(fotg210
, info2
);
3061 qh
->is_out
= !is_input
;
3062 usb_settoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
), !is_input
, 1);
3063 qh_refresh(fotg210
, qh
);
3067 /*-------------------------------------------------------------------------*/
3069 static void enable_async(struct fotg210_hcd
*fotg210
)
3071 if (fotg210
->async_count
++)
3074 /* Stop waiting to turn off the async schedule */
3075 fotg210
->enabled_hrtimer_events
&= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC
);
3077 /* Don't start the schedule until ASS is 0 */
3078 fotg210_poll_ASS(fotg210
);
3079 turn_on_io_watchdog(fotg210
);
3082 static void disable_async(struct fotg210_hcd
*fotg210
)
3084 if (--fotg210
->async_count
)
3087 /* The async schedule and async_unlink list are supposed to be empty */
3088 WARN_ON(fotg210
->async
->qh_next
.qh
|| fotg210
->async_unlink
);
3090 /* Don't turn off the schedule until ASS is 1 */
3091 fotg210_poll_ASS(fotg210
);
3094 /* move qh (and its qtds) onto async queue; maybe enable queue. */
3096 static void qh_link_async(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
3098 __hc32 dma
= QH_NEXT(fotg210
, qh
->qh_dma
);
3099 struct fotg210_qh
*head
;
3101 /* Don't link a QH if there's a Clear-TT-Buffer pending */
3102 if (unlikely(qh
->clearing_tt
))
3105 WARN_ON(qh
->qh_state
!= QH_STATE_IDLE
);
3107 /* clear halt and/or toggle; and maybe recover from silicon quirk */
3108 qh_refresh(fotg210
, qh
);
3110 /* splice right after start */
3111 head
= fotg210
->async
;
3112 qh
->qh_next
= head
->qh_next
;
3113 qh
->hw
->hw_next
= head
->hw
->hw_next
;
3116 head
->qh_next
.qh
= qh
;
3117 head
->hw
->hw_next
= dma
;
3120 qh
->qh_state
= QH_STATE_LINKED
;
3121 /* qtd completions reported later by interrupt */
3123 enable_async(fotg210
);
3126 /*-------------------------------------------------------------------------*/
3129 * For control/bulk/interrupt, return QH with these TDs appended.
3130 * Allocates and initializes the QH if necessary.
3131 * Returns null if it can't allocate a QH it needs to.
3132 * If the QH has TDs (urbs) already, that's great.
3134 static struct fotg210_qh
*qh_append_tds(
3135 struct fotg210_hcd
*fotg210
,
3137 struct list_head
*qtd_list
,
3142 struct fotg210_qh
*qh
= NULL
;
3143 __hc32 qh_addr_mask
= cpu_to_hc32(fotg210
, 0x7f);
3145 qh
= (struct fotg210_qh
*) *ptr
;
3146 if (unlikely(qh
== NULL
)) {
3147 /* can't sleep here, we have fotg210->lock... */
3148 qh
= qh_make(fotg210
, urb
, GFP_ATOMIC
);
3151 if (likely(qh
!= NULL
)) {
3152 struct fotg210_qtd
*qtd
;
3154 if (unlikely(list_empty(qtd_list
)))
3157 qtd
= list_entry(qtd_list
->next
, struct fotg210_qtd
,
3160 /* control qh may need patching ... */
3161 if (unlikely(epnum
== 0)) {
3162 /* usb_reset_device() briefly reverts to address 0 */
3163 if (usb_pipedevice(urb
->pipe
) == 0)
3164 qh
->hw
->hw_info1
&= ~qh_addr_mask
;
3167 /* just one way to queue requests: swap with the dummy qtd.
3168 * only hc or qh_refresh() ever modify the overlay.
3170 if (likely(qtd
!= NULL
)) {
3171 struct fotg210_qtd
*dummy
;
3175 /* to avoid racing the HC, use the dummy td instead of
3176 * the first td of our list (becomes new dummy). both
3177 * tds stay deactivated until we're done, when the
3178 * HC is allowed to fetch the old dummy (4.10.2).
3180 token
= qtd
->hw_token
;
3181 qtd
->hw_token
= HALT_BIT(fotg210
);
3185 dma
= dummy
->qtd_dma
;
3187 dummy
->qtd_dma
= dma
;
3189 list_del(&qtd
->qtd_list
);
3190 list_add(&dummy
->qtd_list
, qtd_list
);
3191 list_splice_tail(qtd_list
, &qh
->qtd_list
);
3193 fotg210_qtd_init(fotg210
, qtd
, qtd
->qtd_dma
);
3196 /* hc must see the new dummy at list end */
3198 qtd
= list_entry(qh
->qtd_list
.prev
,
3199 struct fotg210_qtd
, qtd_list
);
3200 qtd
->hw_next
= QTD_NEXT(fotg210
, dma
);
3202 /* let the hc process these next qtds */
3204 dummy
->hw_token
= token
;
3212 /*-------------------------------------------------------------------------*/
3216 struct fotg210_hcd
*fotg210
,
3218 struct list_head
*qtd_list
,
3222 unsigned long flags
;
3223 struct fotg210_qh
*qh
= NULL
;
3226 epnum
= urb
->ep
->desc
.bEndpointAddress
;
3228 #ifdef FOTG210_URB_TRACE
3230 struct fotg210_qtd
*qtd
;
3231 qtd
= list_entry(qtd_list
->next
, struct fotg210_qtd
, qtd_list
);
3232 fotg210_dbg(fotg210
,
3233 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
3234 __func__
, urb
->dev
->devpath
, urb
,
3235 epnum
& 0x0f, (epnum
& USB_DIR_IN
) ? "in" : "out",
3236 urb
->transfer_buffer_length
,
3237 qtd
, urb
->ep
->hcpriv
);
3241 spin_lock_irqsave(&fotg210
->lock
, flags
);
3242 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210
)))) {
3246 rc
= usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210
), urb
);
3250 qh
= qh_append_tds(fotg210
, urb
, qtd_list
, epnum
, &urb
->ep
->hcpriv
);
3251 if (unlikely(qh
== NULL
)) {
3252 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210
), urb
);
3257 /* Control/bulk operations through TTs don't need scheduling,
3258 * the HC and TT handle it when the TT has a buffer ready.
3260 if (likely(qh
->qh_state
== QH_STATE_IDLE
))
3261 qh_link_async(fotg210
, qh
);
3263 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
3264 if (unlikely(qh
== NULL
))
3265 qtd_list_free(fotg210
, urb
, qtd_list
);
3269 /*-------------------------------------------------------------------------*/
3271 static void single_unlink_async(struct fotg210_hcd
*fotg210
,
3272 struct fotg210_qh
*qh
)
3274 struct fotg210_qh
*prev
;
3276 /* Add to the end of the list of QHs waiting for the next IAAD */
3277 qh
->qh_state
= QH_STATE_UNLINK
;
3278 if (fotg210
->async_unlink
)
3279 fotg210
->async_unlink_last
->unlink_next
= qh
;
3281 fotg210
->async_unlink
= qh
;
3282 fotg210
->async_unlink_last
= qh
;
3284 /* Unlink it from the schedule */
3285 prev
= fotg210
->async
;
3286 while (prev
->qh_next
.qh
!= qh
)
3287 prev
= prev
->qh_next
.qh
;
3289 prev
->hw
->hw_next
= qh
->hw
->hw_next
;
3290 prev
->qh_next
= qh
->qh_next
;
3291 if (fotg210
->qh_scan_next
== qh
)
3292 fotg210
->qh_scan_next
= qh
->qh_next
.qh
;
3295 static void start_iaa_cycle(struct fotg210_hcd
*fotg210
, bool nested
)
3298 * Do nothing if an IAA cycle is already running or
3299 * if one will be started shortly.
3301 if (fotg210
->async_iaa
|| fotg210
->async_unlinking
)
3304 /* Do all the waiting QHs at once */
3305 fotg210
->async_iaa
= fotg210
->async_unlink
;
3306 fotg210
->async_unlink
= NULL
;
3308 /* If the controller isn't running, we don't have to wait for it */
3309 if (unlikely(fotg210
->rh_state
< FOTG210_RH_RUNNING
)) {
3310 if (!nested
) /* Avoid recursion */
3311 end_unlink_async(fotg210
);
3313 /* Otherwise start a new IAA cycle */
3314 } else if (likely(fotg210
->rh_state
== FOTG210_RH_RUNNING
)) {
3315 /* Make sure the unlinks are all visible to the hardware */
3318 fotg210_writel(fotg210
, fotg210
->command
| CMD_IAAD
,
3319 &fotg210
->regs
->command
);
3320 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
3321 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_IAA_WATCHDOG
,
3326 /* the async qh for the qtds being unlinked are now gone from the HC */
3328 static void end_unlink_async(struct fotg210_hcd
*fotg210
)
3330 struct fotg210_qh
*qh
;
3332 /* Process the idle QHs */
3334 fotg210
->async_unlinking
= true;
3335 while (fotg210
->async_iaa
) {
3336 qh
= fotg210
->async_iaa
;
3337 fotg210
->async_iaa
= qh
->unlink_next
;
3338 qh
->unlink_next
= NULL
;
3340 qh
->qh_state
= QH_STATE_IDLE
;
3341 qh
->qh_next
.qh
= NULL
;
3343 qh_completions(fotg210
, qh
);
3344 if (!list_empty(&qh
->qtd_list
) &&
3345 fotg210
->rh_state
== FOTG210_RH_RUNNING
)
3346 qh_link_async(fotg210
, qh
);
3347 disable_async(fotg210
);
3349 fotg210
->async_unlinking
= false;
3351 /* Start a new IAA cycle if any QHs are waiting for it */
3352 if (fotg210
->async_unlink
) {
3353 start_iaa_cycle(fotg210
, true);
3354 if (unlikely(fotg210
->rh_state
< FOTG210_RH_RUNNING
))
3359 static void unlink_empty_async(struct fotg210_hcd
*fotg210
)
3361 struct fotg210_qh
*qh
, *next
;
3362 bool stopped
= (fotg210
->rh_state
< FOTG210_RH_RUNNING
);
3363 bool check_unlinks_later
= false;
3365 /* Unlink all the async QHs that have been empty for a timer cycle */
3366 next
= fotg210
->async
->qh_next
.qh
;
3369 next
= qh
->qh_next
.qh
;
3371 if (list_empty(&qh
->qtd_list
) &&
3372 qh
->qh_state
== QH_STATE_LINKED
) {
3373 if (!stopped
&& qh
->unlink_cycle
==
3374 fotg210
->async_unlink_cycle
)
3375 check_unlinks_later
= true;
3377 single_unlink_async(fotg210
, qh
);
3381 /* Start a new IAA cycle if any QHs are waiting for it */
3382 if (fotg210
->async_unlink
)
3383 start_iaa_cycle(fotg210
, false);
3385 /* QHs that haven't been empty for long enough will be handled later */
3386 if (check_unlinks_later
) {
3387 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_ASYNC_UNLINKS
,
3389 ++fotg210
->async_unlink_cycle
;
3393 /* makes sure the async qh will become idle */
3394 /* caller must own fotg210->lock */
3396 static void start_unlink_async(struct fotg210_hcd
*fotg210
,
3397 struct fotg210_qh
*qh
)
3400 * If the QH isn't linked then there's nothing we can do
3401 * unless we were called during a giveback, in which case
3402 * qh_completions() has to deal with it.
3404 if (qh
->qh_state
!= QH_STATE_LINKED
) {
3405 if (qh
->qh_state
== QH_STATE_COMPLETING
)
3406 qh
->needs_rescan
= 1;
3410 single_unlink_async(fotg210
, qh
);
3411 start_iaa_cycle(fotg210
, false);
3414 /*-------------------------------------------------------------------------*/
3416 static void scan_async(struct fotg210_hcd
*fotg210
)
3418 struct fotg210_qh
*qh
;
3419 bool check_unlinks_later
= false;
3421 fotg210
->qh_scan_next
= fotg210
->async
->qh_next
.qh
;
3422 while (fotg210
->qh_scan_next
) {
3423 qh
= fotg210
->qh_scan_next
;
3424 fotg210
->qh_scan_next
= qh
->qh_next
.qh
;
3426 /* clean any finished work for this qh */
3427 if (!list_empty(&qh
->qtd_list
)) {
3431 * Unlinks could happen here; completion reporting
3432 * drops the lock. That's why fotg210->qh_scan_next
3433 * always holds the next qh to scan; if the next qh
3434 * gets unlinked then fotg210->qh_scan_next is adjusted
3435 * in single_unlink_async().
3437 temp
= qh_completions(fotg210
, qh
);
3438 if (qh
->needs_rescan
) {
3439 start_unlink_async(fotg210
, qh
);
3440 } else if (list_empty(&qh
->qtd_list
)
3441 && qh
->qh_state
== QH_STATE_LINKED
) {
3442 qh
->unlink_cycle
= fotg210
->async_unlink_cycle
;
3443 check_unlinks_later
= true;
3444 } else if (temp
!= 0)
3450 * Unlink empty entries, reducing DMA usage as well
3451 * as HCD schedule-scanning costs. Delay for any qh
3452 * we just scanned, there's a not-unusual case that it
3453 * doesn't stay idle for long.
3455 if (check_unlinks_later
&& fotg210
->rh_state
== FOTG210_RH_RUNNING
&&
3456 !(fotg210
->enabled_hrtimer_events
&
3457 BIT(FOTG210_HRTIMER_ASYNC_UNLINKS
))) {
3458 fotg210_enable_event(fotg210
,
3459 FOTG210_HRTIMER_ASYNC_UNLINKS
, true);
3460 ++fotg210
->async_unlink_cycle
;
3463 /*-------------------------------------------------------------------------*/
3465 * EHCI scheduled transaction support: interrupt, iso, split iso
3466 * These are called "periodic" transactions in the EHCI spec.
3468 * Note that for interrupt transfers, the QH/QTD manipulation is shared
3469 * with the "asynchronous" transaction support (control/bulk transfers).
3470 * The only real difference is in how interrupt transfers are scheduled.
3472 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
3473 * It keeps track of every ITD (or SITD) that's linked, and holds enough
3474 * pre-calculated schedule data to make appending to the queue be quick.
3477 static int fotg210_get_frame(struct usb_hcd
*hcd
);
3479 /*-------------------------------------------------------------------------*/
3482 * periodic_next_shadow - return "next" pointer on shadow list
3483 * @periodic: host pointer to qh/itd
3484 * @tag: hardware tag for type of this record
3486 static union fotg210_shadow
*
3487 periodic_next_shadow(struct fotg210_hcd
*fotg210
,
3488 union fotg210_shadow
*periodic
, __hc32 tag
)
3490 switch (hc32_to_cpu(fotg210
, tag
)) {
3492 return &periodic
->qh
->qh_next
;
3494 return &periodic
->fstn
->fstn_next
;
3496 return &periodic
->itd
->itd_next
;
3501 shadow_next_periodic(struct fotg210_hcd
*fotg210
,
3502 union fotg210_shadow
*periodic
, __hc32 tag
)
3504 switch (hc32_to_cpu(fotg210
, tag
)) {
3505 /* our fotg210_shadow.qh is actually software part */
3507 return &periodic
->qh
->hw
->hw_next
;
3508 /* others are hw parts */
3510 return periodic
->hw_next
;
3514 /* caller must hold fotg210->lock */
3515 static void periodic_unlink(struct fotg210_hcd
*fotg210
, unsigned frame
,
3518 union fotg210_shadow
*prev_p
= &fotg210
->pshadow
[frame
];
3519 __hc32
*hw_p
= &fotg210
->periodic
[frame
];
3520 union fotg210_shadow here
= *prev_p
;
3522 /* find predecessor of "ptr"; hw and shadow lists are in sync */
3523 while (here
.ptr
&& here
.ptr
!= ptr
) {
3524 prev_p
= periodic_next_shadow(fotg210
, prev_p
,
3525 Q_NEXT_TYPE(fotg210
, *hw_p
));
3526 hw_p
= shadow_next_periodic(fotg210
, &here
,
3527 Q_NEXT_TYPE(fotg210
, *hw_p
));
3530 /* an interrupt entry (at list end) could have been shared */
3534 /* update shadow and hardware lists ... the old "next" pointers
3535 * from ptr may still be in use, the caller updates them.
3537 *prev_p
= *periodic_next_shadow(fotg210
, &here
,
3538 Q_NEXT_TYPE(fotg210
, *hw_p
));
3540 *hw_p
= *shadow_next_periodic(fotg210
, &here
,
3541 Q_NEXT_TYPE(fotg210
, *hw_p
));
3544 /* how many of the uframe's 125 usecs are allocated? */
3545 static unsigned short
3546 periodic_usecs(struct fotg210_hcd
*fotg210
, unsigned frame
, unsigned uframe
)
3548 __hc32
*hw_p
= &fotg210
->periodic
[frame
];
3549 union fotg210_shadow
*q
= &fotg210
->pshadow
[frame
];
3551 struct fotg210_qh_hw
*hw
;
3554 switch (hc32_to_cpu(fotg210
, Q_NEXT_TYPE(fotg210
, *hw_p
))) {
3557 /* is it in the S-mask? */
3558 if (hw
->hw_info2
& cpu_to_hc32(fotg210
, 1 << uframe
))
3559 usecs
+= q
->qh
->usecs
;
3560 /* ... or C-mask? */
3561 if (hw
->hw_info2
& cpu_to_hc32(fotg210
,
3563 usecs
+= q
->qh
->c_usecs
;
3564 hw_p
= &hw
->hw_next
;
3565 q
= &q
->qh
->qh_next
;
3567 /* case Q_TYPE_FSTN: */
3569 /* for "save place" FSTNs, count the relevant INTR
3570 * bandwidth from the previous frame
3572 if (q
->fstn
->hw_prev
!= FOTG210_LIST_END(fotg210
))
3573 fotg210_dbg(fotg210
, "ignoring FSTN cost ...\n");
3575 hw_p
= &q
->fstn
->hw_next
;
3576 q
= &q
->fstn
->fstn_next
;
3579 if (q
->itd
->hw_transaction
[uframe
])
3580 usecs
+= q
->itd
->stream
->usecs
;
3581 hw_p
= &q
->itd
->hw_next
;
3582 q
= &q
->itd
->itd_next
;
3587 if (usecs
> fotg210
->uframe_periodic_max
)
3588 fotg210_err(fotg210
, "uframe %d sched overrun: %d usecs\n",
3589 frame
* 8 + uframe
, usecs
);
3594 /*-------------------------------------------------------------------------*/
3596 static int same_tt(struct usb_device
*dev1
, struct usb_device
*dev2
)
3598 if (!dev1
->tt
|| !dev2
->tt
)
3600 if (dev1
->tt
!= dev2
->tt
)
3602 if (dev1
->tt
->multi
)
3603 return dev1
->ttport
== dev2
->ttport
;
3608 /* return true iff the device's transaction translator is available
3609 * for a periodic transfer starting at the specified frame, using
3610 * all the uframes in the mask.
3612 static int tt_no_collision(
3613 struct fotg210_hcd
*fotg210
,
3615 struct usb_device
*dev
,
3620 if (period
== 0) /* error */
3623 /* note bandwidth wastage: split never follows csplit
3624 * (different dev or endpoint) until the next uframe.
3625 * calling convention doesn't make that distinction.
3627 for (; frame
< fotg210
->periodic_size
; frame
+= period
) {
3628 union fotg210_shadow here
;
3630 struct fotg210_qh_hw
*hw
;
3632 here
= fotg210
->pshadow
[frame
];
3633 type
= Q_NEXT_TYPE(fotg210
, fotg210
->periodic
[frame
]);
3635 switch (hc32_to_cpu(fotg210
, type
)) {
3637 type
= Q_NEXT_TYPE(fotg210
, here
.itd
->hw_next
);
3638 here
= here
.itd
->itd_next
;
3642 if (same_tt(dev
, here
.qh
->dev
)) {
3645 mask
= hc32_to_cpu(fotg210
,
3647 /* "knows" no gap is needed */
3652 type
= Q_NEXT_TYPE(fotg210
, hw
->hw_next
);
3653 here
= here
.qh
->qh_next
;
3655 /* case Q_TYPE_FSTN: */
3657 fotg210_dbg(fotg210
,
3658 "periodic frame %d bogus type %d\n",
3662 /* collision or error */
3671 /*-------------------------------------------------------------------------*/
3673 static void enable_periodic(struct fotg210_hcd
*fotg210
)
3675 if (fotg210
->periodic_count
++)
3678 /* Stop waiting to turn off the periodic schedule */
3679 fotg210
->enabled_hrtimer_events
&=
3680 ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC
);
3682 /* Don't start the schedule until PSS is 0 */
3683 fotg210_poll_PSS(fotg210
);
3684 turn_on_io_watchdog(fotg210
);
3687 static void disable_periodic(struct fotg210_hcd
*fotg210
)
3689 if (--fotg210
->periodic_count
)
3692 /* Don't turn off the schedule until PSS is 1 */
3693 fotg210_poll_PSS(fotg210
);
3696 /*-------------------------------------------------------------------------*/
3698 /* periodic schedule slots have iso tds (normal or split) first, then a
3699 * sparse tree for active interrupt transfers.
3701 * this just links in a qh; caller guarantees uframe masks are set right.
3702 * no FSTN support (yet; fotg210 0.96+)
3704 static void qh_link_periodic(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
3707 unsigned period
= qh
->period
;
3709 dev_dbg(&qh
->dev
->dev
,
3710 "link qh%d-%04x/%p start %d [%d/%d us]\n",
3711 period
, hc32_to_cpup(fotg210
, &qh
->hw
->hw_info2
)
3712 & (QH_CMASK
| QH_SMASK
),
3713 qh
, qh
->start
, qh
->usecs
, qh
->c_usecs
);
3715 /* high bandwidth, or otherwise every microframe */
3719 for (i
= qh
->start
; i
< fotg210
->periodic_size
; i
+= period
) {
3720 union fotg210_shadow
*prev
= &fotg210
->pshadow
[i
];
3721 __hc32
*hw_p
= &fotg210
->periodic
[i
];
3722 union fotg210_shadow here
= *prev
;
3725 /* skip the iso nodes at list head */
3727 type
= Q_NEXT_TYPE(fotg210
, *hw_p
);
3728 if (type
== cpu_to_hc32(fotg210
, Q_TYPE_QH
))
3730 prev
= periodic_next_shadow(fotg210
, prev
, type
);
3731 hw_p
= shadow_next_periodic(fotg210
, &here
, type
);
3735 /* sorting each branch by period (slow-->fast)
3736 * enables sharing interior tree nodes
3738 while (here
.ptr
&& qh
!= here
.qh
) {
3739 if (qh
->period
> here
.qh
->period
)
3741 prev
= &here
.qh
->qh_next
;
3742 hw_p
= &here
.qh
->hw
->hw_next
;
3745 /* link in this qh, unless some earlier pass did that */
3746 if (qh
!= here
.qh
) {
3749 qh
->hw
->hw_next
= *hw_p
;
3752 *hw_p
= QH_NEXT(fotg210
, qh
->qh_dma
);
3755 qh
->qh_state
= QH_STATE_LINKED
;
3758 /* update per-qh bandwidth for usbfs */
3759 fotg210_to_hcd(fotg210
)->self
.bandwidth_allocated
+= qh
->period
3760 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
3763 list_add(&qh
->intr_node
, &fotg210
->intr_qh_list
);
3765 /* maybe enable periodic schedule processing */
3766 ++fotg210
->intr_count
;
3767 enable_periodic(fotg210
);
3770 static void qh_unlink_periodic(struct fotg210_hcd
*fotg210
,
3771 struct fotg210_qh
*qh
)
3777 * If qh is for a low/full-speed device, simply unlinking it
3778 * could interfere with an ongoing split transaction. To unlink
3779 * it safely would require setting the QH_INACTIVATE bit and
3780 * waiting at least one frame, as described in EHCI 4.12.2.5.
3782 * We won't bother with any of this. Instead, we assume that the
3783 * only reason for unlinking an interrupt QH while the current URB
3784 * is still active is to dequeue all the URBs (flush the whole
3787 * If rebalancing the periodic schedule is ever implemented, this
3788 * approach will no longer be valid.
3791 /* high bandwidth, or otherwise part of every microframe */
3792 period
= qh
->period
;
3796 for (i
= qh
->start
; i
< fotg210
->periodic_size
; i
+= period
)
3797 periodic_unlink(fotg210
, i
, qh
);
3799 /* update per-qh bandwidth for usbfs */
3800 fotg210_to_hcd(fotg210
)->self
.bandwidth_allocated
-= qh
->period
3801 ? ((qh
->usecs
+ qh
->c_usecs
) / qh
->period
)
3804 dev_dbg(&qh
->dev
->dev
,
3805 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
3807 hc32_to_cpup(fotg210
, &qh
->hw
->hw_info2
) &
3808 (QH_CMASK
| QH_SMASK
), qh
, qh
->start
, qh
->usecs
, qh
->c_usecs
);
3810 /* qh->qh_next still "live" to HC */
3811 qh
->qh_state
= QH_STATE_UNLINK
;
3812 qh
->qh_next
.ptr
= NULL
;
3814 if (fotg210
->qh_scan_next
== qh
)
3815 fotg210
->qh_scan_next
= list_entry(qh
->intr_node
.next
,
3816 struct fotg210_qh
, intr_node
);
3817 list_del(&qh
->intr_node
);
3820 static void start_unlink_intr(struct fotg210_hcd
*fotg210
,
3821 struct fotg210_qh
*qh
)
3823 /* If the QH isn't linked then there's nothing we can do
3824 * unless we were called during a giveback, in which case
3825 * qh_completions() has to deal with it.
3827 if (qh
->qh_state
!= QH_STATE_LINKED
) {
3828 if (qh
->qh_state
== QH_STATE_COMPLETING
)
3829 qh
->needs_rescan
= 1;
3833 qh_unlink_periodic(fotg210
, qh
);
3835 /* Make sure the unlinks are visible before starting the timer */
3839 * The EHCI spec doesn't say how long it takes the controller to
3840 * stop accessing an unlinked interrupt QH. The timer delay is
3841 * 9 uframes; presumably that will be long enough.
3843 qh
->unlink_cycle
= fotg210
->intr_unlink_cycle
;
3845 /* New entries go at the end of the intr_unlink list */
3846 if (fotg210
->intr_unlink
)
3847 fotg210
->intr_unlink_last
->unlink_next
= qh
;
3849 fotg210
->intr_unlink
= qh
;
3850 fotg210
->intr_unlink_last
= qh
;
3852 if (fotg210
->intr_unlinking
)
3853 ; /* Avoid recursive calls */
3854 else if (fotg210
->rh_state
< FOTG210_RH_RUNNING
)
3855 fotg210_handle_intr_unlinks(fotg210
);
3856 else if (fotg210
->intr_unlink
== qh
) {
3857 fotg210_enable_event(fotg210
, FOTG210_HRTIMER_UNLINK_INTR
,
3859 ++fotg210
->intr_unlink_cycle
;
3863 static void end_unlink_intr(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
3865 struct fotg210_qh_hw
*hw
= qh
->hw
;
3868 qh
->qh_state
= QH_STATE_IDLE
;
3869 hw
->hw_next
= FOTG210_LIST_END(fotg210
);
3871 qh_completions(fotg210
, qh
);
3873 /* reschedule QH iff another request is queued */
3874 if (!list_empty(&qh
->qtd_list
) &&
3875 fotg210
->rh_state
== FOTG210_RH_RUNNING
) {
3876 rc
= qh_schedule(fotg210
, qh
);
3878 /* An error here likely indicates handshake failure
3879 * or no space left in the schedule. Neither fault
3880 * should happen often ...
3882 * FIXME kill the now-dysfunctional queued urbs
3885 fotg210_err(fotg210
, "can't reschedule qh %p, err %d\n",
3889 /* maybe turn off periodic schedule */
3890 --fotg210
->intr_count
;
3891 disable_periodic(fotg210
);
3894 /*-------------------------------------------------------------------------*/
3896 static int check_period(
3897 struct fotg210_hcd
*fotg210
,
3905 /* complete split running into next frame?
3906 * given FSTN support, we could sometimes check...
3911 /* convert "usecs we need" to "max already claimed" */
3912 usecs
= fotg210
->uframe_periodic_max
- usecs
;
3914 /* we "know" 2 and 4 uframe intervals were rejected; so
3915 * for period 0, check _every_ microframe in the schedule.
3917 if (unlikely(period
== 0)) {
3919 for (uframe
= 0; uframe
< 7; uframe
++) {
3920 claimed
= periodic_usecs(fotg210
, frame
,
3922 if (claimed
> usecs
)
3925 } while ((frame
+= 1) < fotg210
->periodic_size
);
3927 /* just check the specified uframe, at that period */
3930 claimed
= periodic_usecs(fotg210
, frame
, uframe
);
3931 if (claimed
> usecs
)
3933 } while ((frame
+= period
) < fotg210
->periodic_size
);
3940 static int check_intr_schedule(
3941 struct fotg210_hcd
*fotg210
,
3944 const struct fotg210_qh
*qh
,
3948 int retval
= -ENOSPC
;
3951 if (qh
->c_usecs
&& uframe
>= 6) /* FSTN territory? */
3954 if (!check_period(fotg210
, frame
, uframe
, qh
->period
, qh
->usecs
))
3962 /* Make sure this tt's buffer is also available for CSPLITs.
3963 * We pessimize a bit; probably the typical full speed case
3964 * doesn't need the second CSPLIT.
3966 * NOTE: both SPLIT and CSPLIT could be checked in just
3969 mask
= 0x03 << (uframe
+ qh
->gap_uf
);
3970 *c_maskp
= cpu_to_hc32(fotg210
, mask
<< 8);
3972 mask
|= 1 << uframe
;
3973 if (tt_no_collision(fotg210
, qh
->period
, qh
->dev
, frame
, mask
)) {
3974 if (!check_period(fotg210
, frame
, uframe
+ qh
->gap_uf
+ 1,
3975 qh
->period
, qh
->c_usecs
))
3977 if (!check_period(fotg210
, frame
, uframe
+ qh
->gap_uf
,
3978 qh
->period
, qh
->c_usecs
))
3986 /* "first fit" scheduling policy used the first time through,
3987 * or when the previous schedule slot can't be re-used.
3989 static int qh_schedule(struct fotg210_hcd
*fotg210
, struct fotg210_qh
*qh
)
3994 unsigned frame
; /* 0..(qh->period - 1), or NO_FRAME */
3995 struct fotg210_qh_hw
*hw
= qh
->hw
;
3997 qh_refresh(fotg210
, qh
);
3998 hw
->hw_next
= FOTG210_LIST_END(fotg210
);
4001 /* reuse the previous schedule slots, if we can */
4002 if (frame
< qh
->period
) {
4003 uframe
= ffs(hc32_to_cpup(fotg210
, &hw
->hw_info2
) & QH_SMASK
);
4004 status
= check_intr_schedule(fotg210
, frame
, --uframe
,
4012 /* else scan the schedule to find a group of slots such that all
4013 * uframes have enough periodic bandwidth available.
4016 /* "normal" case, uframing flexible except with splits */
4020 for (i
= qh
->period
; status
&& i
> 0; --i
) {
4021 frame
= ++fotg210
->random_frame
% qh
->period
;
4022 for (uframe
= 0; uframe
< 8; uframe
++) {
4023 status
= check_intr_schedule(fotg210
,
4031 /* qh->period == 0 means every uframe */
4034 status
= check_intr_schedule(fotg210
, 0, 0, qh
,
4041 /* reset S-frame and (maybe) C-frame masks */
4042 hw
->hw_info2
&= cpu_to_hc32(fotg210
, ~(QH_CMASK
| QH_SMASK
));
4043 hw
->hw_info2
|= qh
->period
4044 ? cpu_to_hc32(fotg210
, 1 << uframe
)
4045 : cpu_to_hc32(fotg210
, QH_SMASK
);
4046 hw
->hw_info2
|= c_mask
;
4048 fotg210_dbg(fotg210
, "reused qh %p schedule\n", qh
);
4050 /* stuff into the periodic schedule */
4051 qh_link_periodic(fotg210
, qh
);
4056 static int intr_submit(
4057 struct fotg210_hcd
*fotg210
,
4059 struct list_head
*qtd_list
,
4063 unsigned long flags
;
4064 struct fotg210_qh
*qh
;
4066 struct list_head empty
;
4068 /* get endpoint and transfer/schedule data */
4069 epnum
= urb
->ep
->desc
.bEndpointAddress
;
4071 spin_lock_irqsave(&fotg210
->lock
, flags
);
4073 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210
)))) {
4074 status
= -ESHUTDOWN
;
4075 goto done_not_linked
;
4077 status
= usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210
), urb
);
4078 if (unlikely(status
))
4079 goto done_not_linked
;
4081 /* get qh and force any scheduling errors */
4082 INIT_LIST_HEAD(&empty
);
4083 qh
= qh_append_tds(fotg210
, urb
, &empty
, epnum
, &urb
->ep
->hcpriv
);
4088 if (qh
->qh_state
== QH_STATE_IDLE
) {
4089 status
= qh_schedule(fotg210
, qh
);
4094 /* then queue the urb's tds to the qh */
4095 qh
= qh_append_tds(fotg210
, urb
, qtd_list
, epnum
, &urb
->ep
->hcpriv
);
4098 /* ... update usbfs periodic stats */
4099 fotg210_to_hcd(fotg210
)->self
.bandwidth_int_reqs
++;
4102 if (unlikely(status
))
4103 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210
), urb
);
4105 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4107 qtd_list_free(fotg210
, urb
, qtd_list
);
4112 static void scan_intr(struct fotg210_hcd
*fotg210
)
4114 struct fotg210_qh
*qh
;
4116 list_for_each_entry_safe(qh
, fotg210
->qh_scan_next
,
4117 &fotg210
->intr_qh_list
, intr_node
) {
4119 /* clean any finished work for this qh */
4120 if (!list_empty(&qh
->qtd_list
)) {
4124 * Unlinks could happen here; completion reporting
4125 * drops the lock. That's why fotg210->qh_scan_next
4126 * always holds the next qh to scan; if the next qh
4127 * gets unlinked then fotg210->qh_scan_next is adjusted
4128 * in qh_unlink_periodic().
4130 temp
= qh_completions(fotg210
, qh
);
4131 if (unlikely(qh
->needs_rescan
||
4132 (list_empty(&qh
->qtd_list
) &&
4133 qh
->qh_state
== QH_STATE_LINKED
)))
4134 start_unlink_intr(fotg210
, qh
);
4141 /*-------------------------------------------------------------------------*/
4143 /* fotg210_iso_stream ops work with both ITD and SITD */
4145 static struct fotg210_iso_stream
*
4146 iso_stream_alloc(gfp_t mem_flags
)
4148 struct fotg210_iso_stream
*stream
;
4150 stream
= kzalloc(sizeof(*stream
), mem_flags
);
4151 if (likely(stream
!= NULL
)) {
4152 INIT_LIST_HEAD(&stream
->td_list
);
4153 INIT_LIST_HEAD(&stream
->free_list
);
4154 stream
->next_uframe
= -1;
4161 struct fotg210_hcd
*fotg210
,
4162 struct fotg210_iso_stream
*stream
,
4163 struct usb_device
*dev
,
4169 unsigned epnum
, maxp
;
4175 * this might be a "high bandwidth" highspeed endpoint,
4176 * as encoded in the ep descriptor's wMaxPacket field
4178 epnum
= usb_pipeendpoint(pipe
);
4179 is_input
= usb_pipein(pipe
) ? USB_DIR_IN
: 0;
4180 maxp
= usb_maxpacket(dev
, pipe
, !is_input
);
4186 maxp
= max_packet(maxp
);
4187 multi
= hb_mult(maxp
);
4191 stream
->buf0
= cpu_to_hc32(fotg210
, (epnum
<< 8) | dev
->devnum
);
4192 stream
->buf1
= cpu_to_hc32(fotg210
, buf1
);
4193 stream
->buf2
= cpu_to_hc32(fotg210
, multi
);
4195 /* usbfs wants to report the average usecs per frame tied up
4196 * when transfers on this endpoint are scheduled ...
4198 if (dev
->speed
== USB_SPEED_FULL
) {
4200 stream
->usecs
= NS_TO_US(usb_calc_bus_time(dev
->speed
,
4201 is_input
, 1, maxp
));
4204 stream
->highspeed
= 1;
4205 stream
->usecs
= HS_USECS_ISO(maxp
);
4207 bandwidth
= stream
->usecs
* 8;
4208 bandwidth
/= interval
;
4210 stream
->bandwidth
= bandwidth
;
4212 stream
->bEndpointAddress
= is_input
| epnum
;
4213 stream
->interval
= interval
;
4214 stream
->maxp
= maxp
;
4217 static struct fotg210_iso_stream
*
4218 iso_stream_find(struct fotg210_hcd
*fotg210
, struct urb
*urb
)
4221 struct fotg210_iso_stream
*stream
;
4222 struct usb_host_endpoint
*ep
;
4223 unsigned long flags
;
4225 epnum
= usb_pipeendpoint(urb
->pipe
);
4226 if (usb_pipein(urb
->pipe
))
4227 ep
= urb
->dev
->ep_in
[epnum
];
4229 ep
= urb
->dev
->ep_out
[epnum
];
4231 spin_lock_irqsave(&fotg210
->lock
, flags
);
4232 stream
= ep
->hcpriv
;
4234 if (unlikely(stream
== NULL
)) {
4235 stream
= iso_stream_alloc(GFP_ATOMIC
);
4236 if (likely(stream
!= NULL
)) {
4237 ep
->hcpriv
= stream
;
4239 iso_stream_init(fotg210
, stream
, urb
->dev
, urb
->pipe
,
4243 /* if dev->ep[epnum] is a QH, hw is set */
4244 } else if (unlikely(stream
->hw
!= NULL
)) {
4245 fotg210_dbg(fotg210
, "dev %s ep%d%s, not iso??\n",
4246 urb
->dev
->devpath
, epnum
,
4247 usb_pipein(urb
->pipe
) ? "in" : "out");
4251 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4255 /*-------------------------------------------------------------------------*/
4257 /* fotg210_iso_sched ops can be ITD-only or SITD-only */
4259 static struct fotg210_iso_sched
*
4260 iso_sched_alloc(unsigned packets
, gfp_t mem_flags
)
4262 struct fotg210_iso_sched
*iso_sched
;
4263 int size
= sizeof(*iso_sched
);
4265 size
+= packets
* sizeof(struct fotg210_iso_packet
);
4266 iso_sched
= kzalloc(size
, mem_flags
);
4267 if (likely(iso_sched
!= NULL
))
4268 INIT_LIST_HEAD(&iso_sched
->td_list
);
4275 struct fotg210_hcd
*fotg210
,
4276 struct fotg210_iso_sched
*iso_sched
,
4277 struct fotg210_iso_stream
*stream
,
4282 dma_addr_t dma
= urb
->transfer_dma
;
4284 /* how many uframes are needed for these transfers */
4285 iso_sched
->span
= urb
->number_of_packets
* stream
->interval
;
4287 /* figure out per-uframe itd fields that we'll need later
4288 * when we fit new itds into the schedule.
4290 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
4291 struct fotg210_iso_packet
*uframe
= &iso_sched
->packet
[i
];
4296 length
= urb
->iso_frame_desc
[i
].length
;
4297 buf
= dma
+ urb
->iso_frame_desc
[i
].offset
;
4299 trans
= FOTG210_ISOC_ACTIVE
;
4300 trans
|= buf
& 0x0fff;
4301 if (unlikely(((i
+ 1) == urb
->number_of_packets
))
4302 && !(urb
->transfer_flags
& URB_NO_INTERRUPT
))
4303 trans
|= FOTG210_ITD_IOC
;
4304 trans
|= length
<< 16;
4305 uframe
->transaction
= cpu_to_hc32(fotg210
, trans
);
4307 /* might need to cross a buffer page within a uframe */
4308 uframe
->bufp
= (buf
& ~(u64
)0x0fff);
4310 if (unlikely((uframe
->bufp
!= (buf
& ~(u64
)0x0fff))))
4317 struct fotg210_iso_stream
*stream
,
4318 struct fotg210_iso_sched
*iso_sched
4323 /* caller must hold fotg210->lock!*/
4324 list_splice(&iso_sched
->td_list
, &stream
->free_list
);
4329 itd_urb_transaction(
4330 struct fotg210_iso_stream
*stream
,
4331 struct fotg210_hcd
*fotg210
,
4336 struct fotg210_itd
*itd
;
4340 struct fotg210_iso_sched
*sched
;
4341 unsigned long flags
;
4343 sched
= iso_sched_alloc(urb
->number_of_packets
, mem_flags
);
4344 if (unlikely(sched
== NULL
))
4347 itd_sched_init(fotg210
, sched
, stream
, urb
);
4349 if (urb
->interval
< 8)
4350 num_itds
= 1 + (sched
->span
+ 7) / 8;
4352 num_itds
= urb
->number_of_packets
;
4354 /* allocate/init ITDs */
4355 spin_lock_irqsave(&fotg210
->lock
, flags
);
4356 for (i
= 0; i
< num_itds
; i
++) {
4359 * Use iTDs from the free list, but not iTDs that may
4360 * still be in use by the hardware.
4362 if (likely(!list_empty(&stream
->free_list
))) {
4363 itd
= list_first_entry(&stream
->free_list
,
4364 struct fotg210_itd
, itd_list
);
4365 if (itd
->frame
== fotg210
->now_frame
)
4367 list_del(&itd
->itd_list
);
4368 itd_dma
= itd
->itd_dma
;
4371 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4372 itd
= dma_pool_alloc(fotg210
->itd_pool
, mem_flags
,
4374 spin_lock_irqsave(&fotg210
->lock
, flags
);
4376 iso_sched_free(stream
, sched
);
4377 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4382 memset(itd
, 0, sizeof(*itd
));
4383 itd
->itd_dma
= itd_dma
;
4384 list_add(&itd
->itd_list
, &sched
->td_list
);
4386 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4388 /* temporarily store schedule info in hcpriv */
4389 urb
->hcpriv
= sched
;
4390 urb
->error_count
= 0;
4394 /*-------------------------------------------------------------------------*/
4398 struct fotg210_hcd
*fotg210
,
4407 /* can't commit more than uframe_periodic_max usec */
4408 if (periodic_usecs(fotg210
, uframe
>> 3, uframe
& 0x7)
4409 > (fotg210
->uframe_periodic_max
- usecs
))
4412 /* we know urb->interval is 2^N uframes */
4414 } while (uframe
< mod
);
4419 * This scheduler plans almost as far into the future as it has actual
4420 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
4421 * "as small as possible" to be cache-friendlier.) That limits the size
4422 * transfers you can stream reliably; avoid more than 64 msec per urb.
4423 * Also avoid queue depths of less than fotg210's worst irq latency (affected
4424 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
4425 * and other factors); or more than about 230 msec total (for portability,
4426 * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
4429 #define SCHEDULE_SLOP 80 /* microframes */
4432 iso_stream_schedule(
4433 struct fotg210_hcd
*fotg210
,
4435 struct fotg210_iso_stream
*stream
4438 u32 now
, next
, start
, period
, span
;
4440 unsigned mod
= fotg210
->periodic_size
<< 3;
4441 struct fotg210_iso_sched
*sched
= urb
->hcpriv
;
4443 period
= urb
->interval
;
4446 if (span
> mod
- SCHEDULE_SLOP
) {
4447 fotg210_dbg(fotg210
, "iso request %p too long\n", urb
);
4452 now
= fotg210_read_frame_index(fotg210
) & (mod
- 1);
4454 /* Typical case: reuse current schedule, stream is still active.
4455 * Hopefully there are no gaps from the host falling behind
4456 * (irq delays etc), but if there are we'll take the next
4457 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
4459 if (likely(!list_empty(&stream
->td_list
))) {
4462 /* For high speed devices, allow scheduling within the
4463 * isochronous scheduling threshold. For full speed devices
4464 * and Intel PCI-based controllers, don't (work around for
4467 if (!stream
->highspeed
&& fotg210
->fs_i_thresh
)
4468 next
= now
+ fotg210
->i_thresh
;
4472 /* Fell behind (by up to twice the slop amount)?
4473 * We decide based on the time of the last currently-scheduled
4474 * slot, not the time of the next available slot.
4476 excess
= (stream
->next_uframe
- period
- next
) & (mod
- 1);
4477 if (excess
>= mod
- 2 * SCHEDULE_SLOP
)
4478 start
= next
+ excess
- mod
+ period
*
4479 DIV_ROUND_UP(mod
- excess
, period
);
4481 start
= next
+ excess
+ period
;
4482 if (start
- now
>= mod
) {
4483 fotg210_dbg(fotg210
, "request %p would overflow (%d+%d >= %d)\n",
4484 urb
, start
- now
- period
, period
,
4491 /* need to schedule; when's the next (u)frame we could start?
4492 * this is bigger than fotg210->i_thresh allows; scheduling itself
4493 * isn't free, the slop should handle reasonably slow cpus. it
4494 * can also help high bandwidth if the dma and irq loads don't
4495 * jump until after the queue is primed.
4499 start
= SCHEDULE_SLOP
+ (now
& ~0x07);
4501 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
4503 /* find a uframe slot with enough bandwidth.
4504 * Early uframes are more precious because full-speed
4505 * iso IN transfers can't use late uframes,
4506 * and therefore they should be allocated last.
4512 /* check schedule: enough space? */
4513 if (itd_slot_ok(fotg210
, mod
, start
,
4514 stream
->usecs
, period
))
4516 } while (start
> next
&& !done
);
4518 /* no room in the schedule */
4520 fotg210_dbg(fotg210
, "iso resched full %p (now %d max %d)\n",
4521 urb
, now
, now
+ mod
);
4527 /* Tried to schedule too far into the future? */
4528 if (unlikely(start
- now
+ span
- period
4529 >= mod
- 2 * SCHEDULE_SLOP
)) {
4530 fotg210_dbg(fotg210
, "request %p would overflow (%d+%d >= %d)\n",
4531 urb
, start
- now
, span
- period
,
4532 mod
- 2 * SCHEDULE_SLOP
);
4537 stream
->next_uframe
= start
& (mod
- 1);
4539 /* report high speed start in uframes; full speed, in frames */
4540 urb
->start_frame
= stream
->next_uframe
;
4541 if (!stream
->highspeed
)
4542 urb
->start_frame
>>= 3;
4544 /* Make sure scan_isoc() sees these */
4545 if (fotg210
->isoc_count
== 0)
4546 fotg210
->next_frame
= now
>> 3;
4550 iso_sched_free(stream
, sched
);
4555 /*-------------------------------------------------------------------------*/
4558 itd_init(struct fotg210_hcd
*fotg210
, struct fotg210_iso_stream
*stream
,
4559 struct fotg210_itd
*itd
)
4563 /* it's been recently zeroed */
4564 itd
->hw_next
= FOTG210_LIST_END(fotg210
);
4565 itd
->hw_bufp
[0] = stream
->buf0
;
4566 itd
->hw_bufp
[1] = stream
->buf1
;
4567 itd
->hw_bufp
[2] = stream
->buf2
;
4569 for (i
= 0; i
< 8; i
++)
4572 /* All other fields are filled when scheduling */
4577 struct fotg210_hcd
*fotg210
,
4578 struct fotg210_itd
*itd
,
4579 struct fotg210_iso_sched
*iso_sched
,
4584 struct fotg210_iso_packet
*uf
= &iso_sched
->packet
[index
];
4585 unsigned pg
= itd
->pg
;
4588 itd
->index
[uframe
] = index
;
4590 itd
->hw_transaction
[uframe
] = uf
->transaction
;
4591 itd
->hw_transaction
[uframe
] |= cpu_to_hc32(fotg210
, pg
<< 12);
4592 itd
->hw_bufp
[pg
] |= cpu_to_hc32(fotg210
, uf
->bufp
& ~(u32
)0);
4593 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(fotg210
, (u32
)(uf
->bufp
>> 32));
4595 /* iso_frame_desc[].offset must be strictly increasing */
4596 if (unlikely(uf
->cross
)) {
4597 u64 bufp
= uf
->bufp
+ 4096;
4600 itd
->hw_bufp
[pg
] |= cpu_to_hc32(fotg210
, bufp
& ~(u32
)0);
4601 itd
->hw_bufp_hi
[pg
] |= cpu_to_hc32(fotg210
, (u32
)(bufp
>> 32));
4606 itd_link(struct fotg210_hcd
*fotg210
, unsigned frame
, struct fotg210_itd
*itd
)
4608 union fotg210_shadow
*prev
= &fotg210
->pshadow
[frame
];
4609 __hc32
*hw_p
= &fotg210
->periodic
[frame
];
4610 union fotg210_shadow here
= *prev
;
4613 /* skip any iso nodes which might belong to previous microframes */
4615 type
= Q_NEXT_TYPE(fotg210
, *hw_p
);
4616 if (type
== cpu_to_hc32(fotg210
, Q_TYPE_QH
))
4618 prev
= periodic_next_shadow(fotg210
, prev
, type
);
4619 hw_p
= shadow_next_periodic(fotg210
, &here
, type
);
4623 itd
->itd_next
= here
;
4624 itd
->hw_next
= *hw_p
;
4628 *hw_p
= cpu_to_hc32(fotg210
, itd
->itd_dma
| Q_TYPE_ITD
);
4631 /* fit urb's itds into the selected schedule slot; activate as needed */
4632 static void itd_link_urb(
4633 struct fotg210_hcd
*fotg210
,
4636 struct fotg210_iso_stream
*stream
4640 unsigned next_uframe
, uframe
, frame
;
4641 struct fotg210_iso_sched
*iso_sched
= urb
->hcpriv
;
4642 struct fotg210_itd
*itd
;
4644 next_uframe
= stream
->next_uframe
& (mod
- 1);
4646 if (unlikely(list_empty(&stream
->td_list
))) {
4647 fotg210_to_hcd(fotg210
)->self
.bandwidth_allocated
4648 += stream
->bandwidth
;
4649 fotg210_vdbg(fotg210
,
4650 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
4651 urb
->dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
4652 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out",
4654 next_uframe
>> 3, next_uframe
& 0x7);
4657 /* fill iTDs uframe by uframe */
4658 for (packet
= 0, itd
= NULL
; packet
< urb
->number_of_packets
;) {
4660 /* ASSERT: we have all necessary itds */
4662 /* ASSERT: no itds for this endpoint in this uframe */
4664 itd
= list_entry(iso_sched
->td_list
.next
,
4665 struct fotg210_itd
, itd_list
);
4666 list_move_tail(&itd
->itd_list
, &stream
->td_list
);
4667 itd
->stream
= stream
;
4669 itd_init(fotg210
, stream
, itd
);
4672 uframe
= next_uframe
& 0x07;
4673 frame
= next_uframe
>> 3;
4675 itd_patch(fotg210
, itd
, iso_sched
, packet
, uframe
);
4677 next_uframe
+= stream
->interval
;
4678 next_uframe
&= mod
- 1;
4681 /* link completed itds into the schedule */
4682 if (((next_uframe
>> 3) != frame
)
4683 || packet
== urb
->number_of_packets
) {
4684 itd_link(fotg210
, frame
& (fotg210
->periodic_size
- 1),
4689 stream
->next_uframe
= next_uframe
;
4691 /* don't need that schedule data any more */
4692 iso_sched_free(stream
, iso_sched
);
4695 ++fotg210
->isoc_count
;
4696 enable_periodic(fotg210
);
4699 #define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
4700 FOTG210_ISOC_XACTERR)
4702 /* Process and recycle a completed ITD. Return true iff its urb completed,
4703 * and hence its completion callback probably added things to the hardware
4706 * Note that we carefully avoid recycling this descriptor until after any
4707 * completion callback runs, so that it won't be reused quickly. That is,
4708 * assuming (a) no more than two urbs per frame on this endpoint, and also
4709 * (b) only this endpoint's completions submit URBs. It seems some silicon
4710 * corrupts things if you reuse completed descriptors very quickly...
4712 static bool itd_complete(struct fotg210_hcd
*fotg210
, struct fotg210_itd
*itd
)
4714 struct urb
*urb
= itd
->urb
;
4715 struct usb_iso_packet_descriptor
*desc
;
4719 struct fotg210_iso_stream
*stream
= itd
->stream
;
4720 struct usb_device
*dev
;
4721 bool retval
= false;
4723 /* for each uframe with a packet */
4724 for (uframe
= 0; uframe
< 8; uframe
++) {
4725 if (likely(itd
->index
[uframe
] == -1))
4727 urb_index
= itd
->index
[uframe
];
4728 desc
= &urb
->iso_frame_desc
[urb_index
];
4730 t
= hc32_to_cpup(fotg210
, &itd
->hw_transaction
[uframe
]);
4731 itd
->hw_transaction
[uframe
] = 0;
4733 /* report transfer status */
4734 if (unlikely(t
& ISO_ERRS
)) {
4736 if (t
& FOTG210_ISOC_BUF_ERR
)
4737 desc
->status
= usb_pipein(urb
->pipe
)
4738 ? -ENOSR
/* hc couldn't read */
4739 : -ECOMM
; /* hc couldn't write */
4740 else if (t
& FOTG210_ISOC_BABBLE
)
4741 desc
->status
= -EOVERFLOW
;
4742 else /* (t & FOTG210_ISOC_XACTERR) */
4743 desc
->status
= -EPROTO
;
4745 /* HC need not update length with this error */
4746 if (!(t
& FOTG210_ISOC_BABBLE
)) {
4747 desc
->actual_length
=
4748 fotg210_itdlen(urb
, desc
, t
);
4749 urb
->actual_length
+= desc
->actual_length
;
4751 } else if (likely((t
& FOTG210_ISOC_ACTIVE
) == 0)) {
4753 desc
->actual_length
= fotg210_itdlen(urb
, desc
, t
);
4754 urb
->actual_length
+= desc
->actual_length
;
4756 /* URB was too late */
4757 desc
->status
= -EXDEV
;
4761 /* handle completion now? */
4762 if (likely((urb_index
+ 1) != urb
->number_of_packets
))
4765 /* ASSERT: it's really the last itd for this urb
4766 list_for_each_entry (itd, &stream->td_list, itd_list)
4767 BUG_ON (itd->urb == urb);
4770 /* give urb back to the driver; completion often (re)submits */
4772 fotg210_urb_done(fotg210
, urb
, 0);
4776 --fotg210
->isoc_count
;
4777 disable_periodic(fotg210
);
4779 if (unlikely(list_is_singular(&stream
->td_list
))) {
4780 fotg210_to_hcd(fotg210
)->self
.bandwidth_allocated
4781 -= stream
->bandwidth
;
4782 fotg210_vdbg(fotg210
,
4783 "deschedule devp %s ep%d%s-iso\n",
4784 dev
->devpath
, stream
->bEndpointAddress
& 0x0f,
4785 (stream
->bEndpointAddress
& USB_DIR_IN
) ? "in" : "out");
4791 /* Add to the end of the free list for later reuse */
4792 list_move_tail(&itd
->itd_list
, &stream
->free_list
);
4794 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
4795 if (list_empty(&stream
->td_list
)) {
4796 list_splice_tail_init(&stream
->free_list
,
4797 &fotg210
->cached_itd_list
);
4798 start_free_itds(fotg210
);
4804 /*-------------------------------------------------------------------------*/
4806 static int itd_submit(struct fotg210_hcd
*fotg210
, struct urb
*urb
,
4809 int status
= -EINVAL
;
4810 unsigned long flags
;
4811 struct fotg210_iso_stream
*stream
;
4813 /* Get iso_stream head */
4814 stream
= iso_stream_find(fotg210
, urb
);
4815 if (unlikely(stream
== NULL
)) {
4816 fotg210_dbg(fotg210
, "can't get iso stream\n");
4819 if (unlikely(urb
->interval
!= stream
->interval
&&
4820 fotg210_port_speed(fotg210
, 0) ==
4821 USB_PORT_STAT_HIGH_SPEED
)) {
4822 fotg210_dbg(fotg210
, "can't change iso interval %d --> %d\n",
4823 stream
->interval
, urb
->interval
);
4827 #ifdef FOTG210_URB_TRACE
4828 fotg210_dbg(fotg210
,
4829 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
4830 __func__
, urb
->dev
->devpath
, urb
,
4831 usb_pipeendpoint(urb
->pipe
),
4832 usb_pipein(urb
->pipe
) ? "in" : "out",
4833 urb
->transfer_buffer_length
,
4834 urb
->number_of_packets
, urb
->interval
,
4838 /* allocate ITDs w/o locking anything */
4839 status
= itd_urb_transaction(stream
, fotg210
, urb
, mem_flags
);
4840 if (unlikely(status
< 0)) {
4841 fotg210_dbg(fotg210
, "can't init itds\n");
4845 /* schedule ... need to lock */
4846 spin_lock_irqsave(&fotg210
->lock
, flags
);
4847 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210
)))) {
4848 status
= -ESHUTDOWN
;
4849 goto done_not_linked
;
4851 status
= usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210
), urb
);
4852 if (unlikely(status
))
4853 goto done_not_linked
;
4854 status
= iso_stream_schedule(fotg210
, urb
, stream
);
4855 if (likely(status
== 0))
4856 itd_link_urb(fotg210
, urb
, fotg210
->periodic_size
<< 3, stream
);
4858 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210
), urb
);
4860 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
4865 /*-------------------------------------------------------------------------*/
4867 static void scan_isoc(struct fotg210_hcd
*fotg210
)
4869 unsigned uf
, now_frame
, frame
;
4870 unsigned fmask
= fotg210
->periodic_size
- 1;
4871 bool modified
, live
;
4874 * When running, scan from last scan point up to "now"
4875 * else clean up by scanning everything that's left.
4876 * Touches as few pages as possible: cache-friendly.
4878 if (fotg210
->rh_state
>= FOTG210_RH_RUNNING
) {
4879 uf
= fotg210_read_frame_index(fotg210
);
4880 now_frame
= (uf
>> 3) & fmask
;
4883 now_frame
= (fotg210
->next_frame
- 1) & fmask
;
4886 fotg210
->now_frame
= now_frame
;
4888 frame
= fotg210
->next_frame
;
4890 union fotg210_shadow q
, *q_p
;
4894 /* scan each element in frame's queue for completions */
4895 q_p
= &fotg210
->pshadow
[frame
];
4896 hw_p
= &fotg210
->periodic
[frame
];
4898 type
= Q_NEXT_TYPE(fotg210
, *hw_p
);
4901 while (q
.ptr
!= NULL
) {
4902 switch (hc32_to_cpu(fotg210
, type
)) {
4904 /* If this ITD is still active, leave it for
4905 * later processing ... check the next entry.
4906 * No need to check for activity unless the
4909 if (frame
== now_frame
&& live
) {
4911 for (uf
= 0; uf
< 8; uf
++) {
4912 if (q
.itd
->hw_transaction
[uf
] &
4913 ITD_ACTIVE(fotg210
))
4917 q_p
= &q
.itd
->itd_next
;
4918 hw_p
= &q
.itd
->hw_next
;
4919 type
= Q_NEXT_TYPE(fotg210
,
4926 /* Take finished ITDs out of the schedule
4927 * and process them: recycle, maybe report
4928 * URB completion. HC won't cache the
4929 * pointer for much longer, if at all.
4931 *q_p
= q
.itd
->itd_next
;
4932 *hw_p
= q
.itd
->hw_next
;
4933 type
= Q_NEXT_TYPE(fotg210
, q
.itd
->hw_next
);
4935 modified
= itd_complete(fotg210
, q
.itd
);
4939 fotg210_dbg(fotg210
, "corrupt type %d frame %d shadow %p\n",
4940 type
, frame
, q
.ptr
);
4944 /* End of the iTDs and siTDs */
4949 /* assume completion callbacks modify the queue */
4950 if (unlikely(modified
&& fotg210
->isoc_count
> 0))
4954 /* Stop when we have reached the current frame */
4955 if (frame
== now_frame
)
4957 frame
= (frame
+ 1) & fmask
;
4959 fotg210
->next_frame
= now_frame
;
4961 /*-------------------------------------------------------------------------*/
4963 * Display / Set uframe_periodic_max
4965 static ssize_t
show_uframe_periodic_max(struct device
*dev
,
4966 struct device_attribute
*attr
,
4969 struct fotg210_hcd
*fotg210
;
4972 fotg210
= hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev
)));
4973 n
= scnprintf(buf
, PAGE_SIZE
, "%d\n", fotg210
->uframe_periodic_max
);
4978 static ssize_t
store_uframe_periodic_max(struct device
*dev
,
4979 struct device_attribute
*attr
,
4980 const char *buf
, size_t count
)
4982 struct fotg210_hcd
*fotg210
;
4983 unsigned uframe_periodic_max
;
4984 unsigned frame
, uframe
;
4985 unsigned short allocated_max
;
4986 unsigned long flags
;
4989 fotg210
= hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev
)));
4990 if (kstrtouint(buf
, 0, &uframe_periodic_max
) < 0)
4993 if (uframe_periodic_max
< 100 || uframe_periodic_max
>= 125) {
4994 fotg210_info(fotg210
, "rejecting invalid request for uframe_periodic_max=%u\n",
4995 uframe_periodic_max
);
5002 * lock, so that our checking does not race with possible periodic
5003 * bandwidth allocation through submitting new urbs.
5005 spin_lock_irqsave(&fotg210
->lock
, flags
);
5008 * for request to decrease max periodic bandwidth, we have to check
5009 * every microframe in the schedule to see whether the decrease is
5012 if (uframe_periodic_max
< fotg210
->uframe_periodic_max
) {
5015 for (frame
= 0; frame
< fotg210
->periodic_size
; ++frame
)
5016 for (uframe
= 0; uframe
< 7; ++uframe
)
5017 allocated_max
= max(allocated_max
,
5018 periodic_usecs(fotg210
, frame
, uframe
));
5020 if (allocated_max
> uframe_periodic_max
) {
5021 fotg210_info(fotg210
,
5022 "cannot decrease uframe_periodic_max becase "
5023 "periodic bandwidth is already allocated "
5025 allocated_max
, uframe_periodic_max
);
5030 /* increasing is always ok */
5032 fotg210_info(fotg210
, "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
5033 100 * uframe_periodic_max
/125, uframe_periodic_max
);
5035 if (uframe_periodic_max
!= 100)
5036 fotg210_warn(fotg210
, "max periodic bandwidth set is non-standard\n");
5038 fotg210
->uframe_periodic_max
= uframe_periodic_max
;
5042 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5046 static DEVICE_ATTR(uframe_periodic_max
, 0644, show_uframe_periodic_max
,
5047 store_uframe_periodic_max
);
5049 static inline int create_sysfs_files(struct fotg210_hcd
*fotg210
)
5051 struct device
*controller
= fotg210_to_hcd(fotg210
)->self
.controller
;
5057 i
= device_create_file(controller
, &dev_attr_uframe_periodic_max
);
5062 static inline void remove_sysfs_files(struct fotg210_hcd
*fotg210
)
5064 struct device
*controller
= fotg210_to_hcd(fotg210
)->self
.controller
;
5066 device_remove_file(controller
, &dev_attr_uframe_periodic_max
);
5068 /*-------------------------------------------------------------------------*/
5070 /* On some systems, leaving remote wakeup enabled prevents system shutdown.
5071 * The firmware seems to think that powering off is a wakeup event!
5072 * This routine turns off remote wakeup and everything else, on all ports.
5074 static void fotg210_turn_off_all_ports(struct fotg210_hcd
*fotg210
)
5076 u32 __iomem
*status_reg
= &fotg210
->regs
->port_status
;
5078 fotg210_writel(fotg210
, PORT_RWC_BITS
, status_reg
);
5082 * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
5083 * Must be called with interrupts enabled and the lock not held.
5085 static void fotg210_silence_controller(struct fotg210_hcd
*fotg210
)
5087 fotg210_halt(fotg210
);
5089 spin_lock_irq(&fotg210
->lock
);
5090 fotg210
->rh_state
= FOTG210_RH_HALTED
;
5091 fotg210_turn_off_all_ports(fotg210
);
5092 spin_unlock_irq(&fotg210
->lock
);
5095 /* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
5096 * This forcibly disables dma and IRQs, helping kexec and other cases
5097 * where the next system software may expect clean state.
5099 static void fotg210_shutdown(struct usb_hcd
*hcd
)
5101 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5103 spin_lock_irq(&fotg210
->lock
);
5104 fotg210
->shutdown
= true;
5105 fotg210
->rh_state
= FOTG210_RH_STOPPING
;
5106 fotg210
->enabled_hrtimer_events
= 0;
5107 spin_unlock_irq(&fotg210
->lock
);
5109 fotg210_silence_controller(fotg210
);
5111 hrtimer_cancel(&fotg210
->hrtimer
);
5114 /*-------------------------------------------------------------------------*/
5117 * fotg210_work is called from some interrupts, timers, and so on.
5118 * it calls driver completion functions, after dropping fotg210->lock.
5120 static void fotg210_work(struct fotg210_hcd
*fotg210
)
5122 /* another CPU may drop fotg210->lock during a schedule scan while
5123 * it reports urb completions. this flag guards against bogus
5124 * attempts at re-entrant schedule scanning.
5126 if (fotg210
->scanning
) {
5127 fotg210
->need_rescan
= true;
5130 fotg210
->scanning
= true;
5133 fotg210
->need_rescan
= false;
5134 if (fotg210
->async_count
)
5135 scan_async(fotg210
);
5136 if (fotg210
->intr_count
> 0)
5138 if (fotg210
->isoc_count
> 0)
5140 if (fotg210
->need_rescan
)
5142 fotg210
->scanning
= false;
5144 /* the IO watchdog guards against hardware or driver bugs that
5145 * misplace IRQs, and should let us run completely without IRQs.
5146 * such lossage has been observed on both VT6202 and VT8235.
5148 turn_on_io_watchdog(fotg210
);
5152 * Called when the fotg210_hcd module is removed.
5154 static void fotg210_stop(struct usb_hcd
*hcd
)
5156 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5158 fotg210_dbg(fotg210
, "stop\n");
5160 /* no more interrupts ... */
5162 spin_lock_irq(&fotg210
->lock
);
5163 fotg210
->enabled_hrtimer_events
= 0;
5164 spin_unlock_irq(&fotg210
->lock
);
5166 fotg210_quiesce(fotg210
);
5167 fotg210_silence_controller(fotg210
);
5168 fotg210_reset(fotg210
);
5170 hrtimer_cancel(&fotg210
->hrtimer
);
5171 remove_sysfs_files(fotg210
);
5172 remove_debug_files(fotg210
);
5174 /* root hub is shut down separately (first, when possible) */
5175 spin_lock_irq(&fotg210
->lock
);
5176 end_free_itds(fotg210
);
5177 spin_unlock_irq(&fotg210
->lock
);
5178 fotg210_mem_cleanup(fotg210
);
5180 #ifdef FOTG210_STATS
5181 fotg210_dbg(fotg210
, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
5182 fotg210
->stats
.normal
, fotg210
->stats
.error
, fotg210
->stats
.iaa
,
5183 fotg210
->stats
.lost_iaa
);
5184 fotg210_dbg(fotg210
, "complete %ld unlink %ld\n",
5185 fotg210
->stats
.complete
, fotg210
->stats
.unlink
);
5188 dbg_status(fotg210
, "fotg210_stop completed",
5189 fotg210_readl(fotg210
, &fotg210
->regs
->status
));
5192 /* one-time init, only for memory state */
5193 static int hcd_fotg210_init(struct usb_hcd
*hcd
)
5195 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5199 struct fotg210_qh_hw
*hw
;
5201 spin_lock_init(&fotg210
->lock
);
5204 * keep io watchdog by default, those good HCDs could turn off it later
5206 fotg210
->need_io_watchdog
= 1;
5208 hrtimer_init(&fotg210
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
5209 fotg210
->hrtimer
.function
= fotg210_hrtimer_func
;
5210 fotg210
->next_hrtimer_event
= FOTG210_HRTIMER_NO_EVENT
;
5212 hcc_params
= fotg210_readl(fotg210
, &fotg210
->caps
->hcc_params
);
5215 * by default set standard 80% (== 100 usec/uframe) max periodic
5216 * bandwidth as required by USB 2.0
5218 fotg210
->uframe_periodic_max
= 100;
5221 * hw default: 1K periodic list heads, one per frame.
5222 * periodic_size can shrink by USBCMD update if hcc_params allows.
5224 fotg210
->periodic_size
= DEFAULT_I_TDPS
;
5225 INIT_LIST_HEAD(&fotg210
->intr_qh_list
);
5226 INIT_LIST_HEAD(&fotg210
->cached_itd_list
);
5228 if (HCC_PGM_FRAMELISTLEN(hcc_params
)) {
5229 /* periodic schedule size can be smaller than default */
5230 switch (FOTG210_TUNE_FLS
) {
5232 fotg210
->periodic_size
= 1024;
5235 fotg210
->periodic_size
= 512;
5238 fotg210
->periodic_size
= 256;
5244 retval
= fotg210_mem_init(fotg210
, GFP_KERNEL
);
5248 /* controllers may cache some of the periodic schedule ... */
5249 fotg210
->i_thresh
= 2;
5252 * dedicate a qh for the async ring head, since we couldn't unlink
5253 * a 'real' qh without stopping the async schedule [4.8]. use it
5254 * as the 'reclamation list head' too.
5255 * its dummy is used in hw_alt_next of many tds, to prevent the qh
5256 * from automatically advancing to the next td after short reads.
5258 fotg210
->async
->qh_next
.qh
= NULL
;
5259 hw
= fotg210
->async
->hw
;
5260 hw
->hw_next
= QH_NEXT(fotg210
, fotg210
->async
->qh_dma
);
5261 hw
->hw_info1
= cpu_to_hc32(fotg210
, QH_HEAD
);
5262 hw
->hw_token
= cpu_to_hc32(fotg210
, QTD_STS_HALT
);
5263 hw
->hw_qtd_next
= FOTG210_LIST_END(fotg210
);
5264 fotg210
->async
->qh_state
= QH_STATE_LINKED
;
5265 hw
->hw_alt_next
= QTD_NEXT(fotg210
, fotg210
->async
->dummy
->qtd_dma
);
5267 /* clear interrupt enables, set irq latency */
5268 if (log2_irq_thresh
< 0 || log2_irq_thresh
> 6)
5269 log2_irq_thresh
= 0;
5270 temp
= 1 << (16 + log2_irq_thresh
);
5271 if (HCC_CANPARK(hcc_params
)) {
5272 /* HW default park == 3, on hardware that supports it (like
5273 * NVidia and ALI silicon), maximizes throughput on the async
5274 * schedule by avoiding QH fetches between transfers.
5276 * With fast usb storage devices and NForce2, "park" seems to
5277 * make problems: throughput reduction (!), data errors...
5280 park
= min_t(unsigned, park
, 3);
5284 fotg210_dbg(fotg210
, "park %d\n", park
);
5286 if (HCC_PGM_FRAMELISTLEN(hcc_params
)) {
5287 /* periodic schedule size can be smaller than default */
5289 temp
|= (FOTG210_TUNE_FLS
<< 2);
5291 fotg210
->command
= temp
;
5293 /* Accept arbitrarily long scatter-gather lists */
5294 if (!(hcd
->driver
->flags
& HCD_LOCAL_MEM
))
5295 hcd
->self
.sg_tablesize
= ~0;
5299 /* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
5300 static int fotg210_run(struct usb_hcd
*hcd
)
5302 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5306 hcd
->uses_new_polling
= 1;
5308 /* EHCI spec section 4.1 */
5310 fotg210_writel(fotg210
, fotg210
->periodic_dma
,
5311 &fotg210
->regs
->frame_list
);
5312 fotg210_writel(fotg210
, (u32
)fotg210
->async
->qh_dma
,
5313 &fotg210
->regs
->async_next
);
5316 * hcc_params controls whether fotg210->regs->segment must (!!!)
5317 * be used; it constrains QH/ITD/SITD and QTD locations.
5318 * pci_pool consistent memory always uses segment zero.
5319 * streaming mappings for I/O buffers, like pci_map_single(),
5320 * can return segments above 4GB, if the device allows.
5322 * NOTE: the dma mask is visible through dma_supported(), so
5323 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
5324 * Scsi_Host.highmem_io, and so forth. It's readonly to all
5325 * host side drivers though.
5327 hcc_params
= fotg210_readl(fotg210
, &fotg210
->caps
->hcc_params
);
5330 * Philips, Intel, and maybe others need CMD_RUN before the
5331 * root hub will detect new devices (why?); NEC doesn't
5333 fotg210
->command
&= ~(CMD_IAAD
|CMD_PSE
|CMD_ASE
|CMD_RESET
);
5334 fotg210
->command
|= CMD_RUN
;
5335 fotg210_writel(fotg210
, fotg210
->command
, &fotg210
->regs
->command
);
5336 dbg_cmd(fotg210
, "init", fotg210
->command
);
5339 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5340 * are explicitly handed to companion controller(s), so no TT is
5341 * involved with the root hub. (Except where one is integrated,
5342 * and there's no companion controller unless maybe for USB OTG.)
5344 * Turning on the CF flag will transfer ownership of all ports
5345 * from the companions to the EHCI controller. If any of the
5346 * companions are in the middle of a port reset at the time, it
5347 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
5348 * guarantees that no resets are in progress. After we set CF,
5349 * a short delay lets the hardware catch up; new resets shouldn't
5350 * be started before the port switching actions could complete.
5352 down_write(&ehci_cf_port_reset_rwsem
);
5353 fotg210
->rh_state
= FOTG210_RH_RUNNING
;
5354 /* unblock posted writes */
5355 fotg210_readl(fotg210
, &fotg210
->regs
->command
);
5357 up_write(&ehci_cf_port_reset_rwsem
);
5358 fotg210
->last_periodic_enable
= ktime_get_real();
5360 temp
= HC_VERSION(fotg210
,
5361 fotg210_readl(fotg210
, &fotg210
->caps
->hc_capbase
));
5362 fotg210_info(fotg210
,
5363 "USB %x.%x started, EHCI %x.%02x\n",
5364 ((fotg210
->sbrn
& 0xf0)>>4), (fotg210
->sbrn
& 0x0f),
5365 temp
>> 8, temp
& 0xff);
5367 fotg210_writel(fotg210
, INTR_MASK
,
5368 &fotg210
->regs
->intr_enable
); /* Turn On Interrupts */
5370 /* GRR this is run-once init(), being done every time the HC starts.
5371 * So long as they're part of class devices, we can't do it init()
5372 * since the class device isn't created that early.
5374 create_debug_files(fotg210
);
5375 create_sysfs_files(fotg210
);
5380 static int fotg210_setup(struct usb_hcd
*hcd
)
5382 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5385 fotg210
->regs
= (void __iomem
*)fotg210
->caps
+
5387 fotg210_readl(fotg210
, &fotg210
->caps
->hc_capbase
));
5388 dbg_hcs_params(fotg210
, "reset");
5389 dbg_hcc_params(fotg210
, "reset");
5391 /* cache this readonly data; minimize chip reads */
5392 fotg210
->hcs_params
= fotg210_readl(fotg210
,
5393 &fotg210
->caps
->hcs_params
);
5395 fotg210
->sbrn
= HCD_USB2
;
5397 /* data structure init */
5398 retval
= hcd_fotg210_init(hcd
);
5402 retval
= fotg210_halt(fotg210
);
5406 fotg210_reset(fotg210
);
5411 /*-------------------------------------------------------------------------*/
5413 static irqreturn_t
fotg210_irq(struct usb_hcd
*hcd
)
5415 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5416 u32 status
, masked_status
, pcd_status
= 0, cmd
;
5419 spin_lock(&fotg210
->lock
);
5421 status
= fotg210_readl(fotg210
, &fotg210
->regs
->status
);
5423 /* e.g. cardbus physical eject */
5424 if (status
== ~(u32
) 0) {
5425 fotg210_dbg(fotg210
, "device removed\n");
5430 * We don't use STS_FLR, but some controllers don't like it to
5431 * remain on, so mask it out along with the other status bits.
5433 masked_status
= status
& (INTR_MASK
| STS_FLR
);
5436 if (!masked_status
||
5437 unlikely(fotg210
->rh_state
== FOTG210_RH_HALTED
)) {
5438 spin_unlock(&fotg210
->lock
);
5442 /* clear (just) interrupts */
5443 fotg210_writel(fotg210
, masked_status
, &fotg210
->regs
->status
);
5444 cmd
= fotg210_readl(fotg210
, &fotg210
->regs
->command
);
5447 #ifdef VERBOSE_DEBUG
5448 /* unrequested/ignored: Frame List Rollover */
5449 dbg_status(fotg210
, "irq", status
);
5452 /* INT, ERR, and IAA interrupt rates can be throttled */
5454 /* normal [4.15.1.2] or error [4.15.1.1] completion */
5455 if (likely((status
& (STS_INT
|STS_ERR
)) != 0)) {
5456 if (likely((status
& STS_ERR
) == 0))
5457 COUNT(fotg210
->stats
.normal
);
5459 COUNT(fotg210
->stats
.error
);
5463 /* complete the unlinking of some qh [4.15.2.3] */
5464 if (status
& STS_IAA
) {
5466 /* Turn off the IAA watchdog */
5467 fotg210
->enabled_hrtimer_events
&=
5468 ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG
);
5471 * Mild optimization: Allow another IAAD to reset the
5472 * hrtimer, if one occurs before the next expiration.
5473 * In theory we could always cancel the hrtimer, but
5474 * tests show that about half the time it will be reset
5475 * for some other event anyway.
5477 if (fotg210
->next_hrtimer_event
== FOTG210_HRTIMER_IAA_WATCHDOG
)
5478 ++fotg210
->next_hrtimer_event
;
5480 /* guard against (alleged) silicon errata */
5482 fotg210_dbg(fotg210
, "IAA with IAAD still set?\n");
5483 if (fotg210
->async_iaa
) {
5484 COUNT(fotg210
->stats
.iaa
);
5485 end_unlink_async(fotg210
);
5487 fotg210_dbg(fotg210
, "IAA with nothing unlinked?\n");
5490 /* remote wakeup [4.3.1] */
5491 if (status
& STS_PCD
) {
5493 u32 __iomem
*status_reg
= &fotg210
->regs
->port_status
;
5495 /* kick root hub later */
5496 pcd_status
= status
;
5498 /* resume root hub? */
5499 if (fotg210
->rh_state
== FOTG210_RH_SUSPENDED
)
5500 usb_hcd_resume_root_hub(hcd
);
5502 pstatus
= fotg210_readl(fotg210
, status_reg
);
5504 if (test_bit(0, &fotg210
->suspended_ports
) &&
5505 ((pstatus
& PORT_RESUME
) ||
5506 !(pstatus
& PORT_SUSPEND
)) &&
5507 (pstatus
& PORT_PE
) &&
5508 fotg210
->reset_done
[0] == 0) {
5510 /* start 20 msec resume signaling from this port,
5511 * and make khubd collect PORT_STAT_C_SUSPEND to
5512 * stop that signaling. Use 5 ms extra for safety,
5513 * like usb_port_resume() does.
5515 fotg210
->reset_done
[0] = jiffies
+ msecs_to_jiffies(25);
5516 set_bit(0, &fotg210
->resuming_ports
);
5517 fotg210_dbg(fotg210
, "port 1 remote wakeup\n");
5518 mod_timer(&hcd
->rh_timer
, fotg210
->reset_done
[0]);
5522 /* PCI errors [4.15.2.4] */
5523 if (unlikely((status
& STS_FATAL
) != 0)) {
5524 fotg210_err(fotg210
, "fatal error\n");
5525 dbg_cmd(fotg210
, "fatal", cmd
);
5526 dbg_status(fotg210
, "fatal", status
);
5530 /* Don't let the controller do anything more */
5531 fotg210
->shutdown
= true;
5532 fotg210
->rh_state
= FOTG210_RH_STOPPING
;
5533 fotg210
->command
&= ~(CMD_RUN
| CMD_ASE
| CMD_PSE
);
5534 fotg210_writel(fotg210
, fotg210
->command
,
5535 &fotg210
->regs
->command
);
5536 fotg210_writel(fotg210
, 0, &fotg210
->regs
->intr_enable
);
5537 fotg210_handle_controller_death(fotg210
);
5539 /* Handle completions when the controller stops */
5544 fotg210_work(fotg210
);
5545 spin_unlock(&fotg210
->lock
);
5547 usb_hcd_poll_rh_status(hcd
);
5551 /*-------------------------------------------------------------------------*/
5554 * non-error returns are a promise to giveback() the urb later
5555 * we drop ownership so next owner (or urb unlink) can get it
5557 * urb + dev is in hcd.self.controller.urb_list
5558 * we're queueing TDs onto software and hardware lists
5560 * hcd-specific init for hcpriv hasn't been done yet
5562 * NOTE: control, bulk, and interrupt share the same code to append TDs
5563 * to a (possibly active) QH, and the same QH scanning code.
5565 static int fotg210_urb_enqueue(
5566 struct usb_hcd
*hcd
,
5570 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5571 struct list_head qtd_list
;
5573 INIT_LIST_HEAD(&qtd_list
);
5575 switch (usb_pipetype(urb
->pipe
)) {
5577 /* qh_completions() code doesn't handle all the fault cases
5578 * in multi-TD control transfers. Even 1KB is rare anyway.
5580 if (urb
->transfer_buffer_length
> (16 * 1024))
5583 /* case PIPE_BULK: */
5585 if (!qh_urb_transaction(fotg210
, urb
, &qtd_list
, mem_flags
))
5587 return submit_async(fotg210
, urb
, &qtd_list
, mem_flags
);
5589 case PIPE_INTERRUPT
:
5590 if (!qh_urb_transaction(fotg210
, urb
, &qtd_list
, mem_flags
))
5592 return intr_submit(fotg210
, urb
, &qtd_list
, mem_flags
);
5594 case PIPE_ISOCHRONOUS
:
5595 return itd_submit(fotg210
, urb
, mem_flags
);
5599 /* remove from hardware lists
5600 * completions normally happen asynchronously
5603 static int fotg210_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
5605 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5606 struct fotg210_qh
*qh
;
5607 unsigned long flags
;
5610 spin_lock_irqsave(&fotg210
->lock
, flags
);
5611 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
5615 switch (usb_pipetype(urb
->pipe
)) {
5616 /* case PIPE_CONTROL: */
5617 /* case PIPE_BULK:*/
5619 qh
= (struct fotg210_qh
*) urb
->hcpriv
;
5622 switch (qh
->qh_state
) {
5623 case QH_STATE_LINKED
:
5624 case QH_STATE_COMPLETING
:
5625 start_unlink_async(fotg210
, qh
);
5627 case QH_STATE_UNLINK
:
5628 case QH_STATE_UNLINK_WAIT
:
5629 /* already started */
5632 /* QH might be waiting for a Clear-TT-Buffer */
5633 qh_completions(fotg210
, qh
);
5638 case PIPE_INTERRUPT
:
5639 qh
= (struct fotg210_qh
*) urb
->hcpriv
;
5642 switch (qh
->qh_state
) {
5643 case QH_STATE_LINKED
:
5644 case QH_STATE_COMPLETING
:
5645 start_unlink_intr(fotg210
, qh
);
5648 qh_completions(fotg210
, qh
);
5651 fotg210_dbg(fotg210
, "bogus qh %p state %d\n",
5657 case PIPE_ISOCHRONOUS
:
5660 /* wait till next completion, do it then. */
5661 /* completion irqs can wait up to 1024 msec, */
5665 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5669 /*-------------------------------------------------------------------------*/
5671 /* bulk qh holds the data toggle */
5674 fotg210_endpoint_disable(struct usb_hcd
*hcd
, struct usb_host_endpoint
*ep
)
5676 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5677 unsigned long flags
;
5678 struct fotg210_qh
*qh
, *tmp
;
5680 /* ASSERT: any requests/urbs are being unlinked */
5681 /* ASSERT: nobody can be submitting urbs for this any more */
5684 spin_lock_irqsave(&fotg210
->lock
, flags
);
5689 /* endpoints can be iso streams. for now, we don't
5690 * accelerate iso completions ... so spin a while.
5692 if (qh
->hw
== NULL
) {
5693 struct fotg210_iso_stream
*stream
= ep
->hcpriv
;
5695 if (!list_empty(&stream
->td_list
))
5698 /* BUG_ON(!list_empty(&stream->free_list)); */
5703 if (fotg210
->rh_state
< FOTG210_RH_RUNNING
)
5704 qh
->qh_state
= QH_STATE_IDLE
;
5705 switch (qh
->qh_state
) {
5706 case QH_STATE_LINKED
:
5707 case QH_STATE_COMPLETING
:
5708 for (tmp
= fotg210
->async
->qh_next
.qh
;
5710 tmp
= tmp
->qh_next
.qh
)
5712 /* periodic qh self-unlinks on empty, and a COMPLETING qh
5713 * may already be unlinked.
5716 start_unlink_async(fotg210
, qh
);
5718 case QH_STATE_UNLINK
: /* wait for hw to finish? */
5719 case QH_STATE_UNLINK_WAIT
:
5721 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5722 schedule_timeout_uninterruptible(1);
5724 case QH_STATE_IDLE
: /* fully unlinked */
5725 if (qh
->clearing_tt
)
5727 if (list_empty(&qh
->qtd_list
)) {
5728 qh_destroy(fotg210
, qh
);
5731 /* else FALL THROUGH */
5733 /* caller was supposed to have unlinked any requests;
5734 * that's not our job. just leak this memory.
5736 fotg210_err(fotg210
, "qh %p (#%02x) state %d%s\n",
5737 qh
, ep
->desc
.bEndpointAddress
, qh
->qh_state
,
5738 list_empty(&qh
->qtd_list
) ? "" : "(has tds)");
5743 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5747 fotg210_endpoint_reset(struct usb_hcd
*hcd
, struct usb_host_endpoint
*ep
)
5749 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5750 struct fotg210_qh
*qh
;
5751 int eptype
= usb_endpoint_type(&ep
->desc
);
5752 int epnum
= usb_endpoint_num(&ep
->desc
);
5753 int is_out
= usb_endpoint_dir_out(&ep
->desc
);
5754 unsigned long flags
;
5756 if (eptype
!= USB_ENDPOINT_XFER_BULK
&& eptype
!= USB_ENDPOINT_XFER_INT
)
5759 spin_lock_irqsave(&fotg210
->lock
, flags
);
5762 /* For Bulk and Interrupt endpoints we maintain the toggle state
5763 * in the hardware; the toggle bits in udev aren't used at all.
5764 * When an endpoint is reset by usb_clear_halt() we must reset
5765 * the toggle bit in the QH.
5768 usb_settoggle(qh
->dev
, epnum
, is_out
, 0);
5769 if (!list_empty(&qh
->qtd_list
)) {
5770 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
5771 } else if (qh
->qh_state
== QH_STATE_LINKED
||
5772 qh
->qh_state
== QH_STATE_COMPLETING
) {
5774 /* The toggle value in the QH can't be updated
5775 * while the QH is active. Unlink it now;
5776 * re-linking will call qh_refresh().
5778 if (eptype
== USB_ENDPOINT_XFER_BULK
)
5779 start_unlink_async(fotg210
, qh
);
5781 start_unlink_intr(fotg210
, qh
);
5784 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
5787 static int fotg210_get_frame(struct usb_hcd
*hcd
)
5789 struct fotg210_hcd
*fotg210
= hcd_to_fotg210(hcd
);
5790 return (fotg210_read_frame_index(fotg210
) >> 3) %
5791 fotg210
->periodic_size
;
5794 /*-------------------------------------------------------------------------*/
5797 * The EHCI in ChipIdea HDRC cannot be a separate module or device,
5798 * because its registers (and irq) are shared between host/gadget/otg
5799 * functions and in order to facilitate role switching we cannot
5800 * give the fotg210 driver exclusive access to those.
5802 MODULE_DESCRIPTION(DRIVER_DESC
);
5803 MODULE_AUTHOR(DRIVER_AUTHOR
);
5804 MODULE_LICENSE("GPL");
5806 static const struct hc_driver fotg210_fotg210_hc_driver
= {
5807 .description
= hcd_name
,
5808 .product_desc
= "Faraday USB2.0 Host Controller",
5809 .hcd_priv_size
= sizeof(struct fotg210_hcd
),
5812 * generic hardware linkage
5815 .flags
= HCD_MEMORY
| HCD_USB2
,
5818 * basic lifecycle operations
5820 .reset
= hcd_fotg210_init
,
5821 .start
= fotg210_run
,
5822 .stop
= fotg210_stop
,
5823 .shutdown
= fotg210_shutdown
,
5826 * managing i/o requests and associated device resources
5828 .urb_enqueue
= fotg210_urb_enqueue
,
5829 .urb_dequeue
= fotg210_urb_dequeue
,
5830 .endpoint_disable
= fotg210_endpoint_disable
,
5831 .endpoint_reset
= fotg210_endpoint_reset
,
5834 * scheduling support
5836 .get_frame_number
= fotg210_get_frame
,
5841 .hub_status_data
= fotg210_hub_status_data
,
5842 .hub_control
= fotg210_hub_control
,
5843 .bus_suspend
= fotg210_bus_suspend
,
5844 .bus_resume
= fotg210_bus_resume
,
5846 .relinquish_port
= fotg210_relinquish_port
,
5847 .port_handed_over
= fotg210_port_handed_over
,
5849 .clear_tt_buffer_complete
= fotg210_clear_tt_buffer_complete
,
5852 static void fotg210_init(struct fotg210_hcd
*fotg210
)
5856 iowrite32(GMIR_MDEV_INT
| GMIR_MOTG_INT
| GMIR_INT_POLARITY
,
5857 &fotg210
->regs
->gmir
);
5859 value
= ioread32(&fotg210
->regs
->otgcsr
);
5860 value
&= ~OTGCSR_A_BUS_DROP
;
5861 value
|= OTGCSR_A_BUS_REQ
;
5862 iowrite32(value
, &fotg210
->regs
->otgcsr
);
5866 * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
5868 * Allocates basic resources for this USB host controller, and
5869 * then invokes the start() method for the HCD associated with it
5870 * through the hotplug entry's driver_data.
5872 static int fotg210_hcd_probe(struct platform_device
*pdev
)
5874 struct device
*dev
= &pdev
->dev
;
5875 struct usb_hcd
*hcd
;
5876 struct resource
*res
;
5878 int retval
= -ENODEV
;
5879 struct fotg210_hcd
*fotg210
;
5884 pdev
->dev
.power
.power_state
= PMSG_ON
;
5886 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
5889 "Found HC with no IRQ. Check %s setup!\n",
5896 hcd
= usb_create_hcd(&fotg210_fotg210_hc_driver
, dev
,
5899 dev_err(dev
, "failed to create hcd with err %d\n", retval
);
5901 goto fail_create_hcd
;
5904 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
5907 "Found HC with no register addr. Check %s setup!\n",
5910 goto fail_request_resource
;
5913 hcd
->rsrc_start
= res
->start
;
5914 hcd
->rsrc_len
= resource_size(res
);
5917 if (!request_mem_region(hcd
->rsrc_start
, hcd
->rsrc_len
,
5918 fotg210_fotg210_hc_driver
.description
)) {
5919 dev_dbg(dev
, "controller already in use\n");
5921 goto fail_request_resource
;
5924 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
5927 "Found HC with no register addr. Check %s setup!\n",
5930 goto fail_request_resource
;
5933 hcd
->regs
= ioremap_nocache(res
->start
, resource_size(res
));
5934 if (hcd
->regs
== NULL
) {
5935 dev_dbg(dev
, "error mapping memory\n");
5940 fotg210
= hcd_to_fotg210(hcd
);
5942 fotg210
->caps
= hcd
->regs
;
5944 retval
= fotg210_setup(hcd
);
5948 fotg210_init(fotg210
);
5950 retval
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
5952 dev_err(dev
, "failed to add hcd with err %d\n", retval
);
5961 release_mem_region(hcd
->rsrc_start
, hcd
->rsrc_len
);
5962 fail_request_resource
:
5965 dev_err(dev
, "init %s fail, %d\n", dev_name(dev
), retval
);
5970 * fotg210_hcd_remove - shutdown processing for EHCI HCDs
5971 * @dev: USB Host Controller being removed
5974 static int fotg210_hcd_remove(struct platform_device
*pdev
)
5976 struct device
*dev
= &pdev
->dev
;
5977 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
5982 usb_remove_hcd(hcd
);
5984 release_mem_region(hcd
->rsrc_start
, hcd
->rsrc_len
);
5990 static struct platform_driver fotg210_hcd_driver
= {
5992 .name
= "fotg210-hcd",
5994 .probe
= fotg210_hcd_probe
,
5995 .remove
= fotg210_hcd_remove
,
5998 static int __init
fotg210_hcd_init(void)
6005 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
6006 set_bit(USB_EHCI_LOADED
, &usb_hcds_loaded
);
6007 if (test_bit(USB_UHCI_LOADED
, &usb_hcds_loaded
) ||
6008 test_bit(USB_OHCI_LOADED
, &usb_hcds_loaded
))
6009 pr_warn(KERN_WARNING
"Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
6011 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd\n",
6013 sizeof(struct fotg210_qh
), sizeof(struct fotg210_qtd
),
6014 sizeof(struct fotg210_itd
));
6017 fotg210_debug_root
= debugfs_create_dir("fotg210", usb_debug_root
);
6018 if (!fotg210_debug_root
) {
6024 retval
= platform_driver_register(&fotg210_hcd_driver
);
6029 platform_driver_unregister(&fotg210_hcd_driver
);
6032 debugfs_remove(fotg210_debug_root
);
6033 fotg210_debug_root
= NULL
;
6036 clear_bit(USB_EHCI_LOADED
, &usb_hcds_loaded
);
6039 module_init(fotg210_hcd_init
);
6041 static void __exit
fotg210_hcd_cleanup(void)
6043 platform_driver_unregister(&fotg210_hcd_driver
);
6045 debugfs_remove(fotg210_debug_root
);
6047 clear_bit(USB_EHCI_LOADED
, &usb_hcds_loaded
);
6049 module_exit(fotg210_hcd_cleanup
);