fotg210-hcd.c 158 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2
/* Faraday FOTG210 EHCI-like driver
3
4
5
6
7
8
9
10
11
12
 *
 * Copyright (c) 2013 Faraday Technology Corporation
 *
 * Author: Yuan-Hsin Chen <yhchen@faraday-tech.com>
 *	   Feng-Hsin Chiang <john453@faraday-tech.com>
 *	   Po-Yu Chuang <ratbert.chuang@gmail.com>
 *
 * Most of code borrowed from the Linux-3.7 EHCI driver
 */
#include <linux/module.h>
13
#include <linux/of.h>
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <linux/device.h>
#include <linux/dmapool.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/vmalloc.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/hrtimer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#include <linux/moduleparam.h>
#include <linux/dma-mapping.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/platform_device.h>
#include <linux/io.h>
35
#include <linux/clk.h>
36
37
38
39
40
41
42

#include <asm/byteorder.h>
#include <asm/irq.h>
#include <asm/unaligned.h>

#define DRIVER_AUTHOR "Yuan-Hsin Chen"
#define DRIVER_DESC "FOTG210 Host Controller (EHCI) Driver"
43
static const char hcd_name[] = "fotg210_hcd";
44
45
46
47
48

#undef FOTG210_URB_TRACE
#define FOTG210_STATS

/* magic numbers that can affect system performance */
49
50
51
52
53
54
55
56
#define FOTG210_TUNE_CERR	3 /* 0-3 qtd retries; 0 == don't stop */
#define FOTG210_TUNE_RL_HS	4 /* nak throttle; see 4.9 */
#define FOTG210_TUNE_RL_TT	0
#define FOTG210_TUNE_MULT_HS	1 /* 1-3 transactions/uframe; 4.10.3 */
#define FOTG210_TUNE_MULT_TT	1

/* Some drivers think it's safe to schedule isochronous transfers more than 256
 * ms into the future (partly as a result of an old bug in the scheduling
57
58
59
 * code).  In an attempt to avoid trouble, we will use a minimum scheduling
 * length of 512 frames instead of 256.
 */
60
#define FOTG210_TUNE_FLS 1 /* (medium) 512-frame schedule */
61
62

/* Initial IRQ latency:  faster than hw default */
63
static int log2_irq_thresh; /* 0 to 6 */
64
65
66
67
68
69
70
71
72
73
74
75
76
module_param(log2_irq_thresh, int, S_IRUGO);
MODULE_PARM_DESC(log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");

/* initial park setting:  slower than hw default */
static unsigned park;
module_param(park, uint, S_IRUGO);
MODULE_PARM_DESC(park, "park setting; 1-3 back-to-back async packets");

/* for link power management(LPM) feature */
static unsigned int hird;
module_param(hird, int, S_IRUGO);
MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");

77
#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
78
79
80
81

#include "fotg210.h"

#define fotg210_dbg(fotg210, fmt, args...) \
82
	dev_dbg(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
83
#define fotg210_err(fotg210, fmt, args...) \
84
	dev_err(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
85
#define fotg210_info(fotg210, fmt, args...) \
86
	dev_info(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
87
#define fotg210_warn(fotg210, fmt, args...) \
88
	dev_warn(fotg210_to_hcd(fotg210)->self.controller, fmt, ## args)
89

90
91
/* check the values in the HCSPARAMS register (host controller _Structural_
 * parameters) see EHCI spec, Table 2-4 for each value
92
93
94
 */
static void dbg_hcs_params(struct fotg210_hcd *fotg210, char *label)
{
95
	u32 params = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
96

97
98
	fotg210_dbg(fotg210, "%s hcs_params 0x%x ports=%d\n", label, params,
			HCS_N_PORTS(params));
99
100
}

101
102
103
/* check the values in the HCCPARAMS register (host controller _Capability_
 * parameters) see EHCI Spec, Table 2-5 for each value
 */
104
105
static void dbg_hcc_params(struct fotg210_hcd *fotg210, char *label)
{
106
	u32 params = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
107

108
109
110
111
	fotg210_dbg(fotg210, "%s hcc_params %04x uframes %s%s\n", label,
			params,
			HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
			HCC_CANPARK(params) ? " park" : "");
112
113
114
115
116
117
}

static void __maybe_unused
dbg_qtd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qtd *qtd)
{
	fotg210_dbg(fotg210, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
118
119
120
121
			hc32_to_cpup(fotg210, &qtd->hw_next),
			hc32_to_cpup(fotg210, &qtd->hw_alt_next),
			hc32_to_cpup(fotg210, &qtd->hw_token),
			hc32_to_cpup(fotg210, &qtd->hw_buf[0]));
122
123
	if (qtd->hw_buf[1])
		fotg210_dbg(fotg210, "  p1=%08x p2=%08x p3=%08x p4=%08x\n",
124
125
126
127
				hc32_to_cpup(fotg210, &qtd->hw_buf[1]),
				hc32_to_cpup(fotg210, &qtd->hw_buf[2]),
				hc32_to_cpup(fotg210, &qtd->hw_buf[3]),
				hc32_to_cpup(fotg210, &qtd->hw_buf[4]));
128
129
130
131
132
133
134
}

static void __maybe_unused
dbg_qh(const char *label, struct fotg210_hcd *fotg210, struct fotg210_qh *qh)
{
	struct fotg210_qh_hw *hw = qh->hw;

135
136
137
138
	fotg210_dbg(fotg210, "%s qh %p n%08x info %x %x qtd %x\n", label, qh,
			hw->hw_next, hw->hw_info1, hw->hw_info2,
			hw->hw_current);

139
140
141
142
143
144
	dbg_qtd("overlay", fotg210, (struct fotg210_qtd *) &hw->hw_qtd_next);
}

static void __maybe_unused
dbg_itd(const char *label, struct fotg210_hcd *fotg210, struct fotg210_itd *itd)
{
145
146
147
148
	fotg210_dbg(fotg210, "%s[%d] itd %p, next %08x, urb %p\n", label,
			itd->frame, itd, hc32_to_cpu(fotg210, itd->hw_next),
			itd->urb);

149
	fotg210_dbg(fotg210,
150
151
152
153
154
155
156
157
158
159
			"  trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
			hc32_to_cpu(fotg210, itd->hw_transaction[0]),
			hc32_to_cpu(fotg210, itd->hw_transaction[1]),
			hc32_to_cpu(fotg210, itd->hw_transaction[2]),
			hc32_to_cpu(fotg210, itd->hw_transaction[3]),
			hc32_to_cpu(fotg210, itd->hw_transaction[4]),
			hc32_to_cpu(fotg210, itd->hw_transaction[5]),
			hc32_to_cpu(fotg210, itd->hw_transaction[6]),
			hc32_to_cpu(fotg210, itd->hw_transaction[7]));

160
	fotg210_dbg(fotg210,
161
162
163
164
165
166
167
168
169
			"  buf:   %08x %08x %08x %08x %08x %08x %08x\n",
			hc32_to_cpu(fotg210, itd->hw_bufp[0]),
			hc32_to_cpu(fotg210, itd->hw_bufp[1]),
			hc32_to_cpu(fotg210, itd->hw_bufp[2]),
			hc32_to_cpu(fotg210, itd->hw_bufp[3]),
			hc32_to_cpu(fotg210, itd->hw_bufp[4]),
			hc32_to_cpu(fotg210, itd->hw_bufp[5]),
			hc32_to_cpu(fotg210, itd->hw_bufp[6]));

170
	fotg210_dbg(fotg210, "  index: %d %d %d %d %d %d %d %d\n",
171
172
173
			itd->index[0], itd->index[1], itd->index[2],
			itd->index[3], itd->index[4], itd->index[5],
			itd->index[6], itd->index[7]);
174
175
176
177
178
}

static int __maybe_unused
dbg_status_buf(char *buf, unsigned len, const char *label, u32 status)
{
179
180
181
182
183
184
185
186
187
188
189
190
	return scnprintf(buf, len, "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s",
			label, label[0] ? " " : "", status,
			(status & STS_ASS) ? " Async" : "",
			(status & STS_PSS) ? " Periodic" : "",
			(status & STS_RECL) ? " Recl" : "",
			(status & STS_HALT) ? " Halt" : "",
			(status & STS_IAA) ? " IAA" : "",
			(status & STS_FATAL) ? " FATAL" : "",
			(status & STS_FLR) ? " FLR" : "",
			(status & STS_PCD) ? " PCD" : "",
			(status & STS_ERR) ? " ERR" : "",
			(status & STS_INT) ? " INT" : "");
191
192
193
194
195
}

static int __maybe_unused
dbg_intr_buf(char *buf, unsigned len, const char *label, u32 enable)
{
196
197
198
199
200
201
202
203
	return scnprintf(buf, len, "%s%sintrenable %02x%s%s%s%s%s%s",
			label, label[0] ? " " : "", enable,
			(enable & STS_IAA) ? " IAA" : "",
			(enable & STS_FATAL) ? " FATAL" : "",
			(enable & STS_FLR) ? " FLR" : "",
			(enable & STS_PCD) ? " PCD" : "",
			(enable & STS_ERR) ? " ERR" : "",
			(enable & STS_INT) ? " INT" : "");
204
205
206
207
}

static const char *const fls_strings[] = { "1024", "512", "256", "??" };

208
209
static int dbg_command_buf(char *buf, unsigned len, const char *label,
		u32 command)
210
211
{
	return scnprintf(buf, len,
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
			"%s%scommand %07x %s=%d ithresh=%d%s%s%s period=%s%s %s",
			label, label[0] ? " " : "", command,
			(command & CMD_PARK) ? " park" : "(park)",
			CMD_PARK_CNT(command),
			(command >> 16) & 0x3f,
			(command & CMD_IAAD) ? " IAAD" : "",
			(command & CMD_ASE) ? " Async" : "",
			(command & CMD_PSE) ? " Periodic" : "",
			fls_strings[(command >> 2) & 0x3],
			(command & CMD_RESET) ? " Reset" : "",
			(command & CMD_RUN) ? "RUN" : "HALT");
}

static char *dbg_port_buf(char *buf, unsigned len, const char *label, int port,
		u32 status)
{
	char *sig;
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

	/* signaling state */
	switch (status & (3 << 10)) {
	case 0 << 10:
		sig = "se0";
		break;
	case 1 << 10:
		sig = "k";
		break; /* low speed */
	case 2 << 10:
		sig = "j";
		break;
	default:
		sig = "?";
		break;
	}

246
247
248
249
250
251
252
253
254
255
256
257
	scnprintf(buf, len, "%s%sport:%d status %06x %d sig=%s%s%s%s%s%s%s%s",
			label, label[0] ? " " : "", port, status,
			status >> 25, /*device address */
			sig,
			(status & PORT_RESET) ? " RESET" : "",
			(status & PORT_SUSPEND) ? " SUSPEND" : "",
			(status & PORT_RESUME) ? " RESUME" : "",
			(status & PORT_PEC) ? " PEC" : "",
			(status & PORT_PE) ? " PE" : "",
			(status & PORT_CSC) ? " CSC" : "",
			(status & PORT_CONNECT) ? " CONNECT" : "");

258
	return buf;
259
260
261
}

/* functions have the "wrong" filename when they're output... */
262
263
264
265
#define dbg_status(fotg210, label, status) {			\
	char _buf[80];						\
	dbg_status_buf(_buf, sizeof(_buf), label, status);	\
	fotg210_dbg(fotg210, "%s\n", _buf);			\
266
267
}

268
269
270
271
#define dbg_cmd(fotg210, label, command) {			\
	char _buf[80];						\
	dbg_command_buf(_buf, sizeof(_buf), label, command);	\
	fotg210_dbg(fotg210, "%s\n", _buf);			\
272
273
}

274
275
276
277
#define dbg_port(fotg210, label, port, status) {			       \
	char _buf[80];							       \
	fotg210_dbg(fotg210, "%s\n",					       \
			dbg_port_buf(_buf, sizeof(_buf), label, port, status));\
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
}

/* troubleshooting help: expose state in debugfs */
static int debug_async_open(struct inode *, struct file *);
static int debug_periodic_open(struct inode *, struct file *);
static int debug_registers_open(struct inode *, struct file *);
static int debug_async_open(struct inode *, struct file *);

static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
static int debug_close(struct inode *, struct file *);

static const struct file_operations debug_async_fops = {
	.owner		= THIS_MODULE,
	.open		= debug_async_open,
	.read		= debug_output,
	.release	= debug_close,
	.llseek		= default_llseek,
};
static const struct file_operations debug_periodic_fops = {
	.owner		= THIS_MODULE,
	.open		= debug_periodic_open,
	.read		= debug_output,
	.release	= debug_close,
	.llseek		= default_llseek,
};
static const struct file_operations debug_registers_fops = {
	.owner		= THIS_MODULE,
	.open		= debug_registers_open,
	.read		= debug_output,
	.release	= debug_close,
	.llseek		= default_llseek,
};

static struct dentry *fotg210_debug_root;

struct debug_buffer {
	ssize_t (*fill_func)(struct debug_buffer *);	/* fill method */
	struct usb_bus *bus;
	struct mutex mutex;	/* protect filling of buffer */
	size_t count;		/* number of characters filled into buffer */
	char *output_buf;
	size_t alloc_size;
};

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
static inline char speed_char(u32 scratch)
{
	switch (scratch & (3 << 12)) {
	case QH_FULL_SPEED:
		return 'f';

	case QH_LOW_SPEED:
		return 'l';

	case QH_HIGH_SPEED:
		return 'h';

	default:
		return '?';
	}
}
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352

static inline char token_mark(struct fotg210_hcd *fotg210, __hc32 token)
{
	__u32 v = hc32_to_cpu(fotg210, token);

	if (v & QTD_STS_ACTIVE)
		return '*';
	if (v & QTD_STS_HALT)
		return '-';
	if (!IS_SHORT_READ(v))
		return ' ';
	/* tries to advance through hw_alt_next */
	return '/';
}

353
354
355
356
357
358
359
360
361
362
363
364
365
366
static void qh_lines(struct fotg210_hcd *fotg210, struct fotg210_qh *qh,
		char **nextp, unsigned *sizep)
{
	u32 scratch;
	u32 hw_curr;
	struct fotg210_qtd *td;
	unsigned temp;
	unsigned size = *sizep;
	char *next = *nextp;
	char mark;
	__le32 list_end = FOTG210_LIST_END(fotg210);
	struct fotg210_qh_hw *hw = qh->hw;

	if (hw->hw_qtd_next == list_end) /* NEC does this */
367
368
369
		mark = '@';
	else
		mark = token_mark(fotg210, hw->hw_token);
370
371
372
373
	if (mark == '/') { /* qh_alt_next controls qh advance? */
		if ((hw->hw_alt_next & QTD_MASK(fotg210)) ==
		    fotg210->async->hw->hw_alt_next)
			mark = '#'; /* blocked */
374
		else if (hw->hw_alt_next == list_end)
375
			mark = '.'; /* use hw_qtd_next */
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
		/* else alt_next points to some other qtd */
	}
	scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
	hw_curr = (mark == '*') ? hc32_to_cpup(fotg210, &hw->hw_current) : 0;
	temp = scnprintf(next, size,
			"qh/%p dev%d %cs ep%d %08x %08x(%08x%c %s nak%d)",
			qh, scratch & 0x007f,
			speed_char(scratch),
			(scratch >> 8) & 0x000f,
			scratch, hc32_to_cpup(fotg210, &hw->hw_info2),
			hc32_to_cpup(fotg210, &hw->hw_token), mark,
			(cpu_to_hc32(fotg210, QTD_TOGGLE) & hw->hw_token)
				? "data1" : "data0",
			(hc32_to_cpup(fotg210, &hw->hw_alt_next) >> 1) & 0x0f);
	size -= temp;
	next += temp;

	/* hc may be modifying the list as we read it ... */
	list_for_each_entry(td, &qh->qtd_list, qtd_list) {
		scratch = hc32_to_cpup(fotg210, &td->hw_token);
		mark = ' ';
		if (hw_curr == td->qtd_dma)
			mark = '*';
		else if (hw->hw_qtd_next == cpu_to_hc32(fotg210, td->qtd_dma))
			mark = '+';
		else if (QTD_LENGTH(scratch)) {
			if (td->hw_alt_next == fotg210->async->hw->hw_alt_next)
				mark = '#';
			else if (td->hw_alt_next != list_end)
				mark = '/';
		}
		temp = snprintf(next, size,
				"\n\t%p%c%s len=%d %08x urb %p",
				td, mark, ({ char *tmp;
				 switch ((scratch>>8)&0x03) {
				 case 0:
					tmp = "out";
					break;
				 case 1:
					tmp = "in";
					break;
				 case 2:
					tmp = "setup";
					break;
				 default:
					tmp = "?";
					break;
				 } tmp; }),
				(scratch >> 16) & 0x7fff,
				scratch,
				td->urb);
		if (size < temp)
			temp = size;
		size -= temp;
		next += temp;
		if (temp == size)
			goto done;
	}

	temp = snprintf(next, size, "\n");
	if (size < temp)
		temp = size;
438

439
440
441
442
443
444
445
446
447
448
	size -= temp;
	next += temp;

done:
	*sizep = size;
	*nextp = next;
}

static ssize_t fill_async_buffer(struct debug_buffer *buf)
{
449
450
451
452
453
454
	struct usb_hcd *hcd;
	struct fotg210_hcd *fotg210;
	unsigned long flags;
	unsigned temp, size;
	char *next;
	struct fotg210_qh *qh;
455
456
457
458
459
460
461
462
463
464
465
466
467
468

	hcd = bus_to_hcd(buf->bus);
	fotg210 = hcd_to_fotg210(hcd);
	next = buf->output_buf;
	size = buf->alloc_size;

	*next = 0;

	/* dumps a snapshot of the async schedule.
	 * usually empty except for long-term bulk reads, or head.
	 * one QH per line, and TDs we know about
	 */
	spin_lock_irqsave(&fotg210->lock, flags);
	for (qh = fotg210->async->qh_next.qh; size > 0 && qh;
469
			qh = qh->qh_next.qh)
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
		qh_lines(fotg210, qh, &next, &size);
	if (fotg210->async_unlink && size > 0) {
		temp = scnprintf(next, size, "\nunlink =\n");
		size -= temp;
		next += temp;

		for (qh = fotg210->async_unlink; size > 0 && qh;
				qh = qh->unlink_next)
			qh_lines(fotg210, qh, &next, &size);
	}
	spin_unlock_irqrestore(&fotg210->lock, flags);

	return strlen(buf->output_buf);
}

485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* count tds, get ep direction */
static unsigned output_buf_tds_dir(char *buf, struct fotg210_hcd *fotg210,
		struct fotg210_qh_hw *hw, struct fotg210_qh *qh, unsigned size)
{
	u32 scratch = hc32_to_cpup(fotg210, &hw->hw_info1);
	struct fotg210_qtd *qtd;
	char *type = "";
	unsigned temp = 0;

	/* count tds, get ep direction */
	list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
		temp++;
		switch ((hc32_to_cpu(fotg210, qtd->hw_token) >> 8) & 0x03) {
		case 0:
			type = "out";
			continue;
		case 1:
			type = "in";
			continue;
		}
	}

	return scnprintf(buf, size, "(%c%d ep%d%s [%d/%d] q%d p%d)",
			speed_char(scratch), scratch & 0x007f,
			(scratch >> 8) & 0x000f, type, qh->usecs,
			qh->c_usecs, temp, (scratch >> 16) & 0x7ff);
}

513
514
515
#define DBG_SCHED_LIMIT 64
static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
{
516
517
518
519
520
521
522
523
	struct usb_hcd *hcd;
	struct fotg210_hcd *fotg210;
	unsigned long flags;
	union fotg210_shadow p, *seen;
	unsigned temp, size, seen_count;
	char *next;
	unsigned i;
	__hc32 tag;
524

525
	seen = kmalloc_array(DBG_SCHED_LIMIT, sizeof(*seen), GFP_ATOMIC);
526
527
	if (!seen)
		return 0;
528

529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
	seen_count = 0;

	hcd = bus_to_hcd(buf->bus);
	fotg210 = hcd_to_fotg210(hcd);
	next = buf->output_buf;
	size = buf->alloc_size;

	temp = scnprintf(next, size, "size = %d\n", fotg210->periodic_size);
	size -= temp;
	next += temp;

	/* dump a snapshot of the periodic schedule.
	 * iso changes, interrupt usually doesn't.
	 */
	spin_lock_irqsave(&fotg210->lock, flags);
	for (i = 0; i < fotg210->periodic_size; i++) {
		p = fotg210->pshadow[i];
		if (likely(!p.ptr))
			continue;
548

549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
		tag = Q_NEXT_TYPE(fotg210, fotg210->periodic[i]);

		temp = scnprintf(next, size, "%4d: ", i);
		size -= temp;
		next += temp;

		do {
			struct fotg210_qh_hw *hw;

			switch (hc32_to_cpu(fotg210, tag)) {
			case Q_TYPE_QH:
				hw = p.qh->hw;
				temp = scnprintf(next, size, " qh%d-%04x/%p",
						p.qh->period,
						hc32_to_cpup(fotg210,
							&hw->hw_info2)
							/* uframe masks */
							& (QH_CMASK | QH_SMASK),
						p.qh);
				size -= temp;
				next += temp;
				/* don't repeat what follows this qh */
				for (temp = 0; temp < seen_count; temp++) {
					if (seen[temp].ptr != p.ptr)
						continue;
					if (p.qh->qh_next.ptr) {
						temp = scnprintf(next, size,
576
								" ...");
577
578
579
580
581
582
583
						size -= temp;
						next += temp;
					}
					break;
				}
				/* show more info the first time around */
				if (temp == seen_count) {
584
585
586
					temp = output_buf_tds_dir(next,
							fotg210, hw,
							p.qh, size);
587
588
589
590
591
592
593
594
595
596

					if (seen_count < DBG_SCHED_LIMIT)
						seen[seen_count++].qh = p.qh;
				} else
					temp = 0;
				tag = Q_NEXT_TYPE(fotg210, hw->hw_next);
				p = p.qh->qh_next;
				break;
			case Q_TYPE_FSTN:
				temp = scnprintf(next, size,
597
598
						" fstn-%8x/%p",
						p.fstn->hw_prev, p.fstn);
599
600
601
602
603
				tag = Q_NEXT_TYPE(fotg210, p.fstn->hw_next);
				p = p.fstn->fstn_next;
				break;
			case Q_TYPE_ITD:
				temp = scnprintf(next, size,
604
						" itd/%p", p.itd);
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
				tag = Q_NEXT_TYPE(fotg210, p.itd->hw_next);
				p = p.itd->itd_next;
				break;
			}
			size -= temp;
			next += temp;
		} while (p.ptr);

		temp = scnprintf(next, size, "\n");
		size -= temp;
		next += temp;
	}
	spin_unlock_irqrestore(&fotg210->lock, flags);
	kfree(seen);

	return buf->alloc_size - size;
}
#undef DBG_SCHED_LIMIT

static const char *rh_state_string(struct fotg210_hcd *fotg210)
{
	switch (fotg210->rh_state) {
	case FOTG210_RH_HALTED:
		return "halted";
	case FOTG210_RH_SUSPENDED:
		return "suspended";
	case FOTG210_RH_RUNNING:
		return "running";
	case FOTG210_RH_STOPPING:
		return "stopping";
	}
	return "?";
}

static ssize_t fill_registers_buffer(struct debug_buffer *buf)
{
641
642
643
644
645
646
647
	struct usb_hcd *hcd;
	struct fotg210_hcd *fotg210;
	unsigned long flags;
	unsigned temp, size, i;
	char *next, scratch[80];
	static const char fmt[] = "%*s\n";
	static const char label[] = "";
648
649
650
651
652
653
654
655
656
657

	hcd = bus_to_hcd(buf->bus);
	fotg210 = hcd_to_fotg210(hcd);
	next = buf->output_buf;
	size = buf->alloc_size;

	spin_lock_irqsave(&fotg210->lock, flags);

	if (!HCD_HW_ACCESSIBLE(hcd)) {
		size = scnprintf(next, size,
658
659
660
661
662
663
				"bus %s, device %s\n"
				"%s\n"
				"SUSPENDED(no register access)\n",
				hcd->self.controller->bus->name,
				dev_name(hcd->self.controller),
				hcd->product_desc);
664
665
666
667
668
		goto done;
	}

	/* Capability Registers */
	i = HC_VERSION(fotg210, fotg210_readl(fotg210,
669
			&fotg210->caps->hc_capbase));
670
	temp = scnprintf(next, size,
671
672
673
674
675
676
677
			"bus %s, device %s\n"
			"%s\n"
			"EHCI %x.%02x, rh state %s\n",
			hcd->self.controller->bus->name,
			dev_name(hcd->self.controller),
			hcd->product_desc,
			i >> 8, i & 0x0ff, rh_state_string(fotg210));
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
	size -= temp;
	next += temp;

	/* FIXME interpret both types of params */
	i = fotg210_readl(fotg210, &fotg210->caps->hcs_params);
	temp = scnprintf(next, size, "structural params 0x%08x\n", i);
	size -= temp;
	next += temp;

	i = fotg210_readl(fotg210, &fotg210->caps->hcc_params);
	temp = scnprintf(next, size, "capability params 0x%08x\n", i);
	size -= temp;
	next += temp;

	/* Operational Registers */
	temp = dbg_status_buf(scratch, sizeof(scratch), label,
			fotg210_readl(fotg210, &fotg210->regs->status));
	temp = scnprintf(next, size, fmt, temp, scratch);
	size -= temp;
	next += temp;

	temp = dbg_command_buf(scratch, sizeof(scratch), label,
			fotg210_readl(fotg210, &fotg210->regs->command));
	temp = scnprintf(next, size, fmt, temp, scratch);
	size -= temp;
	next += temp;

	temp = dbg_intr_buf(scratch, sizeof(scratch), label,
			fotg210_readl(fotg210, &fotg210->regs->intr_enable));
	temp = scnprintf(next, size, fmt, temp, scratch);
	size -= temp;
	next += temp;

	temp = scnprintf(next, size, "uframe %04x\n",
			fotg210_read_frame_index(fotg210));
	size -= temp;
	next += temp;

	if (fotg210->async_unlink) {
		temp = scnprintf(next, size, "async unlink qh %p\n",
				fotg210->async_unlink);
		size -= temp;
		next += temp;
	}

#ifdef FOTG210_STATS
	temp = scnprintf(next, size,
725
726
727
			"irq normal %ld err %ld iaa %ld(lost %ld)\n",
			fotg210->stats.normal, fotg210->stats.error,
			fotg210->stats.iaa, fotg210->stats.lost_iaa);
728
729
730
731
	size -= temp;
	next += temp;

	temp = scnprintf(next, size, "complete %ld unlink %ld\n",
732
			fotg210->stats.complete, fotg210->stats.unlink);
733
734
735
736
737
738
739
740
741
742
	size -= temp;
	next += temp;
#endif

done:
	spin_unlock_irqrestore(&fotg210->lock, flags);

	return buf->alloc_size - size;
}

743
744
static struct debug_buffer
*alloc_buffer(struct usb_bus *bus, ssize_t (*fill_func)(struct debug_buffer *))
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
{
	struct debug_buffer *buf;

	buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);

	if (buf) {
		buf->bus = bus;
		buf->fill_func = fill_func;
		mutex_init(&buf->mutex);
		buf->alloc_size = PAGE_SIZE;
	}

	return buf;
}

static int fill_buffer(struct debug_buffer *buf)
{
	int ret = 0;

	if (!buf->output_buf)
		buf->output_buf = vmalloc(buf->alloc_size);

	if (!buf->output_buf) {
		ret = -ENOMEM;
		goto out;
	}

	ret = buf->fill_func(buf);

	if (ret >= 0) {
		buf->count = ret;
		ret = 0;
	}

out:
	return ret;
}

static ssize_t debug_output(struct file *file, char __user *user_buf,
784
		size_t len, loff_t *offset)
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
{
	struct debug_buffer *buf = file->private_data;
	int ret = 0;

	mutex_lock(&buf->mutex);
	if (buf->count == 0) {
		ret = fill_buffer(buf);
		if (ret != 0) {
			mutex_unlock(&buf->mutex);
			goto out;
		}
	}
	mutex_unlock(&buf->mutex);

	ret = simple_read_from_buffer(user_buf, len, offset,
800
			buf->output_buf, buf->count);
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827

out:
	return ret;

}

static int debug_close(struct inode *inode, struct file *file)
{
	struct debug_buffer *buf = file->private_data;

	if (buf) {
		vfree(buf->output_buf);
		kfree(buf);
	}

	return 0;
}
static int debug_async_open(struct inode *inode, struct file *file)
{
	file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);

	return file->private_data ? 0 : -ENOMEM;
}

static int debug_periodic_open(struct inode *inode, struct file *file)
{
	struct debug_buffer *buf;
828

829
830
831
832
833
834
835
836
837
838
839
840
	buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
	if (!buf)
		return -ENOMEM;

	buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
	file->private_data = buf;
	return 0;
}

static int debug_registers_open(struct inode *inode, struct file *file)
{
	file->private_data = alloc_buffer(inode->i_private,
841
			fill_registers_buffer);
842
843
844
845
846
847
848

	return file->private_data ? 0 : -ENOMEM;
}

static inline void create_debug_files(struct fotg210_hcd *fotg210)
{
	struct usb_bus *bus = &fotg210_to_hcd(fotg210)->self;
849
	struct dentry *root;
850

851
852
	root = debugfs_create_dir(bus->bus_name, fotg210_debug_root);
	fotg210->debug_dir = root;
853

854
855
856
857
858
	debugfs_create_file("async", S_IRUGO, root, bus, &debug_async_fops);
	debugfs_create_file("periodic", S_IRUGO, root, bus,
			    &debug_periodic_fops);
	debugfs_create_file("registers", S_IRUGO, root, bus,
			    &debug_registers_fops);
859
860
861
862
863
864
865
}

static inline void remove_debug_files(struct fotg210_hcd *fotg210)
{
	debugfs_remove_recursive(fotg210->debug_dir);
}

866
/* handshake - spin reading hc until handshake completes or fails
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
 * @ptr: address of hc register to be read
 * @mask: bits to look at in result of read
 * @done: value of those bits when handshake succeeds
 * @usec: timeout in microseconds
 *
 * Returns negative errno, or zero on success
 *
 * Success happens when the "mask" bits have the specified value (hardware
 * handshake done).  There are two failure modes:  "usec" have passed (major
 * hardware flakeout), or the register reads as all-ones (hardware removed).
 *
 * That last failure should_only happen in cases like physical cardbus eject
 * before driver shutdown. But it also seems to be caused by bugs in cardbus
 * bridge shutdown:  shutting down the bridge before the devices using it.
 */
static int handshake(struct fotg210_hcd *fotg210, void __iomem *ptr,
883
		u32 mask, u32 done, int usec)
884
{
885
	u32 result;
886
887
888
889
890
891
892
893
894
895
896
897
898
899

	do {
		result = fotg210_readl(fotg210, ptr);
		if (result == ~(u32)0)		/* card removed */
			return -ENODEV;
		result &= mask;
		if (result == done)
			return 0;
		udelay(1);
		usec--;
	} while (usec > 0);
	return -ETIMEDOUT;
}

900
/* Force HC to halt state from unknown (EHCI spec section 2.3).
901
902
903
904
 * Must be called with interrupts enabled and the lock not held.
 */
static int fotg210_halt(struct fotg210_hcd *fotg210)
{
905
	u32 temp;
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924

	spin_lock_irq(&fotg210->lock);

	/* disable any irqs left enabled by previous code */
	fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);

	/*
	 * This routine gets called during probe before fotg210->command
	 * has been initialized, so we can't rely on its value.
	 */
	fotg210->command &= ~CMD_RUN;
	temp = fotg210_readl(fotg210, &fotg210->regs->command);
	temp &= ~(CMD_RUN | CMD_IAAD);
	fotg210_writel(fotg210, temp, &fotg210->regs->command);

	spin_unlock_irq(&fotg210->lock);
	synchronize_irq(fotg210_to_hcd(fotg210)->irq);

	return handshake(fotg210, &fotg210->regs->status,
925
			STS_HALT, STS_HALT, 16 * 125);
926
927
}

928
/* Reset a non-running (STS_HALT == 1) controller.
929
930
931
932
 * Must be called with interrupts enabled and the lock not held.
 */
static int fotg210_reset(struct fotg210_hcd *fotg210)
{
933
934
	int retval;
	u32 command = fotg210_readl(fotg210, &fotg210->regs->command);
935
936

	/* If the EHCI debug controller is active, special care must be
937
938
	 * taken before and after a host controller reset
	 */
939
940
941
942
943
944
945
946
947
	if (fotg210->debug && !dbgp_reset_prep(fotg210_to_hcd(fotg210)))
		fotg210->debug = NULL;

	command |= CMD_RESET;
	dbg_cmd(fotg210, "reset", command);
	fotg210_writel(fotg210, command, &fotg210->regs->command);
	fotg210->rh_state = FOTG210_RH_HALTED;
	fotg210->next_statechange = jiffies;
	retval = handshake(fotg210, &fotg210->regs->command,
948
			CMD_RESET, 0, 250 * 1000);
949
950
951
952
953
954
955
956
957
958
959
960

	if (retval)
		return retval;

	if (fotg210->debug)
		dbgp_external_startup(fotg210_to_hcd(fotg210));

	fotg210->port_c_suspend = fotg210->suspended_ports =
			fotg210->resuming_ports = 0;
	return retval;
}

961
/* Idle the controller (turn off the schedules).
962
963
964
965
 * Must be called with interrupts enabled and the lock not held.
 */
static void fotg210_quiesce(struct fotg210_hcd *fotg210)
{
966
	u32 temp;
967
968
969
970
971
972
973

	if (fotg210->rh_state != FOTG210_RH_RUNNING)
		return;

	/* wait for any schedule enables/disables to take effect */
	temp = (fotg210->command << 10) & (STS_ASS | STS_PSS);
	handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, temp,
974
			16 * 125);
975
976
977
978
979
980
981
982
983

	/* then disable anything that's still active */
	spin_lock_irq(&fotg210->lock);
	fotg210->command &= ~(CMD_ASE | CMD_PSE);
	fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
	spin_unlock_irq(&fotg210->lock);

	/* hardware can take 16 microframes to turn off ... */
	handshake(fotg210, &fotg210->regs->status, STS_ASS | STS_PSS, 0,
984
			16 * 125);
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
}

static void end_unlink_async(struct fotg210_hcd *fotg210);
static void unlink_empty_async(struct fotg210_hcd *fotg210);
static void fotg210_work(struct fotg210_hcd *fotg210);
static void start_unlink_intr(struct fotg210_hcd *fotg210,
			      struct fotg210_qh *qh);
static void end_unlink_intr(struct fotg210_hcd *fotg210, struct fotg210_qh *qh);

/* Set a bit in the USBCMD register */
static void fotg210_set_command_bit(struct fotg210_hcd *fotg210, u32 bit)
{
	fotg210->command |= bit;
	fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);

	/* unblock posted write */
	fotg210_readl(fotg210, &fotg210->regs->command);
}

/* Clear a bit in the USBCMD register */
static void fotg210_clear_command_bit(struct fotg210_hcd *fotg210, u32 bit)
{
	fotg210->command &= ~bit;
	fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);

	/* unblock posted write */
	fotg210_readl(fotg210, &fotg210->regs->command);
}

1014
/* EHCI timer support...  Now using hrtimers.
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
 *
 * Lots of different events are triggered from fotg210->hrtimer.  Whenever
 * the timer routine runs, it checks each possible event; events that are
 * currently enabled and whose expiration time has passed get handled.
 * The set of enabled events is stored as a collection of bitflags in
 * fotg210->enabled_hrtimer_events, and they are numbered in order of
 * increasing delay values (ranging between 1 ms and 100 ms).
 *
 * Rather than implementing a sorted list or tree of all pending events,
 * we keep track only of the lowest-numbered pending event, in
 * fotg210->next_hrtimer_event.  Whenever fotg210->hrtimer gets restarted, its
 * expiration time is set to the timeout value for this event.
 *
 * As a result, events might not get handled right away; the actual delay
 * could be anywhere up to twice the requested delay.  This doesn't
 * matter, because none of the events are especially time-critical.  The
 * ones that matter most all have a delay of 1 ms, so they will be
 * handled after 2 ms at most, which is okay.  In addition to this, we
 * allow for an expiration range of 1 ms.
 */

1036
/* Delay lengths for the hrtimer event types.
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
 * Keep this list sorted by delay length, in the same order as
 * the event types indexed by enum fotg210_hrtimer_event in fotg210.h.
 */
static unsigned event_delays_ns[] = {
	1 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_POLL_ASS */
	1 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_POLL_PSS */
	1 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_POLL_DEAD */
	1125 * NSEC_PER_USEC,	/* FOTG210_HRTIMER_UNLINK_INTR */
	2 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_FREE_ITDS */
	6 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_ASYNC_UNLINKS */
	10 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_IAA_WATCHDOG */
	10 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_DISABLE_PERIODIC */
	15 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_DISABLE_ASYNC */
	100 * NSEC_PER_MSEC,	/* FOTG210_HRTIMER_IO_WATCHDOG */
};

/* Enable a pending hrtimer event */
static void fotg210_enable_event(struct fotg210_hcd *fotg210, unsigned event,
		bool resched)
{
1057
	ktime_t *timeout = &fotg210->hr_timeouts[event];
1058
1059

	if (resched)
1060
		*timeout = ktime_add(ktime_get(), event_delays_ns[event]);
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
	fotg210->enabled_hrtimer_events |= (1 << event);

	/* Track only the lowest-numbered pending event */
	if (event < fotg210->next_hrtimer_event) {
		fotg210->next_hrtimer_event = event;
		hrtimer_start_range_ns(&fotg210->hrtimer, *timeout,
				NSEC_PER_MSEC, HRTIMER_MODE_ABS);
	}
}


/* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */
static void fotg210_poll_ASS(struct fotg210_hcd *fotg210)
{
1075
	unsigned actual, want;
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088

	/* Don't enable anything if the controller isn't running (e.g., died) */
	if (fotg210->rh_state != FOTG210_RH_RUNNING)
		return;

	want = (fotg210->command & CMD_ASE) ? STS_ASS : 0;
	actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_ASS;

	if (want != actual) {

		/* Poll again later, but give up after about 20 ms */
		if (fotg210->ASS_poll_count++ < 20) {
			fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_ASS,
1089
					true);
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
			return;
		}
		fotg210_dbg(fotg210, "Waited too long for the async schedule status (%x/%x), giving up\n",
				want, actual);
	}
	fotg210->ASS_poll_count = 0;

	/* The status is up-to-date; restart or stop the schedule as needed */
	if (want == 0) {	/* Stopped */
		if (fotg210->async_count > 0)
			fotg210_set_command_bit(fotg210, CMD_ASE);

	} else {		/* Running */
		if (fotg210->async_count == 0) {

			/* Turn off the schedule after a while */
			fotg210_enable_event(fotg210,
1107
1108
					FOTG210_HRTIMER_DISABLE_ASYNC,
					true);
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
		}
	}
}

/* Turn off the async schedule after a brief delay */
static void fotg210_disable_ASE(struct fotg210_hcd *fotg210)
{
	fotg210_clear_command_bit(fotg210, CMD_ASE);
}


/* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */
static void fotg210_poll_PSS(struct fotg210_hcd *fotg210)
{
1123
	unsigned actual, want;
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136

	/* Don't do anything if the controller isn't running (e.g., died) */
	if (fotg210->rh_state != FOTG210_RH_RUNNING)
		return;

	want = (fotg210->command & CMD_PSE) ? STS_PSS : 0;
	actual = fotg210_readl(fotg210, &fotg210->regs->status) & STS_PSS;

	if (want != actual) {

		/* Poll again later, but give up after about 20 ms */
		if (fotg210->PSS_poll_count++ < 20) {
			fotg210_enable_event(fotg210, FOTG210_HRTIMER_POLL_PSS,
1137
					true);
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
			return;
		}
		fotg210_dbg(fotg210, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
				want, actual);
	}
	fotg210->PSS_poll_count = 0;

	/* The status is up-to-date; restart or stop the schedule as needed */
	if (want == 0) {	/* Stopped */
		if (fotg210->periodic_count > 0)
			fotg210_set_command_bit(fotg210, CMD_PSE);

	} else {		/* Running */
		if (fotg210->periodic_count == 0) {

			/* Turn off the schedule after a while */
			fotg210_enable_event(fotg210,
1155
1156
					FOTG210_HRTIMER_DISABLE_PERIODIC,
					true);
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
		}
	}
}

/* Turn off the periodic schedule after a brief delay */
static void fotg210_disable_PSE(struct fotg210_hcd *fotg210)
{
	fotg210_clear_command_bit(fotg210, CMD_PSE);
}


/* Poll the STS_HALT status bit; see when a dead controller stops */
static void fotg210_handle_controller_death(struct fotg210_hcd *fotg210)
{
	if (!(fotg210_readl(fotg210, &fotg210->regs->status) & STS_HALT)) {

		/* Give up after a few milliseconds */
		if (fotg210->died_poll_count++ < 5) {
			/* Try again later */
			fotg210_enable_event(fotg210,
1177
					FOTG210_HRTIMER_POLL_DEAD, true);
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
			return;
		}
		fotg210_warn(fotg210, "Waited too long for the controller to stop, giving up\n");
	}

	/* Clean up the mess */
	fotg210->rh_state = FOTG210_RH_HALTED;
	fotg210_writel(fotg210, 0, &fotg210->regs->intr_enable);
	fotg210_work(fotg210);
	end_unlink_async(fotg210);

	/* Not in process context, so don't try to reset the controller */
}


/* Handle unlinked interrupt QHs once they are gone from the hardware */
static void fotg210_handle_intr_unlinks(struct fotg210_hcd *fotg210)
{
1196
	bool stopped = (fotg210->rh_state < FOTG210_RH_RUNNING);
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206

	/*
	 * Process all the QHs on the intr_unlink list that were added
	 * before the current unlink cycle began.  The list is in
	 * temporal order, so stop when we reach the first entry in the
	 * current cycle.  But if the root hub isn't running then
	 * process all the QHs on the list.
	 */
	fotg210->intr_unlinking = true;
	while (fotg210->intr_unlink) {
1207
		struct fotg210_qh *qh = fotg210->intr_unlink;
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218

		if (!stopped && qh->unlink_cycle == fotg210->intr_unlink_cycle)
			break;
		fotg210->intr_unlink = qh->unlink_next;
		qh->unlink_next = NULL;
		end_unlink_intr(fotg210, qh);
	}

	/* Handle remaining entries later */
	if (fotg210->intr_unlink) {
		fotg210_enable_event(fotg210, FOTG210_HRTIMER_UNLINK_INTR,
1219
				true);
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
		++fotg210->intr_unlink_cycle;
	}
	fotg210->intr_unlinking = false;
}


/* Start another free-iTDs/siTDs cycle */
static void start_free_itds(struct fotg210_hcd *fotg210)
{
	if (!(fotg210->enabled_hrtimer_events &
			BIT(FOTG210_HRTIMER_FREE_ITDS))) {
		fotg210->last_itd_to_free = list_entry(
				fotg210->cached_itd_list.prev,
				struct fotg210_itd, itd_list);
		fotg210_enable_event(fotg210, FOTG210_HRTIMER_FREE_ITDS, true);
	}
}

/* Wait for controller to stop using old iTDs and siTDs */
static void end_free_itds(struct fotg210_hcd *fotg210)
{
1241
	struct fotg210_itd *itd, *n;
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289

	if (fotg210->rh_state < FOTG210_RH_RUNNING)
		fotg210->last_itd_to_free = NULL;

	list_for_each_entry_safe(itd, n, &fotg210->cached_itd_list, itd_list) {
		list_del(&itd->itd_list);
		dma_pool_free(fotg210->itd_pool, itd, itd->itd_dma);
		if (itd == fotg210->last_itd_to_free)
			break;
	}

	if (!list_empty(&fotg210->cached_itd_list))
		start_free_itds(fotg210);
}


/* Handle lost (or very late) IAA interrupts */
static void fotg210_iaa_watchdog(struct fotg210_hcd *fotg210)
{
	if (fotg210->rh_state != FOTG210_RH_RUNNING)
		return;

	/*
	 * Lost IAA irqs wedge things badly; seen first with a vt8235.
	 * So we need this watchdog, but must protect it against both
	 * (a) SMP races against real IAA firing and retriggering, and
	 * (b) clean HC shutdown, when IAA watchdog was pending.
	 */
	if (fotg210->async_iaa) {
		u32 cmd, status;

		/* If we get here, IAA is *REALLY* late.  It's barely
		 * conceivable that the system is so busy that CMD_IAAD
		 * is still legitimately set, so let's be sure it's
		 * clear before we read STS_IAA.  (The HC should clear
		 * CMD_IAAD when it sets STS_IAA.)
		 */
		cmd = fotg210_readl(fotg210, &fotg210->regs->command);

		/*
		 * If IAA is set here it either legitimately triggered
		 * after the watchdog timer expired (_way_ late, so we'll
		 * still count it as lost) ... or a silicon erratum:
		 * - VIA seems to set IAA without triggering the IRQ;
		 * - IAAD potentially cleared without setting IAA.
		 */
		status = fotg210_readl(fotg210, &fotg210->regs->status);
		if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
1290
			INCR(fotg210->stats.lost_iaa);
1291
			fotg210_writel(fotg210, STS_IAA,
1292
					&fotg210->regs->status);
1293
1294
		}

Oliver Neukum's avatar
Oliver Neukum committed
1295
		fotg210_dbg(fotg210, "IAA watchdog: status %x cmd %x\n",
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
				status, cmd);
		end_unlink_async(fotg210);
	}
}


/* Enable the I/O watchdog, if appropriate */
static void turn_on_io_watchdog(struct fotg210_hcd *fotg210)
{
	/* Not needed if the controller isn't running or it's already enabled */
	if (fotg210->rh_state != FOTG210_RH_RUNNING ||
			(fotg210->enabled_hrtimer_events &
1308
			BIT(FOTG210_HRTIMER_IO_WATCHDOG)))
1309
1310
1311
1312
1313
1314
1315
1316
1317
		return;

	/*
	 * Isochronous transfers always need the watchdog.
	 * For other sorts we use it only if the flag is set.
	 */
	if (fotg210->isoc_count > 0 || (fotg210->need_io_watchdog &&
			fotg210->async_count + fotg210->intr_count > 0))
		fotg210_enable_event(fotg210, FOTG210_HRTIMER_IO_WATCHDOG,
1318
				true);
1319
1320
1321
}


1322
/* Handler functions for the hrtimer event types.
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
 * Keep this array in the same order as the event types indexed by
 * enum fotg210_hrtimer_event in fotg210.h.
 */
static void (*event_handlers[])(struct fotg210_hcd *) = {
	fotg210_poll_ASS,			/* FOTG210_HRTIMER_POLL_ASS */
	fotg210_poll_PSS,			/* FOTG210_HRTIMER_POLL_PSS */
	fotg210_handle_controller_death,	/* FOTG210_HRTIMER_POLL_DEAD */
	fotg210_handle_intr_unlinks,	/* FOTG210_HRTIMER_UNLINK_INTR */
	end_free_itds,			/* FOTG210_HRTIMER_FREE_ITDS */
	unlink_empty_async,		/* FOTG210_HRTIMER_ASYNC_UNLINKS */
	fotg210_iaa_watchdog,		/* FOTG210_HRTIMER_IAA_WATCHDOG */
	fotg210_disable_PSE,		/* FOTG210_HRTIMER_DISABLE_PERIODIC */
	fotg210_disable_ASE,		/* FOTG210_HRTIMER_DISABLE_ASYNC */
	fotg210_work,			/* FOTG210_HRTIMER_IO_WATCHDOG */
};

static enum hrtimer_restart fotg210_hrtimer_func(struct hrtimer *t)
{
	struct fotg210_hcd *fotg210 =
			container_of(t, struct fotg210_hcd, hrtimer);
1343
1344
1345
1346
	ktime_t now;
	unsigned long events;
	unsigned long flags;
	unsigned e;
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359

	spin_lock_irqsave(&fotg210->lock, flags);

	events = fotg210->enabled_hrtimer_events;
	fotg210->enabled_hrtimer_events = 0;
	fotg210->next_hrtimer_event = FOTG210_HRTIMER_NO_EVENT;

	/*
	 * Check each pending event.  If its time has expired, handle
	 * the event; otherwise re-enable it.
	 */
	now = ktime_get();
	for_each_set_bit(e, &events, FOTG210_HRTIMER_NUM_EVENTS) {
1360
		if (ktime_compare(now, fotg210->hr_timeouts[e]) >= 0)
1361
1362
1363
1364
1365
1366
1367
1368
1369
			event_handlers[e](fotg210);
		else
			fotg210_enable_event(fotg210, e, false);
	}

	spin_unlock_irqrestore(&fotg210->lock, flags);
	return HRTIMER_NORESTART;
}

1370
1371
#define fotg210_bus_suspend NULL
#define fotg210_bus_resume NULL
1372

1373
1374
1375
static int check_reset_complete(struct fotg210_hcd *fotg210, int index,
		u32 __iomem *status_reg, int port_status)
{
1376
1377
1378
1379
	if (!(port_status & PORT_CONNECT))
		return port_status;

	/* if reset finished and it's still not enabled -- handoff */
1380
	if (!(port_status & PORT_PE))
1381
		/* with integrated TT, there's nobody to hand it to! */
1382
1383
1384
1385
		fotg210_dbg(fotg210, "Failed to enable port %d on root hub TT\n",
				index + 1);
	else
		fotg210_dbg(fotg210, "port %d reset complete, port enabled\n",
1386
				index + 1);
1387
1388
1389
1390
1391
1392
1393

	return port_status;
}


/* build "status change" packet (one or two bytes) from HC registers */

1394
static int fotg210_hub_status_data(struct usb_hcd *hcd, char *buf)
1395
{
1396
1397
1398
1399
1400
	struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
	u32 temp, status;
	u32 mask;
	int retval = 1;
	unsigned long flags;
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421

	/* init status to no-changes */
	buf[0] = 0;

	/* Inform the core about resumes-in-progress by returning
	 * a non-zero value even if there are no status changes.
	 */
	status = fotg210->resuming_ports;

	mask = PORT_CSC | PORT_PEC;
	/* PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND */

	/* no hub change reports (bit 0) for now (power, ...) */

	/* port N changes (bit N)? */
	spin_lock_irqsave(&fotg210->lock, flags);

	temp = fotg210_readl(fotg210, &fotg210->regs->port_status);

	/*
	 * Return status information even for ports with OWNER set.
1422
	 * Otherwise hub_wq wouldn't see the disconnect event when a
1423
1424
1425
1426
	 * high-speed device is switched over to the companion
	 * controller by the user.
	 */

1427
1428
1429
	if ((temp & mask) != 0 || test_bit(0, &fotg210->port_c_suspend) ||
			(fotg210->reset_done[0] &&
			time_after_eq(jiffies, fotg210->reset_done[0]))) {
1430
1431
1432
1433
1434
1435
1436
1437
		buf[0] |= 1 << 1;
		status = STS_PCD;
	}
	/* FIXME autosuspend idle root hubs */
	spin_unlock_irqrestore(&fotg210->lock, flags);
	return status ? retval : 0;
}

1438
1439
1440
1441
1442
static void fotg210_hub_descriptor(struct fotg210_hcd *fotg210,
		struct usb_hub_descriptor *desc)
{
	int ports = HCS_N_PORTS(fotg210->hcs_params);
	u16 temp;
1443

Sergei Shtylyov's avatar
Sergei Shtylyov committed
1444
	desc->bDescriptorType = USB_DT_HUB;
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
	desc->bPwrOn2PwrGood = 10;	/* fotg210 1.0, 2.3.9 says 20ms max */
	desc->bHubContrCurrent = 0;

	desc->bNbrPorts = ports;
	temp = 1 + (ports / 8);
	desc->bDescLength = 7 + 2 * temp;

	/* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
	memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
	memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);

Sergei Shtylyov's avatar
Sergei Shtylyov committed
1456
1457
	temp = HUB_CHAR_INDV_PORT_OCPM;	/* per-port overcurrent reporting */
	temp |= HUB_CHAR_NO_LPSM;	/* no power switching */
1458
1459
1460
	desc->wHubCharacteristics = cpu_to_le16(temp);
}

1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
static int fotg210_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
		u16 wIndex, char *buf, u16 wLength)
{
	struct fotg210_hcd *fotg210 = hcd_to_fotg210(hcd);
	int ports = HCS_N_PORTS(fotg210->hcs_params);
	u32 __iomem *status_reg = &fotg210->regs->port_status;
	u32 temp, temp1, status;
	unsigned long flags;
	int retval = 0;
	unsigned selector;
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499

	/*
	 * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
	 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
	 * (track current state ourselves) ... blink for diagnostics,
	 * power, "this is the one", etc.  EHCI spec supports this.
	 */

	spin_lock_irqsave(&fotg210->lock, flags);
	switch (typeReq) {
	case ClearHubFeature:
		switch (wValue) {
		case C_HUB_LOCAL_POWER:
		case C_HUB_OVER_CURRENT:
			/* no hub-wide feature/status flags */
			break;
		default:
			goto error;
		}
		break;
	case ClearPortFeature:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
		temp = fotg210_readl(fotg210, status_reg);
		temp &= ~PORT_RWC_BITS;

		/*
		 * Even if OWNER is set, so the port is owned by the
1500
		 * companion controller, hub_wq needs to be able to clear
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
		 * the port-change status bits (especially
		 * USB_PORT_STAT_C_CONNECTION).
		 */

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
			fotg210_writel(fotg210, temp & ~PORT_PE, status_reg);
			break;
		case USB_PORT_FEAT_C_ENABLE:
			fotg210_writel(fotg210, temp | PORT_PEC, status_reg);
			break;
		case USB_PORT_FEAT_SUSPEND:
			if (temp & PORT_RESET)
				goto error;
			if (!(temp & PORT_SUSPEND))
				break;
			if ((temp & PORT_PE) == 0)
				goto error;

			/* resume signaling for 20 msec */
			fotg210_writel(fotg210, temp | PORT_RESUME, status_reg);
			fotg210->reset_done[wIndex] = jiffies
1523
					+ msecs_to_jiffies(USB_RESUME_TIMEOUT);
1524
1525
1526
1527
1528
1529
1530
1531
1532
			break;
		case USB_PORT_FEAT_C_SUSPEND:
			clear_bit(wIndex, &fotg210->port_c_suspend);
			break;
		case USB_PORT_FEAT_C_CONNECTION:
			fotg210_writel(fotg210, temp | PORT_CSC, status_reg);
			break;
		case USB_PORT_FEAT_C_OVER_CURRENT:
			fotg210_writel(fotg210, temp | OTGISR_OVC,
1533
					&fotg210->regs->otgisr);
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
			break;
		case USB_PORT_FEAT_C_RESET:
			/* GetPortStatus clears reset */
			break;
		default:
			goto error;
		}
		fotg210_readl(fotg210, &fotg210->regs->command);
		break;
	case GetHubDescriptor:
		fotg210_hub_descriptor(fotg210, (struct usb_hub_descriptor *)
1545
				buf);
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
		break;
	case GetHubStatus:
		/* no hub-wide feature/status flags */
		memset(buf, 0, 4);
		/*cpu_to_le32s ((u32 *) buf); */
		break;
	case GetPortStatus:
		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
		status = 0;
		temp = fotg210_readl(fotg210, status_reg);

		/* wPortChange bits */
		if (temp & PORT_CSC)
			status |= USB_PORT_STAT_C_CONNECTION << 16;
		if (temp & PORT_PEC)
			status |= USB_PORT_STAT_C_ENABLE << 16;

		temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
		if (temp1 & OTGISR_OVC)
			status |= USB_PORT_STAT_C_OVERCURRENT << 16;

		/* whoever resumes must GetPortStatus to complete it!! */
		if (temp & PORT_RESUME) {

			/* Remote Wakeup received? */
			if (!fotg210->reset_done[wIndex]) {
				/* resume signaling for 20 msec */
				fotg210->reset_done[wIndex] = jiffies
						+ msecs_to_jiffies(20);
				/* check the port again */
				mod_timer(&fotg210_to_hcd(fotg210)->rh_timer,
						fotg210->reset_done[wIndex]);
			}

			/* resume completed? */
			else if (time_after_eq(jiffies,
					fotg210->reset_done[wIndex])) {
				clear_bit(wIndex, &fotg210->suspended_ports);
				set_bit(wIndex, &fotg210->port_c_suspend);
				fotg210->reset_done[wIndex] = 0;

				/* stop resume signaling */
				temp = fotg210_readl(fotg210, status_reg);
1591
1592
1593
				fotg210_writel(fotg210, temp &
						~(PORT_RWC_BITS | PORT_RESUME),
						status_reg);
1594
1595
				clear_bit(wIndex, &fotg210->resuming_ports);
				retval = handshake(fotg210, status_reg,
1596
						PORT_RESUME, 0, 2000);/* 2ms */
1597
1598
				if (retval != 0) {
					fotg210_err(fotg210,
1599
1600
							"port %d resume error %d\n",
							wIndex + 1, retval);
1601
1602
1603
1604
1605
1606
1607
					goto error;
				}
				temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
			}
		}

		/* whoever resets must GetPortStatus to complete it!! */
1608
1609
		if ((temp & PORT_RESET) && time_after_eq(jiffies,
				fotg210->reset_done[wIndex])) {
1610
1611
1612
1613
1614
1615
			status |= USB_PORT_STAT_C_RESET << 16;
			fotg210->reset_done[wIndex] = 0;
			clear_bit(wIndex, &fotg210->resuming_ports);

			/* force reset to complete */
			fotg210_writel(fotg210,
1616
1617
					temp & ~(PORT_RWC_BITS | PORT_RESET),
					status_reg);
1618
1619
1620
1621
1622
1623
1624
			/* REVISIT:  some hardware needs 550+ usec to clear
			 * this bit; seems too long to spin routinely...
			 */
			retval = handshake(fotg210, status_reg,
					PORT_RESET, 0, 1000);
			if (retval != 0) {
				fotg210_err(fotg210, "port %d reset error %d\n",
1625
						wIndex + 1, retval);
1626
1627
1628
1629
1630
1631
				goto error;
			}

			/* see what we found out */
			temp = check_reset_complete(fotg210, wIndex, status_reg,
					fotg210_readl(fotg210, status_reg));
1632
1633
1634
1635

			/* restart schedule */
			fotg210->command |= CMD_RUN;
			fotg210_writel(fotg210, fotg210->command, &fotg210->regs->command);
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
		}

		if (!(temp & (PORT_RESUME|PORT_RESET))) {
			fotg210->reset_done[wIndex] = 0;
			clear_bit(wIndex, &fotg210->resuming_ports);
		}

		/* transfer dedicated ports to the companion hc */
		if ((temp & PORT_CONNECT) &&
				test_bit(wIndex, &fotg210->companion_ports)) {
			temp &= ~PORT_RWC_BITS;
			fotg210_writel(fotg210, temp, status_reg);
			fotg210_dbg(fotg210, "port %d --> companion\n",
1649
					wIndex + 1);
1650
1651
1652
1653
			temp = fotg210_readl(fotg210, status_reg);
		}

		/*
1654
		 * Even if OWNER is set, there's no harm letting hub_wq
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
		 * see the wPortStatus values (they should all be 0 except
		 * for PORT_POWER anyway).
		 */

		if (temp & PORT_CONNECT) {
			status |= USB_PORT_STAT_CONNECTION;
			status |= fotg210_port_speed(fotg210, temp);
		}
		if (temp & PORT_PE)
			status |= USB_PORT_STAT_ENABLE;

		/* maybe the port was unsuspended without our knowledge */
		if (temp & (PORT_SUSPEND|PORT_RESUME)) {
			status |= USB_PORT_STAT_SUSPEND;
		} else if (test_bit(wIndex, &fotg210->suspended_ports)) {
			clear_bit(wIndex, &fotg210->suspended_ports);
			clear_bit(wIndex, &fotg210->resuming_ports);
			fotg210->reset_done[wIndex] = 0;
			if (temp & PORT_PE)
				set_bit(wIndex, &fotg210->port_c_suspend);
		}

		temp1 = fotg210_readl(fotg210, &fotg210->regs->otgisr);
		if (temp1 & OTGISR_OVC)
			status |= USB_PORT_STAT_OVERCURRENT;
		if (temp & PORT_RESET)
			status |= USB_PORT_STAT_RESET;
		if (test_bit(wIndex, &fotg210->port_c_suspend))
			status |= USB_PORT_STAT_C_SUSPEND << 16;

1685
1686
		if (status & ~0xffff)	/* only if wPortChange is interesting */
			dbg_port(fotg210, "GetStatus", wIndex + 1, temp);
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
		put_unaligned_le32(status, buf);
		break;
	case SetHubFeature:
		switch (wValue) {
		case C_HUB_LOCAL_POWER:
		case C_HUB_OVER_CURRENT:
			/* no hub-wide feature/status flags */
			break;
		default:
			goto error;
		}
		break;
	case SetPortFeature:
		selector = wIndex >> 8;
		wIndex &= 0xff;

		if (!wIndex || wIndex > ports)
			goto error;
		wIndex--;
		temp = fotg210_readl(fotg210, status_reg);
		temp &= ~PORT_RWC_BITS;
		switch (wValue) {
		case USB_PORT_FEAT_SUSPEND:
			if ((temp & PORT_PE) == 0
					|| (temp & PORT_RESET) != 0)
				goto error;

			/* After above check the port must be connected.
			 * Set appropriate bit thus could put phy into low power
			 * mode if we have hostpc feature
			 */
			fotg210_writel(fotg210, temp | PORT_SUSPEND,
1719
					status_reg);
1720
1721
1722
1723
1724
1725
1726
1727
1728
			set_bit(wIndex, &fotg210->suspended_ports);
			break;
		case USB_PORT_FEAT_RESET:
			if (temp & PORT_RESUME)
				goto error;
			/* line status bits may report this as low speed,
			 * which can be fine if this root hub has a
			 * transaction translator built in.
			 */
Oliver Neukum's avatar
Oliver Neukum committed
1729
			fotg210_dbg(fotg210, "port %d reset\n", wIndex + 1);
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
			temp |= PORT_RESET;
			temp &= ~PORT_PE;

			/*
			 * caller must wait, then call GetPortStatus
			 * usb 2.0 spec says 50 ms resets on root
			 */
			fotg210->reset_done[wIndex] = jiffies
					+ msecs_to_jiffies(50);
			fotg210_writel(fotg210, temp, status_reg);
			break;

		/* For downstream facing ports (these):  one hub port is put
		 * into test mode according to USB2 11.24.2.13, then the hub
		 * must be reset (which for root hub now means rmmod+modprobe,
		 * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
		 * about the EHCI-specific stuff.
		 */
		case USB_PORT_FEAT_TEST:
			if (!selector || selector > 5)
				goto error;
			spin_unlock_irqrestore(&fotg210->lock, flags);
			fotg210_quiesce(fotg210);
			spin_lock_irqsave(&fotg210->lock, flags);

			/* Put all enabled ports into suspend */
			temp = fotg210_readl(fotg210, status_reg) &
				~PORT_RWC_BITS;
			if (temp & PORT_PE)
				fotg210_writel(fotg210, temp | PORT_SUSPEND,
						status_reg);

			spin_unlock_irqrestore(&fotg210->lock, flags);
			fotg210_halt(fotg210);
			spin_lock_irqsave(&fotg210->lock, flags);

			temp = fotg210_readl(fotg210, status_reg);
			temp |= selector << 16;
			fotg210_writel(fotg210, temp, status_reg);
			break;

		default:
			goto error;
		}
		fotg210_readl(fotg210, &fotg210->regs->command);
		break;

	default:
error:
		/* "stall" on error */
		retval = -EPIPE;
	}
	spin_unlock_irqrestore(&fotg210->lock, flags);
	return retval;
}

static void __maybe_unused fotg210_relinquish_port(struct usb_hcd *hcd,
		int portnum)
{
	return;
}

static int __maybe_unused fotg210_port_handed_over(struct usb_hcd *hcd,
		int portnum)
{
	return 0;
}
1797
1798

/* There's basically three types of memory:
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
 *	- data used only by the HCD ... kmalloc is fine
 *	- async and periodic schedules, shared by HC and HCD ... these
 *	  need to use dma_pool or dma_alloc_coherent
 *	- driver buffers, read/written by HC ... single shot DMA mapped
 *
 * There's also "register" data (e.g. PCI or SOC), which is memory mapped.
 * No memory seen by this driver is pageable.
 */

/* Allocate the key transfer structures from the previously allocated pool */
static inline void fotg210_qtd_init(struct fotg210_hcd *fotg210,