serial.c 8.1 KB

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