debug.c 11 KB

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