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