serial.c 7.4 KB

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