f_serial.c 8.1 KB

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