uhci-debug.c 15 KB

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