mass_storage.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/utsname.h>
  29. #include <linux/usb/ch9.h>
  30. /*-------------------------------------------------------------------------*/
  31. #define DRIVER_DESC "Mass Storage Gadget"
  32. #define DRIVER_VERSION "2009/09/11"
  33. /*-------------------------------------------------------------------------*/
  34. /*
  35. * kbuild is not very cooperative with respect to linking separately
  36. * compiled library objects into one module. So for now we won't use
  37. * separate compilation ... ensuring init/exit sections work to shrink
  38. * the runtime footprint, and giving us at least some parts of what
  39. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  40. */
  41. #include "composite.c"
  42. #include "f_mass_storage.c"
  43. /*-------------------------------------------------------------------------*/
  44. USB_GADGET_COMPOSITE_OPTIONS();
  45. static struct usb_device_descriptor msg_device_desc = {
  46. .bLength = sizeof msg_device_desc,
  47. .bDescriptorType = USB_DT_DEVICE,
  48. .bcdUSB = cpu_to_le16(0x0200),
  49. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  50. /* Vendor and product id can be overridden by module parameters. */
  51. .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  52. .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  53. .bNumConfigurations = 1,
  54. };
  55. static struct usb_otg_descriptor otg_descriptor = {
  56. .bLength = sizeof otg_descriptor,
  57. .bDescriptorType = USB_DT_OTG,
  58. /*
  59. * REVISIT SRP-only hardware is possible, although
  60. * it would not be called "OTG" ...
  61. */
  62. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  63. };
  64. static const struct usb_descriptor_header *otg_desc[] = {
  65. (struct usb_descriptor_header *) &otg_descriptor,
  66. NULL,
  67. };
  68. /****************************** Configurations ******************************/
  69. static struct fsg_module_parameters mod_data = {
  70. .stall = 1
  71. };
  72. FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
  73. static unsigned long msg_registered;
  74. static void msg_cleanup(void);
  75. static int msg_thread_exits(struct fsg_common *common)
  76. {
  77. msg_cleanup();
  78. return 0;
  79. }
  80. static int __init msg_do_config(struct usb_configuration *c)
  81. {
  82. static const struct fsg_operations ops = {
  83. .thread_exits = msg_thread_exits,
  84. };
  85. static struct fsg_common common;
  86. struct fsg_common *retp;
  87. struct fsg_config config;
  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. fsg_config_from_params(&config, &mod_data);
  94. config.ops = &ops;
  95. retp = fsg_common_init(&common, c->cdev, &config);
  96. if (IS_ERR(retp))
  97. return PTR_ERR(retp);
  98. ret = fsg_bind_config(c->cdev, c, &common);
  99. fsg_common_put(&common);
  100. return ret;
  101. }
  102. static struct usb_configuration msg_config_driver = {
  103. .label = "Linux File-Backed Storage",
  104. .bConfigurationValue = 1,
  105. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  106. };
  107. /****************************** Gadget Bind ******************************/
  108. static int __init msg_bind(struct usb_composite_dev *cdev)
  109. {
  110. int status;
  111. status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
  112. if (status < 0)
  113. return status;
  114. usb_composite_overwrite_options(cdev, &coverwrite);
  115. dev_info(&cdev->gadget->dev,
  116. DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  117. set_bit(0, &msg_registered);
  118. return 0;
  119. }
  120. /****************************** Some noise ******************************/
  121. static __refdata struct usb_composite_driver msg_driver = {
  122. .name = "g_mass_storage",
  123. .dev = &msg_device_desc,
  124. .iProduct = DRIVER_DESC,
  125. .max_speed = USB_SPEED_SUPER,
  126. .needs_serial = 1,
  127. .bind = msg_bind,
  128. };
  129. MODULE_DESCRIPTION(DRIVER_DESC);
  130. MODULE_AUTHOR("Michal Nazarewicz");
  131. MODULE_LICENSE("GPL");
  132. static int __init msg_init(void)
  133. {
  134. return usb_composite_probe(&msg_driver);
  135. }
  136. module_init(msg_init);
  137. static void msg_cleanup(void)
  138. {
  139. if (test_and_clear_bit(0, &msg_registered))
  140. usb_composite_unregister(&msg_driver);
  141. }
  142. module_exit(msg_cleanup);