serial.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * serial.c -- USB gadget serial 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 <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include "u_serial.h"
  17. #include "gadget_chips.h"
  18. /* Defines */
  19. #define GS_VERSION_STR "v2.4"
  20. #define GS_VERSION_NUM 0x2400
  21. #define GS_LONG_NAME "Gadget Serial"
  22. #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
  23. /*-------------------------------------------------------------------------*/
  24. /*
  25. * Kbuild is not very cooperative with respect to linking separately
  26. * compiled library objects into one module. So for now we won't use
  27. * separate compilation ... ensuring init/exit sections work to shrink
  28. * the runtime footprint, and giving us at least some parts of what
  29. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  30. */
  31. #include "f_obex.c"
  32. #define USB_FSERIAL_INCLUDED
  33. #include "f_serial.c"
  34. /*-------------------------------------------------------------------------*/
  35. USB_GADGET_COMPOSITE_OPTIONS();
  36. /* Thanks to NetChip Technologies for donating this product ID.
  37. *
  38. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  39. * Instead: allocate your own, using normal USB-IF procedures.
  40. */
  41. #define GS_VENDOR_ID 0x0525 /* NetChip */
  42. #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
  43. #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
  44. #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
  45. /* string IDs are assigned dynamically */
  46. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  47. static struct usb_string strings_dev[] = {
  48. [USB_GADGET_MANUFACTURER_IDX].s = "",
  49. [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
  50. [USB_GADGET_SERIAL_IDX].s = "",
  51. [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
  52. { } /* end of list */
  53. };
  54. static struct usb_gadget_strings stringtab_dev = {
  55. .language = 0x0409, /* en-us */
  56. .strings = strings_dev,
  57. };
  58. static struct usb_gadget_strings *dev_strings[] = {
  59. &stringtab_dev,
  60. NULL,
  61. };
  62. static struct usb_device_descriptor device_desc = {
  63. .bLength = USB_DT_DEVICE_SIZE,
  64. .bDescriptorType = USB_DT_DEVICE,
  65. .bcdUSB = cpu_to_le16(0x0200),
  66. /* .bDeviceClass = f(use_acm) */
  67. .bDeviceSubClass = 0,
  68. .bDeviceProtocol = 0,
  69. /* .bMaxPacketSize0 = f(hardware) */
  70. .idVendor = cpu_to_le16(GS_VENDOR_ID),
  71. /* .idProduct = f(use_acm) */
  72. .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
  73. /* .iManufacturer = DYNAMIC */
  74. /* .iProduct = DYNAMIC */
  75. .bNumConfigurations = 1,
  76. };
  77. static struct usb_otg_descriptor otg_descriptor = {
  78. .bLength = sizeof otg_descriptor,
  79. .bDescriptorType = USB_DT_OTG,
  80. /* REVISIT SRP-only hardware is possible, although
  81. * it would not be called "OTG" ...
  82. */
  83. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  84. };
  85. static const struct usb_descriptor_header *otg_desc[] = {
  86. (struct usb_descriptor_header *) &otg_descriptor,
  87. NULL,
  88. };
  89. /*-------------------------------------------------------------------------*/
  90. /* Module */
  91. MODULE_DESCRIPTION(GS_VERSION_NAME);
  92. MODULE_AUTHOR("Al Borchers");
  93. MODULE_AUTHOR("David Brownell");
  94. MODULE_LICENSE("GPL");
  95. static bool use_acm = true;
  96. module_param(use_acm, bool, 0);
  97. MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
  98. static bool use_obex = false;
  99. module_param(use_obex, bool, 0);
  100. MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
  101. static unsigned n_ports = 1;
  102. module_param(n_ports, uint, 0);
  103. MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
  104. /*-------------------------------------------------------------------------*/
  105. static unsigned char tty_lines[MAX_U_SERIAL_PORTS];
  106. static int __init serial_bind_obex_config(struct usb_configuration *c)
  107. {
  108. unsigned i;
  109. int status = 0;
  110. for (i = 0; i < n_ports && status == 0; i++)
  111. status = obex_bind_config(c, tty_lines[i]);
  112. return status;
  113. }
  114. static int __init serial_bind_gser_config(struct usb_configuration *c)
  115. {
  116. unsigned i;
  117. int status = 0;
  118. for (i = 0; i < n_ports && status == 0; i++)
  119. status = gser_bind_config(c, tty_lines[i]);
  120. return status;
  121. }
  122. static struct usb_configuration serial_config_driver = {
  123. /* .label = f(use_acm) */
  124. /* .bConfigurationValue = f(use_acm) */
  125. /* .iConfiguration = DYNAMIC */
  126. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  127. };
  128. static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
  129. static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
  130. static int serial_register_ports(struct usb_composite_dev *cdev,
  131. struct usb_configuration *c, const char *f_name)
  132. {
  133. int i;
  134. int ret;
  135. ret = usb_add_config_only(cdev, c);
  136. if (ret)
  137. goto out;
  138. for (i = 0; i < n_ports; i++) {
  139. fi_serial[i] = usb_get_function_instance(f_name);
  140. if (IS_ERR(fi_serial[i])) {
  141. ret = PTR_ERR(fi_serial[i]);
  142. goto fail;
  143. }
  144. f_serial[i] = usb_get_function(fi_serial[i]);
  145. if (IS_ERR(f_serial[i])) {
  146. ret = PTR_ERR(f_serial[i]);
  147. goto err_get_func;
  148. }
  149. ret = usb_add_function(c, f_serial[i]);
  150. if (ret)
  151. goto err_add_func;
  152. }
  153. return 0;
  154. err_add_func:
  155. usb_put_function(f_serial[i]);
  156. err_get_func:
  157. usb_put_function_instance(fi_serial[i]);
  158. fail:
  159. i--;
  160. while (i >= 0) {
  161. usb_remove_function(c, f_serial[i]);
  162. usb_put_function(f_serial[i]);
  163. usb_put_function_instance(fi_serial[i]);
  164. i--;
  165. }
  166. out:
  167. return ret;
  168. }
  169. static int __init gs_bind(struct usb_composite_dev *cdev)
  170. {
  171. int status;
  172. int cur_line = 0;
  173. if (!use_acm) {
  174. for (cur_line = 0; cur_line < n_ports; cur_line++) {
  175. status = gserial_alloc_line(&tty_lines[cur_line]);
  176. if (status)
  177. goto fail;
  178. }
  179. }
  180. /* Allocate string descriptor numbers ... note that string
  181. * contents can be overridden by the composite_dev glue.
  182. */
  183. status = usb_string_ids_tab(cdev, strings_dev);
  184. if (status < 0)
  185. goto fail;
  186. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  187. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  188. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  189. serial_config_driver.iConfiguration = status;
  190. if (gadget_is_otg(cdev->gadget)) {
  191. serial_config_driver.descriptors = otg_desc;
  192. serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  193. }
  194. /* register our configuration */
  195. if (use_acm) {
  196. status = serial_register_ports(cdev, &serial_config_driver,
  197. "acm");
  198. usb_ep_autoconfig_reset(cdev->gadget);
  199. } else if (use_obex)
  200. status = usb_add_config(cdev, &serial_config_driver,
  201. serial_bind_obex_config);
  202. else
  203. status = usb_add_config(cdev, &serial_config_driver,
  204. serial_bind_gser_config);
  205. if (status < 0)
  206. goto fail;
  207. usb_composite_overwrite_options(cdev, &coverwrite);
  208. INFO(cdev, "%s\n", GS_VERSION_NAME);
  209. return 0;
  210. fail:
  211. cur_line--;
  212. while (cur_line >= 0 && !use_acm)
  213. gserial_free_line(tty_lines[cur_line--]);
  214. return status;
  215. }
  216. static int gs_unbind(struct usb_composite_dev *cdev)
  217. {
  218. int i;
  219. for (i = 0; i < n_ports; i++) {
  220. usb_put_function(f_serial[i]);
  221. usb_put_function_instance(fi_serial[i]);
  222. if (!use_acm)
  223. gserial_free_line(tty_lines[i]);
  224. }
  225. return 0;
  226. }
  227. static __refdata struct usb_composite_driver gserial_driver = {
  228. .name = "g_serial",
  229. .dev = &device_desc,
  230. .strings = dev_strings,
  231. .max_speed = USB_SPEED_SUPER,
  232. .bind = gs_bind,
  233. .unbind = gs_unbind,
  234. };
  235. static int __init init(void)
  236. {
  237. /* We *could* export two configs; that'd be much cleaner...
  238. * but neither of these product IDs was defined that way.
  239. */
  240. if (use_acm) {
  241. serial_config_driver.label = "CDC ACM config";
  242. serial_config_driver.bConfigurationValue = 2;
  243. device_desc.bDeviceClass = USB_CLASS_COMM;
  244. device_desc.idProduct =
  245. cpu_to_le16(GS_CDC_PRODUCT_ID);
  246. } else if (use_obex) {
  247. serial_config_driver.label = "CDC OBEX config";
  248. serial_config_driver.bConfigurationValue = 3;
  249. device_desc.bDeviceClass = USB_CLASS_COMM;
  250. device_desc.idProduct =
  251. cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
  252. } else {
  253. serial_config_driver.label = "Generic Serial config";
  254. serial_config_driver.bConfigurationValue = 1;
  255. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  256. device_desc.idProduct =
  257. cpu_to_le16(GS_PRODUCT_ID);
  258. }
  259. strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
  260. return usb_composite_probe(&gserial_driver);
  261. }
  262. module_init(init);
  263. static void __exit cleanup(void)
  264. {
  265. usb_composite_unregister(&gserial_driver);
  266. }
  267. module_exit(cleanup);