f_serial.c 8.2 KB

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