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