serial.c 8.1 KB

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