uhci-debug.c 15 KB

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