uhci-debug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #include <linux/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/smp_lock.h>
  15. #include <asm/io.h>
  16. #include "uhci-hcd.h"
  17. #define uhci_debug_operations (* (struct file_operations *) NULL)
  18. static struct dentry *uhci_debugfs_root;
  19. #ifdef DEBUG
  20. /* Handle REALLY large printks so we don't overflow buffers */
  21. static void lprintk(char *buf)
  22. {
  23. char *p;
  24. /* Just write one line at a time */
  25. while (buf) {
  26. p = strchr(buf, '\n');
  27. if (p)
  28. *p = 0;
  29. printk(KERN_DEBUG "%s\n", buf);
  30. buf = p;
  31. if (buf)
  32. buf++;
  33. }
  34. }
  35. static int uhci_show_td(struct uhci_td *td, char *buf, int len, int space)
  36. {
  37. char *out = buf;
  38. char *spid;
  39. u32 status, token;
  40. /* Try to make sure there's enough memory */
  41. if (len < 160)
  42. return 0;
  43. status = td_status(td);
  44. out += sprintf(out, "%*s[%p] link (%08x) ", space, "", td, le32_to_cpu(td->link));
  45. out += sprintf(out, "e%d %s%s%s%s%s%s%s%s%s%sLength=%x ",
  46. ((status >> 27) & 3),
  47. (status & TD_CTRL_SPD) ? "SPD " : "",
  48. (status & TD_CTRL_LS) ? "LS " : "",
  49. (status & TD_CTRL_IOC) ? "IOC " : "",
  50. (status & TD_CTRL_ACTIVE) ? "Active " : "",
  51. (status & TD_CTRL_STALLED) ? "Stalled " : "",
  52. (status & TD_CTRL_DBUFERR) ? "DataBufErr " : "",
  53. (status & TD_CTRL_BABBLE) ? "Babble " : "",
  54. (status & TD_CTRL_NAK) ? "NAK " : "",
  55. (status & TD_CTRL_CRCTIMEO) ? "CRC/Timeo " : "",
  56. (status & TD_CTRL_BITSTUFF) ? "BitStuff " : "",
  57. status & 0x7ff);
  58. token = td_token(td);
  59. switch (uhci_packetid(token)) {
  60. case USB_PID_SETUP:
  61. spid = "SETUP";
  62. break;
  63. case USB_PID_OUT:
  64. spid = "OUT";
  65. break;
  66. case USB_PID_IN:
  67. spid = "IN";
  68. break;
  69. default:
  70. spid = "?";
  71. break;
  72. }
  73. out += sprintf(out, "MaxLen=%x DT%d EndPt=%x Dev=%x, PID=%x(%s) ",
  74. token >> 21,
  75. ((token >> 19) & 1),
  76. (token >> 15) & 15,
  77. (token >> 8) & 127,
  78. (token & 0xff),
  79. spid);
  80. out += sprintf(out, "(buf=%08x)\n", le32_to_cpu(td->buffer));
  81. return out - buf;
  82. }
  83. static int uhci_show_urbp(struct urb_priv *urbp, char *buf, int len, int space)
  84. {
  85. char *out = buf;
  86. struct uhci_td *td;
  87. int i, nactive, ninactive;
  88. char *ptype;
  89. if (len < 200)
  90. return 0;
  91. out += sprintf(out, "urb_priv [%p] ", urbp);
  92. out += sprintf(out, "urb [%p] ", urbp->urb);
  93. out += sprintf(out, "qh [%p] ", urbp->qh);
  94. out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
  95. out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe),
  96. (usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
  97. switch (usb_pipetype(urbp->urb->pipe)) {
  98. case PIPE_ISOCHRONOUS: ptype = "ISO"; break;
  99. case PIPE_INTERRUPT: ptype = "INT"; break;
  100. case PIPE_BULK: ptype = "BLK"; break;
  101. default:
  102. case PIPE_CONTROL: ptype = "CTL"; break;
  103. }
  104. out += sprintf(out, "%s%s", ptype, (urbp->fsbr ? " FSBR" : ""));
  105. out += sprintf(out, " Actlen=%d", urbp->urb->actual_length);
  106. if (urbp->urb->status != -EINPROGRESS)
  107. out += sprintf(out, " Status=%d", urbp->urb->status);
  108. out += sprintf(out, "\n");
  109. i = nactive = ninactive = 0;
  110. list_for_each_entry(td, &urbp->td_list, list) {
  111. if (++i <= 10 || debug > 2) {
  112. out += sprintf(out, "%*s%d: ", space + 2, "", i);
  113. out += uhci_show_td(td, out, len - (out - buf), 0);
  114. } else {
  115. if (td_status(td) & TD_CTRL_ACTIVE)
  116. ++nactive;
  117. else
  118. ++ninactive;
  119. }
  120. }
  121. if (nactive + ninactive > 0)
  122. out += sprintf(out, "%*s[skipped %d inactive and %d active "
  123. "TDs]\n",
  124. space, "", ninactive, nactive);
  125. return out - buf;
  126. }
  127. static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space)
  128. {
  129. char *out = buf;
  130. int i, nurbs;
  131. __le32 element = qh_element(qh);
  132. char *qtype;
  133. /* Try to make sure there's enough memory */
  134. if (len < 80 * 6)
  135. return 0;
  136. switch (qh->type) {
  137. case USB_ENDPOINT_XFER_ISOC: qtype = "ISO"; break;
  138. case USB_ENDPOINT_XFER_INT: qtype = "INT"; break;
  139. case USB_ENDPOINT_XFER_BULK: qtype = "BLK"; break;
  140. case USB_ENDPOINT_XFER_CONTROL: qtype = "CTL"; break;
  141. default: qtype = "Skel" ; break;
  142. }
  143. out += sprintf(out, "%*s[%p] %s QH link (%08x) element (%08x)\n",
  144. space, "", qh, qtype,
  145. le32_to_cpu(qh->link), le32_to_cpu(element));
  146. if (element & UHCI_PTR_QH)
  147. out += sprintf(out, "%*s Element points to QH (bug?)\n", space, "");
  148. if (element & UHCI_PTR_DEPTH)
  149. out += sprintf(out, "%*s Depth traverse\n", space, "");
  150. if (element & cpu_to_le32(8))
  151. out += sprintf(out, "%*s Bit 3 set (bug?)\n", space, "");
  152. if (!(element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH)))
  153. out += sprintf(out, "%*s Element is NULL (bug?)\n", space, "");
  154. if (list_empty(&qh->queue)) {
  155. out += sprintf(out, "%*s queue is empty\n", space, "");
  156. } else {
  157. struct urb_priv *urbp = list_entry(qh->queue.next,
  158. struct urb_priv, node);
  159. struct uhci_td *td = list_entry(urbp->td_list.next,
  160. struct uhci_td, list);
  161. if (cpu_to_le32(td->dma_handle) != (element & ~UHCI_PTR_BITS))
  162. out += sprintf(out, "%*s Element != First TD\n",
  163. space, "");
  164. i = nurbs = 0;
  165. list_for_each_entry(urbp, &qh->queue, node) {
  166. if (++i <= 10)
  167. out += uhci_show_urbp(urbp, out,
  168. len - (out - buf), space + 2);
  169. else
  170. ++nurbs;
  171. }
  172. if (nurbs > 0)
  173. out += sprintf(out, "%*s Skipped %d URBs\n",
  174. space, "", nurbs);
  175. }
  176. if (qh->udev) {
  177. out += sprintf(out, "%*s Dummy TD\n", space, "");
  178. out += uhci_show_td(qh->dummy_td, out, len - (out - buf), 0);
  179. }
  180. return out - buf;
  181. }
  182. static const char * const qh_names[] = {
  183. "skel_unlink_qh", "skel_iso_qh",
  184. "skel_int128_qh", "skel_int64_qh",
  185. "skel_int32_qh", "skel_int16_qh",
  186. "skel_int8_qh", "skel_int4_qh",
  187. "skel_int2_qh", "skel_int1_qh",
  188. "skel_ls_control_qh", "skel_fs_control_qh",
  189. "skel_bulk_qh", "skel_term_qh"
  190. };
  191. static int uhci_show_sc(int port, unsigned short status, char *buf, int len)
  192. {
  193. char *out = buf;
  194. /* Try to make sure there's enough memory */
  195. if (len < 160)
  196. return 0;
  197. out += sprintf(out, " stat%d = %04x %s%s%s%s%s%s%s%s%s%s\n",
  198. port,
  199. status,
  200. (status & USBPORTSC_SUSP) ? " Suspend" : "",
  201. (status & USBPORTSC_OCC) ? " OverCurrentChange" : "",
  202. (status & USBPORTSC_OC) ? " OverCurrent" : "",
  203. (status & USBPORTSC_PR) ? " Reset" : "",
  204. (status & USBPORTSC_LSDA) ? " LowSpeed" : "",
  205. (status & USBPORTSC_RD) ? " ResumeDetect" : "",
  206. (status & USBPORTSC_PEC) ? " EnableChange" : "",
  207. (status & USBPORTSC_PE) ? " Enabled" : "",
  208. (status & USBPORTSC_CSC) ? " ConnectChange" : "",
  209. (status & USBPORTSC_CCS) ? " Connected" : "");
  210. return out - buf;
  211. }
  212. static int uhci_show_root_hub_state(struct uhci_hcd *uhci, char *buf, int len)
  213. {
  214. char *out = buf;
  215. char *rh_state;
  216. /* Try to make sure there's enough memory */
  217. if (len < 60)
  218. return 0;
  219. switch (uhci->rh_state) {
  220. case UHCI_RH_RESET:
  221. rh_state = "reset"; break;
  222. case UHCI_RH_SUSPENDED:
  223. rh_state = "suspended"; break;
  224. case UHCI_RH_AUTO_STOPPED:
  225. rh_state = "auto-stopped"; break;
  226. case UHCI_RH_RESUMING:
  227. rh_state = "resuming"; break;
  228. case UHCI_RH_SUSPENDING:
  229. rh_state = "suspending"; break;
  230. case UHCI_RH_RUNNING:
  231. rh_state = "running"; break;
  232. case UHCI_RH_RUNNING_NODEVS:
  233. rh_state = "running, no devs"; break;
  234. default:
  235. rh_state = "?"; break;
  236. }
  237. out += sprintf(out, "Root-hub state: %s\n", rh_state);
  238. return out - buf;
  239. }
  240. static int uhci_show_status(struct uhci_hcd *uhci, char *buf, int len)
  241. {
  242. char *out = buf;
  243. unsigned long io_addr = uhci->io_addr;
  244. unsigned short usbcmd, usbstat, usbint, usbfrnum;
  245. unsigned int flbaseadd;
  246. unsigned char sof;
  247. unsigned short portsc1, portsc2;
  248. /* Try to make sure there's enough memory */
  249. if (len < 80 * 6)
  250. return 0;
  251. usbcmd = inw(io_addr + 0);
  252. usbstat = inw(io_addr + 2);
  253. usbint = inw(io_addr + 4);
  254. usbfrnum = inw(io_addr + 6);
  255. flbaseadd = inl(io_addr + 8);
  256. sof = inb(io_addr + 12);
  257. portsc1 = inw(io_addr + 16);
  258. portsc2 = inw(io_addr + 18);
  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. out += sprintf(out, " usbstat = %04x %s%s%s%s%s%s\n",
  270. usbstat,
  271. (usbstat & USBSTS_HCH) ? "HCHalted " : "",
  272. (usbstat & USBSTS_HCPE) ? "HostControllerProcessError " : "",
  273. (usbstat & USBSTS_HSE) ? "HostSystemError " : "",
  274. (usbstat & USBSTS_RD) ? "ResumeDetect " : "",
  275. (usbstat & USBSTS_ERROR) ? "USBError " : "",
  276. (usbstat & USBSTS_USBINT) ? "USBINT " : "");
  277. out += sprintf(out, " usbint = %04x\n", usbint);
  278. out += sprintf(out, " usbfrnum = (%d)%03x\n", (usbfrnum >> 10) & 1,
  279. 0xfff & (4*(unsigned int)usbfrnum));
  280. out += sprintf(out, " flbaseadd = %08x\n", flbaseadd);
  281. out += sprintf(out, " sof = %02x\n", sof);
  282. out += uhci_show_sc(1, portsc1, out, len - (out - buf));
  283. out += uhci_show_sc(2, portsc2, out, len - (out - buf));
  284. return out - buf;
  285. }
  286. static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
  287. {
  288. char *out = buf;
  289. int i, j;
  290. struct uhci_qh *qh;
  291. struct uhci_td *td;
  292. struct list_head *tmp, *head;
  293. out += uhci_show_root_hub_state(uhci, out, len - (out - buf));
  294. out += sprintf(out, "HC status\n");
  295. out += uhci_show_status(uhci, out, len - (out - buf));
  296. if (debug <= 1)
  297. return out - buf;
  298. out += sprintf(out, "Frame List\n");
  299. for (i = 0; i < UHCI_NUMFRAMES; ++i) {
  300. td = uhci->frame_cpu[i];
  301. if (!td)
  302. continue;
  303. out += sprintf(out, "- Frame %d\n", i); \
  304. if (td->dma_handle != (dma_addr_t)uhci->frame[i])
  305. out += sprintf(out, " frame list does not match td->dma_handle!\n");
  306. head = &td->fl_list;
  307. tmp = head;
  308. do {
  309. td = list_entry(tmp, struct uhci_td, fl_list);
  310. tmp = tmp->next;
  311. out += uhci_show_td(td, out, len - (out - buf), 4);
  312. } while (tmp != head);
  313. }
  314. out += sprintf(out, "Skeleton QHs\n");
  315. for (i = 0; i < UHCI_NUM_SKELQH; ++i) {
  316. int cnt = 0;
  317. qh = uhci->skelqh[i];
  318. out += sprintf(out, "- %s\n", qh_names[i]); \
  319. out += uhci_show_qh(qh, out, len - (out - buf), 4);
  320. /* Last QH is the Terminating QH, it's different */
  321. if (i == UHCI_NUM_SKELQH - 1) {
  322. if (qh->link != UHCI_PTR_TERM)
  323. out += sprintf(out, " bandwidth reclamation on!\n");
  324. if (qh_element(qh) != cpu_to_le32(uhci->term_td->dma_handle))
  325. out += sprintf(out, " skel_term_qh element is not set to term_td!\n");
  326. continue;
  327. }
  328. j = (i < 9) ? 9 : i+1; /* Next skeleton */
  329. head = &qh->node;
  330. tmp = head->next;
  331. while (tmp != head) {
  332. qh = list_entry(tmp, struct uhci_qh, node);
  333. tmp = tmp->next;
  334. if (++cnt <= 10)
  335. out += uhci_show_qh(qh, out,
  336. len - (out - buf), 4);
  337. }
  338. if ((cnt -= 10) > 0)
  339. out += sprintf(out, " Skipped %d QHs\n", cnt);
  340. if (i > 1 && i < UHCI_NUM_SKELQH - 1) {
  341. if (qh->link !=
  342. (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH))
  343. out += sprintf(out, " last QH not linked to next skeleton!\n");
  344. }
  345. }
  346. return out - buf;
  347. }
  348. #ifdef CONFIG_DEBUG_FS
  349. #define MAX_OUTPUT (64 * 1024)
  350. struct uhci_debug {
  351. int size;
  352. char *data;
  353. };
  354. static int uhci_debug_open(struct inode *inode, struct file *file)
  355. {
  356. struct uhci_hcd *uhci = inode->u.generic_ip;
  357. struct uhci_debug *up;
  358. int ret = -ENOMEM;
  359. unsigned long flags;
  360. lock_kernel();
  361. up = kmalloc(sizeof(*up), GFP_KERNEL);
  362. if (!up)
  363. goto out;
  364. up->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
  365. if (!up->data) {
  366. kfree(up);
  367. goto out;
  368. }
  369. up->size = 0;
  370. spin_lock_irqsave(&uhci->lock, flags);
  371. if (uhci->is_initialized)
  372. up->size = uhci_sprint_schedule(uhci, up->data, MAX_OUTPUT);
  373. spin_unlock_irqrestore(&uhci->lock, flags);
  374. file->private_data = up;
  375. ret = 0;
  376. out:
  377. unlock_kernel();
  378. return ret;
  379. }
  380. static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence)
  381. {
  382. struct uhci_debug *up;
  383. loff_t new = -1;
  384. lock_kernel();
  385. up = file->private_data;
  386. switch (whence) {
  387. case 0:
  388. new = off;
  389. break;
  390. case 1:
  391. new = file->f_pos + off;
  392. break;
  393. }
  394. if (new < 0 || new > up->size) {
  395. unlock_kernel();
  396. return -EINVAL;
  397. }
  398. unlock_kernel();
  399. return (file->f_pos = new);
  400. }
  401. static ssize_t uhci_debug_read(struct file *file, char __user *buf,
  402. size_t nbytes, loff_t *ppos)
  403. {
  404. struct uhci_debug *up = file->private_data;
  405. return simple_read_from_buffer(buf, nbytes, ppos, up->data, up->size);
  406. }
  407. static int uhci_debug_release(struct inode *inode, struct file *file)
  408. {
  409. struct uhci_debug *up = file->private_data;
  410. kfree(up->data);
  411. kfree(up);
  412. return 0;
  413. }
  414. #undef uhci_debug_operations
  415. static struct file_operations uhci_debug_operations = {
  416. .owner = THIS_MODULE,
  417. .open = uhci_debug_open,
  418. .llseek = uhci_debug_lseek,
  419. .read = uhci_debug_read,
  420. .release = uhci_debug_release,
  421. };
  422. #endif /* CONFIG_DEBUG_FS */
  423. #else /* DEBUG */
  424. static inline void lprintk(char *buf)
  425. {}
  426. static inline int uhci_show_qh(struct uhci_qh *qh, char *buf,
  427. int len, int space)
  428. {
  429. return 0;
  430. }
  431. static inline int uhci_sprint_schedule(struct uhci_hcd *uhci,
  432. char *buf, int len)
  433. {
  434. return 0;
  435. }
  436. #endif