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