acm_ms.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * acm_ms.c -- Composite driver, with ACM and mass storage support
  4. *
  5. * Copyright (C) 2008 David Brownell
  6. * Copyright (C) 2008 Nokia Corporation
  7. * Author: David Brownell
  8. * Modified: Klaus Schwarzkopf <schwarzkopf@sensortherm.de>
  9. *
  10. * Heavily based on multi.c and cdc2.c
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include "u_serial.h"
  15. #define DRIVER_DESC "Composite Gadget (ACM + MS)"
  16. #define DRIVER_VERSION "2011/10/10"
  17. /*-------------------------------------------------------------------------*/
  18. /*
  19. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  20. * Instead: allocate your own, using normal USB-IF procedures.
  21. */
  22. #define ACM_MS_VENDOR_NUM 0x1d6b /* Linux Foundation */
  23. #define ACM_MS_PRODUCT_NUM 0x0106 /* Composite Gadget: ACM + MS*/
  24. #include "f_mass_storage.h"
  25. /*-------------------------------------------------------------------------*/
  26. USB_GADGET_COMPOSITE_OPTIONS();
  27. static struct usb_device_descriptor device_desc = {
  28. .bLength = sizeof device_desc,
  29. .bDescriptorType = USB_DT_DEVICE,
  30. /* .bcdUSB = DYNAMIC */
  31. .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
  32. .bDeviceSubClass = 2,
  33. .bDeviceProtocol = 1,
  34. /* .bMaxPacketSize0 = f(hardware) */
  35. /* Vendor and product id can be overridden by module parameters. */
  36. .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM),
  37. .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM),
  38. /* .bcdDevice = f(hardware) */
  39. /* .iManufacturer = DYNAMIC */
  40. /* .iProduct = DYNAMIC */
  41. /* NO SERIAL NUMBER */
  42. /*.bNumConfigurations = DYNAMIC*/
  43. };
  44. static const struct usb_descriptor_header *otg_desc[2];
  45. /* string IDs are assigned dynamically */
  46. static struct usb_string strings_dev[] = {
  47. [USB_GADGET_MANUFACTURER_IDX].s = "",
  48. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  49. [USB_GADGET_SERIAL_IDX].s = "",
  50. { } /* end of list */
  51. };
  52. static struct usb_gadget_strings stringtab_dev = {
  53. .language = 0x0409, /* en-us */
  54. .strings = strings_dev,
  55. };
  56. static struct usb_gadget_strings *dev_strings[] = {
  57. &stringtab_dev,
  58. NULL,
  59. };
  60. /****************************** Configurations ******************************/
  61. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  62. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  63. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  64. #else
  65. /*
  66. * Number of buffers we will use.
  67. * 2 is usually enough for good buffering pipeline
  68. */
  69. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  70. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  71. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  72. /*-------------------------------------------------------------------------*/
  73. static struct usb_function *f_acm;
  74. static struct usb_function_instance *f_acm_inst;
  75. static struct usb_function_instance *fi_msg;
  76. static struct usb_function *f_msg;
  77. /*
  78. * We _always_ have both ACM and mass storage functions.
  79. */
  80. static int acm_ms_do_config(struct usb_configuration *c)
  81. {
  82. struct fsg_opts *opts;
  83. int status;
  84. if (gadget_is_otg(c->cdev->gadget)) {
  85. c->descriptors = otg_desc;
  86. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  87. }
  88. opts = fsg_opts_from_func_inst(fi_msg);
  89. f_acm = usb_get_function(f_acm_inst);
  90. if (IS_ERR(f_acm))
  91. return PTR_ERR(f_acm);
  92. f_msg = usb_get_function(fi_msg);
  93. if (IS_ERR(f_msg)) {
  94. status = PTR_ERR(f_msg);
  95. goto put_acm;
  96. }
  97. status = usb_add_function(c, f_acm);
  98. if (status < 0)
  99. goto put_msg;
  100. status = usb_add_function(c, f_msg);
  101. if (status)
  102. goto remove_acm;
  103. return 0;
  104. remove_acm:
  105. usb_remove_function(c, f_acm);
  106. put_msg:
  107. usb_put_function(f_msg);
  108. put_acm:
  109. usb_put_function(f_acm);
  110. return status;
  111. }
  112. static struct usb_configuration acm_ms_config_driver = {
  113. .label = DRIVER_DESC,
  114. .bConfigurationValue = 1,
  115. /* .iConfiguration = DYNAMIC */
  116. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  117. };
  118. /*-------------------------------------------------------------------------*/
  119. static int acm_ms_bind(struct usb_composite_dev *cdev)
  120. {
  121. struct usb_gadget *gadget = cdev->gadget;
  122. struct fsg_opts *opts;
  123. struct fsg_config config;
  124. int status;
  125. f_acm_inst = usb_get_function_instance("acm");
  126. if (IS_ERR(f_acm_inst))
  127. return PTR_ERR(f_acm_inst);
  128. fi_msg = usb_get_function_instance("mass_storage");
  129. if (IS_ERR(fi_msg)) {
  130. status = PTR_ERR(fi_msg);
  131. goto fail_get_msg;
  132. }
  133. /* set up mass storage function */
  134. fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
  135. opts = fsg_opts_from_func_inst(fi_msg);
  136. opts->no_configfs = true;
  137. status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
  138. if (status)
  139. goto fail;
  140. status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
  141. if (status)
  142. goto fail_set_cdev;
  143. fsg_common_set_sysfs(opts->common, true);
  144. status = fsg_common_create_luns(opts->common, &config);
  145. if (status)
  146. goto fail_set_cdev;
  147. fsg_common_set_inquiry_string(opts->common, config.vendor_name,
  148. config.product_name);
  149. /*
  150. * Allocate string descriptor numbers ... note that string
  151. * contents can be overridden by the composite_dev glue.
  152. */
  153. status = usb_string_ids_tab(cdev, strings_dev);
  154. if (status < 0)
  155. goto fail_string_ids;
  156. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  157. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  158. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  159. struct usb_descriptor_header *usb_desc;
  160. usb_desc = usb_otg_descriptor_alloc(gadget);
  161. if (!usb_desc) {
  162. status = -ENOMEM;
  163. goto fail_string_ids;
  164. }
  165. usb_otg_descriptor_init(gadget, usb_desc);
  166. otg_desc[0] = usb_desc;
  167. otg_desc[1] = NULL;
  168. }
  169. /* register our configuration */
  170. status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
  171. if (status < 0)
  172. goto fail_otg_desc;
  173. usb_composite_overwrite_options(cdev, &coverwrite);
  174. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  175. DRIVER_DESC);
  176. return 0;
  177. /* error recovery */
  178. fail_otg_desc:
  179. kfree(otg_desc[0]);
  180. otg_desc[0] = NULL;
  181. fail_string_ids:
  182. fsg_common_remove_luns(opts->common);
  183. fail_set_cdev:
  184. fsg_common_free_buffers(opts->common);
  185. fail:
  186. usb_put_function_instance(fi_msg);
  187. fail_get_msg:
  188. usb_put_function_instance(f_acm_inst);
  189. return status;
  190. }
  191. static int acm_ms_unbind(struct usb_composite_dev *cdev)
  192. {
  193. usb_put_function(f_msg);
  194. usb_put_function_instance(fi_msg);
  195. usb_put_function(f_acm);
  196. usb_put_function_instance(f_acm_inst);
  197. kfree(otg_desc[0]);
  198. otg_desc[0] = NULL;
  199. return 0;
  200. }
  201. static struct usb_composite_driver acm_ms_driver = {
  202. .name = "g_acm_ms",
  203. .dev = &device_desc,
  204. .max_speed = USB_SPEED_SUPER,
  205. .strings = dev_strings,
  206. .bind = acm_ms_bind,
  207. .unbind = acm_ms_unbind,
  208. };
  209. module_usb_composite_driver(acm_ms_driver);
  210. MODULE_DESCRIPTION(DRIVER_DESC);
  211. MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
  212. MODULE_LICENSE("GPL v2");