1/*
2 * Copyright (c) 2001-2002 by David Brownell
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* this file is part of ehci-hcd.c */
20
21#define ehci_dbg(ehci, fmt, args...) \
22	dev_dbg (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
23#define ehci_err(ehci, fmt, args...) \
24	dev_err (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
25#define ehci_info(ehci, fmt, args...) \
26	dev_info (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
27#define ehci_warn(ehci, fmt, args...) \
28	dev_warn (ehci_to_hcd(ehci)->self.controller , fmt , ## args )
29
30#ifdef VERBOSE_DEBUG
31#	define ehci_vdbg ehci_dbg
32#else
33	static inline void ehci_vdbg(struct ehci_hcd *ehci, ...) {}
34#endif
35
36#ifdef	DEBUG
37
38/* check the values in the HCSPARAMS register
39 * (host controller _Structural_ parameters)
40 * see EHCI spec, Table 2-4 for each value
41 */
42static void dbg_hcs_params (struct ehci_hcd *ehci, char *label)
43{
44	u32	params = ehci_readl(ehci, &ehci->caps->hcs_params);
45
46	ehci_dbg (ehci,
47		"%s hcs_params 0x%x dbg=%d%s cc=%d pcc=%d%s%s ports=%d\n",
48		label, params,
49		HCS_DEBUG_PORT (params),
50		HCS_INDICATOR (params) ? " ind" : "",
51		HCS_N_CC (params),
52		HCS_N_PCC (params),
53		HCS_PORTROUTED (params) ? "" : " ordered",
54		HCS_PPC (params) ? "" : " !ppc",
55		HCS_N_PORTS (params)
56		);
57	/* Port routing, per EHCI 0.95 Spec, Section 2.2.5 */
58	if (HCS_PORTROUTED (params)) {
59		int i;
60		char buf [46], tmp [7], byte;
61
62		buf[0] = 0;
63		for (i = 0; i < HCS_N_PORTS (params); i++) {
64			// FIXME MIPS won't readb() ...
65			byte = readb (&ehci->caps->portroute[(i>>1)]);
66			sprintf(tmp, "%d ",
67				((i & 0x1) ? ((byte)&0xf) : ((byte>>4)&0xf)));
68			strcat(buf, tmp);
69		}
70		ehci_dbg (ehci, "%s portroute %s\n",
71				label, buf);
72	}
73}
74#else
75
76static inline void dbg_hcs_params (struct ehci_hcd *ehci, char *label) {}
77
78#endif
79
80#ifdef	DEBUG
81
82/* check the values in the HCCPARAMS register
83 * (host controller _Capability_ parameters)
84 * see EHCI Spec, Table 2-5 for each value
85 * */
86static void dbg_hcc_params (struct ehci_hcd *ehci, char *label)
87{
88	u32	params = ehci_readl(ehci, &ehci->caps->hcc_params);
89
90	if (HCC_ISOC_CACHE (params)) {
91		ehci_dbg (ehci,
92			"%s hcc_params %04x caching frame %s%s%s\n",
93			label, params,
94			HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
95			HCC_CANPARK(params) ? " park" : "",
96			HCC_64BIT_ADDR(params) ? " 64 bit addr" : "");
97	} else {
98		ehci_dbg (ehci,
99			"%s hcc_params %04x thresh %d uframes %s%s%s%s%s%s%s\n",
100			label,
101			params,
102			HCC_ISOC_THRES(params),
103			HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
104			HCC_CANPARK(params) ? " park" : "",
105			HCC_64BIT_ADDR(params) ? " 64 bit addr" : "",
106			HCC_LPM(params) ? " LPM" : "",
107			HCC_PER_PORT_CHANGE_EVENT(params) ? " ppce" : "",
108			HCC_HW_PREFETCH(params) ? " hw prefetch" : "",
109			HCC_32FRAME_PERIODIC_LIST(params) ?
110				" 32 periodic list" : "");
111	}
112}
113#else
114
115static inline void dbg_hcc_params (struct ehci_hcd *ehci, char *label) {}
116
117#endif
118
119#ifdef	DEBUG
120
121static void __maybe_unused
122dbg_qtd (const char *label, struct ehci_hcd *ehci, struct ehci_qtd *qtd)
123{
124	ehci_dbg(ehci, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
125		hc32_to_cpup(ehci, &qtd->hw_next),
126		hc32_to_cpup(ehci, &qtd->hw_alt_next),
127		hc32_to_cpup(ehci, &qtd->hw_token),
128		hc32_to_cpup(ehci, &qtd->hw_buf [0]));
129	if (qtd->hw_buf [1])
130		ehci_dbg(ehci, "  p1=%08x p2=%08x p3=%08x p4=%08x\n",
131			hc32_to_cpup(ehci, &qtd->hw_buf[1]),
132			hc32_to_cpup(ehci, &qtd->hw_buf[2]),
133			hc32_to_cpup(ehci, &qtd->hw_buf[3]),
134			hc32_to_cpup(ehci, &qtd->hw_buf[4]));
135}
136
137static void __maybe_unused
138dbg_qh (const char *label, struct ehci_hcd *ehci, struct ehci_qh *qh)
139{
140	struct ehci_qh_hw *hw = qh->hw;
141
142	ehci_dbg (ehci, "%s qh %p n%08x info %x %x qtd %x\n", label,
143		qh, hw->hw_next, hw->hw_info1, hw->hw_info2, hw->hw_current);
144	dbg_qtd("overlay", ehci, (struct ehci_qtd *) &hw->hw_qtd_next);
145}
146
147static void __maybe_unused
148dbg_itd (const char *label, struct ehci_hcd *ehci, struct ehci_itd *itd)
149{
150	ehci_dbg (ehci, "%s [%d] itd %p, next %08x, urb %p\n",
151		label, itd->frame, itd, hc32_to_cpu(ehci, itd->hw_next),
152		itd->urb);
153	ehci_dbg (ehci,
154		"  trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
155		hc32_to_cpu(ehci, itd->hw_transaction[0]),
156		hc32_to_cpu(ehci, itd->hw_transaction[1]),
157		hc32_to_cpu(ehci, itd->hw_transaction[2]),
158		hc32_to_cpu(ehci, itd->hw_transaction[3]),
159		hc32_to_cpu(ehci, itd->hw_transaction[4]),
160		hc32_to_cpu(ehci, itd->hw_transaction[5]),
161		hc32_to_cpu(ehci, itd->hw_transaction[6]),
162		hc32_to_cpu(ehci, itd->hw_transaction[7]));
163	ehci_dbg (ehci,
164		"  buf:   %08x %08x %08x %08x %08x %08x %08x\n",
165		hc32_to_cpu(ehci, itd->hw_bufp[0]),
166		hc32_to_cpu(ehci, itd->hw_bufp[1]),
167		hc32_to_cpu(ehci, itd->hw_bufp[2]),
168		hc32_to_cpu(ehci, itd->hw_bufp[3]),
169		hc32_to_cpu(ehci, itd->hw_bufp[4]),
170		hc32_to_cpu(ehci, itd->hw_bufp[5]),
171		hc32_to_cpu(ehci, itd->hw_bufp[6]));
172	ehci_dbg (ehci, "  index: %d %d %d %d %d %d %d %d\n",
173		itd->index[0], itd->index[1], itd->index[2],
174		itd->index[3], itd->index[4], itd->index[5],
175		itd->index[6], itd->index[7]);
176}
177
178static void __maybe_unused
179dbg_sitd (const char *label, struct ehci_hcd *ehci, struct ehci_sitd *sitd)
180{
181	ehci_dbg (ehci, "%s [%d] sitd %p, next %08x, urb %p\n",
182		label, sitd->frame, sitd, hc32_to_cpu(ehci, sitd->hw_next),
183		sitd->urb);
184	ehci_dbg (ehci,
185		"  addr %08x sched %04x result %08x buf %08x %08x\n",
186		hc32_to_cpu(ehci, sitd->hw_fullspeed_ep),
187		hc32_to_cpu(ehci, sitd->hw_uframe),
188		hc32_to_cpu(ehci, sitd->hw_results),
189		hc32_to_cpu(ehci, sitd->hw_buf[0]),
190		hc32_to_cpu(ehci, sitd->hw_buf[1]));
191}
192
193static int __maybe_unused
194dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
195{
196	return scnprintf (buf, len,
197		"%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s%s",
198		label, label [0] ? " " : "", status,
199		(status & STS_PPCE_MASK) ? " PPCE" : "",
200		(status & STS_ASS) ? " Async" : "",
201		(status & STS_PSS) ? " Periodic" : "",
202		(status & STS_RECL) ? " Recl" : "",
203		(status & STS_HALT) ? " Halt" : "",
204		(status & STS_IAA) ? " IAA" : "",
205		(status & STS_FATAL) ? " FATAL" : "",
206		(status & STS_FLR) ? " FLR" : "",
207		(status & STS_PCD) ? " PCD" : "",
208		(status & STS_ERR) ? " ERR" : "",
209		(status & STS_INT) ? " INT" : ""
210		);
211}
212
213static int __maybe_unused
214dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
215{
216	return scnprintf (buf, len,
217		"%s%sintrenable %02x%s%s%s%s%s%s%s",
218		label, label [0] ? " " : "", enable,
219		(enable & STS_PPCE_MASK) ? " PPCE" : "",
220		(enable & STS_IAA) ? " IAA" : "",
221		(enable & STS_FATAL) ? " FATAL" : "",
222		(enable & STS_FLR) ? " FLR" : "",
223		(enable & STS_PCD) ? " PCD" : "",
224		(enable & STS_ERR) ? " ERR" : "",
225		(enable & STS_INT) ? " INT" : ""
226		);
227}
228
229static const char *const fls_strings [] =
230    { "1024", "512", "256", "??" };
231
232static int
233dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
234{
235	return scnprintf (buf, len,
236		"%s%scommand %07x %s%s%s%s%s%s=%d ithresh=%d%s%s%s%s "
237		"period=%s%s %s",
238		label, label [0] ? " " : "", command,
239		(command & CMD_HIRD) ? " HIRD" : "",
240		(command & CMD_PPCEE) ? " PPCEE" : "",
241		(command & CMD_FSP) ? " FSP" : "",
242		(command & CMD_ASPE) ? " ASPE" : "",
243		(command & CMD_PSPE) ? " PSPE" : "",
244		(command & CMD_PARK) ? " park" : "(park)",
245		CMD_PARK_CNT (command),
246		(command >> 16) & 0x3f,
247		(command & CMD_LRESET) ? " LReset" : "",
248		(command & CMD_IAAD) ? " IAAD" : "",
249		(command & CMD_ASE) ? " Async" : "",
250		(command & CMD_PSE) ? " Periodic" : "",
251		fls_strings [(command >> 2) & 0x3],
252		(command & CMD_RESET) ? " Reset" : "",
253		(command & CMD_RUN) ? "RUN" : "HALT"
254		);
255}
256
257static int
258dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
259{
260	char	*sig;
261
262	/* signaling state */
263	switch (status & (3 << 10)) {
264	case 0 << 10: sig = "se0"; break;
265	case 1 << 10: sig = "k"; break;		/* low speed */
266	case 2 << 10: sig = "j"; break;
267	default: sig = "?"; break;
268	}
269
270	return scnprintf (buf, len,
271		"%s%sport:%d status %06x %d %s%s%s%s%s%s "
272		"sig=%s%s%s%s%s%s%s%s%s%s%s",
273		label, label [0] ? " " : "", port, status,
274		status>>25,/*device address */
275		(status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_ACK ?
276						" ACK" : "",
277		(status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_NYET ?
278						" NYET" : "",
279		(status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_STALL ?
280						" STALL" : "",
281		(status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_ERR ?
282						" ERR" : "",
283		(status & PORT_POWER) ? " POWER" : "",
284		(status & PORT_OWNER) ? " OWNER" : "",
285		sig,
286		(status & PORT_LPM) ? " LPM" : "",
287		(status & PORT_RESET) ? " RESET" : "",
288		(status & PORT_SUSPEND) ? " SUSPEND" : "",
289		(status & PORT_RESUME) ? " RESUME" : "",
290		(status & PORT_OCC) ? " OCC" : "",
291		(status & PORT_OC) ? " OC" : "",
292		(status & PORT_PEC) ? " PEC" : "",
293		(status & PORT_PE) ? " PE" : "",
294		(status & PORT_CSC) ? " CSC" : "",
295		(status & PORT_CONNECT) ? " CONNECT" : "");
296}
297
298#else
299static inline void __maybe_unused
300dbg_qh (char *label, struct ehci_hcd *ehci, struct ehci_qh *qh)
301{}
302
303static inline int __maybe_unused
304dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
305{ return 0; }
306
307static inline int __maybe_unused
308dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
309{ return 0; }
310
311static inline int __maybe_unused
312dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
313{ return 0; }
314
315static inline int __maybe_unused
316dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
317{ return 0; }
318
319#endif	/* DEBUG */
320
321/* functions have the "wrong" filename when they're output... */
322#define dbg_status(ehci, label, status) { \
323	char _buf [80]; \
324	dbg_status_buf (_buf, sizeof _buf, label, status); \
325	ehci_dbg (ehci, "%s\n", _buf); \
326}
327
328#define dbg_cmd(ehci, label, command) { \
329	char _buf [80]; \
330	dbg_command_buf (_buf, sizeof _buf, label, command); \
331	ehci_dbg (ehci, "%s\n", _buf); \
332}
333
334#define dbg_port(ehci, label, port, status) { \
335	char _buf [80]; \
336	dbg_port_buf (_buf, sizeof _buf, label, port, status); \
337	ehci_dbg (ehci, "%s\n", _buf); \
338}
339
340/*-------------------------------------------------------------------------*/
341
342#ifdef STUB_DEBUG_FILES
343
344static inline void create_debug_files (struct ehci_hcd *bus) { }
345static inline void remove_debug_files (struct ehci_hcd *bus) { }
346
347#else
348
349/* troubleshooting help: expose state in debugfs */
350
351static int debug_async_open(struct inode *, struct file *);
352static int debug_periodic_open(struct inode *, struct file *);
353static int debug_registers_open(struct inode *, struct file *);
354static int debug_async_open(struct inode *, struct file *);
355static ssize_t debug_lpm_read(struct file *file, char __user *user_buf,
356				   size_t count, loff_t *ppos);
357static ssize_t debug_lpm_write(struct file *file, const char __user *buffer,
358			      size_t count, loff_t *ppos);
359static int debug_lpm_close(struct inode *inode, struct file *file);
360
361static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
362static int debug_close(struct inode *, struct file *);
363
364static const struct file_operations debug_async_fops = {
365	.owner		= THIS_MODULE,
366	.open		= debug_async_open,
367	.read		= debug_output,
368	.release	= debug_close,
369	.llseek		= default_llseek,
370};
371static const struct file_operations debug_periodic_fops = {
372	.owner		= THIS_MODULE,
373	.open		= debug_periodic_open,
374	.read		= debug_output,
375	.release	= debug_close,
376	.llseek		= default_llseek,
377};
378static const struct file_operations debug_registers_fops = {
379	.owner		= THIS_MODULE,
380	.open		= debug_registers_open,
381	.read		= debug_output,
382	.release	= debug_close,
383	.llseek		= default_llseek,
384};
385static const struct file_operations debug_lpm_fops = {
386	.owner		= THIS_MODULE,
387	.open		= simple_open,
388	.read		= debug_lpm_read,
389	.write		= debug_lpm_write,
390	.release	= debug_lpm_close,
391	.llseek		= noop_llseek,
392};
393
394static struct dentry *ehci_debug_root;
395
396struct debug_buffer {
397	ssize_t (*fill_func)(struct debug_buffer *);	/* fill method */
398	struct usb_bus *bus;
399	struct mutex mutex;	/* protect filling of buffer */
400	size_t count;		/* number of characters filled into buffer */
401	char *output_buf;
402	size_t alloc_size;
403};
404
405#define speed_char(info1) ({ char tmp; \
406		switch (info1 & (3 << 12)) { \
407		case 0 << 12: tmp = 'f'; break; \
408		case 1 << 12: tmp = 'l'; break; \
409		case 2 << 12: tmp = 'h'; break; \
410		default: tmp = '?'; break; \
411		}; tmp; })
412
413static inline char token_mark(struct ehci_hcd *ehci, __hc32 token)
414{
415	__u32 v = hc32_to_cpu(ehci, token);
416
417	if (v & QTD_STS_ACTIVE)
418		return '*';
419	if (v & QTD_STS_HALT)
420		return '-';
421	if (!IS_SHORT_READ (v))
422		return ' ';
423	/* tries to advance through hw_alt_next */
424	return '/';
425}
426
427static void qh_lines (
428	struct ehci_hcd *ehci,
429	struct ehci_qh *qh,
430	char **nextp,
431	unsigned *sizep
432)
433{
434	u32			scratch;
435	u32			hw_curr;
436	struct list_head	*entry;
437	struct ehci_qtd		*td;
438	unsigned		temp;
439	unsigned		size = *sizep;
440	char			*next = *nextp;
441	char			mark;
442	__le32			list_end = EHCI_LIST_END(ehci);
443	struct ehci_qh_hw	*hw = qh->hw;
444
445	if (hw->hw_qtd_next == list_end)	/* NEC does this */
446		mark = '@';
447	else
448		mark = token_mark(ehci, hw->hw_token);
449	if (mark == '/') {	/* qh_alt_next controls qh advance? */
450		if ((hw->hw_alt_next & QTD_MASK(ehci))
451				== ehci->async->hw->hw_alt_next)
452			mark = '#';	/* blocked */
453		else if (hw->hw_alt_next == list_end)
454			mark = '.';	/* use hw_qtd_next */
455		/* else alt_next points to some other qtd */
456	}
457	scratch = hc32_to_cpup(ehci, &hw->hw_info1);
458	hw_curr = (mark == '*') ? hc32_to_cpup(ehci, &hw->hw_current) : 0;
459	temp = scnprintf (next, size,
460			"qh/%p dev%d %cs ep%d %08x %08x (%08x%c %s nak%d)",
461			qh, scratch & 0x007f,
462			speed_char (scratch),
463			(scratch >> 8) & 0x000f,
464			scratch, hc32_to_cpup(ehci, &hw->hw_info2),
465			hc32_to_cpup(ehci, &hw->hw_token), mark,
466			(cpu_to_hc32(ehci, QTD_TOGGLE) & hw->hw_token)
467				? "data1" : "data0",
468			(hc32_to_cpup(ehci, &hw->hw_alt_next) >> 1) & 0x0f);
469	size -= temp;
470	next += temp;
471
472	/* hc may be modifying the list as we read it ... */
473	list_for_each (entry, &qh->qtd_list) {
474		td = list_entry (entry, struct ehci_qtd, qtd_list);
475		scratch = hc32_to_cpup(ehci, &td->hw_token);
476		mark = ' ';
477		if (hw_curr == td->qtd_dma)
478			mark = '*';
479		else if (hw->hw_qtd_next == cpu_to_hc32(ehci, td->qtd_dma))
480			mark = '+';
481		else if (QTD_LENGTH (scratch)) {
482			if (td->hw_alt_next == ehci->async->hw->hw_alt_next)
483				mark = '#';
484			else if (td->hw_alt_next != list_end)
485				mark = '/';
486		}
487		temp = snprintf (next, size,
488				"\n\t%p%c%s len=%d %08x urb %p",
489				td, mark, ({ char *tmp;
490				 switch ((scratch>>8)&0x03) {
491				 case 0: tmp = "out"; break;
492				 case 1: tmp = "in"; break;
493				 case 2: tmp = "setup"; break;
494				 default: tmp = "?"; break;
495				 } tmp;}),
496				(scratch >> 16) & 0x7fff,
497				scratch,
498				td->urb);
499		if (size < temp)
500			temp = size;
501		size -= temp;
502		next += temp;
503		if (temp == size)
504			goto done;
505	}
506
507	temp = snprintf (next, size, "\n");
508	if (size < temp)
509		temp = size;
510	size -= temp;
511	next += temp;
512
513done:
514	*sizep = size;
515	*nextp = next;
516}
517
518static ssize_t fill_async_buffer(struct debug_buffer *buf)
519{
520	struct usb_hcd		*hcd;
521	struct ehci_hcd		*ehci;
522	unsigned long		flags;
523	unsigned		temp, size;
524	char			*next;
525	struct ehci_qh		*qh;
526
527	hcd = bus_to_hcd(buf->bus);
528	ehci = hcd_to_ehci (hcd);
529	next = buf->output_buf;
530	size = buf->alloc_size;
531
532	*next = 0;
533
534	/* dumps a snapshot of the async schedule.
535	 * usually empty except for long-term bulk reads, or head.
536	 * one QH per line, and TDs we know about
537	 */
538	spin_lock_irqsave (&ehci->lock, flags);
539	for (qh = ehci->async->qh_next.qh; size > 0 && qh; qh = qh->qh_next.qh)
540		qh_lines (ehci, qh, &next, &size);
541	if (ehci->reclaim && size > 0) {
542		temp = scnprintf (next, size, "\nreclaim =\n");
543		size -= temp;
544		next += temp;
545
546		for (qh = ehci->reclaim; size > 0 && qh; qh = qh->reclaim)
547			qh_lines (ehci, qh, &next, &size);
548	}
549	spin_unlock_irqrestore (&ehci->lock, flags);
550
551	return strlen(buf->output_buf);
552}
553
554#define DBG_SCHED_LIMIT 64
555static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
556{
557	struct usb_hcd		*hcd;
558	struct ehci_hcd		*ehci;
559	unsigned long		flags;
560	union ehci_shadow	p, *seen;
561	unsigned		temp, size, seen_count;
562	char			*next;
563	unsigned		i;
564	__hc32			tag;
565
566	if (!(seen = kmalloc (DBG_SCHED_LIMIT * sizeof *seen, GFP_ATOMIC)))
567		return 0;
568	seen_count = 0;
569
570	hcd = bus_to_hcd(buf->bus);
571	ehci = hcd_to_ehci (hcd);
572	next = buf->output_buf;
573	size = buf->alloc_size;
574
575	temp = scnprintf (next, size, "size = %d\n", ehci->periodic_size);
576	size -= temp;
577	next += temp;
578
579	/* dump a snapshot of the periodic schedule.
580	 * iso changes, interrupt usually doesn't.
581	 */
582	spin_lock_irqsave (&ehci->lock, flags);
583	for (i = 0; i < ehci->periodic_size; i++) {
584		p = ehci->pshadow [i];
585		if (likely (!p.ptr))
586			continue;
587		tag = Q_NEXT_TYPE(ehci, ehci->periodic [i]);
588
589		temp = scnprintf (next, size, "%4d: ", i);
590		size -= temp;
591		next += temp;
592
593		do {
594			struct ehci_qh_hw *hw;
595
596			switch (hc32_to_cpu(ehci, tag)) {
597			case Q_TYPE_QH:
598				hw = p.qh->hw;
599				temp = scnprintf (next, size, " qh%d-%04x/%p",
600						p.qh->period,
601						hc32_to_cpup(ehci,
602							&hw->hw_info2)
603							/* uframe masks */
604							& (QH_CMASK | QH_SMASK),
605						p.qh);
606				size -= temp;
607				next += temp;
608				/* don't repeat what follows this qh */
609				for (temp = 0; temp < seen_count; temp++) {
610					if (seen [temp].ptr != p.ptr)
611						continue;
612					if (p.qh->qh_next.ptr) {
613						temp = scnprintf (next, size,
614							" ...");
615						size -= temp;
616						next += temp;
617					}
618					break;
619				}
620				/* show more info the first time around */
621				if (temp == seen_count) {
622					u32	scratch = hc32_to_cpup(ehci,
623							&hw->hw_info1);
624					struct ehci_qtd	*qtd;
625					char		*type = "";
626
627					/* count tds, get ep direction */
628					temp = 0;
629					list_for_each_entry (qtd,
630							&p.qh->qtd_list,
631							qtd_list) {
632						temp++;
633						switch (0x03 & (hc32_to_cpu(
634							ehci,
635							qtd->hw_token) >> 8)) {
636						case 0: type = "out"; continue;
637						case 1: type = "in"; continue;
638						}
639					}
640
641					temp = scnprintf (next, size,
642						" (%c%d ep%d%s "
643						"[%d/%d] q%d p%d)",
644						speed_char (scratch),
645						scratch & 0x007f,
646						(scratch >> 8) & 0x000f, type,
647						p.qh->usecs, p.qh->c_usecs,
648						temp,
649						0x7ff & (scratch >> 16));
650
651					if (seen_count < DBG_SCHED_LIMIT)
652						seen [seen_count++].qh = p.qh;
653				} else
654					temp = 0;
655				if (p.qh) {
656					tag = Q_NEXT_TYPE(ehci, hw->hw_next);
657					p = p.qh->qh_next;
658				}
659				break;
660			case Q_TYPE_FSTN:
661				temp = scnprintf (next, size,
662					" fstn-%8x/%p", p.fstn->hw_prev,
663					p.fstn);
664				tag = Q_NEXT_TYPE(ehci, p.fstn->hw_next);
665				p = p.fstn->fstn_next;
666				break;
667			case Q_TYPE_ITD:
668				temp = scnprintf (next, size,
669					" itd/%p", p.itd);
670				tag = Q_NEXT_TYPE(ehci, p.itd->hw_next);
671				p = p.itd->itd_next;
672				break;
673			case Q_TYPE_SITD:
674				temp = scnprintf (next, size,
675					" sitd%d-%04x/%p",
676					p.sitd->stream->interval,
677					hc32_to_cpup(ehci, &p.sitd->hw_uframe)
678						& 0x0000ffff,
679					p.sitd);
680				tag = Q_NEXT_TYPE(ehci, p.sitd->hw_next);
681				p = p.sitd->sitd_next;
682				break;
683			}
684			size -= temp;
685			next += temp;
686		} while (p.ptr);
687
688		temp = scnprintf (next, size, "\n");
689		size -= temp;
690		next += temp;
691	}
692	spin_unlock_irqrestore (&ehci->lock, flags);
693	kfree (seen);
694
695	return buf->alloc_size - size;
696}
697#undef DBG_SCHED_LIMIT
698
699static const char *rh_state_string(struct ehci_hcd *ehci)
700{
701	switch (ehci->rh_state) {
702	case EHCI_RH_HALTED:
703		return "halted";
704	case EHCI_RH_SUSPENDED:
705		return "suspended";
706	case EHCI_RH_RUNNING:
707		return "running";
708	}
709	return "?";
710}
711
712static ssize_t fill_registers_buffer(struct debug_buffer *buf)
713{
714	struct usb_hcd		*hcd;
715	struct ehci_hcd		*ehci;
716	unsigned long		flags;
717	unsigned		temp, size, i;
718	char			*next, scratch [80];
719	static char		fmt [] = "%*s\n";
720	static char		label [] = "";
721
722	hcd = bus_to_hcd(buf->bus);
723	ehci = hcd_to_ehci (hcd);
724	next = buf->output_buf;
725	size = buf->alloc_size;
726
727	spin_lock_irqsave (&ehci->lock, flags);
728
729	if (!HCD_HW_ACCESSIBLE(hcd)) {
730		size = scnprintf (next, size,
731			"bus %s, device %s\n"
732			"%s\n"
733			"SUSPENDED (no register access)\n",
734			hcd->self.controller->bus->name,
735			dev_name(hcd->self.controller),
736			hcd->product_desc);
737		goto done;
738	}
739
740	/* Capability Registers */
741	i = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
742	temp = scnprintf (next, size,
743		"bus %s, device %s\n"
744		"%s\n"
745		"EHCI %x.%02x, rh state %s\n",
746		hcd->self.controller->bus->name,
747		dev_name(hcd->self.controller),
748		hcd->product_desc,
749		i >> 8, i & 0x0ff, rh_state_string(ehci));
750	size -= temp;
751	next += temp;
752
753#ifdef	CONFIG_PCI
754	/* EHCI 0.96 and later may have "extended capabilities" */
755	if (hcd->self.controller->bus == &pci_bus_type) {
756		struct pci_dev	*pdev;
757		u32		offset, cap, cap2;
758		unsigned	count = 256/4;
759
760		pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller);
761		offset = HCC_EXT_CAPS(ehci_readl(ehci,
762				&ehci->caps->hcc_params));
763		while (offset && count--) {
764			pci_read_config_dword (pdev, offset, &cap);
765			switch (cap & 0xff) {
766			case 1:
767				temp = scnprintf (next, size,
768					"ownership %08x%s%s\n", cap,
769					(cap & (1 << 24)) ? " linux" : "",
770					(cap & (1 << 16)) ? " firmware" : "");
771				size -= temp;
772				next += temp;
773
774				offset += 4;
775				pci_read_config_dword (pdev, offset, &cap2);
776				temp = scnprintf (next, size,
777					"SMI sts/enable 0x%08x\n", cap2);
778				size -= temp;
779				next += temp;
780				break;
781			case 0:		/* illegal reserved capability */
782				cap = 0;
783				/* FALLTHROUGH */
784			default:		/* unknown */
785				break;
786			}
787			temp = (cap >> 8) & 0xff;
788		}
789	}
790#endif
791
792	// FIXME interpret both types of params
793	i = ehci_readl(ehci, &ehci->caps->hcs_params);
794	temp = scnprintf (next, size, "structural params 0x%08x\n", i);
795	size -= temp;
796	next += temp;
797
798	i = ehci_readl(ehci, &ehci->caps->hcc_params);
799	temp = scnprintf (next, size, "capability params 0x%08x\n", i);
800	size -= temp;
801	next += temp;
802
803	/* Operational Registers */
804	temp = dbg_status_buf (scratch, sizeof scratch, label,
805			ehci_readl(ehci, &ehci->regs->status));
806	temp = scnprintf (next, size, fmt, temp, scratch);
807	size -= temp;
808	next += temp;
809
810	temp = dbg_command_buf (scratch, sizeof scratch, label,
811			ehci_readl(ehci, &ehci->regs->command));
812	temp = scnprintf (next, size, fmt, temp, scratch);
813	size -= temp;
814	next += temp;
815
816	temp = dbg_intr_buf (scratch, sizeof scratch, label,
817			ehci_readl(ehci, &ehci->regs->intr_enable));
818	temp = scnprintf (next, size, fmt, temp, scratch);
819	size -= temp;
820	next += temp;
821
822	temp = scnprintf (next, size, "uframe %04x\n",
823			ehci_read_frame_index(ehci));
824	size -= temp;
825	next += temp;
826
827	for (i = 1; i <= HCS_N_PORTS (ehci->hcs_params); i++) {
828		temp = dbg_port_buf (scratch, sizeof scratch, label, i,
829				ehci_readl(ehci,
830					&ehci->regs->port_status[i - 1]));
831		temp = scnprintf (next, size, fmt, temp, scratch);
832		size -= temp;
833		next += temp;
834		if (i == HCS_DEBUG_PORT(ehci->hcs_params) && ehci->debug) {
835			temp = scnprintf (next, size,
836					"    debug control %08x\n",
837					ehci_readl(ehci,
838						&ehci->debug->control));
839			size -= temp;
840			next += temp;
841		}
842	}
843
844	if (ehci->reclaim) {
845		temp = scnprintf(next, size, "reclaim qh %p\n", ehci->reclaim);
846		size -= temp;
847		next += temp;
848	}
849
850#ifdef EHCI_STATS
851	temp = scnprintf (next, size,
852		"irq normal %ld err %ld reclaim %ld (lost %ld)\n",
853		ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
854		ehci->stats.lost_iaa);
855	size -= temp;
856	next += temp;
857
858	temp = scnprintf (next, size, "complete %ld unlink %ld\n",
859		ehci->stats.complete, ehci->stats.unlink);
860	size -= temp;
861	next += temp;
862#endif
863
864done:
865	spin_unlock_irqrestore (&ehci->lock, flags);
866
867	return buf->alloc_size - size;
868}
869
870static struct debug_buffer *alloc_buffer(struct usb_bus *bus,
871				ssize_t (*fill_func)(struct debug_buffer *))
872{
873	struct debug_buffer *buf;
874
875	buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
876
877	if (buf) {
878		buf->bus = bus;
879		buf->fill_func = fill_func;
880		mutex_init(&buf->mutex);
881		buf->alloc_size = PAGE_SIZE;
882	}
883
884	return buf;
885}
886
887static int fill_buffer(struct debug_buffer *buf)
888{
889	int ret = 0;
890
891	if (!buf->output_buf)
892		buf->output_buf = vmalloc(buf->alloc_size);
893
894	if (!buf->output_buf) {
895		ret = -ENOMEM;
896		goto out;
897	}
898
899	ret = buf->fill_func(buf);
900
901	if (ret >= 0) {
902		buf->count = ret;
903		ret = 0;
904	}
905
906out:
907	return ret;
908}
909
910static ssize_t debug_output(struct file *file, char __user *user_buf,
911			    size_t len, loff_t *offset)
912{
913	struct debug_buffer *buf = file->private_data;
914	int ret = 0;
915
916	mutex_lock(&buf->mutex);
917	if (buf->count == 0) {
918		ret = fill_buffer(buf);
919		if (ret != 0) {
920			mutex_unlock(&buf->mutex);
921			goto out;
922		}
923	}
924	mutex_unlock(&buf->mutex);
925
926	ret = simple_read_from_buffer(user_buf, len, offset,
927				      buf->output_buf, buf->count);
928
929out:
930	return ret;
931
932}
933
934static int debug_close(struct inode *inode, struct file *file)
935{
936	struct debug_buffer *buf = file->private_data;
937
938	if (buf) {
939		vfree(buf->output_buf);
940		kfree(buf);
941	}
942
943	return 0;
944}
945static int debug_async_open(struct inode *inode, struct file *file)
946{
947	file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
948
949	return file->private_data ? 0 : -ENOMEM;
950}
951
952static int debug_periodic_open(struct inode *inode, struct file *file)
953{
954	struct debug_buffer *buf;
955	buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
956	if (!buf)
957		return -ENOMEM;
958
959	buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
960	file->private_data = buf;
961	return 0;
962}
963
964static int debug_registers_open(struct inode *inode, struct file *file)
965{
966	file->private_data = alloc_buffer(inode->i_private,
967					  fill_registers_buffer);
968
969	return file->private_data ? 0 : -ENOMEM;
970}
971
972static int debug_lpm_close(struct inode *inode, struct file *file)
973{
974	return 0;
975}
976
977static ssize_t debug_lpm_read(struct file *file, char __user *user_buf,
978				   size_t count, loff_t *ppos)
979{
980	/* TODO: show lpm stats */
981	return 0;
982}
983
984static ssize_t debug_lpm_write(struct file *file, const char __user *user_buf,
985			      size_t count, loff_t *ppos)
986{
987	struct usb_hcd		*hcd;
988	struct ehci_hcd		*ehci;
989	char buf[50];
990	size_t len;
991	u32 temp;
992	unsigned long port;
993	u32 __iomem	*portsc ;
994	u32 params;
995
996	hcd = bus_to_hcd(file->private_data);
997	ehci = hcd_to_ehci(hcd);
998
999	len = min(count, sizeof(buf) - 1);
1000	if (copy_from_user(buf, user_buf, len))
1001		return -EFAULT;
1002	buf[len] = '\0';
1003	if (len > 0 && buf[len - 1] == '\n')
1004		buf[len - 1] = '\0';
1005
1006	if (strncmp(buf, "enable", 5) == 0) {
1007		if (strict_strtoul(buf + 7, 10, &port))
1008			return -EINVAL;
1009		params = ehci_readl(ehci, &ehci->caps->hcs_params);
1010		if (port > HCS_N_PORTS(params)) {
1011			ehci_dbg(ehci, "ERR: LPM on bad port %lu\n", port);
1012			return -ENODEV;
1013		}
1014		portsc = &ehci->regs->port_status[port-1];
1015		temp = ehci_readl(ehci, portsc);
1016		if (!(temp & PORT_DEV_ADDR)) {
1017			ehci_dbg(ehci, "LPM: no device attached\n");
1018			return -ENODEV;
1019		}
1020		temp |= PORT_LPM;
1021		ehci_writel(ehci, temp, portsc);
1022		printk(KERN_INFO "force enable LPM for port %lu\n", port);
1023	} else if (strncmp(buf, "hird=", 5) == 0) {
1024		unsigned long hird;
1025		if (strict_strtoul(buf + 5, 16, &hird))
1026			return -EINVAL;
1027		printk(KERN_INFO "setting hird %s %lu\n", buf + 6, hird);
1028		temp = ehci_readl(ehci, &ehci->regs->command);
1029		temp &= ~CMD_HIRD;
1030		temp |= hird << 24;
1031		ehci_writel(ehci, temp, &ehci->regs->command);
1032	} else if (strncmp(buf, "disable", 7) == 0) {
1033		if (strict_strtoul(buf + 8, 10, &port))
1034			return -EINVAL;
1035		params = ehci_readl(ehci, &ehci->caps->hcs_params);
1036		if (port > HCS_N_PORTS(params)) {
1037			ehci_dbg(ehci, "ERR: LPM off bad port %lu\n", port);
1038			return -ENODEV;
1039		}
1040		portsc = &ehci->regs->port_status[port-1];
1041		temp = ehci_readl(ehci, portsc);
1042		if (!(temp & PORT_DEV_ADDR)) {
1043			ehci_dbg(ehci, "ERR: no device attached\n");
1044			return -ENODEV;
1045		}
1046		temp &= ~PORT_LPM;
1047		ehci_writel(ehci, temp, portsc);
1048		printk(KERN_INFO "disabled LPM for port %lu\n", port);
1049	} else
1050		return -EOPNOTSUPP;
1051	return count;
1052}
1053
1054static inline void create_debug_files (struct ehci_hcd *ehci)
1055{
1056	struct usb_bus *bus = &ehci_to_hcd(ehci)->self;
1057
1058	ehci->debug_dir = debugfs_create_dir(bus->bus_name, ehci_debug_root);
1059	if (!ehci->debug_dir)
1060		return;
1061
1062	if (!debugfs_create_file("async", S_IRUGO, ehci->debug_dir, bus,
1063						&debug_async_fops))
1064		goto file_error;
1065
1066	if (!debugfs_create_file("periodic", S_IRUGO, ehci->debug_dir, bus,
1067						&debug_periodic_fops))
1068		goto file_error;
1069
1070	if (!debugfs_create_file("registers", S_IRUGO, ehci->debug_dir, bus,
1071						    &debug_registers_fops))
1072		goto file_error;
1073
1074	if (!debugfs_create_file("lpm", S_IRUGO|S_IWUSR, ehci->debug_dir, bus,
1075						    &debug_lpm_fops))
1076		goto file_error;
1077
1078	return;
1079
1080file_error:
1081	debugfs_remove_recursive(ehci->debug_dir);
1082}
1083
1084static inline void remove_debug_files (struct ehci_hcd *ehci)
1085{
1086	debugfs_remove_recursive(ehci->debug_dir);
1087}
1088
1089#endif /* STUB_DEBUG_FILES */
1090