serial.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/utsname.h>
  14. #include <linux/device.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_flip.h>
  17. #include "u_serial.h"
  18. #include "gadget_chips.h"
  19. /* Defines */
  20. #define GS_VERSION_STR "v2.4"
  21. #define GS_VERSION_NUM 0x2400
  22. #define GS_LONG_NAME "Gadget Serial"
  23. #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
  24. /*-------------------------------------------------------------------------*/
  25. /*
  26. * Kbuild is not very cooperative with respect to linking separately
  27. * compiled library objects into one module. So for now we won't use
  28. * separate compilation ... ensuring init/exit sections work to shrink
  29. * the runtime footprint, and giving us at least some parts of what
  30. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  31. */
  32. #include "composite.c"
  33. #include "usbstring.c"
  34. #include "config.c"
  35. #include "epautoconf.c"
  36. #include "f_acm.c"
  37. #include "f_obex.c"
  38. #include "f_serial.c"
  39. #include "u_serial.c"
  40. /*-------------------------------------------------------------------------*/
  41. /* Thanks to NetChip Technologies for donating this product ID.
  42. *
  43. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  44. * Instead: allocate your own, using normal USB-IF procedures.
  45. */
  46. #define GS_VENDOR_ID 0x0525 /* NetChip */
  47. #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
  48. #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
  49. #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
  50. /* string IDs are assigned dynamically */
  51. #define STRING_MANUFACTURER_IDX 0
  52. #define STRING_PRODUCT_IDX 1
  53. #define STRING_DESCRIPTION_IDX 2
  54. static char manufacturer[50];
  55. static struct usb_string strings_dev[] = {
  56. [STRING_MANUFACTURER_IDX].s = manufacturer,
  57. [STRING_PRODUCT_IDX].s = GS_VERSION_NAME,
  58. [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
  59. { } /* end of list */
  60. };
  61. static struct usb_gadget_strings stringtab_dev = {
  62. .language = 0x0409, /* en-us */
  63. .strings = strings_dev,
  64. };
  65. static struct usb_gadget_strings *dev_strings[] = {
  66. &stringtab_dev,
  67. NULL,
  68. };
  69. static struct usb_device_descriptor device_desc = {
  70. .bLength = USB_DT_DEVICE_SIZE,
  71. .bDescriptorType = USB_DT_DEVICE,
  72. .bcdUSB = __constant_cpu_to_le16(0x0200),
  73. /* .bDeviceClass = f(use_acm) */
  74. .bDeviceSubClass = 0,
  75. .bDeviceProtocol = 0,
  76. /* .bMaxPacketSize0 = f(hardware) */
  77. .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
  78. /* .idProduct = f(use_acm) */
  79. /* .bcdDevice = f(hardware) */
  80. /* .iManufacturer = DYNAMIC */
  81. /* .iProduct = DYNAMIC */
  82. .bNumConfigurations = 1,
  83. };
  84. static struct usb_otg_descriptor otg_descriptor = {
  85. .bLength = sizeof otg_descriptor,
  86. .bDescriptorType = USB_DT_OTG,
  87. /* REVISIT SRP-only hardware is possible, although
  88. * it would not be called "OTG" ...
  89. */
  90. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  91. };
  92. static const struct usb_descriptor_header *otg_desc[] = {
  93. (struct usb_descriptor_header *) &otg_descriptor,
  94. NULL,
  95. };
  96. /*-------------------------------------------------------------------------*/
  97. /* Module */
  98. MODULE_DESCRIPTION(GS_VERSION_NAME);
  99. MODULE_AUTHOR("Al Borchers");
  100. MODULE_AUTHOR("David Brownell");
  101. MODULE_LICENSE("GPL");
  102. static int use_acm = true;
  103. module_param(use_acm, bool, 0);
  104. MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
  105. static int use_obex = false;
  106. module_param(use_obex, bool, 0);
  107. MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
  108. static unsigned n_ports = 1;
  109. module_param(n_ports, uint, 0);
  110. MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
  111. /*-------------------------------------------------------------------------*/
  112. static int __init serial_bind_config(struct usb_configuration *c)
  113. {
  114. unsigned i;
  115. int status = 0;
  116. for (i = 0; i < n_ports && status == 0; i++) {
  117. if (use_acm)
  118. status = acm_bind_config(c, i);
  119. else if (use_obex)
  120. status = obex_bind_config(c, i);
  121. else
  122. status = gser_bind_config(c, i);
  123. }
  124. return status;
  125. }
  126. static struct usb_configuration serial_config_driver = {
  127. /* .label = f(use_acm) */
  128. .bind = serial_bind_config,
  129. /* .bConfigurationValue = f(use_acm) */
  130. /* .iConfiguration = DYNAMIC */
  131. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  132. .bMaxPower = 1, /* 2 mA, minimal */
  133. };
  134. static int __init gs_bind(struct usb_composite_dev *cdev)
  135. {
  136. int gcnum;
  137. struct usb_gadget *gadget = cdev->gadget;
  138. int status;
  139. status = gserial_setup(cdev->gadget, n_ports);
  140. if (status < 0)
  141. return status;
  142. /* Allocate string descriptor numbers ... note that string
  143. * contents can be overridden by the composite_dev glue.
  144. */
  145. /* device description: manufacturer, product */
  146. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  147. init_utsname()->sysname, init_utsname()->release,
  148. gadget->name);
  149. status = usb_string_id(cdev);
  150. if (status < 0)
  151. goto fail;
  152. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  153. device_desc.iManufacturer = status;
  154. status = usb_string_id(cdev);
  155. if (status < 0)
  156. goto fail;
  157. strings_dev[STRING_PRODUCT_IDX].id = status;
  158. device_desc.iProduct = status;
  159. /* config description */
  160. status = usb_string_id(cdev);
  161. if (status < 0)
  162. goto fail;
  163. strings_dev[STRING_DESCRIPTION_IDX].id = status;
  164. serial_config_driver.iConfiguration = status;
  165. /* set up other descriptors */
  166. gcnum = usb_gadget_controller_number(gadget);
  167. if (gcnum >= 0)
  168. device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum);
  169. else {
  170. /* this is so simple (for now, no altsettings) that it
  171. * SHOULD NOT have problems with bulk-capable hardware.
  172. * so warn about unrcognized controllers -- don't panic.
  173. *
  174. * things like configuration and altsetting numbering
  175. * can need hardware-specific attention though.
  176. */
  177. pr_warning("gs_bind: controller '%s' not recognized\n",
  178. gadget->name);
  179. device_desc.bcdDevice =
  180. __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099);
  181. }
  182. if (gadget_is_otg(cdev->gadget)) {
  183. serial_config_driver.descriptors = otg_desc;
  184. serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  185. }
  186. /* register our configuration */
  187. status = usb_add_config(cdev, &serial_config_driver);
  188. if (status < 0)
  189. goto fail;
  190. INFO(cdev, "%s\n", GS_VERSION_NAME);
  191. return 0;
  192. fail:
  193. gserial_cleanup();
  194. return status;
  195. }
  196. static struct usb_composite_driver gserial_driver = {
  197. .name = "g_serial",
  198. .dev = &device_desc,
  199. .strings = dev_strings,
  200. .bind = gs_bind,
  201. };
  202. static int __init init(void)
  203. {
  204. /* We *could* export two configs; that'd be much cleaner...
  205. * but neither of these product IDs was defined that way.
  206. */
  207. if (use_acm) {
  208. serial_config_driver.label = "CDC ACM config";
  209. serial_config_driver.bConfigurationValue = 2;
  210. device_desc.bDeviceClass = USB_CLASS_COMM;
  211. device_desc.idProduct =
  212. __constant_cpu_to_le16(GS_CDC_PRODUCT_ID);
  213. } else if (use_obex) {
  214. serial_config_driver.label = "CDC OBEX config";
  215. serial_config_driver.bConfigurationValue = 3;
  216. device_desc.bDeviceClass = USB_CLASS_COMM;
  217. device_desc.idProduct =
  218. __constant_cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
  219. } else {
  220. serial_config_driver.label = "Generic Serial config";
  221. serial_config_driver.bConfigurationValue = 1;
  222. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  223. device_desc.idProduct =
  224. __constant_cpu_to_le16(GS_PRODUCT_ID);
  225. }
  226. strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
  227. return usb_composite_register(&gserial_driver);
  228. }
  229. module_init(init);
  230. static void __exit cleanup(void)
  231. {
  232. usb_composite_unregister(&gserial_driver);
  233. gserial_cleanup();
  234. }
  235. module_exit(cleanup);