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