mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / usb / host / fusbh200-hcd.c
blob0def3ed720137bccbffb0d9b094f814c0eb2bd46
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 fusbh200_writel(fusbh200, temp | PORT_RESUME, status_reg);
1614 fusbh200->reset_done[wIndex] = jiffies
1615 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
1616 break;
1617 case USB_PORT_FEAT_C_SUSPEND:
1618 clear_bit(wIndex, &fusbh200->port_c_suspend);
1619 break;
1620 case USB_PORT_FEAT_C_CONNECTION:
1621 fusbh200_writel(fusbh200, temp | PORT_CSC, status_reg);
1622 break;
1623 case USB_PORT_FEAT_C_OVER_CURRENT:
1624 fusbh200_writel(fusbh200, temp | BMISR_OVC, &fusbh200->regs->bmisr);
1625 break;
1626 case USB_PORT_FEAT_C_RESET:
1627 /* GetPortStatus clears reset */
1628 break;
1629 default:
1630 goto error;
1632 fusbh200_readl(fusbh200, &fusbh200->regs->command); /* unblock posted write */
1633 break;
1634 case GetHubDescriptor:
1635 fusbh200_hub_descriptor (fusbh200, (struct usb_hub_descriptor *)
1636 buf);
1637 break;
1638 case GetHubStatus:
1639 /* no hub-wide feature/status flags */
1640 memset (buf, 0, 4);
1641 //cpu_to_le32s ((u32 *) buf);
1642 break;
1643 case GetPortStatus:
1644 if (!wIndex || wIndex > ports)
1645 goto error;
1646 wIndex--;
1647 status = 0;
1648 temp = fusbh200_readl(fusbh200, status_reg);
1650 // wPortChange bits
1651 if (temp & PORT_CSC)
1652 status |= USB_PORT_STAT_C_CONNECTION << 16;
1653 if (temp & PORT_PEC)
1654 status |= USB_PORT_STAT_C_ENABLE << 16;
1656 temp1 = fusbh200_readl(fusbh200, &fusbh200->regs->bmisr);
1657 if (temp1 & BMISR_OVC)
1658 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1660 /* whoever resumes must GetPortStatus to complete it!! */
1661 if (temp & PORT_RESUME) {
1663 /* Remote Wakeup received? */
1664 if (!fusbh200->reset_done[wIndex]) {
1665 /* resume signaling for 20 msec */
1666 fusbh200->reset_done[wIndex] = jiffies
1667 + msecs_to_jiffies(20);
1668 /* check the port again */
1669 mod_timer(&fusbh200_to_hcd(fusbh200)->rh_timer,
1670 fusbh200->reset_done[wIndex]);
1673 /* resume completed? */
1674 else if (time_after_eq(jiffies,
1675 fusbh200->reset_done[wIndex])) {
1676 clear_bit(wIndex, &fusbh200->suspended_ports);
1677 set_bit(wIndex, &fusbh200->port_c_suspend);
1678 fusbh200->reset_done[wIndex] = 0;
1680 /* stop resume signaling */
1681 temp = fusbh200_readl(fusbh200, status_reg);
1682 fusbh200_writel(fusbh200,
1683 temp & ~(PORT_RWC_BITS | PORT_RESUME),
1684 status_reg);
1685 clear_bit(wIndex, &fusbh200->resuming_ports);
1686 retval = handshake(fusbh200, status_reg,
1687 PORT_RESUME, 0, 2000 /* 2msec */);
1688 if (retval != 0) {
1689 fusbh200_err(fusbh200,
1690 "port %d resume error %d\n",
1691 wIndex + 1, retval);
1692 goto error;
1694 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1698 /* whoever resets must GetPortStatus to complete it!! */
1699 if ((temp & PORT_RESET)
1700 && time_after_eq(jiffies,
1701 fusbh200->reset_done[wIndex])) {
1702 status |= USB_PORT_STAT_C_RESET << 16;
1703 fusbh200->reset_done [wIndex] = 0;
1704 clear_bit(wIndex, &fusbh200->resuming_ports);
1706 /* force reset to complete */
1707 fusbh200_writel(fusbh200, temp & ~(PORT_RWC_BITS | PORT_RESET),
1708 status_reg);
1709 /* REVISIT: some hardware needs 550+ usec to clear
1710 * this bit; seems too long to spin routinely...
1712 retval = handshake(fusbh200, status_reg,
1713 PORT_RESET, 0, 1000);
1714 if (retval != 0) {
1715 fusbh200_err (fusbh200, "port %d reset error %d\n",
1716 wIndex + 1, retval);
1717 goto error;
1720 /* see what we found out */
1721 temp = check_reset_complete (fusbh200, wIndex, status_reg,
1722 fusbh200_readl(fusbh200, status_reg));
1725 if (!(temp & (PORT_RESUME|PORT_RESET))) {
1726 fusbh200->reset_done[wIndex] = 0;
1727 clear_bit(wIndex, &fusbh200->resuming_ports);
1730 /* transfer dedicated ports to the companion hc */
1731 if ((temp & PORT_CONNECT) &&
1732 test_bit(wIndex, &fusbh200->companion_ports)) {
1733 temp &= ~PORT_RWC_BITS;
1734 fusbh200_writel(fusbh200, temp, status_reg);
1735 fusbh200_dbg(fusbh200, "port %d --> companion\n", wIndex + 1);
1736 temp = fusbh200_readl(fusbh200, status_reg);
1740 * Even if OWNER is set, there's no harm letting khubd
1741 * see the wPortStatus values (they should all be 0 except
1742 * for PORT_POWER anyway).
1745 if (temp & PORT_CONNECT) {
1746 status |= USB_PORT_STAT_CONNECTION;
1747 status |= fusbh200_port_speed(fusbh200, temp);
1749 if (temp & PORT_PE)
1750 status |= USB_PORT_STAT_ENABLE;
1752 /* maybe the port was unsuspended without our knowledge */
1753 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1754 status |= USB_PORT_STAT_SUSPEND;
1755 } else if (test_bit(wIndex, &fusbh200->suspended_ports)) {
1756 clear_bit(wIndex, &fusbh200->suspended_ports);
1757 clear_bit(wIndex, &fusbh200->resuming_ports);
1758 fusbh200->reset_done[wIndex] = 0;
1759 if (temp & PORT_PE)
1760 set_bit(wIndex, &fusbh200->port_c_suspend);
1763 temp1 = fusbh200_readl(fusbh200, &fusbh200->regs->bmisr);
1764 if (temp1 & BMISR_OVC)
1765 status |= USB_PORT_STAT_OVERCURRENT;
1766 if (temp & PORT_RESET)
1767 status |= USB_PORT_STAT_RESET;
1768 if (test_bit(wIndex, &fusbh200->port_c_suspend))
1769 status |= USB_PORT_STAT_C_SUSPEND << 16;
1771 #ifndef VERBOSE_DEBUG
1772 if (status & ~0xffff) /* only if wPortChange is interesting */
1773 #endif
1774 dbg_port (fusbh200, "GetStatus", wIndex + 1, temp);
1775 put_unaligned_le32(status, buf);
1776 break;
1777 case SetHubFeature:
1778 switch (wValue) {
1779 case C_HUB_LOCAL_POWER:
1780 case C_HUB_OVER_CURRENT:
1781 /* no hub-wide feature/status flags */
1782 break;
1783 default:
1784 goto error;
1786 break;
1787 case SetPortFeature:
1788 selector = wIndex >> 8;
1789 wIndex &= 0xff;
1791 if (!wIndex || wIndex > ports)
1792 goto error;
1793 wIndex--;
1794 temp = fusbh200_readl(fusbh200, status_reg);
1795 temp &= ~PORT_RWC_BITS;
1796 switch (wValue) {
1797 case USB_PORT_FEAT_SUSPEND:
1798 if ((temp & PORT_PE) == 0
1799 || (temp & PORT_RESET) != 0)
1800 goto error;
1802 /* After above check the port must be connected.
1803 * Set appropriate bit thus could put phy into low power
1804 * mode if we have hostpc feature
1806 fusbh200_writel(fusbh200, temp | PORT_SUSPEND, status_reg);
1807 set_bit(wIndex, &fusbh200->suspended_ports);
1808 break;
1809 case USB_PORT_FEAT_RESET:
1810 if (temp & PORT_RESUME)
1811 goto error;
1812 /* line status bits may report this as low speed,
1813 * which can be fine if this root hub has a
1814 * transaction translator built in.
1816 fusbh200_vdbg (fusbh200, "port %d reset\n", wIndex + 1);
1817 temp |= PORT_RESET;
1818 temp &= ~PORT_PE;
1821 * caller must wait, then call GetPortStatus
1822 * usb 2.0 spec says 50 ms resets on root
1824 fusbh200->reset_done [wIndex] = jiffies
1825 + msecs_to_jiffies (50);
1826 fusbh200_writel(fusbh200, temp, status_reg);
1827 break;
1829 /* For downstream facing ports (these): one hub port is put
1830 * into test mode according to USB2 11.24.2.13, then the hub
1831 * must be reset (which for root hub now means rmmod+modprobe,
1832 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1833 * about the EHCI-specific stuff.
1835 case USB_PORT_FEAT_TEST:
1836 if (!selector || selector > 5)
1837 goto error;
1838 spin_unlock_irqrestore(&fusbh200->lock, flags);
1839 fusbh200_quiesce(fusbh200);
1840 spin_lock_irqsave(&fusbh200->lock, flags);
1842 /* Put all enabled ports into suspend */
1843 temp = fusbh200_readl(fusbh200, status_reg) & ~PORT_RWC_BITS;
1844 if (temp & PORT_PE)
1845 fusbh200_writel(fusbh200, temp | PORT_SUSPEND,
1846 status_reg);
1848 spin_unlock_irqrestore(&fusbh200->lock, flags);
1849 fusbh200_halt(fusbh200);
1850 spin_lock_irqsave(&fusbh200->lock, flags);
1852 temp = fusbh200_readl(fusbh200, status_reg);
1853 temp |= selector << 16;
1854 fusbh200_writel(fusbh200, temp, status_reg);
1855 break;
1857 default:
1858 goto error;
1860 fusbh200_readl(fusbh200, &fusbh200->regs->command); /* unblock posted writes */
1861 break;
1863 default:
1864 error:
1865 /* "stall" on error */
1866 retval = -EPIPE;
1868 spin_unlock_irqrestore (&fusbh200->lock, flags);
1869 return retval;
1872 static void __maybe_unused fusbh200_relinquish_port(struct usb_hcd *hcd,
1873 int portnum)
1875 return;
1878 static int __maybe_unused fusbh200_port_handed_over(struct usb_hcd *hcd,
1879 int portnum)
1881 return 0;
1883 /*-------------------------------------------------------------------------*/
1885 * There's basically three types of memory:
1886 * - data used only by the HCD ... kmalloc is fine
1887 * - async and periodic schedules, shared by HC and HCD ... these
1888 * need to use dma_pool or dma_alloc_coherent
1889 * - driver buffers, read/written by HC ... single shot DMA mapped
1891 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
1892 * No memory seen by this driver is pageable.
1895 /*-------------------------------------------------------------------------*/
1897 /* Allocate the key transfer structures from the previously allocated pool */
1899 static inline void fusbh200_qtd_init(struct fusbh200_hcd *fusbh200, struct fusbh200_qtd *qtd,
1900 dma_addr_t dma)
1902 memset (qtd, 0, sizeof *qtd);
1903 qtd->qtd_dma = dma;
1904 qtd->hw_token = cpu_to_hc32(fusbh200, QTD_STS_HALT);
1905 qtd->hw_next = FUSBH200_LIST_END(fusbh200);
1906 qtd->hw_alt_next = FUSBH200_LIST_END(fusbh200);
1907 INIT_LIST_HEAD (&qtd->qtd_list);
1910 static struct fusbh200_qtd *fusbh200_qtd_alloc (struct fusbh200_hcd *fusbh200, gfp_t flags)
1912 struct fusbh200_qtd *qtd;
1913 dma_addr_t dma;
1915 qtd = dma_pool_alloc (fusbh200->qtd_pool, flags, &dma);
1916 if (qtd != NULL) {
1917 fusbh200_qtd_init(fusbh200, qtd, dma);
1919 return qtd;
1922 static inline void fusbh200_qtd_free (struct fusbh200_hcd *fusbh200, struct fusbh200_qtd *qtd)
1924 dma_pool_free (fusbh200->qtd_pool, qtd, qtd->qtd_dma);
1928 static void qh_destroy(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
1930 /* clean qtds first, and know this is not linked */
1931 if (!list_empty (&qh->qtd_list) || qh->qh_next.ptr) {
1932 fusbh200_dbg (fusbh200, "unused qh not empty!\n");
1933 BUG ();
1935 if (qh->dummy)
1936 fusbh200_qtd_free (fusbh200, qh->dummy);
1937 dma_pool_free(fusbh200->qh_pool, qh->hw, qh->qh_dma);
1938 kfree(qh);
1941 static struct fusbh200_qh *fusbh200_qh_alloc (struct fusbh200_hcd *fusbh200, gfp_t flags)
1943 struct fusbh200_qh *qh;
1944 dma_addr_t dma;
1946 qh = kzalloc(sizeof *qh, GFP_ATOMIC);
1947 if (!qh)
1948 goto done;
1949 qh->hw = (struct fusbh200_qh_hw *)
1950 dma_pool_alloc(fusbh200->qh_pool, flags, &dma);
1951 if (!qh->hw)
1952 goto fail;
1953 memset(qh->hw, 0, sizeof *qh->hw);
1954 qh->qh_dma = dma;
1955 // INIT_LIST_HEAD (&qh->qh_list);
1956 INIT_LIST_HEAD (&qh->qtd_list);
1958 /* dummy td enables safe urb queuing */
1959 qh->dummy = fusbh200_qtd_alloc (fusbh200, flags);
1960 if (qh->dummy == NULL) {
1961 fusbh200_dbg (fusbh200, "no dummy td\n");
1962 goto fail1;
1964 done:
1965 return qh;
1966 fail1:
1967 dma_pool_free(fusbh200->qh_pool, qh->hw, qh->qh_dma);
1968 fail:
1969 kfree(qh);
1970 return NULL;
1973 /*-------------------------------------------------------------------------*/
1975 /* The queue heads and transfer descriptors are managed from pools tied
1976 * to each of the "per device" structures.
1977 * This is the initialisation and cleanup code.
1980 static void fusbh200_mem_cleanup (struct fusbh200_hcd *fusbh200)
1982 if (fusbh200->async)
1983 qh_destroy(fusbh200, fusbh200->async);
1984 fusbh200->async = NULL;
1986 if (fusbh200->dummy)
1987 qh_destroy(fusbh200, fusbh200->dummy);
1988 fusbh200->dummy = NULL;
1990 /* DMA consistent memory and pools */
1991 if (fusbh200->qtd_pool)
1992 dma_pool_destroy (fusbh200->qtd_pool);
1993 fusbh200->qtd_pool = NULL;
1995 if (fusbh200->qh_pool) {
1996 dma_pool_destroy (fusbh200->qh_pool);
1997 fusbh200->qh_pool = NULL;
2000 if (fusbh200->itd_pool)
2001 dma_pool_destroy (fusbh200->itd_pool);
2002 fusbh200->itd_pool = NULL;
2004 if (fusbh200->periodic)
2005 dma_free_coherent (fusbh200_to_hcd(fusbh200)->self.controller,
2006 fusbh200->periodic_size * sizeof (u32),
2007 fusbh200->periodic, fusbh200->periodic_dma);
2008 fusbh200->periodic = NULL;
2010 /* shadow periodic table */
2011 kfree(fusbh200->pshadow);
2012 fusbh200->pshadow = NULL;
2015 /* remember to add cleanup code (above) if you add anything here */
2016 static int fusbh200_mem_init (struct fusbh200_hcd *fusbh200, gfp_t flags)
2018 int i;
2020 /* QTDs for control/bulk/intr transfers */
2021 fusbh200->qtd_pool = dma_pool_create ("fusbh200_qtd",
2022 fusbh200_to_hcd(fusbh200)->self.controller,
2023 sizeof (struct fusbh200_qtd),
2024 32 /* byte alignment (for hw parts) */,
2025 4096 /* can't cross 4K */);
2026 if (!fusbh200->qtd_pool) {
2027 goto fail;
2030 /* QHs for control/bulk/intr transfers */
2031 fusbh200->qh_pool = dma_pool_create ("fusbh200_qh",
2032 fusbh200_to_hcd(fusbh200)->self.controller,
2033 sizeof(struct fusbh200_qh_hw),
2034 32 /* byte alignment (for hw parts) */,
2035 4096 /* can't cross 4K */);
2036 if (!fusbh200->qh_pool) {
2037 goto fail;
2039 fusbh200->async = fusbh200_qh_alloc (fusbh200, flags);
2040 if (!fusbh200->async) {
2041 goto fail;
2044 /* ITD for high speed ISO transfers */
2045 fusbh200->itd_pool = dma_pool_create ("fusbh200_itd",
2046 fusbh200_to_hcd(fusbh200)->self.controller,
2047 sizeof (struct fusbh200_itd),
2048 64 /* byte alignment (for hw parts) */,
2049 4096 /* can't cross 4K */);
2050 if (!fusbh200->itd_pool) {
2051 goto fail;
2054 /* Hardware periodic table */
2055 fusbh200->periodic = (__le32 *)
2056 dma_alloc_coherent (fusbh200_to_hcd(fusbh200)->self.controller,
2057 fusbh200->periodic_size * sizeof(__le32),
2058 &fusbh200->periodic_dma, 0);
2059 if (fusbh200->periodic == NULL) {
2060 goto fail;
2063 for (i = 0; i < fusbh200->periodic_size; i++)
2064 fusbh200->periodic[i] = FUSBH200_LIST_END(fusbh200);
2066 /* software shadow of hardware table */
2067 fusbh200->pshadow = kcalloc(fusbh200->periodic_size, sizeof(void *), flags);
2068 if (fusbh200->pshadow != NULL)
2069 return 0;
2071 fail:
2072 fusbh200_dbg (fusbh200, "couldn't init memory\n");
2073 fusbh200_mem_cleanup (fusbh200);
2074 return -ENOMEM;
2076 /*-------------------------------------------------------------------------*/
2078 * EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
2080 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
2081 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
2082 * buffers needed for the larger number). We use one QH per endpoint, queue
2083 * multiple urbs (all three types) per endpoint. URBs may need several qtds.
2085 * ISO traffic uses "ISO TD" (itd) records, and (along with
2086 * interrupts) needs careful scheduling. Performance improvements can be
2087 * an ongoing challenge. That's in "ehci-sched.c".
2089 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
2090 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
2091 * (b) special fields in qh entries or (c) split iso entries. TTs will
2092 * buffer low/full speed data so the host collects it at high speed.
2095 /*-------------------------------------------------------------------------*/
2097 /* fill a qtd, returning how much of the buffer we were able to queue up */
2099 static int
2100 qtd_fill(struct fusbh200_hcd *fusbh200, struct fusbh200_qtd *qtd, dma_addr_t buf,
2101 size_t len, int token, int maxpacket)
2103 int i, count;
2104 u64 addr = buf;
2106 /* one buffer entry per 4K ... first might be short or unaligned */
2107 qtd->hw_buf[0] = cpu_to_hc32(fusbh200, (u32)addr);
2108 qtd->hw_buf_hi[0] = cpu_to_hc32(fusbh200, (u32)(addr >> 32));
2109 count = 0x1000 - (buf & 0x0fff); /* rest of that page */
2110 if (likely (len < count)) /* ... iff needed */
2111 count = len;
2112 else {
2113 buf += 0x1000;
2114 buf &= ~0x0fff;
2116 /* per-qtd limit: from 16K to 20K (best alignment) */
2117 for (i = 1; count < len && i < 5; i++) {
2118 addr = buf;
2119 qtd->hw_buf[i] = cpu_to_hc32(fusbh200, (u32)addr);
2120 qtd->hw_buf_hi[i] = cpu_to_hc32(fusbh200,
2121 (u32)(addr >> 32));
2122 buf += 0x1000;
2123 if ((count + 0x1000) < len)
2124 count += 0x1000;
2125 else
2126 count = len;
2129 /* short packets may only terminate transfers */
2130 if (count != len)
2131 count -= (count % maxpacket);
2133 qtd->hw_token = cpu_to_hc32(fusbh200, (count << 16) | token);
2134 qtd->length = count;
2136 return count;
2139 /*-------------------------------------------------------------------------*/
2141 static inline void
2142 qh_update (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh, struct fusbh200_qtd *qtd)
2144 struct fusbh200_qh_hw *hw = qh->hw;
2146 /* writes to an active overlay are unsafe */
2147 BUG_ON(qh->qh_state != QH_STATE_IDLE);
2149 hw->hw_qtd_next = QTD_NEXT(fusbh200, qtd->qtd_dma);
2150 hw->hw_alt_next = FUSBH200_LIST_END(fusbh200);
2152 /* Except for control endpoints, we make hardware maintain data
2153 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
2154 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
2155 * ever clear it.
2157 if (!(hw->hw_info1 & cpu_to_hc32(fusbh200, QH_TOGGLE_CTL))) {
2158 unsigned is_out, epnum;
2160 is_out = qh->is_out;
2161 epnum = (hc32_to_cpup(fusbh200, &hw->hw_info1) >> 8) & 0x0f;
2162 if (unlikely (!usb_gettoggle (qh->dev, epnum, is_out))) {
2163 hw->hw_token &= ~cpu_to_hc32(fusbh200, QTD_TOGGLE);
2164 usb_settoggle (qh->dev, epnum, is_out, 1);
2168 hw->hw_token &= cpu_to_hc32(fusbh200, QTD_TOGGLE | QTD_STS_PING);
2171 /* if it weren't for a common silicon quirk (writing the dummy into the qh
2172 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
2173 * recovery (including urb dequeue) would need software changes to a QH...
2175 static void
2176 qh_refresh (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
2178 struct fusbh200_qtd *qtd;
2180 if (list_empty (&qh->qtd_list))
2181 qtd = qh->dummy;
2182 else {
2183 qtd = list_entry (qh->qtd_list.next,
2184 struct fusbh200_qtd, qtd_list);
2186 * first qtd may already be partially processed.
2187 * If we come here during unlink, the QH overlay region
2188 * might have reference to the just unlinked qtd. The
2189 * qtd is updated in qh_completions(). Update the QH
2190 * overlay here.
2192 if (cpu_to_hc32(fusbh200, qtd->qtd_dma) == qh->hw->hw_current) {
2193 qh->hw->hw_qtd_next = qtd->hw_next;
2194 qtd = NULL;
2198 if (qtd)
2199 qh_update (fusbh200, qh, qtd);
2202 /*-------------------------------------------------------------------------*/
2204 static void qh_link_async(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh);
2206 static void fusbh200_clear_tt_buffer_complete(struct usb_hcd *hcd,
2207 struct usb_host_endpoint *ep)
2209 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
2210 struct fusbh200_qh *qh = ep->hcpriv;
2211 unsigned long flags;
2213 spin_lock_irqsave(&fusbh200->lock, flags);
2214 qh->clearing_tt = 0;
2215 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
2216 && fusbh200->rh_state == FUSBH200_RH_RUNNING)
2217 qh_link_async(fusbh200, qh);
2218 spin_unlock_irqrestore(&fusbh200->lock, flags);
2221 static void fusbh200_clear_tt_buffer(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh,
2222 struct urb *urb, u32 token)
2225 /* If an async split transaction gets an error or is unlinked,
2226 * the TT buffer may be left in an indeterminate state. We
2227 * have to clear the TT buffer.
2229 * Note: this routine is never called for Isochronous transfers.
2231 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
2232 #ifdef DEBUG
2233 struct usb_device *tt = urb->dev->tt->hub;
2234 dev_dbg(&tt->dev,
2235 "clear tt buffer port %d, a%d ep%d t%08x\n",
2236 urb->dev->ttport, urb->dev->devnum,
2237 usb_pipeendpoint(urb->pipe), token);
2238 #endif /* DEBUG */
2239 if (urb->dev->tt->hub !=
2240 fusbh200_to_hcd(fusbh200)->self.root_hub) {
2241 if (usb_hub_clear_tt_buffer(urb) == 0)
2242 qh->clearing_tt = 1;
2247 static int qtd_copy_status (
2248 struct fusbh200_hcd *fusbh200,
2249 struct urb *urb,
2250 size_t length,
2251 u32 token
2254 int status = -EINPROGRESS;
2256 /* count IN/OUT bytes, not SETUP (even short packets) */
2257 if (likely (QTD_PID (token) != 2))
2258 urb->actual_length += length - QTD_LENGTH (token);
2260 /* don't modify error codes */
2261 if (unlikely(urb->unlinked))
2262 return status;
2264 /* force cleanup after short read; not always an error */
2265 if (unlikely (IS_SHORT_READ (token)))
2266 status = -EREMOTEIO;
2268 /* serious "can't proceed" faults reported by the hardware */
2269 if (token & QTD_STS_HALT) {
2270 if (token & QTD_STS_BABBLE) {
2271 /* FIXME "must" disable babbling device's port too */
2272 status = -EOVERFLOW;
2273 /* CERR nonzero + halt --> stall */
2274 } else if (QTD_CERR(token)) {
2275 status = -EPIPE;
2277 /* In theory, more than one of the following bits can be set
2278 * since they are sticky and the transaction is retried.
2279 * Which to test first is rather arbitrary.
2281 } else if (token & QTD_STS_MMF) {
2282 /* fs/ls interrupt xfer missed the complete-split */
2283 status = -EPROTO;
2284 } else if (token & QTD_STS_DBE) {
2285 status = (QTD_PID (token) == 1) /* IN ? */
2286 ? -ENOSR /* hc couldn't read data */
2287 : -ECOMM; /* hc couldn't write data */
2288 } else if (token & QTD_STS_XACT) {
2289 /* timeout, bad CRC, wrong PID, etc */
2290 fusbh200_dbg(fusbh200, "devpath %s ep%d%s 3strikes\n",
2291 urb->dev->devpath,
2292 usb_pipeendpoint(urb->pipe),
2293 usb_pipein(urb->pipe) ? "in" : "out");
2294 status = -EPROTO;
2295 } else { /* unknown */
2296 status = -EPROTO;
2299 fusbh200_vdbg (fusbh200,
2300 "dev%d ep%d%s qtd token %08x --> status %d\n",
2301 usb_pipedevice (urb->pipe),
2302 usb_pipeendpoint (urb->pipe),
2303 usb_pipein (urb->pipe) ? "in" : "out",
2304 token, status);
2307 return status;
2310 static void
2311 fusbh200_urb_done(struct fusbh200_hcd *fusbh200, struct urb *urb, int status)
2312 __releases(fusbh200->lock)
2313 __acquires(fusbh200->lock)
2315 if (likely (urb->hcpriv != NULL)) {
2316 struct fusbh200_qh *qh = (struct fusbh200_qh *) urb->hcpriv;
2318 /* S-mask in a QH means it's an interrupt urb */
2319 if ((qh->hw->hw_info2 & cpu_to_hc32(fusbh200, QH_SMASK)) != 0) {
2321 /* ... update hc-wide periodic stats (for usbfs) */
2322 fusbh200_to_hcd(fusbh200)->self.bandwidth_int_reqs--;
2326 if (unlikely(urb->unlinked)) {
2327 COUNT(fusbh200->stats.unlink);
2328 } else {
2329 /* report non-error and short read status as zero */
2330 if (status == -EINPROGRESS || status == -EREMOTEIO)
2331 status = 0;
2332 COUNT(fusbh200->stats.complete);
2335 #ifdef FUSBH200_URB_TRACE
2336 fusbh200_dbg (fusbh200,
2337 "%s %s urb %p ep%d%s status %d len %d/%d\n",
2338 __func__, urb->dev->devpath, urb,
2339 usb_pipeendpoint (urb->pipe),
2340 usb_pipein (urb->pipe) ? "in" : "out",
2341 status,
2342 urb->actual_length, urb->transfer_buffer_length);
2343 #endif
2345 /* complete() can reenter this HCD */
2346 usb_hcd_unlink_urb_from_ep(fusbh200_to_hcd(fusbh200), urb);
2347 spin_unlock (&fusbh200->lock);
2348 usb_hcd_giveback_urb(fusbh200_to_hcd(fusbh200), urb, status);
2349 spin_lock (&fusbh200->lock);
2352 static int qh_schedule (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh);
2355 * Process and free completed qtds for a qh, returning URBs to drivers.
2356 * Chases up to qh->hw_current. Returns number of completions called,
2357 * indicating how much "real" work we did.
2359 static unsigned
2360 qh_completions (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
2362 struct fusbh200_qtd *last, *end = qh->dummy;
2363 struct list_head *entry, *tmp;
2364 int last_status;
2365 int stopped;
2366 unsigned count = 0;
2367 u8 state;
2368 struct fusbh200_qh_hw *hw = qh->hw;
2370 if (unlikely (list_empty (&qh->qtd_list)))
2371 return count;
2373 /* completions (or tasks on other cpus) must never clobber HALT
2374 * till we've gone through and cleaned everything up, even when
2375 * they add urbs to this qh's queue or mark them for unlinking.
2377 * NOTE: unlinking expects to be done in queue order.
2379 * It's a bug for qh->qh_state to be anything other than
2380 * QH_STATE_IDLE, unless our caller is scan_async() or
2381 * scan_intr().
2383 state = qh->qh_state;
2384 qh->qh_state = QH_STATE_COMPLETING;
2385 stopped = (state == QH_STATE_IDLE);
2387 rescan:
2388 last = NULL;
2389 last_status = -EINPROGRESS;
2390 qh->needs_rescan = 0;
2392 /* remove de-activated QTDs from front of queue.
2393 * after faults (including short reads), cleanup this urb
2394 * then let the queue advance.
2395 * if queue is stopped, handles unlinks.
2397 list_for_each_safe (entry, tmp, &qh->qtd_list) {
2398 struct fusbh200_qtd *qtd;
2399 struct urb *urb;
2400 u32 token = 0;
2402 qtd = list_entry (entry, struct fusbh200_qtd, qtd_list);
2403 urb = qtd->urb;
2405 /* clean up any state from previous QTD ...*/
2406 if (last) {
2407 if (likely (last->urb != urb)) {
2408 fusbh200_urb_done(fusbh200, last->urb, last_status);
2409 count++;
2410 last_status = -EINPROGRESS;
2412 fusbh200_qtd_free (fusbh200, last);
2413 last = NULL;
2416 /* ignore urbs submitted during completions we reported */
2417 if (qtd == end)
2418 break;
2420 /* hardware copies qtd out of qh overlay */
2421 rmb ();
2422 token = hc32_to_cpu(fusbh200, qtd->hw_token);
2424 /* always clean up qtds the hc de-activated */
2425 retry_xacterr:
2426 if ((token & QTD_STS_ACTIVE) == 0) {
2428 /* Report Data Buffer Error: non-fatal but useful */
2429 if (token & QTD_STS_DBE)
2430 fusbh200_dbg(fusbh200,
2431 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
2432 urb,
2433 usb_endpoint_num(&urb->ep->desc),
2434 usb_endpoint_dir_in(&urb->ep->desc) ? "in" : "out",
2435 urb->transfer_buffer_length,
2436 qtd,
2437 qh);
2439 /* on STALL, error, and short reads this urb must
2440 * complete and all its qtds must be recycled.
2442 if ((token & QTD_STS_HALT) != 0) {
2444 /* retry transaction errors until we
2445 * reach the software xacterr limit
2447 if ((token & QTD_STS_XACT) &&
2448 QTD_CERR(token) == 0 &&
2449 ++qh->xacterrs < QH_XACTERR_MAX &&
2450 !urb->unlinked) {
2451 fusbh200_dbg(fusbh200,
2452 "detected XactErr len %zu/%zu retry %d\n",
2453 qtd->length - QTD_LENGTH(token), qtd->length, qh->xacterrs);
2455 /* reset the token in the qtd and the
2456 * qh overlay (which still contains
2457 * the qtd) so that we pick up from
2458 * where we left off
2460 token &= ~QTD_STS_HALT;
2461 token |= QTD_STS_ACTIVE |
2462 (FUSBH200_TUNE_CERR << 10);
2463 qtd->hw_token = cpu_to_hc32(fusbh200,
2464 token);
2465 wmb();
2466 hw->hw_token = cpu_to_hc32(fusbh200,
2467 token);
2468 goto retry_xacterr;
2470 stopped = 1;
2472 /* magic dummy for some short reads; qh won't advance.
2473 * that silicon quirk can kick in with this dummy too.
2475 * other short reads won't stop the queue, including
2476 * control transfers (status stage handles that) or
2477 * most other single-qtd reads ... the queue stops if
2478 * URB_SHORT_NOT_OK was set so the driver submitting
2479 * the urbs could clean it up.
2481 } else if (IS_SHORT_READ (token)
2482 && !(qtd->hw_alt_next
2483 & FUSBH200_LIST_END(fusbh200))) {
2484 stopped = 1;
2487 /* stop scanning when we reach qtds the hc is using */
2488 } else if (likely (!stopped
2489 && fusbh200->rh_state >= FUSBH200_RH_RUNNING)) {
2490 break;
2492 /* scan the whole queue for unlinks whenever it stops */
2493 } else {
2494 stopped = 1;
2496 /* cancel everything if we halt, suspend, etc */
2497 if (fusbh200->rh_state < FUSBH200_RH_RUNNING)
2498 last_status = -ESHUTDOWN;
2500 /* this qtd is active; skip it unless a previous qtd
2501 * for its urb faulted, or its urb was canceled.
2503 else if (last_status == -EINPROGRESS && !urb->unlinked)
2504 continue;
2506 /* qh unlinked; token in overlay may be most current */
2507 if (state == QH_STATE_IDLE
2508 && cpu_to_hc32(fusbh200, qtd->qtd_dma)
2509 == hw->hw_current) {
2510 token = hc32_to_cpu(fusbh200, hw->hw_token);
2512 /* An unlink may leave an incomplete
2513 * async transaction in the TT buffer.
2514 * We have to clear it.
2516 fusbh200_clear_tt_buffer(fusbh200, qh, urb, token);
2520 /* unless we already know the urb's status, collect qtd status
2521 * and update count of bytes transferred. in common short read
2522 * cases with only one data qtd (including control transfers),
2523 * queue processing won't halt. but with two or more qtds (for
2524 * example, with a 32 KB transfer), when the first qtd gets a
2525 * short read the second must be removed by hand.
2527 if (last_status == -EINPROGRESS) {
2528 last_status = qtd_copy_status(fusbh200, urb,
2529 qtd->length, token);
2530 if (last_status == -EREMOTEIO
2531 && (qtd->hw_alt_next
2532 & FUSBH200_LIST_END(fusbh200)))
2533 last_status = -EINPROGRESS;
2535 /* As part of low/full-speed endpoint-halt processing
2536 * we must clear the TT buffer (11.17.5).
2538 if (unlikely(last_status != -EINPROGRESS &&
2539 last_status != -EREMOTEIO)) {
2540 /* The TT's in some hubs malfunction when they
2541 * receive this request following a STALL (they
2542 * stop sending isochronous packets). Since a
2543 * STALL can't leave the TT buffer in a busy
2544 * state (if you believe Figures 11-48 - 11-51
2545 * in the USB 2.0 spec), we won't clear the TT
2546 * buffer in this case. Strictly speaking this
2547 * is a violation of the spec.
2549 if (last_status != -EPIPE)
2550 fusbh200_clear_tt_buffer(fusbh200, qh, urb,
2551 token);
2555 /* if we're removing something not at the queue head,
2556 * patch the hardware queue pointer.
2558 if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
2559 last = list_entry (qtd->qtd_list.prev,
2560 struct fusbh200_qtd, qtd_list);
2561 last->hw_next = qtd->hw_next;
2564 /* remove qtd; it's recycled after possible urb completion */
2565 list_del (&qtd->qtd_list);
2566 last = qtd;
2568 /* reinit the xacterr counter for the next qtd */
2569 qh->xacterrs = 0;
2572 /* last urb's completion might still need calling */
2573 if (likely (last != NULL)) {
2574 fusbh200_urb_done(fusbh200, last->urb, last_status);
2575 count++;
2576 fusbh200_qtd_free (fusbh200, last);
2579 /* Do we need to rescan for URBs dequeued during a giveback? */
2580 if (unlikely(qh->needs_rescan)) {
2581 /* If the QH is already unlinked, do the rescan now. */
2582 if (state == QH_STATE_IDLE)
2583 goto rescan;
2585 /* Otherwise we have to wait until the QH is fully unlinked.
2586 * Our caller will start an unlink if qh->needs_rescan is
2587 * set. But if an unlink has already started, nothing needs
2588 * to be done.
2590 if (state != QH_STATE_LINKED)
2591 qh->needs_rescan = 0;
2594 /* restore original state; caller must unlink or relink */
2595 qh->qh_state = state;
2597 /* be sure the hardware's done with the qh before refreshing
2598 * it after fault cleanup, or recovering from silicon wrongly
2599 * overlaying the dummy qtd (which reduces DMA chatter).
2601 if (stopped != 0 || hw->hw_qtd_next == FUSBH200_LIST_END(fusbh200)) {
2602 switch (state) {
2603 case QH_STATE_IDLE:
2604 qh_refresh(fusbh200, qh);
2605 break;
2606 case QH_STATE_LINKED:
2607 /* We won't refresh a QH that's linked (after the HC
2608 * stopped the queue). That avoids a race:
2609 * - HC reads first part of QH;
2610 * - CPU updates that first part and the token;
2611 * - HC reads rest of that QH, including token
2612 * Result: HC gets an inconsistent image, and then
2613 * DMAs to/from the wrong memory (corrupting it).
2615 * That should be rare for interrupt transfers,
2616 * except maybe high bandwidth ...
2619 /* Tell the caller to start an unlink */
2620 qh->needs_rescan = 1;
2621 break;
2622 /* otherwise, unlink already started */
2626 return count;
2629 /*-------------------------------------------------------------------------*/
2631 // high bandwidth multiplier, as encoded in highspeed endpoint descriptors
2632 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
2633 // ... and packet size, for any kind of endpoint descriptor
2634 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
2637 * reverse of qh_urb_transaction: free a list of TDs.
2638 * used for cleanup after errors, before HC sees an URB's TDs.
2640 static void qtd_list_free (
2641 struct fusbh200_hcd *fusbh200,
2642 struct urb *urb,
2643 struct list_head *qtd_list
2645 struct list_head *entry, *temp;
2647 list_for_each_safe (entry, temp, qtd_list) {
2648 struct fusbh200_qtd *qtd;
2650 qtd = list_entry (entry, struct fusbh200_qtd, qtd_list);
2651 list_del (&qtd->qtd_list);
2652 fusbh200_qtd_free (fusbh200, qtd);
2657 * create a list of filled qtds for this URB; won't link into qh.
2659 static struct list_head *
2660 qh_urb_transaction (
2661 struct fusbh200_hcd *fusbh200,
2662 struct urb *urb,
2663 struct list_head *head,
2664 gfp_t flags
2666 struct fusbh200_qtd *qtd, *qtd_prev;
2667 dma_addr_t buf;
2668 int len, this_sg_len, maxpacket;
2669 int is_input;
2670 u32 token;
2671 int i;
2672 struct scatterlist *sg;
2675 * URBs map to sequences of QTDs: one logical transaction
2677 qtd = fusbh200_qtd_alloc (fusbh200, flags);
2678 if (unlikely (!qtd))
2679 return NULL;
2680 list_add_tail (&qtd->qtd_list, head);
2681 qtd->urb = urb;
2683 token = QTD_STS_ACTIVE;
2684 token |= (FUSBH200_TUNE_CERR << 10);
2685 /* for split transactions, SplitXState initialized to zero */
2687 len = urb->transfer_buffer_length;
2688 is_input = usb_pipein (urb->pipe);
2689 if (usb_pipecontrol (urb->pipe)) {
2690 /* SETUP pid */
2691 qtd_fill(fusbh200, qtd, urb->setup_dma,
2692 sizeof (struct usb_ctrlrequest),
2693 token | (2 /* "setup" */ << 8), 8);
2695 /* ... and always at least one more pid */
2696 token ^= QTD_TOGGLE;
2697 qtd_prev = qtd;
2698 qtd = fusbh200_qtd_alloc (fusbh200, flags);
2699 if (unlikely (!qtd))
2700 goto cleanup;
2701 qtd->urb = urb;
2702 qtd_prev->hw_next = QTD_NEXT(fusbh200, qtd->qtd_dma);
2703 list_add_tail (&qtd->qtd_list, head);
2705 /* for zero length DATA stages, STATUS is always IN */
2706 if (len == 0)
2707 token |= (1 /* "in" */ << 8);
2711 * data transfer stage: buffer setup
2713 i = urb->num_mapped_sgs;
2714 if (len > 0 && i > 0) {
2715 sg = urb->sg;
2716 buf = sg_dma_address(sg);
2718 /* urb->transfer_buffer_length may be smaller than the
2719 * size of the scatterlist (or vice versa)
2721 this_sg_len = min_t(int, sg_dma_len(sg), len);
2722 } else {
2723 sg = NULL;
2724 buf = urb->transfer_dma;
2725 this_sg_len = len;
2728 if (is_input)
2729 token |= (1 /* "in" */ << 8);
2730 /* else it's already initted to "out" pid (0 << 8) */
2732 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
2735 * buffer gets wrapped in one or more qtds;
2736 * last one may be "short" (including zero len)
2737 * and may serve as a control status ack
2739 for (;;) {
2740 int this_qtd_len;
2742 this_qtd_len = qtd_fill(fusbh200, qtd, buf, this_sg_len, token,
2743 maxpacket);
2744 this_sg_len -= this_qtd_len;
2745 len -= this_qtd_len;
2746 buf += this_qtd_len;
2749 * short reads advance to a "magic" dummy instead of the next
2750 * qtd ... that forces the queue to stop, for manual cleanup.
2751 * (this will usually be overridden later.)
2753 if (is_input)
2754 qtd->hw_alt_next = fusbh200->async->hw->hw_alt_next;
2756 /* qh makes control packets use qtd toggle; maybe switch it */
2757 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
2758 token ^= QTD_TOGGLE;
2760 if (likely(this_sg_len <= 0)) {
2761 if (--i <= 0 || len <= 0)
2762 break;
2763 sg = sg_next(sg);
2764 buf = sg_dma_address(sg);
2765 this_sg_len = min_t(int, sg_dma_len(sg), len);
2768 qtd_prev = qtd;
2769 qtd = fusbh200_qtd_alloc (fusbh200, flags);
2770 if (unlikely (!qtd))
2771 goto cleanup;
2772 qtd->urb = urb;
2773 qtd_prev->hw_next = QTD_NEXT(fusbh200, qtd->qtd_dma);
2774 list_add_tail (&qtd->qtd_list, head);
2778 * unless the caller requires manual cleanup after short reads,
2779 * have the alt_next mechanism keep the queue running after the
2780 * last data qtd (the only one, for control and most other cases).
2782 if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
2783 || usb_pipecontrol (urb->pipe)))
2784 qtd->hw_alt_next = FUSBH200_LIST_END(fusbh200);
2787 * control requests may need a terminating data "status" ack;
2788 * other OUT ones may need a terminating short packet
2789 * (zero length).
2791 if (likely (urb->transfer_buffer_length != 0)) {
2792 int one_more = 0;
2794 if (usb_pipecontrol (urb->pipe)) {
2795 one_more = 1;
2796 token ^= 0x0100; /* "in" <--> "out" */
2797 token |= QTD_TOGGLE; /* force DATA1 */
2798 } else if (usb_pipeout(urb->pipe)
2799 && (urb->transfer_flags & URB_ZERO_PACKET)
2800 && !(urb->transfer_buffer_length % maxpacket)) {
2801 one_more = 1;
2803 if (one_more) {
2804 qtd_prev = qtd;
2805 qtd = fusbh200_qtd_alloc (fusbh200, flags);
2806 if (unlikely (!qtd))
2807 goto cleanup;
2808 qtd->urb = urb;
2809 qtd_prev->hw_next = QTD_NEXT(fusbh200, qtd->qtd_dma);
2810 list_add_tail (&qtd->qtd_list, head);
2812 /* never any data in such packets */
2813 qtd_fill(fusbh200, qtd, 0, 0, token, 0);
2817 /* by default, enable interrupt on urb completion */
2818 if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
2819 qtd->hw_token |= cpu_to_hc32(fusbh200, QTD_IOC);
2820 return head;
2822 cleanup:
2823 qtd_list_free (fusbh200, urb, head);
2824 return NULL;
2827 /*-------------------------------------------------------------------------*/
2829 // Would be best to create all qh's from config descriptors,
2830 // when each interface/altsetting is established. Unlink
2831 // any previous qh and cancel its urbs first; endpoints are
2832 // implicitly reset then (data toggle too).
2833 // That'd mean updating how usbcore talks to HCDs. (2.7?)
2837 * Each QH holds a qtd list; a QH is used for everything except iso.
2839 * For interrupt urbs, the scheduler must set the microframe scheduling
2840 * mask(s) each time the QH gets scheduled. For highspeed, that's
2841 * just one microframe in the s-mask. For split interrupt transactions
2842 * there are additional complications: c-mask, maybe FSTNs.
2844 static struct fusbh200_qh *
2845 qh_make (
2846 struct fusbh200_hcd *fusbh200,
2847 struct urb *urb,
2848 gfp_t flags
2850 struct fusbh200_qh *qh = fusbh200_qh_alloc (fusbh200, flags);
2851 u32 info1 = 0, info2 = 0;
2852 int is_input, type;
2853 int maxp = 0;
2854 struct usb_tt *tt = urb->dev->tt;
2855 struct fusbh200_qh_hw *hw;
2857 if (!qh)
2858 return qh;
2861 * init endpoint/device data for this QH
2863 info1 |= usb_pipeendpoint (urb->pipe) << 8;
2864 info1 |= usb_pipedevice (urb->pipe) << 0;
2866 is_input = usb_pipein (urb->pipe);
2867 type = usb_pipetype (urb->pipe);
2868 maxp = usb_maxpacket (urb->dev, urb->pipe, !is_input);
2870 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
2871 * acts like up to 3KB, but is built from smaller packets.
2873 if (max_packet(maxp) > 1024) {
2874 fusbh200_dbg(fusbh200, "bogus qh maxpacket %d\n", max_packet(maxp));
2875 goto done;
2878 /* Compute interrupt scheduling parameters just once, and save.
2879 * - allowing for high bandwidth, how many nsec/uframe are used?
2880 * - split transactions need a second CSPLIT uframe; same question
2881 * - splits also need a schedule gap (for full/low speed I/O)
2882 * - qh has a polling interval
2884 * For control/bulk requests, the HC or TT handles these.
2886 if (type == PIPE_INTERRUPT) {
2887 qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
2888 is_input, 0,
2889 hb_mult(maxp) * max_packet(maxp)));
2890 qh->start = NO_FRAME;
2892 if (urb->dev->speed == USB_SPEED_HIGH) {
2893 qh->c_usecs = 0;
2894 qh->gap_uf = 0;
2896 qh->period = urb->interval >> 3;
2897 if (qh->period == 0 && urb->interval != 1) {
2898 /* NOTE interval 2 or 4 uframes could work.
2899 * But interval 1 scheduling is simpler, and
2900 * includes high bandwidth.
2902 urb->interval = 1;
2903 } else if (qh->period > fusbh200->periodic_size) {
2904 qh->period = fusbh200->periodic_size;
2905 urb->interval = qh->period << 3;
2907 } else {
2908 int think_time;
2910 /* gap is f(FS/LS transfer times) */
2911 qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,
2912 is_input, 0, maxp) / (125 * 1000);
2914 /* FIXME this just approximates SPLIT/CSPLIT times */
2915 if (is_input) { // SPLIT, gap, CSPLIT+DATA
2916 qh->c_usecs = qh->usecs + HS_USECS (0);
2917 qh->usecs = HS_USECS (1);
2918 } else { // SPLIT+DATA, gap, CSPLIT
2919 qh->usecs += HS_USECS (1);
2920 qh->c_usecs = HS_USECS (0);
2923 think_time = tt ? tt->think_time : 0;
2924 qh->tt_usecs = NS_TO_US (think_time +
2925 usb_calc_bus_time (urb->dev->speed,
2926 is_input, 0, max_packet (maxp)));
2927 qh->period = urb->interval;
2928 if (qh->period > fusbh200->periodic_size) {
2929 qh->period = fusbh200->periodic_size;
2930 urb->interval = qh->period;
2935 /* support for tt scheduling, and access to toggles */
2936 qh->dev = urb->dev;
2938 /* using TT? */
2939 switch (urb->dev->speed) {
2940 case USB_SPEED_LOW:
2941 info1 |= QH_LOW_SPEED;
2942 /* FALL THROUGH */
2944 case USB_SPEED_FULL:
2945 /* EPS 0 means "full" */
2946 if (type != PIPE_INTERRUPT)
2947 info1 |= (FUSBH200_TUNE_RL_TT << 28);
2948 if (type == PIPE_CONTROL) {
2949 info1 |= QH_CONTROL_EP; /* for TT */
2950 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2952 info1 |= maxp << 16;
2954 info2 |= (FUSBH200_TUNE_MULT_TT << 30);
2956 /* Some Freescale processors have an erratum in which the
2957 * port number in the queue head was 0..N-1 instead of 1..N.
2959 if (fusbh200_has_fsl_portno_bug(fusbh200))
2960 info2 |= (urb->dev->ttport-1) << 23;
2961 else
2962 info2 |= urb->dev->ttport << 23;
2964 /* set the address of the TT; for TDI's integrated
2965 * root hub tt, leave it zeroed.
2967 if (tt && tt->hub != fusbh200_to_hcd(fusbh200)->self.root_hub)
2968 info2 |= tt->hub->devnum << 16;
2970 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
2972 break;
2974 case USB_SPEED_HIGH: /* no TT involved */
2975 info1 |= QH_HIGH_SPEED;
2976 if (type == PIPE_CONTROL) {
2977 info1 |= (FUSBH200_TUNE_RL_HS << 28);
2978 info1 |= 64 << 16; /* usb2 fixed maxpacket */
2979 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2980 info2 |= (FUSBH200_TUNE_MULT_HS << 30);
2981 } else if (type == PIPE_BULK) {
2982 info1 |= (FUSBH200_TUNE_RL_HS << 28);
2983 /* The USB spec says that high speed bulk endpoints
2984 * always use 512 byte maxpacket. But some device
2985 * vendors decided to ignore that, and MSFT is happy
2986 * to help them do so. So now people expect to use
2987 * such nonconformant devices with Linux too; sigh.
2989 info1 |= max_packet(maxp) << 16;
2990 info2 |= (FUSBH200_TUNE_MULT_HS << 30);
2991 } else { /* PIPE_INTERRUPT */
2992 info1 |= max_packet (maxp) << 16;
2993 info2 |= hb_mult (maxp) << 30;
2995 break;
2996 default:
2997 fusbh200_dbg(fusbh200, "bogus dev %p speed %d\n", urb->dev,
2998 urb->dev->speed);
2999 done:
3000 qh_destroy(fusbh200, qh);
3001 return NULL;
3004 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
3006 /* init as live, toggle clear, advance to dummy */
3007 qh->qh_state = QH_STATE_IDLE;
3008 hw = qh->hw;
3009 hw->hw_info1 = cpu_to_hc32(fusbh200, info1);
3010 hw->hw_info2 = cpu_to_hc32(fusbh200, info2);
3011 qh->is_out = !is_input;
3012 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
3013 qh_refresh (fusbh200, qh);
3014 return qh;
3017 /*-------------------------------------------------------------------------*/
3019 static void enable_async(struct fusbh200_hcd *fusbh200)
3021 if (fusbh200->async_count++)
3022 return;
3024 /* Stop waiting to turn off the async schedule */
3025 fusbh200->enabled_hrtimer_events &= ~BIT(FUSBH200_HRTIMER_DISABLE_ASYNC);
3027 /* Don't start the schedule until ASS is 0 */
3028 fusbh200_poll_ASS(fusbh200);
3029 turn_on_io_watchdog(fusbh200);
3032 static void disable_async(struct fusbh200_hcd *fusbh200)
3034 if (--fusbh200->async_count)
3035 return;
3037 /* The async schedule and async_unlink list are supposed to be empty */
3038 WARN_ON(fusbh200->async->qh_next.qh || fusbh200->async_unlink);
3040 /* Don't turn off the schedule until ASS is 1 */
3041 fusbh200_poll_ASS(fusbh200);
3044 /* move qh (and its qtds) onto async queue; maybe enable queue. */
3046 static void qh_link_async (struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3048 __hc32 dma = QH_NEXT(fusbh200, qh->qh_dma);
3049 struct fusbh200_qh *head;
3051 /* Don't link a QH if there's a Clear-TT-Buffer pending */
3052 if (unlikely(qh->clearing_tt))
3053 return;
3055 WARN_ON(qh->qh_state != QH_STATE_IDLE);
3057 /* clear halt and/or toggle; and maybe recover from silicon quirk */
3058 qh_refresh(fusbh200, qh);
3060 /* splice right after start */
3061 head = fusbh200->async;
3062 qh->qh_next = head->qh_next;
3063 qh->hw->hw_next = head->hw->hw_next;
3064 wmb ();
3066 head->qh_next.qh = qh;
3067 head->hw->hw_next = dma;
3069 qh->xacterrs = 0;
3070 qh->qh_state = QH_STATE_LINKED;
3071 /* qtd completions reported later by interrupt */
3073 enable_async(fusbh200);
3076 /*-------------------------------------------------------------------------*/
3079 * For control/bulk/interrupt, return QH with these TDs appended.
3080 * Allocates and initializes the QH if necessary.
3081 * Returns null if it can't allocate a QH it needs to.
3082 * If the QH has TDs (urbs) already, that's great.
3084 static struct fusbh200_qh *qh_append_tds (
3085 struct fusbh200_hcd *fusbh200,
3086 struct urb *urb,
3087 struct list_head *qtd_list,
3088 int epnum,
3089 void **ptr
3092 struct fusbh200_qh *qh = NULL;
3093 __hc32 qh_addr_mask = cpu_to_hc32(fusbh200, 0x7f);
3095 qh = (struct fusbh200_qh *) *ptr;
3096 if (unlikely (qh == NULL)) {
3097 /* can't sleep here, we have fusbh200->lock... */
3098 qh = qh_make (fusbh200, urb, GFP_ATOMIC);
3099 *ptr = qh;
3101 if (likely (qh != NULL)) {
3102 struct fusbh200_qtd *qtd;
3104 if (unlikely (list_empty (qtd_list)))
3105 qtd = NULL;
3106 else
3107 qtd = list_entry (qtd_list->next, struct fusbh200_qtd,
3108 qtd_list);
3110 /* control qh may need patching ... */
3111 if (unlikely (epnum == 0)) {
3113 /* usb_reset_device() briefly reverts to address 0 */
3114 if (usb_pipedevice (urb->pipe) == 0)
3115 qh->hw->hw_info1 &= ~qh_addr_mask;
3118 /* just one way to queue requests: swap with the dummy qtd.
3119 * only hc or qh_refresh() ever modify the overlay.
3121 if (likely (qtd != NULL)) {
3122 struct fusbh200_qtd *dummy;
3123 dma_addr_t dma;
3124 __hc32 token;
3126 /* to avoid racing the HC, use the dummy td instead of
3127 * the first td of our list (becomes new dummy). both
3128 * tds stay deactivated until we're done, when the
3129 * HC is allowed to fetch the old dummy (4.10.2).
3131 token = qtd->hw_token;
3132 qtd->hw_token = HALT_BIT(fusbh200);
3134 dummy = qh->dummy;
3136 dma = dummy->qtd_dma;
3137 *dummy = *qtd;
3138 dummy->qtd_dma = dma;
3140 list_del (&qtd->qtd_list);
3141 list_add (&dummy->qtd_list, qtd_list);
3142 list_splice_tail(qtd_list, &qh->qtd_list);
3144 fusbh200_qtd_init(fusbh200, qtd, qtd->qtd_dma);
3145 qh->dummy = qtd;
3147 /* hc must see the new dummy at list end */
3148 dma = qtd->qtd_dma;
3149 qtd = list_entry (qh->qtd_list.prev,
3150 struct fusbh200_qtd, qtd_list);
3151 qtd->hw_next = QTD_NEXT(fusbh200, dma);
3153 /* let the hc process these next qtds */
3154 wmb ();
3155 dummy->hw_token = token;
3157 urb->hcpriv = qh;
3160 return qh;
3163 /*-------------------------------------------------------------------------*/
3165 static int
3166 submit_async (
3167 struct fusbh200_hcd *fusbh200,
3168 struct urb *urb,
3169 struct list_head *qtd_list,
3170 gfp_t mem_flags
3172 int epnum;
3173 unsigned long flags;
3174 struct fusbh200_qh *qh = NULL;
3175 int rc;
3177 epnum = urb->ep->desc.bEndpointAddress;
3179 #ifdef FUSBH200_URB_TRACE
3181 struct fusbh200_qtd *qtd;
3182 qtd = list_entry(qtd_list->next, struct fusbh200_qtd, qtd_list);
3183 fusbh200_dbg(fusbh200,
3184 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
3185 __func__, urb->dev->devpath, urb,
3186 epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
3187 urb->transfer_buffer_length,
3188 qtd, urb->ep->hcpriv);
3190 #endif
3192 spin_lock_irqsave (&fusbh200->lock, flags);
3193 if (unlikely(!HCD_HW_ACCESSIBLE(fusbh200_to_hcd(fusbh200)))) {
3194 rc = -ESHUTDOWN;
3195 goto done;
3197 rc = usb_hcd_link_urb_to_ep(fusbh200_to_hcd(fusbh200), urb);
3198 if (unlikely(rc))
3199 goto done;
3201 qh = qh_append_tds(fusbh200, urb, qtd_list, epnum, &urb->ep->hcpriv);
3202 if (unlikely(qh == NULL)) {
3203 usb_hcd_unlink_urb_from_ep(fusbh200_to_hcd(fusbh200), urb);
3204 rc = -ENOMEM;
3205 goto done;
3208 /* Control/bulk operations through TTs don't need scheduling,
3209 * the HC and TT handle it when the TT has a buffer ready.
3211 if (likely (qh->qh_state == QH_STATE_IDLE))
3212 qh_link_async(fusbh200, qh);
3213 done:
3214 spin_unlock_irqrestore (&fusbh200->lock, flags);
3215 if (unlikely (qh == NULL))
3216 qtd_list_free (fusbh200, urb, qtd_list);
3217 return rc;
3220 /*-------------------------------------------------------------------------*/
3222 static void single_unlink_async(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3224 struct fusbh200_qh *prev;
3226 /* Add to the end of the list of QHs waiting for the next IAAD */
3227 qh->qh_state = QH_STATE_UNLINK;
3228 if (fusbh200->async_unlink)
3229 fusbh200->async_unlink_last->unlink_next = qh;
3230 else
3231 fusbh200->async_unlink = qh;
3232 fusbh200->async_unlink_last = qh;
3234 /* Unlink it from the schedule */
3235 prev = fusbh200->async;
3236 while (prev->qh_next.qh != qh)
3237 prev = prev->qh_next.qh;
3239 prev->hw->hw_next = qh->hw->hw_next;
3240 prev->qh_next = qh->qh_next;
3241 if (fusbh200->qh_scan_next == qh)
3242 fusbh200->qh_scan_next = qh->qh_next.qh;
3245 static void start_iaa_cycle(struct fusbh200_hcd *fusbh200, bool nested)
3248 * Do nothing if an IAA cycle is already running or
3249 * if one will be started shortly.
3251 if (fusbh200->async_iaa || fusbh200->async_unlinking)
3252 return;
3254 /* Do all the waiting QHs at once */
3255 fusbh200->async_iaa = fusbh200->async_unlink;
3256 fusbh200->async_unlink = NULL;
3258 /* If the controller isn't running, we don't have to wait for it */
3259 if (unlikely(fusbh200->rh_state < FUSBH200_RH_RUNNING)) {
3260 if (!nested) /* Avoid recursion */
3261 end_unlink_async(fusbh200);
3263 /* Otherwise start a new IAA cycle */
3264 } else if (likely(fusbh200->rh_state == FUSBH200_RH_RUNNING)) {
3265 /* Make sure the unlinks are all visible to the hardware */
3266 wmb();
3268 fusbh200_writel(fusbh200, fusbh200->command | CMD_IAAD,
3269 &fusbh200->regs->command);
3270 fusbh200_readl(fusbh200, &fusbh200->regs->command);
3271 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_IAA_WATCHDOG, true);
3275 /* the async qh for the qtds being unlinked are now gone from the HC */
3277 static void end_unlink_async(struct fusbh200_hcd *fusbh200)
3279 struct fusbh200_qh *qh;
3281 /* Process the idle QHs */
3282 restart:
3283 fusbh200->async_unlinking = true;
3284 while (fusbh200->async_iaa) {
3285 qh = fusbh200->async_iaa;
3286 fusbh200->async_iaa = qh->unlink_next;
3287 qh->unlink_next = NULL;
3289 qh->qh_state = QH_STATE_IDLE;
3290 qh->qh_next.qh = NULL;
3292 qh_completions(fusbh200, qh);
3293 if (!list_empty(&qh->qtd_list) &&
3294 fusbh200->rh_state == FUSBH200_RH_RUNNING)
3295 qh_link_async(fusbh200, qh);
3296 disable_async(fusbh200);
3298 fusbh200->async_unlinking = false;
3300 /* Start a new IAA cycle if any QHs are waiting for it */
3301 if (fusbh200->async_unlink) {
3302 start_iaa_cycle(fusbh200, true);
3303 if (unlikely(fusbh200->rh_state < FUSBH200_RH_RUNNING))
3304 goto restart;
3308 static void unlink_empty_async(struct fusbh200_hcd *fusbh200)
3310 struct fusbh200_qh *qh, *next;
3311 bool stopped = (fusbh200->rh_state < FUSBH200_RH_RUNNING);
3312 bool check_unlinks_later = false;
3314 /* Unlink all the async QHs that have been empty for a timer cycle */
3315 next = fusbh200->async->qh_next.qh;
3316 while (next) {
3317 qh = next;
3318 next = qh->qh_next.qh;
3320 if (list_empty(&qh->qtd_list) &&
3321 qh->qh_state == QH_STATE_LINKED) {
3322 if (!stopped && qh->unlink_cycle ==
3323 fusbh200->async_unlink_cycle)
3324 check_unlinks_later = true;
3325 else
3326 single_unlink_async(fusbh200, qh);
3330 /* Start a new IAA cycle if any QHs are waiting for it */
3331 if (fusbh200->async_unlink)
3332 start_iaa_cycle(fusbh200, false);
3334 /* QHs that haven't been empty for long enough will be handled later */
3335 if (check_unlinks_later) {
3336 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_ASYNC_UNLINKS, true);
3337 ++fusbh200->async_unlink_cycle;
3341 /* makes sure the async qh will become idle */
3342 /* caller must own fusbh200->lock */
3344 static void start_unlink_async(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3347 * If the QH isn't linked then there's nothing we can do
3348 * unless we were called during a giveback, in which case
3349 * qh_completions() has to deal with it.
3351 if (qh->qh_state != QH_STATE_LINKED) {
3352 if (qh->qh_state == QH_STATE_COMPLETING)
3353 qh->needs_rescan = 1;
3354 return;
3357 single_unlink_async(fusbh200, qh);
3358 start_iaa_cycle(fusbh200, false);
3361 /*-------------------------------------------------------------------------*/
3363 static void scan_async (struct fusbh200_hcd *fusbh200)
3365 struct fusbh200_qh *qh;
3366 bool check_unlinks_later = false;
3368 fusbh200->qh_scan_next = fusbh200->async->qh_next.qh;
3369 while (fusbh200->qh_scan_next) {
3370 qh = fusbh200->qh_scan_next;
3371 fusbh200->qh_scan_next = qh->qh_next.qh;
3372 rescan:
3373 /* clean any finished work for this qh */
3374 if (!list_empty(&qh->qtd_list)) {
3375 int temp;
3378 * Unlinks could happen here; completion reporting
3379 * drops the lock. That's why fusbh200->qh_scan_next
3380 * always holds the next qh to scan; if the next qh
3381 * gets unlinked then fusbh200->qh_scan_next is adjusted
3382 * in single_unlink_async().
3384 temp = qh_completions(fusbh200, qh);
3385 if (qh->needs_rescan) {
3386 start_unlink_async(fusbh200, qh);
3387 } else if (list_empty(&qh->qtd_list)
3388 && qh->qh_state == QH_STATE_LINKED) {
3389 qh->unlink_cycle = fusbh200->async_unlink_cycle;
3390 check_unlinks_later = true;
3391 } else if (temp != 0)
3392 goto rescan;
3397 * Unlink empty entries, reducing DMA usage as well
3398 * as HCD schedule-scanning costs. Delay for any qh
3399 * we just scanned, there's a not-unusual case that it
3400 * doesn't stay idle for long.
3402 if (check_unlinks_later && fusbh200->rh_state == FUSBH200_RH_RUNNING &&
3403 !(fusbh200->enabled_hrtimer_events &
3404 BIT(FUSBH200_HRTIMER_ASYNC_UNLINKS))) {
3405 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_ASYNC_UNLINKS, true);
3406 ++fusbh200->async_unlink_cycle;
3409 /*-------------------------------------------------------------------------*/
3411 * EHCI scheduled transaction support: interrupt, iso, split iso
3412 * These are called "periodic" transactions in the EHCI spec.
3414 * Note that for interrupt transfers, the QH/QTD manipulation is shared
3415 * with the "asynchronous" transaction support (control/bulk transfers).
3416 * The only real difference is in how interrupt transfers are scheduled.
3418 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
3419 * It keeps track of every ITD (or SITD) that's linked, and holds enough
3420 * pre-calculated schedule data to make appending to the queue be quick.
3423 static int fusbh200_get_frame (struct usb_hcd *hcd);
3425 /*-------------------------------------------------------------------------*/
3428 * periodic_next_shadow - return "next" pointer on shadow list
3429 * @periodic: host pointer to qh/itd
3430 * @tag: hardware tag for type of this record
3432 static union fusbh200_shadow *
3433 periodic_next_shadow(struct fusbh200_hcd *fusbh200, union fusbh200_shadow *periodic,
3434 __hc32 tag)
3436 switch (hc32_to_cpu(fusbh200, tag)) {
3437 case Q_TYPE_QH:
3438 return &periodic->qh->qh_next;
3439 case Q_TYPE_FSTN:
3440 return &periodic->fstn->fstn_next;
3441 default:
3442 return &periodic->itd->itd_next;
3446 static __hc32 *
3447 shadow_next_periodic(struct fusbh200_hcd *fusbh200, union fusbh200_shadow *periodic,
3448 __hc32 tag)
3450 switch (hc32_to_cpu(fusbh200, tag)) {
3451 /* our fusbh200_shadow.qh is actually software part */
3452 case Q_TYPE_QH:
3453 return &periodic->qh->hw->hw_next;
3454 /* others are hw parts */
3455 default:
3456 return periodic->hw_next;
3460 /* caller must hold fusbh200->lock */
3461 static void periodic_unlink (struct fusbh200_hcd *fusbh200, unsigned frame, void *ptr)
3463 union fusbh200_shadow *prev_p = &fusbh200->pshadow[frame];
3464 __hc32 *hw_p = &fusbh200->periodic[frame];
3465 union fusbh200_shadow here = *prev_p;
3467 /* find predecessor of "ptr"; hw and shadow lists are in sync */
3468 while (here.ptr && here.ptr != ptr) {
3469 prev_p = periodic_next_shadow(fusbh200, prev_p,
3470 Q_NEXT_TYPE(fusbh200, *hw_p));
3471 hw_p = shadow_next_periodic(fusbh200, &here,
3472 Q_NEXT_TYPE(fusbh200, *hw_p));
3473 here = *prev_p;
3475 /* an interrupt entry (at list end) could have been shared */
3476 if (!here.ptr)
3477 return;
3479 /* update shadow and hardware lists ... the old "next" pointers
3480 * from ptr may still be in use, the caller updates them.
3482 *prev_p = *periodic_next_shadow(fusbh200, &here,
3483 Q_NEXT_TYPE(fusbh200, *hw_p));
3485 *hw_p = *shadow_next_periodic(fusbh200, &here,
3486 Q_NEXT_TYPE(fusbh200, *hw_p));
3489 /* how many of the uframe's 125 usecs are allocated? */
3490 static unsigned short
3491 periodic_usecs (struct fusbh200_hcd *fusbh200, unsigned frame, unsigned uframe)
3493 __hc32 *hw_p = &fusbh200->periodic [frame];
3494 union fusbh200_shadow *q = &fusbh200->pshadow [frame];
3495 unsigned usecs = 0;
3496 struct fusbh200_qh_hw *hw;
3498 while (q->ptr) {
3499 switch (hc32_to_cpu(fusbh200, Q_NEXT_TYPE(fusbh200, *hw_p))) {
3500 case Q_TYPE_QH:
3501 hw = q->qh->hw;
3502 /* is it in the S-mask? */
3503 if (hw->hw_info2 & cpu_to_hc32(fusbh200, 1 << uframe))
3504 usecs += q->qh->usecs;
3505 /* ... or C-mask? */
3506 if (hw->hw_info2 & cpu_to_hc32(fusbh200,
3507 1 << (8 + uframe)))
3508 usecs += q->qh->c_usecs;
3509 hw_p = &hw->hw_next;
3510 q = &q->qh->qh_next;
3511 break;
3512 // case Q_TYPE_FSTN:
3513 default:
3514 /* for "save place" FSTNs, count the relevant INTR
3515 * bandwidth from the previous frame
3517 if (q->fstn->hw_prev != FUSBH200_LIST_END(fusbh200)) {
3518 fusbh200_dbg (fusbh200, "ignoring FSTN cost ...\n");
3520 hw_p = &q->fstn->hw_next;
3521 q = &q->fstn->fstn_next;
3522 break;
3523 case Q_TYPE_ITD:
3524 if (q->itd->hw_transaction[uframe])
3525 usecs += q->itd->stream->usecs;
3526 hw_p = &q->itd->hw_next;
3527 q = &q->itd->itd_next;
3528 break;
3531 #ifdef DEBUG
3532 if (usecs > fusbh200->uframe_periodic_max)
3533 fusbh200_err (fusbh200, "uframe %d sched overrun: %d usecs\n",
3534 frame * 8 + uframe, usecs);
3535 #endif
3536 return usecs;
3539 /*-------------------------------------------------------------------------*/
3541 static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
3543 if (!dev1->tt || !dev2->tt)
3544 return 0;
3545 if (dev1->tt != dev2->tt)
3546 return 0;
3547 if (dev1->tt->multi)
3548 return dev1->ttport == dev2->ttport;
3549 else
3550 return 1;
3553 /* return true iff the device's transaction translator is available
3554 * for a periodic transfer starting at the specified frame, using
3555 * all the uframes in the mask.
3557 static int tt_no_collision (
3558 struct fusbh200_hcd *fusbh200,
3559 unsigned period,
3560 struct usb_device *dev,
3561 unsigned frame,
3562 u32 uf_mask
3565 if (period == 0) /* error */
3566 return 0;
3568 /* note bandwidth wastage: split never follows csplit
3569 * (different dev or endpoint) until the next uframe.
3570 * calling convention doesn't make that distinction.
3572 for (; frame < fusbh200->periodic_size; frame += period) {
3573 union fusbh200_shadow here;
3574 __hc32 type;
3575 struct fusbh200_qh_hw *hw;
3577 here = fusbh200->pshadow [frame];
3578 type = Q_NEXT_TYPE(fusbh200, fusbh200->periodic [frame]);
3579 while (here.ptr) {
3580 switch (hc32_to_cpu(fusbh200, type)) {
3581 case Q_TYPE_ITD:
3582 type = Q_NEXT_TYPE(fusbh200, here.itd->hw_next);
3583 here = here.itd->itd_next;
3584 continue;
3585 case Q_TYPE_QH:
3586 hw = here.qh->hw;
3587 if (same_tt (dev, here.qh->dev)) {
3588 u32 mask;
3590 mask = hc32_to_cpu(fusbh200,
3591 hw->hw_info2);
3592 /* "knows" no gap is needed */
3593 mask |= mask >> 8;
3594 if (mask & uf_mask)
3595 break;
3597 type = Q_NEXT_TYPE(fusbh200, hw->hw_next);
3598 here = here.qh->qh_next;
3599 continue;
3600 // case Q_TYPE_FSTN:
3601 default:
3602 fusbh200_dbg (fusbh200,
3603 "periodic frame %d bogus type %d\n",
3604 frame, type);
3607 /* collision or error */
3608 return 0;
3612 /* no collision */
3613 return 1;
3616 /*-------------------------------------------------------------------------*/
3618 static void enable_periodic(struct fusbh200_hcd *fusbh200)
3620 if (fusbh200->periodic_count++)
3621 return;
3623 /* Stop waiting to turn off the periodic schedule */
3624 fusbh200->enabled_hrtimer_events &= ~BIT(FUSBH200_HRTIMER_DISABLE_PERIODIC);
3626 /* Don't start the schedule until PSS is 0 */
3627 fusbh200_poll_PSS(fusbh200);
3628 turn_on_io_watchdog(fusbh200);
3631 static void disable_periodic(struct fusbh200_hcd *fusbh200)
3633 if (--fusbh200->periodic_count)
3634 return;
3636 /* Don't turn off the schedule until PSS is 1 */
3637 fusbh200_poll_PSS(fusbh200);
3640 /*-------------------------------------------------------------------------*/
3642 /* periodic schedule slots have iso tds (normal or split) first, then a
3643 * sparse tree for active interrupt transfers.
3645 * this just links in a qh; caller guarantees uframe masks are set right.
3646 * no FSTN support (yet; fusbh200 0.96+)
3648 static void qh_link_periodic(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3650 unsigned i;
3651 unsigned period = qh->period;
3653 dev_dbg (&qh->dev->dev,
3654 "link qh%d-%04x/%p start %d [%d/%d us]\n",
3655 period, hc32_to_cpup(fusbh200, &qh->hw->hw_info2)
3656 & (QH_CMASK | QH_SMASK),
3657 qh, qh->start, qh->usecs, qh->c_usecs);
3659 /* high bandwidth, or otherwise every microframe */
3660 if (period == 0)
3661 period = 1;
3663 for (i = qh->start; i < fusbh200->periodic_size; i += period) {
3664 union fusbh200_shadow *prev = &fusbh200->pshadow[i];
3665 __hc32 *hw_p = &fusbh200->periodic[i];
3666 union fusbh200_shadow here = *prev;
3667 __hc32 type = 0;
3669 /* skip the iso nodes at list head */
3670 while (here.ptr) {
3671 type = Q_NEXT_TYPE(fusbh200, *hw_p);
3672 if (type == cpu_to_hc32(fusbh200, Q_TYPE_QH))
3673 break;
3674 prev = periodic_next_shadow(fusbh200, prev, type);
3675 hw_p = shadow_next_periodic(fusbh200, &here, type);
3676 here = *prev;
3679 /* sorting each branch by period (slow-->fast)
3680 * enables sharing interior tree nodes
3682 while (here.ptr && qh != here.qh) {
3683 if (qh->period > here.qh->period)
3684 break;
3685 prev = &here.qh->qh_next;
3686 hw_p = &here.qh->hw->hw_next;
3687 here = *prev;
3689 /* link in this qh, unless some earlier pass did that */
3690 if (qh != here.qh) {
3691 qh->qh_next = here;
3692 if (here.qh)
3693 qh->hw->hw_next = *hw_p;
3694 wmb ();
3695 prev->qh = qh;
3696 *hw_p = QH_NEXT (fusbh200, qh->qh_dma);
3699 qh->qh_state = QH_STATE_LINKED;
3700 qh->xacterrs = 0;
3702 /* update per-qh bandwidth for usbfs */
3703 fusbh200_to_hcd(fusbh200)->self.bandwidth_allocated += qh->period
3704 ? ((qh->usecs + qh->c_usecs) / qh->period)
3705 : (qh->usecs * 8);
3707 list_add(&qh->intr_node, &fusbh200->intr_qh_list);
3709 /* maybe enable periodic schedule processing */
3710 ++fusbh200->intr_count;
3711 enable_periodic(fusbh200);
3714 static void qh_unlink_periodic(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3716 unsigned i;
3717 unsigned period;
3720 * If qh is for a low/full-speed device, simply unlinking it
3721 * could interfere with an ongoing split transaction. To unlink
3722 * it safely would require setting the QH_INACTIVATE bit and
3723 * waiting at least one frame, as described in EHCI 4.12.2.5.
3725 * We won't bother with any of this. Instead, we assume that the
3726 * only reason for unlinking an interrupt QH while the current URB
3727 * is still active is to dequeue all the URBs (flush the whole
3728 * endpoint queue).
3730 * If rebalancing the periodic schedule is ever implemented, this
3731 * approach will no longer be valid.
3734 /* high bandwidth, or otherwise part of every microframe */
3735 if ((period = qh->period) == 0)
3736 period = 1;
3738 for (i = qh->start; i < fusbh200->periodic_size; i += period)
3739 periodic_unlink (fusbh200, i, qh);
3741 /* update per-qh bandwidth for usbfs */
3742 fusbh200_to_hcd(fusbh200)->self.bandwidth_allocated -= qh->period
3743 ? ((qh->usecs + qh->c_usecs) / qh->period)
3744 : (qh->usecs * 8);
3746 dev_dbg (&qh->dev->dev,
3747 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
3748 qh->period,
3749 hc32_to_cpup(fusbh200, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
3750 qh, qh->start, qh->usecs, qh->c_usecs);
3752 /* qh->qh_next still "live" to HC */
3753 qh->qh_state = QH_STATE_UNLINK;
3754 qh->qh_next.ptr = NULL;
3756 if (fusbh200->qh_scan_next == qh)
3757 fusbh200->qh_scan_next = list_entry(qh->intr_node.next,
3758 struct fusbh200_qh, intr_node);
3759 list_del(&qh->intr_node);
3762 static void start_unlink_intr(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3764 /* If the QH isn't linked then there's nothing we can do
3765 * unless we were called during a giveback, in which case
3766 * qh_completions() has to deal with it.
3768 if (qh->qh_state != QH_STATE_LINKED) {
3769 if (qh->qh_state == QH_STATE_COMPLETING)
3770 qh->needs_rescan = 1;
3771 return;
3774 qh_unlink_periodic (fusbh200, qh);
3776 /* Make sure the unlinks are visible before starting the timer */
3777 wmb();
3780 * The EHCI spec doesn't say how long it takes the controller to
3781 * stop accessing an unlinked interrupt QH. The timer delay is
3782 * 9 uframes; presumably that will be long enough.
3784 qh->unlink_cycle = fusbh200->intr_unlink_cycle;
3786 /* New entries go at the end of the intr_unlink list */
3787 if (fusbh200->intr_unlink)
3788 fusbh200->intr_unlink_last->unlink_next = qh;
3789 else
3790 fusbh200->intr_unlink = qh;
3791 fusbh200->intr_unlink_last = qh;
3793 if (fusbh200->intr_unlinking)
3794 ; /* Avoid recursive calls */
3795 else if (fusbh200->rh_state < FUSBH200_RH_RUNNING)
3796 fusbh200_handle_intr_unlinks(fusbh200);
3797 else if (fusbh200->intr_unlink == qh) {
3798 fusbh200_enable_event(fusbh200, FUSBH200_HRTIMER_UNLINK_INTR, true);
3799 ++fusbh200->intr_unlink_cycle;
3803 static void end_unlink_intr(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3805 struct fusbh200_qh_hw *hw = qh->hw;
3806 int rc;
3808 qh->qh_state = QH_STATE_IDLE;
3809 hw->hw_next = FUSBH200_LIST_END(fusbh200);
3811 qh_completions(fusbh200, qh);
3813 /* reschedule QH iff another request is queued */
3814 if (!list_empty(&qh->qtd_list) && fusbh200->rh_state == FUSBH200_RH_RUNNING) {
3815 rc = qh_schedule(fusbh200, qh);
3817 /* An error here likely indicates handshake failure
3818 * or no space left in the schedule. Neither fault
3819 * should happen often ...
3821 * FIXME kill the now-dysfunctional queued urbs
3823 if (rc != 0)
3824 fusbh200_err(fusbh200, "can't reschedule qh %p, err %d\n",
3825 qh, rc);
3828 /* maybe turn off periodic schedule */
3829 --fusbh200->intr_count;
3830 disable_periodic(fusbh200);
3833 /*-------------------------------------------------------------------------*/
3835 static int check_period (
3836 struct fusbh200_hcd *fusbh200,
3837 unsigned frame,
3838 unsigned uframe,
3839 unsigned period,
3840 unsigned usecs
3842 int claimed;
3844 /* complete split running into next frame?
3845 * given FSTN support, we could sometimes check...
3847 if (uframe >= 8)
3848 return 0;
3850 /* convert "usecs we need" to "max already claimed" */
3851 usecs = fusbh200->uframe_periodic_max - usecs;
3853 /* we "know" 2 and 4 uframe intervals were rejected; so
3854 * for period 0, check _every_ microframe in the schedule.
3856 if (unlikely (period == 0)) {
3857 do {
3858 for (uframe = 0; uframe < 7; uframe++) {
3859 claimed = periodic_usecs (fusbh200, frame, uframe);
3860 if (claimed > usecs)
3861 return 0;
3863 } while ((frame += 1) < fusbh200->periodic_size);
3865 /* just check the specified uframe, at that period */
3866 } else {
3867 do {
3868 claimed = periodic_usecs (fusbh200, frame, uframe);
3869 if (claimed > usecs)
3870 return 0;
3871 } while ((frame += period) < fusbh200->periodic_size);
3874 // success!
3875 return 1;
3878 static int check_intr_schedule (
3879 struct fusbh200_hcd *fusbh200,
3880 unsigned frame,
3881 unsigned uframe,
3882 const struct fusbh200_qh *qh,
3883 __hc32 *c_maskp
3886 int retval = -ENOSPC;
3887 u8 mask = 0;
3889 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
3890 goto done;
3892 if (!check_period (fusbh200, frame, uframe, qh->period, qh->usecs))
3893 goto done;
3894 if (!qh->c_usecs) {
3895 retval = 0;
3896 *c_maskp = 0;
3897 goto done;
3900 /* Make sure this tt's buffer is also available for CSPLITs.
3901 * We pessimize a bit; probably the typical full speed case
3902 * doesn't need the second CSPLIT.
3904 * NOTE: both SPLIT and CSPLIT could be checked in just
3905 * one smart pass...
3907 mask = 0x03 << (uframe + qh->gap_uf);
3908 *c_maskp = cpu_to_hc32(fusbh200, mask << 8);
3910 mask |= 1 << uframe;
3911 if (tt_no_collision (fusbh200, qh->period, qh->dev, frame, mask)) {
3912 if (!check_period (fusbh200, frame, uframe + qh->gap_uf + 1,
3913 qh->period, qh->c_usecs))
3914 goto done;
3915 if (!check_period (fusbh200, frame, uframe + qh->gap_uf,
3916 qh->period, qh->c_usecs))
3917 goto done;
3918 retval = 0;
3920 done:
3921 return retval;
3924 /* "first fit" scheduling policy used the first time through,
3925 * or when the previous schedule slot can't be re-used.
3927 static int qh_schedule(struct fusbh200_hcd *fusbh200, struct fusbh200_qh *qh)
3929 int status;
3930 unsigned uframe;
3931 __hc32 c_mask;
3932 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
3933 struct fusbh200_qh_hw *hw = qh->hw;
3935 qh_refresh(fusbh200, qh);
3936 hw->hw_next = FUSBH200_LIST_END(fusbh200);
3937 frame = qh->start;
3939 /* reuse the previous schedule slots, if we can */
3940 if (frame < qh->period) {
3941 uframe = ffs(hc32_to_cpup(fusbh200, &hw->hw_info2) & QH_SMASK);
3942 status = check_intr_schedule (fusbh200, frame, --uframe,
3943 qh, &c_mask);
3944 } else {
3945 uframe = 0;
3946 c_mask = 0;
3947 status = -ENOSPC;
3950 /* else scan the schedule to find a group of slots such that all
3951 * uframes have enough periodic bandwidth available.
3953 if (status) {
3954 /* "normal" case, uframing flexible except with splits */
3955 if (qh->period) {
3956 int i;
3958 for (i = qh->period; status && i > 0; --i) {
3959 frame = ++fusbh200->random_frame % qh->period;
3960 for (uframe = 0; uframe < 8; uframe++) {
3961 status = check_intr_schedule (fusbh200,
3962 frame, uframe, qh,
3963 &c_mask);
3964 if (status == 0)
3965 break;
3969 /* qh->period == 0 means every uframe */
3970 } else {
3971 frame = 0;
3972 status = check_intr_schedule (fusbh200, 0, 0, qh, &c_mask);
3974 if (status)
3975 goto done;
3976 qh->start = frame;
3978 /* reset S-frame and (maybe) C-frame masks */
3979 hw->hw_info2 &= cpu_to_hc32(fusbh200, ~(QH_CMASK | QH_SMASK));
3980 hw->hw_info2 |= qh->period
3981 ? cpu_to_hc32(fusbh200, 1 << uframe)
3982 : cpu_to_hc32(fusbh200, QH_SMASK);
3983 hw->hw_info2 |= c_mask;
3984 } else
3985 fusbh200_dbg (fusbh200, "reused qh %p schedule\n", qh);
3987 /* stuff into the periodic schedule */
3988 qh_link_periodic(fusbh200, qh);
3989 done:
3990 return status;
3993 static int intr_submit (
3994 struct fusbh200_hcd *fusbh200,
3995 struct urb *urb,
3996 struct list_head *qtd_list,
3997 gfp_t mem_flags
3999 unsigned epnum;
4000 unsigned long flags;
4001 struct fusbh200_qh *qh;
4002 int status;
4003 struct list_head empty;
4005 /* get endpoint and transfer/schedule data */
4006 epnum = urb->ep->desc.bEndpointAddress;
4008 spin_lock_irqsave (&fusbh200->lock, flags);
4010 if (unlikely(!HCD_HW_ACCESSIBLE(fusbh200_to_hcd(fusbh200)))) {
4011 status = -ESHUTDOWN;
4012 goto done_not_linked;
4014 status = usb_hcd_link_urb_to_ep(fusbh200_to_hcd(fusbh200), urb);
4015 if (unlikely(status))
4016 goto done_not_linked;
4018 /* get qh and force any scheduling errors */
4019 INIT_LIST_HEAD (&empty);
4020 qh = qh_append_tds(fusbh200, urb, &empty, epnum, &urb->ep->hcpriv);
4021 if (qh == NULL) {
4022 status = -ENOMEM;
4023 goto done;
4025 if (qh->qh_state == QH_STATE_IDLE) {
4026 if ((status = qh_schedule (fusbh200, qh)) != 0)
4027 goto done;
4030 /* then queue the urb's tds to the qh */
4031 qh = qh_append_tds(fusbh200, urb, qtd_list, epnum, &urb->ep->hcpriv);
4032 BUG_ON (qh == NULL);
4034 /* ... update usbfs periodic stats */
4035 fusbh200_to_hcd(fusbh200)->self.bandwidth_int_reqs++;
4037 done:
4038 if (unlikely(status))
4039 usb_hcd_unlink_urb_from_ep(fusbh200_to_hcd(fusbh200), urb);
4040 done_not_linked:
4041 spin_unlock_irqrestore (&fusbh200->lock, flags);
4042 if (status)
4043 qtd_list_free (fusbh200, urb, qtd_list);
4045 return status;
4048 static void scan_intr(struct fusbh200_hcd *fusbh200)
4050 struct fusbh200_qh *qh;
4052 list_for_each_entry_safe(qh, fusbh200->qh_scan_next, &fusbh200->intr_qh_list,
4053 intr_node) {
4054 rescan:
4055 /* clean any finished work for this qh */
4056 if (!list_empty(&qh->qtd_list)) {
4057 int temp;
4060 * Unlinks could happen here; completion reporting
4061 * drops the lock. That's why fusbh200->qh_scan_next
4062 * always holds the next qh to scan; if the next qh
4063 * gets unlinked then fusbh200->qh_scan_next is adjusted
4064 * in qh_unlink_periodic().
4066 temp = qh_completions(fusbh200, qh);
4067 if (unlikely(qh->needs_rescan ||
4068 (list_empty(&qh->qtd_list) &&
4069 qh->qh_state == QH_STATE_LINKED)))
4070 start_unlink_intr(fusbh200, qh);
4071 else if (temp != 0)
4072 goto rescan;
4077 /*-------------------------------------------------------------------------*/
4079 /* fusbh200_iso_stream ops work with both ITD and SITD */
4081 static struct fusbh200_iso_stream *
4082 iso_stream_alloc (gfp_t mem_flags)
4084 struct fusbh200_iso_stream *stream;
4086 stream = kzalloc(sizeof *stream, mem_flags);
4087 if (likely (stream != NULL)) {
4088 INIT_LIST_HEAD(&stream->td_list);
4089 INIT_LIST_HEAD(&stream->free_list);
4090 stream->next_uframe = -1;
4092 return stream;
4095 static void
4096 iso_stream_init (
4097 struct fusbh200_hcd *fusbh200,
4098 struct fusbh200_iso_stream *stream,
4099 struct usb_device *dev,
4100 int pipe,
4101 unsigned interval
4104 u32 buf1;
4105 unsigned epnum, maxp;
4106 int is_input;
4107 long bandwidth;
4108 unsigned multi;
4111 * this might be a "high bandwidth" highspeed endpoint,
4112 * as encoded in the ep descriptor's wMaxPacket field
4114 epnum = usb_pipeendpoint (pipe);
4115 is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
4116 maxp = usb_maxpacket(dev, pipe, !is_input);
4117 if (is_input) {
4118 buf1 = (1 << 11);
4119 } else {
4120 buf1 = 0;
4123 maxp = max_packet(maxp);
4124 multi = hb_mult(maxp);
4125 buf1 |= maxp;
4126 maxp *= multi;
4128 stream->buf0 = cpu_to_hc32(fusbh200, (epnum << 8) | dev->devnum);
4129 stream->buf1 = cpu_to_hc32(fusbh200, buf1);
4130 stream->buf2 = cpu_to_hc32(fusbh200, multi);
4132 /* usbfs wants to report the average usecs per frame tied up
4133 * when transfers on this endpoint are scheduled ...
4135 if (dev->speed == USB_SPEED_FULL) {
4136 interval <<= 3;
4137 stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
4138 is_input, 1, maxp));
4139 stream->usecs /= 8;
4140 } else {
4141 stream->highspeed = 1;
4142 stream->usecs = HS_USECS_ISO (maxp);
4144 bandwidth = stream->usecs * 8;
4145 bandwidth /= interval;
4147 stream->bandwidth = bandwidth;
4148 stream->udev = dev;
4149 stream->bEndpointAddress = is_input | epnum;
4150 stream->interval = interval;
4151 stream->maxp = maxp;
4154 static struct fusbh200_iso_stream *
4155 iso_stream_find (struct fusbh200_hcd *fusbh200, struct urb *urb)
4157 unsigned epnum;
4158 struct fusbh200_iso_stream *stream;
4159 struct usb_host_endpoint *ep;
4160 unsigned long flags;
4162 epnum = usb_pipeendpoint (urb->pipe);
4163 if (usb_pipein(urb->pipe))
4164 ep = urb->dev->ep_in[epnum];
4165 else
4166 ep = urb->dev->ep_out[epnum];
4168 spin_lock_irqsave (&fusbh200->lock, flags);
4169 stream = ep->hcpriv;
4171 if (unlikely (stream == NULL)) {
4172 stream = iso_stream_alloc(GFP_ATOMIC);
4173 if (likely (stream != NULL)) {
4174 ep->hcpriv = stream;
4175 stream->ep = ep;
4176 iso_stream_init(fusbh200, stream, urb->dev, urb->pipe,
4177 urb->interval);
4180 /* if dev->ep [epnum] is a QH, hw is set */
4181 } else if (unlikely (stream->hw != NULL)) {
4182 fusbh200_dbg (fusbh200, "dev %s ep%d%s, not iso??\n",
4183 urb->dev->devpath, epnum,
4184 usb_pipein(urb->pipe) ? "in" : "out");
4185 stream = NULL;
4188 spin_unlock_irqrestore (&fusbh200->lock, flags);
4189 return stream;
4192 /*-------------------------------------------------------------------------*/
4194 /* fusbh200_iso_sched ops can be ITD-only or SITD-only */
4196 static struct fusbh200_iso_sched *
4197 iso_sched_alloc (unsigned packets, gfp_t mem_flags)
4199 struct fusbh200_iso_sched *iso_sched;
4200 int size = sizeof *iso_sched;
4202 size += packets * sizeof (struct fusbh200_iso_packet);
4203 iso_sched = kzalloc(size, mem_flags);
4204 if (likely (iso_sched != NULL)) {
4205 INIT_LIST_HEAD (&iso_sched->td_list);
4207 return iso_sched;
4210 static inline void
4211 itd_sched_init(
4212 struct fusbh200_hcd *fusbh200,
4213 struct fusbh200_iso_sched *iso_sched,
4214 struct fusbh200_iso_stream *stream,
4215 struct urb *urb
4218 unsigned i;
4219 dma_addr_t dma = urb->transfer_dma;
4221 /* how many uframes are needed for these transfers */
4222 iso_sched->span = urb->number_of_packets * stream->interval;
4224 /* figure out per-uframe itd fields that we'll need later
4225 * when we fit new itds into the schedule.
4227 for (i = 0; i < urb->number_of_packets; i++) {
4228 struct fusbh200_iso_packet *uframe = &iso_sched->packet [i];
4229 unsigned length;
4230 dma_addr_t buf;
4231 u32 trans;
4233 length = urb->iso_frame_desc [i].length;
4234 buf = dma + urb->iso_frame_desc [i].offset;
4236 trans = FUSBH200_ISOC_ACTIVE;
4237 trans |= buf & 0x0fff;
4238 if (unlikely (((i + 1) == urb->number_of_packets))
4239 && !(urb->transfer_flags & URB_NO_INTERRUPT))
4240 trans |= FUSBH200_ITD_IOC;
4241 trans |= length << 16;
4242 uframe->transaction = cpu_to_hc32(fusbh200, trans);
4244 /* might need to cross a buffer page within a uframe */
4245 uframe->bufp = (buf & ~(u64)0x0fff);
4246 buf += length;
4247 if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
4248 uframe->cross = 1;
4252 static void
4253 iso_sched_free (
4254 struct fusbh200_iso_stream *stream,
4255 struct fusbh200_iso_sched *iso_sched
4258 if (!iso_sched)
4259 return;
4260 // caller must hold fusbh200->lock!
4261 list_splice (&iso_sched->td_list, &stream->free_list);
4262 kfree (iso_sched);
4265 static int
4266 itd_urb_transaction (
4267 struct fusbh200_iso_stream *stream,
4268 struct fusbh200_hcd *fusbh200,
4269 struct urb *urb,
4270 gfp_t mem_flags
4273 struct fusbh200_itd *itd;
4274 dma_addr_t itd_dma;
4275 int i;
4276 unsigned num_itds;
4277 struct fusbh200_iso_sched *sched;
4278 unsigned long flags;
4280 sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
4281 if (unlikely (sched == NULL))
4282 return -ENOMEM;
4284 itd_sched_init(fusbh200, sched, stream, urb);
4286 if (urb->interval < 8)
4287 num_itds = 1 + (sched->span + 7) / 8;
4288 else
4289 num_itds = urb->number_of_packets;
4291 /* allocate/init ITDs */
4292 spin_lock_irqsave (&fusbh200->lock, flags);
4293 for (i = 0; i < num_itds; i++) {
4296 * Use iTDs from the free list, but not iTDs that may
4297 * still be in use by the hardware.
4299 if (likely(!list_empty(&stream->free_list))) {
4300 itd = list_first_entry(&stream->free_list,
4301 struct fusbh200_itd, itd_list);
4302 if (itd->frame == fusbh200->now_frame)
4303 goto alloc_itd;
4304 list_del (&itd->itd_list);
4305 itd_dma = itd->itd_dma;
4306 } else {
4307 alloc_itd:
4308 spin_unlock_irqrestore (&fusbh200->lock, flags);
4309 itd = dma_pool_alloc (fusbh200->itd_pool, mem_flags,
4310 &itd_dma);
4311 spin_lock_irqsave (&fusbh200->lock, flags);
4312 if (!itd) {
4313 iso_sched_free(stream, sched);
4314 spin_unlock_irqrestore(&fusbh200->lock, flags);
4315 return -ENOMEM;
4319 memset (itd, 0, sizeof *itd);
4320 itd->itd_dma = itd_dma;
4321 list_add (&itd->itd_list, &sched->td_list);
4323 spin_unlock_irqrestore (&fusbh200->lock, flags);
4325 /* temporarily store schedule info in hcpriv */
4326 urb->hcpriv = sched;
4327 urb->error_count = 0;
4328 return 0;
4331 /*-------------------------------------------------------------------------*/
4333 static inline int
4334 itd_slot_ok (
4335 struct fusbh200_hcd *fusbh200,
4336 u32 mod,
4337 u32 uframe,
4338 u8 usecs,
4339 u32 period
4342 uframe %= period;
4343 do {
4344 /* can't commit more than uframe_periodic_max usec */
4345 if (periodic_usecs (fusbh200, uframe >> 3, uframe & 0x7)
4346 > (fusbh200->uframe_periodic_max - usecs))
4347 return 0;
4349 /* we know urb->interval is 2^N uframes */
4350 uframe += period;
4351 } while (uframe < mod);
4352 return 1;
4356 * This scheduler plans almost as far into the future as it has actual
4357 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
4358 * "as small as possible" to be cache-friendlier.) That limits the size
4359 * transfers you can stream reliably; avoid more than 64 msec per urb.
4360 * Also avoid queue depths of less than fusbh200's worst irq latency (affected
4361 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
4362 * and other factors); or more than about 230 msec total (for portability,
4363 * given FUSBH200_TUNE_FLS and the slop). Or, write a smarter scheduler!
4366 #define SCHEDULE_SLOP 80 /* microframes */
4368 static int
4369 iso_stream_schedule (
4370 struct fusbh200_hcd *fusbh200,
4371 struct urb *urb,
4372 struct fusbh200_iso_stream *stream
4375 u32 now, next, start, period, span;
4376 int status;
4377 unsigned mod = fusbh200->periodic_size << 3;
4378 struct fusbh200_iso_sched *sched = urb->hcpriv;
4380 period = urb->interval;
4381 span = sched->span;
4383 if (span > mod - SCHEDULE_SLOP) {
4384 fusbh200_dbg (fusbh200, "iso request %p too long\n", urb);
4385 status = -EFBIG;
4386 goto fail;
4389 now = fusbh200_read_frame_index(fusbh200) & (mod - 1);
4391 /* Typical case: reuse current schedule, stream is still active.
4392 * Hopefully there are no gaps from the host falling behind
4393 * (irq delays etc), but if there are we'll take the next
4394 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
4396 if (likely (!list_empty (&stream->td_list))) {
4397 u32 excess;
4399 /* For high speed devices, allow scheduling within the
4400 * isochronous scheduling threshold. For full speed devices
4401 * and Intel PCI-based controllers, don't (work around for
4402 * Intel ICH9 bug).
4404 if (!stream->highspeed && fusbh200->fs_i_thresh)
4405 next = now + fusbh200->i_thresh;
4406 else
4407 next = now;
4409 /* Fell behind (by up to twice the slop amount)?
4410 * We decide based on the time of the last currently-scheduled
4411 * slot, not the time of the next available slot.
4413 excess = (stream->next_uframe - period - next) & (mod - 1);
4414 if (excess >= mod - 2 * SCHEDULE_SLOP)
4415 start = next + excess - mod + period *
4416 DIV_ROUND_UP(mod - excess, period);
4417 else
4418 start = next + excess + period;
4419 if (start - now >= mod) {
4420 fusbh200_dbg(fusbh200, "request %p would overflow (%d+%d >= %d)\n",
4421 urb, start - now - period, period,
4422 mod);
4423 status = -EFBIG;
4424 goto fail;
4428 /* need to schedule; when's the next (u)frame we could start?
4429 * this is bigger than fusbh200->i_thresh allows; scheduling itself
4430 * isn't free, the slop should handle reasonably slow cpus. it
4431 * can also help high bandwidth if the dma and irq loads don't
4432 * jump until after the queue is primed.
4434 else {
4435 int done = 0;
4436 start = SCHEDULE_SLOP + (now & ~0x07);
4438 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
4440 /* find a uframe slot with enough bandwidth.
4441 * Early uframes are more precious because full-speed
4442 * iso IN transfers can't use late uframes,
4443 * and therefore they should be allocated last.
4445 next = start;
4446 start += period;
4447 do {
4448 start--;
4449 /* check schedule: enough space? */
4450 if (itd_slot_ok(fusbh200, mod, start,
4451 stream->usecs, period))
4452 done = 1;
4453 } while (start > next && !done);
4455 /* no room in the schedule */
4456 if (!done) {
4457 fusbh200_dbg(fusbh200, "iso resched full %p (now %d max %d)\n",
4458 urb, now, now + mod);
4459 status = -ENOSPC;
4460 goto fail;
4464 /* Tried to schedule too far into the future? */
4465 if (unlikely(start - now + span - period
4466 >= mod - 2 * SCHEDULE_SLOP)) {
4467 fusbh200_dbg(fusbh200, "request %p would overflow (%d+%d >= %d)\n",
4468 urb, start - now, span - period,
4469 mod - 2 * SCHEDULE_SLOP);
4470 status = -EFBIG;
4471 goto fail;
4474 stream->next_uframe = start & (mod - 1);
4476 /* report high speed start in uframes; full speed, in frames */
4477 urb->start_frame = stream->next_uframe;
4478 if (!stream->highspeed)
4479 urb->start_frame >>= 3;
4481 /* Make sure scan_isoc() sees these */
4482 if (fusbh200->isoc_count == 0)
4483 fusbh200->next_frame = now >> 3;
4484 return 0;
4486 fail:
4487 iso_sched_free(stream, sched);
4488 urb->hcpriv = NULL;
4489 return status;
4492 /*-------------------------------------------------------------------------*/
4494 static inline void
4495 itd_init(struct fusbh200_hcd *fusbh200, struct fusbh200_iso_stream *stream,
4496 struct fusbh200_itd *itd)
4498 int i;
4500 /* it's been recently zeroed */
4501 itd->hw_next = FUSBH200_LIST_END(fusbh200);
4502 itd->hw_bufp [0] = stream->buf0;
4503 itd->hw_bufp [1] = stream->buf1;
4504 itd->hw_bufp [2] = stream->buf2;
4506 for (i = 0; i < 8; i++)
4507 itd->index[i] = -1;
4509 /* All other fields are filled when scheduling */
4512 static inline void
4513 itd_patch(
4514 struct fusbh200_hcd *fusbh200,
4515 struct fusbh200_itd *itd,
4516 struct fusbh200_iso_sched *iso_sched,
4517 unsigned index,
4518 u16 uframe
4521 struct fusbh200_iso_packet *uf = &iso_sched->packet [index];
4522 unsigned pg = itd->pg;
4524 // BUG_ON (pg == 6 && uf->cross);
4526 uframe &= 0x07;
4527 itd->index [uframe] = index;
4529 itd->hw_transaction[uframe] = uf->transaction;
4530 itd->hw_transaction[uframe] |= cpu_to_hc32(fusbh200, pg << 12);
4531 itd->hw_bufp[pg] |= cpu_to_hc32(fusbh200, uf->bufp & ~(u32)0);
4532 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fusbh200, (u32)(uf->bufp >> 32));
4534 /* iso_frame_desc[].offset must be strictly increasing */
4535 if (unlikely (uf->cross)) {
4536 u64 bufp = uf->bufp + 4096;
4538 itd->pg = ++pg;
4539 itd->hw_bufp[pg] |= cpu_to_hc32(fusbh200, bufp & ~(u32)0);
4540 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fusbh200, (u32)(bufp >> 32));
4544 static inline void
4545 itd_link (struct fusbh200_hcd *fusbh200, unsigned frame, struct fusbh200_itd *itd)
4547 union fusbh200_shadow *prev = &fusbh200->pshadow[frame];
4548 __hc32 *hw_p = &fusbh200->periodic[frame];
4549 union fusbh200_shadow here = *prev;
4550 __hc32 type = 0;
4552 /* skip any iso nodes which might belong to previous microframes */
4553 while (here.ptr) {
4554 type = Q_NEXT_TYPE(fusbh200, *hw_p);
4555 if (type == cpu_to_hc32(fusbh200, Q_TYPE_QH))
4556 break;
4557 prev = periodic_next_shadow(fusbh200, prev, type);
4558 hw_p = shadow_next_periodic(fusbh200, &here, type);
4559 here = *prev;
4562 itd->itd_next = here;
4563 itd->hw_next = *hw_p;
4564 prev->itd = itd;
4565 itd->frame = frame;
4566 wmb ();
4567 *hw_p = cpu_to_hc32(fusbh200, itd->itd_dma | Q_TYPE_ITD);
4570 /* fit urb's itds into the selected schedule slot; activate as needed */
4571 static void itd_link_urb(
4572 struct fusbh200_hcd *fusbh200,
4573 struct urb *urb,
4574 unsigned mod,
4575 struct fusbh200_iso_stream *stream
4578 int packet;
4579 unsigned next_uframe, uframe, frame;
4580 struct fusbh200_iso_sched *iso_sched = urb->hcpriv;
4581 struct fusbh200_itd *itd;
4583 next_uframe = stream->next_uframe & (mod - 1);
4585 if (unlikely (list_empty(&stream->td_list))) {
4586 fusbh200_to_hcd(fusbh200)->self.bandwidth_allocated
4587 += stream->bandwidth;
4588 fusbh200_vdbg (fusbh200,
4589 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
4590 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
4591 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
4592 urb->interval,
4593 next_uframe >> 3, next_uframe & 0x7);
4596 /* fill iTDs uframe by uframe */
4597 for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
4598 if (itd == NULL) {
4599 /* ASSERT: we have all necessary itds */
4600 // BUG_ON (list_empty (&iso_sched->td_list));
4602 /* ASSERT: no itds for this endpoint in this uframe */
4604 itd = list_entry (iso_sched->td_list.next,
4605 struct fusbh200_itd, itd_list);
4606 list_move_tail (&itd->itd_list, &stream->td_list);
4607 itd->stream = stream;
4608 itd->urb = urb;
4609 itd_init (fusbh200, stream, itd);
4612 uframe = next_uframe & 0x07;
4613 frame = next_uframe >> 3;
4615 itd_patch(fusbh200, itd, iso_sched, packet, uframe);
4617 next_uframe += stream->interval;
4618 next_uframe &= mod - 1;
4619 packet++;
4621 /* link completed itds into the schedule */
4622 if (((next_uframe >> 3) != frame)
4623 || packet == urb->number_of_packets) {
4624 itd_link(fusbh200, frame & (fusbh200->periodic_size - 1), itd);
4625 itd = NULL;
4628 stream->next_uframe = next_uframe;
4630 /* don't need that schedule data any more */
4631 iso_sched_free (stream, iso_sched);
4632 urb->hcpriv = NULL;
4634 ++fusbh200->isoc_count;
4635 enable_periodic(fusbh200);
4638 #define ISO_ERRS (FUSBH200_ISOC_BUF_ERR | FUSBH200_ISOC_BABBLE | FUSBH200_ISOC_XACTERR)
4640 /* Process and recycle a completed ITD. Return true iff its urb completed,
4641 * and hence its completion callback probably added things to the hardware
4642 * schedule.
4644 * Note that we carefully avoid recycling this descriptor until after any
4645 * completion callback runs, so that it won't be reused quickly. That is,
4646 * assuming (a) no more than two urbs per frame on this endpoint, and also
4647 * (b) only this endpoint's completions submit URBs. It seems some silicon
4648 * corrupts things if you reuse completed descriptors very quickly...
4650 static bool itd_complete(struct fusbh200_hcd *fusbh200, struct fusbh200_itd *itd)
4652 struct urb *urb = itd->urb;
4653 struct usb_iso_packet_descriptor *desc;
4654 u32 t;
4655 unsigned uframe;
4656 int urb_index = -1;
4657 struct fusbh200_iso_stream *stream = itd->stream;
4658 struct usb_device *dev;
4659 bool retval = false;
4661 /* for each uframe with a packet */
4662 for (uframe = 0; uframe < 8; uframe++) {
4663 if (likely (itd->index[uframe] == -1))
4664 continue;
4665 urb_index = itd->index[uframe];
4666 desc = &urb->iso_frame_desc [urb_index];
4668 t = hc32_to_cpup(fusbh200, &itd->hw_transaction [uframe]);
4669 itd->hw_transaction [uframe] = 0;
4671 /* report transfer status */
4672 if (unlikely (t & ISO_ERRS)) {
4673 urb->error_count++;
4674 if (t & FUSBH200_ISOC_BUF_ERR)
4675 desc->status = usb_pipein (urb->pipe)
4676 ? -ENOSR /* hc couldn't read */
4677 : -ECOMM; /* hc couldn't write */
4678 else if (t & FUSBH200_ISOC_BABBLE)
4679 desc->status = -EOVERFLOW;
4680 else /* (t & FUSBH200_ISOC_XACTERR) */
4681 desc->status = -EPROTO;
4683 /* HC need not update length with this error */
4684 if (!(t & FUSBH200_ISOC_BABBLE)) {
4685 desc->actual_length = fusbh200_itdlen(urb, desc, t);
4686 urb->actual_length += desc->actual_length;
4688 } else if (likely ((t & FUSBH200_ISOC_ACTIVE) == 0)) {
4689 desc->status = 0;
4690 desc->actual_length = fusbh200_itdlen(urb, desc, t);
4691 urb->actual_length += desc->actual_length;
4692 } else {
4693 /* URB was too late */
4694 desc->status = -EXDEV;
4698 /* handle completion now? */
4699 if (likely ((urb_index + 1) != urb->number_of_packets))
4700 goto done;
4702 /* ASSERT: it's really the last itd for this urb
4703 list_for_each_entry (itd, &stream->td_list, itd_list)
4704 BUG_ON (itd->urb == urb);
4707 /* give urb back to the driver; completion often (re)submits */
4708 dev = urb->dev;
4709 fusbh200_urb_done(fusbh200, urb, 0);
4710 retval = true;
4711 urb = NULL;
4713 --fusbh200->isoc_count;
4714 disable_periodic(fusbh200);
4716 if (unlikely(list_is_singular(&stream->td_list))) {
4717 fusbh200_to_hcd(fusbh200)->self.bandwidth_allocated
4718 -= stream->bandwidth;
4719 fusbh200_vdbg (fusbh200,
4720 "deschedule devp %s ep%d%s-iso\n",
4721 dev->devpath, stream->bEndpointAddress & 0x0f,
4722 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
4725 done:
4726 itd->urb = NULL;
4728 /* Add to the end of the free list for later reuse */
4729 list_move_tail(&itd->itd_list, &stream->free_list);
4731 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
4732 if (list_empty(&stream->td_list)) {
4733 list_splice_tail_init(&stream->free_list,
4734 &fusbh200->cached_itd_list);
4735 start_free_itds(fusbh200);
4738 return retval;
4741 /*-------------------------------------------------------------------------*/
4743 static int itd_submit (struct fusbh200_hcd *fusbh200, struct urb *urb,
4744 gfp_t mem_flags)
4746 int status = -EINVAL;
4747 unsigned long flags;
4748 struct fusbh200_iso_stream *stream;
4750 /* Get iso_stream head */
4751 stream = iso_stream_find (fusbh200, urb);
4752 if (unlikely (stream == NULL)) {
4753 fusbh200_dbg (fusbh200, "can't get iso stream\n");
4754 return -ENOMEM;
4756 if (unlikely (urb->interval != stream->interval &&
4757 fusbh200_port_speed(fusbh200, 0) == USB_PORT_STAT_HIGH_SPEED)) {
4758 fusbh200_dbg (fusbh200, "can't change iso interval %d --> %d\n",
4759 stream->interval, urb->interval);
4760 goto done;
4763 #ifdef FUSBH200_URB_TRACE
4764 fusbh200_dbg (fusbh200,
4765 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
4766 __func__, urb->dev->devpath, urb,
4767 usb_pipeendpoint (urb->pipe),
4768 usb_pipein (urb->pipe) ? "in" : "out",
4769 urb->transfer_buffer_length,
4770 urb->number_of_packets, urb->interval,
4771 stream);
4772 #endif
4774 /* allocate ITDs w/o locking anything */
4775 status = itd_urb_transaction (stream, fusbh200, urb, mem_flags);
4776 if (unlikely (status < 0)) {
4777 fusbh200_dbg (fusbh200, "can't init itds\n");
4778 goto done;
4781 /* schedule ... need to lock */
4782 spin_lock_irqsave (&fusbh200->lock, flags);
4783 if (unlikely(!HCD_HW_ACCESSIBLE(fusbh200_to_hcd(fusbh200)))) {
4784 status = -ESHUTDOWN;
4785 goto done_not_linked;
4787 status = usb_hcd_link_urb_to_ep(fusbh200_to_hcd(fusbh200), urb);
4788 if (unlikely(status))
4789 goto done_not_linked;
4790 status = iso_stream_schedule(fusbh200, urb, stream);
4791 if (likely (status == 0))
4792 itd_link_urb (fusbh200, urb, fusbh200->periodic_size << 3, stream);
4793 else
4794 usb_hcd_unlink_urb_from_ep(fusbh200_to_hcd(fusbh200), urb);
4795 done_not_linked:
4796 spin_unlock_irqrestore (&fusbh200->lock, flags);
4797 done:
4798 return status;
4801 /*-------------------------------------------------------------------------*/
4803 static void scan_isoc(struct fusbh200_hcd *fusbh200)
4805 unsigned uf, now_frame, frame;
4806 unsigned fmask = fusbh200->periodic_size - 1;
4807 bool modified, live;
4810 * When running, scan from last scan point up to "now"
4811 * else clean up by scanning everything that's left.
4812 * Touches as few pages as possible: cache-friendly.
4814 if (fusbh200->rh_state >= FUSBH200_RH_RUNNING) {
4815 uf = fusbh200_read_frame_index(fusbh200);
4816 now_frame = (uf >> 3) & fmask;
4817 live = true;
4818 } else {
4819 now_frame = (fusbh200->next_frame - 1) & fmask;
4820 live = false;
4822 fusbh200->now_frame = now_frame;
4824 frame = fusbh200->next_frame;
4825 for (;;) {
4826 union fusbh200_shadow q, *q_p;
4827 __hc32 type, *hw_p;
4829 restart:
4830 /* scan each element in frame's queue for completions */
4831 q_p = &fusbh200->pshadow [frame];
4832 hw_p = &fusbh200->periodic [frame];
4833 q.ptr = q_p->ptr;
4834 type = Q_NEXT_TYPE(fusbh200, *hw_p);
4835 modified = false;
4837 while (q.ptr != NULL) {
4838 switch (hc32_to_cpu(fusbh200, type)) {
4839 case Q_TYPE_ITD:
4840 /* If this ITD is still active, leave it for
4841 * later processing ... check the next entry.
4842 * No need to check for activity unless the
4843 * frame is current.
4845 if (frame == now_frame && live) {
4846 rmb();
4847 for (uf = 0; uf < 8; uf++) {
4848 if (q.itd->hw_transaction[uf] &
4849 ITD_ACTIVE(fusbh200))
4850 break;
4852 if (uf < 8) {
4853 q_p = &q.itd->itd_next;
4854 hw_p = &q.itd->hw_next;
4855 type = Q_NEXT_TYPE(fusbh200,
4856 q.itd->hw_next);
4857 q = *q_p;
4858 break;
4862 /* Take finished ITDs out of the schedule
4863 * and process them: recycle, maybe report
4864 * URB completion. HC won't cache the
4865 * pointer for much longer, if at all.
4867 *q_p = q.itd->itd_next;
4868 *hw_p = q.itd->hw_next;
4869 type = Q_NEXT_TYPE(fusbh200, q.itd->hw_next);
4870 wmb();
4871 modified = itd_complete (fusbh200, q.itd);
4872 q = *q_p;
4873 break;
4874 default:
4875 fusbh200_dbg(fusbh200, "corrupt type %d frame %d shadow %p\n",
4876 type, frame, q.ptr);
4877 // BUG ();
4878 /* FALL THROUGH */
4879 case Q_TYPE_QH:
4880 case Q_TYPE_FSTN:
4881 /* End of the iTDs and siTDs */
4882 q.ptr = NULL;
4883 break;
4886 /* assume completion callbacks modify the queue */
4887 if (unlikely(modified && fusbh200->isoc_count > 0))
4888 goto restart;
4891 /* Stop when we have reached the current frame */
4892 if (frame == now_frame)
4893 break;
4894 frame = (frame + 1) & fmask;
4896 fusbh200->next_frame = now_frame;
4898 /*-------------------------------------------------------------------------*/
4900 * Display / Set uframe_periodic_max
4902 static ssize_t show_uframe_periodic_max(struct device *dev,
4903 struct device_attribute *attr,
4904 char *buf)
4906 struct fusbh200_hcd *fusbh200;
4907 int n;
4909 fusbh200 = hcd_to_fusbh200(bus_to_hcd(dev_get_drvdata(dev)));
4910 n = scnprintf(buf, PAGE_SIZE, "%d\n", fusbh200->uframe_periodic_max);
4911 return n;
4915 static ssize_t store_uframe_periodic_max(struct device *dev,
4916 struct device_attribute *attr,
4917 const char *buf, size_t count)
4919 struct fusbh200_hcd *fusbh200;
4920 unsigned uframe_periodic_max;
4921 unsigned frame, uframe;
4922 unsigned short allocated_max;
4923 unsigned long flags;
4924 ssize_t ret;
4926 fusbh200 = hcd_to_fusbh200(bus_to_hcd(dev_get_drvdata(dev)));
4927 if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
4928 return -EINVAL;
4930 if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
4931 fusbh200_info(fusbh200, "rejecting invalid request for "
4932 "uframe_periodic_max=%u\n", uframe_periodic_max);
4933 return -EINVAL;
4936 ret = -EINVAL;
4939 * lock, so that our checking does not race with possible periodic
4940 * bandwidth allocation through submitting new urbs.
4942 spin_lock_irqsave (&fusbh200->lock, flags);
4945 * for request to decrease max periodic bandwidth, we have to check
4946 * every microframe in the schedule to see whether the decrease is
4947 * possible.
4949 if (uframe_periodic_max < fusbh200->uframe_periodic_max) {
4950 allocated_max = 0;
4952 for (frame = 0; frame < fusbh200->periodic_size; ++frame)
4953 for (uframe = 0; uframe < 7; ++uframe)
4954 allocated_max = max(allocated_max,
4955 periodic_usecs (fusbh200, frame, uframe));
4957 if (allocated_max > uframe_periodic_max) {
4958 fusbh200_info(fusbh200,
4959 "cannot decrease uframe_periodic_max becase "
4960 "periodic bandwidth is already allocated "
4961 "(%u > %u)\n",
4962 allocated_max, uframe_periodic_max);
4963 goto out_unlock;
4967 /* increasing is always ok */
4969 fusbh200_info(fusbh200, "setting max periodic bandwidth to %u%% "
4970 "(== %u usec/uframe)\n",
4971 100*uframe_periodic_max/125, uframe_periodic_max);
4973 if (uframe_periodic_max != 100)
4974 fusbh200_warn(fusbh200, "max periodic bandwidth set is non-standard\n");
4976 fusbh200->uframe_periodic_max = uframe_periodic_max;
4977 ret = count;
4979 out_unlock:
4980 spin_unlock_irqrestore (&fusbh200->lock, flags);
4981 return ret;
4983 static DEVICE_ATTR(uframe_periodic_max, 0644, show_uframe_periodic_max, store_uframe_periodic_max);
4986 static inline int create_sysfs_files(struct fusbh200_hcd *fusbh200)
4988 struct device *controller = fusbh200_to_hcd(fusbh200)->self.controller;
4989 int i = 0;
4991 if (i)
4992 goto out;
4994 i = device_create_file(controller, &dev_attr_uframe_periodic_max);
4995 out:
4996 return i;
4999 static inline void remove_sysfs_files(struct fusbh200_hcd *fusbh200)
5001 struct device *controller = fusbh200_to_hcd(fusbh200)->self.controller;
5003 device_remove_file(controller, &dev_attr_uframe_periodic_max);
5005 /*-------------------------------------------------------------------------*/
5007 /* On some systems, leaving remote wakeup enabled prevents system shutdown.
5008 * The firmware seems to think that powering off is a wakeup event!
5009 * This routine turns off remote wakeup and everything else, on all ports.
5011 static void fusbh200_turn_off_all_ports(struct fusbh200_hcd *fusbh200)
5013 u32 __iomem *status_reg = &fusbh200->regs->port_status;
5015 fusbh200_writel(fusbh200, PORT_RWC_BITS, status_reg);
5019 * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
5020 * Must be called with interrupts enabled and the lock not held.
5022 static void fusbh200_silence_controller(struct fusbh200_hcd *fusbh200)
5024 fusbh200_halt(fusbh200);
5026 spin_lock_irq(&fusbh200->lock);
5027 fusbh200->rh_state = FUSBH200_RH_HALTED;
5028 fusbh200_turn_off_all_ports(fusbh200);
5029 spin_unlock_irq(&fusbh200->lock);
5032 /* fusbh200_shutdown kick in for silicon on any bus (not just pci, etc).
5033 * This forcibly disables dma and IRQs, helping kexec and other cases
5034 * where the next system software may expect clean state.
5036 static void fusbh200_shutdown(struct usb_hcd *hcd)
5038 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
5040 spin_lock_irq(&fusbh200->lock);
5041 fusbh200->shutdown = true;
5042 fusbh200->rh_state = FUSBH200_RH_STOPPING;
5043 fusbh200->enabled_hrtimer_events = 0;
5044 spin_unlock_irq(&fusbh200->lock);
5046 fusbh200_silence_controller(fusbh200);
5048 hrtimer_cancel(&fusbh200->hrtimer);
5051 /*-------------------------------------------------------------------------*/
5054 * fusbh200_work is called from some interrupts, timers, and so on.
5055 * it calls driver completion functions, after dropping fusbh200->lock.
5057 static void fusbh200_work (struct fusbh200_hcd *fusbh200)
5059 /* another CPU may drop fusbh200->lock during a schedule scan while
5060 * it reports urb completions. this flag guards against bogus
5061 * attempts at re-entrant schedule scanning.
5063 if (fusbh200->scanning) {
5064 fusbh200->need_rescan = true;
5065 return;
5067 fusbh200->scanning = true;
5069 rescan:
5070 fusbh200->need_rescan = false;
5071 if (fusbh200->async_count)
5072 scan_async(fusbh200);
5073 if (fusbh200->intr_count > 0)
5074 scan_intr(fusbh200);
5075 if (fusbh200->isoc_count > 0)
5076 scan_isoc(fusbh200);
5077 if (fusbh200->need_rescan)
5078 goto rescan;
5079 fusbh200->scanning = false;
5081 /* the IO watchdog guards against hardware or driver bugs that
5082 * misplace IRQs, and should let us run completely without IRQs.
5083 * such lossage has been observed on both VT6202 and VT8235.
5085 turn_on_io_watchdog(fusbh200);
5089 * Called when the fusbh200_hcd module is removed.
5091 static void fusbh200_stop (struct usb_hcd *hcd)
5093 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5095 fusbh200_dbg (fusbh200, "stop\n");
5097 /* no more interrupts ... */
5099 spin_lock_irq(&fusbh200->lock);
5100 fusbh200->enabled_hrtimer_events = 0;
5101 spin_unlock_irq(&fusbh200->lock);
5103 fusbh200_quiesce(fusbh200);
5104 fusbh200_silence_controller(fusbh200);
5105 fusbh200_reset (fusbh200);
5107 hrtimer_cancel(&fusbh200->hrtimer);
5108 remove_sysfs_files(fusbh200);
5109 remove_debug_files (fusbh200);
5111 /* root hub is shut down separately (first, when possible) */
5112 spin_lock_irq (&fusbh200->lock);
5113 end_free_itds(fusbh200);
5114 spin_unlock_irq (&fusbh200->lock);
5115 fusbh200_mem_cleanup (fusbh200);
5117 #ifdef FUSBH200_STATS
5118 fusbh200_dbg(fusbh200, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
5119 fusbh200->stats.normal, fusbh200->stats.error, fusbh200->stats.iaa,
5120 fusbh200->stats.lost_iaa);
5121 fusbh200_dbg (fusbh200, "complete %ld unlink %ld\n",
5122 fusbh200->stats.complete, fusbh200->stats.unlink);
5123 #endif
5125 dbg_status (fusbh200, "fusbh200_stop completed",
5126 fusbh200_readl(fusbh200, &fusbh200->regs->status));
5129 /* one-time init, only for memory state */
5130 static int hcd_fusbh200_init(struct usb_hcd *hcd)
5132 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
5133 u32 temp;
5134 int retval;
5135 u32 hcc_params;
5136 struct fusbh200_qh_hw *hw;
5138 spin_lock_init(&fusbh200->lock);
5141 * keep io watchdog by default, those good HCDs could turn off it later
5143 fusbh200->need_io_watchdog = 1;
5145 hrtimer_init(&fusbh200->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
5146 fusbh200->hrtimer.function = fusbh200_hrtimer_func;
5147 fusbh200->next_hrtimer_event = FUSBH200_HRTIMER_NO_EVENT;
5149 hcc_params = fusbh200_readl(fusbh200, &fusbh200->caps->hcc_params);
5152 * by default set standard 80% (== 100 usec/uframe) max periodic
5153 * bandwidth as required by USB 2.0
5155 fusbh200->uframe_periodic_max = 100;
5158 * hw default: 1K periodic list heads, one per frame.
5159 * periodic_size can shrink by USBCMD update if hcc_params allows.
5161 fusbh200->periodic_size = DEFAULT_I_TDPS;
5162 INIT_LIST_HEAD(&fusbh200->intr_qh_list);
5163 INIT_LIST_HEAD(&fusbh200->cached_itd_list);
5165 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5166 /* periodic schedule size can be smaller than default */
5167 switch (FUSBH200_TUNE_FLS) {
5168 case 0: fusbh200->periodic_size = 1024; break;
5169 case 1: fusbh200->periodic_size = 512; break;
5170 case 2: fusbh200->periodic_size = 256; break;
5171 default: BUG();
5174 if ((retval = fusbh200_mem_init(fusbh200, GFP_KERNEL)) < 0)
5175 return retval;
5177 /* controllers may cache some of the periodic schedule ... */
5178 fusbh200->i_thresh = 2;
5181 * dedicate a qh for the async ring head, since we couldn't unlink
5182 * a 'real' qh without stopping the async schedule [4.8]. use it
5183 * as the 'reclamation list head' too.
5184 * its dummy is used in hw_alt_next of many tds, to prevent the qh
5185 * from automatically advancing to the next td after short reads.
5187 fusbh200->async->qh_next.qh = NULL;
5188 hw = fusbh200->async->hw;
5189 hw->hw_next = QH_NEXT(fusbh200, fusbh200->async->qh_dma);
5190 hw->hw_info1 = cpu_to_hc32(fusbh200, QH_HEAD);
5191 hw->hw_token = cpu_to_hc32(fusbh200, QTD_STS_HALT);
5192 hw->hw_qtd_next = FUSBH200_LIST_END(fusbh200);
5193 fusbh200->async->qh_state = QH_STATE_LINKED;
5194 hw->hw_alt_next = QTD_NEXT(fusbh200, fusbh200->async->dummy->qtd_dma);
5196 /* clear interrupt enables, set irq latency */
5197 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
5198 log2_irq_thresh = 0;
5199 temp = 1 << (16 + log2_irq_thresh);
5200 if (HCC_CANPARK(hcc_params)) {
5201 /* HW default park == 3, on hardware that supports it (like
5202 * NVidia and ALI silicon), maximizes throughput on the async
5203 * schedule by avoiding QH fetches between transfers.
5205 * With fast usb storage devices and NForce2, "park" seems to
5206 * make problems: throughput reduction (!), data errors...
5208 if (park) {
5209 park = min(park, (unsigned) 3);
5210 temp |= CMD_PARK;
5211 temp |= park << 8;
5213 fusbh200_dbg(fusbh200, "park %d\n", park);
5215 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5216 /* periodic schedule size can be smaller than default */
5217 temp &= ~(3 << 2);
5218 temp |= (FUSBH200_TUNE_FLS << 2);
5220 fusbh200->command = temp;
5222 /* Accept arbitrarily long scatter-gather lists */
5223 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
5224 hcd->self.sg_tablesize = ~0;
5225 return 0;
5228 /* start HC running; it's halted, hcd_fusbh200_init() has been run (once) */
5229 static int fusbh200_run (struct usb_hcd *hcd)
5231 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5232 u32 temp;
5233 u32 hcc_params;
5235 hcd->uses_new_polling = 1;
5237 /* EHCI spec section 4.1 */
5239 fusbh200_writel(fusbh200, fusbh200->periodic_dma, &fusbh200->regs->frame_list);
5240 fusbh200_writel(fusbh200, (u32)fusbh200->async->qh_dma, &fusbh200->regs->async_next);
5243 * hcc_params controls whether fusbh200->regs->segment must (!!!)
5244 * be used; it constrains QH/ITD/SITD and QTD locations.
5245 * pci_pool consistent memory always uses segment zero.
5246 * streaming mappings for I/O buffers, like pci_map_single(),
5247 * can return segments above 4GB, if the device allows.
5249 * NOTE: the dma mask is visible through dma_supported(), so
5250 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
5251 * Scsi_Host.highmem_io, and so forth. It's readonly to all
5252 * host side drivers though.
5254 hcc_params = fusbh200_readl(fusbh200, &fusbh200->caps->hcc_params);
5256 // Philips, Intel, and maybe others need CMD_RUN before the
5257 // root hub will detect new devices (why?); NEC doesn't
5258 fusbh200->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
5259 fusbh200->command |= CMD_RUN;
5260 fusbh200_writel(fusbh200, fusbh200->command, &fusbh200->regs->command);
5261 dbg_cmd (fusbh200, "init", fusbh200->command);
5264 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5265 * are explicitly handed to companion controller(s), so no TT is
5266 * involved with the root hub. (Except where one is integrated,
5267 * and there's no companion controller unless maybe for USB OTG.)
5269 * Turning on the CF flag will transfer ownership of all ports
5270 * from the companions to the EHCI controller. If any of the
5271 * companions are in the middle of a port reset at the time, it
5272 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
5273 * guarantees that no resets are in progress. After we set CF,
5274 * a short delay lets the hardware catch up; new resets shouldn't
5275 * be started before the port switching actions could complete.
5277 down_write(&ehci_cf_port_reset_rwsem);
5278 fusbh200->rh_state = FUSBH200_RH_RUNNING;
5279 fusbh200_readl(fusbh200, &fusbh200->regs->command); /* unblock posted writes */
5280 msleep(5);
5281 up_write(&ehci_cf_port_reset_rwsem);
5282 fusbh200->last_periodic_enable = ktime_get_real();
5284 temp = HC_VERSION(fusbh200, fusbh200_readl(fusbh200, &fusbh200->caps->hc_capbase));
5285 fusbh200_info (fusbh200,
5286 "USB %x.%x started, EHCI %x.%02x\n",
5287 ((fusbh200->sbrn & 0xf0)>>4), (fusbh200->sbrn & 0x0f),
5288 temp >> 8, temp & 0xff);
5290 fusbh200_writel(fusbh200, INTR_MASK,
5291 &fusbh200->regs->intr_enable); /* Turn On Interrupts */
5293 /* GRR this is run-once init(), being done every time the HC starts.
5294 * So long as they're part of class devices, we can't do it init()
5295 * since the class device isn't created that early.
5297 create_debug_files(fusbh200);
5298 create_sysfs_files(fusbh200);
5300 return 0;
5303 static int fusbh200_setup(struct usb_hcd *hcd)
5305 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
5306 int retval;
5308 fusbh200->regs = (void __iomem *)fusbh200->caps +
5309 HC_LENGTH(fusbh200, fusbh200_readl(fusbh200, &fusbh200->caps->hc_capbase));
5310 dbg_hcs_params(fusbh200, "reset");
5311 dbg_hcc_params(fusbh200, "reset");
5313 /* cache this readonly data; minimize chip reads */
5314 fusbh200->hcs_params = fusbh200_readl(fusbh200, &fusbh200->caps->hcs_params);
5316 fusbh200->sbrn = HCD_USB2;
5318 /* data structure init */
5319 retval = hcd_fusbh200_init(hcd);
5320 if (retval)
5321 return retval;
5323 retval = fusbh200_halt(fusbh200);
5324 if (retval)
5325 return retval;
5327 fusbh200_reset(fusbh200);
5329 return 0;
5332 /*-------------------------------------------------------------------------*/
5334 static irqreturn_t fusbh200_irq (struct usb_hcd *hcd)
5336 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5337 u32 status, masked_status, pcd_status = 0, cmd;
5338 int bh;
5340 spin_lock (&fusbh200->lock);
5342 status = fusbh200_readl(fusbh200, &fusbh200->regs->status);
5344 /* e.g. cardbus physical eject */
5345 if (status == ~(u32) 0) {
5346 fusbh200_dbg (fusbh200, "device removed\n");
5347 goto dead;
5351 * We don't use STS_FLR, but some controllers don't like it to
5352 * remain on, so mask it out along with the other status bits.
5354 masked_status = status & (INTR_MASK | STS_FLR);
5356 /* Shared IRQ? */
5357 if (!masked_status || unlikely(fusbh200->rh_state == FUSBH200_RH_HALTED)) {
5358 spin_unlock(&fusbh200->lock);
5359 return IRQ_NONE;
5362 /* clear (just) interrupts */
5363 fusbh200_writel(fusbh200, masked_status, &fusbh200->regs->status);
5364 cmd = fusbh200_readl(fusbh200, &fusbh200->regs->command);
5365 bh = 0;
5367 #ifdef VERBOSE_DEBUG
5368 /* unrequested/ignored: Frame List Rollover */
5369 dbg_status (fusbh200, "irq", status);
5370 #endif
5372 /* INT, ERR, and IAA interrupt rates can be throttled */
5374 /* normal [4.15.1.2] or error [4.15.1.1] completion */
5375 if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
5376 if (likely ((status & STS_ERR) == 0))
5377 COUNT (fusbh200->stats.normal);
5378 else
5379 COUNT (fusbh200->stats.error);
5380 bh = 1;
5383 /* complete the unlinking of some qh [4.15.2.3] */
5384 if (status & STS_IAA) {
5386 /* Turn off the IAA watchdog */
5387 fusbh200->enabled_hrtimer_events &= ~BIT(FUSBH200_HRTIMER_IAA_WATCHDOG);
5390 * Mild optimization: Allow another IAAD to reset the
5391 * hrtimer, if one occurs before the next expiration.
5392 * In theory we could always cancel the hrtimer, but
5393 * tests show that about half the time it will be reset
5394 * for some other event anyway.
5396 if (fusbh200->next_hrtimer_event == FUSBH200_HRTIMER_IAA_WATCHDOG)
5397 ++fusbh200->next_hrtimer_event;
5399 /* guard against (alleged) silicon errata */
5400 if (cmd & CMD_IAAD)
5401 fusbh200_dbg(fusbh200, "IAA with IAAD still set?\n");
5402 if (fusbh200->async_iaa) {
5403 COUNT(fusbh200->stats.iaa);
5404 end_unlink_async(fusbh200);
5405 } else
5406 fusbh200_dbg(fusbh200, "IAA with nothing unlinked?\n");
5409 /* remote wakeup [4.3.1] */
5410 if (status & STS_PCD) {
5411 int pstatus;
5412 u32 __iomem *status_reg = &fusbh200->regs->port_status;
5414 /* kick root hub later */
5415 pcd_status = status;
5417 /* resume root hub? */
5418 if (fusbh200->rh_state == FUSBH200_RH_SUSPENDED)
5419 usb_hcd_resume_root_hub(hcd);
5421 pstatus = fusbh200_readl(fusbh200, status_reg);
5423 if (test_bit(0, &fusbh200->suspended_ports) &&
5424 ((pstatus & PORT_RESUME) ||
5425 !(pstatus & PORT_SUSPEND)) &&
5426 (pstatus & PORT_PE) &&
5427 fusbh200->reset_done[0] == 0) {
5429 /* start 20 msec resume signaling from this port,
5430 * and make khubd collect PORT_STAT_C_SUSPEND to
5431 * stop that signaling. Use 5 ms extra for safety,
5432 * like usb_port_resume() does.
5434 fusbh200->reset_done[0] = jiffies + msecs_to_jiffies(25);
5435 set_bit(0, &fusbh200->resuming_ports);
5436 fusbh200_dbg (fusbh200, "port 1 remote wakeup\n");
5437 mod_timer(&hcd->rh_timer, fusbh200->reset_done[0]);
5441 /* PCI errors [4.15.2.4] */
5442 if (unlikely ((status & STS_FATAL) != 0)) {
5443 fusbh200_err(fusbh200, "fatal error\n");
5444 dbg_cmd(fusbh200, "fatal", cmd);
5445 dbg_status(fusbh200, "fatal", status);
5446 dead:
5447 usb_hc_died(hcd);
5449 /* Don't let the controller do anything more */
5450 fusbh200->shutdown = true;
5451 fusbh200->rh_state = FUSBH200_RH_STOPPING;
5452 fusbh200->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
5453 fusbh200_writel(fusbh200, fusbh200->command, &fusbh200->regs->command);
5454 fusbh200_writel(fusbh200, 0, &fusbh200->regs->intr_enable);
5455 fusbh200_handle_controller_death(fusbh200);
5457 /* Handle completions when the controller stops */
5458 bh = 0;
5461 if (bh)
5462 fusbh200_work (fusbh200);
5463 spin_unlock (&fusbh200->lock);
5464 if (pcd_status)
5465 usb_hcd_poll_rh_status(hcd);
5466 return IRQ_HANDLED;
5469 /*-------------------------------------------------------------------------*/
5472 * non-error returns are a promise to giveback() the urb later
5473 * we drop ownership so next owner (or urb unlink) can get it
5475 * urb + dev is in hcd.self.controller.urb_list
5476 * we're queueing TDs onto software and hardware lists
5478 * hcd-specific init for hcpriv hasn't been done yet
5480 * NOTE: control, bulk, and interrupt share the same code to append TDs
5481 * to a (possibly active) QH, and the same QH scanning code.
5483 static int fusbh200_urb_enqueue (
5484 struct usb_hcd *hcd,
5485 struct urb *urb,
5486 gfp_t mem_flags
5488 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5489 struct list_head qtd_list;
5491 INIT_LIST_HEAD (&qtd_list);
5493 switch (usb_pipetype (urb->pipe)) {
5494 case PIPE_CONTROL:
5495 /* qh_completions() code doesn't handle all the fault cases
5496 * in multi-TD control transfers. Even 1KB is rare anyway.
5498 if (urb->transfer_buffer_length > (16 * 1024))
5499 return -EMSGSIZE;
5500 /* FALLTHROUGH */
5501 /* case PIPE_BULK: */
5502 default:
5503 if (!qh_urb_transaction (fusbh200, urb, &qtd_list, mem_flags))
5504 return -ENOMEM;
5505 return submit_async(fusbh200, urb, &qtd_list, mem_flags);
5507 case PIPE_INTERRUPT:
5508 if (!qh_urb_transaction (fusbh200, urb, &qtd_list, mem_flags))
5509 return -ENOMEM;
5510 return intr_submit(fusbh200, urb, &qtd_list, mem_flags);
5512 case PIPE_ISOCHRONOUS:
5513 return itd_submit (fusbh200, urb, mem_flags);
5517 /* remove from hardware lists
5518 * completions normally happen asynchronously
5521 static int fusbh200_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
5523 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5524 struct fusbh200_qh *qh;
5525 unsigned long flags;
5526 int rc;
5528 spin_lock_irqsave (&fusbh200->lock, flags);
5529 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
5530 if (rc)
5531 goto done;
5533 switch (usb_pipetype (urb->pipe)) {
5534 // case PIPE_CONTROL:
5535 // case PIPE_BULK:
5536 default:
5537 qh = (struct fusbh200_qh *) urb->hcpriv;
5538 if (!qh)
5539 break;
5540 switch (qh->qh_state) {
5541 case QH_STATE_LINKED:
5542 case QH_STATE_COMPLETING:
5543 start_unlink_async(fusbh200, qh);
5544 break;
5545 case QH_STATE_UNLINK:
5546 case QH_STATE_UNLINK_WAIT:
5547 /* already started */
5548 break;
5549 case QH_STATE_IDLE:
5550 /* QH might be waiting for a Clear-TT-Buffer */
5551 qh_completions(fusbh200, qh);
5552 break;
5554 break;
5556 case PIPE_INTERRUPT:
5557 qh = (struct fusbh200_qh *) urb->hcpriv;
5558 if (!qh)
5559 break;
5560 switch (qh->qh_state) {
5561 case QH_STATE_LINKED:
5562 case QH_STATE_COMPLETING:
5563 start_unlink_intr(fusbh200, qh);
5564 break;
5565 case QH_STATE_IDLE:
5566 qh_completions (fusbh200, qh);
5567 break;
5568 default:
5569 fusbh200_dbg (fusbh200, "bogus qh %p state %d\n",
5570 qh, qh->qh_state);
5571 goto done;
5573 break;
5575 case PIPE_ISOCHRONOUS:
5576 // itd...
5578 // wait till next completion, do it then.
5579 // completion irqs can wait up to 1024 msec,
5580 break;
5582 done:
5583 spin_unlock_irqrestore (&fusbh200->lock, flags);
5584 return rc;
5587 /*-------------------------------------------------------------------------*/
5589 // bulk qh holds the data toggle
5591 static void
5592 fusbh200_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
5594 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5595 unsigned long flags;
5596 struct fusbh200_qh *qh, *tmp;
5598 /* ASSERT: any requests/urbs are being unlinked */
5599 /* ASSERT: nobody can be submitting urbs for this any more */
5601 rescan:
5602 spin_lock_irqsave (&fusbh200->lock, flags);
5603 qh = ep->hcpriv;
5604 if (!qh)
5605 goto done;
5607 /* endpoints can be iso streams. for now, we don't
5608 * accelerate iso completions ... so spin a while.
5610 if (qh->hw == NULL) {
5611 struct fusbh200_iso_stream *stream = ep->hcpriv;
5613 if (!list_empty(&stream->td_list))
5614 goto idle_timeout;
5616 /* BUG_ON(!list_empty(&stream->free_list)); */
5617 kfree(stream);
5618 goto done;
5621 if (fusbh200->rh_state < FUSBH200_RH_RUNNING)
5622 qh->qh_state = QH_STATE_IDLE;
5623 switch (qh->qh_state) {
5624 case QH_STATE_LINKED:
5625 case QH_STATE_COMPLETING:
5626 for (tmp = fusbh200->async->qh_next.qh;
5627 tmp && tmp != qh;
5628 tmp = tmp->qh_next.qh)
5629 continue;
5630 /* periodic qh self-unlinks on empty, and a COMPLETING qh
5631 * may already be unlinked.
5633 if (tmp)
5634 start_unlink_async(fusbh200, qh);
5635 /* FALL THROUGH */
5636 case QH_STATE_UNLINK: /* wait for hw to finish? */
5637 case QH_STATE_UNLINK_WAIT:
5638 idle_timeout:
5639 spin_unlock_irqrestore (&fusbh200->lock, flags);
5640 schedule_timeout_uninterruptible(1);
5641 goto rescan;
5642 case QH_STATE_IDLE: /* fully unlinked */
5643 if (qh->clearing_tt)
5644 goto idle_timeout;
5645 if (list_empty (&qh->qtd_list)) {
5646 qh_destroy(fusbh200, qh);
5647 break;
5649 /* else FALL THROUGH */
5650 default:
5651 /* caller was supposed to have unlinked any requests;
5652 * that's not our job. just leak this memory.
5654 fusbh200_err (fusbh200, "qh %p (#%02x) state %d%s\n",
5655 qh, ep->desc.bEndpointAddress, qh->qh_state,
5656 list_empty (&qh->qtd_list) ? "" : "(has tds)");
5657 break;
5659 done:
5660 ep->hcpriv = NULL;
5661 spin_unlock_irqrestore (&fusbh200->lock, flags);
5664 static void
5665 fusbh200_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
5667 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200(hcd);
5668 struct fusbh200_qh *qh;
5669 int eptype = usb_endpoint_type(&ep->desc);
5670 int epnum = usb_endpoint_num(&ep->desc);
5671 int is_out = usb_endpoint_dir_out(&ep->desc);
5672 unsigned long flags;
5674 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
5675 return;
5677 spin_lock_irqsave(&fusbh200->lock, flags);
5678 qh = ep->hcpriv;
5680 /* For Bulk and Interrupt endpoints we maintain the toggle state
5681 * in the hardware; the toggle bits in udev aren't used at all.
5682 * When an endpoint is reset by usb_clear_halt() we must reset
5683 * the toggle bit in the QH.
5685 if (qh) {
5686 usb_settoggle(qh->dev, epnum, is_out, 0);
5687 if (!list_empty(&qh->qtd_list)) {
5688 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
5689 } else if (qh->qh_state == QH_STATE_LINKED ||
5690 qh->qh_state == QH_STATE_COMPLETING) {
5692 /* The toggle value in the QH can't be updated
5693 * while the QH is active. Unlink it now;
5694 * re-linking will call qh_refresh().
5696 if (eptype == USB_ENDPOINT_XFER_BULK)
5697 start_unlink_async(fusbh200, qh);
5698 else
5699 start_unlink_intr(fusbh200, qh);
5702 spin_unlock_irqrestore(&fusbh200->lock, flags);
5705 static int fusbh200_get_frame (struct usb_hcd *hcd)
5707 struct fusbh200_hcd *fusbh200 = hcd_to_fusbh200 (hcd);
5708 return (fusbh200_read_frame_index(fusbh200) >> 3) % fusbh200->periodic_size;
5711 /*-------------------------------------------------------------------------*/
5714 * The EHCI in ChipIdea HDRC cannot be a separate module or device,
5715 * because its registers (and irq) are shared between host/gadget/otg
5716 * functions and in order to facilitate role switching we cannot
5717 * give the fusbh200 driver exclusive access to those.
5719 MODULE_DESCRIPTION(DRIVER_DESC);
5720 MODULE_AUTHOR (DRIVER_AUTHOR);
5721 MODULE_LICENSE ("GPL");
5723 static const struct hc_driver fusbh200_fusbh200_hc_driver = {
5724 .description = hcd_name,
5725 .product_desc = "Faraday USB2.0 Host Controller",
5726 .hcd_priv_size = sizeof(struct fusbh200_hcd),
5729 * generic hardware linkage
5731 .irq = fusbh200_irq,
5732 .flags = HCD_MEMORY | HCD_USB2,
5735 * basic lifecycle operations
5737 .reset = hcd_fusbh200_init,
5738 .start = fusbh200_run,
5739 .stop = fusbh200_stop,
5740 .shutdown = fusbh200_shutdown,
5743 * managing i/o requests and associated device resources
5745 .urb_enqueue = fusbh200_urb_enqueue,
5746 .urb_dequeue = fusbh200_urb_dequeue,
5747 .endpoint_disable = fusbh200_endpoint_disable,
5748 .endpoint_reset = fusbh200_endpoint_reset,
5751 * scheduling support
5753 .get_frame_number = fusbh200_get_frame,
5756 * root hub support
5758 .hub_status_data = fusbh200_hub_status_data,
5759 .hub_control = fusbh200_hub_control,
5760 .bus_suspend = fusbh200_bus_suspend,
5761 .bus_resume = fusbh200_bus_resume,
5763 .relinquish_port = fusbh200_relinquish_port,
5764 .port_handed_over = fusbh200_port_handed_over,
5766 .clear_tt_buffer_complete = fusbh200_clear_tt_buffer_complete,
5769 static void fusbh200_init(struct fusbh200_hcd *fusbh200)
5771 u32 reg;
5773 reg = fusbh200_readl(fusbh200, &fusbh200->regs->bmcsr);
5774 reg |= BMCSR_INT_POLARITY;
5775 reg &= ~BMCSR_VBUS_OFF;
5776 fusbh200_writel(fusbh200, reg, &fusbh200->regs->bmcsr);
5778 reg = fusbh200_readl(fusbh200, &fusbh200->regs->bmier);
5779 fusbh200_writel(fusbh200, reg | BMIER_OVC_EN | BMIER_VBUS_ERR_EN,
5780 &fusbh200->regs->bmier);
5784 * fusbh200_hcd_probe - initialize faraday FUSBH200 HCDs
5786 * Allocates basic resources for this USB host controller, and
5787 * then invokes the start() method for the HCD associated with it
5788 * through the hotplug entry's driver_data.
5790 static int fusbh200_hcd_probe(struct platform_device *pdev)
5792 struct device *dev = &pdev->dev;
5793 struct usb_hcd *hcd;
5794 struct resource *res;
5795 int irq;
5796 int retval = -ENODEV;
5797 struct fusbh200_hcd *fusbh200;
5799 if (usb_disabled())
5800 return -ENODEV;
5802 pdev->dev.power.power_state = PMSG_ON;
5804 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
5805 if (!res) {
5806 dev_err(dev,
5807 "Found HC with no IRQ. Check %s setup!\n",
5808 dev_name(dev));
5809 return -ENODEV;
5812 irq = res->start;
5814 hcd = usb_create_hcd(&fusbh200_fusbh200_hc_driver, dev,
5815 dev_name(dev));
5816 if (!hcd) {
5817 dev_err(dev, "failed to create hcd with err %d\n", retval);
5818 retval = -ENOMEM;
5819 goto fail_create_hcd;
5822 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5823 if (!res) {
5824 dev_err(dev,
5825 "Found HC with no register addr. Check %s setup!\n",
5826 dev_name(dev));
5827 retval = -ENODEV;
5828 goto fail_request_resource;
5831 hcd->rsrc_start = res->start;
5832 hcd->rsrc_len = resource_size(res);
5833 hcd->has_tt = 1;
5835 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
5836 fusbh200_fusbh200_hc_driver.description)) {
5837 dev_dbg(dev, "controller already in use\n");
5838 retval = -EBUSY;
5839 goto fail_request_resource;
5842 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
5843 if (!res) {
5844 dev_err(dev,
5845 "Found HC with no register addr. Check %s setup!\n",
5846 dev_name(dev));
5847 retval = -ENODEV;
5848 goto fail_request_resource;
5851 hcd->regs = ioremap_nocache(res->start, resource_size(res));
5852 if (hcd->regs == NULL) {
5853 dev_dbg(dev, "error mapping memory\n");
5854 retval = -EFAULT;
5855 goto fail_ioremap;
5858 fusbh200 = hcd_to_fusbh200(hcd);
5860 fusbh200->caps = hcd->regs;
5862 retval = fusbh200_setup(hcd);
5863 if (retval)
5864 goto fail_add_hcd;
5866 fusbh200_init(fusbh200);
5868 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
5869 if (retval) {
5870 dev_err(dev, "failed to add hcd with err %d\n", retval);
5871 goto fail_add_hcd;
5874 return retval;
5876 fail_add_hcd:
5877 iounmap(hcd->regs);
5878 fail_ioremap:
5879 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
5880 fail_request_resource:
5881 usb_put_hcd(hcd);
5882 fail_create_hcd:
5883 dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
5884 return retval;
5888 * fusbh200_hcd_remove - shutdown processing for EHCI HCDs
5889 * @dev: USB Host Controller being removed
5891 * Reverses the effect of fotg2xx_usb_hcd_probe(), first invoking
5892 * the HCD's stop() method. It is always called from a thread
5893 * context, normally "rmmod", "apmd", or something similar.
5895 static int fusbh200_hcd_remove(struct platform_device *pdev)
5897 struct device *dev = &pdev->dev;
5898 struct usb_hcd *hcd = dev_get_drvdata(dev);
5900 if (!hcd)
5901 return 0;
5903 usb_remove_hcd(hcd);
5904 iounmap(hcd->regs);
5905 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
5906 usb_put_hcd(hcd);
5908 return 0;
5911 static struct platform_driver fusbh200_hcd_fusbh200_driver = {
5912 .driver = {
5913 .name = "fusbh200",
5915 .probe = fusbh200_hcd_probe,
5916 .remove = fusbh200_hcd_remove,
5919 static int __init fusbh200_hcd_init(void)
5921 int retval = 0;
5923 if (usb_disabled())
5924 return -ENODEV;
5926 printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
5927 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5928 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
5929 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
5930 printk(KERN_WARNING "Warning! fusbh200_hcd should always be loaded"
5931 " before uhci_hcd and ohci_hcd, not after\n");
5933 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd\n",
5934 hcd_name,
5935 sizeof(struct fusbh200_qh), sizeof(struct fusbh200_qtd),
5936 sizeof(struct fusbh200_itd));
5938 #ifdef DEBUG
5939 fusbh200_debug_root = debugfs_create_dir("fusbh200", usb_debug_root);
5940 if (!fusbh200_debug_root) {
5941 retval = -ENOENT;
5942 goto err_debug;
5944 #endif
5946 retval = platform_driver_register(&fusbh200_hcd_fusbh200_driver);
5947 if (retval < 0)
5948 goto clean;
5949 return retval;
5951 platform_driver_unregister(&fusbh200_hcd_fusbh200_driver);
5952 clean:
5953 #ifdef DEBUG
5954 debugfs_remove(fusbh200_debug_root);
5955 fusbh200_debug_root = NULL;
5956 err_debug:
5957 #endif
5958 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5959 return retval;
5961 module_init(fusbh200_hcd_init);
5963 static void __exit fusbh200_hcd_cleanup(void)
5965 platform_driver_unregister(&fusbh200_hcd_fusbh200_driver);
5966 #ifdef DEBUG
5967 debugfs_remove(fusbh200_debug_root);
5968 #endif
5969 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5971 module_exit(fusbh200_hcd_cleanup);