uhci-debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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", urbp->urb->actual_length);
  105. if (urbp->urb->status != -EINPROGRESS)
  106. out += sprintf(out, " Status=%d", urbp->urb->status);
  107. out += sprintf(out, "\n");
  108. i = nactive = ninactive = 0;
  109. list_for_each_entry(td, &urbp->td_list, list) {
  110. if (urbp->qh->type != USB_ENDPOINT_XFER_ISOC &&
  111. (++i <= 10 || debug > 2)) {
  112. out += sprintf(out, "%*s%d: ", space + 2, "", i);
  113. out += uhci_show_td(td, out, len - (out - buf), 0);
  114. } else {
  115. if (td_status(td) & TD_CTRL_ACTIVE)
  116. ++nactive;
  117. else
  118. ++ninactive;
  119. }
  120. }
  121. if (nactive + ninactive > 0)
  122. out += sprintf(out, "%*s[skipped %d inactive and %d active "
  123. "TDs]\n",
  124. space, "", ninactive, nactive);
  125. return out - buf;
  126. }
  127. static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space)
  128. {
  129. char *out = buf;
  130. int i, nurbs;
  131. __le32 element = qh_element(qh);
  132. char *qtype;
  133. /* Try to make sure there's enough memory */
  134. if (len < 80 * 7)
  135. return 0;
  136. switch (qh->type) {
  137. case USB_ENDPOINT_XFER_ISOC: qtype = "ISO"; break;
  138. case USB_ENDPOINT_XFER_INT: qtype = "INT"; break;
  139. case USB_ENDPOINT_XFER_BULK: qtype = "BLK"; break;
  140. case USB_ENDPOINT_XFER_CONTROL: qtype = "CTL"; break;
  141. default: qtype = "Skel" ; break;
  142. }
  143. out += sprintf(out, "%*s[%p] %s QH link (%08x) element (%08x)\n",
  144. space, "", qh, qtype,
  145. le32_to_cpu(qh->link), le32_to_cpu(element));
  146. if (qh->type == USB_ENDPOINT_XFER_ISOC)
  147. out += sprintf(out, "%*s period %d frame %x desc [%p]\n",
  148. space, "", qh->period, qh->iso_frame,
  149. qh->iso_packet_desc);
  150. if (element & UHCI_PTR_QH)
  151. out += sprintf(out, "%*s Element points to QH (bug?)\n", space, "");
  152. if (element & UHCI_PTR_DEPTH)
  153. out += sprintf(out, "%*s Depth traverse\n", space, "");
  154. if (element & cpu_to_le32(8))
  155. out += sprintf(out, "%*s Bit 3 set (bug?)\n", space, "");
  156. if (!(element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH)))
  157. out += sprintf(out, "%*s Element is NULL (bug?)\n", space, "");
  158. if (list_empty(&qh->queue)) {
  159. out += sprintf(out, "%*s queue is empty\n", space, "");
  160. } else {
  161. struct urb_priv *urbp = list_entry(qh->queue.next,
  162. struct urb_priv, node);
  163. struct uhci_td *td = list_entry(urbp->td_list.next,
  164. struct uhci_td, list);
  165. if (cpu_to_le32(td->dma_handle) != (element & ~UHCI_PTR_BITS))
  166. out += sprintf(out, "%*s Element != First TD\n",
  167. space, "");
  168. i = nurbs = 0;
  169. list_for_each_entry(urbp, &qh->queue, node) {
  170. if (++i <= 10)
  171. out += uhci_show_urbp(urbp, out,
  172. len - (out - buf), space + 2);
  173. else
  174. ++nurbs;
  175. }
  176. if (nurbs > 0)
  177. out += sprintf(out, "%*s Skipped %d URBs\n",
  178. space, "", nurbs);
  179. }
  180. if (qh->dummy_td) {
  181. out += sprintf(out, "%*s Dummy TD\n", space, "");
  182. out += uhci_show_td(qh->dummy_td, out, len - (out - buf), 0);
  183. }
  184. return out - buf;
  185. }
  186. static const char * const qh_names[] = {
  187. "skel_unlink_qh", "skel_iso_qh",
  188. "skel_int128_qh", "skel_int64_qh",
  189. "skel_int32_qh", "skel_int16_qh",
  190. "skel_int8_qh", "skel_int4_qh",
  191. "skel_int2_qh", "skel_int1_qh",
  192. "skel_ls_control_qh", "skel_fs_control_qh",
  193. "skel_bulk_qh", "skel_term_qh"
  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. out += uhci_show_root_hub_state(uhci, out, len - (out - buf));
  304. out += sprintf(out, "HC status\n");
  305. out += uhci_show_status(uhci, out, len - (out - buf));
  306. if (debug <= 1)
  307. return out - buf;
  308. out += sprintf(out, "Frame List\n");
  309. nframes = 10;
  310. nerrs = 0;
  311. for (i = 0; i < UHCI_NUMFRAMES; ++i) {
  312. __le32 link, qh_dma;
  313. j = 0;
  314. td = uhci->frame_cpu[i];
  315. link = uhci->frame[i];
  316. if (!td)
  317. goto check_link;
  318. if (nframes > 0) {
  319. out += sprintf(out, "- Frame %d -> (%08x)\n",
  320. i, le32_to_cpu(link));
  321. j = 1;
  322. }
  323. head = &td->fl_list;
  324. tmp = head;
  325. do {
  326. td = list_entry(tmp, struct uhci_td, fl_list);
  327. tmp = tmp->next;
  328. if (cpu_to_le32(td->dma_handle) != link) {
  329. if (nframes > 0)
  330. out += sprintf(out, " link does "
  331. "not match list entry!\n");
  332. else
  333. ++nerrs;
  334. }
  335. if (nframes > 0)
  336. out += uhci_show_td(td, out,
  337. len - (out - buf), 4);
  338. link = td->link;
  339. } while (tmp != head);
  340. check_link:
  341. qh_dma = uhci_frame_skel_link(uhci, i);
  342. if (link != qh_dma) {
  343. if (nframes > 0) {
  344. if (!j) {
  345. out += sprintf(out,
  346. "- Frame %d -> (%08x)\n",
  347. i, le32_to_cpu(link));
  348. j = 1;
  349. }
  350. out += sprintf(out, " link does not match "
  351. "QH (%08x)!\n", le32_to_cpu(qh_dma));
  352. } else
  353. ++nerrs;
  354. }
  355. nframes -= j;
  356. }
  357. if (nerrs > 0)
  358. out += sprintf(out, "Skipped %d bad links\n", nerrs);
  359. out += sprintf(out, "Skeleton QHs\n");
  360. for (i = 0; i < UHCI_NUM_SKELQH; ++i) {
  361. int cnt = 0;
  362. qh = uhci->skelqh[i];
  363. out += sprintf(out, "- %s\n", qh_names[i]); \
  364. out += uhci_show_qh(qh, out, len - (out - buf), 4);
  365. /* Last QH is the Terminating QH, it's different */
  366. if (i == UHCI_NUM_SKELQH - 1) {
  367. if (qh->link != UHCI_PTR_TERM)
  368. out += sprintf(out, " bandwidth reclamation on!\n");
  369. if (qh_element(qh) != cpu_to_le32(uhci->term_td->dma_handle))
  370. out += sprintf(out, " skel_term_qh element is not set to term_td!\n");
  371. continue;
  372. }
  373. j = (i < 9) ? 9 : i+1; /* Next skeleton */
  374. head = &qh->node;
  375. tmp = head->next;
  376. while (tmp != head) {
  377. qh = list_entry(tmp, struct uhci_qh, node);
  378. tmp = tmp->next;
  379. if (++cnt <= 10)
  380. out += uhci_show_qh(qh, out,
  381. len - (out - buf), 4);
  382. }
  383. if ((cnt -= 10) > 0)
  384. out += sprintf(out, " Skipped %d QHs\n", cnt);
  385. if (i > 1 && i < UHCI_NUM_SKELQH - 1) {
  386. if (qh->link !=
  387. (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH))
  388. out += sprintf(out, " last QH not linked to next skeleton!\n");
  389. }
  390. }
  391. return out - buf;
  392. }
  393. #ifdef CONFIG_DEBUG_FS
  394. #define MAX_OUTPUT (64 * 1024)
  395. struct uhci_debug {
  396. int size;
  397. char *data;
  398. };
  399. static int uhci_debug_open(struct inode *inode, struct file *file)
  400. {
  401. struct uhci_hcd *uhci = inode->i_private;
  402. struct uhci_debug *up;
  403. int ret = -ENOMEM;
  404. unsigned long flags;
  405. lock_kernel();
  406. up = kmalloc(sizeof(*up), GFP_KERNEL);
  407. if (!up)
  408. goto out;
  409. up->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
  410. if (!up->data) {
  411. kfree(up);
  412. goto out;
  413. }
  414. up->size = 0;
  415. spin_lock_irqsave(&uhci->lock, flags);
  416. if (uhci->is_initialized)
  417. up->size = uhci_sprint_schedule(uhci, up->data, MAX_OUTPUT);
  418. spin_unlock_irqrestore(&uhci->lock, flags);
  419. file->private_data = up;
  420. ret = 0;
  421. out:
  422. unlock_kernel();
  423. return ret;
  424. }
  425. static loff_t uhci_debug_lseek(struct file *file, loff_t off, int whence)
  426. {
  427. struct uhci_debug *up;
  428. loff_t new = -1;
  429. lock_kernel();
  430. up = file->private_data;
  431. switch (whence) {
  432. case 0:
  433. new = off;
  434. break;
  435. case 1:
  436. new = file->f_pos + off;
  437. break;
  438. }
  439. if (new < 0 || new > up->size) {
  440. unlock_kernel();
  441. return -EINVAL;
  442. }
  443. unlock_kernel();
  444. return (file->f_pos = new);
  445. }
  446. static ssize_t uhci_debug_read(struct file *file, char __user *buf,
  447. size_t nbytes, loff_t *ppos)
  448. {
  449. struct uhci_debug *up = file->private_data;
  450. return simple_read_from_buffer(buf, nbytes, ppos, up->data, up->size);
  451. }
  452. static int uhci_debug_release(struct inode *inode, struct file *file)
  453. {
  454. struct uhci_debug *up = file->private_data;
  455. kfree(up->data);
  456. kfree(up);
  457. return 0;
  458. }
  459. #undef uhci_debug_operations
  460. static const struct file_operations uhci_debug_operations = {
  461. .owner = THIS_MODULE,
  462. .open = uhci_debug_open,
  463. .llseek = uhci_debug_lseek,
  464. .read = uhci_debug_read,
  465. .release = uhci_debug_release,
  466. };
  467. #endif /* CONFIG_DEBUG_FS */
  468. #else /* DEBUG */
  469. static inline void lprintk(char *buf)
  470. {}
  471. static inline int uhci_show_qh(struct uhci_qh *qh, char *buf,
  472. int len, int space)
  473. {
  474. return 0;
  475. }
  476. static inline int uhci_sprint_schedule(struct uhci_hcd *uhci,
  477. char *buf, int len)
  478. {
  479. return 0;
  480. }
  481. #endif