ehci-dbg.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * Copyright (c) 2001-2002 by David Brownell
  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 ehci-hcd.c */
  19. #ifdef DEBUG
  20. /* check the values in the HCSPARAMS register
  21. * (host controller _Structural_ parameters)
  22. * see EHCI spec, Table 2-4 for each value
  23. */
  24. static void dbg_hcs_params (struct ehci_hcd *ehci, char *label)
  25. {
  26. u32 params = ehci_readl(ehci, &ehci->caps->hcs_params);
  27. ehci_dbg (ehci,
  28. "%s hcs_params 0x%x dbg=%d%s cc=%d pcc=%d%s%s ports=%d\n",
  29. label, params,
  30. HCS_DEBUG_PORT (params),
  31. HCS_INDICATOR (params) ? " ind" : "",
  32. HCS_N_CC (params),
  33. HCS_N_PCC (params),
  34. HCS_PORTROUTED (params) ? "" : " ordered",
  35. HCS_PPC (params) ? "" : " !ppc",
  36. HCS_N_PORTS (params)
  37. );
  38. /* Port routing, per EHCI 0.95 Spec, Section 2.2.5 */
  39. if (HCS_PORTROUTED (params)) {
  40. int i;
  41. char buf [46], tmp [7], byte;
  42. buf[0] = 0;
  43. for (i = 0; i < HCS_N_PORTS (params); i++) {
  44. // FIXME MIPS won't readb() ...
  45. byte = readb (&ehci->caps->portroute[(i>>1)]);
  46. sprintf(tmp, "%d ",
  47. ((i & 0x1) ? ((byte)&0xf) : ((byte>>4)&0xf)));
  48. strcat(buf, tmp);
  49. }
  50. ehci_dbg (ehci, "%s portroute %s\n",
  51. label, buf);
  52. }
  53. }
  54. #else
  55. static inline void dbg_hcs_params (struct ehci_hcd *ehci, char *label) {}
  56. #endif
  57. #ifdef DEBUG
  58. /* check the values in the HCCPARAMS register
  59. * (host controller _Capability_ parameters)
  60. * see EHCI Spec, Table 2-5 for each value
  61. * */
  62. static void dbg_hcc_params (struct ehci_hcd *ehci, char *label)
  63. {
  64. u32 params = ehci_readl(ehci, &ehci->caps->hcc_params);
  65. if (HCC_ISOC_CACHE (params)) {
  66. ehci_dbg (ehci,
  67. "%s hcc_params %04x caching frame %s%s%s\n",
  68. label, params,
  69. HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
  70. HCC_CANPARK(params) ? " park" : "",
  71. HCC_64BIT_ADDR(params) ? " 64 bit addr" : "");
  72. } else {
  73. ehci_dbg (ehci,
  74. "%s hcc_params %04x thresh %d uframes %s%s%s%s%s%s%s\n",
  75. label,
  76. params,
  77. HCC_ISOC_THRES(params),
  78. HCC_PGM_FRAMELISTLEN(params) ? "256/512/1024" : "1024",
  79. HCC_CANPARK(params) ? " park" : "",
  80. HCC_64BIT_ADDR(params) ? " 64 bit addr" : "",
  81. HCC_LPM(params) ? " LPM" : "",
  82. HCC_PER_PORT_CHANGE_EVENT(params) ? " ppce" : "",
  83. HCC_HW_PREFETCH(params) ? " hw prefetch" : "",
  84. HCC_32FRAME_PERIODIC_LIST(params) ?
  85. " 32 periodic list" : "");
  86. }
  87. }
  88. #else
  89. static inline void dbg_hcc_params (struct ehci_hcd *ehci, char *label) {}
  90. #endif
  91. #ifdef DEBUG
  92. static void __maybe_unused
  93. dbg_qtd (const char *label, struct ehci_hcd *ehci, struct ehci_qtd *qtd)
  94. {
  95. ehci_dbg(ehci, "%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
  96. hc32_to_cpup(ehci, &qtd->hw_next),
  97. hc32_to_cpup(ehci, &qtd->hw_alt_next),
  98. hc32_to_cpup(ehci, &qtd->hw_token),
  99. hc32_to_cpup(ehci, &qtd->hw_buf [0]));
  100. if (qtd->hw_buf [1])
  101. ehci_dbg(ehci, " p1=%08x p2=%08x p3=%08x p4=%08x\n",
  102. hc32_to_cpup(ehci, &qtd->hw_buf[1]),
  103. hc32_to_cpup(ehci, &qtd->hw_buf[2]),
  104. hc32_to_cpup(ehci, &qtd->hw_buf[3]),
  105. hc32_to_cpup(ehci, &qtd->hw_buf[4]));
  106. }
  107. static void __maybe_unused
  108. dbg_qh (const char *label, struct ehci_hcd *ehci, struct ehci_qh *qh)
  109. {
  110. struct ehci_qh_hw *hw = qh->hw;
  111. ehci_dbg (ehci, "%s qh %p n%08x info %x %x qtd %x\n", label,
  112. qh, hw->hw_next, hw->hw_info1, hw->hw_info2, hw->hw_current);
  113. dbg_qtd("overlay", ehci, (struct ehci_qtd *) &hw->hw_qtd_next);
  114. }
  115. static void __maybe_unused
  116. dbg_itd (const char *label, struct ehci_hcd *ehci, struct ehci_itd *itd)
  117. {
  118. ehci_dbg (ehci, "%s [%d] itd %p, next %08x, urb %p\n",
  119. label, itd->frame, itd, hc32_to_cpu(ehci, itd->hw_next),
  120. itd->urb);
  121. ehci_dbg (ehci,
  122. " trans: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  123. hc32_to_cpu(ehci, itd->hw_transaction[0]),
  124. hc32_to_cpu(ehci, itd->hw_transaction[1]),
  125. hc32_to_cpu(ehci, itd->hw_transaction[2]),
  126. hc32_to_cpu(ehci, itd->hw_transaction[3]),
  127. hc32_to_cpu(ehci, itd->hw_transaction[4]),
  128. hc32_to_cpu(ehci, itd->hw_transaction[5]),
  129. hc32_to_cpu(ehci, itd->hw_transaction[6]),
  130. hc32_to_cpu(ehci, itd->hw_transaction[7]));
  131. ehci_dbg (ehci,
  132. " buf: %08x %08x %08x %08x %08x %08x %08x\n",
  133. hc32_to_cpu(ehci, itd->hw_bufp[0]),
  134. hc32_to_cpu(ehci, itd->hw_bufp[1]),
  135. hc32_to_cpu(ehci, itd->hw_bufp[2]),
  136. hc32_to_cpu(ehci, itd->hw_bufp[3]),
  137. hc32_to_cpu(ehci, itd->hw_bufp[4]),
  138. hc32_to_cpu(ehci, itd->hw_bufp[5]),
  139. hc32_to_cpu(ehci, itd->hw_bufp[6]));
  140. ehci_dbg (ehci, " index: %d %d %d %d %d %d %d %d\n",
  141. itd->index[0], itd->index[1], itd->index[2],
  142. itd->index[3], itd->index[4], itd->index[5],
  143. itd->index[6], itd->index[7]);
  144. }
  145. static void __maybe_unused
  146. dbg_sitd (const char *label, struct ehci_hcd *ehci, struct ehci_sitd *sitd)
  147. {
  148. ehci_dbg (ehci, "%s [%d] sitd %p, next %08x, urb %p\n",
  149. label, sitd->frame, sitd, hc32_to_cpu(ehci, sitd->hw_next),
  150. sitd->urb);
  151. ehci_dbg (ehci,
  152. " addr %08x sched %04x result %08x buf %08x %08x\n",
  153. hc32_to_cpu(ehci, sitd->hw_fullspeed_ep),
  154. hc32_to_cpu(ehci, sitd->hw_uframe),
  155. hc32_to_cpu(ehci, sitd->hw_results),
  156. hc32_to_cpu(ehci, sitd->hw_buf[0]),
  157. hc32_to_cpu(ehci, sitd->hw_buf[1]));
  158. }
  159. static int __maybe_unused
  160. dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
  161. {
  162. return scnprintf (buf, len,
  163. "%s%sstatus %04x%s%s%s%s%s%s%s%s%s%s%s",
  164. label, label [0] ? " " : "", status,
  165. (status & STS_PPCE_MASK) ? " PPCE" : "",
  166. (status & STS_ASS) ? " Async" : "",
  167. (status & STS_PSS) ? " Periodic" : "",
  168. (status & STS_RECL) ? " Recl" : "",
  169. (status & STS_HALT) ? " Halt" : "",
  170. (status & STS_IAA) ? " IAA" : "",
  171. (status & STS_FATAL) ? " FATAL" : "",
  172. (status & STS_FLR) ? " FLR" : "",
  173. (status & STS_PCD) ? " PCD" : "",
  174. (status & STS_ERR) ? " ERR" : "",
  175. (status & STS_INT) ? " INT" : ""
  176. );
  177. }
  178. static int __maybe_unused
  179. dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
  180. {
  181. return scnprintf (buf, len,
  182. "%s%sintrenable %02x%s%s%s%s%s%s%s",
  183. label, label [0] ? " " : "", enable,
  184. (enable & STS_PPCE_MASK) ? " PPCE" : "",
  185. (enable & STS_IAA) ? " IAA" : "",
  186. (enable & STS_FATAL) ? " FATAL" : "",
  187. (enable & STS_FLR) ? " FLR" : "",
  188. (enable & STS_PCD) ? " PCD" : "",
  189. (enable & STS_ERR) ? " ERR" : "",
  190. (enable & STS_INT) ? " INT" : ""
  191. );
  192. }
  193. static const char *const fls_strings [] =
  194. { "1024", "512", "256", "??" };
  195. static int
  196. dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
  197. {
  198. return scnprintf (buf, len,
  199. "%s%scommand %07x %s%s%s%s%s%s=%d ithresh=%d%s%s%s%s "
  200. "period=%s%s %s",
  201. label, label [0] ? " " : "", command,
  202. (command & CMD_HIRD) ? " HIRD" : "",
  203. (command & CMD_PPCEE) ? " PPCEE" : "",
  204. (command & CMD_FSP) ? " FSP" : "",
  205. (command & CMD_ASPE) ? " ASPE" : "",
  206. (command & CMD_PSPE) ? " PSPE" : "",
  207. (command & CMD_PARK) ? " park" : "(park)",
  208. CMD_PARK_CNT (command),
  209. (command >> 16) & 0x3f,
  210. (command & CMD_LRESET) ? " LReset" : "",
  211. (command & CMD_IAAD) ? " IAAD" : "",
  212. (command & CMD_ASE) ? " Async" : "",
  213. (command & CMD_PSE) ? " Periodic" : "",
  214. fls_strings [(command >> 2) & 0x3],
  215. (command & CMD_RESET) ? " Reset" : "",
  216. (command & CMD_RUN) ? "RUN" : "HALT"
  217. );
  218. }
  219. static int
  220. dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
  221. {
  222. char *sig;
  223. /* signaling state */
  224. switch (status & (3 << 10)) {
  225. case 0 << 10: sig = "se0"; break;
  226. case 1 << 10: sig = "k"; break; /* low speed */
  227. case 2 << 10: sig = "j"; break;
  228. default: sig = "?"; break;
  229. }
  230. return scnprintf (buf, len,
  231. "%s%sport:%d status %06x %d %s%s%s%s%s%s "
  232. "sig=%s%s%s%s%s%s%s%s%s%s%s",
  233. label, label [0] ? " " : "", port, status,
  234. status>>25,/*device address */
  235. (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_ACK ?
  236. " ACK" : "",
  237. (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_NYET ?
  238. " NYET" : "",
  239. (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_STALL ?
  240. " STALL" : "",
  241. (status & PORT_SSTS)>>23 == PORTSC_SUSPEND_STS_ERR ?
  242. " ERR" : "",
  243. (status & PORT_POWER) ? " POWER" : "",
  244. (status & PORT_OWNER) ? " OWNER" : "",
  245. sig,
  246. (status & PORT_LPM) ? " LPM" : "",
  247. (status & PORT_RESET) ? " RESET" : "",
  248. (status & PORT_SUSPEND) ? " SUSPEND" : "",
  249. (status & PORT_RESUME) ? " RESUME" : "",
  250. (status & PORT_OCC) ? " OCC" : "",
  251. (status & PORT_OC) ? " OC" : "",
  252. (status & PORT_PEC) ? " PEC" : "",
  253. (status & PORT_PE) ? " PE" : "",
  254. (status & PORT_CSC) ? " CSC" : "",
  255. (status & PORT_CONNECT) ? " CONNECT" : "");
  256. }
  257. #else
  258. static inline void __maybe_unused
  259. dbg_qh (char *label, struct ehci_hcd *ehci, struct ehci_qh *qh)
  260. {}
  261. static inline int __maybe_unused
  262. dbg_status_buf (char *buf, unsigned len, const char *label, u32 status)
  263. { return 0; }
  264. static inline int __maybe_unused
  265. dbg_command_buf (char *buf, unsigned len, const char *label, u32 command)
  266. { return 0; }
  267. static inline int __maybe_unused
  268. dbg_intr_buf (char *buf, unsigned len, const char *label, u32 enable)
  269. { return 0; }
  270. static inline int __maybe_unused
  271. dbg_port_buf (char *buf, unsigned len, const char *label, int port, u32 status)
  272. { return 0; }
  273. #endif /* DEBUG */
  274. /* functions have the "wrong" filename when they're output... */
  275. #define dbg_status(ehci, label, status) { \
  276. char _buf [80]; \
  277. dbg_status_buf (_buf, sizeof _buf, label, status); \
  278. ehci_dbg (ehci, "%s\n", _buf); \
  279. }
  280. #define dbg_cmd(ehci, label, command) { \
  281. char _buf [80]; \
  282. dbg_command_buf (_buf, sizeof _buf, label, command); \
  283. ehci_dbg (ehci, "%s\n", _buf); \
  284. }
  285. #define dbg_port(ehci, label, port, status) { \
  286. char _buf [80]; \
  287. dbg_port_buf (_buf, sizeof _buf, label, port, status); \
  288. ehci_dbg (ehci, "%s\n", _buf); \
  289. }
  290. /*-------------------------------------------------------------------------*/
  291. #ifdef STUB_DEBUG_FILES
  292. static inline void create_debug_files (struct ehci_hcd *bus) { }
  293. static inline void remove_debug_files (struct ehci_hcd *bus) { }
  294. #else
  295. /* troubleshooting help: expose state in debugfs */
  296. static int debug_async_open(struct inode *, struct file *);
  297. static int debug_periodic_open(struct inode *, struct file *);
  298. static int debug_registers_open(struct inode *, struct file *);
  299. static int debug_async_open(struct inode *, struct file *);
  300. static ssize_t debug_lpm_read(struct file *file, char __user *user_buf,
  301. size_t count, loff_t *ppos);
  302. static ssize_t debug_lpm_write(struct file *file, const char __user *buffer,
  303. size_t count, loff_t *ppos);
  304. static int debug_lpm_close(struct inode *inode, struct file *file);
  305. static ssize_t debug_output(struct file*, char __user*, size_t, loff_t*);
  306. static int debug_close(struct inode *, struct file *);
  307. static const struct file_operations debug_async_fops = {
  308. .owner = THIS_MODULE,
  309. .open = debug_async_open,
  310. .read = debug_output,
  311. .release = debug_close,
  312. .llseek = default_llseek,
  313. };
  314. static const struct file_operations debug_periodic_fops = {
  315. .owner = THIS_MODULE,
  316. .open = debug_periodic_open,
  317. .read = debug_output,
  318. .release = debug_close,
  319. .llseek = default_llseek,
  320. };
  321. static const struct file_operations debug_registers_fops = {
  322. .owner = THIS_MODULE,
  323. .open = debug_registers_open,
  324. .read = debug_output,
  325. .release = debug_close,
  326. .llseek = default_llseek,
  327. };
  328. static const struct file_operations debug_lpm_fops = {
  329. .owner = THIS_MODULE,
  330. .open = simple_open,
  331. .read = debug_lpm_read,
  332. .write = debug_lpm_write,
  333. .release = debug_lpm_close,
  334. .llseek = noop_llseek,
  335. };
  336. static struct dentry *ehci_debug_root;
  337. struct debug_buffer {
  338. ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
  339. struct usb_bus *bus;
  340. struct mutex mutex; /* protect filling of buffer */
  341. size_t count; /* number of characters filled into buffer */
  342. char *output_buf;
  343. size_t alloc_size;
  344. };
  345. #define speed_char(info1) ({ char tmp; \
  346. switch (info1 & (3 << 12)) { \
  347. case QH_FULL_SPEED: tmp = 'f'; break; \
  348. case QH_LOW_SPEED: tmp = 'l'; break; \
  349. case QH_HIGH_SPEED: tmp = 'h'; break; \
  350. default: tmp = '?'; break; \
  351. }; tmp; })
  352. static inline char token_mark(struct ehci_hcd *ehci, __hc32 token)
  353. {
  354. __u32 v = hc32_to_cpu(ehci, token);
  355. if (v & QTD_STS_ACTIVE)
  356. return '*';
  357. if (v & QTD_STS_HALT)
  358. return '-';
  359. if (!IS_SHORT_READ (v))
  360. return ' ';
  361. /* tries to advance through hw_alt_next */
  362. return '/';
  363. }
  364. static void qh_lines (
  365. struct ehci_hcd *ehci,
  366. struct ehci_qh *qh,
  367. char **nextp,
  368. unsigned *sizep
  369. )
  370. {
  371. u32 scratch;
  372. u32 hw_curr;
  373. struct list_head *entry;
  374. struct ehci_qtd *td;
  375. unsigned temp;
  376. unsigned size = *sizep;
  377. char *next = *nextp;
  378. char mark;
  379. __le32 list_end = EHCI_LIST_END(ehci);
  380. struct ehci_qh_hw *hw = qh->hw;
  381. if (hw->hw_qtd_next == list_end) /* NEC does this */
  382. mark = '@';
  383. else
  384. mark = token_mark(ehci, hw->hw_token);
  385. if (mark == '/') { /* qh_alt_next controls qh advance? */
  386. if ((hw->hw_alt_next & QTD_MASK(ehci))
  387. == ehci->async->hw->hw_alt_next)
  388. mark = '#'; /* blocked */
  389. else if (hw->hw_alt_next == list_end)
  390. mark = '.'; /* use hw_qtd_next */
  391. /* else alt_next points to some other qtd */
  392. }
  393. scratch = hc32_to_cpup(ehci, &hw->hw_info1);
  394. hw_curr = (mark == '*') ? hc32_to_cpup(ehci, &hw->hw_current) : 0;
  395. temp = scnprintf (next, size,
  396. "qh/%p dev%d %cs ep%d %08x %08x (%08x%c %s nak%d)",
  397. qh, scratch & 0x007f,
  398. speed_char (scratch),
  399. (scratch >> 8) & 0x000f,
  400. scratch, hc32_to_cpup(ehci, &hw->hw_info2),
  401. hc32_to_cpup(ehci, &hw->hw_token), mark,
  402. (cpu_to_hc32(ehci, QTD_TOGGLE) & hw->hw_token)
  403. ? "data1" : "data0",
  404. (hc32_to_cpup(ehci, &hw->hw_alt_next) >> 1) & 0x0f);
  405. size -= temp;
  406. next += temp;
  407. /* hc may be modifying the list as we read it ... */
  408. list_for_each (entry, &qh->qtd_list) {
  409. td = list_entry (entry, struct ehci_qtd, qtd_list);
  410. scratch = hc32_to_cpup(ehci, &td->hw_token);
  411. mark = ' ';
  412. if (hw_curr == td->qtd_dma)
  413. mark = '*';
  414. else if (hw->hw_qtd_next == cpu_to_hc32(ehci, td->qtd_dma))
  415. mark = '+';
  416. else if (QTD_LENGTH (scratch)) {
  417. if (td->hw_alt_next == ehci->async->hw->hw_alt_next)
  418. mark = '#';
  419. else if (td->hw_alt_next != list_end)
  420. mark = '/';
  421. }
  422. temp = snprintf (next, size,
  423. "\n\t%p%c%s len=%d %08x urb %p",
  424. td, mark, ({ char *tmp;
  425. switch ((scratch>>8)&0x03) {
  426. case 0: tmp = "out"; break;
  427. case 1: tmp = "in"; break;
  428. case 2: tmp = "setup"; break;
  429. default: tmp = "?"; break;
  430. } tmp;}),
  431. (scratch >> 16) & 0x7fff,
  432. scratch,
  433. td->urb);
  434. if (size < temp)
  435. temp = size;
  436. size -= temp;
  437. next += temp;
  438. if (temp == size)
  439. goto done;
  440. }
  441. temp = snprintf (next, size, "\n");
  442. if (size < temp)
  443. temp = size;
  444. size -= temp;
  445. next += temp;
  446. done:
  447. *sizep = size;
  448. *nextp = next;
  449. }
  450. static ssize_t fill_async_buffer(struct debug_buffer *buf)
  451. {
  452. struct usb_hcd *hcd;
  453. struct ehci_hcd *ehci;
  454. unsigned long flags;
  455. unsigned temp, size;
  456. char *next;
  457. struct ehci_qh *qh;
  458. hcd = bus_to_hcd(buf->bus);
  459. ehci = hcd_to_ehci (hcd);
  460. next = buf->output_buf;
  461. size = buf->alloc_size;
  462. *next = 0;
  463. /* dumps a snapshot of the async schedule.
  464. * usually empty except for long-term bulk reads, or head.
  465. * one QH per line, and TDs we know about
  466. */
  467. spin_lock_irqsave (&ehci->lock, flags);
  468. for (qh = ehci->async->qh_next.qh; size > 0 && qh; qh = qh->qh_next.qh)
  469. qh_lines (ehci, qh, &next, &size);
  470. if (ehci->async_unlink && size > 0) {
  471. temp = scnprintf(next, size, "\nunlink =\n");
  472. size -= temp;
  473. next += temp;
  474. for (qh = ehci->async_unlink; size > 0 && qh;
  475. qh = qh->unlink_next)
  476. qh_lines (ehci, qh, &next, &size);
  477. }
  478. spin_unlock_irqrestore (&ehci->lock, flags);
  479. return strlen(buf->output_buf);
  480. }
  481. #define DBG_SCHED_LIMIT 64
  482. static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
  483. {
  484. struct usb_hcd *hcd;
  485. struct ehci_hcd *ehci;
  486. unsigned long flags;
  487. union ehci_shadow p, *seen;
  488. unsigned temp, size, seen_count;
  489. char *next;
  490. unsigned i;
  491. __hc32 tag;
  492. if (!(seen = kmalloc (DBG_SCHED_LIMIT * sizeof *seen, GFP_ATOMIC)))
  493. return 0;
  494. seen_count = 0;
  495. hcd = bus_to_hcd(buf->bus);
  496. ehci = hcd_to_ehci (hcd);
  497. next = buf->output_buf;
  498. size = buf->alloc_size;
  499. temp = scnprintf (next, size, "size = %d\n", ehci->periodic_size);
  500. size -= temp;
  501. next += temp;
  502. /* dump a snapshot of the periodic schedule.
  503. * iso changes, interrupt usually doesn't.
  504. */
  505. spin_lock_irqsave (&ehci->lock, flags);
  506. for (i = 0; i < ehci->periodic_size; i++) {
  507. p = ehci->pshadow [i];
  508. if (likely (!p.ptr))
  509. continue;
  510. tag = Q_NEXT_TYPE(ehci, ehci->periodic [i]);
  511. temp = scnprintf (next, size, "%4d: ", i);
  512. size -= temp;
  513. next += temp;
  514. do {
  515. struct ehci_qh_hw *hw;
  516. switch (hc32_to_cpu(ehci, tag)) {
  517. case Q_TYPE_QH:
  518. hw = p.qh->hw;
  519. temp = scnprintf (next, size, " qh%d-%04x/%p",
  520. p.qh->period,
  521. hc32_to_cpup(ehci,
  522. &hw->hw_info2)
  523. /* uframe masks */
  524. & (QH_CMASK | QH_SMASK),
  525. p.qh);
  526. size -= temp;
  527. next += temp;
  528. /* don't repeat what follows this qh */
  529. for (temp = 0; temp < seen_count; temp++) {
  530. if (seen [temp].ptr != p.ptr)
  531. continue;
  532. if (p.qh->qh_next.ptr) {
  533. temp = scnprintf (next, size,
  534. " ...");
  535. size -= temp;
  536. next += temp;
  537. }
  538. break;
  539. }
  540. /* show more info the first time around */
  541. if (temp == seen_count) {
  542. u32 scratch = hc32_to_cpup(ehci,
  543. &hw->hw_info1);
  544. struct ehci_qtd *qtd;
  545. char *type = "";
  546. /* count tds, get ep direction */
  547. temp = 0;
  548. list_for_each_entry (qtd,
  549. &p.qh->qtd_list,
  550. qtd_list) {
  551. temp++;
  552. switch (0x03 & (hc32_to_cpu(
  553. ehci,
  554. qtd->hw_token) >> 8)) {
  555. case 0: type = "out"; continue;
  556. case 1: type = "in"; continue;
  557. }
  558. }
  559. temp = scnprintf (next, size,
  560. " (%c%d ep%d%s "
  561. "[%d/%d] q%d p%d)",
  562. speed_char (scratch),
  563. scratch & 0x007f,
  564. (scratch >> 8) & 0x000f, type,
  565. p.qh->usecs, p.qh->c_usecs,
  566. temp,
  567. 0x7ff & (scratch >> 16));
  568. if (seen_count < DBG_SCHED_LIMIT)
  569. seen [seen_count++].qh = p.qh;
  570. } else
  571. temp = 0;
  572. tag = Q_NEXT_TYPE(ehci, hw->hw_next);
  573. p = p.qh->qh_next;
  574. break;
  575. case Q_TYPE_FSTN:
  576. temp = scnprintf (next, size,
  577. " fstn-%8x/%p", p.fstn->hw_prev,
  578. p.fstn);
  579. tag = Q_NEXT_TYPE(ehci, p.fstn->hw_next);
  580. p = p.fstn->fstn_next;
  581. break;
  582. case Q_TYPE_ITD:
  583. temp = scnprintf (next, size,
  584. " itd/%p", p.itd);
  585. tag = Q_NEXT_TYPE(ehci, p.itd->hw_next);
  586. p = p.itd->itd_next;
  587. break;
  588. case Q_TYPE_SITD:
  589. temp = scnprintf (next, size,
  590. " sitd%d-%04x/%p",
  591. p.sitd->stream->interval,
  592. hc32_to_cpup(ehci, &p.sitd->hw_uframe)
  593. & 0x0000ffff,
  594. p.sitd);
  595. tag = Q_NEXT_TYPE(ehci, p.sitd->hw_next);
  596. p = p.sitd->sitd_next;
  597. break;
  598. }
  599. size -= temp;
  600. next += temp;
  601. } while (p.ptr);
  602. temp = scnprintf (next, size, "\n");
  603. size -= temp;
  604. next += temp;
  605. }
  606. spin_unlock_irqrestore (&ehci->lock, flags);
  607. kfree (seen);
  608. return buf->alloc_size - size;
  609. }
  610. #undef DBG_SCHED_LIMIT
  611. static const char *rh_state_string(struct ehci_hcd *ehci)
  612. {
  613. switch (ehci->rh_state) {
  614. case EHCI_RH_HALTED:
  615. return "halted";
  616. case EHCI_RH_SUSPENDED:
  617. return "suspended";
  618. case EHCI_RH_RUNNING:
  619. return "running";
  620. case EHCI_RH_STOPPING:
  621. return "stopping";
  622. }
  623. return "?";
  624. }
  625. static ssize_t fill_registers_buffer(struct debug_buffer *buf)
  626. {
  627. struct usb_hcd *hcd;
  628. struct ehci_hcd *ehci;
  629. unsigned long flags;
  630. unsigned temp, size, i;
  631. char *next, scratch [80];
  632. static char fmt [] = "%*s\n";
  633. static char label [] = "";
  634. hcd = bus_to_hcd(buf->bus);
  635. ehci = hcd_to_ehci (hcd);
  636. next = buf->output_buf;
  637. size = buf->alloc_size;
  638. spin_lock_irqsave (&ehci->lock, flags);
  639. if (!HCD_HW_ACCESSIBLE(hcd)) {
  640. size = scnprintf (next, size,
  641. "bus %s, device %s\n"
  642. "%s\n"
  643. "SUSPENDED (no register access)\n",
  644. hcd->self.controller->bus->name,
  645. dev_name(hcd->self.controller),
  646. hcd->product_desc);
  647. goto done;
  648. }
  649. /* Capability Registers */
  650. i = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  651. temp = scnprintf (next, size,
  652. "bus %s, device %s\n"
  653. "%s\n"
  654. "EHCI %x.%02x, rh state %s\n",
  655. hcd->self.controller->bus->name,
  656. dev_name(hcd->self.controller),
  657. hcd->product_desc,
  658. i >> 8, i & 0x0ff, rh_state_string(ehci));
  659. size -= temp;
  660. next += temp;
  661. #ifdef CONFIG_PCI
  662. /* EHCI 0.96 and later may have "extended capabilities" */
  663. if (hcd->self.controller->bus == &pci_bus_type) {
  664. struct pci_dev *pdev;
  665. u32 offset, cap, cap2;
  666. unsigned count = 256/4;
  667. pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller);
  668. offset = HCC_EXT_CAPS(ehci_readl(ehci,
  669. &ehci->caps->hcc_params));
  670. while (offset && count--) {
  671. pci_read_config_dword (pdev, offset, &cap);
  672. switch (cap & 0xff) {
  673. case 1:
  674. temp = scnprintf (next, size,
  675. "ownership %08x%s%s\n", cap,
  676. (cap & (1 << 24)) ? " linux" : "",
  677. (cap & (1 << 16)) ? " firmware" : "");
  678. size -= temp;
  679. next += temp;
  680. offset += 4;
  681. pci_read_config_dword (pdev, offset, &cap2);
  682. temp = scnprintf (next, size,
  683. "SMI sts/enable 0x%08x\n", cap2);
  684. size -= temp;
  685. next += temp;
  686. break;
  687. case 0: /* illegal reserved capability */
  688. cap = 0;
  689. /* FALLTHROUGH */
  690. default: /* unknown */
  691. break;
  692. }
  693. temp = (cap >> 8) & 0xff;
  694. }
  695. }
  696. #endif
  697. // FIXME interpret both types of params
  698. i = ehci_readl(ehci, &ehci->caps->hcs_params);
  699. temp = scnprintf (next, size, "structural params 0x%08x\n", i);
  700. size -= temp;
  701. next += temp;
  702. i = ehci_readl(ehci, &ehci->caps->hcc_params);
  703. temp = scnprintf (next, size, "capability params 0x%08x\n", i);
  704. size -= temp;
  705. next += temp;
  706. /* Operational Registers */
  707. temp = dbg_status_buf (scratch, sizeof scratch, label,
  708. ehci_readl(ehci, &ehci->regs->status));
  709. temp = scnprintf (next, size, fmt, temp, scratch);
  710. size -= temp;
  711. next += temp;
  712. temp = dbg_command_buf (scratch, sizeof scratch, label,
  713. ehci_readl(ehci, &ehci->regs->command));
  714. temp = scnprintf (next, size, fmt, temp, scratch);
  715. size -= temp;
  716. next += temp;
  717. temp = dbg_intr_buf (scratch, sizeof scratch, label,
  718. ehci_readl(ehci, &ehci->regs->intr_enable));
  719. temp = scnprintf (next, size, fmt, temp, scratch);
  720. size -= temp;
  721. next += temp;
  722. temp = scnprintf (next, size, "uframe %04x\n",
  723. ehci_read_frame_index(ehci));
  724. size -= temp;
  725. next += temp;
  726. for (i = 1; i <= HCS_N_PORTS (ehci->hcs_params); i++) {
  727. temp = dbg_port_buf (scratch, sizeof scratch, label, i,
  728. ehci_readl(ehci,
  729. &ehci->regs->port_status[i - 1]));
  730. temp = scnprintf (next, size, fmt, temp, scratch);
  731. size -= temp;
  732. next += temp;
  733. if (i == HCS_DEBUG_PORT(ehci->hcs_params) && ehci->debug) {
  734. temp = scnprintf (next, size,
  735. " debug control %08x\n",
  736. ehci_readl(ehci,
  737. &ehci->debug->control));
  738. size -= temp;
  739. next += temp;
  740. }
  741. }
  742. if (ehci->async_unlink) {
  743. temp = scnprintf(next, size, "async unlink qh %p\n",
  744. ehci->async_unlink);
  745. size -= temp;
  746. next += temp;
  747. }
  748. #ifdef EHCI_STATS
  749. temp = scnprintf (next, size,
  750. "irq normal %ld err %ld iaa %ld (lost %ld)\n",
  751. ehci->stats.normal, ehci->stats.error, ehci->stats.iaa,
  752. ehci->stats.lost_iaa);
  753. size -= temp;
  754. next += temp;
  755. temp = scnprintf (next, size, "complete %ld unlink %ld\n",
  756. ehci->stats.complete, ehci->stats.unlink);
  757. size -= temp;
  758. next += temp;
  759. #endif
  760. done:
  761. spin_unlock_irqrestore (&ehci->lock, flags);
  762. return buf->alloc_size - size;
  763. }
  764. static struct debug_buffer *alloc_buffer(struct usb_bus *bus,
  765. ssize_t (*fill_func)(struct debug_buffer *))
  766. {
  767. struct debug_buffer *buf;
  768. buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
  769. if (buf) {
  770. buf->bus = bus;
  771. buf->fill_func = fill_func;
  772. mutex_init(&buf->mutex);
  773. buf->alloc_size = PAGE_SIZE;
  774. }
  775. return buf;
  776. }
  777. static int fill_buffer(struct debug_buffer *buf)
  778. {
  779. int ret = 0;
  780. if (!buf->output_buf)
  781. buf->output_buf = vmalloc(buf->alloc_size);
  782. if (!buf->output_buf) {
  783. ret = -ENOMEM;
  784. goto out;
  785. }
  786. ret = buf->fill_func(buf);
  787. if (ret >= 0) {
  788. buf->count = ret;
  789. ret = 0;
  790. }
  791. out:
  792. return ret;
  793. }
  794. static ssize_t debug_output(struct file *file, char __user *user_buf,
  795. size_t len, loff_t *offset)
  796. {
  797. struct debug_buffer *buf = file->private_data;
  798. int ret = 0;
  799. mutex_lock(&buf->mutex);
  800. if (buf->count == 0) {
  801. ret = fill_buffer(buf);
  802. if (ret != 0) {
  803. mutex_unlock(&buf->mutex);
  804. goto out;
  805. }
  806. }
  807. mutex_unlock(&buf->mutex);
  808. ret = simple_read_from_buffer(user_buf, len, offset,
  809. buf->output_buf, buf->count);
  810. out:
  811. return ret;
  812. }
  813. static int debug_close(struct inode *inode, struct file *file)
  814. {
  815. struct debug_buffer *buf = file->private_data;
  816. if (buf) {
  817. vfree(buf->output_buf);
  818. kfree(buf);
  819. }
  820. return 0;
  821. }
  822. static int debug_async_open(struct inode *inode, struct file *file)
  823. {
  824. file->private_data = alloc_buffer(inode->i_private, fill_async_buffer);
  825. return file->private_data ? 0 : -ENOMEM;
  826. }
  827. static int debug_periodic_open(struct inode *inode, struct file *file)
  828. {
  829. struct debug_buffer *buf;
  830. buf = alloc_buffer(inode->i_private, fill_periodic_buffer);
  831. if (!buf)
  832. return -ENOMEM;
  833. buf->alloc_size = (sizeof(void *) == 4 ? 6 : 8)*PAGE_SIZE;
  834. file->private_data = buf;
  835. return 0;
  836. }
  837. static int debug_registers_open(struct inode *inode, struct file *file)
  838. {
  839. file->private_data = alloc_buffer(inode->i_private,
  840. fill_registers_buffer);
  841. return file->private_data ? 0 : -ENOMEM;
  842. }
  843. static int debug_lpm_close(struct inode *inode, struct file *file)
  844. {
  845. return 0;
  846. }
  847. static ssize_t debug_lpm_read(struct file *file, char __user *user_buf,
  848. size_t count, loff_t *ppos)
  849. {
  850. /* TODO: show lpm stats */
  851. return 0;
  852. }
  853. static ssize_t debug_lpm_write(struct file *file, const char __user *user_buf,
  854. size_t count, loff_t *ppos)
  855. {
  856. struct usb_hcd *hcd;
  857. struct ehci_hcd *ehci;
  858. char buf[50];
  859. size_t len;
  860. u32 temp;
  861. unsigned long port;
  862. u32 __iomem *portsc ;
  863. u32 params;
  864. hcd = bus_to_hcd(file->private_data);
  865. ehci = hcd_to_ehci(hcd);
  866. len = min(count, sizeof(buf) - 1);
  867. if (copy_from_user(buf, user_buf, len))
  868. return -EFAULT;
  869. buf[len] = '\0';
  870. if (len > 0 && buf[len - 1] == '\n')
  871. buf[len - 1] = '\0';
  872. if (strncmp(buf, "enable", 5) == 0) {
  873. if (strict_strtoul(buf + 7, 10, &port))
  874. return -EINVAL;
  875. params = ehci_readl(ehci, &ehci->caps->hcs_params);
  876. if (port > HCS_N_PORTS(params)) {
  877. ehci_dbg(ehci, "ERR: LPM on bad port %lu\n", port);
  878. return -ENODEV;
  879. }
  880. portsc = &ehci->regs->port_status[port-1];
  881. temp = ehci_readl(ehci, portsc);
  882. if (!(temp & PORT_DEV_ADDR)) {
  883. ehci_dbg(ehci, "LPM: no device attached\n");
  884. return -ENODEV;
  885. }
  886. temp |= PORT_LPM;
  887. ehci_writel(ehci, temp, portsc);
  888. printk(KERN_INFO "force enable LPM for port %lu\n", port);
  889. } else if (strncmp(buf, "hird=", 5) == 0) {
  890. unsigned long hird;
  891. if (strict_strtoul(buf + 5, 16, &hird))
  892. return -EINVAL;
  893. printk(KERN_INFO "setting hird %s %lu\n", buf + 6, hird);
  894. ehci->command = (ehci->command & ~CMD_HIRD) | (hird << 24);
  895. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  896. } else if (strncmp(buf, "disable", 7) == 0) {
  897. if (strict_strtoul(buf + 8, 10, &port))
  898. return -EINVAL;
  899. params = ehci_readl(ehci, &ehci->caps->hcs_params);
  900. if (port > HCS_N_PORTS(params)) {
  901. ehci_dbg(ehci, "ERR: LPM off bad port %lu\n", port);
  902. return -ENODEV;
  903. }
  904. portsc = &ehci->regs->port_status[port-1];
  905. temp = ehci_readl(ehci, portsc);
  906. if (!(temp & PORT_DEV_ADDR)) {
  907. ehci_dbg(ehci, "ERR: no device attached\n");
  908. return -ENODEV;
  909. }
  910. temp &= ~PORT_LPM;
  911. ehci_writel(ehci, temp, portsc);
  912. printk(KERN_INFO "disabled LPM for port %lu\n", port);
  913. } else
  914. return -EOPNOTSUPP;
  915. return count;
  916. }
  917. static inline void create_debug_files (struct ehci_hcd *ehci)
  918. {
  919. struct usb_bus *bus = &ehci_to_hcd(ehci)->self;
  920. ehci->debug_dir = debugfs_create_dir(bus->bus_name, ehci_debug_root);
  921. if (!ehci->debug_dir)
  922. return;
  923. if (!debugfs_create_file("async", S_IRUGO, ehci->debug_dir, bus,
  924. &debug_async_fops))
  925. goto file_error;
  926. if (!debugfs_create_file("periodic", S_IRUGO, ehci->debug_dir, bus,
  927. &debug_periodic_fops))
  928. goto file_error;
  929. if (!debugfs_create_file("registers", S_IRUGO, ehci->debug_dir, bus,
  930. &debug_registers_fops))
  931. goto file_error;
  932. if (!debugfs_create_file("lpm", S_IRUGO|S_IWUSR, ehci->debug_dir, bus,
  933. &debug_lpm_fops))
  934. goto file_error;
  935. return;
  936. file_error:
  937. debugfs_remove_recursive(ehci->debug_dir);
  938. }
  939. static inline void remove_debug_files (struct ehci_hcd *ehci)
  940. {
  941. debugfs_remove_recursive(ehci->debug_dir);
  942. }
  943. #endif /* STUB_DEBUG_FILES */