blob: 7af17c8e069b356dc4dd4865be90b83d6dfb5e5b [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002/* Faraday FOTG210 EHCI-like driver
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003 *
4 * Copyright (c) 2013 Faraday Technology Corporation
5 *
6 * Author: Yuan-Hsin Chen <yhchen@faraday-tech.com>
7 * Feng-Hsin Chiang <john453@faraday-tech.com>
8 * Po-Yu Chuang <ratbert.chuang@gmail.com>
9 *
10 * Most of code borrowed from the Linux-3.7 EHCI driver
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000011 */
12#include <linux/module.h>
Hans Ulli Krolld7863de2019-06-13 17:07:35 +020013#include <linux/of.h>
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000014#include <linux/device.h>
15#include <linux/dmapool.h>
16#include <linux/kernel.h>
17#include <linux/delay.h>
18#include <linux/ioport.h>
19#include <linux/sched.h>
20#include <linux/vmalloc.h>
21#include <linux/errno.h>
22#include <linux/init.h>
23#include <linux/hrtimer.h>
24#include <linux/list.h>
25#include <linux/interrupt.h>
26#include <linux/usb.h>
27#include <linux/usb/hcd.h>
28#include <linux/moduleparam.h>
29#include <linux/dma-mapping.h>
30#include <linux/debugfs.h>
31#include <linux/slab.h>
32#include <linux/uaccess.h>
33#include <linux/platform_device.h>
34#include <linux/io.h>
Chunfeng Yune7d82632020-09-21 14:13:30 +080035#include <linux/iopoll.h>
Linus Walleijffa8a312018-09-01 18:03:12 +020036#include <linux/clk.h>
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000037
38#include <asm/byteorder.h>
39#include <asm/irq.h>
40#include <asm/unaligned.h>
41
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000042#define DRIVER_AUTHOR "Yuan-Hsin Chen"
43#define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020044static const char hcd_name[] = "fotg210_hcd";
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000045
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000046#undef FOTG210_URB_TRACE
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000047#define FOTG210_STATS
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000048
49/* magic numbers that can affect system performance */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020050#define FOTG210_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
51#define FOTG210_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
52#define FOTG210_TUNE_RL_TT 0
53#define FOTG210_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
54#define FOTG210_TUNE_MULT_TT 1
55
56/* Some drivers think it's safe to schedule isochronous transfers more than 256
57 * ms into the future (partly as a result of an old bug in the scheduling
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000058 * code). In an attempt to avoid trouble, we will use a minimum scheduling
59 * length of 512 frames instead of 256.
60 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020061#define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000062
63/* Initial IRQ latency: faster than hw default */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020064static int log2_irq_thresh; /* 0 to 6 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000065module_param(log2_irq_thresh, int, S_IRUGO);
66MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
67
68/* initial park setting: slower than hw default */
69static unsigned park;
70module_param(park, uint, S_IRUGO);
71MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");
72
73/* for link power management(LPM) feature */
74static unsigned int hird;
75module_param(hird, int, S_IRUGO);
76MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
77
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020078#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000079
80#include "fotg210.h"
81
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000082#define fotg210_dbg(fotg210, fmt, args...) \
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020083 dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000084#define fotg210_err(fotg210, fmt, args...) \
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020085 dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000086#define fotg210_info(fotg210, fmt, args...) \
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020087 dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000088#define fotg210_warn(fotg210, fmt, args...) \
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020089 dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000090
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020091/* check the values in the HCSPARAMS register (host controller _Structural_
92 * parameters) see EHCI spec, Table 2-4 for each value
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000093 */
94static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
95{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020096 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +000097
Peter Senna Tschudin259127b2015-10-12 23:22:32 +020098 fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
99 HCS_N_PORTS(params));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000100}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000101
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200102/* check the values in the HCCPARAMS register (host controller _Capability_
103 * parameters) see EHCI Spec, Table 2-5 for each value
104 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000105static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
106{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200107 u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000108
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200109 fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label,
110 params,
111 HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
112 HCC_CANPARK(params) ? " park" : "");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000113}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000114
115static void __maybe_unused
116dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
117{
118 fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200119 hc32_to_cpup(fotg210, &qtd->hw_next),
120 hc32_to_cpup(fotg210, &qtd->hw_alt_next),
121 hc32_to_cpup(fotg210, &qtd->hw_token),
122 hc32_to_cpup(fotg210, &qtd->hw_buf[0]));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000123 if (qtd->hw_buf[1])
124 fotg210_dbg(fotg210, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200125 hc32_to_cpup(fotg210, &qtd->hw_buf[1]),
126 hc32_to_cpup(fotg210, &qtd->hw_buf[2]),
127 hc32_to_cpup(fotg210, &qtd->hw_buf[3]),
128 hc32_to_cpup(fotg210, &qtd->hw_buf[4]));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000129}
130
131static void __maybe_unused
132dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
133{
134 struct fotg210_qh_hw *hw = qh->hw;
135
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200136 fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label, qh,
137 hw->hw_next, hw->hw_info1, hw->hw_info2,
138 hw->hw_current);
139
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000140 dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
141}
142
143static void __maybe_unused
144dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
145{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200146 fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n", label,
147 itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next),
148 itd->urb);
149
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000150 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200151 " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
152 hc32_to_cpu(fotg210, itd->hw_transaction[0]),
153 hc32_to_cpu(fotg210, itd->hw_transaction[1]),
154 hc32_to_cpu(fotg210, itd->hw_transaction[2]),
155 hc32_to_cpu(fotg210, itd->hw_transaction[3]),
156 hc32_to_cpu(fotg210, itd->hw_transaction[4]),
157 hc32_to_cpu(fotg210, itd->hw_transaction[5]),
158 hc32_to_cpu(fotg210, itd->hw_transaction[6]),
159 hc32_to_cpu(fotg210, itd->hw_transaction[7]));
160
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000161 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200162 " buf: %08x %08x %08x %08x %08x %08x %08x\n",
163 hc32_to_cpu(fotg210, itd->hw_bufp[0]),
164 hc32_to_cpu(fotg210, itd->hw_bufp[1]),
165 hc32_to_cpu(fotg210, itd->hw_bufp[2]),
166 hc32_to_cpu(fotg210, itd->hw_bufp[3]),
167 hc32_to_cpu(fotg210, itd->hw_bufp[4]),
168 hc32_to_cpu(fotg210, itd->hw_bufp[5]),
169 hc32_to_cpu(fotg210, itd->hw_bufp[6]));
170
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000171 fotg210_dbg(fotg210, " index: %d %d %d %d %d %d %d %d\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200172 itd->index[0], itd->index[1], itd->index[2],
173 itd->index[3], itd->index[4], itd->index[5],
174 itd->index[6], itd->index[7]);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000175}
176
177static int __maybe_unused
178dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
179{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200180 return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
181 label, label[0] ? " " : "", status,
182 (status & STS_ASS) ? " Async" : "",
183 (status & STS_PSS) ? " Periodic" : "",
184 (status & STS_RECL) ? " Recl" : "",
185 (status & STS_HALT) ? " Halt" : "",
186 (status & STS_IAA) ? " IAA" : "",
187 (status & STS_FATAL) ? " FATAL" : "",
188 (status & STS_FLR) ? " FLR" : "",
189 (status & STS_PCD) ? " PCD" : "",
190 (status & STS_ERR) ? " ERR" : "",
191 (status & STS_INT) ? " INT" : "");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000192}
193
194static int __maybe_unused
195dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
196{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200197 return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
198 label, label[0] ? " " : "", enable,
199 (enable & STS_IAA) ? " IAA" : "",
200 (enable & STS_FATAL) ? " FATAL" : "",
201 (enable & STS_FLR) ? " FLR" : "",
202 (enable & STS_PCD) ? " PCD" : "",
203 (enable & STS_ERR) ? " ERR" : "",
204 (enable & STS_INT) ? " INT" : "");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000205}
206
207static const char *const fls_strings[] = { "1024", "512", "256", "??" };
208
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200209static int dbg_command_buf(char *buf, unsigned len, const char *label,
210 u32 command)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000211{
212 return scnprintf(buf, len,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200213 "%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s",
214 label, label[0] ? " " : "", command,
215 (command & CMD_PARK) ? " park" : "(park)",
216 CMD_PARK_CNT(command),
217 (command >> 16) & 0x3f,
218 (command & CMD_IAAD) ? " IAAD" : "",
219 (command & CMD_ASE) ? " Async" : "",
220 (command & CMD_PSE) ? " Periodic" : "",
221 fls_strings[(command >> 2) & 0x3],
222 (command & CMD_RESET) ? " Reset" : "",
223 (command & CMD_RUN) ? "RUN" : "HALT");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000224}
225
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200226static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
227 u32 status)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000228{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200229 char *sig;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000230
231 /* signaling state */
232 switch (status & (3 << 10)) {
233 case 0 << 10:
234 sig = "se0";
235 break;
236 case 1 << 10:
237 sig = "k";
238 break; /* low speed */
239 case 2 << 10:
240 sig = "j";
241 break;
242 default:
243 sig = "?";
244 break;
245 }
246
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200247 scnprintf(buf, len, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s",
248 label, label[0] ? " " : "", port, status,
249 status >> 25, /*device address */
250 sig,
251 (status & PORT_RESET) ? " RESET" : "",
252 (status & PORT_SUSPEND) ? " SUSPEND" : "",
253 (status & PORT_RESUME) ? " RESUME" : "",
254 (status & PORT_PEC) ? " PEC" : "",
255 (status & PORT_PE) ? " PE" : "",
256 (status & PORT_CSC) ? " CSC" : "",
257 (status & PORT_CONNECT) ? " CONNECT" : "");
258
Oliver Neukumf848a882013-11-18 13:23:05 +0100259 return buf;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000260}
261
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000262/* functions have the "wrong" filename when they're output... */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200263#define dbg_status(fotg210, label, status) { \
264 char _buf[80]; \
265 dbg_status_buf(_buf, sizeof(_buf), label, status); \
266 fotg210_dbg(fotg210, "%s\n", _buf); \
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000267}
268
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200269#define dbg_cmd(fotg210, label, command) { \
270 char _buf[80]; \
271 dbg_command_buf(_buf, sizeof(_buf), label, command); \
272 fotg210_dbg(fotg210, "%s\n", _buf); \
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000273}
274
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200275#define dbg_port(fotg210, label, port, status) { \
276 char _buf[80]; \
277 fotg210_dbg(fotg210, "%s\n", \
278 dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000279}
280
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000281/* troubleshooting help: expose state in debugfs */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000282static int debug_async_open(struct inode *, struct file *);
283static int debug_periodic_open(struct inode *, struct file *);
284static int debug_registers_open(struct inode *, struct file *);
285static int debug_async_open(struct inode *, struct file *);
286
287static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
288static int debug_close(struct inode *, struct file *);
289
290static const struct file_operations debug_async_fops = {
291 .owner = THIS_MODULE,
292 .open = debug_async_open,
293 .read = debug_output,
294 .release = debug_close,
295 .llseek = default_llseek,
296};
297static const struct file_operations debug_periodic_fops = {
298 .owner = THIS_MODULE,
299 .open = debug_periodic_open,
300 .read = debug_output,
301 .release = debug_close,
302 .llseek = default_llseek,
303};
304static const struct file_operations debug_registers_fops = {
305 .owner = THIS_MODULE,
306 .open = debug_registers_open,
307 .read = debug_output,
308 .release = debug_close,
309 .llseek = default_llseek,
310};
311
312static struct dentry *fotg210_debug_root;
313
314struct debug_buffer {
315 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
316 struct usb_bus *bus;
317 struct mutex mutex; /* protect filling of buffer */
318 size_t count; /* number of characters filled into buffer */
319 char *output_buf;
320 size_t alloc_size;
321};
322
Peter Senna Tschudinc4d66b52015-10-12 23:22:38 +0200323static inline char speed_char(u32 scratch)
324{
325 switch (scratch & (3 << 12)) {
326 case QH_FULL_SPEED:
327 return 'f';
328
329 case QH_LOW_SPEED:
330 return 'l';
331
332 case QH_HIGH_SPEED:
333 return 'h';
334
335 default:
336 return '?';
337 }
338}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000339
340static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
341{
342 __u32 v = hc32_to_cpu(fotg210, token);
343
344 if (v & QTD_STS_ACTIVE)
345 return '*';
346 if (v & QTD_STS_HALT)
347 return '-';
348 if (!IS_SHORT_READ(v))
349 return ' ';
350 /* tries to advance through hw_alt_next */
351 return '/';
352}
353
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200354static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
355 char **nextp, unsigned *sizep)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000356{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200357 u32 scratch;
358 u32 hw_curr;
359 struct fotg210_qtd *td;
360 unsigned temp;
361 unsigned size = *sizep;
362 char *next = *nextp;
363 char mark;
364 __le32 list_end = FOTG210_LIST_END(fotg210);
365 struct fotg210_qh_hw *hw = qh->hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000366
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200367 if (hw->hw_qtd_next == list_end) /* NEC does this */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000368 mark = '@';
369 else
370 mark = token_mark(fotg210, hw->hw_token);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200371 if (mark == '/') { /* qh_alt_next controls qh advance? */
372 if ((hw->hw_alt_next & QTD_MASK(fotg210)) ==
373 fotg210->async->hw->hw_alt_next)
374 mark = '#'; /* blocked */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000375 else if (hw->hw_alt_next == list_end)
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200376 mark = '.'; /* use hw_qtd_next */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000377 /* else alt_next points to some other qtd */
378 }
379 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
380 hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0;
381 temp = scnprintf(next, size,
382 "qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
383 qh, scratch & 0x007f,
384 speed_char(scratch),
385 (scratch >> 8) & 0x000f,
386 scratch, hc32_to_cpup(fotg210, &hw->hw_info2),
387 hc32_to_cpup(fotg210, &hw->hw_token), mark,
388 (cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token)
389 ? "data1" : "data0",
390 (hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
391 size -= temp;
392 next += temp;
393
394 /* hc may be modifying the list as we read it ... */
395 list_for_each_entry(td, &qh->qtd_list, qtd_list) {
396 scratch = hc32_to_cpup(fotg210, &td->hw_token);
397 mark = ' ';
398 if (hw_curr == td->qtd_dma)
399 mark = '*';
400 else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
401 mark = '+';
402 else if (QTD_LENGTH(scratch)) {
403 if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
404 mark = '#';
405 else if (td->hw_alt_next != list_end)
406 mark = '/';
407 }
408 temp = snprintf(next, size,
409 "\n\t%p%c%s len=%d %08x urb %p",
410 td, mark, ({ char *tmp;
Shubhankar Kuranagatti4e6e8512021-03-12 00:30:58 +0530411 switch ((scratch>>8)&0x03) {
412 case 0:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000413 tmp = "out";
414 break;
Shubhankar Kuranagatti4e6e8512021-03-12 00:30:58 +0530415 case 1:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000416 tmp = "in";
417 break;
Shubhankar Kuranagatti4e6e8512021-03-12 00:30:58 +0530418 case 2:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000419 tmp = "setup";
420 break;
Shubhankar Kuranagatti4e6e8512021-03-12 00:30:58 +0530421 default:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000422 tmp = "?";
423 break;
424 } tmp; }),
425 (scratch >> 16) & 0x7fff,
426 scratch,
427 td->urb);
428 if (size < temp)
429 temp = size;
430 size -= temp;
431 next += temp;
432 if (temp == size)
433 goto done;
434 }
435
436 temp = snprintf(next, size, "\n");
437 if (size < temp)
438 temp = size;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200439
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000440 size -= temp;
441 next += temp;
442
443done:
444 *sizep = size;
445 *nextp = next;
446}
447
448static ssize_t fill_async_buffer(struct debug_buffer *buf)
449{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200450 struct usb_hcd *hcd;
451 struct fotg210_hcd *fotg210;
452 unsigned long flags;
453 unsigned temp, size;
454 char *next;
455 struct fotg210_qh *qh;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000456
457 hcd = bus_to_hcd(buf->bus);
458 fotg210 = hcd_to_fotg210(hcd);
459 next = buf->output_buf;
460 size = buf->alloc_size;
461
462 *next = 0;
463
464 /* dumps a snapshot of the async schedule.
465 * usually empty except for long-term bulk reads, or head.
466 * one QH per line, and TDs we know about
467 */
468 spin_lock_irqsave(&fotg210->lock, flags);
469 for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200470 qh = qh->qh_next.qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000471 qh_lines(fotg210, qh, &next, &size);
472 if (fotg210->async_unlink && size > 0) {
473 temp = scnprintf(next, size, "\nunlink =\n");
474 size -= temp;
475 next += temp;
476
477 for (qh = fotg210->async_unlink; size > 0 && qh;
478 qh = qh->unlink_next)
479 qh_lines(fotg210, qh, &next, &size);
480 }
481 spin_unlock_irqrestore(&fotg210->lock, flags);
482
483 return strlen(buf->output_buf);
484}
485
Peter Senna Tschudin32fb1932015-10-12 23:22:39 +0200486/* count tds, get ep direction */
487static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
488 struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
489{
490 u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
491 struct fotg210_qtd *qtd;
492 char *type = "";
493 unsigned temp = 0;
494
495 /* count tds, get ep direction */
496 list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
497 temp++;
498 switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
499 case 0:
500 type = "out";
501 continue;
502 case 1:
503 type = "in";
504 continue;
505 }
506 }
507
508 return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)",
509 speed_char(scratch), scratch & 0x007f,
510 (scratch >> 8) & 0x000f, type, qh->usecs,
511 qh->c_usecs, temp, (scratch >> 16) & 0x7ff);
512}
513
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000514#define DBG_SCHED_LIMIT 64
515static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
516{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200517 struct usb_hcd *hcd;
518 struct fotg210_hcd *fotg210;
519 unsigned long flags;
520 union fotg210_shadow p, *seen;
521 unsigned temp, size, seen_count;
522 char *next;
523 unsigned i;
524 __hc32 tag;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000525
Peter Senna Tschudin0d880022015-10-12 23:22:36 +0200526 seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000527 if (!seen)
528 return 0;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200529
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000530 seen_count = 0;
531
532 hcd = bus_to_hcd(buf->bus);
533 fotg210 = hcd_to_fotg210(hcd);
534 next = buf->output_buf;
535 size = buf->alloc_size;
536
537 temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
538 size -= temp;
539 next += temp;
540
541 /* dump a snapshot of the periodic schedule.
542 * iso changes, interrupt usually doesn't.
543 */
544 spin_lock_irqsave(&fotg210->lock, flags);
545 for (i = 0; i < fotg210->periodic_size; i++) {
546 p = fotg210->pshadow[i];
547 if (likely(!p.ptr))
548 continue;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200549
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000550 tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);
551
552 temp = scnprintf(next, size, "%4d: ", i);
553 size -= temp;
554 next += temp;
555
556 do {
557 struct fotg210_qh_hw *hw;
558
559 switch (hc32_to_cpu(fotg210, tag)) {
560 case Q_TYPE_QH:
561 hw = p.qh->hw;
562 temp = scnprintf(next, size, " qh%d-%04x/%p",
563 p.qh->period,
564 hc32_to_cpup(fotg210,
565 &hw->hw_info2)
566 /* uframe masks */
567 & (QH_CMASK | QH_SMASK),
568 p.qh);
569 size -= temp;
570 next += temp;
571 /* don't repeat what follows this qh */
572 for (temp = 0; temp < seen_count; temp++) {
573 if (seen[temp].ptr != p.ptr)
574 continue;
575 if (p.qh->qh_next.ptr) {
576 temp = scnprintf(next, size,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200577 " ...");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000578 size -= temp;
579 next += temp;
580 }
581 break;
582 }
583 /* show more info the first time around */
584 if (temp == seen_count) {
Peter Senna Tschudin32fb1932015-10-12 23:22:39 +0200585 temp = output_buf_tds_dir(next,
586 fotg210, hw,
587 p.qh, size);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000588
589 if (seen_count < DBG_SCHED_LIMIT)
590 seen[seen_count++].qh = p.qh;
591 } else
592 temp = 0;
593 tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
594 p = p.qh->qh_next;
595 break;
596 case Q_TYPE_FSTN:
597 temp = scnprintf(next, size,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200598 " fstn-%8x/%p",
599 p.fstn->hw_prev, p.fstn);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000600 tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
601 p = p.fstn->fstn_next;
602 break;
603 case Q_TYPE_ITD:
604 temp = scnprintf(next, size,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200605 " itd/%p", p.itd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000606 tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
607 p = p.itd->itd_next;
608 break;
609 }
610 size -= temp;
611 next += temp;
612 } while (p.ptr);
613
614 temp = scnprintf(next, size, "\n");
615 size -= temp;
616 next += temp;
617 }
618 spin_unlock_irqrestore(&fotg210->lock, flags);
619 kfree(seen);
620
621 return buf->alloc_size - size;
622}
623#undef DBG_SCHED_LIMIT
624
625static const char *rh_state_string(struct fotg210_hcd *fotg210)
626{
627 switch (fotg210->rh_state) {
628 case FOTG210_RH_HALTED:
629 return "halted";
630 case FOTG210_RH_SUSPENDED:
631 return "suspended";
632 case FOTG210_RH_RUNNING:
633 return "running";
634 case FOTG210_RH_STOPPING:
635 return "stopping";
636 }
637 return "?";
638}
639
640static ssize_t fill_registers_buffer(struct debug_buffer *buf)
641{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200642 struct usb_hcd *hcd;
643 struct fotg210_hcd *fotg210;
644 unsigned long flags;
645 unsigned temp, size, i;
646 char *next, scratch[80];
647 static const char fmt[] = "%*s\n";
648 static const char label[] = "";
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000649
650 hcd = bus_to_hcd(buf->bus);
651 fotg210 = hcd_to_fotg210(hcd);
652 next = buf->output_buf;
653 size = buf->alloc_size;
654
655 spin_lock_irqsave(&fotg210->lock, flags);
656
657 if (!HCD_HW_ACCESSIBLE(hcd)) {
658 size = scnprintf(next, size,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200659 "bus %s, device %s\n"
660 "%s\n"
661 "SUSPENDED(no register access)\n",
662 hcd->self.controller->bus->name,
663 dev_name(hcd->self.controller),
664 hcd->product_desc);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000665 goto done;
666 }
667
668 /* Capability Registers */
669 i = HC_VERSION(fotg210, fotg210_readl(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200670 &fotg210->caps->hc_capbase));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000671 temp = scnprintf(next, size,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200672 "bus %s, device %s\n"
673 "%s\n"
674 "EHCI %x.%02x, rh state %s\n",
675 hcd->self.controller->bus->name,
676 dev_name(hcd->self.controller),
677 hcd->product_desc,
678 i >> 8, i & 0x0ff, rh_state_string(fotg210));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000679 size -= temp;
680 next += temp;
681
682 /* FIXME interpret both types of params */
683 i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
684 temp = scnprintf(next, size, "structural params 0x%08x\n", i);
685 size -= temp;
686 next += temp;
687
688 i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
689 temp = scnprintf(next, size, "capability params 0x%08x\n", i);
690 size -= temp;
691 next += temp;
692
693 /* Operational Registers */
694 temp = dbg_status_buf(scratch, sizeof(scratch), label,
695 fotg210_readl(fotg210, &fotg210->regs->status));
696 temp = scnprintf(next, size, fmt, temp, scratch);
697 size -= temp;
698 next += temp;
699
700 temp = dbg_command_buf(scratch, sizeof(scratch), label,
701 fotg210_readl(fotg210, &fotg210->regs->command));
702 temp = scnprintf(next, size, fmt, temp, scratch);
703 size -= temp;
704 next += temp;
705
706 temp = dbg_intr_buf(scratch, sizeof(scratch), label,
707 fotg210_readl(fotg210, &fotg210->regs->intr_enable));
708 temp = scnprintf(next, size, fmt, temp, scratch);
709 size -= temp;
710 next += temp;
711
712 temp = scnprintf(next, size, "uframe %04x\n",
713 fotg210_read_frame_index(fotg210));
714 size -= temp;
715 next += temp;
716
717 if (fotg210->async_unlink) {
718 temp = scnprintf(next, size, "async unlink qh %p\n",
719 fotg210->async_unlink);
720 size -= temp;
721 next += temp;
722 }
723
724#ifdef FOTG210_STATS
725 temp = scnprintf(next, size,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200726 "irq normal %ld err %ld iaa %ld(lost %ld)\n",
727 fotg210->stats.normal, fotg210->stats.error,
728 fotg210->stats.iaa, fotg210->stats.lost_iaa);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000729 size -= temp;
730 next += temp;
731
732 temp = scnprintf(next, size, "complete %ld unlink %ld\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200733 fotg210->stats.complete, fotg210->stats.unlink);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000734 size -= temp;
735 next += temp;
736#endif
737
738done:
739 spin_unlock_irqrestore(&fotg210->lock, flags);
740
741 return buf->alloc_size - size;
742}
743
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200744static struct debug_buffer
745*alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000746{
747 struct debug_buffer *buf;
748
749 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
750
751 if (buf) {
752 buf->bus = bus;
753 buf->fill_func = fill_func;
754 mutex_init(&buf->mutex);
755 buf->alloc_size = PAGE_SIZE;
756 }
757
758 return buf;
759}
760
761static int fill_buffer(struct debug_buffer *buf)
762{
763 int ret = 0;
764
765 if (!buf->output_buf)
766 buf->output_buf = vmalloc(buf->alloc_size);
767
768 if (!buf->output_buf) {
769 ret = -ENOMEM;
770 goto out;
771 }
772
773 ret = buf->fill_func(buf);
774
775 if (ret >= 0) {
776 buf->count = ret;
777 ret = 0;
778 }
779
780out:
781 return ret;
782}
783
784static ssize_t debug_output(struct file *file, char __user *user_buf,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200785 size_t len, loff_t *offset)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000786{
787 struct debug_buffer *buf = file->private_data;
788 int ret = 0;
789
790 mutex_lock(&buf->mutex);
791 if (buf->count == 0) {
792 ret = fill_buffer(buf);
793 if (ret != 0) {
794 mutex_unlock(&buf->mutex);
795 goto out;
796 }
797 }
798 mutex_unlock(&buf->mutex);
799
800 ret = simple_read_from_buffer(user_buf, len, offset,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200801 buf->output_buf, buf->count);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000802
803out:
804 return ret;
805
806}
807
808static int debug_close(struct inode *inode, struct file *file)
809{
810 struct debug_buffer *buf = file->private_data;
811
812 if (buf) {
813 vfree(buf->output_buf);
814 kfree(buf);
815 }
816
817 return 0;
818}
819static int debug_async_open(struct inode *inode, struct file *file)
820{
821 file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
822
823 return file->private_data ? 0 : -ENOMEM;
824}
825
826static int debug_periodic_open(struct inode *inode, struct file *file)
827{
828 struct debug_buffer *buf;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200829
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000830 buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
831 if (!buf)
832 return -ENOMEM;
833
834 buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
835 file->private_data = buf;
836 return 0;
837}
838
839static int debug_registers_open(struct inode *inode, struct file *file)
840{
841 file->private_data = alloc_buffer(inode->i_private,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200842 fill_registers_buffer);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000843
844 return file->private_data ? 0 : -ENOMEM;
845}
846
847static inline void create_debug_files(struct fotg210_hcd *fotg210)
848{
849 struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
Greg Kroah-Hartman9b105162018-05-29 17:30:55 +0200850 struct dentry *root;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000851
Greg Kroah-Hartman9b105162018-05-29 17:30:55 +0200852 root = debugfs_create_dir(bus->bus_name, fotg210_debug_root);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000853
Greg Kroah-Hartman9b105162018-05-29 17:30:55 +0200854 debugfs_create_file("async", S_IRUGO, root, bus, &debug_async_fops);
855 debugfs_create_file("periodic", S_IRUGO, root, bus,
856 &debug_periodic_fops);
857 debugfs_create_file("registers", S_IRUGO, root, bus,
858 &debug_registers_fops);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000859}
860
861static inline void remove_debug_files(struct fotg210_hcd *fotg210)
862{
Greg Kroah-Hartman0f602032021-05-25 19:25:34 +0200863 struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
864
865 debugfs_remove(debugfs_lookup(bus->bus_name, fotg210_debug_root));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000866}
867
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200868/* handshake - spin reading hc until handshake completes or fails
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000869 * @ptr: address of hc register to be read
870 * @mask: bits to look at in result of read
871 * @done: value of those bits when handshake succeeds
872 * @usec: timeout in microseconds
873 *
874 * Returns negative errno, or zero on success
875 *
876 * Success happens when the "mask" bits have the specified value (hardware
877 * handshake done). There are two failure modes: "usec" have passed (major
878 * hardware flakeout), or the register reads as all-ones (hardware removed).
879 *
880 * That last failure should_only happen in cases like physical cardbus eject
881 * before driver shutdown. But it also seems to be caused by bugs in cardbus
882 * bridge shutdown: shutting down the bridge before the devices using it.
883 */
884static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200885 u32 mask, u32 done, int usec)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000886{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200887 u32 result;
Chunfeng Yune7d82632020-09-21 14:13:30 +0800888 int ret;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000889
Chunfeng Yune7d82632020-09-21 14:13:30 +0800890 ret = readl_poll_timeout_atomic(ptr, result,
891 ((result & mask) == done ||
892 result == U32_MAX), 1, usec);
893 if (result == U32_MAX) /* card removed */
894 return -ENODEV;
895
896 return ret;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000897}
898
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200899/* Force HC to halt state from unknown (EHCI spec section 2.3).
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000900 * Must be called with interrupts enabled and the lock not held.
901 */
902static int fotg210_halt(struct fotg210_hcd *fotg210)
903{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200904 u32 temp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000905
906 spin_lock_irq(&fotg210->lock);
907
908 /* disable any irqs left enabled by previous code */
909 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
910
911 /*
912 * This routine gets called during probe before fotg210->command
913 * has been initialized, so we can't rely on its value.
914 */
915 fotg210->command &= ~CMD_RUN;
916 temp = fotg210_readl(fotg210, &fotg210->regs->command);
917 temp &= ~(CMD_RUN | CMD_IAAD);
918 fotg210_writel(fotg210, temp, &fotg210->regs->command);
919
920 spin_unlock_irq(&fotg210->lock);
921 synchronize_irq(fotg210_to_hcd(fotg210)->irq);
922
923 return handshake(fotg210, &fotg210->regs->status,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200924 STS_HALT, STS_HALT, 16 * 125);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000925}
926
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200927/* Reset a non-running (STS_HALT == 1) controller.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000928 * Must be called with interrupts enabled and the lock not held.
929 */
930static int fotg210_reset(struct fotg210_hcd *fotg210)
931{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200932 int retval;
933 u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000934
935 /* If the EHCI debug controller is active, special care must be
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200936 * taken before and after a host controller reset
937 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000938 if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
939 fotg210->debug = NULL;
940
941 command |= CMD_RESET;
942 dbg_cmd(fotg210, "reset", command);
943 fotg210_writel(fotg210, command, &fotg210->regs->command);
944 fotg210->rh_state = FOTG210_RH_HALTED;
945 fotg210->next_statechange = jiffies;
946 retval = handshake(fotg210, &fotg210->regs->command,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200947 CMD_RESET, 0, 250 * 1000);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000948
949 if (retval)
950 return retval;
951
952 if (fotg210->debug)
953 dbgp_external_startup(fotg210_to_hcd(fotg210));
954
955 fotg210->port_c_suspend = fotg210->suspended_ports =
956 fotg210->resuming_ports = 0;
957 return retval;
958}
959
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200960/* Idle the controller (turn off the schedules).
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000961 * Must be called with interrupts enabled and the lock not held.
962 */
963static void fotg210_quiesce(struct fotg210_hcd *fotg210)
964{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200965 u32 temp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000966
967 if (fotg210->rh_state != FOTG210_RH_RUNNING)
968 return;
969
970 /* wait for any schedule enables/disables to take effect */
971 temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
972 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200973 16 * 125);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000974
975 /* then disable anything that's still active */
976 spin_lock_irq(&fotg210->lock);
977 fotg210->command &= ~(CMD_ASE | CMD_PSE);
978 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
979 spin_unlock_irq(&fotg210->lock);
980
981 /* hardware can take 16 microframes to turn off ... */
982 handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +0200983 16 * 125);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000984}
985
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000986static void end_unlink_async(struct fotg210_hcd *fotg210);
987static void unlink_empty_async(struct fotg210_hcd *fotg210);
988static void fotg210_work(struct fotg210_hcd *fotg210);
989static void start_unlink_intr(struct fotg210_hcd *fotg210,
990 struct fotg210_qh *qh);
991static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
992
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +0000993/* Set a bit in the USBCMD register */
994static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
995{
996 fotg210->command |= bit;
997 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
998
999 /* unblock posted write */
1000 fotg210_readl(fotg210, &fotg210->regs->command);
1001}
1002
1003/* Clear a bit in the USBCMD register */
1004static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
1005{
1006 fotg210->command &= ~bit;
1007 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1008
1009 /* unblock posted write */
1010 fotg210_readl(fotg210, &fotg210->regs->command);
1011}
1012
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001013/* EHCI timer support... Now using hrtimers.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001014 *
1015 * Lots of different events are triggered from fotg210->hrtimer. Whenever
1016 * the timer routine runs, it checks each possible event; events that are
1017 * currently enabled and whose expiration time has passed get handled.
1018 * The set of enabled events is stored as a collection of bitflags in
1019 * fotg210->enabled_hrtimer_events, and they are numbered in order of
1020 * increasing delay values (ranging between 1 ms and 100 ms).
1021 *
1022 * Rather than implementing a sorted list or tree of all pending events,
1023 * we keep track only of the lowest-numbered pending event, in
1024 * fotg210->next_hrtimer_event. Whenever fotg210->hrtimer gets restarted, its
1025 * expiration time is set to the timeout value for this event.
1026 *
1027 * As a result, events might not get handled right away; the actual delay
1028 * could be anywhere up to twice the requested delay. This doesn't
1029 * matter, because none of the events are especially time-critical. The
1030 * ones that matter most all have a delay of 1 ms, so they will be
1031 * handled after 2 ms at most, which is okay. In addition to this, we
1032 * allow for an expiration range of 1 ms.
1033 */
1034
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001035/* Delay lengths for the hrtimer event types.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001036 * Keep this list sorted by delay length, in the same order as
1037 * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
1038 */
1039static unsigned event_delays_ns[] = {
1040 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_ASS */
1041 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_PSS */
1042 1 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_POLL_DEAD */
1043 1125 * NSEC_PER_USEC, /* FOTG210_HRTIMER_UNLINK_INTR */
1044 2 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_FREE_ITDS */
1045 6 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1046 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1047 10 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1048 15 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1049 100 * NSEC_PER_MSEC, /* FOTG210_HRTIMER_IO_WATCHDOG */
1050};
1051
1052/* Enable a pending hrtimer event */
1053static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
1054 bool resched)
1055{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001056 ktime_t *timeout = &fotg210->hr_timeouts[event];
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001057
1058 if (resched)
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001059 *timeout = ktime_add(ktime_get(), event_delays_ns[event]);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001060 fotg210->enabled_hrtimer_events |= (1 << event);
1061
1062 /* Track only the lowest-numbered pending event */
1063 if (event < fotg210->next_hrtimer_event) {
1064 fotg210->next_hrtimer_event = event;
1065 hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
1066 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
1067 }
1068}
1069
1070
1071/* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
1072static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
1073{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001074 unsigned actual, want;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001075
1076 /* Don't enable anything if the controller isn't running (e.g., died) */
1077 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1078 return;
1079
1080 want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
1081 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;
1082
1083 if (want != actual) {
1084
1085 /* Poll again later, but give up after about 20 ms */
1086 if (fotg210->ASS_poll_count++ < 20) {
1087 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001088 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001089 return;
1090 }
1091 fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
1092 want, actual);
1093 }
1094 fotg210->ASS_poll_count = 0;
1095
1096 /* The status is up-to-date; restart or stop the schedule as needed */
1097 if (want == 0) { /* Stopped */
1098 if (fotg210->async_count > 0)
1099 fotg210_set_command_bit(fotg210, CMD_ASE);
1100
1101 } else { /* Running */
1102 if (fotg210->async_count == 0) {
1103
1104 /* Turn off the schedule after a while */
1105 fotg210_enable_event(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001106 FOTG210_HRTIMER_DISABLE_ASYNC,
1107 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001108 }
1109 }
1110}
1111
1112/* Turn off the async schedule after a brief delay */
1113static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
1114{
1115 fotg210_clear_command_bit(fotg210, CMD_ASE);
1116}
1117
1118
1119/* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
1120static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
1121{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001122 unsigned actual, want;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001123
1124 /* Don't do anything if the controller isn't running (e.g., died) */
1125 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1126 return;
1127
1128 want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
1129 actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;
1130
1131 if (want != actual) {
1132
1133 /* Poll again later, but give up after about 20 ms */
1134 if (fotg210->PSS_poll_count++ < 20) {
1135 fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001136 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001137 return;
1138 }
1139 fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
1140 want, actual);
1141 }
1142 fotg210->PSS_poll_count = 0;
1143
1144 /* The status is up-to-date; restart or stop the schedule as needed */
1145 if (want == 0) { /* Stopped */
1146 if (fotg210->periodic_count > 0)
1147 fotg210_set_command_bit(fotg210, CMD_PSE);
1148
1149 } else { /* Running */
1150 if (fotg210->periodic_count == 0) {
1151
1152 /* Turn off the schedule after a while */
1153 fotg210_enable_event(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001154 FOTG210_HRTIMER_DISABLE_PERIODIC,
1155 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001156 }
1157 }
1158}
1159
1160/* Turn off the periodic schedule after a brief delay */
1161static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
1162{
1163 fotg210_clear_command_bit(fotg210, CMD_PSE);
1164}
1165
1166
1167/* Poll the STS_HALT status bit; see when a dead controller stops */
1168static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
1169{
1170 if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {
1171
1172 /* Give up after a few milliseconds */
1173 if (fotg210->died_poll_count++ < 5) {
1174 /* Try again later */
1175 fotg210_enable_event(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001176 FOTG210_HRTIMER_POLL_DEAD, true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001177 return;
1178 }
1179 fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
1180 }
1181
1182 /* Clean up the mess */
1183 fotg210->rh_state = FOTG210_RH_HALTED;
1184 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
1185 fotg210_work(fotg210);
1186 end_unlink_async(fotg210);
1187
1188 /* Not in process context, so don't try to reset the controller */
1189}
1190
1191
1192/* Handle unlinked interrupt QHs once they are gone from the hardware */
1193static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
1194{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001195 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001196
1197 /*
1198 * Process all the QHs on the intr_unlink list that were added
1199 * before the current unlink cycle began. The list is in
1200 * temporal order, so stop when we reach the first entry in the
1201 * current cycle. But if the root hub isn't running then
1202 * process all the QHs on the list.
1203 */
1204 fotg210->intr_unlinking = true;
1205 while (fotg210->intr_unlink) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001206 struct fotg210_qh *qh = fotg210->intr_unlink;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001207
1208 if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
1209 break;
1210 fotg210->intr_unlink = qh->unlink_next;
1211 qh->unlink_next = NULL;
1212 end_unlink_intr(fotg210, qh);
1213 }
1214
1215 /* Handle remaining entries later */
1216 if (fotg210->intr_unlink) {
1217 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001218 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001219 ++fotg210->intr_unlink_cycle;
1220 }
1221 fotg210->intr_unlinking = false;
1222}
1223
1224
1225/* Start another free-iTDs/siTDs cycle */
1226static void start_free_itds(struct fotg210_hcd *fotg210)
1227{
1228 if (!(fotg210->enabled_hrtimer_events &
1229 BIT(FOTG210_HRTIMER_FREE_ITDS))) {
1230 fotg210->last_itd_to_free = list_entry(
1231 fotg210->cached_itd_list.prev,
1232 struct fotg210_itd, itd_list);
1233 fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
1234 }
1235}
1236
1237/* Wait for controller to stop using old iTDs and siTDs */
1238static void end_free_itds(struct fotg210_hcd *fotg210)
1239{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001240 struct fotg210_itd *itd, *n;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001241
1242 if (fotg210->rh_state < FOTG210_RH_RUNNING)
1243 fotg210->last_itd_to_free = NULL;
1244
1245 list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
1246 list_del(&itd->itd_list);
1247 dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
1248 if (itd == fotg210->last_itd_to_free)
1249 break;
1250 }
1251
1252 if (!list_empty(&fotg210->cached_itd_list))
1253 start_free_itds(fotg210);
1254}
1255
1256
1257/* Handle lost (or very late) IAA interrupts */
1258static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
1259{
1260 if (fotg210->rh_state != FOTG210_RH_RUNNING)
1261 return;
1262
1263 /*
1264 * Lost IAA irqs wedge things badly; seen first with a vt8235.
1265 * So we need this watchdog, but must protect it against both
1266 * (a) SMP races against real IAA firing and retriggering, and
1267 * (b) clean HC shutdown, when IAA watchdog was pending.
1268 */
1269 if (fotg210->async_iaa) {
1270 u32 cmd, status;
1271
1272 /* If we get here, IAA is *REALLY* late. It's barely
1273 * conceivable that the system is so busy that CMD_IAAD
1274 * is still legitimately set, so let's be sure it's
1275 * clear before we read STS_IAA. (The HC should clear
1276 * CMD_IAAD when it sets STS_IAA.)
1277 */
1278 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
1279
1280 /*
1281 * If IAA is set here it either legitimately triggered
1282 * after the watchdog timer expired (_way_ late, so we'll
1283 * still count it as lost) ... or a silicon erratum:
1284 * - VIA seems to set IAA without triggering the IRQ;
1285 * - IAAD potentially cleared without setting IAA.
1286 */
1287 status = fotg210_readl(fotg210, &fotg210->regs->status);
1288 if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
Corentin Labbea0ef2bd2018-09-19 19:48:53 +00001289 INCR(fotg210->stats.lost_iaa);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001290 fotg210_writel(fotg210, STS_IAA,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001291 &fotg210->regs->status);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001292 }
1293
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01001294 fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001295 status, cmd);
1296 end_unlink_async(fotg210);
1297 }
1298}
1299
1300
1301/* Enable the I/O watchdog, if appropriate */
1302static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
1303{
1304 /* Not needed if the controller isn't running or it's already enabled */
1305 if (fotg210->rh_state != FOTG210_RH_RUNNING ||
1306 (fotg210->enabled_hrtimer_events &
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001307 BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001308 return;
1309
1310 /*
1311 * Isochronous transfers always need the watchdog.
1312 * For other sorts we use it only if the flag is set.
1313 */
1314 if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog &&
1315 fotg210->async_count + fotg210->intr_count > 0))
1316 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001317 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001318}
1319
1320
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001321/* Handler functions for the hrtimer event types.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001322 * Keep this array in the same order as the event types indexed by
1323 * enum fotg210_hrtimer_event in fotg210.h.
1324 */
1325static void (*event_handlers[])(struct fotg210_hcd *) = {
1326 fotg210_poll_ASS, /* FOTG210_HRTIMER_POLL_ASS */
1327 fotg210_poll_PSS, /* FOTG210_HRTIMER_POLL_PSS */
1328 fotg210_handle_controller_death, /* FOTG210_HRTIMER_POLL_DEAD */
1329 fotg210_handle_intr_unlinks, /* FOTG210_HRTIMER_UNLINK_INTR */
1330 end_free_itds, /* FOTG210_HRTIMER_FREE_ITDS */
1331 unlink_empty_async, /* FOTG210_HRTIMER_ASYNC_UNLINKS */
1332 fotg210_iaa_watchdog, /* FOTG210_HRTIMER_IAA_WATCHDOG */
1333 fotg210_disable_PSE, /* FOTG210_HRTIMER_DISABLE_PERIODIC */
1334 fotg210_disable_ASE, /* FOTG210_HRTIMER_DISABLE_ASYNC */
1335 fotg210_work, /* FOTG210_HRTIMER_IO_WATCHDOG */
1336};
1337
1338static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
1339{
1340 struct fotg210_hcd *fotg210 =
1341 container_of(t, struct fotg210_hcd, hrtimer);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001342 ktime_t now;
1343 unsigned long events;
1344 unsigned long flags;
1345 unsigned e;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001346
1347 spin_lock_irqsave(&fotg210->lock, flags);
1348
1349 events = fotg210->enabled_hrtimer_events;
1350 fotg210->enabled_hrtimer_events = 0;
1351 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
1352
1353 /*
1354 * Check each pending event. If its time has expired, handle
1355 * the event; otherwise re-enable it.
1356 */
1357 now = ktime_get();
1358 for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) {
Mariusz Skamrafa72e6a2017-05-26 12:15:59 +02001359 if (ktime_compare(now, fotg210->hr_timeouts[e]) >= 0)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001360 event_handlers[e](fotg210);
1361 else
1362 fotg210_enable_event(fotg210, e, false);
1363 }
1364
1365 spin_unlock_irqrestore(&fotg210->lock, flags);
1366 return HRTIMER_NORESTART;
1367}
1368
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001369#define fotg210_bus_suspend NULL
1370#define fotg210_bus_resume NULL
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001371
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001372static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
1373 u32 __iomem *status_reg, int port_status)
1374{
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001375 if (!(port_status & PORT_CONNECT))
1376 return port_status;
1377
1378 /* if reset finished and it's still not enabled -- handoff */
Peter Senna Tschudinffa23662015-10-17 21:28:45 +02001379 if (!(port_status & PORT_PE))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001380 /* with integrated TT, there's nobody to hand it to! */
Peter Senna Tschudinffa23662015-10-17 21:28:45 +02001381 fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001382 index + 1);
Peter Senna Tschudinffa23662015-10-17 21:28:45 +02001383 else
1384 fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
1385 index + 1);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001386
1387 return port_status;
1388}
1389
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001390
1391/* build "status change" packet (one or two bytes) from HC registers */
1392
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001393static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001394{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001395 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
1396 u32 temp, status;
1397 u32 mask;
1398 int retval = 1;
1399 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001400
1401 /* init status to no-changes */
1402 buf[0] = 0;
1403
1404 /* Inform the core about resumes-in-progress by returning
1405 * a non-zero value even if there are no status changes.
1406 */
1407 status = fotg210->resuming_ports;
1408
1409 mask = PORT_CSC | PORT_PEC;
1410 /* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */
1411
1412 /* no hub change reports (bit 0) for now (power, ...) */
1413
1414 /* port N changes (bit N)? */
1415 spin_lock_irqsave(&fotg210->lock, flags);
1416
1417 temp = fotg210_readl(fotg210, &fotg210->regs->port_status);
1418
1419 /*
1420 * Return status information even for ports with OWNER set.
Petr Mladek37ebb542014-09-19 17:32:23 +02001421 * Otherwise hub_wq wouldn't see the disconnect event when a
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001422 * high-speed device is switched over to the companion
1423 * controller by the user.
1424 */
1425
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001426 if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend) ||
1427 (fotg210->reset_done[0] &&
1428 time_after_eq(jiffies, fotg210->reset_done[0]))) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001429 buf[0] |= 1 << 1;
1430 status = STS_PCD;
1431 }
1432 /* FIXME autosuspend idle root hubs */
1433 spin_unlock_irqrestore(&fotg210->lock, flags);
1434 return status ? retval : 0;
1435}
1436
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001437static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210,
1438 struct usb_hub_descriptor *desc)
1439{
1440 int ports = HCS_N_PORTS(fotg210->hcs_params);
1441 u16 temp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001442
Sergei Shtylyov4631f4e2015-03-29 01:06:21 +03001443 desc->bDescriptorType = USB_DT_HUB;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001444 desc->bPwrOn2PwrGood = 10; /* fotg210 1.0, 2.3.9 says 20ms max */
1445 desc->bHubContrCurrent = 0;
1446
1447 desc->bNbrPorts = ports;
1448 temp = 1 + (ports / 8);
1449 desc->bDescLength = 7 + 2 * temp;
1450
1451 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
1452 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
1453 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
1454
Sergei Shtylyov7538bd62015-01-19 01:29:03 +03001455 temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */
1456 temp |= HUB_CHAR_NO_LPSM; /* no power switching */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001457 desc->wHubCharacteristics = cpu_to_le16(temp);
1458}
1459
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001460static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1461 u16 wIndex, char *buf, u16 wLength)
1462{
1463 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
1464 int ports = HCS_N_PORTS(fotg210->hcs_params);
1465 u32 __iomem *status_reg = &fotg210->regs->port_status;
1466 u32 temp, temp1, status;
1467 unsigned long flags;
1468 int retval = 0;
1469 unsigned selector;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001470
1471 /*
1472 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
1473 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
1474 * (track current state ourselves) ... blink for diagnostics,
1475 * power, "this is the one", etc. EHCI spec supports this.
1476 */
1477
1478 spin_lock_irqsave(&fotg210->lock, flags);
1479 switch (typeReq) {
1480 case ClearHubFeature:
1481 switch (wValue) {
1482 case C_HUB_LOCAL_POWER:
1483 case C_HUB_OVER_CURRENT:
1484 /* no hub-wide feature/status flags */
1485 break;
1486 default:
1487 goto error;
1488 }
1489 break;
1490 case ClearPortFeature:
1491 if (!wIndex || wIndex > ports)
1492 goto error;
1493 wIndex--;
1494 temp = fotg210_readl(fotg210, status_reg);
1495 temp &= ~PORT_RWC_BITS;
1496
1497 /*
1498 * Even if OWNER is set, so the port is owned by the
Petr Mladek37ebb542014-09-19 17:32:23 +02001499 * companion controller, hub_wq needs to be able to clear
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001500 * the port-change status bits (especially
1501 * USB_PORT_STAT_C_CONNECTION).
1502 */
1503
1504 switch (wValue) {
1505 case USB_PORT_FEAT_ENABLE:
1506 fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
1507 break;
1508 case USB_PORT_FEAT_C_ENABLE:
1509 fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
1510 break;
1511 case USB_PORT_FEAT_SUSPEND:
1512 if (temp & PORT_RESET)
1513 goto error;
1514 if (!(temp & PORT_SUSPEND))
1515 break;
1516 if ((temp & PORT_PE) == 0)
1517 goto error;
1518
1519 /* resume signaling for 20 msec */
1520 fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
1521 fotg210->reset_done[wIndex] = jiffies
Felipe Balbi7e136bb2015-02-13 14:54:38 -06001522 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001523 break;
1524 case USB_PORT_FEAT_C_SUSPEND:
1525 clear_bit(wIndex, &fotg210->port_c_suspend);
1526 break;
1527 case USB_PORT_FEAT_C_CONNECTION:
1528 fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
1529 break;
1530 case USB_PORT_FEAT_C_OVER_CURRENT:
1531 fotg210_writel(fotg210, temp | OTGISR_OVC,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001532 &fotg210->regs->otgisr);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001533 break;
1534 case USB_PORT_FEAT_C_RESET:
1535 /* GetPortStatus clears reset */
1536 break;
1537 default:
1538 goto error;
1539 }
1540 fotg210_readl(fotg210, &fotg210->regs->command);
1541 break;
1542 case GetHubDescriptor:
1543 fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001544 buf);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001545 break;
1546 case GetHubStatus:
1547 /* no hub-wide feature/status flags */
1548 memset(buf, 0, 4);
1549 /*cpu_to_le32s ((u32 *) buf); */
1550 break;
1551 case GetPortStatus:
1552 if (!wIndex || wIndex > ports)
1553 goto error;
1554 wIndex--;
1555 status = 0;
1556 temp = fotg210_readl(fotg210, status_reg);
1557
1558 /* wPortChange bits */
1559 if (temp & PORT_CSC)
1560 status |= USB_PORT_STAT_C_CONNECTION << 16;
1561 if (temp & PORT_PEC)
1562 status |= USB_PORT_STAT_C_ENABLE << 16;
1563
1564 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
1565 if (temp1 & OTGISR_OVC)
1566 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1567
1568 /* whoever resumes must GetPortStatus to complete it!! */
1569 if (temp & PORT_RESUME) {
1570
1571 /* Remote Wakeup received? */
1572 if (!fotg210->reset_done[wIndex]) {
1573 /* resume signaling for 20 msec */
1574 fotg210->reset_done[wIndex] = jiffies
1575 + msecs_to_jiffies(20);
1576 /* check the port again */
1577 mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
1578 fotg210->reset_done[wIndex]);
1579 }
1580
1581 /* resume completed? */
1582 else if (time_after_eq(jiffies,
1583 fotg210->reset_done[wIndex])) {
1584 clear_bit(wIndex, &fotg210->suspended_ports);
1585 set_bit(wIndex, &fotg210->port_c_suspend);
1586 fotg210->reset_done[wIndex] = 0;
1587
1588 /* stop resume signaling */
1589 temp = fotg210_readl(fotg210, status_reg);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001590 fotg210_writel(fotg210, temp &
1591 ~(PORT_RWC_BITS | PORT_RESUME),
1592 status_reg);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001593 clear_bit(wIndex, &fotg210->resuming_ports);
1594 retval = handshake(fotg210, status_reg,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001595 PORT_RESUME, 0, 2000);/* 2ms */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001596 if (retval != 0) {
1597 fotg210_err(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001598 "port %d resume error %d\n",
1599 wIndex + 1, retval);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001600 goto error;
1601 }
1602 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1603 }
1604 }
1605
1606 /* whoever resets must GetPortStatus to complete it!! */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001607 if ((temp & PORT_RESET) && time_after_eq(jiffies,
1608 fotg210->reset_done[wIndex])) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001609 status |= USB_PORT_STAT_C_RESET << 16;
1610 fotg210->reset_done[wIndex] = 0;
1611 clear_bit(wIndex, &fotg210->resuming_ports);
1612
1613 /* force reset to complete */
1614 fotg210_writel(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001615 temp & ~(PORT_RWC_BITS | PORT_RESET),
1616 status_reg);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001617 /* REVISIT: some hardware needs 550+ usec to clear
1618 * this bit; seems too long to spin routinely...
1619 */
1620 retval = handshake(fotg210, status_reg,
1621 PORT_RESET, 0, 1000);
1622 if (retval != 0) {
1623 fotg210_err(fotg210, "port %d reset error %d\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001624 wIndex + 1, retval);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001625 goto error;
1626 }
1627
1628 /* see what we found out */
1629 temp = check_reset_complete(fotg210, wIndex, status_reg,
1630 fotg210_readl(fotg210, status_reg));
Hans Ulli Kroll77775882019-08-10 17:04:58 +02001631
1632 /* restart schedule */
1633 fotg210->command |= CMD_RUN;
1634 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001635 }
1636
1637 if (!(temp & (PORT_RESUME|PORT_RESET))) {
1638 fotg210->reset_done[wIndex] = 0;
1639 clear_bit(wIndex, &fotg210->resuming_ports);
1640 }
1641
1642 /* transfer dedicated ports to the companion hc */
1643 if ((temp & PORT_CONNECT) &&
1644 test_bit(wIndex, &fotg210->companion_ports)) {
1645 temp &= ~PORT_RWC_BITS;
1646 fotg210_writel(fotg210, temp, status_reg);
1647 fotg210_dbg(fotg210, "port %d --> companion\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001648 wIndex + 1);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001649 temp = fotg210_readl(fotg210, status_reg);
1650 }
1651
1652 /*
Petr Mladek37ebb542014-09-19 17:32:23 +02001653 * Even if OWNER is set, there's no harm letting hub_wq
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001654 * see the wPortStatus values (they should all be 0 except
1655 * for PORT_POWER anyway).
1656 */
1657
1658 if (temp & PORT_CONNECT) {
1659 status |= USB_PORT_STAT_CONNECTION;
1660 status |= fotg210_port_speed(fotg210, temp);
1661 }
1662 if (temp & PORT_PE)
1663 status |= USB_PORT_STAT_ENABLE;
1664
1665 /* maybe the port was unsuspended without our knowledge */
1666 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1667 status |= USB_PORT_STAT_SUSPEND;
1668 } else if (test_bit(wIndex, &fotg210->suspended_ports)) {
1669 clear_bit(wIndex, &fotg210->suspended_ports);
1670 clear_bit(wIndex, &fotg210->resuming_ports);
1671 fotg210->reset_done[wIndex] = 0;
1672 if (temp & PORT_PE)
1673 set_bit(wIndex, &fotg210->port_c_suspend);
1674 }
1675
1676 temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
1677 if (temp1 & OTGISR_OVC)
1678 status |= USB_PORT_STAT_OVERCURRENT;
1679 if (temp & PORT_RESET)
1680 status |= USB_PORT_STAT_RESET;
1681 if (test_bit(wIndex, &fotg210->port_c_suspend))
1682 status |= USB_PORT_STAT_C_SUSPEND << 16;
1683
Oliver Neukum3b707ec2013-11-18 13:23:06 +01001684 if (status & ~0xffff) /* only if wPortChange is interesting */
1685 dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001686 put_unaligned_le32(status, buf);
1687 break;
1688 case SetHubFeature:
1689 switch (wValue) {
1690 case C_HUB_LOCAL_POWER:
1691 case C_HUB_OVER_CURRENT:
1692 /* no hub-wide feature/status flags */
1693 break;
1694 default:
1695 goto error;
1696 }
1697 break;
1698 case SetPortFeature:
1699 selector = wIndex >> 8;
1700 wIndex &= 0xff;
1701
1702 if (!wIndex || wIndex > ports)
1703 goto error;
1704 wIndex--;
1705 temp = fotg210_readl(fotg210, status_reg);
1706 temp &= ~PORT_RWC_BITS;
1707 switch (wValue) {
1708 case USB_PORT_FEAT_SUSPEND:
1709 if ((temp & PORT_PE) == 0
1710 || (temp & PORT_RESET) != 0)
1711 goto error;
1712
1713 /* After above check the port must be connected.
1714 * Set appropriate bit thus could put phy into low power
1715 * mode if we have hostpc feature
1716 */
1717 fotg210_writel(fotg210, temp | PORT_SUSPEND,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001718 status_reg);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001719 set_bit(wIndex, &fotg210->suspended_ports);
1720 break;
1721 case USB_PORT_FEAT_RESET:
1722 if (temp & PORT_RESUME)
1723 goto error;
1724 /* line status bits may report this as low speed,
1725 * which can be fine if this root hub has a
1726 * transaction translator built in.
1727 */
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01001728 fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001729 temp |= PORT_RESET;
1730 temp &= ~PORT_PE;
1731
1732 /*
1733 * caller must wait, then call GetPortStatus
1734 * usb 2.0 spec says 50 ms resets on root
1735 */
1736 fotg210->reset_done[wIndex] = jiffies
1737 + msecs_to_jiffies(50);
1738 fotg210_writel(fotg210, temp, status_reg);
1739 break;
1740
1741 /* For downstream facing ports (these): one hub port is put
1742 * into test mode according to USB2 11.24.2.13, then the hub
1743 * must be reset (which for root hub now means rmmod+modprobe,
1744 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1745 * about the EHCI-specific stuff.
1746 */
1747 case USB_PORT_FEAT_TEST:
1748 if (!selector || selector > 5)
1749 goto error;
1750 spin_unlock_irqrestore(&fotg210->lock, flags);
1751 fotg210_quiesce(fotg210);
1752 spin_lock_irqsave(&fotg210->lock, flags);
1753
1754 /* Put all enabled ports into suspend */
1755 temp = fotg210_readl(fotg210, status_reg) &
1756 ~PORT_RWC_BITS;
1757 if (temp & PORT_PE)
1758 fotg210_writel(fotg210, temp | PORT_SUSPEND,
1759 status_reg);
1760
1761 spin_unlock_irqrestore(&fotg210->lock, flags);
1762 fotg210_halt(fotg210);
1763 spin_lock_irqsave(&fotg210->lock, flags);
1764
1765 temp = fotg210_readl(fotg210, status_reg);
1766 temp |= selector << 16;
1767 fotg210_writel(fotg210, temp, status_reg);
1768 break;
1769
1770 default:
1771 goto error;
1772 }
1773 fotg210_readl(fotg210, &fotg210->regs->command);
1774 break;
1775
1776 default:
1777error:
1778 /* "stall" on error */
1779 retval = -EPIPE;
1780 }
1781 spin_unlock_irqrestore(&fotg210->lock, flags);
1782 return retval;
1783}
1784
1785static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
1786 int portnum)
1787{
1788 return;
1789}
1790
1791static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
1792 int portnum)
1793{
1794 return 0;
1795}
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001796
1797/* There's basically three types of memory:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001798 * - data used only by the HCD ... kmalloc is fine
1799 * - async and periodic schedules, shared by HC and HCD ... these
1800 * need to use dma_pool or dma_alloc_coherent
1801 * - driver buffers, read/written by HC ... single shot DMA mapped
1802 *
1803 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
1804 * No memory seen by this driver is pageable.
1805 */
1806
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001807/* Allocate the key transfer structures from the previously allocated pool */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001808static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001809 struct fotg210_qtd *qtd, dma_addr_t dma)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001810{
1811 memset(qtd, 0, sizeof(*qtd));
1812 qtd->qtd_dma = dma;
1813 qtd->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
1814 qtd->hw_next = FOTG210_LIST_END(fotg210);
1815 qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
1816 INIT_LIST_HEAD(&qtd->qtd_list);
1817}
1818
1819static struct fotg210_qtd *fotg210_qtd_alloc(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001820 gfp_t flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001821{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001822 struct fotg210_qtd *qtd;
1823 dma_addr_t dma;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001824
1825 qtd = dma_pool_alloc(fotg210->qtd_pool, flags, &dma);
1826 if (qtd != NULL)
1827 fotg210_qtd_init(fotg210, qtd, dma);
1828
1829 return qtd;
1830}
1831
1832static inline void fotg210_qtd_free(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001833 struct fotg210_qtd *qtd)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001834{
1835 dma_pool_free(fotg210->qtd_pool, qtd, qtd->qtd_dma);
1836}
1837
1838
1839static void qh_destroy(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
1840{
1841 /* clean qtds first, and know this is not linked */
1842 if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
1843 fotg210_dbg(fotg210, "unused qh not empty!\n");
1844 BUG();
1845 }
1846 if (qh->dummy)
1847 fotg210_qtd_free(fotg210, qh->dummy);
1848 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1849 kfree(qh);
1850}
1851
1852static struct fotg210_qh *fotg210_qh_alloc(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001853 gfp_t flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001854{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001855 struct fotg210_qh *qh;
1856 dma_addr_t dma;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001857
1858 qh = kzalloc(sizeof(*qh), GFP_ATOMIC);
1859 if (!qh)
1860 goto done;
Kelly Devillivc4c1faf2021-06-27 20:57:45 +08001861 qh->hw = (struct fotg210_qh_hw *)
Cai Huoqing81dddf72021-10-18 21:17:08 +08001862 dma_pool_zalloc(fotg210->qh_pool, flags, &dma);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001863 if (!qh->hw)
1864 goto fail;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001865 qh->qh_dma = dma;
1866 INIT_LIST_HEAD(&qh->qtd_list);
1867
1868 /* dummy td enables safe urb queuing */
1869 qh->dummy = fotg210_qtd_alloc(fotg210, flags);
1870 if (qh->dummy == NULL) {
1871 fotg210_dbg(fotg210, "no dummy td\n");
1872 goto fail1;
1873 }
1874done:
1875 return qh;
1876fail1:
1877 dma_pool_free(fotg210->qh_pool, qh->hw, qh->qh_dma);
1878fail:
1879 kfree(qh);
1880 return NULL;
1881}
1882
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001883/* The queue heads and transfer descriptors are managed from pools tied
1884 * to each of the "per device" structures.
1885 * This is the initialisation and cleanup code.
1886 */
1887
1888static void fotg210_mem_cleanup(struct fotg210_hcd *fotg210)
1889{
1890 if (fotg210->async)
1891 qh_destroy(fotg210, fotg210->async);
1892 fotg210->async = NULL;
1893
1894 if (fotg210->dummy)
1895 qh_destroy(fotg210, fotg210->dummy);
1896 fotg210->dummy = NULL;
1897
1898 /* DMA consistent memory and pools */
Julia Lawalla4c1f0c2015-09-13 14:15:32 +02001899 dma_pool_destroy(fotg210->qtd_pool);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001900 fotg210->qtd_pool = NULL;
1901
Julia Lawalla4c1f0c2015-09-13 14:15:32 +02001902 dma_pool_destroy(fotg210->qh_pool);
1903 fotg210->qh_pool = NULL;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001904
Julia Lawalla4c1f0c2015-09-13 14:15:32 +02001905 dma_pool_destroy(fotg210->itd_pool);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001906 fotg210->itd_pool = NULL;
1907
1908 if (fotg210->periodic)
1909 dma_free_coherent(fotg210_to_hcd(fotg210)->self.controller,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001910 fotg210->periodic_size * sizeof(u32),
1911 fotg210->periodic, fotg210->periodic_dma);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001912 fotg210->periodic = NULL;
1913
1914 /* shadow periodic table */
1915 kfree(fotg210->pshadow);
1916 fotg210->pshadow = NULL;
1917}
1918
1919/* remember to add cleanup code (above) if you add anything here */
1920static int fotg210_mem_init(struct fotg210_hcd *fotg210, gfp_t flags)
1921{
1922 int i;
1923
1924 /* QTDs for control/bulk/intr transfers */
1925 fotg210->qtd_pool = dma_pool_create("fotg210_qtd",
1926 fotg210_to_hcd(fotg210)->self.controller,
1927 sizeof(struct fotg210_qtd),
1928 32 /* byte alignment (for hw parts) */,
1929 4096 /* can't cross 4K */);
1930 if (!fotg210->qtd_pool)
1931 goto fail;
1932
1933 /* QHs for control/bulk/intr transfers */
1934 fotg210->qh_pool = dma_pool_create("fotg210_qh",
1935 fotg210_to_hcd(fotg210)->self.controller,
1936 sizeof(struct fotg210_qh_hw),
1937 32 /* byte alignment (for hw parts) */,
1938 4096 /* can't cross 4K */);
1939 if (!fotg210->qh_pool)
1940 goto fail;
1941
1942 fotg210->async = fotg210_qh_alloc(fotg210, flags);
1943 if (!fotg210->async)
1944 goto fail;
1945
1946 /* ITD for high speed ISO transfers */
1947 fotg210->itd_pool = dma_pool_create("fotg210_itd",
1948 fotg210_to_hcd(fotg210)->self.controller,
1949 sizeof(struct fotg210_itd),
1950 64 /* byte alignment (for hw parts) */,
1951 4096 /* can't cross 4K */);
1952 if (!fotg210->itd_pool)
1953 goto fail;
1954
1955 /* Hardware periodic table */
Xu Wang3b78ef02020-11-20 08:30:54 +00001956 fotg210->periodic =
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001957 dma_alloc_coherent(fotg210_to_hcd(fotg210)->self.controller,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001958 fotg210->periodic_size * sizeof(__le32),
1959 &fotg210->periodic_dma, 0);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001960 if (fotg210->periodic == NULL)
1961 goto fail;
1962
1963 for (i = 0; i < fotg210->periodic_size; i++)
1964 fotg210->periodic[i] = FOTG210_LIST_END(fotg210);
1965
1966 /* software shadow of hardware table */
1967 fotg210->pshadow = kcalloc(fotg210->periodic_size, sizeof(void *),
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001968 flags);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001969 if (fotg210->pshadow != NULL)
1970 return 0;
1971
1972fail:
1973 fotg210_dbg(fotg210, "couldn't init memory\n");
1974 fotg210_mem_cleanup(fotg210);
1975 return -ENOMEM;
1976}
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001977/* EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001978 *
1979 * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
1980 * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
1981 * buffers needed for the larger number). We use one QH per endpoint, queue
1982 * multiple urbs (all three types) per endpoint. URBs may need several qtds.
1983 *
1984 * ISO traffic uses "ISO TD" (itd) records, and (along with
1985 * interrupts) needs careful scheduling. Performance improvements can be
1986 * an ongoing challenge. That's in "ehci-sched.c".
1987 *
1988 * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
1989 * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
1990 * (b) special fields in qh entries or (c) split iso entries. TTs will
1991 * buffer low/full speed data so the host collects it at high speed.
1992 */
1993
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001994/* fill a qtd, returning how much of the buffer we were able to queue up */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001995static int qtd_fill(struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd,
1996 dma_addr_t buf, size_t len, int token, int maxpacket)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00001997{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02001998 int i, count;
1999 u64 addr = buf;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002000
2001 /* one buffer entry per 4K ... first might be short or unaligned */
2002 qtd->hw_buf[0] = cpu_to_hc32(fotg210, (u32)addr);
2003 qtd->hw_buf_hi[0] = cpu_to_hc32(fotg210, (u32)(addr >> 32));
2004 count = 0x1000 - (buf & 0x0fff); /* rest of that page */
2005 if (likely(len < count)) /* ... iff needed */
2006 count = len;
2007 else {
2008 buf += 0x1000;
2009 buf &= ~0x0fff;
2010
2011 /* per-qtd limit: from 16K to 20K (best alignment) */
2012 for (i = 1; count < len && i < 5; i++) {
2013 addr = buf;
2014 qtd->hw_buf[i] = cpu_to_hc32(fotg210, (u32)addr);
2015 qtd->hw_buf_hi[i] = cpu_to_hc32(fotg210,
2016 (u32)(addr >> 32));
2017 buf += 0x1000;
2018 if ((count + 0x1000) < len)
2019 count += 0x1000;
2020 else
2021 count = len;
2022 }
2023
2024 /* short packets may only terminate transfers */
2025 if (count != len)
2026 count -= (count % maxpacket);
2027 }
2028 qtd->hw_token = cpu_to_hc32(fotg210, (count << 16) | token);
2029 qtd->length = count;
2030
2031 return count;
2032}
2033
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002034static inline void qh_update(struct fotg210_hcd *fotg210,
2035 struct fotg210_qh *qh, struct fotg210_qtd *qtd)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002036{
2037 struct fotg210_qh_hw *hw = qh->hw;
2038
2039 /* writes to an active overlay are unsafe */
2040 BUG_ON(qh->qh_state != QH_STATE_IDLE);
2041
2042 hw->hw_qtd_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2043 hw->hw_alt_next = FOTG210_LIST_END(fotg210);
2044
2045 /* Except for control endpoints, we make hardware maintain data
2046 * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
2047 * and set the pseudo-toggle in udev. Only usb_clear_halt() will
2048 * ever clear it.
2049 */
2050 if (!(hw->hw_info1 & cpu_to_hc32(fotg210, QH_TOGGLE_CTL))) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002051 unsigned is_out, epnum;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002052
2053 is_out = qh->is_out;
2054 epnum = (hc32_to_cpup(fotg210, &hw->hw_info1) >> 8) & 0x0f;
2055 if (unlikely(!usb_gettoggle(qh->dev, epnum, is_out))) {
2056 hw->hw_token &= ~cpu_to_hc32(fotg210, QTD_TOGGLE);
2057 usb_settoggle(qh->dev, epnum, is_out, 1);
2058 }
2059 }
2060
2061 hw->hw_token &= cpu_to_hc32(fotg210, QTD_TOGGLE | QTD_STS_PING);
2062}
2063
2064/* if it weren't for a common silicon quirk (writing the dummy into the qh
2065 * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
2066 * recovery (including urb dequeue) would need software changes to a QH...
2067 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002068static void qh_refresh(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002069{
2070 struct fotg210_qtd *qtd;
2071
2072 if (list_empty(&qh->qtd_list))
2073 qtd = qh->dummy;
2074 else {
2075 qtd = list_entry(qh->qtd_list.next,
2076 struct fotg210_qtd, qtd_list);
2077 /*
2078 * first qtd may already be partially processed.
2079 * If we come here during unlink, the QH overlay region
2080 * might have reference to the just unlinked qtd. The
2081 * qtd is updated in qh_completions(). Update the QH
2082 * overlay here.
2083 */
2084 if (cpu_to_hc32(fotg210, qtd->qtd_dma) == qh->hw->hw_current) {
2085 qh->hw->hw_qtd_next = qtd->hw_next;
2086 qtd = NULL;
2087 }
2088 }
2089
2090 if (qtd)
2091 qh_update(fotg210, qh, qtd);
2092}
2093
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002094static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2095
2096static void fotg210_clear_tt_buffer_complete(struct usb_hcd *hcd,
2097 struct usb_host_endpoint *ep)
2098{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002099 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
2100 struct fotg210_qh *qh = ep->hcpriv;
2101 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002102
2103 spin_lock_irqsave(&fotg210->lock, flags);
2104 qh->clearing_tt = 0;
2105 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
2106 && fotg210->rh_state == FOTG210_RH_RUNNING)
2107 qh_link_async(fotg210, qh);
2108 spin_unlock_irqrestore(&fotg210->lock, flags);
2109}
2110
2111static void fotg210_clear_tt_buffer(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002112 struct fotg210_qh *qh, struct urb *urb, u32 token)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002113{
2114
2115 /* If an async split transaction gets an error or is unlinked,
2116 * the TT buffer may be left in an indeterminate state. We
2117 * have to clear the TT buffer.
2118 *
2119 * Note: this routine is never called for Isochronous transfers.
2120 */
2121 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002122 struct usb_device *tt = urb->dev->tt->hub;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002123
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002124 dev_dbg(&tt->dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002125 "clear tt buffer port %d, a%d ep%d t%08x\n",
2126 urb->dev->ttport, urb->dev->devnum,
2127 usb_pipeendpoint(urb->pipe), token);
Oliver Neukum3b707ec2013-11-18 13:23:06 +01002128
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002129 if (urb->dev->tt->hub !=
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002130 fotg210_to_hcd(fotg210)->self.root_hub) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002131 if (usb_hub_clear_tt_buffer(urb) == 0)
2132 qh->clearing_tt = 1;
2133 }
2134 }
2135}
2136
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002137static int qtd_copy_status(struct fotg210_hcd *fotg210, struct urb *urb,
2138 size_t length, u32 token)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002139{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002140 int status = -EINPROGRESS;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002141
2142 /* count IN/OUT bytes, not SETUP (even short packets) */
2143 if (likely(QTD_PID(token) != 2))
2144 urb->actual_length += length - QTD_LENGTH(token);
2145
2146 /* don't modify error codes */
2147 if (unlikely(urb->unlinked))
2148 return status;
2149
2150 /* force cleanup after short read; not always an error */
2151 if (unlikely(IS_SHORT_READ(token)))
2152 status = -EREMOTEIO;
2153
2154 /* serious "can't proceed" faults reported by the hardware */
2155 if (token & QTD_STS_HALT) {
2156 if (token & QTD_STS_BABBLE) {
2157 /* FIXME "must" disable babbling device's port too */
2158 status = -EOVERFLOW;
2159 /* CERR nonzero + halt --> stall */
2160 } else if (QTD_CERR(token)) {
2161 status = -EPIPE;
2162
2163 /* In theory, more than one of the following bits can be set
2164 * since they are sticky and the transaction is retried.
2165 * Which to test first is rather arbitrary.
2166 */
2167 } else if (token & QTD_STS_MMF) {
2168 /* fs/ls interrupt xfer missed the complete-split */
2169 status = -EPROTO;
2170 } else if (token & QTD_STS_DBE) {
2171 status = (QTD_PID(token) == 1) /* IN ? */
2172 ? -ENOSR /* hc couldn't read data */
2173 : -ECOMM; /* hc couldn't write data */
2174 } else if (token & QTD_STS_XACT) {
2175 /* timeout, bad CRC, wrong PID, etc */
2176 fotg210_dbg(fotg210, "devpath %s ep%d%s 3strikes\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002177 urb->dev->devpath,
2178 usb_pipeendpoint(urb->pipe),
2179 usb_pipein(urb->pipe) ? "in" : "out");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002180 status = -EPROTO;
2181 } else { /* unknown */
2182 status = -EPROTO;
2183 }
2184
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01002185 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002186 "dev%d ep%d%s qtd token %08x --> status %d\n",
2187 usb_pipedevice(urb->pipe),
2188 usb_pipeendpoint(urb->pipe),
2189 usb_pipein(urb->pipe) ? "in" : "out",
2190 token, status);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002191 }
2192
2193 return status;
2194}
2195
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002196static void fotg210_urb_done(struct fotg210_hcd *fotg210, struct urb *urb,
2197 int status)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002198__releases(fotg210->lock)
2199__acquires(fotg210->lock)
2200{
2201 if (likely(urb->hcpriv != NULL)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002202 struct fotg210_qh *qh = (struct fotg210_qh *) urb->hcpriv;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002203
2204 /* S-mask in a QH means it's an interrupt urb */
2205 if ((qh->hw->hw_info2 & cpu_to_hc32(fotg210, QH_SMASK)) != 0) {
2206
2207 /* ... update hc-wide periodic stats (for usbfs) */
2208 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs--;
2209 }
2210 }
2211
2212 if (unlikely(urb->unlinked)) {
Corentin Labbea0ef2bd2018-09-19 19:48:53 +00002213 INCR(fotg210->stats.unlink);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002214 } else {
2215 /* report non-error and short read status as zero */
2216 if (status == -EINPROGRESS || status == -EREMOTEIO)
2217 status = 0;
Corentin Labbea0ef2bd2018-09-19 19:48:53 +00002218 INCR(fotg210->stats.complete);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002219 }
2220
2221#ifdef FOTG210_URB_TRACE
2222 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002223 "%s %s urb %p ep%d%s status %d len %d/%d\n",
2224 __func__, urb->dev->devpath, urb,
2225 usb_pipeendpoint(urb->pipe),
2226 usb_pipein(urb->pipe) ? "in" : "out",
2227 status,
2228 urb->actual_length, urb->transfer_buffer_length);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002229#endif
2230
2231 /* complete() can reenter this HCD */
2232 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
2233 spin_unlock(&fotg210->lock);
2234 usb_hcd_giveback_urb(fotg210_to_hcd(fotg210), urb, status);
2235 spin_lock(&fotg210->lock);
2236}
2237
2238static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);
2239
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002240/* Process and free completed qtds for a qh, returning URBs to drivers.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002241 * Chases up to qh->hw_current. Returns number of completions called,
2242 * indicating how much "real" work we did.
2243 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002244static unsigned qh_completions(struct fotg210_hcd *fotg210,
2245 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002246{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002247 struct fotg210_qtd *last, *end = qh->dummy;
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002248 struct fotg210_qtd *qtd, *tmp;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002249 int last_status;
2250 int stopped;
2251 unsigned count = 0;
2252 u8 state;
2253 struct fotg210_qh_hw *hw = qh->hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002254
2255 if (unlikely(list_empty(&qh->qtd_list)))
2256 return count;
2257
2258 /* completions (or tasks on other cpus) must never clobber HALT
2259 * till we've gone through and cleaned everything up, even when
2260 * they add urbs to this qh's queue or mark them for unlinking.
2261 *
2262 * NOTE: unlinking expects to be done in queue order.
2263 *
2264 * It's a bug for qh->qh_state to be anything other than
2265 * QH_STATE_IDLE, unless our caller is scan_async() or
2266 * scan_intr().
2267 */
2268 state = qh->qh_state;
2269 qh->qh_state = QH_STATE_COMPLETING;
2270 stopped = (state == QH_STATE_IDLE);
2271
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002272rescan:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002273 last = NULL;
2274 last_status = -EINPROGRESS;
2275 qh->needs_rescan = 0;
2276
2277 /* remove de-activated QTDs from front of queue.
2278 * after faults (including short reads), cleanup this urb
2279 * then let the queue advance.
2280 * if queue is stopped, handles unlinks.
2281 */
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002282 list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002283 struct urb *urb;
2284 u32 token = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002285
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002286 urb = qtd->urb;
2287
2288 /* clean up any state from previous QTD ...*/
2289 if (last) {
2290 if (likely(last->urb != urb)) {
2291 fotg210_urb_done(fotg210, last->urb,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002292 last_status);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002293 count++;
2294 last_status = -EINPROGRESS;
2295 }
2296 fotg210_qtd_free(fotg210, last);
2297 last = NULL;
2298 }
2299
2300 /* ignore urbs submitted during completions we reported */
2301 if (qtd == end)
2302 break;
2303
2304 /* hardware copies qtd out of qh overlay */
2305 rmb();
2306 token = hc32_to_cpu(fotg210, qtd->hw_token);
2307
2308 /* always clean up qtds the hc de-activated */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002309retry_xacterr:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002310 if ((token & QTD_STS_ACTIVE) == 0) {
2311
2312 /* Report Data Buffer Error: non-fatal but useful */
2313 if (token & QTD_STS_DBE)
2314 fotg210_dbg(fotg210,
2315 "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002316 urb, usb_endpoint_num(&urb->ep->desc),
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002317 usb_endpoint_dir_in(&urb->ep->desc)
2318 ? "in" : "out",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002319 urb->transfer_buffer_length, qtd, qh);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002320
2321 /* on STALL, error, and short reads this urb must
2322 * complete and all its qtds must be recycled.
2323 */
2324 if ((token & QTD_STS_HALT) != 0) {
2325
2326 /* retry transaction errors until we
2327 * reach the software xacterr limit
2328 */
2329 if ((token & QTD_STS_XACT) &&
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002330 QTD_CERR(token) == 0 &&
2331 ++qh->xacterrs < QH_XACTERR_MAX &&
2332 !urb->unlinked) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002333 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002334 "detected XactErr len %zu/%zu retry %d\n",
2335 qtd->length - QTD_LENGTH(token),
2336 qtd->length,
2337 qh->xacterrs);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002338
2339 /* reset the token in the qtd and the
2340 * qh overlay (which still contains
2341 * the qtd) so that we pick up from
2342 * where we left off
2343 */
2344 token &= ~QTD_STS_HALT;
2345 token |= QTD_STS_ACTIVE |
2346 (FOTG210_TUNE_CERR << 10);
2347 qtd->hw_token = cpu_to_hc32(fotg210,
2348 token);
2349 wmb();
2350 hw->hw_token = cpu_to_hc32(fotg210,
2351 token);
2352 goto retry_xacterr;
2353 }
2354 stopped = 1;
2355
2356 /* magic dummy for some short reads; qh won't advance.
2357 * that silicon quirk can kick in with this dummy too.
2358 *
2359 * other short reads won't stop the queue, including
2360 * control transfers (status stage handles that) or
2361 * most other single-qtd reads ... the queue stops if
2362 * URB_SHORT_NOT_OK was set so the driver submitting
2363 * the urbs could clean it up.
2364 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002365 } else if (IS_SHORT_READ(token) &&
2366 !(qtd->hw_alt_next &
2367 FOTG210_LIST_END(fotg210))) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002368 stopped = 1;
2369 }
2370
2371 /* stop scanning when we reach qtds the hc is using */
2372 } else if (likely(!stopped
2373 && fotg210->rh_state >= FOTG210_RH_RUNNING)) {
2374 break;
2375
2376 /* scan the whole queue for unlinks whenever it stops */
2377 } else {
2378 stopped = 1;
2379
2380 /* cancel everything if we halt, suspend, etc */
2381 if (fotg210->rh_state < FOTG210_RH_RUNNING)
2382 last_status = -ESHUTDOWN;
2383
2384 /* this qtd is active; skip it unless a previous qtd
2385 * for its urb faulted, or its urb was canceled.
2386 */
2387 else if (last_status == -EINPROGRESS && !urb->unlinked)
2388 continue;
2389
2390 /* qh unlinked; token in overlay may be most current */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002391 if (state == QH_STATE_IDLE &&
2392 cpu_to_hc32(fotg210, qtd->qtd_dma)
2393 == hw->hw_current) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002394 token = hc32_to_cpu(fotg210, hw->hw_token);
2395
2396 /* An unlink may leave an incomplete
2397 * async transaction in the TT buffer.
2398 * We have to clear it.
2399 */
2400 fotg210_clear_tt_buffer(fotg210, qh, urb,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002401 token);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002402 }
2403 }
2404
2405 /* unless we already know the urb's status, collect qtd status
2406 * and update count of bytes transferred. in common short read
2407 * cases with only one data qtd (including control transfers),
2408 * queue processing won't halt. but with two or more qtds (for
2409 * example, with a 32 KB transfer), when the first qtd gets a
2410 * short read the second must be removed by hand.
2411 */
2412 if (last_status == -EINPROGRESS) {
2413 last_status = qtd_copy_status(fotg210, urb,
2414 qtd->length, token);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002415 if (last_status == -EREMOTEIO &&
2416 (qtd->hw_alt_next &
2417 FOTG210_LIST_END(fotg210)))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002418 last_status = -EINPROGRESS;
2419
2420 /* As part of low/full-speed endpoint-halt processing
2421 * we must clear the TT buffer (11.17.5).
2422 */
2423 if (unlikely(last_status != -EINPROGRESS &&
2424 last_status != -EREMOTEIO)) {
2425 /* The TT's in some hubs malfunction when they
2426 * receive this request following a STALL (they
2427 * stop sending isochronous packets). Since a
2428 * STALL can't leave the TT buffer in a busy
2429 * state (if you believe Figures 11-48 - 11-51
2430 * in the USB 2.0 spec), we won't clear the TT
2431 * buffer in this case. Strictly speaking this
2432 * is a violation of the spec.
2433 */
2434 if (last_status != -EPIPE)
2435 fotg210_clear_tt_buffer(fotg210, qh,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002436 urb, token);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002437 }
2438 }
2439
2440 /* if we're removing something not at the queue head,
2441 * patch the hardware queue pointer.
2442 */
2443 if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
2444 last = list_entry(qtd->qtd_list.prev,
2445 struct fotg210_qtd, qtd_list);
2446 last->hw_next = qtd->hw_next;
2447 }
2448
2449 /* remove qtd; it's recycled after possible urb completion */
2450 list_del(&qtd->qtd_list);
2451 last = qtd;
2452
2453 /* reinit the xacterr counter for the next qtd */
2454 qh->xacterrs = 0;
2455 }
2456
2457 /* last urb's completion might still need calling */
2458 if (likely(last != NULL)) {
2459 fotg210_urb_done(fotg210, last->urb, last_status);
2460 count++;
2461 fotg210_qtd_free(fotg210, last);
2462 }
2463
2464 /* Do we need to rescan for URBs dequeued during a giveback? */
2465 if (unlikely(qh->needs_rescan)) {
2466 /* If the QH is already unlinked, do the rescan now. */
2467 if (state == QH_STATE_IDLE)
2468 goto rescan;
2469
2470 /* Otherwise we have to wait until the QH is fully unlinked.
2471 * Our caller will start an unlink if qh->needs_rescan is
2472 * set. But if an unlink has already started, nothing needs
2473 * to be done.
2474 */
2475 if (state != QH_STATE_LINKED)
2476 qh->needs_rescan = 0;
2477 }
2478
2479 /* restore original state; caller must unlink or relink */
2480 qh->qh_state = state;
2481
2482 /* be sure the hardware's done with the qh before refreshing
2483 * it after fault cleanup, or recovering from silicon wrongly
2484 * overlaying the dummy qtd (which reduces DMA chatter).
2485 */
2486 if (stopped != 0 || hw->hw_qtd_next == FOTG210_LIST_END(fotg210)) {
2487 switch (state) {
2488 case QH_STATE_IDLE:
2489 qh_refresh(fotg210, qh);
2490 break;
2491 case QH_STATE_LINKED:
2492 /* We won't refresh a QH that's linked (after the HC
2493 * stopped the queue). That avoids a race:
2494 * - HC reads first part of QH;
2495 * - CPU updates that first part and the token;
2496 * - HC reads rest of that QH, including token
2497 * Result: HC gets an inconsistent image, and then
2498 * DMAs to/from the wrong memory (corrupting it).
2499 *
2500 * That should be rare for interrupt transfers,
2501 * except maybe high bandwidth ...
2502 */
2503
2504 /* Tell the caller to start an unlink */
2505 qh->needs_rescan = 1;
2506 break;
2507 /* otherwise, unlink already started */
2508 }
2509 }
2510
2511 return count;
2512}
2513
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002514/* reverse of qh_urb_transaction: free a list of TDs.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002515 * used for cleanup after errors, before HC sees an URB's TDs.
2516 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002517static void qtd_list_free(struct fotg210_hcd *fotg210, struct urb *urb,
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002518 struct list_head *head)
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002519{
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002520 struct fotg210_qtd *qtd, *temp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002521
Geliang Tang2f4c8e52015-12-19 00:34:26 +08002522 list_for_each_entry_safe(qtd, temp, head, qtd_list) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002523 list_del(&qtd->qtd_list);
2524 fotg210_qtd_free(fotg210, qtd);
2525 }
2526}
2527
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002528/* create a list of filled qtds for this URB; won't link into qh.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002529 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002530static struct list_head *qh_urb_transaction(struct fotg210_hcd *fotg210,
2531 struct urb *urb, struct list_head *head, gfp_t flags)
2532{
2533 struct fotg210_qtd *qtd, *qtd_prev;
2534 dma_addr_t buf;
2535 int len, this_sg_len, maxpacket;
2536 int is_input;
2537 u32 token;
2538 int i;
2539 struct scatterlist *sg;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002540
2541 /*
2542 * URBs map to sequences of QTDs: one logical transaction
2543 */
2544 qtd = fotg210_qtd_alloc(fotg210, flags);
2545 if (unlikely(!qtd))
2546 return NULL;
2547 list_add_tail(&qtd->qtd_list, head);
2548 qtd->urb = urb;
2549
2550 token = QTD_STS_ACTIVE;
2551 token |= (FOTG210_TUNE_CERR << 10);
2552 /* for split transactions, SplitXState initialized to zero */
2553
2554 len = urb->transfer_buffer_length;
2555 is_input = usb_pipein(urb->pipe);
2556 if (usb_pipecontrol(urb->pipe)) {
2557 /* SETUP pid */
2558 qtd_fill(fotg210, qtd, urb->setup_dma,
2559 sizeof(struct usb_ctrlrequest),
2560 token | (2 /* "setup" */ << 8), 8);
2561
2562 /* ... and always at least one more pid */
2563 token ^= QTD_TOGGLE;
2564 qtd_prev = qtd;
2565 qtd = fotg210_qtd_alloc(fotg210, flags);
2566 if (unlikely(!qtd))
2567 goto cleanup;
2568 qtd->urb = urb;
2569 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2570 list_add_tail(&qtd->qtd_list, head);
2571
2572 /* for zero length DATA stages, STATUS is always IN */
2573 if (len == 0)
2574 token |= (1 /* "in" */ << 8);
2575 }
2576
2577 /*
2578 * data transfer stage: buffer setup
2579 */
2580 i = urb->num_mapped_sgs;
2581 if (len > 0 && i > 0) {
2582 sg = urb->sg;
2583 buf = sg_dma_address(sg);
2584
2585 /* urb->transfer_buffer_length may be smaller than the
2586 * size of the scatterlist (or vice versa)
2587 */
2588 this_sg_len = min_t(int, sg_dma_len(sg), len);
2589 } else {
2590 sg = NULL;
2591 buf = urb->transfer_dma;
2592 this_sg_len = len;
2593 }
2594
2595 if (is_input)
2596 token |= (1 /* "in" */ << 8);
2597 /* else it's already initted to "out" pid (0 << 8) */
2598
Kelly Devillivc2e89872021-06-27 20:57:46 +08002599 maxpacket = usb_maxpacket(urb->dev, urb->pipe, !is_input);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002600
2601 /*
2602 * buffer gets wrapped in one or more qtds;
2603 * last one may be "short" (including zero len)
2604 * and may serve as a control status ack
2605 */
2606 for (;;) {
2607 int this_qtd_len;
2608
2609 this_qtd_len = qtd_fill(fotg210, qtd, buf, this_sg_len, token,
2610 maxpacket);
2611 this_sg_len -= this_qtd_len;
2612 len -= this_qtd_len;
2613 buf += this_qtd_len;
2614
2615 /*
2616 * short reads advance to a "magic" dummy instead of the next
2617 * qtd ... that forces the queue to stop, for manual cleanup.
2618 * (this will usually be overridden later.)
2619 */
2620 if (is_input)
2621 qtd->hw_alt_next = fotg210->async->hw->hw_alt_next;
2622
2623 /* qh makes control packets use qtd toggle; maybe switch it */
2624 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
2625 token ^= QTD_TOGGLE;
2626
2627 if (likely(this_sg_len <= 0)) {
2628 if (--i <= 0 || len <= 0)
2629 break;
2630 sg = sg_next(sg);
2631 buf = sg_dma_address(sg);
2632 this_sg_len = min_t(int, sg_dma_len(sg), len);
2633 }
2634
2635 qtd_prev = qtd;
2636 qtd = fotg210_qtd_alloc(fotg210, flags);
2637 if (unlikely(!qtd))
2638 goto cleanup;
2639 qtd->urb = urb;
2640 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2641 list_add_tail(&qtd->qtd_list, head);
2642 }
2643
2644 /*
2645 * unless the caller requires manual cleanup after short reads,
2646 * have the alt_next mechanism keep the queue running after the
2647 * last data qtd (the only one, for control and most other cases).
2648 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002649 if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0 ||
2650 usb_pipecontrol(urb->pipe)))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002651 qtd->hw_alt_next = FOTG210_LIST_END(fotg210);
2652
2653 /*
2654 * control requests may need a terminating data "status" ack;
2655 * other OUT ones may need a terminating short packet
2656 * (zero length).
2657 */
2658 if (likely(urb->transfer_buffer_length != 0)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002659 int one_more = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002660
2661 if (usb_pipecontrol(urb->pipe)) {
2662 one_more = 1;
2663 token ^= 0x0100; /* "in" <--> "out" */
2664 token |= QTD_TOGGLE; /* force DATA1 */
2665 } else if (usb_pipeout(urb->pipe)
2666 && (urb->transfer_flags & URB_ZERO_PACKET)
2667 && !(urb->transfer_buffer_length % maxpacket)) {
2668 one_more = 1;
2669 }
2670 if (one_more) {
2671 qtd_prev = qtd;
2672 qtd = fotg210_qtd_alloc(fotg210, flags);
2673 if (unlikely(!qtd))
2674 goto cleanup;
2675 qtd->urb = urb;
2676 qtd_prev->hw_next = QTD_NEXT(fotg210, qtd->qtd_dma);
2677 list_add_tail(&qtd->qtd_list, head);
2678
2679 /* never any data in such packets */
2680 qtd_fill(fotg210, qtd, 0, 0, token, 0);
2681 }
2682 }
2683
2684 /* by default, enable interrupt on urb completion */
2685 if (likely(!(urb->transfer_flags & URB_NO_INTERRUPT)))
2686 qtd->hw_token |= cpu_to_hc32(fotg210, QTD_IOC);
2687 return head;
2688
2689cleanup:
2690 qtd_list_free(fotg210, urb, head);
2691 return NULL;
2692}
2693
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002694/* Would be best to create all qh's from config descriptors,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002695 * when each interface/altsetting is established. Unlink
2696 * any previous qh and cancel its urbs first; endpoints are
2697 * implicitly reset then (data toggle too).
2698 * That'd mean updating how usbcore talks to HCDs. (2.7?)
Shubhankar Kuranagatti575b1ac2021-03-12 00:39:01 +05302699 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002700
2701
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002702/* Each QH holds a qtd list; a QH is used for everything except iso.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002703 *
2704 * For interrupt urbs, the scheduler must set the microframe scheduling
2705 * mask(s) each time the QH gets scheduled. For highspeed, that's
2706 * just one microframe in the s-mask. For split interrupt transactions
2707 * there are additional complications: c-mask, maybe FSTNs.
2708 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002709static struct fotg210_qh *qh_make(struct fotg210_hcd *fotg210, struct urb *urb,
2710 gfp_t flags)
2711{
2712 struct fotg210_qh *qh = fotg210_qh_alloc(fotg210, flags);
Kelly Devillivc2e89872021-06-27 20:57:46 +08002713 struct usb_host_endpoint *ep;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002714 u32 info1 = 0, info2 = 0;
2715 int is_input, type;
2716 int maxp = 0;
Kelly Devillivc2e89872021-06-27 20:57:46 +08002717 int mult;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002718 struct usb_tt *tt = urb->dev->tt;
2719 struct fotg210_qh_hw *hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002720
2721 if (!qh)
2722 return qh;
2723
2724 /*
2725 * init endpoint/device data for this QH
2726 */
2727 info1 |= usb_pipeendpoint(urb->pipe) << 8;
2728 info1 |= usb_pipedevice(urb->pipe) << 0;
2729
2730 is_input = usb_pipein(urb->pipe);
2731 type = usb_pipetype(urb->pipe);
Kelly Devillivc2e89872021-06-27 20:57:46 +08002732 ep = usb_pipe_endpoint(urb->dev, urb->pipe);
2733 maxp = usb_endpoint_maxp(&ep->desc);
2734 mult = usb_endpoint_maxp_mult(&ep->desc);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002735
2736 /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
2737 * acts like up to 3KB, but is built from smaller packets.
2738 */
Kelly Devillivc2e89872021-06-27 20:57:46 +08002739 if (maxp > 1024) {
2740 fotg210_dbg(fotg210, "bogus qh maxpacket %d\n", maxp);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002741 goto done;
2742 }
2743
2744 /* Compute interrupt scheduling parameters just once, and save.
2745 * - allowing for high bandwidth, how many nsec/uframe are used?
2746 * - split transactions need a second CSPLIT uframe; same question
2747 * - splits also need a schedule gap (for full/low speed I/O)
2748 * - qh has a polling interval
2749 *
2750 * For control/bulk requests, the HC or TT handles these.
2751 */
2752 if (type == PIPE_INTERRUPT) {
2753 qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
Kelly Devillivc2e89872021-06-27 20:57:46 +08002754 is_input, 0, mult * maxp));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002755 qh->start = NO_FRAME;
2756
2757 if (urb->dev->speed == USB_SPEED_HIGH) {
2758 qh->c_usecs = 0;
2759 qh->gap_uf = 0;
2760
2761 qh->period = urb->interval >> 3;
2762 if (qh->period == 0 && urb->interval != 1) {
2763 /* NOTE interval 2 or 4 uframes could work.
2764 * But interval 1 scheduling is simpler, and
2765 * includes high bandwidth.
2766 */
2767 urb->interval = 1;
2768 } else if (qh->period > fotg210->periodic_size) {
2769 qh->period = fotg210->periodic_size;
2770 urb->interval = qh->period << 3;
2771 }
2772 } else {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002773 int think_time;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002774
2775 /* gap is f(FS/LS transfer times) */
2776 qh->gap_uf = 1 + usb_calc_bus_time(urb->dev->speed,
2777 is_input, 0, maxp) / (125 * 1000);
2778
2779 /* FIXME this just approximates SPLIT/CSPLIT times */
2780 if (is_input) { /* SPLIT, gap, CSPLIT+DATA */
2781 qh->c_usecs = qh->usecs + HS_USECS(0);
2782 qh->usecs = HS_USECS(1);
2783 } else { /* SPLIT+DATA, gap, CSPLIT */
2784 qh->usecs += HS_USECS(1);
2785 qh->c_usecs = HS_USECS(0);
2786 }
2787
2788 think_time = tt ? tt->think_time : 0;
2789 qh->tt_usecs = NS_TO_US(think_time +
2790 usb_calc_bus_time(urb->dev->speed,
Kelly Devillivc2e89872021-06-27 20:57:46 +08002791 is_input, 0, maxp));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002792 qh->period = urb->interval;
2793 if (qh->period > fotg210->periodic_size) {
2794 qh->period = fotg210->periodic_size;
2795 urb->interval = qh->period;
2796 }
2797 }
2798 }
2799
2800 /* support for tt scheduling, and access to toggles */
2801 qh->dev = urb->dev;
2802
2803 /* using TT? */
2804 switch (urb->dev->speed) {
2805 case USB_SPEED_LOW:
2806 info1 |= QH_LOW_SPEED;
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05002807 fallthrough;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002808
2809 case USB_SPEED_FULL:
2810 /* EPS 0 means "full" */
2811 if (type != PIPE_INTERRUPT)
2812 info1 |= (FOTG210_TUNE_RL_TT << 28);
2813 if (type == PIPE_CONTROL) {
2814 info1 |= QH_CONTROL_EP; /* for TT */
2815 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2816 }
2817 info1 |= maxp << 16;
2818
2819 info2 |= (FOTG210_TUNE_MULT_TT << 30);
2820
2821 /* Some Freescale processors have an erratum in which the
2822 * port number in the queue head was 0..N-1 instead of 1..N.
2823 */
2824 if (fotg210_has_fsl_portno_bug(fotg210))
2825 info2 |= (urb->dev->ttport-1) << 23;
2826 else
2827 info2 |= urb->dev->ttport << 23;
2828
2829 /* set the address of the TT; for TDI's integrated
2830 * root hub tt, leave it zeroed.
2831 */
2832 if (tt && tt->hub != fotg210_to_hcd(fotg210)->self.root_hub)
2833 info2 |= tt->hub->devnum << 16;
2834
2835 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
2836
2837 break;
2838
2839 case USB_SPEED_HIGH: /* no TT involved */
2840 info1 |= QH_HIGH_SPEED;
2841 if (type == PIPE_CONTROL) {
2842 info1 |= (FOTG210_TUNE_RL_HS << 28);
2843 info1 |= 64 << 16; /* usb2 fixed maxpacket */
2844 info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
2845 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2846 } else if (type == PIPE_BULK) {
2847 info1 |= (FOTG210_TUNE_RL_HS << 28);
2848 /* The USB spec says that high speed bulk endpoints
2849 * always use 512 byte maxpacket. But some device
2850 * vendors decided to ignore that, and MSFT is happy
2851 * to help them do so. So now people expect to use
2852 * such nonconformant devices with Linux too; sigh.
2853 */
Kelly Devillivc2e89872021-06-27 20:57:46 +08002854 info1 |= maxp << 16;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002855 info2 |= (FOTG210_TUNE_MULT_HS << 30);
2856 } else { /* PIPE_INTERRUPT */
Kelly Devillivc2e89872021-06-27 20:57:46 +08002857 info1 |= maxp << 16;
2858 info2 |= mult << 30;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002859 }
2860 break;
2861 default:
2862 fotg210_dbg(fotg210, "bogus dev %p speed %d\n", urb->dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002863 urb->dev->speed);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002864done:
2865 qh_destroy(fotg210, qh);
2866 return NULL;
2867 }
2868
2869 /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
2870
2871 /* init as live, toggle clear, advance to dummy */
2872 qh->qh_state = QH_STATE_IDLE;
2873 hw = qh->hw;
2874 hw->hw_info1 = cpu_to_hc32(fotg210, info1);
2875 hw->hw_info2 = cpu_to_hc32(fotg210, info2);
2876 qh->is_out = !is_input;
2877 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), !is_input, 1);
2878 qh_refresh(fotg210, qh);
2879 return qh;
2880}
2881
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002882static void enable_async(struct fotg210_hcd *fotg210)
2883{
2884 if (fotg210->async_count++)
2885 return;
2886
2887 /* Stop waiting to turn off the async schedule */
2888 fotg210->enabled_hrtimer_events &= ~BIT(FOTG210_HRTIMER_DISABLE_ASYNC);
2889
2890 /* Don't start the schedule until ASS is 0 */
2891 fotg210_poll_ASS(fotg210);
2892 turn_on_io_watchdog(fotg210);
2893}
2894
2895static void disable_async(struct fotg210_hcd *fotg210)
2896{
2897 if (--fotg210->async_count)
2898 return;
2899
2900 /* The async schedule and async_unlink list are supposed to be empty */
2901 WARN_ON(fotg210->async->qh_next.qh || fotg210->async_unlink);
2902
2903 /* Don't turn off the schedule until ASS is 1 */
2904 fotg210_poll_ASS(fotg210);
2905}
2906
2907/* move qh (and its qtds) onto async queue; maybe enable queue. */
2908
2909static void qh_link_async(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
2910{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002911 __hc32 dma = QH_NEXT(fotg210, qh->qh_dma);
2912 struct fotg210_qh *head;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002913
2914 /* Don't link a QH if there's a Clear-TT-Buffer pending */
2915 if (unlikely(qh->clearing_tt))
2916 return;
2917
2918 WARN_ON(qh->qh_state != QH_STATE_IDLE);
2919
2920 /* clear halt and/or toggle; and maybe recover from silicon quirk */
2921 qh_refresh(fotg210, qh);
2922
2923 /* splice right after start */
2924 head = fotg210->async;
2925 qh->qh_next = head->qh_next;
2926 qh->hw->hw_next = head->hw->hw_next;
2927 wmb();
2928
2929 head->qh_next.qh = qh;
2930 head->hw->hw_next = dma;
2931
2932 qh->xacterrs = 0;
2933 qh->qh_state = QH_STATE_LINKED;
2934 /* qtd completions reported later by interrupt */
2935
2936 enable_async(fotg210);
2937}
2938
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002939/* For control/bulk/interrupt, return QH with these TDs appended.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002940 * Allocates and initializes the QH if necessary.
2941 * Returns null if it can't allocate a QH it needs to.
2942 * If the QH has TDs (urbs) already, that's great.
2943 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002944static struct fotg210_qh *qh_append_tds(struct fotg210_hcd *fotg210,
2945 struct urb *urb, struct list_head *qtd_list,
2946 int epnum, void **ptr)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002947{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002948 struct fotg210_qh *qh = NULL;
2949 __hc32 qh_addr_mask = cpu_to_hc32(fotg210, 0x7f);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002950
2951 qh = (struct fotg210_qh *) *ptr;
2952 if (unlikely(qh == NULL)) {
2953 /* can't sleep here, we have fotg210->lock... */
2954 qh = qh_make(fotg210, urb, GFP_ATOMIC);
2955 *ptr = qh;
2956 }
2957 if (likely(qh != NULL)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002958 struct fotg210_qtd *qtd;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002959
2960 if (unlikely(list_empty(qtd_list)))
2961 qtd = NULL;
2962 else
2963 qtd = list_entry(qtd_list->next, struct fotg210_qtd,
2964 qtd_list);
2965
2966 /* control qh may need patching ... */
2967 if (unlikely(epnum == 0)) {
2968 /* usb_reset_device() briefly reverts to address 0 */
2969 if (usb_pipedevice(urb->pipe) == 0)
2970 qh->hw->hw_info1 &= ~qh_addr_mask;
2971 }
2972
2973 /* just one way to queue requests: swap with the dummy qtd.
2974 * only hc or qh_refresh() ever modify the overlay.
2975 */
2976 if (likely(qtd != NULL)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02002977 struct fotg210_qtd *dummy;
2978 dma_addr_t dma;
2979 __hc32 token;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00002980
2981 /* to avoid racing the HC, use the dummy td instead of
2982 * the first td of our list (becomes new dummy). both
2983 * tds stay deactivated until we're done, when the
2984 * HC is allowed to fetch the old dummy (4.10.2).
2985 */
2986 token = qtd->hw_token;
2987 qtd->hw_token = HALT_BIT(fotg210);
2988
2989 dummy = qh->dummy;
2990
2991 dma = dummy->qtd_dma;
2992 *dummy = *qtd;
2993 dummy->qtd_dma = dma;
2994
2995 list_del(&qtd->qtd_list);
2996 list_add(&dummy->qtd_list, qtd_list);
2997 list_splice_tail(qtd_list, &qh->qtd_list);
2998
2999 fotg210_qtd_init(fotg210, qtd, qtd->qtd_dma);
3000 qh->dummy = qtd;
3001
3002 /* hc must see the new dummy at list end */
3003 dma = qtd->qtd_dma;
3004 qtd = list_entry(qh->qtd_list.prev,
3005 struct fotg210_qtd, qtd_list);
3006 qtd->hw_next = QTD_NEXT(fotg210, dma);
3007
3008 /* let the hc process these next qtds */
3009 wmb();
3010 dummy->hw_token = token;
3011
3012 urb->hcpriv = qh;
3013 }
3014 }
3015 return qh;
3016}
3017
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003018static int submit_async(struct fotg210_hcd *fotg210, struct urb *urb,
3019 struct list_head *qtd_list, gfp_t mem_flags)
3020{
3021 int epnum;
3022 unsigned long flags;
3023 struct fotg210_qh *qh = NULL;
3024 int rc;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003025
3026 epnum = urb->ep->desc.bEndpointAddress;
3027
3028#ifdef FOTG210_URB_TRACE
3029 {
3030 struct fotg210_qtd *qtd;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003031
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003032 qtd = list_entry(qtd_list->next, struct fotg210_qtd, qtd_list);
3033 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003034 "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
3035 __func__, urb->dev->devpath, urb,
3036 epnum & 0x0f, (epnum & USB_DIR_IN)
3037 ? "in" : "out",
3038 urb->transfer_buffer_length,
3039 qtd, urb->ep->hcpriv);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003040 }
3041#endif
3042
3043 spin_lock_irqsave(&fotg210->lock, flags);
3044 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3045 rc = -ESHUTDOWN;
3046 goto done;
3047 }
3048 rc = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
3049 if (unlikely(rc))
3050 goto done;
3051
3052 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
3053 if (unlikely(qh == NULL)) {
3054 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
3055 rc = -ENOMEM;
3056 goto done;
3057 }
3058
3059 /* Control/bulk operations through TTs don't need scheduling,
3060 * the HC and TT handle it when the TT has a buffer ready.
3061 */
3062 if (likely(qh->qh_state == QH_STATE_IDLE))
3063 qh_link_async(fotg210, qh);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003064done:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003065 spin_unlock_irqrestore(&fotg210->lock, flags);
3066 if (unlikely(qh == NULL))
3067 qtd_list_free(fotg210, urb, qtd_list);
3068 return rc;
3069}
3070
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003071static void single_unlink_async(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003072 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003073{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003074 struct fotg210_qh *prev;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003075
3076 /* Add to the end of the list of QHs waiting for the next IAAD */
3077 qh->qh_state = QH_STATE_UNLINK;
3078 if (fotg210->async_unlink)
3079 fotg210->async_unlink_last->unlink_next = qh;
3080 else
3081 fotg210->async_unlink = qh;
3082 fotg210->async_unlink_last = qh;
3083
3084 /* Unlink it from the schedule */
3085 prev = fotg210->async;
3086 while (prev->qh_next.qh != qh)
3087 prev = prev->qh_next.qh;
3088
3089 prev->hw->hw_next = qh->hw->hw_next;
3090 prev->qh_next = qh->qh_next;
3091 if (fotg210->qh_scan_next == qh)
3092 fotg210->qh_scan_next = qh->qh_next.qh;
3093}
3094
3095static void start_iaa_cycle(struct fotg210_hcd *fotg210, bool nested)
3096{
3097 /*
3098 * Do nothing if an IAA cycle is already running or
3099 * if one will be started shortly.
3100 */
3101 if (fotg210->async_iaa || fotg210->async_unlinking)
3102 return;
3103
3104 /* Do all the waiting QHs at once */
3105 fotg210->async_iaa = fotg210->async_unlink;
3106 fotg210->async_unlink = NULL;
3107
3108 /* If the controller isn't running, we don't have to wait for it */
3109 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING)) {
3110 if (!nested) /* Avoid recursion */
3111 end_unlink_async(fotg210);
3112
3113 /* Otherwise start a new IAA cycle */
3114 } else if (likely(fotg210->rh_state == FOTG210_RH_RUNNING)) {
3115 /* Make sure the unlinks are all visible to the hardware */
3116 wmb();
3117
3118 fotg210_writel(fotg210, fotg210->command | CMD_IAAD,
3119 &fotg210->regs->command);
3120 fotg210_readl(fotg210, &fotg210->regs->command);
3121 fotg210_enable_event(fotg210, FOTG210_HRTIMER_IAA_WATCHDOG,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003122 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003123 }
3124}
3125
3126/* the async qh for the qtds being unlinked are now gone from the HC */
3127
3128static void end_unlink_async(struct fotg210_hcd *fotg210)
3129{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003130 struct fotg210_qh *qh;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003131
3132 /* Process the idle QHs */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003133restart:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003134 fotg210->async_unlinking = true;
3135 while (fotg210->async_iaa) {
3136 qh = fotg210->async_iaa;
3137 fotg210->async_iaa = qh->unlink_next;
3138 qh->unlink_next = NULL;
3139
3140 qh->qh_state = QH_STATE_IDLE;
3141 qh->qh_next.qh = NULL;
3142
3143 qh_completions(fotg210, qh);
3144 if (!list_empty(&qh->qtd_list) &&
3145 fotg210->rh_state == FOTG210_RH_RUNNING)
3146 qh_link_async(fotg210, qh);
3147 disable_async(fotg210);
3148 }
3149 fotg210->async_unlinking = false;
3150
3151 /* Start a new IAA cycle if any QHs are waiting for it */
3152 if (fotg210->async_unlink) {
3153 start_iaa_cycle(fotg210, true);
3154 if (unlikely(fotg210->rh_state < FOTG210_RH_RUNNING))
3155 goto restart;
3156 }
3157}
3158
3159static void unlink_empty_async(struct fotg210_hcd *fotg210)
3160{
3161 struct fotg210_qh *qh, *next;
3162 bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
3163 bool check_unlinks_later = false;
3164
3165 /* Unlink all the async QHs that have been empty for a timer cycle */
3166 next = fotg210->async->qh_next.qh;
3167 while (next) {
3168 qh = next;
3169 next = qh->qh_next.qh;
3170
3171 if (list_empty(&qh->qtd_list) &&
3172 qh->qh_state == QH_STATE_LINKED) {
3173 if (!stopped && qh->unlink_cycle ==
3174 fotg210->async_unlink_cycle)
3175 check_unlinks_later = true;
3176 else
3177 single_unlink_async(fotg210, qh);
3178 }
3179 }
3180
3181 /* Start a new IAA cycle if any QHs are waiting for it */
3182 if (fotg210->async_unlink)
3183 start_iaa_cycle(fotg210, false);
3184
3185 /* QHs that haven't been empty for long enough will be handled later */
3186 if (check_unlinks_later) {
3187 fotg210_enable_event(fotg210, FOTG210_HRTIMER_ASYNC_UNLINKS,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003188 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003189 ++fotg210->async_unlink_cycle;
3190 }
3191}
3192
3193/* makes sure the async qh will become idle */
3194/* caller must own fotg210->lock */
3195
3196static void start_unlink_async(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003197 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003198{
3199 /*
3200 * If the QH isn't linked then there's nothing we can do
3201 * unless we were called during a giveback, in which case
3202 * qh_completions() has to deal with it.
3203 */
3204 if (qh->qh_state != QH_STATE_LINKED) {
3205 if (qh->qh_state == QH_STATE_COMPLETING)
3206 qh->needs_rescan = 1;
3207 return;
3208 }
3209
3210 single_unlink_async(fotg210, qh);
3211 start_iaa_cycle(fotg210, false);
3212}
3213
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003214static void scan_async(struct fotg210_hcd *fotg210)
3215{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003216 struct fotg210_qh *qh;
3217 bool check_unlinks_later = false;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003218
3219 fotg210->qh_scan_next = fotg210->async->qh_next.qh;
3220 while (fotg210->qh_scan_next) {
3221 qh = fotg210->qh_scan_next;
3222 fotg210->qh_scan_next = qh->qh_next.qh;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003223rescan:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003224 /* clean any finished work for this qh */
3225 if (!list_empty(&qh->qtd_list)) {
3226 int temp;
3227
3228 /*
3229 * Unlinks could happen here; completion reporting
3230 * drops the lock. That's why fotg210->qh_scan_next
3231 * always holds the next qh to scan; if the next qh
3232 * gets unlinked then fotg210->qh_scan_next is adjusted
3233 * in single_unlink_async().
3234 */
3235 temp = qh_completions(fotg210, qh);
3236 if (qh->needs_rescan) {
3237 start_unlink_async(fotg210, qh);
3238 } else if (list_empty(&qh->qtd_list)
3239 && qh->qh_state == QH_STATE_LINKED) {
3240 qh->unlink_cycle = fotg210->async_unlink_cycle;
3241 check_unlinks_later = true;
3242 } else if (temp != 0)
3243 goto rescan;
3244 }
3245 }
3246
3247 /*
3248 * Unlink empty entries, reducing DMA usage as well
3249 * as HCD schedule-scanning costs. Delay for any qh
3250 * we just scanned, there's a not-unusual case that it
3251 * doesn't stay idle for long.
3252 */
3253 if (check_unlinks_later && fotg210->rh_state == FOTG210_RH_RUNNING &&
3254 !(fotg210->enabled_hrtimer_events &
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003255 BIT(FOTG210_HRTIMER_ASYNC_UNLINKS))) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003256 fotg210_enable_event(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003257 FOTG210_HRTIMER_ASYNC_UNLINKS, true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003258 ++fotg210->async_unlink_cycle;
3259 }
3260}
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003261/* EHCI scheduled transaction support: interrupt, iso, split iso
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003262 * These are called "periodic" transactions in the EHCI spec.
3263 *
3264 * Note that for interrupt transfers, the QH/QTD manipulation is shared
3265 * with the "asynchronous" transaction support (control/bulk transfers).
3266 * The only real difference is in how interrupt transfers are scheduled.
3267 *
3268 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
3269 * It keeps track of every ITD (or SITD) that's linked, and holds enough
3270 * pre-calculated schedule data to make appending to the queue be quick.
3271 */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003272static int fotg210_get_frame(struct usb_hcd *hcd);
3273
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003274/* periodic_next_shadow - return "next" pointer on shadow list
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003275 * @periodic: host pointer to qh/itd
3276 * @tag: hardware tag for type of this record
3277 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003278static union fotg210_shadow *periodic_next_shadow(struct fotg210_hcd *fotg210,
3279 union fotg210_shadow *periodic, __hc32 tag)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003280{
3281 switch (hc32_to_cpu(fotg210, tag)) {
3282 case Q_TYPE_QH:
3283 return &periodic->qh->qh_next;
3284 case Q_TYPE_FSTN:
3285 return &periodic->fstn->fstn_next;
3286 default:
3287 return &periodic->itd->itd_next;
3288 }
3289}
3290
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003291static __hc32 *shadow_next_periodic(struct fotg210_hcd *fotg210,
3292 union fotg210_shadow *periodic, __hc32 tag)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003293{
3294 switch (hc32_to_cpu(fotg210, tag)) {
3295 /* our fotg210_shadow.qh is actually software part */
3296 case Q_TYPE_QH:
3297 return &periodic->qh->hw->hw_next;
3298 /* others are hw parts */
3299 default:
3300 return periodic->hw_next;
3301 }
3302}
3303
3304/* caller must hold fotg210->lock */
3305static void periodic_unlink(struct fotg210_hcd *fotg210, unsigned frame,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003306 void *ptr)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003307{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003308 union fotg210_shadow *prev_p = &fotg210->pshadow[frame];
3309 __hc32 *hw_p = &fotg210->periodic[frame];
3310 union fotg210_shadow here = *prev_p;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003311
3312 /* find predecessor of "ptr"; hw and shadow lists are in sync */
3313 while (here.ptr && here.ptr != ptr) {
3314 prev_p = periodic_next_shadow(fotg210, prev_p,
3315 Q_NEXT_TYPE(fotg210, *hw_p));
3316 hw_p = shadow_next_periodic(fotg210, &here,
3317 Q_NEXT_TYPE(fotg210, *hw_p));
3318 here = *prev_p;
3319 }
3320 /* an interrupt entry (at list end) could have been shared */
3321 if (!here.ptr)
3322 return;
3323
3324 /* update shadow and hardware lists ... the old "next" pointers
3325 * from ptr may still be in use, the caller updates them.
3326 */
3327 *prev_p = *periodic_next_shadow(fotg210, &here,
3328 Q_NEXT_TYPE(fotg210, *hw_p));
3329
3330 *hw_p = *shadow_next_periodic(fotg210, &here,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003331 Q_NEXT_TYPE(fotg210, *hw_p));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003332}
3333
3334/* how many of the uframe's 125 usecs are allocated? */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003335static unsigned short periodic_usecs(struct fotg210_hcd *fotg210,
3336 unsigned frame, unsigned uframe)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003337{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003338 __hc32 *hw_p = &fotg210->periodic[frame];
3339 union fotg210_shadow *q = &fotg210->pshadow[frame];
3340 unsigned usecs = 0;
3341 struct fotg210_qh_hw *hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003342
3343 while (q->ptr) {
3344 switch (hc32_to_cpu(fotg210, Q_NEXT_TYPE(fotg210, *hw_p))) {
3345 case Q_TYPE_QH:
3346 hw = q->qh->hw;
3347 /* is it in the S-mask? */
3348 if (hw->hw_info2 & cpu_to_hc32(fotg210, 1 << uframe))
3349 usecs += q->qh->usecs;
3350 /* ... or C-mask? */
3351 if (hw->hw_info2 & cpu_to_hc32(fotg210,
3352 1 << (8 + uframe)))
3353 usecs += q->qh->c_usecs;
3354 hw_p = &hw->hw_next;
3355 q = &q->qh->qh_next;
3356 break;
3357 /* case Q_TYPE_FSTN: */
3358 default:
3359 /* for "save place" FSTNs, count the relevant INTR
3360 * bandwidth from the previous frame
3361 */
3362 if (q->fstn->hw_prev != FOTG210_LIST_END(fotg210))
3363 fotg210_dbg(fotg210, "ignoring FSTN cost ...\n");
3364
3365 hw_p = &q->fstn->hw_next;
3366 q = &q->fstn->fstn_next;
3367 break;
3368 case Q_TYPE_ITD:
3369 if (q->itd->hw_transaction[uframe])
3370 usecs += q->itd->stream->usecs;
3371 hw_p = &q->itd->hw_next;
3372 q = &q->itd->itd_next;
3373 break;
3374 }
3375 }
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003376 if (usecs > fotg210->uframe_periodic_max)
3377 fotg210_err(fotg210, "uframe %d sched overrun: %d usecs\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003378 frame * 8 + uframe, usecs);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003379 return usecs;
3380}
3381
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003382static int same_tt(struct usb_device *dev1, struct usb_device *dev2)
3383{
3384 if (!dev1->tt || !dev2->tt)
3385 return 0;
3386 if (dev1->tt != dev2->tt)
3387 return 0;
3388 if (dev1->tt->multi)
3389 return dev1->ttport == dev2->ttport;
3390 else
3391 return 1;
3392}
3393
3394/* return true iff the device's transaction translator is available
3395 * for a periodic transfer starting at the specified frame, using
3396 * all the uframes in the mask.
3397 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003398static int tt_no_collision(struct fotg210_hcd *fotg210, unsigned period,
3399 struct usb_device *dev, unsigned frame, u32 uf_mask)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003400{
3401 if (period == 0) /* error */
3402 return 0;
3403
3404 /* note bandwidth wastage: split never follows csplit
3405 * (different dev or endpoint) until the next uframe.
3406 * calling convention doesn't make that distinction.
3407 */
3408 for (; frame < fotg210->periodic_size; frame += period) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003409 union fotg210_shadow here;
3410 __hc32 type;
3411 struct fotg210_qh_hw *hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003412
3413 here = fotg210->pshadow[frame];
3414 type = Q_NEXT_TYPE(fotg210, fotg210->periodic[frame]);
3415 while (here.ptr) {
3416 switch (hc32_to_cpu(fotg210, type)) {
3417 case Q_TYPE_ITD:
3418 type = Q_NEXT_TYPE(fotg210, here.itd->hw_next);
3419 here = here.itd->itd_next;
3420 continue;
3421 case Q_TYPE_QH:
3422 hw = here.qh->hw;
3423 if (same_tt(dev, here.qh->dev)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003424 u32 mask;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003425
3426 mask = hc32_to_cpu(fotg210,
3427 hw->hw_info2);
3428 /* "knows" no gap is needed */
3429 mask |= mask >> 8;
3430 if (mask & uf_mask)
3431 break;
3432 }
3433 type = Q_NEXT_TYPE(fotg210, hw->hw_next);
3434 here = here.qh->qh_next;
3435 continue;
3436 /* case Q_TYPE_FSTN: */
3437 default:
3438 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003439 "periodic frame %d bogus type %d\n",
3440 frame, type);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003441 }
3442
3443 /* collision or error */
3444 return 0;
3445 }
3446 }
3447
3448 /* no collision */
3449 return 1;
3450}
3451
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003452static void enable_periodic(struct fotg210_hcd *fotg210)
3453{
3454 if (fotg210->periodic_count++)
3455 return;
3456
3457 /* Stop waiting to turn off the periodic schedule */
3458 fotg210->enabled_hrtimer_events &=
3459 ~BIT(FOTG210_HRTIMER_DISABLE_PERIODIC);
3460
3461 /* Don't start the schedule until PSS is 0 */
3462 fotg210_poll_PSS(fotg210);
3463 turn_on_io_watchdog(fotg210);
3464}
3465
3466static void disable_periodic(struct fotg210_hcd *fotg210)
3467{
3468 if (--fotg210->periodic_count)
3469 return;
3470
3471 /* Don't turn off the schedule until PSS is 1 */
3472 fotg210_poll_PSS(fotg210);
3473}
3474
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003475/* periodic schedule slots have iso tds (normal or split) first, then a
3476 * sparse tree for active interrupt transfers.
3477 *
3478 * this just links in a qh; caller guarantees uframe masks are set right.
3479 * no FSTN support (yet; fotg210 0.96+)
3480 */
3481static void qh_link_periodic(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3482{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003483 unsigned i;
3484 unsigned period = qh->period;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003485
3486 dev_dbg(&qh->dev->dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003487 "link qh%d-%04x/%p start %d [%d/%d us]\n", period,
3488 hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
3489 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
3490 qh->c_usecs);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003491
3492 /* high bandwidth, or otherwise every microframe */
3493 if (period == 0)
3494 period = 1;
3495
3496 for (i = qh->start; i < fotg210->periodic_size; i += period) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003497 union fotg210_shadow *prev = &fotg210->pshadow[i];
3498 __hc32 *hw_p = &fotg210->periodic[i];
3499 union fotg210_shadow here = *prev;
3500 __hc32 type = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003501
3502 /* skip the iso nodes at list head */
3503 while (here.ptr) {
3504 type = Q_NEXT_TYPE(fotg210, *hw_p);
3505 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
3506 break;
3507 prev = periodic_next_shadow(fotg210, prev, type);
3508 hw_p = shadow_next_periodic(fotg210, &here, type);
3509 here = *prev;
3510 }
3511
3512 /* sorting each branch by period (slow-->fast)
3513 * enables sharing interior tree nodes
3514 */
3515 while (here.ptr && qh != here.qh) {
3516 if (qh->period > here.qh->period)
3517 break;
3518 prev = &here.qh->qh_next;
3519 hw_p = &here.qh->hw->hw_next;
3520 here = *prev;
3521 }
3522 /* link in this qh, unless some earlier pass did that */
3523 if (qh != here.qh) {
3524 qh->qh_next = here;
3525 if (here.qh)
3526 qh->hw->hw_next = *hw_p;
3527 wmb();
3528 prev->qh = qh;
3529 *hw_p = QH_NEXT(fotg210, qh->qh_dma);
3530 }
3531 }
3532 qh->qh_state = QH_STATE_LINKED;
3533 qh->xacterrs = 0;
3534
3535 /* update per-qh bandwidth for usbfs */
3536 fotg210_to_hcd(fotg210)->self.bandwidth_allocated += qh->period
3537 ? ((qh->usecs + qh->c_usecs) / qh->period)
3538 : (qh->usecs * 8);
3539
3540 list_add(&qh->intr_node, &fotg210->intr_qh_list);
3541
3542 /* maybe enable periodic schedule processing */
3543 ++fotg210->intr_count;
3544 enable_periodic(fotg210);
3545}
3546
3547static void qh_unlink_periodic(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003548 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003549{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003550 unsigned i;
3551 unsigned period;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003552
3553 /*
3554 * If qh is for a low/full-speed device, simply unlinking it
3555 * could interfere with an ongoing split transaction. To unlink
3556 * it safely would require setting the QH_INACTIVATE bit and
3557 * waiting at least one frame, as described in EHCI 4.12.2.5.
3558 *
3559 * We won't bother with any of this. Instead, we assume that the
3560 * only reason for unlinking an interrupt QH while the current URB
3561 * is still active is to dequeue all the URBs (flush the whole
3562 * endpoint queue).
3563 *
3564 * If rebalancing the periodic schedule is ever implemented, this
3565 * approach will no longer be valid.
3566 */
3567
3568 /* high bandwidth, or otherwise part of every microframe */
3569 period = qh->period;
3570 if (!period)
3571 period = 1;
3572
3573 for (i = qh->start; i < fotg210->periodic_size; i += period)
3574 periodic_unlink(fotg210, i, qh);
3575
3576 /* update per-qh bandwidth for usbfs */
3577 fotg210_to_hcd(fotg210)->self.bandwidth_allocated -= qh->period
3578 ? ((qh->usecs + qh->c_usecs) / qh->period)
3579 : (qh->usecs * 8);
3580
3581 dev_dbg(&qh->dev->dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003582 "unlink qh%d-%04x/%p start %d [%d/%d us]\n",
3583 qh->period, hc32_to_cpup(fotg210, &qh->hw->hw_info2) &
3584 (QH_CMASK | QH_SMASK), qh, qh->start, qh->usecs,
3585 qh->c_usecs);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003586
3587 /* qh->qh_next still "live" to HC */
3588 qh->qh_state = QH_STATE_UNLINK;
3589 qh->qh_next.ptr = NULL;
3590
3591 if (fotg210->qh_scan_next == qh)
3592 fotg210->qh_scan_next = list_entry(qh->intr_node.next,
3593 struct fotg210_qh, intr_node);
3594 list_del(&qh->intr_node);
3595}
3596
3597static void start_unlink_intr(struct fotg210_hcd *fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003598 struct fotg210_qh *qh)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003599{
3600 /* If the QH isn't linked then there's nothing we can do
3601 * unless we were called during a giveback, in which case
3602 * qh_completions() has to deal with it.
3603 */
3604 if (qh->qh_state != QH_STATE_LINKED) {
3605 if (qh->qh_state == QH_STATE_COMPLETING)
3606 qh->needs_rescan = 1;
3607 return;
3608 }
3609
3610 qh_unlink_periodic(fotg210, qh);
3611
3612 /* Make sure the unlinks are visible before starting the timer */
3613 wmb();
3614
3615 /*
3616 * The EHCI spec doesn't say how long it takes the controller to
3617 * stop accessing an unlinked interrupt QH. The timer delay is
3618 * 9 uframes; presumably that will be long enough.
3619 */
3620 qh->unlink_cycle = fotg210->intr_unlink_cycle;
3621
3622 /* New entries go at the end of the intr_unlink list */
3623 if (fotg210->intr_unlink)
3624 fotg210->intr_unlink_last->unlink_next = qh;
3625 else
3626 fotg210->intr_unlink = qh;
3627 fotg210->intr_unlink_last = qh;
3628
3629 if (fotg210->intr_unlinking)
3630 ; /* Avoid recursive calls */
3631 else if (fotg210->rh_state < FOTG210_RH_RUNNING)
3632 fotg210_handle_intr_unlinks(fotg210);
3633 else if (fotg210->intr_unlink == qh) {
3634 fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003635 true);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003636 ++fotg210->intr_unlink_cycle;
3637 }
3638}
3639
3640static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3641{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003642 struct fotg210_qh_hw *hw = qh->hw;
3643 int rc;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003644
3645 qh->qh_state = QH_STATE_IDLE;
3646 hw->hw_next = FOTG210_LIST_END(fotg210);
3647
3648 qh_completions(fotg210, qh);
3649
3650 /* reschedule QH iff another request is queued */
3651 if (!list_empty(&qh->qtd_list) &&
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003652 fotg210->rh_state == FOTG210_RH_RUNNING) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003653 rc = qh_schedule(fotg210, qh);
3654
3655 /* An error here likely indicates handshake failure
3656 * or no space left in the schedule. Neither fault
3657 * should happen often ...
3658 *
3659 * FIXME kill the now-dysfunctional queued urbs
3660 */
3661 if (rc != 0)
3662 fotg210_err(fotg210, "can't reschedule qh %p, err %d\n",
3663 qh, rc);
3664 }
3665
3666 /* maybe turn off periodic schedule */
3667 --fotg210->intr_count;
3668 disable_periodic(fotg210);
3669}
3670
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003671static int check_period(struct fotg210_hcd *fotg210, unsigned frame,
3672 unsigned uframe, unsigned period, unsigned usecs)
3673{
3674 int claimed;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003675
3676 /* complete split running into next frame?
3677 * given FSTN support, we could sometimes check...
3678 */
3679 if (uframe >= 8)
3680 return 0;
3681
3682 /* convert "usecs we need" to "max already claimed" */
3683 usecs = fotg210->uframe_periodic_max - usecs;
3684
3685 /* we "know" 2 and 4 uframe intervals were rejected; so
3686 * for period 0, check _every_ microframe in the schedule.
3687 */
3688 if (unlikely(period == 0)) {
3689 do {
3690 for (uframe = 0; uframe < 7; uframe++) {
3691 claimed = periodic_usecs(fotg210, frame,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003692 uframe);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003693 if (claimed > usecs)
3694 return 0;
3695 }
3696 } while ((frame += 1) < fotg210->periodic_size);
3697
3698 /* just check the specified uframe, at that period */
3699 } else {
3700 do {
3701 claimed = periodic_usecs(fotg210, frame, uframe);
3702 if (claimed > usecs)
3703 return 0;
3704 } while ((frame += period) < fotg210->periodic_size);
3705 }
3706
3707 /* success! */
3708 return 1;
3709}
3710
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003711static int check_intr_schedule(struct fotg210_hcd *fotg210, unsigned frame,
3712 unsigned uframe, const struct fotg210_qh *qh, __hc32 *c_maskp)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003713{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003714 int retval = -ENOSPC;
3715 u8 mask = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003716
3717 if (qh->c_usecs && uframe >= 6) /* FSTN territory? */
3718 goto done;
3719
3720 if (!check_period(fotg210, frame, uframe, qh->period, qh->usecs))
3721 goto done;
3722 if (!qh->c_usecs) {
3723 retval = 0;
3724 *c_maskp = 0;
3725 goto done;
3726 }
3727
3728 /* Make sure this tt's buffer is also available for CSPLITs.
3729 * We pessimize a bit; probably the typical full speed case
3730 * doesn't need the second CSPLIT.
3731 *
3732 * NOTE: both SPLIT and CSPLIT could be checked in just
3733 * one smart pass...
3734 */
3735 mask = 0x03 << (uframe + qh->gap_uf);
3736 *c_maskp = cpu_to_hc32(fotg210, mask << 8);
3737
3738 mask |= 1 << uframe;
3739 if (tt_no_collision(fotg210, qh->period, qh->dev, frame, mask)) {
3740 if (!check_period(fotg210, frame, uframe + qh->gap_uf + 1,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003741 qh->period, qh->c_usecs))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003742 goto done;
3743 if (!check_period(fotg210, frame, uframe + qh->gap_uf,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003744 qh->period, qh->c_usecs))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003745 goto done;
3746 retval = 0;
3747 }
3748done:
3749 return retval;
3750}
3751
3752/* "first fit" scheduling policy used the first time through,
3753 * or when the previous schedule slot can't be re-used.
3754 */
3755static int qh_schedule(struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
3756{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003757 int status;
3758 unsigned uframe;
3759 __hc32 c_mask;
3760 unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
3761 struct fotg210_qh_hw *hw = qh->hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003762
3763 qh_refresh(fotg210, qh);
3764 hw->hw_next = FOTG210_LIST_END(fotg210);
3765 frame = qh->start;
3766
3767 /* reuse the previous schedule slots, if we can */
3768 if (frame < qh->period) {
3769 uframe = ffs(hc32_to_cpup(fotg210, &hw->hw_info2) & QH_SMASK);
3770 status = check_intr_schedule(fotg210, frame, --uframe,
3771 qh, &c_mask);
3772 } else {
3773 uframe = 0;
3774 c_mask = 0;
3775 status = -ENOSPC;
3776 }
3777
3778 /* else scan the schedule to find a group of slots such that all
3779 * uframes have enough periodic bandwidth available.
3780 */
3781 if (status) {
3782 /* "normal" case, uframing flexible except with splits */
3783 if (qh->period) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003784 int i;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003785
3786 for (i = qh->period; status && i > 0; --i) {
3787 frame = ++fotg210->random_frame % qh->period;
3788 for (uframe = 0; uframe < 8; uframe++) {
3789 status = check_intr_schedule(fotg210,
3790 frame, uframe, qh,
3791 &c_mask);
3792 if (status == 0)
3793 break;
3794 }
3795 }
3796
3797 /* qh->period == 0 means every uframe */
3798 } else {
3799 frame = 0;
3800 status = check_intr_schedule(fotg210, 0, 0, qh,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003801 &c_mask);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003802 }
3803 if (status)
3804 goto done;
3805 qh->start = frame;
3806
3807 /* reset S-frame and (maybe) C-frame masks */
3808 hw->hw_info2 &= cpu_to_hc32(fotg210, ~(QH_CMASK | QH_SMASK));
3809 hw->hw_info2 |= qh->period
3810 ? cpu_to_hc32(fotg210, 1 << uframe)
3811 : cpu_to_hc32(fotg210, QH_SMASK);
3812 hw->hw_info2 |= c_mask;
3813 } else
3814 fotg210_dbg(fotg210, "reused qh %p schedule\n", qh);
3815
3816 /* stuff into the periodic schedule */
3817 qh_link_periodic(fotg210, qh);
3818done:
3819 return status;
3820}
3821
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003822static int intr_submit(struct fotg210_hcd *fotg210, struct urb *urb,
3823 struct list_head *qtd_list, gfp_t mem_flags)
3824{
3825 unsigned epnum;
3826 unsigned long flags;
3827 struct fotg210_qh *qh;
3828 int status;
3829 struct list_head empty;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003830
3831 /* get endpoint and transfer/schedule data */
3832 epnum = urb->ep->desc.bEndpointAddress;
3833
3834 spin_lock_irqsave(&fotg210->lock, flags);
3835
3836 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
3837 status = -ESHUTDOWN;
3838 goto done_not_linked;
3839 }
3840 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
3841 if (unlikely(status))
3842 goto done_not_linked;
3843
3844 /* get qh and force any scheduling errors */
3845 INIT_LIST_HEAD(&empty);
3846 qh = qh_append_tds(fotg210, urb, &empty, epnum, &urb->ep->hcpriv);
3847 if (qh == NULL) {
3848 status = -ENOMEM;
3849 goto done;
3850 }
3851 if (qh->qh_state == QH_STATE_IDLE) {
3852 status = qh_schedule(fotg210, qh);
3853 if (status)
3854 goto done;
3855 }
3856
3857 /* then queue the urb's tds to the qh */
3858 qh = qh_append_tds(fotg210, urb, qtd_list, epnum, &urb->ep->hcpriv);
3859 BUG_ON(qh == NULL);
3860
3861 /* ... update usbfs periodic stats */
3862 fotg210_to_hcd(fotg210)->self.bandwidth_int_reqs++;
3863
3864done:
3865 if (unlikely(status))
3866 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
3867done_not_linked:
3868 spin_unlock_irqrestore(&fotg210->lock, flags);
3869 if (status)
3870 qtd_list_free(fotg210, urb, qtd_list);
3871
3872 return status;
3873}
3874
3875static void scan_intr(struct fotg210_hcd *fotg210)
3876{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003877 struct fotg210_qh *qh;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003878
3879 list_for_each_entry_safe(qh, fotg210->qh_scan_next,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003880 &fotg210->intr_qh_list, intr_node) {
3881rescan:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003882 /* clean any finished work for this qh */
3883 if (!list_empty(&qh->qtd_list)) {
3884 int temp;
3885
3886 /*
3887 * Unlinks could happen here; completion reporting
3888 * drops the lock. That's why fotg210->qh_scan_next
3889 * always holds the next qh to scan; if the next qh
3890 * gets unlinked then fotg210->qh_scan_next is adjusted
3891 * in qh_unlink_periodic().
3892 */
3893 temp = qh_completions(fotg210, qh);
3894 if (unlikely(qh->needs_rescan ||
3895 (list_empty(&qh->qtd_list) &&
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003896 qh->qh_state == QH_STATE_LINKED)))
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003897 start_unlink_intr(fotg210, qh);
3898 else if (temp != 0)
3899 goto rescan;
3900 }
3901 }
3902}
3903
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003904/* fotg210_iso_stream ops work with both ITD and SITD */
3905
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003906static struct fotg210_iso_stream *iso_stream_alloc(gfp_t mem_flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003907{
3908 struct fotg210_iso_stream *stream;
3909
3910 stream = kzalloc(sizeof(*stream), mem_flags);
3911 if (likely(stream != NULL)) {
3912 INIT_LIST_HEAD(&stream->td_list);
3913 INIT_LIST_HEAD(&stream->free_list);
3914 stream->next_uframe = -1;
3915 }
3916 return stream;
3917}
3918
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003919static void iso_stream_init(struct fotg210_hcd *fotg210,
3920 struct fotg210_iso_stream *stream, struct usb_device *dev,
3921 int pipe, unsigned interval)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003922{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003923 u32 buf1;
3924 unsigned epnum, maxp;
3925 int is_input;
3926 long bandwidth;
3927 unsigned multi;
Kelly Devillivc2e89872021-06-27 20:57:46 +08003928 struct usb_host_endpoint *ep;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003929
3930 /*
3931 * this might be a "high bandwidth" highspeed endpoint,
3932 * as encoded in the ep descriptor's wMaxPacket field
3933 */
3934 epnum = usb_pipeendpoint(pipe);
3935 is_input = usb_pipein(pipe) ? USB_DIR_IN : 0;
Kelly Devillivc2e89872021-06-27 20:57:46 +08003936 ep = usb_pipe_endpoint(dev, pipe);
3937 maxp = usb_endpoint_maxp(&ep->desc);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003938 if (is_input)
3939 buf1 = (1 << 11);
3940 else
3941 buf1 = 0;
3942
Kelly Devillivc2e89872021-06-27 20:57:46 +08003943 multi = usb_endpoint_maxp_mult(&ep->desc);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003944 buf1 |= maxp;
3945 maxp *= multi;
3946
3947 stream->buf0 = cpu_to_hc32(fotg210, (epnum << 8) | dev->devnum);
3948 stream->buf1 = cpu_to_hc32(fotg210, buf1);
3949 stream->buf2 = cpu_to_hc32(fotg210, multi);
3950
3951 /* usbfs wants to report the average usecs per frame tied up
3952 * when transfers on this endpoint are scheduled ...
3953 */
3954 if (dev->speed == USB_SPEED_FULL) {
3955 interval <<= 3;
3956 stream->usecs = NS_TO_US(usb_calc_bus_time(dev->speed,
3957 is_input, 1, maxp));
3958 stream->usecs /= 8;
3959 } else {
3960 stream->highspeed = 1;
3961 stream->usecs = HS_USECS_ISO(maxp);
3962 }
3963 bandwidth = stream->usecs * 8;
3964 bandwidth /= interval;
3965
3966 stream->bandwidth = bandwidth;
3967 stream->udev = dev;
3968 stream->bEndpointAddress = is_input | epnum;
3969 stream->interval = interval;
3970 stream->maxp = maxp;
3971}
3972
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003973static struct fotg210_iso_stream *iso_stream_find(struct fotg210_hcd *fotg210,
3974 struct urb *urb)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003975{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003976 unsigned epnum;
3977 struct fotg210_iso_stream *stream;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003978 struct usb_host_endpoint *ep;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02003979 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00003980
3981 epnum = usb_pipeendpoint(urb->pipe);
3982 if (usb_pipein(urb->pipe))
3983 ep = urb->dev->ep_in[epnum];
3984 else
3985 ep = urb->dev->ep_out[epnum];
3986
3987 spin_lock_irqsave(&fotg210->lock, flags);
3988 stream = ep->hcpriv;
3989
3990 if (unlikely(stream == NULL)) {
3991 stream = iso_stream_alloc(GFP_ATOMIC);
3992 if (likely(stream != NULL)) {
3993 ep->hcpriv = stream;
3994 stream->ep = ep;
3995 iso_stream_init(fotg210, stream, urb->dev, urb->pipe,
3996 urb->interval);
3997 }
3998
3999 /* if dev->ep[epnum] is a QH, hw is set */
4000 } else if (unlikely(stream->hw != NULL)) {
4001 fotg210_dbg(fotg210, "dev %s ep%d%s, not iso??\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004002 urb->dev->devpath, epnum,
4003 usb_pipein(urb->pipe) ? "in" : "out");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004004 stream = NULL;
4005 }
4006
4007 spin_unlock_irqrestore(&fotg210->lock, flags);
4008 return stream;
4009}
4010
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004011/* fotg210_iso_sched ops can be ITD-only or SITD-only */
4012
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004013static struct fotg210_iso_sched *iso_sched_alloc(unsigned packets,
4014 gfp_t mem_flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004015{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004016 struct fotg210_iso_sched *iso_sched;
4017 int size = sizeof(*iso_sched);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004018
4019 size += packets * sizeof(struct fotg210_iso_packet);
4020 iso_sched = kzalloc(size, mem_flags);
4021 if (likely(iso_sched != NULL))
4022 INIT_LIST_HEAD(&iso_sched->td_list);
4023
4024 return iso_sched;
4025}
4026
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004027static inline void itd_sched_init(struct fotg210_hcd *fotg210,
4028 struct fotg210_iso_sched *iso_sched,
4029 struct fotg210_iso_stream *stream, struct urb *urb)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004030{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004031 unsigned i;
4032 dma_addr_t dma = urb->transfer_dma;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004033
4034 /* how many uframes are needed for these transfers */
4035 iso_sched->span = urb->number_of_packets * stream->interval;
4036
4037 /* figure out per-uframe itd fields that we'll need later
4038 * when we fit new itds into the schedule.
4039 */
4040 for (i = 0; i < urb->number_of_packets; i++) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004041 struct fotg210_iso_packet *uframe = &iso_sched->packet[i];
4042 unsigned length;
4043 dma_addr_t buf;
4044 u32 trans;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004045
4046 length = urb->iso_frame_desc[i].length;
4047 buf = dma + urb->iso_frame_desc[i].offset;
4048
4049 trans = FOTG210_ISOC_ACTIVE;
4050 trans |= buf & 0x0fff;
4051 if (unlikely(((i + 1) == urb->number_of_packets))
4052 && !(urb->transfer_flags & URB_NO_INTERRUPT))
4053 trans |= FOTG210_ITD_IOC;
4054 trans |= length << 16;
4055 uframe->transaction = cpu_to_hc32(fotg210, trans);
4056
4057 /* might need to cross a buffer page within a uframe */
4058 uframe->bufp = (buf & ~(u64)0x0fff);
4059 buf += length;
4060 if (unlikely((uframe->bufp != (buf & ~(u64)0x0fff))))
4061 uframe->cross = 1;
4062 }
4063}
4064
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004065static void iso_sched_free(struct fotg210_iso_stream *stream,
4066 struct fotg210_iso_sched *iso_sched)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004067{
4068 if (!iso_sched)
4069 return;
4070 /* caller must hold fotg210->lock!*/
4071 list_splice(&iso_sched->td_list, &stream->free_list);
4072 kfree(iso_sched);
4073}
4074
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004075static int itd_urb_transaction(struct fotg210_iso_stream *stream,
4076 struct fotg210_hcd *fotg210, struct urb *urb, gfp_t mem_flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004077{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004078 struct fotg210_itd *itd;
4079 dma_addr_t itd_dma;
4080 int i;
4081 unsigned num_itds;
4082 struct fotg210_iso_sched *sched;
4083 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004084
4085 sched = iso_sched_alloc(urb->number_of_packets, mem_flags);
4086 if (unlikely(sched == NULL))
4087 return -ENOMEM;
4088
4089 itd_sched_init(fotg210, sched, stream, urb);
4090
4091 if (urb->interval < 8)
4092 num_itds = 1 + (sched->span + 7) / 8;
4093 else
4094 num_itds = urb->number_of_packets;
4095
4096 /* allocate/init ITDs */
4097 spin_lock_irqsave(&fotg210->lock, flags);
4098 for (i = 0; i < num_itds; i++) {
4099
4100 /*
4101 * Use iTDs from the free list, but not iTDs that may
4102 * still be in use by the hardware.
4103 */
4104 if (likely(!list_empty(&stream->free_list))) {
4105 itd = list_first_entry(&stream->free_list,
4106 struct fotg210_itd, itd_list);
4107 if (itd->frame == fotg210->now_frame)
4108 goto alloc_itd;
4109 list_del(&itd->itd_list);
4110 itd_dma = itd->itd_dma;
4111 } else {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004112alloc_itd:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004113 spin_unlock_irqrestore(&fotg210->lock, flags);
Kelly Devillivc4c1faf2021-06-27 20:57:45 +08004114 itd = dma_pool_alloc(fotg210->itd_pool, mem_flags,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004115 &itd_dma);
4116 spin_lock_irqsave(&fotg210->lock, flags);
4117 if (!itd) {
4118 iso_sched_free(stream, sched);
4119 spin_unlock_irqrestore(&fotg210->lock, flags);
4120 return -ENOMEM;
4121 }
4122 }
4123
Kelly Devillivc4c1faf2021-06-27 20:57:45 +08004124 memset(itd, 0, sizeof(*itd));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004125 itd->itd_dma = itd_dma;
4126 list_add(&itd->itd_list, &sched->td_list);
4127 }
4128 spin_unlock_irqrestore(&fotg210->lock, flags);
4129
4130 /* temporarily store schedule info in hcpriv */
4131 urb->hcpriv = sched;
4132 urb->error_count = 0;
4133 return 0;
4134}
4135
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004136static inline int itd_slot_ok(struct fotg210_hcd *fotg210, u32 mod, u32 uframe,
4137 u8 usecs, u32 period)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004138{
4139 uframe %= period;
4140 do {
4141 /* can't commit more than uframe_periodic_max usec */
4142 if (periodic_usecs(fotg210, uframe >> 3, uframe & 0x7)
4143 > (fotg210->uframe_periodic_max - usecs))
4144 return 0;
4145
4146 /* we know urb->interval is 2^N uframes */
4147 uframe += period;
4148 } while (uframe < mod);
4149 return 1;
4150}
4151
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004152/* This scheduler plans almost as far into the future as it has actual
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004153 * periodic schedule slots. (Affected by TUNE_FLS, which defaults to
4154 * "as small as possible" to be cache-friendlier.) That limits the size
4155 * transfers you can stream reliably; avoid more than 64 msec per urb.
4156 * Also avoid queue depths of less than fotg210's worst irq latency (affected
4157 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
4158 * and other factors); or more than about 230 msec total (for portability,
4159 * given FOTG210_TUNE_FLS and the slop). Or, write a smarter scheduler!
4160 */
4161
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004162#define SCHEDULE_SLOP 80 /* microframes */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004163
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004164static int iso_stream_schedule(struct fotg210_hcd *fotg210, struct urb *urb,
4165 struct fotg210_iso_stream *stream)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004166{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004167 u32 now, next, start, period, span;
4168 int status;
4169 unsigned mod = fotg210->periodic_size << 3;
4170 struct fotg210_iso_sched *sched = urb->hcpriv;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004171
4172 period = urb->interval;
4173 span = sched->span;
4174
4175 if (span > mod - SCHEDULE_SLOP) {
4176 fotg210_dbg(fotg210, "iso request %p too long\n", urb);
4177 status = -EFBIG;
4178 goto fail;
4179 }
4180
4181 now = fotg210_read_frame_index(fotg210) & (mod - 1);
4182
4183 /* Typical case: reuse current schedule, stream is still active.
4184 * Hopefully there are no gaps from the host falling behind
4185 * (irq delays etc), but if there are we'll take the next
4186 * slot in the schedule, implicitly assuming URB_ISO_ASAP.
4187 */
4188 if (likely(!list_empty(&stream->td_list))) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004189 u32 excess;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004190
4191 /* For high speed devices, allow scheduling within the
4192 * isochronous scheduling threshold. For full speed devices
4193 * and Intel PCI-based controllers, don't (work around for
4194 * Intel ICH9 bug).
4195 */
4196 if (!stream->highspeed && fotg210->fs_i_thresh)
4197 next = now + fotg210->i_thresh;
4198 else
4199 next = now;
4200
4201 /* Fell behind (by up to twice the slop amount)?
4202 * We decide based on the time of the last currently-scheduled
4203 * slot, not the time of the next available slot.
4204 */
4205 excess = (stream->next_uframe - period - next) & (mod - 1);
4206 if (excess >= mod - 2 * SCHEDULE_SLOP)
4207 start = next + excess - mod + period *
4208 DIV_ROUND_UP(mod - excess, period);
4209 else
4210 start = next + excess + period;
4211 if (start - now >= mod) {
4212 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
4213 urb, start - now - period, period,
4214 mod);
4215 status = -EFBIG;
4216 goto fail;
4217 }
4218 }
4219
4220 /* need to schedule; when's the next (u)frame we could start?
4221 * this is bigger than fotg210->i_thresh allows; scheduling itself
4222 * isn't free, the slop should handle reasonably slow cpus. it
4223 * can also help high bandwidth if the dma and irq loads don't
4224 * jump until after the queue is primed.
4225 */
4226 else {
4227 int done = 0;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004228
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004229 start = SCHEDULE_SLOP + (now & ~0x07);
4230
4231 /* NOTE: assumes URB_ISO_ASAP, to limit complexity/bugs */
4232
4233 /* find a uframe slot with enough bandwidth.
4234 * Early uframes are more precious because full-speed
4235 * iso IN transfers can't use late uframes,
4236 * and therefore they should be allocated last.
4237 */
4238 next = start;
4239 start += period;
4240 do {
4241 start--;
4242 /* check schedule: enough space? */
4243 if (itd_slot_ok(fotg210, mod, start,
4244 stream->usecs, period))
4245 done = 1;
4246 } while (start > next && !done);
4247
4248 /* no room in the schedule */
4249 if (!done) {
4250 fotg210_dbg(fotg210, "iso resched full %p (now %d max %d)\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004251 urb, now, now + mod);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004252 status = -ENOSPC;
4253 goto fail;
4254 }
4255 }
4256
4257 /* Tried to schedule too far into the future? */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004258 if (unlikely(start - now + span - period >=
4259 mod - 2 * SCHEDULE_SLOP)) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004260 fotg210_dbg(fotg210, "request %p would overflow (%d+%d >= %d)\n",
4261 urb, start - now, span - period,
4262 mod - 2 * SCHEDULE_SLOP);
4263 status = -EFBIG;
4264 goto fail;
4265 }
4266
4267 stream->next_uframe = start & (mod - 1);
4268
4269 /* report high speed start in uframes; full speed, in frames */
4270 urb->start_frame = stream->next_uframe;
4271 if (!stream->highspeed)
4272 urb->start_frame >>= 3;
4273
4274 /* Make sure scan_isoc() sees these */
4275 if (fotg210->isoc_count == 0)
4276 fotg210->next_frame = now >> 3;
4277 return 0;
4278
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004279fail:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004280 iso_sched_free(stream, sched);
4281 urb->hcpriv = NULL;
4282 return status;
4283}
4284
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004285static inline void itd_init(struct fotg210_hcd *fotg210,
4286 struct fotg210_iso_stream *stream, struct fotg210_itd *itd)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004287{
4288 int i;
4289
4290 /* it's been recently zeroed */
4291 itd->hw_next = FOTG210_LIST_END(fotg210);
4292 itd->hw_bufp[0] = stream->buf0;
4293 itd->hw_bufp[1] = stream->buf1;
4294 itd->hw_bufp[2] = stream->buf2;
4295
4296 for (i = 0; i < 8; i++)
4297 itd->index[i] = -1;
4298
4299 /* All other fields are filled when scheduling */
4300}
4301
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004302static inline void itd_patch(struct fotg210_hcd *fotg210,
4303 struct fotg210_itd *itd, struct fotg210_iso_sched *iso_sched,
4304 unsigned index, u16 uframe)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004305{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004306 struct fotg210_iso_packet *uf = &iso_sched->packet[index];
4307 unsigned pg = itd->pg;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004308
4309 uframe &= 0x07;
4310 itd->index[uframe] = index;
4311
4312 itd->hw_transaction[uframe] = uf->transaction;
4313 itd->hw_transaction[uframe] |= cpu_to_hc32(fotg210, pg << 12);
4314 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, uf->bufp & ~(u32)0);
4315 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(uf->bufp >> 32));
4316
4317 /* iso_frame_desc[].offset must be strictly increasing */
4318 if (unlikely(uf->cross)) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004319 u64 bufp = uf->bufp + 4096;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004320
4321 itd->pg = ++pg;
4322 itd->hw_bufp[pg] |= cpu_to_hc32(fotg210, bufp & ~(u32)0);
4323 itd->hw_bufp_hi[pg] |= cpu_to_hc32(fotg210, (u32)(bufp >> 32));
4324 }
4325}
4326
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004327static inline void itd_link(struct fotg210_hcd *fotg210, unsigned frame,
4328 struct fotg210_itd *itd)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004329{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004330 union fotg210_shadow *prev = &fotg210->pshadow[frame];
4331 __hc32 *hw_p = &fotg210->periodic[frame];
4332 union fotg210_shadow here = *prev;
4333 __hc32 type = 0;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004334
4335 /* skip any iso nodes which might belong to previous microframes */
4336 while (here.ptr) {
4337 type = Q_NEXT_TYPE(fotg210, *hw_p);
4338 if (type == cpu_to_hc32(fotg210, Q_TYPE_QH))
4339 break;
4340 prev = periodic_next_shadow(fotg210, prev, type);
4341 hw_p = shadow_next_periodic(fotg210, &here, type);
4342 here = *prev;
4343 }
4344
4345 itd->itd_next = here;
4346 itd->hw_next = *hw_p;
4347 prev->itd = itd;
4348 itd->frame = frame;
4349 wmb();
4350 *hw_p = cpu_to_hc32(fotg210, itd->itd_dma | Q_TYPE_ITD);
4351}
4352
4353/* fit urb's itds into the selected schedule slot; activate as needed */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004354static void itd_link_urb(struct fotg210_hcd *fotg210, struct urb *urb,
4355 unsigned mod, struct fotg210_iso_stream *stream)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004356{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004357 int packet;
4358 unsigned next_uframe, uframe, frame;
4359 struct fotg210_iso_sched *iso_sched = urb->hcpriv;
4360 struct fotg210_itd *itd;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004361
4362 next_uframe = stream->next_uframe & (mod - 1);
4363
4364 if (unlikely(list_empty(&stream->td_list))) {
4365 fotg210_to_hcd(fotg210)->self.bandwidth_allocated
4366 += stream->bandwidth;
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01004367 fotg210_dbg(fotg210,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004368 "schedule devp %s ep%d%s-iso period %d start %d.%d\n",
4369 urb->dev->devpath, stream->bEndpointAddress & 0x0f,
4370 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
4371 urb->interval,
4372 next_uframe >> 3, next_uframe & 0x7);
4373 }
4374
4375 /* fill iTDs uframe by uframe */
4376 for (packet = 0, itd = NULL; packet < urb->number_of_packets;) {
4377 if (itd == NULL) {
4378 /* ASSERT: we have all necessary itds */
4379
4380 /* ASSERT: no itds for this endpoint in this uframe */
4381
4382 itd = list_entry(iso_sched->td_list.next,
4383 struct fotg210_itd, itd_list);
4384 list_move_tail(&itd->itd_list, &stream->td_list);
4385 itd->stream = stream;
4386 itd->urb = urb;
4387 itd_init(fotg210, stream, itd);
4388 }
4389
4390 uframe = next_uframe & 0x07;
4391 frame = next_uframe >> 3;
4392
4393 itd_patch(fotg210, itd, iso_sched, packet, uframe);
4394
4395 next_uframe += stream->interval;
4396 next_uframe &= mod - 1;
4397 packet++;
4398
4399 /* link completed itds into the schedule */
4400 if (((next_uframe >> 3) != frame)
4401 || packet == urb->number_of_packets) {
4402 itd_link(fotg210, frame & (fotg210->periodic_size - 1),
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004403 itd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004404 itd = NULL;
4405 }
4406 }
4407 stream->next_uframe = next_uframe;
4408
4409 /* don't need that schedule data any more */
4410 iso_sched_free(stream, iso_sched);
4411 urb->hcpriv = NULL;
4412
4413 ++fotg210->isoc_count;
4414 enable_periodic(fotg210);
4415}
4416
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004417#define ISO_ERRS (FOTG210_ISOC_BUF_ERR | FOTG210_ISOC_BABBLE |\
4418 FOTG210_ISOC_XACTERR)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004419
4420/* Process and recycle a completed ITD. Return true iff its urb completed,
4421 * and hence its completion callback probably added things to the hardware
4422 * schedule.
4423 *
4424 * Note that we carefully avoid recycling this descriptor until after any
4425 * completion callback runs, so that it won't be reused quickly. That is,
4426 * assuming (a) no more than two urbs per frame on this endpoint, and also
4427 * (b) only this endpoint's completions submit URBs. It seems some silicon
4428 * corrupts things if you reuse completed descriptors very quickly...
4429 */
4430static bool itd_complete(struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
4431{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004432 struct urb *urb = itd->urb;
4433 struct usb_iso_packet_descriptor *desc;
4434 u32 t;
4435 unsigned uframe;
4436 int urb_index = -1;
4437 struct fotg210_iso_stream *stream = itd->stream;
4438 struct usb_device *dev;
4439 bool retval = false;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004440
4441 /* for each uframe with a packet */
4442 for (uframe = 0; uframe < 8; uframe++) {
4443 if (likely(itd->index[uframe] == -1))
4444 continue;
4445 urb_index = itd->index[uframe];
4446 desc = &urb->iso_frame_desc[urb_index];
4447
4448 t = hc32_to_cpup(fotg210, &itd->hw_transaction[uframe]);
4449 itd->hw_transaction[uframe] = 0;
4450
4451 /* report transfer status */
4452 if (unlikely(t & ISO_ERRS)) {
4453 urb->error_count++;
4454 if (t & FOTG210_ISOC_BUF_ERR)
4455 desc->status = usb_pipein(urb->pipe)
4456 ? -ENOSR /* hc couldn't read */
4457 : -ECOMM; /* hc couldn't write */
4458 else if (t & FOTG210_ISOC_BABBLE)
4459 desc->status = -EOVERFLOW;
4460 else /* (t & FOTG210_ISOC_XACTERR) */
4461 desc->status = -EPROTO;
4462
4463 /* HC need not update length with this error */
4464 if (!(t & FOTG210_ISOC_BABBLE)) {
Kelly Devilliv091cb2f2021-06-27 20:57:47 +08004465 desc->actual_length = FOTG210_ITD_LENGTH(t);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004466 urb->actual_length += desc->actual_length;
4467 }
4468 } else if (likely((t & FOTG210_ISOC_ACTIVE) == 0)) {
4469 desc->status = 0;
Kelly Devilliv091cb2f2021-06-27 20:57:47 +08004470 desc->actual_length = FOTG210_ITD_LENGTH(t);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004471 urb->actual_length += desc->actual_length;
4472 } else {
4473 /* URB was too late */
4474 desc->status = -EXDEV;
4475 }
4476 }
4477
4478 /* handle completion now? */
4479 if (likely((urb_index + 1) != urb->number_of_packets))
4480 goto done;
4481
4482 /* ASSERT: it's really the last itd for this urb
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004483 * list_for_each_entry (itd, &stream->td_list, itd_list)
4484 * BUG_ON (itd->urb == urb);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004485 */
4486
4487 /* give urb back to the driver; completion often (re)submits */
4488 dev = urb->dev;
4489 fotg210_urb_done(fotg210, urb, 0);
4490 retval = true;
4491 urb = NULL;
4492
4493 --fotg210->isoc_count;
4494 disable_periodic(fotg210);
4495
4496 if (unlikely(list_is_singular(&stream->td_list))) {
4497 fotg210_to_hcd(fotg210)->self.bandwidth_allocated
4498 -= stream->bandwidth;
Oliver Neukumbe5ac4c2013-11-18 13:23:07 +01004499 fotg210_dbg(fotg210,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004500 "deschedule devp %s ep%d%s-iso\n",
4501 dev->devpath, stream->bEndpointAddress & 0x0f,
4502 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
4503 }
4504
4505done:
4506 itd->urb = NULL;
4507
4508 /* Add to the end of the free list for later reuse */
4509 list_move_tail(&itd->itd_list, &stream->free_list);
4510
4511 /* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
4512 if (list_empty(&stream->td_list)) {
4513 list_splice_tail_init(&stream->free_list,
4514 &fotg210->cached_itd_list);
4515 start_free_itds(fotg210);
4516 }
4517
4518 return retval;
4519}
4520
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004521static int itd_submit(struct fotg210_hcd *fotg210, struct urb *urb,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004522 gfp_t mem_flags)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004523{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004524 int status = -EINVAL;
4525 unsigned long flags;
4526 struct fotg210_iso_stream *stream;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004527
4528 /* Get iso_stream head */
4529 stream = iso_stream_find(fotg210, urb);
4530 if (unlikely(stream == NULL)) {
4531 fotg210_dbg(fotg210, "can't get iso stream\n");
4532 return -ENOMEM;
4533 }
4534 if (unlikely(urb->interval != stream->interval &&
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004535 fotg210_port_speed(fotg210, 0) ==
4536 USB_PORT_STAT_HIGH_SPEED)) {
4537 fotg210_dbg(fotg210, "can't change iso interval %d --> %d\n",
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004538 stream->interval, urb->interval);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004539 goto done;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004540 }
4541
4542#ifdef FOTG210_URB_TRACE
4543 fotg210_dbg(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004544 "%s %s urb %p ep%d%s len %d, %d pkts %d uframes[%p]\n",
4545 __func__, urb->dev->devpath, urb,
4546 usb_pipeendpoint(urb->pipe),
4547 usb_pipein(urb->pipe) ? "in" : "out",
4548 urb->transfer_buffer_length,
4549 urb->number_of_packets, urb->interval,
4550 stream);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004551#endif
4552
4553 /* allocate ITDs w/o locking anything */
4554 status = itd_urb_transaction(stream, fotg210, urb, mem_flags);
4555 if (unlikely(status < 0)) {
4556 fotg210_dbg(fotg210, "can't init itds\n");
4557 goto done;
4558 }
4559
4560 /* schedule ... need to lock */
4561 spin_lock_irqsave(&fotg210->lock, flags);
4562 if (unlikely(!HCD_HW_ACCESSIBLE(fotg210_to_hcd(fotg210)))) {
4563 status = -ESHUTDOWN;
4564 goto done_not_linked;
4565 }
4566 status = usb_hcd_link_urb_to_ep(fotg210_to_hcd(fotg210), urb);
4567 if (unlikely(status))
4568 goto done_not_linked;
4569 status = iso_stream_schedule(fotg210, urb, stream);
4570 if (likely(status == 0))
4571 itd_link_urb(fotg210, urb, fotg210->periodic_size << 3, stream);
4572 else
4573 usb_hcd_unlink_urb_from_ep(fotg210_to_hcd(fotg210), urb);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004574done_not_linked:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004575 spin_unlock_irqrestore(&fotg210->lock, flags);
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004576done:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004577 return status;
4578}
4579
Peter Senna Tschudin50bdb122015-10-12 23:22:40 +02004580static inline int scan_frame_queue(struct fotg210_hcd *fotg210, unsigned frame,
4581 unsigned now_frame, bool live)
4582{
4583 unsigned uf;
4584 bool modified;
4585 union fotg210_shadow q, *q_p;
4586 __hc32 type, *hw_p;
4587
4588 /* scan each element in frame's queue for completions */
4589 q_p = &fotg210->pshadow[frame];
4590 hw_p = &fotg210->periodic[frame];
4591 q.ptr = q_p->ptr;
4592 type = Q_NEXT_TYPE(fotg210, *hw_p);
4593 modified = false;
4594
4595 while (q.ptr) {
4596 switch (hc32_to_cpu(fotg210, type)) {
4597 case Q_TYPE_ITD:
4598 /* If this ITD is still active, leave it for
4599 * later processing ... check the next entry.
4600 * No need to check for activity unless the
4601 * frame is current.
4602 */
4603 if (frame == now_frame && live) {
4604 rmb();
4605 for (uf = 0; uf < 8; uf++) {
4606 if (q.itd->hw_transaction[uf] &
4607 ITD_ACTIVE(fotg210))
4608 break;
4609 }
4610 if (uf < 8) {
4611 q_p = &q.itd->itd_next;
4612 hw_p = &q.itd->hw_next;
4613 type = Q_NEXT_TYPE(fotg210,
4614 q.itd->hw_next);
4615 q = *q_p;
4616 break;
4617 }
4618 }
4619
4620 /* Take finished ITDs out of the schedule
4621 * and process them: recycle, maybe report
4622 * URB completion. HC won't cache the
4623 * pointer for much longer, if at all.
4624 */
4625 *q_p = q.itd->itd_next;
4626 *hw_p = q.itd->hw_next;
4627 type = Q_NEXT_TYPE(fotg210, q.itd->hw_next);
4628 wmb();
4629 modified = itd_complete(fotg210, q.itd);
4630 q = *q_p;
4631 break;
4632 default:
4633 fotg210_dbg(fotg210, "corrupt type %d frame %d shadow %p\n",
4634 type, frame, q.ptr);
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05004635 fallthrough;
Peter Senna Tschudin50bdb122015-10-12 23:22:40 +02004636 case Q_TYPE_QH:
4637 case Q_TYPE_FSTN:
4638 /* End of the iTDs and siTDs */
4639 q.ptr = NULL;
4640 break;
4641 }
4642
4643 /* assume completion callbacks modify the queue */
4644 if (unlikely(modified && fotg210->isoc_count > 0))
4645 return -EINVAL;
4646 }
4647 return 0;
4648}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004649
4650static void scan_isoc(struct fotg210_hcd *fotg210)
4651{
Peter Senna Tschudin50bdb122015-10-12 23:22:40 +02004652 unsigned uf, now_frame, frame, ret;
4653 unsigned fmask = fotg210->periodic_size - 1;
4654 bool live;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004655
4656 /*
4657 * When running, scan from last scan point up to "now"
4658 * else clean up by scanning everything that's left.
4659 * Touches as few pages as possible: cache-friendly.
4660 */
4661 if (fotg210->rh_state >= FOTG210_RH_RUNNING) {
4662 uf = fotg210_read_frame_index(fotg210);
4663 now_frame = (uf >> 3) & fmask;
4664 live = true;
4665 } else {
4666 now_frame = (fotg210->next_frame - 1) & fmask;
4667 live = false;
4668 }
4669 fotg210->now_frame = now_frame;
4670
4671 frame = fotg210->next_frame;
4672 for (;;) {
Peter Senna Tschudin50bdb122015-10-12 23:22:40 +02004673 ret = 1;
4674 while (ret != 0)
4675 ret = scan_frame_queue(fotg210, frame,
4676 now_frame, live);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004677
4678 /* Stop when we have reached the current frame */
4679 if (frame == now_frame)
4680 break;
4681 frame = (frame + 1) & fmask;
4682 }
4683 fotg210->next_frame = now_frame;
4684}
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004685
4686/* Display / Set uframe_periodic_max
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004687 */
Greg Kroah-Hartmaned5bd7a2018-01-23 11:24:05 +01004688static ssize_t uframe_periodic_max_show(struct device *dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004689 struct device_attribute *attr, char *buf)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004690{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004691 struct fotg210_hcd *fotg210;
4692 int n;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004693
4694 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4695 n = scnprintf(buf, PAGE_SIZE, "%d\n", fotg210->uframe_periodic_max);
4696 return n;
4697}
4698
4699
Greg Kroah-Hartmaned5bd7a2018-01-23 11:24:05 +01004700static ssize_t uframe_periodic_max_store(struct device *dev,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004701 struct device_attribute *attr, const char *buf, size_t count)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004702{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004703 struct fotg210_hcd *fotg210;
4704 unsigned uframe_periodic_max;
4705 unsigned frame, uframe;
4706 unsigned short allocated_max;
4707 unsigned long flags;
4708 ssize_t ret;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004709
4710 fotg210 = hcd_to_fotg210(bus_to_hcd(dev_get_drvdata(dev)));
4711 if (kstrtouint(buf, 0, &uframe_periodic_max) < 0)
4712 return -EINVAL;
4713
4714 if (uframe_periodic_max < 100 || uframe_periodic_max >= 125) {
4715 fotg210_info(fotg210, "rejecting invalid request for uframe_periodic_max=%u\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004716 uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004717 return -EINVAL;
4718 }
4719
4720 ret = -EINVAL;
4721
4722 /*
4723 * lock, so that our checking does not race with possible periodic
4724 * bandwidth allocation through submitting new urbs.
4725 */
4726 spin_lock_irqsave(&fotg210->lock, flags);
4727
4728 /*
4729 * for request to decrease max periodic bandwidth, we have to check
4730 * every microframe in the schedule to see whether the decrease is
4731 * possible.
4732 */
4733 if (uframe_periodic_max < fotg210->uframe_periodic_max) {
4734 allocated_max = 0;
4735
4736 for (frame = 0; frame < fotg210->periodic_size; ++frame)
4737 for (uframe = 0; uframe < 7; ++uframe)
4738 allocated_max = max(allocated_max,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004739 periodic_usecs(fotg210, frame,
4740 uframe));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004741
4742 if (allocated_max > uframe_periodic_max) {
4743 fotg210_info(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004744 "cannot decrease uframe_periodic_max because periodic bandwidth is already allocated (%u > %u)\n",
4745 allocated_max, uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004746 goto out_unlock;
4747 }
4748 }
4749
4750 /* increasing is always ok */
4751
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004752 fotg210_info(fotg210,
4753 "setting max periodic bandwidth to %u%% (== %u usec/uframe)\n",
4754 100 * uframe_periodic_max/125, uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004755
4756 if (uframe_periodic_max != 100)
4757 fotg210_warn(fotg210, "max periodic bandwidth set is non-standard\n");
4758
4759 fotg210->uframe_periodic_max = uframe_periodic_max;
4760 ret = count;
4761
4762out_unlock:
4763 spin_unlock_irqrestore(&fotg210->lock, flags);
4764 return ret;
4765}
4766
Greg Kroah-Hartmaned5bd7a2018-01-23 11:24:05 +01004767static DEVICE_ATTR_RW(uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004768
4769static inline int create_sysfs_files(struct fotg210_hcd *fotg210)
4770{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004771 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004772
Colin Ian King9f3b3f62016-05-08 20:14:29 +01004773 return device_create_file(controller, &dev_attr_uframe_periodic_max);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004774}
4775
4776static inline void remove_sysfs_files(struct fotg210_hcd *fotg210)
4777{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004778 struct device *controller = fotg210_to_hcd(fotg210)->self.controller;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004779
4780 device_remove_file(controller, &dev_attr_uframe_periodic_max);
4781}
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004782/* On some systems, leaving remote wakeup enabled prevents system shutdown.
4783 * The firmware seems to think that powering off is a wakeup event!
4784 * This routine turns off remote wakeup and everything else, on all ports.
4785 */
4786static void fotg210_turn_off_all_ports(struct fotg210_hcd *fotg210)
4787{
4788 u32 __iomem *status_reg = &fotg210->regs->port_status;
4789
4790 fotg210_writel(fotg210, PORT_RWC_BITS, status_reg);
4791}
4792
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004793/* Halt HC, turn off all ports, and let the BIOS use the companion controllers.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004794 * Must be called with interrupts enabled and the lock not held.
4795 */
4796static void fotg210_silence_controller(struct fotg210_hcd *fotg210)
4797{
4798 fotg210_halt(fotg210);
4799
4800 spin_lock_irq(&fotg210->lock);
4801 fotg210->rh_state = FOTG210_RH_HALTED;
4802 fotg210_turn_off_all_ports(fotg210);
4803 spin_unlock_irq(&fotg210->lock);
4804}
4805
4806/* fotg210_shutdown kick in for silicon on any bus (not just pci, etc).
4807 * This forcibly disables dma and IRQs, helping kexec and other cases
4808 * where the next system software may expect clean state.
4809 */
4810static void fotg210_shutdown(struct usb_hcd *hcd)
4811{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004812 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004813
4814 spin_lock_irq(&fotg210->lock);
4815 fotg210->shutdown = true;
4816 fotg210->rh_state = FOTG210_RH_STOPPING;
4817 fotg210->enabled_hrtimer_events = 0;
4818 spin_unlock_irq(&fotg210->lock);
4819
4820 fotg210_silence_controller(fotg210);
4821
4822 hrtimer_cancel(&fotg210->hrtimer);
4823}
4824
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004825/* fotg210_work is called from some interrupts, timers, and so on.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004826 * it calls driver completion functions, after dropping fotg210->lock.
4827 */
4828static void fotg210_work(struct fotg210_hcd *fotg210)
4829{
4830 /* another CPU may drop fotg210->lock during a schedule scan while
4831 * it reports urb completions. this flag guards against bogus
4832 * attempts at re-entrant schedule scanning.
4833 */
4834 if (fotg210->scanning) {
4835 fotg210->need_rescan = true;
4836 return;
4837 }
4838 fotg210->scanning = true;
4839
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004840rescan:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004841 fotg210->need_rescan = false;
4842 if (fotg210->async_count)
4843 scan_async(fotg210);
4844 if (fotg210->intr_count > 0)
4845 scan_intr(fotg210);
4846 if (fotg210->isoc_count > 0)
4847 scan_isoc(fotg210);
4848 if (fotg210->need_rescan)
4849 goto rescan;
4850 fotg210->scanning = false;
4851
4852 /* the IO watchdog guards against hardware or driver bugs that
4853 * misplace IRQs, and should let us run completely without IRQs.
4854 * such lossage has been observed on both VT6202 and VT8235.
4855 */
4856 turn_on_io_watchdog(fotg210);
4857}
4858
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004859/* Called when the fotg210_hcd module is removed.
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004860 */
4861static void fotg210_stop(struct usb_hcd *hcd)
4862{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004863 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004864
4865 fotg210_dbg(fotg210, "stop\n");
4866
4867 /* no more interrupts ... */
4868
4869 spin_lock_irq(&fotg210->lock);
4870 fotg210->enabled_hrtimer_events = 0;
4871 spin_unlock_irq(&fotg210->lock);
4872
4873 fotg210_quiesce(fotg210);
4874 fotg210_silence_controller(fotg210);
4875 fotg210_reset(fotg210);
4876
4877 hrtimer_cancel(&fotg210->hrtimer);
4878 remove_sysfs_files(fotg210);
4879 remove_debug_files(fotg210);
4880
4881 /* root hub is shut down separately (first, when possible) */
4882 spin_lock_irq(&fotg210->lock);
4883 end_free_itds(fotg210);
4884 spin_unlock_irq(&fotg210->lock);
4885 fotg210_mem_cleanup(fotg210);
4886
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004887#ifdef FOTG210_STATS
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004888 fotg210_dbg(fotg210, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004889 fotg210->stats.normal, fotg210->stats.error,
4890 fotg210->stats.iaa, fotg210->stats.lost_iaa);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004891 fotg210_dbg(fotg210, "complete %ld unlink %ld\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004892 fotg210->stats.complete, fotg210->stats.unlink);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004893#endif
4894
4895 dbg_status(fotg210, "fotg210_stop completed",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004896 fotg210_readl(fotg210, &fotg210->regs->status));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004897}
4898
4899/* one-time init, only for memory state */
4900static int hcd_fotg210_init(struct usb_hcd *hcd)
4901{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02004902 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
4903 u32 temp;
4904 int retval;
4905 u32 hcc_params;
4906 struct fotg210_qh_hw *hw;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00004907
4908 spin_lock_init(&fotg210->lock);
4909
4910 /*
4911 * keep io watchdog by default, those good HCDs could turn off it later
4912 */
4913 fotg210->need_io_watchdog = 1;
4914
4915 hrtimer_init(&fotg210->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
4916 fotg210->hrtimer.function = fotg210_hrtimer_func;
4917 fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;
4918
4919 hcc_params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
4920
4921 /*
4922 * by default set standard 80% (== 100 usec/uframe) max periodic
4923 * bandwidth as required by USB 2.0
4924 */
4925 fotg210->uframe_periodic_max = 100;
4926
4927 /*
4928 * hw default: 1K periodic list heads, one per frame.
4929 * periodic_size can shrink by USBCMD update if hcc_params allows.
4930 */
4931 fotg210->periodic_size = DEFAULT_I_TDPS;
4932 INIT_LIST_HEAD(&fotg210->intr_qh_list);
4933 INIT_LIST_HEAD(&fotg210->cached_itd_list);
4934
4935 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
4936 /* periodic schedule size can be smaller than default */
4937 switch (FOTG210_TUNE_FLS) {
4938 case 0:
4939 fotg210->periodic_size = 1024;
4940 break;
4941 case 1:
4942 fotg210->periodic_size = 512;
4943 break;
4944 case 2:
4945 fotg210->periodic_size = 256;
4946 break;
4947 default:
4948 BUG();
4949 }
4950 }
4951 retval = fotg210_mem_init(fotg210, GFP_KERNEL);
4952 if (retval < 0)
4953 return retval;
4954
4955 /* controllers may cache some of the periodic schedule ... */
4956 fotg210->i_thresh = 2;
4957
4958 /*
4959 * dedicate a qh for the async ring head, since we couldn't unlink
4960 * a 'real' qh without stopping the async schedule [4.8]. use it
4961 * as the 'reclamation list head' too.
4962 * its dummy is used in hw_alt_next of many tds, to prevent the qh
4963 * from automatically advancing to the next td after short reads.
4964 */
4965 fotg210->async->qh_next.qh = NULL;
4966 hw = fotg210->async->hw;
4967 hw->hw_next = QH_NEXT(fotg210, fotg210->async->qh_dma);
4968 hw->hw_info1 = cpu_to_hc32(fotg210, QH_HEAD);
4969 hw->hw_token = cpu_to_hc32(fotg210, QTD_STS_HALT);
4970 hw->hw_qtd_next = FOTG210_LIST_END(fotg210);
4971 fotg210->async->qh_state = QH_STATE_LINKED;
4972 hw->hw_alt_next = QTD_NEXT(fotg210, fotg210->async->dummy->qtd_dma);
4973
4974 /* clear interrupt enables, set irq latency */
4975 if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
4976 log2_irq_thresh = 0;
4977 temp = 1 << (16 + log2_irq_thresh);
4978 if (HCC_CANPARK(hcc_params)) {
4979 /* HW default park == 3, on hardware that supports it (like
4980 * NVidia and ALI silicon), maximizes throughput on the async
4981 * schedule by avoiding QH fetches between transfers.
4982 *
4983 * With fast usb storage devices and NForce2, "park" seems to
4984 * make problems: throughput reduction (!), data errors...
4985 */
4986 if (park) {
4987 park = min_t(unsigned, park, 3);
4988 temp |= CMD_PARK;
4989 temp |= park << 8;
4990 }
4991 fotg210_dbg(fotg210, "park %d\n", park);
4992 }
4993 if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
4994 /* periodic schedule size can be smaller than default */
4995 temp &= ~(3 << 2);
4996 temp |= (FOTG210_TUNE_FLS << 2);
4997 }
4998 fotg210->command = temp;
4999
5000 /* Accept arbitrarily long scatter-gather lists */
Laurentiu Tudor2d7a3dc2019-05-29 13:28:43 +03005001 if (!hcd->localmem_pool)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005002 hcd->self.sg_tablesize = ~0;
5003 return 0;
5004}
5005
5006/* start HC running; it's halted, hcd_fotg210_init() has been run (once) */
5007static int fotg210_run(struct usb_hcd *hcd)
5008{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005009 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5010 u32 temp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005011
5012 hcd->uses_new_polling = 1;
5013
5014 /* EHCI spec section 4.1 */
5015
5016 fotg210_writel(fotg210, fotg210->periodic_dma,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005017 &fotg210->regs->frame_list);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005018 fotg210_writel(fotg210, (u32)fotg210->async->qh_dma,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005019 &fotg210->regs->async_next);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005020
5021 /*
5022 * hcc_params controls whether fotg210->regs->segment must (!!!)
5023 * be used; it constrains QH/ITD/SITD and QTD locations.
Romain Perier324c54f2017-03-08 17:19:56 +01005024 * dma_pool consistent memory always uses segment zero.
Cai Huoqing4b0f13e2021-09-25 20:49:18 +08005025 * streaming mappings for I/O buffers, like dma_map_single(),
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005026 * can return segments above 4GB, if the device allows.
5027 *
Christoph Hellwig0e77ace2015-11-09 14:58:18 -08005028 * NOTE: the dma mask is visible through dev->dma_mask, so
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005029 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
5030 * Scsi_Host.highmem_io, and so forth. It's readonly to all
5031 * host side drivers though.
5032 */
Lee Jones522514e2020-07-06 14:33:27 +01005033 fotg210_readl(fotg210, &fotg210->caps->hcc_params);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005034
5035 /*
5036 * Philips, Intel, and maybe others need CMD_RUN before the
5037 * root hub will detect new devices (why?); NEC doesn't
5038 */
5039 fotg210->command &= ~(CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
5040 fotg210->command |= CMD_RUN;
5041 fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
5042 dbg_cmd(fotg210, "init", fotg210->command);
5043
5044 /*
5045 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
5046 * are explicitly handed to companion controller(s), so no TT is
5047 * involved with the root hub. (Except where one is integrated,
5048 * and there's no companion controller unless maybe for USB OTG.)
5049 *
5050 * Turning on the CF flag will transfer ownership of all ports
5051 * from the companions to the EHCI controller. If any of the
5052 * companions are in the middle of a port reset at the time, it
5053 * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
5054 * guarantees that no resets are in progress. After we set CF,
5055 * a short delay lets the hardware catch up; new resets shouldn't
5056 * be started before the port switching actions could complete.
5057 */
5058 down_write(&ehci_cf_port_reset_rwsem);
5059 fotg210->rh_state = FOTG210_RH_RUNNING;
5060 /* unblock posted writes */
5061 fotg210_readl(fotg210, &fotg210->regs->command);
Peter Senna Tschudin05ebc362015-10-12 23:22:37 +02005062 usleep_range(5000, 10000);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005063 up_write(&ehci_cf_port_reset_rwsem);
5064 fotg210->last_periodic_enable = ktime_get_real();
5065
5066 temp = HC_VERSION(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005067 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005068 fotg210_info(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005069 "USB %x.%x started, EHCI %x.%02x\n",
5070 ((fotg210->sbrn & 0xf0) >> 4), (fotg210->sbrn & 0x0f),
5071 temp >> 8, temp & 0xff);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005072
5073 fotg210_writel(fotg210, INTR_MASK,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005074 &fotg210->regs->intr_enable); /* Turn On Interrupts */
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005075
5076 /* GRR this is run-once init(), being done every time the HC starts.
5077 * So long as they're part of class devices, we can't do it init()
5078 * since the class device isn't created that early.
5079 */
5080 create_debug_files(fotg210);
5081 create_sysfs_files(fotg210);
5082
5083 return 0;
5084}
5085
5086static int fotg210_setup(struct usb_hcd *hcd)
5087{
5088 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5089 int retval;
5090
5091 fotg210->regs = (void __iomem *)fotg210->caps +
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005092 HC_LENGTH(fotg210,
5093 fotg210_readl(fotg210, &fotg210->caps->hc_capbase));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005094 dbg_hcs_params(fotg210, "reset");
5095 dbg_hcc_params(fotg210, "reset");
5096
5097 /* cache this readonly data; minimize chip reads */
5098 fotg210->hcs_params = fotg210_readl(fotg210,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005099 &fotg210->caps->hcs_params);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005100
5101 fotg210->sbrn = HCD_USB2;
5102
5103 /* data structure init */
5104 retval = hcd_fotg210_init(hcd);
5105 if (retval)
5106 return retval;
5107
5108 retval = fotg210_halt(fotg210);
5109 if (retval)
5110 return retval;
5111
5112 fotg210_reset(fotg210);
5113
5114 return 0;
5115}
5116
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005117static irqreturn_t fotg210_irq(struct usb_hcd *hcd)
5118{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005119 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5120 u32 status, masked_status, pcd_status = 0, cmd;
5121 int bh;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005122
5123 spin_lock(&fotg210->lock);
5124
5125 status = fotg210_readl(fotg210, &fotg210->regs->status);
5126
5127 /* e.g. cardbus physical eject */
5128 if (status == ~(u32) 0) {
5129 fotg210_dbg(fotg210, "device removed\n");
5130 goto dead;
5131 }
5132
5133 /*
5134 * We don't use STS_FLR, but some controllers don't like it to
5135 * remain on, so mask it out along with the other status bits.
5136 */
5137 masked_status = status & (INTR_MASK | STS_FLR);
5138
5139 /* Shared IRQ? */
5140 if (!masked_status ||
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005141 unlikely(fotg210->rh_state == FOTG210_RH_HALTED)) {
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005142 spin_unlock(&fotg210->lock);
5143 return IRQ_NONE;
5144 }
5145
5146 /* clear (just) interrupts */
5147 fotg210_writel(fotg210, masked_status, &fotg210->regs->status);
5148 cmd = fotg210_readl(fotg210, &fotg210->regs->command);
5149 bh = 0;
5150
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005151 /* unrequested/ignored: Frame List Rollover */
5152 dbg_status(fotg210, "irq", status);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005153
5154 /* INT, ERR, and IAA interrupt rates can be throttled */
5155
5156 /* normal [4.15.1.2] or error [4.15.1.1] completion */
5157 if (likely((status & (STS_INT|STS_ERR)) != 0)) {
5158 if (likely((status & STS_ERR) == 0))
Corentin Labbea0ef2bd2018-09-19 19:48:53 +00005159 INCR(fotg210->stats.normal);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005160 else
Corentin Labbea0ef2bd2018-09-19 19:48:53 +00005161 INCR(fotg210->stats.error);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005162 bh = 1;
5163 }
5164
5165 /* complete the unlinking of some qh [4.15.2.3] */
5166 if (status & STS_IAA) {
5167
5168 /* Turn off the IAA watchdog */
5169 fotg210->enabled_hrtimer_events &=
5170 ~BIT(FOTG210_HRTIMER_IAA_WATCHDOG);
5171
5172 /*
5173 * Mild optimization: Allow another IAAD to reset the
5174 * hrtimer, if one occurs before the next expiration.
5175 * In theory we could always cancel the hrtimer, but
5176 * tests show that about half the time it will be reset
5177 * for some other event anyway.
5178 */
5179 if (fotg210->next_hrtimer_event == FOTG210_HRTIMER_IAA_WATCHDOG)
5180 ++fotg210->next_hrtimer_event;
5181
5182 /* guard against (alleged) silicon errata */
5183 if (cmd & CMD_IAAD)
5184 fotg210_dbg(fotg210, "IAA with IAAD still set?\n");
5185 if (fotg210->async_iaa) {
Corentin Labbea0ef2bd2018-09-19 19:48:53 +00005186 INCR(fotg210->stats.iaa);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005187 end_unlink_async(fotg210);
5188 } else
5189 fotg210_dbg(fotg210, "IAA with nothing unlinked?\n");
5190 }
5191
5192 /* remote wakeup [4.3.1] */
5193 if (status & STS_PCD) {
5194 int pstatus;
5195 u32 __iomem *status_reg = &fotg210->regs->port_status;
5196
5197 /* kick root hub later */
5198 pcd_status = status;
5199
5200 /* resume root hub? */
5201 if (fotg210->rh_state == FOTG210_RH_SUSPENDED)
5202 usb_hcd_resume_root_hub(hcd);
5203
5204 pstatus = fotg210_readl(fotg210, status_reg);
5205
5206 if (test_bit(0, &fotg210->suspended_ports) &&
5207 ((pstatus & PORT_RESUME) ||
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005208 !(pstatus & PORT_SUSPEND)) &&
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005209 (pstatus & PORT_PE) &&
5210 fotg210->reset_done[0] == 0) {
5211
5212 /* start 20 msec resume signaling from this port,
Petr Mladek37ebb542014-09-19 17:32:23 +02005213 * and make hub_wq collect PORT_STAT_C_SUSPEND to
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005214 * stop that signaling. Use 5 ms extra for safety,
5215 * like usb_port_resume() does.
5216 */
5217 fotg210->reset_done[0] = jiffies + msecs_to_jiffies(25);
5218 set_bit(0, &fotg210->resuming_ports);
5219 fotg210_dbg(fotg210, "port 1 remote wakeup\n");
5220 mod_timer(&hcd->rh_timer, fotg210->reset_done[0]);
5221 }
5222 }
5223
5224 /* PCI errors [4.15.2.4] */
5225 if (unlikely((status & STS_FATAL) != 0)) {
5226 fotg210_err(fotg210, "fatal error\n");
5227 dbg_cmd(fotg210, "fatal", cmd);
5228 dbg_status(fotg210, "fatal", status);
5229dead:
5230 usb_hc_died(hcd);
5231
5232 /* Don't let the controller do anything more */
5233 fotg210->shutdown = true;
5234 fotg210->rh_state = FOTG210_RH_STOPPING;
5235 fotg210->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
5236 fotg210_writel(fotg210, fotg210->command,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005237 &fotg210->regs->command);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005238 fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
5239 fotg210_handle_controller_death(fotg210);
5240
5241 /* Handle completions when the controller stops */
5242 bh = 0;
5243 }
5244
5245 if (bh)
5246 fotg210_work(fotg210);
5247 spin_unlock(&fotg210->lock);
5248 if (pcd_status)
5249 usb_hcd_poll_rh_status(hcd);
5250 return IRQ_HANDLED;
5251}
5252
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005253/* non-error returns are a promise to giveback() the urb later
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005254 * we drop ownership so next owner (or urb unlink) can get it
5255 *
5256 * urb + dev is in hcd.self.controller.urb_list
5257 * we're queueing TDs onto software and hardware lists
5258 *
5259 * hcd-specific init for hcpriv hasn't been done yet
5260 *
5261 * NOTE: control, bulk, and interrupt share the same code to append TDs
5262 * to a (possibly active) QH, and the same QH scanning code.
5263 */
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005264static int fotg210_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
5265 gfp_t mem_flags)
5266{
5267 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5268 struct list_head qtd_list;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005269
5270 INIT_LIST_HEAD(&qtd_list);
5271
5272 switch (usb_pipetype(urb->pipe)) {
5273 case PIPE_CONTROL:
5274 /* qh_completions() code doesn't handle all the fault cases
5275 * in multi-TD control transfers. Even 1KB is rare anyway.
5276 */
5277 if (urb->transfer_buffer_length > (16 * 1024))
5278 return -EMSGSIZE;
Gustavo A. R. Silva93c747e2020-11-20 12:40:27 -06005279 fallthrough;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005280 /* case PIPE_BULK: */
5281 default:
5282 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5283 return -ENOMEM;
5284 return submit_async(fotg210, urb, &qtd_list, mem_flags);
5285
5286 case PIPE_INTERRUPT:
5287 if (!qh_urb_transaction(fotg210, urb, &qtd_list, mem_flags))
5288 return -ENOMEM;
5289 return intr_submit(fotg210, urb, &qtd_list, mem_flags);
5290
5291 case PIPE_ISOCHRONOUS:
5292 return itd_submit(fotg210, urb, mem_flags);
5293 }
5294}
5295
5296/* remove from hardware lists
5297 * completions normally happen asynchronously
5298 */
5299
5300static int fotg210_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
5301{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005302 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5303 struct fotg210_qh *qh;
5304 unsigned long flags;
5305 int rc;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005306
5307 spin_lock_irqsave(&fotg210->lock, flags);
5308 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
5309 if (rc)
5310 goto done;
5311
5312 switch (usb_pipetype(urb->pipe)) {
5313 /* case PIPE_CONTROL: */
5314 /* case PIPE_BULK:*/
5315 default:
5316 qh = (struct fotg210_qh *) urb->hcpriv;
5317 if (!qh)
5318 break;
5319 switch (qh->qh_state) {
5320 case QH_STATE_LINKED:
5321 case QH_STATE_COMPLETING:
5322 start_unlink_async(fotg210, qh);
5323 break;
5324 case QH_STATE_UNLINK:
5325 case QH_STATE_UNLINK_WAIT:
5326 /* already started */
5327 break;
5328 case QH_STATE_IDLE:
5329 /* QH might be waiting for a Clear-TT-Buffer */
5330 qh_completions(fotg210, qh);
5331 break;
5332 }
5333 break;
5334
5335 case PIPE_INTERRUPT:
5336 qh = (struct fotg210_qh *) urb->hcpriv;
5337 if (!qh)
5338 break;
5339 switch (qh->qh_state) {
5340 case QH_STATE_LINKED:
5341 case QH_STATE_COMPLETING:
5342 start_unlink_intr(fotg210, qh);
5343 break;
5344 case QH_STATE_IDLE:
5345 qh_completions(fotg210, qh);
5346 break;
5347 default:
5348 fotg210_dbg(fotg210, "bogus qh %p state %d\n",
5349 qh, qh->qh_state);
5350 goto done;
5351 }
5352 break;
5353
5354 case PIPE_ISOCHRONOUS:
5355 /* itd... */
5356
5357 /* wait till next completion, do it then. */
5358 /* completion irqs can wait up to 1024 msec, */
5359 break;
5360 }
5361done:
5362 spin_unlock_irqrestore(&fotg210->lock, flags);
5363 return rc;
5364}
5365
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005366/* bulk qh holds the data toggle */
5367
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005368static void fotg210_endpoint_disable(struct usb_hcd *hcd,
5369 struct usb_host_endpoint *ep)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005370{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005371 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5372 unsigned long flags;
5373 struct fotg210_qh *qh, *tmp;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005374
5375 /* ASSERT: any requests/urbs are being unlinked */
5376 /* ASSERT: nobody can be submitting urbs for this any more */
5377
5378rescan:
5379 spin_lock_irqsave(&fotg210->lock, flags);
5380 qh = ep->hcpriv;
5381 if (!qh)
5382 goto done;
5383
5384 /* endpoints can be iso streams. for now, we don't
5385 * accelerate iso completions ... so spin a while.
5386 */
5387 if (qh->hw == NULL) {
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005388 struct fotg210_iso_stream *stream = ep->hcpriv;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005389
5390 if (!list_empty(&stream->td_list))
5391 goto idle_timeout;
5392
5393 /* BUG_ON(!list_empty(&stream->free_list)); */
5394 kfree(stream);
5395 goto done;
5396 }
5397
5398 if (fotg210->rh_state < FOTG210_RH_RUNNING)
5399 qh->qh_state = QH_STATE_IDLE;
5400 switch (qh->qh_state) {
5401 case QH_STATE_LINKED:
5402 case QH_STATE_COMPLETING:
5403 for (tmp = fotg210->async->qh_next.qh;
5404 tmp && tmp != qh;
5405 tmp = tmp->qh_next.qh)
5406 continue;
5407 /* periodic qh self-unlinks on empty, and a COMPLETING qh
5408 * may already be unlinked.
5409 */
5410 if (tmp)
5411 start_unlink_async(fotg210, qh);
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05005412 fallthrough;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005413 case QH_STATE_UNLINK: /* wait for hw to finish? */
5414 case QH_STATE_UNLINK_WAIT:
5415idle_timeout:
5416 spin_unlock_irqrestore(&fotg210->lock, flags);
5417 schedule_timeout_uninterruptible(1);
5418 goto rescan;
5419 case QH_STATE_IDLE: /* fully unlinked */
5420 if (qh->clearing_tt)
5421 goto idle_timeout;
5422 if (list_empty(&qh->qtd_list)) {
5423 qh_destroy(fotg210, qh);
5424 break;
5425 }
Gustavo A. R. Silva0d9b6d42020-07-07 14:56:07 -05005426 fallthrough;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005427 default:
5428 /* caller was supposed to have unlinked any requests;
5429 * that's not our job. just leak this memory.
5430 */
5431 fotg210_err(fotg210, "qh %p (#%02x) state %d%s\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005432 qh, ep->desc.bEndpointAddress, qh->qh_state,
5433 list_empty(&qh->qtd_list) ? "" : "(has tds)");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005434 break;
5435 }
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005436done:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005437 ep->hcpriv = NULL;
5438 spin_unlock_irqrestore(&fotg210->lock, flags);
5439}
5440
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005441static void fotg210_endpoint_reset(struct usb_hcd *hcd,
5442 struct usb_host_endpoint *ep)
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005443{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005444 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5445 struct fotg210_qh *qh;
5446 int eptype = usb_endpoint_type(&ep->desc);
5447 int epnum = usb_endpoint_num(&ep->desc);
5448 int is_out = usb_endpoint_dir_out(&ep->desc);
5449 unsigned long flags;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005450
5451 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
5452 return;
5453
5454 spin_lock_irqsave(&fotg210->lock, flags);
5455 qh = ep->hcpriv;
5456
5457 /* For Bulk and Interrupt endpoints we maintain the toggle state
5458 * in the hardware; the toggle bits in udev aren't used at all.
5459 * When an endpoint is reset by usb_clear_halt() we must reset
5460 * the toggle bit in the QH.
5461 */
5462 if (qh) {
5463 usb_settoggle(qh->dev, epnum, is_out, 0);
5464 if (!list_empty(&qh->qtd_list)) {
5465 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
5466 } else if (qh->qh_state == QH_STATE_LINKED ||
5467 qh->qh_state == QH_STATE_COMPLETING) {
5468
5469 /* The toggle value in the QH can't be updated
5470 * while the QH is active. Unlink it now;
5471 * re-linking will call qh_refresh().
5472 */
5473 if (eptype == USB_ENDPOINT_XFER_BULK)
5474 start_unlink_async(fotg210, qh);
5475 else
5476 start_unlink_intr(fotg210, qh);
5477 }
5478 }
5479 spin_unlock_irqrestore(&fotg210->lock, flags);
5480}
5481
5482static int fotg210_get_frame(struct usb_hcd *hcd)
5483{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005484 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5485
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005486 return (fotg210_read_frame_index(fotg210) >> 3) %
5487 fotg210->periodic_size;
5488}
5489
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005490/* The EHCI in ChipIdea HDRC cannot be a separate module or device,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005491 * because its registers (and irq) are shared between host/gadget/otg
5492 * functions and in order to facilitate role switching we cannot
5493 * give the fotg210 driver exclusive access to those.
5494 */
5495MODULE_DESCRIPTION(DRIVER_DESC);
5496MODULE_AUTHOR(DRIVER_AUTHOR);
5497MODULE_LICENSE("GPL");
5498
5499static const struct hc_driver fotg210_fotg210_hc_driver = {
5500 .description = hcd_name,
5501 .product_desc = "Faraday USB2.0 Host Controller",
5502 .hcd_priv_size = sizeof(struct fotg210_hcd),
5503
5504 /*
5505 * generic hardware linkage
5506 */
5507 .irq = fotg210_irq,
Christoph Hellwig7b81cb62019-08-16 08:24:32 +02005508 .flags = HCD_MEMORY | HCD_DMA | HCD_USB2,
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005509
5510 /*
5511 * basic lifecycle operations
5512 */
5513 .reset = hcd_fotg210_init,
5514 .start = fotg210_run,
5515 .stop = fotg210_stop,
5516 .shutdown = fotg210_shutdown,
5517
5518 /*
5519 * managing i/o requests and associated device resources
5520 */
5521 .urb_enqueue = fotg210_urb_enqueue,
5522 .urb_dequeue = fotg210_urb_dequeue,
5523 .endpoint_disable = fotg210_endpoint_disable,
5524 .endpoint_reset = fotg210_endpoint_reset,
5525
5526 /*
5527 * scheduling support
5528 */
5529 .get_frame_number = fotg210_get_frame,
5530
5531 /*
5532 * root hub support
5533 */
5534 .hub_status_data = fotg210_hub_status_data,
5535 .hub_control = fotg210_hub_control,
5536 .bus_suspend = fotg210_bus_suspend,
5537 .bus_resume = fotg210_bus_resume,
5538
5539 .relinquish_port = fotg210_relinquish_port,
5540 .port_handed_over = fotg210_port_handed_over,
5541
5542 .clear_tt_buffer_complete = fotg210_clear_tt_buffer_complete,
5543};
5544
5545static void fotg210_init(struct fotg210_hcd *fotg210)
5546{
5547 u32 value;
5548
5549 iowrite32(GMIR_MDEV_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005550 &fotg210->regs->gmir);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005551
5552 value = ioread32(&fotg210->regs->otgcsr);
5553 value &= ~OTGCSR_A_BUS_DROP;
5554 value |= OTGCSR_A_BUS_REQ;
5555 iowrite32(value, &fotg210->regs->otgcsr);
5556}
5557
Lee Jones15ee5d02020-07-06 14:33:30 +01005558/*
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005559 * fotg210_hcd_probe - initialize faraday FOTG210 HCDs
5560 *
5561 * Allocates basic resources for this USB host controller, and
5562 * then invokes the start() method for the HCD associated with it
5563 * through the hotplug entry's driver_data.
5564 */
5565static int fotg210_hcd_probe(struct platform_device *pdev)
5566{
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005567 struct device *dev = &pdev->dev;
5568 struct usb_hcd *hcd;
5569 struct resource *res;
5570 int irq;
Christophe JAILLETa60a3432021-05-06 22:39:10 +02005571 int retval;
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005572 struct fotg210_hcd *fotg210;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005573
5574 if (usb_disabled())
5575 return -ENODEV;
5576
5577 pdev->dev.power.power_state = PMSG_ON;
5578
Lad Prabhakar9198e022021-12-20 01:04:06 +00005579 irq = platform_get_irq(pdev, 0);
5580 if (irq < 0)
5581 return irq;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005582
5583 hcd = usb_create_hcd(&fotg210_fotg210_hc_driver, dev,
5584 dev_name(dev));
5585 if (!hcd) {
Christophe JAILLETa60a3432021-05-06 22:39:10 +02005586 dev_err(dev, "failed to create hcd\n");
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005587 retval = -ENOMEM;
5588 goto fail_create_hcd;
5589 }
5590
Himangi Saraogi0e278b32014-07-02 02:04:44 +05305591 hcd->has_tt = 1;
5592
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005593 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Himangi Saraogi0e278b32014-07-02 02:04:44 +05305594 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
5595 if (IS_ERR(hcd->regs)) {
5596 retval = PTR_ERR(hcd->regs);
Linus Walleijffa8a312018-09-01 18:03:12 +02005597 goto failed_put_hcd;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005598 }
5599
5600 hcd->rsrc_start = res->start;
5601 hcd->rsrc_len = resource_size(res);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005602
5603 fotg210 = hcd_to_fotg210(hcd);
5604
5605 fotg210->caps = hcd->regs;
5606
Linus Walleijffa8a312018-09-01 18:03:12 +02005607 /* It's OK not to supply this clock */
5608 fotg210->pclk = clk_get(dev, "PCLK");
5609 if (!IS_ERR(fotg210->pclk)) {
5610 retval = clk_prepare_enable(fotg210->pclk);
5611 if (retval) {
5612 dev_err(dev, "failed to enable PCLK\n");
5613 goto failed_put_hcd;
5614 }
5615 } else if (PTR_ERR(fotg210->pclk) == -EPROBE_DEFER) {
5616 /*
5617 * Percolate deferrals, for anything else,
5618 * just live without the clocking.
5619 */
5620 retval = PTR_ERR(fotg210->pclk);
5621 goto failed_dis_clk;
5622 }
5623
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005624 retval = fotg210_setup(hcd);
5625 if (retval)
Linus Walleijffa8a312018-09-01 18:03:12 +02005626 goto failed_dis_clk;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005627
5628 fotg210_init(fotg210);
5629
5630 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
5631 if (retval) {
5632 dev_err(dev, "failed to add hcd with err %d\n", retval);
Linus Walleijffa8a312018-09-01 18:03:12 +02005633 goto failed_dis_clk;
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005634 }
Peter Chen3c9740a2013-11-05 10:46:02 +08005635 device_wakeup_enable(hcd->self.controller);
Linus Walleij46216502018-09-18 16:25:06 -07005636 platform_set_drvdata(pdev, hcd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005637
5638 return retval;
5639
Linus Walleijffa8a312018-09-01 18:03:12 +02005640failed_dis_clk:
Chuhong Yuana363d502019-11-02 14:22:45 +08005641 if (!IS_ERR(fotg210->pclk)) {
Linus Walleijffa8a312018-09-01 18:03:12 +02005642 clk_disable_unprepare(fotg210->pclk);
Chuhong Yuana363d502019-11-02 14:22:45 +08005643 clk_put(fotg210->pclk);
5644 }
Linus Walleijffa8a312018-09-01 18:03:12 +02005645failed_put_hcd:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005646 usb_put_hcd(hcd);
5647fail_create_hcd:
5648 dev_err(dev, "init %s fail, %d\n", dev_name(dev), retval);
5649 return retval;
5650}
5651
Lee Jones15ee5d02020-07-06 14:33:30 +01005652/*
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005653 * fotg210_hcd_remove - shutdown processing for EHCI HCDs
5654 * @dev: USB Host Controller being removed
5655 *
5656 */
5657static int fotg210_hcd_remove(struct platform_device *pdev)
5658{
Linus Walleij46216502018-09-18 16:25:06 -07005659 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Linus Walleijffa8a312018-09-01 18:03:12 +02005660 struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
5661
Chuhong Yuana363d502019-11-02 14:22:45 +08005662 if (!IS_ERR(fotg210->pclk)) {
Linus Walleijffa8a312018-09-01 18:03:12 +02005663 clk_disable_unprepare(fotg210->pclk);
Chuhong Yuana363d502019-11-02 14:22:45 +08005664 clk_put(fotg210->pclk);
5665 }
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005666
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005667 usb_remove_hcd(hcd);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005668 usb_put_hcd(hcd);
5669
5670 return 0;
5671}
5672
Hans Ulli Krolld7863de2019-06-13 17:07:35 +02005673#ifdef CONFIG_OF
5674static const struct of_device_id fotg210_of_match[] = {
5675 { .compatible = "faraday,fotg210" },
5676 {},
5677};
5678MODULE_DEVICE_TABLE(of, fotg210_of_match);
5679#endif
5680
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005681static struct platform_driver fotg210_hcd_driver = {
5682 .driver = {
5683 .name = "fotg210-hcd",
Hans Ulli Krolld7863de2019-06-13 17:07:35 +02005684 .of_match_table = of_match_ptr(fotg210_of_match),
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005685 },
5686 .probe = fotg210_hcd_probe,
5687 .remove = fotg210_hcd_remove,
5688};
5689
5690static int __init fotg210_hcd_init(void)
5691{
5692 int retval = 0;
5693
5694 if (usb_disabled())
5695 return -ENODEV;
5696
5697 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
5698 set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5699 if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
5700 test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
Peter Senna Tschudin5ec9b892015-10-12 23:22:33 +02005701 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 +00005702
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08005703 pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd\n",
Peter Senna Tschudin259127b2015-10-12 23:22:32 +02005704 hcd_name, sizeof(struct fotg210_qh),
5705 sizeof(struct fotg210_qtd),
5706 sizeof(struct fotg210_itd));
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005707
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005708 fotg210_debug_root = debugfs_create_dir("fotg210", usb_debug_root);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005709
5710 retval = platform_driver_register(&fotg210_hcd_driver);
5711 if (retval < 0)
5712 goto clean;
5713 return retval;
5714
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005715clean:
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005716 debugfs_remove(fotg210_debug_root);
5717 fotg210_debug_root = NULL;
Greg Kroah-Hartman9b105162018-05-29 17:30:55 +02005718
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005719 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5720 return retval;
5721}
5722module_init(fotg210_hcd_init);
5723
5724static void __exit fotg210_hcd_cleanup(void)
5725{
5726 platform_driver_unregister(&fotg210_hcd_driver);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005727 debugfs_remove(fotg210_debug_root);
Feng-Hsin Chiang7d501952013-07-29 16:48:32 +00005728 clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
5729}
5730module_exit(fotg210_hcd_cleanup);