Version:  2.0.40 2.2.26 2.4.37 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5

Linux/drivers/usb/host/fotg210-hcd.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us