serial.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * serial.c -- USB gadget serial driver
  4. *
  5. * Copyright (C) 2003 Al Borchers ([email protected])
  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. static bool enable = true;
  80. static int switch_gserial_enable(bool do_enable);
  81. static int enable_set(const char *s, const struct kernel_param *kp)
  82. {
  83. bool do_enable;
  84. int ret;
  85. if (!s) /* called for no-arg enable == default */
  86. return 0;
  87. ret = strtobool(s, &do_enable);
  88. if (ret || enable == do_enable)
  89. return ret;
  90. ret = switch_gserial_enable(do_enable);
  91. if (!ret)
  92. enable = do_enable;
  93. return ret;
  94. }
  95. static const struct kernel_param_ops enable_ops = {
  96. .set = enable_set,
  97. .get = param_get_bool,
  98. };
  99. module_param_cb(enable, &enable_ops, &enable, 0644);
  100. /*-------------------------------------------------------------------------*/
  101. static struct usb_configuration serial_config_driver = {
  102. /* .label = f(use_acm) */
  103. /* .bConfigurationValue = f(use_acm) */
  104. /* .iConfiguration = DYNAMIC */
  105. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  106. };
  107. static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
  108. static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
  109. static int serial_register_ports(struct usb_composite_dev *cdev,
  110. struct usb_configuration *c, const char *f_name)
  111. {
  112. int i;
  113. int ret;
  114. ret = usb_add_config_only(cdev, c);
  115. if (ret)
  116. goto out;
  117. for (i = 0; i < n_ports; i++) {
  118. fi_serial[i] = usb_get_function_instance(f_name);
  119. if (IS_ERR(fi_serial[i])) {
  120. ret = PTR_ERR(fi_serial[i]);
  121. goto fail;
  122. }
  123. f_serial[i] = usb_get_function(fi_serial[i]);
  124. if (IS_ERR(f_serial[i])) {
  125. ret = PTR_ERR(f_serial[i]);
  126. goto err_get_func;
  127. }
  128. ret = usb_add_function(c, f_serial[i]);
  129. if (ret)
  130. goto err_add_func;
  131. }
  132. return 0;
  133. err_add_func:
  134. usb_put_function(f_serial[i]);
  135. err_get_func:
  136. usb_put_function_instance(fi_serial[i]);
  137. fail:
  138. i--;
  139. while (i >= 0) {
  140. usb_remove_function(c, f_serial[i]);
  141. usb_put_function(f_serial[i]);
  142. usb_put_function_instance(fi_serial[i]);
  143. i--;
  144. }
  145. out:
  146. return ret;
  147. }
  148. static int gs_bind(struct usb_composite_dev *cdev)
  149. {
  150. int status;
  151. /* Allocate string descriptor numbers ... note that string
  152. * contents can be overridden by the composite_dev glue.
  153. */
  154. status = usb_string_ids_tab(cdev, strings_dev);
  155. if (status < 0)
  156. goto fail;
  157. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  158. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  159. status = strings_dev[STRING_DESCRIPTION_IDX].id;
  160. serial_config_driver.iConfiguration = status;
  161. if (gadget_is_otg(cdev->gadget)) {
  162. if (!otg_desc[0]) {
  163. struct usb_descriptor_header *usb_desc;
  164. usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
  165. if (!usb_desc) {
  166. status = -ENOMEM;
  167. goto fail;
  168. }
  169. usb_otg_descriptor_init(cdev->gadget, usb_desc);
  170. otg_desc[0] = usb_desc;
  171. otg_desc[1] = NULL;
  172. }
  173. serial_config_driver.descriptors = otg_desc;
  174. serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  175. }
  176. /* register our configuration */
  177. if (use_acm) {
  178. status = serial_register_ports(cdev, &serial_config_driver,
  179. "acm");
  180. usb_ep_autoconfig_reset(cdev->gadget);
  181. } else if (use_obex)
  182. status = serial_register_ports(cdev, &serial_config_driver,
  183. "obex");
  184. else {
  185. status = serial_register_ports(cdev, &serial_config_driver,
  186. "gser");
  187. }
  188. if (status < 0)
  189. goto fail1;
  190. usb_composite_overwrite_options(cdev, &coverwrite);
  191. INFO(cdev, "%s\n", GS_VERSION_NAME);
  192. return 0;
  193. fail1:
  194. kfree(otg_desc[0]);
  195. otg_desc[0] = NULL;
  196. fail:
  197. return status;
  198. }
  199. static int gs_unbind(struct usb_composite_dev *cdev)
  200. {
  201. int i;
  202. for (i = 0; i < n_ports; i++) {
  203. usb_put_function(f_serial[i]);
  204. usb_put_function_instance(fi_serial[i]);
  205. }
  206. kfree(otg_desc[0]);
  207. otg_desc[0] = NULL;
  208. return 0;
  209. }
  210. static struct usb_composite_driver gserial_driver = {
  211. .name = "g_serial",
  212. .dev = &device_desc,
  213. .strings = dev_strings,
  214. .max_speed = USB_SPEED_SUPER,
  215. .bind = gs_bind,
  216. .unbind = gs_unbind,
  217. };
  218. static int switch_gserial_enable(bool do_enable)
  219. {
  220. if (!serial_config_driver.label)
  221. /* gserial_init() was not called, yet */
  222. return 0;
  223. if (do_enable)
  224. return usb_composite_probe(&gserial_driver);
  225. usb_composite_unregister(&gserial_driver);
  226. return 0;
  227. }
  228. static int __init gserial_init(void)
  229. {
  230. /* We *could* export two configs; that'd be much cleaner...
  231. * but neither of these product IDs was defined that way.
  232. */
  233. if (use_acm) {
  234. serial_config_driver.label = "CDC ACM config";
  235. serial_config_driver.bConfigurationValue = 2;
  236. device_desc.bDeviceClass = USB_CLASS_COMM;
  237. device_desc.idProduct =
  238. cpu_to_le16(GS_CDC_PRODUCT_ID);
  239. } else if (use_obex) {
  240. serial_config_driver.label = "CDC OBEX config";
  241. serial_config_driver.bConfigurationValue = 3;
  242. device_desc.bDeviceClass = USB_CLASS_COMM;
  243. device_desc.idProduct =
  244. cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
  245. } else {
  246. serial_config_driver.label = "Generic Serial config";
  247. serial_config_driver.bConfigurationValue = 1;
  248. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  249. device_desc.idProduct =
  250. cpu_to_le16(GS_PRODUCT_ID);
  251. }
  252. strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
  253. if (!enable)
  254. return 0;
  255. return usb_composite_probe(&gserial_driver);
  256. }
  257. module_init(gserial_init);
  258. static void __exit gserial_cleanup(void)
  259. {
  260. if (enable)
  261. usb_composite_unregister(&gserial_driver);
  262. }
  263. module_exit(gserial_cleanup);