blob: f0ec538a8813b844805b197223ad3e55e811c1b6 [file] [log] [blame]
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001/* Faraday FOTG210 EHCI-like driver
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002 *
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
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000052#define DRIVER_AUTHOR "Yuan-Hsin Chen"
53#define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020054static const char hcd_name[] = "fotg210_hcd";
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000055
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000056#undef FOTG210_URB_TRACE
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000057#define FOTG210_STATS
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000058
59/* magic numbers that can affect system performance */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020060#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
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000068 * code). In an attempt to avoid trouble, we will use a minimum scheduling
69 * length of 512 frames instead of 256.
70 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020071#define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000072
73/* Initial IRQ latency: faster than hw default */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020074static int log2_irq_thresh; /* 0 to 6 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000075module_param(log2_irq_thresh, int, S_IRUGO);
76MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
77
78/* initial park setting: slower than hw default */
79static unsigned park;
80module_param(park, uint, S_IRUGO);
81MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
82
83/* for link power management(LPM) feature */
84static unsigned int hird;
85module_param(hird, int, S_IRUGO);
86MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
87
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020088#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000089
90#include "fotg210.h"
91
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000092#define fotg210_dbg(fotg210, fmt, args...) \
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020093 dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000094#define fotg210_err(fotg210, fmt, args...) \
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020095 dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000096#define fotg210_info(fotg210, fmt, args...) \
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020097 dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000098#define fotg210_warn(fotg210, fmt, args...) \
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020099 dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000100
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200101/* check the values in the HCSPARAMS register (host controller _Structural_
102 * parameters) see EHCI spec, Table 2-4 for each value
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000103 */
104static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
105{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200106 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000107
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200108 fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
109 HCS_N_PORTS(params));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000110}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000111
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200112/* check the values in the HCCPARAMS register (host controller _Capability_
113 * parameters) see EHCI Spec, Table 2-5 for each value
114 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000115static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
116{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200117 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000118
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200119 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" : "");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000123}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000124
125static void __maybe_unused
126dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
127{
128 fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200129 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]));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000133 if (qtd->hw_buf[1])
134 fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200135 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]));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000139}
140
141static void __maybe_unused
142dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
143{
144 struct fotg210_qh_hw *hw = qh->hw;
145
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200146 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
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000150 dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
151}
152
153static void __maybe_unused
154dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
155{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200156 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
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000160 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200161 " 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
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000171 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200172 " 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
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000181 fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200182 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]);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000185}
186
187static int __maybe_unused
188dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
189{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200190 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" : "");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000202}
203
204static int __maybe_unused
205dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
206{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200207 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" : "");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000215}
216
217static const char *const fls_strings[] = { "1024", "512", "256", "??" };
218
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200219static int dbg_command_buf(char *buf, unsigned len, const char *label,
220 u32 command)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000221{
222 return scnprintf(buf, len,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200223 "%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");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000234}
235
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200236static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
237 u32 status)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000238{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200239 char *sig;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000240
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
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200257 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
Oliver Neukumf848a882013-11-18 13:23:05 +0100269 return buf;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000270}
271
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000272/* functions have the "wrong" filename when they're output... */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200273#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); \
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000277}
278
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200279#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); \
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000283}
284
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200285#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));\
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000289}
290
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000291/* troubleshooting help: expose state in debugfs */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000292static int debug_async_open(struct inode *, struct file *);
293static int debug_periodic_open(struct inode *, struct file *);
294static int debug_registers_open(struct inode *, struct file *);
295static int debug_async_open(struct inode *, struct file *);
296
297static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
298static int debug_close(struct inode *, struct file *);
299
300static const struct file_operations debug_async_fops = {
301 .owner = THIS_MODULE,
302 .open = debug_async_open,
303 .read = debug_output,
304 .release = debug_close,
305 .llseek = default_llseek,
306};
307static const struct file_operations debug_periodic_fops = {
308 .owner = THIS_MODULE,
309 .open = debug_periodic_open,
310 .read = debug_output,
311 .release = debug_close,
312 .llseek = default_llseek,
313};
314static const struct file_operations debug_registers_fops = {
315 .owner = THIS_MODULE,
316 .open = debug_registers_open,
317 .read = debug_output,
318 .release = debug_close,
319 .llseek = default_llseek,
320};
321
322static struct dentry *fotg210_debug_root;
323
324struct debug_buffer {
325 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
326 struct usb_bus *bus;
327 struct mutex mutex; /* protect filling of buffer */
328 size_t count; /* number of characters filled into buffer */
329 char *output_buf;
330 size_t alloc_size;
331};
332
Peter Senna Tschudinc4d66b52015-10-12 23:22:38 +0200333static 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}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000349
350static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
351{
352 __u32 v = hc32_to_cpu(fotg210, token);
353
354 if (v & QTD_STS_ACTIVE)
355 return '*';
356 if (v & QTD_STS_HALT)
357 return '-';
358 if (!IS_SHORT_READ(v))
359 return ' ';
360 /* tries to advance through hw_alt_next */
361 return '/';
362}
363
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200364static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
365 char **nextp, unsigned *sizep)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000366{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200367 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;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000376
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200377 if (hw->hw_qtd_next == list_end) /* NEC does this */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000378 mark = '@';
379 else
380 mark = token_mark(fotg210, hw->hw_token);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200381 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 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000385 else if (hw->hw_alt_next == list_end)
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200386 mark = '.'; /* use hw_qtd_next */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000387 /* 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;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200449
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000450 size -= temp;
451 next += temp;
452
453done:
454 *sizep = size;
455 *nextp = next;
456}
457
458static ssize_t fill_async_buffer(struct debug_buffer *buf)
459{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200460 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;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000466
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;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200480 qh = qh->qh_next.qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000481 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
Peter Senna Tschudin32fb1932015-10-12 23:22:39 +0200496/* count tds, get ep direction */
497static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
498 struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
499{
500 u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
501 struct fotg210_qtd *qtd;
502 char *type = "";
503 unsigned temp = 0;
504
505 /* count tds, get ep direction */
506 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
507 temp++;
508 switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
509 case 0:
510 type = "out";
511 continue;
512 case 1:
513 type = "in";
514 continue;
515 }
516 }
517
518 return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)",
519 speed_char(scratch), scratch & 0x007f,
520 (scratch >> 8) & 0x000f, type, qh->usecs,
521 qh->c_usecs, temp, (scratch >> 16) & 0x7ff);
522}
523
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000524#define DBG_SCHED_LIMIT 64
525static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
526{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200527 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;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000535
Peter Senna Tschudin0d880022015-10-12 23:22:36 +0200536 seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000537 if (!seen)
538 return 0;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200539
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000540 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;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200559
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000560 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200587 " ...");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000588 size -= temp;
589 next += temp;
590 }
591 break;
592 }
593 /* show more info the first time around */
594 if (temp == seen_count) {
Peter Senna Tschudin32fb1932015-10-12 23:22:39 +0200595 temp = output_buf_tds_dir(next,
596 fotg210, hw,
597 p.qh, size);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000598
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200608 " fstn-%8x/%p",
609 p.fstn->hw_prev, p.fstn);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000610 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200615 " itd/%p", p.itd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000616 tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
617 p = p.itd->itd_next;
618 break;
619 }
620 size -= temp;
621 next += temp;
622 } while (p.ptr);
623
624 temp = scnprintf(next, size, "\n");
625 size -= temp;
626 next += temp;
627 }
628 spin_unlock_irqrestore(&fotg210->lock, flags);
629 kfree(seen);
630
631 return buf->alloc_size - size;
632}
633#undef DBG_SCHED_LIMIT
634
635static const char *rh_state_string(struct fotg210_hcd *fotg210)
636{
637 switch (fotg210->rh_state) {
638 case FOTG210_RH_HALTED:
639 return "halted";
640 case FOTG210_RH_SUSPENDED:
641 return "suspended";
642 case FOTG210_RH_RUNNING:
643 return "running";
644 case FOTG210_RH_STOPPING:
645 return "stopping";
646 }
647 return "?";
648}
649
650static ssize_t fill_registers_buffer(struct debug_buffer *buf)
651{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200652 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[] = "";
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000659
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200669 "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);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000675 goto done;
676 }
677
678 /* Capability Registers */
679 i = HC_VERSION(fotg210, fotg210_readl(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200680 &fotg210->caps->hc_capbase));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000681 temp = scnprintf(next, size,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200682 "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));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000689 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200736 "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);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000739 size -= temp;
740 next += temp;
741
742 temp = scnprintf(next, size, "complete %ld unlink %ld\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200743 fotg210->stats.complete, fotg210->stats.unlink);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000744 size -= temp;
745 next += temp;
746#endif
747
748done:
749 spin_unlock_irqrestore(&fotg210->lock, flags);
750
751 return buf->alloc_size - size;
752}
753
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200754static struct debug_buffer
755*alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000756{
757 struct debug_buffer *buf;
758
759 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
760
761 if (buf) {
762 buf->bus = bus;
763 buf->fill_func = fill_func;
764 mutex_init(&buf->mutex);
765 buf->alloc_size = PAGE_SIZE;
766 }
767
768 return buf;
769}
770
771static int fill_buffer(struct debug_buffer *buf)
772{
773 int ret = 0;
774
775 if (!buf->output_buf)
776 buf->output_buf = vmalloc(buf->alloc_size);
777
778 if (!buf->output_buf) {
779 ret = -ENOMEM;
780 goto out;
781 }
782
783 ret = buf->fill_func(buf);
784
785 if (ret >= 0) {
786 buf->count = ret;
787 ret = 0;
788 }
789
790out:
791 return ret;
792}
793
794static ssize_t debug_output(struct file *file, char __user *user_buf,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200795 size_t len, loff_t *offset)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000796{
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200811 buf->output_buf, buf->count);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000812
813out:
814 return ret;
815
816}
817
818static int debug_close(struct inode *inode, struct file *file)
819{
820 struct debug_buffer *buf = file->private_data;
821
822 if (buf) {
823 vfree(buf->output_buf);
824 kfree(buf);
825 }
826
827 return 0;
828}
829static int debug_async_open(struct inode *inode, struct file *file)
830{
831 file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
832
833 return file->private_data ? 0 : -ENOMEM;
834}
835
836static int debug_periodic_open(struct inode *inode, struct file *file)
837{
838 struct debug_buffer *buf;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200839
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000840 buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
841 if (!buf)
842 return -ENOMEM;
843
844 buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
845 file->private_data = buf;
846 return 0;
847}
848
849static int debug_registers_open(struct inode *inode, struct file *file)
850{
851 file->private_data = alloc_buffer(inode->i_private,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200852 fill_registers_buffer);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000853
854 return file->private_data ? 0 : -ENOMEM;
855}
856
857static inline void create_debug_files(struct fotg210_hcd *fotg210)
858{
859 struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
860
861 fotg210->debug_dir = debugfs_create_dir(bus->bus_name,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200862 fotg210_debug_root);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000863 if (!fotg210->debug_dir)
864 return;
865
866 if (!debugfs_create_file("async", S_IRUGO, fotg210->debug_dir, bus,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200867 &debug_async_fops))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000868 goto file_error;
869
870 if (!debugfs_create_file("periodic", S_IRUGO, fotg210->debug_dir, bus,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200871 &debug_periodic_fops))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000872 goto file_error;
873
874 if (!debugfs_create_file("registers", S_IRUGO, fotg210->debug_dir, bus,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200875 &debug_registers_fops))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000876 goto file_error;
877
878 return;
879
880file_error:
881 debugfs_remove_recursive(fotg210->debug_dir);
882}
883
884static inline void remove_debug_files(struct fotg210_hcd *fotg210)
885{
886 debugfs_remove_recursive(fotg210->debug_dir);
887}
888
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200889/* handshake - spin reading hc until handshake completes or fails
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000890 * @ptr: address of hc register to be read
891 * @mask: bits to look at in result of read
892 * @done: value of those bits when handshake succeeds
893 * @usec: timeout in microseconds
894 *
895 * Returns negative errno, or zero on success
896 *
897 * Success happens when the "mask" bits have the specified value (hardware
898 * handshake done). There are two failure modes: "usec" have passed (major
899 * hardware flakeout), or the register reads as all-ones (hardware removed).
900 *
901 * That last failure should_only happen in cases like physical cardbus eject
902 * before driver shutdown. But it also seems to be caused by bugs in cardbus
903 * bridge shutdown: shutting down the bridge before the devices using it.
904 */
905static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200906 u32 mask, u32 done, int usec)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000907{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200908 u32 result;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000909
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
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200923/* Force HC to halt state from unknown (EHCI spec section 2.3).
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000924 * Must be called with interrupts enabled and the lock not held.
925 */
926static int fotg210_halt(struct fotg210_hcd *fotg210)
927{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200928 u32 temp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000929
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200948 STS_HALT, STS_HALT, 16 * 125);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000949}
950
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200951/* Reset a non-running (STS_HALT == 1) controller.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000952 * Must be called with interrupts enabled and the lock not held.
953 */
954static int fotg210_reset(struct fotg210_hcd *fotg210)
955{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200956 int retval;
957 u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000958
959 /* If the EHCI debug controller is active, special care must be
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200960 * taken before and after a host controller reset
961 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000962 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200971 CMD_RESET, 0, 250 * 1000);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000972
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
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200984/* Idle the controller (turn off the schedules).
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000985 * Must be called with interrupts enabled and the lock not held.
986 */
987static void fotg210_quiesce(struct fotg210_hcd *fotg210)
988{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200989 u32 temp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000990
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200997 16 * 125);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000998
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001007 16 * 125);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001008}
1009
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001010static void end_unlink_async(struct fotg210_hcd *fotg210);
1011static void unlink_empty_async(struct fotg210_hcd *fotg210);
1012static void fotg210_work(struct fotg210_hcd *fotg210);
1013static void start_unlink_intr(struct fotg210_hcd *fotg210,
1014 struct fotg210_qh *qh);
1015static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
1016
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001017/* Set a bit in the USBCMD register */
1018static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
1019{
1020 fotg210->command |= bit;
1021 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1022
1023 /* unblock posted write */
1024 fotg210_readl(fotg210, &fotg210->regs->command);
1025}
1026
1027/* Clear a bit in the USBCMD register */
1028static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
1029{
1030 fotg210->command &= ~bit;
1031 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1032
1033 /* unblock posted write */
1034 fotg210_readl(fotg210, &fotg210->regs->command);
1035}
1036
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001037/* EHCI timer support... Now using hrtimers.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001038 *
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
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001059/* Delay lengths for the hrtimer event types.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001060 * Keep this list sorted by delay length, in the same order as
1061 * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
1062 */
1063static unsigned event_delays_ns[] = {
1064 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */
1065 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */
1066 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */
1067 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */
1068 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */
1069 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1070 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1071 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1072 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1073 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */
1074};
1075
1076/* Enable a pending hrtimer event */
1077static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
1078 bool resched)
1079{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001080 ktime_t *timeout = &fotg210->hr_timeouts[event];
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001081
1082 if (resched)
1083 *timeout = ktime_add(ktime_get(),
1084 ktime_set(0, event_delays_ns[event]));
1085 fotg210->enabled_hrtimer_events |= (1 << event);
1086
1087 /* Track only the lowest-numbered pending event */
1088 if (event < fotg210->next_hrtimer_event) {
1089 fotg210->next_hrtimer_event = event;
1090 hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
1091 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
1092 }
1093}
1094
1095
1096/* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
1097static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
1098{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001099 unsigned actual, want;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001100
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001113 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001114 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001131 FOTG210_HRTIMER_DISABLE_ASYNC,
1132 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001133 }
1134 }
1135}
1136
1137/* Turn off the async schedule after a brief delay */
1138static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
1139{
1140 fotg210_clear_command_bit(fotg210, CMD_ASE);
1141}
1142
1143
1144/* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
1145static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
1146{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001147 unsigned actual, want;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001148
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001161 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001162 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001179 FOTG210_HRTIMER_DISABLE_PERIODIC,
1180 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001181 }
1182 }
1183}
1184
1185/* Turn off the periodic schedule after a brief delay */
1186static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
1187{
1188 fotg210_clear_command_bit(fotg210, CMD_PSE);
1189}
1190
1191
1192/* Poll the STS_HALT status bit; see when a dead controller stops */
1193static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
1194{
1195 if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
1196
1197 /* Give up after a few milliseconds */
1198 if (fotg210->died_poll_count++ < 5) {
1199 /* Try again later */
1200 fotg210_enable_event(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001201 FOTG210_HRTIMER_POLL_DEAD, true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001202 return;
1203 }
1204 fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
1205 }
1206
1207 /* Clean up the mess */
1208 fotg210->rh_state = FOTG210_RH_HALTED;
1209 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
1210 fotg210_work(fotg210);
1211 end_unlink_async(fotg210);
1212
1213 /* Not in process context, so don't try to reset the controller */
1214}
1215
1216
1217/* Handle unlinked interrupt QHs once they are gone from the hardware */
1218static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
1219{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001220 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001221
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) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001231 struct fotg210_qh *qh = fotg210->intr_unlink;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001232
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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001243 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001244 ++fotg210->intr_unlink_cycle;
1245 }
1246 fotg210->intr_unlinking = false;
1247}
1248
1249
1250/* Start another free-iTDs/siTDs cycle */
1251static void start_free_itds(struct fotg210_hcd *fotg210)
1252{
1253 if (!(fotg210->enabled_hrtimer_events &
1254 BIT(FOTG210_HRTIMER_FREE_ITDS))) {
1255 fotg210->last_itd_to_free = list_entry(
1256 fotg210->cached_itd_list.prev,
1257 struct fotg210_itd, itd_list);
1258 fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
1259 }
1260}
1261
1262/* Wait for controller to stop using old iTDs and siTDs */
1263static void end_free_itds(struct fotg210_hcd *fotg210)
1264{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001265 struct fotg210_itd *itd, *n;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001266
1267 if (fotg210->rh_state < FOTG210_RH_RUNNING)
1268 fotg210->last_itd_to_free = NULL;
1269
1270 list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
1271 list_del(&itd->itd_list);
1272 dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
1273 if (itd == fotg210->last_itd_to_free)
1274 break;
1275 }
1276
1277 if (!list_empty(&fotg210->cached_itd_list))
1278 start_free_itds(fotg210);
1279}
1280
1281
1282/* Handle lost (or very late) IAA interrupts */
1283static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
1284{
1285 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1286 return;
1287
1288 /*
1289 * Lost IAA irqs wedge things badly; seen first with a vt8235.
1290 * So we need this watchdog, but must protect it against both
1291 * (a) SMP races against real IAA firing and retriggering, and
1292 * (b) clean HC shutdown, when IAA watchdog was pending.
1293 */
1294 if (fotg210->async_iaa) {
1295 u32 cmd, status;
1296
1297 /* If we get here, IAA is *REALLY* late. It's barely
1298 * conceivable that the system is so busy that CMD_IAAD
1299 * is still legitimately set, so let's be sure it's
1300 * clear before we read STS_IAA. (The HC should clear
1301 * CMD_IAAD when it sets STS_IAA.)
1302 */
1303 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
1304
1305 /*
1306 * If IAA is set here it either legitimately triggered
1307 * after the watchdog timer expired (_way_ late, so we'll
1308 * still count it as lost) ... or a silicon erratum:
1309 * - VIA seems to set IAA without triggering the IRQ;
1310 * - IAAD potentially cleared without setting IAA.
1311 */
1312 status = fotg210_readl(fotg210, &fotg210->regs->status);
1313 if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
1314 COUNT(fotg210->stats.lost_iaa);
1315 fotg210_writel(fotg210, STS_IAA,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001316 &fotg210->regs->status);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001317 }
1318
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01001319 fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001320 status, cmd);
1321 end_unlink_async(fotg210);
1322 }
1323}
1324
1325
1326/* Enable the I/O watchdog, if appropriate */
1327static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
1328{
1329 /* Not needed if the controller isn't running or it's already enabled */
1330 if (fotg210->rh_state != FOTG210_RH_RUNNING ||
1331 (fotg210->enabled_hrtimer_events &
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001332 BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001333 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001342 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001343}
1344
1345
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001346/* Handler functions for the hrtimer event types.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001347 * Keep this array in the same order as the event types indexed by
1348 * enum fotg210_hrtimer_event in fotg210.h.
1349 */
1350static void (*event_handlers[])(struct fotg210_hcd *) = {
1351 fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */
1352 fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */
1353 fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */
1354 fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */
1355 end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */
1356 unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1357 fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1358 fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1359 fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1360 fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */
1361};
1362
1363static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
1364{
1365 struct fotg210_hcd *fotg210 =
1366 container_of(t, struct fotg210_hcd, hrtimer);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001367 ktime_t now;
1368 unsigned long events;
1369 unsigned long flags;
1370 unsigned e;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001371
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
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001394#define fotg210_bus_suspend NULL
1395#define fotg210_bus_resume NULL
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001396
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001397static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
1398 u32 __iomem *status_reg, int port_status)
1399{
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001400 if (!(port_status & PORT_CONNECT))
1401 return port_status;
1402
1403 /* if reset finished and it's still not enabled -- handoff */
Peter Senna Tschudinffa23662015-10-17 21:28:45 +02001404 if (!(port_status & PORT_PE))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001405 /* with integrated TT, there's nobody to hand it to! */
Peter Senna Tschudinffa23662015-10-17 21:28:45 +02001406 fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001407 index + 1);
Peter Senna Tschudinffa23662015-10-17 21:28:45 +02001408 else
1409 fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
1410 index + 1);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001411
1412 return port_status;
1413}
1414
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001415
1416/* build "status change" packet (one or two bytes) from HC registers */
1417
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001418static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001419{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001420 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
1421 u32 temp, status;
1422 u32 mask;
1423 int retval = 1;
1424 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001425
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.
Petr Mladek37ebb542014-09-19 17:32:23 +02001446 * Otherwise hub_wq wouldn't see the disconnect event when a
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001447 * high-speed device is switched over to the companion
1448 * controller by the user.
1449 */
1450
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001451 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]))) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001454 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
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001462static 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;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001467
Sergei Shtylyov4631f4e2015-03-29 01:06:21 +03001468 desc->bDescriptorType = USB_DT_HUB;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001469 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
Sergei Shtylyov7538bd62015-01-19 01:29:03 +03001480 temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
1481 temp |= HUB_CHAR_NO_LPSM; /* no power switching */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001482 desc->wHubCharacteristics = cpu_to_le16(temp);
1483}
1484
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001485static 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;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001495
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
Petr Mladek37ebb542014-09-19 17:32:23 +02001524 * companion controller, hub_wq needs to be able to clear
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001525 * 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
Felipe Balbi7e136bb2015-02-13 14:54:38 -06001547 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001548 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001557 &fotg210->regs->otgisr);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001558 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 *)
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001569 buf);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001570 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);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001615 fotg210_writel(fotg210, temp &
1616 ~(PORT_RWC_BITS | PORT_RESUME),
1617 status_reg);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001618 clear_bit(wIndex, &fotg210->resuming_ports);
1619 retval = handshake(fotg210, status_reg,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001620 PORT_RESUME, 0, 2000);/* 2ms */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001621 if (retval != 0) {
1622 fotg210_err(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001623 "port %d resume error %d\n",
1624 wIndex + 1, retval);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001625 goto error;
1626 }
1627 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1628 }
1629 }
1630
1631 /* whoever resets must GetPortStatus to complete it!! */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001632 if ((temp & PORT_RESET) && time_after_eq(jiffies,
1633 fotg210->reset_done[wIndex])) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001634 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,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001640 temp & ~(PORT_RWC_BITS | PORT_RESET),
1641 status_reg);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001642 /* 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",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001649 wIndex + 1, retval);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001650 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));
Hans Ulli Kroll25b0bb72019-08-10 17:04:58 +02001656
1657 /* restart schedule */
1658 fotg210->command |= CMD_RUN;
1659 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001660 }
1661
1662 if (!(temp & (PORT_RESUME|PORT_RESET))) {
1663 fotg210->reset_done[wIndex] = 0;
1664 clear_bit(wIndex, &fotg210->resuming_ports);
1665 }
1666
1667 /* transfer dedicated ports to the companion hc */
1668 if ((temp & PORT_CONNECT) &&
1669 test_bit(wIndex, &fotg210->companion_ports)) {
1670 temp &= ~PORT_RWC_BITS;
1671 fotg210_writel(fotg210, temp, status_reg);
1672 fotg210_dbg(fotg210, "port %d --> companion\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001673 wIndex + 1);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001674 temp = fotg210_readl(fotg210, status_reg);
1675 }
1676
1677 /*
Petr Mladek37ebb542014-09-19 17:32:23 +02001678 * Even if OWNER is set, there's no harm letting hub_wq
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001679 * see the wPortStatus values (they should all be 0 except
1680 * for PORT_POWER anyway).
1681 */
1682
1683 if (temp & PORT_CONNECT) {
1684 status |= USB_PORT_STAT_CONNECTION;
1685 status |= fotg210_port_speed(fotg210, temp);
1686 }
1687 if (temp & PORT_PE)
1688 status |= USB_PORT_STAT_ENABLE;
1689
1690 /* maybe the port was unsuspended without our knowledge */
1691 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1692 status |= USB_PORT_STAT_SUSPEND;
1693 } else if (test_bit(wIndex, &fotg210->suspended_ports)) {
1694 clear_bit(wIndex, &fotg210->suspended_ports);
1695 clear_bit(wIndex, &fotg210->resuming_ports);
1696 fotg210->reset_done[wIndex] = 0;
1697 if (temp & PORT_PE)
1698 set_bit(wIndex, &fotg210->port_c_suspend);
1699 }
1700
1701 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
1702 if (temp1 & OTGISR_OVC)
1703 status |= USB_PORT_STAT_OVERCURRENT;
1704 if (temp & PORT_RESET)
1705 status |= USB_PORT_STAT_RESET;
1706 if (test_bit(wIndex, &fotg210->port_c_suspend))
1707 status |= USB_PORT_STAT_C_SUSPEND << 16;
1708
Oliver Neukum3b707ec2013-11-18 13:23:06 +01001709 if (status & ~0xffff) /* only if wPortChange is interesting */
1710 dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001711 put_unaligned_le32(status, buf);
1712 break;
1713 case SetHubFeature:
1714 switch (wValue) {
1715 case C_HUB_LOCAL_POWER:
1716 case C_HUB_OVER_CURRENT:
1717 /* no hub-wide feature/status flags */
1718 break;
1719 default:
1720 goto error;
1721 }
1722 break;
1723 case SetPortFeature:
1724 selector = wIndex >> 8;
1725 wIndex &= 0xff;
1726
1727 if (!wIndex || wIndex > ports)
1728 goto error;
1729 wIndex--;
1730 temp = fotg210_readl(fotg210, status_reg);
1731 temp &= ~PORT_RWC_BITS;
1732 switch (wValue) {
1733 case USB_PORT_FEAT_SUSPEND:
1734 if ((temp & PORT_PE) == 0
1735 || (temp & PORT_RESET) != 0)
1736 goto error;
1737
1738 /* After above check the port must be connected.
1739 * Set appropriate bit thus could put phy into low power
1740 * mode if we have hostpc feature
1741 */
1742 fotg210_writel(fotg210, temp | PORT_SUSPEND,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001743 status_reg);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001744 set_bit(wIndex, &fotg210->suspended_ports);
1745 break;
1746 case USB_PORT_FEAT_RESET:
1747 if (temp & PORT_RESUME)
1748 goto error;
1749 /* line status bits may report this as low speed,
1750 * which can be fine if this root hub has a
1751 * transaction translator built in.
1752 */
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01001753 fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001754 temp |= PORT_RESET;
1755 temp &= ~PORT_PE;
1756
1757 /*
1758 * caller must wait, then call GetPortStatus
1759 * usb 2.0 spec says 50 ms resets on root
1760 */
1761 fotg210->reset_done[wIndex] = jiffies
1762 + msecs_to_jiffies(50);
1763 fotg210_writel(fotg210, temp, status_reg);
1764 break;
1765
1766 /* For downstream facing ports (these): one hub port is put
1767 * into test mode according to USB2 11.24.2.13, then the hub
1768 * must be reset (which for root hub now means rmmod+modprobe,
1769 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1770 * about the EHCI-specific stuff.
1771 */
1772 case USB_PORT_FEAT_TEST:
1773 if (!selector || selector > 5)
1774 goto error;
1775 spin_unlock_irqrestore(&fotg210->lock, flags);
1776 fotg210_quiesce(fotg210);
1777 spin_lock_irqsave(&fotg210->lock, flags);
1778
1779 /* Put all enabled ports into suspend */
1780 temp = fotg210_readl(fotg210, status_reg) &
1781 ~PORT_RWC_BITS;
1782 if (temp & PORT_PE)
1783 fotg210_writel(fotg210, temp | PORT_SUSPEND,
1784 status_reg);
1785
1786 spin_unlock_irqrestore(&fotg210->lock, flags);
1787 fotg210_halt(fotg210);
1788 spin_lock_irqsave(&fotg210->lock, flags);
1789
1790 temp = fotg210_readl(fotg210, status_reg);
1791 temp |= selector << 16;
1792 fotg210_writel(fotg210, temp, status_reg);
1793 break;
1794
1795 default:
1796 goto error;
1797 }
1798 fotg210_readl(fotg210, &fotg210->regs->command);
1799 break;
1800
1801 default:
1802error:
1803 /* "stall" on error */
1804 retval = -EPIPE;
1805 }
1806 spin_unlock_irqrestore(&fotg210->lock, flags);
1807 return retval;
1808}
1809
1810static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
1811 int portnum)
1812{
1813 return;
1814}
1815
1816static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
1817 int portnum)
1818{
1819 return 0;
1820}
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001821
1822/* There's basically three types of memory:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001823 * - data used only by the HCD ... kmalloc is fine
1824 * - async and periodic schedules, shared by HC and HCD ... these
1825 * need to use dma_pool or dma_alloc_coherent
1826 * - driver buffers, read/written by HC ... single shot DMA mapped
1827 *
1828 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
1829 * No memory seen by this driver is pageable.
1830 */
1831
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001832/* Allocate the key transfer structures from the previously allocated pool */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001833static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001834 struct fotg210_qtd *qtd, dma_addr_t dma)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001835{
1836 memset(qtd, 0, sizeof(*qtd));
1837 qtd->qtd_dma = dma;
1838 qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
1839 qtd->hw_next = FOTG210_LIST_END(fotg210);
1840 qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
1841 INIT_LIST_HEAD(&qtd->qtd_list);
1842}
1843
1844static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001845 gfp_t flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001846{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001847 struct fotg210_qtd *qtd;
1848 dma_addr_t dma;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001849
1850 qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
1851 if (qtd != NULL)
1852 fotg210_qtd_init(fotg210, qtd, dma);
1853
1854 return qtd;
1855}
1856
1857static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001858 struct fotg210_qtd *qtd)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001859{
1860 dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
1861}
1862
1863
1864static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
1865{
1866 /* clean qtds first, and know this is not linked */
1867 if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
1868 fotg210_dbg(fotg210, "unused qh not empty!\n");
1869 BUG();
1870 }
1871 if (qh->dummy)
1872 fotg210_qtd_free(fotg210, qh->dummy);
1873 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1874 kfree(qh);
1875}
1876
1877static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001878 gfp_t flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001879{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001880 struct fotg210_qh *qh;
1881 dma_addr_t dma;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001882
1883 qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
1884 if (!qh)
1885 goto done;
1886 qh->hw = (struct fotg210_qh_hw *)
1887 dma_pool_alloc(fotg210->qh_pool, flags, &dma);
1888 if (!qh->hw)
1889 goto fail;
1890 memset(qh->hw, 0, sizeof(*qh->hw));
1891 qh->qh_dma = dma;
1892 INIT_LIST_HEAD(&qh->qtd_list);
1893
1894 /* dummy td enables safe urb queuing */
1895 qh->dummy = fotg210_qtd_alloc(fotg210, flags);
1896 if (qh->dummy == NULL) {
1897 fotg210_dbg(fotg210, "no dummy td\n");
1898 goto fail1;
1899 }
1900done:
1901 return qh;
1902fail1:
1903 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1904fail:
1905 kfree(qh);
1906 return NULL;
1907}
1908
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001909/* The queue heads and transfer descriptors are managed from pools tied
1910 * to each of the "per device" structures.
1911 * This is the initialisation and cleanup code.
1912 */
1913
1914static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
1915{
1916 if (fotg210->async)
1917 qh_destroy(fotg210, fotg210->async);
1918 fotg210->async = NULL;
1919
1920 if (fotg210->dummy)
1921 qh_destroy(fotg210, fotg210->dummy);
1922 fotg210->dummy = NULL;
1923
1924 /* DMA consistent memory and pools */
Julia Lawalla4c1f0c2015-09-13 14:15:32 +02001925 dma_pool_destroy(fotg210->qtd_pool);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001926 fotg210->qtd_pool = NULL;
1927
Julia Lawalla4c1f0c2015-09-13 14:15:32 +02001928 dma_pool_destroy(fotg210->qh_pool);
1929 fotg210->qh_pool = NULL;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001930
Julia Lawalla4c1f0c2015-09-13 14:15:32 +02001931 dma_pool_destroy(fotg210->itd_pool);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001932 fotg210->itd_pool = NULL;
1933
1934 if (fotg210->periodic)
1935 dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001936 fotg210->periodic_size * sizeof(u32),
1937 fotg210->periodic, fotg210->periodic_dma);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001938 fotg210->periodic = NULL;
1939
1940 /* shadow periodic table */
1941 kfree(fotg210->pshadow);
1942 fotg210->pshadow = NULL;
1943}
1944
1945/* remember to add cleanup code (above) if you add anything here */
1946static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
1947{
1948 int i;
1949
1950 /* QTDs for control/bulk/intr transfers */
1951 fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
1952 fotg210_to_hcd(fotg210)->self.controller,
1953 sizeof(struct fotg210_qtd),
1954 32 /* byte alignment (for hw parts) */,
1955 4096 /* can't cross 4K */);
1956 if (!fotg210->qtd_pool)
1957 goto fail;
1958
1959 /* QHs for control/bulk/intr transfers */
1960 fotg210->qh_pool = dma_pool_create("fotg210_qh",
1961 fotg210_to_hcd(fotg210)->self.controller,
1962 sizeof(struct fotg210_qh_hw),
1963 32 /* byte alignment (for hw parts) */,
1964 4096 /* can't cross 4K */);
1965 if (!fotg210->qh_pool)
1966 goto fail;
1967
1968 fotg210->async = fotg210_qh_alloc(fotg210, flags);
1969 if (!fotg210->async)
1970 goto fail;
1971
1972 /* ITD for high speed ISO transfers */
1973 fotg210->itd_pool = dma_pool_create("fotg210_itd",
1974 fotg210_to_hcd(fotg210)->self.controller,
1975 sizeof(struct fotg210_itd),
1976 64 /* byte alignment (for hw parts) */,
1977 4096 /* can't cross 4K */);
1978 if (!fotg210->itd_pool)
1979 goto fail;
1980
1981 /* Hardware periodic table */
1982 fotg210->periodic = (__le32 *)
1983 dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001984 fotg210->periodic_size * sizeof(__le32),
1985 &fotg210->periodic_dma, 0);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001986 if (fotg210->periodic == NULL)
1987 goto fail;
1988
1989 for (i = 0; i < fotg210->periodic_size; i++)
1990 fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
1991
1992 /* software shadow of hardware table */
1993 fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001994 flags);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001995 if (fotg210->pshadow != NULL)
1996 return 0;
1997
1998fail:
1999 fotg210_dbg(fotg210, "couldn't init memory\n");
2000 fotg210_mem_cleanup(fotg210);
2001 return -ENOMEM;
2002}
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002003/* EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002004 *
2005 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
2006 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
2007 * buffers needed for the larger number). We use one QH per endpoint, queue
2008 * multiple urbs (all three types) per endpoint. URBs may need several qtds.
2009 *
2010 * ISO traffic uses "ISO TD" (itd) records, and (along with
2011 * interrupts) needs careful scheduling. Performance improvements can be
2012 * an ongoing challenge. That's in "ehci-sched.c".
2013 *
2014 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
2015 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
2016 * (b) special fields in qh entries or (c) split iso entries. TTs will
2017 * buffer low/full speed data so the host collects it at high speed.
2018 */
2019
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002020/* fill a qtd, returning how much of the buffer we were able to queue up */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002021static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd,
2022 dma_addr_t buf, size_t len, int token, int maxpacket)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002023{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002024 int i, count;
2025 u64 addr = buf;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002026
2027 /* one buffer entry per 4K ... first might be short or unaligned */
2028 qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr);
2029 qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32));
2030 count = 0x1000 - (buf & 0x0fff); /* rest of that page */
2031 if (likely(len < count)) /* ... iff needed */
2032 count = len;
2033 else {
2034 buf += 0x1000;
2035 buf &= ~0x0fff;
2036
2037 /* per-qtd limit: from 16K to 20K (best alignment) */
2038 for (i = 1; count < len && i < 5; i++) {
2039 addr = buf;
2040 qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
2041 qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
2042 (u32)(addr >> 32));
2043 buf += 0x1000;
2044 if ((count + 0x1000) < len)
2045 count += 0x1000;
2046 else
2047 count = len;
2048 }
2049
2050 /* short packets may only terminate transfers */
2051 if (count != len)
2052 count -= (count % maxpacket);
2053 }
2054 qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
2055 qtd->length = count;
2056
2057 return count;
2058}
2059
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002060static inline void qh_update(struct fotg210_hcd *fotg210,
2061 struct fotg210_qh *qh, struct fotg210_qtd *qtd)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002062{
2063 struct fotg210_qh_hw *hw = qh->hw;
2064
2065 /* writes to an active overlay are unsafe */
2066 BUG_ON(qh->qh_state != QH_STATE_IDLE);
2067
2068 hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2069 hw->hw_alt_next = FOTG210_LIST_END(fotg210);
2070
2071 /* Except for control endpoints, we make hardware maintain data
2072 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
2073 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
2074 * ever clear it.
2075 */
2076 if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002077 unsigned is_out, epnum;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002078
2079 is_out = qh->is_out;
2080 epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f;
2081 if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
2082 hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE);
2083 usb_settoggle(qh->dev, epnum, is_out, 1);
2084 }
2085 }
2086
2087 hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
2088}
2089
2090/* if it weren't for a common silicon quirk (writing the dummy into the qh
2091 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
2092 * recovery (including urb dequeue) would need software changes to a QH...
2093 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002094static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002095{
2096 struct fotg210_qtd *qtd;
2097
2098 if (list_empty(&qh->qtd_list))
2099 qtd = qh->dummy;
2100 else {
2101 qtd = list_entry(qh->qtd_list.next,
2102 struct fotg210_qtd, qtd_list);
2103 /*
2104 * first qtd may already be partially processed.
2105 * If we come here during unlink, the QH overlay region
2106 * might have reference to the just unlinked qtd. The
2107 * qtd is updated in qh_completions(). Update the QH
2108 * overlay here.
2109 */
2110 if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
2111 qh->hw->hw_qtd_next = qtd->hw_next;
2112 qtd = NULL;
2113 }
2114 }
2115
2116 if (qtd)
2117 qh_update(fotg210, qh, qtd);
2118}
2119
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002120static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2121
2122static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
2123 struct usb_host_endpoint *ep)
2124{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002125 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
2126 struct fotg210_qh *qh = ep->hcpriv;
2127 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002128
2129 spin_lock_irqsave(&fotg210->lock, flags);
2130 qh->clearing_tt = 0;
2131 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
2132 && fotg210->rh_state == FOTG210_RH_RUNNING)
2133 qh_link_async(fotg210, qh);
2134 spin_unlock_irqrestore(&fotg210->lock, flags);
2135}
2136
2137static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002138 struct fotg210_qh *qh, struct urb *urb, u32 token)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002139{
2140
2141 /* If an async split transaction gets an error or is unlinked,
2142 * the TT buffer may be left in an indeterminate state. We
2143 * have to clear the TT buffer.
2144 *
2145 * Note: this routine is never called for Isochronous transfers.
2146 */
2147 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002148 struct usb_device *tt = urb->dev->tt->hub;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002149
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002150 dev_dbg(&tt->dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002151 "clear tt buffer port %d, a%d ep%d t%08x\n",
2152 urb->dev->ttport, urb->dev->devnum,
2153 usb_pipeendpoint(urb->pipe), token);
Oliver Neukum3b707ec2013-11-18 13:23:06 +01002154
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002155 if (urb->dev->tt->hub !=
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002156 fotg210_to_hcd(fotg210)->self.root_hub) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002157 if (usb_hub_clear_tt_buffer(urb) == 0)
2158 qh->clearing_tt = 1;
2159 }
2160 }
2161}
2162
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002163static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb,
2164 size_t length, u32 token)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002165{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002166 int status = -EINPROGRESS;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002167
2168 /* count IN/OUT bytes, not SETUP (even short packets) */
2169 if (likely(QTD_PID(token) != 2))
2170 urb->actual_length += length - QTD_LENGTH(token);
2171
2172 /* don't modify error codes */
2173 if (unlikely(urb->unlinked))
2174 return status;
2175
2176 /* force cleanup after short read; not always an error */
2177 if (unlikely(IS_SHORT_READ(token)))
2178 status = -EREMOTEIO;
2179
2180 /* serious "can't proceed" faults reported by the hardware */
2181 if (token & QTD_STS_HALT) {
2182 if (token & QTD_STS_BABBLE) {
2183 /* FIXME "must" disable babbling device's port too */
2184 status = -EOVERFLOW;
2185 /* CERR nonzero + halt --> stall */
2186 } else if (QTD_CERR(token)) {
2187 status = -EPIPE;
2188
2189 /* In theory, more than one of the following bits can be set
2190 * since they are sticky and the transaction is retried.
2191 * Which to test first is rather arbitrary.
2192 */
2193 } else if (token & QTD_STS_MMF) {
2194 /* fs/ls interrupt xfer missed the complete-split */
2195 status = -EPROTO;
2196 } else if (token & QTD_STS_DBE) {
2197 status = (QTD_PID(token) == 1) /* IN ? */
2198 ? -ENOSR /* hc couldn't read data */
2199 : -ECOMM; /* hc couldn't write data */
2200 } else if (token & QTD_STS_XACT) {
2201 /* timeout, bad CRC, wrong PID, etc */
2202 fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002203 urb->dev->devpath,
2204 usb_pipeendpoint(urb->pipe),
2205 usb_pipein(urb->pipe) ? "in" : "out");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002206 status = -EPROTO;
2207 } else { /* unknown */
2208 status = -EPROTO;
2209 }
2210
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01002211 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002212 "dev%d ep%d%s qtd token %08x --> status %d\n",
2213 usb_pipedevice(urb->pipe),
2214 usb_pipeendpoint(urb->pipe),
2215 usb_pipein(urb->pipe) ? "in" : "out",
2216 token, status);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002217 }
2218
2219 return status;
2220}
2221
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002222static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb,
2223 int status)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002224__releases(fotg210->lock)
2225__acquires(fotg210->lock)
2226{
2227 if (likely(urb->hcpriv != NULL)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002228 struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002229
2230 /* S-mask in a QH means it's an interrupt urb */
2231 if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
2232
2233 /* ... update hc-wide periodic stats (for usbfs) */
2234 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
2235 }
2236 }
2237
2238 if (unlikely(urb->unlinked)) {
2239 COUNT(fotg210->stats.unlink);
2240 } else {
2241 /* report non-error and short read status as zero */
2242 if (status == -EINPROGRESS || status == -EREMOTEIO)
2243 status = 0;
2244 COUNT(fotg210->stats.complete);
2245 }
2246
2247#ifdef FOTG210_URB_TRACE
2248 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002249 "%s %s urb %p ep%d%s status %d len %d/%d\n",
2250 __func__, urb->dev->devpath, urb,
2251 usb_pipeendpoint(urb->pipe),
2252 usb_pipein(urb->pipe) ? "in" : "out",
2253 status,
2254 urb->actual_length, urb->transfer_buffer_length);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002255#endif
2256
2257 /* complete() can reenter this HCD */
2258 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
2259 spin_unlock(&fotg210->lock);
2260 usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status);
2261 spin_lock(&fotg210->lock);
2262}
2263
2264static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2265
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002266/* Process and free completed qtds for a qh, returning URBs to drivers.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002267 * Chases up to qh->hw_current. Returns number of completions called,
2268 * indicating how much "real" work we did.
2269 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002270static unsigned qh_completions(struct fotg210_hcd *fotg210,
2271 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002272{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002273 struct fotg210_qtd *last, *end = qh->dummy;
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002274 struct fotg210_qtd *qtd, *tmp;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002275 int last_status;
2276 int stopped;
2277 unsigned count = 0;
2278 u8 state;
2279 struct fotg210_qh_hw *hw = qh->hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002280
2281 if (unlikely(list_empty(&qh->qtd_list)))
2282 return count;
2283
2284 /* completions (or tasks on other cpus) must never clobber HALT
2285 * till we've gone through and cleaned everything up, even when
2286 * they add urbs to this qh's queue or mark them for unlinking.
2287 *
2288 * NOTE: unlinking expects to be done in queue order.
2289 *
2290 * It's a bug for qh->qh_state to be anything other than
2291 * QH_STATE_IDLE, unless our caller is scan_async() or
2292 * scan_intr().
2293 */
2294 state = qh->qh_state;
2295 qh->qh_state = QH_STATE_COMPLETING;
2296 stopped = (state == QH_STATE_IDLE);
2297
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002298rescan:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002299 last = NULL;
2300 last_status = -EINPROGRESS;
2301 qh->needs_rescan = 0;
2302
2303 /* remove de-activated QTDs from front of queue.
2304 * after faults (including short reads), cleanup this urb
2305 * then let the queue advance.
2306 * if queue is stopped, handles unlinks.
2307 */
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002308 list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002309 struct urb *urb;
2310 u32 token = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002311
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002312 urb = qtd->urb;
2313
2314 /* clean up any state from previous QTD ...*/
2315 if (last) {
2316 if (likely(last->urb != urb)) {
2317 fotg210_urb_done(fotg210, last->urb,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002318 last_status);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002319 count++;
2320 last_status = -EINPROGRESS;
2321 }
2322 fotg210_qtd_free(fotg210, last);
2323 last = NULL;
2324 }
2325
2326 /* ignore urbs submitted during completions we reported */
2327 if (qtd == end)
2328 break;
2329
2330 /* hardware copies qtd out of qh overlay */
2331 rmb();
2332 token = hc32_to_cpu(fotg210, qtd->hw_token);
2333
2334 /* always clean up qtds the hc de-activated */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002335retry_xacterr:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002336 if ((token & QTD_STS_ACTIVE) == 0) {
2337
2338 /* Report Data Buffer Error: non-fatal but useful */
2339 if (token & QTD_STS_DBE)
2340 fotg210_dbg(fotg210,
2341 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002342 urb, usb_endpoint_num(&urb->ep->desc),
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002343 usb_endpoint_dir_in(&urb->ep->desc)
2344 ? "in" : "out",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002345 urb->transfer_buffer_length, qtd, qh);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002346
2347 /* on STALL, error, and short reads this urb must
2348 * complete and all its qtds must be recycled.
2349 */
2350 if ((token & QTD_STS_HALT) != 0) {
2351
2352 /* retry transaction errors until we
2353 * reach the software xacterr limit
2354 */
2355 if ((token & QTD_STS_XACT) &&
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002356 QTD_CERR(token) == 0 &&
2357 ++qh->xacterrs < QH_XACTERR_MAX &&
2358 !urb->unlinked) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002359 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002360 "detected XactErr len %zu/%zu retry %d\n",
2361 qtd->length - QTD_LENGTH(token),
2362 qtd->length,
2363 qh->xacterrs);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002364
2365 /* reset the token in the qtd and the
2366 * qh overlay (which still contains
2367 * the qtd) so that we pick up from
2368 * where we left off
2369 */
2370 token &= ~QTD_STS_HALT;
2371 token |= QTD_STS_ACTIVE |
2372 (FOTG210_TUNE_CERR << 10);
2373 qtd->hw_token = cpu_to_hc32(fotg210,
2374 token);
2375 wmb();
2376 hw->hw_token = cpu_to_hc32(fotg210,
2377 token);
2378 goto retry_xacterr;
2379 }
2380 stopped = 1;
2381
2382 /* magic dummy for some short reads; qh won't advance.
2383 * that silicon quirk can kick in with this dummy too.
2384 *
2385 * other short reads won't stop the queue, including
2386 * control transfers (status stage handles that) or
2387 * most other single-qtd reads ... the queue stops if
2388 * URB_SHORT_NOT_OK was set so the driver submitting
2389 * the urbs could clean it up.
2390 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002391 } else if (IS_SHORT_READ(token) &&
2392 !(qtd->hw_alt_next &
2393 FOTG210_LIST_END(fotg210))) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002394 stopped = 1;
2395 }
2396
2397 /* stop scanning when we reach qtds the hc is using */
2398 } else if (likely(!stopped
2399 && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
2400 break;
2401
2402 /* scan the whole queue for unlinks whenever it stops */
2403 } else {
2404 stopped = 1;
2405
2406 /* cancel everything if we halt, suspend, etc */
2407 if (fotg210->rh_state < FOTG210_RH_RUNNING)
2408 last_status = -ESHUTDOWN;
2409
2410 /* this qtd is active; skip it unless a previous qtd
2411 * for its urb faulted, or its urb was canceled.
2412 */
2413 else if (last_status == -EINPROGRESS && !urb->unlinked)
2414 continue;
2415
2416 /* qh unlinked; token in overlay may be most current */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002417 if (state == QH_STATE_IDLE &&
2418 cpu_to_hc32(fotg210, qtd->qtd_dma)
2419 == hw->hw_current) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002420 token = hc32_to_cpu(fotg210, hw->hw_token);
2421
2422 /* An unlink may leave an incomplete
2423 * async transaction in the TT buffer.
2424 * We have to clear it.
2425 */
2426 fotg210_clear_tt_buffer(fotg210, qh, urb,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002427 token);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002428 }
2429 }
2430
2431 /* unless we already know the urb's status, collect qtd status
2432 * and update count of bytes transferred. in common short read
2433 * cases with only one data qtd (including control transfers),
2434 * queue processing won't halt. but with two or more qtds (for
2435 * example, with a 32 KB transfer), when the first qtd gets a
2436 * short read the second must be removed by hand.
2437 */
2438 if (last_status == -EINPROGRESS) {
2439 last_status = qtd_copy_status(fotg210, urb,
2440 qtd->length, token);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002441 if (last_status == -EREMOTEIO &&
2442 (qtd->hw_alt_next &
2443 FOTG210_LIST_END(fotg210)))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002444 last_status = -EINPROGRESS;
2445
2446 /* As part of low/full-speed endpoint-halt processing
2447 * we must clear the TT buffer (11.17.5).
2448 */
2449 if (unlikely(last_status != -EINPROGRESS &&
2450 last_status != -EREMOTEIO)) {
2451 /* The TT's in some hubs malfunction when they
2452 * receive this request following a STALL (they
2453 * stop sending isochronous packets). Since a
2454 * STALL can't leave the TT buffer in a busy
2455 * state (if you believe Figures 11-48 - 11-51
2456 * in the USB 2.0 spec), we won't clear the TT
2457 * buffer in this case. Strictly speaking this
2458 * is a violation of the spec.
2459 */
2460 if (last_status != -EPIPE)
2461 fotg210_clear_tt_buffer(fotg210, qh,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002462 urb, token);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002463 }
2464 }
2465
2466 /* if we're removing something not at the queue head,
2467 * patch the hardware queue pointer.
2468 */
2469 if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
2470 last = list_entry(qtd->qtd_list.prev,
2471 struct fotg210_qtd, qtd_list);
2472 last->hw_next = qtd->hw_next;
2473 }
2474
2475 /* remove qtd; it's recycled after possible urb completion */
2476 list_del(&qtd->qtd_list);
2477 last = qtd;
2478
2479 /* reinit the xacterr counter for the next qtd */
2480 qh->xacterrs = 0;
2481 }
2482
2483 /* last urb's completion might still need calling */
2484 if (likely(last != NULL)) {
2485 fotg210_urb_done(fotg210, last->urb, last_status);
2486 count++;
2487 fotg210_qtd_free(fotg210, last);
2488 }
2489
2490 /* Do we need to rescan for URBs dequeued during a giveback? */
2491 if (unlikely(qh->needs_rescan)) {
2492 /* If the QH is already unlinked, do the rescan now. */
2493 if (state == QH_STATE_IDLE)
2494 goto rescan;
2495
2496 /* Otherwise we have to wait until the QH is fully unlinked.
2497 * Our caller will start an unlink if qh->needs_rescan is
2498 * set. But if an unlink has already started, nothing needs
2499 * to be done.
2500 */
2501 if (state != QH_STATE_LINKED)
2502 qh->needs_rescan = 0;
2503 }
2504
2505 /* restore original state; caller must unlink or relink */
2506 qh->qh_state = state;
2507
2508 /* be sure the hardware's done with the qh before refreshing
2509 * it after fault cleanup, or recovering from silicon wrongly
2510 * overlaying the dummy qtd (which reduces DMA chatter).
2511 */
2512 if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
2513 switch (state) {
2514 case QH_STATE_IDLE:
2515 qh_refresh(fotg210, qh);
2516 break;
2517 case QH_STATE_LINKED:
2518 /* We won't refresh a QH that's linked (after the HC
2519 * stopped the queue). That avoids a race:
2520 * - HC reads first part of QH;
2521 * - CPU updates that first part and the token;
2522 * - HC reads rest of that QH, including token
2523 * Result: HC gets an inconsistent image, and then
2524 * DMAs to/from the wrong memory (corrupting it).
2525 *
2526 * That should be rare for interrupt transfers,
2527 * except maybe high bandwidth ...
2528 */
2529
2530 /* Tell the caller to start an unlink */
2531 qh->needs_rescan = 1;
2532 break;
2533 /* otherwise, unlink already started */
2534 }
2535 }
2536
2537 return count;
2538}
2539
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002540/* reverse of qh_urb_transaction: free a list of TDs.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002541 * used for cleanup after errors, before HC sees an URB's TDs.
2542 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002543static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb,
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002544 struct list_head *head)
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002545{
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002546 struct fotg210_qtd *qtd, *temp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002547
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002548 list_for_each_entry_safe(qtd, temp, head, qtd_list) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002549 list_del(&qtd->qtd_list);
2550 fotg210_qtd_free(fotg210, qtd);
2551 }
2552}
2553
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002554/* create a list of filled qtds for this URB; won't link into qh.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002555 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002556static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210,
2557 struct urb *urb, struct list_head *head, gfp_t flags)
2558{
2559 struct fotg210_qtd *qtd, *qtd_prev;
2560 dma_addr_t buf;
2561 int len, this_sg_len, maxpacket;
2562 int is_input;
2563 u32 token;
2564 int i;
2565 struct scatterlist *sg;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002566
2567 /*
2568 * URBs map to sequences of QTDs: one logical transaction
2569 */
2570 qtd = fotg210_qtd_alloc(fotg210, flags);
2571 if (unlikely(!qtd))
2572 return NULL;
2573 list_add_tail(&qtd->qtd_list, head);
2574 qtd->urb = urb;
2575
2576 token = QTD_STS_ACTIVE;
2577 token |= (FOTG210_TUNE_CERR << 10);
2578 /* for split transactions, SplitXState initialized to zero */
2579
2580 len = urb->transfer_buffer_length;
2581 is_input = usb_pipein(urb->pipe);
2582 if (usb_pipecontrol(urb->pipe)) {
2583 /* SETUP pid */
2584 qtd_fill(fotg210, qtd, urb->setup_dma,
2585 sizeof(struct usb_ctrlrequest),
2586 token | (2 /* "setup" */ << 8), 8);
2587
2588 /* ... and always at least one more pid */
2589 token ^= QTD_TOGGLE;
2590 qtd_prev = qtd;
2591 qtd = fotg210_qtd_alloc(fotg210, flags);
2592 if (unlikely(!qtd))
2593 goto cleanup;
2594 qtd->urb = urb;
2595 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2596 list_add_tail(&qtd->qtd_list, head);
2597
2598 /* for zero length DATA stages, STATUS is always IN */
2599 if (len == 0)
2600 token |= (1 /* "in" */ << 8);
2601 }
2602
2603 /*
2604 * data transfer stage: buffer setup
2605 */
2606 i = urb->num_mapped_sgs;
2607 if (len > 0 && i > 0) {
2608 sg = urb->sg;
2609 buf = sg_dma_address(sg);
2610
2611 /* urb->transfer_buffer_length may be smaller than the
2612 * size of the scatterlist (or vice versa)
2613 */
2614 this_sg_len = min_t(int, sg_dma_len(sg), len);
2615 } else {
2616 sg = NULL;
2617 buf = urb->transfer_dma;
2618 this_sg_len = len;
2619 }
2620
2621 if (is_input)
2622 token |= (1 /* "in" */ << 8);
2623 /* else it's already initted to "out" pid (0 << 8) */
2624
Kelly Devillivff5609a2021-06-27 20:57:46 +08002625 maxpacket = usb_maxpacket(urb->dev, urb->pipe, !is_input);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002626
2627 /*
2628 * buffer gets wrapped in one or more qtds;
2629 * last one may be "short" (including zero len)
2630 * and may serve as a control status ack
2631 */
2632 for (;;) {
2633 int this_qtd_len;
2634
2635 this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
2636 maxpacket);
2637 this_sg_len -= this_qtd_len;
2638 len -= this_qtd_len;
2639 buf += this_qtd_len;
2640
2641 /*
2642 * short reads advance to a "magic" dummy instead of the next
2643 * qtd ... that forces the queue to stop, for manual cleanup.
2644 * (this will usually be overridden later.)
2645 */
2646 if (is_input)
2647 qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
2648
2649 /* qh makes control packets use qtd toggle; maybe switch it */
2650 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
2651 token ^= QTD_TOGGLE;
2652
2653 if (likely(this_sg_len <= 0)) {
2654 if (--i <= 0 || len <= 0)
2655 break;
2656 sg = sg_next(sg);
2657 buf = sg_dma_address(sg);
2658 this_sg_len = min_t(int, sg_dma_len(sg), len);
2659 }
2660
2661 qtd_prev = qtd;
2662 qtd = fotg210_qtd_alloc(fotg210, flags);
2663 if (unlikely(!qtd))
2664 goto cleanup;
2665 qtd->urb = urb;
2666 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2667 list_add_tail(&qtd->qtd_list, head);
2668 }
2669
2670 /*
2671 * unless the caller requires manual cleanup after short reads,
2672 * have the alt_next mechanism keep the queue running after the
2673 * last data qtd (the only one, for control and most other cases).
2674 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002675 if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 ||
2676 usb_pipecontrol(urb->pipe)))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002677 qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
2678
2679 /*
2680 * control requests may need a terminating data "status" ack;
2681 * other OUT ones may need a terminating short packet
2682 * (zero length).
2683 */
2684 if (likely(urb->transfer_buffer_length != 0)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002685 int one_more = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002686
2687 if (usb_pipecontrol(urb->pipe)) {
2688 one_more = 1;
2689 token ^= 0x0100; /* "in" <--> "out" */
2690 token |= QTD_TOGGLE; /* force DATA1 */
2691 } else if (usb_pipeout(urb->pipe)
2692 && (urb->transfer_flags & URB_ZERO_PACKET)
2693 && !(urb->transfer_buffer_length % maxpacket)) {
2694 one_more = 1;
2695 }
2696 if (one_more) {
2697 qtd_prev = qtd;
2698 qtd = fotg210_qtd_alloc(fotg210, flags);
2699 if (unlikely(!qtd))
2700 goto cleanup;
2701 qtd->urb = urb;
2702 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2703 list_add_tail(&qtd->qtd_list, head);
2704
2705 /* never any data in such packets */
2706 qtd_fill(fotg210, qtd, 0, 0, token, 0);
2707 }
2708 }
2709
2710 /* by default, enable interrupt on urb completion */
2711 if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
2712 qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
2713 return head;
2714
2715cleanup:
2716 qtd_list_free(fotg210, urb, head);
2717 return NULL;
2718}
2719
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002720/* Would be best to create all qh's from config descriptors,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002721 * when each interface/altsetting is established. Unlink
2722 * any previous qh and cancel its urbs first; endpoints are
2723 * implicitly reset then (data toggle too).
2724 * That'd mean updating how usbcore talks to HCDs. (2.7?)
2725*/
2726
2727
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002728/* Each QH holds a qtd list; a QH is used for everything except iso.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002729 *
2730 * For interrupt urbs, the scheduler must set the microframe scheduling
2731 * mask(s) each time the QH gets scheduled. For highspeed, that's
2732 * just one microframe in the s-mask. For split interrupt transactions
2733 * there are additional complications: c-mask, maybe FSTNs.
2734 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002735static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb,
2736 gfp_t flags)
2737{
2738 struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags);
Kelly Devillivff5609a2021-06-27 20:57:46 +08002739 struct usb_host_endpoint *ep;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002740 u32 info1 = 0, info2 = 0;
2741 int is_input, type;
2742 int maxp = 0;
Kelly Devillivff5609a2021-06-27 20:57:46 +08002743 int mult;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002744 struct usb_tt *tt = urb->dev->tt;
2745 struct fotg210_qh_hw *hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002746
2747 if (!qh)
2748 return qh;
2749
2750 /*
2751 * init endpoint/device data for this QH
2752 */
2753 info1 |= usb_pipeendpoint(urb->pipe) << 8;
2754 info1 |= usb_pipedevice(urb->pipe) << 0;
2755
2756 is_input = usb_pipein(urb->pipe);
2757 type = usb_pipetype(urb->pipe);
Kelly Devillivff5609a2021-06-27 20:57:46 +08002758 ep = usb_pipe_endpoint(urb->dev, urb->pipe);
2759 maxp = usb_endpoint_maxp(&ep->desc);
2760 mult = usb_endpoint_maxp_mult(&ep->desc);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002761
2762 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
2763 * acts like up to 3KB, but is built from smaller packets.
2764 */
Kelly Devillivff5609a2021-06-27 20:57:46 +08002765 if (maxp > 1024) {
2766 fotg210_dbg(fotg210, "bogus qh maxpacket %d\n", maxp);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002767 goto done;
2768 }
2769
2770 /* Compute interrupt scheduling parameters just once, and save.
2771 * - allowing for high bandwidth, how many nsec/uframe are used?
2772 * - split transactions need a second CSPLIT uframe; same question
2773 * - splits also need a schedule gap (for full/low speed I/O)
2774 * - qh has a polling interval
2775 *
2776 * For control/bulk requests, the HC or TT handles these.
2777 */
2778 if (type == PIPE_INTERRUPT) {
2779 qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
Kelly Devillivff5609a2021-06-27 20:57:46 +08002780 is_input, 0, mult * maxp));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002781 qh->start = NO_FRAME;
2782
2783 if (urb->dev->speed == USB_SPEED_HIGH) {
2784 qh->c_usecs = 0;
2785 qh->gap_uf = 0;
2786
2787 qh->period = urb->interval >> 3;
2788 if (qh->period == 0 && urb->interval != 1) {
2789 /* NOTE interval 2 or 4 uframes could work.
2790 * But interval 1 scheduling is simpler, and
2791 * includes high bandwidth.
2792 */
2793 urb->interval = 1;
2794 } else if (qh->period > fotg210->periodic_size) {
2795 qh->period = fotg210->periodic_size;
2796 urb->interval = qh->period << 3;
2797 }
2798 } else {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002799 int think_time;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002800
2801 /* gap is f(FS/LS transfer times) */
2802 qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
2803 is_input, 0, maxp) / (125 * 1000);
2804
2805 /* FIXME this just approximates SPLIT/CSPLIT times */
2806 if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
2807 qh->c_usecs = qh->usecs + HS_USECS(0);
2808 qh->usecs = HS_USECS(1);
2809 } else { /* SPLIT+DATA, gap, CSPLIT */
2810 qh->usecs += HS_USECS(1);
2811 qh->c_usecs = HS_USECS(0);
2812 }
2813
2814 think_time = tt ? tt->think_time : 0;
2815 qh->tt_usecs = NS_TO_US(think_time +
2816 usb_calc_bus_time(urb->dev->speed,
Kelly Devillivff5609a2021-06-27 20:57:46 +08002817 is_input, 0, maxp));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002818 qh->period = urb->interval;
2819 if (qh->period > fotg210->periodic_size) {
2820 qh->period = fotg210->periodic_size;
2821 urb->interval = qh->period;
2822 }
2823 }
2824 }
2825
2826 /* support for tt scheduling, and access to toggles */
2827 qh->dev = urb->dev;
2828
2829 /* using TT? */
2830 switch (urb->dev->speed) {
2831 case USB_SPEED_LOW:
2832 info1 |= QH_LOW_SPEED;
2833 /* FALL THROUGH */
2834
2835 case USB_SPEED_FULL:
2836 /* EPS 0 means "full" */
2837 if (type != PIPE_INTERRUPT)
2838 info1 |= (FOTG210_TUNE_RL_TT << 28);
2839 if (type == PIPE_CONTROL) {
2840 info1 |= QH_CONTROL_EP; /* for TT */
2841 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2842 }
2843 info1 |= maxp << 16;
2844
2845 info2 |= (FOTG210_TUNE_MULT_TT << 30);
2846
2847 /* Some Freescale processors have an erratum in which the
2848 * port number in the queue head was 0..N-1 instead of 1..N.
2849 */
2850 if (fotg210_has_fsl_portno_bug(fotg210))
2851 info2 |= (urb->dev->ttport-1) << 23;
2852 else
2853 info2 |= urb->dev->ttport << 23;
2854
2855 /* set the address of the TT; for TDI's integrated
2856 * root hub tt, leave it zeroed.
2857 */
2858 if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
2859 info2 |= tt->hub->devnum << 16;
2860
2861 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
2862
2863 break;
2864
2865 case USB_SPEED_HIGH: /* no TT involved */
2866 info1 |= QH_HIGH_SPEED;
2867 if (type == PIPE_CONTROL) {
2868 info1 |= (FOTG210_TUNE_RL_HS << 28);
2869 info1 |= 64 << 16; /* usb2 fixed maxpacket */
2870 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2871 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2872 } else if (type == PIPE_BULK) {
2873 info1 |= (FOTG210_TUNE_RL_HS << 28);
2874 /* The USB spec says that high speed bulk endpoints
2875 * always use 512 byte maxpacket. But some device
2876 * vendors decided to ignore that, and MSFT is happy
2877 * to help them do so. So now people expect to use
2878 * such nonconformant devices with Linux too; sigh.
2879 */
Kelly Devillivff5609a2021-06-27 20:57:46 +08002880 info1 |= maxp << 16;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002881 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2882 } else { /* PIPE_INTERRUPT */
Kelly Devillivff5609a2021-06-27 20:57:46 +08002883 info1 |= maxp << 16;
2884 info2 |= mult << 30;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002885 }
2886 break;
2887 default:
2888 fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002889 urb->dev->speed);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002890done:
2891 qh_destroy(fotg210, qh);
2892 return NULL;
2893 }
2894
2895 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
2896
2897 /* init as live, toggle clear, advance to dummy */
2898 qh->qh_state = QH_STATE_IDLE;
2899 hw = qh->hw;
2900 hw->hw_info1 = cpu_to_hc32(fotg210, info1);
2901 hw->hw_info2 = cpu_to_hc32(fotg210, info2);
2902 qh->is_out = !is_input;
2903 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
2904 qh_refresh(fotg210, qh);
2905 return qh;
2906}
2907
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002908static void enable_async(struct fotg210_hcd *fotg210)
2909{
2910 if (fotg210->async_count++)
2911 return;
2912
2913 /* Stop waiting to turn off the async schedule */
2914 fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
2915
2916 /* Don't start the schedule until ASS is 0 */
2917 fotg210_poll_ASS(fotg210);
2918 turn_on_io_watchdog(fotg210);
2919}
2920
2921static void disable_async(struct fotg210_hcd *fotg210)
2922{
2923 if (--fotg210->async_count)
2924 return;
2925
2926 /* The async schedule and async_unlink list are supposed to be empty */
2927 WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
2928
2929 /* Don't turn off the schedule until ASS is 1 */
2930 fotg210_poll_ASS(fotg210);
2931}
2932
2933/* move qh (and its qtds) onto async queue; maybe enable queue. */
2934
2935static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
2936{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002937 __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
2938 struct fotg210_qh *head;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002939
2940 /* Don't link a QH if there's a Clear-TT-Buffer pending */
2941 if (unlikely(qh->clearing_tt))
2942 return;
2943
2944 WARN_ON(qh->qh_state != QH_STATE_IDLE);
2945
2946 /* clear halt and/or toggle; and maybe recover from silicon quirk */
2947 qh_refresh(fotg210, qh);
2948
2949 /* splice right after start */
2950 head = fotg210->async;
2951 qh->qh_next = head->qh_next;
2952 qh->hw->hw_next = head->hw->hw_next;
2953 wmb();
2954
2955 head->qh_next.qh = qh;
2956 head->hw->hw_next = dma;
2957
2958 qh->xacterrs = 0;
2959 qh->qh_state = QH_STATE_LINKED;
2960 /* qtd completions reported later by interrupt */
2961
2962 enable_async(fotg210);
2963}
2964
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002965/* For control/bulk/interrupt, return QH with these TDs appended.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002966 * Allocates and initializes the QH if necessary.
2967 * Returns null if it can't allocate a QH it needs to.
2968 * If the QH has TDs (urbs) already, that's great.
2969 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002970static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210,
2971 struct urb *urb, struct list_head *qtd_list,
2972 int epnum, void **ptr)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002973{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002974 struct fotg210_qh *qh = NULL;
2975 __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002976
2977 qh = (struct fotg210_qh *) *ptr;
2978 if (unlikely(qh == NULL)) {
2979 /* can't sleep here, we have fotg210->lock... */
2980 qh = qh_make(fotg210, urb, GFP_ATOMIC);
2981 *ptr = qh;
2982 }
2983 if (likely(qh != NULL)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002984 struct fotg210_qtd *qtd;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002985
2986 if (unlikely(list_empty(qtd_list)))
2987 qtd = NULL;
2988 else
2989 qtd = list_entry(qtd_list->next, struct fotg210_qtd,
2990 qtd_list);
2991
2992 /* control qh may need patching ... */
2993 if (unlikely(epnum == 0)) {
2994 /* usb_reset_device() briefly reverts to address 0 */
2995 if (usb_pipedevice(urb->pipe) == 0)
2996 qh->hw->hw_info1 &= ~qh_addr_mask;
2997 }
2998
2999 /* just one way to queue requests: swap with the dummy qtd.
3000 * only hc or qh_refresh() ever modify the overlay.
3001 */
3002 if (likely(qtd != NULL)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003003 struct fotg210_qtd *dummy;
3004 dma_addr_t dma;
3005 __hc32 token;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003006
3007 /* to avoid racing the HC, use the dummy td instead of
3008 * the first td of our list (becomes new dummy). both
3009 * tds stay deactivated until we're done, when the
3010 * HC is allowed to fetch the old dummy (4.10.2).
3011 */
3012 token = qtd->hw_token;
3013 qtd->hw_token = HALT_BIT(fotg210);
3014
3015 dummy = qh->dummy;
3016
3017 dma = dummy->qtd_dma;
3018 *dummy = *qtd;
3019 dummy->qtd_dma = dma;
3020
3021 list_del(&qtd->qtd_list);
3022 list_add(&dummy->qtd_list, qtd_list);
3023 list_splice_tail(qtd_list, &qh->qtd_list);
3024
3025 fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
3026 qh->dummy = qtd;
3027
3028 /* hc must see the new dummy at list end */
3029 dma = qtd->qtd_dma;
3030 qtd = list_entry(qh->qtd_list.prev,
3031 struct fotg210_qtd, qtd_list);
3032 qtd->hw_next = QTD_NEXT(fotg210, dma);
3033
3034 /* let the hc process these next qtds */
3035 wmb();
3036 dummy->hw_token = token;
3037
3038 urb->hcpriv = qh;
3039 }
3040 }
3041 return qh;
3042}
3043
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003044static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb,
3045 struct list_head *qtd_list, gfp_t mem_flags)
3046{
3047 int epnum;
3048 unsigned long flags;
3049 struct fotg210_qh *qh = NULL;
3050 int rc;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003051
3052 epnum = urb->ep->desc.bEndpointAddress;
3053
3054#ifdef FOTG210_URB_TRACE
3055 {
3056 struct fotg210_qtd *qtd;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003057
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003058 qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list);
3059 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003060 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
3061 __func__, urb->dev->devpath, urb,
3062 epnum & 0x0f, (epnum & USB_DIR_IN)
3063 ? "in" : "out",
3064 urb->transfer_buffer_length,
3065 qtd, urb->ep->hcpriv);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003066 }
3067#endif
3068
3069 spin_lock_irqsave(&fotg210->lock, flags);
3070 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3071 rc = -ESHUTDOWN;
3072 goto done;
3073 }
3074 rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
3075 if (unlikely(rc))
3076 goto done;
3077
3078 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
3079 if (unlikely(qh == NULL)) {
3080 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
3081 rc = -ENOMEM;
3082 goto done;
3083 }
3084
3085 /* Control/bulk operations through TTs don't need scheduling,
3086 * the HC and TT handle it when the TT has a buffer ready.
3087 */
3088 if (likely(qh->qh_state == QH_STATE_IDLE))
3089 qh_link_async(fotg210, qh);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003090done:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003091 spin_unlock_irqrestore(&fotg210->lock, flags);
3092 if (unlikely(qh == NULL))
3093 qtd_list_free(fotg210, urb, qtd_list);
3094 return rc;
3095}
3096
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003097static void single_unlink_async(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003098 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003099{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003100 struct fotg210_qh *prev;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003101
3102 /* Add to the end of the list of QHs waiting for the next IAAD */
3103 qh->qh_state = QH_STATE_UNLINK;
3104 if (fotg210->async_unlink)
3105 fotg210->async_unlink_last->unlink_next = qh;
3106 else
3107 fotg210->async_unlink = qh;
3108 fotg210->async_unlink_last = qh;
3109
3110 /* Unlink it from the schedule */
3111 prev = fotg210->async;
3112 while (prev->qh_next.qh != qh)
3113 prev = prev->qh_next.qh;
3114
3115 prev->hw->hw_next = qh->hw->hw_next;
3116 prev->qh_next = qh->qh_next;
3117 if (fotg210->qh_scan_next == qh)
3118 fotg210->qh_scan_next = qh->qh_next.qh;
3119}
3120
3121static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
3122{
3123 /*
3124 * Do nothing if an IAA cycle is already running or
3125 * if one will be started shortly.
3126 */
3127 if (fotg210->async_iaa || fotg210->async_unlinking)
3128 return;
3129
3130 /* Do all the waiting QHs at once */
3131 fotg210->async_iaa = fotg210->async_unlink;
3132 fotg210->async_unlink = NULL;
3133
3134 /* If the controller isn't running, we don't have to wait for it */
3135 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
3136 if (!nested) /* Avoid recursion */
3137 end_unlink_async(fotg210);
3138
3139 /* Otherwise start a new IAA cycle */
3140 } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
3141 /* Make sure the unlinks are all visible to the hardware */
3142 wmb();
3143
3144 fotg210_writel(fotg210, fotg210->command | CMD_IAAD,
3145 &fotg210->regs->command);
3146 fotg210_readl(fotg210, &fotg210->regs->command);
3147 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003148 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003149 }
3150}
3151
3152/* the async qh for the qtds being unlinked are now gone from the HC */
3153
3154static void end_unlink_async(struct fotg210_hcd *fotg210)
3155{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003156 struct fotg210_qh *qh;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003157
3158 /* Process the idle QHs */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003159restart:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003160 fotg210->async_unlinking = true;
3161 while (fotg210->async_iaa) {
3162 qh = fotg210->async_iaa;
3163 fotg210->async_iaa = qh->unlink_next;
3164 qh->unlink_next = NULL;
3165
3166 qh->qh_state = QH_STATE_IDLE;
3167 qh->qh_next.qh = NULL;
3168
3169 qh_completions(fotg210, qh);
3170 if (!list_empty(&qh->qtd_list) &&
3171 fotg210->rh_state == FOTG210_RH_RUNNING)
3172 qh_link_async(fotg210, qh);
3173 disable_async(fotg210);
3174 }
3175 fotg210->async_unlinking = false;
3176
3177 /* Start a new IAA cycle if any QHs are waiting for it */
3178 if (fotg210->async_unlink) {
3179 start_iaa_cycle(fotg210, true);
3180 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
3181 goto restart;
3182 }
3183}
3184
3185static void unlink_empty_async(struct fotg210_hcd *fotg210)
3186{
3187 struct fotg210_qh *qh, *next;
3188 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
3189 bool check_unlinks_later = false;
3190
3191 /* Unlink all the async QHs that have been empty for a timer cycle */
3192 next = fotg210->async->qh_next.qh;
3193 while (next) {
3194 qh = next;
3195 next = qh->qh_next.qh;
3196
3197 if (list_empty(&qh->qtd_list) &&
3198 qh->qh_state == QH_STATE_LINKED) {
3199 if (!stopped && qh->unlink_cycle ==
3200 fotg210->async_unlink_cycle)
3201 check_unlinks_later = true;
3202 else
3203 single_unlink_async(fotg210, qh);
3204 }
3205 }
3206
3207 /* Start a new IAA cycle if any QHs are waiting for it */
3208 if (fotg210->async_unlink)
3209 start_iaa_cycle(fotg210, false);
3210
3211 /* QHs that haven't been empty for long enough will be handled later */
3212 if (check_unlinks_later) {
3213 fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003214 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003215 ++fotg210->async_unlink_cycle;
3216 }
3217}
3218
3219/* makes sure the async qh will become idle */
3220/* caller must own fotg210->lock */
3221
3222static void start_unlink_async(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003223 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003224{
3225 /*
3226 * If the QH isn't linked then there's nothing we can do
3227 * unless we were called during a giveback, in which case
3228 * qh_completions() has to deal with it.
3229 */
3230 if (qh->qh_state != QH_STATE_LINKED) {
3231 if (qh->qh_state == QH_STATE_COMPLETING)
3232 qh->needs_rescan = 1;
3233 return;
3234 }
3235
3236 single_unlink_async(fotg210, qh);
3237 start_iaa_cycle(fotg210, false);
3238}
3239
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003240static void scan_async(struct fotg210_hcd *fotg210)
3241{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003242 struct fotg210_qh *qh;
3243 bool check_unlinks_later = false;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003244
3245 fotg210->qh_scan_next = fotg210->async->qh_next.qh;
3246 while (fotg210->qh_scan_next) {
3247 qh = fotg210->qh_scan_next;
3248 fotg210->qh_scan_next = qh->qh_next.qh;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003249rescan:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003250 /* clean any finished work for this qh */
3251 if (!list_empty(&qh->qtd_list)) {
3252 int temp;
3253
3254 /*
3255 * Unlinks could happen here; completion reporting
3256 * drops the lock. That's why fotg210->qh_scan_next
3257 * always holds the next qh to scan; if the next qh
3258 * gets unlinked then fotg210->qh_scan_next is adjusted
3259 * in single_unlink_async().
3260 */
3261 temp = qh_completions(fotg210, qh);
3262 if (qh->needs_rescan) {
3263 start_unlink_async(fotg210, qh);
3264 } else if (list_empty(&qh->qtd_list)
3265 && qh->qh_state == QH_STATE_LINKED) {
3266 qh->unlink_cycle = fotg210->async_unlink_cycle;
3267 check_unlinks_later = true;
3268 } else if (temp != 0)
3269 goto rescan;
3270 }
3271 }
3272
3273 /*
3274 * Unlink empty entries, reducing DMA usage as well
3275 * as HCD schedule-scanning costs. Delay for any qh
3276 * we just scanned, there's a not-unusual case that it
3277 * doesn't stay idle for long.
3278 */
3279 if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING &&
3280 !(fotg210->enabled_hrtimer_events &
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003281 BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003282 fotg210_enable_event(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003283 FOTG210_HRTIMER_ASYNC_UNLINKS, true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003284 ++fotg210->async_unlink_cycle;
3285 }
3286}
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003287/* EHCI scheduled transaction support: interrupt, iso, split iso
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003288 * These are called "periodic" transactions in the EHCI spec.
3289 *
3290 * Note that for interrupt transfers, the QH/QTD manipulation is shared
3291 * with the "asynchronous" transaction support (control/bulk transfers).
3292 * The only real difference is in how interrupt transfers are scheduled.
3293 *
3294 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
3295 * It keeps track of every ITD (or SITD) that's linked, and holds enough
3296 * pre-calculated schedule data to make appending to the queue be quick.
3297 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003298static int fotg210_get_frame(struct usb_hcd *hcd);
3299
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003300/* periodic_next_shadow - return "next" pointer on shadow list
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003301 * @periodic: host pointer to qh/itd
3302 * @tag: hardware tag for type of this record
3303 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003304static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210,
3305 union fotg210_shadow *periodic, __hc32 tag)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003306{
3307 switch (hc32_to_cpu(fotg210, tag)) {
3308 case Q_TYPE_QH:
3309 return &periodic->qh->qh_next;
3310 case Q_TYPE_FSTN:
3311 return &periodic->fstn->fstn_next;
3312 default:
3313 return &periodic->itd->itd_next;
3314 }
3315}
3316
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003317static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210,
3318 union fotg210_shadow *periodic, __hc32 tag)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003319{
3320 switch (hc32_to_cpu(fotg210, tag)) {
3321 /* our fotg210_shadow.qh is actually software part */
3322 case Q_TYPE_QH:
3323 return &periodic->qh->hw->hw_next;
3324 /* others are hw parts */
3325 default:
3326 return periodic->hw_next;
3327 }
3328}
3329
3330/* caller must hold fotg210->lock */
3331static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003332 void *ptr)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003333{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003334 union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
3335 __hc32 *hw_p = &fotg210->periodic[frame];
3336 union fotg210_shadow here = *prev_p;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003337
3338 /* find predecessor of "ptr"; hw and shadow lists are in sync */
3339 while (here.ptr && here.ptr != ptr) {
3340 prev_p = periodic_next_shadow(fotg210, prev_p,
3341 Q_NEXT_TYPE(fotg210, *hw_p));
3342 hw_p = shadow_next_periodic(fotg210, &here,
3343 Q_NEXT_TYPE(fotg210, *hw_p));
3344 here = *prev_p;
3345 }
3346 /* an interrupt entry (at list end) could have been shared */
3347 if (!here.ptr)
3348 return;
3349
3350 /* update shadow and hardware lists ... the old "next" pointers
3351 * from ptr may still be in use, the caller updates them.
3352 */
3353 *prev_p = *periodic_next_shadow(fotg210, &here,
3354 Q_NEXT_TYPE(fotg210, *hw_p));
3355
3356 *hw_p = *shadow_next_periodic(fotg210, &here,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003357 Q_NEXT_TYPE(fotg210, *hw_p));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003358}
3359
3360/* how many of the uframe's 125 usecs are allocated? */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003361static unsigned short periodic_usecs(struct fotg210_hcd *fotg210,
3362 unsigned frame, unsigned uframe)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003363{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003364 __hc32 *hw_p = &fotg210->periodic[frame];
3365 union fotg210_shadow *q = &fotg210->pshadow[frame];
3366 unsigned usecs = 0;
3367 struct fotg210_qh_hw *hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003368
3369 while (q->ptr) {
3370 switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
3371 case Q_TYPE_QH:
3372 hw = q->qh->hw;
3373 /* is it in the S-mask? */
3374 if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
3375 usecs += q->qh->usecs;
3376 /* ... or C-mask? */
3377 if (hw->hw_info2 & cpu_to_hc32(fotg210,
3378 1 << (8 + uframe)))
3379 usecs += q->qh->c_usecs;
3380 hw_p = &hw->hw_next;
3381 q = &q->qh->qh_next;
3382 break;
3383 /* case Q_TYPE_FSTN: */
3384 default:
3385 /* for "save place" FSTNs, count the relevant INTR
3386 * bandwidth from the previous frame
3387 */
3388 if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
3389 fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
3390
3391 hw_p = &q->fstn->hw_next;
3392 q = &q->fstn->fstn_next;
3393 break;
3394 case Q_TYPE_ITD:
3395 if (q->itd->hw_transaction[uframe])
3396 usecs += q->itd->stream->usecs;
3397 hw_p = &q->itd->hw_next;
3398 q = &q->itd->itd_next;
3399 break;
3400 }
3401 }
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003402 if (usecs > fotg210->uframe_periodic_max)
3403 fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003404 frame * 8 + uframe, usecs);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003405 return usecs;
3406}
3407
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003408static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
3409{
3410 if (!dev1->tt || !dev2->tt)
3411 return 0;
3412 if (dev1->tt != dev2->tt)
3413 return 0;
3414 if (dev1->tt->multi)
3415 return dev1->ttport == dev2->ttport;
3416 else
3417 return 1;
3418}
3419
3420/* return true iff the device's transaction translator is available
3421 * for a periodic transfer starting at the specified frame, using
3422 * all the uframes in the mask.
3423 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003424static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period,
3425 struct usb_device *dev, unsigned frame, u32 uf_mask)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003426{
3427 if (period == 0) /* error */
3428 return 0;
3429
3430 /* note bandwidth wastage: split never follows csplit
3431 * (different dev or endpoint) until the next uframe.
3432 * calling convention doesn't make that distinction.
3433 */
3434 for (; frame < fotg210->periodic_size; frame += period) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003435 union fotg210_shadow here;
3436 __hc32 type;
3437 struct fotg210_qh_hw *hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003438
3439 here = fotg210->pshadow[frame];
3440 type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
3441 while (here.ptr) {
3442 switch (hc32_to_cpu(fotg210, type)) {
3443 case Q_TYPE_ITD:
3444 type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
3445 here = here.itd->itd_next;
3446 continue;
3447 case Q_TYPE_QH:
3448 hw = here.qh->hw;
3449 if (same_tt(dev, here.qh->dev)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003450 u32 mask;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003451
3452 mask = hc32_to_cpu(fotg210,
3453 hw->hw_info2);
3454 /* "knows" no gap is needed */
3455 mask |= mask >> 8;
3456 if (mask & uf_mask)
3457 break;
3458 }
3459 type = Q_NEXT_TYPE(fotg210, hw->hw_next);
3460 here = here.qh->qh_next;
3461 continue;
3462 /* case Q_TYPE_FSTN: */
3463 default:
3464 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003465 "periodic frame %d bogus type %d\n",
3466 frame, type);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003467 }
3468
3469 /* collision or error */
3470 return 0;
3471 }
3472 }
3473
3474 /* no collision */
3475 return 1;
3476}
3477
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003478static void enable_periodic(struct fotg210_hcd *fotg210)
3479{
3480 if (fotg210->periodic_count++)
3481 return;
3482
3483 /* Stop waiting to turn off the periodic schedule */
3484 fotg210->enabled_hrtimer_events &=
3485 ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
3486
3487 /* Don't start the schedule until PSS is 0 */
3488 fotg210_poll_PSS(fotg210);
3489 turn_on_io_watchdog(fotg210);
3490}
3491
3492static void disable_periodic(struct fotg210_hcd *fotg210)
3493{
3494 if (--fotg210->periodic_count)
3495 return;
3496
3497 /* Don't turn off the schedule until PSS is 1 */
3498 fotg210_poll_PSS(fotg210);
3499}
3500
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003501/* periodic schedule slots have iso tds (normal or split) first, then a
3502 * sparse tree for active interrupt transfers.
3503 *
3504 * this just links in a qh; caller guarantees uframe masks are set right.
3505 * no FSTN support (yet; fotg210 0.96+)
3506 */
3507static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3508{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003509 unsigned i;
3510 unsigned period = qh->period;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003511
3512 dev_dbg(&qh->dev->dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003513 "link qh%d-%04x/%p start %d [%d/%d us]\n", period,
3514 hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
3515 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
3516 qh->c_usecs);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003517
3518 /* high bandwidth, or otherwise every microframe */
3519 if (period == 0)
3520 period = 1;
3521
3522 for (i = qh->start; i < fotg210->periodic_size; i += period) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003523 union fotg210_shadow *prev = &fotg210->pshadow[i];
3524 __hc32 *hw_p = &fotg210->periodic[i];
3525 union fotg210_shadow here = *prev;
3526 __hc32 type = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003527
3528 /* skip the iso nodes at list head */
3529 while (here.ptr) {
3530 type = Q_NEXT_TYPE(fotg210, *hw_p);
3531 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
3532 break;
3533 prev = periodic_next_shadow(fotg210, prev, type);
3534 hw_p = shadow_next_periodic(fotg210, &here, type);
3535 here = *prev;
3536 }
3537
3538 /* sorting each branch by period (slow-->fast)
3539 * enables sharing interior tree nodes
3540 */
3541 while (here.ptr && qh != here.qh) {
3542 if (qh->period > here.qh->period)
3543 break;
3544 prev = &here.qh->qh_next;
3545 hw_p = &here.qh->hw->hw_next;
3546 here = *prev;
3547 }
3548 /* link in this qh, unless some earlier pass did that */
3549 if (qh != here.qh) {
3550 qh->qh_next = here;
3551 if (here.qh)
3552 qh->hw->hw_next = *hw_p;
3553 wmb();
3554 prev->qh = qh;
3555 *hw_p = QH_NEXT(fotg210, qh->qh_dma);
3556 }
3557 }
3558 qh->qh_state = QH_STATE_LINKED;
3559 qh->xacterrs = 0;
3560
3561 /* update per-qh bandwidth for usbfs */
3562 fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
3563 ? ((qh->usecs + qh->c_usecs) / qh->period)
3564 : (qh->usecs * 8);
3565
3566 list_add(&qh->intr_node, &fotg210->intr_qh_list);
3567
3568 /* maybe enable periodic schedule processing */
3569 ++fotg210->intr_count;
3570 enable_periodic(fotg210);
3571}
3572
3573static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003574 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003575{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003576 unsigned i;
3577 unsigned period;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003578
3579 /*
3580 * If qh is for a low/full-speed device, simply unlinking it
3581 * could interfere with an ongoing split transaction. To unlink
3582 * it safely would require setting the QH_INACTIVATE bit and
3583 * waiting at least one frame, as described in EHCI 4.12.2.5.
3584 *
3585 * We won't bother with any of this. Instead, we assume that the
3586 * only reason for unlinking an interrupt QH while the current URB
3587 * is still active is to dequeue all the URBs (flush the whole
3588 * endpoint queue).
3589 *
3590 * If rebalancing the periodic schedule is ever implemented, this
3591 * approach will no longer be valid.
3592 */
3593
3594 /* high bandwidth, or otherwise part of every microframe */
3595 period = qh->period;
3596 if (!period)
3597 period = 1;
3598
3599 for (i = qh->start; i < fotg210->periodic_size; i += period)
3600 periodic_unlink(fotg210, i, qh);
3601
3602 /* update per-qh bandwidth for usbfs */
3603 fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
3604 ? ((qh->usecs + qh->c_usecs) / qh->period)
3605 : (qh->usecs * 8);
3606
3607 dev_dbg(&qh->dev->dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003608 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
3609 qh->period, hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
3610 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
3611 qh->c_usecs);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003612
3613 /* qh->qh_next still "live" to HC */
3614 qh->qh_state = QH_STATE_UNLINK;
3615 qh->qh_next.ptr = NULL;
3616
3617 if (fotg210->qh_scan_next == qh)
3618 fotg210->qh_scan_next = list_entry(qh->intr_node.next,
3619 struct fotg210_qh, intr_node);
3620 list_del(&qh->intr_node);
3621}
3622
3623static void start_unlink_intr(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003624 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003625{
3626 /* If the QH isn't linked then there's nothing we can do
3627 * unless we were called during a giveback, in which case
3628 * qh_completions() has to deal with it.
3629 */
3630 if (qh->qh_state != QH_STATE_LINKED) {
3631 if (qh->qh_state == QH_STATE_COMPLETING)
3632 qh->needs_rescan = 1;
3633 return;
3634 }
3635
3636 qh_unlink_periodic(fotg210, qh);
3637
3638 /* Make sure the unlinks are visible before starting the timer */
3639 wmb();
3640
3641 /*
3642 * The EHCI spec doesn't say how long it takes the controller to
3643 * stop accessing an unlinked interrupt QH. The timer delay is
3644 * 9 uframes; presumably that will be long enough.
3645 */
3646 qh->unlink_cycle = fotg210->intr_unlink_cycle;
3647
3648 /* New entries go at the end of the intr_unlink list */
3649 if (fotg210->intr_unlink)
3650 fotg210->intr_unlink_last->unlink_next = qh;
3651 else
3652 fotg210->intr_unlink = qh;
3653 fotg210->intr_unlink_last = qh;
3654
3655 if (fotg210->intr_unlinking)
3656 ; /* Avoid recursive calls */
3657 else if (fotg210->rh_state < FOTG210_RH_RUNNING)
3658 fotg210_handle_intr_unlinks(fotg210);
3659 else if (fotg210->intr_unlink == qh) {
3660 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003661 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003662 ++fotg210->intr_unlink_cycle;
3663 }
3664}
3665
3666static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3667{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003668 struct fotg210_qh_hw *hw = qh->hw;
3669 int rc;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003670
3671 qh->qh_state = QH_STATE_IDLE;
3672 hw->hw_next = FOTG210_LIST_END(fotg210);
3673
3674 qh_completions(fotg210, qh);
3675
3676 /* reschedule QH iff another request is queued */
3677 if (!list_empty(&qh->qtd_list) &&
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003678 fotg210->rh_state == FOTG210_RH_RUNNING) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003679 rc = qh_schedule(fotg210, qh);
3680
3681 /* An error here likely indicates handshake failure
3682 * or no space left in the schedule. Neither fault
3683 * should happen often ...
3684 *
3685 * FIXME kill the now-dysfunctional queued urbs
3686 */
3687 if (rc != 0)
3688 fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
3689 qh, rc);
3690 }
3691
3692 /* maybe turn off periodic schedule */
3693 --fotg210->intr_count;
3694 disable_periodic(fotg210);
3695}
3696
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003697static int check_period(struct fotg210_hcd *fotg210, unsigned frame,
3698 unsigned uframe, unsigned period, unsigned usecs)
3699{
3700 int claimed;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003701
3702 /* complete split running into next frame?
3703 * given FSTN support, we could sometimes check...
3704 */
3705 if (uframe >= 8)
3706 return 0;
3707
3708 /* convert "usecs we need" to "max already claimed" */
3709 usecs = fotg210->uframe_periodic_max - usecs;
3710
3711 /* we "know" 2 and 4 uframe intervals were rejected; so
3712 * for period 0, check _every_ microframe in the schedule.
3713 */
3714 if (unlikely(period == 0)) {
3715 do {
3716 for (uframe = 0; uframe < 7; uframe++) {
3717 claimed = periodic_usecs(fotg210, frame,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003718 uframe);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003719 if (claimed > usecs)
3720 return 0;
3721 }
3722 } while ((frame += 1) < fotg210->periodic_size);
3723
3724 /* just check the specified uframe, at that period */
3725 } else {
3726 do {
3727 claimed = periodic_usecs(fotg210, frame, uframe);
3728 if (claimed > usecs)
3729 return 0;
3730 } while ((frame += period) < fotg210->periodic_size);
3731 }
3732
3733 /* success! */
3734 return 1;
3735}
3736
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003737static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame,
3738 unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003739{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003740 int retval = -ENOSPC;
3741 u8 mask = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003742
3743 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
3744 goto done;
3745
3746 if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
3747 goto done;
3748 if (!qh->c_usecs) {
3749 retval = 0;
3750 *c_maskp = 0;
3751 goto done;
3752 }
3753
3754 /* Make sure this tt's buffer is also available for CSPLITs.
3755 * We pessimize a bit; probably the typical full speed case
3756 * doesn't need the second CSPLIT.
3757 *
3758 * NOTE: both SPLIT and CSPLIT could be checked in just
3759 * one smart pass...
3760 */
3761 mask = 0x03 << (uframe + qh->gap_uf);
3762 *c_maskp = cpu_to_hc32(fotg210, mask << 8);
3763
3764 mask |= 1 << uframe;
3765 if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) {
3766 if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003767 qh->period, qh->c_usecs))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003768 goto done;
3769 if (!check_period(fotg210, frame, uframe + qh->gap_uf,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003770 qh->period, qh->c_usecs))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003771 goto done;
3772 retval = 0;
3773 }
3774done:
3775 return retval;
3776}
3777
3778/* "first fit" scheduling policy used the first time through,
3779 * or when the previous schedule slot can't be re-used.
3780 */
3781static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3782{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003783 int status;
3784 unsigned uframe;
3785 __hc32 c_mask;
3786 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
3787 struct fotg210_qh_hw *hw = qh->hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003788
3789 qh_refresh(fotg210, qh);
3790 hw->hw_next = FOTG210_LIST_END(fotg210);
3791 frame = qh->start;
3792
3793 /* reuse the previous schedule slots, if we can */
3794 if (frame < qh->period) {
3795 uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK);
3796 status = check_intr_schedule(fotg210, frame, --uframe,
3797 qh, &c_mask);
3798 } else {
3799 uframe = 0;
3800 c_mask = 0;
3801 status = -ENOSPC;
3802 }
3803
3804 /* else scan the schedule to find a group of slots such that all
3805 * uframes have enough periodic bandwidth available.
3806 */
3807 if (status) {
3808 /* "normal" case, uframing flexible except with splits */
3809 if (qh->period) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003810 int i;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003811
3812 for (i = qh->period; status && i > 0; --i) {
3813 frame = ++fotg210->random_frame % qh->period;
3814 for (uframe = 0; uframe < 8; uframe++) {
3815 status = check_intr_schedule(fotg210,
3816 frame, uframe, qh,
3817 &c_mask);
3818 if (status == 0)
3819 break;
3820 }
3821 }
3822
3823 /* qh->period == 0 means every uframe */
3824 } else {
3825 frame = 0;
3826 status = check_intr_schedule(fotg210, 0, 0, qh,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003827 &c_mask);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003828 }
3829 if (status)
3830 goto done;
3831 qh->start = frame;
3832
3833 /* reset S-frame and (maybe) C-frame masks */
3834 hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK));
3835 hw->hw_info2 |= qh->period
3836 ? cpu_to_hc32(fotg210, 1 << uframe)
3837 : cpu_to_hc32(fotg210, QH_SMASK);
3838 hw->hw_info2 |= c_mask;
3839 } else
3840 fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
3841
3842 /* stuff into the periodic schedule */
3843 qh_link_periodic(fotg210, qh);
3844done:
3845 return status;
3846}
3847
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003848static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb,
3849 struct list_head *qtd_list, gfp_t mem_flags)
3850{
3851 unsigned epnum;
3852 unsigned long flags;
3853 struct fotg210_qh *qh;
3854 int status;
3855 struct list_head empty;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003856
3857 /* get endpoint and transfer/schedule data */
3858 epnum = urb->ep->desc.bEndpointAddress;
3859
3860 spin_lock_irqsave(&fotg210->lock, flags);
3861
3862 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3863 status = -ESHUTDOWN;
3864 goto done_not_linked;
3865 }
3866 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
3867 if (unlikely(status))
3868 goto done_not_linked;
3869
3870 /* get qh and force any scheduling errors */
3871 INIT_LIST_HEAD(&empty);
3872 qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
3873 if (qh == NULL) {
3874 status = -ENOMEM;
3875 goto done;
3876 }
3877 if (qh->qh_state == QH_STATE_IDLE) {
3878 status = qh_schedule(fotg210, qh);
3879 if (status)
3880 goto done;
3881 }
3882
3883 /* then queue the urb's tds to the qh */
3884 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
3885 BUG_ON(qh == NULL);
3886
3887 /* ... update usbfs periodic stats */
3888 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
3889
3890done:
3891 if (unlikely(status))
3892 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
3893done_not_linked:
3894 spin_unlock_irqrestore(&fotg210->lock, flags);
3895 if (status)
3896 qtd_list_free(fotg210, urb, qtd_list);
3897
3898 return status;
3899}
3900
3901static void scan_intr(struct fotg210_hcd *fotg210)
3902{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003903 struct fotg210_qh *qh;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003904
3905 list_for_each_entry_safe(qh, fotg210->qh_scan_next,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003906 &fotg210->intr_qh_list, intr_node) {
3907rescan:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003908 /* clean any finished work for this qh */
3909 if (!list_empty(&qh->qtd_list)) {
3910 int temp;
3911
3912 /*
3913 * Unlinks could happen here; completion reporting
3914 * drops the lock. That's why fotg210->qh_scan_next
3915 * always holds the next qh to scan; if the next qh
3916 * gets unlinked then fotg210->qh_scan_next is adjusted
3917 * in qh_unlink_periodic().
3918 */
3919 temp = qh_completions(fotg210, qh);
3920 if (unlikely(qh->needs_rescan ||
3921 (list_empty(&qh->qtd_list) &&
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003922 qh->qh_state == QH_STATE_LINKED)))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003923 start_unlink_intr(fotg210, qh);
3924 else if (temp != 0)
3925 goto rescan;
3926 }
3927 }
3928}
3929
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003930/* fotg210_iso_stream ops work with both ITD and SITD */
3931
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003932static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003933{
3934 struct fotg210_iso_stream *stream;
3935
3936 stream = kzalloc(sizeof(*stream), mem_flags);
3937 if (likely(stream != NULL)) {
3938 INIT_LIST_HEAD(&stream->td_list);
3939 INIT_LIST_HEAD(&stream->free_list);
3940 stream->next_uframe = -1;
3941 }
3942 return stream;
3943}
3944
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003945static void iso_stream_init(struct fotg210_hcd *fotg210,
3946 struct fotg210_iso_stream *stream, struct usb_device *dev,
3947 int pipe, unsigned interval)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003948{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003949 u32 buf1;
3950 unsigned epnum, maxp;
3951 int is_input;
3952 long bandwidth;
3953 unsigned multi;
Kelly Devillivff5609a2021-06-27 20:57:46 +08003954 struct usb_host_endpoint *ep;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003955
3956 /*
3957 * this might be a "high bandwidth" highspeed endpoint,
3958 * as encoded in the ep descriptor's wMaxPacket field
3959 */
3960 epnum = usb_pipeendpoint(pipe);
3961 is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
Kelly Devillivff5609a2021-06-27 20:57:46 +08003962 ep = usb_pipe_endpoint(dev, pipe);
3963 maxp = usb_endpoint_maxp(&ep->desc);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003964 if (is_input)
3965 buf1 = (1 << 11);
3966 else
3967 buf1 = 0;
3968
Kelly Devillivff5609a2021-06-27 20:57:46 +08003969 multi = usb_endpoint_maxp_mult(&ep->desc);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003970 buf1 |= maxp;
3971 maxp *= multi;
3972
3973 stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum);
3974 stream->buf1 = cpu_to_hc32(fotg210, buf1);
3975 stream->buf2 = cpu_to_hc32(fotg210, multi);
3976
3977 /* usbfs wants to report the average usecs per frame tied up
3978 * when transfers on this endpoint are scheduled ...
3979 */
3980 if (dev->speed == USB_SPEED_FULL) {
3981 interval <<= 3;
3982 stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
3983 is_input, 1, maxp));
3984 stream->usecs /= 8;
3985 } else {
3986 stream->highspeed = 1;
3987 stream->usecs = HS_USECS_ISO(maxp);
3988 }
3989 bandwidth = stream->usecs * 8;
3990 bandwidth /= interval;
3991
3992 stream->bandwidth = bandwidth;
3993 stream->udev = dev;
3994 stream->bEndpointAddress = is_input | epnum;
3995 stream->interval = interval;
3996 stream->maxp = maxp;
3997}
3998
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003999static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210,
4000 struct urb *urb)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004001{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004002 unsigned epnum;
4003 struct fotg210_iso_stream *stream;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004004 struct usb_host_endpoint *ep;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004005 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004006
4007 epnum = usb_pipeendpoint(urb->pipe);
4008 if (usb_pipein(urb->pipe))
4009 ep = urb->dev->ep_in[epnum];
4010 else
4011 ep = urb->dev->ep_out[epnum];
4012
4013 spin_lock_irqsave(&fotg210->lock, flags);
4014 stream = ep->hcpriv;
4015
4016 if (unlikely(stream == NULL)) {
4017 stream = iso_stream_alloc(GFP_ATOMIC);
4018 if (likely(stream != NULL)) {
4019 ep->hcpriv = stream;
4020 stream->ep = ep;
4021 iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
4022 urb->interval);
4023 }
4024
4025 /* if dev->ep[epnum] is a QH, hw is set */
4026 } else if (unlikely(stream->hw != NULL)) {
4027 fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004028 urb->dev->devpath, epnum,
4029 usb_pipein(urb->pipe) ? "in" : "out");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004030 stream = NULL;
4031 }
4032
4033 spin_unlock_irqrestore(&fotg210->lock, flags);
4034 return stream;
4035}
4036
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004037/* fotg210_iso_sched ops can be ITD-only or SITD-only */
4038
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004039static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
4040 gfp_t mem_flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004041{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004042 struct fotg210_iso_sched *iso_sched;
4043 int size = sizeof(*iso_sched);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004044
4045 size += packets * sizeof(struct fotg210_iso_packet);
4046 iso_sched = kzalloc(size, mem_flags);
4047 if (likely(iso_sched != NULL))
4048 INIT_LIST_HEAD(&iso_sched->td_list);
4049
4050 return iso_sched;
4051}
4052
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004053static inline void itd_sched_init(struct fotg210_hcd *fotg210,
4054 struct fotg210_iso_sched *iso_sched,
4055 struct fotg210_iso_stream *stream, struct urb *urb)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004056{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004057 unsigned i;
4058 dma_addr_t dma = urb->transfer_dma;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004059
4060 /* how many uframes are needed for these transfers */
4061 iso_sched->span = urb->number_of_packets * stream->interval;
4062
4063 /* figure out per-uframe itd fields that we'll need later
4064 * when we fit new itds into the schedule.
4065 */
4066 for (i = 0; i < urb->number_of_packets; i++) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004067 struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
4068 unsigned length;
4069 dma_addr_t buf;
4070 u32 trans;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004071
4072 length = urb->iso_frame_desc[i].length;
4073 buf = dma + urb->iso_frame_desc[i].offset;
4074
4075 trans = FOTG210_ISOC_ACTIVE;
4076 trans |= buf & 0x0fff;
4077 if (unlikely(((i + 1) == urb->number_of_packets))
4078 && !(urb->transfer_flags & URB_NO_INTERRUPT))
4079 trans |= FOTG210_ITD_IOC;
4080 trans |= length << 16;
4081 uframe->transaction = cpu_to_hc32(fotg210, trans);
4082
4083 /* might need to cross a buffer page within a uframe */
4084 uframe->bufp = (buf & ~(u64)0x0fff);
4085 buf += length;
4086 if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
4087 uframe->cross = 1;
4088 }
4089}
4090
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004091static void iso_sched_free(struct fotg210_iso_stream *stream,
4092 struct fotg210_iso_sched *iso_sched)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004093{
4094 if (!iso_sched)
4095 return;
4096 /* caller must hold fotg210->lock!*/
4097 list_splice(&iso_sched->td_list, &stream->free_list);
4098 kfree(iso_sched);
4099}
4100
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004101static int itd_urb_transaction(struct fotg210_iso_stream *stream,
4102 struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004103{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004104 struct fotg210_itd *itd;
4105 dma_addr_t itd_dma;
4106 int i;
4107 unsigned num_itds;
4108 struct fotg210_iso_sched *sched;
4109 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004110
4111 sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
4112 if (unlikely(sched == NULL))
4113 return -ENOMEM;
4114
4115 itd_sched_init(fotg210, sched, stream, urb);
4116
4117 if (urb->interval < 8)
4118 num_itds = 1 + (sched->span + 7) / 8;
4119 else
4120 num_itds = urb->number_of_packets;
4121
4122 /* allocate/init ITDs */
4123 spin_lock_irqsave(&fotg210->lock, flags);
4124 for (i = 0; i < num_itds; i++) {
4125
4126 /*
4127 * Use iTDs from the free list, but not iTDs that may
4128 * still be in use by the hardware.
4129 */
4130 if (likely(!list_empty(&stream->free_list))) {
4131 itd = list_first_entry(&stream->free_list,
4132 struct fotg210_itd, itd_list);
4133 if (itd->frame == fotg210->now_frame)
4134 goto alloc_itd;
4135 list_del(&itd->itd_list);
4136 itd_dma = itd->itd_dma;
4137 } else {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004138alloc_itd:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004139 spin_unlock_irqrestore(&fotg210->lock, flags);
4140 itd = dma_pool_alloc(fotg210->itd_pool, mem_flags,
4141 &itd_dma);
4142 spin_lock_irqsave(&fotg210->lock, flags);
4143 if (!itd) {
4144 iso_sched_free(stream, sched);
4145 spin_unlock_irqrestore(&fotg210->lock, flags);
4146 return -ENOMEM;
4147 }
4148 }
4149
4150 memset(itd, 0, sizeof(*itd));
4151 itd->itd_dma = itd_dma;
4152 list_add(&itd->itd_list, &sched->td_list);
4153 }
4154 spin_unlock_irqrestore(&fotg210->lock, flags);
4155
4156 /* temporarily store schedule info in hcpriv */
4157 urb->hcpriv = sched;
4158 urb->error_count = 0;
4159 return 0;
4160}
4161
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004162static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe,
4163 u8 usecs, u32 period)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004164{
4165 uframe %= period;
4166 do {
4167 /* can't commit more than uframe_periodic_max usec */
4168 if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
4169 > (fotg210->uframe_periodic_max - usecs))
4170 return 0;
4171
4172 /* we know urb->interval is 2^N uframes */
4173 uframe += period;
4174 } while (uframe < mod);
4175 return 1;
4176}
4177
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004178/* This scheduler plans almost as far into the future as it has actual
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004179 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
4180 * "as small as possible" to be cache-friendlier.) That limits the size
4181 * transfers you can stream reliably; avoid more than 64 msec per urb.
4182 * Also avoid queue depths of less than fotg210's worst irq latency (affected
4183 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
4184 * and other factors); or more than about 230 msec total (for portability,
4185 * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
4186 */
4187
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004188#define SCHEDULE_SLOP 80 /* microframes */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004189
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004190static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb,
4191 struct fotg210_iso_stream *stream)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004192{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004193 u32 now, next, start, period, span;
4194 int status;
4195 unsigned mod = fotg210->periodic_size << 3;
4196 struct fotg210_iso_sched *sched = urb->hcpriv;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004197
4198 period = urb->interval;
4199 span = sched->span;
4200
4201 if (span > mod - SCHEDULE_SLOP) {
4202 fotg210_dbg(fotg210, "iso request %p too long\n", urb);
4203 status = -EFBIG;
4204 goto fail;
4205 }
4206
4207 now = fotg210_read_frame_index(fotg210) & (mod - 1);
4208
4209 /* Typical case: reuse current schedule, stream is still active.
4210 * Hopefully there are no gaps from the host falling behind
4211 * (irq delays etc), but if there are we'll take the next
4212 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
4213 */
4214 if (likely(!list_empty(&stream->td_list))) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004215 u32 excess;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004216
4217 /* For high speed devices, allow scheduling within the
4218 * isochronous scheduling threshold. For full speed devices
4219 * and Intel PCI-based controllers, don't (work around for
4220 * Intel ICH9 bug).
4221 */
4222 if (!stream->highspeed && fotg210->fs_i_thresh)
4223 next = now + fotg210->i_thresh;
4224 else
4225 next = now;
4226
4227 /* Fell behind (by up to twice the slop amount)?
4228 * We decide based on the time of the last currently-scheduled
4229 * slot, not the time of the next available slot.
4230 */
4231 excess = (stream->next_uframe - period - next) & (mod - 1);
4232 if (excess >= mod - 2 * SCHEDULE_SLOP)
4233 start = next + excess - mod + period *
4234 DIV_ROUND_UP(mod - excess, period);
4235 else
4236 start = next + excess + period;
4237 if (start - now >= mod) {
4238 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
4239 urb, start - now - period, period,
4240 mod);
4241 status = -EFBIG;
4242 goto fail;
4243 }
4244 }
4245
4246 /* need to schedule; when's the next (u)frame we could start?
4247 * this is bigger than fotg210->i_thresh allows; scheduling itself
4248 * isn't free, the slop should handle reasonably slow cpus. it
4249 * can also help high bandwidth if the dma and irq loads don't
4250 * jump until after the queue is primed.
4251 */
4252 else {
4253 int done = 0;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004254
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004255 start = SCHEDULE_SLOP + (now & ~0x07);
4256
4257 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
4258
4259 /* find a uframe slot with enough bandwidth.
4260 * Early uframes are more precious because full-speed
4261 * iso IN transfers can't use late uframes,
4262 * and therefore they should be allocated last.
4263 */
4264 next = start;
4265 start += period;
4266 do {
4267 start--;
4268 /* check schedule: enough space? */
4269 if (itd_slot_ok(fotg210, mod, start,
4270 stream->usecs, period))
4271 done = 1;
4272 } while (start > next && !done);
4273
4274 /* no room in the schedule */
4275 if (!done) {
4276 fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004277 urb, now, now + mod);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004278 status = -ENOSPC;
4279 goto fail;
4280 }
4281 }
4282
4283 /* Tried to schedule too far into the future? */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004284 if (unlikely(start - now + span - period >=
4285 mod - 2 * SCHEDULE_SLOP)) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004286 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
4287 urb, start - now, span - period,
4288 mod - 2 * SCHEDULE_SLOP);
4289 status = -EFBIG;
4290 goto fail;
4291 }
4292
4293 stream->next_uframe = start & (mod - 1);
4294
4295 /* report high speed start in uframes; full speed, in frames */
4296 urb->start_frame = stream->next_uframe;
4297 if (!stream->highspeed)
4298 urb->start_frame >>= 3;
4299
4300 /* Make sure scan_isoc() sees these */
4301 if (fotg210->isoc_count == 0)
4302 fotg210->next_frame = now >> 3;
4303 return 0;
4304
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004305fail:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004306 iso_sched_free(stream, sched);
4307 urb->hcpriv = NULL;
4308 return status;
4309}
4310
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004311static inline void itd_init(struct fotg210_hcd *fotg210,
4312 struct fotg210_iso_stream *stream, struct fotg210_itd *itd)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004313{
4314 int i;
4315
4316 /* it's been recently zeroed */
4317 itd->hw_next = FOTG210_LIST_END(fotg210);
4318 itd->hw_bufp[0] = stream->buf0;
4319 itd->hw_bufp[1] = stream->buf1;
4320 itd->hw_bufp[2] = stream->buf2;
4321
4322 for (i = 0; i < 8; i++)
4323 itd->index[i] = -1;
4324
4325 /* All other fields are filled when scheduling */
4326}
4327
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004328static inline void itd_patch(struct fotg210_hcd *fotg210,
4329 struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched,
4330 unsigned index, u16 uframe)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004331{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004332 struct fotg210_iso_packet *uf = &iso_sched->packet[index];
4333 unsigned pg = itd->pg;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004334
4335 uframe &= 0x07;
4336 itd->index[uframe] = index;
4337
4338 itd->hw_transaction[uframe] = uf->transaction;
4339 itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12);
4340 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0);
4341 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32));
4342
4343 /* iso_frame_desc[].offset must be strictly increasing */
4344 if (unlikely(uf->cross)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004345 u64 bufp = uf->bufp + 4096;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004346
4347 itd->pg = ++pg;
4348 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0);
4349 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32));
4350 }
4351}
4352
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004353static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame,
4354 struct fotg210_itd *itd)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004355{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004356 union fotg210_shadow *prev = &fotg210->pshadow[frame];
4357 __hc32 *hw_p = &fotg210->periodic[frame];
4358 union fotg210_shadow here = *prev;
4359 __hc32 type = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004360
4361 /* skip any iso nodes which might belong to previous microframes */
4362 while (here.ptr) {
4363 type = Q_NEXT_TYPE(fotg210, *hw_p);
4364 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
4365 break;
4366 prev = periodic_next_shadow(fotg210, prev, type);
4367 hw_p = shadow_next_periodic(fotg210, &here, type);
4368 here = *prev;
4369 }
4370
4371 itd->itd_next = here;
4372 itd->hw_next = *hw_p;
4373 prev->itd = itd;
4374 itd->frame = frame;
4375 wmb();
4376 *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
4377}
4378
4379/* fit urb's itds into the selected schedule slot; activate as needed */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004380static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb,
4381 unsigned mod, struct fotg210_iso_stream *stream)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004382{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004383 int packet;
4384 unsigned next_uframe, uframe, frame;
4385 struct fotg210_iso_sched *iso_sched = urb->hcpriv;
4386 struct fotg210_itd *itd;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004387
4388 next_uframe = stream->next_uframe & (mod - 1);
4389
4390 if (unlikely(list_empty(&stream->td_list))) {
4391 fotg210_to_hcd(fotg210)->self.bandwidth_allocated
4392 += stream->bandwidth;
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01004393 fotg210_dbg(fotg210,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004394 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
4395 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
4396 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
4397 urb->interval,
4398 next_uframe >> 3, next_uframe & 0x7);
4399 }
4400
4401 /* fill iTDs uframe by uframe */
4402 for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
4403 if (itd == NULL) {
4404 /* ASSERT: we have all necessary itds */
4405
4406 /* ASSERT: no itds for this endpoint in this uframe */
4407
4408 itd = list_entry(iso_sched->td_list.next,
4409 struct fotg210_itd, itd_list);
4410 list_move_tail(&itd->itd_list, &stream->td_list);
4411 itd->stream = stream;
4412 itd->urb = urb;
4413 itd_init(fotg210, stream, itd);
4414 }
4415
4416 uframe = next_uframe & 0x07;
4417 frame = next_uframe >> 3;
4418
4419 itd_patch(fotg210, itd, iso_sched, packet, uframe);
4420
4421 next_uframe += stream->interval;
4422 next_uframe &= mod - 1;
4423 packet++;
4424
4425 /* link completed itds into the schedule */
4426 if (((next_uframe >> 3) != frame)
4427 || packet == urb->number_of_packets) {
4428 itd_link(fotg210, frame & (fotg210->periodic_size - 1),
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004429 itd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004430 itd = NULL;
4431 }
4432 }
4433 stream->next_uframe = next_uframe;
4434
4435 /* don't need that schedule data any more */
4436 iso_sched_free(stream, iso_sched);
4437 urb->hcpriv = NULL;
4438
4439 ++fotg210->isoc_count;
4440 enable_periodic(fotg210);
4441}
4442
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004443#define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
4444 FOTG210_ISOC_XACTERR)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004445
4446/* Process and recycle a completed ITD. Return true iff its urb completed,
4447 * and hence its completion callback probably added things to the hardware
4448 * schedule.
4449 *
4450 * Note that we carefully avoid recycling this descriptor until after any
4451 * completion callback runs, so that it won't be reused quickly. That is,
4452 * assuming (a) no more than two urbs per frame on this endpoint, and also
4453 * (b) only this endpoint's completions submit URBs. It seems some silicon
4454 * corrupts things if you reuse completed descriptors very quickly...
4455 */
4456static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
4457{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004458 struct urb *urb = itd->urb;
4459 struct usb_iso_packet_descriptor *desc;
4460 u32 t;
4461 unsigned uframe;
4462 int urb_index = -1;
4463 struct fotg210_iso_stream *stream = itd->stream;
4464 struct usb_device *dev;
4465 bool retval = false;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004466
4467 /* for each uframe with a packet */
4468 for (uframe = 0; uframe < 8; uframe++) {
4469 if (likely(itd->index[uframe] == -1))
4470 continue;
4471 urb_index = itd->index[uframe];
4472 desc = &urb->iso_frame_desc[urb_index];
4473
4474 t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
4475 itd->hw_transaction[uframe] = 0;
4476
4477 /* report transfer status */
4478 if (unlikely(t & ISO_ERRS)) {
4479 urb->error_count++;
4480 if (t & FOTG210_ISOC_BUF_ERR)
4481 desc->status = usb_pipein(urb->pipe)
4482 ? -ENOSR /* hc couldn't read */
4483 : -ECOMM; /* hc couldn't write */
4484 else if (t & FOTG210_ISOC_BABBLE)
4485 desc->status = -EOVERFLOW;
4486 else /* (t & FOTG210_ISOC_XACTERR) */
4487 desc->status = -EPROTO;
4488
4489 /* HC need not update length with this error */
4490 if (!(t & FOTG210_ISOC_BABBLE)) {
Kelly Devilliv20351ef2021-06-27 20:57:47 +08004491 desc->actual_length = FOTG210_ITD_LENGTH(t);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004492 urb->actual_length += desc->actual_length;
4493 }
4494 } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
4495 desc->status = 0;
Kelly Devilliv20351ef2021-06-27 20:57:47 +08004496 desc->actual_length = FOTG210_ITD_LENGTH(t);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004497 urb->actual_length += desc->actual_length;
4498 } else {
4499 /* URB was too late */
4500 desc->status = -EXDEV;
4501 }
4502 }
4503
4504 /* handle completion now? */
4505 if (likely((urb_index + 1) != urb->number_of_packets))
4506 goto done;
4507
4508 /* ASSERT: it's really the last itd for this urb
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004509 * list_for_each_entry (itd, &stream->td_list, itd_list)
4510 * BUG_ON (itd->urb == urb);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004511 */
4512
4513 /* give urb back to the driver; completion often (re)submits */
4514 dev = urb->dev;
4515 fotg210_urb_done(fotg210, urb, 0);
4516 retval = true;
4517 urb = NULL;
4518
4519 --fotg210->isoc_count;
4520 disable_periodic(fotg210);
4521
4522 if (unlikely(list_is_singular(&stream->td_list))) {
4523 fotg210_to_hcd(fotg210)->self.bandwidth_allocated
4524 -= stream->bandwidth;
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01004525 fotg210_dbg(fotg210,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004526 "deschedule devp %s ep%d%s-iso\n",
4527 dev->devpath, stream->bEndpointAddress & 0x0f,
4528 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
4529 }
4530
4531done:
4532 itd->urb = NULL;
4533
4534 /* Add to the end of the free list for later reuse */
4535 list_move_tail(&itd->itd_list, &stream->free_list);
4536
4537 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
4538 if (list_empty(&stream->td_list)) {
4539 list_splice_tail_init(&stream->free_list,
4540 &fotg210->cached_itd_list);
4541 start_free_itds(fotg210);
4542 }
4543
4544 return retval;
4545}
4546
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004547static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004548 gfp_t mem_flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004549{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004550 int status = -EINVAL;
4551 unsigned long flags;
4552 struct fotg210_iso_stream *stream;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004553
4554 /* Get iso_stream head */
4555 stream = iso_stream_find(fotg210, urb);
4556 if (unlikely(stream == NULL)) {
4557 fotg210_dbg(fotg210, "can't get iso stream\n");
4558 return -ENOMEM;
4559 }
4560 if (unlikely(urb->interval != stream->interval &&
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004561 fotg210_port_speed(fotg210, 0) ==
4562 USB_PORT_STAT_HIGH_SPEED)) {
4563 fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n",
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004564 stream->interval, urb->interval);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004565 goto done;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004566 }
4567
4568#ifdef FOTG210_URB_TRACE
4569 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004570 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
4571 __func__, urb->dev->devpath, urb,
4572 usb_pipeendpoint(urb->pipe),
4573 usb_pipein(urb->pipe) ? "in" : "out",
4574 urb->transfer_buffer_length,
4575 urb->number_of_packets, urb->interval,
4576 stream);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004577#endif
4578
4579 /* allocate ITDs w/o locking anything */
4580 status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
4581 if (unlikely(status < 0)) {
4582 fotg210_dbg(fotg210, "can't init itds\n");
4583 goto done;
4584 }
4585
4586 /* schedule ... need to lock */
4587 spin_lock_irqsave(&fotg210->lock, flags);
4588 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
4589 status = -ESHUTDOWN;
4590 goto done_not_linked;
4591 }
4592 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
4593 if (unlikely(status))
4594 goto done_not_linked;
4595 status = iso_stream_schedule(fotg210, urb, stream);
4596 if (likely(status == 0))
4597 itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream);
4598 else
4599 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004600done_not_linked:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004601 spin_unlock_irqrestore(&fotg210->lock, flags);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004602done:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004603 return status;
4604}
4605
Peter Senna Tschudin50bdb122015-10-12 23:22:40 +02004606static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame,
4607 unsigned now_frame, bool live)
4608{
4609 unsigned uf;
4610 bool modified;
4611 union fotg210_shadow q, *q_p;
4612 __hc32 type, *hw_p;
4613
4614 /* scan each element in frame's queue for completions */
4615 q_p = &fotg210->pshadow[frame];
4616 hw_p = &fotg210->periodic[frame];
4617 q.ptr = q_p->ptr;
4618 type = Q_NEXT_TYPE(fotg210, *hw_p);
4619 modified = false;
4620
4621 while (q.ptr) {
4622 switch (hc32_to_cpu(fotg210, type)) {
4623 case Q_TYPE_ITD:
4624 /* If this ITD is still active, leave it for
4625 * later processing ... check the next entry.
4626 * No need to check for activity unless the
4627 * frame is current.
4628 */
4629 if (frame == now_frame && live) {
4630 rmb();
4631 for (uf = 0; uf < 8; uf++) {
4632 if (q.itd->hw_transaction[uf] &
4633 ITD_ACTIVE(fotg210))
4634 break;
4635 }
4636 if (uf < 8) {
4637 q_p = &q.itd->itd_next;
4638 hw_p = &q.itd->hw_next;
4639 type = Q_NEXT_TYPE(fotg210,
4640 q.itd->hw_next);
4641 q = *q_p;
4642 break;
4643 }
4644 }
4645
4646 /* Take finished ITDs out of the schedule
4647 * and process them: recycle, maybe report
4648 * URB completion. HC won't cache the
4649 * pointer for much longer, if at all.
4650 */
4651 *q_p = q.itd->itd_next;
4652 *hw_p = q.itd->hw_next;
4653 type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
4654 wmb();
4655 modified = itd_complete(fotg210, q.itd);
4656 q = *q_p;
4657 break;
4658 default:
4659 fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
4660 type, frame, q.ptr);
4661 /* FALL THROUGH */
4662 case Q_TYPE_QH:
4663 case Q_TYPE_FSTN:
4664 /* End of the iTDs and siTDs */
4665 q.ptr = NULL;
4666 break;
4667 }
4668
4669 /* assume completion callbacks modify the queue */
4670 if (unlikely(modified && fotg210->isoc_count > 0))
4671 return -EINVAL;
4672 }
4673 return 0;
4674}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004675
4676static void scan_isoc(struct fotg210_hcd *fotg210)
4677{
Peter Senna Tschudin50bdb122015-10-12 23:22:40 +02004678 unsigned uf, now_frame, frame, ret;
4679 unsigned fmask = fotg210->periodic_size - 1;
4680 bool live;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004681
4682 /*
4683 * When running, scan from last scan point up to "now"
4684 * else clean up by scanning everything that's left.
4685 * Touches as few pages as possible: cache-friendly.
4686 */
4687 if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
4688 uf = fotg210_read_frame_index(fotg210);
4689 now_frame = (uf >> 3) & fmask;
4690 live = true;
4691 } else {
4692 now_frame = (fotg210->next_frame - 1) & fmask;
4693 live = false;
4694 }
4695 fotg210->now_frame = now_frame;
4696
4697 frame = fotg210->next_frame;
4698 for (;;) {
Peter Senna Tschudin50bdb122015-10-12 23:22:40 +02004699 ret = 1;
4700 while (ret != 0)
4701 ret = scan_frame_queue(fotg210, frame,
4702 now_frame, live);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004703
4704 /* Stop when we have reached the current frame */
4705 if (frame == now_frame)
4706 break;
4707 frame = (frame + 1) & fmask;
4708 }
4709 fotg210->next_frame = now_frame;
4710}
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004711
4712/* Display / Set uframe_periodic_max
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004713 */
4714static ssize_t show_uframe_periodic_max(struct device *dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004715 struct device_attribute *attr, char *buf)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004716{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004717 struct fotg210_hcd *fotg210;
4718 int n;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004719
4720 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4721 n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
4722 return n;
4723}
4724
4725
4726static ssize_t store_uframe_periodic_max(struct device *dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004727 struct device_attribute *attr, const char *buf, size_t count)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004728{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004729 struct fotg210_hcd *fotg210;
4730 unsigned uframe_periodic_max;
4731 unsigned frame, uframe;
4732 unsigned short allocated_max;
4733 unsigned long flags;
4734 ssize_t ret;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004735
4736 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4737 if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
4738 return -EINVAL;
4739
4740 if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
4741 fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004742 uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004743 return -EINVAL;
4744 }
4745
4746 ret = -EINVAL;
4747
4748 /*
4749 * lock, so that our checking does not race with possible periodic
4750 * bandwidth allocation through submitting new urbs.
4751 */
4752 spin_lock_irqsave(&fotg210->lock, flags);
4753
4754 /*
4755 * for request to decrease max periodic bandwidth, we have to check
4756 * every microframe in the schedule to see whether the decrease is
4757 * possible.
4758 */
4759 if (uframe_periodic_max < fotg210->uframe_periodic_max) {
4760 allocated_max = 0;
4761
4762 for (frame = 0; frame < fotg210->periodic_size; ++frame)
4763 for (uframe = 0; uframe < 7; ++uframe)
4764 allocated_max = max(allocated_max,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004765 periodic_usecs(fotg210, frame,
4766 uframe));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004767
4768 if (allocated_max > uframe_periodic_max) {
4769 fotg210_info(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004770 "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n",
4771 allocated_max, uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004772 goto out_unlock;
4773 }
4774 }
4775
4776 /* increasing is always ok */
4777
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004778 fotg210_info(fotg210,
4779 "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
4780 100 * uframe_periodic_max/125, uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004781
4782 if (uframe_periodic_max != 100)
4783 fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
4784
4785 fotg210->uframe_periodic_max = uframe_periodic_max;
4786 ret = count;
4787
4788out_unlock:
4789 spin_unlock_irqrestore(&fotg210->lock, flags);
4790 return ret;
4791}
4792
4793static DEVICE_ATTR(uframe_periodic_max, 0644, show_uframe_periodic_max,
4794 store_uframe_periodic_max);
4795
4796static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
4797{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004798 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004799
Colin Ian King9f3b3f62016-05-08 20:14:29 +01004800 return device_create_file(controller, &dev_attr_uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004801}
4802
4803static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
4804{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004805 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004806
4807 device_remove_file(controller, &dev_attr_uframe_periodic_max);
4808}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004809/* On some systems, leaving remote wakeup enabled prevents system shutdown.
4810 * The firmware seems to think that powering off is a wakeup event!
4811 * This routine turns off remote wakeup and everything else, on all ports.
4812 */
4813static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
4814{
4815 u32 __iomem *status_reg = &fotg210->regs->port_status;
4816
4817 fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
4818}
4819
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004820/* Halt HC, turn off all ports, and let the BIOS use the companion controllers.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004821 * Must be called with interrupts enabled and the lock not held.
4822 */
4823static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
4824{
4825 fotg210_halt(fotg210);
4826
4827 spin_lock_irq(&fotg210->lock);
4828 fotg210->rh_state = FOTG210_RH_HALTED;
4829 fotg210_turn_off_all_ports(fotg210);
4830 spin_unlock_irq(&fotg210->lock);
4831}
4832
4833/* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
4834 * This forcibly disables dma and IRQs, helping kexec and other cases
4835 * where the next system software may expect clean state.
4836 */
4837static void fotg210_shutdown(struct usb_hcd *hcd)
4838{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004839 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004840
4841 spin_lock_irq(&fotg210->lock);
4842 fotg210->shutdown = true;
4843 fotg210->rh_state = FOTG210_RH_STOPPING;
4844 fotg210->enabled_hrtimer_events = 0;
4845 spin_unlock_irq(&fotg210->lock);
4846
4847 fotg210_silence_controller(fotg210);
4848
4849 hrtimer_cancel(&fotg210->hrtimer);
4850}
4851
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004852/* fotg210_work is called from some interrupts, timers, and so on.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004853 * it calls driver completion functions, after dropping fotg210->lock.
4854 */
4855static void fotg210_work(struct fotg210_hcd *fotg210)
4856{
4857 /* another CPU may drop fotg210->lock during a schedule scan while
4858 * it reports urb completions. this flag guards against bogus
4859 * attempts at re-entrant schedule scanning.
4860 */
4861 if (fotg210->scanning) {
4862 fotg210->need_rescan = true;
4863 return;
4864 }
4865 fotg210->scanning = true;
4866
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004867rescan:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004868 fotg210->need_rescan = false;
4869 if (fotg210->async_count)
4870 scan_async(fotg210);
4871 if (fotg210->intr_count > 0)
4872 scan_intr(fotg210);
4873 if (fotg210->isoc_count > 0)
4874 scan_isoc(fotg210);
4875 if (fotg210->need_rescan)
4876 goto rescan;
4877 fotg210->scanning = false;
4878
4879 /* the IO watchdog guards against hardware or driver bugs that
4880 * misplace IRQs, and should let us run completely without IRQs.
4881 * such lossage has been observed on both VT6202 and VT8235.
4882 */
4883 turn_on_io_watchdog(fotg210);
4884}
4885
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004886/* Called when the fotg210_hcd module is removed.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004887 */
4888static void fotg210_stop(struct usb_hcd *hcd)
4889{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004890 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004891
4892 fotg210_dbg(fotg210, "stop\n");
4893
4894 /* no more interrupts ... */
4895
4896 spin_lock_irq(&fotg210->lock);
4897 fotg210->enabled_hrtimer_events = 0;
4898 spin_unlock_irq(&fotg210->lock);
4899
4900 fotg210_quiesce(fotg210);
4901 fotg210_silence_controller(fotg210);
4902 fotg210_reset(fotg210);
4903
4904 hrtimer_cancel(&fotg210->hrtimer);
4905 remove_sysfs_files(fotg210);
4906 remove_debug_files(fotg210);
4907
4908 /* root hub is shut down separately (first, when possible) */
4909 spin_lock_irq(&fotg210->lock);
4910 end_free_itds(fotg210);
4911 spin_unlock_irq(&fotg210->lock);
4912 fotg210_mem_cleanup(fotg210);
4913
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004914#ifdef FOTG210_STATS
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004915 fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004916 fotg210->stats.normal, fotg210->stats.error,
4917 fotg210->stats.iaa, fotg210->stats.lost_iaa);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004918 fotg210_dbg(fotg210, "complete %ld unlink %ld\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004919 fotg210->stats.complete, fotg210->stats.unlink);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004920#endif
4921
4922 dbg_status(fotg210, "fotg210_stop completed",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004923 fotg210_readl(fotg210, &fotg210->regs->status));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004924}
4925
4926/* one-time init, only for memory state */
4927static int hcd_fotg210_init(struct usb_hcd *hcd)
4928{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004929 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
4930 u32 temp;
4931 int retval;
4932 u32 hcc_params;
4933 struct fotg210_qh_hw *hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004934
4935 spin_lock_init(&fotg210->lock);
4936
4937 /*
4938 * keep io watchdog by default, those good HCDs could turn off it later
4939 */
4940 fotg210->need_io_watchdog = 1;
4941
4942 hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
4943 fotg210->hrtimer.function = fotg210_hrtimer_func;
4944 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
4945
4946 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
4947
4948 /*
4949 * by default set standard 80% (== 100 usec/uframe) max periodic
4950 * bandwidth as required by USB 2.0
4951 */
4952 fotg210->uframe_periodic_max = 100;
4953
4954 /*
4955 * hw default: 1K periodic list heads, one per frame.
4956 * periodic_size can shrink by USBCMD update if hcc_params allows.
4957 */
4958 fotg210->periodic_size = DEFAULT_I_TDPS;
4959 INIT_LIST_HEAD(&fotg210->intr_qh_list);
4960 INIT_LIST_HEAD(&fotg210->cached_itd_list);
4961
4962 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
4963 /* periodic schedule size can be smaller than default */
4964 switch (FOTG210_TUNE_FLS) {
4965 case 0:
4966 fotg210->periodic_size = 1024;
4967 break;
4968 case 1:
4969 fotg210->periodic_size = 512;
4970 break;
4971 case 2:
4972 fotg210->periodic_size = 256;
4973 break;
4974 default:
4975 BUG();
4976 }
4977 }
4978 retval = fotg210_mem_init(fotg210, GFP_KERNEL);
4979 if (retval < 0)
4980 return retval;
4981
4982 /* controllers may cache some of the periodic schedule ... */
4983 fotg210->i_thresh = 2;
4984
4985 /*
4986 * dedicate a qh for the async ring head, since we couldn't unlink
4987 * a 'real' qh without stopping the async schedule [4.8]. use it
4988 * as the 'reclamation list head' too.
4989 * its dummy is used in hw_alt_next of many tds, to prevent the qh
4990 * from automatically advancing to the next td after short reads.
4991 */
4992 fotg210->async->qh_next.qh = NULL;
4993 hw = fotg210->async->hw;
4994 hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma);
4995 hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD);
4996 hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
4997 hw->hw_qtd_next = FOTG210_LIST_END(fotg210);
4998 fotg210->async->qh_state = QH_STATE_LINKED;
4999 hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma);
5000
5001 /* clear interrupt enables, set irq latency */
5002 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
5003 log2_irq_thresh = 0;
5004 temp = 1 << (16 + log2_irq_thresh);
5005 if (HCC_CANPARK(hcc_params)) {
5006 /* HW default park == 3, on hardware that supports it (like
5007 * NVidia and ALI silicon), maximizes throughput on the async
5008 * schedule by avoiding QH fetches between transfers.
5009 *
5010 * With fast usb storage devices and NForce2, "park" seems to
5011 * make problems: throughput reduction (!), data errors...
5012 */
5013 if (park) {
5014 park = min_t(unsigned, park, 3);
5015 temp |= CMD_PARK;
5016 temp |= park << 8;
5017 }
5018 fotg210_dbg(fotg210, "park %d\n", park);
5019 }
5020 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
5021 /* periodic schedule size can be smaller than default */
5022 temp &= ~(3 << 2);
5023 temp |= (FOTG210_TUNE_FLS << 2);
5024 }
5025 fotg210->command = temp;
5026
5027 /* Accept arbitrarily long scatter-gather lists */
5028 if (!(hcd->driver->flags & HCD_LOCAL_MEM))
5029 hcd->self.sg_tablesize = ~0;
5030 return 0;
5031}
5032
5033/* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
5034static int fotg210_run(struct usb_hcd *hcd)
5035{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005036 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5037 u32 temp;
5038 u32 hcc_params;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005039
5040 hcd->uses_new_polling = 1;
5041
5042 /* EHCI spec section 4.1 */
5043
5044 fotg210_writel(fotg210, fotg210->periodic_dma,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005045 &fotg210->regs->frame_list);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005046 fotg210_writel(fotg210, (u32)fotg210->async->qh_dma,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005047 &fotg210->regs->async_next);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005048
5049 /*
5050 * hcc_params controls whether fotg210->regs->segment must (!!!)
5051 * be used; it constrains QH/ITD/SITD and QTD locations.
5052 * pci_pool consistent memory always uses segment zero.
5053 * streaming mappings for I/O buffers, like pci_map_single(),
5054 * can return segments above 4GB, if the device allows.
5055 *
Christoph Hellwig0e77ace2015-11-09 14:58:18 -08005056 * NOTE: the dma mask is visible through dev->dma_mask, so
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005057 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
5058 * Scsi_Host.highmem_io, and so forth. It's readonly to all
5059 * host side drivers though.
5060 */
5061 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
5062
5063 /*
5064 * Philips, Intel, and maybe others need CMD_RUN before the
5065 * root hub will detect new devices (why?); NEC doesn't
5066 */
5067 fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
5068 fotg210->command |= CMD_RUN;
5069 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
5070 dbg_cmd(fotg210, "init", fotg210->command);
5071
5072 /*
5073 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5074 * are explicitly handed to companion controller(s), so no TT is
5075 * involved with the root hub. (Except where one is integrated,
5076 * and there's no companion controller unless maybe for USB OTG.)
5077 *
5078 * Turning on the CF flag will transfer ownership of all ports
5079 * from the companions to the EHCI controller. If any of the
5080 * companions are in the middle of a port reset at the time, it
5081 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
5082 * guarantees that no resets are in progress. After we set CF,
5083 * a short delay lets the hardware catch up; new resets shouldn't
5084 * be started before the port switching actions could complete.
5085 */
5086 down_write(&ehci_cf_port_reset_rwsem);
5087 fotg210->rh_state = FOTG210_RH_RUNNING;
5088 /* unblock posted writes */
5089 fotg210_readl(fotg210, &fotg210->regs->command);
Peter Senna Tschudin05ebc362015-10-12 23:22:37 +02005090 usleep_range(5000, 10000);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005091 up_write(&ehci_cf_port_reset_rwsem);
5092 fotg210->last_periodic_enable = ktime_get_real();
5093
5094 temp = HC_VERSION(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005095 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005096 fotg210_info(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005097 "USB %x.%x started, EHCI %x.%02x\n",
5098 ((fotg210->sbrn & 0xf0) >> 4), (fotg210->sbrn & 0x0f),
5099 temp >> 8, temp & 0xff);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005100
5101 fotg210_writel(fotg210, INTR_MASK,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005102 &fotg210->regs->intr_enable); /* Turn On Interrupts */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005103
5104 /* GRR this is run-once init(), being done every time the HC starts.
5105 * So long as they're part of class devices, we can't do it init()
5106 * since the class device isn't created that early.
5107 */
5108 create_debug_files(fotg210);
5109 create_sysfs_files(fotg210);
5110
5111 return 0;
5112}
5113
5114static int fotg210_setup(struct usb_hcd *hcd)
5115{
5116 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5117 int retval;
5118
5119 fotg210->regs = (void __iomem *)fotg210->caps +
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005120 HC_LENGTH(fotg210,
5121 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005122 dbg_hcs_params(fotg210, "reset");
5123 dbg_hcc_params(fotg210, "reset");
5124
5125 /* cache this readonly data; minimize chip reads */
5126 fotg210->hcs_params = fotg210_readl(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005127 &fotg210->caps->hcs_params);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005128
5129 fotg210->sbrn = HCD_USB2;
5130
5131 /* data structure init */
5132 retval = hcd_fotg210_init(hcd);
5133 if (retval)
5134 return retval;
5135
5136 retval = fotg210_halt(fotg210);
5137 if (retval)
5138 return retval;
5139
5140 fotg210_reset(fotg210);
5141
5142 return 0;
5143}
5144
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005145static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
5146{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005147 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5148 u32 status, masked_status, pcd_status = 0, cmd;
5149 int bh;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005150
5151 spin_lock(&fotg210->lock);
5152
5153 status = fotg210_readl(fotg210, &fotg210->regs->status);
5154
5155 /* e.g. cardbus physical eject */
5156 if (status == ~(u32) 0) {
5157 fotg210_dbg(fotg210, "device removed\n");
5158 goto dead;
5159 }
5160
5161 /*
5162 * We don't use STS_FLR, but some controllers don't like it to
5163 * remain on, so mask it out along with the other status bits.
5164 */
5165 masked_status = status & (INTR_MASK | STS_FLR);
5166
5167 /* Shared IRQ? */
5168 if (!masked_status ||
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005169 unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005170 spin_unlock(&fotg210->lock);
5171 return IRQ_NONE;
5172 }
5173
5174 /* clear (just) interrupts */
5175 fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
5176 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
5177 bh = 0;
5178
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005179 /* unrequested/ignored: Frame List Rollover */
5180 dbg_status(fotg210, "irq", status);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005181
5182 /* INT, ERR, and IAA interrupt rates can be throttled */
5183
5184 /* normal [4.15.1.2] or error [4.15.1.1] completion */
5185 if (likely((status & (STS_INT|STS_ERR)) != 0)) {
5186 if (likely((status & STS_ERR) == 0))
5187 COUNT(fotg210->stats.normal);
5188 else
5189 COUNT(fotg210->stats.error);
5190 bh = 1;
5191 }
5192
5193 /* complete the unlinking of some qh [4.15.2.3] */
5194 if (status & STS_IAA) {
5195
5196 /* Turn off the IAA watchdog */
5197 fotg210->enabled_hrtimer_events &=
5198 ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
5199
5200 /*
5201 * Mild optimization: Allow another IAAD to reset the
5202 * hrtimer, if one occurs before the next expiration.
5203 * In theory we could always cancel the hrtimer, but
5204 * tests show that about half the time it will be reset
5205 * for some other event anyway.
5206 */
5207 if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
5208 ++fotg210->next_hrtimer_event;
5209
5210 /* guard against (alleged) silicon errata */
5211 if (cmd & CMD_IAAD)
5212 fotg210_dbg(fotg210, "IAA with IAAD still set?\n");
5213 if (fotg210->async_iaa) {
5214 COUNT(fotg210->stats.iaa);
5215 end_unlink_async(fotg210);
5216 } else
5217 fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
5218 }
5219
5220 /* remote wakeup [4.3.1] */
5221 if (status & STS_PCD) {
5222 int pstatus;
5223 u32 __iomem *status_reg = &fotg210->regs->port_status;
5224
5225 /* kick root hub later */
5226 pcd_status = status;
5227
5228 /* resume root hub? */
5229 if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
5230 usb_hcd_resume_root_hub(hcd);
5231
5232 pstatus = fotg210_readl(fotg210, status_reg);
5233
5234 if (test_bit(0, &fotg210->suspended_ports) &&
5235 ((pstatus & PORT_RESUME) ||
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005236 !(pstatus & PORT_SUSPEND)) &&
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005237 (pstatus & PORT_PE) &&
5238 fotg210->reset_done[0] == 0) {
5239
5240 /* start 20 msec resume signaling from this port,
Petr Mladek37ebb542014-09-19 17:32:23 +02005241 * and make hub_wq collect PORT_STAT_C_SUSPEND to
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005242 * stop that signaling. Use 5 ms extra for safety,
5243 * like usb_port_resume() does.
5244 */
5245 fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25);
5246 set_bit(0, &fotg210->resuming_ports);
5247 fotg210_dbg(fotg210, "port 1 remote wakeup\n");
5248 mod_timer(&hcd->rh_timer, fotg210->reset_done[0]);
5249 }
5250 }
5251
5252 /* PCI errors [4.15.2.4] */
5253 if (unlikely((status & STS_FATAL) != 0)) {
5254 fotg210_err(fotg210, "fatal error\n");
5255 dbg_cmd(fotg210, "fatal", cmd);
5256 dbg_status(fotg210, "fatal", status);
5257dead:
5258 usb_hc_died(hcd);
5259
5260 /* Don't let the controller do anything more */
5261 fotg210->shutdown = true;
5262 fotg210->rh_state = FOTG210_RH_STOPPING;
5263 fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
5264 fotg210_writel(fotg210, fotg210->command,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005265 &fotg210->regs->command);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005266 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
5267 fotg210_handle_controller_death(fotg210);
5268
5269 /* Handle completions when the controller stops */
5270 bh = 0;
5271 }
5272
5273 if (bh)
5274 fotg210_work(fotg210);
5275 spin_unlock(&fotg210->lock);
5276 if (pcd_status)
5277 usb_hcd_poll_rh_status(hcd);
5278 return IRQ_HANDLED;
5279}
5280
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005281/* non-error returns are a promise to giveback() the urb later
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005282 * we drop ownership so next owner (or urb unlink) can get it
5283 *
5284 * urb + dev is in hcd.self.controller.urb_list
5285 * we're queueing TDs onto software and hardware lists
5286 *
5287 * hcd-specific init for hcpriv hasn't been done yet
5288 *
5289 * NOTE: control, bulk, and interrupt share the same code to append TDs
5290 * to a (possibly active) QH, and the same QH scanning code.
5291 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005292static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
5293 gfp_t mem_flags)
5294{
5295 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5296 struct list_head qtd_list;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005297
5298 INIT_LIST_HEAD(&qtd_list);
5299
5300 switch (usb_pipetype(urb->pipe)) {
5301 case PIPE_CONTROL:
5302 /* qh_completions() code doesn't handle all the fault cases
5303 * in multi-TD control transfers. Even 1KB is rare anyway.
5304 */
5305 if (urb->transfer_buffer_length > (16 * 1024))
5306 return -EMSGSIZE;
5307 /* FALLTHROUGH */
5308 /* case PIPE_BULK: */
5309 default:
5310 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5311 return -ENOMEM;
5312 return submit_async(fotg210, urb, &qtd_list, mem_flags);
5313
5314 case PIPE_INTERRUPT:
5315 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5316 return -ENOMEM;
5317 return intr_submit(fotg210, urb, &qtd_list, mem_flags);
5318
5319 case PIPE_ISOCHRONOUS:
5320 return itd_submit(fotg210, urb, mem_flags);
5321 }
5322}
5323
5324/* remove from hardware lists
5325 * completions normally happen asynchronously
5326 */
5327
5328static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
5329{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005330 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5331 struct fotg210_qh *qh;
5332 unsigned long flags;
5333 int rc;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005334
5335 spin_lock_irqsave(&fotg210->lock, flags);
5336 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
5337 if (rc)
5338 goto done;
5339
5340 switch (usb_pipetype(urb->pipe)) {
5341 /* case PIPE_CONTROL: */
5342 /* case PIPE_BULK:*/
5343 default:
5344 qh = (struct fotg210_qh *) urb->hcpriv;
5345 if (!qh)
5346 break;
5347 switch (qh->qh_state) {
5348 case QH_STATE_LINKED:
5349 case QH_STATE_COMPLETING:
5350 start_unlink_async(fotg210, qh);
5351 break;
5352 case QH_STATE_UNLINK:
5353 case QH_STATE_UNLINK_WAIT:
5354 /* already started */
5355 break;
5356 case QH_STATE_IDLE:
5357 /* QH might be waiting for a Clear-TT-Buffer */
5358 qh_completions(fotg210, qh);
5359 break;
5360 }
5361 break;
5362
5363 case PIPE_INTERRUPT:
5364 qh = (struct fotg210_qh *) urb->hcpriv;
5365 if (!qh)
5366 break;
5367 switch (qh->qh_state) {
5368 case QH_STATE_LINKED:
5369 case QH_STATE_COMPLETING:
5370 start_unlink_intr(fotg210, qh);
5371 break;
5372 case QH_STATE_IDLE:
5373 qh_completions(fotg210, qh);
5374 break;
5375 default:
5376 fotg210_dbg(fotg210, "bogus qh %p state %d\n",
5377 qh, qh->qh_state);
5378 goto done;
5379 }
5380 break;
5381
5382 case PIPE_ISOCHRONOUS:
5383 /* itd... */
5384
5385 /* wait till next completion, do it then. */
5386 /* completion irqs can wait up to 1024 msec, */
5387 break;
5388 }
5389done:
5390 spin_unlock_irqrestore(&fotg210->lock, flags);
5391 return rc;
5392}
5393
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005394/* bulk qh holds the data toggle */
5395
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005396static void fotg210_endpoint_disable(struct usb_hcd *hcd,
5397 struct usb_host_endpoint *ep)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005398{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005399 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5400 unsigned long flags;
5401 struct fotg210_qh *qh, *tmp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005402
5403 /* ASSERT: any requests/urbs are being unlinked */
5404 /* ASSERT: nobody can be submitting urbs for this any more */
5405
5406rescan:
5407 spin_lock_irqsave(&fotg210->lock, flags);
5408 qh = ep->hcpriv;
5409 if (!qh)
5410 goto done;
5411
5412 /* endpoints can be iso streams. for now, we don't
5413 * accelerate iso completions ... so spin a while.
5414 */
5415 if (qh->hw == NULL) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005416 struct fotg210_iso_stream *stream = ep->hcpriv;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005417
5418 if (!list_empty(&stream->td_list))
5419 goto idle_timeout;
5420
5421 /* BUG_ON(!list_empty(&stream->free_list)); */
5422 kfree(stream);
5423 goto done;
5424 }
5425
5426 if (fotg210->rh_state < FOTG210_RH_RUNNING)
5427 qh->qh_state = QH_STATE_IDLE;
5428 switch (qh->qh_state) {
5429 case QH_STATE_LINKED:
5430 case QH_STATE_COMPLETING:
5431 for (tmp = fotg210->async->qh_next.qh;
5432 tmp && tmp != qh;
5433 tmp = tmp->qh_next.qh)
5434 continue;
5435 /* periodic qh self-unlinks on empty, and a COMPLETING qh
5436 * may already be unlinked.
5437 */
5438 if (tmp)
5439 start_unlink_async(fotg210, qh);
5440 /* FALL THROUGH */
5441 case QH_STATE_UNLINK: /* wait for hw to finish? */
5442 case QH_STATE_UNLINK_WAIT:
5443idle_timeout:
5444 spin_unlock_irqrestore(&fotg210->lock, flags);
5445 schedule_timeout_uninterruptible(1);
5446 goto rescan;
5447 case QH_STATE_IDLE: /* fully unlinked */
5448 if (qh->clearing_tt)
5449 goto idle_timeout;
5450 if (list_empty(&qh->qtd_list)) {
5451 qh_destroy(fotg210, qh);
5452 break;
5453 }
5454 /* else FALL THROUGH */
5455 default:
5456 /* caller was supposed to have unlinked any requests;
5457 * that's not our job. just leak this memory.
5458 */
5459 fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005460 qh, ep->desc.bEndpointAddress, qh->qh_state,
5461 list_empty(&qh->qtd_list) ? "" : "(has tds)");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005462 break;
5463 }
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005464done:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005465 ep->hcpriv = NULL;
5466 spin_unlock_irqrestore(&fotg210->lock, flags);
5467}
5468
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005469static void fotg210_endpoint_reset(struct usb_hcd *hcd,
5470 struct usb_host_endpoint *ep)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005471{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005472 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5473 struct fotg210_qh *qh;
5474 int eptype = usb_endpoint_type(&ep->desc);
5475 int epnum = usb_endpoint_num(&ep->desc);
5476 int is_out = usb_endpoint_dir_out(&ep->desc);
5477 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005478
5479 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
5480 return;
5481
5482 spin_lock_irqsave(&fotg210->lock, flags);
5483 qh = ep->hcpriv;
5484
5485 /* For Bulk and Interrupt endpoints we maintain the toggle state
5486 * in the hardware; the toggle bits in udev aren't used at all.
5487 * When an endpoint is reset by usb_clear_halt() we must reset
5488 * the toggle bit in the QH.
5489 */
5490 if (qh) {
5491 usb_settoggle(qh->dev, epnum, is_out, 0);
5492 if (!list_empty(&qh->qtd_list)) {
5493 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
5494 } else if (qh->qh_state == QH_STATE_LINKED ||
5495 qh->qh_state == QH_STATE_COMPLETING) {
5496
5497 /* The toggle value in the QH can't be updated
5498 * while the QH is active. Unlink it now;
5499 * re-linking will call qh_refresh().
5500 */
5501 if (eptype == USB_ENDPOINT_XFER_BULK)
5502 start_unlink_async(fotg210, qh);
5503 else
5504 start_unlink_intr(fotg210, qh);
5505 }
5506 }
5507 spin_unlock_irqrestore(&fotg210->lock, flags);
5508}
5509
5510static int fotg210_get_frame(struct usb_hcd *hcd)
5511{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005512 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5513
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005514 return (fotg210_read_frame_index(fotg210) >> 3) %
5515 fotg210->periodic_size;
5516}
5517
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005518/* The EHCI in ChipIdea HDRC cannot be a separate module or device,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005519 * because its registers (and irq) are shared between host/gadget/otg
5520 * functions and in order to facilitate role switching we cannot
5521 * give the fotg210 driver exclusive access to those.
5522 */
5523MODULE_DESCRIPTION(DRIVER_DESC);
5524MODULE_AUTHOR(DRIVER_AUTHOR);
5525MODULE_LICENSE("GPL");
5526
5527static const struct hc_driver fotg210_fotg210_hc_driver = {
5528 .description = hcd_name,
5529 .product_desc = "Faraday USB2.0 Host Controller",
5530 .hcd_priv_size = sizeof(struct fotg210_hcd),
5531
5532 /*
5533 * generic hardware linkage
5534 */
5535 .irq = fotg210_irq,
5536 .flags = HCD_MEMORY | HCD_USB2,
5537
5538 /*
5539 * basic lifecycle operations
5540 */
5541 .reset = hcd_fotg210_init,
5542 .start = fotg210_run,
5543 .stop = fotg210_stop,
5544 .shutdown = fotg210_shutdown,
5545
5546 /*
5547 * managing i/o requests and associated device resources
5548 */
5549 .urb_enqueue = fotg210_urb_enqueue,
5550 .urb_dequeue = fotg210_urb_dequeue,
5551 .endpoint_disable = fotg210_endpoint_disable,
5552 .endpoint_reset = fotg210_endpoint_reset,
5553
5554 /*
5555 * scheduling support
5556 */
5557 .get_frame_number = fotg210_get_frame,
5558
5559 /*
5560 * root hub support
5561 */
5562 .hub_status_data = fotg210_hub_status_data,
5563 .hub_control = fotg210_hub_control,
5564 .bus_suspend = fotg210_bus_suspend,
5565 .bus_resume = fotg210_bus_resume,
5566
5567 .relinquish_port = fotg210_relinquish_port,
5568 .port_handed_over = fotg210_port_handed_over,
5569
5570 .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
5571};
5572
5573static void fotg210_init(struct fotg210_hcd *fotg210)
5574{
5575 u32 value;
5576
5577 iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005578 &fotg210->regs->gmir);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005579
5580 value = ioread32(&fotg210->regs->otgcsr);
5581 value &= ~OTGCSR_A_BUS_DROP;
5582 value |= OTGCSR_A_BUS_REQ;
5583 iowrite32(value, &fotg210->regs->otgcsr);
5584}
5585
5586/**
5587 * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
5588 *
5589 * Allocates basic resources for this USB host controller, and
5590 * then invokes the start() method for the HCD associated with it
5591 * through the hotplug entry's driver_data.
5592 */
5593static int fotg210_hcd_probe(struct platform_device *pdev)
5594{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005595 struct device *dev = &pdev->dev;
5596 struct usb_hcd *hcd;
5597 struct resource *res;
5598 int irq;
Christophe JAILLETe9035d82021-05-06 22:39:10 +02005599 int retval;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005600 struct fotg210_hcd *fotg210;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005601
5602 if (usb_disabled())
5603 return -ENODEV;
5604
5605 pdev->dev.power.power_state = PMSG_ON;
5606
5607 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
5608 if (!res) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005609 dev_err(dev, "Found HC with no IRQ. Check %s setup!\n",
5610 dev_name(dev));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005611 return -ENODEV;
5612 }
5613
5614 irq = res->start;
5615
5616 hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
5617 dev_name(dev));
5618 if (!hcd) {
Christophe JAILLETe9035d82021-05-06 22:39:10 +02005619 dev_err(dev, "failed to create hcd\n");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005620 retval = -ENOMEM;
5621 goto fail_create_hcd;
5622 }
5623
Himangi Saraogi0e278b32014-07-02 02:04:44 +05305624 hcd->has_tt = 1;
5625
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005626 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Himangi Saraogi0e278b32014-07-02 02:04:44 +05305627 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
5628 if (IS_ERR(hcd->regs)) {
5629 retval = PTR_ERR(hcd->regs);
5630 goto failed;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005631 }
5632
5633 hcd->rsrc_start = res->start;
5634 hcd->rsrc_len = resource_size(res);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005635
5636 fotg210 = hcd_to_fotg210(hcd);
5637
5638 fotg210->caps = hcd->regs;
5639
5640 retval = fotg210_setup(hcd);
5641 if (retval)
Himangi Saraogi0e278b32014-07-02 02:04:44 +05305642 goto failed;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005643
5644 fotg210_init(fotg210);
5645
5646 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
5647 if (retval) {
5648 dev_err(dev, "failed to add hcd with err %d\n", retval);
Himangi Saraogi0e278b32014-07-02 02:04:44 +05305649 goto failed;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005650 }
Peter Chen3c9740a2013-11-05 10:46:02 +08005651 device_wakeup_enable(hcd->self.controller);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005652
5653 return retval;
5654
Himangi Saraogi0e278b32014-07-02 02:04:44 +05305655failed:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005656 usb_put_hcd(hcd);
5657fail_create_hcd:
5658 dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
5659 return retval;
5660}
5661
5662/**
5663 * fotg210_hcd_remove - shutdown processing for EHCI HCDs
5664 * @dev: USB Host Controller being removed
5665 *
5666 */
5667static int fotg210_hcd_remove(struct platform_device *pdev)
5668{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005669 struct device *dev = &pdev->dev;
5670 struct usb_hcd *hcd = dev_get_drvdata(dev);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005671
5672 if (!hcd)
5673 return 0;
5674
5675 usb_remove_hcd(hcd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005676 usb_put_hcd(hcd);
5677
5678 return 0;
5679}
5680
5681static struct platform_driver fotg210_hcd_driver = {
5682 .driver = {
5683 .name = "fotg210-hcd",
5684 },
5685 .probe = fotg210_hcd_probe,
5686 .remove = fotg210_hcd_remove,
5687};
5688
5689static int __init fotg210_hcd_init(void)
5690{
5691 int retval = 0;
5692
5693 if (usb_disabled())
5694 return -ENODEV;
5695
5696 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
5697 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5698 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
5699 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
Peter Senna Tschudin5ec9b892015-10-12 23:22:33 +02005700 pr_warn("Warning! fotg210_hcd should always be loaded before uhci_hcd and ohci_hcd, not after\n");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005701
5702 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005703 hcd_name, sizeof(struct fotg210_qh),
5704 sizeof(struct fotg210_qtd),
5705 sizeof(struct fotg210_itd));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005706
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005707 fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
5708 if (!fotg210_debug_root) {
5709 retval = -ENOENT;
5710 goto err_debug;
5711 }
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005712
5713 retval = platform_driver_register(&fotg210_hcd_driver);
5714 if (retval < 0)
5715 goto clean;
5716 return retval;
5717
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005718clean:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005719 debugfs_remove(fotg210_debug_root);
5720 fotg210_debug_root = NULL;
5721err_debug:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005722 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5723 return retval;
5724}
5725module_init(fotg210_hcd_init);
5726
5727static void __exit fotg210_hcd_cleanup(void)
5728{
5729 platform_driver_unregister(&fotg210_hcd_driver);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005730 debugfs_remove(fotg210_debug_root);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005731 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5732}
5733module_exit(fotg210_hcd_cleanup);