serial.c 7.1 KB

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