Merge master.kernel.org:/pub/scm/linux/kernel/git/sam/kbuild
[pandora-kernel.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 #define uhci_debug_operations (* (struct file_operations *) NULL)
21 static struct dentry *uhci_debugfs_root;
22
23 #ifdef DEBUG
24
25 /* Handle REALLY large printks so we don't overflow buffers */
26 static 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
42 static int uhci_show_td(struct uhci_td *td, char *buf, int len, int space)
43 {
44         char *out = buf;
45         char *spid;
46         u32 status, token;
47
48         /* Try to make sure there's enough memory */
49         if (len < 160)
50                 return 0;
51
52         status = td_status(td);
53         out += sprintf(out, "%*s[%p] link (%08x) ", space, "", td, le32_to_cpu(td->link));
54         out += sprintf(out, "e%d %s%s%s%s%s%s%s%s%s%sLength=%x ",
55                 ((status >> 27) & 3),
56                 (status & TD_CTRL_SPD) ?      "SPD " : "",
57                 (status & TD_CTRL_LS) ?       "LS " : "",
58                 (status & TD_CTRL_IOC) ?      "IOC " : "",
59                 (status & TD_CTRL_ACTIVE) ?   "Active " : "",
60                 (status & TD_CTRL_STALLED) ?  "Stalled " : "",
61                 (status & TD_CTRL_DBUFERR) ?  "DataBufErr " : "",
62                 (status & TD_CTRL_BABBLE) ?   "Babble " : "",
63                 (status & TD_CTRL_NAK) ?      "NAK " : "",
64                 (status & TD_CTRL_CRCTIMEO) ? "CRC/Timeo " : "",
65                 (status & TD_CTRL_BITSTUFF) ? "BitStuff " : "",
66                 status & 0x7ff);
67
68         token = td_token(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", le32_to_cpu(td->buffer));
92
93         return out - buf;
94 }
95
96 static int uhci_show_urbp(struct urb_priv *urbp, char *buf, int len, int space)
97 {
98         char *out = buf;
99         struct uhci_td *td;
100         int i, nactive, ninactive;
101
102         if (len < 200)
103                 return 0;
104
105         out += sprintf(out, "urb_priv [%p] ", urbp);
106         out += sprintf(out, "urb [%p] ", urbp->urb);
107         out += sprintf(out, "qh [%p] ", urbp->qh);
108         out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
109         out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe),
110                         (usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
111
112         switch (usb_pipetype(urbp->urb->pipe)) {
113         case PIPE_ISOCHRONOUS: out += sprintf(out, "ISO"); break;
114         case PIPE_INTERRUPT: out += sprintf(out, "INT"); break;
115         case PIPE_BULK: out += sprintf(out, "BLK"); break;
116         case PIPE_CONTROL: out += sprintf(out, "CTL"); break;
117         }
118
119         out += sprintf(out, "%s", (urbp->fsbr ? " FSBR" : ""));
120
121         if (urbp->urb->status != -EINPROGRESS)
122                 out += sprintf(out, " Status=%d", urbp->urb->status);
123         out += sprintf(out, "\n");
124
125         i = nactive = ninactive = 0;
126         list_for_each_entry(td, &urbp->td_list, list) {
127                 if (++i <= 10 || debug > 2) {
128                         out += sprintf(out, "%*s%d: ", space + 2, "", i);
129                         out += uhci_show_td(td, out, len - (out - buf), 0);
130                 } else {
131                         if (td_status(td) & TD_CTRL_ACTIVE)
132                                 ++nactive;
133                         else
134                                 ++ninactive;
135                 }
136         }
137         if (nactive + ninactive > 0)
138                 out += sprintf(out, "%*s[skipped %d inactive and %d active "
139                                 "TDs]\n",
140                                 space, "", ninactive, nactive);
141
142         return out - buf;
143 }
144
145 static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space)
146 {
147         char *out = buf;
148         int i, nurbs;
149         __le32 element = qh_element(qh);
150
151         /* Try to make sure there's enough memory */
152         if (len < 80 * 6)
153                 return 0;
154
155         out += sprintf(out, "%*s[%p] link (%08x) element (%08x)\n", space, "",
156                         qh, le32_to_cpu(qh->link), le32_to_cpu(element));
157
158         if (element & UHCI_PTR_QH)
159                 out += sprintf(out, "%*s  Element points to QH (bug?)\n", space, "");
160
161         if (element & UHCI_PTR_DEPTH)
162                 out += sprintf(out, "%*s  Depth traverse\n", space, "");
163
164         if (element & cpu_to_le32(8))
165                 out += sprintf(out, "%*s  Bit 3 set (bug?)\n", space, "");
166
167         if (!(element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH)))
168                 out += sprintf(out, "%*s  Element is NULL (bug?)\n", space, "");
169
170         if (list_empty(&qh->queue)) {
171                 out += sprintf(out, "%*s  queue is empty\n", space, "");
172         } else {
173                 struct urb_priv *urbp = list_entry(qh->queue.next,
174                                 struct urb_priv, node);
175                 struct uhci_td *td = list_entry(urbp->td_list.next,
176                                 struct uhci_td, list);
177
178                 if (cpu_to_le32(td->dma_handle) != (element & ~UHCI_PTR_BITS))
179                         out += sprintf(out, "%*s Element != First TD\n",
180                                         space, "");
181                 i = nurbs = 0;
182                 list_for_each_entry(urbp, &qh->queue, node) {
183                         if (++i <= 10)
184                                 out += uhci_show_urbp(urbp, out,
185                                                 len - (out - buf), space + 2);
186                         else
187                                 ++nurbs;
188                 }
189                 if (nurbs > 0)
190                         out += sprintf(out, "%*s Skipped %d URBs\n",
191                                         space, "", nurbs);
192         }
193
194         if (qh->udev) {
195                 out += sprintf(out, "%*s  Dummy TD\n", space, "");
196                 out += uhci_show_td(qh->dummy_td, out, len - (out - buf), 0);
197         }
198
199         return out - buf;
200 }
201
202 static const char * const qh_names[] = {
203   "skel_unlink_qh", "skel_iso_qh",
204   "skel_int128_qh", "skel_int64_qh",
205   "skel_int32_qh", "skel_int16_qh",
206   "skel_int8_qh", "skel_int4_qh",
207   "skel_int2_qh", "skel_int1_qh",
208   "skel_ls_control_qh", "skel_fs_control_qh",
209   "skel_bulk_qh", "skel_term_qh"
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_root_hub_state(struct uhci_hcd *uhci, char *buf, int len)
238 {
239         char *out = buf;
240         char *rh_state;
241
242         /* Try to make sure there's enough memory */
243         if (len < 60)
244                 return 0;
245
246         switch (uhci->rh_state) {
247             case UHCI_RH_RESET:
248                 rh_state = "reset";             break;
249             case UHCI_RH_SUSPENDED:
250                 rh_state = "suspended";         break;
251             case UHCI_RH_AUTO_STOPPED:
252                 rh_state = "auto-stopped";      break;
253             case UHCI_RH_RESUMING:
254                 rh_state = "resuming";          break;
255             case UHCI_RH_SUSPENDING:
256                 rh_state = "suspending";        break;
257             case UHCI_RH_RUNNING:
258                 rh_state = "running";           break;
259             case UHCI_RH_RUNNING_NODEVS:
260                 rh_state = "running, no devs";  break;
261             default:
262                 rh_state = "?";                 break;
263         }
264         out += sprintf(out, "Root-hub state: %s\n", rh_state);
265         return out - buf;
266 }
267
268 static int uhci_show_status(struct uhci_hcd *uhci, char *buf, int len)
269 {
270         char *out = buf;
271         unsigned long io_addr = uhci->io_addr;
272         unsigned short usbcmd, usbstat, usbint, usbfrnum;
273         unsigned int flbaseadd;
274         unsigned char sof;
275         unsigned short portsc1, portsc2;
276
277         /* Try to make sure there's enough memory */
278         if (len < 80 * 6)
279                 return 0;
280
281         usbcmd    = inw(io_addr + 0);
282         usbstat   = inw(io_addr + 2);
283         usbint    = inw(io_addr + 4);
284         usbfrnum  = inw(io_addr + 6);
285         flbaseadd = inl(io_addr + 8);
286         sof       = inb(io_addr + 12);
287         portsc1   = inw(io_addr + 16);
288         portsc2   = inw(io_addr + 18);
289
290         out += sprintf(out, "  usbcmd    =     %04x   %s%s%s%s%s%s%s%s\n",
291                 usbcmd,
292                 (usbcmd & USBCMD_MAXP) ?    "Maxp64 " : "Maxp32 ",
293                 (usbcmd & USBCMD_CF) ?      "CF " : "",
294                 (usbcmd & USBCMD_SWDBG) ?   "SWDBG " : "",
295                 (usbcmd & USBCMD_FGR) ?     "FGR " : "",
296                 (usbcmd & USBCMD_EGSM) ?    "EGSM " : "",
297                 (usbcmd & USBCMD_GRESET) ?  "GRESET " : "",
298                 (usbcmd & USBCMD_HCRESET) ? "HCRESET " : "",
299                 (usbcmd & USBCMD_RS) ?      "RS " : "");
300
301         out += sprintf(out, "  usbstat   =     %04x   %s%s%s%s%s%s\n",
302                 usbstat,
303                 (usbstat & USBSTS_HCH) ?    "HCHalted " : "",
304                 (usbstat & USBSTS_HCPE) ?   "HostControllerProcessError " : "",
305                 (usbstat & USBSTS_HSE) ?    "HostSystemError " : "",
306                 (usbstat & USBSTS_RD) ?     "ResumeDetect " : "",
307                 (usbstat & USBSTS_ERROR) ?  "USBError " : "",
308                 (usbstat & USBSTS_USBINT) ? "USBINT " : "");
309
310         out += sprintf(out, "  usbint    =     %04x\n", usbint);
311         out += sprintf(out, "  usbfrnum  =   (%d)%03x\n", (usbfrnum >> 10) & 1,
312                 0xfff & (4*(unsigned int)usbfrnum));
313         out += sprintf(out, "  flbaseadd = %08x\n", flbaseadd);
314         out += sprintf(out, "  sof       =       %02x\n", sof);
315         out += uhci_show_sc(1, portsc1, out, len - (out - buf));
316         out += uhci_show_sc(2, portsc2, out, len - (out - buf));
317
318         return out - buf;
319 }
320
321 static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
322 {
323         char *out = buf;
324         int i, j;
325         struct uhci_qh *qh;
326         struct uhci_td *td;
327         struct list_head *tmp, *head;
328
329         out += uhci_show_root_hub_state(uhci, out, len - (out - buf));
330         out += sprintf(out, "HC status\n");
331         out += uhci_show_status(uhci, out, len - (out - buf));
332         if (debug <= 1)
333                 return out - buf;
334
335         out += sprintf(out, "Frame List\n");
336         for (i = 0; i < UHCI_NUMFRAMES; ++i) {
337                 td = uhci->frame_cpu[i];
338                 if (!td)
339                         continue;
340
341                 out += sprintf(out, "- Frame %d\n", i); \
342                 if (td->dma_handle != (dma_addr_t)uhci->frame[i])
343                         out += sprintf(out, "    frame list does not match td->dma_handle!\n");
344
345                 head = &td->fl_list;
346                 tmp = head;
347                 do {
348                         td = list_entry(tmp, struct uhci_td, fl_list);
349                         tmp = tmp->next;
350                         out += uhci_show_td(td, out, len - (out - buf), 4);
351                 } while (tmp != head);
352         }
353
354         out += sprintf(out, "Skeleton QHs\n");
355
356         for (i = 0; i < UHCI_NUM_SKELQH; ++i) {
357                 int cnt = 0;
358
359                 qh = uhci->skelqh[i];
360                 out += sprintf(out, "- %s\n", qh_names[i]); \
361                 out += uhci_show_qh(qh, out, len - (out - buf), 4);
362
363                 /* Last QH is the Terminating QH, it's different */
364                 if (i == UHCI_NUM_SKELQH - 1) {
365                         if (qh->link != UHCI_PTR_TERM)
366                                 out += sprintf(out, "    bandwidth reclamation on!\n");
367
368                         if (qh_element(qh) != cpu_to_le32(uhci->term_td->dma_handle))
369                                 out += sprintf(out, "    skel_term_qh element is not set to term_td!\n");
370
371                         continue;
372                 }
373
374                 j = (i < 9) ? 9 : i+1;          /* Next skeleton */
375                 head = &qh->node;
376                 tmp = head->next;
377
378                 while (tmp != head) {
379                         qh = list_entry(tmp, struct uhci_qh, node);
380                         tmp = tmp->next;
381                         if (++cnt <= 10)
382                                 out += uhci_show_qh(qh, out,
383                                                 len - (out - buf), 4);
384                 }
385                 if ((cnt -= 10) > 0)
386                         out += sprintf(out, "    Skipped %d QHs\n", cnt);
387
388                 if (i > 1 && i < UHCI_NUM_SKELQH - 1) {
389                         if (qh->link !=
390                             (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH))
391                                 out += sprintf(out, "    last QH not linked to next skeleton!\n");
392                 }
393         }
394
395         return out - buf;
396 }
397
398 #ifdef CONFIG_DEBUG_FS
399
400 #define MAX_OUTPUT      (64 * 1024)
401
402 struct uhci_debug {
403         int size;
404         char *data;
405 };
406
407 static int uhci_debug_open(struct inode *inode, struct file *file)
408 {
409         struct uhci_hcd *uhci = inode->u.generic_ip;
410         struct uhci_debug *up;
411         int ret = -ENOMEM;
412         unsigned long flags;
413
414         lock_kernel();
415         up = kmalloc(sizeof(*up), GFP_KERNEL);
416         if (!up)
417                 goto out;
418
419         up->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
420         if (!up->data) {
421                 kfree(up);
422                 goto out;
423         }
424
425         up->size = 0;
426         spin_lock_irqsave(&uhci->lock, flags);
427         if (uhci->is_initialized)
428                 up->size = uhci_sprint_schedule(uhci, up->data, MAX_OUTPUT);
429         spin_unlock_irqrestore(&uhci->lock, flags);
430
431         file->private_data = up;
432
433         ret = 0;
434 out:
435         unlock_kernel();
436         return ret;
437 }
438
439 static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence)
440 {
441         struct uhci_debug *up;
442         loff_t new = -1;
443
444         lock_kernel();
445         up = file->private_data;
446
447         switch (whence) {
448         case 0:
449                 new = off;
450                 break;
451         case 1:
452                 new = file->f_pos + off;
453                 break;
454         }
455         if (new < 0 || new > up->size) {
456                 unlock_kernel();
457                 return -EINVAL;
458         }
459         unlock_kernel();
460         return (file->f_pos = new);
461 }
462
463 static ssize_t uhci_debug_read(struct file *file, char __user *buf,
464                                 size_t nbytes, loff_t *ppos)
465 {
466         struct uhci_debug *up = file->private_data;
467         return simple_read_from_buffer(buf, nbytes, ppos, up->data, up->size);
468 }
469
470 static int uhci_debug_release(struct inode *inode, struct file *file)
471 {
472         struct uhci_debug *up = file->private_data;
473
474         kfree(up->data);
475         kfree(up);
476
477         return 0;
478 }
479
480 #undef uhci_debug_operations
481 static struct file_operations uhci_debug_operations = {
482         .owner =        THIS_MODULE,
483         .open =         uhci_debug_open,
484         .llseek =       uhci_debug_lseek,
485         .read =         uhci_debug_read,
486         .release =      uhci_debug_release,
487 };
488
489 #endif  /* CONFIG_DEBUG_FS */
490
491 #else   /* DEBUG */
492
493 static inline void lprintk(char *buf)
494 {}
495
496 static inline int uhci_show_qh(struct uhci_qh *qh, char *buf,
497                 int len, int space)
498 {
499         return 0;
500 }
501
502 static inline int uhci_sprint_schedule(struct uhci_hcd *uhci,
503                 char *buf, int len)
504 {
505         return 0;
506 }
507
508 #endif