mass_storage.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <m.nazarewicz@samsung.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. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /*
  24. * The Mass Storage Gadget acts as a USB Mass Storage device,
  25. * appearing to the host as a disk drive or as a CD-ROM drive. In
  26. * addition to providing an example of a genuinely useful gadget
  27. * driver for a USB device, it also illustrates a technique of
  28. * double-buffering for increased throughput. Last but not least, it
  29. * gives an easy way to probe the behavior of the Mass Storage drivers
  30. * in a USB host.
  31. *
  32. * Since this file serves only administrative purposes and all the
  33. * business logic is implemented in f_mass_storage.* file. Read
  34. * comments in this file for more detailed description.
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/utsname.h>
  38. #include <linux/usb/ch9.h>
  39. /*-------------------------------------------------------------------------*/
  40. #define DRIVER_DESC "Mass Storage Gadget"
  41. #define DRIVER_VERSION "2009/09/11"
  42. /*-------------------------------------------------------------------------*/
  43. /*
  44. * kbuild is not very cooperative with respect to linking separately
  45. * compiled library objects into one module. So for now we won't use
  46. * separate compilation ... ensuring init/exit sections work to shrink
  47. * the runtime footprint, and giving us at least some parts of what
  48. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  49. */
  50. #include "composite.c"
  51. #include "usbstring.c"
  52. #include "config.c"
  53. #include "epautoconf.c"
  54. #include "f_mass_storage.c"
  55. /*-------------------------------------------------------------------------*/
  56. static struct usb_device_descriptor msg_device_desc = {
  57. .bLength = sizeof msg_device_desc,
  58. .bDescriptorType = USB_DT_DEVICE,
  59. .bcdUSB = cpu_to_le16(0x0200),
  60. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  61. /* Vendor and product id can be overridden by module parameters. */
  62. .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  63. .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  64. /* .bcdDevice = f(hardware) */
  65. /* .iManufacturer = DYNAMIC */
  66. /* .iProduct = DYNAMIC */
  67. /* NO SERIAL NUMBER */
  68. .bNumConfigurations = 1,
  69. };
  70. static struct usb_otg_descriptor otg_descriptor = {
  71. .bLength = sizeof otg_descriptor,
  72. .bDescriptorType = USB_DT_OTG,
  73. /* REVISIT SRP-only hardware is possible, although
  74. * it would not be called "OTG" ...
  75. */
  76. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  77. };
  78. static const struct usb_descriptor_header *otg_desc[] = {
  79. (struct usb_descriptor_header *) &otg_descriptor,
  80. NULL,
  81. };
  82. /* string IDs are assigned dynamically */
  83. #define STRING_MANUFACTURER_IDX 0
  84. #define STRING_PRODUCT_IDX 1
  85. #define STRING_CONFIGURATION_IDX 2
  86. static char manufacturer[50];
  87. static struct usb_string strings_dev[] = {
  88. [STRING_MANUFACTURER_IDX].s = manufacturer,
  89. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  90. [STRING_CONFIGURATION_IDX].s = "Self Powered",
  91. { } /* end of list */
  92. };
  93. static struct usb_gadget_strings stringtab_dev = {
  94. .language = 0x0409, /* en-us */
  95. .strings = strings_dev,
  96. };
  97. static struct usb_gadget_strings *dev_strings[] = {
  98. &stringtab_dev,
  99. NULL,
  100. };
  101. /****************************** Configurations ******************************/
  102. static struct fsg_module_parameters mod_data = {
  103. .stall = 1
  104. };
  105. FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
  106. static unsigned long msg_registered = 0;
  107. static void msg_cleanup(void);
  108. static int msg_thread_exits(struct fsg_common *common)
  109. {
  110. msg_cleanup();
  111. return 0;
  112. }
  113. static int __init msg_do_config(struct usb_configuration *c)
  114. {
  115. static struct fsg_common common;
  116. struct fsg_common *retp;
  117. struct fsg_config config;
  118. int ret;
  119. if (gadget_is_otg(c->cdev->gadget)) {
  120. c->descriptors = otg_desc;
  121. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  122. }
  123. fsg_config_from_params(&config, &mod_data);
  124. config.thread_exits = msg_thread_exits;
  125. retp = fsg_common_init(&common, c->cdev, &config);
  126. if (IS_ERR(retp))
  127. return PTR_ERR(retp);
  128. ret = fsg_bind_config(c->cdev, c, &common);
  129. fsg_common_put(&common);
  130. return ret;
  131. }
  132. static struct usb_configuration msg_config_driver = {
  133. .label = "Linux File-Backed Storage",
  134. .bind = msg_do_config,
  135. .bConfigurationValue = 1,
  136. /* .iConfiguration = DYNAMIC */
  137. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  138. };
  139. /****************************** Gadget Bind ******************************/
  140. static int __init msg_bind(struct usb_composite_dev *cdev)
  141. {
  142. struct usb_gadget *gadget = cdev->gadget;
  143. int status;
  144. /* Allocate string descriptor numbers ... note that string
  145. * contents can be overridden by the composite_dev glue.
  146. */
  147. /* device descriptor strings: manufacturer, product */
  148. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  149. init_utsname()->sysname, init_utsname()->release,
  150. gadget->name);
  151. status = usb_string_id(cdev);
  152. if (status < 0)
  153. return status;
  154. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  155. msg_device_desc.iManufacturer = status;
  156. status = usb_string_id(cdev);
  157. if (status < 0)
  158. return status;
  159. strings_dev[STRING_PRODUCT_IDX].id = status;
  160. msg_device_desc.iProduct = status;
  161. status = usb_string_id(cdev);
  162. if (status < 0)
  163. return status;
  164. strings_dev[STRING_CONFIGURATION_IDX].id = status;
  165. msg_config_driver.iConfiguration = status;
  166. /* register our second configuration */
  167. status = usb_add_config(cdev, &msg_config_driver);
  168. if (status < 0)
  169. return status;
  170. dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  171. set_bit(0, &msg_registered);
  172. return 0;
  173. }
  174. /****************************** Some noise ******************************/
  175. static struct usb_composite_driver msg_driver = {
  176. .name = "g_mass_storage",
  177. .dev = &msg_device_desc,
  178. .strings = dev_strings,
  179. .bind = msg_bind,
  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_register(&msg_driver);
  187. }
  188. module_init(msg_init);
  189. static void msg_cleanup(void)
  190. {
  191. if (test_and_clear_bit(0, &msg_registered))
  192. usb_composite_unregister(&msg_driver);
  193. }
  194. module_exit(msg_cleanup);