dbgp.c 8.1 KB

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