serial.c 7.2 KB

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