dbgp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dbgp.c -- EHCI Debug Port device gadget
  4. *
  5. * Copyright (C) 2010 Stephane Duverger
  6. *
  7. * Released under the GPLv2.
  8. */
  9. /* verbose messages */
  10. #include <linux/kernel.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/usb/ch9.h>
  14. #include <linux/usb/gadget.h>
  15. #include "u_serial.h"
  16. #define DRIVER_VENDOR_ID 0x0525 /* NetChip */
  17. #define DRIVER_PRODUCT_ID 0xc0de /* undefined */
  18. #define USB_DEBUG_MAX_PACKET_SIZE 8
  19. #define DBGP_REQ_EP0_LEN 128
  20. #define DBGP_REQ_LEN 512
  21. static struct dbgp {
  22. struct usb_gadget *gadget;
  23. struct usb_request *req;
  24. struct usb_ep *i_ep;
  25. struct usb_ep *o_ep;
  26. #ifdef CONFIG_USB_G_DBGP_SERIAL
  27. struct gserial *serial;
  28. #endif
  29. } dbgp;
  30. static struct usb_device_descriptor device_desc = {
  31. .bLength = sizeof device_desc,
  32. .bDescriptorType = USB_DT_DEVICE,
  33. .bcdUSB = cpu_to_le16(0x0200),
  34. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  35. .idVendor = cpu_to_le16(DRIVER_VENDOR_ID),
  36. .idProduct = cpu_to_le16(DRIVER_PRODUCT_ID),
  37. .bNumConfigurations = 1,
  38. };
  39. static struct usb_debug_descriptor dbg_desc = {
  40. .bLength = sizeof dbg_desc,
  41. .bDescriptorType = USB_DT_DEBUG,
  42. };
  43. static struct usb_endpoint_descriptor i_desc = {
  44. .bLength = USB_DT_ENDPOINT_SIZE,
  45. .bDescriptorType = USB_DT_ENDPOINT,
  46. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  47. .bEndpointAddress = USB_DIR_IN,
  48. };
  49. static struct usb_endpoint_descriptor o_desc = {
  50. .bLength = USB_DT_ENDPOINT_SIZE,
  51. .bDescriptorType = USB_DT_ENDPOINT,
  52. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  53. .bEndpointAddress = USB_DIR_OUT,
  54. };
  55. #ifdef CONFIG_USB_G_DBGP_PRINTK
  56. static int dbgp_consume(char *buf, unsigned len)
  57. {
  58. char c;
  59. if (!len)
  60. return 0;
  61. c = buf[len-1];
  62. if (c != 0)
  63. buf[len-1] = 0;
  64. printk(KERN_NOTICE "%s%c", buf, c);
  65. return 0;
  66. }
  67. static void __disable_ep(struct usb_ep *ep)
  68. {
  69. usb_ep_disable(ep);
  70. }
  71. static void dbgp_disable_ep(void)
  72. {
  73. __disable_ep(dbgp.i_ep);
  74. __disable_ep(dbgp.o_ep);
  75. }
  76. static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
  77. {
  78. int stp;
  79. int err = 0;
  80. int status = req->status;
  81. if (ep == dbgp.i_ep) {
  82. stp = 1;
  83. goto fail;
  84. }
  85. if (status != 0) {
  86. stp = 2;
  87. goto release_req;
  88. }
  89. dbgp_consume(req->buf, req->actual);
  90. req->length = DBGP_REQ_LEN;
  91. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  92. if (err < 0) {
  93. stp = 3;
  94. goto release_req;
  95. }
  96. return;
  97. release_req:
  98. kfree(req->buf);
  99. usb_ep_free_request(dbgp.o_ep, req);
  100. dbgp_disable_ep();
  101. fail:
  102. dev_dbg(&dbgp.gadget->dev,
  103. "complete: failure (%d:%d) ==> %d\n", stp, err, status);
  104. }
  105. static int dbgp_enable_ep_req(struct usb_ep *ep)
  106. {
  107. int err, stp;
  108. struct usb_request *req;
  109. req = usb_ep_alloc_request(ep, GFP_KERNEL);
  110. if (!req) {
  111. err = -ENOMEM;
  112. stp = 1;
  113. goto fail_1;
  114. }
  115. req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL);
  116. if (!req->buf) {
  117. err = -ENOMEM;
  118. stp = 2;
  119. goto fail_2;
  120. }
  121. req->complete = dbgp_complete;
  122. req->length = DBGP_REQ_LEN;
  123. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  124. if (err < 0) {
  125. stp = 3;
  126. goto fail_3;
  127. }
  128. return 0;
  129. fail_3:
  130. kfree(req->buf);
  131. fail_2:
  132. usb_ep_free_request(dbgp.o_ep, req);
  133. fail_1:
  134. dev_dbg(&dbgp.gadget->dev,
  135. "enable ep req: failure (%d:%d)\n", stp, err);
  136. return err;
  137. }
  138. static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
  139. {
  140. int err;
  141. ep->desc = desc;
  142. err = usb_ep_enable(ep);
  143. return err;
  144. }
  145. static int dbgp_enable_ep(void)
  146. {
  147. int err, stp;
  148. err = __enable_ep(dbgp.i_ep, &i_desc);
  149. if (err < 0) {
  150. stp = 1;
  151. goto fail_1;
  152. }
  153. err = __enable_ep(dbgp.o_ep, &o_desc);
  154. if (err < 0) {
  155. stp = 2;
  156. goto fail_2;
  157. }
  158. err = dbgp_enable_ep_req(dbgp.o_ep);
  159. if (err < 0) {
  160. stp = 3;
  161. goto fail_3;
  162. }
  163. return 0;
  164. fail_3:
  165. __disable_ep(dbgp.o_ep);
  166. fail_2:
  167. __disable_ep(dbgp.i_ep);
  168. fail_1:
  169. dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
  170. return err;
  171. }
  172. #endif
  173. static void dbgp_disconnect(struct usb_gadget *gadget)
  174. {
  175. #ifdef CONFIG_USB_G_DBGP_PRINTK
  176. dbgp_disable_ep();
  177. #else
  178. gserial_disconnect(dbgp.serial);
  179. #endif
  180. }
  181. static void dbgp_unbind(struct usb_gadget *gadget)
  182. {
  183. #ifdef CONFIG_USB_G_DBGP_SERIAL
  184. kfree(dbgp.serial);
  185. dbgp.serial = NULL;
  186. #endif
  187. if (dbgp.req) {
  188. kfree(dbgp.req->buf);
  189. usb_ep_free_request(gadget->ep0, dbgp.req);
  190. dbgp.req = NULL;
  191. }
  192. }
  193. #ifdef CONFIG_USB_G_DBGP_SERIAL
  194. static unsigned char tty_line;
  195. #endif
  196. static int dbgp_configure_endpoints(struct usb_gadget *gadget)
  197. {
  198. int stp;
  199. usb_ep_autoconfig_reset(gadget);
  200. dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc);
  201. if (!dbgp.i_ep) {
  202. stp = 1;
  203. goto fail_1;
  204. }
  205. i_desc.wMaxPacketSize =
  206. cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
  207. dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc);
  208. if (!dbgp.o_ep) {
  209. stp = 2;
  210. goto fail_1;
  211. }
  212. o_desc.wMaxPacketSize =
  213. cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
  214. dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress;
  215. dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress;
  216. #ifdef CONFIG_USB_G_DBGP_SERIAL
  217. dbgp.serial->in = dbgp.i_ep;
  218. dbgp.serial->out = dbgp.o_ep;
  219. dbgp.serial->in->desc = &i_desc;
  220. dbgp.serial->out->desc = &o_desc;
  221. #endif
  222. return 0;
  223. fail_1:
  224. dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
  225. return -ENODEV;
  226. }
  227. static int dbgp_bind(struct usb_gadget *gadget,
  228. struct usb_gadget_driver *driver)
  229. {
  230. int err, stp;
  231. dbgp.gadget = gadget;
  232. dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  233. if (!dbgp.req) {
  234. err = -ENOMEM;
  235. stp = 1;
  236. goto fail;
  237. }
  238. dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
  239. if (!dbgp.req->buf) {
  240. err = -ENOMEM;
  241. stp = 2;
  242. goto fail;
  243. }
  244. dbgp.req->length = DBGP_REQ_EP0_LEN;
  245. #ifdef CONFIG_USB_G_DBGP_SERIAL
  246. dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
  247. if (!dbgp.serial) {
  248. stp = 3;
  249. err = -ENOMEM;
  250. goto fail;
  251. }
  252. if (gserial_alloc_line(&tty_line)) {
  253. stp = 4;
  254. err = -ENODEV;
  255. goto fail;
  256. }
  257. #endif
  258. err = dbgp_configure_endpoints(gadget);
  259. if (err < 0) {
  260. stp = 5;
  261. goto fail;
  262. }
  263. dev_dbg(&dbgp.gadget->dev, "bind: success\n");
  264. return 0;
  265. fail:
  266. dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
  267. dbgp_unbind(gadget);
  268. return err;
  269. }
  270. static void dbgp_setup_complete(struct usb_ep *ep,
  271. struct usb_request *req)
  272. {
  273. dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
  274. req->status, req->actual, req->length);
  275. }
  276. static int dbgp_setup(struct usb_gadget *gadget,
  277. const struct usb_ctrlrequest *ctrl)
  278. {
  279. struct usb_request *req = dbgp.req;
  280. u8 request = ctrl->bRequest;
  281. u16 value = le16_to_cpu(ctrl->wValue);
  282. u16 length = le16_to_cpu(ctrl->wLength);
  283. int err = -EOPNOTSUPP;
  284. void *data = NULL;
  285. u16 len = 0;
  286. if (request == USB_REQ_GET_DESCRIPTOR) {
  287. switch (value>>8) {
  288. case USB_DT_DEVICE:
  289. dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
  290. len = sizeof device_desc;
  291. data = &device_desc;
  292. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  293. break;
  294. case USB_DT_DEBUG:
  295. dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
  296. len = sizeof dbg_desc;
  297. data = &dbg_desc;
  298. break;
  299. default:
  300. goto fail;
  301. }
  302. err = 0;
  303. } else if (request == USB_REQ_SET_FEATURE &&
  304. value == USB_DEVICE_DEBUG_MODE) {
  305. dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
  306. #ifdef CONFIG_USB_G_DBGP_PRINTK
  307. err = dbgp_enable_ep();
  308. #else
  309. err = dbgp_configure_endpoints(gadget);
  310. if (err < 0) {
  311. goto fail;
  312. }
  313. err = gserial_connect(dbgp.serial, tty_line);
  314. #endif
  315. if (err < 0)
  316. goto fail;
  317. } else
  318. goto fail;
  319. req->length = min(length, len);
  320. req->zero = len < req->length;
  321. if (data && req->length)
  322. memcpy(req->buf, data, req->length);
  323. req->complete = dbgp_setup_complete;
  324. return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  325. fail:
  326. dev_dbg(&dbgp.gadget->dev,
  327. "setup: failure req %x v %x\n", request, value);
  328. return err;
  329. }
  330. static struct usb_gadget_driver dbgp_driver = {
  331. .function = "dbgp",
  332. .max_speed = USB_SPEED_HIGH,
  333. .bind = dbgp_bind,
  334. .unbind = dbgp_unbind,
  335. .setup = dbgp_setup,
  336. .reset = dbgp_disconnect,
  337. .disconnect = dbgp_disconnect,
  338. .driver = {
  339. .owner = THIS_MODULE,
  340. .name = "dbgp"
  341. },
  342. };
  343. static int __init dbgp_init(void)
  344. {
  345. return usb_gadget_probe_driver(&dbgp_driver);
  346. }
  347. static void __exit dbgp_exit(void)
  348. {
  349. usb_gadget_unregister_driver(&dbgp_driver);
  350. #ifdef CONFIG_USB_G_DBGP_SERIAL
  351. gserial_free_line(tty_line);
  352. #endif
  353. }
  354. MODULE_AUTHOR("Stephane Duverger");
  355. MODULE_LICENSE("GPL");
  356. module_init(dbgp_init);
  357. module_exit(dbgp_exit);