mass_storage.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /*-------------------------------------------------------------------------*/
  42. /*
  43. * kbuild is not very cooperative with respect to linking separately
  44. * compiled library objects into one module. So for now we won't use
  45. * separate compilation ... ensuring init/exit sections work to shrink
  46. * the runtime footprint, and giving us at least some parts of what
  47. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  48. */
  49. #define USB_FMS_INCLUDED
  50. #include "f_mass_storage.c"
  51. /*-------------------------------------------------------------------------*/
  52. USB_GADGET_COMPOSITE_OPTIONS();
  53. static struct usb_device_descriptor msg_device_desc = {
  54. .bLength = sizeof msg_device_desc,
  55. .bDescriptorType = USB_DT_DEVICE,
  56. .bcdUSB = cpu_to_le16(0x0200),
  57. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  58. /* Vendor and product id can be overridden by module parameters. */
  59. .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  60. .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  61. .bNumConfigurations = 1,
  62. };
  63. static struct usb_otg_descriptor otg_descriptor = {
  64. .bLength = sizeof otg_descriptor,
  65. .bDescriptorType = USB_DT_OTG,
  66. /*
  67. * REVISIT SRP-only hardware is possible, although
  68. * it would not be called "OTG" ...
  69. */
  70. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  71. };
  72. static const struct usb_descriptor_header *otg_desc[] = {
  73. (struct usb_descriptor_header *) &otg_descriptor,
  74. NULL,
  75. };
  76. static struct usb_string strings_dev[] = {
  77. [USB_GADGET_MANUFACTURER_IDX].s = "",
  78. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  79. [USB_GADGET_SERIAL_IDX].s = "",
  80. { } /* end of list */
  81. };
  82. static struct usb_gadget_strings stringtab_dev = {
  83. .language = 0x0409, /* en-us */
  84. .strings = strings_dev,
  85. };
  86. static struct usb_gadget_strings *dev_strings[] = {
  87. &stringtab_dev,
  88. NULL,
  89. };
  90. /****************************** Configurations ******************************/
  91. static struct fsg_module_parameters mod_data = {
  92. .stall = 1
  93. };
  94. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  95. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  96. #else
  97. /*
  98. * Number of buffers we will use.
  99. * 2 is usually enough for good buffering pipeline
  100. */
  101. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  102. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  103. FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
  104. static unsigned long msg_registered;
  105. static void msg_cleanup(void);
  106. static int msg_thread_exits(struct fsg_common *common)
  107. {
  108. msg_cleanup();
  109. return 0;
  110. }
  111. static int __init msg_do_config(struct usb_configuration *c)
  112. {
  113. static const struct fsg_operations ops = {
  114. .thread_exits = msg_thread_exits,
  115. };
  116. static struct fsg_common common;
  117. struct fsg_common *retp;
  118. struct fsg_config config;
  119. int ret;
  120. if (gadget_is_otg(c->cdev->gadget)) {
  121. c->descriptors = otg_desc;
  122. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  123. }
  124. fsg_config_from_params(&config, &mod_data, fsg_num_buffers);
  125. config.ops = &ops;
  126. retp = fsg_common_init(&common, c->cdev, &config);
  127. if (IS_ERR(retp))
  128. return PTR_ERR(retp);
  129. ret = fsg_bind_config(c->cdev, c, &common);
  130. fsg_common_put(&common);
  131. return ret;
  132. }
  133. static struct usb_configuration msg_config_driver = {
  134. .label = "Linux File-Backed Storage",
  135. .bConfigurationValue = 1,
  136. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  137. };
  138. /****************************** Gadget Bind ******************************/
  139. static int __init msg_bind(struct usb_composite_dev *cdev)
  140. {
  141. int status;
  142. status = usb_string_ids_tab(cdev, strings_dev);
  143. if (status < 0)
  144. return status;
  145. msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  146. status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
  147. if (status < 0)
  148. return status;
  149. usb_composite_overwrite_options(cdev, &coverwrite);
  150. dev_info(&cdev->gadget->dev,
  151. DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  152. set_bit(0, &msg_registered);
  153. return 0;
  154. }
  155. /****************************** Some noise ******************************/
  156. static __refdata struct usb_composite_driver msg_driver = {
  157. .name = "g_mass_storage",
  158. .dev = &msg_device_desc,
  159. .max_speed = USB_SPEED_SUPER,
  160. .needs_serial = 1,
  161. .strings = dev_strings,
  162. .bind = msg_bind,
  163. };
  164. MODULE_DESCRIPTION(DRIVER_DESC);
  165. MODULE_AUTHOR("Michal Nazarewicz");
  166. MODULE_LICENSE("GPL");
  167. static int __init msg_init(void)
  168. {
  169. return usb_composite_probe(&msg_driver);
  170. }
  171. module_init(msg_init);
  172. static void msg_cleanup(void)
  173. {
  174. if (test_and_clear_bit(0, &msg_registered))
  175. usb_composite_unregister(&msg_driver);
  176. }
  177. module_exit(msg_cleanup);