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