uhci-debug.c 12 KB

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