1/*
2 * UHCI-specific debugging code. Invaluable when something
3 * goes wrong, but don't get in my face.
4 *
5 * Kernel visible pointers are surrounded in []s and bus
6 * visible pointers are surrounded in ()s
7 *
8 * (C) Copyright 1999 Linus Torvalds
9 * (C) Copyright 1999-2001 Johannes Erdfelt
10 */
11
12#include <linux/slab.h>
13#include <linux/kernel.h>
14#include <linux/debugfs.h>
15#include <asm/io.h>
16
17#include "uhci-hcd.h"
18
19#define EXTRA_SPACE	1024
20
21static struct dentry *uhci_debugfs_root;
22
23#ifdef CONFIG_DYNAMIC_DEBUG
24
25/* Handle REALLY large printks so we don't overflow buffers */
26static void lprintk(char *buf)
27{
28	char *p;
29
30	/* Just write one line at a time */
31	while (buf) {
32		p = strchr(buf, '\n');
33		if (p)
34			*p = 0;
35		printk(KERN_DEBUG "%s\n", buf);
36		buf = p;
37		if (buf)
38			buf++;
39	}
40}
41
42static int uhci_show_td(struct uhci_hcd *uhci, struct uhci_td *td, char *buf,
43			int len, int space)
44{
45	char *out = buf;
46	char *spid;
47	u32 status, token;
48
49	status = td_status(uhci, td);
50	out += sprintf(out, "%*s[%p] link (%08x) ", space, "", td,
51		hc32_to_cpu(uhci, td->link));
52	out += sprintf(out, "e%d %s%s%s%s%s%s%s%s%s%sLength=%x ",
53		((status >> 27) & 3),
54		(status & TD_CTRL_SPD) ?      "SPD " : "",
55		(status & TD_CTRL_LS) ?       "LS " : "",
56		(status & TD_CTRL_IOC) ?      "IOC " : "",
57		(status & TD_CTRL_ACTIVE) ?   "Active " : "",
58		(status & TD_CTRL_STALLED) ?  "Stalled " : "",
59		(status & TD_CTRL_DBUFERR) ?  "DataBufErr " : "",
60		(status & TD_CTRL_BABBLE) ?   "Babble " : "",
61		(status & TD_CTRL_NAK) ?      "NAK " : "",
62		(status & TD_CTRL_CRCTIMEO) ? "CRC/Timeo " : "",
63		(status & TD_CTRL_BITSTUFF) ? "BitStuff " : "",
64		status & 0x7ff);
65	if (out - buf > len)
66		goto done;
67
68	token = td_token(uhci, td);
69	switch (uhci_packetid(token)) {
70		case USB_PID_SETUP:
71			spid = "SETUP";
72			break;
73		case USB_PID_OUT:
74			spid = "OUT";
75			break;
76		case USB_PID_IN:
77			spid = "IN";
78			break;
79		default:
80			spid = "?";
81			break;
82	}
83
84	out += sprintf(out, "MaxLen=%x DT%d EndPt=%x Dev=%x, PID=%x(%s) ",
85		token >> 21,
86		((token >> 19) & 1),
87		(token >> 15) & 15,
88		(token >> 8) & 127,
89		(token & 0xff),
90		spid);
91	out += sprintf(out, "(buf=%08x)\n", hc32_to_cpu(uhci, td->buffer));
92
93done:
94	if (out - buf > len)
95		out += sprintf(out, " ...\n");
96	return out - buf;
97}
98
99static int uhci_show_urbp(struct uhci_hcd *uhci, struct urb_priv *urbp,
100			char *buf, int len, int space)
101{
102	char *out = buf;
103	struct uhci_td *td;
104	int i, nactive, ninactive;
105	char *ptype;
106
107
108	out += sprintf(out, "urb_priv [%p] ", urbp);
109	out += sprintf(out, "urb [%p] ", urbp->urb);
110	out += sprintf(out, "qh [%p] ", urbp->qh);
111	out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
112	out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe),
113			(usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
114	if (out - buf > len)
115		goto done;
116
117	switch (usb_pipetype(urbp->urb->pipe)) {
118	case PIPE_ISOCHRONOUS: ptype = "ISO"; break;
119	case PIPE_INTERRUPT: ptype = "INT"; break;
120	case PIPE_BULK: ptype = "BLK"; break;
121	default:
122	case PIPE_CONTROL: ptype = "CTL"; break;
123	}
124
125	out += sprintf(out, "%s%s", ptype, (urbp->fsbr ? " FSBR" : ""));
126	out += sprintf(out, " Actlen=%d%s", urbp->urb->actual_length,
127			(urbp->qh->type == USB_ENDPOINT_XFER_CONTROL ?
128				"-8" : ""));
129
130	if (urbp->urb->unlinked)
131		out += sprintf(out, " Unlinked=%d", urbp->urb->unlinked);
132	out += sprintf(out, "\n");
133
134	if (out - buf > len)
135		goto done;
136
137	i = nactive = ninactive = 0;
138	list_for_each_entry(td, &urbp->td_list, list) {
139		if (urbp->qh->type != USB_ENDPOINT_XFER_ISOC &&
140				(++i <= 10 || debug > 2)) {
141			out += sprintf(out, "%*s%d: ", space + 2, "", i);
142			out += uhci_show_td(uhci, td, out,
143					len - (out - buf), 0);
144			if (out - buf > len)
145				goto tail;
146		} else {
147			if (td_status(uhci, td) & TD_CTRL_ACTIVE)
148				++nactive;
149			else
150				++ninactive;
151		}
152	}
153	if (nactive + ninactive > 0)
154		out += sprintf(out,
155				"%*s[skipped %d inactive and %d active TDs]\n",
156				space, "", ninactive, nactive);
157done:
158	if (out - buf > len)
159		out += sprintf(out, " ...\n");
160tail:
161	return out - buf;
162}
163
164static int uhci_show_qh(struct uhci_hcd *uhci,
165		struct uhci_qh *qh, char *buf, int len, int space)
166{
167	char *out = buf;
168	int i, nurbs;
169	__hc32 element = qh_element(qh);
170	char *qtype;
171
172	switch (qh->type) {
173	case USB_ENDPOINT_XFER_ISOC: qtype = "ISO"; break;
174	case USB_ENDPOINT_XFER_INT: qtype = "INT"; break;
175	case USB_ENDPOINT_XFER_BULK: qtype = "BLK"; break;
176	case USB_ENDPOINT_XFER_CONTROL: qtype = "CTL"; break;
177	default: qtype = "Skel" ; break;
178	}
179
180	out += sprintf(out, "%*s[%p] %s QH link (%08x) element (%08x)\n",
181			space, "", qh, qtype,
182			hc32_to_cpu(uhci, qh->link),
183			hc32_to_cpu(uhci, element));
184	if (qh->type == USB_ENDPOINT_XFER_ISOC)
185		out += sprintf(out,
186				"%*s    period %d phase %d load %d us, frame %x desc [%p]\n",
187				space, "", qh->period, qh->phase, qh->load,
188				qh->iso_frame, qh->iso_packet_desc);
189	else if (qh->type == USB_ENDPOINT_XFER_INT)
190		out += sprintf(out, "%*s    period %d phase %d load %d us\n",
191				space, "", qh->period, qh->phase, qh->load);
192	if (out - buf > len)
193		goto done;
194
195	if (element & UHCI_PTR_QH(uhci))
196		out += sprintf(out, "%*s  Element points to QH (bug?)\n", space, "");
197
198	if (element & UHCI_PTR_DEPTH(uhci))
199		out += sprintf(out, "%*s  Depth traverse\n", space, "");
200
201	if (element & cpu_to_hc32(uhci, 8))
202		out += sprintf(out, "%*s  Bit 3 set (bug?)\n", space, "");
203
204	if (!(element & ~(UHCI_PTR_QH(uhci) | UHCI_PTR_DEPTH(uhci))))
205		out += sprintf(out, "%*s  Element is NULL (bug?)\n", space, "");
206
207	if (out - buf > len)
208		goto done;
209
210	if (list_empty(&qh->queue)) {
211		out += sprintf(out, "%*s  queue is empty\n", space, "");
212		if (qh == uhci->skel_async_qh) {
213			out += uhci_show_td(uhci, uhci->term_td, out,
214					len - (out - buf), 0);
215			if (out - buf > len)
216				goto tail;
217		}
218	} else {
219		struct urb_priv *urbp = list_entry(qh->queue.next,
220				struct urb_priv, node);
221		struct uhci_td *td = list_entry(urbp->td_list.next,
222				struct uhci_td, list);
223
224		if (element != LINK_TO_TD(uhci, td))
225			out += sprintf(out, "%*s Element != First TD\n",
226					space, "");
227		i = nurbs = 0;
228		list_for_each_entry(urbp, &qh->queue, node) {
229			if (++i <= 10) {
230				out += uhci_show_urbp(uhci, urbp, out,
231						len - (out - buf), space + 2);
232				if (out - buf > len)
233					goto tail;
234			}
235			else
236				++nurbs;
237		}
238		if (nurbs > 0)
239			out += sprintf(out, "%*s Skipped %d URBs\n",
240					space, "", nurbs);
241	}
242
243	if (out - buf > len)
244		goto done;
245
246	if (qh->dummy_td) {
247		out += sprintf(out, "%*s  Dummy TD\n", space, "");
248		out += uhci_show_td(uhci, qh->dummy_td, out,
249				len - (out - buf), 0);
250		if (out - buf > len)
251			goto tail;
252	}
253
254done:
255	if (out - buf > len)
256		out += sprintf(out, " ...\n");
257tail:
258	return out - buf;
259}
260
261static int uhci_show_sc(int port, unsigned short status, char *buf)
262{
263	return sprintf(buf, "  stat%d     =     %04x  %s%s%s%s%s%s%s%s%s%s\n",
264		port,
265		status,
266		(status & USBPORTSC_SUSP) ?	" Suspend" : "",
267		(status & USBPORTSC_OCC) ?	" OverCurrentChange" : "",
268		(status & USBPORTSC_OC) ?	" OverCurrent" : "",
269		(status & USBPORTSC_PR) ?	" Reset" : "",
270		(status & USBPORTSC_LSDA) ?	" LowSpeed" : "",
271		(status & USBPORTSC_RD) ?	" ResumeDetect" : "",
272		(status & USBPORTSC_PEC) ?	" EnableChange" : "",
273		(status & USBPORTSC_PE) ?	" Enabled" : "",
274		(status & USBPORTSC_CSC) ?	" ConnectChange" : "",
275		(status & USBPORTSC_CCS) ?	" Connected" : "");
276}
277
278static int uhci_show_root_hub_state(struct uhci_hcd *uhci, char *buf)
279{
280	char *rh_state;
281
282	switch (uhci->rh_state) {
283	    case UHCI_RH_RESET:
284		rh_state = "reset";		break;
285	    case UHCI_RH_SUSPENDED:
286		rh_state = "suspended";		break;
287	    case UHCI_RH_AUTO_STOPPED:
288		rh_state = "auto-stopped";	break;
289	    case UHCI_RH_RESUMING:
290		rh_state = "resuming";		break;
291	    case UHCI_RH_SUSPENDING:
292		rh_state = "suspending";	break;
293	    case UHCI_RH_RUNNING:
294		rh_state = "running";		break;
295	    case UHCI_RH_RUNNING_NODEVS:
296		rh_state = "running, no devs";	break;
297	    default:
298		rh_state = "?";			break;
299	}
300	return sprintf(buf, "Root-hub state: %s   FSBR: %d\n",
301			rh_state, uhci->fsbr_is_on);
302}
303
304static int uhci_show_status(struct uhci_hcd *uhci, char *buf, int len)
305{
306	char *out = buf;
307	unsigned short usbcmd, usbstat, usbint, usbfrnum;
308	unsigned int flbaseadd;
309	unsigned char sof;
310	unsigned short portsc1, portsc2;
311
312
313	usbcmd    = uhci_readw(uhci, USBCMD);
314	usbstat   = uhci_readw(uhci, USBSTS);
315	usbint    = uhci_readw(uhci, USBINTR);
316	usbfrnum  = uhci_readw(uhci, USBFRNUM);
317	flbaseadd = uhci_readl(uhci, USBFLBASEADD);
318	sof       = uhci_readb(uhci, USBSOF);
319	portsc1   = uhci_readw(uhci, USBPORTSC1);
320	portsc2   = uhci_readw(uhci, USBPORTSC2);
321
322	out += sprintf(out, "  usbcmd    =     %04x   %s%s%s%s%s%s%s%s\n",
323		usbcmd,
324		(usbcmd & USBCMD_MAXP) ?    "Maxp64 " : "Maxp32 ",
325		(usbcmd & USBCMD_CF) ?      "CF " : "",
326		(usbcmd & USBCMD_SWDBG) ?   "SWDBG " : "",
327		(usbcmd & USBCMD_FGR) ?     "FGR " : "",
328		(usbcmd & USBCMD_EGSM) ?    "EGSM " : "",
329		(usbcmd & USBCMD_GRESET) ?  "GRESET " : "",
330		(usbcmd & USBCMD_HCRESET) ? "HCRESET " : "",
331		(usbcmd & USBCMD_RS) ?      "RS " : "");
332	if (out - buf > len)
333		goto done;
334
335	out += sprintf(out, "  usbstat   =     %04x   %s%s%s%s%s%s\n",
336		usbstat,
337		(usbstat & USBSTS_HCH) ?    "HCHalted " : "",
338		(usbstat & USBSTS_HCPE) ?   "HostControllerProcessError " : "",
339		(usbstat & USBSTS_HSE) ?    "HostSystemError " : "",
340		(usbstat & USBSTS_RD) ?     "ResumeDetect " : "",
341		(usbstat & USBSTS_ERROR) ?  "USBError " : "",
342		(usbstat & USBSTS_USBINT) ? "USBINT " : "");
343	if (out - buf > len)
344		goto done;
345
346	out += sprintf(out, "  usbint    =     %04x\n", usbint);
347	out += sprintf(out, "  usbfrnum  =   (%d)%03x\n", (usbfrnum >> 10) & 1,
348		0xfff & (4*(unsigned int)usbfrnum));
349	out += sprintf(out, "  flbaseadd = %08x\n", flbaseadd);
350	out += sprintf(out, "  sof       =       %02x\n", sof);
351	if (out - buf > len)
352		goto done;
353
354	out += uhci_show_sc(1, portsc1, out);
355	if (out - buf > len)
356		goto done;
357
358	out += uhci_show_sc(2, portsc2, out);
359	if (out - buf > len)
360		goto done;
361
362	out += sprintf(out,
363			"Most recent frame: %x (%d)   Last ISO frame: %x (%d)\n",
364			uhci->frame_number, uhci->frame_number & 1023,
365			uhci->last_iso_frame, uhci->last_iso_frame & 1023);
366
367done:
368	if (out - buf > len)
369		out += sprintf(out, " ...\n");
370	return out - buf;
371}
372
373static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
374{
375	char *out = buf;
376	int i, j;
377	struct uhci_qh *qh;
378	struct uhci_td *td;
379	struct list_head *tmp, *head;
380	int nframes, nerrs;
381	__hc32 link;
382	__hc32 fsbr_link;
383
384	static const char * const qh_names[] = {
385		"unlink", "iso", "int128", "int64", "int32", "int16",
386		"int8", "int4", "int2", "async", "term"
387	};
388
389	out += uhci_show_root_hub_state(uhci, out);
390	if (out - buf > len)
391		goto done;
392	out += sprintf(out, "HC status\n");
393	out += uhci_show_status(uhci, out, len - (out - buf));
394	if (out - buf > len)
395		goto tail;
396
397	out += sprintf(out, "Periodic load table\n");
398	for (i = 0; i < MAX_PHASE; ++i) {
399		out += sprintf(out, "\t%d", uhci->load[i]);
400		if (i % 8 == 7)
401			*out++ = '\n';
402	}
403	out += sprintf(out, "Total: %d, #INT: %d, #ISO: %d\n",
404			uhci->total_load,
405			uhci_to_hcd(uhci)->self.bandwidth_int_reqs,
406			uhci_to_hcd(uhci)->self.bandwidth_isoc_reqs);
407	if (debug <= 1)
408		goto tail;
409
410	out += sprintf(out, "Frame List\n");
411	nframes = 10;
412	nerrs = 0;
413	for (i = 0; i < UHCI_NUMFRAMES; ++i) {
414		__hc32 qh_dma;
415
416		if (out - buf > len)
417			goto done;
418		j = 0;
419		td = uhci->frame_cpu[i];
420		link = uhci->frame[i];
421		if (!td)
422			goto check_link;
423
424		if (nframes > 0) {
425			out += sprintf(out, "- Frame %d -> (%08x)\n",
426					i, hc32_to_cpu(uhci, link));
427			j = 1;
428		}
429
430		head = &td->fl_list;
431		tmp = head;
432		do {
433			td = list_entry(tmp, struct uhci_td, fl_list);
434			tmp = tmp->next;
435			if (link != LINK_TO_TD(uhci, td)) {
436				if (nframes > 0) {
437					out += sprintf(out,
438						"    link does not match list entry!\n");
439					if (out - buf > len)
440						goto done;
441				} else
442					++nerrs;
443			}
444			if (nframes > 0) {
445				out += uhci_show_td(uhci, td, out,
446						len - (out - buf), 4);
447				if (out - buf > len)
448					goto tail;
449			}
450			link = td->link;
451		} while (tmp != head);
452
453check_link:
454		qh_dma = uhci_frame_skel_link(uhci, i);
455		if (link != qh_dma) {
456			if (nframes > 0) {
457				if (!j) {
458					out += sprintf(out,
459						"- Frame %d -> (%08x)\n",
460						i, hc32_to_cpu(uhci, link));
461					j = 1;
462				}
463				out += sprintf(out,
464					"   link does not match QH (%08x)!\n",
465					hc32_to_cpu(uhci, qh_dma));
466				if (out - buf > len)
467					goto done;
468			} else
469				++nerrs;
470		}
471		nframes -= j;
472	}
473	if (nerrs > 0)
474		out += sprintf(out, "Skipped %d bad links\n", nerrs);
475
476	out += sprintf(out, "Skeleton QHs\n");
477
478	if (out - buf > len)
479		goto done;
480
481	fsbr_link = 0;
482	for (i = 0; i < UHCI_NUM_SKELQH; ++i) {
483		int cnt = 0;
484
485		qh = uhci->skelqh[i];
486		out += sprintf(out, "- skel_%s_qh\n", qh_names[i]);
487		out += uhci_show_qh(uhci, qh, out, len - (out - buf), 4);
488		if (out - buf > len)
489			goto tail;
490
491		/* Last QH is the Terminating QH, it's different */
492		if (i == SKEL_TERM) {
493			if (qh_element(qh) != LINK_TO_TD(uhci, uhci->term_td)) {
494				out += sprintf(out,
495					"    skel_term_qh element is not set to term_td!\n");
496				if (out - buf > len)
497					goto done;
498			}
499			link = fsbr_link;
500			if (!link)
501				link = LINK_TO_QH(uhci, uhci->skel_term_qh);
502			goto check_qh_link;
503		}
504
505		head = &qh->node;
506		tmp = head->next;
507
508		while (tmp != head) {
509			qh = list_entry(tmp, struct uhci_qh, node);
510			tmp = tmp->next;
511			if (++cnt <= 10) {
512				out += uhci_show_qh(uhci, qh, out,
513						len - (out - buf), 4);
514				if (out - buf > len)
515					goto tail;
516			}
517			if (!fsbr_link && qh->skel >= SKEL_FSBR)
518				fsbr_link = LINK_TO_QH(uhci, qh);
519		}
520		if ((cnt -= 10) > 0)
521			out += sprintf(out, "    Skipped %d QHs\n", cnt);
522
523		link = UHCI_PTR_TERM(uhci);
524		if (i <= SKEL_ISO)
525			;
526		else if (i < SKEL_ASYNC)
527			link = LINK_TO_QH(uhci, uhci->skel_async_qh);
528		else if (!uhci->fsbr_is_on)
529			;
530		else
531			link = LINK_TO_QH(uhci, uhci->skel_term_qh);
532check_qh_link:
533		if (qh->link != link)
534			out += sprintf(out,
535				"    last QH not linked to next skeleton!\n");
536
537		if (out - buf > len)
538			goto done;
539	}
540
541done:
542	if (out - buf > len)
543		out += sprintf(out, " ...\n");
544tail:
545	return out - buf;
546}
547
548#ifdef CONFIG_DEBUG_FS
549
550#define MAX_OUTPUT	(64 * 1024)
551
552struct uhci_debug {
553	int size;
554	char *data;
555};
556
557static int uhci_debug_open(struct inode *inode, struct file *file)
558{
559	struct uhci_hcd *uhci = inode->i_private;
560	struct uhci_debug *up;
561	unsigned long flags;
562
563	up = kmalloc(sizeof(*up), GFP_KERNEL);
564	if (!up)
565		return -ENOMEM;
566
567	up->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
568	if (!up->data) {
569		kfree(up);
570		return -ENOMEM;
571	}
572
573	up->size = 0;
574	spin_lock_irqsave(&uhci->lock, flags);
575	if (uhci->is_initialized)
576		up->size = uhci_sprint_schedule(uhci, up->data,
577					MAX_OUTPUT - EXTRA_SPACE);
578	spin_unlock_irqrestore(&uhci->lock, flags);
579
580	file->private_data = up;
581
582	return 0;
583}
584
585static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence)
586{
587	struct uhci_debug *up;
588	loff_t new = -1;
589
590	up = file->private_data;
591
592	/*
593	 * XXX: atomic 64bit seek access, but that needs to be fixed in the VFS
594	 */
595	switch (whence) {
596	case 0:
597		new = off;
598		break;
599	case 1:
600		new = file->f_pos + off;
601		break;
602	}
603
604	if (new < 0 || new > up->size)
605		return -EINVAL;
606
607	return (file->f_pos = new);
608}
609
610static ssize_t uhci_debug_read(struct file *file, char __user *buf,
611				size_t nbytes, loff_t *ppos)
612{
613	struct uhci_debug *up = file->private_data;
614	return simple_read_from_buffer(buf, nbytes, ppos, up->data, up->size);
615}
616
617static int uhci_debug_release(struct inode *inode, struct file *file)
618{
619	struct uhci_debug *up = file->private_data;
620
621	kfree(up->data);
622	kfree(up);
623
624	return 0;
625}
626
627static const struct file_operations uhci_debug_operations = {
628	.owner =	THIS_MODULE,
629	.open =		uhci_debug_open,
630	.llseek =	uhci_debug_lseek,
631	.read =		uhci_debug_read,
632	.release =	uhci_debug_release,
633};
634#define UHCI_DEBUG_OPS
635
636#endif	/* CONFIG_DEBUG_FS */
637
638#else	/* CONFIG_DYNAMIC_DEBUG*/
639
640static inline void lprintk(char *buf)
641{}
642
643static inline int uhci_show_qh(struct uhci_hcd *uhci,
644		struct uhci_qh *qh, char *buf, int len, int space)
645{
646	return 0;
647}
648
649static inline int uhci_sprint_schedule(struct uhci_hcd *uhci,
650		char *buf, int len)
651{
652	return 0;
653}
654
655#endif
656