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