debug.c 11 KB

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