mass_storage.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * mass_storage.c -- Mass Storage USB Gadget
  4. *
  5. * Copyright (C) 2003-2008 Alan Stern
  6. * Copyright (C) 2009 Samsung Electronics
  7. * Author: Michal Nazarewicz <mina86@mina86.com>
  8. * All rights reserved.
  9. */
  10. /*
  11. * The Mass Storage Gadget acts as a USB Mass Storage device,
  12. * appearing to the host as a disk drive or as a CD-ROM drive. In
  13. * addition to providing an example of a genuinely useful gadget
  14. * driver for a USB device, it also illustrates a technique of
  15. * double-buffering for increased throughput. Last but not least, it
  16. * gives an easy way to probe the behavior of the Mass Storage drivers
  17. * in a USB host.
  18. *
  19. * Since this file serves only administrative purposes and all the
  20. * business logic is implemented in f_mass_storage.* file. Read
  21. * comments in this file for more detailed description.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/usb/ch9.h>
  25. #include <linux/module.h>
  26. /*-------------------------------------------------------------------------*/
  27. #define DRIVER_DESC "Mass Storage Gadget"
  28. #define DRIVER_VERSION "2009/09/11"
  29. /*
  30. * Thanks to NetChip Technologies for donating this product ID.
  31. *
  32. * DO NOT REUSE THESE IDs with any other driver!! Ever!!
  33. * Instead: allocate your own, using normal USB-IF procedures.
  34. */
  35. #define FSG_VENDOR_ID 0x0525 /* NetChip */
  36. #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  37. #include "f_mass_storage.h"
  38. /*-------------------------------------------------------------------------*/
  39. USB_GADGET_COMPOSITE_OPTIONS();
  40. static struct usb_device_descriptor msg_device_desc = {
  41. .bLength = sizeof msg_device_desc,
  42. .bDescriptorType = USB_DT_DEVICE,
  43. /* .bcdUSB = DYNAMIC */
  44. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  45. /* Vendor and product id can be overridden by module parameters. */
  46. .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  47. .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  48. .bNumConfigurations = 1,
  49. };
  50. static const struct usb_descriptor_header *otg_desc[2];
  51. static struct usb_string strings_dev[] = {
  52. [USB_GADGET_MANUFACTURER_IDX].s = "",
  53. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  54. [USB_GADGET_SERIAL_IDX].s = "",
  55. { } /* end of list */
  56. };
  57. static struct usb_gadget_strings stringtab_dev = {
  58. .language = 0x0409, /* en-us */
  59. .strings = strings_dev,
  60. };
  61. static struct usb_gadget_strings *dev_strings[] = {
  62. &stringtab_dev,
  63. NULL,
  64. };
  65. static struct usb_function_instance *fi_msg;
  66. static struct usb_function *f_msg;
  67. /****************************** Configurations ******************************/
  68. static struct fsg_module_parameters mod_data = {
  69. .stall = 1
  70. };
  71. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  72. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  73. #else
  74. /*
  75. * Number of buffers we will use.
  76. * 2 is usually enough for good buffering pipeline
  77. */
  78. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  79. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  80. FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
  81. static int msg_do_config(struct usb_configuration *c)
  82. {
  83. struct fsg_opts *opts;
  84. int ret;
  85. if (gadget_is_otg(c->cdev->gadget)) {
  86. c->descriptors = otg_desc;
  87. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  88. }
  89. opts = fsg_opts_from_func_inst(fi_msg);
  90. f_msg = usb_get_function(fi_msg);
  91. if (IS_ERR(f_msg))
  92. return PTR_ERR(f_msg);
  93. ret = usb_add_function(c, f_msg);
  94. if (ret)
  95. goto put_func;
  96. return 0;
  97. put_func:
  98. usb_put_function(f_msg);
  99. return ret;
  100. }
  101. static struct usb_configuration msg_config_driver = {
  102. .label = "Linux File-Backed Storage",
  103. .bConfigurationValue = 1,
  104. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  105. };
  106. /****************************** Gadget Bind ******************************/
  107. static int msg_bind(struct usb_composite_dev *cdev)
  108. {
  109. struct fsg_opts *opts;
  110. struct fsg_config config;
  111. int status;
  112. fi_msg = usb_get_function_instance("mass_storage");
  113. if (IS_ERR(fi_msg))
  114. return PTR_ERR(fi_msg);
  115. fsg_config_from_params(&config, &mod_data, fsg_num_buffers);
  116. opts = fsg_opts_from_func_inst(fi_msg);
  117. opts->no_configfs = true;
  118. status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
  119. if (status)
  120. goto fail;
  121. status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
  122. if (status)
  123. goto fail_set_cdev;
  124. fsg_common_set_sysfs(opts->common, true);
  125. status = fsg_common_create_luns(opts->common, &config);
  126. if (status)
  127. goto fail_set_cdev;
  128. fsg_common_set_inquiry_string(opts->common, config.vendor_name,
  129. config.product_name);
  130. status = usb_string_ids_tab(cdev, strings_dev);
  131. if (status < 0)
  132. goto fail_string_ids;
  133. msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  134. if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
  135. struct usb_descriptor_header *usb_desc;
  136. usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
  137. if (!usb_desc)
  138. goto fail_string_ids;
  139. usb_otg_descriptor_init(cdev->gadget, usb_desc);
  140. otg_desc[0] = usb_desc;
  141. otg_desc[1] = NULL;
  142. }
  143. status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
  144. if (status < 0)
  145. goto fail_otg_desc;
  146. usb_composite_overwrite_options(cdev, &coverwrite);
  147. dev_info(&cdev->gadget->dev,
  148. DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  149. return 0;
  150. fail_otg_desc:
  151. kfree(otg_desc[0]);
  152. otg_desc[0] = NULL;
  153. fail_string_ids:
  154. fsg_common_remove_luns(opts->common);
  155. fail_set_cdev:
  156. fsg_common_free_buffers(opts->common);
  157. fail:
  158. usb_put_function_instance(fi_msg);
  159. return status;
  160. }
  161. static int msg_unbind(struct usb_composite_dev *cdev)
  162. {
  163. if (!IS_ERR(f_msg))
  164. usb_put_function(f_msg);
  165. if (!IS_ERR(fi_msg))
  166. usb_put_function_instance(fi_msg);
  167. kfree(otg_desc[0]);
  168. otg_desc[0] = NULL;
  169. return 0;
  170. }
  171. /****************************** Some noise ******************************/
  172. static struct usb_composite_driver msg_driver = {
  173. .name = "g_mass_storage",
  174. .dev = &msg_device_desc,
  175. .max_speed = USB_SPEED_SUPER_PLUS,
  176. .needs_serial = 1,
  177. .strings = dev_strings,
  178. .bind = msg_bind,
  179. .unbind = msg_unbind,
  180. };
  181. MODULE_DESCRIPTION(DRIVER_DESC);
  182. MODULE_AUTHOR("Michal Nazarewicz");
  183. MODULE_LICENSE("GPL");
  184. static int __init msg_init(void)
  185. {
  186. return usb_composite_probe(&msg_driver);
  187. }
  188. module_init(msg_init);
  189. static void __exit msg_cleanup(void)
  190. {
  191. usb_composite_unregister(&msg_driver);
  192. }
  193. module_exit(msg_cleanup);