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