f_serial.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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/module.h>
  15. #include <linux/device.h>
  16. #include "u_serial.h"
  17. #include "gadget_chips.h"
  18. /*
  19. * This function packages a simple "generic serial" port with no real
  20. * control mechanisms, just raw data transfer over two bulk endpoints.
  21. *
  22. * Because it's not standardized, this isn't as interoperable as the
  23. * CDC ACM driver. However, for many purposes it's just as functional
  24. * if you can arrange appropriate host side drivers.
  25. */
  26. struct f_gser {
  27. struct gserial port;
  28. u8 data_id;
  29. u8 port_num;
  30. };
  31. static inline struct f_gser *func_to_gser(struct usb_function *f)
  32. {
  33. return container_of(f, struct f_gser, port.func);
  34. }
  35. /*-------------------------------------------------------------------------*/
  36. /* interface descriptor: */
  37. static struct usb_interface_descriptor gser_interface_desc = {
  38. .bLength = USB_DT_INTERFACE_SIZE,
  39. .bDescriptorType = USB_DT_INTERFACE,
  40. /* .bInterfaceNumber = DYNAMIC */
  41. .bNumEndpoints = 2,
  42. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  43. .bInterfaceSubClass = 0,
  44. .bInterfaceProtocol = 0,
  45. /* .iInterface = DYNAMIC */
  46. };
  47. /* full speed support: */
  48. static struct usb_endpoint_descriptor gser_fs_in_desc = {
  49. .bLength = USB_DT_ENDPOINT_SIZE,
  50. .bDescriptorType = USB_DT_ENDPOINT,
  51. .bEndpointAddress = USB_DIR_IN,
  52. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  53. };
  54. static struct usb_endpoint_descriptor gser_fs_out_desc = {
  55. .bLength = USB_DT_ENDPOINT_SIZE,
  56. .bDescriptorType = USB_DT_ENDPOINT,
  57. .bEndpointAddress = USB_DIR_OUT,
  58. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  59. };
  60. static struct usb_descriptor_header *gser_fs_function[] = {
  61. (struct usb_descriptor_header *) &gser_interface_desc,
  62. (struct usb_descriptor_header *) &gser_fs_in_desc,
  63. (struct usb_descriptor_header *) &gser_fs_out_desc,
  64. NULL,
  65. };
  66. /* high speed support: */
  67. static struct usb_endpoint_descriptor gser_hs_in_desc = {
  68. .bLength = USB_DT_ENDPOINT_SIZE,
  69. .bDescriptorType = USB_DT_ENDPOINT,
  70. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  71. .wMaxPacketSize = cpu_to_le16(512),
  72. };
  73. static struct usb_endpoint_descriptor gser_hs_out_desc = {
  74. .bLength = USB_DT_ENDPOINT_SIZE,
  75. .bDescriptorType = USB_DT_ENDPOINT,
  76. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  77. .wMaxPacketSize = cpu_to_le16(512),
  78. };
  79. static struct usb_descriptor_header *gser_hs_function[] = {
  80. (struct usb_descriptor_header *) &gser_interface_desc,
  81. (struct usb_descriptor_header *) &gser_hs_in_desc,
  82. (struct usb_descriptor_header *) &gser_hs_out_desc,
  83. NULL,
  84. };
  85. static struct usb_endpoint_descriptor gser_ss_in_desc = {
  86. .bLength = USB_DT_ENDPOINT_SIZE,
  87. .bDescriptorType = USB_DT_ENDPOINT,
  88. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  89. .wMaxPacketSize = cpu_to_le16(1024),
  90. };
  91. static struct usb_endpoint_descriptor gser_ss_out_desc = {
  92. .bLength = USB_DT_ENDPOINT_SIZE,
  93. .bDescriptorType = USB_DT_ENDPOINT,
  94. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  95. .wMaxPacketSize = cpu_to_le16(1024),
  96. };
  97. static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
  98. .bLength = sizeof gser_ss_bulk_comp_desc,
  99. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  100. };
  101. static struct usb_descriptor_header *gser_ss_function[] = {
  102. (struct usb_descriptor_header *) &gser_interface_desc,
  103. (struct usb_descriptor_header *) &gser_ss_in_desc,
  104. (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
  105. (struct usb_descriptor_header *) &gser_ss_out_desc,
  106. (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
  107. NULL,
  108. };
  109. /* string descriptors: */
  110. static struct usb_string gser_string_defs[] = {
  111. [0].s = "Generic Serial",
  112. { } /* end of list */
  113. };
  114. static struct usb_gadget_strings gser_string_table = {
  115. .language = 0x0409, /* en-us */
  116. .strings = gser_string_defs,
  117. };
  118. static struct usb_gadget_strings *gser_strings[] = {
  119. &gser_string_table,
  120. NULL,
  121. };
  122. /*-------------------------------------------------------------------------*/
  123. static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  124. {
  125. struct f_gser *gser = func_to_gser(f);
  126. struct usb_composite_dev *cdev = f->config->cdev;
  127. /* we know alt == 0, so this is an activation or a reset */
  128. if (gser->port.in->driver_data) {
  129. DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
  130. gserial_disconnect(&gser->port);
  131. }
  132. if (!gser->port.in->desc || !gser->port.out->desc) {
  133. DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
  134. if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
  135. config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
  136. gser->port.in->desc = NULL;
  137. gser->port.out->desc = NULL;
  138. return -EINVAL;
  139. }
  140. }
  141. gserial_connect(&gser->port, gser->port_num);
  142. return 0;
  143. }
  144. static void gser_disable(struct usb_function *f)
  145. {
  146. struct f_gser *gser = func_to_gser(f);
  147. struct usb_composite_dev *cdev = f->config->cdev;
  148. DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
  149. gserial_disconnect(&gser->port);
  150. }
  151. /*-------------------------------------------------------------------------*/
  152. /* serial function driver setup/binding */
  153. static int 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. /* REVISIT might want instance-specific strings to help
  160. * distinguish instances ...
  161. */
  162. /* maybe allocate device-global string ID */
  163. if (gser_string_defs[0].id == 0) {
  164. status = usb_string_id(c->cdev);
  165. if (status < 0)
  166. return status;
  167. gser_string_defs[0].id = status;
  168. }
  169. /* allocate instance-specific interface IDs */
  170. status = usb_interface_id(c, f);
  171. if (status < 0)
  172. goto fail;
  173. gser->data_id = status;
  174. gser_interface_desc.bInterfaceNumber = status;
  175. status = -ENODEV;
  176. /* allocate instance-specific endpoints */
  177. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
  178. if (!ep)
  179. goto fail;
  180. gser->port.in = ep;
  181. ep->driver_data = cdev; /* claim */
  182. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
  183. if (!ep)
  184. goto fail;
  185. gser->port.out = ep;
  186. ep->driver_data = cdev; /* claim */
  187. /* support all relevant hardware speeds... we expect that when
  188. * hardware is dual speed, all bulk-capable endpoints work at
  189. * both speeds
  190. */
  191. gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
  192. gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
  193. gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
  194. gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
  195. status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
  196. gser_ss_function);
  197. if (status)
  198. goto fail;
  199. DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
  200. gser->port_num,
  201. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  202. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  203. gser->port.in->name, gser->port.out->name);
  204. return 0;
  205. fail:
  206. /* we might as well release our claims on endpoints */
  207. if (gser->port.out)
  208. gser->port.out->driver_data = NULL;
  209. if (gser->port.in)
  210. gser->port.in->driver_data = NULL;
  211. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  212. return status;
  213. }
  214. #ifdef USB_FSERIAL_INCLUDED
  215. static void
  216. gser_old_unbind(struct usb_configuration *c, struct usb_function *f)
  217. {
  218. usb_free_all_descriptors(f);
  219. kfree(func_to_gser(f));
  220. }
  221. /**
  222. * gser_bind_config - add a generic serial function to a configuration
  223. * @c: the configuration to support the serial instance
  224. * @port_num: /dev/ttyGS* port this interface will use
  225. * Context: single threaded during gadget setup
  226. *
  227. * Returns zero on success, else negative errno.
  228. */
  229. int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
  230. {
  231. struct f_gser *gser;
  232. int status;
  233. /* allocate and initialize one new instance */
  234. gser = kzalloc(sizeof *gser, GFP_KERNEL);
  235. if (!gser)
  236. return -ENOMEM;
  237. gser->port_num = port_num;
  238. gser->port.func.name = "gser";
  239. gser->port.func.strings = gser_strings;
  240. gser->port.func.bind = gser_bind;
  241. gser->port.func.unbind = gser_old_unbind;
  242. gser->port.func.set_alt = gser_set_alt;
  243. gser->port.func.disable = gser_disable;
  244. status = usb_add_function(c, &gser->port.func);
  245. if (status)
  246. kfree(gser);
  247. return status;
  248. }
  249. #else
  250. static void gser_free_inst(struct usb_function_instance *f)
  251. {
  252. struct f_serial_opts *opts;
  253. opts = container_of(f, struct f_serial_opts, func_inst);
  254. gserial_free_line(opts->port_num);
  255. kfree(opts);
  256. }
  257. static struct usb_function_instance *gser_alloc_inst(void)
  258. {
  259. struct f_serial_opts *opts;
  260. int ret;
  261. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  262. if (!opts)
  263. return ERR_PTR(-ENOMEM);
  264. opts->func_inst.free_func_inst = gser_free_inst;
  265. ret = gserial_alloc_line(&opts->port_num);
  266. if (ret) {
  267. kfree(opts);
  268. return ERR_PTR(ret);
  269. }
  270. return &opts->func_inst;
  271. }
  272. static void gser_free(struct usb_function *f)
  273. {
  274. struct f_gser *serial;
  275. serial = func_to_gser(f);
  276. kfree(serial);
  277. }
  278. static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
  279. {
  280. usb_free_all_descriptors(f);
  281. }
  282. struct usb_function *gser_alloc(struct usb_function_instance *fi)
  283. {
  284. struct f_gser *gser;
  285. struct f_serial_opts *opts;
  286. /* allocate and initialize one new instance */
  287. gser = kzalloc(sizeof(*gser), GFP_KERNEL);
  288. if (!gser)
  289. return ERR_PTR(-ENOMEM);
  290. opts = container_of(fi, struct f_serial_opts, func_inst);
  291. gser->port_num = opts->port_num;
  292. gser->port.func.name = "gser";
  293. gser->port.func.strings = gser_strings;
  294. gser->port.func.bind = gser_bind;
  295. gser->port.func.unbind = gser_unbind;
  296. gser->port.func.set_alt = gser_set_alt;
  297. gser->port.func.disable = gser_disable;
  298. gser->port.func.free_func = gser_free;
  299. return &gser->port.func;
  300. }
  301. DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
  302. MODULE_LICENSE("GPL");
  303. MODULE_AUTHOR("Al Borchers");
  304. MODULE_AUTHOR("David Brownell");
  305. #endif