f_serial.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /* support all relevant hardware speeds... we expect that when
  178. * hardware is dual speed, all bulk-capable endpoints work at
  179. * both speeds
  180. */
  181. gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
  182. gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
  183. gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
  184. gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
  185. status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
  186. gser_ss_function);
  187. if (status)
  188. goto fail;
  189. DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
  190. gser->port_num,
  191. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  192. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  193. gser->port.in->name, gser->port.out->name);
  194. return 0;
  195. fail:
  196. /* we might as well release our claims on endpoints */
  197. if (gser->port.out)
  198. gser->port.out->driver_data = NULL;
  199. if (gser->port.in)
  200. gser->port.in->driver_data = NULL;
  201. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  202. return status;
  203. }
  204. static void
  205. gser_unbind(struct usb_configuration *c, struct usb_function *f)
  206. {
  207. usb_free_all_descriptors(f);
  208. kfree(func_to_gser(f));
  209. }
  210. /**
  211. * gser_bind_config - add a generic serial function to a configuration
  212. * @c: the configuration to support the serial instance
  213. * @port_num: /dev/ttyGS* port this interface will use
  214. * Context: single threaded during gadget setup
  215. *
  216. * Returns zero on success, else negative errno.
  217. */
  218. int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
  219. {
  220. struct f_gser *gser;
  221. int status;
  222. /* REVISIT might want instance-specific strings to help
  223. * distinguish instances ...
  224. */
  225. /* maybe allocate device-global string ID */
  226. if (gser_string_defs[0].id == 0) {
  227. status = usb_string_id(c->cdev);
  228. if (status < 0)
  229. return status;
  230. gser_string_defs[0].id = status;
  231. }
  232. /* allocate and initialize one new instance */
  233. gser = kzalloc(sizeof *gser, GFP_KERNEL);
  234. if (!gser)
  235. return -ENOMEM;
  236. gser->port_num = port_num;
  237. gser->port.func.name = "gser";
  238. gser->port.func.strings = gser_strings;
  239. gser->port.func.bind = gser_bind;
  240. gser->port.func.unbind = gser_unbind;
  241. gser->port.func.set_alt = gser_set_alt;
  242. gser->port.func.disable = gser_disable;
  243. status = usb_add_function(c, &gser->port.func);
  244. if (status)
  245. kfree(gser);
  246. return status;
  247. }