imx21-dbg.c 13 KB

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