uhci-debug.c 15 KB

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