debug.c 9.2 KB

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