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