acm_ms.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <[email protected]>
  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. int status;
  83. if (gadget_is_otg(c->cdev->gadget)) {
  84. c->descriptors = otg_desc;
  85. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  86. }
  87. f_acm = usb_get_function(f_acm_inst);
  88. if (IS_ERR(f_acm))
  89. return PTR_ERR(f_acm);
  90. f_msg = usb_get_function(fi_msg);
  91. if (IS_ERR(f_msg)) {
  92. status = PTR_ERR(f_msg);
  93. goto put_acm;
  94. }
  95. status = usb_add_function(c, f_acm);
  96. if (status < 0)
  97. goto put_msg;
  98. status = usb_add_function(c, f_msg);
  99. if (status)
  100. goto remove_acm;
  101. return 0;
  102. remove_acm:
  103. usb_remove_function(c, f_acm);
  104. put_msg:
  105. usb_put_function(f_msg);
  106. put_acm:
  107. usb_put_function(f_acm);
  108. return status;
  109. }
  110. static struct usb_configuration acm_ms_config_driver = {
  111. .label = DRIVER_DESC,
  112. .bConfigurationValue = 1,
  113. /* .iConfiguration = DYNAMIC */
  114. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  115. };
  116. /*-------------------------------------------------------------------------*/
  117. static int acm_ms_bind(struct usb_composite_dev *cdev)
  118. {
  119. struct usb_gadget *gadget = cdev->gadget;
  120. struct fsg_opts *opts;
  121. struct fsg_config config;
  122. int status;
  123. f_acm_inst = usb_get_function_instance("acm");
  124. if (IS_ERR(f_acm_inst))
  125. return PTR_ERR(f_acm_inst);
  126. fi_msg = usb_get_function_instance("mass_storage");
  127. if (IS_ERR(fi_msg)) {
  128. status = PTR_ERR(fi_msg);
  129. goto fail_get_msg;
  130. }
  131. /* set up mass storage function */
  132. fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
  133. opts = fsg_opts_from_func_inst(fi_msg);
  134. opts->no_configfs = true;
  135. status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
  136. if (status)
  137. goto fail;
  138. status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
  139. if (status)
  140. goto fail_set_cdev;
  141. fsg_common_set_sysfs(opts->common, true);
  142. status = fsg_common_create_luns(opts->common, &config);
  143. if (status)
  144. goto fail_set_cdev;
  145. fsg_common_set_inquiry_string(opts->common, config.vendor_name,
  146. config.product_name);
  147. /*
  148. * Allocate string descriptor numbers ... note that string
  149. * contents can be overridden by the composite_dev glue.
  150. */
  151. status = usb_string_ids_tab(cdev, strings_dev);
  152. if (status < 0)
  153. goto fail_string_ids;
  154. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  155. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  156. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  157. struct usb_descriptor_header *usb_desc;
  158. usb_desc = usb_otg_descriptor_alloc(gadget);
  159. if (!usb_desc) {
  160. status = -ENOMEM;
  161. goto fail_string_ids;
  162. }
  163. usb_otg_descriptor_init(gadget, usb_desc);
  164. otg_desc[0] = usb_desc;
  165. otg_desc[1] = NULL;
  166. }
  167. /* register our configuration */
  168. status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
  169. if (status < 0)
  170. goto fail_otg_desc;
  171. usb_composite_overwrite_options(cdev, &coverwrite);
  172. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  173. DRIVER_DESC);
  174. return 0;
  175. /* error recovery */
  176. fail_otg_desc:
  177. kfree(otg_desc[0]);
  178. otg_desc[0] = NULL;
  179. fail_string_ids:
  180. fsg_common_remove_luns(opts->common);
  181. fail_set_cdev:
  182. fsg_common_free_buffers(opts->common);
  183. fail:
  184. usb_put_function_instance(fi_msg);
  185. fail_get_msg:
  186. usb_put_function_instance(f_acm_inst);
  187. return status;
  188. }
  189. static int acm_ms_unbind(struct usb_composite_dev *cdev)
  190. {
  191. usb_put_function(f_msg);
  192. usb_put_function_instance(fi_msg);
  193. usb_put_function(f_acm);
  194. usb_put_function_instance(f_acm_inst);
  195. kfree(otg_desc[0]);
  196. otg_desc[0] = NULL;
  197. return 0;
  198. }
  199. static struct usb_composite_driver acm_ms_driver = {
  200. .name = "g_acm_ms",
  201. .dev = &device_desc,
  202. .max_speed = USB_SPEED_SUPER,
  203. .strings = dev_strings,
  204. .bind = acm_ms_bind,
  205. .unbind = acm_ms_unbind,
  206. };
  207. module_usb_composite_driver(acm_ms_driver);
  208. MODULE_DESCRIPTION(DRIVER_DESC);
  209. MODULE_AUTHOR("Klaus Schwarzkopf <[email protected]>");
  210. MODULE_LICENSE("GPL v2");