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