mass_storage.c 6.3 KB

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