Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / drivers / usb / host / fusbh200-hcd.c
blobe1c6d850a7e1320fa38ffcd518afee2dac46b78b
1 /*
2 * Faraday FUSBH200 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
20 * for more details.
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.
27 #include <linux/module.h>
28 #include <linux/device.h>
29 #include <linux/dmapool.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/ioport.h>
33 #include <linux/sched.h>
34 #include <linux/vmalloc.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/hrtimer.h>
38 #include <linux/list.h>
39 #include <linux/interrupt.h>
40 #include <linux/usb.h>
41 #include <linux/usb/hcd.h>
42 #include <linux/moduleparam.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/debugfs.h>
45 #include <linux/slab.h>
46 #include <linux/uaccess.h>
47 #include <linux/platform_device.h>
49 #include <asm/byteorder.h>
50 #include <asm/io.h>
51 #include <asm/irq.h>
52 #include <asm/unaligned.h>
54 /*-------------------------------------------------------------------------*/
55 #define DRIVER_AUTHOR "Yuan-Hsin Chen"
56 #define DRIVER_DESC "FUSBH200 Host Controller (EHCI) Driver"
58 static const char hcd_name [] = "fusbh200_hcd";
60 #undef VERBOSE_DEBUG
61 #undef FUSBH200_URB_TRACE
63 #ifdef DEBUG
64 #define FUSBH200_STATS
65 #endif
67 /* magic numbers that can affect system performance */
68 #define FUSBH200_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
69 #define FUSBH200_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
70 #define FUSBH200_TUNE_RL_TT 0
71 #define FUSBH200_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
72 #define FUSBH200_TUNE_MULT_TT 1
74 * Some drivers think it's safe to schedule isochronous transfers more than
75 * 256 ms into the future (partly as a result of an old bug in the scheduling
76 * code). In an attempt to avoid trouble, we will use a minimum scheduling
77 * length of 512 frames instead of 256.
79 #define FUSBH200_TUNE_FLS 1 /* (medium) 512-frame schedule */
81 /* Initial IRQ latency: faster than hw default */
82 static int log2_irq_thresh = 0; // 0 to 6
83 module_param (log2_irq_thresh, int, S_IRUGO);
84 MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
86 /* initial park setting: slower than hw default */
87 static unsigned park = 0;
88 module_param (park, uint, S_IRUGO);
89 MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
91 /* for link power management(LPM) feature */
92 static unsigned int hird;
93 module_param(hird, int, S_IRUGO);
94 MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
96 #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
98 #include "fusbh200.h"
100 /*-------------------------------------------------------------------------*/
102 #define fusbh200_dbg(fusbh200, fmt, args...) \
103 dev_dbg (fusbh200_to_hcd(fusbh200)->self.controller , fmt , ## args )
104 #define fusbh200_err(fusbh200, fmt, args...) \
105 dev_err (fusbh200_to_hcd(fusbh200)->self.controller , fmt , ## args )
106 #define fusbh200_info(fusbh200, fmt, args...) \
107 dev_info (fusbh200_to_hcd(fusbh200)->self.controller , fmt , ## args )
108 #define fusbh200_warn(fusbh200, fmt, args...) \
109 dev_warn (fusbh200_to_hcd(fusbh200)->self.controller , fmt , ## args )
111 #ifdef VERBOSE_DEBUG
112 # define fusbh200_vdbg fusbh200_dbg
113 #else
114 static inline void fusbh200_vdbg(struct fusbh200_hcd *fusbh200, ...) {}
115 #endif
117 #ifdef DEBUG
119 /* check the values in the HCSPARAMS register
120 * (host controller _Structural_ parameters)
121 * see EHCI spec, Table 2-4 for each value
123 static void dbg_hcs_params (struct fusbh200_hcd *fusbh200, char *label)
125 u32 params = fusbh200_readl(fusbh200, &fusbh200->caps->hcs_params);
127 fusbh200_dbg (fusbh200,
128 "%s hcs_params 0x%x ports=%d\n",
129 label, params,
130 HCS_N_PORTS (params)
133 #else
135 static inline void dbg_hcs_params (struct fusbh200_hcd *fusbh200, char *label) {}
137 #endif
139 #ifdef DEBUG
141 /* check the values in the HCCPARAMS register
142 * (host controller _Capability_ parameters)
143 * see EHCI Spec, Table 2-5 for each value
144 * */
145 static void dbg_hcc_params (struct fusbh200_hcd *fusbh200, char *label)
147 u32 params = fusbh200_readl(fusbh200, &fusbh200->caps->hcc_params);
149 fusbh200_dbg (fusbh200,
150 "%s hcc_params %04x uframes %s%s\n",
151 label,
152 params,
153 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
154 HCC_CANPARK(params) ? " park" : "");
156 #else
158 static inline void dbg_hcc_params (struct fusbh200_hcd *fusbh200, char *label) {}
160 #endif
162 #ifdef DEBUG
164 static void __maybe_unused
165 dbg_qtd (const char *label, struct fusbh200_hcd *fusbh200, struct fusbh200_qtd *qtd)
167 fusbh200_dbg(fusbh200, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
168 hc32_to_cpup(fusbh200, &qtd->hw_next),
169 hc32_to_cpup(fusbh200, &qtd->hw_alt_next),
170 hc32_to_cpup(fusbh200, &qtd->hw_token),
171 hc32_to_cpup(fusbh200, &qtd->hw_buf [0]));
172 if (qtd->hw_buf [1])
173 fusbh200_dbg(fusbh200, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
174 hc32_to_cpup(fusbh200, &qtd->hw_buf[1]),
175 hc32_to_cpup(fusbh200, &qtd->hw_buf[2]),
176 hc32_to_cpup(fusbh200, &qtd->hw_buf[3]),
177 hc32_to_cpup(fusbh200, &qtd->hw_buf[4]));
180 static void __maybe_unused
181 dbg_qh (const char *label, struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
183 struct fusbh200_qh_hw *hw = qh->hw;
185 fusbh200_dbg (fusbh200, "%s qh %p n%08x info %x %x qtd %x\n", label,
186 qh, hw->hw_next, hw->hw_info1, hw->hw_info2, hw->hw_current);
187 dbg_qtd("overlay", fusbh200, (struct fusbh200_qtd *) &hw->hw_qtd_next);
190 static void __maybe_unused
191 dbg_itd (const char *label, struct fusbh200_hcd *fusbh200, struct fusbh200_itd *itd)
193 fusbh200_dbg (fusbh200, "%s [%d] itd %p, next %08x, urb %p\n",
194 label, itd->frame, itd, hc32_to_cpu(fusbh200, itd->hw_next),
195 itd->urb);
196 fusbh200_dbg (fusbh200,
197 " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
198 hc32_to_cpu(fusbh200, itd->hw_transaction[0]),
199 hc32_to_cpu(fusbh200, itd->hw_transaction[1]),
200 hc32_to_cpu(fusbh200, itd->hw_transaction[2]),
201 hc32_to_cpu(fusbh200, itd->hw_transaction[3]),
202 hc32_to_cpu(fusbh200, itd->hw_transaction[4]),
203 hc32_to_cpu(fusbh200, itd->hw_transaction[5]),
204 hc32_to_cpu(fusbh200, itd->hw_transaction[6]),
205 hc32_to_cpu(fusbh200, itd->hw_transaction[7]));
206 fusbh200_dbg (fusbh200,
207 " buf: %08x %08x %08x %08x %08x %08x %08x\n",
208 hc32_to_cpu(fusbh200, itd->hw_bufp[0]),
209 hc32_to_cpu(fusbh200, itd->hw_bufp[1]),
210 hc32_to_cpu(fusbh200, itd->hw_bufp[2]),
211 hc32_to_cpu(fusbh200, itd->hw_bufp[3]),
212 hc32_to_cpu(fusbh200, itd->hw_bufp[4]),
213 hc32_to_cpu(fusbh200, itd->hw_bufp[5]),
214 hc32_to_cpu(fusbh200, itd->hw_bufp[6]));
215 fusbh200_dbg (fusbh200, " index: %d %d %d %d %d %d %d %d\n",
216 itd->index[0], itd->index[1], itd->index[2],
217 itd->index[3], itd->index[4], itd->index[5],
218 itd->index[6], itd->index[7]);
221 static int __maybe_unused
222 dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
224 return scnprintf (buf, len,
225 "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
226 label, label [0] ? " " : "", status,
227 (status & STS_ASS) ? " Async" : "",
228 (status & STS_PSS) ? " Periodic" : "",
229 (status & STS_RECL) ? " Recl" : "",
230 (status & STS_HALT) ? " Halt" : "",
231 (status & STS_IAA) ? " IAA" : "",
232 (status & STS_FATAL) ? " FATAL" : "",
233 (status & STS_FLR) ? " FLR" : "",
234 (status & STS_PCD) ? " PCD" : "",
235 (status & STS_ERR) ? " ERR" : "",
236 (status & STS_INT) ? " INT" : ""
240 static int __maybe_unused
241 dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
243 return scnprintf (buf, len,
244 "%s%sintrenable %02x%s%s%s%s%s%s",
245 label, label [0] ? " " : "", enable,
246 (enable & STS_IAA) ? " IAA" : "",
247 (enable & STS_FATAL) ? " FATAL" : "",
248 (enable & STS_FLR) ? " FLR" : "",
249 (enable & STS_PCD) ? " PCD" : "",
250 (enable & STS_ERR) ? " ERR" : "",
251 (enable & STS_INT) ? " INT" : ""
255 static const char *const fls_strings [] =
256 { "1024", "512", "256", "??" };
258 static int
259 dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
261 return scnprintf (buf, len,
262 "%s%scommand %07x %s=%d ithresh=%d%s%s%s "
263 "period=%s%s %s",
264 label, label [0] ? " " : "", command,
265 (command & CMD_PARK) ? " park" : "(park)",
266 CMD_PARK_CNT (command),
267 (command >> 16) & 0x3f,
268 (command & CMD_IAAD) ? " IAAD" : "",
269 (command & CMD_ASE) ? " Async" : "",
270 (command & CMD_PSE) ? " Periodic" : "",
271 fls_strings [(command >> 2) & 0x3],
272 (command & CMD_RESET) ? " Reset" : "",
273 (command & CMD_RUN) ? "RUN" : "HALT"
277 static int
278 dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
280 char *sig;
282 /* signaling state */
283 switch (status & (3 << 10)) {
284 case 0 << 10: sig = "se0"; break;
285 case 1 << 10: sig = "k"; break; /* low speed */
286 case 2 << 10: sig = "j"; break;
287 default: sig = "?"; break;
290 return scnprintf (buf, len,
291 "%s%sport:%d status %06x %d "
292 "sig=%s%s%s%s%s%s%s%s",
293 label, label [0] ? " " : "", port, status,
294 status>>25,/*device address */
295 sig,
296 (status & PORT_RESET) ? " RESET" : "",
297 (status & PORT_SUSPEND) ? " SUSPEND" : "",
298 (status & PORT_RESUME) ? " RESUME" : "",
299 (status & PORT_PEC) ? " PEC" : "",
300 (status & PORT_PE) ? " PE" : "",
301 (status & PORT_CSC) ? " CSC" : "",
302 (status & PORT_CONNECT) ? " CONNECT" : "");
305 #else
306 static inline void __maybe_unused
307 dbg_qh (char *label, struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
310 static inline int __maybe_unused
311 dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
312 { return 0; }
314 static inline int __maybe_unused
315 dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
316 { return 0; }
318 static inline int __maybe_unused
319 dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
320 { return 0; }
322 static inline int __maybe_unused
323 dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
324 { return 0; }
326 #endif /* DEBUG */
328 /* functions have the "wrong" filename when they're output... */
329 #define dbg_status(fusbh200, label, status) { \
330 char _buf [80]; \
331 dbg_status_buf (_buf, sizeof _buf, label, status); \
332 fusbh200_dbg (fusbh200, "%s\n", _buf); \
335 #define dbg_cmd(fusbh200, label, command) { \
336 char _buf [80]; \
337 dbg_command_buf (_buf, sizeof _buf, label, command); \
338 fusbh200_dbg (fusbh200, "%s\n", _buf); \
341 #define dbg_port(fusbh200, label, port, status) { \
342 char _buf [80]; \
343 dbg_port_buf (_buf, sizeof _buf, label, port, status); \
344 fusbh200_dbg (fusbh200, "%s\n", _buf); \
347 /*-------------------------------------------------------------------------*/
349 #ifdef STUB_DEBUG_FILES
351 static inline void create_debug_files (struct fusbh200_hcd *bus) { }
352 static inline void remove_debug_files (struct fusbh200_hcd *bus) { }
354 #else
356 /* troubleshooting help: expose state in debugfs */
358 static int debug_async_open(struct inode *, struct file *);
359 static int debug_periodic_open(struct inode *, struct file *);
360 static int debug_registers_open(struct inode *, struct file *);
361 static int debug_async_open(struct inode *, struct file *);
363 static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
364 static int debug_close(struct inode *, struct file *);
366 static const struct file_operations debug_async_fops = {
367 .owner = THIS_MODULE,
368 .open = debug_async_open,
369 .read = debug_output,
370 .release = debug_close,
371 .llseek = default_llseek,
373 static const struct file_operations debug_periodic_fops = {
374 .owner = THIS_MODULE,
375 .open = debug_periodic_open,
376 .read = debug_output,
377 .release = debug_close,
378 .llseek = default_llseek,
380 static const struct file_operations debug_registers_fops = {
381 .owner = THIS_MODULE,
382 .open = debug_registers_open,
383 .read = debug_output,
384 .release = debug_close,
385 .llseek = default_llseek,
388 static struct dentry *fusbh200_debug_root;
390 struct debug_buffer {
391 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
392 struct usb_bus *bus;
393 struct mutex mutex; /* protect filling of buffer */
394 size_t count; /* number of characters filled into buffer */
395 char *output_buf;
396 size_t alloc_size;
399 #define speed_char(info1) ({ char tmp; \
400 switch (info1 & (3 << 12)) { \
401 case QH_FULL_SPEED: tmp = 'f'; break; \
402 case QH_LOW_SPEED: tmp = 'l'; break; \
403 case QH_HIGH_SPEED: tmp = 'h'; break; \
404 default: tmp = '?'; break; \
405 } tmp; })
407 static inline char token_mark(struct fusbh200_hcd *fusbh200, __hc32 token)
409 __u32 v = hc32_to_cpu(fusbh200, token);
411 if (v & QTD_STS_ACTIVE)
412 return '*';
413 if (v & QTD_STS_HALT)
414 return '-';
415 if (!IS_SHORT_READ (v))
416 return ' ';
417 /* tries to advance through hw_alt_next */
418 return '/';
421 static void qh_lines (
422 struct fusbh200_hcd *fusbh200,
423 struct fusbh200_qh *qh,
424 char **nextp,
425 unsigned *sizep
428 u32 scratch;
429 u32 hw_curr;
430 struct fusbh200_qtd *td;
431 unsigned temp;
432 unsigned size = *sizep;
433 char *next = *nextp;
434 char mark;
435 __le32 list_end = FUSBH200_LIST_END(fusbh200);
436 struct fusbh200_qh_hw *hw = qh->hw;
438 if (hw->hw_qtd_next == list_end) /* NEC does this */
439 mark = '@';
440 else
441 mark = token_mark(fusbh200, hw->hw_token);
442 if (mark == '/') { /* qh_alt_next controls qh advance? */
443 if ((hw->hw_alt_next & QTD_MASK(fusbh200))
444 == fusbh200->async->hw->hw_alt_next)
445 mark = '#'; /* blocked */
446 else if (hw->hw_alt_next == list_end)
447 mark = '.'; /* use hw_qtd_next */
448 /* else alt_next points to some other qtd */
450 scratch = hc32_to_cpup(fusbh200, &hw->hw_info1);
451 hw_curr = (mark == '*') ? hc32_to_cpup(fusbh200, &hw->hw_current) : 0;
452 temp = scnprintf (next, size,
453 "qh/%p dev%d %cs ep%d %08x %08x (%08x%c %s nak%d)",
454 qh, scratch & 0x007f,
455 speed_char (scratch),
456 (scratch >> 8) & 0x000f,
457 scratch, hc32_to_cpup(fusbh200, &hw->hw_info2),
458 hc32_to_cpup(fusbh200, &hw->hw_token), mark,
459 (cpu_to_hc32(fusbh200, QTD_TOGGLE) & hw->hw_token)
460 ? "data1" : "data0",
461 (hc32_to_cpup(fusbh200, &hw->hw_alt_next) >> 1) & 0x0f);
462 size -= temp;
463 next += temp;
465 /* hc may be modifying the list as we read it ... */
466 list_for_each_entry(td, &qh->qtd_list, qtd_list) {
467 scratch = hc32_to_cpup(fusbh200, &td->hw_token);
468 mark = ' ';
469 if (hw_curr == td->qtd_dma)
470 mark = '*';
471 else if (hw->hw_qtd_next == cpu_to_hc32(fusbh200, td->qtd_dma))
472 mark = '+';
473 else if (QTD_LENGTH (scratch)) {
474 if (td->hw_alt_next == fusbh200->async->hw->hw_alt_next)
475 mark = '#';
476 else if (td->hw_alt_next != list_end)
477 mark = '/';
479 temp = snprintf (next, size,
480 "\n\t%p%c%s len=%d %08x urb %p",
481 td, mark, ({ char *tmp;
482 switch ((scratch>>8)&0x03) {
483 case 0: tmp = "out"; break;
484 case 1: tmp = "in"; break;
485 case 2: tmp = "setup"; break;
486 default: tmp = "?"; break;
487 } tmp;}),
488 (scratch >> 16) & 0x7fff,
489 scratch,
490 td->urb);
491 if (size < temp)
492 temp = size;
493 size -= temp;
494 next += temp;
495 if (temp == size)
496 goto done;
499 temp = snprintf (next, size, "\n");
500 if (size < temp)
501 temp = size;
502 size -= temp;
503 next += temp;
505 done:
506 *sizep = size;
507 *nextp = next;
510 static ssize_t fill_async_buffer(struct debug_buffer *buf)
512 struct usb_hcd *hcd;
513 struct fusbh200_hcd *fusbh200;
514 unsigned long flags;
515 unsigned temp, size;
516 char *next;
517 struct fusbh200_qh *qh;
519 hcd = bus_to_hcd(buf->bus);
520 fusbh200 = hcd_to_fusbh200 (hcd);
521 next = buf->output_buf;
522 size = buf->alloc_size;
524 *next = 0;
526 /* dumps a snapshot of the async schedule.
527 * usually empty except for long-term bulk reads, or head.
528 * one QH per line, and TDs we know about
530 spin_lock_irqsave (&fusbh200->lock, flags);
531 for (qh = fusbh200->async->qh_next.qh; size > 0 && qh; qh = qh->qh_next.qh)
532 qh_lines (fusbh200, qh, &next, &size);
533 if (fusbh200->async_unlink && size > 0) {
534 temp = scnprintf(next, size, "\nunlink =\n");
535 size -= temp;
536 next += temp;
538 for (qh = fusbh200->async_unlink; size > 0 && qh;
539 qh = qh->unlink_next)
540 qh_lines (fusbh200, qh, &next, &size);
542 spin_unlock_irqrestore (&fusbh200->lock, flags);
544 return strlen(buf->output_buf);
547 #define DBG_SCHED_LIMIT 64
548 static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
550 struct usb_hcd *hcd;
551 struct fusbh200_hcd *fusbh200;
552 unsigned long flags;
553 union fusbh200_shadow p, *seen;
554 unsigned temp, size, seen_count;
555 char *next;
556 unsigned i;
557 __hc32 tag;
559 if (!(seen = kmalloc (DBG_SCHED_LIMIT * sizeof *seen, GFP_ATOMIC)))
560 return 0;
561 seen_count = 0;
563 hcd = bus_to_hcd(buf->bus);
564 fusbh200 = hcd_to_fusbh200 (hcd);
565 next = buf->output_buf;
566 size = buf->alloc_size;
568 temp = scnprintf (next, size, "size = %d\n", fusbh200->periodic_size);
569 size -= temp;
570 next += temp;
572 /* dump a snapshot of the periodic schedule.
573 * iso changes, interrupt usually doesn't.
575 spin_lock_irqsave (&fusbh200->lock, flags);
576 for (i = 0; i < fusbh200->periodic_size; i++) {
577 p = fusbh200->pshadow [i];
578 if (likely (!p.ptr))
579 continue;
580 tag = Q_NEXT_TYPE(fusbh200, fusbh200->periodic [i]);
582 temp = scnprintf (next, size, "%4d: ", i);
583 size -= temp;
584 next += temp;
586 do {
587 struct fusbh200_qh_hw *hw;
589 switch (hc32_to_cpu(fusbh200, tag)) {
590 case Q_TYPE_QH:
591 hw = p.qh->hw;
592 temp = scnprintf (next, size, " qh%d-%04x/%p",
593 p.qh->period,
594 hc32_to_cpup(fusbh200,
595 &hw->hw_info2)
596 /* uframe masks */
597 & (QH_CMASK | QH_SMASK),
598 p.qh);
599 size -= temp;
600 next += temp;
601 /* don't repeat what follows this qh */
602 for (temp = 0; temp < seen_count; temp++) {
603 if (seen [temp].ptr != p.ptr)
604 continue;
605 if (p.qh->qh_next.ptr) {
606 temp = scnprintf (next, size,
607 " ...");
608 size -= temp;
609 next += temp;
611 break;
613 /* show more info the first time around */
614 if (temp == seen_count) {
615 u32 scratch = hc32_to_cpup(fusbh200,
616 &hw->hw_info1);
617 struct fusbh200_qtd *qtd;
618 char *type = "";
620 /* count tds, get ep direction */
621 temp = 0;
622 list_for_each_entry (qtd,
623 &p.qh->qtd_list,
624 qtd_list) {
625 temp++;
626 switch (0x03 & (hc32_to_cpu(
627 fusbh200,
628 qtd->hw_token) >> 8)) {
629 case 0: type = "out"; continue;
630 case 1: type = "in"; continue;
634 temp = scnprintf (next, size,
635 " (%c%d ep%d%s "
636 "[%d/%d] q%d p%d)",
637 speed_char (scratch),
638 scratch & 0x007f,
639 (scratch >> 8) & 0x000f, type,
640 p.qh->usecs, p.qh->c_usecs,
641 temp,
642 0x7ff & (scratch >> 16));
644 if (seen_count < DBG_SCHED_LIMIT)
645 seen [seen_count++].qh = p.qh;
646 } else
647 temp = 0;
648 tag = Q_NEXT_TYPE(fusbh200, hw->hw_next);
649 p = p.qh->qh_next;
650 break;
651 case Q_TYPE_FSTN:
652 temp = scnprintf (next, size,
653 " fstn-%8x/%p", p.fstn->hw_prev,
654 p.fstn);
655 tag = Q_NEXT_TYPE(fusbh200, p.fstn->hw_next);
656 p = p.fstn->fstn_next;
657 break;
658 case Q_TYPE_ITD:
659 temp = scnprintf (next, size,
660 " itd/%p", p.itd);
661 tag = Q_NEXT_TYPE(fusbh200, p.itd->hw_next);
662 p = p.itd->itd_next;
663 break;
665 size -= temp;
666 next += temp;
667 } while (p.ptr);
669 temp = scnprintf (next, size, "\n");
670 size -= temp;
671 next += temp;
673 spin_unlock_irqrestore (&fusbh200->lock, flags);
674 kfree (seen);
676 return buf->alloc_size - size;
678 #undef DBG_SCHED_LIMIT
680 static const char *rh_state_string(struct fusbh200_hcd *fusbh200)
682 switch (fusbh200->rh_state) {
683 case FUSBH200_RH_HALTED:
684 return "halted";
685 case FUSBH200_RH_SUSPENDED:
686 return "suspended";
687 case FUSBH200_RH_RUNNING:
688 return "running";
689 case FUSBH200_RH_STOPPING:
690 return "stopping";
692 return "?";
695 static ssize_t fill_registers_buffer(struct debug_buffer *buf)
697 struct usb_hcd *hcd;
698 struct fusbh200_hcd *fusbh200;
699 unsigned long flags;
700 unsigned temp, size, i;
701 char *next, scratch [80];
702 static char fmt [] = "%*s\n";
703 static char label [] = "";
705 hcd = bus_to_hcd(buf->bus);
706 fusbh200 = hcd_to_fusbh200 (hcd);
707 next = buf->output_buf;
708 size = buf->alloc_size;
710 spin_lock_irqsave (&fusbh200->lock, flags);
712 if (!HCD_HW_ACCESSIBLE(hcd)) {
713 size = scnprintf (next, size,
714 "bus %s, device %s\n"
715 "%s\n"
716 "SUSPENDED (no register access)\n",
717 hcd->self.controller->bus->name,
718 dev_name(hcd->self.controller),
719 hcd->product_desc);
720 goto done;
723 /* Capability Registers */
724 i = HC_VERSION(fusbh200, fusbh200_readl(fusbh200, &fusbh200->caps->hc_capbase));
725 temp = scnprintf (next, size,
726 "bus %s, device %s\n"
727 "%s\n"
728 "EHCI %x.%02x, rh state %s\n",
729 hcd->self.controller->bus->name,
730 dev_name(hcd->self.controller),
731 hcd->product_desc,
732 i >> 8, i & 0x0ff, rh_state_string(fusbh200));
733 size -= temp;
734 next += temp;
736 // FIXME interpret both types of params
737 i = fusbh200_readl(fusbh200, &fusbh200->caps->hcs_params);
738 temp = scnprintf (next, size, "structural params 0x%08x\n", i);
739 size -= temp;
740 next += temp;
742 i = fusbh200_readl(fusbh200, &fusbh200->caps->hcc_params);
743 temp = scnprintf (next, size, "capability params 0x%08x\n", i);
744 size -= temp;
745 next += temp;
747 /* Operational Registers */
748 temp = dbg_status_buf (scratch, sizeof scratch, label,
749 fusbh200_readl(fusbh200, &fusbh200->regs->status));
750 temp = scnprintf (next, size, fmt, temp, scratch);
751 size -= temp;
752 next += temp;
754 temp = dbg_command_buf (scratch, sizeof scratch, label,
755 fusbh200_readl(fusbh200, &fusbh200->regs->command));
756 temp = scnprintf (next, size, fmt, temp, scratch);
757 size -= temp;
758 next += temp;
760 temp = dbg_intr_buf (scratch, sizeof scratch, label,
761 fusbh200_readl(fusbh200, &fusbh200->regs->intr_enable));
762 temp = scnprintf (next, size, fmt, temp, scratch);
763 size -= temp;
764 next += temp;
766 temp = scnprintf (next, size, "uframe %04x\n",
767 fusbh200_read_frame_index(fusbh200));
768 size -= temp;
769 next += temp;
771 if (fusbh200->async_unlink) {
772 temp = scnprintf(next, size, "async unlink qh %p\n",
773 fusbh200->async_unlink);
774 size -= temp;
775 next += temp;
778 #ifdef FUSBH200_STATS
779 temp = scnprintf (next, size,
780 "irq normal %ld err %ld iaa %ld (lost %ld)\n",
781 fusbh200->stats.normal, fusbh200->stats.error, fusbh200->stats.iaa,
782 fusbh200->stats.lost_iaa);
783 size -= temp;
784 next += temp;
786 temp = scnprintf (next, size, "complete %ld unlink %ld\n",
787 fusbh200->stats.complete, fusbh200->stats.unlink);
788 size -= temp;
789 next += temp;
790 #endif
792 done:
793 spin_unlock_irqrestore (&fusbh200->lock, flags);
795 return buf->alloc_size - size;
798 static struct debug_buffer *alloc_buffer(struct usb_bus *bus,
799 ssize_t (*fill_func)(struct debug_buffer *))
801 struct debug_buffer *buf;
803 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
805 if (buf) {
806 buf->bus = bus;
807 buf->fill_func = fill_func;
808 mutex_init(&buf->mutex);
809 buf->alloc_size = PAGE_SIZE;
812 return buf;
815 static int fill_buffer(struct debug_buffer *buf)
817 int ret = 0;
819 if (!buf->output_buf)
820 buf->output_buf = vmalloc(buf->alloc_size);
822 if (!buf->output_buf) {
823 ret = -ENOMEM;
824 goto out;
827 ret = buf->fill_func(buf);
829 if (ret >= 0) {
830 buf->count = ret;
831 ret = 0;
834 out:
835 return ret;
838 static ssize_t debug_output(struct file *file, char __user *user_buf,
839 size_t len, loff_t *offset)
841 struct debug_buffer *buf = file->private_data;
842 int ret = 0;
844 mutex_lock(&buf->mutex);
845 if (buf->count == 0) {
846 ret = fill_buffer(buf);
847 if (ret != 0) {
848 mutex_unlock(&buf->mutex);
849 goto out;
852 mutex_unlock(&buf->mutex);
854 ret = simple_read_from_buffer(user_buf, len, offset,
855 buf->output_buf, buf->count);
857 out:
858 return ret;
862 static int debug_close(struct inode *inode, struct file *file)
864 struct debug_buffer *buf = file->private_data;
866 if (buf) {
867 vfree(buf->output_buf);
868 kfree(buf);
871 return 0;
873 static int debug_async_open(struct inode *inode, struct file *file)
875 file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
877 return file->private_data ? 0 : -ENOMEM;
880 static int debug_periodic_open(struct inode *inode, struct file *file)
882 struct debug_buffer *buf;
883 buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
884 if (!buf)
885 return -ENOMEM;
887 buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
888 file->private_data = buf;
889 return 0;
892 static int debug_registers_open(struct inode *inode, struct file *file)
894 file->private_data = alloc_buffer(inode->i_private,
895 fill_registers_buffer);
897 return file->private_data ? 0 : -ENOMEM;
900 static inline void create_debug_files (struct fusbh200_hcd *fusbh200)
902 struct usb_bus *bus = &fusbh200_to_hcd(fusbh200)->self;
904 fusbh200->debug_dir = debugfs_create_dir(bus->bus_name, fusbh200_debug_root);
905 if (!fusbh200->debug_dir)
906 return;
908 if (!debugfs_create_file("async", S_IRUGO, fusbh200->debug_dir, bus,
909 &debug_async_fops))
910 goto file_error;
912 if (!debugfs_create_file("periodic", S_IRUGO, fusbh200->debug_dir, bus,
913 &debug_periodic_fops))
914 goto file_error;
916 if (!debugfs_create_file("registers", S_IRUGO, fusbh200->debug_dir, bus,
917 &debug_registers_fops))
918 goto file_error;
920 return;
922 file_error:
923 debugfs_remove_recursive(fusbh200->debug_dir);
926 static inline void remove_debug_files (struct fusbh200_hcd *fusbh200)
928 debugfs_remove_recursive(fusbh200->debug_dir);
931 #endif /* STUB_DEBUG_FILES */
932 /*-------------------------------------------------------------------------*/
935 * handshake - spin reading hc until handshake completes or fails
936 * @ptr: address of hc register to be read
937 * @mask: bits to look at in result of read
938 * @done: value of those bits when handshake succeeds
939 * @usec: timeout in microseconds
941 * Returns negative errno, or zero on success
943 * Success happens when the "mask" bits have the specified value (hardware
944 * handshake done). There are two failure modes: "usec" have passed (major
945 * hardware flakeout), or the register reads as all-ones (hardware removed).
947 * That last failure should_only happen in cases like physical cardbus eject
948 * before driver shutdown. But it also seems to be caused by bugs in cardbus
949 * bridge shutdown: shutting down the bridge before the devices using it.
951 static int handshake (struct fusbh200_hcd *fusbh200, void __iomem *ptr,
952 u32 mask, u32 done, int usec)
954 u32 result;
956 do {
957 result = fusbh200_readl(fusbh200, ptr);
958 if (result == ~(u32)0) /* card removed */
959 return -ENODEV;
960 result &= mask;
961 if (result == done)
962 return 0;
963 udelay (1);
964 usec--;
965 } while (usec > 0);
966 return -ETIMEDOUT;
970 * Force HC to halt state from unknown (EHCI spec section 2.3).
971 * Must be called with interrupts enabled and the lock not held.
973 static int fusbh200_halt (struct fusbh200_hcd *fusbh200)
975 u32 temp;
977 spin_lock_irq(&fusbh200->lock);
979 /* disable any irqs left enabled by previous code */
980 fusbh200_writel(fusbh200, 0, &fusbh200->regs->intr_enable);
983 * This routine gets called during probe before fusbh200->command
984 * has been initialized, so we can't rely on its value.
986 fusbh200->command &= ~CMD_RUN;
987 temp = fusbh200_readl(fusbh200, &fusbh200->regs->command);
988 temp &= ~(CMD_RUN | CMD_IAAD);
989 fusbh200_writel(fusbh200, temp, &fusbh200->regs->command);
991 spin_unlock_irq(&fusbh200->lock);
992 synchronize_irq(fusbh200_to_hcd(fusbh200)->irq);
994 return handshake(fusbh200, &fusbh200->regs->status,
995 STS_HALT, STS_HALT, 16 * 125);
999 * Reset a non-running (STS_HALT == 1) controller.
1000 * Must be called with interrupts enabled and the lock not held.
1002 static int fusbh200_reset (struct fusbh200_hcd *fusbh200)
1004 int retval;
1005 u32 command = fusbh200_readl(fusbh200, &fusbh200->regs->command);
1007 /* If the EHCI debug controller is active, special care must be
1008 * taken before and after a host controller reset */
1009 if (fusbh200->debug && !dbgp_reset_prep(fusbh200_to_hcd(fusbh200)))
1010 fusbh200->debug = NULL;
1012 command |= CMD_RESET;
1013 dbg_cmd (fusbh200, "reset", command);
1014 fusbh200_writel(fusbh200, command, &fusbh200->regs->command);
1015 fusbh200->rh_state = FUSBH200_RH_HALTED;
1016 fusbh200->next_statechange = jiffies;
1017 retval = handshake (fusbh200, &fusbh200->regs->command,
1018 CMD_RESET, 0, 250 * 1000);
1020 if (retval)
1021 return retval;
1023 if (fusbh200->debug)
1024 dbgp_external_startup(fusbh200_to_hcd(fusbh200));
1026 fusbh200->port_c_suspend = fusbh200->suspended_ports =
1027 fusbh200->resuming_ports = 0;
1028 return retval;
1032 * Idle the controller (turn off the schedules).
1033 * Must be called with interrupts enabled and the lock not held.
1035 static void fusbh200_quiesce (struct fusbh200_hcd *fusbh200)
1037 u32 temp;
1039 if (fusbh200->rh_state != FUSBH200_RH_RUNNING)
1040 return;
1042 /* wait for any schedule enables/disables to take effect */
1043 temp = (fusbh200->command << 10) & (STS_ASS | STS_PSS);
1044 handshake(fusbh200, &fusbh200->regs->status, STS_ASS | STS_PSS, temp, 16 * 125);
1046 /* then disable anything that's still active */
1047 spin_lock_irq(&fusbh200->lock);
1048 fusbh200->command &= ~(CMD_ASE | CMD_PSE);
1049 fusbh200_writel(fusbh200, fusbh200->command, &fusbh200->regs->command);
1050 spin_unlock_irq(&fusbh200->lock);
1052 /* hardware can take 16 microframes to turn off ... */
1053 handshake(fusbh200, &fusbh200->regs->status, STS_ASS | STS_PSS, 0, 16 * 125);
1056 /*-------------------------------------------------------------------------*/
1058 static void end_unlink_async(struct fusbh200_hcd *fusbh200);
1059 static void unlink_empty_async(struct fusbh200_hcd *fusbh200);
1060 static void fusbh200_work(struct fusbh200_hcd *fusbh200);
1061 static void start_unlink_intr(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh);
1062 static void end_unlink_intr(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh);
1064 /*-------------------------------------------------------------------------*/
1066 /* Set a bit in the USBCMD register */
1067 static void fusbh200_set_command_bit(struct fusbh200_hcd *fusbh200, u32 bit)
1069 fusbh200->command |= bit;
1070 fusbh200_writel(fusbh200, fusbh200->command, &fusbh200->regs->command);
1072 /* unblock posted write */
1073 fusbh200_readl(fusbh200, &fusbh200->regs->command);
1076 /* Clear a bit in the USBCMD register */
1077 static void fusbh200_clear_command_bit(struct fusbh200_hcd *fusbh200, u32 bit)
1079 fusbh200->command &= ~bit;
1080 fusbh200_writel(fusbh200, fusbh200->command, &fusbh200->regs->command);
1082 /* unblock posted write */
1083 fusbh200_readl(fusbh200, &fusbh200->regs->command);
1086 /*-------------------------------------------------------------------------*/
1089 * EHCI timer support... Now using hrtimers.
1091 * Lots of different events are triggered from fusbh200->hrtimer. Whenever
1092 * the timer routine runs, it checks each possible event; events that are
1093 * currently enabled and whose expiration time has passed get handled.
1094 * The set of enabled events is stored as a collection of bitflags in
1095 * fusbh200->enabled_hrtimer_events, and they are numbered in order of
1096 * increasing delay values (ranging between 1 ms and 100 ms).
1098 * Rather than implementing a sorted list or tree of all pending events,
1099 * we keep track only of the lowest-numbered pending event, in
1100 * fusbh200->next_hrtimer_event. Whenever fusbh200->hrtimer gets restarted, its
1101 * expiration time is set to the timeout value for this event.
1103 * As a result, events might not get handled right away; the actual delay
1104 * could be anywhere up to twice the requested delay. This doesn't
1105 * matter, because none of the events are especially time-critical. The
1106 * ones that matter most all have a delay of 1 ms, so they will be
1107 * handled after 2 ms at most, which is okay. In addition to this, we
1108 * allow for an expiration range of 1 ms.
1112 * Delay lengths for the hrtimer event types.
1113 * Keep this list sorted by delay length, in the same order as
1114 * the event types indexed by enum fusbh200_hrtimer_event in fusbh200.h.
1116 static unsigned event_delays_ns[] = {
1117 1 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_POLL_ASS */
1118 1 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_POLL_PSS */
1119 1 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_POLL_DEAD */
1120 1125 * NSEC_PER_USEC, /* FUSBH200_HRTIMER_UNLINK_INTR */
1121 2 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_FREE_ITDS */
1122 6 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_ASYNC_UNLINKS */
1123 10 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_IAA_WATCHDOG */
1124 10 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_DISABLE_PERIODIC */
1125 15 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_DISABLE_ASYNC */
1126 100 * NSEC_PER_MSEC, /* FUSBH200_HRTIMER_IO_WATCHDOG */
1129 /* Enable a pending hrtimer event */
1130 static void fusbh200_enable_event(struct fusbh200_hcd *fusbh200, unsigned event,
1131 bool resched)
1133 ktime_t *timeout = &fusbh200->hr_timeouts[event];
1135 if (resched)
1136 *timeout = ktime_add(ktime_get(),
1137 ktime_set(0, event_delays_ns[event]));
1138 fusbh200->enabled_hrtimer_events |= (1 << event);
1140 /* Track only the lowest-numbered pending event */
1141 if (event < fusbh200->next_hrtimer_event) {
1142 fusbh200->next_hrtimer_event = event;
1143 hrtimer_start_range_ns(&fusbh200->hrtimer, *timeout,
1144 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
1149 /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
1150 static void fusbh200_poll_ASS(struct fusbh200_hcd *fusbh200)
1152 unsigned actual, want;
1154 /* Don't enable anything if the controller isn't running (e.g., died) */
1155 if (fusbh200->rh_state != FUSBH200_RH_RUNNING)
1156 return;
1158 want = (fusbh200->command & CMD_ASE) ? STS_ASS : 0;
1159 actual = fusbh200_readl(fusbh200, &fusbh200->regs->status) & STS_ASS;
1161 if (want != actual) {
1163 /* Poll again later, but give up after about 20 ms */
1164 if (fusbh200->ASS_poll_count++ < 20) {
1165 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_POLL_ASS, true);
1166 return;
1168 fusbh200_dbg(fusbh200, "Waited too long for the async schedule status (%x/%x), giving up\n",
1169 want, actual);
1171 fusbh200->ASS_poll_count = 0;
1173 /* The status is up-to-date; restart or stop the schedule as needed */
1174 if (want == 0) { /* Stopped */
1175 if (fusbh200->async_count > 0)
1176 fusbh200_set_command_bit(fusbh200, CMD_ASE);
1178 } else { /* Running */
1179 if (fusbh200->async_count == 0) {
1181 /* Turn off the schedule after a while */
1182 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_DISABLE_ASYNC,
1183 true);
1188 /* Turn off the async schedule after a brief delay */
1189 static void fusbh200_disable_ASE(struct fusbh200_hcd *fusbh200)
1191 fusbh200_clear_command_bit(fusbh200, CMD_ASE);
1195 /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
1196 static void fusbh200_poll_PSS(struct fusbh200_hcd *fusbh200)
1198 unsigned actual, want;
1200 /* Don't do anything if the controller isn't running (e.g., died) */
1201 if (fusbh200->rh_state != FUSBH200_RH_RUNNING)
1202 return;
1204 want = (fusbh200->command & CMD_PSE) ? STS_PSS : 0;
1205 actual = fusbh200_readl(fusbh200, &fusbh200->regs->status) & STS_PSS;
1207 if (want != actual) {
1209 /* Poll again later, but give up after about 20 ms */
1210 if (fusbh200->PSS_poll_count++ < 20) {
1211 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_POLL_PSS, true);
1212 return;
1214 fusbh200_dbg(fusbh200, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
1215 want, actual);
1217 fusbh200->PSS_poll_count = 0;
1219 /* The status is up-to-date; restart or stop the schedule as needed */
1220 if (want == 0) { /* Stopped */
1221 if (fusbh200->periodic_count > 0)
1222 fusbh200_set_command_bit(fusbh200, CMD_PSE);
1224 } else { /* Running */
1225 if (fusbh200->periodic_count == 0) {
1227 /* Turn off the schedule after a while */
1228 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_DISABLE_PERIODIC,
1229 true);
1234 /* Turn off the periodic schedule after a brief delay */
1235 static void fusbh200_disable_PSE(struct fusbh200_hcd *fusbh200)
1237 fusbh200_clear_command_bit(fusbh200, CMD_PSE);
1241 /* Poll the STS_HALT status bit; see when a dead controller stops */
1242 static void fusbh200_handle_controller_death(struct fusbh200_hcd *fusbh200)
1244 if (!(fusbh200_readl(fusbh200, &fusbh200->regs->status) & STS_HALT)) {
1246 /* Give up after a few milliseconds */
1247 if (fusbh200->died_poll_count++ < 5) {
1248 /* Try again later */
1249 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_POLL_DEAD, true);
1250 return;
1252 fusbh200_warn(fusbh200, "Waited too long for the controller to stop, giving up\n");
1255 /* Clean up the mess */
1256 fusbh200->rh_state = FUSBH200_RH_HALTED;
1257 fusbh200_writel(fusbh200, 0, &fusbh200->regs->intr_enable);
1258 fusbh200_work(fusbh200);
1259 end_unlink_async(fusbh200);
1261 /* Not in process context, so don't try to reset the controller */
1265 /* Handle unlinked interrupt QHs once they are gone from the hardware */
1266 static void fusbh200_handle_intr_unlinks(struct fusbh200_hcd *fusbh200)
1268 bool stopped = (fusbh200->rh_state < FUSBH200_RH_RUNNING);
1271 * Process all the QHs on the intr_unlink list that were added
1272 * before the current unlink cycle began. The list is in
1273 * temporal order, so stop when we reach the first entry in the
1274 * current cycle. But if the root hub isn't running then
1275 * process all the QHs on the list.
1277 fusbh200->intr_unlinking = true;
1278 while (fusbh200->intr_unlink) {
1279 struct fusbh200_qh *qh = fusbh200->intr_unlink;
1281 if (!stopped && qh->unlink_cycle == fusbh200->intr_unlink_cycle)
1282 break;
1283 fusbh200->intr_unlink = qh->unlink_next;
1284 qh->unlink_next = NULL;
1285 end_unlink_intr(fusbh200, qh);
1288 /* Handle remaining entries later */
1289 if (fusbh200->intr_unlink) {
1290 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_UNLINK_INTR, true);
1291 ++fusbh200->intr_unlink_cycle;
1293 fusbh200->intr_unlinking = false;
1297 /* Start another free-iTDs/siTDs cycle */
1298 static void start_free_itds(struct fusbh200_hcd *fusbh200)
1300 if (!(fusbh200->enabled_hrtimer_events & BIT(FUSBH200_HRTIMER_FREE_ITDS))) {
1301 fusbh200->last_itd_to_free = list_entry(
1302 fusbh200->cached_itd_list.prev,
1303 struct fusbh200_itd, itd_list);
1304 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_FREE_ITDS, true);
1308 /* Wait for controller to stop using old iTDs and siTDs */
1309 static void end_free_itds(struct fusbh200_hcd *fusbh200)
1311 struct fusbh200_itd *itd, *n;
1313 if (fusbh200->rh_state < FUSBH200_RH_RUNNING) {
1314 fusbh200->last_itd_to_free = NULL;
1317 list_for_each_entry_safe(itd, n, &fusbh200->cached_itd_list, itd_list) {
1318 list_del(&itd->itd_list);
1319 dma_pool_free(fusbh200->itd_pool, itd, itd->itd_dma);
1320 if (itd == fusbh200->last_itd_to_free)
1321 break;
1324 if (!list_empty(&fusbh200->cached_itd_list))
1325 start_free_itds(fusbh200);
1329 /* Handle lost (or very late) IAA interrupts */
1330 static void fusbh200_iaa_watchdog(struct fusbh200_hcd *fusbh200)
1332 if (fusbh200->rh_state != FUSBH200_RH_RUNNING)
1333 return;
1336 * Lost IAA irqs wedge things badly; seen first with a vt8235.
1337 * So we need this watchdog, but must protect it against both
1338 * (a) SMP races against real IAA firing and retriggering, and
1339 * (b) clean HC shutdown, when IAA watchdog was pending.
1341 if (fusbh200->async_iaa) {
1342 u32 cmd, status;
1344 /* If we get here, IAA is *REALLY* late. It's barely
1345 * conceivable that the system is so busy that CMD_IAAD
1346 * is still legitimately set, so let's be sure it's
1347 * clear before we read STS_IAA. (The HC should clear
1348 * CMD_IAAD when it sets STS_IAA.)
1350 cmd = fusbh200_readl(fusbh200, &fusbh200->regs->command);
1353 * If IAA is set here it either legitimately triggered
1354 * after the watchdog timer expired (_way_ late, so we'll
1355 * still count it as lost) ... or a silicon erratum:
1356 * - VIA seems to set IAA without triggering the IRQ;
1357 * - IAAD potentially cleared without setting IAA.
1359 status = fusbh200_readl(fusbh200, &fusbh200->regs->status);
1360 if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
1361 COUNT(fusbh200->stats.lost_iaa);
1362 fusbh200_writel(fusbh200, STS_IAA, &fusbh200->regs->status);
1365 fusbh200_vdbg(fusbh200, "IAA watchdog: status %x cmd %x\n",
1366 status, cmd);
1367 end_unlink_async(fusbh200);
1372 /* Enable the I/O watchdog, if appropriate */
1373 static void turn_on_io_watchdog(struct fusbh200_hcd *fusbh200)
1375 /* Not needed if the controller isn't running or it's already enabled */
1376 if (fusbh200->rh_state != FUSBH200_RH_RUNNING ||
1377 (fusbh200->enabled_hrtimer_events &
1378 BIT(FUSBH200_HRTIMER_IO_WATCHDOG)))
1379 return;
1382 * Isochronous transfers always need the watchdog.
1383 * For other sorts we use it only if the flag is set.
1385 if (fusbh200->isoc_count > 0 || (fusbh200->need_io_watchdog &&
1386 fusbh200->async_count + fusbh200->intr_count > 0))
1387 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_IO_WATCHDOG, true);
1392 * Handler functions for the hrtimer event types.
1393 * Keep this array in the same order as the event types indexed by
1394 * enum fusbh200_hrtimer_event in fusbh200.h.
1396 static void (*event_handlers[])(struct fusbh200_hcd *) = {
1397 fusbh200_poll_ASS, /* FUSBH200_HRTIMER_POLL_ASS */
1398 fusbh200_poll_PSS, /* FUSBH200_HRTIMER_POLL_PSS */
1399 fusbh200_handle_controller_death, /* FUSBH200_HRTIMER_POLL_DEAD */
1400 fusbh200_handle_intr_unlinks, /* FUSBH200_HRTIMER_UNLINK_INTR */
1401 end_free_itds, /* FUSBH200_HRTIMER_FREE_ITDS */
1402 unlink_empty_async, /* FUSBH200_HRTIMER_ASYNC_UNLINKS */
1403 fusbh200_iaa_watchdog, /* FUSBH200_HRTIMER_IAA_WATCHDOG */
1404 fusbh200_disable_PSE, /* FUSBH200_HRTIMER_DISABLE_PERIODIC */
1405 fusbh200_disable_ASE, /* FUSBH200_HRTIMER_DISABLE_ASYNC */
1406 fusbh200_work, /* FUSBH200_HRTIMER_IO_WATCHDOG */
1409 static enum hrtimer_restart fusbh200_hrtimer_func(struct hrtimer *t)
1411 struct fusbh200_hcd *fusbh200 = container_of(t, struct fusbh200_hcd, hrtimer);
1412 ktime_t now;
1413 unsigned long events;
1414 unsigned long flags;
1415 unsigned e;
1417 spin_lock_irqsave(&fusbh200->lock, flags);
1419 events = fusbh200->enabled_hrtimer_events;
1420 fusbh200->enabled_hrtimer_events = 0;
1421 fusbh200->next_hrtimer_event = FUSBH200_HRTIMER_NO_EVENT;
1424 * Check each pending event. If its time has expired, handle
1425 * the event; otherwise re-enable it.
1427 now = ktime_get();
1428 for_each_set_bit(e, &events, FUSBH200_HRTIMER_NUM_EVENTS) {
1429 if (now.tv64 >= fusbh200->hr_timeouts[e].tv64)
1430 event_handlers[e](fusbh200);
1431 else
1432 fusbh200_enable_event(fusbh200, e, false);
1435 spin_unlock_irqrestore(&fusbh200->lock, flags);
1436 return HRTIMER_NORESTART;
1439 /*-------------------------------------------------------------------------*/
1441 #define fusbh200_bus_suspend NULL
1442 #define fusbh200_bus_resume NULL
1444 /*-------------------------------------------------------------------------*/
1446 static int check_reset_complete (
1447 struct fusbh200_hcd *fusbh200,
1448 int index,
1449 u32 __iomem *status_reg,
1450 int port_status
1452 if (!(port_status & PORT_CONNECT))
1453 return port_status;
1455 /* if reset finished and it's still not enabled -- handoff */
1456 if (!(port_status & PORT_PE)) {
1457 /* with integrated TT, there's nobody to hand it to! */
1458 fusbh200_dbg (fusbh200,
1459 "Failed to enable port %d on root hub TT\n",
1460 index+1);
1461 return port_status;
1462 } else {
1463 fusbh200_dbg(fusbh200, "port %d reset complete, port enabled\n",
1464 index + 1);
1467 return port_status;
1470 /*-------------------------------------------------------------------------*/
1473 /* build "status change" packet (one or two bytes) from HC registers */
1475 static int
1476 fusbh200_hub_status_data (struct usb_hcd *hcd, char *buf)
1478 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
1479 u32 temp, status;
1480 u32 mask;
1481 int retval = 1;
1482 unsigned long flags;
1484 /* init status to no-changes */
1485 buf [0] = 0;
1487 /* Inform the core about resumes-in-progress by returning
1488 * a non-zero value even if there are no status changes.
1490 status = fusbh200->resuming_ports;
1492 mask = PORT_CSC | PORT_PEC;
1493 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
1495 /* no hub change reports (bit 0) for now (power, ...) */
1497 /* port N changes (bit N)? */
1498 spin_lock_irqsave (&fusbh200->lock, flags);
1500 temp = fusbh200_readl(fusbh200, &fusbh200->regs->port_status);
1503 * Return status information even for ports with OWNER set.
1504 * Otherwise khubd wouldn't see the disconnect event when a
1505 * high-speed device is switched over to the companion
1506 * controller by the user.
1509 if ((temp & mask) != 0 || test_bit(0, &fusbh200->port_c_suspend)
1510 || (fusbh200->reset_done[0] && time_after_eq(
1511 jiffies, fusbh200->reset_done[0]))) {
1512 buf [0] |= 1 << 1;
1513 status = STS_PCD;
1515 /* FIXME autosuspend idle root hubs */
1516 spin_unlock_irqrestore (&fusbh200->lock, flags);
1517 return status ? retval : 0;
1520 /*-------------------------------------------------------------------------*/
1522 static void
1523 fusbh200_hub_descriptor (
1524 struct fusbh200_hcd *fusbh200,
1525 struct usb_hub_descriptor *desc
1527 int ports = HCS_N_PORTS (fusbh200->hcs_params);
1528 u16 temp;
1530 desc->bDescriptorType = 0x29;
1531 desc->bPwrOn2PwrGood = 10; /* fusbh200 1.0, 2.3.9 says 20ms max */
1532 desc->bHubContrCurrent = 0;
1534 desc->bNbrPorts = ports;
1535 temp = 1 + (ports / 8);
1536 desc->bDescLength = 7 + 2 * temp;
1538 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1539 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1540 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
1542 temp = 0x0008; /* per-port overcurrent reporting */
1543 temp |= 0x0002; /* no power switching */
1544 desc->wHubCharacteristics = cpu_to_le16(temp);
1547 /*-------------------------------------------------------------------------*/
1549 static int fusbh200_hub_control (
1550 struct usb_hcd *hcd,
1551 u16 typeReq,
1552 u16 wValue,
1553 u16 wIndex,
1554 char *buf,
1555 u16 wLength
1557 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
1558 int ports = HCS_N_PORTS (fusbh200->hcs_params);
1559 u32 __iomem *status_reg = &fusbh200->regs->port_status;
1560 u32 temp, temp1, status;
1561 unsigned long flags;
1562 int retval = 0;
1563 unsigned selector;
1566 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1567 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1568 * (track current state ourselves) ... blink for diagnostics,
1569 * power, "this is the one", etc. EHCI spec supports this.
1572 spin_lock_irqsave (&fusbh200->lock, flags);
1573 switch (typeReq) {
1574 case ClearHubFeature:
1575 switch (wValue) {
1576 case C_HUB_LOCAL_POWER:
1577 case C_HUB_OVER_CURRENT:
1578 /* no hub-wide feature/status flags */
1579 break;
1580 default:
1581 goto error;
1583 break;
1584 case ClearPortFeature:
1585 if (!wIndex || wIndex > ports)
1586 goto error;
1587 wIndex--;
1588 temp = fusbh200_readl(fusbh200, status_reg);
1589 temp &= ~PORT_RWC_BITS;
1592 * Even if OWNER is set, so the port is owned by the
1593 * companion controller, khubd needs to be able to clear
1594 * the port-change status bits (especially
1595 * USB_PORT_STAT_C_CONNECTION).
1598 switch (wValue) {
1599 case USB_PORT_FEAT_ENABLE:
1600 fusbh200_writel(fusbh200, temp & ~PORT_PE, status_reg);
1601 break;
1602 case USB_PORT_FEAT_C_ENABLE:
1603 fusbh200_writel(fusbh200, temp | PORT_PEC, status_reg);
1604 break;
1605 case USB_PORT_FEAT_SUSPEND:
1606 if (temp & PORT_RESET)
1607 goto error;
1608 if (!(temp & PORT_SUSPEND))
1609 break;
1610 if ((temp & PORT_PE) == 0)
1611 goto error;
1613 /* resume signaling for 20 msec */
1614 fusbh200_writel(fusbh200, temp | PORT_RESUME, status_reg);
1615 fusbh200->reset_done[wIndex] = jiffies
1616 + msecs_to_jiffies(20);
1617 break;
1618 case USB_PORT_FEAT_C_SUSPEND:
1619 clear_bit(wIndex, &fusbh200->port_c_suspend);
1620 break;
1621 case USB_PORT_FEAT_C_CONNECTION:
1622 fusbh200_writel(fusbh200, temp | PORT_CSC, status_reg);
1623 break;
1624 case USB_PORT_FEAT_C_OVER_CURRENT:
1625 fusbh200_writel(fusbh200, temp | BMISR_OVC, &fusbh200->regs->bmisr);
1626 break;
1627 case USB_PORT_FEAT_C_RESET:
1628 /* GetPortStatus clears reset */
1629 break;
1630 default:
1631 goto error;
1633 fusbh200_readl(fusbh200, &fusbh200->regs->command); /* unblock posted write */
1634 break;
1635 case GetHubDescriptor:
1636 fusbh200_hub_descriptor (fusbh200, (struct usb_hub_descriptor *)
1637 buf);
1638 break;
1639 case GetHubStatus:
1640 /* no hub-wide feature/status flags */
1641 memset (buf, 0, 4);
1642 //cpu_to_le32s ((u32 *) buf);
1643 break;
1644 case GetPortStatus:
1645 if (!wIndex || wIndex > ports)
1646 goto error;
1647 wIndex--;
1648 status = 0;
1649 temp = fusbh200_readl(fusbh200, status_reg);
1651 // wPortChange bits
1652 if (temp & PORT_CSC)
1653 status |= USB_PORT_STAT_C_CONNECTION << 16;
1654 if (temp & PORT_PEC)
1655 status |= USB_PORT_STAT_C_ENABLE << 16;
1657 temp1 = fusbh200_readl(fusbh200, &fusbh200->regs->bmisr);
1658 if (temp1 & BMISR_OVC)
1659 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1661 /* whoever resumes must GetPortStatus to complete it!! */
1662 if (temp & PORT_RESUME) {
1664 /* Remote Wakeup received? */
1665 if (!fusbh200->reset_done[wIndex]) {
1666 /* resume signaling for 20 msec */
1667 fusbh200->reset_done[wIndex] = jiffies
1668 + msecs_to_jiffies(20);
1669 /* check the port again */
1670 mod_timer(&fusbh200_to_hcd(fusbh200)->rh_timer,
1671 fusbh200->reset_done[wIndex]);
1674 /* resume completed? */
1675 else if (time_after_eq(jiffies,
1676 fusbh200->reset_done[wIndex])) {
1677 clear_bit(wIndex, &fusbh200->suspended_ports);
1678 set_bit(wIndex, &fusbh200->port_c_suspend);
1679 fusbh200->reset_done[wIndex] = 0;
1681 /* stop resume signaling */
1682 temp = fusbh200_readl(fusbh200, status_reg);
1683 fusbh200_writel(fusbh200,
1684 temp & ~(PORT_RWC_BITS | PORT_RESUME),
1685 status_reg);
1686 clear_bit(wIndex, &fusbh200->resuming_ports);
1687 retval = handshake(fusbh200, status_reg,
1688 PORT_RESUME, 0, 2000 /* 2msec */);
1689 if (retval != 0) {
1690 fusbh200_err(fusbh200,
1691 "port %d resume error %d\n",
1692 wIndex + 1, retval);
1693 goto error;
1695 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1699 /* whoever resets must GetPortStatus to complete it!! */
1700 if ((temp & PORT_RESET)
1701 && time_after_eq(jiffies,
1702 fusbh200->reset_done[wIndex])) {
1703 status |= USB_PORT_STAT_C_RESET << 16;
1704 fusbh200->reset_done [wIndex] = 0;
1705 clear_bit(wIndex, &fusbh200->resuming_ports);
1707 /* force reset to complete */
1708 fusbh200_writel(fusbh200, temp & ~(PORT_RWC_BITS | PORT_RESET),
1709 status_reg);
1710 /* REVISIT: some hardware needs 550+ usec to clear
1711 * this bit; seems too long to spin routinely...
1713 retval = handshake(fusbh200, status_reg,
1714 PORT_RESET, 0, 1000);
1715 if (retval != 0) {
1716 fusbh200_err (fusbh200, "port %d reset error %d\n",
1717 wIndex + 1, retval);
1718 goto error;
1721 /* see what we found out */
1722 temp = check_reset_complete (fusbh200, wIndex, status_reg,
1723 fusbh200_readl(fusbh200, status_reg));
1726 if (!(temp & (PORT_RESUME|PORT_RESET))) {
1727 fusbh200->reset_done[wIndex] = 0;
1728 clear_bit(wIndex, &fusbh200->resuming_ports);
1731 /* transfer dedicated ports to the companion hc */
1732 if ((temp & PORT_CONNECT) &&
1733 test_bit(wIndex, &fusbh200->companion_ports)) {
1734 temp &= ~PORT_RWC_BITS;
1735 fusbh200_writel(fusbh200, temp, status_reg);
1736 fusbh200_dbg(fusbh200, "port %d --> companion\n", wIndex + 1);
1737 temp = fusbh200_readl(fusbh200, status_reg);
1741 * Even if OWNER is set, there's no harm letting khubd
1742 * see the wPortStatus values (they should all be 0 except
1743 * for PORT_POWER anyway).
1746 if (temp & PORT_CONNECT) {
1747 status |= USB_PORT_STAT_CONNECTION;
1748 status |= fusbh200_port_speed(fusbh200, temp);
1750 if (temp & PORT_PE)
1751 status |= USB_PORT_STAT_ENABLE;
1753 /* maybe the port was unsuspended without our knowledge */
1754 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1755 status |= USB_PORT_STAT_SUSPEND;
1756 } else if (test_bit(wIndex, &fusbh200->suspended_ports)) {
1757 clear_bit(wIndex, &fusbh200->suspended_ports);
1758 clear_bit(wIndex, &fusbh200->resuming_ports);
1759 fusbh200->reset_done[wIndex] = 0;
1760 if (temp & PORT_PE)
1761 set_bit(wIndex, &fusbh200->port_c_suspend);
1764 temp1 = fusbh200_readl(fusbh200, &fusbh200->regs->bmisr);
1765 if (temp1 & BMISR_OVC)
1766 status |= USB_PORT_STAT_OVERCURRENT;
1767 if (temp & PORT_RESET)
1768 status |= USB_PORT_STAT_RESET;
1769 if (test_bit(wIndex, &fusbh200->port_c_suspend))
1770 status |= USB_PORT_STAT_C_SUSPEND << 16;
1772 #ifndef VERBOSE_DEBUG
1773 if (status & ~0xffff) /* only if wPortChange is interesting */
1774 #endif
1775 dbg_port (fusbh200, "GetStatus", wIndex + 1, temp);
1776 put_unaligned_le32(status, buf);
1777 break;
1778 case SetHubFeature:
1779 switch (wValue) {
1780 case C_HUB_LOCAL_POWER:
1781 case C_HUB_OVER_CURRENT:
1782 /* no hub-wide feature/status flags */
1783 break;
1784 default:
1785 goto error;
1787 break;
1788 case SetPortFeature:
1789 selector = wIndex >> 8;
1790 wIndex &= 0xff;
1792 if (!wIndex || wIndex > ports)
1793 goto error;
1794 wIndex--;
1795 temp = fusbh200_readl(fusbh200, status_reg);
1796 temp &= ~PORT_RWC_BITS;
1797 switch (wValue) {
1798 case USB_PORT_FEAT_SUSPEND:
1799 if ((temp & PORT_PE) == 0
1800 || (temp & PORT_RESET) != 0)
1801 goto error;
1803 /* After above check the port must be connected.
1804 * Set appropriate bit thus could put phy into low power
1805 * mode if we have hostpc feature
1807 fusbh200_writel(fusbh200, temp | PORT_SUSPEND, status_reg);
1808 set_bit(wIndex, &fusbh200->suspended_ports);
1809 break;
1810 case USB_PORT_FEAT_RESET:
1811 if (temp & PORT_RESUME)
1812 goto error;
1813 /* line status bits may report this as low speed,
1814 * which can be fine if this root hub has a
1815 * transaction translator built in.
1817 fusbh200_vdbg (fusbh200, "port %d reset\n", wIndex + 1);
1818 temp |= PORT_RESET;
1819 temp &= ~PORT_PE;
1822 * caller must wait, then call GetPortStatus
1823 * usb 2.0 spec says 50 ms resets on root
1825 fusbh200->reset_done [wIndex] = jiffies
1826 + msecs_to_jiffies (50);
1827 fusbh200_writel(fusbh200, temp, status_reg);
1828 break;
1830 /* For downstream facing ports (these): one hub port is put
1831 * into test mode according to USB2 11.24.2.13, then the hub
1832 * must be reset (which for root hub now means rmmod+modprobe,
1833 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1834 * about the EHCI-specific stuff.
1836 case USB_PORT_FEAT_TEST:
1837 if (!selector || selector > 5)
1838 goto error;
1839 spin_unlock_irqrestore(&fusbh200->lock, flags);
1840 fusbh200_quiesce(fusbh200);
1841 spin_lock_irqsave(&fusbh200->lock, flags);
1843 /* Put all enabled ports into suspend */
1844 temp = fusbh200_readl(fusbh200, status_reg) & ~PORT_RWC_BITS;
1845 if (temp & PORT_PE)
1846 fusbh200_writel(fusbh200, temp | PORT_SUSPEND,
1847 status_reg);
1849 spin_unlock_irqrestore(&fusbh200->lock, flags);
1850 fusbh200_halt(fusbh200);
1851 spin_lock_irqsave(&fusbh200->lock, flags);
1853 temp = fusbh200_readl(fusbh200, status_reg);
1854 temp |= selector << 16;
1855 fusbh200_writel(fusbh200, temp, status_reg);
1856 break;
1858 default:
1859 goto error;
1861 fusbh200_readl(fusbh200, &fusbh200->regs->command); /* unblock posted writes */
1862 break;
1864 default:
1865 error:
1866 /* "stall" on error */
1867 retval = -EPIPE;
1869 spin_unlock_irqrestore (&fusbh200->lock, flags);
1870 return retval;
1873 static void __maybe_unused fusbh200_relinquish_port(struct usb_hcd *hcd,
1874 int portnum)
1876 return;
1879 static int __maybe_unused fusbh200_port_handed_over(struct usb_hcd *hcd,
1880 int portnum)
1882 return 0;
1884 /*-------------------------------------------------------------------------*/
1886 * There's basically three types of memory:
1887 * - data used only by the HCD ... kmalloc is fine
1888 * - async and periodic schedules, shared by HC and HCD ... these
1889 * need to use dma_pool or dma_alloc_coherent
1890 * - driver buffers, read/written by HC ... single shot DMA mapped
1892 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
1893 * No memory seen by this driver is pageable.
1896 /*-------------------------------------------------------------------------*/
1898 /* Allocate the key transfer structures from the previously allocated pool */
1900 static inline void fusbh200_qtd_init(struct fusbh200_hcd *fusbh200, struct fusbh200_qtd *qtd,
1901 dma_addr_t dma)
1903 memset (qtd, 0, sizeof *qtd);
1904 qtd->qtd_dma = dma;
1905 qtd->hw_token = cpu_to_hc32(fusbh200, QTD_STS_HALT);
1906 qtd->hw_next = FUSBH200_LIST_END(fusbh200);
1907 qtd->hw_alt_next = FUSBH200_LIST_END(fusbh200);
1908 INIT_LIST_HEAD (&qtd->qtd_list);
1911 static struct fusbh200_qtd *fusbh200_qtd_alloc (struct fusbh200_hcd *fusbh200, gfp_t flags)
1913 struct fusbh200_qtd *qtd;
1914 dma_addr_t dma;
1916 qtd = dma_pool_alloc (fusbh200->qtd_pool, flags, &dma);
1917 if (qtd != NULL) {
1918 fusbh200_qtd_init(fusbh200, qtd, dma);
1920 return qtd;
1923 static inline void fusbh200_qtd_free (struct fusbh200_hcd *fusbh200, struct fusbh200_qtd *qtd)
1925 dma_pool_free (fusbh200->qtd_pool, qtd, qtd->qtd_dma);
1929 static void qh_destroy(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
1931 /* clean qtds first, and know this is not linked */
1932 if (!list_empty (&qh->qtd_list) || qh->qh_next.ptr) {
1933 fusbh200_dbg (fusbh200, "unused qh not empty!\n");
1934 BUG ();
1936 if (qh->dummy)
1937 fusbh200_qtd_free (fusbh200, qh->dummy);
1938 dma_pool_free(fusbh200->qh_pool, qh->hw, qh->qh_dma);
1939 kfree(qh);
1942 static struct fusbh200_qh *fusbh200_qh_alloc (struct fusbh200_hcd *fusbh200, gfp_t flags)
1944 struct fusbh200_qh *qh;
1945 dma_addr_t dma;
1947 qh = kzalloc(sizeof *qh, GFP_ATOMIC);
1948 if (!qh)
1949 goto done;
1950 qh->hw = (struct fusbh200_qh_hw *)
1951 dma_pool_alloc(fusbh200->qh_pool, flags, &dma);
1952 if (!qh->hw)
1953 goto fail;
1954 memset(qh->hw, 0, sizeof *qh->hw);
1955 qh->qh_dma = dma;
1956 // INIT_LIST_HEAD (&qh->qh_list);
1957 INIT_LIST_HEAD (&qh->qtd_list);
1959 /* dummy td enables safe urb queuing */
1960 qh->dummy = fusbh200_qtd_alloc (fusbh200, flags);
1961 if (qh->dummy == NULL) {
1962 fusbh200_dbg (fusbh200, "no dummy td\n");
1963 goto fail1;
1965 done:
1966 return qh;
1967 fail1:
1968 dma_pool_free(fusbh200->qh_pool, qh->hw, qh->qh_dma);
1969 fail:
1970 kfree(qh);
1971 return NULL;
1974 /*-------------------------------------------------------------------------*/
1976 /* The queue heads and transfer descriptors are managed from pools tied
1977 * to each of the "per device" structures.
1978 * This is the initialisation and cleanup code.
1981 static void fusbh200_mem_cleanup (struct fusbh200_hcd *fusbh200)
1983 if (fusbh200->async)
1984 qh_destroy(fusbh200, fusbh200->async);
1985 fusbh200->async = NULL;
1987 if (fusbh200->dummy)
1988 qh_destroy(fusbh200, fusbh200->dummy);
1989 fusbh200->dummy = NULL;
1991 /* DMA consistent memory and pools */
1992 if (fusbh200->qtd_pool)
1993 dma_pool_destroy (fusbh200->qtd_pool);
1994 fusbh200->qtd_pool = NULL;
1996 if (fusbh200->qh_pool) {
1997 dma_pool_destroy (fusbh200->qh_pool);
1998 fusbh200->qh_pool = NULL;
2001 if (fusbh200->itd_pool)
2002 dma_pool_destroy (fusbh200->itd_pool);
2003 fusbh200->itd_pool = NULL;
2005 if (fusbh200->periodic)
2006 dma_free_coherent (fusbh200_to_hcd(fusbh200)->self.controller,
2007 fusbh200->periodic_size * sizeof (u32),
2008 fusbh200->periodic, fusbh200->periodic_dma);
2009 fusbh200->periodic = NULL;
2011 /* shadow periodic table */
2012 kfree(fusbh200->pshadow);
2013 fusbh200->pshadow = NULL;
2016 /* remember to add cleanup code (above) if you add anything here */
2017 static int fusbh200_mem_init (struct fusbh200_hcd *fusbh200, gfp_t flags)
2019 int i;
2021 /* QTDs for control/bulk/intr transfers */
2022 fusbh200->qtd_pool = dma_pool_create ("fusbh200_qtd",
2023 fusbh200_to_hcd(fusbh200)->self.controller,
2024 sizeof (struct fusbh200_qtd),
2025 32 /* byte alignment (for hw parts) */,
2026 4096 /* can't cross 4K */);
2027 if (!fusbh200->qtd_pool) {
2028 goto fail;
2031 /* QHs for control/bulk/intr transfers */
2032 fusbh200->qh_pool = dma_pool_create ("fusbh200_qh",
2033 fusbh200_to_hcd(fusbh200)->self.controller,
2034 sizeof(struct fusbh200_qh_hw),
2035 32 /* byte alignment (for hw parts) */,
2036 4096 /* can't cross 4K */);
2037 if (!fusbh200->qh_pool) {
2038 goto fail;
2040 fusbh200->async = fusbh200_qh_alloc (fusbh200, flags);
2041 if (!fusbh200->async) {
2042 goto fail;
2045 /* ITD for high speed ISO transfers */
2046 fusbh200->itd_pool = dma_pool_create ("fusbh200_itd",
2047 fusbh200_to_hcd(fusbh200)->self.controller,
2048 sizeof (struct fusbh200_itd),
2049 64 /* byte alignment (for hw parts) */,
2050 4096 /* can't cross 4K */);
2051 if (!fusbh200->itd_pool) {
2052 goto fail;
2055 /* Hardware periodic table */
2056 fusbh200->periodic = (__le32 *)
2057 dma_alloc_coherent (fusbh200_to_hcd(fusbh200)->self.controller,
2058 fusbh200->periodic_size * sizeof(__le32),
2059 &fusbh200->periodic_dma, 0);
2060 if (fusbh200->periodic == NULL) {
2061 goto fail;
2064 for (i = 0; i < fusbh200->periodic_size; i++)
2065 fusbh200->periodic[i] = FUSBH200_LIST_END(fusbh200);
2067 /* software shadow of hardware table */
2068 fusbh200->pshadow = kcalloc(fusbh200->periodic_size, sizeof(void *), flags);
2069 if (fusbh200->pshadow != NULL)
2070 return 0;
2072 fail:
2073 fusbh200_dbg (fusbh200, "couldn't init memory\n");
2074 fusbh200_mem_cleanup (fusbh200);
2075 return -ENOMEM;
2077 /*-------------------------------------------------------------------------*/
2079 * EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
2081 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
2082 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
2083 * buffers needed for the larger number). We use one QH per endpoint, queue
2084 * multiple urbs (all three types) per endpoint. URBs may need several qtds.
2086 * ISO traffic uses "ISO TD" (itd) records, and (along with
2087 * interrupts) needs careful scheduling. Performance improvements can be
2088 * an ongoing challenge. That's in "ehci-sched.c".
2090 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
2091 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
2092 * (b) special fields in qh entries or (c) split iso entries. TTs will
2093 * buffer low/full speed data so the host collects it at high speed.
2096 /*-------------------------------------------------------------------------*/
2098 /* fill a qtd, returning how much of the buffer we were able to queue up */
2100 static int
2101 qtd_fill(struct fusbh200_hcd *fusbh200, struct fusbh200_qtd *qtd, dma_addr_t buf,
2102 size_t len, int token, int maxpacket)
2104 int i, count;
2105 u64 addr = buf;
2107 /* one buffer entry per 4K ... first might be short or unaligned */
2108 qtd->hw_buf[0] = cpu_to_hc32(fusbh200, (u32)addr);
2109 qtd->hw_buf_hi[0] = cpu_to_hc32(fusbh200, (u32)(addr >> 32));
2110 count = 0x1000 - (buf & 0x0fff); /* rest of that page */
2111 if (likely (len < count)) /* ... iff needed */
2112 count = len;
2113 else {
2114 buf += 0x1000;
2115 buf &= ~0x0fff;
2117 /* per-qtd limit: from 16K to 20K (best alignment) */
2118 for (i = 1; count < len && i < 5; i++) {
2119 addr = buf;
2120 qtd->hw_buf[i] = cpu_to_hc32(fusbh200, (u32)addr);
2121 qtd->hw_buf_hi[i] = cpu_to_hc32(fusbh200,
2122 (u32)(addr >> 32));
2123 buf += 0x1000;
2124 if ((count + 0x1000) < len)
2125 count += 0x1000;
2126 else
2127 count = len;
2130 /* short packets may only terminate transfers */
2131 if (count != len)
2132 count -= (count % maxpacket);
2134 qtd->hw_token = cpu_to_hc32(fusbh200, (count << 16) | token);
2135 qtd->length = count;
2137 return count;
2140 /*-------------------------------------------------------------------------*/
2142 static inline void
2143 qh_update (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh, struct fusbh200_qtd *qtd)
2145 struct fusbh200_qh_hw *hw = qh->hw;
2147 /* writes to an active overlay are unsafe */
2148 BUG_ON(qh->qh_state != QH_STATE_IDLE);
2150 hw->hw_qtd_next = QTD_NEXT(fusbh200, qtd->qtd_dma);
2151 hw->hw_alt_next = FUSBH200_LIST_END(fusbh200);
2153 /* Except for control endpoints, we make hardware maintain data
2154 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
2155 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
2156 * ever clear it.
2158 if (!(hw->hw_info1 & cpu_to_hc32(fusbh200, QH_TOGGLE_CTL))) {
2159 unsigned is_out, epnum;
2161 is_out = qh->is_out;
2162 epnum = (hc32_to_cpup(fusbh200, &hw->hw_info1) >> 8) & 0x0f;
2163 if (unlikely (!usb_gettoggle (qh->dev, epnum, is_out))) {
2164 hw->hw_token &= ~cpu_to_hc32(fusbh200, QTD_TOGGLE);
2165 usb_settoggle (qh->dev, epnum, is_out, 1);
2169 hw->hw_token &= cpu_to_hc32(fusbh200, QTD_TOGGLE | QTD_STS_PING);
2172 /* if it weren't for a common silicon quirk (writing the dummy into the qh
2173 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
2174 * recovery (including urb dequeue) would need software changes to a QH...
2176 static void
2177 qh_refresh (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
2179 struct fusbh200_qtd *qtd;
2181 if (list_empty (&qh->qtd_list))
2182 qtd = qh->dummy;
2183 else {
2184 qtd = list_entry (qh->qtd_list.next,
2185 struct fusbh200_qtd, qtd_list);
2187 * first qtd may already be partially processed.
2188 * If we come here during unlink, the QH overlay region
2189 * might have reference to the just unlinked qtd. The
2190 * qtd is updated in qh_completions(). Update the QH
2191 * overlay here.
2193 if (cpu_to_hc32(fusbh200, qtd->qtd_dma) == qh->hw->hw_current) {
2194 qh->hw->hw_qtd_next = qtd->hw_next;
2195 qtd = NULL;
2199 if (qtd)
2200 qh_update (fusbh200, qh, qtd);
2203 /*-------------------------------------------------------------------------*/
2205 static void qh_link_async(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh);
2207 static void fusbh200_clear_tt_buffer_complete(struct usb_hcd *hcd,
2208 struct usb_host_endpoint *ep)
2210 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
2211 struct fusbh200_qh *qh = ep->hcpriv;
2212 unsigned long flags;
2214 spin_lock_irqsave(&fusbh200->lock, flags);
2215 qh->clearing_tt = 0;
2216 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
2217 && fusbh200->rh_state == FUSBH200_RH_RUNNING)
2218 qh_link_async(fusbh200, qh);
2219 spin_unlock_irqrestore(&fusbh200->lock, flags);
2222 static void fusbh200_clear_tt_buffer(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh,
2223 struct urb *urb, u32 token)
2226 /* If an async split transaction gets an error or is unlinked,
2227 * the TT buffer may be left in an indeterminate state. We
2228 * have to clear the TT buffer.
2230 * Note: this routine is never called for Isochronous transfers.
2232 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
2233 #ifdef DEBUG
2234 struct usb_device *tt = urb->dev->tt->hub;
2235 dev_dbg(&tt->dev,
2236 "clear tt buffer port %d, a%d ep%d t%08x\n",
2237 urb->dev->ttport, urb->dev->devnum,
2238 usb_pipeendpoint(urb->pipe), token);
2239 #endif /* DEBUG */
2240 if (urb->dev->tt->hub !=
2241 fusbh200_to_hcd(fusbh200)->self.root_hub) {
2242 if (usb_hub_clear_tt_buffer(urb) == 0)
2243 qh->clearing_tt = 1;
2248 static int qtd_copy_status (
2249 struct fusbh200_hcd *fusbh200,
2250 struct urb *urb,
2251 size_t length,
2252 u32 token
2255 int status = -EINPROGRESS;
2257 /* count IN/OUT bytes, not SETUP (even short packets) */
2258 if (likely (QTD_PID (token) != 2))
2259 urb->actual_length += length - QTD_LENGTH (token);
2261 /* don't modify error codes */
2262 if (unlikely(urb->unlinked))
2263 return status;
2265 /* force cleanup after short read; not always an error */
2266 if (unlikely (IS_SHORT_READ (token)))
2267 status = -EREMOTEIO;
2269 /* serious "can't proceed" faults reported by the hardware */
2270 if (token & QTD_STS_HALT) {
2271 if (token & QTD_STS_BABBLE) {
2272 /* FIXME "must" disable babbling device's port too */
2273 status = -EOVERFLOW;
2274 /* CERR nonzero + halt --> stall */
2275 } else if (QTD_CERR(token)) {
2276 status = -EPIPE;
2278 /* In theory, more than one of the following bits can be set
2279 * since they are sticky and the transaction is retried.
2280 * Which to test first is rather arbitrary.
2282 } else if (token & QTD_STS_MMF) {
2283 /* fs/ls interrupt xfer missed the complete-split */
2284 status = -EPROTO;
2285 } else if (token & QTD_STS_DBE) {
2286 status = (QTD_PID (token) == 1) /* IN ? */
2287 ? -ENOSR /* hc couldn't read data */
2288 : -ECOMM; /* hc couldn't write data */
2289 } else if (token & QTD_STS_XACT) {
2290 /* timeout, bad CRC, wrong PID, etc */
2291 fusbh200_dbg(fusbh200, "devpath %s ep%d%s 3strikes\n",
2292 urb->dev->devpath,
2293 usb_pipeendpoint(urb->pipe),
2294 usb_pipein(urb->pipe) ? "in" : "out");
2295 status = -EPROTO;
2296 } else { /* unknown */
2297 status = -EPROTO;
2300 fusbh200_vdbg (fusbh200,
2301 "dev%d ep%d%s qtd token %08x --> status %d\n",
2302 usb_pipedevice (urb->pipe),
2303 usb_pipeendpoint (urb->pipe),
2304 usb_pipein (urb->pipe) ? "in" : "out",
2305 token, status);
2308 return status;
2311 static void
2312 fusbh200_urb_done(struct fusbh200_hcd *fusbh200, struct urb *urb, int status)
2313 __releases(fusbh200->lock)
2314 __acquires(fusbh200->lock)
2316 if (likely (urb->hcpriv != NULL)) {
2317 struct fusbh200_qh *qh = (struct fusbh200_qh *) urb->hcpriv;
2319 /* S-mask in a QH means it's an interrupt urb */
2320 if ((qh->hw->hw_info2 & cpu_to_hc32(fusbh200, QH_SMASK)) != 0) {
2322 /* ... update hc-wide periodic stats (for usbfs) */
2323 fusbh200_to_hcd(fusbh200)->self.bandwidth_int_reqs--;
2327 if (unlikely(urb->unlinked)) {
2328 COUNT(fusbh200->stats.unlink);
2329 } else {
2330 /* report non-error and short read status as zero */
2331 if (status == -EINPROGRESS || status == -EREMOTEIO)
2332 status = 0;
2333 COUNT(fusbh200->stats.complete);
2336 #ifdef FUSBH200_URB_TRACE
2337 fusbh200_dbg (fusbh200,
2338 "%s %s urb %p ep%d%s status %d len %d/%d\n",
2339 __func__, urb->dev->devpath, urb,
2340 usb_pipeendpoint (urb->pipe),
2341 usb_pipein (urb->pipe) ? "in" : "out",
2342 status,
2343 urb->actual_length, urb->transfer_buffer_length);
2344 #endif
2346 /* complete() can reenter this HCD */
2347 usb_hcd_unlink_urb_from_ep(fusbh200_to_hcd(fusbh200), urb);
2348 spin_unlock (&fusbh200->lock);
2349 usb_hcd_giveback_urb(fusbh200_to_hcd(fusbh200), urb, status);
2350 spin_lock (&fusbh200->lock);
2353 static int qh_schedule (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh);
2356 * Process and free completed qtds for a qh, returning URBs to drivers.
2357 * Chases up to qh->hw_current. Returns number of completions called,
2358 * indicating how much "real" work we did.
2360 static unsigned
2361 qh_completions (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
2363 struct fusbh200_qtd *last, *end = qh->dummy;
2364 struct list_head *entry, *tmp;
2365 int last_status;
2366 int stopped;
2367 unsigned count = 0;
2368 u8 state;
2369 struct fusbh200_qh_hw *hw = qh->hw;
2371 if (unlikely (list_empty (&qh->qtd_list)))
2372 return count;
2374 /* completions (or tasks on other cpus) must never clobber HALT
2375 * till we've gone through and cleaned everything up, even when
2376 * they add urbs to this qh's queue or mark them for unlinking.
2378 * NOTE: unlinking expects to be done in queue order.
2380 * It's a bug for qh->qh_state to be anything other than
2381 * QH_STATE_IDLE, unless our caller is scan_async() or
2382 * scan_intr().
2384 state = qh->qh_state;
2385 qh->qh_state = QH_STATE_COMPLETING;
2386 stopped = (state == QH_STATE_IDLE);
2388 rescan:
2389 last = NULL;
2390 last_status = -EINPROGRESS;
2391 qh->needs_rescan = 0;
2393 /* remove de-activated QTDs from front of queue.
2394 * after faults (including short reads), cleanup this urb
2395 * then let the queue advance.
2396 * if queue is stopped, handles unlinks.
2398 list_for_each_safe (entry, tmp, &qh->qtd_list) {
2399 struct fusbh200_qtd *qtd;
2400 struct urb *urb;
2401 u32 token = 0;
2403 qtd = list_entry (entry, struct fusbh200_qtd, qtd_list);
2404 urb = qtd->urb;
2406 /* clean up any state from previous QTD ...*/
2407 if (last) {
2408 if (likely (last->urb != urb)) {
2409 fusbh200_urb_done(fusbh200, last->urb, last_status);
2410 count++;
2411 last_status = -EINPROGRESS;
2413 fusbh200_qtd_free (fusbh200, last);
2414 last = NULL;
2417 /* ignore urbs submitted during completions we reported */
2418 if (qtd == end)
2419 break;
2421 /* hardware copies qtd out of qh overlay */
2422 rmb ();
2423 token = hc32_to_cpu(fusbh200, qtd->hw_token);
2425 /* always clean up qtds the hc de-activated */
2426 retry_xacterr:
2427 if ((token & QTD_STS_ACTIVE) == 0) {
2429 /* Report Data Buffer Error: non-fatal but useful */
2430 if (token & QTD_STS_DBE)
2431 fusbh200_dbg(fusbh200,
2432 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
2433 urb,
2434 usb_endpoint_num(&urb->ep->desc),
2435 usb_endpoint_dir_in(&urb->ep->desc) ? "in" : "out",
2436 urb->transfer_buffer_length,
2437 qtd,
2438 qh);
2440 /* on STALL, error, and short reads this urb must
2441 * complete and all its qtds must be recycled.
2443 if ((token & QTD_STS_HALT) != 0) {
2445 /* retry transaction errors until we
2446 * reach the software xacterr limit
2448 if ((token & QTD_STS_XACT) &&
2449 QTD_CERR(token) == 0 &&
2450 ++qh->xacterrs < QH_XACTERR_MAX &&
2451 !urb->unlinked) {
2452 fusbh200_dbg(fusbh200,
2453 "detected XactErr len %zu/%zu retry %d\n",
2454 qtd->length - QTD_LENGTH(token), qtd->length, qh->xacterrs);
2456 /* reset the token in the qtd and the
2457 * qh overlay (which still contains
2458 * the qtd) so that we pick up from
2459 * where we left off
2461 token &= ~QTD_STS_HALT;
2462 token |= QTD_STS_ACTIVE |
2463 (FUSBH200_TUNE_CERR << 10);
2464 qtd->hw_token = cpu_to_hc32(fusbh200,
2465 token);
2466 wmb();
2467 hw->hw_token = cpu_to_hc32(fusbh200,
2468 token);
2469 goto retry_xacterr;
2471 stopped = 1;
2473 /* magic dummy for some short reads; qh won't advance.
2474 * that silicon quirk can kick in with this dummy too.
2476 * other short reads won't stop the queue, including
2477 * control transfers (status stage handles that) or
2478 * most other single-qtd reads ... the queue stops if
2479 * URB_SHORT_NOT_OK was set so the driver submitting
2480 * the urbs could clean it up.
2482 } else if (IS_SHORT_READ (token)
2483 && !(qtd->hw_alt_next
2484 & FUSBH200_LIST_END(fusbh200))) {
2485 stopped = 1;
2488 /* stop scanning when we reach qtds the hc is using */
2489 } else if (likely (!stopped
2490 && fusbh200->rh_state >= FUSBH200_RH_RUNNING)) {
2491 break;
2493 /* scan the whole queue for unlinks whenever it stops */
2494 } else {
2495 stopped = 1;
2497 /* cancel everything if we halt, suspend, etc */
2498 if (fusbh200->rh_state < FUSBH200_RH_RUNNING)
2499 last_status = -ESHUTDOWN;
2501 /* this qtd is active; skip it unless a previous qtd
2502 * for its urb faulted, or its urb was canceled.
2504 else if (last_status == -EINPROGRESS && !urb->unlinked)
2505 continue;
2507 /* qh unlinked; token in overlay may be most current */
2508 if (state == QH_STATE_IDLE
2509 && cpu_to_hc32(fusbh200, qtd->qtd_dma)
2510 == hw->hw_current) {
2511 token = hc32_to_cpu(fusbh200, hw->hw_token);
2513 /* An unlink may leave an incomplete
2514 * async transaction in the TT buffer.
2515 * We have to clear it.
2517 fusbh200_clear_tt_buffer(fusbh200, qh, urb, token);
2521 /* unless we already know the urb's status, collect qtd status
2522 * and update count of bytes transferred. in common short read
2523 * cases with only one data qtd (including control transfers),
2524 * queue processing won't halt. but with two or more qtds (for
2525 * example, with a 32 KB transfer), when the first qtd gets a
2526 * short read the second must be removed by hand.
2528 if (last_status == -EINPROGRESS) {
2529 last_status = qtd_copy_status(fusbh200, urb,
2530 qtd->length, token);
2531 if (last_status == -EREMOTEIO
2532 && (qtd->hw_alt_next
2533 & FUSBH200_LIST_END(fusbh200)))
2534 last_status = -EINPROGRESS;
2536 /* As part of low/full-speed endpoint-halt processing
2537 * we must clear the TT buffer (11.17.5).
2539 if (unlikely(last_status != -EINPROGRESS &&
2540 last_status != -EREMOTEIO)) {
2541 /* The TT's in some hubs malfunction when they
2542 * receive this request following a STALL (they
2543 * stop sending isochronous packets). Since a
2544 * STALL can't leave the TT buffer in a busy
2545 * state (if you believe Figures 11-48 - 11-51
2546 * in the USB 2.0 spec), we won't clear the TT
2547 * buffer in this case. Strictly speaking this
2548 * is a violation of the spec.
2550 if (last_status != -EPIPE)
2551 fusbh200_clear_tt_buffer(fusbh200, qh, urb,
2552 token);
2556 /* if we're removing something not at the queue head,
2557 * patch the hardware queue pointer.
2559 if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
2560 last = list_entry (qtd->qtd_list.prev,
2561 struct fusbh200_qtd, qtd_list);
2562 last->hw_next = qtd->hw_next;
2565 /* remove qtd; it's recycled after possible urb completion */
2566 list_del (&qtd->qtd_list);
2567 last = qtd;
2569 /* reinit the xacterr counter for the next qtd */
2570 qh->xacterrs = 0;
2573 /* last urb's completion might still need calling */
2574 if (likely (last != NULL)) {
2575 fusbh200_urb_done(fusbh200, last->urb, last_status);
2576 count++;
2577 fusbh200_qtd_free (fusbh200, last);
2580 /* Do we need to rescan for URBs dequeued during a giveback? */
2581 if (unlikely(qh->needs_rescan)) {
2582 /* If the QH is already unlinked, do the rescan now. */
2583 if (state == QH_STATE_IDLE)
2584 goto rescan;
2586 /* Otherwise we have to wait until the QH is fully unlinked.
2587 * Our caller will start an unlink if qh->needs_rescan is
2588 * set. But if an unlink has already started, nothing needs
2589 * to be done.
2591 if (state != QH_STATE_LINKED)
2592 qh->needs_rescan = 0;
2595 /* restore original state; caller must unlink or relink */
2596 qh->qh_state = state;
2598 /* be sure the hardware's done with the qh before refreshing
2599 * it after fault cleanup, or recovering from silicon wrongly
2600 * overlaying the dummy qtd (which reduces DMA chatter).
2602 if (stopped != 0 || hw->hw_qtd_next == FUSBH200_LIST_END(fusbh200)) {
2603 switch (state) {
2604 case QH_STATE_IDLE:
2605 qh_refresh(fusbh200, qh);
2606 break;
2607 case QH_STATE_LINKED:
2608 /* We won't refresh a QH that's linked (after the HC
2609 * stopped the queue). That avoids a race:
2610 * - HC reads first part of QH;
2611 * - CPU updates that first part and the token;
2612 * - HC reads rest of that QH, including token
2613 * Result: HC gets an inconsistent image, and then
2614 * DMAs to/from the wrong memory (corrupting it).
2616 * That should be rare for interrupt transfers,
2617 * except maybe high bandwidth ...
2620 /* Tell the caller to start an unlink */
2621 qh->needs_rescan = 1;
2622 break;
2623 /* otherwise, unlink already started */
2627 return count;
2630 /*-------------------------------------------------------------------------*/
2632 // high bandwidth multiplier, as encoded in highspeed endpoint descriptors
2633 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
2634 // ... and packet size, for any kind of endpoint descriptor
2635 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
2638 * reverse of qh_urb_transaction: free a list of TDs.
2639 * used for cleanup after errors, before HC sees an URB's TDs.
2641 static void qtd_list_free (
2642 struct fusbh200_hcd *fusbh200,
2643 struct urb *urb,
2644 struct list_head *qtd_list
2646 struct list_head *entry, *temp;
2648 list_for_each_safe (entry, temp, qtd_list) {
2649 struct fusbh200_qtd *qtd;
2651 qtd = list_entry (entry, struct fusbh200_qtd, qtd_list);
2652 list_del (&qtd->qtd_list);
2653 fusbh200_qtd_free (fusbh200, qtd);
2658 * create a list of filled qtds for this URB; won't link into qh.
2660 static struct list_head *
2661 qh_urb_transaction (
2662 struct fusbh200_hcd *fusbh200,
2663 struct urb *urb,
2664 struct list_head *head,
2665 gfp_t flags
2667 struct fusbh200_qtd *qtd, *qtd_prev;
2668 dma_addr_t buf;
2669 int len, this_sg_len, maxpacket;
2670 int is_input;
2671 u32 token;
2672 int i;
2673 struct scatterlist *sg;
2676 * URBs map to sequences of QTDs: one logical transaction
2678 qtd = fusbh200_qtd_alloc (fusbh200, flags);
2679 if (unlikely (!qtd))
2680 return NULL;
2681 list_add_tail (&qtd->qtd_list, head);
2682 qtd->urb = urb;
2684 token = QTD_STS_ACTIVE;
2685 token |= (FUSBH200_TUNE_CERR << 10);
2686 /* for split transactions, SplitXState initialized to zero */
2688 len = urb->transfer_buffer_length;
2689 is_input = usb_pipein (urb->pipe);
2690 if (usb_pipecontrol (urb->pipe)) {
2691 /* SETUP pid */
2692 qtd_fill(fusbh200, qtd, urb->setup_dma,
2693 sizeof (struct usb_ctrlrequest),
2694 token | (2 /* "setup" */ << 8), 8);
2696 /* ... and always at least one more pid */
2697 token ^= QTD_TOGGLE;
2698 qtd_prev = qtd;
2699 qtd = fusbh200_qtd_alloc (fusbh200, flags);
2700 if (unlikely (!qtd))
2701 goto cleanup;
2702 qtd->urb = urb;
2703 qtd_prev->hw_next = QTD_NEXT(fusbh200, qtd->qtd_dma);
2704 list_add_tail (&qtd->qtd_list, head);
2706 /* for zero length DATA stages, STATUS is always IN */
2707 if (len == 0)
2708 token |= (1 /* "in" */ << 8);
2712 * data transfer stage: buffer setup
2714 i = urb->num_mapped_sgs;
2715 if (len > 0 && i > 0) {
2716 sg = urb->sg;
2717 buf = sg_dma_address(sg);
2719 /* urb->transfer_buffer_length may be smaller than the
2720 * size of the scatterlist (or vice versa)
2722 this_sg_len = min_t(int, sg_dma_len(sg), len);
2723 } else {
2724 sg = NULL;
2725 buf = urb->transfer_dma;
2726 this_sg_len = len;
2729 if (is_input)
2730 token |= (1 /* "in" */ << 8);
2731 /* else it's already initted to "out" pid (0 << 8) */
2733 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
2736 * buffer gets wrapped in one or more qtds;
2737 * last one may be "short" (including zero len)
2738 * and may serve as a control status ack
2740 for (;;) {
2741 int this_qtd_len;
2743 this_qtd_len = qtd_fill(fusbh200, qtd, buf, this_sg_len, token,
2744 maxpacket);
2745 this_sg_len -= this_qtd_len;
2746 len -= this_qtd_len;
2747 buf += this_qtd_len;
2750 * short reads advance to a "magic" dummy instead of the next
2751 * qtd ... that forces the queue to stop, for manual cleanup.
2752 * (this will usually be overridden later.)
2754 if (is_input)
2755 qtd->hw_alt_next = fusbh200->async->hw->hw_alt_next;
2757 /* qh makes control packets use qtd toggle; maybe switch it */
2758 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
2759 token ^= QTD_TOGGLE;
2761 if (likely(this_sg_len <= 0)) {
2762 if (--i <= 0 || len <= 0)
2763 break;
2764 sg = sg_next(sg);
2765 buf = sg_dma_address(sg);
2766 this_sg_len = min_t(int, sg_dma_len(sg), len);
2769 qtd_prev = qtd;
2770 qtd = fusbh200_qtd_alloc (fusbh200, flags);
2771 if (unlikely (!qtd))
2772 goto cleanup;
2773 qtd->urb = urb;
2774 qtd_prev->hw_next = QTD_NEXT(fusbh200, qtd->qtd_dma);
2775 list_add_tail (&qtd->qtd_list, head);
2779 * unless the caller requires manual cleanup after short reads,
2780 * have the alt_next mechanism keep the queue running after the
2781 * last data qtd (the only one, for control and most other cases).
2783 if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
2784 || usb_pipecontrol (urb->pipe)))
2785 qtd->hw_alt_next = FUSBH200_LIST_END(fusbh200);
2788 * control requests may need a terminating data "status" ack;
2789 * other OUT ones may need a terminating short packet
2790 * (zero length).
2792 if (likely (urb->transfer_buffer_length != 0)) {
2793 int one_more = 0;
2795 if (usb_pipecontrol (urb->pipe)) {
2796 one_more = 1;
2797 token ^= 0x0100; /* "in" <--> "out" */
2798 token |= QTD_TOGGLE; /* force DATA1 */
2799 } else if (usb_pipeout(urb->pipe)
2800 && (urb->transfer_flags & URB_ZERO_PACKET)
2801 && !(urb->transfer_buffer_length % maxpacket)) {
2802 one_more = 1;
2804 if (one_more) {
2805 qtd_prev = qtd;
2806 qtd = fusbh200_qtd_alloc (fusbh200, flags);
2807 if (unlikely (!qtd))
2808 goto cleanup;
2809 qtd->urb = urb;
2810 qtd_prev->hw_next = QTD_NEXT(fusbh200, qtd->qtd_dma);
2811 list_add_tail (&qtd->qtd_list, head);
2813 /* never any data in such packets */
2814 qtd_fill(fusbh200, qtd, 0, 0, token, 0);
2818 /* by default, enable interrupt on urb completion */
2819 if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
2820 qtd->hw_token |= cpu_to_hc32(fusbh200, QTD_IOC);
2821 return head;
2823 cleanup:
2824 qtd_list_free (fusbh200, urb, head);
2825 return NULL;
2828 /*-------------------------------------------------------------------------*/
2830 // Would be best to create all qh's from config descriptors,
2831 // when each interface/altsetting is established. Unlink
2832 // any previous qh and cancel its urbs first; endpoints are
2833 // implicitly reset then (data toggle too).
2834 // That'd mean updating how usbcore talks to HCDs. (2.7?)
2838 * Each QH holds a qtd list; a QH is used for everything except iso.
2840 * For interrupt urbs, the scheduler must set the microframe scheduling
2841 * mask(s) each time the QH gets scheduled. For highspeed, that's
2842 * just one microframe in the s-mask. For split interrupt transactions
2843 * there are additional complications: c-mask, maybe FSTNs.
2845 static struct fusbh200_qh *
2846 qh_make (
2847 struct fusbh200_hcd *fusbh200,
2848 struct urb *urb,
2849 gfp_t flags
2851 struct fusbh200_qh *qh = fusbh200_qh_alloc (fusbh200, flags);
2852 u32 info1 = 0, info2 = 0;
2853 int is_input, type;
2854 int maxp = 0;
2855 struct usb_tt *tt = urb->dev->tt;
2856 struct fusbh200_qh_hw *hw;
2858 if (!qh)
2859 return qh;
2862 * init endpoint/device data for this QH
2864 info1 |= usb_pipeendpoint (urb->pipe) << 8;
2865 info1 |= usb_pipedevice (urb->pipe) << 0;
2867 is_input = usb_pipein (urb->pipe);
2868 type = usb_pipetype (urb->pipe);
2869 maxp = usb_maxpacket (urb->dev, urb->pipe, !is_input);
2871 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
2872 * acts like up to 3KB, but is built from smaller packets.
2874 if (max_packet(maxp) > 1024) {
2875 fusbh200_dbg(fusbh200, "bogus qh maxpacket %d\n", max_packet(maxp));
2876 goto done;
2879 /* Compute interrupt scheduling parameters just once, and save.
2880 * - allowing for high bandwidth, how many nsec/uframe are used?
2881 * - split transactions need a second CSPLIT uframe; same question
2882 * - splits also need a schedule gap (for full/low speed I/O)
2883 * - qh has a polling interval
2885 * For control/bulk requests, the HC or TT handles these.
2887 if (type == PIPE_INTERRUPT) {
2888 qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
2889 is_input, 0,
2890 hb_mult(maxp) * max_packet(maxp)));
2891 qh->start = NO_FRAME;
2893 if (urb->dev->speed == USB_SPEED_HIGH) {
2894 qh->c_usecs = 0;
2895 qh->gap_uf = 0;
2897 qh->period = urb->interval >> 3;
2898 if (qh->period == 0 && urb->interval != 1) {
2899 /* NOTE interval 2 or 4 uframes could work.
2900 * But interval 1 scheduling is simpler, and
2901 * includes high bandwidth.
2903 urb->interval = 1;
2904 } else if (qh->period > fusbh200->periodic_size) {
2905 qh->period = fusbh200->periodic_size;
2906 urb->interval = qh->period << 3;
2908 } else {
2909 int think_time;
2911 /* gap is f(FS/LS transfer times) */
2912 qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,
2913 is_input, 0, maxp) / (125 * 1000);
2915 /* FIXME this just approximates SPLIT/CSPLIT times */
2916 if (is_input) { // SPLIT, gap, CSPLIT+DATA
2917 qh->c_usecs = qh->usecs + HS_USECS (0);
2918 qh->usecs = HS_USECS (1);
2919 } else { // SPLIT+DATA, gap, CSPLIT
2920 qh->usecs += HS_USECS (1);
2921 qh->c_usecs = HS_USECS (0);
2924 think_time = tt ? tt->think_time : 0;
2925 qh->tt_usecs = NS_TO_US (think_time +
2926 usb_calc_bus_time (urb->dev->speed,
2927 is_input, 0, max_packet (maxp)));
2928 qh->period = urb->interval;
2929 if (qh->period > fusbh200->periodic_size) {
2930 qh->period = fusbh200->periodic_size;
2931 urb->interval = qh->period;
2936 /* support for tt scheduling, and access to toggles */
2937 qh->dev = urb->dev;
2939 /* using TT? */
2940 switch (urb->dev->speed) {
2941 case USB_SPEED_LOW:
2942 info1 |= QH_LOW_SPEED;
2943 /* FALL THROUGH */
2945 case USB_SPEED_FULL:
2946 /* EPS 0 means "full" */
2947 if (type != PIPE_INTERRUPT)
2948 info1 |= (FUSBH200_TUNE_RL_TT << 28);
2949 if (type == PIPE_CONTROL) {
2950 info1 |= QH_CONTROL_EP; /* for TT */
2951 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2953 info1 |= maxp << 16;
2955 info2 |= (FUSBH200_TUNE_MULT_TT << 30);
2957 /* Some Freescale processors have an erratum in which the
2958 * port number in the queue head was 0..N-1 instead of 1..N.
2960 if (fusbh200_has_fsl_portno_bug(fusbh200))
2961 info2 |= (urb->dev->ttport-1) << 23;
2962 else
2963 info2 |= urb->dev->ttport << 23;
2965 /* set the address of the TT; for TDI's integrated
2966 * root hub tt, leave it zeroed.
2968 if (tt && tt->hub != fusbh200_to_hcd(fusbh200)->self.root_hub)
2969 info2 |= tt->hub->devnum << 16;
2971 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
2973 break;
2975 case USB_SPEED_HIGH: /* no TT involved */
2976 info1 |= QH_HIGH_SPEED;
2977 if (type == PIPE_CONTROL) {
2978 info1 |= (FUSBH200_TUNE_RL_HS << 28);
2979 info1 |= 64 << 16; /* usb2 fixed maxpacket */
2980 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2981 info2 |= (FUSBH200_TUNE_MULT_HS << 30);
2982 } else if (type == PIPE_BULK) {
2983 info1 |= (FUSBH200_TUNE_RL_HS << 28);
2984 /* The USB spec says that high speed bulk endpoints
2985 * always use 512 byte maxpacket. But some device
2986 * vendors decided to ignore that, and MSFT is happy
2987 * to help them do so. So now people expect to use
2988 * such nonconformant devices with Linux too; sigh.
2990 info1 |= max_packet(maxp) << 16;
2991 info2 |= (FUSBH200_TUNE_MULT_HS << 30);
2992 } else { /* PIPE_INTERRUPT */
2993 info1 |= max_packet (maxp) << 16;
2994 info2 |= hb_mult (maxp) << 30;
2996 break;
2997 default:
2998 fusbh200_dbg(fusbh200, "bogus dev %p speed %d\n", urb->dev,
2999 urb->dev->speed);
3000 done:
3001 qh_destroy(fusbh200, qh);
3002 return NULL;
3005 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
3007 /* init as live, toggle clear, advance to dummy */
3008 qh->qh_state = QH_STATE_IDLE;
3009 hw = qh->hw;
3010 hw->hw_info1 = cpu_to_hc32(fusbh200, info1);
3011 hw->hw_info2 = cpu_to_hc32(fusbh200, info2);
3012 qh->is_out = !is_input;
3013 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
3014 qh_refresh (fusbh200, qh);
3015 return qh;
3018 /*-------------------------------------------------------------------------*/
3020 static void enable_async(struct fusbh200_hcd *fusbh200)
3022 if (fusbh200->async_count++)
3023 return;
3025 /* Stop waiting to turn off the async schedule */
3026 fusbh200->enabled_hrtimer_events &= ~BIT(FUSBH200_HRTIMER_DISABLE_ASYNC);
3028 /* Don't start the schedule until ASS is 0 */
3029 fusbh200_poll_ASS(fusbh200);
3030 turn_on_io_watchdog(fusbh200);
3033 static void disable_async(struct fusbh200_hcd *fusbh200)
3035 if (--fusbh200->async_count)
3036 return;
3038 /* The async schedule and async_unlink list are supposed to be empty */
3039 WARN_ON(fusbh200->async->qh_next.qh || fusbh200->async_unlink);
3041 /* Don't turn off the schedule until ASS is 1 */
3042 fusbh200_poll_ASS(fusbh200);
3045 /* move qh (and its qtds) onto async queue; maybe enable queue. */
3047 static void qh_link_async (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3049 __hc32 dma = QH_NEXT(fusbh200, qh->qh_dma);
3050 struct fusbh200_qh *head;
3052 /* Don't link a QH if there's a Clear-TT-Buffer pending */
3053 if (unlikely(qh->clearing_tt))
3054 return;
3056 WARN_ON(qh->qh_state != QH_STATE_IDLE);
3058 /* clear halt and/or toggle; and maybe recover from silicon quirk */
3059 qh_refresh(fusbh200, qh);
3061 /* splice right after start */
3062 head = fusbh200->async;
3063 qh->qh_next = head->qh_next;
3064 qh->hw->hw_next = head->hw->hw_next;
3065 wmb ();
3067 head->qh_next.qh = qh;
3068 head->hw->hw_next = dma;
3070 qh->xacterrs = 0;
3071 qh->qh_state = QH_STATE_LINKED;
3072 /* qtd completions reported later by interrupt */
3074 enable_async(fusbh200);
3077 /*-------------------------------------------------------------------------*/
3080 * For control/bulk/interrupt, return QH with these TDs appended.
3081 * Allocates and initializes the QH if necessary.
3082 * Returns null if it can't allocate a QH it needs to.
3083 * If the QH has TDs (urbs) already, that's great.
3085 static struct fusbh200_qh *qh_append_tds (
3086 struct fusbh200_hcd *fusbh200,
3087 struct urb *urb,
3088 struct list_head *qtd_list,
3089 int epnum,
3090 void **ptr
3093 struct fusbh200_qh *qh = NULL;
3094 __hc32 qh_addr_mask = cpu_to_hc32(fusbh200, 0x7f);
3096 qh = (struct fusbh200_qh *) *ptr;
3097 if (unlikely (qh == NULL)) {
3098 /* can't sleep here, we have fusbh200->lock... */
3099 qh = qh_make (fusbh200, urb, GFP_ATOMIC);
3100 *ptr = qh;
3102 if (likely (qh != NULL)) {
3103 struct fusbh200_qtd *qtd;
3105 if (unlikely (list_empty (qtd_list)))
3106 qtd = NULL;
3107 else
3108 qtd = list_entry (qtd_list->next, struct fusbh200_qtd,
3109 qtd_list);
3111 /* control qh may need patching ... */
3112 if (unlikely (epnum == 0)) {
3114 /* usb_reset_device() briefly reverts to address 0 */
3115 if (usb_pipedevice (urb->pipe) == 0)
3116 qh->hw->hw_info1 &= ~qh_addr_mask;
3119 /* just one way to queue requests: swap with the dummy qtd.
3120 * only hc or qh_refresh() ever modify the overlay.
3122 if (likely (qtd != NULL)) {
3123 struct fusbh200_qtd *dummy;
3124 dma_addr_t dma;
3125 __hc32 token;
3127 /* to avoid racing the HC, use the dummy td instead of
3128 * the first td of our list (becomes new dummy). both
3129 * tds stay deactivated until we're done, when the
3130 * HC is allowed to fetch the old dummy (4.10.2).
3132 token = qtd->hw_token;
3133 qtd->hw_token = HALT_BIT(fusbh200);
3135 dummy = qh->dummy;
3137 dma = dummy->qtd_dma;
3138 *dummy = *qtd;
3139 dummy->qtd_dma = dma;
3141 list_del (&qtd->qtd_list);
3142 list_add (&dummy->qtd_list, qtd_list);
3143 list_splice_tail(qtd_list, &qh->qtd_list);
3145 fusbh200_qtd_init(fusbh200, qtd, qtd->qtd_dma);
3146 qh->dummy = qtd;
3148 /* hc must see the new dummy at list end */
3149 dma = qtd->qtd_dma;
3150 qtd = list_entry (qh->qtd_list.prev,
3151 struct fusbh200_qtd, qtd_list);
3152 qtd->hw_next = QTD_NEXT(fusbh200, dma);
3154 /* let the hc process these next qtds */
3155 wmb ();
3156 dummy->hw_token = token;
3158 urb->hcpriv = qh;
3161 return qh;
3164 /*-------------------------------------------------------------------------*/
3166 static int
3167 submit_async (
3168 struct fusbh200_hcd *fusbh200,
3169 struct urb *urb,
3170 struct list_head *qtd_list,
3171 gfp_t mem_flags
3173 int epnum;
3174 unsigned long flags;
3175 struct fusbh200_qh *qh = NULL;
3176 int rc;
3178 epnum = urb->ep->desc.bEndpointAddress;
3180 #ifdef FUSBH200_URB_TRACE
3182 struct fusbh200_qtd *qtd;
3183 qtd = list_entry(qtd_list->next, struct fusbh200_qtd, qtd_list);
3184 fusbh200_dbg(fusbh200,
3185 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
3186 __func__, urb->dev->devpath, urb,
3187 epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
3188 urb->transfer_buffer_length,
3189 qtd, urb->ep->hcpriv);
3191 #endif
3193 spin_lock_irqsave (&fusbh200->lock, flags);
3194 if (unlikely(!HCD_HW_ACCESSIBLE(fusbh200_to_hcd(fusbh200)))) {
3195 rc = -ESHUTDOWN;
3196 goto done;
3198 rc = usb_hcd_link_urb_to_ep(fusbh200_to_hcd(fusbh200), urb);
3199 if (unlikely(rc))
3200 goto done;
3202 qh = qh_append_tds(fusbh200, urb, qtd_list, epnum, &urb->ep->hcpriv);
3203 if (unlikely(qh == NULL)) {
3204 usb_hcd_unlink_urb_from_ep(fusbh200_to_hcd(fusbh200), urb);
3205 rc = -ENOMEM;
3206 goto done;
3209 /* Control/bulk operations through TTs don't need scheduling,
3210 * the HC and TT handle it when the TT has a buffer ready.
3212 if (likely (qh->qh_state == QH_STATE_IDLE))
3213 qh_link_async(fusbh200, qh);
3214 done:
3215 spin_unlock_irqrestore (&fusbh200->lock, flags);
3216 if (unlikely (qh == NULL))
3217 qtd_list_free (fusbh200, urb, qtd_list);
3218 return rc;
3221 /*-------------------------------------------------------------------------*/
3223 static void single_unlink_async(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3225 struct fusbh200_qh *prev;
3227 /* Add to the end of the list of QHs waiting for the next IAAD */
3228 qh->qh_state = QH_STATE_UNLINK;
3229 if (fusbh200->async_unlink)
3230 fusbh200->async_unlink_last->unlink_next = qh;
3231 else
3232 fusbh200->async_unlink = qh;
3233 fusbh200->async_unlink_last = qh;
3235 /* Unlink it from the schedule */
3236 prev = fusbh200->async;
3237 while (prev->qh_next.qh != qh)
3238 prev = prev->qh_next.qh;
3240 prev->hw->hw_next = qh->hw->hw_next;
3241 prev->qh_next = qh->qh_next;
3242 if (fusbh200->qh_scan_next == qh)
3243 fusbh200->qh_scan_next = qh->qh_next.qh;
3246 static void start_iaa_cycle(struct fusbh200_hcd *fusbh200, bool nested)
3249 * Do nothing if an IAA cycle is already running or
3250 * if one will be started shortly.
3252 if (fusbh200->async_iaa || fusbh200->async_unlinking)
3253 return;
3255 /* Do all the waiting QHs at once */
3256 fusbh200->async_iaa = fusbh200->async_unlink;
3257 fusbh200->async_unlink = NULL;
3259 /* If the controller isn't running, we don't have to wait for it */
3260 if (unlikely(fusbh200->rh_state < FUSBH200_RH_RUNNING)) {
3261 if (!nested) /* Avoid recursion */
3262 end_unlink_async(fusbh200);
3264 /* Otherwise start a new IAA cycle */
3265 } else if (likely(fusbh200->rh_state == FUSBH200_RH_RUNNING)) {
3266 /* Make sure the unlinks are all visible to the hardware */
3267 wmb();
3269 fusbh200_writel(fusbh200, fusbh200->command | CMD_IAAD,
3270 &fusbh200->regs->command);
3271 fusbh200_readl(fusbh200, &fusbh200->regs->command);
3272 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_IAA_WATCHDOG, true);
3276 /* the async qh for the qtds being unlinked are now gone from the HC */
3278 static void end_unlink_async(struct fusbh200_hcd *fusbh200)
3280 struct fusbh200_qh *qh;
3282 /* Process the idle QHs */
3283 restart:
3284 fusbh200->async_unlinking = true;
3285 while (fusbh200->async_iaa) {
3286 qh = fusbh200->async_iaa;
3287 fusbh200->async_iaa = qh->unlink_next;
3288 qh->unlink_next = NULL;
3290 qh->qh_state = QH_STATE_IDLE;
3291 qh->qh_next.qh = NULL;
3293 qh_completions(fusbh200, qh);
3294 if (!list_empty(&qh->qtd_list) &&
3295 fusbh200->rh_state == FUSBH200_RH_RUNNING)
3296 qh_link_async(fusbh200, qh);
3297 disable_async(fusbh200);
3299 fusbh200->async_unlinking = false;
3301 /* Start a new IAA cycle if any QHs are waiting for it */
3302 if (fusbh200->async_unlink) {
3303 start_iaa_cycle(fusbh200, true);
3304 if (unlikely(fusbh200->rh_state < FUSBH200_RH_RUNNING))
3305 goto restart;
3309 static void unlink_empty_async(struct fusbh200_hcd *fusbh200)
3311 struct fusbh200_qh *qh, *next;
3312 bool stopped = (fusbh200->rh_state < FUSBH200_RH_RUNNING);
3313 bool check_unlinks_later = false;
3315 /* Unlink all the async QHs that have been empty for a timer cycle */
3316 next = fusbh200->async->qh_next.qh;
3317 while (next) {
3318 qh = next;
3319 next = qh->qh_next.qh;
3321 if (list_empty(&qh->qtd_list) &&
3322 qh->qh_state == QH_STATE_LINKED) {
3323 if (!stopped && qh->unlink_cycle ==
3324 fusbh200->async_unlink_cycle)
3325 check_unlinks_later = true;
3326 else
3327 single_unlink_async(fusbh200, qh);
3331 /* Start a new IAA cycle if any QHs are waiting for it */
3332 if (fusbh200->async_unlink)
3333 start_iaa_cycle(fusbh200, false);
3335 /* QHs that haven't been empty for long enough will be handled later */
3336 if (check_unlinks_later) {
3337 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_ASYNC_UNLINKS, true);
3338 ++fusbh200->async_unlink_cycle;
3342 /* makes sure the async qh will become idle */
3343 /* caller must own fusbh200->lock */
3345 static void start_unlink_async(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3348 * If the QH isn't linked then there's nothing we can do
3349 * unless we were called during a giveback, in which case
3350 * qh_completions() has to deal with it.
3352 if (qh->qh_state != QH_STATE_LINKED) {
3353 if (qh->qh_state == QH_STATE_COMPLETING)
3354 qh->needs_rescan = 1;
3355 return;
3358 single_unlink_async(fusbh200, qh);
3359 start_iaa_cycle(fusbh200, false);
3362 /*-------------------------------------------------------------------------*/
3364 static void scan_async (struct fusbh200_hcd *fusbh200)
3366 struct fusbh200_qh *qh;
3367 bool check_unlinks_later = false;
3369 fusbh200->qh_scan_next = fusbh200->async->qh_next.qh;
3370 while (fusbh200->qh_scan_next) {
3371 qh = fusbh200->qh_scan_next;
3372 fusbh200->qh_scan_next = qh->qh_next.qh;
3373 rescan:
3374 /* clean any finished work for this qh */
3375 if (!list_empty(&qh->qtd_list)) {
3376 int temp;
3379 * Unlinks could happen here; completion reporting
3380 * drops the lock. That's why fusbh200->qh_scan_next
3381 * always holds the next qh to scan; if the next qh
3382 * gets unlinked then fusbh200->qh_scan_next is adjusted
3383 * in single_unlink_async().
3385 temp = qh_completions(fusbh200, qh);
3386 if (qh->needs_rescan) {
3387 start_unlink_async(fusbh200, qh);
3388 } else if (list_empty(&qh->qtd_list)
3389 && qh->qh_state == QH_STATE_LINKED) {
3390 qh->unlink_cycle = fusbh200->async_unlink_cycle;
3391 check_unlinks_later = true;
3392 } else if (temp != 0)
3393 goto rescan;
3398 * Unlink empty entries, reducing DMA usage as well
3399 * as HCD schedule-scanning costs. Delay for any qh
3400 * we just scanned, there's a not-unusual case that it
3401 * doesn't stay idle for long.
3403 if (check_unlinks_later && fusbh200->rh_state == FUSBH200_RH_RUNNING &&
3404 !(fusbh200->enabled_hrtimer_events &
3405 BIT(FUSBH200_HRTIMER_ASYNC_UNLINKS))) {
3406 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_ASYNC_UNLINKS, true);
3407 ++fusbh200->async_unlink_cycle;
3410 /*-------------------------------------------------------------------------*/
3412 * EHCI scheduled transaction support: interrupt, iso, split iso
3413 * These are called "periodic" transactions in the EHCI spec.
3415 * Note that for interrupt transfers, the QH/QTD manipulation is shared
3416 * with the "asynchronous" transaction support (control/bulk transfers).
3417 * The only real difference is in how interrupt transfers are scheduled.
3419 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
3420 * It keeps track of every ITD (or SITD) that's linked, and holds enough
3421 * pre-calculated schedule data to make appending to the queue be quick.
3424 static int fusbh200_get_frame (struct usb_hcd *hcd);
3426 /*-------------------------------------------------------------------------*/
3429 * periodic_next_shadow - return "next" pointer on shadow list
3430 * @periodic: host pointer to qh/itd
3431 * @tag: hardware tag for type of this record
3433 static union fusbh200_shadow *
3434 periodic_next_shadow(struct fusbh200_hcd *fusbh200, union fusbh200_shadow *periodic,
3435 __hc32 tag)
3437 switch (hc32_to_cpu(fusbh200, tag)) {
3438 case Q_TYPE_QH:
3439 return &periodic->qh->qh_next;
3440 case Q_TYPE_FSTN:
3441 return &periodic->fstn->fstn_next;
3442 default:
3443 return &periodic->itd->itd_next;
3447 static __hc32 *
3448 shadow_next_periodic(struct fusbh200_hcd *fusbh200, union fusbh200_shadow *periodic,
3449 __hc32 tag)
3451 switch (hc32_to_cpu(fusbh200, tag)) {
3452 /* our fusbh200_shadow.qh is actually software part */
3453 case Q_TYPE_QH:
3454 return &periodic->qh->hw->hw_next;
3455 /* others are hw parts */
3456 default:
3457 return periodic->hw_next;
3461 /* caller must hold fusbh200->lock */
3462 static void periodic_unlink (struct fusbh200_hcd *fusbh200, unsigned frame, void *ptr)
3464 union fusbh200_shadow *prev_p = &fusbh200->pshadow[frame];
3465 __hc32 *hw_p = &fusbh200->periodic[frame];
3466 union fusbh200_shadow here = *prev_p;
3468 /* find predecessor of "ptr"; hw and shadow lists are in sync */
3469 while (here.ptr && here.ptr != ptr) {
3470 prev_p = periodic_next_shadow(fusbh200, prev_p,
3471 Q_NEXT_TYPE(fusbh200, *hw_p));
3472 hw_p = shadow_next_periodic(fusbh200, &here,
3473 Q_NEXT_TYPE(fusbh200, *hw_p));
3474 here = *prev_p;
3476 /* an interrupt entry (at list end) could have been shared */
3477 if (!here.ptr)
3478 return;
3480 /* update shadow and hardware lists ... the old "next" pointers
3481 * from ptr may still be in use, the caller updates them.
3483 *prev_p = *periodic_next_shadow(fusbh200, &here,
3484 Q_NEXT_TYPE(fusbh200, *hw_p));
3486 *hw_p = *shadow_next_periodic(fusbh200, &here,
3487 Q_NEXT_TYPE(fusbh200, *hw_p));
3490 /* how many of the uframe's 125 usecs are allocated? */
3491 static unsigned short
3492 periodic_usecs (struct fusbh200_hcd *fusbh200, unsigned frame, unsigned uframe)
3494 __hc32 *hw_p = &fusbh200->periodic [frame];
3495 union fusbh200_shadow *q = &fusbh200->pshadow [frame];
3496 unsigned usecs = 0;
3497 struct fusbh200_qh_hw *hw;
3499 while (q->ptr) {
3500 switch (hc32_to_cpu(fusbh200, Q_NEXT_TYPE(fusbh200, *hw_p))) {
3501 case Q_TYPE_QH:
3502 hw = q->qh->hw;
3503 /* is it in the S-mask? */
3504 if (hw->hw_info2 & cpu_to_hc32(fusbh200, 1 << uframe))
3505 usecs += q->qh->usecs;
3506 /* ... or C-mask? */
3507 if (hw->hw_info2 & cpu_to_hc32(fusbh200,
3508 1 << (8 + uframe)))
3509 usecs += q->qh->c_usecs;
3510 hw_p = &hw->hw_next;
3511 q = &q->qh->qh_next;
3512 break;
3513 // case Q_TYPE_FSTN:
3514 default:
3515 /* for "save place" FSTNs, count the relevant INTR
3516 * bandwidth from the previous frame
3518 if (q->fstn->hw_prev != FUSBH200_LIST_END(fusbh200)) {
3519 fusbh200_dbg (fusbh200, "ignoring FSTN cost ...\n");
3521 hw_p = &q->fstn->hw_next;
3522 q = &q->fstn->fstn_next;
3523 break;
3524 case Q_TYPE_ITD:
3525 if (q->itd->hw_transaction[uframe])
3526 usecs += q->itd->stream->usecs;
3527 hw_p = &q->itd->hw_next;
3528 q = &q->itd->itd_next;
3529 break;
3532 #ifdef DEBUG
3533 if (usecs > fusbh200->uframe_periodic_max)
3534 fusbh200_err (fusbh200, "uframe %d sched overrun: %d usecs\n",
3535 frame * 8 + uframe, usecs);
3536 #endif
3537 return usecs;
3540 /*-------------------------------------------------------------------------*/
3542 static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
3544 if (!dev1->tt || !dev2->tt)
3545 return 0;
3546 if (dev1->tt != dev2->tt)
3547 return 0;
3548 if (dev1->tt->multi)
3549 return dev1->ttport == dev2->ttport;
3550 else
3551 return 1;
3554 /* return true iff the device's transaction translator is available
3555 * for a periodic transfer starting at the specified frame, using
3556 * all the uframes in the mask.
3558 static int tt_no_collision (
3559 struct fusbh200_hcd *fusbh200,
3560 unsigned period,
3561 struct usb_device *dev,
3562 unsigned frame,
3563 u32 uf_mask
3566 if (period == 0) /* error */
3567 return 0;
3569 /* note bandwidth wastage: split never follows csplit
3570 * (different dev or endpoint) until the next uframe.
3571 * calling convention doesn't make that distinction.
3573 for (; frame < fusbh200->periodic_size; frame += period) {
3574 union fusbh200_shadow here;
3575 __hc32 type;
3576 struct fusbh200_qh_hw *hw;
3578 here = fusbh200->pshadow [frame];
3579 type = Q_NEXT_TYPE(fusbh200, fusbh200->periodic [frame]);
3580 while (here.ptr) {
3581 switch (hc32_to_cpu(fusbh200, type)) {
3582 case Q_TYPE_ITD:
3583 type = Q_NEXT_TYPE(fusbh200, here.itd->hw_next);
3584 here = here.itd->itd_next;
3585 continue;
3586 case Q_TYPE_QH:
3587 hw = here.qh->hw;
3588 if (same_tt (dev, here.qh->dev)) {
3589 u32 mask;
3591 mask = hc32_to_cpu(fusbh200,
3592 hw->hw_info2);
3593 /* "knows" no gap is needed */
3594 mask |= mask >> 8;
3595 if (mask & uf_mask)
3596 break;
3598 type = Q_NEXT_TYPE(fusbh200, hw->hw_next);
3599 here = here.qh->qh_next;
3600 continue;
3601 // case Q_TYPE_FSTN:
3602 default:
3603 fusbh200_dbg (fusbh200,
3604 "periodic frame %d bogus type %d\n",
3605 frame, type);
3608 /* collision or error */
3609 return 0;
3613 /* no collision */
3614 return 1;
3617 /*-------------------------------------------------------------------------*/
3619 static void enable_periodic(struct fusbh200_hcd *fusbh200)
3621 if (fusbh200->periodic_count++)
3622 return;
3624 /* Stop waiting to turn off the periodic schedule */
3625 fusbh200->enabled_hrtimer_events &= ~BIT(FUSBH200_HRTIMER_DISABLE_PERIODIC);
3627 /* Don't start the schedule until PSS is 0 */
3628 fusbh200_poll_PSS(fusbh200);
3629 turn_on_io_watchdog(fusbh200);
3632 static void disable_periodic(struct fusbh200_hcd *fusbh200)
3634 if (--fusbh200->periodic_count)
3635 return;
3637 /* Don't turn off the schedule until PSS is 1 */
3638 fusbh200_poll_PSS(fusbh200);
3641 /*-------------------------------------------------------------------------*/
3643 /* periodic schedule slots have iso tds (normal or split) first, then a
3644 * sparse tree for active interrupt transfers.
3646 * this just links in a qh; caller guarantees uframe masks are set right.
3647 * no FSTN support (yet; fusbh200 0.96+)
3649 static void qh_link_periodic(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3651 unsigned i;
3652 unsigned period = qh->period;
3654 dev_dbg (&qh->dev->dev,
3655 "link qh%d-%04x/%p start %d [%d/%d us]\n",
3656 period, hc32_to_cpup(fusbh200, &qh->hw->hw_info2)
3657 & (QH_CMASK | QH_SMASK),
3658 qh, qh->start, qh->usecs, qh->c_usecs);
3660 /* high bandwidth, or otherwise every microframe */
3661 if (period == 0)
3662 period = 1;
3664 for (i = qh->start; i < fusbh200->periodic_size; i += period) {
3665 union fusbh200_shadow *prev = &fusbh200->pshadow[i];
3666 __hc32 *hw_p = &fusbh200->periodic[i];
3667 union fusbh200_shadow here = *prev;
3668 __hc32 type = 0;
3670 /* skip the iso nodes at list head */
3671 while (here.ptr) {
3672 type = Q_NEXT_TYPE(fusbh200, *hw_p);
3673 if (type == cpu_to_hc32(fusbh200, Q_TYPE_QH))
3674 break;
3675 prev = periodic_next_shadow(fusbh200, prev, type);
3676 hw_p = shadow_next_periodic(fusbh200, &here, type);
3677 here = *prev;
3680 /* sorting each branch by period (slow-->fast)
3681 * enables sharing interior tree nodes
3683 while (here.ptr && qh != here.qh) {
3684 if (qh->period > here.qh->period)
3685 break;
3686 prev = &here.qh->qh_next;
3687 hw_p = &here.qh->hw->hw_next;
3688 here = *prev;
3690 /* link in this qh, unless some earlier pass did that */
3691 if (qh != here.qh) {
3692 qh->qh_next = here;
3693 if (here.qh)
3694 qh->hw->hw_next = *hw_p;
3695 wmb ();
3696 prev->qh = qh;
3697 *hw_p = QH_NEXT (fusbh200, qh->qh_dma);
3700 qh->qh_state = QH_STATE_LINKED;
3701 qh->xacterrs = 0;
3703 /* update per-qh bandwidth for usbfs */
3704 fusbh200_to_hcd(fusbh200)->self.bandwidth_allocated += qh->period
3705 ? ((qh->usecs + qh->c_usecs) / qh->period)
3706 : (qh->usecs * 8);
3708 list_add(&qh->intr_node, &fusbh200->intr_qh_list);
3710 /* maybe enable periodic schedule processing */
3711 ++fusbh200->intr_count;
3712 enable_periodic(fusbh200);
3715 static void qh_unlink_periodic(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3717 unsigned i;
3718 unsigned period;
3721 * If qh is for a low/full-speed device, simply unlinking it
3722 * could interfere with an ongoing split transaction. To unlink
3723 * it safely would require setting the QH_INACTIVATE bit and
3724 * waiting at least one frame, as described in EHCI 4.12.2.5.
3726 * We won't bother with any of this. Instead, we assume that the
3727 * only reason for unlinking an interrupt QH while the current URB
3728 * is still active is to dequeue all the URBs (flush the whole
3729 * endpoint queue).
3731 * If rebalancing the periodic schedule is ever implemented, this
3732 * approach will no longer be valid.
3735 /* high bandwidth, or otherwise part of every microframe */
3736 if ((period = qh->period) == 0)
3737 period = 1;
3739 for (i = qh->start; i < fusbh200->periodic_size; i += period)
3740 periodic_unlink (fusbh200, i, qh);
3742 /* update per-qh bandwidth for usbfs */
3743 fusbh200_to_hcd(fusbh200)->self.bandwidth_allocated -= qh->period
3744 ? ((qh->usecs + qh->c_usecs) / qh->period)
3745 : (qh->usecs * 8);
3747 dev_dbg (&qh->dev->dev,
3748 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
3749 qh->period,
3750 hc32_to_cpup(fusbh200, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
3751 qh, qh->start, qh->usecs, qh->c_usecs);
3753 /* qh->qh_next still "live" to HC */
3754 qh->qh_state = QH_STATE_UNLINK;
3755 qh->qh_next.ptr = NULL;
3757 if (fusbh200->qh_scan_next == qh)
3758 fusbh200->qh_scan_next = list_entry(qh->intr_node.next,
3759 struct fusbh200_qh, intr_node);
3760 list_del(&qh->intr_node);
3763 static void start_unlink_intr(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3765 /* If the QH isn't linked then there's nothing we can do
3766 * unless we were called during a giveback, in which case
3767 * qh_completions() has to deal with it.
3769 if (qh->qh_state != QH_STATE_LINKED) {
3770 if (qh->qh_state == QH_STATE_COMPLETING)
3771 qh->needs_rescan = 1;
3772 return;
3775 qh_unlink_periodic (fusbh200, qh);
3777 /* Make sure the unlinks are visible before starting the timer */
3778 wmb();
3781 * The EHCI spec doesn't say how long it takes the controller to
3782 * stop accessing an unlinked interrupt QH. The timer delay is
3783 * 9 uframes; presumably that will be long enough.
3785 qh->unlink_cycle = fusbh200->intr_unlink_cycle;
3787 /* New entries go at the end of the intr_unlink list */
3788 if (fusbh200->intr_unlink)
3789 fusbh200->intr_unlink_last->unlink_next = qh;
3790 else
3791 fusbh200->intr_unlink = qh;
3792 fusbh200->intr_unlink_last = qh;
3794 if (fusbh200->intr_unlinking)
3795 ; /* Avoid recursive calls */
3796 else if (fusbh200->rh_state < FUSBH200_RH_RUNNING)
3797 fusbh200_handle_intr_unlinks(fusbh200);
3798 else if (fusbh200->intr_unlink == qh) {
3799 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_UNLINK_INTR, true);
3800 ++fusbh200->intr_unlink_cycle;
3804 static void end_unlink_intr(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3806 struct fusbh200_qh_hw *hw = qh->hw;
3807 int rc;
3809 qh->qh_state = QH_STATE_IDLE;
3810 hw->hw_next = FUSBH200_LIST_END(fusbh200);
3812 qh_completions(fusbh200, qh);
3814 /* reschedule QH iff another request is queued */
3815 if (!list_empty(&qh->qtd_list) && fusbh200->rh_state == FUSBH200_RH_RUNNING) {
3816 rc = qh_schedule(fusbh200, qh);
3818 /* An error here likely indicates handshake failure
3819 * or no space left in the schedule. Neither fault
3820 * should happen often ...
3822 * FIXME kill the now-dysfunctional queued urbs
3824 if (rc != 0)
3825 fusbh200_err(fusbh200, "can't reschedule qh %p, err %d\n",
3826 qh, rc);
3829 /* maybe turn off periodic schedule */
3830 --fusbh200->intr_count;
3831 disable_periodic(fusbh200);
3834 /*-------------------------------------------------------------------------*/
3836 static int check_period (
3837 struct fusbh200_hcd *fusbh200,
3838 unsigned frame,
3839 unsigned uframe,
3840 unsigned period,
3841 unsigned usecs
3843 int claimed;
3845 /* complete split running into next frame?
3846 * given FSTN support, we could sometimes check...
3848 if (uframe >= 8)
3849 return 0;
3851 /* convert "usecs we need" to "max already claimed" */
3852 usecs = fusbh200->uframe_periodic_max - usecs;
3854 /* we "know" 2 and 4 uframe intervals were rejected; so
3855 * for period 0, check _every_ microframe in the schedule.
3857 if (unlikely (period == 0)) {
3858 do {
3859 for (uframe = 0; uframe < 7; uframe++) {
3860 claimed = periodic_usecs (fusbh200, frame, uframe);
3861 if (claimed > usecs)
3862 return 0;
3864 } while ((frame += 1) < fusbh200->periodic_size);
3866 /* just check the specified uframe, at that period */
3867 } else {
3868 do {
3869 claimed = periodic_usecs (fusbh200, frame, uframe);
3870 if (claimed > usecs)
3871 return 0;
3872 } while ((frame += period) < fusbh200->periodic_size);
3875 // success!
3876 return 1;
3879 static int check_intr_schedule (
3880 struct fusbh200_hcd *fusbh200,
3881 unsigned frame,
3882 unsigned uframe,
3883 const struct fusbh200_qh *qh,
3884 __hc32 *c_maskp
3887 int retval = -ENOSPC;
3888 u8 mask = 0;
3890 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
3891 goto done;
3893 if (!check_period (fusbh200, frame, uframe, qh->period, qh->usecs))
3894 goto done;
3895 if (!qh->c_usecs) {
3896 retval = 0;
3897 *c_maskp = 0;
3898 goto done;
3901 /* Make sure this tt's buffer is also available for CSPLITs.
3902 * We pessimize a bit; probably the typical full speed case
3903 * doesn't need the second CSPLIT.
3905 * NOTE: both SPLIT and CSPLIT could be checked in just
3906 * one smart pass...
3908 mask = 0x03 << (uframe + qh->gap_uf);
3909 *c_maskp = cpu_to_hc32(fusbh200, mask << 8);
3911 mask |= 1 << uframe;
3912 if (tt_no_collision (fusbh200, qh->period, qh->dev, frame, mask)) {
3913 if (!check_period (fusbh200, frame, uframe + qh->gap_uf + 1,
3914 qh->period, qh->c_usecs))
3915 goto done;
3916 if (!check_period (fusbh200, frame, uframe + qh->gap_uf,
3917 qh->period, qh->c_usecs))
3918 goto done;
3919 retval = 0;
3921 done:
3922 return retval;
3925 /* "first fit" scheduling policy used the first time through,
3926 * or when the previous schedule slot can't be re-used.
3928 static int qh_schedule(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3930 int status;
3931 unsigned uframe;
3932 __hc32 c_mask;
3933 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
3934 struct fusbh200_qh_hw *hw = qh->hw;
3936 qh_refresh(fusbh200, qh);
3937 hw->hw_next = FUSBH200_LIST_END(fusbh200);
3938 frame = qh->start;
3940 /* reuse the previous schedule slots, if we can */
3941 if (frame < qh->period) {
3942 uframe = ffs(hc32_to_cpup(fusbh200, &hw->hw_info2) & QH_SMASK);
3943 status = check_intr_schedule (fusbh200, frame, --uframe,
3944 qh, &c_mask);
3945 } else {
3946 uframe = 0;
3947 c_mask = 0;
3948 status = -ENOSPC;
3951 /* else scan the schedule to find a group of slots such that all
3952 * uframes have enough periodic bandwidth available.
3954 if (status) {
3955 /* "normal" case, uframing flexible except with splits */
3956 if (qh->period) {
3957 int i;
3959 for (i = qh->period; status && i > 0; --i) {
3960 frame = ++fusbh200->random_frame % qh->period;
3961 for (uframe = 0; uframe < 8; uframe++) {
3962 status = check_intr_schedule (fusbh200,
3963 frame, uframe, qh,
3964 &c_mask);
3965 if (status == 0)
3966 break;
3970 /* qh->period == 0 means every uframe */
3971 } else {
3972 frame = 0;
3973 status = check_intr_schedule (fusbh200, 0, 0, qh, &c_mask);
3975 if (status)
3976 goto done;
3977 qh->start = frame;
3979 /* reset S-frame and (maybe) C-frame masks */
3980 hw->hw_info2 &= cpu_to_hc32(fusbh200, ~(QH_CMASK | QH_SMASK));
3981 hw->hw_info2 |= qh->period
3982 ? cpu_to_hc32(fusbh200, 1 << uframe)
3983 : cpu_to_hc32(fusbh200, QH_SMASK);
3984 hw->hw_info2 |= c_mask;
3985 } else
3986 fusbh200_dbg (fusbh200, "reused qh %p schedule\n", qh);
3988 /* stuff into the periodic schedule */
3989 qh_link_periodic(fusbh200, qh);
3990 done:
3991 return status;
3994 static int intr_submit (
3995 struct fusbh200_hcd *fusbh200,
3996 struct urb *urb,
3997 struct list_head *qtd_list,
3998 gfp_t mem_flags
4000 unsigned epnum;
4001 unsigned long flags;
4002 struct fusbh200_qh *qh;
4003 int status;
4004 struct list_head empty;
4006 /* get endpoint and transfer/schedule data */
4007 epnum = urb->ep->desc.bEndpointAddress;
4009 spin_lock_irqsave (&fusbh200->lock, flags);
4011 if (unlikely(!HCD_HW_ACCESSIBLE(fusbh200_to_hcd(fusbh200)))) {
4012 status = -ESHUTDOWN;
4013 goto done_not_linked;
4015 status = usb_hcd_link_urb_to_ep(fusbh200_to_hcd(fusbh200), urb);
4016 if (unlikely(status))
4017 goto done_not_linked;
4019 /* get qh and force any scheduling errors */
4020 INIT_LIST_HEAD (&empty);
4021 qh = qh_append_tds(fusbh200, urb, &empty, epnum, &urb->ep->hcpriv);
4022 if (qh == NULL) {
4023 status = -ENOMEM;
4024 goto done;
4026 if (qh->qh_state == QH_STATE_IDLE) {
4027 if ((status = qh_schedule (fusbh200, qh)) != 0)
4028 goto done;
4031 /* then queue the urb's tds to the qh */
4032 qh = qh_append_tds(fusbh200, urb, qtd_list, epnum, &urb->ep->hcpriv);
4033 BUG_ON (qh == NULL);
4035 /* ... update usbfs periodic stats */
4036 fusbh200_to_hcd(fusbh200)->self.bandwidth_int_reqs++;
4038 done:
4039 if (unlikely(status))
4040 usb_hcd_unlink_urb_from_ep(fusbh200_to_hcd(fusbh200), urb);
4041 done_not_linked:
4042 spin_unlock_irqrestore (&fusbh200->lock, flags);
4043 if (status)
4044 qtd_list_free (fusbh200, urb, qtd_list);
4046 return status;
4049 static void scan_intr(struct fusbh200_hcd *fusbh200)
4051 struct fusbh200_qh *qh;
4053 list_for_each_entry_safe(qh, fusbh200->qh_scan_next, &fusbh200->intr_qh_list,
4054 intr_node) {
4055 rescan:
4056 /* clean any finished work for this qh */
4057 if (!list_empty(&qh->qtd_list)) {
4058 int temp;
4061 * Unlinks could happen here; completion reporting
4062 * drops the lock. That's why fusbh200->qh_scan_next
4063 * always holds the next qh to scan; if the next qh
4064 * gets unlinked then fusbh200->qh_scan_next is adjusted
4065 * in qh_unlink_periodic().
4067 temp = qh_completions(fusbh200, qh);
4068 if (unlikely(qh->needs_rescan ||
4069 (list_empty(&qh->qtd_list) &&
4070 qh->qh_state == QH_STATE_LINKED)))
4071 start_unlink_intr(fusbh200, qh);
4072 else if (temp != 0)
4073 goto rescan;
4078 /*-------------------------------------------------------------------------*/
4080 /* fusbh200_iso_stream ops work with both ITD and SITD */
4082 static struct fusbh200_iso_stream *
4083 iso_stream_alloc (gfp_t mem_flags)
4085 struct fusbh200_iso_stream *stream;
4087 stream = kzalloc(sizeof *stream, mem_flags);
4088 if (likely (stream != NULL)) {
4089 INIT_LIST_HEAD(&stream->td_list);
4090 INIT_LIST_HEAD(&stream->free_list);
4091 stream->next_uframe = -1;
4093 return stream;
4096 static void
4097 iso_stream_init (
4098 struct fusbh200_hcd *fusbh200,
4099 struct fusbh200_iso_stream *stream,
4100 struct usb_device *dev,
4101 int pipe,
4102 unsigned interval
4105 u32 buf1;
4106 unsigned epnum, maxp;
4107 int is_input;
4108 long bandwidth;
4109 unsigned multi;
4112 * this might be a "high bandwidth" highspeed endpoint,
4113 * as encoded in the ep descriptor's wMaxPacket field
4115 epnum = usb_pipeendpoint (pipe);
4116 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
4117 maxp = usb_maxpacket(dev, pipe, !is_input);
4118 if (is_input) {
4119 buf1 = (1 << 11);
4120 } else {
4121 buf1 = 0;
4124 maxp = max_packet(maxp);
4125 multi = hb_mult(maxp);
4126 buf1 |= maxp;
4127 maxp *= multi;
4129 stream->buf0 = cpu_to_hc32(fusbh200, (epnum << 8) | dev->devnum);
4130 stream->buf1 = cpu_to_hc32(fusbh200, buf1);
4131 stream->buf2 = cpu_to_hc32(fusbh200, multi);
4133 /* usbfs wants to report the average usecs per frame tied up
4134 * when transfers on this endpoint are scheduled ...
4136 if (dev->speed == USB_SPEED_FULL) {
4137 interval <<= 3;
4138 stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
4139 is_input, 1, maxp));
4140 stream->usecs /= 8;
4141 } else {
4142 stream->highspeed = 1;
4143 stream->usecs = HS_USECS_ISO (maxp);
4145 bandwidth = stream->usecs * 8;
4146 bandwidth /= interval;
4148 stream->bandwidth = bandwidth;
4149 stream->udev = dev;
4150 stream->bEndpointAddress = is_input | epnum;
4151 stream->interval = interval;
4152 stream->maxp = maxp;
4155 static struct fusbh200_iso_stream *
4156 iso_stream_find (struct fusbh200_hcd *fusbh200, struct urb *urb)
4158 unsigned epnum;
4159 struct fusbh200_iso_stream *stream;
4160 struct usb_host_endpoint *ep;
4161 unsigned long flags;
4163 epnum = usb_pipeendpoint (urb->pipe);
4164 if (usb_pipein(urb->pipe))
4165 ep = urb->dev->ep_in[epnum];
4166 else
4167 ep = urb->dev->ep_out[epnum];
4169 spin_lock_irqsave (&fusbh200->lock, flags);
4170 stream = ep->hcpriv;
4172 if (unlikely (stream == NULL)) {
4173 stream = iso_stream_alloc(GFP_ATOMIC);
4174 if (likely (stream != NULL)) {
4175 ep->hcpriv = stream;
4176 stream->ep = ep;
4177 iso_stream_init(fusbh200, stream, urb->dev, urb->pipe,
4178 urb->interval);
4181 /* if dev->ep [epnum] is a QH, hw is set */
4182 } else if (unlikely (stream->hw != NULL)) {
4183 fusbh200_dbg (fusbh200, "dev %s ep%d%s, not iso??\n",
4184 urb->dev->devpath, epnum,
4185 usb_pipein(urb->pipe) ? "in" : "out");
4186 stream = NULL;
4189 spin_unlock_irqrestore (&fusbh200->lock, flags);
4190 return stream;
4193 /*-------------------------------------------------------------------------*/
4195 /* fusbh200_iso_sched ops can be ITD-only or SITD-only */
4197 static struct fusbh200_iso_sched *
4198 iso_sched_alloc (unsigned packets, gfp_t mem_flags)
4200 struct fusbh200_iso_sched *iso_sched;
4201 int size = sizeof *iso_sched;
4203 size += packets * sizeof (struct fusbh200_iso_packet);
4204 iso_sched = kzalloc(size, mem_flags);
4205 if (likely (iso_sched != NULL)) {
4206 INIT_LIST_HEAD (&iso_sched->td_list);
4208 return iso_sched;
4211 static inline void
4212 itd_sched_init(
4213 struct fusbh200_hcd *fusbh200,
4214 struct fusbh200_iso_sched *iso_sched,
4215 struct fusbh200_iso_stream *stream,
4216 struct urb *urb
4219 unsigned i;
4220 dma_addr_t dma = urb->transfer_dma;
4222 /* how many uframes are needed for these transfers */
4223 iso_sched->span = urb->number_of_packets * stream->interval;
4225 /* figure out per-uframe itd fields that we'll need later
4226 * when we fit new itds into the schedule.
4228 for (i = 0; i < urb->number_of_packets; i++) {
4229 struct fusbh200_iso_packet *uframe = &iso_sched->packet [i];
4230 unsigned length;
4231 dma_addr_t buf;
4232 u32 trans;
4234 length = urb->iso_frame_desc [i].length;
4235 buf = dma + urb->iso_frame_desc [i].offset;
4237 trans = FUSBH200_ISOC_ACTIVE;
4238 trans |= buf & 0x0fff;
4239 if (unlikely (((i + 1) == urb->number_of_packets))
4240 && !(urb->transfer_flags & URB_NO_INTERRUPT))
4241 trans |= FUSBH200_ITD_IOC;
4242 trans |= length << 16;
4243 uframe->transaction = cpu_to_hc32(fusbh200, trans);
4245 /* might need to cross a buffer page within a uframe */
4246 uframe->bufp = (buf & ~(u64)0x0fff);
4247 buf += length;
4248 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
4249 uframe->cross = 1;
4253 static void
4254 iso_sched_free (
4255 struct fusbh200_iso_stream *stream,
4256 struct fusbh200_iso_sched *iso_sched
4259 if (!iso_sched)
4260 return;
4261 // caller must hold fusbh200->lock!
4262 list_splice (&iso_sched->td_list, &stream->free_list);
4263 kfree (iso_sched);
4266 static int
4267 itd_urb_transaction (
4268 struct fusbh200_iso_stream *stream,
4269 struct fusbh200_hcd *fusbh200,
4270 struct urb *urb,
4271 gfp_t mem_flags
4274 struct fusbh200_itd *itd;
4275 dma_addr_t itd_dma;
4276 int i;
4277 unsigned num_itds;
4278 struct fusbh200_iso_sched *sched;
4279 unsigned long flags;
4281 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
4282 if (unlikely (sched == NULL))
4283 return -ENOMEM;
4285 itd_sched_init(fusbh200, sched, stream, urb);
4287 if (urb->interval < 8)
4288 num_itds = 1 + (sched->span + 7) / 8;
4289 else
4290 num_itds = urb->number_of_packets;
4292 /* allocate/init ITDs */
4293 spin_lock_irqsave (&fusbh200->lock, flags);
4294 for (i = 0; i < num_itds; i++) {
4297 * Use iTDs from the free list, but not iTDs that may
4298 * still be in use by the hardware.
4300 if (likely(!list_empty(&stream->free_list))) {
4301 itd = list_first_entry(&stream->free_list,
4302 struct fusbh200_itd, itd_list);
4303 if (itd->frame == fusbh200->now_frame)
4304 goto alloc_itd;
4305 list_del (&itd->itd_list);
4306 itd_dma = itd->itd_dma;
4307 } else {
4308 alloc_itd:
4309 spin_unlock_irqrestore (&fusbh200->lock, flags);
4310 itd = dma_pool_alloc (fusbh200->itd_pool, mem_flags,
4311 &itd_dma);
4312 spin_lock_irqsave (&fusbh200->lock, flags);
4313 if (!itd) {
4314 iso_sched_free(stream, sched);
4315 spin_unlock_irqrestore(&fusbh200->lock, flags);
4316 return -ENOMEM;
4320 memset (itd, 0, sizeof *itd);
4321 itd->itd_dma = itd_dma;
4322 list_add (&itd->itd_list, &sched->td_list);
4324 spin_unlock_irqrestore (&fusbh200->lock, flags);
4326 /* temporarily store schedule info in hcpriv */
4327 urb->hcpriv = sched;
4328 urb->error_count = 0;
4329 return 0;
4332 /*-------------------------------------------------------------------------*/
4334 static inline int
4335 itd_slot_ok (
4336 struct fusbh200_hcd *fusbh200,
4337 u32 mod,
4338 u32 uframe,
4339 u8 usecs,
4340 u32 period
4343 uframe %= period;
4344 do {
4345 /* can't commit more than uframe_periodic_max usec */
4346 if (periodic_usecs (fusbh200, uframe >> 3, uframe & 0x7)
4347 > (fusbh200->uframe_periodic_max - usecs))
4348 return 0;
4350 /* we know urb->interval is 2^N uframes */
4351 uframe += period;
4352 } while (uframe < mod);
4353 return 1;
4357 * This scheduler plans almost as far into the future as it has actual
4358 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
4359 * "as small as possible" to be cache-friendlier.) That limits the size
4360 * transfers you can stream reliably; avoid more than 64 msec per urb.
4361 * Also avoid queue depths of less than fusbh200's worst irq latency (affected
4362 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
4363 * and other factors); or more than about 230 msec total (for portability,
4364 * given FUSBH200_TUNE_FLS and the slop). Or, write a smarter scheduler!
4367 #define SCHEDULE_SLOP 80 /* microframes */
4369 static int
4370 iso_stream_schedule (
4371 struct fusbh200_hcd *fusbh200,
4372 struct urb *urb,
4373 struct fusbh200_iso_stream *stream
4376 u32 now, next, start, period, span;
4377 int status;
4378 unsigned mod = fusbh200->periodic_size << 3;
4379 struct fusbh200_iso_sched *sched = urb->hcpriv;
4381 period = urb->interval;
4382 span = sched->span;
4384 if (span > mod - SCHEDULE_SLOP) {
4385 fusbh200_dbg (fusbh200, "iso request %p too long\n", urb);
4386 status = -EFBIG;
4387 goto fail;
4390 now = fusbh200_read_frame_index(fusbh200) & (mod - 1);
4392 /* Typical case: reuse current schedule, stream is still active.
4393 * Hopefully there are no gaps from the host falling behind
4394 * (irq delays etc), but if there are we'll take the next
4395 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
4397 if (likely (!list_empty (&stream->td_list))) {
4398 u32 excess;
4400 /* For high speed devices, allow scheduling within the
4401 * isochronous scheduling threshold. For full speed devices
4402 * and Intel PCI-based controllers, don't (work around for
4403 * Intel ICH9 bug).
4405 if (!stream->highspeed && fusbh200->fs_i_thresh)
4406 next = now + fusbh200->i_thresh;
4407 else
4408 next = now;
4410 /* Fell behind (by up to twice the slop amount)?
4411 * We decide based on the time of the last currently-scheduled
4412 * slot, not the time of the next available slot.
4414 excess = (stream->next_uframe - period - next) & (mod - 1);
4415 if (excess >= mod - 2 * SCHEDULE_SLOP)
4416 start = next + excess - mod + period *
4417 DIV_ROUND_UP(mod - excess, period);
4418 else
4419 start = next + excess + period;
4420 if (start - now >= mod) {
4421 fusbh200_dbg(fusbh200, "request %p would overflow (%d+%d >= %d)\n",
4422 urb, start - now - period, period,
4423 mod);
4424 status = -EFBIG;
4425 goto fail;
4429 /* need to schedule; when's the next (u)frame we could start?
4430 * this is bigger than fusbh200->i_thresh allows; scheduling itself
4431 * isn't free, the slop should handle reasonably slow cpus. it
4432 * can also help high bandwidth if the dma and irq loads don't
4433 * jump until after the queue is primed.
4435 else {
4436 int done = 0;
4437 start = SCHEDULE_SLOP + (now & ~0x07);
4439 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
4441 /* find a uframe slot with enough bandwidth.
4442 * Early uframes are more precious because full-speed
4443 * iso IN transfers can't use late uframes,
4444 * and therefore they should be allocated last.
4446 next = start;
4447 start += period;
4448 do {
4449 start--;
4450 /* check schedule: enough space? */
4451 if (itd_slot_ok(fusbh200, mod, start,
4452 stream->usecs, period))
4453 done = 1;
4454 } while (start > next && !done);
4456 /* no room in the schedule */
4457 if (!done) {
4458 fusbh200_dbg(fusbh200, "iso resched full %p (now %d max %d)\n",
4459 urb, now, now + mod);
4460 status = -ENOSPC;
4461 goto fail;
4465 /* Tried to schedule too far into the future? */
4466 if (unlikely(start - now + span - period
4467 >= mod - 2 * SCHEDULE_SLOP)) {
4468 fusbh200_dbg(fusbh200, "request %p would overflow (%d+%d >= %d)\n",
4469 urb, start - now, span - period,
4470 mod - 2 * SCHEDULE_SLOP);
4471 status = -EFBIG;
4472 goto fail;
4475 stream->next_uframe = start & (mod - 1);
4477 /* report high speed start in uframes; full speed, in frames */
4478 urb->start_frame = stream->next_uframe;
4479 if (!stream->highspeed)
4480 urb->start_frame >>= 3;
4482 /* Make sure scan_isoc() sees these */
4483 if (fusbh200->isoc_count == 0)
4484 fusbh200->next_frame = now >> 3;
4485 return 0;
4487 fail:
4488 iso_sched_free(stream, sched);
4489 urb->hcpriv = NULL;
4490 return status;
4493 /*-------------------------------------------------------------------------*/
4495 static inline void
4496 itd_init(struct fusbh200_hcd *fusbh200, struct fusbh200_iso_stream *stream,
4497 struct fusbh200_itd *itd)
4499 int i;
4501 /* it's been recently zeroed */
4502 itd->hw_next = FUSBH200_LIST_END(fusbh200);
4503 itd->hw_bufp [0] = stream->buf0;
4504 itd->hw_bufp [1] = stream->buf1;
4505 itd->hw_bufp [2] = stream->buf2;
4507 for (i = 0; i < 8; i++)
4508 itd->index[i] = -1;
4510 /* All other fields are filled when scheduling */
4513 static inline void
4514 itd_patch(
4515 struct fusbh200_hcd *fusbh200,
4516 struct fusbh200_itd *itd,
4517 struct fusbh200_iso_sched *iso_sched,
4518 unsigned index,
4519 u16 uframe
4522 struct fusbh200_iso_packet *uf = &iso_sched->packet [index];
4523 unsigned pg = itd->pg;
4525 // BUG_ON (pg == 6 && uf->cross);
4527 uframe &= 0x07;
4528 itd->index [uframe] = index;
4530 itd->hw_transaction[uframe] = uf->transaction;
4531 itd->hw_transaction[uframe] |= cpu_to_hc32(fusbh200, pg << 12);
4532 itd->hw_bufp[pg] |= cpu_to_hc32(fusbh200, uf->bufp & ~(u32)0);
4533 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fusbh200, (u32)(uf->bufp >> 32));
4535 /* iso_frame_desc[].offset must be strictly increasing */
4536 if (unlikely (uf->cross)) {
4537 u64 bufp = uf->bufp + 4096;
4539 itd->pg = ++pg;
4540 itd->hw_bufp[pg] |= cpu_to_hc32(fusbh200, bufp & ~(u32)0);
4541 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fusbh200, (u32)(bufp >> 32));
4545 static inline void
4546 itd_link (struct fusbh200_hcd *fusbh200, unsigned frame, struct fusbh200_itd *itd)
4548 union fusbh200_shadow *prev = &fusbh200->pshadow[frame];
4549 __hc32 *hw_p = &fusbh200->periodic[frame];
4550 union fusbh200_shadow here = *prev;
4551 __hc32 type = 0;
4553 /* skip any iso nodes which might belong to previous microframes */
4554 while (here.ptr) {
4555 type = Q_NEXT_TYPE(fusbh200, *hw_p);
4556 if (type == cpu_to_hc32(fusbh200, Q_TYPE_QH))
4557 break;
4558 prev = periodic_next_shadow(fusbh200, prev, type);
4559 hw_p = shadow_next_periodic(fusbh200, &here, type);
4560 here = *prev;
4563 itd->itd_next = here;
4564 itd->hw_next = *hw_p;
4565 prev->itd = itd;
4566 itd->frame = frame;
4567 wmb ();
4568 *hw_p = cpu_to_hc32(fusbh200, itd->itd_dma | Q_TYPE_ITD);
4571 /* fit urb's itds into the selected schedule slot; activate as needed */
4572 static void itd_link_urb(
4573 struct fusbh200_hcd *fusbh200,
4574 struct urb *urb,
4575 unsigned mod,
4576 struct fusbh200_iso_stream *stream
4579 int packet;
4580 unsigned next_uframe, uframe, frame;
4581 struct fusbh200_iso_sched *iso_sched = urb->hcpriv;
4582 struct fusbh200_itd *itd;
4584 next_uframe = stream->next_uframe & (mod - 1);
4586 if (unlikely (list_empty(&stream->td_list))) {
4587 fusbh200_to_hcd(fusbh200)->self.bandwidth_allocated
4588 += stream->bandwidth;
4589 fusbh200_vdbg (fusbh200,
4590 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
4591 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
4592 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
4593 urb->interval,
4594 next_uframe >> 3, next_uframe & 0x7);
4597 /* fill iTDs uframe by uframe */
4598 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
4599 if (itd == NULL) {
4600 /* ASSERT: we have all necessary itds */
4601 // BUG_ON (list_empty (&iso_sched->td_list));
4603 /* ASSERT: no itds for this endpoint in this uframe */
4605 itd = list_entry (iso_sched->td_list.next,
4606 struct fusbh200_itd, itd_list);
4607 list_move_tail (&itd->itd_list, &stream->td_list);
4608 itd->stream = stream;
4609 itd->urb = urb;
4610 itd_init (fusbh200, stream, itd);
4613 uframe = next_uframe & 0x07;
4614 frame = next_uframe >> 3;
4616 itd_patch(fusbh200, itd, iso_sched, packet, uframe);
4618 next_uframe += stream->interval;
4619 next_uframe &= mod - 1;
4620 packet++;
4622 /* link completed itds into the schedule */
4623 if (((next_uframe >> 3) != frame)
4624 || packet == urb->number_of_packets) {
4625 itd_link(fusbh200, frame & (fusbh200->periodic_size - 1), itd);
4626 itd = NULL;
4629 stream->next_uframe = next_uframe;
4631 /* don't need that schedule data any more */
4632 iso_sched_free (stream, iso_sched);
4633 urb->hcpriv = NULL;
4635 ++fusbh200->isoc_count;
4636 enable_periodic(fusbh200);
4639 #define ISO_ERRS (FUSBH200_ISOC_BUF_ERR | FUSBH200_ISOC_BABBLE | FUSBH200_ISOC_XACTERR)
4641 /* Process and recycle a completed ITD. Return true iff its urb completed,
4642 * and hence its completion callback probably added things to the hardware
4643 * schedule.
4645 * Note that we carefully avoid recycling this descriptor until after any
4646 * completion callback runs, so that it won't be reused quickly. That is,
4647 * assuming (a) no more than two urbs per frame on this endpoint, and also
4648 * (b) only this endpoint's completions submit URBs. It seems some silicon
4649 * corrupts things if you reuse completed descriptors very quickly...
4651 static bool itd_complete(struct fusbh200_hcd *fusbh200, struct fusbh200_itd *itd)
4653 struct urb *urb = itd->urb;
4654 struct usb_iso_packet_descriptor *desc;
4655 u32 t;
4656 unsigned uframe;
4657 int urb_index = -1;
4658 struct fusbh200_iso_stream *stream = itd->stream;
4659 struct usb_device *dev;
4660 bool retval = false;
4662 /* for each uframe with a packet */
4663 for (uframe = 0; uframe < 8; uframe++) {
4664 if (likely (itd->index[uframe] == -1))
4665 continue;
4666 urb_index = itd->index[uframe];
4667 desc = &urb->iso_frame_desc [urb_index];
4669 t = hc32_to_cpup(fusbh200, &itd->hw_transaction [uframe]);
4670 itd->hw_transaction [uframe] = 0;
4672 /* report transfer status */
4673 if (unlikely (t & ISO_ERRS)) {
4674 urb->error_count++;
4675 if (t & FUSBH200_ISOC_BUF_ERR)
4676 desc->status = usb_pipein (urb->pipe)
4677 ? -ENOSR /* hc couldn't read */
4678 : -ECOMM; /* hc couldn't write */
4679 else if (t & FUSBH200_ISOC_BABBLE)
4680 desc->status = -EOVERFLOW;
4681 else /* (t & FUSBH200_ISOC_XACTERR) */
4682 desc->status = -EPROTO;
4684 /* HC need not update length with this error */
4685 if (!(t & FUSBH200_ISOC_BABBLE)) {
4686 desc->actual_length = fusbh200_itdlen(urb, desc, t);
4687 urb->actual_length += desc->actual_length;
4689 } else if (likely ((t & FUSBH200_ISOC_ACTIVE) == 0)) {
4690 desc->status = 0;
4691 desc->actual_length = fusbh200_itdlen(urb, desc, t);
4692 urb->actual_length += desc->actual_length;
4693 } else {
4694 /* URB was too late */
4695 desc->status = -EXDEV;
4699 /* handle completion now? */
4700 if (likely ((urb_index + 1) != urb->number_of_packets))
4701 goto done;
4703 /* ASSERT: it's really the last itd for this urb
4704 list_for_each_entry (itd, &stream->td_list, itd_list)
4705 BUG_ON (itd->urb == urb);
4708 /* give urb back to the driver; completion often (re)submits */
4709 dev = urb->dev;
4710 fusbh200_urb_done(fusbh200, urb, 0);
4711 retval = true;
4712 urb = NULL;
4714 --fusbh200->isoc_count;
4715 disable_periodic(fusbh200);
4717 if (unlikely(list_is_singular(&stream->td_list))) {
4718 fusbh200_to_hcd(fusbh200)->self.bandwidth_allocated
4719 -= stream->bandwidth;
4720 fusbh200_vdbg (fusbh200,
4721 "deschedule devp %s ep%d%s-iso\n",
4722 dev->devpath, stream->bEndpointAddress & 0x0f,
4723 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
4726 done:
4727 itd->urb = NULL;
4729 /* Add to the end of the free list for later reuse */
4730 list_move_tail(&itd->itd_list, &stream->free_list);
4732 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
4733 if (list_empty(&stream->td_list)) {
4734 list_splice_tail_init(&stream->free_list,
4735 &fusbh200->cached_itd_list);
4736 start_free_itds(fusbh200);
4739 return retval;
4742 /*-------------------------------------------------------------------------*/
4744 static int itd_submit (struct fusbh200_hcd *fusbh200, struct urb *urb,
4745 gfp_t mem_flags)
4747 int status = -EINVAL;
4748 unsigned long flags;
4749 struct fusbh200_iso_stream *stream;
4751 /* Get iso_stream head */
4752 stream = iso_stream_find (fusbh200, urb);
4753 if (unlikely (stream == NULL)) {
4754 fusbh200_dbg (fusbh200, "can't get iso stream\n");
4755 return -ENOMEM;
4757 if (unlikely (urb->interval != stream->interval &&
4758 fusbh200_port_speed(fusbh200, 0) == USB_PORT_STAT_HIGH_SPEED)) {
4759 fusbh200_dbg (fusbh200, "can't change iso interval %d --> %d\n",
4760 stream->interval, urb->interval);
4761 goto done;
4764 #ifdef FUSBH200_URB_TRACE
4765 fusbh200_dbg (fusbh200,
4766 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
4767 __func__, urb->dev->devpath, urb,
4768 usb_pipeendpoint (urb->pipe),
4769 usb_pipein (urb->pipe) ? "in" : "out",
4770 urb->transfer_buffer_length,
4771 urb->number_of_packets, urb->interval,
4772 stream);
4773 #endif
4775 /* allocate ITDs w/o locking anything */
4776 status = itd_urb_transaction (stream, fusbh200, urb, mem_flags);
4777 if (unlikely (status < 0)) {
4778 fusbh200_dbg (fusbh200, "can't init itds\n");
4779 goto done;
4782 /* schedule ... need to lock */
4783 spin_lock_irqsave (&fusbh200->lock, flags);
4784 if (unlikely(!HCD_HW_ACCESSIBLE(fusbh200_to_hcd(fusbh200)))) {
4785 status = -ESHUTDOWN;
4786 goto done_not_linked;
4788 status = usb_hcd_link_urb_to_ep(fusbh200_to_hcd(fusbh200), urb);
4789 if (unlikely(status))
4790 goto done_not_linked;
4791 status = iso_stream_schedule(fusbh200, urb, stream);
4792 if (likely (status == 0))
4793 itd_link_urb (fusbh200, urb, fusbh200->periodic_size << 3, stream);
4794 else
4795 usb_hcd_unlink_urb_from_ep(fusbh200_to_hcd(fusbh200), urb);
4796 done_not_linked:
4797 spin_unlock_irqrestore (&fusbh200->lock, flags);
4798 done:
4799 return status;
4802 /*-------------------------------------------------------------------------*/
4804 static void scan_isoc(struct fusbh200_hcd *fusbh200)
4806 unsigned uf, now_frame, frame;
4807 unsigned fmask = fusbh200->periodic_size - 1;
4808 bool modified, live;
4811 * When running, scan from last scan point up to "now"
4812 * else clean up by scanning everything that's left.
4813 * Touches as few pages as possible: cache-friendly.
4815 if (fusbh200->rh_state >= FUSBH200_RH_RUNNING) {
4816 uf = fusbh200_read_frame_index(fusbh200);
4817 now_frame = (uf >> 3) & fmask;
4818 live = true;
4819 } else {
4820 now_frame = (fusbh200->next_frame - 1) & fmask;
4821 live = false;
4823 fusbh200->now_frame = now_frame;
4825 frame = fusbh200->next_frame;
4826 for (;;) {
4827 union fusbh200_shadow q, *q_p;
4828 __hc32 type, *hw_p;
4830 restart:
4831 /* scan each element in frame's queue for completions */
4832 q_p = &fusbh200->pshadow [frame];
4833 hw_p = &fusbh200->periodic [frame];
4834 q.ptr = q_p->ptr;
4835 type = Q_NEXT_TYPE(fusbh200, *hw_p);
4836 modified = false;
4838 while (q.ptr != NULL) {
4839 switch (hc32_to_cpu(fusbh200, type)) {
4840 case Q_TYPE_ITD:
4841 /* If this ITD is still active, leave it for
4842 * later processing ... check the next entry.
4843 * No need to check for activity unless the
4844 * frame is current.
4846 if (frame == now_frame && live) {
4847 rmb();
4848 for (uf = 0; uf < 8; uf++) {
4849 if (q.itd->hw_transaction[uf] &
4850 ITD_ACTIVE(fusbh200))
4851 break;
4853 if (uf < 8) {
4854 q_p = &q.itd->itd_next;
4855 hw_p = &q.itd->hw_next;
4856 type = Q_NEXT_TYPE(fusbh200,
4857 q.itd->hw_next);
4858 q = *q_p;
4859 break;
4863 /* Take finished ITDs out of the schedule
4864 * and process them: recycle, maybe report
4865 * URB completion. HC won't cache the
4866 * pointer for much longer, if at all.
4868 *q_p = q.itd->itd_next;
4869 *hw_p = q.itd->hw_next;
4870 type = Q_NEXT_TYPE(fusbh200, q.itd->hw_next);
4871 wmb();
4872 modified = itd_complete (fusbh200, q.itd);
4873 q = *q_p;
4874 break;
4875 default:
4876 fusbh200_dbg(fusbh200, "corrupt type %d frame %d shadow %p\n",
4877 type, frame, q.ptr);
4878 // BUG ();
4879 /* FALL THROUGH */
4880 case Q_TYPE_QH:
4881 case Q_TYPE_FSTN:
4882 /* End of the iTDs and siTDs */
4883 q.ptr = NULL;
4884 break;
4887 /* assume completion callbacks modify the queue */
4888 if (unlikely(modified && fusbh200->isoc_count > 0))
4889 goto restart;
4892 /* Stop when we have reached the current frame */
4893 if (frame == now_frame)
4894 break;
4895 frame = (frame + 1) & fmask;
4897 fusbh200->next_frame = now_frame;
4899 /*-------------------------------------------------------------------------*/
4901 * Display / Set uframe_periodic_max
4903 static ssize_t show_uframe_periodic_max(struct device *dev,
4904 struct device_attribute *attr,
4905 char *buf)
4907 struct fusbh200_hcd *fusbh200;
4908 int n;
4910 fusbh200 = hcd_to_fusbh200(bus_to_hcd(dev_get_drvdata(dev)));
4911 n = scnprintf(buf, PAGE_SIZE, "%d\n", fusbh200->uframe_periodic_max);
4912 return n;
4916 static ssize_t store_uframe_periodic_max(struct device *dev,
4917 struct device_attribute *attr,
4918 const char *buf, size_t count)
4920 struct fusbh200_hcd *fusbh200;
4921 unsigned uframe_periodic_max;
4922 unsigned frame, uframe;
4923 unsigned short allocated_max;
4924 unsigned long flags;
4925 ssize_t ret;
4927 fusbh200 = hcd_to_fusbh200(bus_to_hcd(dev_get_drvdata(dev)));
4928 if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
4929 return -EINVAL;
4931 if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
4932 fusbh200_info(fusbh200, "rejecting invalid request for "
4933 "uframe_periodic_max=%u\n", uframe_periodic_max);
4934 return -EINVAL;
4937 ret = -EINVAL;
4940 * lock, so that our checking does not race with possible periodic
4941 * bandwidth allocation through submitting new urbs.
4943 spin_lock_irqsave (&fusbh200->lock, flags);
4946 * for request to decrease max periodic bandwidth, we have to check
4947 * every microframe in the schedule to see whether the decrease is
4948 * possible.
4950 if (uframe_periodic_max < fusbh200->uframe_periodic_max) {
4951 allocated_max = 0;
4953 for (frame = 0; frame < fusbh200->periodic_size; ++frame)
4954 for (uframe = 0; uframe < 7; ++uframe)
4955 allocated_max = max(allocated_max,
4956 periodic_usecs (fusbh200, frame, uframe));
4958 if (allocated_max > uframe_periodic_max) {
4959 fusbh200_info(fusbh200,
4960 "cannot decrease uframe_periodic_max becase "
4961 "periodic bandwidth is already allocated "
4962 "(%u > %u)\n",
4963 allocated_max, uframe_periodic_max);
4964 goto out_unlock;
4968 /* increasing is always ok */
4970 fusbh200_info(fusbh200, "setting max periodic bandwidth to %u%% "
4971 "(== %u usec/uframe)\n",
4972 100*uframe_periodic_max/125, uframe_periodic_max);
4974 if (uframe_periodic_max != 100)
4975 fusbh200_warn(fusbh200, "max periodic bandwidth set is non-standard\n");
4977 fusbh200->uframe_periodic_max = uframe_periodic_max;
4978 ret = count;
4980 out_unlock:
4981 spin_unlock_irqrestore (&fusbh200->lock, flags);
4982 return ret;
4984 static DEVICE_ATTR(uframe_periodic_max, 0644, show_uframe_periodic_max, store_uframe_periodic_max);
4987 static inline int create_sysfs_files(struct fusbh200_hcd *fusbh200)
4989 struct device *controller = fusbh200_to_hcd(fusbh200)->self.controller;
4990 int i = 0;
4992 if (i)
4993 goto out;
4995 i = device_create_file(controller, &dev_attr_uframe_periodic_max);
4996 out:
4997 return i;
5000 static inline void remove_sysfs_files(struct fusbh200_hcd *fusbh200)
5002 struct device *controller = fusbh200_to_hcd(fusbh200)->self.controller;
5004 device_remove_file(controller, &dev_attr_uframe_periodic_max);
5006 /*-------------------------------------------------------------------------*/
5008 /* On some systems, leaving remote wakeup enabled prevents system shutdown.
5009 * The firmware seems to think that powering off is a wakeup event!
5010 * This routine turns off remote wakeup and everything else, on all ports.
5012 static void fusbh200_turn_off_all_ports(struct fusbh200_hcd *fusbh200)
5014 u32 __iomem *status_reg = &fusbh200->regs->port_status;
5016 fusbh200_writel(fusbh200, PORT_RWC_BITS, status_reg);
5020 * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
5021 * Must be called with interrupts enabled and the lock not held.
5023 static void fusbh200_silence_controller(struct fusbh200_hcd *fusbh200)
5025 fusbh200_halt(fusbh200);
5027 spin_lock_irq(&fusbh200->lock);
5028 fusbh200->rh_state = FUSBH200_RH_HALTED;
5029 fusbh200_turn_off_all_ports(fusbh200);
5030 spin_unlock_irq(&fusbh200->lock);
5033 /* fusbh200_shutdown kick in for silicon on any bus (not just pci, etc).
5034 * This forcibly disables dma and IRQs, helping kexec and other cases
5035 * where the next system software may expect clean state.
5037 static void fusbh200_shutdown(struct usb_hcd *hcd)
5039 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
5041 spin_lock_irq(&fusbh200->lock);
5042 fusbh200->shutdown = true;
5043 fusbh200->rh_state = FUSBH200_RH_STOPPING;
5044 fusbh200->enabled_hrtimer_events = 0;
5045 spin_unlock_irq(&fusbh200->lock);
5047 fusbh200_silence_controller(fusbh200);
5049 hrtimer_cancel(&fusbh200->hrtimer);
5052 /*-------------------------------------------------------------------------*/
5055 * fusbh200_work is called from some interrupts, timers, and so on.
5056 * it calls driver completion functions, after dropping fusbh200->lock.
5058 static void fusbh200_work (struct fusbh200_hcd *fusbh200)
5060 /* another CPU may drop fusbh200->lock during a schedule scan while
5061 * it reports urb completions. this flag guards against bogus
5062 * attempts at re-entrant schedule scanning.
5064 if (fusbh200->scanning) {
5065 fusbh200->need_rescan = true;
5066 return;
5068 fusbh200->scanning = true;
5070 rescan:
5071 fusbh200->need_rescan = false;
5072 if (fusbh200->async_count)
5073 scan_async(fusbh200);
5074 if (fusbh200->intr_count > 0)
5075 scan_intr(fusbh200);
5076 if (fusbh200->isoc_count > 0)
5077 scan_isoc(fusbh200);
5078 if (fusbh200->need_rescan)
5079 goto rescan;
5080 fusbh200->scanning = false;
5082 /* the IO watchdog guards against hardware or driver bugs that
5083 * misplace IRQs, and should let us run completely without IRQs.
5084 * such lossage has been observed on both VT6202 and VT8235.
5086 turn_on_io_watchdog(fusbh200);
5090 * Called when the fusbh200_hcd module is removed.
5092 static void fusbh200_stop (struct usb_hcd *hcd)
5094 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5096 fusbh200_dbg (fusbh200, "stop\n");
5098 /* no more interrupts ... */
5100 spin_lock_irq(&fusbh200->lock);
5101 fusbh200->enabled_hrtimer_events = 0;
5102 spin_unlock_irq(&fusbh200->lock);
5104 fusbh200_quiesce(fusbh200);
5105 fusbh200_silence_controller(fusbh200);
5106 fusbh200_reset (fusbh200);
5108 hrtimer_cancel(&fusbh200->hrtimer);
5109 remove_sysfs_files(fusbh200);
5110 remove_debug_files (fusbh200);
5112 /* root hub is shut down separately (first, when possible) */
5113 spin_lock_irq (&fusbh200->lock);
5114 end_free_itds(fusbh200);
5115 spin_unlock_irq (&fusbh200->lock);
5116 fusbh200_mem_cleanup (fusbh200);
5118 #ifdef FUSBH200_STATS
5119 fusbh200_dbg(fusbh200, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
5120 fusbh200->stats.normal, fusbh200->stats.error, fusbh200->stats.iaa,
5121 fusbh200->stats.lost_iaa);
5122 fusbh200_dbg (fusbh200, "complete %ld unlink %ld\n",
5123 fusbh200->stats.complete, fusbh200->stats.unlink);
5124 #endif
5126 dbg_status (fusbh200, "fusbh200_stop completed",
5127 fusbh200_readl(fusbh200, &fusbh200->regs->status));
5130 /* one-time init, only for memory state */
5131 static int hcd_fusbh200_init(struct usb_hcd *hcd)
5133 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
5134 u32 temp;
5135 int retval;
5136 u32 hcc_params;
5137 struct fusbh200_qh_hw *hw;
5139 spin_lock_init(&fusbh200->lock);
5142 * keep io watchdog by default, those good HCDs could turn off it later
5144 fusbh200->need_io_watchdog = 1;
5146 hrtimer_init(&fusbh200->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
5147 fusbh200->hrtimer.function = fusbh200_hrtimer_func;
5148 fusbh200->next_hrtimer_event = FUSBH200_HRTIMER_NO_EVENT;
5150 hcc_params = fusbh200_readl(fusbh200, &fusbh200->caps->hcc_params);
5153 * by default set standard 80% (== 100 usec/uframe) max periodic
5154 * bandwidth as required by USB 2.0
5156 fusbh200->uframe_periodic_max = 100;
5159 * hw default: 1K periodic list heads, one per frame.
5160 * periodic_size can shrink by USBCMD update if hcc_params allows.
5162 fusbh200->periodic_size = DEFAULT_I_TDPS;
5163 INIT_LIST_HEAD(&fusbh200->intr_qh_list);
5164 INIT_LIST_HEAD(&fusbh200->cached_itd_list);
5166 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5167 /* periodic schedule size can be smaller than default */
5168 switch (FUSBH200_TUNE_FLS) {
5169 case 0: fusbh200->periodic_size = 1024; break;
5170 case 1: fusbh200->periodic_size = 512; break;
5171 case 2: fusbh200->periodic_size = 256; break;
5172 default: BUG();
5175 if ((retval = fusbh200_mem_init(fusbh200, GFP_KERNEL)) < 0)
5176 return retval;
5178 /* controllers may cache some of the periodic schedule ... */
5179 fusbh200->i_thresh = 2;
5182 * dedicate a qh for the async ring head, since we couldn't unlink
5183 * a 'real' qh without stopping the async schedule [4.8]. use it
5184 * as the 'reclamation list head' too.
5185 * its dummy is used in hw_alt_next of many tds, to prevent the qh
5186 * from automatically advancing to the next td after short reads.
5188 fusbh200->async->qh_next.qh = NULL;
5189 hw = fusbh200->async->hw;
5190 hw->hw_next = QH_NEXT(fusbh200, fusbh200->async->qh_dma);
5191 hw->hw_info1 = cpu_to_hc32(fusbh200, QH_HEAD);
5192 hw->hw_token = cpu_to_hc32(fusbh200, QTD_STS_HALT);
5193 hw->hw_qtd_next = FUSBH200_LIST_END(fusbh200);
5194 fusbh200->async->qh_state = QH_STATE_LINKED;
5195 hw->hw_alt_next = QTD_NEXT(fusbh200, fusbh200->async->dummy->qtd_dma);
5197 /* clear interrupt enables, set irq latency */
5198 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
5199 log2_irq_thresh = 0;
5200 temp = 1 << (16 + log2_irq_thresh);
5201 if (HCC_CANPARK(hcc_params)) {
5202 /* HW default park == 3, on hardware that supports it (like
5203 * NVidia and ALI silicon), maximizes throughput on the async
5204 * schedule by avoiding QH fetches between transfers.
5206 * With fast usb storage devices and NForce2, "park" seems to
5207 * make problems: throughput reduction (!), data errors...
5209 if (park) {
5210 park = min(park, (unsigned) 3);
5211 temp |= CMD_PARK;
5212 temp |= park << 8;
5214 fusbh200_dbg(fusbh200, "park %d\n", park);
5216 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5217 /* periodic schedule size can be smaller than default */
5218 temp &= ~(3 << 2);
5219 temp |= (FUSBH200_TUNE_FLS << 2);
5221 fusbh200->command = temp;
5223 /* Accept arbitrarily long scatter-gather lists */
5224 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
5225 hcd->self.sg_tablesize = ~0;
5226 return 0;
5229 /* start HC running; it's halted, hcd_fusbh200_init() has been run (once) */
5230 static int fusbh200_run (struct usb_hcd *hcd)
5232 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5233 u32 temp;
5234 u32 hcc_params;
5236 hcd->uses_new_polling = 1;
5238 /* EHCI spec section 4.1 */
5240 fusbh200_writel(fusbh200, fusbh200->periodic_dma, &fusbh200->regs->frame_list);
5241 fusbh200_writel(fusbh200, (u32)fusbh200->async->qh_dma, &fusbh200->regs->async_next);
5244 * hcc_params controls whether fusbh200->regs->segment must (!!!)
5245 * be used; it constrains QH/ITD/SITD and QTD locations.
5246 * pci_pool consistent memory always uses segment zero.
5247 * streaming mappings for I/O buffers, like pci_map_single(),
5248 * can return segments above 4GB, if the device allows.
5250 * NOTE: the dma mask is visible through dma_supported(), so
5251 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
5252 * Scsi_Host.highmem_io, and so forth. It's readonly to all
5253 * host side drivers though.
5255 hcc_params = fusbh200_readl(fusbh200, &fusbh200->caps->hcc_params);
5257 // Philips, Intel, and maybe others need CMD_RUN before the
5258 // root hub will detect new devices (why?); NEC doesn't
5259 fusbh200->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
5260 fusbh200->command |= CMD_RUN;
5261 fusbh200_writel(fusbh200, fusbh200->command, &fusbh200->regs->command);
5262 dbg_cmd (fusbh200, "init", fusbh200->command);
5265 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5266 * are explicitly handed to companion controller(s), so no TT is
5267 * involved with the root hub. (Except where one is integrated,
5268 * and there's no companion controller unless maybe for USB OTG.)
5270 * Turning on the CF flag will transfer ownership of all ports
5271 * from the companions to the EHCI controller. If any of the
5272 * companions are in the middle of a port reset at the time, it
5273 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
5274 * guarantees that no resets are in progress. After we set CF,
5275 * a short delay lets the hardware catch up; new resets shouldn't
5276 * be started before the port switching actions could complete.
5278 down_write(&ehci_cf_port_reset_rwsem);
5279 fusbh200->rh_state = FUSBH200_RH_RUNNING;
5280 fusbh200_readl(fusbh200, &fusbh200->regs->command); /* unblock posted writes */
5281 msleep(5);
5282 up_write(&ehci_cf_port_reset_rwsem);
5283 fusbh200->last_periodic_enable = ktime_get_real();
5285 temp = HC_VERSION(fusbh200, fusbh200_readl(fusbh200, &fusbh200->caps->hc_capbase));
5286 fusbh200_info (fusbh200,
5287 "USB %x.%x started, EHCI %x.%02x\n",
5288 ((fusbh200->sbrn & 0xf0)>>4), (fusbh200->sbrn & 0x0f),
5289 temp >> 8, temp & 0xff);
5291 fusbh200_writel(fusbh200, INTR_MASK,
5292 &fusbh200->regs->intr_enable); /* Turn On Interrupts */
5294 /* GRR this is run-once init(), being done every time the HC starts.
5295 * So long as they're part of class devices, we can't do it init()
5296 * since the class device isn't created that early.
5298 create_debug_files(fusbh200);
5299 create_sysfs_files(fusbh200);
5301 return 0;
5304 static int fusbh200_setup(struct usb_hcd *hcd)
5306 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
5307 int retval;
5309 fusbh200->regs = (void __iomem *)fusbh200->caps +
5310 HC_LENGTH(fusbh200, fusbh200_readl(fusbh200, &fusbh200->caps->hc_capbase));
5311 dbg_hcs_params(fusbh200, "reset");
5312 dbg_hcc_params(fusbh200, "reset");
5314 /* cache this readonly data; minimize chip reads */
5315 fusbh200->hcs_params = fusbh200_readl(fusbh200, &fusbh200->caps->hcs_params);
5317 fusbh200->sbrn = HCD_USB2;
5319 /* data structure init */
5320 retval = hcd_fusbh200_init(hcd);
5321 if (retval)
5322 return retval;
5324 retval = fusbh200_halt(fusbh200);
5325 if (retval)
5326 return retval;
5328 fusbh200_reset(fusbh200);
5330 return 0;
5333 /*-------------------------------------------------------------------------*/
5335 static irqreturn_t fusbh200_irq (struct usb_hcd *hcd)
5337 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5338 u32 status, masked_status, pcd_status = 0, cmd;
5339 int bh;
5341 spin_lock (&fusbh200->lock);
5343 status = fusbh200_readl(fusbh200, &fusbh200->regs->status);
5345 /* e.g. cardbus physical eject */
5346 if (status == ~(u32) 0) {
5347 fusbh200_dbg (fusbh200, "device removed\n");
5348 goto dead;
5352 * We don't use STS_FLR, but some controllers don't like it to
5353 * remain on, so mask it out along with the other status bits.
5355 masked_status = status & (INTR_MASK | STS_FLR);
5357 /* Shared IRQ? */
5358 if (!masked_status || unlikely(fusbh200->rh_state == FUSBH200_RH_HALTED)) {
5359 spin_unlock(&fusbh200->lock);
5360 return IRQ_NONE;
5363 /* clear (just) interrupts */
5364 fusbh200_writel(fusbh200, masked_status, &fusbh200->regs->status);
5365 cmd = fusbh200_readl(fusbh200, &fusbh200->regs->command);
5366 bh = 0;
5368 #ifdef VERBOSE_DEBUG
5369 /* unrequested/ignored: Frame List Rollover */
5370 dbg_status (fusbh200, "irq", status);
5371 #endif
5373 /* INT, ERR, and IAA interrupt rates can be throttled */
5375 /* normal [4.15.1.2] or error [4.15.1.1] completion */
5376 if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
5377 if (likely ((status & STS_ERR) == 0))
5378 COUNT (fusbh200->stats.normal);
5379 else
5380 COUNT (fusbh200->stats.error);
5381 bh = 1;
5384 /* complete the unlinking of some qh [4.15.2.3] */
5385 if (status & STS_IAA) {
5387 /* Turn off the IAA watchdog */
5388 fusbh200->enabled_hrtimer_events &= ~BIT(FUSBH200_HRTIMER_IAA_WATCHDOG);
5391 * Mild optimization: Allow another IAAD to reset the
5392 * hrtimer, if one occurs before the next expiration.
5393 * In theory we could always cancel the hrtimer, but
5394 * tests show that about half the time it will be reset
5395 * for some other event anyway.
5397 if (fusbh200->next_hrtimer_event == FUSBH200_HRTIMER_IAA_WATCHDOG)
5398 ++fusbh200->next_hrtimer_event;
5400 /* guard against (alleged) silicon errata */
5401 if (cmd & CMD_IAAD)
5402 fusbh200_dbg(fusbh200, "IAA with IAAD still set?\n");
5403 if (fusbh200->async_iaa) {
5404 COUNT(fusbh200->stats.iaa);
5405 end_unlink_async(fusbh200);
5406 } else
5407 fusbh200_dbg(fusbh200, "IAA with nothing unlinked?\n");
5410 /* remote wakeup [4.3.1] */
5411 if (status & STS_PCD) {
5412 int pstatus;
5413 u32 __iomem *status_reg = &fusbh200->regs->port_status;
5415 /* kick root hub later */
5416 pcd_status = status;
5418 /* resume root hub? */
5419 if (fusbh200->rh_state == FUSBH200_RH_SUSPENDED)
5420 usb_hcd_resume_root_hub(hcd);
5422 pstatus = fusbh200_readl(fusbh200, status_reg);
5424 if (test_bit(0, &fusbh200->suspended_ports) &&
5425 ((pstatus & PORT_RESUME) ||
5426 !(pstatus & PORT_SUSPEND)) &&
5427 (pstatus & PORT_PE) &&
5428 fusbh200->reset_done[0] == 0) {
5430 /* start 20 msec resume signaling from this port,
5431 * and make khubd collect PORT_STAT_C_SUSPEND to
5432 * stop that signaling. Use 5 ms extra for safety,
5433 * like usb_port_resume() does.
5435 fusbh200->reset_done[0] = jiffies + msecs_to_jiffies(25);
5436 set_bit(0, &fusbh200->resuming_ports);
5437 fusbh200_dbg (fusbh200, "port 1 remote wakeup\n");
5438 mod_timer(&hcd->rh_timer, fusbh200->reset_done[0]);
5442 /* PCI errors [4.15.2.4] */
5443 if (unlikely ((status & STS_FATAL) != 0)) {
5444 fusbh200_err(fusbh200, "fatal error\n");
5445 dbg_cmd(fusbh200, "fatal", cmd);
5446 dbg_status(fusbh200, "fatal", status);
5447 dead:
5448 usb_hc_died(hcd);
5450 /* Don't let the controller do anything more */
5451 fusbh200->shutdown = true;
5452 fusbh200->rh_state = FUSBH200_RH_STOPPING;
5453 fusbh200->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
5454 fusbh200_writel(fusbh200, fusbh200->command, &fusbh200->regs->command);
5455 fusbh200_writel(fusbh200, 0, &fusbh200->regs->intr_enable);
5456 fusbh200_handle_controller_death(fusbh200);
5458 /* Handle completions when the controller stops */
5459 bh = 0;
5462 if (bh)
5463 fusbh200_work (fusbh200);
5464 spin_unlock (&fusbh200->lock);
5465 if (pcd_status)
5466 usb_hcd_poll_rh_status(hcd);
5467 return IRQ_HANDLED;
5470 /*-------------------------------------------------------------------------*/
5473 * non-error returns are a promise to giveback() the urb later
5474 * we drop ownership so next owner (or urb unlink) can get it
5476 * urb + dev is in hcd.self.controller.urb_list
5477 * we're queueing TDs onto software and hardware lists
5479 * hcd-specific init for hcpriv hasn't been done yet
5481 * NOTE: control, bulk, and interrupt share the same code to append TDs
5482 * to a (possibly active) QH, and the same QH scanning code.
5484 static int fusbh200_urb_enqueue (
5485 struct usb_hcd *hcd,
5486 struct urb *urb,
5487 gfp_t mem_flags
5489 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5490 struct list_head qtd_list;
5492 INIT_LIST_HEAD (&qtd_list);
5494 switch (usb_pipetype (urb->pipe)) {
5495 case PIPE_CONTROL:
5496 /* qh_completions() code doesn't handle all the fault cases
5497 * in multi-TD control transfers. Even 1KB is rare anyway.
5499 if (urb->transfer_buffer_length > (16 * 1024))
5500 return -EMSGSIZE;
5501 /* FALLTHROUGH */
5502 /* case PIPE_BULK: */
5503 default:
5504 if (!qh_urb_transaction (fusbh200, urb, &qtd_list, mem_flags))
5505 return -ENOMEM;
5506 return submit_async(fusbh200, urb, &qtd_list, mem_flags);
5508 case PIPE_INTERRUPT:
5509 if (!qh_urb_transaction (fusbh200, urb, &qtd_list, mem_flags))
5510 return -ENOMEM;
5511 return intr_submit(fusbh200, urb, &qtd_list, mem_flags);
5513 case PIPE_ISOCHRONOUS:
5514 return itd_submit (fusbh200, urb, mem_flags);
5518 /* remove from hardware lists
5519 * completions normally happen asynchronously
5522 static int fusbh200_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
5524 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5525 struct fusbh200_qh *qh;
5526 unsigned long flags;
5527 int rc;
5529 spin_lock_irqsave (&fusbh200->lock, flags);
5530 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
5531 if (rc)
5532 goto done;
5534 switch (usb_pipetype (urb->pipe)) {
5535 // case PIPE_CONTROL:
5536 // case PIPE_BULK:
5537 default:
5538 qh = (struct fusbh200_qh *) urb->hcpriv;
5539 if (!qh)
5540 break;
5541 switch (qh->qh_state) {
5542 case QH_STATE_LINKED:
5543 case QH_STATE_COMPLETING:
5544 start_unlink_async(fusbh200, qh);
5545 break;
5546 case QH_STATE_UNLINK:
5547 case QH_STATE_UNLINK_WAIT:
5548 /* already started */
5549 break;
5550 case QH_STATE_IDLE:
5551 /* QH might be waiting for a Clear-TT-Buffer */
5552 qh_completions(fusbh200, qh);
5553 break;
5555 break;
5557 case PIPE_INTERRUPT:
5558 qh = (struct fusbh200_qh *) urb->hcpriv;
5559 if (!qh)
5560 break;
5561 switch (qh->qh_state) {
5562 case QH_STATE_LINKED:
5563 case QH_STATE_COMPLETING:
5564 start_unlink_intr(fusbh200, qh);
5565 break;
5566 case QH_STATE_IDLE:
5567 qh_completions (fusbh200, qh);
5568 break;
5569 default:
5570 fusbh200_dbg (fusbh200, "bogus qh %p state %d\n",
5571 qh, qh->qh_state);
5572 goto done;
5574 break;
5576 case PIPE_ISOCHRONOUS:
5577 // itd...
5579 // wait till next completion, do it then.
5580 // completion irqs can wait up to 1024 msec,
5581 break;
5583 done:
5584 spin_unlock_irqrestore (&fusbh200->lock, flags);
5585 return rc;
5588 /*-------------------------------------------------------------------------*/
5590 // bulk qh holds the data toggle
5592 static void
5593 fusbh200_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
5595 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5596 unsigned long flags;
5597 struct fusbh200_qh *qh, *tmp;
5599 /* ASSERT: any requests/urbs are being unlinked */
5600 /* ASSERT: nobody can be submitting urbs for this any more */
5602 rescan:
5603 spin_lock_irqsave (&fusbh200->lock, flags);
5604 qh = ep->hcpriv;
5605 if (!qh)
5606 goto done;
5608 /* endpoints can be iso streams. for now, we don't
5609 * accelerate iso completions ... so spin a while.
5611 if (qh->hw == NULL) {
5612 struct fusbh200_iso_stream *stream = ep->hcpriv;
5614 if (!list_empty(&stream->td_list))
5615 goto idle_timeout;
5617 /* BUG_ON(!list_empty(&stream->free_list)); */
5618 kfree(stream);
5619 goto done;
5622 if (fusbh200->rh_state < FUSBH200_RH_RUNNING)
5623 qh->qh_state = QH_STATE_IDLE;
5624 switch (qh->qh_state) {
5625 case QH_STATE_LINKED:
5626 case QH_STATE_COMPLETING:
5627 for (tmp = fusbh200->async->qh_next.qh;
5628 tmp && tmp != qh;
5629 tmp = tmp->qh_next.qh)
5630 continue;
5631 /* periodic qh self-unlinks on empty, and a COMPLETING qh
5632 * may already be unlinked.
5634 if (tmp)
5635 start_unlink_async(fusbh200, qh);
5636 /* FALL THROUGH */
5637 case QH_STATE_UNLINK: /* wait for hw to finish? */
5638 case QH_STATE_UNLINK_WAIT:
5639 idle_timeout:
5640 spin_unlock_irqrestore (&fusbh200->lock, flags);
5641 schedule_timeout_uninterruptible(1);
5642 goto rescan;
5643 case QH_STATE_IDLE: /* fully unlinked */
5644 if (qh->clearing_tt)
5645 goto idle_timeout;
5646 if (list_empty (&qh->qtd_list)) {
5647 qh_destroy(fusbh200, qh);
5648 break;
5650 /* else FALL THROUGH */
5651 default:
5652 /* caller was supposed to have unlinked any requests;
5653 * that's not our job. just leak this memory.
5655 fusbh200_err (fusbh200, "qh %p (#%02x) state %d%s\n",
5656 qh, ep->desc.bEndpointAddress, qh->qh_state,
5657 list_empty (&qh->qtd_list) ? "" : "(has tds)");
5658 break;
5660 done:
5661 ep->hcpriv = NULL;
5662 spin_unlock_irqrestore (&fusbh200->lock, flags);
5665 static void
5666 fusbh200_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
5668 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
5669 struct fusbh200_qh *qh;
5670 int eptype = usb_endpoint_type(&ep->desc);
5671 int epnum = usb_endpoint_num(&ep->desc);
5672 int is_out = usb_endpoint_dir_out(&ep->desc);
5673 unsigned long flags;
5675 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
5676 return;
5678 spin_lock_irqsave(&fusbh200->lock, flags);
5679 qh = ep->hcpriv;
5681 /* For Bulk and Interrupt endpoints we maintain the toggle state
5682 * in the hardware; the toggle bits in udev aren't used at all.
5683 * When an endpoint is reset by usb_clear_halt() we must reset
5684 * the toggle bit in the QH.
5686 if (qh) {
5687 usb_settoggle(qh->dev, epnum, is_out, 0);
5688 if (!list_empty(&qh->qtd_list)) {
5689 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
5690 } else if (qh->qh_state == QH_STATE_LINKED ||
5691 qh->qh_state == QH_STATE_COMPLETING) {
5693 /* The toggle value in the QH can't be updated
5694 * while the QH is active. Unlink it now;
5695 * re-linking will call qh_refresh().
5697 if (eptype == USB_ENDPOINT_XFER_BULK)
5698 start_unlink_async(fusbh200, qh);
5699 else
5700 start_unlink_intr(fusbh200, qh);
5703 spin_unlock_irqrestore(&fusbh200->lock, flags);
5706 static int fusbh200_get_frame (struct usb_hcd *hcd)
5708 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5709 return (fusbh200_read_frame_index(fusbh200) >> 3) % fusbh200->periodic_size;
5712 /*-------------------------------------------------------------------------*/
5715 * The EHCI in ChipIdea HDRC cannot be a separate module or device,
5716 * because its registers (and irq) are shared between host/gadget/otg
5717 * functions and in order to facilitate role switching we cannot
5718 * give the fusbh200 driver exclusive access to those.
5720 MODULE_DESCRIPTION(DRIVER_DESC);
5721 MODULE_AUTHOR (DRIVER_AUTHOR);
5722 MODULE_LICENSE ("GPL");
5724 static const struct hc_driver fusbh200_fusbh200_hc_driver = {
5725 .description = hcd_name,
5726 .product_desc = "Faraday USB2.0 Host Controller",
5727 .hcd_priv_size = sizeof(struct fusbh200_hcd),
5730 * generic hardware linkage
5732 .irq = fusbh200_irq,
5733 .flags = HCD_MEMORY | HCD_USB2,
5736 * basic lifecycle operations
5738 .reset = hcd_fusbh200_init,
5739 .start = fusbh200_run,
5740 .stop = fusbh200_stop,
5741 .shutdown = fusbh200_shutdown,
5744 * managing i/o requests and associated device resources
5746 .urb_enqueue = fusbh200_urb_enqueue,
5747 .urb_dequeue = fusbh200_urb_dequeue,
5748 .endpoint_disable = fusbh200_endpoint_disable,
5749 .endpoint_reset = fusbh200_endpoint_reset,
5752 * scheduling support
5754 .get_frame_number = fusbh200_get_frame,
5757 * root hub support
5759 .hub_status_data = fusbh200_hub_status_data,
5760 .hub_control = fusbh200_hub_control,
5761 .bus_suspend = fusbh200_bus_suspend,
5762 .bus_resume = fusbh200_bus_resume,
5764 .relinquish_port = fusbh200_relinquish_port,
5765 .port_handed_over = fusbh200_port_handed_over,
5767 .clear_tt_buffer_complete = fusbh200_clear_tt_buffer_complete,
5770 static void fusbh200_init(struct fusbh200_hcd *fusbh200)
5772 u32 reg;
5774 reg = fusbh200_readl(fusbh200, &fusbh200->regs->bmcsr);
5775 reg |= BMCSR_INT_POLARITY;
5776 reg &= ~BMCSR_VBUS_OFF;
5777 fusbh200_writel(fusbh200, reg, &fusbh200->regs->bmcsr);
5779 reg = fusbh200_readl(fusbh200, &fusbh200->regs->bmier);
5780 fusbh200_writel(fusbh200, reg | BMIER_OVC_EN | BMIER_VBUS_ERR_EN,
5781 &fusbh200->regs->bmier);
5785 * fusbh200_hcd_probe - initialize faraday FUSBH200 HCDs
5787 * Allocates basic resources for this USB host controller, and
5788 * then invokes the start() method for the HCD associated with it
5789 * through the hotplug entry's driver_data.
5791 static int fusbh200_hcd_probe(struct platform_device *pdev)
5793 struct device *dev = &pdev->dev;
5794 struct usb_hcd *hcd;
5795 struct resource *res;
5796 int irq;
5797 int retval = -ENODEV;
5798 struct fusbh200_hcd *fusbh200;
5800 if (usb_disabled())
5801 return -ENODEV;
5803 pdev->dev.power.power_state = PMSG_ON;
5805 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
5806 if (!res) {
5807 dev_err(dev,
5808 "Found HC with no IRQ. Check %s setup!\n",
5809 dev_name(dev));
5810 return -ENODEV;
5813 irq = res->start;
5815 hcd = usb_create_hcd(&fusbh200_fusbh200_hc_driver, dev,
5816 dev_name(dev));
5817 if (!hcd) {
5818 dev_err(dev, "failed to create hcd with err %d\n", retval);
5819 retval = -ENOMEM;
5820 goto fail_create_hcd;
5823 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5824 if (!res) {
5825 dev_err(dev,
5826 "Found HC with no register addr. Check %s setup!\n",
5827 dev_name(dev));
5828 retval = -ENODEV;
5829 goto fail_request_resource;
5832 hcd->rsrc_start = res->start;
5833 hcd->rsrc_len = resource_size(res);
5834 hcd->has_tt = 1;
5836 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
5837 fusbh200_fusbh200_hc_driver.description)) {
5838 dev_dbg(dev, "controller already in use\n");
5839 retval = -EBUSY;
5840 goto fail_request_resource;
5843 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
5844 if (!res) {
5845 dev_err(dev,
5846 "Found HC with no register addr. Check %s setup!\n",
5847 dev_name(dev));
5848 retval = -ENODEV;
5849 goto fail_request_resource;
5852 hcd->regs = ioremap_nocache(res->start, resource_size(res));
5853 if (hcd->regs == NULL) {
5854 dev_dbg(dev, "error mapping memory\n");
5855 retval = -EFAULT;
5856 goto fail_ioremap;
5859 fusbh200 = hcd_to_fusbh200(hcd);
5861 fusbh200->caps = hcd->regs;
5863 retval = fusbh200_setup(hcd);
5864 if (retval)
5865 goto fail_add_hcd;
5867 fusbh200_init(fusbh200);
5869 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
5870 if (retval) {
5871 dev_err(dev, "failed to add hcd with err %d\n", retval);
5872 goto fail_add_hcd;
5875 return retval;
5877 fail_add_hcd:
5878 iounmap(hcd->regs);
5879 fail_ioremap:
5880 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
5881 fail_request_resource:
5882 usb_put_hcd(hcd);
5883 fail_create_hcd:
5884 dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
5885 return retval;
5889 * fusbh200_hcd_remove - shutdown processing for EHCI HCDs
5890 * @dev: USB Host Controller being removed
5892 * Reverses the effect of fotg2xx_usb_hcd_probe(), first invoking
5893 * the HCD's stop() method. It is always called from a thread
5894 * context, normally "rmmod", "apmd", or something similar.
5896 static int fusbh200_hcd_remove(struct platform_device *pdev)
5898 struct device *dev = &pdev->dev;
5899 struct usb_hcd *hcd = dev_get_drvdata(dev);
5901 if (!hcd)
5902 return 0;
5904 usb_remove_hcd(hcd);
5905 iounmap(hcd->regs);
5906 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
5907 usb_put_hcd(hcd);
5909 return 0;
5912 static struct platform_driver fusbh200_hcd_fusbh200_driver = {
5913 .driver = {
5914 .name = "fusbh200",
5916 .probe = fusbh200_hcd_probe,
5917 .remove = fusbh200_hcd_remove,
5920 static int __init fusbh200_hcd_init(void)
5922 int retval = 0;
5924 if (usb_disabled())
5925 return -ENODEV;
5927 printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
5928 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5929 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
5930 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
5931 printk(KERN_WARNING "Warning! fusbh200_hcd should always be loaded"
5932 " before uhci_hcd and ohci_hcd, not after\n");
5934 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd\n",
5935 hcd_name,
5936 sizeof(struct fusbh200_qh), sizeof(struct fusbh200_qtd),
5937 sizeof(struct fusbh200_itd));
5939 #ifdef DEBUG
5940 fusbh200_debug_root = debugfs_create_dir("fusbh200", usb_debug_root);
5941 if (!fusbh200_debug_root) {
5942 retval = -ENOENT;
5943 goto err_debug;
5945 #endif
5947 retval = platform_driver_register(&fusbh200_hcd_fusbh200_driver);
5948 if (retval < 0)
5949 goto clean;
5950 return retval;
5952 platform_driver_unregister(&fusbh200_hcd_fusbh200_driver);
5953 clean:
5954 #ifdef DEBUG
5955 debugfs_remove(fusbh200_debug_root);
5956 fusbh200_debug_root = NULL;
5957 err_debug:
5958 #endif
5959 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5960 return retval;
5962 module_init(fusbh200_hcd_init);
5964 static void __exit fusbh200_hcd_cleanup(void)
5966 platform_driver_unregister(&fusbh200_hcd_fusbh200_driver);
5967 #ifdef DEBUG
5968 debugfs_remove(fusbh200_debug_root);
5969 #endif
5970 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5972 module_exit(fusbh200_hcd_cleanup);