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