imx21-dbg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (c) 2009 by Martin Fuzzey
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* this file is part of imx21-hcd.c */
  19. #ifndef DEBUG
  20. static inline void create_debug_files(struct imx21 *imx21) { }
  21. static inline void remove_debug_files(struct imx21 *imx21) { }
  22. static inline void debug_urb_submitted(struct imx21 *imx21, struct urb *urb) {}
  23. static inline void debug_urb_completed(struct imx21 *imx21, struct urb *urb,
  24. int status) {}
  25. static inline void debug_urb_unlinked(struct imx21 *imx21, struct urb *urb) {}
  26. static inline void debug_urb_queued_for_etd(struct imx21 *imx21,
  27. struct urb *urb) {}
  28. static inline void debug_urb_queued_for_dmem(struct imx21 *imx21,
  29. struct urb *urb) {}
  30. static inline void debug_etd_allocated(struct imx21 *imx21) {}
  31. static inline void debug_etd_freed(struct imx21 *imx21) {}
  32. static inline void debug_dmem_allocated(struct imx21 *imx21, int size) {}
  33. static inline void debug_dmem_freed(struct imx21 *imx21, int size) {}
  34. static inline void debug_isoc_submitted(struct imx21 *imx21,
  35. int frame, struct td *td) {}
  36. static inline void debug_isoc_completed(struct imx21 *imx21,
  37. int frame, struct td *td, int cc, int len) {}
  38. #else
  39. #include <linux/debugfs.h>
  40. #include <linux/seq_file.h>
  41. static const char *dir_labels[] = {
  42. "TD 0",
  43. "OUT",
  44. "IN",
  45. "TD 1"
  46. };
  47. static const char *speed_labels[] = {
  48. "Full",
  49. "Low"
  50. };
  51. static const char *format_labels[] = {
  52. "Control",
  53. "ISO",
  54. "Bulk",
  55. "Interrupt"
  56. };
  57. static inline struct debug_stats *stats_for_urb(struct imx21 *imx21,
  58. struct urb *urb)
  59. {
  60. return usb_pipeisoc(urb->pipe) ?
  61. &imx21->isoc_stats : &imx21->nonisoc_stats;
  62. }
  63. static void debug_urb_submitted(struct imx21 *imx21, struct urb *urb)
  64. {
  65. stats_for_urb(imx21, urb)->submitted++;
  66. }
  67. static void debug_urb_completed(struct imx21 *imx21, struct urb *urb, int st)
  68. {
  69. if (st)
  70. stats_for_urb(imx21, urb)->completed_failed++;
  71. else
  72. stats_for_urb(imx21, urb)->completed_ok++;
  73. }
  74. static void debug_urb_unlinked(struct imx21 *imx21, struct urb *urb)
  75. {
  76. stats_for_urb(imx21, urb)->unlinked++;
  77. }
  78. static void debug_urb_queued_for_etd(struct imx21 *imx21, struct urb *urb)
  79. {
  80. stats_for_urb(imx21, urb)->queue_etd++;
  81. }
  82. static void debug_urb_queued_for_dmem(struct imx21 *imx21, struct urb *urb)
  83. {
  84. stats_for_urb(imx21, urb)->queue_dmem++;
  85. }
  86. static inline void debug_etd_allocated(struct imx21 *imx21)
  87. {
  88. imx21->etd_usage.maximum = max(
  89. ++(imx21->etd_usage.value),
  90. imx21->etd_usage.maximum);
  91. }
  92. static inline void debug_etd_freed(struct imx21 *imx21)
  93. {
  94. imx21->etd_usage.value--;
  95. }
  96. static inline void debug_dmem_allocated(struct imx21 *imx21, int size)
  97. {
  98. imx21->dmem_usage.value += size;
  99. imx21->dmem_usage.maximum = max(
  100. imx21->dmem_usage.value,
  101. imx21->dmem_usage.maximum);
  102. }
  103. static inline void debug_dmem_freed(struct imx21 *imx21, int size)
  104. {
  105. imx21->dmem_usage.value -= size;
  106. }
  107. static void debug_isoc_submitted(struct imx21 *imx21,
  108. int frame, struct td *td)
  109. {
  110. struct debug_isoc_trace *trace = &imx21->isoc_trace[
  111. imx21->isoc_trace_index++];
  112. imx21->isoc_trace_index %= ARRAY_SIZE(imx21->isoc_trace);
  113. trace->schedule_frame = td->frame;
  114. trace->submit_frame = frame;
  115. trace->request_len = td->len;
  116. trace->td = td;
  117. }
  118. static inline void debug_isoc_completed(struct imx21 *imx21,
  119. int frame, struct td *td, int cc, int len)
  120. {
  121. struct debug_isoc_trace *trace, *trace_failed;
  122. int i;
  123. int found = 0;
  124. trace = imx21->isoc_trace;
  125. for (i = 0; i < ARRAY_SIZE(imx21->isoc_trace); i++, trace++) {
  126. if (trace->td == td) {
  127. trace->done_frame = frame;
  128. trace->done_len = len;
  129. trace->cc = cc;
  130. trace->td = NULL;
  131. found = 1;
  132. break;
  133. }
  134. }
  135. if (found && cc) {
  136. trace_failed = &imx21->isoc_trace_failed[
  137. imx21->isoc_trace_index_failed++];
  138. imx21->isoc_trace_index_failed %= ARRAY_SIZE(
  139. imx21->isoc_trace_failed);
  140. *trace_failed = *trace;
  141. }
  142. }
  143. static char *format_ep(struct usb_host_endpoint *ep, char *buf, int bufsize)
  144. {
  145. if (ep)
  146. snprintf(buf, bufsize, "ep_%02x (type:%02X kaddr:%pK)",
  147. ep->desc.bEndpointAddress,
  148. usb_endpoint_type(&ep->desc),
  149. ep);
  150. else
  151. snprintf(buf, bufsize, "none");
  152. return buf;
  153. }
  154. static char *format_etd_dword0(u32 value, char *buf, int bufsize)
  155. {
  156. snprintf(buf, bufsize,
  157. "addr=%d ep=%d dir=%s speed=%s format=%s halted=%d",
  158. value & 0x7F,
  159. (value >> DW0_ENDPNT) & 0x0F,
  160. dir_labels[(value >> DW0_DIRECT) & 0x03],
  161. speed_labels[(value >> DW0_SPEED) & 0x01],
  162. format_labels[(value >> DW0_FORMAT) & 0x03],
  163. (value >> DW0_HALTED) & 0x01);
  164. return buf;
  165. }
  166. static int debug_status_show(struct seq_file *s, void *v)
  167. {
  168. struct imx21 *imx21 = s->private;
  169. int etds_allocated = 0;
  170. int etds_sw_busy = 0;
  171. int etds_hw_busy = 0;
  172. int dmem_blocks = 0;
  173. int queued_for_etd = 0;
  174. int queued_for_dmem = 0;
  175. unsigned int dmem_bytes = 0;
  176. int i;
  177. struct etd_priv *etd;
  178. u32 etd_enable_mask;
  179. unsigned long flags;
  180. struct imx21_dmem_area *dmem;
  181. struct ep_priv *ep_priv;
  182. spin_lock_irqsave(&imx21->lock, flags);
  183. etd_enable_mask = readl(imx21->regs + USBH_ETDENSET);
  184. for (i = 0, etd = imx21->etd; i < USB_NUM_ETD; i++, etd++) {
  185. if (etd->alloc)
  186. etds_allocated++;
  187. if (etd->urb)
  188. etds_sw_busy++;
  189. if (etd_enable_mask & (1<<i))
  190. etds_hw_busy++;
  191. }
  192. list_for_each_entry(dmem, &imx21->dmem_list, list) {
  193. dmem_bytes += dmem->size;
  194. dmem_blocks++;
  195. }
  196. list_for_each_entry(ep_priv, &imx21->queue_for_etd, queue)
  197. queued_for_etd++;
  198. list_for_each_entry(etd, &imx21->queue_for_dmem, queue)
  199. queued_for_dmem++;
  200. spin_unlock_irqrestore(&imx21->lock, flags);
  201. seq_printf(s,
  202. "Frame: %d\n"
  203. "ETDs allocated: %d/%d (max=%d)\n"
  204. "ETDs in use sw: %d\n"
  205. "ETDs in use hw: %d\n"
  206. "DMEM allocated: %d/%d (max=%d)\n"
  207. "DMEM blocks: %d\n"
  208. "Queued waiting for ETD: %d\n"
  209. "Queued waiting for DMEM: %d\n",
  210. readl(imx21->regs + USBH_FRMNUB) & 0xFFFF,
  211. etds_allocated, USB_NUM_ETD, imx21->etd_usage.maximum,
  212. etds_sw_busy,
  213. etds_hw_busy,
  214. dmem_bytes, DMEM_SIZE, imx21->dmem_usage.maximum,
  215. dmem_blocks,
  216. queued_for_etd,
  217. queued_for_dmem);
  218. return 0;
  219. }
  220. static int debug_dmem_show(struct seq_file *s, void *v)
  221. {
  222. struct imx21 *imx21 = s->private;
  223. struct imx21_dmem_area *dmem;
  224. unsigned long flags;
  225. char ep_text[40];
  226. spin_lock_irqsave(&imx21->lock, flags);
  227. list_for_each_entry(dmem, &imx21->dmem_list, list)
  228. seq_printf(s,
  229. "%04X: size=0x%X "
  230. "ep=%s\n",
  231. dmem->offset, dmem->size,
  232. format_ep(dmem->ep, ep_text, sizeof(ep_text)));
  233. spin_unlock_irqrestore(&imx21->lock, flags);
  234. return 0;
  235. }
  236. static int debug_etd_show(struct seq_file *s, void *v)
  237. {
  238. struct imx21 *imx21 = s->private;
  239. struct etd_priv *etd;
  240. char buf[60];
  241. u32 dword;
  242. int i, j;
  243. unsigned long flags;
  244. spin_lock_irqsave(&imx21->lock, flags);
  245. for (i = 0, etd = imx21->etd; i < USB_NUM_ETD; i++, etd++) {
  246. int state = -1;
  247. struct urb_priv *urb_priv;
  248. if (etd->urb) {
  249. urb_priv = etd->urb->hcpriv;
  250. if (urb_priv)
  251. state = urb_priv->state;
  252. }
  253. seq_printf(s,
  254. "etd_num: %d\n"
  255. "ep: %s\n"
  256. "alloc: %d\n"
  257. "len: %d\n"
  258. "busy sw: %d\n"
  259. "busy hw: %d\n"
  260. "urb state: %d\n"
  261. "current urb: %pK\n",
  262. i,
  263. format_ep(etd->ep, buf, sizeof(buf)),
  264. etd->alloc,
  265. etd->len,
  266. etd->urb != NULL,
  267. (readl(imx21->regs + USBH_ETDENSET) & (1 << i)) > 0,
  268. state,
  269. etd->urb);
  270. for (j = 0; j < 4; j++) {
  271. dword = etd_readl(imx21, i, j);
  272. switch (j) {
  273. case 0:
  274. format_etd_dword0(dword, buf, sizeof(buf));
  275. break;
  276. case 2:
  277. snprintf(buf, sizeof(buf),
  278. "cc=0X%02X", dword >> DW2_COMPCODE);
  279. break;
  280. default:
  281. *buf = 0;
  282. break;
  283. }
  284. seq_printf(s,
  285. "dword %d: submitted=%08X cur=%08X [%s]\n",
  286. j,
  287. etd->submitted_dwords[j],
  288. dword,
  289. buf);
  290. }
  291. seq_printf(s, "\n");
  292. }
  293. spin_unlock_irqrestore(&imx21->lock, flags);
  294. return 0;
  295. }
  296. static void debug_statistics_show_one(struct seq_file *s,
  297. const char *name, struct debug_stats *stats)
  298. {
  299. seq_printf(s, "%s:\n"
  300. "submitted URBs: %lu\n"
  301. "completed OK: %lu\n"
  302. "completed failed: %lu\n"
  303. "unlinked: %lu\n"
  304. "queued for ETD: %lu\n"
  305. "queued for DMEM: %lu\n\n",
  306. name,
  307. stats->submitted,
  308. stats->completed_ok,
  309. stats->completed_failed,
  310. stats->unlinked,
  311. stats->queue_etd,
  312. stats->queue_dmem);
  313. }
  314. static int debug_statistics_show(struct seq_file *s, void *v)
  315. {
  316. struct imx21 *imx21 = s->private;
  317. unsigned long flags;
  318. spin_lock_irqsave(&imx21->lock, flags);
  319. debug_statistics_show_one(s, "nonisoc", &imx21->nonisoc_stats);
  320. debug_statistics_show_one(s, "isoc", &imx21->isoc_stats);
  321. seq_printf(s, "unblock kludge triggers: %lu\n", imx21->debug_unblocks);
  322. spin_unlock_irqrestore(&imx21->lock, flags);
  323. return 0;
  324. }
  325. static void debug_isoc_show_one(struct seq_file *s,
  326. const char *name, int index, struct debug_isoc_trace *trace)
  327. {
  328. seq_printf(s, "%s %d:\n"
  329. "cc=0X%02X\n"
  330. "scheduled frame %d (%d)\n"
  331. "submitted frame %d (%d)\n"
  332. "completed frame %d (%d)\n"
  333. "requested length=%d\n"
  334. "completed length=%d\n\n",
  335. name, index,
  336. trace->cc,
  337. trace->schedule_frame, trace->schedule_frame & 0xFFFF,
  338. trace->submit_frame, trace->submit_frame & 0xFFFF,
  339. trace->done_frame, trace->done_frame & 0xFFFF,
  340. trace->request_len,
  341. trace->done_len);
  342. }
  343. static int debug_isoc_show(struct seq_file *s, void *v)
  344. {
  345. struct imx21 *imx21 = s->private;
  346. struct debug_isoc_trace *trace;
  347. unsigned long flags;
  348. int i;
  349. spin_lock_irqsave(&imx21->lock, flags);
  350. trace = imx21->isoc_trace_failed;
  351. for (i = 0; i < ARRAY_SIZE(imx21->isoc_trace_failed); i++, trace++)
  352. debug_isoc_show_one(s, "isoc failed", i, trace);
  353. trace = imx21->isoc_trace;
  354. for (i = 0; i < ARRAY_SIZE(imx21->isoc_trace); i++, trace++)
  355. debug_isoc_show_one(s, "isoc", i, trace);
  356. spin_unlock_irqrestore(&imx21->lock, flags);
  357. return 0;
  358. }
  359. static int debug_status_open(struct inode *inode, struct file *file)
  360. {
  361. return single_open(file, debug_status_show, inode->i_private);
  362. }
  363. static int debug_dmem_open(struct inode *inode, struct file *file)
  364. {
  365. return single_open(file, debug_dmem_show, inode->i_private);
  366. }
  367. static int debug_etd_open(struct inode *inode, struct file *file)
  368. {
  369. return single_open(file, debug_etd_show, inode->i_private);
  370. }
  371. static int debug_statistics_open(struct inode *inode, struct file *file)
  372. {
  373. return single_open(file, debug_statistics_show, inode->i_private);
  374. }
  375. static int debug_isoc_open(struct inode *inode, struct file *file)
  376. {
  377. return single_open(file, debug_isoc_show, inode->i_private);
  378. }
  379. static const struct file_operations debug_status_fops = {
  380. .open = debug_status_open,
  381. .read = seq_read,
  382. .llseek = seq_lseek,
  383. .release = single_release,
  384. };
  385. static const struct file_operations debug_dmem_fops = {
  386. .open = debug_dmem_open,
  387. .read = seq_read,
  388. .llseek = seq_lseek,
  389. .release = single_release,
  390. };
  391. static const struct file_operations debug_etd_fops = {
  392. .open = debug_etd_open,
  393. .read = seq_read,
  394. .llseek = seq_lseek,
  395. .release = single_release,
  396. };
  397. static const struct file_operations debug_statistics_fops = {
  398. .open = debug_statistics_open,
  399. .read = seq_read,
  400. .llseek = seq_lseek,
  401. .release = single_release,
  402. };
  403. static const struct file_operations debug_isoc_fops = {
  404. .open = debug_isoc_open,
  405. .read = seq_read,
  406. .llseek = seq_lseek,
  407. .release = single_release,
  408. };
  409. static void create_debug_files(struct imx21 *imx21)
  410. {
  411. imx21->debug_root = debugfs_create_dir(dev_name(imx21->dev), NULL);
  412. if (!imx21->debug_root)
  413. goto failed_create_rootdir;
  414. if (!debugfs_create_file("status", S_IRUGO,
  415. imx21->debug_root, imx21, &debug_status_fops))
  416. goto failed_create;
  417. if (!debugfs_create_file("dmem", S_IRUGO,
  418. imx21->debug_root, imx21, &debug_dmem_fops))
  419. goto failed_create;
  420. if (!debugfs_create_file("etd", S_IRUGO,
  421. imx21->debug_root, imx21, &debug_etd_fops))
  422. goto failed_create;
  423. if (!debugfs_create_file("statistics", S_IRUGO,
  424. imx21->debug_root, imx21, &debug_statistics_fops))
  425. goto failed_create;
  426. if (!debugfs_create_file("isoc", S_IRUGO,
  427. imx21->debug_root, imx21, &debug_isoc_fops))
  428. goto failed_create;
  429. return;
  430. failed_create:
  431. debugfs_remove_recursive(imx21->debug_root);
  432. failed_create_rootdir:
  433. imx21->debug_root = NULL;
  434. }
  435. static void remove_debug_files(struct imx21 *imx21)
  436. {
  437. if (imx21->debug_root) {
  438. debugfs_remove_recursive(imx21->debug_root);
  439. imx21->debug_root = NULL;
  440. }
  441. }
  442. #endif