serial.c 7.8 KB

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