debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #include <linux/kernel.h>
  2. #include <linux/device.h>
  3. #include <linux/types.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/usb/ch9.h>
  6. #include <linux/usb/gadget.h>
  7. #include "ci.h"
  8. #include "udc.h"
  9. #include "bits.h"
  10. #include "debug.h"
  11. /**
  12. * hw_register_read: reads all device registers (execute without interruption)
  13. * @buf: destination buffer
  14. * @size: buffer size
  15. *
  16. * This function returns number of registers read
  17. */
  18. static size_t hw_register_read(struct ci13xxx *ci, u32 *buf, size_t size)
  19. {
  20. unsigned i;
  21. if (size > ci->hw_bank.size)
  22. size = ci->hw_bank.size;
  23. for (i = 0; i < size; i++)
  24. buf[i] = hw_read(ci, i * sizeof(u32), ~0);
  25. return size;
  26. }
  27. /**
  28. * hw_register_write: writes to register
  29. * @addr: register address
  30. * @data: register value
  31. *
  32. * This function returns an error code
  33. */
  34. static int hw_register_write(struct ci13xxx *ci, u16 addr, u32 data)
  35. {
  36. /* align */
  37. addr /= sizeof(u32);
  38. if (addr >= ci->hw_bank.size)
  39. return -EINVAL;
  40. /* align */
  41. addr *= sizeof(u32);
  42. hw_write(ci, addr, ~0, data);
  43. return 0;
  44. }
  45. /**
  46. * hw_intr_clear: disables interrupt & clears interrupt status (execute without
  47. * interruption)
  48. * @n: interrupt bit
  49. *
  50. * This function returns an error code
  51. */
  52. static int hw_intr_clear(struct ci13xxx *ci, int n)
  53. {
  54. if (n >= REG_BITS)
  55. return -EINVAL;
  56. hw_write(ci, OP_USBINTR, BIT(n), 0);
  57. hw_write(ci, OP_USBSTS, BIT(n), BIT(n));
  58. return 0;
  59. }
  60. /**
  61. * hw_intr_force: enables interrupt & forces interrupt status (execute without
  62. * interruption)
  63. * @n: interrupt bit
  64. *
  65. * This function returns an error code
  66. */
  67. static int hw_intr_force(struct ci13xxx *ci, int n)
  68. {
  69. if (n >= REG_BITS)
  70. return -EINVAL;
  71. hw_write(ci, CAP_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
  72. hw_write(ci, OP_USBINTR, BIT(n), BIT(n));
  73. hw_write(ci, OP_USBSTS, BIT(n), BIT(n));
  74. hw_write(ci, CAP_TESTMODE, TESTMODE_FORCE, 0);
  75. return 0;
  76. }
  77. /**
  78. * show_device: prints information about device capabilities and status
  79. *
  80. * Check "device.h" for details
  81. */
  82. static ssize_t show_device(struct device *dev, struct device_attribute *attr,
  83. char *buf)
  84. {
  85. struct ci13xxx *ci = container_of(dev, struct ci13xxx, gadget.dev);
  86. struct usb_gadget *gadget = &ci->gadget;
  87. int n = 0;
  88. if (attr == NULL || buf == NULL) {
  89. dev_err(ci->dev, "[%s] EINVAL\n", __func__);
  90. return 0;
  91. }
  92. n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n",
  93. gadget->speed);
  94. n += scnprintf(buf + n, PAGE_SIZE - n, "max_speed = %d\n",
  95. gadget->max_speed);
  96. n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n",
  97. gadget->is_otg);
  98. n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n",
  99. gadget->is_a_peripheral);
  100. n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n",
  101. gadget->b_hnp_enable);
  102. n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n",
  103. gadget->a_hnp_support);
  104. n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
  105. gadget->a_alt_hnp_support);
  106. n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n",
  107. (gadget->name ? gadget->name : ""));
  108. return n;
  109. }
  110. static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);
  111. /**
  112. * show_driver: prints information about attached gadget (if any)
  113. *
  114. * Check "device.h" for details
  115. */
  116. static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
  117. char *buf)
  118. {
  119. struct ci13xxx *ci = container_of(dev, struct ci13xxx, gadget.dev);
  120. struct usb_gadget_driver *driver = ci->driver;
  121. int n = 0;
  122. if (attr == NULL || buf == NULL) {
  123. dev_err(dev, "[%s] EINVAL\n", __func__);
  124. return 0;
  125. }
  126. if (driver == NULL)
  127. return scnprintf(buf, PAGE_SIZE,
  128. "There is no gadget attached!\n");
  129. n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n",
  130. (driver->function ? driver->function : ""));
  131. n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
  132. driver->max_speed);
  133. return n;
  134. }
  135. static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);
  136. /**
  137. * show_port_test: reads port test mode
  138. *
  139. * Check "device.h" for details
  140. */
  141. static ssize_t show_port_test(struct device *dev,
  142. struct device_attribute *attr, char *buf)
  143. {
  144. struct ci13xxx *ci = container_of(dev, struct ci13xxx, gadget.dev);
  145. unsigned long flags;
  146. unsigned mode;
  147. if (attr == NULL || buf == NULL) {
  148. dev_err(ci->dev, "EINVAL\n");
  149. return 0;
  150. }
  151. spin_lock_irqsave(&ci->lock, flags);
  152. mode = hw_port_test_get(ci);
  153. spin_unlock_irqrestore(&ci->lock, flags);
  154. return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
  155. }
  156. /**
  157. * store_port_test: writes port test mode
  158. *
  159. * Check "device.h" for details
  160. */
  161. static ssize_t store_port_test(struct device *dev,
  162. struct device_attribute *attr,
  163. const char *buf, size_t count)
  164. {
  165. struct ci13xxx *ci = container_of(dev, struct ci13xxx, gadget.dev);
  166. unsigned long flags;
  167. unsigned mode;
  168. if (attr == NULL || buf == NULL) {
  169. dev_err(ci->dev, "[%s] EINVAL\n", __func__);
  170. goto done;
  171. }
  172. if (sscanf(buf, "%u", &mode) != 1) {
  173. dev_err(ci->dev, "<mode>: set port test mode");
  174. goto done;
  175. }
  176. spin_lock_irqsave(&ci->lock, flags);
  177. if (hw_port_test_set(ci, mode))
  178. dev_err(ci->dev, "invalid mode\n");
  179. spin_unlock_irqrestore(&ci->lock, flags);
  180. done:
  181. return count;
  182. }
  183. static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
  184. show_port_test, store_port_test);
  185. /**
  186. * show_qheads: DMA contents of all queue heads
  187. *
  188. * Check "device.h" for details
  189. */
  190. static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct ci13xxx *ci = container_of(dev, struct ci13xxx, gadget.dev);
  194. unsigned long flags;
  195. unsigned i, j, n = 0;
  196. if (attr == NULL || buf == NULL) {
  197. dev_err(ci->dev, "[%s] EINVAL\n", __func__);
  198. return 0;
  199. }
  200. spin_lock_irqsave(&ci->lock, flags);
  201. for (i = 0; i < ci->hw_ep_max/2; i++) {
  202. struct ci13xxx_ep *mEpRx = &ci->ci13xxx_ep[i];
  203. struct ci13xxx_ep *mEpTx =
  204. &ci->ci13xxx_ep[i + ci->hw_ep_max/2];
  205. n += scnprintf(buf + n, PAGE_SIZE - n,
  206. "EP=%02i: RX=%08X TX=%08X\n",
  207. i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
  208. for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
  209. n += scnprintf(buf + n, PAGE_SIZE - n,
  210. " %04X: %08X %08X\n", j,
  211. *((u32 *)mEpRx->qh.ptr + j),
  212. *((u32 *)mEpTx->qh.ptr + j));
  213. }
  214. }
  215. spin_unlock_irqrestore(&ci->lock, flags);
  216. return n;
  217. }
  218. static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);
  219. /**
  220. * show_registers: dumps all registers
  221. *
  222. * Check "device.h" for details
  223. */
  224. #define DUMP_ENTRIES 512
  225. static ssize_t show_registers(struct device *dev,
  226. struct device_attribute *attr, char *buf)
  227. {
  228. struct ci13xxx *ci = container_of(dev, struct ci13xxx, gadget.dev);
  229. unsigned long flags;
  230. u32 *dump;
  231. unsigned i, k, n = 0;
  232. if (attr == NULL || buf == NULL) {
  233. dev_err(ci->dev, "[%s] EINVAL\n", __func__);
  234. return 0;
  235. }
  236. dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL);
  237. if (!dump) {
  238. dev_err(ci->dev, "%s: out of memory\n", __func__);
  239. return 0;
  240. }
  241. spin_lock_irqsave(&ci->lock, flags);
  242. k = hw_register_read(ci, dump, DUMP_ENTRIES);
  243. spin_unlock_irqrestore(&ci->lock, flags);
  244. for (i = 0; i < k; i++) {
  245. n += scnprintf(buf + n, PAGE_SIZE - n,
  246. "reg[0x%04X] = 0x%08X\n",
  247. i * (unsigned)sizeof(u32), dump[i]);
  248. }
  249. kfree(dump);
  250. return n;
  251. }
  252. /**
  253. * store_registers: writes value to register address
  254. *
  255. * Check "device.h" for details
  256. */
  257. static ssize_t store_registers(struct device *dev,
  258. struct device_attribute *attr,
  259. const char *buf, size_t count)
  260. {
  261. struct ci13xxx *ci = container_of(dev, struct ci13xxx, gadget.dev);
  262. unsigned long addr, data, flags;
  263. if (attr == NULL || buf == NULL) {
  264. dev_err(ci->dev, "[%s] EINVAL\n", __func__);
  265. goto done;
  266. }
  267. if (sscanf(buf, "%li %li", &addr, &data) != 2) {
  268. dev_err(ci->dev,
  269. "<addr> <data>: write data to register address\n");
  270. goto done;
  271. }
  272. spin_lock_irqsave(&ci->lock, flags);
  273. if (hw_register_write(ci, addr, data))
  274. dev_err(ci->dev, "invalid address range\n");
  275. spin_unlock_irqrestore(&ci->lock, flags);
  276. done:
  277. return count;
  278. }
  279. static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
  280. show_registers, store_registers);
  281. /**
  282. * show_requests: DMA contents of all requests currently queued (all endpts)
  283. *
  284. * Check "device.h" for details
  285. */
  286. static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
  287. char *buf)
  288. {
  289. struct ci13xxx *ci = container_of(dev, struct ci13xxx, gadget.dev);
  290. unsigned long flags;
  291. struct list_head *ptr = NULL;
  292. struct ci13xxx_req *req = NULL;
  293. unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
  294. if (attr == NULL || buf == NULL) {
  295. dev_err(ci->dev, "[%s] EINVAL\n", __func__);
  296. return 0;
  297. }
  298. spin_lock_irqsave(&ci->lock, flags);
  299. for (i = 0; i < ci->hw_ep_max; i++)
  300. list_for_each(ptr, &ci->ci13xxx_ep[i].qh.queue)
  301. {
  302. req = list_entry(ptr, struct ci13xxx_req, queue);
  303. n += scnprintf(buf + n, PAGE_SIZE - n,
  304. "EP=%02i: TD=%08X %s\n",
  305. i % ci->hw_ep_max/2, (u32)req->dma,
  306. ((i < ci->hw_ep_max/2) ? "RX" : "TX"));
  307. for (j = 0; j < qSize; j++)
  308. n += scnprintf(buf + n, PAGE_SIZE - n,
  309. " %04X: %08X\n", j,
  310. *((u32 *)req->ptr + j));
  311. }
  312. spin_unlock_irqrestore(&ci->lock, flags);
  313. return n;
  314. }
  315. static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);
  316. /**
  317. * dbg_create_files: initializes the attribute interface
  318. * @dev: device
  319. *
  320. * This function returns an error code
  321. */
  322. int dbg_create_files(struct device *dev)
  323. {
  324. int retval = 0;
  325. if (dev == NULL)
  326. return -EINVAL;
  327. retval = device_create_file(dev, &dev_attr_device);
  328. if (retval)
  329. goto done;
  330. retval = device_create_file(dev, &dev_attr_driver);
  331. if (retval)
  332. goto rm_device;
  333. retval = device_create_file(dev, &dev_attr_port_test);
  334. if (retval)
  335. goto rm_driver;
  336. retval = device_create_file(dev, &dev_attr_qheads);
  337. if (retval)
  338. goto rm_port_test;
  339. retval = device_create_file(dev, &dev_attr_registers);
  340. if (retval)
  341. goto rm_qheads;
  342. retval = device_create_file(dev, &dev_attr_requests);
  343. if (retval)
  344. goto rm_registers;
  345. return 0;
  346. rm_registers:
  347. device_remove_file(dev, &dev_attr_registers);
  348. rm_qheads:
  349. device_remove_file(dev, &dev_attr_qheads);
  350. rm_port_test:
  351. device_remove_file(dev, &dev_attr_port_test);
  352. rm_driver:
  353. device_remove_file(dev, &dev_attr_driver);
  354. rm_device:
  355. device_remove_file(dev, &dev_attr_device);
  356. done:
  357. return retval;
  358. }
  359. /**
  360. * dbg_remove_files: destroys the attribute interface
  361. * @dev: device
  362. *
  363. * This function returns an error code
  364. */
  365. int dbg_remove_files(struct device *dev)
  366. {
  367. if (dev == NULL)
  368. return -EINVAL;
  369. device_remove_file(dev, &dev_attr_requests);
  370. device_remove_file(dev, &dev_attr_registers);
  371. device_remove_file(dev, &dev_attr_qheads);
  372. device_remove_file(dev, &dev_attr_port_test);
  373. device_remove_file(dev, &dev_attr_driver);
  374. device_remove_file(dev, &dev_attr_device);
  375. return 0;
  376. }