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