debug.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "ci.h"
  11. #include "udc.h"
  12. #include "bits.h"
  13. #include "debug.h"
  14. /**
  15. * ci_device_show: prints information about device capabilities and status
  16. */
  17. static int ci_device_show(struct seq_file *s, void *data)
  18. {
  19. struct ci13xxx *ci = s->private;
  20. struct usb_gadget *gadget = &ci->gadget;
  21. seq_printf(s, "speed = %d\n", gadget->speed);
  22. seq_printf(s, "max_speed = %d\n", gadget->max_speed);
  23. seq_printf(s, "is_otg = %d\n", gadget->is_otg);
  24. seq_printf(s, "is_a_peripheral = %d\n", gadget->is_a_peripheral);
  25. seq_printf(s, "b_hnp_enable = %d\n", gadget->b_hnp_enable);
  26. seq_printf(s, "a_hnp_support = %d\n", gadget->a_hnp_support);
  27. seq_printf(s, "a_alt_hnp_support = %d\n", gadget->a_alt_hnp_support);
  28. seq_printf(s, "name = %s\n",
  29. (gadget->name ? gadget->name : ""));
  30. if (!ci->driver)
  31. return 0;
  32. seq_printf(s, "gadget function = %s\n",
  33. (ci->driver->function ? ci->driver->function : ""));
  34. seq_printf(s, "gadget max speed = %d\n", ci->driver->max_speed);
  35. return 0;
  36. }
  37. static int ci_device_open(struct inode *inode, struct file *file)
  38. {
  39. return single_open(file, ci_device_show, inode->i_private);
  40. }
  41. static const struct file_operations ci_device_fops = {
  42. .open = ci_device_open,
  43. .read = seq_read,
  44. .llseek = seq_lseek,
  45. .release = single_release,
  46. };
  47. /**
  48. * ci_port_test_show: reads port test mode
  49. */
  50. static int ci_port_test_show(struct seq_file *s, void *data)
  51. {
  52. struct ci13xxx *ci = s->private;
  53. unsigned long flags;
  54. unsigned mode;
  55. spin_lock_irqsave(&ci->lock, flags);
  56. mode = hw_port_test_get(ci);
  57. spin_unlock_irqrestore(&ci->lock, flags);
  58. seq_printf(s, "mode = %u\n", mode);
  59. return 0;
  60. }
  61. /**
  62. * ci_port_test_write: writes port test mode
  63. */
  64. static ssize_t ci_port_test_write(struct file *file, const char __user *ubuf,
  65. size_t count, loff_t *ppos)
  66. {
  67. struct seq_file *s = file->private_data;
  68. struct ci13xxx *ci = s->private;
  69. unsigned long flags;
  70. unsigned mode;
  71. char buf[32];
  72. int ret;
  73. if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  74. return -EFAULT;
  75. if (sscanf(buf, "%u", &mode) != 1)
  76. return -EINVAL;
  77. spin_lock_irqsave(&ci->lock, flags);
  78. ret = hw_port_test_set(ci, mode);
  79. spin_unlock_irqrestore(&ci->lock, flags);
  80. return ret ? ret : count;
  81. }
  82. static int ci_port_test_open(struct inode *inode, struct file *file)
  83. {
  84. return single_open(file, ci_port_test_show, inode->i_private);
  85. }
  86. static const struct file_operations ci_port_test_fops = {
  87. .open = ci_port_test_open,
  88. .write = ci_port_test_write,
  89. .read = seq_read,
  90. .llseek = seq_lseek,
  91. .release = single_release,
  92. };
  93. /**
  94. * ci_qheads_show: DMA contents of all queue heads
  95. */
  96. static int ci_qheads_show(struct seq_file *s, void *data)
  97. {
  98. struct ci13xxx *ci = s->private;
  99. unsigned long flags;
  100. unsigned i, j;
  101. if (ci->role != CI_ROLE_GADGET) {
  102. seq_printf(s, "not in gadget mode\n");
  103. return 0;
  104. }
  105. spin_lock_irqsave(&ci->lock, flags);
  106. for (i = 0; i < ci->hw_ep_max/2; i++) {
  107. struct ci13xxx_ep *mEpRx = &ci->ci13xxx_ep[i];
  108. struct ci13xxx_ep *mEpTx =
  109. &ci->ci13xxx_ep[i + ci->hw_ep_max/2];
  110. seq_printf(s, "EP=%02i: RX=%08X TX=%08X\n",
  111. i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
  112. for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++)
  113. seq_printf(s, " %04X: %08X %08X\n", j,
  114. *((u32 *)mEpRx->qh.ptr + j),
  115. *((u32 *)mEpTx->qh.ptr + j));
  116. }
  117. spin_unlock_irqrestore(&ci->lock, flags);
  118. return 0;
  119. }
  120. static int ci_qheads_open(struct inode *inode, struct file *file)
  121. {
  122. return single_open(file, ci_qheads_show, inode->i_private);
  123. }
  124. static const struct file_operations ci_qheads_fops = {
  125. .open = ci_qheads_open,
  126. .read = seq_read,
  127. .llseek = seq_lseek,
  128. .release = single_release,
  129. };
  130. /**
  131. * ci_requests_show: DMA contents of all requests currently queued (all endpts)
  132. */
  133. static int ci_requests_show(struct seq_file *s, void *data)
  134. {
  135. struct ci13xxx *ci = s->private;
  136. unsigned long flags;
  137. struct list_head *ptr = NULL;
  138. struct ci13xxx_req *req = NULL;
  139. unsigned i, j, qsize = sizeof(struct ci13xxx_td)/sizeof(u32);
  140. if (ci->role != CI_ROLE_GADGET) {
  141. seq_printf(s, "not in gadget mode\n");
  142. return 0;
  143. }
  144. spin_lock_irqsave(&ci->lock, flags);
  145. for (i = 0; i < ci->hw_ep_max; i++)
  146. list_for_each(ptr, &ci->ci13xxx_ep[i].qh.queue) {
  147. req = list_entry(ptr, struct ci13xxx_req, queue);
  148. seq_printf(s, "EP=%02i: TD=%08X %s\n",
  149. i % (ci->hw_ep_max / 2), (u32)req->dma,
  150. ((i < ci->hw_ep_max/2) ? "RX" : "TX"));
  151. for (j = 0; j < qsize; j++)
  152. seq_printf(s, " %04X: %08X\n", j,
  153. *((u32 *)req->ptr + j));
  154. }
  155. spin_unlock_irqrestore(&ci->lock, flags);
  156. return 0;
  157. }
  158. static int ci_requests_open(struct inode *inode, struct file *file)
  159. {
  160. return single_open(file, ci_requests_show, inode->i_private);
  161. }
  162. static const struct file_operations ci_requests_fops = {
  163. .open = ci_requests_open,
  164. .read = seq_read,
  165. .llseek = seq_lseek,
  166. .release = single_release,
  167. };
  168. static int ci_role_show(struct seq_file *s, void *data)
  169. {
  170. struct ci13xxx *ci = s->private;
  171. seq_printf(s, "%s\n", ci_role(ci)->name);
  172. return 0;
  173. }
  174. static ssize_t ci_role_write(struct file *file, const char __user *ubuf,
  175. size_t count, loff_t *ppos)
  176. {
  177. struct seq_file *s = file->private_data;
  178. struct ci13xxx *ci = s->private;
  179. enum ci_role role;
  180. char buf[8];
  181. int ret;
  182. if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  183. return -EFAULT;
  184. for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
  185. if (ci->roles[role] &&
  186. !strncmp(buf, ci->roles[role]->name,
  187. strlen(ci->roles[role]->name)))
  188. break;
  189. if (role == CI_ROLE_END || role == ci->role)
  190. return -EINVAL;
  191. ci_role_stop(ci);
  192. ret = ci_role_start(ci, role);
  193. return ret ? ret : count;
  194. }
  195. static int ci_role_open(struct inode *inode, struct file *file)
  196. {
  197. return single_open(file, ci_role_show, inode->i_private);
  198. }
  199. static const struct file_operations ci_role_fops = {
  200. .open = ci_role_open,
  201. .write = ci_role_write,
  202. .read = seq_read,
  203. .llseek = seq_lseek,
  204. .release = single_release,
  205. };
  206. /**
  207. * dbg_create_files: initializes the attribute interface
  208. * @ci: device
  209. *
  210. * This function returns an error code
  211. */
  212. int dbg_create_files(struct ci13xxx *ci)
  213. {
  214. struct dentry *dent;
  215. ci->debugfs = debugfs_create_dir(dev_name(ci->dev), NULL);
  216. if (!ci->debugfs)
  217. return -ENOMEM;
  218. dent = debugfs_create_file("device", S_IRUGO, ci->debugfs, ci,
  219. &ci_device_fops);
  220. if (!dent)
  221. goto err;
  222. dent = debugfs_create_file("port_test", S_IRUGO | S_IWUSR, ci->debugfs,
  223. ci, &ci_port_test_fops);
  224. if (!dent)
  225. goto err;
  226. dent = debugfs_create_file("qheads", S_IRUGO, ci->debugfs, ci,
  227. &ci_qheads_fops);
  228. if (!dent)
  229. goto err;
  230. dent = debugfs_create_file("requests", S_IRUGO, ci->debugfs, ci,
  231. &ci_requests_fops);
  232. if (!dent)
  233. goto err;
  234. dent = debugfs_create_file("role", S_IRUGO | S_IWUSR, ci->debugfs, ci,
  235. &ci_role_fops);
  236. if (dent)
  237. return 0;
  238. err:
  239. debugfs_remove_recursive(ci->debugfs);
  240. return -ENOMEM;
  241. }
  242. /**
  243. * dbg_remove_files: destroys the attribute interface
  244. * @ci: device
  245. */
  246. void dbg_remove_files(struct ci13xxx *ci)
  247. {
  248. debugfs_remove_recursive(ci->debugfs);
  249. }