debug.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_registers_show(struct seq_file *s, void *unused)
  195. {
  196. struct ci_hdrc *ci = s->private;
  197. u32 tmp_reg;
  198. if (!ci || ci->in_lpm)
  199. return -EPERM;
  200. /* ------ Registers ----- */
  201. tmp_reg = hw_read_intr_enable(ci);
  202. seq_printf(s, "USBINTR reg: %08x\n", tmp_reg);
  203. tmp_reg = hw_read_intr_status(ci);
  204. seq_printf(s, "USBSTS reg: %08x\n", tmp_reg);
  205. tmp_reg = hw_read(ci, OP_USBMODE, ~0);
  206. seq_printf(s, "USBMODE reg: %08x\n", tmp_reg);
  207. tmp_reg = hw_read(ci, OP_USBCMD, ~0);
  208. seq_printf(s, "USBCMD reg: %08x\n", tmp_reg);
  209. tmp_reg = hw_read(ci, OP_PORTSC, ~0);
  210. seq_printf(s, "PORTSC reg: %08x\n", tmp_reg);
  211. if (ci->is_otg) {
  212. tmp_reg = hw_read_otgsc(ci, ~0);
  213. seq_printf(s, "OTGSC reg: %08x\n", tmp_reg);
  214. }
  215. return 0;
  216. }
  217. DEFINE_SHOW_ATTRIBUTE(ci_registers);
  218. /**
  219. * dbg_create_files: initializes the attribute interface
  220. * @ci: device
  221. *
  222. * This function returns an error code
  223. */
  224. void dbg_create_files(struct ci_hdrc *ci)
  225. {
  226. struct dentry *dir;
  227. dir = debugfs_create_dir(dev_name(ci->dev), usb_debug_root);
  228. debugfs_create_file("device", S_IRUGO, dir, ci, &ci_device_fops);
  229. debugfs_create_file("port_test", S_IRUGO | S_IWUSR, dir, ci, &ci_port_test_fops);
  230. debugfs_create_file("qheads", S_IRUGO, dir, ci, &ci_qheads_fops);
  231. debugfs_create_file("requests", S_IRUGO, dir, ci, &ci_requests_fops);
  232. if (ci_otg_is_fsm_mode(ci))
  233. debugfs_create_file("otg", S_IRUGO, dir, ci, &ci_otg_fops);
  234. debugfs_create_file("registers", S_IRUGO, dir, ci, &ci_registers_fops);
  235. }
  236. /**
  237. * dbg_remove_files: destroys the attribute interface
  238. * @ci: device
  239. */
  240. void dbg_remove_files(struct ci_hdrc *ci)
  241. {
  242. debugfs_lookup_and_remove(dev_name(ci->dev), usb_debug_root);
  243. }