serial.c 7.1 KB

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