serial.c 6.9 KB

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