f_serial.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * f_serial.c - generic USB serial function driver
  3. *
  4. * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
  5. * Copyright (C) 2008 by David Brownell
  6. * Copyright (C) 2008 by Nokia Corporation
  7. *
  8. * This software is distributed under the terms of the GNU General
  9. * Public License ("GPL") as published by the Free Software Foundation,
  10. * either version 2 of that License or (at your option) any later version.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/device.h>
  15. #include "u_serial.h"
  16. #include "gadget_chips.h"
  17. /*
  18. * This function packages a simple "generic serial" port with no real
  19. * control mechanisms, just raw data transfer over two bulk endpoints.
  20. *
  21. * Because it's not standardized, this isn't as interoperable as the
  22. * CDC ACM driver. However, for many purposes it's just as functional
  23. * if you can arrange appropriate host side drivers.
  24. */
  25. struct f_gser {
  26. struct gserial port;
  27. u8 data_id;
  28. u8 port_num;
  29. };
  30. static inline struct f_gser *func_to_gser(struct usb_function *f)
  31. {
  32. return container_of(f, struct f_gser, port.func);
  33. }
  34. /*-------------------------------------------------------------------------*/
  35. /* interface descriptor: */
  36. static struct usb_interface_descriptor gser_interface_desc __initdata = {
  37. .bLength = USB_DT_INTERFACE_SIZE,
  38. .bDescriptorType = USB_DT_INTERFACE,
  39. /* .bInterfaceNumber = DYNAMIC */
  40. .bNumEndpoints = 2,
  41. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  42. .bInterfaceSubClass = 0,
  43. .bInterfaceProtocol = 0,
  44. /* .iInterface = DYNAMIC */
  45. };
  46. /* full speed support: */
  47. static struct usb_endpoint_descriptor gser_fs_in_desc __initdata = {
  48. .bLength = USB_DT_ENDPOINT_SIZE,
  49. .bDescriptorType = USB_DT_ENDPOINT,
  50. .bEndpointAddress = USB_DIR_IN,
  51. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  52. };
  53. static struct usb_endpoint_descriptor gser_fs_out_desc __initdata = {
  54. .bLength = USB_DT_ENDPOINT_SIZE,
  55. .bDescriptorType = USB_DT_ENDPOINT,
  56. .bEndpointAddress = USB_DIR_OUT,
  57. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  58. };
  59. static struct usb_descriptor_header *gser_fs_function[] __initdata = {
  60. (struct usb_descriptor_header *) &gser_interface_desc,
  61. (struct usb_descriptor_header *) &gser_fs_in_desc,
  62. (struct usb_descriptor_header *) &gser_fs_out_desc,
  63. NULL,
  64. };
  65. /* high speed support: */
  66. static struct usb_endpoint_descriptor gser_hs_in_desc __initdata = {
  67. .bLength = USB_DT_ENDPOINT_SIZE,
  68. .bDescriptorType = USB_DT_ENDPOINT,
  69. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  70. .wMaxPacketSize = cpu_to_le16(512),
  71. };
  72. static struct usb_endpoint_descriptor gser_hs_out_desc __initdata = {
  73. .bLength = USB_DT_ENDPOINT_SIZE,
  74. .bDescriptorType = USB_DT_ENDPOINT,
  75. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  76. .wMaxPacketSize = cpu_to_le16(512),
  77. };
  78. static struct usb_descriptor_header *gser_hs_function[] __initdata = {
  79. (struct usb_descriptor_header *) &gser_interface_desc,
  80. (struct usb_descriptor_header *) &gser_hs_in_desc,
  81. (struct usb_descriptor_header *) &gser_hs_out_desc,
  82. NULL,
  83. };
  84. static struct usb_endpoint_descriptor gser_ss_in_desc __initdata = {
  85. .bLength = USB_DT_ENDPOINT_SIZE,
  86. .bDescriptorType = USB_DT_ENDPOINT,
  87. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  88. .wMaxPacketSize = cpu_to_le16(1024),
  89. };
  90. static struct usb_endpoint_descriptor gser_ss_out_desc __initdata = {
  91. .bLength = USB_DT_ENDPOINT_SIZE,
  92. .bDescriptorType = USB_DT_ENDPOINT,
  93. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  94. .wMaxPacketSize = cpu_to_le16(1024),
  95. };
  96. static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc __initdata = {
  97. .bLength = sizeof gser_ss_bulk_comp_desc,
  98. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  99. };
  100. static struct usb_descriptor_header *gser_ss_function[] __initdata = {
  101. (struct usb_descriptor_header *) &gser_interface_desc,
  102. (struct usb_descriptor_header *) &gser_ss_in_desc,
  103. (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
  104. (struct usb_descriptor_header *) &gser_ss_out_desc,
  105. (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
  106. NULL,
  107. };
  108. /* string descriptors: */
  109. static struct usb_string gser_string_defs[] = {
  110. [0].s = "Generic Serial",
  111. { } /* end of list */
  112. };
  113. static struct usb_gadget_strings gser_string_table = {
  114. .language = 0x0409, /* en-us */
  115. .strings = gser_string_defs,
  116. };
  117. static struct usb_gadget_strings *gser_strings[] = {
  118. &gser_string_table,
  119. NULL,
  120. };
  121. /*-------------------------------------------------------------------------*/
  122. static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  123. {
  124. struct f_gser *gser = func_to_gser(f);
  125. struct usb_composite_dev *cdev = f->config->cdev;
  126. /* we know alt == 0, so this is an activation or a reset */
  127. if (gser->port.in->driver_data) {
  128. DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
  129. gserial_disconnect(&gser->port);
  130. }
  131. if (!gser->port.in->desc || !gser->port.out->desc) {
  132. DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
  133. if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
  134. config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
  135. gser->port.in->desc = NULL;
  136. gser->port.out->desc = NULL;
  137. return -EINVAL;
  138. }
  139. }
  140. gserial_connect(&gser->port, gser->port_num);
  141. return 0;
  142. }
  143. static void gser_disable(struct usb_function *f)
  144. {
  145. struct f_gser *gser = func_to_gser(f);
  146. struct usb_composite_dev *cdev = f->config->cdev;
  147. DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
  148. gserial_disconnect(&gser->port);
  149. }
  150. /*-------------------------------------------------------------------------*/
  151. /* serial function driver setup/binding */
  152. static int __init
  153. gser_bind(struct usb_configuration *c, struct usb_function *f)
  154. {
  155. struct usb_composite_dev *cdev = c->cdev;
  156. struct f_gser *gser = func_to_gser(f);
  157. int status;
  158. struct usb_ep *ep;
  159. /* allocate instance-specific interface IDs */
  160. status = usb_interface_id(c, f);
  161. if (status < 0)
  162. goto fail;
  163. gser->data_id = status;
  164. gser_interface_desc.bInterfaceNumber = status;
  165. status = -ENODEV;
  166. /* allocate instance-specific endpoints */
  167. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
  168. if (!ep)
  169. goto fail;
  170. gser->port.in = ep;
  171. ep->driver_data = cdev; /* claim */
  172. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
  173. if (!ep)
  174. goto fail;
  175. gser->port.out = ep;
  176. ep->driver_data = cdev; /* claim */
  177. /* copy descriptors, and track endpoint copies */
  178. f->descriptors = usb_copy_descriptors(gser_fs_function);
  179. /* support all relevant hardware speeds... we expect that when
  180. * hardware is dual speed, all bulk-capable endpoints work at
  181. * both speeds
  182. */
  183. if (gadget_is_dualspeed(c->cdev->gadget)) {
  184. gser_hs_in_desc.bEndpointAddress =
  185. gser_fs_in_desc.bEndpointAddress;
  186. gser_hs_out_desc.bEndpointAddress =
  187. gser_fs_out_desc.bEndpointAddress;
  188. /* copy descriptors, and track endpoint copies */
  189. f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
  190. }
  191. if (gadget_is_superspeed(c->cdev->gadget)) {
  192. gser_ss_in_desc.bEndpointAddress =
  193. gser_fs_in_desc.bEndpointAddress;
  194. gser_ss_out_desc.bEndpointAddress =
  195. gser_fs_out_desc.bEndpointAddress;
  196. /* copy descriptors, and track endpoint copies */
  197. f->ss_descriptors = usb_copy_descriptors(gser_ss_function);
  198. if (!f->ss_descriptors)
  199. goto fail;
  200. }
  201. DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
  202. gser->port_num,
  203. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  204. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  205. gser->port.in->name, gser->port.out->name);
  206. return 0;
  207. fail:
  208. /* we might as well release our claims on endpoints */
  209. if (gser->port.out)
  210. gser->port.out->driver_data = NULL;
  211. if (gser->port.in)
  212. gser->port.in->driver_data = NULL;
  213. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  214. return status;
  215. }
  216. static void
  217. gser_unbind(struct usb_configuration *c, struct usb_function *f)
  218. {
  219. if (gadget_is_dualspeed(c->cdev->gadget))
  220. usb_free_descriptors(f->hs_descriptors);
  221. if (gadget_is_superspeed(c->cdev->gadget))
  222. usb_free_descriptors(f->ss_descriptors);
  223. usb_free_descriptors(f->descriptors);
  224. kfree(func_to_gser(f));
  225. }
  226. /**
  227. * gser_bind_config - add a generic serial function to a configuration
  228. * @c: the configuration to support the serial instance
  229. * @port_num: /dev/ttyGS* port this interface will use
  230. * Context: single threaded during gadget setup
  231. *
  232. * Returns zero on success, else negative errno.
  233. *
  234. * Caller must have called @gserial_setup() with enough ports to
  235. * handle all the ones it binds. Caller is also responsible
  236. * for calling @gserial_cleanup() before module unload.
  237. */
  238. int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
  239. {
  240. struct f_gser *gser;
  241. int status;
  242. /* REVISIT might want instance-specific strings to help
  243. * distinguish instances ...
  244. */
  245. /* maybe allocate device-global string ID */
  246. if (gser_string_defs[0].id == 0) {
  247. status = usb_string_id(c->cdev);
  248. if (status < 0)
  249. return status;
  250. gser_string_defs[0].id = status;
  251. }
  252. /* allocate and initialize one new instance */
  253. gser = kzalloc(sizeof *gser, GFP_KERNEL);
  254. if (!gser)
  255. return -ENOMEM;
  256. gser->port_num = port_num;
  257. gser->port.func.name = "gser";
  258. gser->port.func.strings = gser_strings;
  259. gser->port.func.bind = gser_bind;
  260. gser->port.func.unbind = gser_unbind;
  261. gser->port.func.set_alt = gser_set_alt;
  262. gser->port.func.disable = gser_disable;
  263. status = usb_add_function(c, &gser->port.func);
  264. if (status)
  265. kfree(gser);
  266. return status;
  267. }