mass_storage.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 struct usb_otg_descriptor otg_descriptor = {
  55. .bLength = sizeof otg_descriptor,
  56. .bDescriptorType = USB_DT_OTG,
  57. /*
  58. * REVISIT SRP-only hardware is possible, although
  59. * it would not be called "OTG" ...
  60. */
  61. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  62. };
  63. static const struct usb_descriptor_header *otg_desc[] = {
  64. (struct usb_descriptor_header *) &otg_descriptor,
  65. NULL,
  66. };
  67. static struct usb_string strings_dev[] = {
  68. [USB_GADGET_MANUFACTURER_IDX].s = "",
  69. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  70. [USB_GADGET_SERIAL_IDX].s = "",
  71. { } /* end of list */
  72. };
  73. static struct usb_gadget_strings stringtab_dev = {
  74. .language = 0x0409, /* en-us */
  75. .strings = strings_dev,
  76. };
  77. static struct usb_gadget_strings *dev_strings[] = {
  78. &stringtab_dev,
  79. NULL,
  80. };
  81. static struct usb_function_instance *fi_msg;
  82. static struct usb_function *f_msg;
  83. /****************************** Configurations ******************************/
  84. static struct fsg_module_parameters mod_data = {
  85. .stall = 1
  86. };
  87. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  88. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  89. #else
  90. /*
  91. * Number of buffers we will use.
  92. * 2 is usually enough for good buffering pipeline
  93. */
  94. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  95. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  96. FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
  97. static unsigned long msg_registered;
  98. static void msg_cleanup(void);
  99. static int msg_thread_exits(struct fsg_common *common)
  100. {
  101. msg_cleanup();
  102. return 0;
  103. }
  104. static int __init msg_do_config(struct usb_configuration *c)
  105. {
  106. struct fsg_opts *opts;
  107. int ret;
  108. if (gadget_is_otg(c->cdev->gadget)) {
  109. c->descriptors = otg_desc;
  110. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  111. }
  112. opts = fsg_opts_from_func_inst(fi_msg);
  113. f_msg = usb_get_function(fi_msg);
  114. if (IS_ERR(f_msg))
  115. return PTR_ERR(f_msg);
  116. ret = fsg_common_run_thread(opts->common);
  117. if (ret)
  118. goto put_func;
  119. ret = usb_add_function(c, f_msg);
  120. if (ret)
  121. goto put_func;
  122. return 0;
  123. put_func:
  124. usb_put_function(f_msg);
  125. return ret;
  126. }
  127. static struct usb_configuration msg_config_driver = {
  128. .label = "Linux File-Backed Storage",
  129. .bConfigurationValue = 1,
  130. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  131. };
  132. /****************************** Gadget Bind ******************************/
  133. static int __init msg_bind(struct usb_composite_dev *cdev)
  134. {
  135. static const struct fsg_operations ops = {
  136. .thread_exits = msg_thread_exits,
  137. };
  138. struct fsg_opts *opts;
  139. struct fsg_config config;
  140. int status;
  141. fi_msg = usb_get_function_instance("mass_storage");
  142. if (IS_ERR(fi_msg))
  143. return PTR_ERR(fi_msg);
  144. fsg_config_from_params(&config, &mod_data, fsg_num_buffers);
  145. opts = fsg_opts_from_func_inst(fi_msg);
  146. opts->no_configfs = true;
  147. status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
  148. if (status)
  149. goto fail;
  150. status = fsg_common_set_nluns(opts->common, config.nluns);
  151. if (status)
  152. goto fail_set_nluns;
  153. fsg_common_set_ops(opts->common, &ops);
  154. status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
  155. if (status)
  156. goto fail_set_cdev;
  157. fsg_common_set_sysfs(opts->common, true);
  158. status = fsg_common_create_luns(opts->common, &config);
  159. if (status)
  160. goto fail_set_cdev;
  161. fsg_common_set_inquiry_string(opts->common, config.vendor_name,
  162. config.product_name);
  163. status = usb_string_ids_tab(cdev, strings_dev);
  164. if (status < 0)
  165. goto fail_string_ids;
  166. msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  167. status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
  168. if (status < 0)
  169. goto fail_string_ids;
  170. usb_composite_overwrite_options(cdev, &coverwrite);
  171. dev_info(&cdev->gadget->dev,
  172. DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  173. set_bit(0, &msg_registered);
  174. return 0;
  175. fail_string_ids:
  176. fsg_common_remove_luns(opts->common);
  177. fail_set_cdev:
  178. fsg_common_free_luns(opts->common);
  179. fail_set_nluns:
  180. fsg_common_free_buffers(opts->common);
  181. fail:
  182. usb_put_function_instance(fi_msg);
  183. return status;
  184. }
  185. static int msg_unbind(struct usb_composite_dev *cdev)
  186. {
  187. if (!IS_ERR(f_msg))
  188. usb_put_function(f_msg);
  189. if (!IS_ERR(fi_msg))
  190. usb_put_function_instance(fi_msg);
  191. return 0;
  192. }
  193. /****************************** Some noise ******************************/
  194. static __refdata struct usb_composite_driver msg_driver = {
  195. .name = "g_mass_storage",
  196. .dev = &msg_device_desc,
  197. .max_speed = USB_SPEED_SUPER,
  198. .needs_serial = 1,
  199. .strings = dev_strings,
  200. .bind = msg_bind,
  201. .unbind = msg_unbind,
  202. };
  203. MODULE_DESCRIPTION(DRIVER_DESC);
  204. MODULE_AUTHOR("Michal Nazarewicz");
  205. MODULE_LICENSE("GPL");
  206. static int __init msg_init(void)
  207. {
  208. return usb_composite_probe(&msg_driver);
  209. }
  210. module_init(msg_init);
  211. static void msg_cleanup(void)
  212. {
  213. if (test_and_clear_bit(0, &msg_registered))
  214. usb_composite_unregister(&msg_driver);
  215. }
  216. module_exit(msg_cleanup);