upgrade to linux 2.6.10-1.12_FC2
[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/proc_fs.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
99         /* Try to make sure there's enough memory */
100         if (len < 80 * 6)
101                 return 0;
102
103         out += sprintf(out, "%*s[%p] link (%08x) element (%08x)\n", space, "",
104                         qh, le32_to_cpu(qh->link), le32_to_cpu(qh->element));
105
106         if (qh->element & UHCI_PTR_QH)
107                 out += sprintf(out, "%*s  Element points to QH (bug?)\n", space, "");
108
109         if (qh->element & UHCI_PTR_DEPTH)
110                 out += sprintf(out, "%*s  Depth traverse\n", space, "");
111
112         if (qh->element & cpu_to_le32(8))
113                 out += sprintf(out, "%*s  Bit 3 set (bug?)\n", space, "");
114
115         if (!(qh->element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH)))
116                 out += sprintf(out, "%*s  Element is NULL (bug?)\n", space, "");
117
118         if (!qh->urbp) {
119                 out += sprintf(out, "%*s  urbp == NULL\n", space, "");
120                 goto out;
121         }
122
123         urbp = qh->urbp;
124
125         head = &urbp->td_list;
126         tmp = head->next;
127
128         td = list_entry(tmp, struct uhci_td, list);
129
130         if (cpu_to_le32(td->dma_handle) != (qh->element & ~UHCI_PTR_BITS))
131                 out += sprintf(out, "%*s Element != First TD\n", space, "");
132
133         while (tmp != head) {
134                 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
135
136                 tmp = tmp->next;
137
138                 out += sprintf(out, "%*s%d: ", space + 2, "", i++);
139                 out += uhci_show_td(td, out, len - (out - buf), 0);
140
141                 if (i > 10 && !checked && prevactive && tmp != head &&
142                     debug <= 2) {
143                         struct list_head *ntmp = tmp;
144                         struct uhci_td *ntd = td;
145                         int active = 1, ni = i;
146
147                         checked = 1;
148
149                         while (ntmp != head && ntmp->next != head && active) {
150                                 ntd = list_entry(ntmp, struct uhci_td, list);
151
152                                 ntmp = ntmp->next;
153
154                                 active = td_status(ntd) & TD_CTRL_ACTIVE;
155
156                                 ni++;
157                         }
158
159                         if (active && ni > i) {
160                                 out += sprintf(out, "%*s[skipped %d active TD's]\n", space, "", ni - i);
161                                 tmp = ntmp;
162                                 td = ntd;
163                                 i = ni;
164                         }
165                 }
166
167                 prevactive = td_status(td) & TD_CTRL_ACTIVE;
168         }
169
170         if (list_empty(&urbp->queue_list) || urbp->queued)
171                 goto out;
172
173         out += sprintf(out, "%*sQueued QH's:\n", -space, "--");
174
175         head = &urbp->queue_list;
176         tmp = head->next;
177
178         while (tmp != head) {
179                 struct urb_priv *nurbp = list_entry(tmp, struct urb_priv,
180                                                 queue_list);
181                 tmp = tmp->next;
182
183                 out += uhci_show_qh(nurbp->qh, out, len - (out - buf), space);
184         }
185
186 out:
187         return out - buf;
188 }
189
190 #define show_frame_num()        \
191         if (!shown) {           \
192           shown = 1;            \
193           out += sprintf(out, "- Frame %d\n", i); \
194         }
195
196 #ifdef CONFIG_PROC_FS
197 static const char *qh_names[] = {
198   "skel_int128_qh", "skel_int64_qh",
199   "skel_int32_qh", "skel_int16_qh",
200   "skel_int8_qh", "skel_int4_qh",
201   "skel_int2_qh", "skel_int1_qh",
202   "skel_ls_control_qh", "skel_fs_control_qh",
203   "skel_bulk_qh", "skel_term_qh"
204 };
205
206 #define show_qh_name()          \
207         if (!shown) {           \
208           shown = 1;            \
209           out += sprintf(out, "- %s\n", qh_names[i]); \
210         }
211
212 static int uhci_show_sc(int port, unsigned short status, char *buf, int len)
213 {
214         char *out = buf;
215
216         /* Try to make sure there's enough memory */
217         if (len < 160)
218                 return 0;
219
220         out += sprintf(out, "  stat%d     =     %04x  %s%s%s%s%s%s%s%s%s%s\n",
221                 port,
222                 status,
223                 (status & USBPORTSC_SUSP) ?     " Suspend" : "",
224                 (status & USBPORTSC_OCC) ?      " OverCurrentChange" : "",
225                 (status & USBPORTSC_OC) ?       " OverCurrent" : "",
226                 (status & USBPORTSC_PR) ?       " Reset" : "",
227                 (status & USBPORTSC_LSDA) ?     " LowSpeed" : "",
228                 (status & USBPORTSC_RD) ?       " ResumeDetect" : "",
229                 (status & USBPORTSC_PEC) ?      " EnableChange" : "",
230                 (status & USBPORTSC_PE) ?       " Enabled" : "",
231                 (status & USBPORTSC_CSC) ?      " ConnectChange" : "",
232                 (status & USBPORTSC_CCS) ?      " Connected" : "");
233
234         return out - buf;
235 }
236
237 static int uhci_show_status(struct uhci_hcd *uhci, char *buf, int len)
238 {
239         char *out = buf;
240         unsigned long io_addr = uhci->io_addr;
241         unsigned short usbcmd, usbstat, usbint, usbfrnum;
242         unsigned int flbaseadd;
243         unsigned char sof;
244         unsigned short portsc1, portsc2;
245
246         /* Try to make sure there's enough memory */
247         if (len < 80 * 6)
248                 return 0;
249
250         usbcmd    = inw(io_addr + 0);
251         usbstat   = inw(io_addr + 2);
252         usbint    = inw(io_addr + 4);
253         usbfrnum  = inw(io_addr + 6);
254         flbaseadd = inl(io_addr + 8);
255         sof       = inb(io_addr + 12);
256         portsc1   = inw(io_addr + 16);
257         portsc2   = inw(io_addr + 18);
258
259         out += sprintf(out, "  usbcmd    =     %04x   %s%s%s%s%s%s%s%s\n",
260                 usbcmd,
261                 (usbcmd & USBCMD_MAXP) ?    "Maxp64 " : "Maxp32 ",
262                 (usbcmd & USBCMD_CF) ?      "CF " : "",
263                 (usbcmd & USBCMD_SWDBG) ?   "SWDBG " : "",
264                 (usbcmd & USBCMD_FGR) ?     "FGR " : "",
265                 (usbcmd & USBCMD_EGSM) ?    "EGSM " : "",
266                 (usbcmd & USBCMD_GRESET) ?  "GRESET " : "",
267                 (usbcmd & USBCMD_HCRESET) ? "HCRESET " : "",
268                 (usbcmd & USBCMD_RS) ?      "RS " : "");
269
270         out += sprintf(out, "  usbstat   =     %04x   %s%s%s%s%s%s\n",
271                 usbstat,
272                 (usbstat & USBSTS_HCH) ?    "HCHalted " : "",
273                 (usbstat & USBSTS_HCPE) ?   "HostControllerProcessError " : "",
274                 (usbstat & USBSTS_HSE) ?    "HostSystemError " : "",
275                 (usbstat & USBSTS_RD) ?     "ResumeDetect " : "",
276                 (usbstat & USBSTS_ERROR) ?  "USBError " : "",
277                 (usbstat & USBSTS_USBINT) ? "USBINT " : "");
278
279         out += sprintf(out, "  usbint    =     %04x\n", usbint);
280         out += sprintf(out, "  usbfrnum  =   (%d)%03x\n", (usbfrnum >> 10) & 1,
281                 0xfff & (4*(unsigned int)usbfrnum));
282         out += sprintf(out, "  flbaseadd = %08x\n", flbaseadd);
283         out += sprintf(out, "  sof       =       %02x\n", sof);
284         out += uhci_show_sc(1, portsc1, out, len - (out - buf));
285         out += uhci_show_sc(2, portsc2, out, len - (out - buf));
286
287         return out - buf;
288 }
289
290 static int uhci_show_urbp(struct uhci_hcd *uhci, struct urb_priv *urbp, char *buf, int len)
291 {
292         struct list_head *tmp;
293         char *out = buf;
294         int count = 0;
295
296         if (len < 200)
297                 return 0;
298
299         out += sprintf(out, "urb_priv [%p] ", urbp);
300         out += sprintf(out, "urb [%p] ", urbp->urb);
301         out += sprintf(out, "qh [%p] ", urbp->qh);
302         out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
303         out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe), (usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
304
305         switch (usb_pipetype(urbp->urb->pipe)) {
306         case PIPE_ISOCHRONOUS: out += sprintf(out, "ISO "); break;
307         case PIPE_INTERRUPT: out += sprintf(out, "INT "); break;
308         case PIPE_BULK: out += sprintf(out, "BLK "); break;
309         case PIPE_CONTROL: out += sprintf(out, "CTL "); break;
310         }
311
312         out += sprintf(out, "%s", (urbp->fsbr ? "FSBR " : ""));
313         out += sprintf(out, "%s", (urbp->fsbr_timeout ? "FSBR_TO " : ""));
314
315         if (urbp->urb->status != -EINPROGRESS)
316                 out += sprintf(out, "Status=%d ", urbp->urb->status);
317         //out += sprintf(out, "Inserttime=%lx ",urbp->inserttime);
318         //out += sprintf(out, "FSBRtime=%lx ",urbp->fsbrtime);
319
320         count = 0;
321         list_for_each(tmp, &urbp->td_list)
322                 count++;
323         out += sprintf(out, "TDs=%d ",count);
324
325         if (urbp->queued)
326                 out += sprintf(out, "queued\n");
327         else {
328                 count = 0;
329                 list_for_each(tmp, &urbp->queue_list)
330                         count++;
331                 out += sprintf(out, "queued URBs=%d\n", count);
332         }
333
334         return out - buf;
335 }
336
337 static int uhci_show_lists(struct uhci_hcd *uhci, char *buf, int len)
338 {
339         char *out = buf;
340         struct list_head *head, *tmp;
341         int count;
342
343         out += sprintf(out, "Main list URBs:");
344         if (list_empty(&uhci->urb_list))
345                 out += sprintf(out, " Empty\n");
346         else {
347                 out += sprintf(out, "\n");
348                 count = 0;
349                 head = &uhci->urb_list;
350                 tmp = head->next;
351                 while (tmp != head) {
352                         struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
353
354                         out += sprintf(out, "  %d: ", ++count);
355                         out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
356                         tmp = tmp->next;
357                 }
358         }
359
360         out += sprintf(out, "Remove list URBs:");
361         if (list_empty(&uhci->urb_remove_list))
362                 out += sprintf(out, " Empty\n");
363         else {
364                 out += sprintf(out, "\n");
365                 count = 0;
366                 head = &uhci->urb_remove_list;
367                 tmp = head->next;
368                 while (tmp != head) {
369                         struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
370
371                         out += sprintf(out, "  %d: ", ++count);
372                         out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
373                         tmp = tmp->next;
374                 }
375         }
376
377         out += sprintf(out, "Complete list URBs:");
378         if (list_empty(&uhci->complete_list))
379                 out += sprintf(out, " Empty\n");
380         else {
381                 out += sprintf(out, "\n");
382                 count = 0;
383                 head = &uhci->complete_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         return out - buf;
395 }
396
397 static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
398 {
399         unsigned long flags;
400         char *out = buf;
401         int i, j;
402         struct uhci_qh *qh;
403         struct uhci_td *td;
404         struct list_head *tmp, *head;
405
406         spin_lock_irqsave(&uhci->schedule_lock, flags);
407
408         out += sprintf(out, "HC status\n");
409         out += uhci_show_status(uhci, out, len - (out - buf));
410
411         out += sprintf(out, "Frame List\n");
412         for (i = 0; i < UHCI_NUMFRAMES; ++i) {
413                 int shown = 0;
414                 td = uhci->fl->frame_cpu[i];
415                 if (!td)
416                         continue;
417
418                 if (td->dma_handle != (dma_addr_t)uhci->fl->frame[i]) {
419                         show_frame_num();
420                         out += sprintf(out, "    frame list does not match td->dma_handle!\n");
421                 }
422                 show_frame_num();
423
424                 head = &td->fl_list;
425                 tmp = head;
426                 do {
427                         td = list_entry(tmp, struct uhci_td, fl_list);
428                         tmp = tmp->next;
429                         out += uhci_show_td(td, out, len - (out - buf), 4);
430                 } while (tmp != head);
431         }
432
433         out += sprintf(out, "Skeleton QH's\n");
434
435         for (i = 0; i < UHCI_NUM_SKELQH; ++i) {
436                 int shown = 0;
437
438                 qh = uhci->skelqh[i];
439
440                 if (debug > 1) {
441                         show_qh_name();
442                         out += uhci_show_qh(qh, out, len - (out - buf), 4);
443                 }
444
445                 /* Last QH is the Terminating QH, it's different */
446                 if (i == UHCI_NUM_SKELQH - 1) {
447                         if (qh->link != UHCI_PTR_TERM)
448                                 out += sprintf(out, "    bandwidth reclamation on!\n");
449
450                         if (qh->element != cpu_to_le32(uhci->term_td->dma_handle))
451                                 out += sprintf(out, "    skel_term_qh element is not set to term_td!\n");
452
453                         continue;
454                 }
455
456                 j = (i < 7) ? 7 : i+1;          /* Next skeleton */
457                 if (list_empty(&qh->list)) {
458                         if (i < UHCI_NUM_SKELQH - 1) {
459                                 if (qh->link !=
460                                     (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH)) {
461                                         show_qh_name();
462                                         out += sprintf(out, "    skeleton QH not linked to next skeleton QH!\n");
463                                 }
464                         }
465
466                         continue;
467                 }
468
469                 show_qh_name();
470
471                 head = &qh->list;
472                 tmp = head->next;
473
474                 while (tmp != head) {
475                         qh = list_entry(tmp, struct uhci_qh, list);
476
477                         tmp = tmp->next;
478
479                         out += uhci_show_qh(qh, out, len - (out - buf), 4);
480                 }
481
482                 if (i < UHCI_NUM_SKELQH - 1) {
483                         if (qh->link !=
484                             (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH))
485                                 out += sprintf(out, "    last QH not linked to next skeleton!\n");
486                 }
487         }
488
489         if (debug > 2)
490                 out += uhci_show_lists(uhci, out, len - (out - buf));
491
492         spin_unlock_irqrestore(&uhci->schedule_lock, flags);
493
494         return out - buf;
495 }
496
497 #define MAX_OUTPUT      (64 * 1024)
498
499 static struct proc_dir_entry *uhci_proc_root = NULL;
500
501 struct uhci_proc {
502         int size;
503         char *data;
504         struct uhci_hcd *uhci;
505 };
506
507 static int uhci_proc_open(struct inode *inode, struct file *file)
508 {
509         const struct proc_dir_entry *dp = PDE(inode);
510         struct uhci_hcd *uhci = dp->data;
511         struct uhci_proc *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_proc_lseek(struct file *file, loff_t off, int whence)
536 {
537         struct uhci_proc *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_proc_read(struct file *file, char __user *buf,
560                                 size_t nbytes, loff_t *ppos)
561 {
562         struct uhci_proc *up = file->private_data;
563         return simple_read_from_buffer(buf, nbytes, ppos, up->data, up->size);
564 }
565
566 static int uhci_proc_release(struct inode *inode, struct file *file)
567 {
568         struct uhci_proc *up = file->private_data;
569
570         kfree(up->data);
571         kfree(up);
572
573         return 0;
574 }
575
576 static struct file_operations uhci_proc_operations = {
577         .open =         uhci_proc_open,
578         .llseek =       uhci_proc_lseek,
579         .read =         uhci_proc_read,
580 //      write:          uhci_proc_write,
581         .release =      uhci_proc_release,
582 };
583 #endif