blob: 15ee566e46cb008b74001c2f413fdbe91cdf79f7 [file] [log] [blame]
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001/*
2 * Faraday FOTG210 EHCI-like driver
3 *
4 * Copyright (c) 2013 Faraday Technology Corporation
5 *
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>
9 *
10 * Most of code borrowed from the Linux-3.7 EHCI driver
11 *
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.
16 *
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.
21 *
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.
25 */
26#include <linux/module.h>
27#include <linux/device.h>
28#include <linux/dmapool.h>
29#include <linux/kernel.h>
30#include <linux/delay.h>
31#include <linux/ioport.h>
32#include <linux/sched.h>
33#include <linux/vmalloc.h>
34#include <linux/errno.h>
35#include <linux/init.h>
36#include <linux/hrtimer.h>
37#include <linux/list.h>
38#include <linux/interrupt.h>
39#include <linux/usb.h>
40#include <linux/usb/hcd.h>
41#include <linux/moduleparam.h>
42#include <linux/dma-mapping.h>
43#include <linux/debugfs.h>
44#include <linux/slab.h>
45#include <linux/uaccess.h>
46#include <linux/platform_device.h>
47#include <linux/io.h>
48
49#include <asm/byteorder.h>
50#include <asm/irq.h>
51#include <asm/unaligned.h>
52
53/*-------------------------------------------------------------------------*/
54#define DRIVER_AUTHOR "Yuan-Hsin Chen"
55#define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
56
57static const char hcd_name[] = "fotg210_hcd";
58
59#undef VERBOSE_DEBUG
60#undef FOTG210_URB_TRACE
61
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000062#define FOTG210_STATS
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000063
64/* magic numbers that can affect system performance */
65#define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
66#define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
67#define FOTG210_TUNE_RL_TT 0
68#define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
69#define FOTG210_TUNE_MULT_TT 1
70/*
71 * Some drivers think it's safe to schedule isochronous transfers more than
72 * 256 ms into the future (partly as a result of an old bug in the scheduling
73 * code). In an attempt to avoid trouble, we will use a minimum scheduling
74 * length of 512 frames instead of 256.
75 */
76#define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
77
78/* Initial IRQ latency: faster than hw default */
79static int log2_irq_thresh; /* 0 to 6 */
80module_param(log2_irq_thresh, int, S_IRUGO);
81MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
82
83/* initial park setting: slower than hw default */
84static unsigned park;
85module_param(park, uint, S_IRUGO);
86MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
87
88/* for link power management(LPM) feature */
89static unsigned int hird;
90module_param(hird, int, S_IRUGO);
91MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
92
93#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
94
95#include "fotg210.h"
96
97/*-------------------------------------------------------------------------*/
98
99#define fotg210_dbg(fotg210, fmt, args...) \
100 dev_dbg(fotg210_to_hcd(fotg210)->self.controller , fmt , ## args)
101#define fotg210_err(fotg210, fmt, args...) \
102 dev_err(fotg210_to_hcd(fotg210)->self.controller , fmt , ## args)
103#define fotg210_info(fotg210, fmt, args...) \
104 dev_info(fotg210_to_hcd(fotg210)->self.controller , fmt , ## args)
105#define fotg210_warn(fotg210, fmt, args...) \
106 dev_warn(fotg210_to_hcd(fotg210)->self.controller , fmt , ## args)
107
108#ifdef VERBOSE_DEBUG
109# define fotg210_vdbg fotg210_dbg
110#else
111 static inline void fotg210_vdbg(struct fotg210_hcd *fotg210, ...) {}
112#endif
113
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000114/* check the values in the HCSPARAMS register
115 * (host controller _Structural_ parameters)
116 * see EHCI spec, Table 2-4 for each value
117 */
118static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
119{
120 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
121
122 fotg210_dbg(fotg210,
123 "%s hcs_params 0x%x ports=%d\n",
124 label, params,
125 HCS_N_PORTS(params)
126 );
127}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000128
129/* check the values in the HCCPARAMS register
130 * (host controller _Capability_ parameters)
131 * see EHCI Spec, Table 2-5 for each value
132 * */
133static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
134{
135 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
136
137 fotg210_dbg(fotg210,
138 "%s hcc_params %04x uframes %s%s\n",
139 label,
140 params,
141 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
142 HCC_CANPARK(params) ? " park" : "");
143}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000144
145static void __maybe_unused
146dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
147{
148 fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
149 hc32_to_cpup(fotg210, &qtd->hw_next),
150 hc32_to_cpup(fotg210, &qtd->hw_alt_next),
151 hc32_to_cpup(fotg210, &qtd->hw_token),
152 hc32_to_cpup(fotg210, &qtd->hw_buf[0]));
153 if (qtd->hw_buf[1])
154 fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
155 hc32_to_cpup(fotg210, &qtd->hw_buf[1]),
156 hc32_to_cpup(fotg210, &qtd->hw_buf[2]),
157 hc32_to_cpup(fotg210, &qtd->hw_buf[3]),
158 hc32_to_cpup(fotg210, &qtd->hw_buf[4]));
159}
160
161static void __maybe_unused
162dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
163{
164 struct fotg210_qh_hw *hw = qh->hw;
165
166 fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label,
167 qh, hw->hw_next, hw->hw_info1, hw->hw_info2, hw->hw_current);
168 dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
169}
170
171static void __maybe_unused
172dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
173{
174 fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n",
175 label, itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next),
176 itd->urb);
177 fotg210_dbg(fotg210,
178 " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
179 hc32_to_cpu(fotg210, itd->hw_transaction[0]),
180 hc32_to_cpu(fotg210, itd->hw_transaction[1]),
181 hc32_to_cpu(fotg210, itd->hw_transaction[2]),
182 hc32_to_cpu(fotg210, itd->hw_transaction[3]),
183 hc32_to_cpu(fotg210, itd->hw_transaction[4]),
184 hc32_to_cpu(fotg210, itd->hw_transaction[5]),
185 hc32_to_cpu(fotg210, itd->hw_transaction[6]),
186 hc32_to_cpu(fotg210, itd->hw_transaction[7]));
187 fotg210_dbg(fotg210,
188 " buf: %08x %08x %08x %08x %08x %08x %08x\n",
189 hc32_to_cpu(fotg210, itd->hw_bufp[0]),
190 hc32_to_cpu(fotg210, itd->hw_bufp[1]),
191 hc32_to_cpu(fotg210, itd->hw_bufp[2]),
192 hc32_to_cpu(fotg210, itd->hw_bufp[3]),
193 hc32_to_cpu(fotg210, itd->hw_bufp[4]),
194 hc32_to_cpu(fotg210, itd->hw_bufp[5]),
195 hc32_to_cpu(fotg210, itd->hw_bufp[6]));
196 fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n",
197 itd->index[0], itd->index[1], itd->index[2],
198 itd->index[3], itd->index[4], itd->index[5],
199 itd->index[6], itd->index[7]);
200}
201
202static int __maybe_unused
203dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
204{
205 return scnprintf(buf, len,
206 "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
207 label, label[0] ? " " : "", status,
208 (status & STS_ASS) ? " Async" : "",
209 (status & STS_PSS) ? " Periodic" : "",
210 (status & STS_RECL) ? " Recl" : "",
211 (status & STS_HALT) ? " Halt" : "",
212 (status & STS_IAA) ? " IAA" : "",
213 (status & STS_FATAL) ? " FATAL" : "",
214 (status & STS_FLR) ? " FLR" : "",
215 (status & STS_PCD) ? " PCD" : "",
216 (status & STS_ERR) ? " ERR" : "",
217 (status & STS_INT) ? " INT" : ""
218 );
219}
220
221static int __maybe_unused
222dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
223{
224 return scnprintf(buf, len,
225 "%s%sintrenable %02x%s%s%s%s%s%s",
226 label, label[0] ? " " : "", enable,
227 (enable & STS_IAA) ? " IAA" : "",
228 (enable & STS_FATAL) ? " FATAL" : "",
229 (enable & STS_FLR) ? " FLR" : "",
230 (enable & STS_PCD) ? " PCD" : "",
231 (enable & STS_ERR) ? " ERR" : "",
232 (enable & STS_INT) ? " INT" : ""
233 );
234}
235
236static const char *const fls_strings[] = { "1024", "512", "256", "??" };
237
238static int
239dbg_command_buf(char *buf, unsigned len, const char *label, u32 command)
240{
241 return scnprintf(buf, len,
242 "%s%scommand %07x %s=%d ithresh=%d%s%s%s "
243 "period=%s%s %s",
244 label, label[0] ? " " : "", command,
245 (command & CMD_PARK) ? " park" : "(park)",
246 CMD_PARK_CNT(command),
247 (command >> 16) & 0x3f,
248 (command & CMD_IAAD) ? " IAAD" : "",
249 (command & CMD_ASE) ? " Async" : "",
250 (command & CMD_PSE) ? " Periodic" : "",
251 fls_strings[(command >> 2) & 0x3],
252 (command & CMD_RESET) ? " Reset" : "",
253 (command & CMD_RUN) ? "RUN" : "HALT"
254 );
255}
256
257static int
258dbg_port_buf(char *buf, unsigned len, const char *label, int port, u32 status)
259{
260 char *sig;
261
262 /* signaling state */
263 switch (status & (3 << 10)) {
264 case 0 << 10:
265 sig = "se0";
266 break;
267 case 1 << 10:
268 sig = "k";
269 break; /* low speed */
270 case 2 << 10:
271 sig = "j";
272 break;
273 default:
274 sig = "?";
275 break;
276 }
277
278 return scnprintf(buf, len,
279 "%s%sport:%d status %06x %d "
280 "sig=%s%s%s%s%s%s%s%s",
281 label, label[0] ? " " : "", port, status,
282 status>>25,/*device address */
283 sig,
284 (status & PORT_RESET) ? " RESET" : "",
285 (status & PORT_SUSPEND) ? " SUSPEND" : "",
286 (status & PORT_RESUME) ? " RESUME" : "",
287 (status & PORT_PEC) ? " PEC" : "",
288 (status & PORT_PE) ? " PE" : "",
289 (status & PORT_CSC) ? " CSC" : "",
290 (status & PORT_CONNECT) ? " CONNECT" : "");
291}
292
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000293/* functions have the "wrong" filename when they're output... */
294#define dbg_status(fotg210, label, status) { \
295 char _buf[80]; \
296 dbg_status_buf(_buf, sizeof(_buf), label, status); \
297 fotg210_dbg(fotg210, "%s\n", _buf); \
298}
299
300#define dbg_cmd(fotg210, label, command) { \
301 char _buf[80]; \
302 dbg_command_buf(_buf, sizeof(_buf), label, command); \
303 fotg210_dbg(fotg210, "%s\n", _buf); \
304}
305
306#define dbg_port(fotg210, label, port, status) { \
307 char _buf[80]; \
308 dbg_port_buf(_buf, sizeof(_buf), label, port, status); \
309 fotg210_dbg(fotg210, "%s\n", _buf); \
310}
311
312/*-------------------------------------------------------------------------*/
313
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000314/* troubleshooting help: expose state in debugfs */
315
316static int debug_async_open(struct inode *, struct file *);
317static int debug_periodic_open(struct inode *, struct file *);
318static int debug_registers_open(struct inode *, struct file *);
319static int debug_async_open(struct inode *, struct file *);
320
321static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
322static int debug_close(struct inode *, struct file *);
323
324static const struct file_operations debug_async_fops = {
325 .owner = THIS_MODULE,
326 .open = debug_async_open,
327 .read = debug_output,
328 .release = debug_close,
329 .llseek = default_llseek,
330};
331static const struct file_operations debug_periodic_fops = {
332 .owner = THIS_MODULE,
333 .open = debug_periodic_open,
334 .read = debug_output,
335 .release = debug_close,
336 .llseek = default_llseek,
337};
338static const struct file_operations debug_registers_fops = {
339 .owner = THIS_MODULE,
340 .open = debug_registers_open,
341 .read = debug_output,
342 .release = debug_close,
343 .llseek = default_llseek,
344};
345
346static struct dentry *fotg210_debug_root;
347
348struct debug_buffer {
349 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
350 struct usb_bus *bus;
351 struct mutex mutex; /* protect filling of buffer */
352 size_t count; /* number of characters filled into buffer */
353 char *output_buf;
354 size_t alloc_size;
355};
356
357#define speed_char(info1)({ char tmp; \
358 switch (info1 & (3 << 12)) { \
359 case QH_FULL_SPEED: \
360 tmp = 'f'; break; \
361 case QH_LOW_SPEED: \
362 tmp = 'l'; break; \
363 case QH_HIGH_SPEED: \
364 tmp = 'h'; break; \
365 default: \
366 tmp = '?'; break; \
Joe Perches2b84f922013-10-08 16:01:37 -0700367 } tmp; })
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000368
369static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
370{
371 __u32 v = hc32_to_cpu(fotg210, token);
372
373 if (v & QTD_STS_ACTIVE)
374 return '*';
375 if (v & QTD_STS_HALT)
376 return '-';
377 if (!IS_SHORT_READ(v))
378 return ' ';
379 /* tries to advance through hw_alt_next */
380 return '/';
381}
382
383static void qh_lines(
384 struct fotg210_hcd *fotg210,
385 struct fotg210_qh *qh,
386 char **nextp,
387 unsigned *sizep
388)
389{
390 u32 scratch;
391 u32 hw_curr;
392 struct fotg210_qtd *td;
393 unsigned temp;
394 unsigned size = *sizep;
395 char *next = *nextp;
396 char mark;
397 __le32 list_end = FOTG210_LIST_END(fotg210);
398 struct fotg210_qh_hw *hw = qh->hw;
399
400 if (hw->hw_qtd_next == list_end) /* NEC does this */
401 mark = '@';
402 else
403 mark = token_mark(fotg210, hw->hw_token);
404 if (mark == '/') { /* qh_alt_next controls qh advance? */
405 if ((hw->hw_alt_next & QTD_MASK(fotg210))
406 == fotg210->async->hw->hw_alt_next)
407 mark = '#'; /* blocked */
408 else if (hw->hw_alt_next == list_end)
409 mark = '.'; /* use hw_qtd_next */
410 /* else alt_next points to some other qtd */
411 }
412 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
413 hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0;
414 temp = scnprintf(next, size,
415 "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
416 qh, scratch & 0x007f,
417 speed_char(scratch),
418 (scratch >> 8) & 0x000f,
419 scratch, hc32_to_cpup(fotg210, &hw->hw_info2),
420 hc32_to_cpup(fotg210, &hw->hw_token), mark,
421 (cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token)
422 ? "data1" : "data0",
423 (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
424 size -= temp;
425 next += temp;
426
427 /* hc may be modifying the list as we read it ... */
428 list_for_each_entry(td, &qh->qtd_list, qtd_list) {
429 scratch = hc32_to_cpup(fotg210, &td->hw_token);
430 mark = ' ';
431 if (hw_curr == td->qtd_dma)
432 mark = '*';
433 else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
434 mark = '+';
435 else if (QTD_LENGTH(scratch)) {
436 if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
437 mark = '#';
438 else if (td->hw_alt_next != list_end)
439 mark = '/';
440 }
441 temp = snprintf(next, size,
442 "\n\t%p%c%s len=%d %08x urb %p",
443 td, mark, ({ char *tmp;
444 switch ((scratch>>8)&0x03) {
445 case 0:
446 tmp = "out";
447 break;
448 case 1:
449 tmp = "in";
450 break;
451 case 2:
452 tmp = "setup";
453 break;
454 default:
455 tmp = "?";
456 break;
457 } tmp; }),
458 (scratch >> 16) & 0x7fff,
459 scratch,
460 td->urb);
461 if (size < temp)
462 temp = size;
463 size -= temp;
464 next += temp;
465 if (temp == size)
466 goto done;
467 }
468
469 temp = snprintf(next, size, "\n");
470 if (size < temp)
471 temp = size;
472 size -= temp;
473 next += temp;
474
475done:
476 *sizep = size;
477 *nextp = next;
478}
479
480static ssize_t fill_async_buffer(struct debug_buffer *buf)
481{
482 struct usb_hcd *hcd;
483 struct fotg210_hcd *fotg210;
484 unsigned long flags;
485 unsigned temp, size;
486 char *next;
487 struct fotg210_qh *qh;
488
489 hcd = bus_to_hcd(buf->bus);
490 fotg210 = hcd_to_fotg210(hcd);
491 next = buf->output_buf;
492 size = buf->alloc_size;
493
494 *next = 0;
495
496 /* dumps a snapshot of the async schedule.
497 * usually empty except for long-term bulk reads, or head.
498 * one QH per line, and TDs we know about
499 */
500 spin_lock_irqsave(&fotg210->lock, flags);
501 for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
502 qh = qh->qh_next.qh)
503 qh_lines(fotg210, qh, &next, &size);
504 if (fotg210->async_unlink && size > 0) {
505 temp = scnprintf(next, size, "\nunlink =\n");
506 size -= temp;
507 next += temp;
508
509 for (qh = fotg210->async_unlink; size > 0 && qh;
510 qh = qh->unlink_next)
511 qh_lines(fotg210, qh, &next, &size);
512 }
513 spin_unlock_irqrestore(&fotg210->lock, flags);
514
515 return strlen(buf->output_buf);
516}
517
518#define DBG_SCHED_LIMIT 64
519static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
520{
521 struct usb_hcd *hcd;
522 struct fotg210_hcd *fotg210;
523 unsigned long flags;
524 union fotg210_shadow p, *seen;
525 unsigned temp, size, seen_count;
526 char *next;
527 unsigned i;
528 __hc32 tag;
529
530 seen = kmalloc(DBG_SCHED_LIMIT * sizeof(*seen), GFP_ATOMIC);
531 if (!seen)
532 return 0;
533 seen_count = 0;
534
535 hcd = bus_to_hcd(buf->bus);
536 fotg210 = hcd_to_fotg210(hcd);
537 next = buf->output_buf;
538 size = buf->alloc_size;
539
540 temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
541 size -= temp;
542 next += temp;
543
544 /* dump a snapshot of the periodic schedule.
545 * iso changes, interrupt usually doesn't.
546 */
547 spin_lock_irqsave(&fotg210->lock, flags);
548 for (i = 0; i < fotg210->periodic_size; i++) {
549 p = fotg210->pshadow[i];
550 if (likely(!p.ptr))
551 continue;
552 tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);
553
554 temp = scnprintf(next, size, "%4d: ", i);
555 size -= temp;
556 next += temp;
557
558 do {
559 struct fotg210_qh_hw *hw;
560
561 switch (hc32_to_cpu(fotg210, tag)) {
562 case Q_TYPE_QH:
563 hw = p.qh->hw;
564 temp = scnprintf(next, size, " qh%d-%04x/%p",
565 p.qh->period,
566 hc32_to_cpup(fotg210,
567 &hw->hw_info2)
568 /* uframe masks */
569 & (QH_CMASK | QH_SMASK),
570 p.qh);
571 size -= temp;
572 next += temp;
573 /* don't repeat what follows this qh */
574 for (temp = 0; temp < seen_count; temp++) {
575 if (seen[temp].ptr != p.ptr)
576 continue;
577 if (p.qh->qh_next.ptr) {
578 temp = scnprintf(next, size,
579 " ...");
580 size -= temp;
581 next += temp;
582 }
583 break;
584 }
585 /* show more info the first time around */
586 if (temp == seen_count) {
587 u32 scratch = hc32_to_cpup(fotg210,
588 &hw->hw_info1);
589 struct fotg210_qtd *qtd;
590 char *type = "";
591
592 /* count tds, get ep direction */
593 temp = 0;
594 list_for_each_entry(qtd,
595 &p.qh->qtd_list,
596 qtd_list) {
597 temp++;
598 switch (0x03 & (hc32_to_cpu(
599 fotg210,
600 qtd->hw_token) >> 8)) {
601 case 0:
602 type = "out";
603 continue;
604 case 1:
605 type = "in";
606 continue;
607 }
608 }
609
610 temp = scnprintf(next, size,
611 "(%c%d ep%d%s "
612 "[%d/%d] q%d p%d)",
613 speed_char(scratch),
614 scratch & 0x007f,
615 (scratch >> 8) & 0x000f, type,
616 p.qh->usecs, p.qh->c_usecs,
617 temp,
618 0x7ff & (scratch >> 16));
619
620 if (seen_count < DBG_SCHED_LIMIT)
621 seen[seen_count++].qh = p.qh;
622 } else
623 temp = 0;
624 tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
625 p = p.qh->qh_next;
626 break;
627 case Q_TYPE_FSTN:
628 temp = scnprintf(next, size,
629 " fstn-%8x/%p", p.fstn->hw_prev,
630 p.fstn);
631 tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
632 p = p.fstn->fstn_next;
633 break;
634 case Q_TYPE_ITD:
635 temp = scnprintf(next, size,
636 " itd/%p", p.itd);
637 tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
638 p = p.itd->itd_next;
639 break;
640 }
641 size -= temp;
642 next += temp;
643 } while (p.ptr);
644
645 temp = scnprintf(next, size, "\n");
646 size -= temp;
647 next += temp;
648 }
649 spin_unlock_irqrestore(&fotg210->lock, flags);
650 kfree(seen);
651
652 return buf->alloc_size - size;
653}
654#undef DBG_SCHED_LIMIT
655
656static const char *rh_state_string(struct fotg210_hcd *fotg210)
657{
658 switch (fotg210->rh_state) {
659 case FOTG210_RH_HALTED:
660 return "halted";
661 case FOTG210_RH_SUSPENDED:
662 return "suspended";
663 case FOTG210_RH_RUNNING:
664 return "running";
665 case FOTG210_RH_STOPPING:
666 return "stopping";
667 }
668 return "?";
669}
670
671static ssize_t fill_registers_buffer(struct debug_buffer *buf)
672{
673 struct usb_hcd *hcd;
674 struct fotg210_hcd *fotg210;
675 unsigned long flags;
676 unsigned temp, size, i;
677 char *next, scratch[80];
678 static const char fmt[] = "%*s\n";
679 static const char label[] = "";
680
681 hcd = bus_to_hcd(buf->bus);
682 fotg210 = hcd_to_fotg210(hcd);
683 next = buf->output_buf;
684 size = buf->alloc_size;
685
686 spin_lock_irqsave(&fotg210->lock, flags);
687
688 if (!HCD_HW_ACCESSIBLE(hcd)) {
689 size = scnprintf(next, size,
690 "bus %s, device %s\n"
691 "%s\n"
692 "SUSPENDED(no register access)\n",
693 hcd->self.controller->bus->name,
694 dev_name(hcd->self.controller),
695 hcd->product_desc);
696 goto done;
697 }
698
699 /* Capability Registers */
700 i = HC_VERSION(fotg210, fotg210_readl(fotg210,
701 &fotg210->caps->hc_capbase));
702 temp = scnprintf(next, size,
703 "bus %s, device %s\n"
704 "%s\n"
705 "EHCI %x.%02x, rh state %s\n",
706 hcd->self.controller->bus->name,
707 dev_name(hcd->self.controller),
708 hcd->product_desc,
709 i >> 8, i & 0x0ff, rh_state_string(fotg210));
710 size -= temp;
711 next += temp;
712
713 /* FIXME interpret both types of params */
714 i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
715 temp = scnprintf(next, size, "structural params 0x%08x\n", i);
716 size -= temp;
717 next += temp;
718
719 i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
720 temp = scnprintf(next, size, "capability params 0x%08x\n", i);
721 size -= temp;
722 next += temp;
723
724 /* Operational Registers */
725 temp = dbg_status_buf(scratch, sizeof(scratch), label,
726 fotg210_readl(fotg210, &fotg210->regs->status));
727 temp = scnprintf(next, size, fmt, temp, scratch);
728 size -= temp;
729 next += temp;
730
731 temp = dbg_command_buf(scratch, sizeof(scratch), label,
732 fotg210_readl(fotg210, &fotg210->regs->command));
733 temp = scnprintf(next, size, fmt, temp, scratch);
734 size -= temp;
735 next += temp;
736
737 temp = dbg_intr_buf(scratch, sizeof(scratch), label,
738 fotg210_readl(fotg210, &fotg210->regs->intr_enable));
739 temp = scnprintf(next, size, fmt, temp, scratch);
740 size -= temp;
741 next += temp;
742
743 temp = scnprintf(next, size, "uframe %04x\n",
744 fotg210_read_frame_index(fotg210));
745 size -= temp;
746 next += temp;
747
748 if (fotg210->async_unlink) {
749 temp = scnprintf(next, size, "async unlink qh %p\n",
750 fotg210->async_unlink);
751 size -= temp;
752 next += temp;
753 }
754
755#ifdef FOTG210_STATS
756 temp = scnprintf(next, size,
757 "irq normal %ld err %ld iaa %ld(lost %ld)\n",
758 fotg210->stats.normal, fotg210->stats.error, fotg210->stats.iaa,
759 fotg210->stats.lost_iaa);
760 size -= temp;
761 next += temp;
762
763 temp = scnprintf(next, size, "complete %ld unlink %ld\n",
764 fotg210->stats.complete, fotg210->stats.unlink);
765 size -= temp;
766 next += temp;
767#endif
768
769done:
770 spin_unlock_irqrestore(&fotg210->lock, flags);
771
772 return buf->alloc_size - size;
773}
774
775static struct debug_buffer *alloc_buffer(struct usb_bus *bus,
776 ssize_t (*fill_func)(struct debug_buffer *))
777{
778 struct debug_buffer *buf;
779
780 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
781
782 if (buf) {
783 buf->bus = bus;
784 buf->fill_func = fill_func;
785 mutex_init(&buf->mutex);
786 buf->alloc_size = PAGE_SIZE;
787 }
788
789 return buf;
790}
791
792static int fill_buffer(struct debug_buffer *buf)
793{
794 int ret = 0;
795
796 if (!buf->output_buf)
797 buf->output_buf = vmalloc(buf->alloc_size);
798
799 if (!buf->output_buf) {
800 ret = -ENOMEM;
801 goto out;
802 }
803
804 ret = buf->fill_func(buf);
805
806 if (ret >= 0) {
807 buf->count = ret;
808 ret = 0;
809 }
810
811out:
812 return ret;
813}
814
815static ssize_t debug_output(struct file *file, char __user *user_buf,
816 size_t len, loff_t *offset)
817{
818 struct debug_buffer *buf = file->private_data;
819 int ret = 0;
820
821 mutex_lock(&buf->mutex);
822 if (buf->count == 0) {
823 ret = fill_buffer(buf);
824 if (ret != 0) {
825 mutex_unlock(&buf->mutex);
826 goto out;
827 }
828 }
829 mutex_unlock(&buf->mutex);
830
831 ret = simple_read_from_buffer(user_buf, len, offset,
832 buf->output_buf, buf->count);
833
834out:
835 return ret;
836
837}
838
839static int debug_close(struct inode *inode, struct file *file)
840{
841 struct debug_buffer *buf = file->private_data;
842
843 if (buf) {
844 vfree(buf->output_buf);
845 kfree(buf);
846 }
847
848 return 0;
849}
850static int debug_async_open(struct inode *inode, struct file *file)
851{
852 file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
853
854 return file->private_data ? 0 : -ENOMEM;
855}
856
857static int debug_periodic_open(struct inode *inode, struct file *file)
858{
859 struct debug_buffer *buf;
860 buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
861 if (!buf)
862 return -ENOMEM;
863
864 buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
865 file->private_data = buf;
866 return 0;
867}
868
869static int debug_registers_open(struct inode *inode, struct file *file)
870{
871 file->private_data = alloc_buffer(inode->i_private,
872 fill_registers_buffer);
873
874 return file->private_data ? 0 : -ENOMEM;
875}
876
877static inline void create_debug_files(struct fotg210_hcd *fotg210)
878{
879 struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
880
881 fotg210->debug_dir = debugfs_create_dir(bus->bus_name,
882 fotg210_debug_root);
883 if (!fotg210->debug_dir)
884 return;
885
886 if (!debugfs_create_file("async", S_IRUGO, fotg210->debug_dir, bus,
887 &debug_async_fops))
888 goto file_error;
889
890 if (!debugfs_create_file("periodic", S_IRUGO, fotg210->debug_dir, bus,
891 &debug_periodic_fops))
892 goto file_error;
893
894 if (!debugfs_create_file("registers", S_IRUGO, fotg210->debug_dir, bus,
895 &debug_registers_fops))
896 goto file_error;
897
898 return;
899
900file_error:
901 debugfs_remove_recursive(fotg210->debug_dir);
902}
903
904static inline void remove_debug_files(struct fotg210_hcd *fotg210)
905{
906 debugfs_remove_recursive(fotg210->debug_dir);
907}
908
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000909/*-------------------------------------------------------------------------*/
910
911/*
912 * handshake - spin reading hc until handshake completes or fails
913 * @ptr: address of hc register to be read
914 * @mask: bits to look at in result of read
915 * @done: value of those bits when handshake succeeds
916 * @usec: timeout in microseconds
917 *
918 * Returns negative errno, or zero on success
919 *
920 * Success happens when the "mask" bits have the specified value (hardware
921 * handshake done). There are two failure modes: "usec" have passed (major
922 * hardware flakeout), or the register reads as all-ones (hardware removed).
923 *
924 * That last failure should_only happen in cases like physical cardbus eject
925 * before driver shutdown. But it also seems to be caused by bugs in cardbus
926 * bridge shutdown: shutting down the bridge before the devices using it.
927 */
928static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
929 u32 mask, u32 done, int usec)
930{
931 u32 result;
932
933 do {
934 result = fotg210_readl(fotg210, ptr);
935 if (result == ~(u32)0) /* card removed */
936 return -ENODEV;
937 result &= mask;
938 if (result == done)
939 return 0;
940 udelay(1);
941 usec--;
942 } while (usec > 0);
943 return -ETIMEDOUT;
944}
945
946/*
947 * Force HC to halt state from unknown (EHCI spec section 2.3).
948 * Must be called with interrupts enabled and the lock not held.
949 */
950static int fotg210_halt(struct fotg210_hcd *fotg210)
951{
952 u32 temp;
953
954 spin_lock_irq(&fotg210->lock);
955
956 /* disable any irqs left enabled by previous code */
957 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
958
959 /*
960 * This routine gets called during probe before fotg210->command
961 * has been initialized, so we can't rely on its value.
962 */
963 fotg210->command &= ~CMD_RUN;
964 temp = fotg210_readl(fotg210, &fotg210->regs->command);
965 temp &= ~(CMD_RUN | CMD_IAAD);
966 fotg210_writel(fotg210, temp, &fotg210->regs->command);
967
968 spin_unlock_irq(&fotg210->lock);
969 synchronize_irq(fotg210_to_hcd(fotg210)->irq);
970
971 return handshake(fotg210, &fotg210->regs->status,
972 STS_HALT, STS_HALT, 16 * 125);
973}
974
975/*
976 * Reset a non-running (STS_HALT == 1) controller.
977 * Must be called with interrupts enabled and the lock not held.
978 */
979static int fotg210_reset(struct fotg210_hcd *fotg210)
980{
981 int retval;
982 u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
983
984 /* If the EHCI debug controller is active, special care must be
985 * taken before and after a host controller reset */
986 if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
987 fotg210->debug = NULL;
988
989 command |= CMD_RESET;
990 dbg_cmd(fotg210, "reset", command);
991 fotg210_writel(fotg210, command, &fotg210->regs->command);
992 fotg210->rh_state = FOTG210_RH_HALTED;
993 fotg210->next_statechange = jiffies;
994 retval = handshake(fotg210, &fotg210->regs->command,
995 CMD_RESET, 0, 250 * 1000);
996
997 if (retval)
998 return retval;
999
1000 if (fotg210->debug)
1001 dbgp_external_startup(fotg210_to_hcd(fotg210));
1002
1003 fotg210->port_c_suspend = fotg210->suspended_ports =
1004 fotg210->resuming_ports = 0;
1005 return retval;
1006}
1007
1008/*
1009 * Idle the controller (turn off the schedules).
1010 * Must be called with interrupts enabled and the lock not held.
1011 */
1012static void fotg210_quiesce(struct fotg210_hcd *fotg210)
1013{
1014 u32 temp;
1015
1016 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1017 return;
1018
1019 /* wait for any schedule enables/disables to take effect */
1020 temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
1021 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
1022 16 * 125);
1023
1024 /* then disable anything that's still active */
1025 spin_lock_irq(&fotg210->lock);
1026 fotg210->command &= ~(CMD_ASE | CMD_PSE);
1027 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1028 spin_unlock_irq(&fotg210->lock);
1029
1030 /* hardware can take 16 microframes to turn off ... */
1031 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
1032 16 * 125);
1033}
1034
1035/*-------------------------------------------------------------------------*/
1036
1037static void end_unlink_async(struct fotg210_hcd *fotg210);
1038static void unlink_empty_async(struct fotg210_hcd *fotg210);
1039static void fotg210_work(struct fotg210_hcd *fotg210);
1040static void start_unlink_intr(struct fotg210_hcd *fotg210,
1041 struct fotg210_qh *qh);
1042static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
1043
1044/*-------------------------------------------------------------------------*/
1045
1046/* Set a bit in the USBCMD register */
1047static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
1048{
1049 fotg210->command |= bit;
1050 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1051
1052 /* unblock posted write */
1053 fotg210_readl(fotg210, &fotg210->regs->command);
1054}
1055
1056/* Clear a bit in the USBCMD register */
1057static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
1058{
1059 fotg210->command &= ~bit;
1060 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1061
1062 /* unblock posted write */
1063 fotg210_readl(fotg210, &fotg210->regs->command);
1064}
1065
1066/*-------------------------------------------------------------------------*/
1067
1068/*
1069 * EHCI timer support... Now using hrtimers.
1070 *
1071 * Lots of different events are triggered from fotg210->hrtimer. Whenever
1072 * the timer routine runs, it checks each possible event; events that are
1073 * currently enabled and whose expiration time has passed get handled.
1074 * The set of enabled events is stored as a collection of bitflags in
1075 * fotg210->enabled_hrtimer_events, and they are numbered in order of
1076 * increasing delay values (ranging between 1 ms and 100 ms).
1077 *
1078 * Rather than implementing a sorted list or tree of all pending events,
1079 * we keep track only of the lowest-numbered pending event, in
1080 * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
1081 * expiration time is set to the timeout value for this event.
1082 *
1083 * As a result, events might not get handled right away; the actual delay
1084 * could be anywhere up to twice the requested delay. This doesn't
1085 * matter, because none of the events are especially time-critical. The
1086 * ones that matter most all have a delay of 1 ms, so they will be
1087 * handled after 2 ms at most, which is okay. In addition to this, we
1088 * allow for an expiration range of 1 ms.
1089 */
1090
1091/*
1092 * Delay lengths for the hrtimer event types.
1093 * Keep this list sorted by delay length, in the same order as
1094 * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
1095 */
1096static unsigned event_delays_ns[] = {
1097 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */
1098 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */
1099 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */
1100 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */
1101 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */
1102 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1103 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1104 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1105 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1106 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */
1107};
1108
1109/* Enable a pending hrtimer event */
1110static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
1111 bool resched)
1112{
1113 ktime_t *timeout = &fotg210->hr_timeouts[event];
1114
1115 if (resched)
1116 *timeout = ktime_add(ktime_get(),
1117 ktime_set(0, event_delays_ns[event]));
1118 fotg210->enabled_hrtimer_events |= (1 << event);
1119
1120 /* Track only the lowest-numbered pending event */
1121 if (event < fotg210->next_hrtimer_event) {
1122 fotg210->next_hrtimer_event = event;
1123 hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
1124 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
1125 }
1126}
1127
1128
1129/* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
1130static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
1131{
1132 unsigned actual, want;
1133
1134 /* Don't enable anything if the controller isn't running (e.g., died) */
1135 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1136 return;
1137
1138 want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
1139 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;
1140
1141 if (want != actual) {
1142
1143 /* Poll again later, but give up after about 20 ms */
1144 if (fotg210->ASS_poll_count++ < 20) {
1145 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
1146 true);
1147 return;
1148 }
1149 fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
1150 want, actual);
1151 }
1152 fotg210->ASS_poll_count = 0;
1153
1154 /* The status is up-to-date; restart or stop the schedule as needed */
1155 if (want == 0) { /* Stopped */
1156 if (fotg210->async_count > 0)
1157 fotg210_set_command_bit(fotg210, CMD_ASE);
1158
1159 } else { /* Running */
1160 if (fotg210->async_count == 0) {
1161
1162 /* Turn off the schedule after a while */
1163 fotg210_enable_event(fotg210,
1164 FOTG210_HRTIMER_DISABLE_ASYNC,
1165 true);
1166 }
1167 }
1168}
1169
1170/* Turn off the async schedule after a brief delay */
1171static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
1172{
1173 fotg210_clear_command_bit(fotg210, CMD_ASE);
1174}
1175
1176
1177/* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
1178static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
1179{
1180 unsigned actual, want;
1181
1182 /* Don't do anything if the controller isn't running (e.g., died) */
1183 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1184 return;
1185
1186 want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
1187 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;
1188
1189 if (want != actual) {
1190
1191 /* Poll again later, but give up after about 20 ms */
1192 if (fotg210->PSS_poll_count++ < 20) {
1193 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
1194 true);
1195 return;
1196 }
1197 fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
1198 want, actual);
1199 }
1200 fotg210->PSS_poll_count = 0;
1201
1202 /* The status is up-to-date; restart or stop the schedule as needed */
1203 if (want == 0) { /* Stopped */
1204 if (fotg210->periodic_count > 0)
1205 fotg210_set_command_bit(fotg210, CMD_PSE);
1206
1207 } else { /* Running */
1208 if (fotg210->periodic_count == 0) {
1209
1210 /* Turn off the schedule after a while */
1211 fotg210_enable_event(fotg210,
1212 FOTG210_HRTIMER_DISABLE_PERIODIC,
1213 true);
1214 }
1215 }
1216}
1217
1218/* Turn off the periodic schedule after a brief delay */
1219static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
1220{
1221 fotg210_clear_command_bit(fotg210, CMD_PSE);
1222}
1223
1224
1225/* Poll the STS_HALT status bit; see when a dead controller stops */
1226static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
1227{
1228 if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
1229
1230 /* Give up after a few milliseconds */
1231 if (fotg210->died_poll_count++ < 5) {
1232 /* Try again later */
1233 fotg210_enable_event(fotg210,
1234 FOTG210_HRTIMER_POLL_DEAD, true);
1235 return;
1236 }
1237 fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
1238 }
1239
1240 /* Clean up the mess */
1241 fotg210->rh_state = FOTG210_RH_HALTED;
1242 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
1243 fotg210_work(fotg210);
1244 end_unlink_async(fotg210);
1245
1246 /* Not in process context, so don't try to reset the controller */
1247}
1248
1249
1250/* Handle unlinked interrupt QHs once they are gone from the hardware */
1251static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
1252{
1253 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
1254
1255 /*
1256 * Process all the QHs on the intr_unlink list that were added
1257 * before the current unlink cycle began. The list is in
1258 * temporal order, so stop when we reach the first entry in the
1259 * current cycle. But if the root hub isn't running then
1260 * process all the QHs on the list.
1261 */
1262 fotg210->intr_unlinking = true;
1263 while (fotg210->intr_unlink) {
1264 struct fotg210_qh *qh = fotg210->intr_unlink;
1265
1266 if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
1267 break;
1268 fotg210->intr_unlink = qh->unlink_next;
1269 qh->unlink_next = NULL;
1270 end_unlink_intr(fotg210, qh);
1271 }
1272
1273 /* Handle remaining entries later */
1274 if (fotg210->intr_unlink) {
1275 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
1276 true);
1277 ++fotg210->intr_unlink_cycle;
1278 }
1279 fotg210->intr_unlinking = false;
1280}
1281
1282
1283/* Start another free-iTDs/siTDs cycle */
1284static void start_free_itds(struct fotg210_hcd *fotg210)
1285{
1286 if (!(fotg210->enabled_hrtimer_events &
1287 BIT(FOTG210_HRTIMER_FREE_ITDS))) {
1288 fotg210->last_itd_to_free = list_entry(
1289 fotg210->cached_itd_list.prev,
1290 struct fotg210_itd, itd_list);
1291 fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
1292 }
1293}
1294
1295/* Wait for controller to stop using old iTDs and siTDs */
1296static void end_free_itds(struct fotg210_hcd *fotg210)
1297{
1298 struct fotg210_itd *itd, *n;
1299
1300 if (fotg210->rh_state < FOTG210_RH_RUNNING)
1301 fotg210->last_itd_to_free = NULL;
1302
1303 list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
1304 list_del(&itd->itd_list);
1305 dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
1306 if (itd == fotg210->last_itd_to_free)
1307 break;
1308 }
1309
1310 if (!list_empty(&fotg210->cached_itd_list))
1311 start_free_itds(fotg210);
1312}
1313
1314
1315/* Handle lost (or very late) IAA interrupts */
1316static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
1317{
1318 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1319 return;
1320
1321 /*
1322 * Lost IAA irqs wedge things badly; seen first with a vt8235.
1323 * So we need this watchdog, but must protect it against both
1324 * (a) SMP races against real IAA firing and retriggering, and
1325 * (b) clean HC shutdown, when IAA watchdog was pending.
1326 */
1327 if (fotg210->async_iaa) {
1328 u32 cmd, status;
1329
1330 /* If we get here, IAA is *REALLY* late. It's barely
1331 * conceivable that the system is so busy that CMD_IAAD
1332 * is still legitimately set, so let's be sure it's
1333 * clear before we read STS_IAA. (The HC should clear
1334 * CMD_IAAD when it sets STS_IAA.)
1335 */
1336 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
1337
1338 /*
1339 * If IAA is set here it either legitimately triggered
1340 * after the watchdog timer expired (_way_ late, so we'll
1341 * still count it as lost) ... or a silicon erratum:
1342 * - VIA seems to set IAA without triggering the IRQ;
1343 * - IAAD potentially cleared without setting IAA.
1344 */
1345 status = fotg210_readl(fotg210, &fotg210->regs->status);
1346 if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
1347 COUNT(fotg210->stats.lost_iaa);
1348 fotg210_writel(fotg210, STS_IAA,
1349 &fotg210->regs->status);
1350 }
1351
1352 fotg210_vdbg(fotg210, "IAA watchdog: status %x cmd %x\n",
1353 status, cmd);
1354 end_unlink_async(fotg210);
1355 }
1356}
1357
1358
1359/* Enable the I/O watchdog, if appropriate */
1360static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
1361{
1362 /* Not needed if the controller isn't running or it's already enabled */
1363 if (fotg210->rh_state != FOTG210_RH_RUNNING ||
1364 (fotg210->enabled_hrtimer_events &
1365 BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
1366 return;
1367
1368 /*
1369 * Isochronous transfers always need the watchdog.
1370 * For other sorts we use it only if the flag is set.
1371 */
1372 if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog &&
1373 fotg210->async_count + fotg210->intr_count > 0))
1374 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG,
1375 true);
1376}
1377
1378
1379/*
1380 * Handler functions for the hrtimer event types.
1381 * Keep this array in the same order as the event types indexed by
1382 * enum fotg210_hrtimer_event in fotg210.h.
1383 */
1384static void (*event_handlers[])(struct fotg210_hcd *) = {
1385 fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */
1386 fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */
1387 fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */
1388 fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */
1389 end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */
1390 unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1391 fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1392 fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1393 fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1394 fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */
1395};
1396
1397static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
1398{
1399 struct fotg210_hcd *fotg210 =
1400 container_of(t, struct fotg210_hcd, hrtimer);
1401 ktime_t now;
1402 unsigned long events;
1403 unsigned long flags;
1404 unsigned e;
1405
1406 spin_lock_irqsave(&fotg210->lock, flags);
1407
1408 events = fotg210->enabled_hrtimer_events;
1409 fotg210->enabled_hrtimer_events = 0;
1410 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
1411
1412 /*
1413 * Check each pending event. If its time has expired, handle
1414 * the event; otherwise re-enable it.
1415 */
1416 now = ktime_get();
1417 for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) {
1418 if (now.tv64 >= fotg210->hr_timeouts[e].tv64)
1419 event_handlers[e](fotg210);
1420 else
1421 fotg210_enable_event(fotg210, e, false);
1422 }
1423
1424 spin_unlock_irqrestore(&fotg210->lock, flags);
1425 return HRTIMER_NORESTART;
1426}
1427
1428/*-------------------------------------------------------------------------*/
1429
1430#define fotg210_bus_suspend NULL
1431#define fotg210_bus_resume NULL
1432
1433/*-------------------------------------------------------------------------*/
1434
1435static int check_reset_complete(
1436 struct fotg210_hcd *fotg210,
1437 int index,
1438 u32 __iomem *status_reg,
1439 int port_status
1440) {
1441 if (!(port_status & PORT_CONNECT))
1442 return port_status;
1443
1444 /* if reset finished and it's still not enabled -- handoff */
1445 if (!(port_status & PORT_PE)) {
1446 /* with integrated TT, there's nobody to hand it to! */
1447 fotg210_dbg(fotg210,
1448 "Failed to enable port %d on root hub TT\n",
1449 index+1);
1450 return port_status;
1451 } else {
1452 fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
1453 index + 1);
1454 }
1455
1456 return port_status;
1457}
1458
1459/*-------------------------------------------------------------------------*/
1460
1461
1462/* build "status change" packet (one or two bytes) from HC registers */
1463
1464static int
1465fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
1466{
1467 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
1468 u32 temp, status;
1469 u32 mask;
1470 int retval = 1;
1471 unsigned long flags;
1472
1473 /* init status to no-changes */
1474 buf[0] = 0;
1475
1476 /* Inform the core about resumes-in-progress by returning
1477 * a non-zero value even if there are no status changes.
1478 */
1479 status = fotg210->resuming_ports;
1480
1481 mask = PORT_CSC | PORT_PEC;
1482 /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
1483
1484 /* no hub change reports (bit 0) for now (power, ...) */
1485
1486 /* port N changes (bit N)? */
1487 spin_lock_irqsave(&fotg210->lock, flags);
1488
1489 temp = fotg210_readl(fotg210, &fotg210->regs->port_status);
1490
1491 /*
1492 * Return status information even for ports with OWNER set.
1493 * Otherwise khubd wouldn't see the disconnect event when a
1494 * high-speed device is switched over to the companion
1495 * controller by the user.
1496 */
1497
1498 if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend)
1499 || (fotg210->reset_done[0] && time_after_eq(
1500 jiffies, fotg210->reset_done[0]))) {
1501 buf[0] |= 1 << 1;
1502 status = STS_PCD;
1503 }
1504 /* FIXME autosuspend idle root hubs */
1505 spin_unlock_irqrestore(&fotg210->lock, flags);
1506 return status ? retval : 0;
1507}
1508
1509/*-------------------------------------------------------------------------*/
1510
1511static void
1512fotg210_hub_descriptor(
1513 struct fotg210_hcd *fotg210,
1514 struct usb_hub_descriptor *desc
1515) {
1516 int ports = HCS_N_PORTS(fotg210->hcs_params);
1517 u16 temp;
1518
1519 desc->bDescriptorType = 0x29;
1520 desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */
1521 desc->bHubContrCurrent = 0;
1522
1523 desc->bNbrPorts = ports;
1524 temp = 1 + (ports / 8);
1525 desc->bDescLength = 7 + 2 * temp;
1526
1527 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1528 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1529 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
1530
1531 temp = 0x0008; /* per-port overcurrent reporting */
1532 temp |= 0x0002; /* no power switching */
1533 desc->wHubCharacteristics = cpu_to_le16(temp);
1534}
1535
1536/*-------------------------------------------------------------------------*/
1537
1538static int fotg210_hub_control(
1539 struct usb_hcd *hcd,
1540 u16 typeReq,
1541 u16 wValue,
1542 u16 wIndex,
1543 char *buf,
1544 u16 wLength
1545) {
1546 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
1547 int ports = HCS_N_PORTS(fotg210->hcs_params);
1548 u32 __iomem *status_reg = &fotg210->regs->port_status;
1549 u32 temp, temp1, status;
1550 unsigned long flags;
1551 int retval = 0;
1552 unsigned selector;
1553
1554 /*
1555 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1556 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1557 * (track current state ourselves) ... blink for diagnostics,
1558 * power, "this is the one", etc. EHCI spec supports this.
1559 */
1560
1561 spin_lock_irqsave(&fotg210->lock, flags);
1562 switch (typeReq) {
1563 case ClearHubFeature:
1564 switch (wValue) {
1565 case C_HUB_LOCAL_POWER:
1566 case C_HUB_OVER_CURRENT:
1567 /* no hub-wide feature/status flags */
1568 break;
1569 default:
1570 goto error;
1571 }
1572 break;
1573 case ClearPortFeature:
1574 if (!wIndex || wIndex > ports)
1575 goto error;
1576 wIndex--;
1577 temp = fotg210_readl(fotg210, status_reg);
1578 temp &= ~PORT_RWC_BITS;
1579
1580 /*
1581 * Even if OWNER is set, so the port is owned by the
1582 * companion controller, khubd needs to be able to clear
1583 * the port-change status bits (especially
1584 * USB_PORT_STAT_C_CONNECTION).
1585 */
1586
1587 switch (wValue) {
1588 case USB_PORT_FEAT_ENABLE:
1589 fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
1590 break;
1591 case USB_PORT_FEAT_C_ENABLE:
1592 fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
1593 break;
1594 case USB_PORT_FEAT_SUSPEND:
1595 if (temp & PORT_RESET)
1596 goto error;
1597 if (!(temp & PORT_SUSPEND))
1598 break;
1599 if ((temp & PORT_PE) == 0)
1600 goto error;
1601
1602 /* resume signaling for 20 msec */
1603 fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
1604 fotg210->reset_done[wIndex] = jiffies
1605 + msecs_to_jiffies(20);
1606 break;
1607 case USB_PORT_FEAT_C_SUSPEND:
1608 clear_bit(wIndex, &fotg210->port_c_suspend);
1609 break;
1610 case USB_PORT_FEAT_C_CONNECTION:
1611 fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
1612 break;
1613 case USB_PORT_FEAT_C_OVER_CURRENT:
1614 fotg210_writel(fotg210, temp | OTGISR_OVC,
1615 &fotg210->regs->otgisr);
1616 break;
1617 case USB_PORT_FEAT_C_RESET:
1618 /* GetPortStatus clears reset */
1619 break;
1620 default:
1621 goto error;
1622 }
1623 fotg210_readl(fotg210, &fotg210->regs->command);
1624 break;
1625 case GetHubDescriptor:
1626 fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
1627 buf);
1628 break;
1629 case GetHubStatus:
1630 /* no hub-wide feature/status flags */
1631 memset(buf, 0, 4);
1632 /*cpu_to_le32s ((u32 *) buf); */
1633 break;
1634 case GetPortStatus:
1635 if (!wIndex || wIndex > ports)
1636 goto error;
1637 wIndex--;
1638 status = 0;
1639 temp = fotg210_readl(fotg210, status_reg);
1640
1641 /* wPortChange bits */
1642 if (temp & PORT_CSC)
1643 status |= USB_PORT_STAT_C_CONNECTION << 16;
1644 if (temp & PORT_PEC)
1645 status |= USB_PORT_STAT_C_ENABLE << 16;
1646
1647 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
1648 if (temp1 & OTGISR_OVC)
1649 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1650
1651 /* whoever resumes must GetPortStatus to complete it!! */
1652 if (temp & PORT_RESUME) {
1653
1654 /* Remote Wakeup received? */
1655 if (!fotg210->reset_done[wIndex]) {
1656 /* resume signaling for 20 msec */
1657 fotg210->reset_done[wIndex] = jiffies
1658 + msecs_to_jiffies(20);
1659 /* check the port again */
1660 mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
1661 fotg210->reset_done[wIndex]);
1662 }
1663
1664 /* resume completed? */
1665 else if (time_after_eq(jiffies,
1666 fotg210->reset_done[wIndex])) {
1667 clear_bit(wIndex, &fotg210->suspended_ports);
1668 set_bit(wIndex, &fotg210->port_c_suspend);
1669 fotg210->reset_done[wIndex] = 0;
1670
1671 /* stop resume signaling */
1672 temp = fotg210_readl(fotg210, status_reg);
1673 fotg210_writel(fotg210,
1674 temp & ~(PORT_RWC_BITS | PORT_RESUME),
1675 status_reg);
1676 clear_bit(wIndex, &fotg210->resuming_ports);
1677 retval = handshake(fotg210, status_reg,
1678 PORT_RESUME, 0, 2000 /* 2msec */);
1679 if (retval != 0) {
1680 fotg210_err(fotg210,
1681 "port %d resume error %d\n",
1682 wIndex + 1, retval);
1683 goto error;
1684 }
1685 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1686 }
1687 }
1688
1689 /* whoever resets must GetPortStatus to complete it!! */
1690 if ((temp & PORT_RESET)
1691 && time_after_eq(jiffies,
1692 fotg210->reset_done[wIndex])) {
1693 status |= USB_PORT_STAT_C_RESET << 16;
1694 fotg210->reset_done[wIndex] = 0;
1695 clear_bit(wIndex, &fotg210->resuming_ports);
1696
1697 /* force reset to complete */
1698 fotg210_writel(fotg210,
1699 temp & ~(PORT_RWC_BITS | PORT_RESET),
1700 status_reg);
1701 /* REVISIT: some hardware needs 550+ usec to clear
1702 * this bit; seems too long to spin routinely...
1703 */
1704 retval = handshake(fotg210, status_reg,
1705 PORT_RESET, 0, 1000);
1706 if (retval != 0) {
1707 fotg210_err(fotg210, "port %d reset error %d\n",
1708 wIndex + 1, retval);
1709 goto error;
1710 }
1711
1712 /* see what we found out */
1713 temp = check_reset_complete(fotg210, wIndex, status_reg,
1714 fotg210_readl(fotg210, status_reg));
1715 }
1716
1717 if (!(temp & (PORT_RESUME|PORT_RESET))) {
1718 fotg210->reset_done[wIndex] = 0;
1719 clear_bit(wIndex, &fotg210->resuming_ports);
1720 }
1721
1722 /* transfer dedicated ports to the companion hc */
1723 if ((temp & PORT_CONNECT) &&
1724 test_bit(wIndex, &fotg210->companion_ports)) {
1725 temp &= ~PORT_RWC_BITS;
1726 fotg210_writel(fotg210, temp, status_reg);
1727 fotg210_dbg(fotg210, "port %d --> companion\n",
1728 wIndex + 1);
1729 temp = fotg210_readl(fotg210, status_reg);
1730 }
1731
1732 /*
1733 * Even if OWNER is set, there's no harm letting khubd
1734 * see the wPortStatus values (they should all be 0 except
1735 * for PORT_POWER anyway).
1736 */
1737
1738 if (temp & PORT_CONNECT) {
1739 status |= USB_PORT_STAT_CONNECTION;
1740 status |= fotg210_port_speed(fotg210, temp);
1741 }
1742 if (temp & PORT_PE)
1743 status |= USB_PORT_STAT_ENABLE;
1744
1745 /* maybe the port was unsuspended without our knowledge */
1746 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1747 status |= USB_PORT_STAT_SUSPEND;
1748 } else if (test_bit(wIndex, &fotg210->suspended_ports)) {
1749 clear_bit(wIndex, &fotg210->suspended_ports);
1750 clear_bit(wIndex, &fotg210->resuming_ports);
1751 fotg210->reset_done[wIndex] = 0;
1752 if (temp & PORT_PE)
1753 set_bit(wIndex, &fotg210->port_c_suspend);
1754 }
1755
1756 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
1757 if (temp1 & OTGISR_OVC)
1758 status |= USB_PORT_STAT_OVERCURRENT;
1759 if (temp & PORT_RESET)
1760 status |= USB_PORT_STAT_RESET;
1761 if (test_bit(wIndex, &fotg210->port_c_suspend))
1762 status |= USB_PORT_STAT_C_SUSPEND << 16;
1763
1764#ifndef VERBOSE_DEBUG
1765 if (status & ~0xffff) /* only if wPortChange is interesting */
1766#endif
1767 dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
1768 put_unaligned_le32(status, buf);
1769 break;
1770 case SetHubFeature:
1771 switch (wValue) {
1772 case C_HUB_LOCAL_POWER:
1773 case C_HUB_OVER_CURRENT:
1774 /* no hub-wide feature/status flags */
1775 break;
1776 default:
1777 goto error;
1778 }
1779 break;
1780 case SetPortFeature:
1781 selector = wIndex >> 8;
1782 wIndex &= 0xff;
1783
1784 if (!wIndex || wIndex > ports)
1785 goto error;
1786 wIndex--;
1787 temp = fotg210_readl(fotg210, status_reg);
1788 temp &= ~PORT_RWC_BITS;
1789 switch (wValue) {
1790 case USB_PORT_FEAT_SUSPEND:
1791 if ((temp & PORT_PE) == 0
1792 || (temp & PORT_RESET) != 0)
1793 goto error;
1794
1795 /* After above check the port must be connected.
1796 * Set appropriate bit thus could put phy into low power
1797 * mode if we have hostpc feature
1798 */
1799 fotg210_writel(fotg210, temp | PORT_SUSPEND,
1800 status_reg);
1801 set_bit(wIndex, &fotg210->suspended_ports);
1802 break;
1803 case USB_PORT_FEAT_RESET:
1804 if (temp & PORT_RESUME)
1805 goto error;
1806 /* line status bits may report this as low speed,
1807 * which can be fine if this root hub has a
1808 * transaction translator built in.
1809 */
1810 fotg210_vdbg(fotg210, "port %d reset\n", wIndex + 1);
1811 temp |= PORT_RESET;
1812 temp &= ~PORT_PE;
1813
1814 /*
1815 * caller must wait, then call GetPortStatus
1816 * usb 2.0 spec says 50 ms resets on root
1817 */
1818 fotg210->reset_done[wIndex] = jiffies
1819 + msecs_to_jiffies(50);
1820 fotg210_writel(fotg210, temp, status_reg);
1821 break;
1822
1823 /* For downstream facing ports (these): one hub port is put
1824 * into test mode according to USB2 11.24.2.13, then the hub
1825 * must be reset (which for root hub now means rmmod+modprobe,
1826 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1827 * about the EHCI-specific stuff.
1828 */
1829 case USB_PORT_FEAT_TEST:
1830 if (!selector || selector > 5)
1831 goto error;
1832 spin_unlock_irqrestore(&fotg210->lock, flags);
1833 fotg210_quiesce(fotg210);
1834 spin_lock_irqsave(&fotg210->lock, flags);
1835
1836 /* Put all enabled ports into suspend */
1837 temp = fotg210_readl(fotg210, status_reg) &
1838 ~PORT_RWC_BITS;
1839 if (temp & PORT_PE)
1840 fotg210_writel(fotg210, temp | PORT_SUSPEND,
1841 status_reg);
1842
1843 spin_unlock_irqrestore(&fotg210->lock, flags);
1844 fotg210_halt(fotg210);
1845 spin_lock_irqsave(&fotg210->lock, flags);
1846
1847 temp = fotg210_readl(fotg210, status_reg);
1848 temp |= selector << 16;
1849 fotg210_writel(fotg210, temp, status_reg);
1850 break;
1851
1852 default:
1853 goto error;
1854 }
1855 fotg210_readl(fotg210, &fotg210->regs->command);
1856 break;
1857
1858 default:
1859error:
1860 /* "stall" on error */
1861 retval = -EPIPE;
1862 }
1863 spin_unlock_irqrestore(&fotg210->lock, flags);
1864 return retval;
1865}
1866
1867static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
1868 int portnum)
1869{
1870 return;
1871}
1872
1873static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
1874 int portnum)
1875{
1876 return 0;
1877}
1878/*-------------------------------------------------------------------------*/
1879/*
1880 * There's basically three types of memory:
1881 * - data used only by the HCD ... kmalloc is fine
1882 * - async and periodic schedules, shared by HC and HCD ... these
1883 * need to use dma_pool or dma_alloc_coherent
1884 * - driver buffers, read/written by HC ... single shot DMA mapped
1885 *
1886 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
1887 * No memory seen by this driver is pageable.
1888 */
1889
1890/*-------------------------------------------------------------------------*/
1891
1892/* Allocate the key transfer structures from the previously allocated pool */
1893
1894static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
1895 struct fotg210_qtd *qtd, dma_addr_t dma)
1896{
1897 memset(qtd, 0, sizeof(*qtd));
1898 qtd->qtd_dma = dma;
1899 qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
1900 qtd->hw_next = FOTG210_LIST_END(fotg210);
1901 qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
1902 INIT_LIST_HEAD(&qtd->qtd_list);
1903}
1904
1905static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
1906 gfp_t flags)
1907{
1908 struct fotg210_qtd *qtd;
1909 dma_addr_t dma;
1910
1911 qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
1912 if (qtd != NULL)
1913 fotg210_qtd_init(fotg210, qtd, dma);
1914
1915 return qtd;
1916}
1917
1918static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
1919 struct fotg210_qtd *qtd)
1920{
1921 dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
1922}
1923
1924
1925static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
1926{
1927 /* clean qtds first, and know this is not linked */
1928 if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
1929 fotg210_dbg(fotg210, "unused qh not empty!\n");
1930 BUG();
1931 }
1932 if (qh->dummy)
1933 fotg210_qtd_free(fotg210, qh->dummy);
1934 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1935 kfree(qh);
1936}
1937
1938static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
1939 gfp_t flags)
1940{
1941 struct fotg210_qh *qh;
1942 dma_addr_t dma;
1943
1944 qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
1945 if (!qh)
1946 goto done;
1947 qh->hw = (struct fotg210_qh_hw *)
1948 dma_pool_alloc(fotg210->qh_pool, flags, &dma);
1949 if (!qh->hw)
1950 goto fail;
1951 memset(qh->hw, 0, sizeof(*qh->hw));
1952 qh->qh_dma = dma;
1953 INIT_LIST_HEAD(&qh->qtd_list);
1954
1955 /* dummy td enables safe urb queuing */
1956 qh->dummy = fotg210_qtd_alloc(fotg210, flags);
1957 if (qh->dummy == NULL) {
1958 fotg210_dbg(fotg210, "no dummy td\n");
1959 goto fail1;
1960 }
1961done:
1962 return qh;
1963fail1:
1964 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1965fail:
1966 kfree(qh);
1967 return NULL;
1968}
1969
1970/*-------------------------------------------------------------------------*/
1971
1972/* The queue heads and transfer descriptors are managed from pools tied
1973 * to each of the "per device" structures.
1974 * This is the initialisation and cleanup code.
1975 */
1976
1977static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
1978{
1979 if (fotg210->async)
1980 qh_destroy(fotg210, fotg210->async);
1981 fotg210->async = NULL;
1982
1983 if (fotg210->dummy)
1984 qh_destroy(fotg210, fotg210->dummy);
1985 fotg210->dummy = NULL;
1986
1987 /* DMA consistent memory and pools */
1988 if (fotg210->qtd_pool)
1989 dma_pool_destroy(fotg210->qtd_pool);
1990 fotg210->qtd_pool = NULL;
1991
1992 if (fotg210->qh_pool) {
1993 dma_pool_destroy(fotg210->qh_pool);
1994 fotg210->qh_pool = NULL;
1995 }
1996
1997 if (fotg210->itd_pool)
1998 dma_pool_destroy(fotg210->itd_pool);
1999 fotg210->itd_pool = NULL;
2000
2001 if (fotg210->periodic)
2002 dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller,
2003 fotg210->periodic_size * sizeof(u32),
2004 fotg210->periodic, fotg210->periodic_dma);
2005 fotg210->periodic = NULL;
2006
2007 /* shadow periodic table */
2008 kfree(fotg210->pshadow);
2009 fotg210->pshadow = NULL;
2010}
2011
2012/* remember to add cleanup code (above) if you add anything here */
2013static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
2014{
2015 int i;
2016
2017 /* QTDs for control/bulk/intr transfers */
2018 fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
2019 fotg210_to_hcd(fotg210)->self.controller,
2020 sizeof(struct fotg210_qtd),
2021 32 /* byte alignment (for hw parts) */,
2022 4096 /* can't cross 4K */);
2023 if (!fotg210->qtd_pool)
2024 goto fail;
2025
2026 /* QHs for control/bulk/intr transfers */
2027 fotg210->qh_pool = dma_pool_create("fotg210_qh",
2028 fotg210_to_hcd(fotg210)->self.controller,
2029 sizeof(struct fotg210_qh_hw),
2030 32 /* byte alignment (for hw parts) */,
2031 4096 /* can't cross 4K */);
2032 if (!fotg210->qh_pool)
2033 goto fail;
2034
2035 fotg210->async = fotg210_qh_alloc(fotg210, flags);
2036 if (!fotg210->async)
2037 goto fail;
2038
2039 /* ITD for high speed ISO transfers */
2040 fotg210->itd_pool = dma_pool_create("fotg210_itd",
2041 fotg210_to_hcd(fotg210)->self.controller,
2042 sizeof(struct fotg210_itd),
2043 64 /* byte alignment (for hw parts) */,
2044 4096 /* can't cross 4K */);
2045 if (!fotg210->itd_pool)
2046 goto fail;
2047
2048 /* Hardware periodic table */
2049 fotg210->periodic = (__le32 *)
2050 dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller,
2051 fotg210->periodic_size * sizeof(__le32),
2052 &fotg210->periodic_dma, 0);
2053 if (fotg210->periodic == NULL)
2054 goto fail;
2055
2056 for (i = 0; i < fotg210->periodic_size; i++)
2057 fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
2058
2059 /* software shadow of hardware table */
2060 fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
2061 flags);
2062 if (fotg210->pshadow != NULL)
2063 return 0;
2064
2065fail:
2066 fotg210_dbg(fotg210, "couldn't init memory\n");
2067 fotg210_mem_cleanup(fotg210);
2068 return -ENOMEM;
2069}
2070/*-------------------------------------------------------------------------*/
2071/*
2072 * EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
2073 *
2074 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
2075 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
2076 * buffers needed for the larger number). We use one QH per endpoint, queue
2077 * multiple urbs (all three types) per endpoint. URBs may need several qtds.
2078 *
2079 * ISO traffic uses "ISO TD" (itd) records, and (along with
2080 * interrupts) needs careful scheduling. Performance improvements can be
2081 * an ongoing challenge. That's in "ehci-sched.c".
2082 *
2083 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
2084 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
2085 * (b) special fields in qh entries or (c) split iso entries. TTs will
2086 * buffer low/full speed data so the host collects it at high speed.
2087 */
2088
2089/*-------------------------------------------------------------------------*/
2090
2091/* fill a qtd, returning how much of the buffer we were able to queue up */
2092
2093static int
2094qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd, dma_addr_t buf,
2095 size_t len, int token, int maxpacket)
2096{
2097 int i, count;
2098 u64 addr = buf;
2099
2100 /* one buffer entry per 4K ... first might be short or unaligned */
2101 qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr);
2102 qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32));
2103 count = 0x1000 - (buf & 0x0fff); /* rest of that page */
2104 if (likely(len < count)) /* ... iff needed */
2105 count = len;
2106 else {
2107 buf += 0x1000;
2108 buf &= ~0x0fff;
2109
2110 /* per-qtd limit: from 16K to 20K (best alignment) */
2111 for (i = 1; count < len && i < 5; i++) {
2112 addr = buf;
2113 qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
2114 qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
2115 (u32)(addr >> 32));
2116 buf += 0x1000;
2117 if ((count + 0x1000) < len)
2118 count += 0x1000;
2119 else
2120 count = len;
2121 }
2122
2123 /* short packets may only terminate transfers */
2124 if (count != len)
2125 count -= (count % maxpacket);
2126 }
2127 qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
2128 qtd->length = count;
2129
2130 return count;
2131}
2132
2133/*-------------------------------------------------------------------------*/
2134
2135static inline void
2136qh_update(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
2137 struct fotg210_qtd *qtd)
2138{
2139 struct fotg210_qh_hw *hw = qh->hw;
2140
2141 /* writes to an active overlay are unsafe */
2142 BUG_ON(qh->qh_state != QH_STATE_IDLE);
2143
2144 hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2145 hw->hw_alt_next = FOTG210_LIST_END(fotg210);
2146
2147 /* Except for control endpoints, we make hardware maintain data
2148 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
2149 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
2150 * ever clear it.
2151 */
2152 if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
2153 unsigned is_out, epnum;
2154
2155 is_out = qh->is_out;
2156 epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f;
2157 if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
2158 hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE);
2159 usb_settoggle(qh->dev, epnum, is_out, 1);
2160 }
2161 }
2162
2163 hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
2164}
2165
2166/* if it weren't for a common silicon quirk (writing the dummy into the qh
2167 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
2168 * recovery (including urb dequeue) would need software changes to a QH...
2169 */
2170static void
2171qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
2172{
2173 struct fotg210_qtd *qtd;
2174
2175 if (list_empty(&qh->qtd_list))
2176 qtd = qh->dummy;
2177 else {
2178 qtd = list_entry(qh->qtd_list.next,
2179 struct fotg210_qtd, qtd_list);
2180 /*
2181 * first qtd may already be partially processed.
2182 * If we come here during unlink, the QH overlay region
2183 * might have reference to the just unlinked qtd. The
2184 * qtd is updated in qh_completions(). Update the QH
2185 * overlay here.
2186 */
2187 if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
2188 qh->hw->hw_qtd_next = qtd->hw_next;
2189 qtd = NULL;
2190 }
2191 }
2192
2193 if (qtd)
2194 qh_update(fotg210, qh, qtd);
2195}
2196
2197/*-------------------------------------------------------------------------*/
2198
2199static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2200
2201static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
2202 struct usb_host_endpoint *ep)
2203{
2204 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
2205 struct fotg210_qh *qh = ep->hcpriv;
2206 unsigned long flags;
2207
2208 spin_lock_irqsave(&fotg210->lock, flags);
2209 qh->clearing_tt = 0;
2210 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
2211 && fotg210->rh_state == FOTG210_RH_RUNNING)
2212 qh_link_async(fotg210, qh);
2213 spin_unlock_irqrestore(&fotg210->lock, flags);
2214}
2215
2216static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
2217 struct fotg210_qh *qh,
2218 struct urb *urb, u32 token)
2219{
2220
2221 /* If an async split transaction gets an error or is unlinked,
2222 * the TT buffer may be left in an indeterminate state. We
2223 * have to clear the TT buffer.
2224 *
2225 * Note: this routine is never called for Isochronous transfers.
2226 */
2227 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
2228#ifdef DEBUG
2229 struct usb_device *tt = urb->dev->tt->hub;
2230 dev_dbg(&tt->dev,
2231 "clear tt buffer port %d, a%d ep%d t%08x\n",
2232 urb->dev->ttport, urb->dev->devnum,
2233 usb_pipeendpoint(urb->pipe), token);
2234#endif /* DEBUG */
2235 if (urb->dev->tt->hub !=
2236 fotg210_to_hcd(fotg210)->self.root_hub) {
2237 if (usb_hub_clear_tt_buffer(urb) == 0)
2238 qh->clearing_tt = 1;
2239 }
2240 }
2241}
2242
2243static int qtd_copy_status(
2244 struct fotg210_hcd *fotg210,
2245 struct urb *urb,
2246 size_t length,
2247 u32 token
2248)
2249{
2250 int status = -EINPROGRESS;
2251
2252 /* count IN/OUT bytes, not SETUP (even short packets) */
2253 if (likely(QTD_PID(token) != 2))
2254 urb->actual_length += length - QTD_LENGTH(token);
2255
2256 /* don't modify error codes */
2257 if (unlikely(urb->unlinked))
2258 return status;
2259
2260 /* force cleanup after short read; not always an error */
2261 if (unlikely(IS_SHORT_READ(token)))
2262 status = -EREMOTEIO;
2263
2264 /* serious "can't proceed" faults reported by the hardware */
2265 if (token & QTD_STS_HALT) {
2266 if (token & QTD_STS_BABBLE) {
2267 /* FIXME "must" disable babbling device's port too */
2268 status = -EOVERFLOW;
2269 /* CERR nonzero + halt --> stall */
2270 } else if (QTD_CERR(token)) {
2271 status = -EPIPE;
2272
2273 /* In theory, more than one of the following bits can be set
2274 * since they are sticky and the transaction is retried.
2275 * Which to test first is rather arbitrary.
2276 */
2277 } else if (token & QTD_STS_MMF) {
2278 /* fs/ls interrupt xfer missed the complete-split */
2279 status = -EPROTO;
2280 } else if (token & QTD_STS_DBE) {
2281 status = (QTD_PID(token) == 1) /* IN ? */
2282 ? -ENOSR /* hc couldn't read data */
2283 : -ECOMM; /* hc couldn't write data */
2284 } else if (token & QTD_STS_XACT) {
2285 /* timeout, bad CRC, wrong PID, etc */
2286 fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
2287 urb->dev->devpath,
2288 usb_pipeendpoint(urb->pipe),
2289 usb_pipein(urb->pipe) ? "in" : "out");
2290 status = -EPROTO;
2291 } else { /* unknown */
2292 status = -EPROTO;
2293 }
2294
2295 fotg210_vdbg(fotg210,
2296 "dev%d ep%d%s qtd token %08x --> status %d\n",
2297 usb_pipedevice(urb->pipe),
2298 usb_pipeendpoint(urb->pipe),
2299 usb_pipein(urb->pipe) ? "in" : "out",
2300 token, status);
2301 }
2302
2303 return status;
2304}
2305
2306static void
2307fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb, int status)
2308__releases(fotg210->lock)
2309__acquires(fotg210->lock)
2310{
2311 if (likely(urb->hcpriv != NULL)) {
2312 struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
2313
2314 /* S-mask in a QH means it's an interrupt urb */
2315 if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
2316
2317 /* ... update hc-wide periodic stats (for usbfs) */
2318 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
2319 }
2320 }
2321
2322 if (unlikely(urb->unlinked)) {
2323 COUNT(fotg210->stats.unlink);
2324 } else {
2325 /* report non-error and short read status as zero */
2326 if (status == -EINPROGRESS || status == -EREMOTEIO)
2327 status = 0;
2328 COUNT(fotg210->stats.complete);
2329 }
2330
2331#ifdef FOTG210_URB_TRACE
2332 fotg210_dbg(fotg210,
2333 "%s %s urb %p ep%d%s status %d len %d/%d\n",
2334 __func__, urb->dev->devpath, urb,
2335 usb_pipeendpoint(urb->pipe),
2336 usb_pipein(urb->pipe) ? "in" : "out",
2337 status,
2338 urb->actual_length, urb->transfer_buffer_length);
2339#endif
2340
2341 /* complete() can reenter this HCD */
2342 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
2343 spin_unlock(&fotg210->lock);
2344 usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status);
2345 spin_lock(&fotg210->lock);
2346}
2347
2348static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2349
2350/*
2351 * Process and free completed qtds for a qh, returning URBs to drivers.
2352 * Chases up to qh->hw_current. Returns number of completions called,
2353 * indicating how much "real" work we did.
2354 */
2355static unsigned
2356qh_completions(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
2357{
2358 struct fotg210_qtd *last, *end = qh->dummy;
2359 struct list_head *entry, *tmp;
2360 int last_status;
2361 int stopped;
2362 unsigned count = 0;
2363 u8 state;
2364 struct fotg210_qh_hw *hw = qh->hw;
2365
2366 if (unlikely(list_empty(&qh->qtd_list)))
2367 return count;
2368
2369 /* completions (or tasks on other cpus) must never clobber HALT
2370 * till we've gone through and cleaned everything up, even when
2371 * they add urbs to this qh's queue or mark them for unlinking.
2372 *
2373 * NOTE: unlinking expects to be done in queue order.
2374 *
2375 * It's a bug for qh->qh_state to be anything other than
2376 * QH_STATE_IDLE, unless our caller is scan_async() or
2377 * scan_intr().
2378 */
2379 state = qh->qh_state;
2380 qh->qh_state = QH_STATE_COMPLETING;
2381 stopped = (state == QH_STATE_IDLE);
2382
2383 rescan:
2384 last = NULL;
2385 last_status = -EINPROGRESS;
2386 qh->needs_rescan = 0;
2387
2388 /* remove de-activated QTDs from front of queue.
2389 * after faults (including short reads), cleanup this urb
2390 * then let the queue advance.
2391 * if queue is stopped, handles unlinks.
2392 */
2393 list_for_each_safe(entry, tmp, &qh->qtd_list) {
2394 struct fotg210_qtd *qtd;
2395 struct urb *urb;
2396 u32 token = 0;
2397
2398 qtd = list_entry(entry, struct fotg210_qtd, qtd_list);
2399 urb = qtd->urb;
2400
2401 /* clean up any state from previous QTD ...*/
2402 if (last) {
2403 if (likely(last->urb != urb)) {
2404 fotg210_urb_done(fotg210, last->urb,
2405 last_status);
2406 count++;
2407 last_status = -EINPROGRESS;
2408 }
2409 fotg210_qtd_free(fotg210, last);
2410 last = NULL;
2411 }
2412
2413 /* ignore urbs submitted during completions we reported */
2414 if (qtd == end)
2415 break;
2416
2417 /* hardware copies qtd out of qh overlay */
2418 rmb();
2419 token = hc32_to_cpu(fotg210, qtd->hw_token);
2420
2421 /* always clean up qtds the hc de-activated */
2422 retry_xacterr:
2423 if ((token & QTD_STS_ACTIVE) == 0) {
2424
2425 /* Report Data Buffer Error: non-fatal but useful */
2426 if (token & QTD_STS_DBE)
2427 fotg210_dbg(fotg210,
2428 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
2429 urb,
2430 usb_endpoint_num(&urb->ep->desc),
2431 usb_endpoint_dir_in(&urb->ep->desc)
2432 ? "in" : "out",
2433 urb->transfer_buffer_length,
2434 qtd,
2435 qh);
2436
2437 /* on STALL, error, and short reads this urb must
2438 * complete and all its qtds must be recycled.
2439 */
2440 if ((token & QTD_STS_HALT) != 0) {
2441
2442 /* retry transaction errors until we
2443 * reach the software xacterr limit
2444 */
2445 if ((token & QTD_STS_XACT) &&
2446 QTD_CERR(token) == 0 &&
2447 ++qh->xacterrs < QH_XACTERR_MAX &&
2448 !urb->unlinked) {
2449 fotg210_dbg(fotg210,
2450 "detected XactErr len %zu/%zu retry %d\n",
2451 qtd->length - QTD_LENGTH(token), qtd->length, qh->xacterrs);
2452
2453 /* reset the token in the qtd and the
2454 * qh overlay (which still contains
2455 * the qtd) so that we pick up from
2456 * where we left off
2457 */
2458 token &= ~QTD_STS_HALT;
2459 token |= QTD_STS_ACTIVE |
2460 (FOTG210_TUNE_CERR << 10);
2461 qtd->hw_token = cpu_to_hc32(fotg210,
2462 token);
2463 wmb();
2464 hw->hw_token = cpu_to_hc32(fotg210,
2465 token);
2466 goto retry_xacterr;
2467 }
2468 stopped = 1;
2469
2470 /* magic dummy for some short reads; qh won't advance.
2471 * that silicon quirk can kick in with this dummy too.
2472 *
2473 * other short reads won't stop the queue, including
2474 * control transfers (status stage handles that) or
2475 * most other single-qtd reads ... the queue stops if
2476 * URB_SHORT_NOT_OK was set so the driver submitting
2477 * the urbs could clean it up.
2478 */
2479 } else if (IS_SHORT_READ(token)
2480 && !(qtd->hw_alt_next
2481 & FOTG210_LIST_END(fotg210))) {
2482 stopped = 1;
2483 }
2484
2485 /* stop scanning when we reach qtds the hc is using */
2486 } else if (likely(!stopped
2487 && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
2488 break;
2489
2490 /* scan the whole queue for unlinks whenever it stops */
2491 } else {
2492 stopped = 1;
2493
2494 /* cancel everything if we halt, suspend, etc */
2495 if (fotg210->rh_state < FOTG210_RH_RUNNING)
2496 last_status = -ESHUTDOWN;
2497
2498 /* this qtd is active; skip it unless a previous qtd
2499 * for its urb faulted, or its urb was canceled.
2500 */
2501 else if (last_status == -EINPROGRESS && !urb->unlinked)
2502 continue;
2503
2504 /* qh unlinked; token in overlay may be most current */
2505 if (state == QH_STATE_IDLE
2506 && cpu_to_hc32(fotg210, qtd->qtd_dma)
2507 == hw->hw_current) {
2508 token = hc32_to_cpu(fotg210, hw->hw_token);
2509
2510 /* An unlink may leave an incomplete
2511 * async transaction in the TT buffer.
2512 * We have to clear it.
2513 */
2514 fotg210_clear_tt_buffer(fotg210, qh, urb,
2515 token);
2516 }
2517 }
2518
2519 /* unless we already know the urb's status, collect qtd status
2520 * and update count of bytes transferred. in common short read
2521 * cases with only one data qtd (including control transfers),
2522 * queue processing won't halt. but with two or more qtds (for
2523 * example, with a 32 KB transfer), when the first qtd gets a
2524 * short read the second must be removed by hand.
2525 */
2526 if (last_status == -EINPROGRESS) {
2527 last_status = qtd_copy_status(fotg210, urb,
2528 qtd->length, token);
2529 if (last_status == -EREMOTEIO
2530 && (qtd->hw_alt_next
2531 & FOTG210_LIST_END(fotg210)))
2532 last_status = -EINPROGRESS;
2533
2534 /* As part of low/full-speed endpoint-halt processing
2535 * we must clear the TT buffer (11.17.5).
2536 */
2537 if (unlikely(last_status != -EINPROGRESS &&
2538 last_status != -EREMOTEIO)) {
2539 /* The TT's in some hubs malfunction when they
2540 * receive this request following a STALL (they
2541 * stop sending isochronous packets). Since a
2542 * STALL can't leave the TT buffer in a busy
2543 * state (if you believe Figures 11-48 - 11-51
2544 * in the USB 2.0 spec), we won't clear the TT
2545 * buffer in this case. Strictly speaking this
2546 * is a violation of the spec.
2547 */
2548 if (last_status != -EPIPE)
2549 fotg210_clear_tt_buffer(fotg210, qh,
2550 urb, token);
2551 }
2552 }
2553
2554 /* if we're removing something not at the queue head,
2555 * patch the hardware queue pointer.
2556 */
2557 if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
2558 last = list_entry(qtd->qtd_list.prev,
2559 struct fotg210_qtd, qtd_list);
2560 last->hw_next = qtd->hw_next;
2561 }
2562
2563 /* remove qtd; it's recycled after possible urb completion */
2564 list_del(&qtd->qtd_list);
2565 last = qtd;
2566
2567 /* reinit the xacterr counter for the next qtd */
2568 qh->xacterrs = 0;
2569 }
2570
2571 /* last urb's completion might still need calling */
2572 if (likely(last != NULL)) {
2573 fotg210_urb_done(fotg210, last->urb, last_status);
2574 count++;
2575 fotg210_qtd_free(fotg210, last);
2576 }
2577
2578 /* Do we need to rescan for URBs dequeued during a giveback? */
2579 if (unlikely(qh->needs_rescan)) {
2580 /* If the QH is already unlinked, do the rescan now. */
2581 if (state == QH_STATE_IDLE)
2582 goto rescan;
2583
2584 /* Otherwise we have to wait until the QH is fully unlinked.
2585 * Our caller will start an unlink if qh->needs_rescan is
2586 * set. But if an unlink has already started, nothing needs
2587 * to be done.
2588 */
2589 if (state != QH_STATE_LINKED)
2590 qh->needs_rescan = 0;
2591 }
2592
2593 /* restore original state; caller must unlink or relink */
2594 qh->qh_state = state;
2595
2596 /* be sure the hardware's done with the qh before refreshing
2597 * it after fault cleanup, or recovering from silicon wrongly
2598 * overlaying the dummy qtd (which reduces DMA chatter).
2599 */
2600 if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
2601 switch (state) {
2602 case QH_STATE_IDLE:
2603 qh_refresh(fotg210, qh);
2604 break;
2605 case QH_STATE_LINKED:
2606 /* We won't refresh a QH that's linked (after the HC
2607 * stopped the queue). That avoids a race:
2608 * - HC reads first part of QH;
2609 * - CPU updates that first part and the token;
2610 * - HC reads rest of that QH, including token
2611 * Result: HC gets an inconsistent image, and then
2612 * DMAs to/from the wrong memory (corrupting it).
2613 *
2614 * That should be rare for interrupt transfers,
2615 * except maybe high bandwidth ...
2616 */
2617
2618 /* Tell the caller to start an unlink */
2619 qh->needs_rescan = 1;
2620 break;
2621 /* otherwise, unlink already started */
2622 }
2623 }
2624
2625 return count;
2626}
2627
2628/*-------------------------------------------------------------------------*/
2629
2630/* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
2631#define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
2632/* ... and packet size, for any kind of endpoint descriptor */
2633#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
2634
2635/*
2636 * reverse of qh_urb_transaction: free a list of TDs.
2637 * used for cleanup after errors, before HC sees an URB's TDs.
2638 */
2639static void qtd_list_free(
2640 struct fotg210_hcd *fotg210,
2641 struct urb *urb,
2642 struct list_head *qtd_list
2643) {
2644 struct list_head *entry, *temp;
2645
2646 list_for_each_safe(entry, temp, qtd_list) {
2647 struct fotg210_qtd *qtd;
2648
2649 qtd = list_entry(entry, struct fotg210_qtd, qtd_list);
2650 list_del(&qtd->qtd_list);
2651 fotg210_qtd_free(fotg210, qtd);
2652 }
2653}
2654
2655/*
2656 * create a list of filled qtds for this URB; won't link into qh.
2657 */
2658static struct list_head *
2659qh_urb_transaction(
2660 struct fotg210_hcd *fotg210,
2661 struct urb *urb,
2662 struct list_head *head,
2663 gfp_t flags
2664) {
2665 struct fotg210_qtd *qtd, *qtd_prev;
2666 dma_addr_t buf;
2667 int len, this_sg_len, maxpacket;
2668 int is_input;
2669 u32 token;
2670 int i;
2671 struct scatterlist *sg;
2672
2673 /*
2674 * URBs map to sequences of QTDs: one logical transaction
2675 */
2676 qtd = fotg210_qtd_alloc(fotg210, flags);
2677 if (unlikely(!qtd))
2678 return NULL;
2679 list_add_tail(&qtd->qtd_list, head);
2680 qtd->urb = urb;
2681
2682 token = QTD_STS_ACTIVE;
2683 token |= (FOTG210_TUNE_CERR << 10);
2684 /* for split transactions, SplitXState initialized to zero */
2685
2686 len = urb->transfer_buffer_length;
2687 is_input = usb_pipein(urb->pipe);
2688 if (usb_pipecontrol(urb->pipe)) {
2689 /* SETUP pid */
2690 qtd_fill(fotg210, qtd, urb->setup_dma,
2691 sizeof(struct usb_ctrlrequest),
2692 token | (2 /* "setup" */ << 8), 8);
2693
2694 /* ... and always at least one more pid */
2695 token ^= QTD_TOGGLE;
2696 qtd_prev = qtd;
2697 qtd = fotg210_qtd_alloc(fotg210, flags);
2698 if (unlikely(!qtd))
2699 goto cleanup;
2700 qtd->urb = urb;
2701 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2702 list_add_tail(&qtd->qtd_list, head);
2703
2704 /* for zero length DATA stages, STATUS is always IN */
2705 if (len == 0)
2706 token |= (1 /* "in" */ << 8);
2707 }
2708
2709 /*
2710 * data transfer stage: buffer setup
2711 */
2712 i = urb->num_mapped_sgs;
2713 if (len > 0 && i > 0) {
2714 sg = urb->sg;
2715 buf = sg_dma_address(sg);
2716
2717 /* urb->transfer_buffer_length may be smaller than the
2718 * size of the scatterlist (or vice versa)
2719 */
2720 this_sg_len = min_t(int, sg_dma_len(sg), len);
2721 } else {
2722 sg = NULL;
2723 buf = urb->transfer_dma;
2724 this_sg_len = len;
2725 }
2726
2727 if (is_input)
2728 token |= (1 /* "in" */ << 8);
2729 /* else it's already initted to "out" pid (0 << 8) */
2730
2731 maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
2732
2733 /*
2734 * buffer gets wrapped in one or more qtds;
2735 * last one may be "short" (including zero len)
2736 * and may serve as a control status ack
2737 */
2738 for (;;) {
2739 int this_qtd_len;
2740
2741 this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
2742 maxpacket);
2743 this_sg_len -= this_qtd_len;
2744 len -= this_qtd_len;
2745 buf += this_qtd_len;
2746
2747 /*
2748 * short reads advance to a "magic" dummy instead of the next
2749 * qtd ... that forces the queue to stop, for manual cleanup.
2750 * (this will usually be overridden later.)
2751 */
2752 if (is_input)
2753 qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
2754
2755 /* qh makes control packets use qtd toggle; maybe switch it */
2756 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
2757 token ^= QTD_TOGGLE;
2758
2759 if (likely(this_sg_len <= 0)) {
2760 if (--i <= 0 || len <= 0)
2761 break;
2762 sg = sg_next(sg);
2763 buf = sg_dma_address(sg);
2764 this_sg_len = min_t(int, sg_dma_len(sg), len);
2765 }
2766
2767 qtd_prev = qtd;
2768 qtd = fotg210_qtd_alloc(fotg210, flags);
2769 if (unlikely(!qtd))
2770 goto cleanup;
2771 qtd->urb = urb;
2772 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2773 list_add_tail(&qtd->qtd_list, head);
2774 }
2775
2776 /*
2777 * unless the caller requires manual cleanup after short reads,
2778 * have the alt_next mechanism keep the queue running after the
2779 * last data qtd (the only one, for control and most other cases).
2780 */
2781 if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
2782 || usb_pipecontrol(urb->pipe)))
2783 qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
2784
2785 /*
2786 * control requests may need a terminating data "status" ack;
2787 * other OUT ones may need a terminating short packet
2788 * (zero length).
2789 */
2790 if (likely(urb->transfer_buffer_length != 0)) {
2791 int one_more = 0;
2792
2793 if (usb_pipecontrol(urb->pipe)) {
2794 one_more = 1;
2795 token ^= 0x0100; /* "in" <--> "out" */
2796 token |= QTD_TOGGLE; /* force DATA1 */
2797 } else if (usb_pipeout(urb->pipe)
2798 && (urb->transfer_flags & URB_ZERO_PACKET)
2799 && !(urb->transfer_buffer_length % maxpacket)) {
2800 one_more = 1;
2801 }
2802 if (one_more) {
2803 qtd_prev = qtd;
2804 qtd = fotg210_qtd_alloc(fotg210, flags);
2805 if (unlikely(!qtd))
2806 goto cleanup;
2807 qtd->urb = urb;
2808 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2809 list_add_tail(&qtd->qtd_list, head);
2810
2811 /* never any data in such packets */
2812 qtd_fill(fotg210, qtd, 0, 0, token, 0);
2813 }
2814 }
2815
2816 /* by default, enable interrupt on urb completion */
2817 if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
2818 qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
2819 return head;
2820
2821cleanup:
2822 qtd_list_free(fotg210, urb, head);
2823 return NULL;
2824}
2825
2826/*-------------------------------------------------------------------------*/
2827/*
2828 * Would be best to create all qh's from config descriptors,
2829 * when each interface/altsetting is established. Unlink
2830 * any previous qh and cancel its urbs first; endpoints are
2831 * implicitly reset then (data toggle too).
2832 * That'd mean updating how usbcore talks to HCDs. (2.7?)
2833*/
2834
2835
2836/*
2837 * Each QH holds a qtd list; a QH is used for everything except iso.
2838 *
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.
2843 */
2844static struct fotg210_qh *
2845qh_make(
2846 struct fotg210_hcd *fotg210,
2847 struct urb *urb,
2848 gfp_t flags
2849) {
2850 struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, 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 fotg210_qh_hw *hw;
2856
2857 if (!qh)
2858 return qh;
2859
2860 /*
2861 * init endpoint/device data for this QH
2862 */
2863 info1 |= usb_pipeendpoint(urb->pipe) << 8;
2864 info1 |= usb_pipedevice(urb->pipe) << 0;
2865
2866 is_input = usb_pipein(urb->pipe);
2867 type = usb_pipetype(urb->pipe);
2868 maxp = usb_maxpacket(urb->dev, urb->pipe, !is_input);
2869
2870 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
2871 * acts like up to 3KB, but is built from smaller packets.
2872 */
2873 if (max_packet(maxp) > 1024) {
2874 fotg210_dbg(fotg210, "bogus qh maxpacket %d\n",
2875 max_packet(maxp));
2876 goto done;
2877 }
2878
2879 /* Compute interrupt scheduling parameters just once, and save.
2880 * - allowing for high bandwidth, how many nsec/uframe are used?
2881 * - split transactions need a second CSPLIT uframe; same question
2882 * - splits also need a schedule gap (for full/low speed I/O)
2883 * - qh has a polling interval
2884 *
2885 * For control/bulk requests, the HC or TT handles these.
2886 */
2887 if (type == PIPE_INTERRUPT) {
2888 qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
2889 is_input, 0,
2890 hb_mult(maxp) * max_packet(maxp)));
2891 qh->start = NO_FRAME;
2892
2893 if (urb->dev->speed == USB_SPEED_HIGH) {
2894 qh->c_usecs = 0;
2895 qh->gap_uf = 0;
2896
2897 qh->period = urb->interval >> 3;
2898 if (qh->period == 0 && urb->interval != 1) {
2899 /* NOTE interval 2 or 4 uframes could work.
2900 * But interval 1 scheduling is simpler, and
2901 * includes high bandwidth.
2902 */
2903 urb->interval = 1;
2904 } else if (qh->period > fotg210->periodic_size) {
2905 qh->period = fotg210->periodic_size;
2906 urb->interval = qh->period << 3;
2907 }
2908 } else {
2909 int think_time;
2910
2911 /* gap is f(FS/LS transfer times) */
2912 qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
2913 is_input, 0, maxp) / (125 * 1000);
2914
2915 /* FIXME this just approximates SPLIT/CSPLIT times */
2916 if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
2917 qh->c_usecs = qh->usecs + HS_USECS(0);
2918 qh->usecs = HS_USECS(1);
2919 } else { /* SPLIT+DATA, gap, CSPLIT */
2920 qh->usecs += HS_USECS(1);
2921 qh->c_usecs = HS_USECS(0);
2922 }
2923
2924 think_time = tt ? tt->think_time : 0;
2925 qh->tt_usecs = NS_TO_US(think_time +
2926 usb_calc_bus_time(urb->dev->speed,
2927 is_input, 0, max_packet(maxp)));
2928 qh->period = urb->interval;
2929 if (qh->period > fotg210->periodic_size) {
2930 qh->period = fotg210->periodic_size;
2931 urb->interval = qh->period;
2932 }
2933 }
2934 }
2935
2936 /* support for tt scheduling, and access to toggles */
2937 qh->dev = urb->dev;
2938
2939 /* using TT? */
2940 switch (urb->dev->speed) {
2941 case USB_SPEED_LOW:
2942 info1 |= QH_LOW_SPEED;
2943 /* FALL THROUGH */
2944
2945 case USB_SPEED_FULL:
2946 /* EPS 0 means "full" */
2947 if (type != PIPE_INTERRUPT)
2948 info1 |= (FOTG210_TUNE_RL_TT << 28);
2949 if (type == PIPE_CONTROL) {
2950 info1 |= QH_CONTROL_EP; /* for TT */
2951 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2952 }
2953 info1 |= maxp << 16;
2954
2955 info2 |= (FOTG210_TUNE_MULT_TT << 30);
2956
2957 /* Some Freescale processors have an erratum in which the
2958 * port number in the queue head was 0..N-1 instead of 1..N.
2959 */
2960 if (fotg210_has_fsl_portno_bug(fotg210))
2961 info2 |= (urb->dev->ttport-1) << 23;
2962 else
2963 info2 |= urb->dev->ttport << 23;
2964
2965 /* set the address of the TT; for TDI's integrated
2966 * root hub tt, leave it zeroed.
2967 */
2968 if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
2969 info2 |= tt->hub->devnum << 16;
2970
2971 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
2972
2973 break;
2974
2975 case USB_SPEED_HIGH: /* no TT involved */
2976 info1 |= QH_HIGH_SPEED;
2977 if (type == PIPE_CONTROL) {
2978 info1 |= (FOTG210_TUNE_RL_HS << 28);
2979 info1 |= 64 << 16; /* usb2 fixed maxpacket */
2980 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2981 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2982 } else if (type == PIPE_BULK) {
2983 info1 |= (FOTG210_TUNE_RL_HS << 28);
2984 /* The USB spec says that high speed bulk endpoints
2985 * always use 512 byte maxpacket. But some device
2986 * vendors decided to ignore that, and MSFT is happy
2987 * to help them do so. So now people expect to use
2988 * such nonconformant devices with Linux too; sigh.
2989 */
2990 info1 |= max_packet(maxp) << 16;
2991 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2992 } else { /* PIPE_INTERRUPT */
2993 info1 |= max_packet(maxp) << 16;
2994 info2 |= hb_mult(maxp) << 30;
2995 }
2996 break;
2997 default:
2998 fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
2999 urb->dev->speed);
3000done:
3001 qh_destroy(fotg210, qh);
3002 return NULL;
3003 }
3004
3005 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
3006
3007 /* init as live, toggle clear, advance to dummy */
3008 qh->qh_state = QH_STATE_IDLE;
3009 hw = qh->hw;
3010 hw->hw_info1 = cpu_to_hc32(fotg210, info1);
3011 hw->hw_info2 = cpu_to_hc32(fotg210, info2);
3012 qh->is_out = !is_input;
3013 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
3014 qh_refresh(fotg210, qh);
3015 return qh;
3016}
3017
3018/*-------------------------------------------------------------------------*/
3019
3020static void enable_async(struct fotg210_hcd *fotg210)
3021{
3022 if (fotg210->async_count++)
3023 return;
3024
3025 /* Stop waiting to turn off the async schedule */
3026 fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
3027
3028 /* Don't start the schedule until ASS is 0 */
3029 fotg210_poll_ASS(fotg210);
3030 turn_on_io_watchdog(fotg210);
3031}
3032
3033static void disable_async(struct fotg210_hcd *fotg210)
3034{
3035 if (--fotg210->async_count)
3036 return;
3037
3038 /* The async schedule and async_unlink list are supposed to be empty */
3039 WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
3040
3041 /* Don't turn off the schedule until ASS is 1 */
3042 fotg210_poll_ASS(fotg210);
3043}
3044
3045/* move qh (and its qtds) onto async queue; maybe enable queue. */
3046
3047static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3048{
3049 __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
3050 struct fotg210_qh *head;
3051
3052 /* Don't link a QH if there's a Clear-TT-Buffer pending */
3053 if (unlikely(qh->clearing_tt))
3054 return;
3055
3056 WARN_ON(qh->qh_state != QH_STATE_IDLE);
3057
3058 /* clear halt and/or toggle; and maybe recover from silicon quirk */
3059 qh_refresh(fotg210, qh);
3060
3061 /* splice right after start */
3062 head = fotg210->async;
3063 qh->qh_next = head->qh_next;
3064 qh->hw->hw_next = head->hw->hw_next;
3065 wmb();
3066
3067 head->qh_next.qh = qh;
3068 head->hw->hw_next = dma;
3069
3070 qh->xacterrs = 0;
3071 qh->qh_state = QH_STATE_LINKED;
3072 /* qtd completions reported later by interrupt */
3073
3074 enable_async(fotg210);
3075}
3076
3077/*-------------------------------------------------------------------------*/
3078
3079/*
3080 * For control/bulk/interrupt, return QH with these TDs appended.
3081 * Allocates and initializes the QH if necessary.
3082 * Returns null if it can't allocate a QH it needs to.
3083 * If the QH has TDs (urbs) already, that's great.
3084 */
3085static struct fotg210_qh *qh_append_tds(
3086 struct fotg210_hcd *fotg210,
3087 struct urb *urb,
3088 struct list_head *qtd_list,
3089 int epnum,
3090 void **ptr
3091)
3092{
3093 struct fotg210_qh *qh = NULL;
3094 __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
3095
3096 qh = (struct fotg210_qh *) *ptr;
3097 if (unlikely(qh == NULL)) {
3098 /* can't sleep here, we have fotg210->lock... */
3099 qh = qh_make(fotg210, urb, GFP_ATOMIC);
3100 *ptr = qh;
3101 }
3102 if (likely(qh != NULL)) {
3103 struct fotg210_qtd *qtd;
3104
3105 if (unlikely(list_empty(qtd_list)))
3106 qtd = NULL;
3107 else
3108 qtd = list_entry(qtd_list->next, struct fotg210_qtd,
3109 qtd_list);
3110
3111 /* control qh may need patching ... */
3112 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;
3116 }
3117
3118 /* just one way to queue requests: swap with the dummy qtd.
3119 * only hc or qh_refresh() ever modify the overlay.
3120 */
3121 if (likely(qtd != NULL)) {
3122 struct fotg210_qtd *dummy;
3123 dma_addr_t dma;
3124 __hc32 token;
3125
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).
3130 */
3131 token = qtd->hw_token;
3132 qtd->hw_token = HALT_BIT(fotg210);
3133
3134 dummy = qh->dummy;
3135
3136 dma = dummy->qtd_dma;
3137 *dummy = *qtd;
3138 dummy->qtd_dma = dma;
3139
3140 list_del(&qtd->qtd_list);
3141 list_add(&dummy->qtd_list, qtd_list);
3142 list_splice_tail(qtd_list, &qh->qtd_list);
3143
3144 fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
3145 qh->dummy = qtd;
3146
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 fotg210_qtd, qtd_list);
3151 qtd->hw_next = QTD_NEXT(fotg210, dma);
3152
3153 /* let the hc process these next qtds */
3154 wmb();
3155 dummy->hw_token = token;
3156
3157 urb->hcpriv = qh;
3158 }
3159 }
3160 return qh;
3161}
3162
3163/*-------------------------------------------------------------------------*/
3164
3165static int
3166submit_async(
3167 struct fotg210_hcd *fotg210,
3168 struct urb *urb,
3169 struct list_head *qtd_list,
3170 gfp_t mem_flags
3171) {
3172 int epnum;
3173 unsigned long flags;
3174 struct fotg210_qh *qh = NULL;
3175 int rc;
3176
3177 epnum = urb->ep->desc.bEndpointAddress;
3178
3179#ifdef FOTG210_URB_TRACE
3180 {
3181 struct fotg210_qtd *qtd;
3182 qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list);
3183 fotg210_dbg(fotg210,
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);
3189 }
3190#endif
3191
3192 spin_lock_irqsave(&fotg210->lock, flags);
3193 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3194 rc = -ESHUTDOWN;
3195 goto done;
3196 }
3197 rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
3198 if (unlikely(rc))
3199 goto done;
3200
3201 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
3202 if (unlikely(qh == NULL)) {
3203 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
3204 rc = -ENOMEM;
3205 goto done;
3206 }
3207
3208 /* Control/bulk operations through TTs don't need scheduling,
3209 * the HC and TT handle it when the TT has a buffer ready.
3210 */
3211 if (likely(qh->qh_state == QH_STATE_IDLE))
3212 qh_link_async(fotg210, qh);
3213 done:
3214 spin_unlock_irqrestore(&fotg210->lock, flags);
3215 if (unlikely(qh == NULL))
3216 qtd_list_free(fotg210, urb, qtd_list);
3217 return rc;
3218}
3219
3220/*-------------------------------------------------------------------------*/
3221
3222static void single_unlink_async(struct fotg210_hcd *fotg210,
3223 struct fotg210_qh *qh)
3224{
3225 struct fotg210_qh *prev;
3226
3227 /* Add to the end of the list of QHs waiting for the next IAAD */
3228 qh->qh_state = QH_STATE_UNLINK;
3229 if (fotg210->async_unlink)
3230 fotg210->async_unlink_last->unlink_next = qh;
3231 else
3232 fotg210->async_unlink = qh;
3233 fotg210->async_unlink_last = qh;
3234
3235 /* Unlink it from the schedule */
3236 prev = fotg210->async;
3237 while (prev->qh_next.qh != qh)
3238 prev = prev->qh_next.qh;
3239
3240 prev->hw->hw_next = qh->hw->hw_next;
3241 prev->qh_next = qh->qh_next;
3242 if (fotg210->qh_scan_next == qh)
3243 fotg210->qh_scan_next = qh->qh_next.qh;
3244}
3245
3246static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
3247{
3248 /*
3249 * Do nothing if an IAA cycle is already running or
3250 * if one will be started shortly.
3251 */
3252 if (fotg210->async_iaa || fotg210->async_unlinking)
3253 return;
3254
3255 /* Do all the waiting QHs at once */
3256 fotg210->async_iaa = fotg210->async_unlink;
3257 fotg210->async_unlink = NULL;
3258
3259 /* If the controller isn't running, we don't have to wait for it */
3260 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
3261 if (!nested) /* Avoid recursion */
3262 end_unlink_async(fotg210);
3263
3264 /* Otherwise start a new IAA cycle */
3265 } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
3266 /* Make sure the unlinks are all visible to the hardware */
3267 wmb();
3268
3269 fotg210_writel(fotg210, fotg210->command | CMD_IAAD,
3270 &fotg210->regs->command);
3271 fotg210_readl(fotg210, &fotg210->regs->command);
3272 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG,
3273 true);
3274 }
3275}
3276
3277/* the async qh for the qtds being unlinked are now gone from the HC */
3278
3279static void end_unlink_async(struct fotg210_hcd *fotg210)
3280{
3281 struct fotg210_qh *qh;
3282
3283 /* Process the idle QHs */
3284 restart:
3285 fotg210->async_unlinking = true;
3286 while (fotg210->async_iaa) {
3287 qh = fotg210->async_iaa;
3288 fotg210->async_iaa = qh->unlink_next;
3289 qh->unlink_next = NULL;
3290
3291 qh->qh_state = QH_STATE_IDLE;
3292 qh->qh_next.qh = NULL;
3293
3294 qh_completions(fotg210, qh);
3295 if (!list_empty(&qh->qtd_list) &&
3296 fotg210->rh_state == FOTG210_RH_RUNNING)
3297 qh_link_async(fotg210, qh);
3298 disable_async(fotg210);
3299 }
3300 fotg210->async_unlinking = false;
3301
3302 /* Start a new IAA cycle if any QHs are waiting for it */
3303 if (fotg210->async_unlink) {
3304 start_iaa_cycle(fotg210, true);
3305 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
3306 goto restart;
3307 }
3308}
3309
3310static void unlink_empty_async(struct fotg210_hcd *fotg210)
3311{
3312 struct fotg210_qh *qh, *next;
3313 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
3314 bool check_unlinks_later = false;
3315
3316 /* Unlink all the async QHs that have been empty for a timer cycle */
3317 next = fotg210->async->qh_next.qh;
3318 while (next) {
3319 qh = next;
3320 next = qh->qh_next.qh;
3321
3322 if (list_empty(&qh->qtd_list) &&
3323 qh->qh_state == QH_STATE_LINKED) {
3324 if (!stopped && qh->unlink_cycle ==
3325 fotg210->async_unlink_cycle)
3326 check_unlinks_later = true;
3327 else
3328 single_unlink_async(fotg210, qh);
3329 }
3330 }
3331
3332 /* Start a new IAA cycle if any QHs are waiting for it */
3333 if (fotg210->async_unlink)
3334 start_iaa_cycle(fotg210, false);
3335
3336 /* QHs that haven't been empty for long enough will be handled later */
3337 if (check_unlinks_later) {
3338 fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
3339 true);
3340 ++fotg210->async_unlink_cycle;
3341 }
3342}
3343
3344/* makes sure the async qh will become idle */
3345/* caller must own fotg210->lock */
3346
3347static void start_unlink_async(struct fotg210_hcd *fotg210,
3348 struct fotg210_qh *qh)
3349{
3350 /*
3351 * If the QH isn't linked then there's nothing we can do
3352 * unless we were called during a giveback, in which case
3353 * qh_completions() has to deal with it.
3354 */
3355 if (qh->qh_state != QH_STATE_LINKED) {
3356 if (qh->qh_state == QH_STATE_COMPLETING)
3357 qh->needs_rescan = 1;
3358 return;
3359 }
3360
3361 single_unlink_async(fotg210, qh);
3362 start_iaa_cycle(fotg210, false);
3363}
3364
3365/*-------------------------------------------------------------------------*/
3366
3367static void scan_async(struct fotg210_hcd *fotg210)
3368{
3369 struct fotg210_qh *qh;
3370 bool check_unlinks_later = false;
3371
3372 fotg210->qh_scan_next = fotg210->async->qh_next.qh;
3373 while (fotg210->qh_scan_next) {
3374 qh = fotg210->qh_scan_next;
3375 fotg210->qh_scan_next = qh->qh_next.qh;
3376 rescan:
3377 /* clean any finished work for this qh */
3378 if (!list_empty(&qh->qtd_list)) {
3379 int temp;
3380
3381 /*
3382 * Unlinks could happen here; completion reporting
3383 * drops the lock. That's why fotg210->qh_scan_next
3384 * always holds the next qh to scan; if the next qh
3385 * gets unlinked then fotg210->qh_scan_next is adjusted
3386 * in single_unlink_async().
3387 */
3388 temp = qh_completions(fotg210, qh);
3389 if (qh->needs_rescan) {
3390 start_unlink_async(fotg210, qh);
3391 } else if (list_empty(&qh->qtd_list)
3392 && qh->qh_state == QH_STATE_LINKED) {
3393 qh->unlink_cycle = fotg210->async_unlink_cycle;
3394 check_unlinks_later = true;
3395 } else if (temp != 0)
3396 goto rescan;
3397 }
3398 }
3399
3400 /*
3401 * Unlink empty entries, reducing DMA usage as well
3402 * as HCD schedule-scanning costs. Delay for any qh
3403 * we just scanned, there's a not-unusual case that it
3404 * doesn't stay idle for long.
3405 */
3406 if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING &&
3407 !(fotg210->enabled_hrtimer_events &
3408 BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) {
3409 fotg210_enable_event(fotg210,
3410 FOTG210_HRTIMER_ASYNC_UNLINKS, true);
3411 ++fotg210->async_unlink_cycle;
3412 }
3413}
3414/*-------------------------------------------------------------------------*/
3415/*
3416 * EHCI scheduled transaction support: interrupt, iso, split iso
3417 * These are called "periodic" transactions in the EHCI spec.
3418 *
3419 * Note that for interrupt transfers, the QH/QTD manipulation is shared
3420 * with the "asynchronous" transaction support (control/bulk transfers).
3421 * The only real difference is in how interrupt transfers are scheduled.
3422 *
3423 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
3424 * It keeps track of every ITD (or SITD) that's linked, and holds enough
3425 * pre-calculated schedule data to make appending to the queue be quick.
3426 */
3427
3428static int fotg210_get_frame(struct usb_hcd *hcd);
3429
3430/*-------------------------------------------------------------------------*/
3431
3432/*
3433 * periodic_next_shadow - return "next" pointer on shadow list
3434 * @periodic: host pointer to qh/itd
3435 * @tag: hardware tag for type of this record
3436 */
3437static union fotg210_shadow *
3438periodic_next_shadow(struct fotg210_hcd *fotg210,
3439 union fotg210_shadow *periodic, __hc32 tag)
3440{
3441 switch (hc32_to_cpu(fotg210, tag)) {
3442 case Q_TYPE_QH:
3443 return &periodic->qh->qh_next;
3444 case Q_TYPE_FSTN:
3445 return &periodic->fstn->fstn_next;
3446 default:
3447 return &periodic->itd->itd_next;
3448 }
3449}
3450
3451static __hc32 *
3452shadow_next_periodic(struct fotg210_hcd *fotg210,
3453 union fotg210_shadow *periodic, __hc32 tag)
3454{
3455 switch (hc32_to_cpu(fotg210, tag)) {
3456 /* our fotg210_shadow.qh is actually software part */
3457 case Q_TYPE_QH:
3458 return &periodic->qh->hw->hw_next;
3459 /* others are hw parts */
3460 default:
3461 return periodic->hw_next;
3462 }
3463}
3464
3465/* caller must hold fotg210->lock */
3466static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
3467 void *ptr)
3468{
3469 union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
3470 __hc32 *hw_p = &fotg210->periodic[frame];
3471 union fotg210_shadow here = *prev_p;
3472
3473 /* find predecessor of "ptr"; hw and shadow lists are in sync */
3474 while (here.ptr && here.ptr != ptr) {
3475 prev_p = periodic_next_shadow(fotg210, prev_p,
3476 Q_NEXT_TYPE(fotg210, *hw_p));
3477 hw_p = shadow_next_periodic(fotg210, &here,
3478 Q_NEXT_TYPE(fotg210, *hw_p));
3479 here = *prev_p;
3480 }
3481 /* an interrupt entry (at list end) could have been shared */
3482 if (!here.ptr)
3483 return;
3484
3485 /* update shadow and hardware lists ... the old "next" pointers
3486 * from ptr may still be in use, the caller updates them.
3487 */
3488 *prev_p = *periodic_next_shadow(fotg210, &here,
3489 Q_NEXT_TYPE(fotg210, *hw_p));
3490
3491 *hw_p = *shadow_next_periodic(fotg210, &here,
3492 Q_NEXT_TYPE(fotg210, *hw_p));
3493}
3494
3495/* how many of the uframe's 125 usecs are allocated? */
3496static unsigned short
3497periodic_usecs(struct fotg210_hcd *fotg210, unsigned frame, unsigned uframe)
3498{
3499 __hc32 *hw_p = &fotg210->periodic[frame];
3500 union fotg210_shadow *q = &fotg210->pshadow[frame];
3501 unsigned usecs = 0;
3502 struct fotg210_qh_hw *hw;
3503
3504 while (q->ptr) {
3505 switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
3506 case Q_TYPE_QH:
3507 hw = q->qh->hw;
3508 /* is it in the S-mask? */
3509 if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
3510 usecs += q->qh->usecs;
3511 /* ... or C-mask? */
3512 if (hw->hw_info2 & cpu_to_hc32(fotg210,
3513 1 << (8 + uframe)))
3514 usecs += q->qh->c_usecs;
3515 hw_p = &hw->hw_next;
3516 q = &q->qh->qh_next;
3517 break;
3518 /* case Q_TYPE_FSTN: */
3519 default:
3520 /* for "save place" FSTNs, count the relevant INTR
3521 * bandwidth from the previous frame
3522 */
3523 if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
3524 fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
3525
3526 hw_p = &q->fstn->hw_next;
3527 q = &q->fstn->fstn_next;
3528 break;
3529 case Q_TYPE_ITD:
3530 if (q->itd->hw_transaction[uframe])
3531 usecs += q->itd->stream->usecs;
3532 hw_p = &q->itd->hw_next;
3533 q = &q->itd->itd_next;
3534 break;
3535 }
3536 }
3537#ifdef DEBUG
3538 if (usecs > fotg210->uframe_periodic_max)
3539 fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
3540 frame * 8 + uframe, usecs);
3541#endif
3542 return usecs;
3543}
3544
3545/*-------------------------------------------------------------------------*/
3546
3547static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
3548{
3549 if (!dev1->tt || !dev2->tt)
3550 return 0;
3551 if (dev1->tt != dev2->tt)
3552 return 0;
3553 if (dev1->tt->multi)
3554 return dev1->ttport == dev2->ttport;
3555 else
3556 return 1;
3557}
3558
3559/* return true iff the device's transaction translator is available
3560 * for a periodic transfer starting at the specified frame, using
3561 * all the uframes in the mask.
3562 */
3563static int tt_no_collision(
3564 struct fotg210_hcd *fotg210,
3565 unsigned period,
3566 struct usb_device *dev,
3567 unsigned frame,
3568 u32 uf_mask
3569)
3570{
3571 if (period == 0) /* error */
3572 return 0;
3573
3574 /* note bandwidth wastage: split never follows csplit
3575 * (different dev or endpoint) until the next uframe.
3576 * calling convention doesn't make that distinction.
3577 */
3578 for (; frame < fotg210->periodic_size; frame += period) {
3579 union fotg210_shadow here;
3580 __hc32 type;
3581 struct fotg210_qh_hw *hw;
3582
3583 here = fotg210->pshadow[frame];
3584 type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
3585 while (here.ptr) {
3586 switch (hc32_to_cpu(fotg210, type)) {
3587 case Q_TYPE_ITD:
3588 type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
3589 here = here.itd->itd_next;
3590 continue;
3591 case Q_TYPE_QH:
3592 hw = here.qh->hw;
3593 if (same_tt(dev, here.qh->dev)) {
3594 u32 mask;
3595
3596 mask = hc32_to_cpu(fotg210,
3597 hw->hw_info2);
3598 /* "knows" no gap is needed */
3599 mask |= mask >> 8;
3600 if (mask & uf_mask)
3601 break;
3602 }
3603 type = Q_NEXT_TYPE(fotg210, hw->hw_next);
3604 here = here.qh->qh_next;
3605 continue;
3606 /* case Q_TYPE_FSTN: */
3607 default:
3608 fotg210_dbg(fotg210,
3609 "periodic frame %d bogus type %d\n",
3610 frame, type);
3611 }
3612
3613 /* collision or error */
3614 return 0;
3615 }
3616 }
3617
3618 /* no collision */
3619 return 1;
3620}
3621
3622/*-------------------------------------------------------------------------*/
3623
3624static void enable_periodic(struct fotg210_hcd *fotg210)
3625{
3626 if (fotg210->periodic_count++)
3627 return;
3628
3629 /* Stop waiting to turn off the periodic schedule */
3630 fotg210->enabled_hrtimer_events &=
3631 ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
3632
3633 /* Don't start the schedule until PSS is 0 */
3634 fotg210_poll_PSS(fotg210);
3635 turn_on_io_watchdog(fotg210);
3636}
3637
3638static void disable_periodic(struct fotg210_hcd *fotg210)
3639{
3640 if (--fotg210->periodic_count)
3641 return;
3642
3643 /* Don't turn off the schedule until PSS is 1 */
3644 fotg210_poll_PSS(fotg210);
3645}
3646
3647/*-------------------------------------------------------------------------*/
3648
3649/* periodic schedule slots have iso tds (normal or split) first, then a
3650 * sparse tree for active interrupt transfers.
3651 *
3652 * this just links in a qh; caller guarantees uframe masks are set right.
3653 * no FSTN support (yet; fotg210 0.96+)
3654 */
3655static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3656{
3657 unsigned i;
3658 unsigned period = qh->period;
3659
3660 dev_dbg(&qh->dev->dev,
3661 "link qh%d-%04x/%p start %d [%d/%d us]\n",
3662 period, hc32_to_cpup(fotg210, &qh->hw->hw_info2)
3663 & (QH_CMASK | QH_SMASK),
3664 qh, qh->start, qh->usecs, qh->c_usecs);
3665
3666 /* high bandwidth, or otherwise every microframe */
3667 if (period == 0)
3668 period = 1;
3669
3670 for (i = qh->start; i < fotg210->periodic_size; i += period) {
3671 union fotg210_shadow *prev = &fotg210->pshadow[i];
3672 __hc32 *hw_p = &fotg210->periodic[i];
3673 union fotg210_shadow here = *prev;
3674 __hc32 type = 0;
3675
3676 /* skip the iso nodes at list head */
3677 while (here.ptr) {
3678 type = Q_NEXT_TYPE(fotg210, *hw_p);
3679 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
3680 break;
3681 prev = periodic_next_shadow(fotg210, prev, type);
3682 hw_p = shadow_next_periodic(fotg210, &here, type);
3683 here = *prev;
3684 }
3685
3686 /* sorting each branch by period (slow-->fast)
3687 * enables sharing interior tree nodes
3688 */
3689 while (here.ptr && qh != here.qh) {
3690 if (qh->period > here.qh->period)
3691 break;
3692 prev = &here.qh->qh_next;
3693 hw_p = &here.qh->hw->hw_next;
3694 here = *prev;
3695 }
3696 /* link in this qh, unless some earlier pass did that */
3697 if (qh != here.qh) {
3698 qh->qh_next = here;
3699 if (here.qh)
3700 qh->hw->hw_next = *hw_p;
3701 wmb();
3702 prev->qh = qh;
3703 *hw_p = QH_NEXT(fotg210, qh->qh_dma);
3704 }
3705 }
3706 qh->qh_state = QH_STATE_LINKED;
3707 qh->xacterrs = 0;
3708
3709 /* update per-qh bandwidth for usbfs */
3710 fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
3711 ? ((qh->usecs + qh->c_usecs) / qh->period)
3712 : (qh->usecs * 8);
3713
3714 list_add(&qh->intr_node, &fotg210->intr_qh_list);
3715
3716 /* maybe enable periodic schedule processing */
3717 ++fotg210->intr_count;
3718 enable_periodic(fotg210);
3719}
3720
3721static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
3722 struct fotg210_qh *qh)
3723{
3724 unsigned i;
3725 unsigned period;
3726
3727 /*
3728 * If qh is for a low/full-speed device, simply unlinking it
3729 * could interfere with an ongoing split transaction. To unlink
3730 * it safely would require setting the QH_INACTIVATE bit and
3731 * waiting at least one frame, as described in EHCI 4.12.2.5.
3732 *
3733 * We won't bother with any of this. Instead, we assume that the
3734 * only reason for unlinking an interrupt QH while the current URB
3735 * is still active is to dequeue all the URBs (flush the whole
3736 * endpoint queue).
3737 *
3738 * If rebalancing the periodic schedule is ever implemented, this
3739 * approach will no longer be valid.
3740 */
3741
3742 /* high bandwidth, or otherwise part of every microframe */
3743 period = qh->period;
3744 if (!period)
3745 period = 1;
3746
3747 for (i = qh->start; i < fotg210->periodic_size; i += period)
3748 periodic_unlink(fotg210, i, qh);
3749
3750 /* update per-qh bandwidth for usbfs */
3751 fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
3752 ? ((qh->usecs + qh->c_usecs) / qh->period)
3753 : (qh->usecs * 8);
3754
3755 dev_dbg(&qh->dev->dev,
3756 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
3757 qh->period,
3758 hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
3759 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs, qh->c_usecs);
3760
3761 /* qh->qh_next still "live" to HC */
3762 qh->qh_state = QH_STATE_UNLINK;
3763 qh->qh_next.ptr = NULL;
3764
3765 if (fotg210->qh_scan_next == qh)
3766 fotg210->qh_scan_next = list_entry(qh->intr_node.next,
3767 struct fotg210_qh, intr_node);
3768 list_del(&qh->intr_node);
3769}
3770
3771static void start_unlink_intr(struct fotg210_hcd *fotg210,
3772 struct fotg210_qh *qh)
3773{
3774 /* If the QH isn't linked then there's nothing we can do
3775 * unless we were called during a giveback, in which case
3776 * qh_completions() has to deal with it.
3777 */
3778 if (qh->qh_state != QH_STATE_LINKED) {
3779 if (qh->qh_state == QH_STATE_COMPLETING)
3780 qh->needs_rescan = 1;
3781 return;
3782 }
3783
3784 qh_unlink_periodic(fotg210, qh);
3785
3786 /* Make sure the unlinks are visible before starting the timer */
3787 wmb();
3788
3789 /*
3790 * The EHCI spec doesn't say how long it takes the controller to
3791 * stop accessing an unlinked interrupt QH. The timer delay is
3792 * 9 uframes; presumably that will be long enough.
3793 */
3794 qh->unlink_cycle = fotg210->intr_unlink_cycle;
3795
3796 /* New entries go at the end of the intr_unlink list */
3797 if (fotg210->intr_unlink)
3798 fotg210->intr_unlink_last->unlink_next = qh;
3799 else
3800 fotg210->intr_unlink = qh;
3801 fotg210->intr_unlink_last = qh;
3802
3803 if (fotg210->intr_unlinking)
3804 ; /* Avoid recursive calls */
3805 else if (fotg210->rh_state < FOTG210_RH_RUNNING)
3806 fotg210_handle_intr_unlinks(fotg210);
3807 else if (fotg210->intr_unlink == qh) {
3808 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
3809 true);
3810 ++fotg210->intr_unlink_cycle;
3811 }
3812}
3813
3814static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3815{
3816 struct fotg210_qh_hw *hw = qh->hw;
3817 int rc;
3818
3819 qh->qh_state = QH_STATE_IDLE;
3820 hw->hw_next = FOTG210_LIST_END(fotg210);
3821
3822 qh_completions(fotg210, qh);
3823
3824 /* reschedule QH iff another request is queued */
3825 if (!list_empty(&qh->qtd_list) &&
3826 fotg210->rh_state == FOTG210_RH_RUNNING) {
3827 rc = qh_schedule(fotg210, qh);
3828
3829 /* An error here likely indicates handshake failure
3830 * or no space left in the schedule. Neither fault
3831 * should happen often ...
3832 *
3833 * FIXME kill the now-dysfunctional queued urbs
3834 */
3835 if (rc != 0)
3836 fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
3837 qh, rc);
3838 }
3839
3840 /* maybe turn off periodic schedule */
3841 --fotg210->intr_count;
3842 disable_periodic(fotg210);
3843}
3844
3845/*-------------------------------------------------------------------------*/
3846
3847static int check_period(
3848 struct fotg210_hcd *fotg210,
3849 unsigned frame,
3850 unsigned uframe,
3851 unsigned period,
3852 unsigned usecs
3853) {
3854 int claimed;
3855
3856 /* complete split running into next frame?
3857 * given FSTN support, we could sometimes check...
3858 */
3859 if (uframe >= 8)
3860 return 0;
3861
3862 /* convert "usecs we need" to "max already claimed" */
3863 usecs = fotg210->uframe_periodic_max - usecs;
3864
3865 /* we "know" 2 and 4 uframe intervals were rejected; so
3866 * for period 0, check _every_ microframe in the schedule.
3867 */
3868 if (unlikely(period == 0)) {
3869 do {
3870 for (uframe = 0; uframe < 7; uframe++) {
3871 claimed = periodic_usecs(fotg210, frame,
3872 uframe);
3873 if (claimed > usecs)
3874 return 0;
3875 }
3876 } while ((frame += 1) < fotg210->periodic_size);
3877
3878 /* just check the specified uframe, at that period */
3879 } else {
3880 do {
3881 claimed = periodic_usecs(fotg210, frame, uframe);
3882 if (claimed > usecs)
3883 return 0;
3884 } while ((frame += period) < fotg210->periodic_size);
3885 }
3886
3887 /* success! */
3888 return 1;
3889}
3890
3891static int check_intr_schedule(
3892 struct fotg210_hcd *fotg210,
3893 unsigned frame,
3894 unsigned uframe,
3895 const struct fotg210_qh *qh,
3896 __hc32 *c_maskp
3897)
3898{
3899 int retval = -ENOSPC;
3900 u8 mask = 0;
3901
3902 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
3903 goto done;
3904
3905 if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
3906 goto done;
3907 if (!qh->c_usecs) {
3908 retval = 0;
3909 *c_maskp = 0;
3910 goto done;
3911 }
3912
3913 /* Make sure this tt's buffer is also available for CSPLITs.
3914 * We pessimize a bit; probably the typical full speed case
3915 * doesn't need the second CSPLIT.
3916 *
3917 * NOTE: both SPLIT and CSPLIT could be checked in just
3918 * one smart pass...
3919 */
3920 mask = 0x03 << (uframe + qh->gap_uf);
3921 *c_maskp = cpu_to_hc32(fotg210, mask << 8);
3922
3923 mask |= 1 << uframe;
3924 if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) {
3925 if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1,
3926 qh->period, qh->c_usecs))
3927 goto done;
3928 if (!check_period(fotg210, frame, uframe + qh->gap_uf,
3929 qh->period, qh->c_usecs))
3930 goto done;
3931 retval = 0;
3932 }
3933done:
3934 return retval;
3935}
3936
3937/* "first fit" scheduling policy used the first time through,
3938 * or when the previous schedule slot can't be re-used.
3939 */
3940static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3941{
3942 int status;
3943 unsigned uframe;
3944 __hc32 c_mask;
3945 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
3946 struct fotg210_qh_hw *hw = qh->hw;
3947
3948 qh_refresh(fotg210, qh);
3949 hw->hw_next = FOTG210_LIST_END(fotg210);
3950 frame = qh->start;
3951
3952 /* reuse the previous schedule slots, if we can */
3953 if (frame < qh->period) {
3954 uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK);
3955 status = check_intr_schedule(fotg210, frame, --uframe,
3956 qh, &c_mask);
3957 } else {
3958 uframe = 0;
3959 c_mask = 0;
3960 status = -ENOSPC;
3961 }
3962
3963 /* else scan the schedule to find a group of slots such that all
3964 * uframes have enough periodic bandwidth available.
3965 */
3966 if (status) {
3967 /* "normal" case, uframing flexible except with splits */
3968 if (qh->period) {
3969 int i;
3970
3971 for (i = qh->period; status && i > 0; --i) {
3972 frame = ++fotg210->random_frame % qh->period;
3973 for (uframe = 0; uframe < 8; uframe++) {
3974 status = check_intr_schedule(fotg210,
3975 frame, uframe, qh,
3976 &c_mask);
3977 if (status == 0)
3978 break;
3979 }
3980 }
3981
3982 /* qh->period == 0 means every uframe */
3983 } else {
3984 frame = 0;
3985 status = check_intr_schedule(fotg210, 0, 0, qh,
3986 &c_mask);
3987 }
3988 if (status)
3989 goto done;
3990 qh->start = frame;
3991
3992 /* reset S-frame and (maybe) C-frame masks */
3993 hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK));
3994 hw->hw_info2 |= qh->period
3995 ? cpu_to_hc32(fotg210, 1 << uframe)
3996 : cpu_to_hc32(fotg210, QH_SMASK);
3997 hw->hw_info2 |= c_mask;
3998 } else
3999 fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
4000
4001 /* stuff into the periodic schedule */
4002 qh_link_periodic(fotg210, qh);
4003done:
4004 return status;
4005}
4006
4007static int intr_submit(
4008 struct fotg210_hcd *fotg210,
4009 struct urb *urb,
4010 struct list_head *qtd_list,
4011 gfp_t mem_flags
4012) {
4013 unsigned epnum;
4014 unsigned long flags;
4015 struct fotg210_qh *qh;
4016 int status;
4017 struct list_head empty;
4018
4019 /* get endpoint and transfer/schedule data */
4020 epnum = urb->ep->desc.bEndpointAddress;
4021
4022 spin_lock_irqsave(&fotg210->lock, flags);
4023
4024 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
4025 status = -ESHUTDOWN;
4026 goto done_not_linked;
4027 }
4028 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
4029 if (unlikely(status))
4030 goto done_not_linked;
4031
4032 /* get qh and force any scheduling errors */
4033 INIT_LIST_HEAD(&empty);
4034 qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
4035 if (qh == NULL) {
4036 status = -ENOMEM;
4037 goto done;
4038 }
4039 if (qh->qh_state == QH_STATE_IDLE) {
4040 status = qh_schedule(fotg210, qh);
4041 if (status)
4042 goto done;
4043 }
4044
4045 /* then queue the urb's tds to the qh */
4046 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
4047 BUG_ON(qh == NULL);
4048
4049 /* ... update usbfs periodic stats */
4050 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
4051
4052done:
4053 if (unlikely(status))
4054 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
4055done_not_linked:
4056 spin_unlock_irqrestore(&fotg210->lock, flags);
4057 if (status)
4058 qtd_list_free(fotg210, urb, qtd_list);
4059
4060 return status;
4061}
4062
4063static void scan_intr(struct fotg210_hcd *fotg210)
4064{
4065 struct fotg210_qh *qh;
4066
4067 list_for_each_entry_safe(qh, fotg210->qh_scan_next,
4068 &fotg210->intr_qh_list, intr_node) {
4069 rescan:
4070 /* clean any finished work for this qh */
4071 if (!list_empty(&qh->qtd_list)) {
4072 int temp;
4073
4074 /*
4075 * Unlinks could happen here; completion reporting
4076 * drops the lock. That's why fotg210->qh_scan_next
4077 * always holds the next qh to scan; if the next qh
4078 * gets unlinked then fotg210->qh_scan_next is adjusted
4079 * in qh_unlink_periodic().
4080 */
4081 temp = qh_completions(fotg210, qh);
4082 if (unlikely(qh->needs_rescan ||
4083 (list_empty(&qh->qtd_list) &&
4084 qh->qh_state == QH_STATE_LINKED)))
4085 start_unlink_intr(fotg210, qh);
4086 else if (temp != 0)
4087 goto rescan;
4088 }
4089 }
4090}
4091
4092/*-------------------------------------------------------------------------*/
4093
4094/* fotg210_iso_stream ops work with both ITD and SITD */
4095
4096static struct fotg210_iso_stream *
4097iso_stream_alloc(gfp_t mem_flags)
4098{
4099 struct fotg210_iso_stream *stream;
4100
4101 stream = kzalloc(sizeof(*stream), mem_flags);
4102 if (likely(stream != NULL)) {
4103 INIT_LIST_HEAD(&stream->td_list);
4104 INIT_LIST_HEAD(&stream->free_list);
4105 stream->next_uframe = -1;
4106 }
4107 return stream;
4108}
4109
4110static void
4111iso_stream_init(
4112 struct fotg210_hcd *fotg210,
4113 struct fotg210_iso_stream *stream,
4114 struct usb_device *dev,
4115 int pipe,
4116 unsigned interval
4117)
4118{
4119 u32 buf1;
4120 unsigned epnum, maxp;
4121 int is_input;
4122 long bandwidth;
4123 unsigned multi;
4124
4125 /*
4126 * this might be a "high bandwidth" highspeed endpoint,
4127 * as encoded in the ep descriptor's wMaxPacket field
4128 */
4129 epnum = usb_pipeendpoint(pipe);
4130 is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
4131 maxp = usb_maxpacket(dev, pipe, !is_input);
4132 if (is_input)
4133 buf1 = (1 << 11);
4134 else
4135 buf1 = 0;
4136
4137 maxp = max_packet(maxp);
4138 multi = hb_mult(maxp);
4139 buf1 |= maxp;
4140 maxp *= multi;
4141
4142 stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum);
4143 stream->buf1 = cpu_to_hc32(fotg210, buf1);
4144 stream->buf2 = cpu_to_hc32(fotg210, multi);
4145
4146 /* usbfs wants to report the average usecs per frame tied up
4147 * when transfers on this endpoint are scheduled ...
4148 */
4149 if (dev->speed == USB_SPEED_FULL) {
4150 interval <<= 3;
4151 stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
4152 is_input, 1, maxp));
4153 stream->usecs /= 8;
4154 } else {
4155 stream->highspeed = 1;
4156 stream->usecs = HS_USECS_ISO(maxp);
4157 }
4158 bandwidth = stream->usecs * 8;
4159 bandwidth /= interval;
4160
4161 stream->bandwidth = bandwidth;
4162 stream->udev = dev;
4163 stream->bEndpointAddress = is_input | epnum;
4164 stream->interval = interval;
4165 stream->maxp = maxp;
4166}
4167
4168static struct fotg210_iso_stream *
4169iso_stream_find(struct fotg210_hcd *fotg210, struct urb *urb)
4170{
4171 unsigned epnum;
4172 struct fotg210_iso_stream *stream;
4173 struct usb_host_endpoint *ep;
4174 unsigned long flags;
4175
4176 epnum = usb_pipeendpoint(urb->pipe);
4177 if (usb_pipein(urb->pipe))
4178 ep = urb->dev->ep_in[epnum];
4179 else
4180 ep = urb->dev->ep_out[epnum];
4181
4182 spin_lock_irqsave(&fotg210->lock, flags);
4183 stream = ep->hcpriv;
4184
4185 if (unlikely(stream == NULL)) {
4186 stream = iso_stream_alloc(GFP_ATOMIC);
4187 if (likely(stream != NULL)) {
4188 ep->hcpriv = stream;
4189 stream->ep = ep;
4190 iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
4191 urb->interval);
4192 }
4193
4194 /* if dev->ep[epnum] is a QH, hw is set */
4195 } else if (unlikely(stream->hw != NULL)) {
4196 fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n",
4197 urb->dev->devpath, epnum,
4198 usb_pipein(urb->pipe) ? "in" : "out");
4199 stream = NULL;
4200 }
4201
4202 spin_unlock_irqrestore(&fotg210->lock, flags);
4203 return stream;
4204}
4205
4206/*-------------------------------------------------------------------------*/
4207
4208/* fotg210_iso_sched ops can be ITD-only or SITD-only */
4209
4210static struct fotg210_iso_sched *
4211iso_sched_alloc(unsigned packets, gfp_t mem_flags)
4212{
4213 struct fotg210_iso_sched *iso_sched;
4214 int size = sizeof(*iso_sched);
4215
4216 size += packets * sizeof(struct fotg210_iso_packet);
4217 iso_sched = kzalloc(size, mem_flags);
4218 if (likely(iso_sched != NULL))
4219 INIT_LIST_HEAD(&iso_sched->td_list);
4220
4221 return iso_sched;
4222}
4223
4224static inline void
4225itd_sched_init(
4226 struct fotg210_hcd *fotg210,
4227 struct fotg210_iso_sched *iso_sched,
4228 struct fotg210_iso_stream *stream,
4229 struct urb *urb
4230)
4231{
4232 unsigned i;
4233 dma_addr_t dma = urb->transfer_dma;
4234
4235 /* how many uframes are needed for these transfers */
4236 iso_sched->span = urb->number_of_packets * stream->interval;
4237
4238 /* figure out per-uframe itd fields that we'll need later
4239 * when we fit new itds into the schedule.
4240 */
4241 for (i = 0; i < urb->number_of_packets; i++) {
4242 struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
4243 unsigned length;
4244 dma_addr_t buf;
4245 u32 trans;
4246
4247 length = urb->iso_frame_desc[i].length;
4248 buf = dma + urb->iso_frame_desc[i].offset;
4249
4250 trans = FOTG210_ISOC_ACTIVE;
4251 trans |= buf & 0x0fff;
4252 if (unlikely(((i + 1) == urb->number_of_packets))
4253 && !(urb->transfer_flags & URB_NO_INTERRUPT))
4254 trans |= FOTG210_ITD_IOC;
4255 trans |= length << 16;
4256 uframe->transaction = cpu_to_hc32(fotg210, trans);
4257
4258 /* might need to cross a buffer page within a uframe */
4259 uframe->bufp = (buf & ~(u64)0x0fff);
4260 buf += length;
4261 if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
4262 uframe->cross = 1;
4263 }
4264}
4265
4266static void
4267iso_sched_free(
4268 struct fotg210_iso_stream *stream,
4269 struct fotg210_iso_sched *iso_sched
4270)
4271{
4272 if (!iso_sched)
4273 return;
4274 /* caller must hold fotg210->lock!*/
4275 list_splice(&iso_sched->td_list, &stream->free_list);
4276 kfree(iso_sched);
4277}
4278
4279static int
4280itd_urb_transaction(
4281 struct fotg210_iso_stream *stream,
4282 struct fotg210_hcd *fotg210,
4283 struct urb *urb,
4284 gfp_t mem_flags
4285)
4286{
4287 struct fotg210_itd *itd;
4288 dma_addr_t itd_dma;
4289 int i;
4290 unsigned num_itds;
4291 struct fotg210_iso_sched *sched;
4292 unsigned long flags;
4293
4294 sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
4295 if (unlikely(sched == NULL))
4296 return -ENOMEM;
4297
4298 itd_sched_init(fotg210, sched, stream, urb);
4299
4300 if (urb->interval < 8)
4301 num_itds = 1 + (sched->span + 7) / 8;
4302 else
4303 num_itds = urb->number_of_packets;
4304
4305 /* allocate/init ITDs */
4306 spin_lock_irqsave(&fotg210->lock, flags);
4307 for (i = 0; i < num_itds; i++) {
4308
4309 /*
4310 * Use iTDs from the free list, but not iTDs that may
4311 * still be in use by the hardware.
4312 */
4313 if (likely(!list_empty(&stream->free_list))) {
4314 itd = list_first_entry(&stream->free_list,
4315 struct fotg210_itd, itd_list);
4316 if (itd->frame == fotg210->now_frame)
4317 goto alloc_itd;
4318 list_del(&itd->itd_list);
4319 itd_dma = itd->itd_dma;
4320 } else {
4321 alloc_itd:
4322 spin_unlock_irqrestore(&fotg210->lock, flags);
4323 itd = dma_pool_alloc(fotg210->itd_pool, mem_flags,
4324 &itd_dma);
4325 spin_lock_irqsave(&fotg210->lock, flags);
4326 if (!itd) {
4327 iso_sched_free(stream, sched);
4328 spin_unlock_irqrestore(&fotg210->lock, flags);
4329 return -ENOMEM;
4330 }
4331 }
4332
4333 memset(itd, 0, sizeof(*itd));
4334 itd->itd_dma = itd_dma;
4335 list_add(&itd->itd_list, &sched->td_list);
4336 }
4337 spin_unlock_irqrestore(&fotg210->lock, flags);
4338
4339 /* temporarily store schedule info in hcpriv */
4340 urb->hcpriv = sched;
4341 urb->error_count = 0;
4342 return 0;
4343}
4344
4345/*-------------------------------------------------------------------------*/
4346
4347static inline int
4348itd_slot_ok(
4349 struct fotg210_hcd *fotg210,
4350 u32 mod,
4351 u32 uframe,
4352 u8 usecs,
4353 u32 period
4354)
4355{
4356 uframe %= period;
4357 do {
4358 /* can't commit more than uframe_periodic_max usec */
4359 if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
4360 > (fotg210->uframe_periodic_max - usecs))
4361 return 0;
4362
4363 /* we know urb->interval is 2^N uframes */
4364 uframe += period;
4365 } while (uframe < mod);
4366 return 1;
4367}
4368
4369/*
4370 * This scheduler plans almost as far into the future as it has actual
4371 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
4372 * "as small as possible" to be cache-friendlier.) That limits the size
4373 * transfers you can stream reliably; avoid more than 64 msec per urb.
4374 * Also avoid queue depths of less than fotg210's worst irq latency (affected
4375 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
4376 * and other factors); or more than about 230 msec total (for portability,
4377 * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
4378 */
4379
4380#define SCHEDULE_SLOP 80 /* microframes */
4381
4382static int
4383iso_stream_schedule(
4384 struct fotg210_hcd *fotg210,
4385 struct urb *urb,
4386 struct fotg210_iso_stream *stream
4387)
4388{
4389 u32 now, next, start, period, span;
4390 int status;
4391 unsigned mod = fotg210->periodic_size << 3;
4392 struct fotg210_iso_sched *sched = urb->hcpriv;
4393
4394 period = urb->interval;
4395 span = sched->span;
4396
4397 if (span > mod - SCHEDULE_SLOP) {
4398 fotg210_dbg(fotg210, "iso request %p too long\n", urb);
4399 status = -EFBIG;
4400 goto fail;
4401 }
4402
4403 now = fotg210_read_frame_index(fotg210) & (mod - 1);
4404
4405 /* Typical case: reuse current schedule, stream is still active.
4406 * Hopefully there are no gaps from the host falling behind
4407 * (irq delays etc), but if there are we'll take the next
4408 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
4409 */
4410 if (likely(!list_empty(&stream->td_list))) {
4411 u32 excess;
4412
4413 /* For high speed devices, allow scheduling within the
4414 * isochronous scheduling threshold. For full speed devices
4415 * and Intel PCI-based controllers, don't (work around for
4416 * Intel ICH9 bug).
4417 */
4418 if (!stream->highspeed && fotg210->fs_i_thresh)
4419 next = now + fotg210->i_thresh;
4420 else
4421 next = now;
4422
4423 /* Fell behind (by up to twice the slop amount)?
4424 * We decide based on the time of the last currently-scheduled
4425 * slot, not the time of the next available slot.
4426 */
4427 excess = (stream->next_uframe - period - next) & (mod - 1);
4428 if (excess >= mod - 2 * SCHEDULE_SLOP)
4429 start = next + excess - mod + period *
4430 DIV_ROUND_UP(mod - excess, period);
4431 else
4432 start = next + excess + period;
4433 if (start - now >= mod) {
4434 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
4435 urb, start - now - period, period,
4436 mod);
4437 status = -EFBIG;
4438 goto fail;
4439 }
4440 }
4441
4442 /* need to schedule; when's the next (u)frame we could start?
4443 * this is bigger than fotg210->i_thresh allows; scheduling itself
4444 * isn't free, the slop should handle reasonably slow cpus. it
4445 * can also help high bandwidth if the dma and irq loads don't
4446 * jump until after the queue is primed.
4447 */
4448 else {
4449 int done = 0;
4450 start = SCHEDULE_SLOP + (now & ~0x07);
4451
4452 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
4453
4454 /* find a uframe slot with enough bandwidth.
4455 * Early uframes are more precious because full-speed
4456 * iso IN transfers can't use late uframes,
4457 * and therefore they should be allocated last.
4458 */
4459 next = start;
4460 start += period;
4461 do {
4462 start--;
4463 /* check schedule: enough space? */
4464 if (itd_slot_ok(fotg210, mod, start,
4465 stream->usecs, period))
4466 done = 1;
4467 } while (start > next && !done);
4468
4469 /* no room in the schedule */
4470 if (!done) {
4471 fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
4472 urb, now, now + mod);
4473 status = -ENOSPC;
4474 goto fail;
4475 }
4476 }
4477
4478 /* Tried to schedule too far into the future? */
4479 if (unlikely(start - now + span - period
4480 >= mod - 2 * SCHEDULE_SLOP)) {
4481 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
4482 urb, start - now, span - period,
4483 mod - 2 * SCHEDULE_SLOP);
4484 status = -EFBIG;
4485 goto fail;
4486 }
4487
4488 stream->next_uframe = start & (mod - 1);
4489
4490 /* report high speed start in uframes; full speed, in frames */
4491 urb->start_frame = stream->next_uframe;
4492 if (!stream->highspeed)
4493 urb->start_frame >>= 3;
4494
4495 /* Make sure scan_isoc() sees these */
4496 if (fotg210->isoc_count == 0)
4497 fotg210->next_frame = now >> 3;
4498 return 0;
4499
4500 fail:
4501 iso_sched_free(stream, sched);
4502 urb->hcpriv = NULL;
4503 return status;
4504}
4505
4506/*-------------------------------------------------------------------------*/
4507
4508static inline void
4509itd_init(struct fotg210_hcd *fotg210, struct fotg210_iso_stream *stream,
4510 struct fotg210_itd *itd)
4511{
4512 int i;
4513
4514 /* it's been recently zeroed */
4515 itd->hw_next = FOTG210_LIST_END(fotg210);
4516 itd->hw_bufp[0] = stream->buf0;
4517 itd->hw_bufp[1] = stream->buf1;
4518 itd->hw_bufp[2] = stream->buf2;
4519
4520 for (i = 0; i < 8; i++)
4521 itd->index[i] = -1;
4522
4523 /* All other fields are filled when scheduling */
4524}
4525
4526static inline void
4527itd_patch(
4528 struct fotg210_hcd *fotg210,
4529 struct fotg210_itd *itd,
4530 struct fotg210_iso_sched *iso_sched,
4531 unsigned index,
4532 u16 uframe
4533)
4534{
4535 struct fotg210_iso_packet *uf = &iso_sched->packet[index];
4536 unsigned pg = itd->pg;
4537
4538 uframe &= 0x07;
4539 itd->index[uframe] = index;
4540
4541 itd->hw_transaction[uframe] = uf->transaction;
4542 itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12);
4543 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0);
4544 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32));
4545
4546 /* iso_frame_desc[].offset must be strictly increasing */
4547 if (unlikely(uf->cross)) {
4548 u64 bufp = uf->bufp + 4096;
4549
4550 itd->pg = ++pg;
4551 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0);
4552 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32));
4553 }
4554}
4555
4556static inline void
4557itd_link(struct fotg210_hcd *fotg210, unsigned frame, struct fotg210_itd *itd)
4558{
4559 union fotg210_shadow *prev = &fotg210->pshadow[frame];
4560 __hc32 *hw_p = &fotg210->periodic[frame];
4561 union fotg210_shadow here = *prev;
4562 __hc32 type = 0;
4563
4564 /* skip any iso nodes which might belong to previous microframes */
4565 while (here.ptr) {
4566 type = Q_NEXT_TYPE(fotg210, *hw_p);
4567 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
4568 break;
4569 prev = periodic_next_shadow(fotg210, prev, type);
4570 hw_p = shadow_next_periodic(fotg210, &here, type);
4571 here = *prev;
4572 }
4573
4574 itd->itd_next = here;
4575 itd->hw_next = *hw_p;
4576 prev->itd = itd;
4577 itd->frame = frame;
4578 wmb();
4579 *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
4580}
4581
4582/* fit urb's itds into the selected schedule slot; activate as needed */
4583static void itd_link_urb(
4584 struct fotg210_hcd *fotg210,
4585 struct urb *urb,
4586 unsigned mod,
4587 struct fotg210_iso_stream *stream
4588)
4589{
4590 int packet;
4591 unsigned next_uframe, uframe, frame;
4592 struct fotg210_iso_sched *iso_sched = urb->hcpriv;
4593 struct fotg210_itd *itd;
4594
4595 next_uframe = stream->next_uframe & (mod - 1);
4596
4597 if (unlikely(list_empty(&stream->td_list))) {
4598 fotg210_to_hcd(fotg210)->self.bandwidth_allocated
4599 += stream->bandwidth;
4600 fotg210_vdbg(fotg210,
4601 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
4602 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
4603 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
4604 urb->interval,
4605 next_uframe >> 3, next_uframe & 0x7);
4606 }
4607
4608 /* fill iTDs uframe by uframe */
4609 for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
4610 if (itd == NULL) {
4611 /* ASSERT: we have all necessary itds */
4612
4613 /* ASSERT: no itds for this endpoint in this uframe */
4614
4615 itd = list_entry(iso_sched->td_list.next,
4616 struct fotg210_itd, itd_list);
4617 list_move_tail(&itd->itd_list, &stream->td_list);
4618 itd->stream = stream;
4619 itd->urb = urb;
4620 itd_init(fotg210, stream, itd);
4621 }
4622
4623 uframe = next_uframe & 0x07;
4624 frame = next_uframe >> 3;
4625
4626 itd_patch(fotg210, itd, iso_sched, packet, uframe);
4627
4628 next_uframe += stream->interval;
4629 next_uframe &= mod - 1;
4630 packet++;
4631
4632 /* link completed itds into the schedule */
4633 if (((next_uframe >> 3) != frame)
4634 || packet == urb->number_of_packets) {
4635 itd_link(fotg210, frame & (fotg210->periodic_size - 1),
4636 itd);
4637 itd = NULL;
4638 }
4639 }
4640 stream->next_uframe = next_uframe;
4641
4642 /* don't need that schedule data any more */
4643 iso_sched_free(stream, iso_sched);
4644 urb->hcpriv = NULL;
4645
4646 ++fotg210->isoc_count;
4647 enable_periodic(fotg210);
4648}
4649
4650#define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
4651 FOTG210_ISOC_XACTERR)
4652
4653/* Process and recycle a completed ITD. Return true iff its urb completed,
4654 * and hence its completion callback probably added things to the hardware
4655 * schedule.
4656 *
4657 * Note that we carefully avoid recycling this descriptor until after any
4658 * completion callback runs, so that it won't be reused quickly. That is,
4659 * assuming (a) no more than two urbs per frame on this endpoint, and also
4660 * (b) only this endpoint's completions submit URBs. It seems some silicon
4661 * corrupts things if you reuse completed descriptors very quickly...
4662 */
4663static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
4664{
4665 struct urb *urb = itd->urb;
4666 struct usb_iso_packet_descriptor *desc;
4667 u32 t;
4668 unsigned uframe;
4669 int urb_index = -1;
4670 struct fotg210_iso_stream *stream = itd->stream;
4671 struct usb_device *dev;
4672 bool retval = false;
4673
4674 /* for each uframe with a packet */
4675 for (uframe = 0; uframe < 8; uframe++) {
4676 if (likely(itd->index[uframe] == -1))
4677 continue;
4678 urb_index = itd->index[uframe];
4679 desc = &urb->iso_frame_desc[urb_index];
4680
4681 t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
4682 itd->hw_transaction[uframe] = 0;
4683
4684 /* report transfer status */
4685 if (unlikely(t & ISO_ERRS)) {
4686 urb->error_count++;
4687 if (t & FOTG210_ISOC_BUF_ERR)
4688 desc->status = usb_pipein(urb->pipe)
4689 ? -ENOSR /* hc couldn't read */
4690 : -ECOMM; /* hc couldn't write */
4691 else if (t & FOTG210_ISOC_BABBLE)
4692 desc->status = -EOVERFLOW;
4693 else /* (t & FOTG210_ISOC_XACTERR) */
4694 desc->status = -EPROTO;
4695
4696 /* HC need not update length with this error */
4697 if (!(t & FOTG210_ISOC_BABBLE)) {
4698 desc->actual_length =
4699 fotg210_itdlen(urb, desc, t);
4700 urb->actual_length += desc->actual_length;
4701 }
4702 } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
4703 desc->status = 0;
4704 desc->actual_length = fotg210_itdlen(urb, desc, t);
4705 urb->actual_length += desc->actual_length;
4706 } else {
4707 /* URB was too late */
4708 desc->status = -EXDEV;
4709 }
4710 }
4711
4712 /* handle completion now? */
4713 if (likely((urb_index + 1) != urb->number_of_packets))
4714 goto done;
4715
4716 /* ASSERT: it's really the last itd for this urb
4717 list_for_each_entry (itd, &stream->td_list, itd_list)
4718 BUG_ON (itd->urb == urb);
4719 */
4720
4721 /* give urb back to the driver; completion often (re)submits */
4722 dev = urb->dev;
4723 fotg210_urb_done(fotg210, urb, 0);
4724 retval = true;
4725 urb = NULL;
4726
4727 --fotg210->isoc_count;
4728 disable_periodic(fotg210);
4729
4730 if (unlikely(list_is_singular(&stream->td_list))) {
4731 fotg210_to_hcd(fotg210)->self.bandwidth_allocated
4732 -= stream->bandwidth;
4733 fotg210_vdbg(fotg210,
4734 "deschedule devp %s ep%d%s-iso\n",
4735 dev->devpath, stream->bEndpointAddress & 0x0f,
4736 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
4737 }
4738
4739done:
4740 itd->urb = NULL;
4741
4742 /* Add to the end of the free list for later reuse */
4743 list_move_tail(&itd->itd_list, &stream->free_list);
4744
4745 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
4746 if (list_empty(&stream->td_list)) {
4747 list_splice_tail_init(&stream->free_list,
4748 &fotg210->cached_itd_list);
4749 start_free_itds(fotg210);
4750 }
4751
4752 return retval;
4753}
4754
4755/*-------------------------------------------------------------------------*/
4756
4757static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
4758 gfp_t mem_flags)
4759{
4760 int status = -EINVAL;
4761 unsigned long flags;
4762 struct fotg210_iso_stream *stream;
4763
4764 /* Get iso_stream head */
4765 stream = iso_stream_find(fotg210, urb);
4766 if (unlikely(stream == NULL)) {
4767 fotg210_dbg(fotg210, "can't get iso stream\n");
4768 return -ENOMEM;
4769 }
4770 if (unlikely(urb->interval != stream->interval &&
4771 fotg210_port_speed(fotg210, 0) ==
4772 USB_PORT_STAT_HIGH_SPEED)) {
4773 fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n",
4774 stream->interval, urb->interval);
4775 goto done;
4776 }
4777
4778#ifdef FOTG210_URB_TRACE
4779 fotg210_dbg(fotg210,
4780 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
4781 __func__, urb->dev->devpath, urb,
4782 usb_pipeendpoint(urb->pipe),
4783 usb_pipein(urb->pipe) ? "in" : "out",
4784 urb->transfer_buffer_length,
4785 urb->number_of_packets, urb->interval,
4786 stream);
4787#endif
4788
4789 /* allocate ITDs w/o locking anything */
4790 status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
4791 if (unlikely(status < 0)) {
4792 fotg210_dbg(fotg210, "can't init itds\n");
4793 goto done;
4794 }
4795
4796 /* schedule ... need to lock */
4797 spin_lock_irqsave(&fotg210->lock, flags);
4798 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
4799 status = -ESHUTDOWN;
4800 goto done_not_linked;
4801 }
4802 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
4803 if (unlikely(status))
4804 goto done_not_linked;
4805 status = iso_stream_schedule(fotg210, urb, stream);
4806 if (likely(status == 0))
4807 itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream);
4808 else
4809 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
4810 done_not_linked:
4811 spin_unlock_irqrestore(&fotg210->lock, flags);
4812 done:
4813 return status;
4814}
4815
4816/*-------------------------------------------------------------------------*/
4817
4818static void scan_isoc(struct fotg210_hcd *fotg210)
4819{
4820 unsigned uf, now_frame, frame;
4821 unsigned fmask = fotg210->periodic_size - 1;
4822 bool modified, live;
4823
4824 /*
4825 * When running, scan from last scan point up to "now"
4826 * else clean up by scanning everything that's left.
4827 * Touches as few pages as possible: cache-friendly.
4828 */
4829 if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
4830 uf = fotg210_read_frame_index(fotg210);
4831 now_frame = (uf >> 3) & fmask;
4832 live = true;
4833 } else {
4834 now_frame = (fotg210->next_frame - 1) & fmask;
4835 live = false;
4836 }
4837 fotg210->now_frame = now_frame;
4838
4839 frame = fotg210->next_frame;
4840 for (;;) {
4841 union fotg210_shadow q, *q_p;
4842 __hc32 type, *hw_p;
4843
4844restart:
4845 /* scan each element in frame's queue for completions */
4846 q_p = &fotg210->pshadow[frame];
4847 hw_p = &fotg210->periodic[frame];
4848 q.ptr = q_p->ptr;
4849 type = Q_NEXT_TYPE(fotg210, *hw_p);
4850 modified = false;
4851
4852 while (q.ptr != NULL) {
4853 switch (hc32_to_cpu(fotg210, type)) {
4854 case Q_TYPE_ITD:
4855 /* If this ITD is still active, leave it for
4856 * later processing ... check the next entry.
4857 * No need to check for activity unless the
4858 * frame is current.
4859 */
4860 if (frame == now_frame && live) {
4861 rmb();
4862 for (uf = 0; uf < 8; uf++) {
4863 if (q.itd->hw_transaction[uf] &
4864 ITD_ACTIVE(fotg210))
4865 break;
4866 }
4867 if (uf < 8) {
4868 q_p = &q.itd->itd_next;
4869 hw_p = &q.itd->hw_next;
4870 type = Q_NEXT_TYPE(fotg210,
4871 q.itd->hw_next);
4872 q = *q_p;
4873 break;
4874 }
4875 }
4876
4877 /* Take finished ITDs out of the schedule
4878 * and process them: recycle, maybe report
4879 * URB completion. HC won't cache the
4880 * pointer for much longer, if at all.
4881 */
4882 *q_p = q.itd->itd_next;
4883 *hw_p = q.itd->hw_next;
4884 type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
4885 wmb();
4886 modified = itd_complete(fotg210, q.itd);
4887 q = *q_p;
4888 break;
4889 default:
4890 fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
4891 type, frame, q.ptr);
4892 /* FALL THROUGH */
4893 case Q_TYPE_QH:
4894 case Q_TYPE_FSTN:
4895 /* End of the iTDs and siTDs */
4896 q.ptr = NULL;
4897 break;
4898 }
4899
4900 /* assume completion callbacks modify the queue */
4901 if (unlikely(modified && fotg210->isoc_count > 0))
4902 goto restart;
4903 }
4904
4905 /* Stop when we have reached the current frame */
4906 if (frame == now_frame)
4907 break;
4908 frame = (frame + 1) & fmask;
4909 }
4910 fotg210->next_frame = now_frame;
4911}
4912/*-------------------------------------------------------------------------*/
4913/*
4914 * Display / Set uframe_periodic_max
4915 */
4916static ssize_t show_uframe_periodic_max(struct device *dev,
4917 struct device_attribute *attr,
4918 char *buf)
4919{
4920 struct fotg210_hcd *fotg210;
4921 int n;
4922
4923 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4924 n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
4925 return n;
4926}
4927
4928
4929static ssize_t store_uframe_periodic_max(struct device *dev,
4930 struct device_attribute *attr,
4931 const char *buf, size_t count)
4932{
4933 struct fotg210_hcd *fotg210;
4934 unsigned uframe_periodic_max;
4935 unsigned frame, uframe;
4936 unsigned short allocated_max;
4937 unsigned long flags;
4938 ssize_t ret;
4939
4940 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4941 if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
4942 return -EINVAL;
4943
4944 if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
4945 fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n",
4946 uframe_periodic_max);
4947 return -EINVAL;
4948 }
4949
4950 ret = -EINVAL;
4951
4952 /*
4953 * lock, so that our checking does not race with possible periodic
4954 * bandwidth allocation through submitting new urbs.
4955 */
4956 spin_lock_irqsave(&fotg210->lock, flags);
4957
4958 /*
4959 * for request to decrease max periodic bandwidth, we have to check
4960 * every microframe in the schedule to see whether the decrease is
4961 * possible.
4962 */
4963 if (uframe_periodic_max < fotg210->uframe_periodic_max) {
4964 allocated_max = 0;
4965
4966 for (frame = 0; frame < fotg210->periodic_size; ++frame)
4967 for (uframe = 0; uframe < 7; ++uframe)
4968 allocated_max = max(allocated_max,
4969 periodic_usecs(fotg210, frame, uframe));
4970
4971 if (allocated_max > uframe_periodic_max) {
4972 fotg210_info(fotg210,
4973 "cannot decrease uframe_periodic_max becase "
4974 "periodic bandwidth is already allocated "
4975 "(%u > %u)\n",
4976 allocated_max, uframe_periodic_max);
4977 goto out_unlock;
4978 }
4979 }
4980
4981 /* increasing is always ok */
4982
4983 fotg210_info(fotg210, "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
4984 100 * uframe_periodic_max/125, uframe_periodic_max);
4985
4986 if (uframe_periodic_max != 100)
4987 fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
4988
4989 fotg210->uframe_periodic_max = uframe_periodic_max;
4990 ret = count;
4991
4992out_unlock:
4993 spin_unlock_irqrestore(&fotg210->lock, flags);
4994 return ret;
4995}
4996
4997static DEVICE_ATTR(uframe_periodic_max, 0644, show_uframe_periodic_max,
4998 store_uframe_periodic_max);
4999
5000static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
5001{
5002 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
5003 int i = 0;
5004
5005 if (i)
5006 goto out;
5007
5008 i = device_create_file(controller, &dev_attr_uframe_periodic_max);
5009out:
5010 return i;
5011}
5012
5013static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
5014{
5015 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
5016
5017 device_remove_file(controller, &dev_attr_uframe_periodic_max);
5018}
5019/*-------------------------------------------------------------------------*/
5020
5021/* On some systems, leaving remote wakeup enabled prevents system shutdown.
5022 * The firmware seems to think that powering off is a wakeup event!
5023 * This routine turns off remote wakeup and everything else, on all ports.
5024 */
5025static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
5026{
5027 u32 __iomem *status_reg = &fotg210->regs->port_status;
5028
5029 fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
5030}
5031
5032/*
5033 * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
5034 * Must be called with interrupts enabled and the lock not held.
5035 */
5036static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
5037{
5038 fotg210_halt(fotg210);
5039
5040 spin_lock_irq(&fotg210->lock);
5041 fotg210->rh_state = FOTG210_RH_HALTED;
5042 fotg210_turn_off_all_ports(fotg210);
5043 spin_unlock_irq(&fotg210->lock);
5044}
5045
5046/* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
5047 * This forcibly disables dma and IRQs, helping kexec and other cases
5048 * where the next system software may expect clean state.
5049 */
5050static void fotg210_shutdown(struct usb_hcd *hcd)
5051{
5052 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5053
5054 spin_lock_irq(&fotg210->lock);
5055 fotg210->shutdown = true;
5056 fotg210->rh_state = FOTG210_RH_STOPPING;
5057 fotg210->enabled_hrtimer_events = 0;
5058 spin_unlock_irq(&fotg210->lock);
5059
5060 fotg210_silence_controller(fotg210);
5061
5062 hrtimer_cancel(&fotg210->hrtimer);
5063}
5064
5065/*-------------------------------------------------------------------------*/
5066
5067/*
5068 * fotg210_work is called from some interrupts, timers, and so on.
5069 * it calls driver completion functions, after dropping fotg210->lock.
5070 */
5071static void fotg210_work(struct fotg210_hcd *fotg210)
5072{
5073 /* another CPU may drop fotg210->lock during a schedule scan while
5074 * it reports urb completions. this flag guards against bogus
5075 * attempts at re-entrant schedule scanning.
5076 */
5077 if (fotg210->scanning) {
5078 fotg210->need_rescan = true;
5079 return;
5080 }
5081 fotg210->scanning = true;
5082
5083 rescan:
5084 fotg210->need_rescan = false;
5085 if (fotg210->async_count)
5086 scan_async(fotg210);
5087 if (fotg210->intr_count > 0)
5088 scan_intr(fotg210);
5089 if (fotg210->isoc_count > 0)
5090 scan_isoc(fotg210);
5091 if (fotg210->need_rescan)
5092 goto rescan;
5093 fotg210->scanning = false;
5094
5095 /* the IO watchdog guards against hardware or driver bugs that
5096 * misplace IRQs, and should let us run completely without IRQs.
5097 * such lossage has been observed on both VT6202 and VT8235.
5098 */
5099 turn_on_io_watchdog(fotg210);
5100}
5101
5102/*
5103 * Called when the fotg210_hcd module is removed.
5104 */
5105static void fotg210_stop(struct usb_hcd *hcd)
5106{
5107 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5108
5109 fotg210_dbg(fotg210, "stop\n");
5110
5111 /* no more interrupts ... */
5112
5113 spin_lock_irq(&fotg210->lock);
5114 fotg210->enabled_hrtimer_events = 0;
5115 spin_unlock_irq(&fotg210->lock);
5116
5117 fotg210_quiesce(fotg210);
5118 fotg210_silence_controller(fotg210);
5119 fotg210_reset(fotg210);
5120
5121 hrtimer_cancel(&fotg210->hrtimer);
5122 remove_sysfs_files(fotg210);
5123 remove_debug_files(fotg210);
5124
5125 /* root hub is shut down separately (first, when possible) */
5126 spin_lock_irq(&fotg210->lock);
5127 end_free_itds(fotg210);
5128 spin_unlock_irq(&fotg210->lock);
5129 fotg210_mem_cleanup(fotg210);
5130
5131#ifdef FOTG210_STATS
5132 fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
5133 fotg210->stats.normal, fotg210->stats.error, fotg210->stats.iaa,
5134 fotg210->stats.lost_iaa);
5135 fotg210_dbg(fotg210, "complete %ld unlink %ld\n",
5136 fotg210->stats.complete, fotg210->stats.unlink);
5137#endif
5138
5139 dbg_status(fotg210, "fotg210_stop completed",
5140 fotg210_readl(fotg210, &fotg210->regs->status));
5141}
5142
5143/* one-time init, only for memory state */
5144static int hcd_fotg210_init(struct usb_hcd *hcd)
5145{
5146 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5147 u32 temp;
5148 int retval;
5149 u32 hcc_params;
5150 struct fotg210_qh_hw *hw;
5151
5152 spin_lock_init(&fotg210->lock);
5153
5154 /*
5155 * keep io watchdog by default, those good HCDs could turn off it later
5156 */
5157 fotg210->need_io_watchdog = 1;
5158
5159 hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
5160 fotg210->hrtimer.function = fotg210_hrtimer_func;
5161 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
5162
5163 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
5164
5165 /*
5166 * by default set standard 80% (== 100 usec/uframe) max periodic
5167 * bandwidth as required by USB 2.0
5168 */
5169 fotg210->uframe_periodic_max = 100;
5170
5171 /*
5172 * hw default: 1K periodic list heads, one per frame.
5173 * periodic_size can shrink by USBCMD update if hcc_params allows.
5174 */
5175 fotg210->periodic_size = DEFAULT_I_TDPS;
5176 INIT_LIST_HEAD(&fotg210->intr_qh_list);
5177 INIT_LIST_HEAD(&fotg210->cached_itd_list);
5178
5179 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5180 /* periodic schedule size can be smaller than default */
5181 switch (FOTG210_TUNE_FLS) {
5182 case 0:
5183 fotg210->periodic_size = 1024;
5184 break;
5185 case 1:
5186 fotg210->periodic_size = 512;
5187 break;
5188 case 2:
5189 fotg210->periodic_size = 256;
5190 break;
5191 default:
5192 BUG();
5193 }
5194 }
5195 retval = fotg210_mem_init(fotg210, GFP_KERNEL);
5196 if (retval < 0)
5197 return retval;
5198
5199 /* controllers may cache some of the periodic schedule ... */
5200 fotg210->i_thresh = 2;
5201
5202 /*
5203 * dedicate a qh for the async ring head, since we couldn't unlink
5204 * a 'real' qh without stopping the async schedule [4.8]. use it
5205 * as the 'reclamation list head' too.
5206 * its dummy is used in hw_alt_next of many tds, to prevent the qh
5207 * from automatically advancing to the next td after short reads.
5208 */
5209 fotg210->async->qh_next.qh = NULL;
5210 hw = fotg210->async->hw;
5211 hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma);
5212 hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD);
5213 hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
5214 hw->hw_qtd_next = FOTG210_LIST_END(fotg210);
5215 fotg210->async->qh_state = QH_STATE_LINKED;
5216 hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma);
5217
5218 /* clear interrupt enables, set irq latency */
5219 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
5220 log2_irq_thresh = 0;
5221 temp = 1 << (16 + log2_irq_thresh);
5222 if (HCC_CANPARK(hcc_params)) {
5223 /* HW default park == 3, on hardware that supports it (like
5224 * NVidia and ALI silicon), maximizes throughput on the async
5225 * schedule by avoiding QH fetches between transfers.
5226 *
5227 * With fast usb storage devices and NForce2, "park" seems to
5228 * make problems: throughput reduction (!), data errors...
5229 */
5230 if (park) {
5231 park = min_t(unsigned, park, 3);
5232 temp |= CMD_PARK;
5233 temp |= park << 8;
5234 }
5235 fotg210_dbg(fotg210, "park %d\n", park);
5236 }
5237 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5238 /* periodic schedule size can be smaller than default */
5239 temp &= ~(3 << 2);
5240 temp |= (FOTG210_TUNE_FLS << 2);
5241 }
5242 fotg210->command = temp;
5243
5244 /* Accept arbitrarily long scatter-gather lists */
5245 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
5246 hcd->self.sg_tablesize = ~0;
5247 return 0;
5248}
5249
5250/* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
5251static int fotg210_run(struct usb_hcd *hcd)
5252{
5253 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5254 u32 temp;
5255 u32 hcc_params;
5256
5257 hcd->uses_new_polling = 1;
5258
5259 /* EHCI spec section 4.1 */
5260
5261 fotg210_writel(fotg210, fotg210->periodic_dma,
5262 &fotg210->regs->frame_list);
5263 fotg210_writel(fotg210, (u32)fotg210->async->qh_dma,
5264 &fotg210->regs->async_next);
5265
5266 /*
5267 * hcc_params controls whether fotg210->regs->segment must (!!!)
5268 * be used; it constrains QH/ITD/SITD and QTD locations.
5269 * pci_pool consistent memory always uses segment zero.
5270 * streaming mappings for I/O buffers, like pci_map_single(),
5271 * can return segments above 4GB, if the device allows.
5272 *
5273 * NOTE: the dma mask is visible through dma_supported(), so
5274 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
5275 * Scsi_Host.highmem_io, and so forth. It's readonly to all
5276 * host side drivers though.
5277 */
5278 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
5279
5280 /*
5281 * Philips, Intel, and maybe others need CMD_RUN before the
5282 * root hub will detect new devices (why?); NEC doesn't
5283 */
5284 fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
5285 fotg210->command |= CMD_RUN;
5286 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
5287 dbg_cmd(fotg210, "init", fotg210->command);
5288
5289 /*
5290 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5291 * are explicitly handed to companion controller(s), so no TT is
5292 * involved with the root hub. (Except where one is integrated,
5293 * and there's no companion controller unless maybe for USB OTG.)
5294 *
5295 * Turning on the CF flag will transfer ownership of all ports
5296 * from the companions to the EHCI controller. If any of the
5297 * companions are in the middle of a port reset at the time, it
5298 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
5299 * guarantees that no resets are in progress. After we set CF,
5300 * a short delay lets the hardware catch up; new resets shouldn't
5301 * be started before the port switching actions could complete.
5302 */
5303 down_write(&ehci_cf_port_reset_rwsem);
5304 fotg210->rh_state = FOTG210_RH_RUNNING;
5305 /* unblock posted writes */
5306 fotg210_readl(fotg210, &fotg210->regs->command);
5307 msleep(5);
5308 up_write(&ehci_cf_port_reset_rwsem);
5309 fotg210->last_periodic_enable = ktime_get_real();
5310
5311 temp = HC_VERSION(fotg210,
5312 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
5313 fotg210_info(fotg210,
5314 "USB %x.%x started, EHCI %x.%02x\n",
5315 ((fotg210->sbrn & 0xf0)>>4), (fotg210->sbrn & 0x0f),
5316 temp >> 8, temp & 0xff);
5317
5318 fotg210_writel(fotg210, INTR_MASK,
5319 &fotg210->regs->intr_enable); /* Turn On Interrupts */
5320
5321 /* GRR this is run-once init(), being done every time the HC starts.
5322 * So long as they're part of class devices, we can't do it init()
5323 * since the class device isn't created that early.
5324 */
5325 create_debug_files(fotg210);
5326 create_sysfs_files(fotg210);
5327
5328 return 0;
5329}
5330
5331static int fotg210_setup(struct usb_hcd *hcd)
5332{
5333 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5334 int retval;
5335
5336 fotg210->regs = (void __iomem *)fotg210->caps +
5337 HC_LENGTH(fotg210,
5338 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
5339 dbg_hcs_params(fotg210, "reset");
5340 dbg_hcc_params(fotg210, "reset");
5341
5342 /* cache this readonly data; minimize chip reads */
5343 fotg210->hcs_params = fotg210_readl(fotg210,
5344 &fotg210->caps->hcs_params);
5345
5346 fotg210->sbrn = HCD_USB2;
5347
5348 /* data structure init */
5349 retval = hcd_fotg210_init(hcd);
5350 if (retval)
5351 return retval;
5352
5353 retval = fotg210_halt(fotg210);
5354 if (retval)
5355 return retval;
5356
5357 fotg210_reset(fotg210);
5358
5359 return 0;
5360}
5361
5362/*-------------------------------------------------------------------------*/
5363
5364static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
5365{
5366 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5367 u32 status, masked_status, pcd_status = 0, cmd;
5368 int bh;
5369
5370 spin_lock(&fotg210->lock);
5371
5372 status = fotg210_readl(fotg210, &fotg210->regs->status);
5373
5374 /* e.g. cardbus physical eject */
5375 if (status == ~(u32) 0) {
5376 fotg210_dbg(fotg210, "device removed\n");
5377 goto dead;
5378 }
5379
5380 /*
5381 * We don't use STS_FLR, but some controllers don't like it to
5382 * remain on, so mask it out along with the other status bits.
5383 */
5384 masked_status = status & (INTR_MASK | STS_FLR);
5385
5386 /* Shared IRQ? */
5387 if (!masked_status ||
5388 unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
5389 spin_unlock(&fotg210->lock);
5390 return IRQ_NONE;
5391 }
5392
5393 /* clear (just) interrupts */
5394 fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
5395 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
5396 bh = 0;
5397
5398#ifdef VERBOSE_DEBUG
5399 /* unrequested/ignored: Frame List Rollover */
5400 dbg_status(fotg210, "irq", status);
5401#endif
5402
5403 /* INT, ERR, and IAA interrupt rates can be throttled */
5404
5405 /* normal [4.15.1.2] or error [4.15.1.1] completion */
5406 if (likely((status & (STS_INT|STS_ERR)) != 0)) {
5407 if (likely((status & STS_ERR) == 0))
5408 COUNT(fotg210->stats.normal);
5409 else
5410 COUNT(fotg210->stats.error);
5411 bh = 1;
5412 }
5413
5414 /* complete the unlinking of some qh [4.15.2.3] */
5415 if (status & STS_IAA) {
5416
5417 /* Turn off the IAA watchdog */
5418 fotg210->enabled_hrtimer_events &=
5419 ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
5420
5421 /*
5422 * Mild optimization: Allow another IAAD to reset the
5423 * hrtimer, if one occurs before the next expiration.
5424 * In theory we could always cancel the hrtimer, but
5425 * tests show that about half the time it will be reset
5426 * for some other event anyway.
5427 */
5428 if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
5429 ++fotg210->next_hrtimer_event;
5430
5431 /* guard against (alleged) silicon errata */
5432 if (cmd & CMD_IAAD)
5433 fotg210_dbg(fotg210, "IAA with IAAD still set?\n");
5434 if (fotg210->async_iaa) {
5435 COUNT(fotg210->stats.iaa);
5436 end_unlink_async(fotg210);
5437 } else
5438 fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
5439 }
5440
5441 /* remote wakeup [4.3.1] */
5442 if (status & STS_PCD) {
5443 int pstatus;
5444 u32 __iomem *status_reg = &fotg210->regs->port_status;
5445
5446 /* kick root hub later */
5447 pcd_status = status;
5448
5449 /* resume root hub? */
5450 if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
5451 usb_hcd_resume_root_hub(hcd);
5452
5453 pstatus = fotg210_readl(fotg210, status_reg);
5454
5455 if (test_bit(0, &fotg210->suspended_ports) &&
5456 ((pstatus & PORT_RESUME) ||
5457 !(pstatus & PORT_SUSPEND)) &&
5458 (pstatus & PORT_PE) &&
5459 fotg210->reset_done[0] == 0) {
5460
5461 /* start 20 msec resume signaling from this port,
5462 * and make khubd collect PORT_STAT_C_SUSPEND to
5463 * stop that signaling. Use 5 ms extra for safety,
5464 * like usb_port_resume() does.
5465 */
5466 fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25);
5467 set_bit(0, &fotg210->resuming_ports);
5468 fotg210_dbg(fotg210, "port 1 remote wakeup\n");
5469 mod_timer(&hcd->rh_timer, fotg210->reset_done[0]);
5470 }
5471 }
5472
5473 /* PCI errors [4.15.2.4] */
5474 if (unlikely((status & STS_FATAL) != 0)) {
5475 fotg210_err(fotg210, "fatal error\n");
5476 dbg_cmd(fotg210, "fatal", cmd);
5477 dbg_status(fotg210, "fatal", status);
5478dead:
5479 usb_hc_died(hcd);
5480
5481 /* Don't let the controller do anything more */
5482 fotg210->shutdown = true;
5483 fotg210->rh_state = FOTG210_RH_STOPPING;
5484 fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
5485 fotg210_writel(fotg210, fotg210->command,
5486 &fotg210->regs->command);
5487 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
5488 fotg210_handle_controller_death(fotg210);
5489
5490 /* Handle completions when the controller stops */
5491 bh = 0;
5492 }
5493
5494 if (bh)
5495 fotg210_work(fotg210);
5496 spin_unlock(&fotg210->lock);
5497 if (pcd_status)
5498 usb_hcd_poll_rh_status(hcd);
5499 return IRQ_HANDLED;
5500}
5501
5502/*-------------------------------------------------------------------------*/
5503
5504/*
5505 * non-error returns are a promise to giveback() the urb later
5506 * we drop ownership so next owner (or urb unlink) can get it
5507 *
5508 * urb + dev is in hcd.self.controller.urb_list
5509 * we're queueing TDs onto software and hardware lists
5510 *
5511 * hcd-specific init for hcpriv hasn't been done yet
5512 *
5513 * NOTE: control, bulk, and interrupt share the same code to append TDs
5514 * to a (possibly active) QH, and the same QH scanning code.
5515 */
5516static int fotg210_urb_enqueue(
5517 struct usb_hcd *hcd,
5518 struct urb *urb,
5519 gfp_t mem_flags
5520) {
5521 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5522 struct list_head qtd_list;
5523
5524 INIT_LIST_HEAD(&qtd_list);
5525
5526 switch (usb_pipetype(urb->pipe)) {
5527 case PIPE_CONTROL:
5528 /* qh_completions() code doesn't handle all the fault cases
5529 * in multi-TD control transfers. Even 1KB is rare anyway.
5530 */
5531 if (urb->transfer_buffer_length > (16 * 1024))
5532 return -EMSGSIZE;
5533 /* FALLTHROUGH */
5534 /* case PIPE_BULK: */
5535 default:
5536 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5537 return -ENOMEM;
5538 return submit_async(fotg210, urb, &qtd_list, mem_flags);
5539
5540 case PIPE_INTERRUPT:
5541 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5542 return -ENOMEM;
5543 return intr_submit(fotg210, urb, &qtd_list, mem_flags);
5544
5545 case PIPE_ISOCHRONOUS:
5546 return itd_submit(fotg210, urb, mem_flags);
5547 }
5548}
5549
5550/* remove from hardware lists
5551 * completions normally happen asynchronously
5552 */
5553
5554static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
5555{
5556 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5557 struct fotg210_qh *qh;
5558 unsigned long flags;
5559 int rc;
5560
5561 spin_lock_irqsave(&fotg210->lock, flags);
5562 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
5563 if (rc)
5564 goto done;
5565
5566 switch (usb_pipetype(urb->pipe)) {
5567 /* case PIPE_CONTROL: */
5568 /* case PIPE_BULK:*/
5569 default:
5570 qh = (struct fotg210_qh *) urb->hcpriv;
5571 if (!qh)
5572 break;
5573 switch (qh->qh_state) {
5574 case QH_STATE_LINKED:
5575 case QH_STATE_COMPLETING:
5576 start_unlink_async(fotg210, qh);
5577 break;
5578 case QH_STATE_UNLINK:
5579 case QH_STATE_UNLINK_WAIT:
5580 /* already started */
5581 break;
5582 case QH_STATE_IDLE:
5583 /* QH might be waiting for a Clear-TT-Buffer */
5584 qh_completions(fotg210, qh);
5585 break;
5586 }
5587 break;
5588
5589 case PIPE_INTERRUPT:
5590 qh = (struct fotg210_qh *) urb->hcpriv;
5591 if (!qh)
5592 break;
5593 switch (qh->qh_state) {
5594 case QH_STATE_LINKED:
5595 case QH_STATE_COMPLETING:
5596 start_unlink_intr(fotg210, qh);
5597 break;
5598 case QH_STATE_IDLE:
5599 qh_completions(fotg210, qh);
5600 break;
5601 default:
5602 fotg210_dbg(fotg210, "bogus qh %p state %d\n",
5603 qh, qh->qh_state);
5604 goto done;
5605 }
5606 break;
5607
5608 case PIPE_ISOCHRONOUS:
5609 /* itd... */
5610
5611 /* wait till next completion, do it then. */
5612 /* completion irqs can wait up to 1024 msec, */
5613 break;
5614 }
5615done:
5616 spin_unlock_irqrestore(&fotg210->lock, flags);
5617 return rc;
5618}
5619
5620/*-------------------------------------------------------------------------*/
5621
5622/* bulk qh holds the data toggle */
5623
5624static void
5625fotg210_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
5626{
5627 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5628 unsigned long flags;
5629 struct fotg210_qh *qh, *tmp;
5630
5631 /* ASSERT: any requests/urbs are being unlinked */
5632 /* ASSERT: nobody can be submitting urbs for this any more */
5633
5634rescan:
5635 spin_lock_irqsave(&fotg210->lock, flags);
5636 qh = ep->hcpriv;
5637 if (!qh)
5638 goto done;
5639
5640 /* endpoints can be iso streams. for now, we don't
5641 * accelerate iso completions ... so spin a while.
5642 */
5643 if (qh->hw == NULL) {
5644 struct fotg210_iso_stream *stream = ep->hcpriv;
5645
5646 if (!list_empty(&stream->td_list))
5647 goto idle_timeout;
5648
5649 /* BUG_ON(!list_empty(&stream->free_list)); */
5650 kfree(stream);
5651 goto done;
5652 }
5653
5654 if (fotg210->rh_state < FOTG210_RH_RUNNING)
5655 qh->qh_state = QH_STATE_IDLE;
5656 switch (qh->qh_state) {
5657 case QH_STATE_LINKED:
5658 case QH_STATE_COMPLETING:
5659 for (tmp = fotg210->async->qh_next.qh;
5660 tmp && tmp != qh;
5661 tmp = tmp->qh_next.qh)
5662 continue;
5663 /* periodic qh self-unlinks on empty, and a COMPLETING qh
5664 * may already be unlinked.
5665 */
5666 if (tmp)
5667 start_unlink_async(fotg210, qh);
5668 /* FALL THROUGH */
5669 case QH_STATE_UNLINK: /* wait for hw to finish? */
5670 case QH_STATE_UNLINK_WAIT:
5671idle_timeout:
5672 spin_unlock_irqrestore(&fotg210->lock, flags);
5673 schedule_timeout_uninterruptible(1);
5674 goto rescan;
5675 case QH_STATE_IDLE: /* fully unlinked */
5676 if (qh->clearing_tt)
5677 goto idle_timeout;
5678 if (list_empty(&qh->qtd_list)) {
5679 qh_destroy(fotg210, qh);
5680 break;
5681 }
5682 /* else FALL THROUGH */
5683 default:
5684 /* caller was supposed to have unlinked any requests;
5685 * that's not our job. just leak this memory.
5686 */
5687 fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n",
5688 qh, ep->desc.bEndpointAddress, qh->qh_state,
5689 list_empty(&qh->qtd_list) ? "" : "(has tds)");
5690 break;
5691 }
5692 done:
5693 ep->hcpriv = NULL;
5694 spin_unlock_irqrestore(&fotg210->lock, flags);
5695}
5696
5697static void
5698fotg210_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
5699{
5700 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5701 struct fotg210_qh *qh;
5702 int eptype = usb_endpoint_type(&ep->desc);
5703 int epnum = usb_endpoint_num(&ep->desc);
5704 int is_out = usb_endpoint_dir_out(&ep->desc);
5705 unsigned long flags;
5706
5707 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
5708 return;
5709
5710 spin_lock_irqsave(&fotg210->lock, flags);
5711 qh = ep->hcpriv;
5712
5713 /* For Bulk and Interrupt endpoints we maintain the toggle state
5714 * in the hardware; the toggle bits in udev aren't used at all.
5715 * When an endpoint is reset by usb_clear_halt() we must reset
5716 * the toggle bit in the QH.
5717 */
5718 if (qh) {
5719 usb_settoggle(qh->dev, epnum, is_out, 0);
5720 if (!list_empty(&qh->qtd_list)) {
5721 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
5722 } else if (qh->qh_state == QH_STATE_LINKED ||
5723 qh->qh_state == QH_STATE_COMPLETING) {
5724
5725 /* The toggle value in the QH can't be updated
5726 * while the QH is active. Unlink it now;
5727 * re-linking will call qh_refresh().
5728 */
5729 if (eptype == USB_ENDPOINT_XFER_BULK)
5730 start_unlink_async(fotg210, qh);
5731 else
5732 start_unlink_intr(fotg210, qh);
5733 }
5734 }
5735 spin_unlock_irqrestore(&fotg210->lock, flags);
5736}
5737
5738static int fotg210_get_frame(struct usb_hcd *hcd)
5739{
5740 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5741 return (fotg210_read_frame_index(fotg210) >> 3) %
5742 fotg210->periodic_size;
5743}
5744
5745/*-------------------------------------------------------------------------*/
5746
5747/*
5748 * The EHCI in ChipIdea HDRC cannot be a separate module or device,
5749 * because its registers (and irq) are shared between host/gadget/otg
5750 * functions and in order to facilitate role switching we cannot
5751 * give the fotg210 driver exclusive access to those.
5752 */
5753MODULE_DESCRIPTION(DRIVER_DESC);
5754MODULE_AUTHOR(DRIVER_AUTHOR);
5755MODULE_LICENSE("GPL");
5756
5757static const struct hc_driver fotg210_fotg210_hc_driver = {
5758 .description = hcd_name,
5759 .product_desc = "Faraday USB2.0 Host Controller",
5760 .hcd_priv_size = sizeof(struct fotg210_hcd),
5761
5762 /*
5763 * generic hardware linkage
5764 */
5765 .irq = fotg210_irq,
5766 .flags = HCD_MEMORY | HCD_USB2,
5767
5768 /*
5769 * basic lifecycle operations
5770 */
5771 .reset = hcd_fotg210_init,
5772 .start = fotg210_run,
5773 .stop = fotg210_stop,
5774 .shutdown = fotg210_shutdown,
5775
5776 /*
5777 * managing i/o requests and associated device resources
5778 */
5779 .urb_enqueue = fotg210_urb_enqueue,
5780 .urb_dequeue = fotg210_urb_dequeue,
5781 .endpoint_disable = fotg210_endpoint_disable,
5782 .endpoint_reset = fotg210_endpoint_reset,
5783
5784 /*
5785 * scheduling support
5786 */
5787 .get_frame_number = fotg210_get_frame,
5788
5789 /*
5790 * root hub support
5791 */
5792 .hub_status_data = fotg210_hub_status_data,
5793 .hub_control = fotg210_hub_control,
5794 .bus_suspend = fotg210_bus_suspend,
5795 .bus_resume = fotg210_bus_resume,
5796
5797 .relinquish_port = fotg210_relinquish_port,
5798 .port_handed_over = fotg210_port_handed_over,
5799
5800 .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
5801};
5802
5803static void fotg210_init(struct fotg210_hcd *fotg210)
5804{
5805 u32 value;
5806
5807 iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
5808 &fotg210->regs->gmir);
5809
5810 value = ioread32(&fotg210->regs->otgcsr);
5811 value &= ~OTGCSR_A_BUS_DROP;
5812 value |= OTGCSR_A_BUS_REQ;
5813 iowrite32(value, &fotg210->regs->otgcsr);
5814}
5815
5816/**
5817 * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
5818 *
5819 * Allocates basic resources for this USB host controller, and
5820 * then invokes the start() method for the HCD associated with it
5821 * through the hotplug entry's driver_data.
5822 */
5823static int fotg210_hcd_probe(struct platform_device *pdev)
5824{
5825 struct device *dev = &pdev->dev;
5826 struct usb_hcd *hcd;
5827 struct resource *res;
5828 int irq;
5829 int retval = -ENODEV;
5830 struct fotg210_hcd *fotg210;
5831
5832 if (usb_disabled())
5833 return -ENODEV;
5834
5835 pdev->dev.power.power_state = PMSG_ON;
5836
5837 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
5838 if (!res) {
5839 dev_err(dev,
5840 "Found HC with no IRQ. Check %s setup!\n",
5841 dev_name(dev));
5842 return -ENODEV;
5843 }
5844
5845 irq = res->start;
5846
5847 hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
5848 dev_name(dev));
5849 if (!hcd) {
5850 dev_err(dev, "failed to create hcd with err %d\n", retval);
5851 retval = -ENOMEM;
5852 goto fail_create_hcd;
5853 }
5854
5855 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5856 if (!res) {
5857 dev_err(dev,
5858 "Found HC with no register addr. Check %s setup!\n",
5859 dev_name(dev));
5860 retval = -ENODEV;
5861 goto fail_request_resource;
5862 }
5863
5864 hcd->rsrc_start = res->start;
5865 hcd->rsrc_len = resource_size(res);
5866 hcd->has_tt = 1;
5867
5868 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
5869 fotg210_fotg210_hc_driver.description)) {
5870 dev_dbg(dev, "controller already in use\n");
5871 retval = -EBUSY;
5872 goto fail_request_resource;
5873 }
5874
5875 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
5876 if (!res) {
5877 dev_err(dev,
5878 "Found HC with no register addr. Check %s setup!\n",
5879 dev_name(dev));
5880 retval = -ENODEV;
5881 goto fail_request_resource;
5882 }
5883
5884 hcd->regs = ioremap_nocache(res->start, resource_size(res));
5885 if (hcd->regs == NULL) {
5886 dev_dbg(dev, "error mapping memory\n");
5887 retval = -EFAULT;
5888 goto fail_ioremap;
5889 }
5890
5891 fotg210 = hcd_to_fotg210(hcd);
5892
5893 fotg210->caps = hcd->regs;
5894
5895 retval = fotg210_setup(hcd);
5896 if (retval)
5897 goto fail_add_hcd;
5898
5899 fotg210_init(fotg210);
5900
5901 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
5902 if (retval) {
5903 dev_err(dev, "failed to add hcd with err %d\n", retval);
5904 goto fail_add_hcd;
5905 }
5906
5907 return retval;
5908
5909fail_add_hcd:
5910 iounmap(hcd->regs);
5911fail_ioremap:
5912 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
5913fail_request_resource:
5914 usb_put_hcd(hcd);
5915fail_create_hcd:
5916 dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
5917 return retval;
5918}
5919
5920/**
5921 * fotg210_hcd_remove - shutdown processing for EHCI HCDs
5922 * @dev: USB Host Controller being removed
5923 *
5924 */
5925static int fotg210_hcd_remove(struct platform_device *pdev)
5926{
5927 struct device *dev = &pdev->dev;
5928 struct usb_hcd *hcd = dev_get_drvdata(dev);
5929
5930 if (!hcd)
5931 return 0;
5932
5933 usb_remove_hcd(hcd);
5934 iounmap(hcd->regs);
5935 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
5936 usb_put_hcd(hcd);
5937
5938 return 0;
5939}
5940
5941static struct platform_driver fotg210_hcd_driver = {
5942 .driver = {
5943 .name = "fotg210-hcd",
5944 },
5945 .probe = fotg210_hcd_probe,
5946 .remove = fotg210_hcd_remove,
5947};
5948
5949static int __init fotg210_hcd_init(void)
5950{
5951 int retval = 0;
5952
5953 if (usb_disabled())
5954 return -ENODEV;
5955
5956 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
5957 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5958 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
5959 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
5960 pr_warn(KERN_WARNING "Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
5961
5962 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd\n",
5963 hcd_name,
5964 sizeof(struct fotg210_qh), sizeof(struct fotg210_qtd),
5965 sizeof(struct fotg210_itd));
5966
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005967 fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
5968 if (!fotg210_debug_root) {
5969 retval = -ENOENT;
5970 goto err_debug;
5971 }
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005972
5973 retval = platform_driver_register(&fotg210_hcd_driver);
5974 if (retval < 0)
5975 goto clean;
5976 return retval;
5977
5978 platform_driver_unregister(&fotg210_hcd_driver);
5979clean:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005980 debugfs_remove(fotg210_debug_root);
5981 fotg210_debug_root = NULL;
5982err_debug:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005983 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5984 return retval;
5985}
5986module_init(fotg210_hcd_init);
5987
5988static void __exit fotg210_hcd_cleanup(void)
5989{
5990 platform_driver_unregister(&fotg210_hcd_driver);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005991 debugfs_remove(fotg210_debug_root);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005992 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5993}
5994module_exit(fotg210_hcd_cleanup);