uhci-debug.c 15 KB

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