debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. #include <linux/kernel.h>
  2. #include <linux/device.h>
  3. #include <linux/types.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/debugfs.h>
  6. #include <linux/seq_file.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/usb/ch9.h>
  9. #include <linux/usb/gadget.h>
  10. #include <linux/usb/phy.h>
  11. #include <linux/usb/otg.h>
  12. #include <linux/usb/otg-fsm.h>
  13. #include <linux/usb/chipidea.h>
  14. #include "ci.h"
  15. #include "udc.h"
  16. #include "bits.h"
  17. #include "otg.h"
  18. /**
  19. * ci_device_show: prints information about device capabilities and status
  20. */
  21. static int ci_device_show(struct seq_file *s, void *data)
  22. {
  23. struct ci_hdrc *ci = s->private;
  24. struct usb_gadget *gadget = &ci->gadget;
  25. seq_printf(s, "speed = %d\n", gadget->speed);
  26. seq_printf(s, "max_speed = %d\n", gadget->max_speed);
  27. seq_printf(s, "is_otg = %d\n", gadget->is_otg);
  28. seq_printf(s, "is_a_peripheral = %d\n", gadget->is_a_peripheral);
  29. seq_printf(s, "b_hnp_enable = %d\n", gadget->b_hnp_enable);
  30. seq_printf(s, "a_hnp_support = %d\n", gadget->a_hnp_support);
  31. seq_printf(s, "a_alt_hnp_support = %d\n", gadget->a_alt_hnp_support);
  32. seq_printf(s, "name = %s\n",
  33. (gadget->name ? gadget->name : ""));
  34. if (!ci->driver)
  35. return 0;
  36. seq_printf(s, "gadget function = %s\n",
  37. (ci->driver->function ? ci->driver->function : ""));
  38. seq_printf(s, "gadget max speed = %d\n", ci->driver->max_speed);
  39. return 0;
  40. }
  41. static int ci_device_open(struct inode *inode, struct file *file)
  42. {
  43. return single_open(file, ci_device_show, inode->i_private);
  44. }
  45. static const struct file_operations ci_device_fops = {
  46. .open = ci_device_open,
  47. .read = seq_read,
  48. .llseek = seq_lseek,
  49. .release = single_release,
  50. };
  51. /**
  52. * ci_port_test_show: reads port test mode
  53. */
  54. static int ci_port_test_show(struct seq_file *s, void *data)
  55. {
  56. struct ci_hdrc *ci = s->private;
  57. unsigned long flags;
  58. unsigned mode;
  59. pm_runtime_get_sync(ci->dev);
  60. spin_lock_irqsave(&ci->lock, flags);
  61. mode = hw_port_test_get(ci);
  62. spin_unlock_irqrestore(&ci->lock, flags);
  63. pm_runtime_put_sync(ci->dev);
  64. seq_printf(s, "mode = %u\n", mode);
  65. return 0;
  66. }
  67. /**
  68. * ci_port_test_write: writes port test mode
  69. */
  70. static ssize_t ci_port_test_write(struct file *file, const char __user *ubuf,
  71. size_t count, loff_t *ppos)
  72. {
  73. struct seq_file *s = file->private_data;
  74. struct ci_hdrc *ci = s->private;
  75. unsigned long flags;
  76. unsigned mode;
  77. char buf[32];
  78. int ret;
  79. count = min_t(size_t, sizeof(buf) - 1, count);
  80. if (copy_from_user(buf, ubuf, count))
  81. return -EFAULT;
  82. /* sscanf requires a zero terminated string */
  83. buf[count] = '\0';
  84. if (sscanf(buf, "%u", &mode) != 1)
  85. return -EINVAL;
  86. if (mode > 255)
  87. return -EBADRQC;
  88. pm_runtime_get_sync(ci->dev);
  89. spin_lock_irqsave(&ci->lock, flags);
  90. ret = hw_port_test_set(ci, mode);
  91. spin_unlock_irqrestore(&ci->lock, flags);
  92. pm_runtime_put_sync(ci->dev);
  93. return ret ? ret : count;
  94. }
  95. static int ci_port_test_open(struct inode *inode, struct file *file)
  96. {
  97. return single_open(file, ci_port_test_show, inode->i_private);
  98. }
  99. static const struct file_operations ci_port_test_fops = {
  100. .open = ci_port_test_open,
  101. .write = ci_port_test_write,
  102. .read = seq_read,
  103. .llseek = seq_lseek,
  104. .release = single_release,
  105. };
  106. /**
  107. * ci_qheads_show: DMA contents of all queue heads
  108. */
  109. static int ci_qheads_show(struct seq_file *s, void *data)
  110. {
  111. struct ci_hdrc *ci = s->private;
  112. unsigned long flags;
  113. unsigned i, j;
  114. if (ci->role != CI_ROLE_GADGET) {
  115. seq_printf(s, "not in gadget mode\n");
  116. return 0;
  117. }
  118. spin_lock_irqsave(&ci->lock, flags);
  119. for (i = 0; i < ci->hw_ep_max/2; i++) {
  120. struct ci_hw_ep *hweprx = &ci->ci_hw_ep[i];
  121. struct ci_hw_ep *hweptx =
  122. &ci->ci_hw_ep[i + ci->hw_ep_max/2];
  123. seq_printf(s, "EP=%02i: RX=%08X TX=%08X\n",
  124. i, (u32)hweprx->qh.dma, (u32)hweptx->qh.dma);
  125. for (j = 0; j < (sizeof(struct ci_hw_qh)/sizeof(u32)); j++)
  126. seq_printf(s, " %04X: %08X %08X\n", j,
  127. *((u32 *)hweprx->qh.ptr + j),
  128. *((u32 *)hweptx->qh.ptr + j));
  129. }
  130. spin_unlock_irqrestore(&ci->lock, flags);
  131. return 0;
  132. }
  133. static int ci_qheads_open(struct inode *inode, struct file *file)
  134. {
  135. return single_open(file, ci_qheads_show, inode->i_private);
  136. }
  137. static const struct file_operations ci_qheads_fops = {
  138. .open = ci_qheads_open,
  139. .read = seq_read,
  140. .llseek = seq_lseek,
  141. .release = single_release,
  142. };
  143. /**
  144. * ci_requests_show: DMA contents of all requests currently queued (all endpts)
  145. */
  146. static int ci_requests_show(struct seq_file *s, void *data)
  147. {
  148. struct ci_hdrc *ci = s->private;
  149. unsigned long flags;
  150. struct ci_hw_req *req = NULL;
  151. struct td_node *node, *tmpnode;
  152. unsigned i, j, qsize = sizeof(struct ci_hw_td)/sizeof(u32);
  153. if (ci->role != CI_ROLE_GADGET) {
  154. seq_printf(s, "not in gadget mode\n");
  155. return 0;
  156. }
  157. spin_lock_irqsave(&ci->lock, flags);
  158. for (i = 0; i < ci->hw_ep_max; i++)
  159. list_for_each_entry(req, &ci->ci_hw_ep[i].qh.queue, queue) {
  160. list_for_each_entry_safe(node, tmpnode, &req->tds, td) {
  161. seq_printf(s, "EP=%02i: TD=%08X %s\n",
  162. i % (ci->hw_ep_max / 2),
  163. (u32)node->dma,
  164. ((i < ci->hw_ep_max/2) ?
  165. "RX" : "TX"));
  166. for (j = 0; j < qsize; j++)
  167. seq_printf(s, " %04X: %08X\n", j,
  168. *((u32 *)node->ptr + j));
  169. }
  170. }
  171. spin_unlock_irqrestore(&ci->lock, flags);
  172. return 0;
  173. }
  174. static int ci_requests_open(struct inode *inode, struct file *file)
  175. {
  176. return single_open(file, ci_requests_show, inode->i_private);
  177. }
  178. static const struct file_operations ci_requests_fops = {
  179. .open = ci_requests_open,
  180. .read = seq_read,
  181. .llseek = seq_lseek,
  182. .release = single_release,
  183. };
  184. static int ci_otg_show(struct seq_file *s, void *unused)
  185. {
  186. struct ci_hdrc *ci = s->private;
  187. struct otg_fsm *fsm;
  188. if (!ci || !ci_otg_is_fsm_mode(ci))
  189. return 0;
  190. fsm = &ci->fsm;
  191. /* ------ State ----- */
  192. seq_printf(s, "OTG state: %s\n\n",
  193. usb_otg_state_string(ci->otg.state));
  194. /* ------ State Machine Variables ----- */
  195. seq_printf(s, "a_bus_drop: %d\n", fsm->a_bus_drop);
  196. seq_printf(s, "a_bus_req: %d\n", fsm->a_bus_req);
  197. seq_printf(s, "a_srp_det: %d\n", fsm->a_srp_det);
  198. seq_printf(s, "a_vbus_vld: %d\n", fsm->a_vbus_vld);
  199. seq_printf(s, "b_conn: %d\n", fsm->b_conn);
  200. seq_printf(s, "adp_change: %d\n", fsm->adp_change);
  201. seq_printf(s, "power_up: %d\n", fsm->power_up);
  202. seq_printf(s, "a_bus_resume: %d\n", fsm->a_bus_resume);
  203. seq_printf(s, "a_bus_suspend: %d\n", fsm->a_bus_suspend);
  204. seq_printf(s, "a_conn: %d\n", fsm->a_conn);
  205. seq_printf(s, "b_bus_req: %d\n", fsm->b_bus_req);
  206. seq_printf(s, "b_bus_suspend: %d\n", fsm->b_bus_suspend);
  207. seq_printf(s, "b_se0_srp: %d\n", fsm->b_se0_srp);
  208. seq_printf(s, "b_ssend_srp: %d\n", fsm->b_ssend_srp);
  209. seq_printf(s, "b_sess_vld: %d\n", fsm->b_sess_vld);
  210. seq_printf(s, "b_srp_done: %d\n", fsm->b_srp_done);
  211. seq_printf(s, "drv_vbus: %d\n", fsm->drv_vbus);
  212. seq_printf(s, "loc_conn: %d\n", fsm->loc_conn);
  213. seq_printf(s, "loc_sof: %d\n", fsm->loc_sof);
  214. seq_printf(s, "adp_prb: %d\n", fsm->adp_prb);
  215. seq_printf(s, "id: %d\n", fsm->id);
  216. seq_printf(s, "protocol: %d\n", fsm->protocol);
  217. return 0;
  218. }
  219. static int ci_otg_open(struct inode *inode, struct file *file)
  220. {
  221. return single_open(file, ci_otg_show, inode->i_private);
  222. }
  223. static const struct file_operations ci_otg_fops = {
  224. .open = ci_otg_open,
  225. .read = seq_read,
  226. .llseek = seq_lseek,
  227. .release = single_release,
  228. };
  229. static int ci_role_show(struct seq_file *s, void *data)
  230. {
  231. struct ci_hdrc *ci = s->private;
  232. seq_printf(s, "%s\n", ci_role(ci)->name);
  233. return 0;
  234. }
  235. static ssize_t ci_role_write(struct file *file, const char __user *ubuf,
  236. size_t count, loff_t *ppos)
  237. {
  238. struct seq_file *s = file->private_data;
  239. struct ci_hdrc *ci = s->private;
  240. enum ci_role role;
  241. char buf[8];
  242. int ret;
  243. if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  244. return -EFAULT;
  245. for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
  246. if (ci->roles[role] &&
  247. !strncmp(buf, ci->roles[role]->name,
  248. strlen(ci->roles[role]->name)))
  249. break;
  250. if (role == CI_ROLE_END || role == ci->role)
  251. return -EINVAL;
  252. pm_runtime_get_sync(ci->dev);
  253. disable_irq(ci->irq);
  254. ci_role_stop(ci);
  255. ret = ci_role_start(ci, role);
  256. enable_irq(ci->irq);
  257. pm_runtime_put_sync(ci->dev);
  258. return ret ? ret : count;
  259. }
  260. static int ci_role_open(struct inode *inode, struct file *file)
  261. {
  262. return single_open(file, ci_role_show, inode->i_private);
  263. }
  264. static const struct file_operations ci_role_fops = {
  265. .open = ci_role_open,
  266. .write = ci_role_write,
  267. .read = seq_read,
  268. .llseek = seq_lseek,
  269. .release = single_release,
  270. };
  271. static int ci_registers_show(struct seq_file *s, void *unused)
  272. {
  273. struct ci_hdrc *ci = s->private;
  274. u32 tmp_reg;
  275. if (!ci || ci->in_lpm)
  276. return -EPERM;
  277. /* ------ Registers ----- */
  278. tmp_reg = hw_read_intr_enable(ci);
  279. seq_printf(s, "USBINTR reg: %08x\n", tmp_reg);
  280. tmp_reg = hw_read_intr_status(ci);
  281. seq_printf(s, "USBSTS reg: %08x\n", tmp_reg);
  282. tmp_reg = hw_read(ci, OP_USBMODE, ~0);
  283. seq_printf(s, "USBMODE reg: %08x\n", tmp_reg);
  284. tmp_reg = hw_read(ci, OP_USBCMD, ~0);
  285. seq_printf(s, "USBCMD reg: %08x\n", tmp_reg);
  286. tmp_reg = hw_read(ci, OP_PORTSC, ~0);
  287. seq_printf(s, "PORTSC reg: %08x\n", tmp_reg);
  288. if (ci->is_otg) {
  289. tmp_reg = hw_read_otgsc(ci, ~0);
  290. seq_printf(s, "OTGSC reg: %08x\n", tmp_reg);
  291. }
  292. return 0;
  293. }
  294. static int ci_registers_open(struct inode *inode, struct file *file)
  295. {
  296. return single_open(file, ci_registers_show, inode->i_private);
  297. }
  298. static const struct file_operations ci_registers_fops = {
  299. .open = ci_registers_open,
  300. .read = seq_read,
  301. .llseek = seq_lseek,
  302. .release = single_release,
  303. };
  304. /**
  305. * dbg_create_files: initializes the attribute interface
  306. * @ci: device
  307. *
  308. * This function returns an error code
  309. */
  310. int dbg_create_files(struct ci_hdrc *ci)
  311. {
  312. struct dentry *dent;
  313. ci->debugfs = debugfs_create_dir(dev_name(ci->dev), NULL);
  314. if (!ci->debugfs)
  315. return -ENOMEM;
  316. dent = debugfs_create_file("device", S_IRUGO, ci->debugfs, ci,
  317. &ci_device_fops);
  318. if (!dent)
  319. goto err;
  320. dent = debugfs_create_file("port_test", S_IRUGO | S_IWUSR, ci->debugfs,
  321. ci, &ci_port_test_fops);
  322. if (!dent)
  323. goto err;
  324. dent = debugfs_create_file("qheads", S_IRUGO, ci->debugfs, ci,
  325. &ci_qheads_fops);
  326. if (!dent)
  327. goto err;
  328. dent = debugfs_create_file("requests", S_IRUGO, ci->debugfs, ci,
  329. &ci_requests_fops);
  330. if (!dent)
  331. goto err;
  332. if (ci_otg_is_fsm_mode(ci)) {
  333. dent = debugfs_create_file("otg", S_IRUGO, ci->debugfs, ci,
  334. &ci_otg_fops);
  335. if (!dent)
  336. goto err;
  337. }
  338. dent = debugfs_create_file("role", S_IRUGO | S_IWUSR, ci->debugfs, ci,
  339. &ci_role_fops);
  340. if (!dent)
  341. goto err;
  342. dent = debugfs_create_file("registers", S_IRUGO, ci->debugfs, ci,
  343. &ci_registers_fops);
  344. if (dent)
  345. return 0;
  346. err:
  347. debugfs_remove_recursive(ci->debugfs);
  348. return -ENOMEM;
  349. }
  350. /**
  351. * dbg_remove_files: destroys the attribute interface
  352. * @ci: device
  353. */
  354. void dbg_remove_files(struct ci_hdrc *ci)
  355. {
  356. debugfs_remove_recursive(ci->debugfs);
  357. }