vserver 1.9.5.x5
[linux-2.6.git] / drivers / usb / host / uhci-debug.c
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/config.h>
13 #include <linux/kernel.h>
14 #include <linux/debugfs.h>
15 #include <linux/smp_lock.h>
16 #include <asm/io.h>
17
18 #include "uhci-hcd.h"
19
20 /* Handle REALLY large printk's so we don't overflow buffers */
21 static inline void lprintk(char *buf)
22 {
23         char *p;
24
25         /* Just write one line at a time */
26         while (buf) {
27                 p = strchr(buf, '\n');
28                 if (p)
29                         *p = 0;
30                 printk(KERN_DEBUG "%s\n", buf);
31                 buf = p;
32                 if (buf)
33                         buf++;
34         }
35 }
36
37 static int uhci_show_td(struct uhci_td *td, char *buf, int len, int space)
38 {
39         char *out = buf;
40         char *spid;
41         u32 status, token;
42
43         /* Try to make sure there's enough memory */
44         if (len < 160)
45                 return 0;
46
47         status = td_status(td);
48         out += sprintf(out, "%*s[%p] link (%08x) ", space, "", td, le32_to_cpu(td->link));
49         out += sprintf(out, "e%d %s%s%s%s%s%s%s%s%s%sLength=%x ",
50                 ((status >> 27) & 3),
51                 (status & TD_CTRL_SPD) ?      "SPD " : "",
52                 (status & TD_CTRL_LS) ?       "LS " : "",
53                 (status & TD_CTRL_IOC) ?      "IOC " : "",
54                 (status & TD_CTRL_ACTIVE) ?   "Active " : "",
55                 (status & TD_CTRL_STALLED) ?  "Stalled " : "",
56                 (status & TD_CTRL_DBUFERR) ?  "DataBufErr " : "",
57                 (status & TD_CTRL_BABBLE) ?   "Babble " : "",
58                 (status & TD_CTRL_NAK) ?      "NAK " : "",
59                 (status & TD_CTRL_CRCTIMEO) ? "CRC/Timeo " : "",
60                 (status & TD_CTRL_BITSTUFF) ? "BitStuff " : "",
61                 status & 0x7ff);
62
63         token = td_token(td);
64         switch (uhci_packetid(token)) {
65                 case USB_PID_SETUP:
66                         spid = "SETUP";
67                         break;
68                 case USB_PID_OUT:
69                         spid = "OUT";
70                         break;
71                 case USB_PID_IN:
72                         spid = "IN";
73                         break;
74                 default:
75                         spid = "?";
76                         break;
77         }
78
79         out += sprintf(out, "MaxLen=%x DT%d EndPt=%x Dev=%x, PID=%x(%s) ",
80                 token >> 21,
81                 ((token >> 19) & 1),
82                 (token >> 15) & 15,
83                 (token >> 8) & 127,
84                 (token & 0xff),
85                 spid);
86         out += sprintf(out, "(buf=%08x)\n", le32_to_cpu(td->buffer));
87
88         return out - buf;
89 }
90
91 static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space)
92 {
93         char *out = buf;
94         struct urb_priv *urbp;
95         struct list_head *head, *tmp;
96         struct uhci_td *td;
97         int i = 0, checked = 0, prevactive = 0;
98         __le32 element = qh_element(qh);
99
100         /* Try to make sure there's enough memory */
101         if (len < 80 * 6)
102                 return 0;
103
104         out += sprintf(out, "%*s[%p] link (%08x) element (%08x)\n", space, "",
105                         qh, le32_to_cpu(qh->link), le32_to_cpu(element));
106
107         if (element & UHCI_PTR_QH)
108                 out += sprintf(out, "%*s  Element points to QH (bug?)\n", space, "");
109
110         if (element & UHCI_PTR_DEPTH)
111                 out += sprintf(out, "%*s  Depth traverse\n", space, "");
112
113         if (element & cpu_to_le32(8))
114                 out += sprintf(out, "%*s  Bit 3 set (bug?)\n", space, "");
115
116         if (!(element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH)))
117                 out += sprintf(out, "%*s  Element is NULL (bug?)\n", space, "");
118
119         if (!qh->urbp) {
120                 out += sprintf(out, "%*s  urbp == NULL\n", space, "");
121                 goto out;
122         }
123
124         urbp = qh->urbp;
125
126         head = &urbp->td_list;
127         tmp = head->next;
128
129         td = list_entry(tmp, struct uhci_td, list);
130
131         if (cpu_to_le32(td->dma_handle) != (element & ~UHCI_PTR_BITS))
132                 out += sprintf(out, "%*s Element != First TD\n", space, "");
133
134         while (tmp != head) {
135                 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
136
137                 tmp = tmp->next;
138
139                 out += sprintf(out, "%*s%d: ", space + 2, "", i++);
140                 out += uhci_show_td(td, out, len - (out - buf), 0);
141
142                 if (i > 10 && !checked && prevactive && tmp != head &&
143                     debug <= 2) {
144                         struct list_head *ntmp = tmp;
145                         struct uhci_td *ntd = td;
146                         int active = 1, ni = i;
147
148                         checked = 1;
149
150                         while (ntmp != head && ntmp->next != head && active) {
151                                 ntd = list_entry(ntmp, struct uhci_td, list);
152
153                                 ntmp = ntmp->next;
154
155                                 active = td_status(ntd) & TD_CTRL_ACTIVE;
156
157                                 ni++;
158                         }
159
160                         if (active && ni > i) {
161                                 out += sprintf(out, "%*s[skipped %d active TD's]\n", space, "", ni - i);
162                                 tmp = ntmp;
163                                 td = ntd;
164                                 i = ni;
165                         }
166                 }
167
168                 prevactive = td_status(td) & TD_CTRL_ACTIVE;
169         }
170
171         if (list_empty(&urbp->queue_list) || urbp->queued)
172                 goto out;
173
174         out += sprintf(out, "%*sQueued QH's:\n", -space, "--");
175
176         head = &urbp->queue_list;
177         tmp = head->next;
178
179         while (tmp != head) {
180                 struct urb_priv *nurbp = list_entry(tmp, struct urb_priv,
181                                                 queue_list);
182                 tmp = tmp->next;
183
184                 out += uhci_show_qh(nurbp->qh, out, len - (out - buf), space);
185         }
186
187 out:
188         return out - buf;
189 }
190
191 #define show_frame_num()        \
192         if (!shown) {           \
193           shown = 1;            \
194           out += sprintf(out, "- Frame %d\n", i); \
195         }
196
197 #ifdef CONFIG_PROC_FS
198 static const char *qh_names[] = {
199   "skel_int128_qh", "skel_int64_qh",
200   "skel_int32_qh", "skel_int16_qh",
201   "skel_int8_qh", "skel_int4_qh",
202   "skel_int2_qh", "skel_int1_qh",
203   "skel_ls_control_qh", "skel_fs_control_qh",
204   "skel_bulk_qh", "skel_term_qh"
205 };
206
207 #define show_qh_name()          \
208         if (!shown) {           \
209           shown = 1;            \
210           out += sprintf(out, "- %s\n", qh_names[i]); \
211         }
212
213 static int uhci_show_sc(int port, unsigned short status, char *buf, int len)
214 {
215         char *out = buf;
216
217         /* Try to make sure there's enough memory */
218         if (len < 160)
219                 return 0;
220
221         out += sprintf(out, "  stat%d     =     %04x  %s%s%s%s%s%s%s%s%s%s\n",
222                 port,
223                 status,
224                 (status & USBPORTSC_SUSP) ?     " Suspend" : "",
225                 (status & USBPORTSC_OCC) ?      " OverCurrentChange" : "",
226                 (status & USBPORTSC_OC) ?       " OverCurrent" : "",
227                 (status & USBPORTSC_PR) ?       " Reset" : "",
228                 (status & USBPORTSC_LSDA) ?     " LowSpeed" : "",
229                 (status & USBPORTSC_RD) ?       " ResumeDetect" : "",
230                 (status & USBPORTSC_PEC) ?      " EnableChange" : "",
231                 (status & USBPORTSC_PE) ?       " Enabled" : "",
232                 (status & USBPORTSC_CSC) ?      " ConnectChange" : "",
233                 (status & USBPORTSC_CCS) ?      " Connected" : "");
234
235         return out - buf;
236 }
237
238 static int uhci_show_status(struct uhci_hcd *uhci, char *buf, int len)
239 {
240         char *out = buf;
241         unsigned long io_addr = uhci->io_addr;
242         unsigned short usbcmd, usbstat, usbint, usbfrnum;
243         unsigned int flbaseadd;
244         unsigned char sof;
245         unsigned short portsc1, portsc2;
246
247         /* Try to make sure there's enough memory */
248         if (len < 80 * 6)
249                 return 0;
250
251         usbcmd    = inw(io_addr + 0);
252         usbstat   = inw(io_addr + 2);
253         usbint    = inw(io_addr + 4);
254         usbfrnum  = inw(io_addr + 6);
255         flbaseadd = inl(io_addr + 8);
256         sof       = inb(io_addr + 12);
257         portsc1   = inw(io_addr + 16);
258         portsc2   = inw(io_addr + 18);
259
260         out += sprintf(out, "  usbcmd    =     %04x   %s%s%s%s%s%s%s%s\n",
261                 usbcmd,
262                 (usbcmd & USBCMD_MAXP) ?    "Maxp64 " : "Maxp32 ",
263                 (usbcmd & USBCMD_CF) ?      "CF " : "",
264                 (usbcmd & USBCMD_SWDBG) ?   "SWDBG " : "",
265                 (usbcmd & USBCMD_FGR) ?     "FGR " : "",
266                 (usbcmd & USBCMD_EGSM) ?    "EGSM " : "",
267                 (usbcmd & USBCMD_GRESET) ?  "GRESET " : "",
268                 (usbcmd & USBCMD_HCRESET) ? "HCRESET " : "",
269                 (usbcmd & USBCMD_RS) ?      "RS " : "");
270
271         out += sprintf(out, "  usbstat   =     %04x   %s%s%s%s%s%s\n",
272                 usbstat,
273                 (usbstat & USBSTS_HCH) ?    "HCHalted " : "",
274                 (usbstat & USBSTS_HCPE) ?   "HostControllerProcessError " : "",
275                 (usbstat & USBSTS_HSE) ?    "HostSystemError " : "",
276                 (usbstat & USBSTS_RD) ?     "ResumeDetect " : "",
277                 (usbstat & USBSTS_ERROR) ?  "USBError " : "",
278                 (usbstat & USBSTS_USBINT) ? "USBINT " : "");
279
280         out += sprintf(out, "  usbint    =     %04x\n", usbint);
281         out += sprintf(out, "  usbfrnum  =   (%d)%03x\n", (usbfrnum >> 10) & 1,
282                 0xfff & (4*(unsigned int)usbfrnum));
283         out += sprintf(out, "  flbaseadd = %08x\n", flbaseadd);
284         out += sprintf(out, "  sof       =       %02x\n", sof);
285         out += uhci_show_sc(1, portsc1, out, len - (out - buf));
286         out += uhci_show_sc(2, portsc2, out, len - (out - buf));
287
288         return out - buf;
289 }
290
291 static int uhci_show_urbp(struct uhci_hcd *uhci, struct urb_priv *urbp, char *buf, int len)
292 {
293         struct list_head *tmp;
294         char *out = buf;
295         int count = 0;
296
297         if (len < 200)
298                 return 0;
299
300         out += sprintf(out, "urb_priv [%p] ", urbp);
301         out += sprintf(out, "urb [%p] ", urbp->urb);
302         out += sprintf(out, "qh [%p] ", urbp->qh);
303         out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
304         out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe), (usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
305
306         switch (usb_pipetype(urbp->urb->pipe)) {
307         case PIPE_ISOCHRONOUS: out += sprintf(out, "ISO "); break;
308         case PIPE_INTERRUPT: out += sprintf(out, "INT "); break;
309         case PIPE_BULK: out += sprintf(out, "BLK "); break;
310         case PIPE_CONTROL: out += sprintf(out, "CTL "); break;
311         }
312
313         out += sprintf(out, "%s", (urbp->fsbr ? "FSBR " : ""));
314         out += sprintf(out, "%s", (urbp->fsbr_timeout ? "FSBR_TO " : ""));
315
316         if (urbp->urb->status != -EINPROGRESS)
317                 out += sprintf(out, "Status=%d ", urbp->urb->status);
318         //out += sprintf(out, "Inserttime=%lx ",urbp->inserttime);
319         //out += sprintf(out, "FSBRtime=%lx ",urbp->fsbrtime);
320
321         count = 0;
322         list_for_each(tmp, &urbp->td_list)
323                 count++;
324         out += sprintf(out, "TDs=%d ",count);
325
326         if (urbp->queued)
327                 out += sprintf(out, "queued\n");
328         else {
329                 count = 0;
330                 list_for_each(tmp, &urbp->queue_list)
331                         count++;
332                 out += sprintf(out, "queued URBs=%d\n", count);
333         }
334
335         return out - buf;
336 }
337
338 static int uhci_show_lists(struct uhci_hcd *uhci, char *buf, int len)
339 {
340         char *out = buf;
341         struct list_head *head, *tmp;
342         int count;
343
344         out += sprintf(out, "Main list URBs:");
345         if (list_empty(&uhci->urb_list))
346                 out += sprintf(out, " Empty\n");
347         else {
348                 out += sprintf(out, "\n");
349                 count = 0;
350                 head = &uhci->urb_list;
351                 tmp = head->next;
352                 while (tmp != head) {
353                         struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
354
355                         out += sprintf(out, "  %d: ", ++count);
356                         out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
357                         tmp = tmp->next;
358                 }
359         }
360
361         out += sprintf(out, "Remove list URBs:");
362         if (list_empty(&uhci->urb_remove_list))
363                 out += sprintf(out, " Empty\n");
364         else {
365                 out += sprintf(out, "\n");
366                 count = 0;
367                 head = &uhci->urb_remove_list;
368                 tmp = head->next;
369                 while (tmp != head) {
370                         struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
371
372                         out += sprintf(out, "  %d: ", ++count);
373                         out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
374                         tmp = tmp->next;
375                 }
376         }
377
378         out += sprintf(out, "Complete list URBs:");
379         if (list_empty(&uhci->complete_list))
380                 out += sprintf(out, " Empty\n");
381         else {
382                 out += sprintf(out, "\n");
383                 count = 0;
384                 head = &uhci->complete_list;
385                 tmp = head->next;
386                 while (tmp != head) {
387                         struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
388
389                         out += sprintf(out, "  %d: ", ++count);
390                         out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
391                         tmp = tmp->next;
392                 }
393         }
394
395         return out - buf;
396 }
397
398 static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
399 {
400         unsigned long flags;
401         char *out = buf;
402         int i, j;
403         struct uhci_qh *qh;
404         struct uhci_td *td;
405         struct list_head *tmp, *head;
406
407         spin_lock_irqsave(&uhci->schedule_lock, flags);
408
409         out += sprintf(out, "HC status\n");
410         out += uhci_show_status(uhci, out, len - (out - buf));
411
412         out += sprintf(out, "Frame List\n");
413         for (i = 0; i < UHCI_NUMFRAMES; ++i) {
414                 int shown = 0;
415                 td = uhci->fl->frame_cpu[i];
416                 if (!td)
417                         continue;
418
419                 if (td->dma_handle != (dma_addr_t)uhci->fl->frame[i]) {
420                         show_frame_num();
421                         out += sprintf(out, "    frame list does not match td->dma_handle!\n");
422                 }
423                 show_frame_num();
424
425                 head = &td->fl_list;
426                 tmp = head;
427                 do {
428                         td = list_entry(tmp, struct uhci_td, fl_list);
429                         tmp = tmp->next;
430                         out += uhci_show_td(td, out, len - (out - buf), 4);
431                 } while (tmp != head);
432         }
433
434         out += sprintf(out, "Skeleton QH's\n");
435
436         for (i = 0; i < UHCI_NUM_SKELQH; ++i) {
437                 int shown = 0;
438
439                 qh = uhci->skelqh[i];
440
441                 if (debug > 1) {
442                         show_qh_name();
443                         out += uhci_show_qh(qh, out, len - (out - buf), 4);
444                 }
445
446                 /* Last QH is the Terminating QH, it's different */
447                 if (i == UHCI_NUM_SKELQH - 1) {
448                         if (qh->link != UHCI_PTR_TERM)
449                                 out += sprintf(out, "    bandwidth reclamation on!\n");
450
451                         if (qh_element(qh) != cpu_to_le32(uhci->term_td->dma_handle))
452                                 out += sprintf(out, "    skel_term_qh element is not set to term_td!\n");
453
454                         continue;
455                 }
456
457                 j = (i < 7) ? 7 : i+1;          /* Next skeleton */
458                 if (list_empty(&qh->list)) {
459                         if (i < UHCI_NUM_SKELQH - 1) {
460                                 if (qh->link !=
461                                     (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH)) {
462                                         show_qh_name();
463                                         out += sprintf(out, "    skeleton QH not linked to next skeleton QH!\n");
464                                 }
465                         }
466
467                         continue;
468                 }
469
470                 show_qh_name();
471
472                 head = &qh->list;
473                 tmp = head->next;
474
475                 while (tmp != head) {
476                         qh = list_entry(tmp, struct uhci_qh, list);
477
478                         tmp = tmp->next;
479
480                         out += uhci_show_qh(qh, out, len - (out - buf), 4);
481                 }
482
483                 if (i < UHCI_NUM_SKELQH - 1) {
484                         if (qh->link !=
485                             (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH))
486                                 out += sprintf(out, "    last QH not linked to next skeleton!\n");
487                 }
488         }
489
490         if (debug > 2)
491                 out += uhci_show_lists(uhci, out, len - (out - buf));
492
493         spin_unlock_irqrestore(&uhci->schedule_lock, flags);
494
495         return out - buf;
496 }
497
498 #define MAX_OUTPUT      (64 * 1024)
499
500 static struct dentry *uhci_debugfs_root = NULL;
501
502 struct uhci_debug {
503         int size;
504         char *data;
505         struct uhci_hcd *uhci;
506 };
507
508 static int uhci_debug_open(struct inode *inode, struct file *file)
509 {
510         struct uhci_hcd *uhci = inode->u.generic_ip;
511         struct uhci_debug *up;
512         int ret = -ENOMEM;
513
514         lock_kernel();
515         up = kmalloc(sizeof(*up), GFP_KERNEL);
516         if (!up)
517                 goto out;
518
519         up->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
520         if (!up->data) {
521                 kfree(up);
522                 goto out;
523         }
524
525         up->size = uhci_sprint_schedule(uhci, up->data, MAX_OUTPUT);
526
527         file->private_data = up;
528
529         ret = 0;
530 out:
531         unlock_kernel();
532         return ret;
533 }
534
535 static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence)
536 {
537         struct uhci_debug *up;
538         loff_t new = -1;
539
540         lock_kernel();
541         up = file->private_data;
542
543         switch (whence) {
544         case 0:
545                 new = off;
546                 break;
547         case 1:
548                 new = file->f_pos + off;
549                 break;
550         }
551         if (new < 0 || new > up->size) {
552                 unlock_kernel();
553                 return -EINVAL;
554         }
555         unlock_kernel();
556         return (file->f_pos = new);
557 }
558
559 static ssize_t uhci_debug_read(struct file *file, char __user *buf,
560                                 size_t nbytes, loff_t *ppos)
561 {
562         struct uhci_debug *up = file->private_data;
563         return simple_read_from_buffer(buf, nbytes, ppos, up->data, up->size);
564 }
565
566 static int uhci_debug_release(struct inode *inode, struct file *file)
567 {
568         struct uhci_debug *up = file->private_data;
569
570         kfree(up->data);
571         kfree(up);
572
573         return 0;
574 }
575
576 static struct file_operations uhci_debug_operations = {
577         .open =         uhci_debug_open,
578         .llseek =       uhci_debug_lseek,
579         .read =         uhci_debug_read,
580         .release =      uhci_debug_release,
581 };
582 #endif