f_serial.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /* string descriptors: */
  85. static struct usb_string gser_string_defs[] = {
  86. [0].s = "Generic Serial",
  87. { } /* end of list */
  88. };
  89. static struct usb_gadget_strings gser_string_table = {
  90. .language = 0x0409, /* en-us */
  91. .strings = gser_string_defs,
  92. };
  93. static struct usb_gadget_strings *gser_strings[] = {
  94. &gser_string_table,
  95. NULL,
  96. };
  97. /*-------------------------------------------------------------------------*/
  98. static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  99. {
  100. struct f_gser *gser = func_to_gser(f);
  101. struct usb_composite_dev *cdev = f->config->cdev;
  102. /* we know alt == 0, so this is an activation or a reset */
  103. if (gser->port.in->driver_data) {
  104. DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
  105. gserial_disconnect(&gser->port);
  106. }
  107. if (!gser->port.in->desc || !gser->port.out->desc) {
  108. DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
  109. if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
  110. config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
  111. gser->port.in->desc = NULL;
  112. gser->port.out->desc = NULL;
  113. return -EINVAL;
  114. }
  115. }
  116. gserial_connect(&gser->port, gser->port_num);
  117. return 0;
  118. }
  119. static void gser_disable(struct usb_function *f)
  120. {
  121. struct f_gser *gser = func_to_gser(f);
  122. struct usb_composite_dev *cdev = f->config->cdev;
  123. DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
  124. gserial_disconnect(&gser->port);
  125. }
  126. /*-------------------------------------------------------------------------*/
  127. /* serial function driver setup/binding */
  128. static int __init
  129. gser_bind(struct usb_configuration *c, struct usb_function *f)
  130. {
  131. struct usb_composite_dev *cdev = c->cdev;
  132. struct f_gser *gser = func_to_gser(f);
  133. int status;
  134. struct usb_ep *ep;
  135. /* allocate instance-specific interface IDs */
  136. status = usb_interface_id(c, f);
  137. if (status < 0)
  138. goto fail;
  139. gser->data_id = status;
  140. gser_interface_desc.bInterfaceNumber = status;
  141. status = -ENODEV;
  142. /* allocate instance-specific endpoints */
  143. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
  144. if (!ep)
  145. goto fail;
  146. gser->port.in = ep;
  147. ep->driver_data = cdev; /* claim */
  148. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
  149. if (!ep)
  150. goto fail;
  151. gser->port.out = ep;
  152. ep->driver_data = cdev; /* claim */
  153. /* copy descriptors, and track endpoint copies */
  154. f->descriptors = usb_copy_descriptors(gser_fs_function);
  155. /* support all relevant hardware speeds... we expect that when
  156. * hardware is dual speed, all bulk-capable endpoints work at
  157. * both speeds
  158. */
  159. if (gadget_is_dualspeed(c->cdev->gadget)) {
  160. gser_hs_in_desc.bEndpointAddress =
  161. gser_fs_in_desc.bEndpointAddress;
  162. gser_hs_out_desc.bEndpointAddress =
  163. gser_fs_out_desc.bEndpointAddress;
  164. /* copy descriptors, and track endpoint copies */
  165. f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
  166. }
  167. DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
  168. gser->port_num,
  169. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  170. gser->port.in->name, gser->port.out->name);
  171. return 0;
  172. fail:
  173. /* we might as well release our claims on endpoints */
  174. if (gser->port.out)
  175. gser->port.out->driver_data = NULL;
  176. if (gser->port.in)
  177. gser->port.in->driver_data = NULL;
  178. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  179. return status;
  180. }
  181. static void
  182. gser_unbind(struct usb_configuration *c, struct usb_function *f)
  183. {
  184. if (gadget_is_dualspeed(c->cdev->gadget))
  185. usb_free_descriptors(f->hs_descriptors);
  186. usb_free_descriptors(f->descriptors);
  187. kfree(func_to_gser(f));
  188. }
  189. /**
  190. * gser_bind_config - add a generic serial function to a configuration
  191. * @c: the configuration to support the serial instance
  192. * @port_num: /dev/ttyGS* port this interface will use
  193. * Context: single threaded during gadget setup
  194. *
  195. * Returns zero on success, else negative errno.
  196. *
  197. * Caller must have called @gserial_setup() with enough ports to
  198. * handle all the ones it binds. Caller is also responsible
  199. * for calling @gserial_cleanup() before module unload.
  200. */
  201. int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
  202. {
  203. struct f_gser *gser;
  204. int status;
  205. /* REVISIT might want instance-specific strings to help
  206. * distinguish instances ...
  207. */
  208. /* maybe allocate device-global string ID */
  209. if (gser_string_defs[0].id == 0) {
  210. status = usb_string_id(c->cdev);
  211. if (status < 0)
  212. return status;
  213. gser_string_defs[0].id = status;
  214. }
  215. /* allocate and initialize one new instance */
  216. gser = kzalloc(sizeof *gser, GFP_KERNEL);
  217. if (!gser)
  218. return -ENOMEM;
  219. gser->port_num = port_num;
  220. gser->port.func.name = "gser";
  221. gser->port.func.strings = gser_strings;
  222. gser->port.func.bind = gser_bind;
  223. gser->port.func.unbind = gser_unbind;
  224. gser->port.func.set_alt = gser_set_alt;
  225. gser->port.func.disable = gser_disable;
  226. status = usb_add_function(c, &gser->port.func);
  227. if (status)
  228. kfree(gser);
  229. return status;
  230. }