ehci-dbg.c 25.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (c) 2001-2002 by David Brownell
3
 *
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* this file is part of ehci-hcd.c */

#define ehci_dbg(ehci, fmt, args...) \
	dev_dbg (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
#define ehci_err(ehci, fmt, args...) \
	dev_err (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
#define ehci_info(ehci, fmt, args...) \
	dev_info (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
#define ehci_warn(ehci, fmt, args...) \
	dev_warn (ehci_to_hcd(ehci)->self.controller , fmt , ## args )

D
David Brownell 已提交
30
#ifdef VERBOSE_DEBUG
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#	define vdbg dbg
#	define ehci_vdbg ehci_dbg
#else
#	define vdbg(fmt,args...) do { } while (0)
#	define ehci_vdbg(ehci, fmt, args...) do { } while (0)
#endif

#ifdef	DEBUG

/* check the values in the HCSPARAMS register
 * (host controller _Structural_ parameters)
 * see EHCI spec, Table 2-4 for each value
 */
static void dbg_hcs_params (struct ehci_hcd *ehci, char *label)
{
46
	u32	params = ehci_readl(ehci, &ehci->caps->hcs_params);
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54

	ehci_dbg (ehci,
		"%s hcs_params 0x%x dbg=%d%s cc=%d pcc=%d%s%s ports=%d\n",
		label, params,
		HCS_DEBUG_PORT (params),
		HCS_INDICATOR (params) ? " ind" : "",
		HCS_N_CC (params),
		HCS_N_PCC (params),
55
		HCS_PORTROUTED (params) ? "" : " ordered",
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65 66 67
		HCS_PPC (params) ? "" : " !ppc",
		HCS_N_PORTS (params)
		);
	/* Port routing, per EHCI 0.95 Spec, Section 2.2.5 */
	if (HCS_PORTROUTED (params)) {
		int i;
		char buf [46], tmp [7], byte;

		buf[0] = 0;
		for (i = 0; i < HCS_N_PORTS (params); i++) {
			// FIXME MIPS won't readb() ...
			byte = readb (&ehci->caps->portroute[(i>>1)]);
68
			sprintf(tmp, "%d ",
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
				((i & 0x1) ? ((byte)&0xf) : ((byte>>4)&0xf)));
			strcat(buf, tmp);
		}
		ehci_dbg (ehci, "%s portroute %s\n",
				label, buf);
	}
}
#else

static inline void dbg_hcs_params (struct ehci_hcd *ehci, char *label) {}

#endif

#ifdef	DEBUG

/* check the values in the HCCPARAMS register
 * (host controller _Capability_ parameters)
 * see EHCI Spec, Table 2-5 for each value
 * */
static void dbg_hcc_params (struct ehci_hcd *ehci, char *label)
{
90
	u32	params = ehci_readl(ehci, &ehci->caps->hcc_params);
L
Linus Torvalds 已提交
91 92 93

	if (HCC_ISOC_CACHE (params)) {
		ehci_dbg (ehci,
94 95 96 97 98
			"%s hcc_params %04x caching frame %s%s%s\n",
			label, params,
			HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
			HCC_CANPARK(params) ? " park" : "",
			HCC_64BIT_ADDR(params) ? " 64 bit addr" : "");
L
Linus Torvalds 已提交
99 100
	} else {
		ehci_dbg (ehci,
101 102 103 104 105 106 107
			"%s hcc_params %04x thresh %d uframes %s%s%s\n",
			label,
			params,
			HCC_ISOC_THRES(params),
			HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
			HCC_CANPARK(params) ? " park" : "",
			HCC_64BIT_ADDR(params) ? " 64 bit addr" : "");
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116 117
	}
}
#else

static inline void dbg_hcc_params (struct ehci_hcd *ehci, char *label) {}

#endif

#ifdef	DEBUG

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

134
static void __maybe_unused
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142
dbg_qh (const char *label, struct ehci_hcd *ehci, struct ehci_qh *qh)
{
	ehci_dbg (ehci, "%s qh %p n%08x info %x %x qtd %x\n", label,
		qh, qh->hw_next, qh->hw_info1, qh->hw_info2,
		qh->hw_current);
	dbg_qtd ("overlay", ehci, (struct ehci_qtd *) &qh->hw_qtd_next);
}

143
static void __maybe_unused
144
dbg_itd (const char *label, struct ehci_hcd *ehci, struct ehci_itd *itd)
L
Linus Torvalds 已提交
145 146
{
	ehci_dbg (ehci, "%s [%d] itd %p, next %08x, urb %p\n",
147 148
		label, itd->frame, itd, hc32_to_cpu(ehci, itd->hw_next),
		itd->urb);
L
Linus Torvalds 已提交
149
	ehci_dbg (ehci,
150
		"  trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
151 152 153 154 155 156 157 158
		hc32_to_cpu(ehci, itd->hw_transaction[0]),
		hc32_to_cpu(ehci, itd->hw_transaction[1]),
		hc32_to_cpu(ehci, itd->hw_transaction[2]),
		hc32_to_cpu(ehci, itd->hw_transaction[3]),
		hc32_to_cpu(ehci, itd->hw_transaction[4]),
		hc32_to_cpu(ehci, itd->hw_transaction[5]),
		hc32_to_cpu(ehci, itd->hw_transaction[6]),
		hc32_to_cpu(ehci, itd->hw_transaction[7]));
L
Linus Torvalds 已提交
159
	ehci_dbg (ehci,
160
		"  buf:   %08x %08x %08x %08x %08x %08x %08x\n",
161 162 163 164 165 166 167
		hc32_to_cpu(ehci, itd->hw_bufp[0]),
		hc32_to_cpu(ehci, itd->hw_bufp[1]),
		hc32_to_cpu(ehci, itd->hw_bufp[2]),
		hc32_to_cpu(ehci, itd->hw_bufp[3]),
		hc32_to_cpu(ehci, itd->hw_bufp[4]),
		hc32_to_cpu(ehci, itd->hw_bufp[5]),
		hc32_to_cpu(ehci, itd->hw_bufp[6]));
L
Linus Torvalds 已提交
168 169 170 171 172 173
	ehci_dbg (ehci, "  index: %d %d %d %d %d %d %d %d\n",
		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
static void __maybe_unused
175
dbg_sitd (const char *label, struct ehci_hcd *ehci, struct ehci_sitd *sitd)
L
Linus Torvalds 已提交
176 177
{
	ehci_dbg (ehci, "%s [%d] sitd %p, next %08x, urb %p\n",
178 179
		label, sitd->frame, sitd, hc32_to_cpu(ehci, sitd->hw_next),
		sitd->urb);
L
Linus Torvalds 已提交
180
	ehci_dbg (ehci,
181
		"  addr %08x sched %04x result %08x buf %08x %08x\n",
182 183 184 185 186
		hc32_to_cpu(ehci, sitd->hw_fullspeed_ep),
		hc32_to_cpu(ehci, sitd->hw_uframe),
		hc32_to_cpu(ehci, sitd->hw_results),
		hc32_to_cpu(ehci, sitd->hw_buf[0]),
		hc32_to_cpu(ehci, sitd->hw_buf[1]));
L
Linus Torvalds 已提交
187 188
}

189
static int __maybe_unused
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
{
	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" : ""
		);
}

208
static int __maybe_unused
L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
{
	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" : ""
		);
}

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

static int
dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
{
	return scnprintf (buf, len,
		"%s%scommand %06x %s=%d ithresh=%d%s%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_LRESET) ? " LReset" : "",
		(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 int
dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
{
	char	*sig;

	/* 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;
	}

	return scnprintf (buf, len,
D
David Brownell 已提交
259
		"%s%sport %d status %06x%s%s sig=%s%s%s%s%s%s%s%s%s%s",
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270 271
		label, label [0] ? " " : "", port, status,
		(status & PORT_POWER) ? " POWER" : "",
		(status & PORT_OWNER) ? " OWNER" : "",
		sig,
		(status & PORT_RESET) ? " RESET" : "",
		(status & PORT_SUSPEND) ? " SUSPEND" : "",
		(status & PORT_RESUME) ? " RESUME" : "",
		(status & PORT_OCC) ? " OCC" : "",
		(status & PORT_OC) ? " OC" : "",
		(status & PORT_PEC) ? " PEC" : "",
		(status & PORT_PE) ? " PE" : "",
		(status & PORT_CSC) ? " CSC" : "",
272
		(status & PORT_CONNECT) ? " CONNECT" : "");
L
Linus Torvalds 已提交
273 274 275
}

#else
276
static inline void __maybe_unused
L
Linus Torvalds 已提交
277 278 279
dbg_qh (char *label, struct ehci_hcd *ehci, struct ehci_qh *qh)
{}

280
static inline int __maybe_unused
L
Linus Torvalds 已提交
281 282 283
dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
{ return 0; }

284
static inline int __maybe_unused
L
Linus Torvalds 已提交
285 286 287
dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
{ return 0; }

288
static inline int __maybe_unused
L
Linus Torvalds 已提交
289 290 291
dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
{ return 0; }

292
static inline int __maybe_unused
L
Linus Torvalds 已提交
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 322 323 324 325
dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
{ return 0; }

#endif	/* DEBUG */

/* functions have the "wrong" filename when they're output... */
#define dbg_status(ehci, label, status) { \
	char _buf [80]; \
	dbg_status_buf (_buf, sizeof _buf, label, status); \
	ehci_dbg (ehci, "%s\n", _buf); \
}

#define dbg_cmd(ehci, label, command) { \
	char _buf [80]; \
	dbg_command_buf (_buf, sizeof _buf, label, command); \
	ehci_dbg (ehci, "%s\n", _buf); \
}

#define dbg_port(ehci, label, port, status) { \
	char _buf [80]; \
	dbg_port_buf (_buf, sizeof _buf, label, port, status); \
	ehci_dbg (ehci, "%s\n", _buf); \
}

/*-------------------------------------------------------------------------*/

#ifdef STUB_DEBUG_FILES

static inline void create_debug_files (struct ehci_hcd *bus) { }
static inline void remove_debug_files (struct ehci_hcd *bus) { }

#else

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
/* 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,
};
static const struct file_operations debug_periodic_fops = {
	.owner		= THIS_MODULE,
	.open		= debug_periodic_open,
	.read		= debug_output,
	.release	= debug_close,
};
static const struct file_operations debug_registers_fops = {
	.owner		= THIS_MODULE,
	.open		= debug_registers_open,
	.read		= debug_output,
	.release	= debug_close,
};

static struct dentry *ehci_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 *page;
};
L
Linus Torvalds 已提交
363 364 365 366 367 368 369 370 371

#define speed_char(info1) ({ char tmp; \
		switch (info1 & (3 << 12)) { \
		case 0 << 12: tmp = 'f'; break; \
		case 1 << 12: tmp = 'l'; break; \
		case 2 << 12: tmp = 'h'; break; \
		default: tmp = '?'; break; \
		}; tmp; })

372
static inline char token_mark(struct ehci_hcd *ehci, __hc32 token)
L
Linus Torvalds 已提交
373
{
374 375
	__u32 v = hc32_to_cpu(ehci, token);

L
Linus Torvalds 已提交
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
	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 '/';
}

static void qh_lines (
	struct ehci_hcd *ehci,
	struct ehci_qh *qh,
	char **nextp,
	unsigned *sizep
)
{
	u32			scratch;
	u32			hw_curr;
	struct list_head	*entry;
	struct ehci_qtd		*td;
	unsigned		temp;
	unsigned		size = *sizep;
	char			*next = *nextp;
	char			mark;
A
Al Viro 已提交
401
	__le32			list_end = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
402

403
	if (qh->hw_qtd_next == list_end)	/* NEC does this */
L
Linus Torvalds 已提交
404 405
		mark = '@';
	else
406
		mark = token_mark(ehci, qh->hw_token);
L
Linus Torvalds 已提交
407
	if (mark == '/') {	/* qh_alt_next controls qh advance? */
408 409
		if ((qh->hw_alt_next & QTD_MASK(ehci))
				== ehci->async->hw_alt_next)
L
Linus Torvalds 已提交
410
			mark = '#';	/* blocked */
411
		else if (qh->hw_alt_next == list_end)
L
Linus Torvalds 已提交
412 413 414
			mark = '.';	/* use hw_qtd_next */
		/* else alt_next points to some other qtd */
	}
415 416
	scratch = hc32_to_cpup(ehci, &qh->hw_info1);
	hw_curr = (mark == '*') ? hc32_to_cpup(ehci, &qh->hw_current) : 0;
L
Linus Torvalds 已提交
417 418 419 420 421
	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,
422 423 424
			scratch, hc32_to_cpup(ehci, &qh->hw_info2),
			hc32_to_cpup(ehci, &qh->hw_token), mark,
			(cpu_to_hc32(ehci, QTD_TOGGLE) & qh->hw_token)
L
Linus Torvalds 已提交
425
				? "data1" : "data0",
426
			(hc32_to_cpup(ehci, &qh->hw_alt_next) >> 1) & 0x0f);
L
Linus Torvalds 已提交
427 428 429 430 431 432
	size -= temp;
	next += temp;

	/* hc may be modifying the list as we read it ... */
	list_for_each (entry, &qh->qtd_list) {
		td = list_entry (entry, struct ehci_qtd, qtd_list);
433
		scratch = hc32_to_cpup(ehci, &td->hw_token);
L
Linus Torvalds 已提交
434 435 436
		mark = ' ';
		if (hw_curr == td->qtd_dma)
			mark = '*';
437
		else if (qh->hw_qtd_next == cpu_to_hc32(ehci, td->qtd_dma))
L
Linus Torvalds 已提交
438 439 440 441
			mark = '+';
		else if (QTD_LENGTH (scratch)) {
			if (td->hw_alt_next == ehci->async->hw_alt_next)
				mark = '#';
442
			else if (td->hw_alt_next != list_end)
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
				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 (temp < 0)
			temp = 0;
		else if (size < temp)
			temp = size;
		size -= temp;
		next += temp;
		if (temp == size)
			goto done;
	}

	temp = snprintf (next, size, "\n");
	if (temp < 0)
		temp = 0;
	else if (size < temp)
		temp = size;
	size -= temp;
	next += temp;

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

480
static ssize_t fill_async_buffer(struct debug_buffer *buf)
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488
{
	struct usb_hcd		*hcd;
	struct ehci_hcd		*ehci;
	unsigned long		flags;
	unsigned		temp, size;
	char			*next;
	struct ehci_qh		*qh;

489
	hcd = bus_to_hcd(buf->bus);
L
Linus Torvalds 已提交
490
	ehci = hcd_to_ehci (hcd);
491
	next = buf->page;
L
Linus Torvalds 已提交
492 493
	size = PAGE_SIZE;

494 495
	*next = 0;

L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	/* 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 (&ehci->lock, flags);
	for (qh = ehci->async->qh_next.qh; size > 0 && qh; qh = qh->qh_next.qh)
		qh_lines (ehci, qh, &next, &size);
	if (ehci->reclaim && size > 0) {
		temp = scnprintf (next, size, "\nreclaim =\n");
		size -= temp;
		next += temp;

		for (qh = ehci->reclaim; size > 0 && qh; qh = qh->reclaim)
			qh_lines (ehci, qh, &next, &size);
	}
	spin_unlock_irqrestore (&ehci->lock, flags);

513
	return strlen(buf->page);
L
Linus Torvalds 已提交
514 515 516
}

#define DBG_SCHED_LIMIT 64
517
static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525
{
	struct usb_hcd		*hcd;
	struct ehci_hcd		*ehci;
	unsigned long		flags;
	union ehci_shadow	p, *seen;
	unsigned		temp, size, seen_count;
	char			*next;
	unsigned		i;
526
	__hc32			tag;
L
Linus Torvalds 已提交
527

528
	if (!(seen = kmalloc (DBG_SCHED_LIMIT * sizeof *seen, GFP_ATOMIC)))
L
Linus Torvalds 已提交
529 530 531
		return 0;
	seen_count = 0;

532
	hcd = bus_to_hcd(buf->bus);
L
Linus Torvalds 已提交
533
	ehci = hcd_to_ehci (hcd);
534
	next = buf->page;
L
Linus Torvalds 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548
	size = PAGE_SIZE;

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

	/* dump a snapshot of the periodic schedule.
	 * iso changes, interrupt usually doesn't.
	 */
	spin_lock_irqsave (&ehci->lock, flags);
	for (i = 0; i < ehci->periodic_size; i++) {
		p = ehci->pshadow [i];
		if (likely (!p.ptr))
			continue;
549
		tag = Q_NEXT_TYPE(ehci, ehci->periodic [i]);
L
Linus Torvalds 已提交
550 551 552 553 554 555

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

		do {
556
			switch (hc32_to_cpu(ehci, tag)) {
L
Linus Torvalds 已提交
557 558 559
			case Q_TYPE_QH:
				temp = scnprintf (next, size, " qh%d-%04x/%p",
						p.qh->period,
560 561
						hc32_to_cpup(ehci,
								&p.qh->hw_info2)
L
Linus Torvalds 已提交
562
							/* uframe masks */
563
							& (QH_CMASK | QH_SMASK),
L
Linus Torvalds 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
						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,
							" ...");
					p.ptr = NULL;
					break;
				}
				/* show more info the first time around */
				if (temp == seen_count && p.ptr) {
579
					u32	scratch = hc32_to_cpup(ehci,
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587 588 589
							&p.qh->hw_info1);
					struct ehci_qtd	*qtd;
					char		*type = "";

					/* count tds, get ep direction */
					temp = 0;
					list_for_each_entry (qtd,
							&p.qh->qtd_list,
							qtd_list) {
						temp++;
590 591
						switch (0x03 & (hc32_to_cpu(
							ehci,
L
Linus Torvalds 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
							qtd->hw_token) >> 8)) {
						case 0: type = "out"; continue;
						case 1: type = "in"; continue;
						}
					}

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

					if (seen_count < DBG_SCHED_LIMIT)
						seen [seen_count++].qh = p.qh;
				} else
					temp = 0;
				if (p.qh) {
613
					tag = Q_NEXT_TYPE(ehci, p.qh->hw_next);
L
Linus Torvalds 已提交
614 615 616 617 618 619 620
					p = p.qh->qh_next;
				}
				break;
			case Q_TYPE_FSTN:
				temp = scnprintf (next, size,
					" fstn-%8x/%p", p.fstn->hw_prev,
					p.fstn);
621
				tag = Q_NEXT_TYPE(ehci, p.fstn->hw_next);
L
Linus Torvalds 已提交
622 623 624 625 626
				p = p.fstn->fstn_next;
				break;
			case Q_TYPE_ITD:
				temp = scnprintf (next, size,
					" itd/%p", p.itd);
627
				tag = Q_NEXT_TYPE(ehci, p.itd->hw_next);
L
Linus Torvalds 已提交
628 629 630 631 632 633
				p = p.itd->itd_next;
				break;
			case Q_TYPE_SITD:
				temp = scnprintf (next, size,
					" sitd%d-%04x/%p",
					p.sitd->stream->interval,
634
					hc32_to_cpup(ehci, &p.sitd->hw_uframe)
L
Linus Torvalds 已提交
635 636
						& 0x0000ffff,
					p.sitd);
637
				tag = Q_NEXT_TYPE(ehci, p.sitd->hw_next);
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
				p = p.sitd->sitd_next;
				break;
			}
			size -= temp;
			next += temp;
		} while (p.ptr);

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

	return PAGE_SIZE - size;
}
#undef DBG_SCHED_LIMIT

656
static ssize_t fill_registers_buffer(struct debug_buffer *buf)
L
Linus Torvalds 已提交
657 658 659 660 661 662 663 664 665
{
	struct usb_hcd		*hcd;
	struct ehci_hcd		*ehci;
	unsigned long		flags;
	unsigned		temp, size, i;
	char			*next, scratch [80];
	static char		fmt [] = "%*s\n";
	static char		label [] = "";

666
	hcd = bus_to_hcd(buf->bus);
L
Linus Torvalds 已提交
667
	ehci = hcd_to_ehci (hcd);
668
	next = buf->page;
L
Linus Torvalds 已提交
669 670 671 672
	size = PAGE_SIZE;

	spin_lock_irqsave (&ehci->lock, flags);

A
Alan Stern 已提交
673
	if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
L
Linus Torvalds 已提交
674 675
		size = scnprintf (next, size,
			"bus %s, device %s (driver " DRIVER_VERSION ")\n"
D
David Brownell 已提交
676
			"%s\n"
L
Linus Torvalds 已提交
677 678
			"SUSPENDED (no register access)\n",
			hcd->self.controller->bus->name,
679
			dev_name(hcd->self.controller),
D
David Brownell 已提交
680
			hcd->product_desc);
L
Linus Torvalds 已提交
681 682 683 684
		goto done;
	}

	/* Capability Registers */
685
	i = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
L
Linus Torvalds 已提交
686 687
	temp = scnprintf (next, size,
		"bus %s, device %s (driver " DRIVER_VERSION ")\n"
D
David Brownell 已提交
688
		"%s\n"
L
Linus Torvalds 已提交
689 690
		"EHCI %x.%02x, hcd state %d\n",
		hcd->self.controller->bus->name,
691
		dev_name(hcd->self.controller),
D
David Brownell 已提交
692
		hcd->product_desc,
L
Linus Torvalds 已提交
693 694 695 696
		i >> 8, i & 0x0ff, hcd->state);
	size -= temp;
	next += temp;

D
David Brownell 已提交
697 698 699 700 701 702 703 704
#ifdef	CONFIG_PCI
	/* EHCI 0.96 and later may have "extended capabilities" */
	if (hcd->self.controller->bus == &pci_bus_type) {
		struct pci_dev	*pdev;
		u32		offset, cap, cap2;
		unsigned	count = 256/4;

		pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller);
705 706
		offset = HCC_EXT_CAPS(ehci_readl(ehci,
				&ehci->caps->hcc_params));
D
David Brownell 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
		while (offset && count--) {
			pci_read_config_dword (pdev, offset, &cap);
			switch (cap & 0xff) {
			case 1:
				temp = scnprintf (next, size,
					"ownership %08x%s%s\n", cap,
					(cap & (1 << 24)) ? " linux" : "",
					(cap & (1 << 16)) ? " firmware" : "");
				size -= temp;
				next += temp;

				offset += 4;
				pci_read_config_dword (pdev, offset, &cap2);
				temp = scnprintf (next, size,
					"SMI sts/enable 0x%08x\n", cap2);
				size -= temp;
				next += temp;
				break;
			case 0:		/* illegal reserved capability */
				cap = 0;
				/* FALLTHROUGH */
			default:		/* unknown */
				break;
			}
			temp = (cap >> 8) & 0xff;
		}
	}
#endif

L
Linus Torvalds 已提交
736
	// FIXME interpret both types of params
737
	i = ehci_readl(ehci, &ehci->caps->hcs_params);
L
Linus Torvalds 已提交
738 739 740 741
	temp = scnprintf (next, size, "structural params 0x%08x\n", i);
	size -= temp;
	next += temp;

742
	i = ehci_readl(ehci, &ehci->caps->hcc_params);
L
Linus Torvalds 已提交
743 744 745 746 747 748
	temp = scnprintf (next, size, "capability params 0x%08x\n", i);
	size -= temp;
	next += temp;

	/* Operational Registers */
	temp = dbg_status_buf (scratch, sizeof scratch, label,
749
			ehci_readl(ehci, &ehci->regs->status));
L
Linus Torvalds 已提交
750 751 752 753 754
	temp = scnprintf (next, size, fmt, temp, scratch);
	size -= temp;
	next += temp;

	temp = dbg_command_buf (scratch, sizeof scratch, label,
755
			ehci_readl(ehci, &ehci->regs->command));
L
Linus Torvalds 已提交
756 757 758 759 760
	temp = scnprintf (next, size, fmt, temp, scratch);
	size -= temp;
	next += temp;

	temp = dbg_intr_buf (scratch, sizeof scratch, label,
761
			ehci_readl(ehci, &ehci->regs->intr_enable));
L
Linus Torvalds 已提交
762 763 764 765 766
	temp = scnprintf (next, size, fmt, temp, scratch);
	size -= temp;
	next += temp;

	temp = scnprintf (next, size, "uframe %04x\n",
767
			ehci_readl(ehci, &ehci->regs->frame_index));
L
Linus Torvalds 已提交
768 769 770
	size -= temp;
	next += temp;

D
David Brownell 已提交
771 772
	for (i = 1; i <= HCS_N_PORTS (ehci->hcs_params); i++) {
		temp = dbg_port_buf (scratch, sizeof scratch, label, i,
773 774
				ehci_readl(ehci,
					&ehci->regs->port_status[i - 1]));
L
Linus Torvalds 已提交
775 776 777
		temp = scnprintf (next, size, fmt, temp, scratch);
		size -= temp;
		next += temp;
D
David Brownell 已提交
778 779 780
		if (i == HCS_DEBUG_PORT(ehci->hcs_params) && ehci->debug) {
			temp = scnprintf (next, size,
					"    debug control %08x\n",
781 782
					ehci_readl(ehci,
						&ehci->debug->control));
D
David Brownell 已提交
783 784 785
			size -= temp;
			next += temp;
		}
L
Linus Torvalds 已提交
786 787 788
	}

	if (ehci->reclaim) {
789
		temp = scnprintf(next, size, "reclaim qh %p\n", ehci->reclaim);
L
Linus Torvalds 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
		size -= temp;
		next += temp;
	}

#ifdef EHCI_STATS
	temp = scnprintf (next, size,
		"irq normal %ld err %ld reclaim %ld (lost %ld)\n",
		ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
		ehci->stats.lost_iaa);
	size -= temp;
	next += temp;

	temp = scnprintf (next, size, "complete %ld unlink %ld\n",
		ehci->stats.complete, ehci->stats.unlink);
	size -= temp;
	next += temp;
#endif

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

	return PAGE_SIZE - size;
}

814 815
static struct debug_buffer *alloc_buffer(struct usb_bus *bus,
				ssize_t (*fill_func)(struct debug_buffer *))
L
Linus Torvalds 已提交
816
{
817 818 819
	struct debug_buffer *buf;

	buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
L
Linus Torvalds 已提交
820

821 822 823 824 825 826 827
	if (buf) {
		buf->bus = bus;
		buf->fill_func = fill_func;
		mutex_init(&buf->mutex);
	}

	return buf;
L
Linus Torvalds 已提交
828 829
}

830
static int fill_buffer(struct debug_buffer *buf)
L
Linus Torvalds 已提交
831
{
832 833 834 835 836 837 838 839 840 841 842
	int ret = 0;

	if (!buf->page)
		buf->page = (char *)get_zeroed_page(GFP_KERNEL);

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

	ret = buf->fill_func(buf);
L
Linus Torvalds 已提交
843

844 845 846 847 848 849 850
	if (ret >= 0) {
		buf->count = ret;
		ret = 0;
	}

out:
	return ret;
L
Linus Torvalds 已提交
851 852
}

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
static ssize_t debug_output(struct file *file, char __user *user_buf,
			    size_t len, loff_t *offset)
{
	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,
				      buf->page, buf->count);

out:
	return ret;

}

static int debug_close(struct inode *inode, struct file *file)
{
	struct debug_buffer *buf = file->private_data;
L
Linus Torvalds 已提交
880

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
	if (buf) {
		if (buf->page)
			free_page((unsigned long)buf->page);
		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)
{
	file->private_data = alloc_buffer(inode->i_private,
					  fill_periodic_buffer);

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

static int debug_registers_open(struct inode *inode, struct file *file)
{
	file->private_data = alloc_buffer(inode->i_private,
					  fill_registers_buffer);

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

static inline void create_debug_files (struct ehci_hcd *ehci)
{
	struct usb_bus *bus = &ehci_to_hcd(ehci)->self;

	ehci->debug_dir = debugfs_create_dir(bus->bus_name, ehci_debug_root);
	if (!ehci->debug_dir)
		goto dir_error;

	ehci->debug_async = debugfs_create_file("async", S_IRUGO,
						ehci->debug_dir, bus,
						&debug_async_fops);
	if (!ehci->debug_async)
		goto async_error;

	ehci->debug_periodic = debugfs_create_file("periodic", S_IRUGO,
						   ehci->debug_dir, bus,
						   &debug_periodic_fops);
	if (!ehci->debug_periodic)
		goto periodic_error;

	ehci->debug_registers = debugfs_create_file("registers", S_IRUGO,
						    ehci->debug_dir, bus,
						    &debug_registers_fops);
	if (!ehci->debug_registers)
		goto registers_error;
	return;

registers_error:
	debugfs_remove(ehci->debug_periodic);
periodic_error:
	debugfs_remove(ehci->debug_async);
async_error:
	debugfs_remove(ehci->debug_dir);
dir_error:
	ehci->debug_periodic = NULL;
	ehci->debug_async = NULL;
	ehci->debug_dir = NULL;
}

static inline void remove_debug_files (struct ehci_hcd *ehci)
{
	debugfs_remove(ehci->debug_registers);
	debugfs_remove(ehci->debug_periodic);
	debugfs_remove(ehci->debug_async);
	debugfs_remove(ehci->debug_dir);
}

#endif /* STUB_DEBUG_FILES */