mass-storage.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. * Overview
  2. Mass Storage Gadget (or MSG) acts as a USB Mass Storage device,
  3. appearing to the host as a disk or a CD-ROM drive. It supports
  4. multiple logical units (LUNs). Backing storage for each LUN is
  5. provided by a regular file or a block device, access can be limited
  6. to read-only, and gadget can indicate that it is removable and/or
  7. CD-ROM (the latter implies read-only access).
  8. Its requirements are modest; only a bulk-in and a bulk-out endpoint
  9. are needed. The memory requirement amounts to two 16K buffers.
  10. Support is included for full-speed, high-speed and SuperSpeed
  11. operation.
  12. Note that the driver is slightly non-portable in that it assumes
  13. a single memory/DMA buffer will be usable for bulk-in and bulk-out
  14. endpoints. With most device controllers this is not an issue, but
  15. there may be some with hardware restrictions that prevent a buffer
  16. from being used by more than one endpoint.
  17. This document describes how to use the gadget from user space, its
  18. relation to mass storage function (or MSF) and different gadgets
  19. using it, and how it differs from File Storage Gadget (or FSG)
  20. (which is no longer included in Linux). It will talk only briefly
  21. about how to use MSF within composite gadgets.
  22. * Module parameters
  23. The mass storage gadget accepts the following mass storage specific
  24. module parameters:
  25. - file=filename[,filename...]
  26. This parameter lists paths to files or block devices used for
  27. backing storage for each logical unit. There may be at most
  28. FSG_MAX_LUNS (8) LUNs set. If more files are specified, they will
  29. be silently ignored. See also “luns” parameter.
  30. *BEWARE* that if a file is used as a backing storage, it may not
  31. be modified by any other process. This is because the host
  32. assumes the data does not change without its knowledge. It may be
  33. read, but (if the logical unit is writable) due to buffering on
  34. the host side, the contents are not well defined.
  35. The size of the logical unit will be rounded down to a full
  36. logical block. The logical block size is 2048 bytes for LUNs
  37. simulating CD-ROM, block size of the device if the backing file is
  38. a block device, or 512 bytes otherwise.
  39. - removable=b[,b...]
  40. This parameter specifies whether each logical unit should be
  41. removable. “b” here is either “y”, “Y” or “1” for true or “n”,
  42. “N” or “0” for false.
  43. If this option is set for a logical unit, gadget will accept an
  44. “eject” SCSI request (Start/Stop Unit). When it is sent, the
  45. backing file will be closed to simulate ejection and the logical
  46. unit will not be mountable by the host until a new backing file is
  47. specified by userspace on the device (see “sysfs entries”
  48. section).
  49. If a logical unit is not removable (the default), a backing file
  50. must be specified for it with the “file” parameter as the module
  51. is loaded. The same applies if the module is built in, no
  52. exceptions.
  53. The default value of the flag is false, *HOWEVER* it used to be
  54. true. This has been changed to better match File Storage Gadget
  55. and because it seems like a saner default after all. Thus to
  56. maintain compatibility with older kernels, it's best to specify
  57. the default values. Also, if one relied on old default, explicit
  58. “n” needs to be specified now.
  59. Note that “removable” means the logical unit's media can be
  60. ejected or removed (as is true for a CD-ROM drive or a card
  61. reader). It does *not* mean that the entire gadget can be
  62. unplugged from the host; the proper term for that is
  63. “hot-unpluggable”.
  64. - cdrom=b[,b...]
  65. This parameter specifies whether each logical unit should simulate
  66. CD-ROM. The default is false.
  67. - ro=b[,b...]
  68. This parameter specifies whether each logical unit should be
  69. reported as read only. This will prevent host from modifying the
  70. backing files.
  71. Note that if this flag for given logical unit is false but the
  72. backing file could not be opened in read/write mode, the gadget
  73. will fall back to read only mode anyway.
  74. The default value for non-CD-ROM logical units is false; for
  75. logical units simulating CD-ROM it is forced to true.
  76. - nofua=b[,b...]
  77. This parameter specifies whether FUA flag should be ignored in SCSI
  78. Write10 and Write12 commands sent to given logical units.
  79. MS Windows mounts removable storage in “Removal optimised mode” by
  80. default. All the writes to the media are synchronous, which is
  81. achieved by setting the FUA (Force Unit Access) bit in SCSI
  82. Write(10,12) commands. This forces each write to wait until the
  83. data has actually been written out and prevents I/O requests
  84. aggregation in block layer dramatically decreasing performance.
  85. Note that this may mean that if the device is powered from USB and
  86. the user unplugs the device without unmounting it first (which at
  87. least some Windows users do), the data may be lost.
  88. The default value is false.
  89. - luns=N
  90. This parameter specifies number of logical units the gadget will
  91. have. It is limited by FSG_MAX_LUNS (8) and higher value will be
  92. capped.
  93. If this parameter is provided, and the number of files specified
  94. in “file” argument is greater then the value of “luns”, all excess
  95. files will be ignored.
  96. If this parameter is not present, the number of logical units will
  97. be deduced from the number of files specified in the “file”
  98. parameter. If the file parameter is missing as well, one is
  99. assumed.
  100. - stall=b
  101. Specifies whether the gadget is allowed to halt bulk endpoints.
  102. The default is determined according to the type of USB device
  103. controller, but usually true.
  104. In addition to the above, the gadget also accepts the following
  105. parameters defined by the composite framework (they are common to
  106. all composite gadgets so just a quick listing):
  107. - idVendor -- USB Vendor ID (16 bit integer)
  108. - idProduct -- USB Product ID (16 bit integer)
  109. - bcdDevice -- USB Device version (BCD) (16 bit integer)
  110. - iManufacturer -- USB Manufacturer string (string)
  111. - iProduct -- USB Product string (string)
  112. - iSerialNumber -- SerialNumber string (sting)
  113. * sysfs entries
  114. For each logical unit, the gadget creates a directory in the sysfs
  115. hierarchy. Inside of it the following three files are created:
  116. - file
  117. When read it returns the path to the backing file for the given
  118. logical unit. If there is no backing file (possible only if the
  119. logical unit is removable), the content is empty.
  120. When written into, it changes the backing file for given logical
  121. unit. This change can be performed even if given logical unit is
  122. not specified as removable (but that may look strange to the
  123. host). It may fail, however, if host disallowed medium removal
  124. with the Prevent-Allow Medium Removal SCSI command.
  125. - ro
  126. Reflects the state of ro flag for the given logical unit. It can
  127. be read any time, and written to when there is no backing file
  128. open for given logical unit.
  129. - nofua
  130. Reflects the state of nofua flag for given logical unit. It can
  131. be read and written.
  132. Other then those, as usual, the values of module parameters can be
  133. read from /sys/module/g_mass_storage/parameters/* files.
  134. * Other gadgets using mass storage function
  135. The Mass Storage Gadget uses the Mass Storage Function to handle
  136. mass storage protocol. As a composite function, MSF may be used by
  137. other gadgets as well (eg. g_multi and acm_ms).
  138. All of the information in previous sections are valid for other
  139. gadgets using MSF, except that support for mass storage related
  140. module parameters may be missing, or the parameters may have
  141. a prefix. To figure out whether any of this is true one needs to
  142. consult the gadget's documentation or its source code.
  143. For examples of how to include mass storage function in gadgets, one
  144. may take a look at mass_storage.c, acm_ms.c and multi.c (sorted by
  145. complexity).
  146. * Relation to file storage gadget
  147. The Mass Storage Function and thus the Mass Storage Gadget has been
  148. based on the File Storage Gadget. The difference between the two is
  149. that MSG is a composite gadget (ie. uses the composite framework)
  150. while file storage gadget was a traditional gadget. From userspace
  151. point of view this distinction does not really matter, but from
  152. kernel hacker's point of view, this means that (i) MSG does not
  153. duplicate code needed for handling basic USB protocol commands and
  154. (ii) MSF can be used in any other composite gadget.
  155. Because of that, File Storage Gadget has been removed in Linux 3.8.
  156. All users need to transition to the Mass Storage Gadget. The two
  157. gadgets behave mostly the same from the outside except:
  158. 1. In FSG the “removable” and “cdrom” module parameters set the flag
  159. for all logical units whereas in MSG they accept a list of y/n
  160. values for each logical unit. If one uses only a single logical
  161. unit this does not matter, but if there are more, the y/n value
  162. needs to be repeated for each logical unit.
  163. 2. FSG's “serial”, “vendor”, “product” and “release” module
  164. parameters are handled in MSG by the composite layer's parameters
  165. named respectively: “iSerialnumber”, “idVendor”, “idProduct” and
  166. “bcdDevice”.
  167. 3. MSG does not support FSG's test mode, thus “transport”,
  168. “protocol” and “buflen” FSG's module parameters are not
  169. supported. MSG always uses SCSI protocol with bulk only
  170. transport mode and 16 KiB buffers.