1e6aa504d58a9390d196a0b5c3823e3e4f299e69
[linux-2.6.git] / drivers / usb / gadget / file_storage.c
1 /*
2  * file_storage.c -- File-backed USB Storage Gadget, for USB development
3  *
4  * Copyright (C) 2003-2008 Alan Stern
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The names of the above-listed copyright holders may not be used
17  *    to endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * ALTERNATIVELY, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") as published by the Free Software
22  * Foundation, either version 2 of that License or (at your option) any
23  * later version.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38
39 /*
40  * The File-backed Storage Gadget acts as a USB Mass Storage device,
41  * appearing to the host as a disk drive or as a CD-ROM drive.  In addition
42  * to providing an example of a genuinely useful gadget driver for a USB
43  * device, it also illustrates a technique of double-buffering for increased
44  * throughput.  Last but not least, it gives an easy way to probe the
45  * behavior of the Mass Storage drivers in a USB host.
46  *
47  * Backing storage is provided by a regular file or a block device, specified
48  * by the "file" module parameter.  Access can be limited to read-only by
49  * setting the optional "ro" module parameter.  (For CD-ROM emulation,
50  * access is always read-only.)  The gadget will indicate that it has
51  * removable media if the optional "removable" module parameter is set.
52  *
53  * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
54  * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
55  * by the optional "transport" module parameter.  It also supports the
56  * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
57  * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
58  * the optional "protocol" module parameter.  In addition, the default
59  * Vendor ID, Product ID, and release number can be overridden.
60  *
61  * There is support for multiple logical units (LUNs), each of which has
62  * its own backing file.  The number of LUNs can be set using the optional
63  * "luns" module parameter (anywhere from 1 to 8), and the corresponding
64  * files are specified using comma-separated lists for "file" and "ro".
65  * The default number of LUNs is taken from the number of "file" elements;
66  * it is 1 if "file" is not given.  If "removable" is not set then a backing
67  * file must be specified for each LUN.  If it is set, then an unspecified
68  * or empty backing filename means the LUN's medium is not loaded.  Ideally
69  * each LUN would be settable independently as a disk drive or a CD-ROM
70  * drive, but currently all LUNs have to be the same type.  The CD-ROM
71  * emulation includes a single data track and no audio tracks; hence there
72  * need be only one backing file per LUN.  Note also that the CD-ROM block
73  * length is set to 512 rather than the more common value 2048.
74  *
75  * Requirements are modest; only a bulk-in and a bulk-out endpoint are
76  * needed (an interrupt-out endpoint is also needed for CBI).  The memory
77  * requirement amounts to two 16K buffers, size configurable by a parameter.
78  * Support is included for both full-speed and high-speed operation.
79  *
80  * Note that the driver is slightly non-portable in that it assumes a
81  * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
82  * interrupt-in endpoints.  With most device controllers this isn't an
83  * issue, but there may be some with hardware restrictions that prevent
84  * a buffer from being used by more than one endpoint.
85  *
86  * Module options:
87  *
88  *      file=filename[,filename...]
89  *                              Required if "removable" is not set, names of
90  *                                      the files or block devices used for
91  *                                      backing storage
92  *      ro=b[,b...]             Default false, booleans for read-only access
93  *      removable               Default false, boolean for removable media
94  *      luns=N                  Default N = number of filenames, number of
95  *                                      LUNs to support
96  *      stall                   Default determined according to the type of
97  *                                      USB device controller (usually true),
98  *                                      boolean to permit the driver to halt
99  *                                      bulk endpoints
100  *      cdrom                   Default false, boolean for whether to emulate
101  *                                      a CD-ROM drive
102  *      transport=XXX           Default BBB, transport name (CB, CBI, or BBB)
103  *      protocol=YYY            Default SCSI, protocol name (RBC, 8020 or
104  *                                      ATAPI, QIC, UFI, 8070, or SCSI;
105  *                                      also 1 - 6)
106  *      vendor=0xVVVV           Default 0x0525 (NetChip), USB Vendor ID
107  *      product=0xPPPP          Default 0xa4a5 (FSG), USB Product ID
108  *      release=0xRRRR          Override the USB release number (bcdDevice)
109  *      buflen=N                Default N=16384, buffer size used (will be
110  *                                      rounded down to a multiple of
111  *                                      PAGE_CACHE_SIZE)
112  *
113  * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "ro",
114  * "removable", "luns", "stall", and "cdrom" options are available; default
115  * values are used for everything else.
116  *
117  * The pathnames of the backing files and the ro settings are available in
118  * the attribute files "file" and "ro" in the lun<n> subdirectory of the
119  * gadget's sysfs directory.  If the "removable" option is set, writing to
120  * these files will simulate ejecting/loading the medium (writing an empty
121  * line means eject) and adjusting a write-enable tab.  Changes to the ro
122  * setting are not allowed when the medium is loaded or if CD-ROM emulation
123  * is being used.
124  *
125  * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
126  * The driver's SCSI command interface was based on the "Information
127  * technology - Small Computer System Interface - 2" document from
128  * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
129  * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.  The single exception
130  * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
131  * "Universal Serial Bus Mass Storage Class UFI Command Specification"
132  * document, Revision 1.0, December 14, 1998, available at
133  * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
134  */
135
136
137 /*
138  *                              Driver Design
139  *
140  * The FSG driver is fairly straightforward.  There is a main kernel
141  * thread that handles most of the work.  Interrupt routines field
142  * callbacks from the controller driver: bulk- and interrupt-request
143  * completion notifications, endpoint-0 events, and disconnect events.
144  * Completion events are passed to the main thread by wakeup calls.  Many
145  * ep0 requests are handled at interrupt time, but SetInterface,
146  * SetConfiguration, and device reset requests are forwarded to the
147  * thread in the form of "exceptions" using SIGUSR1 signals (since they
148  * should interrupt any ongoing file I/O operations).
149  *
150  * The thread's main routine implements the standard command/data/status
151  * parts of a SCSI interaction.  It and its subroutines are full of tests
152  * for pending signals/exceptions -- all this polling is necessary since
153  * the kernel has no setjmp/longjmp equivalents.  (Maybe this is an
154  * indication that the driver really wants to be running in userspace.)
155  * An important point is that so long as the thread is alive it keeps an
156  * open reference to the backing file.  This will prevent unmounting
157  * the backing file's underlying filesystem and could cause problems
158  * during system shutdown, for example.  To prevent such problems, the
159  * thread catches INT, TERM, and KILL signals and converts them into
160  * an EXIT exception.
161  *
162  * In normal operation the main thread is started during the gadget's
163  * fsg_bind() callback and stopped during fsg_unbind().  But it can also
164  * exit when it receives a signal, and there's no point leaving the
165  * gadget running when the thread is dead.  So just before the thread
166  * exits, it deregisters the gadget driver.  This makes things a little
167  * tricky: The driver is deregistered at two places, and the exiting
168  * thread can indirectly call fsg_unbind() which in turn can tell the
169  * thread to exit.  The first problem is resolved through the use of the
170  * REGISTERED atomic bitflag; the driver will only be deregistered once.
171  * The second problem is resolved by having fsg_unbind() check
172  * fsg->state; it won't try to stop the thread if the state is already
173  * FSG_STATE_TERMINATED.
174  *
175  * To provide maximum throughput, the driver uses a circular pipeline of
176  * buffer heads (struct fsg_buffhd).  In principle the pipeline can be
177  * arbitrarily long; in practice the benefits don't justify having more
178  * than 2 stages (i.e., double buffering).  But it helps to think of the
179  * pipeline as being a long one.  Each buffer head contains a bulk-in and
180  * a bulk-out request pointer (since the buffer can be used for both
181  * output and input -- directions always are given from the host's
182  * point of view) as well as a pointer to the buffer and various state
183  * variables.
184  *
185  * Use of the pipeline follows a simple protocol.  There is a variable
186  * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
187  * At any time that buffer head may still be in use from an earlier
188  * request, so each buffer head has a state variable indicating whether
189  * it is EMPTY, FULL, or BUSY.  Typical use involves waiting for the
190  * buffer head to be EMPTY, filling the buffer either by file I/O or by
191  * USB I/O (during which the buffer head is BUSY), and marking the buffer
192  * head FULL when the I/O is complete.  Then the buffer will be emptied
193  * (again possibly by USB I/O, during which it is marked BUSY) and
194  * finally marked EMPTY again (possibly by a completion routine).
195  *
196  * A module parameter tells the driver to avoid stalling the bulk
197  * endpoints wherever the transport specification allows.  This is
198  * necessary for some UDCs like the SuperH, which cannot reliably clear a
199  * halt on a bulk endpoint.  However, under certain circumstances the
200  * Bulk-only specification requires a stall.  In such cases the driver
201  * will halt the endpoint and set a flag indicating that it should clear
202  * the halt in software during the next device reset.  Hopefully this
203  * will permit everything to work correctly.  Furthermore, although the
204  * specification allows the bulk-out endpoint to halt when the host sends
205  * too much data, implementing this would cause an unavoidable race.
206  * The driver will always use the "no-stall" approach for OUT transfers.
207  *
208  * One subtle point concerns sending status-stage responses for ep0
209  * requests.  Some of these requests, such as device reset, can involve
210  * interrupting an ongoing file I/O operation, which might take an
211  * arbitrarily long time.  During that delay the host might give up on
212  * the original ep0 request and issue a new one.  When that happens the
213  * driver should not notify the host about completion of the original
214  * request, as the host will no longer be waiting for it.  So the driver
215  * assigns to each ep0 request a unique tag, and it keeps track of the
216  * tag value of the request associated with a long-running exception
217  * (device-reset, interface-change, or configuration-change).  When the
218  * exception handler is finished, the status-stage response is submitted
219  * only if the current ep0 request tag is equal to the exception request
220  * tag.  Thus only the most recently received ep0 request will get a
221  * status-stage response.
222  *
223  * Warning: This driver source file is too long.  It ought to be split up
224  * into a header file plus about 3 separate .c files, to handle the details
225  * of the Gadget, USB Mass Storage, and SCSI protocols.
226  */
227
228
229 /* #define VERBOSE_DEBUG */
230 /* #define DUMP_MSGS */
231
232
233 #include <linux/blkdev.h>
234 #include <linux/completion.h>
235 #include <linux/dcache.h>
236 #include <linux/delay.h>
237 #include <linux/device.h>
238 #include <linux/fcntl.h>
239 #include <linux/file.h>
240 #include <linux/fs.h>
241 #include <linux/kref.h>
242 #include <linux/kthread.h>
243 #include <linux/limits.h>
244 #include <linux/rwsem.h>
245 #include <linux/slab.h>
246 #include <linux/spinlock.h>
247 #include <linux/string.h>
248 #include <linux/freezer.h>
249 #include <linux/utsname.h>
250
251 #include <asm/unaligned.h>
252
253 #include <linux/usb/ch9.h>
254 #include <linux/usb/gadget.h>
255
256 #include "gadget_chips.h"
257
258
259
260 /*
261  * Kbuild is not very cooperative with respect to linking separately
262  * compiled library objects into one module.  So for now we won't use
263  * separate compilation ... ensuring init/exit sections work to shrink
264  * the runtime footprint, and giving us at least some parts of what
265  * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
266  */
267 #include "usbstring.c"
268 #include "config.c"
269 #include "epautoconf.c"
270
271 /*-------------------------------------------------------------------------*/
272
273 #define DRIVER_DESC             "File-backed Storage Gadget"
274 #define DRIVER_NAME             "g_file_storage"
275 #define DRIVER_VERSION          "20 November 2008"
276
277 static const char longname[] = DRIVER_DESC;
278 static const char shortname[] = DRIVER_NAME;
279
280 MODULE_DESCRIPTION(DRIVER_DESC);
281 MODULE_AUTHOR("Alan Stern");
282 MODULE_LICENSE("Dual BSD/GPL");
283
284 /* Thanks to NetChip Technologies for donating this product ID.
285  *
286  * DO NOT REUSE THESE IDs with any other driver!!  Ever!!
287  * Instead:  allocate your own, using normal USB-IF procedures. */
288 #define DRIVER_VENDOR_ID        0x0525  // NetChip
289 #define DRIVER_PRODUCT_ID       0xa4a5  // Linux-USB File-backed Storage Gadget
290
291
292 /*
293  * This driver assumes self-powered hardware and has no way for users to
294  * trigger remote wakeup.  It uses autoconfiguration to select endpoints
295  * and endpoint addresses.
296  */
297
298
299 /*-------------------------------------------------------------------------*/
300
301 #define LDBG(lun,fmt,args...) \
302         dev_dbg(&(lun)->dev , fmt , ## args)
303 #define MDBG(fmt,args...) \
304         pr_debug(DRIVER_NAME ": " fmt , ## args)
305
306 #ifndef DEBUG
307 #undef VERBOSE_DEBUG
308 #undef DUMP_MSGS
309 #endif /* !DEBUG */
310
311 #ifdef VERBOSE_DEBUG
312 #define VLDBG   LDBG
313 #else
314 #define VLDBG(lun,fmt,args...) \
315         do { } while (0)
316 #endif /* VERBOSE_DEBUG */
317
318 #define LERROR(lun,fmt,args...) \
319         dev_err(&(lun)->dev , fmt , ## args)
320 #define LWARN(lun,fmt,args...) \
321         dev_warn(&(lun)->dev , fmt , ## args)
322 #define LINFO(lun,fmt,args...) \
323         dev_info(&(lun)->dev , fmt , ## args)
324
325 #define MINFO(fmt,args...) \
326         pr_info(DRIVER_NAME ": " fmt , ## args)
327
328 #define DBG(d, fmt, args...) \
329         dev_dbg(&(d)->gadget->dev , fmt , ## args)
330 #define VDBG(d, fmt, args...) \
331         dev_vdbg(&(d)->gadget->dev , fmt , ## args)
332 #define ERROR(d, fmt, args...) \
333         dev_err(&(d)->gadget->dev , fmt , ## args)
334 #define WARNING(d, fmt, args...) \
335         dev_warn(&(d)->gadget->dev , fmt , ## args)
336 #define INFO(d, fmt, args...) \
337         dev_info(&(d)->gadget->dev , fmt , ## args)
338
339
340 /*-------------------------------------------------------------------------*/
341
342 /* Encapsulate the module parameter settings */
343
344 #define MAX_LUNS        8
345
346 static struct {
347         char            *file[MAX_LUNS];
348         int             ro[MAX_LUNS];
349         unsigned int    num_filenames;
350         unsigned int    num_ros;
351         unsigned int    nluns;
352
353         int             removable;
354         int             can_stall;
355         int             cdrom;
356
357         char            *transport_parm;
358         char            *protocol_parm;
359         unsigned short  vendor;
360         unsigned short  product;
361         unsigned short  release;
362         unsigned int    buflen;
363
364         int             transport_type;
365         char            *transport_name;
366         int             protocol_type;
367         char            *protocol_name;
368
369 } mod_data = {                                  // Default values
370         .transport_parm         = "BBB",
371         .protocol_parm          = "SCSI",
372         .removable              = 0,
373         .can_stall              = 1,
374         .cdrom                  = 0,
375         .vendor                 = DRIVER_VENDOR_ID,
376         .product                = DRIVER_PRODUCT_ID,
377         .release                = 0xffff,       // Use controller chip type
378         .buflen                 = 16384,
379         };
380
381
382 module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
383                 S_IRUGO);
384 MODULE_PARM_DESC(file, "names of backing files or devices");
385
386 module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
387 MODULE_PARM_DESC(ro, "true to force read-only");
388
389 module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
390 MODULE_PARM_DESC(luns, "number of LUNs");
391
392 module_param_named(removable, mod_data.removable, bool, S_IRUGO);
393 MODULE_PARM_DESC(removable, "true to simulate removable media");
394
395 module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
396 MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
397
398 module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
399 MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
400
401
402 /* In the non-TEST version, only the module parameters listed above
403  * are available. */
404 #ifdef CONFIG_USB_FILE_STORAGE_TEST
405
406 module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
407 MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
408
409 module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
410 MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
411                 "8070, or SCSI)");
412
413 module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
414 MODULE_PARM_DESC(vendor, "USB Vendor ID");
415
416 module_param_named(product, mod_data.product, ushort, S_IRUGO);
417 MODULE_PARM_DESC(product, "USB Product ID");
418
419 module_param_named(release, mod_data.release, ushort, S_IRUGO);
420 MODULE_PARM_DESC(release, "USB release number");
421
422 module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
423 MODULE_PARM_DESC(buflen, "I/O buffer size");
424
425 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
426
427
428 /*-------------------------------------------------------------------------*/
429
430 /* SCSI device types */
431 #define TYPE_DISK       0x00
432 #define TYPE_CDROM      0x05
433
434 /* USB protocol value = the transport method */
435 #define USB_PR_CBI      0x00            // Control/Bulk/Interrupt
436 #define USB_PR_CB       0x01            // Control/Bulk w/o interrupt
437 #define USB_PR_BULK     0x50            // Bulk-only
438
439 /* USB subclass value = the protocol encapsulation */
440 #define USB_SC_RBC      0x01            // Reduced Block Commands (flash)
441 #define USB_SC_8020     0x02            // SFF-8020i, MMC-2, ATAPI (CD-ROM)
442 #define USB_SC_QIC      0x03            // QIC-157 (tape)
443 #define USB_SC_UFI      0x04            // UFI (floppy)
444 #define USB_SC_8070     0x05            // SFF-8070i (removable)
445 #define USB_SC_SCSI     0x06            // Transparent SCSI
446
447 /* Bulk-only data structures */
448
449 /* Command Block Wrapper */
450 struct bulk_cb_wrap {
451         __le32  Signature;              // Contains 'USBC'
452         u32     Tag;                    // Unique per command id
453         __le32  DataTransferLength;     // Size of the data
454         u8      Flags;                  // Direction in bit 7
455         u8      Lun;                    // LUN (normally 0)
456         u8      Length;                 // Of the CDB, <= MAX_COMMAND_SIZE
457         u8      CDB[16];                // Command Data Block
458 };
459
460 #define USB_BULK_CB_WRAP_LEN    31
461 #define USB_BULK_CB_SIG         0x43425355      // Spells out USBC
462 #define USB_BULK_IN_FLAG        0x80
463
464 /* Command Status Wrapper */
465 struct bulk_cs_wrap {
466         __le32  Signature;              // Should = 'USBS'
467         u32     Tag;                    // Same as original command
468         __le32  Residue;                // Amount not transferred
469         u8      Status;                 // See below
470 };
471
472 #define USB_BULK_CS_WRAP_LEN    13
473 #define USB_BULK_CS_SIG         0x53425355      // Spells out 'USBS'
474 #define USB_STATUS_PASS         0
475 #define USB_STATUS_FAIL         1
476 #define USB_STATUS_PHASE_ERROR  2
477
478 /* Bulk-only class specific requests */
479 #define USB_BULK_RESET_REQUEST          0xff
480 #define USB_BULK_GET_MAX_LUN_REQUEST    0xfe
481
482
483 /* CBI Interrupt data structure */
484 struct interrupt_data {
485         u8      bType;
486         u8      bValue;
487 };
488
489 #define CBI_INTERRUPT_DATA_LEN          2
490
491 /* CBI Accept Device-Specific Command request */
492 #define USB_CBI_ADSC_REQUEST            0x00
493
494
495 #define MAX_COMMAND_SIZE        16      // Length of a SCSI Command Data Block
496
497 /* SCSI commands that we recognize */
498 #define SC_FORMAT_UNIT                  0x04
499 #define SC_INQUIRY                      0x12
500 #define SC_MODE_SELECT_6                0x15
501 #define SC_MODE_SELECT_10               0x55
502 #define SC_MODE_SENSE_6                 0x1a
503 #define SC_MODE_SENSE_10                0x5a
504 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
505 #define SC_READ_6                       0x08
506 #define SC_READ_10                      0x28
507 #define SC_READ_12                      0xa8
508 #define SC_READ_CAPACITY                0x25
509 #define SC_READ_FORMAT_CAPACITIES       0x23
510 #define SC_READ_HEADER                  0x44
511 #define SC_READ_TOC                     0x43
512 #define SC_RELEASE                      0x17
513 #define SC_REQUEST_SENSE                0x03
514 #define SC_RESERVE                      0x16
515 #define SC_SEND_DIAGNOSTIC              0x1d
516 #define SC_START_STOP_UNIT              0x1b
517 #define SC_SYNCHRONIZE_CACHE            0x35
518 #define SC_TEST_UNIT_READY              0x00
519 #define SC_VERIFY                       0x2f
520 #define SC_WRITE_6                      0x0a
521 #define SC_WRITE_10                     0x2a
522 #define SC_WRITE_12                     0xaa
523
524 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
525 #define SS_NO_SENSE                             0
526 #define SS_COMMUNICATION_FAILURE                0x040800
527 #define SS_INVALID_COMMAND                      0x052000
528 #define SS_INVALID_FIELD_IN_CDB                 0x052400
529 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE   0x052100
530 #define SS_LOGICAL_UNIT_NOT_SUPPORTED           0x052500
531 #define SS_MEDIUM_NOT_PRESENT                   0x023a00
532 #define SS_MEDIUM_REMOVAL_PREVENTED             0x055302
533 #define SS_NOT_READY_TO_READY_TRANSITION        0x062800
534 #define SS_RESET_OCCURRED                       0x062900
535 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED      0x053900
536 #define SS_UNRECOVERED_READ_ERROR               0x031100
537 #define SS_WRITE_ERROR                          0x030c02
538 #define SS_WRITE_PROTECTED                      0x072700
539
540 #define SK(x)           ((u8) ((x) >> 16))      // Sense Key byte, etc.
541 #define ASC(x)          ((u8) ((x) >> 8))
542 #define ASCQ(x)         ((u8) (x))
543
544
545 /*-------------------------------------------------------------------------*/
546
547 /*
548  * These definitions will permit the compiler to avoid generating code for
549  * parts of the driver that aren't used in the non-TEST version.  Even gcc
550  * can recognize when a test of a constant expression yields a dead code
551  * path.
552  */
553
554 #ifdef CONFIG_USB_FILE_STORAGE_TEST
555
556 #define transport_is_bbb()      (mod_data.transport_type == USB_PR_BULK)
557 #define transport_is_cbi()      (mod_data.transport_type == USB_PR_CBI)
558 #define protocol_is_scsi()      (mod_data.protocol_type == USB_SC_SCSI)
559
560 #else
561
562 #define transport_is_bbb()      1
563 #define transport_is_cbi()      0
564 #define protocol_is_scsi()      1
565
566 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
567
568
569 struct lun {
570         struct file     *filp;
571         loff_t          file_length;
572         loff_t          num_sectors;
573
574         unsigned int    ro : 1;
575         unsigned int    prevent_medium_removal : 1;
576         unsigned int    registered : 1;
577         unsigned int    info_valid : 1;
578
579         u32             sense_data;
580         u32             sense_data_info;
581         u32             unit_attention_data;
582
583         struct device   dev;
584 };
585
586 #define backing_file_is_open(curlun)    ((curlun)->filp != NULL)
587
588 static struct lun *dev_to_lun(struct device *dev)
589 {
590         return container_of(dev, struct lun, dev);
591 }
592
593
594 /* Big enough to hold our biggest descriptor */
595 #define EP0_BUFSIZE     256
596 #define DELAYED_STATUS  (EP0_BUFSIZE + 999)     // An impossibly large value
597
598 /* Number of buffers we will use.  2 is enough for double-buffering */
599 #define NUM_BUFFERS     2
600
601 enum fsg_buffer_state {
602         BUF_STATE_EMPTY = 0,
603         BUF_STATE_FULL,
604         BUF_STATE_BUSY
605 };
606
607 struct fsg_buffhd {
608         void                            *buf;
609         enum fsg_buffer_state           state;
610         struct fsg_buffhd               *next;
611
612         /* The NetChip 2280 is faster, and handles some protocol faults
613          * better, if we don't submit any short bulk-out read requests.
614          * So we will record the intended request length here. */
615         unsigned int                    bulk_out_intended_length;
616
617         struct usb_request              *inreq;
618         int                             inreq_busy;
619         struct usb_request              *outreq;
620         int                             outreq_busy;
621 };
622
623 enum fsg_state {
624         FSG_STATE_COMMAND_PHASE = -10,          // This one isn't used anywhere
625         FSG_STATE_DATA_PHASE,
626         FSG_STATE_STATUS_PHASE,
627
628         FSG_STATE_IDLE = 0,
629         FSG_STATE_ABORT_BULK_OUT,
630         FSG_STATE_RESET,
631         FSG_STATE_INTERFACE_CHANGE,
632         FSG_STATE_CONFIG_CHANGE,
633         FSG_STATE_DISCONNECT,
634         FSG_STATE_EXIT,
635         FSG_STATE_TERMINATED
636 };
637
638 enum data_direction {
639         DATA_DIR_UNKNOWN = 0,
640         DATA_DIR_FROM_HOST,
641         DATA_DIR_TO_HOST,
642         DATA_DIR_NONE
643 };
644
645 struct fsg_dev {
646         /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
647         spinlock_t              lock;
648         struct usb_gadget       *gadget;
649
650         /* filesem protects: backing files in use */
651         struct rw_semaphore     filesem;
652
653         /* reference counting: wait until all LUNs are released */
654         struct kref             ref;
655
656         struct usb_ep           *ep0;           // Handy copy of gadget->ep0
657         struct usb_request      *ep0req;        // For control responses
658         unsigned int            ep0_req_tag;
659         const char              *ep0req_name;
660
661         struct usb_request      *intreq;        // For interrupt responses
662         int                     intreq_busy;
663         struct fsg_buffhd       *intr_buffhd;
664
665         unsigned int            bulk_out_maxpacket;
666         enum fsg_state          state;          // For exception handling
667         unsigned int            exception_req_tag;
668
669         u8                      config, new_config;
670
671         unsigned int            running : 1;
672         unsigned int            bulk_in_enabled : 1;
673         unsigned int            bulk_out_enabled : 1;
674         unsigned int            intr_in_enabled : 1;
675         unsigned int            phase_error : 1;
676         unsigned int            short_packet_received : 1;
677         unsigned int            bad_lun_okay : 1;
678
679         unsigned long           atomic_bitflags;
680 #define REGISTERED              0
681 #define IGNORE_BULK_OUT         1
682 #define SUSPENDED               2
683
684         struct usb_ep           *bulk_in;
685         struct usb_ep           *bulk_out;
686         struct usb_ep           *intr_in;
687
688         struct fsg_buffhd       *next_buffhd_to_fill;
689         struct fsg_buffhd       *next_buffhd_to_drain;
690         struct fsg_buffhd       buffhds[NUM_BUFFERS];
691
692         int                     thread_wakeup_needed;
693         struct completion       thread_notifier;
694         struct task_struct      *thread_task;
695
696         int                     cmnd_size;
697         u8                      cmnd[MAX_COMMAND_SIZE];
698         enum data_direction     data_dir;
699         u32                     data_size;
700         u32                     data_size_from_cmnd;
701         u32                     tag;
702         unsigned int            lun;
703         u32                     residue;
704         u32                     usb_amount_left;
705
706         /* The CB protocol offers no way for a host to know when a command
707          * has completed.  As a result the next command may arrive early,
708          * and we will still have to handle it.  For that reason we need
709          * a buffer to store new commands when using CB (or CBI, which
710          * does not oblige a host to wait for command completion either). */
711         int                     cbbuf_cmnd_size;
712         u8                      cbbuf_cmnd[MAX_COMMAND_SIZE];
713
714         unsigned int            nluns;
715         struct lun              *luns;
716         struct lun              *curlun;
717 };
718
719 typedef void (*fsg_routine_t)(struct fsg_dev *);
720
721 static int exception_in_progress(struct fsg_dev *fsg)
722 {
723         return (fsg->state > FSG_STATE_IDLE);
724 }
725
726 /* Make bulk-out requests be divisible by the maxpacket size */
727 static void set_bulk_out_req_length(struct fsg_dev *fsg,
728                 struct fsg_buffhd *bh, unsigned int length)
729 {
730         unsigned int    rem;
731
732         bh->bulk_out_intended_length = length;
733         rem = length % fsg->bulk_out_maxpacket;
734         if (rem > 0)
735                 length += fsg->bulk_out_maxpacket - rem;
736         bh->outreq->length = length;
737 }
738
739 static struct fsg_dev                   *the_fsg;
740 static struct usb_gadget_driver         fsg_driver;
741
742 static void     close_backing_file(struct lun *curlun);
743
744
745 /*-------------------------------------------------------------------------*/
746
747 #ifdef DUMP_MSGS
748
749 static void dump_msg(struct fsg_dev *fsg, const char *label,
750                 const u8 *buf, unsigned int length)
751 {
752         if (length < 512) {
753                 DBG(fsg, "%s, length %u:\n", label, length);
754                 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
755                                 16, 1, buf, length, 0);
756         }
757 }
758
759 static void dump_cdb(struct fsg_dev *fsg)
760 {}
761
762 #else
763
764 static void dump_msg(struct fsg_dev *fsg, const char *label,
765                 const u8 *buf, unsigned int length)
766 {}
767
768 #ifdef VERBOSE_DEBUG
769
770 static void dump_cdb(struct fsg_dev *fsg)
771 {
772         print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE,
773                         16, 1, fsg->cmnd, fsg->cmnd_size, 0);
774 }
775
776 #else
777
778 static void dump_cdb(struct fsg_dev *fsg)
779 {}
780
781 #endif /* VERBOSE_DEBUG */
782 #endif /* DUMP_MSGS */
783
784
785 static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
786 {
787         const char      *name;
788
789         if (ep == fsg->bulk_in)
790                 name = "bulk-in";
791         else if (ep == fsg->bulk_out)
792                 name = "bulk-out";
793         else
794                 name = ep->name;
795         DBG(fsg, "%s set halt\n", name);
796         return usb_ep_set_halt(ep);
797 }
798
799
800 /*-------------------------------------------------------------------------*/
801
802 /* Routines for unaligned data access */
803
804 static u32 get_unaligned_be24(u8 *buf)
805 {
806         return 0xffffff & (u32) get_unaligned_be32(buf - 1);
807 }
808
809
810 /*-------------------------------------------------------------------------*/
811
812 /*
813  * DESCRIPTORS ... most are static, but strings and (full) configuration
814  * descriptors are built on demand.  Also the (static) config and interface
815  * descriptors are adjusted during fsg_bind().
816  */
817 #define STRING_MANUFACTURER     1
818 #define STRING_PRODUCT          2
819 #define STRING_SERIAL           3
820 #define STRING_CONFIG           4
821 #define STRING_INTERFACE        5
822
823 /* There is only one configuration. */
824 #define CONFIG_VALUE            1
825
826 static struct usb_device_descriptor
827 device_desc = {
828         .bLength =              sizeof device_desc,
829         .bDescriptorType =      USB_DT_DEVICE,
830
831         .bcdUSB =               cpu_to_le16(0x0200),
832         .bDeviceClass =         USB_CLASS_PER_INTERFACE,
833
834         /* The next three values can be overridden by module parameters */
835         .idVendor =             cpu_to_le16(DRIVER_VENDOR_ID),
836         .idProduct =            cpu_to_le16(DRIVER_PRODUCT_ID),
837         .bcdDevice =            cpu_to_le16(0xffff),
838
839         .iManufacturer =        STRING_MANUFACTURER,
840         .iProduct =             STRING_PRODUCT,
841         .iSerialNumber =        STRING_SERIAL,
842         .bNumConfigurations =   1,
843 };
844
845 static struct usb_config_descriptor
846 config_desc = {
847         .bLength =              sizeof config_desc,
848         .bDescriptorType =      USB_DT_CONFIG,
849
850         /* wTotalLength computed by usb_gadget_config_buf() */
851         .bNumInterfaces =       1,
852         .bConfigurationValue =  CONFIG_VALUE,
853         .iConfiguration =       STRING_CONFIG,
854         .bmAttributes =         USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
855         .bMaxPower =            CONFIG_USB_GADGET_VBUS_DRAW / 2,
856 };
857
858 static struct usb_otg_descriptor
859 otg_desc = {
860         .bLength =              sizeof(otg_desc),
861         .bDescriptorType =      USB_DT_OTG,
862
863         .bmAttributes =         USB_OTG_SRP,
864 };
865
866 /* There is only one interface. */
867
868 static struct usb_interface_descriptor
869 intf_desc = {
870         .bLength =              sizeof intf_desc,
871         .bDescriptorType =      USB_DT_INTERFACE,
872
873         .bNumEndpoints =        2,              // Adjusted during fsg_bind()
874         .bInterfaceClass =      USB_CLASS_MASS_STORAGE,
875         .bInterfaceSubClass =   USB_SC_SCSI,    // Adjusted during fsg_bind()
876         .bInterfaceProtocol =   USB_PR_BULK,    // Adjusted during fsg_bind()
877         .iInterface =           STRING_INTERFACE,
878 };
879
880 /* Three full-speed endpoint descriptors: bulk-in, bulk-out,
881  * and interrupt-in. */
882
883 static struct usb_endpoint_descriptor
884 fs_bulk_in_desc = {
885         .bLength =              USB_DT_ENDPOINT_SIZE,
886         .bDescriptorType =      USB_DT_ENDPOINT,
887
888         .bEndpointAddress =     USB_DIR_IN,
889         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
890         /* wMaxPacketSize set by autoconfiguration */
891 };
892
893 static struct usb_endpoint_descriptor
894 fs_bulk_out_desc = {
895         .bLength =              USB_DT_ENDPOINT_SIZE,
896         .bDescriptorType =      USB_DT_ENDPOINT,
897
898         .bEndpointAddress =     USB_DIR_OUT,
899         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
900         /* wMaxPacketSize set by autoconfiguration */
901 };
902
903 static struct usb_endpoint_descriptor
904 fs_intr_in_desc = {
905         .bLength =              USB_DT_ENDPOINT_SIZE,
906         .bDescriptorType =      USB_DT_ENDPOINT,
907
908         .bEndpointAddress =     USB_DIR_IN,
909         .bmAttributes =         USB_ENDPOINT_XFER_INT,
910         .wMaxPacketSize =       cpu_to_le16(2),
911         .bInterval =            32,     // frames -> 32 ms
912 };
913
914 static const struct usb_descriptor_header *fs_function[] = {
915         (struct usb_descriptor_header *) &otg_desc,
916         (struct usb_descriptor_header *) &intf_desc,
917         (struct usb_descriptor_header *) &fs_bulk_in_desc,
918         (struct usb_descriptor_header *) &fs_bulk_out_desc,
919         (struct usb_descriptor_header *) &fs_intr_in_desc,
920         NULL,
921 };
922 #define FS_FUNCTION_PRE_EP_ENTRIES      2
923
924
925 /*
926  * USB 2.0 devices need to expose both high speed and full speed
927  * descriptors, unless they only run at full speed.
928  *
929  * That means alternate endpoint descriptors (bigger packets)
930  * and a "device qualifier" ... plus more construction options
931  * for the config descriptor.
932  */
933 static struct usb_qualifier_descriptor
934 dev_qualifier = {
935         .bLength =              sizeof dev_qualifier,
936         .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
937
938         .bcdUSB =               cpu_to_le16(0x0200),
939         .bDeviceClass =         USB_CLASS_PER_INTERFACE,
940
941         .bNumConfigurations =   1,
942 };
943
944 static struct usb_endpoint_descriptor
945 hs_bulk_in_desc = {
946         .bLength =              USB_DT_ENDPOINT_SIZE,
947         .bDescriptorType =      USB_DT_ENDPOINT,
948
949         /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
950         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
951         .wMaxPacketSize =       cpu_to_le16(512),
952 };
953
954 static struct usb_endpoint_descriptor
955 hs_bulk_out_desc = {
956         .bLength =              USB_DT_ENDPOINT_SIZE,
957         .bDescriptorType =      USB_DT_ENDPOINT,
958
959         /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
960         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
961         .wMaxPacketSize =       cpu_to_le16(512),
962         .bInterval =            1,      // NAK every 1 uframe
963 };
964
965 static struct usb_endpoint_descriptor
966 hs_intr_in_desc = {
967         .bLength =              USB_DT_ENDPOINT_SIZE,
968         .bDescriptorType =      USB_DT_ENDPOINT,
969
970         /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
971         .bmAttributes =         USB_ENDPOINT_XFER_INT,
972         .wMaxPacketSize =       cpu_to_le16(2),
973         .bInterval =            9,      // 2**(9-1) = 256 uframes -> 32 ms
974 };
975
976 static const struct usb_descriptor_header *hs_function[] = {
977         (struct usb_descriptor_header *) &otg_desc,
978         (struct usb_descriptor_header *) &intf_desc,
979         (struct usb_descriptor_header *) &hs_bulk_in_desc,
980         (struct usb_descriptor_header *) &hs_bulk_out_desc,
981         (struct usb_descriptor_header *) &hs_intr_in_desc,
982         NULL,
983 };
984 #define HS_FUNCTION_PRE_EP_ENTRIES      2
985
986 /* Maxpacket and other transfer characteristics vary by speed. */
987 static struct usb_endpoint_descriptor *
988 ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
989                 struct usb_endpoint_descriptor *hs)
990 {
991         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
992                 return hs;
993         return fs;
994 }
995
996
997 /* The CBI specification limits the serial string to 12 uppercase hexadecimal
998  * characters. */
999 static char                             manufacturer[64];
1000 static char                             serial[13];
1001
1002 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
1003 static struct usb_string                strings[] = {
1004         {STRING_MANUFACTURER,   manufacturer},
1005         {STRING_PRODUCT,        longname},
1006         {STRING_SERIAL,         serial},
1007         {STRING_CONFIG,         "Self-powered"},
1008         {STRING_INTERFACE,      "Mass Storage"},
1009         {}
1010 };
1011
1012 static struct usb_gadget_strings        stringtab = {
1013         .language       = 0x0409,               // en-us
1014         .strings        = strings,
1015 };
1016
1017
1018 /*
1019  * Config descriptors must agree with the code that sets configurations
1020  * and with code managing interfaces and their altsettings.  They must
1021  * also handle different speeds and other-speed requests.
1022  */
1023 static int populate_config_buf(struct usb_gadget *gadget,
1024                 u8 *buf, u8 type, unsigned index)
1025 {
1026         enum usb_device_speed                   speed = gadget->speed;
1027         int                                     len;
1028         const struct usb_descriptor_header      **function;
1029
1030         if (index > 0)
1031                 return -EINVAL;
1032
1033         if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
1034                 speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
1035         if (gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH)
1036                 function = hs_function;
1037         else
1038                 function = fs_function;
1039
1040         /* for now, don't advertise srp-only devices */
1041         if (!gadget_is_otg(gadget))
1042                 function++;
1043
1044         len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
1045         ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1046         return len;
1047 }
1048
1049
1050 /*-------------------------------------------------------------------------*/
1051
1052 /* These routines may be called in process context or in_irq */
1053
1054 /* Caller must hold fsg->lock */
1055 static void wakeup_thread(struct fsg_dev *fsg)
1056 {
1057         /* Tell the main thread that something has happened */
1058         fsg->thread_wakeup_needed = 1;
1059         if (fsg->thread_task)
1060                 wake_up_process(fsg->thread_task);
1061 }
1062
1063
1064 static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
1065 {
1066         unsigned long           flags;
1067
1068         /* Do nothing if a higher-priority exception is already in progress.
1069          * If a lower-or-equal priority exception is in progress, preempt it
1070          * and notify the main thread by sending it a signal. */
1071         spin_lock_irqsave(&fsg->lock, flags);
1072         if (fsg->state <= new_state) {
1073                 fsg->exception_req_tag = fsg->ep0_req_tag;
1074                 fsg->state = new_state;
1075                 if (fsg->thread_task)
1076                         send_sig_info(SIGUSR1, SEND_SIG_FORCED,
1077                                         fsg->thread_task);
1078         }
1079         spin_unlock_irqrestore(&fsg->lock, flags);
1080 }
1081
1082
1083 /*-------------------------------------------------------------------------*/
1084
1085 /* The disconnect callback and ep0 routines.  These always run in_irq,
1086  * except that ep0_queue() is called in the main thread to acknowledge
1087  * completion of various requests: set config, set interface, and
1088  * Bulk-only device reset. */
1089
1090 static void fsg_disconnect(struct usb_gadget *gadget)
1091 {
1092         struct fsg_dev          *fsg = get_gadget_data(gadget);
1093
1094         DBG(fsg, "disconnect or port reset\n");
1095         raise_exception(fsg, FSG_STATE_DISCONNECT);
1096 }
1097
1098
1099 static int ep0_queue(struct fsg_dev *fsg)
1100 {
1101         int     rc;
1102
1103         rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
1104         if (rc != 0 && rc != -ESHUTDOWN) {
1105
1106                 /* We can't do much more than wait for a reset */
1107                 WARNING(fsg, "error in submission: %s --> %d\n",
1108                                 fsg->ep0->name, rc);
1109         }
1110         return rc;
1111 }
1112
1113 static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
1114 {
1115         struct fsg_dev          *fsg = ep->driver_data;
1116
1117         if (req->actual > 0)
1118                 dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
1119         if (req->status || req->actual != req->length)
1120                 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1121                                 req->status, req->actual, req->length);
1122         if (req->status == -ECONNRESET)         // Request was cancelled
1123                 usb_ep_fifo_flush(ep);
1124
1125         if (req->status == 0 && req->context)
1126                 ((fsg_routine_t) (req->context))(fsg);
1127 }
1128
1129
1130 /*-------------------------------------------------------------------------*/
1131
1132 /* Bulk and interrupt endpoint completion handlers.
1133  * These always run in_irq. */
1134
1135 static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
1136 {
1137         struct fsg_dev          *fsg = ep->driver_data;
1138         struct fsg_buffhd       *bh = req->context;
1139
1140         if (req->status || req->actual != req->length)
1141                 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1142                                 req->status, req->actual, req->length);
1143         if (req->status == -ECONNRESET)         // Request was cancelled
1144                 usb_ep_fifo_flush(ep);
1145
1146         /* Hold the lock while we update the request and buffer states */
1147         smp_wmb();
1148         spin_lock(&fsg->lock);
1149         bh->inreq_busy = 0;
1150         bh->state = BUF_STATE_EMPTY;
1151         wakeup_thread(fsg);
1152         spin_unlock(&fsg->lock);
1153 }
1154
1155 static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
1156 {
1157         struct fsg_dev          *fsg = ep->driver_data;
1158         struct fsg_buffhd       *bh = req->context;
1159
1160         dump_msg(fsg, "bulk-out", req->buf, req->actual);
1161         if (req->status || req->actual != bh->bulk_out_intended_length)
1162                 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1163                                 req->status, req->actual,
1164                                 bh->bulk_out_intended_length);
1165         if (req->status == -ECONNRESET)         // Request was cancelled
1166                 usb_ep_fifo_flush(ep);
1167
1168         /* Hold the lock while we update the request and buffer states */
1169         smp_wmb();
1170         spin_lock(&fsg->lock);
1171         bh->outreq_busy = 0;
1172         bh->state = BUF_STATE_FULL;
1173         wakeup_thread(fsg);
1174         spin_unlock(&fsg->lock);
1175 }
1176
1177
1178 #ifdef CONFIG_USB_FILE_STORAGE_TEST
1179 static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
1180 {
1181         struct fsg_dev          *fsg = ep->driver_data;
1182         struct fsg_buffhd       *bh = req->context;
1183
1184         if (req->status || req->actual != req->length)
1185                 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
1186                                 req->status, req->actual, req->length);
1187         if (req->status == -ECONNRESET)         // Request was cancelled
1188                 usb_ep_fifo_flush(ep);
1189
1190         /* Hold the lock while we update the request and buffer states */
1191         smp_wmb();
1192         spin_lock(&fsg->lock);
1193         fsg->intreq_busy = 0;
1194         bh->state = BUF_STATE_EMPTY;
1195         wakeup_thread(fsg);
1196         spin_unlock(&fsg->lock);
1197 }
1198
1199 #else
1200 static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
1201 {}
1202 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
1203
1204
1205 /*-------------------------------------------------------------------------*/
1206
1207 /* Ep0 class-specific handlers.  These always run in_irq. */
1208
1209 #ifdef CONFIG_USB_FILE_STORAGE_TEST
1210 static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1211 {
1212         struct usb_request      *req = fsg->ep0req;
1213         static u8               cbi_reset_cmnd[6] = {
1214                         SC_SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
1215
1216         /* Error in command transfer? */
1217         if (req->status || req->length != req->actual ||
1218                         req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
1219
1220                 /* Not all controllers allow a protocol stall after
1221                  * receiving control-out data, but we'll try anyway. */
1222                 fsg_set_halt(fsg, fsg->ep0);
1223                 return;                 // Wait for reset
1224         }
1225
1226         /* Is it the special reset command? */
1227         if (req->actual >= sizeof cbi_reset_cmnd &&
1228                         memcmp(req->buf, cbi_reset_cmnd,
1229                                 sizeof cbi_reset_cmnd) == 0) {
1230
1231                 /* Raise an exception to stop the current operation
1232                  * and reinitialize our state. */
1233                 DBG(fsg, "cbi reset request\n");
1234                 raise_exception(fsg, FSG_STATE_RESET);
1235                 return;
1236         }
1237
1238         VDBG(fsg, "CB[I] accept device-specific command\n");
1239         spin_lock(&fsg->lock);
1240
1241         /* Save the command for later */
1242         if (fsg->cbbuf_cmnd_size)
1243                 WARNING(fsg, "CB[I] overwriting previous command\n");
1244         fsg->cbbuf_cmnd_size = req->actual;
1245         memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
1246
1247         wakeup_thread(fsg);
1248         spin_unlock(&fsg->lock);
1249 }
1250
1251 #else
1252 static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1253 {}
1254 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
1255
1256
1257 static int class_setup_req(struct fsg_dev *fsg,
1258                 const struct usb_ctrlrequest *ctrl)
1259 {
1260         struct usb_request      *req = fsg->ep0req;
1261         int                     value = -EOPNOTSUPP;
1262         u16                     w_index = le16_to_cpu(ctrl->wIndex);
1263         u16                     w_value = le16_to_cpu(ctrl->wValue);
1264         u16                     w_length = le16_to_cpu(ctrl->wLength);
1265
1266         if (!fsg->config)
1267                 return value;
1268
1269         /* Handle Bulk-only class-specific requests */
1270         if (transport_is_bbb()) {
1271                 switch (ctrl->bRequest) {
1272
1273                 case USB_BULK_RESET_REQUEST:
1274                         if (ctrl->bRequestType != (USB_DIR_OUT |
1275                                         USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1276                                 break;
1277                         if (w_index != 0 || w_value != 0) {
1278                                 value = -EDOM;
1279                                 break;
1280                         }
1281
1282                         /* Raise an exception to stop the current operation
1283                          * and reinitialize our state. */
1284                         DBG(fsg, "bulk reset request\n");
1285                         raise_exception(fsg, FSG_STATE_RESET);
1286                         value = DELAYED_STATUS;
1287                         break;
1288
1289                 case USB_BULK_GET_MAX_LUN_REQUEST:
1290                         if (ctrl->bRequestType != (USB_DIR_IN |
1291                                         USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1292                                 break;
1293                         if (w_index != 0 || w_value != 0) {
1294                                 value = -EDOM;
1295                                 break;
1296                         }
1297                         VDBG(fsg, "get max LUN\n");
1298                         *(u8 *) req->buf = fsg->nluns - 1;
1299                         value = 1;
1300                         break;
1301                 }
1302         }
1303
1304         /* Handle CBI class-specific requests */
1305         else {
1306                 switch (ctrl->bRequest) {
1307
1308                 case USB_CBI_ADSC_REQUEST:
1309                         if (ctrl->bRequestType != (USB_DIR_OUT |
1310                                         USB_TYPE_CLASS | USB_RECIP_INTERFACE))
1311                                 break;
1312                         if (w_index != 0 || w_value != 0) {
1313                                 value = -EDOM;
1314                                 break;
1315                         }
1316                         if (w_length > MAX_COMMAND_SIZE) {
1317                                 value = -EOVERFLOW;
1318                                 break;
1319                         }
1320                         value = w_length;
1321                         fsg->ep0req->context = received_cbi_adsc;
1322                         break;
1323                 }
1324         }
1325
1326         if (value == -EOPNOTSUPP)
1327                 VDBG(fsg,
1328                         "unknown class-specific control req "
1329                         "%02x.%02x v%04x i%04x l%u\n",
1330                         ctrl->bRequestType, ctrl->bRequest,
1331                         le16_to_cpu(ctrl->wValue), w_index, w_length);
1332         return value;
1333 }
1334
1335
1336 /*-------------------------------------------------------------------------*/
1337
1338 /* Ep0 standard request handlers.  These always run in_irq. */
1339
1340 static int standard_setup_req(struct fsg_dev *fsg,
1341                 const struct usb_ctrlrequest *ctrl)
1342 {
1343         struct usb_request      *req = fsg->ep0req;
1344         int                     value = -EOPNOTSUPP;
1345         u16                     w_index = le16_to_cpu(ctrl->wIndex);
1346         u16                     w_value = le16_to_cpu(ctrl->wValue);
1347
1348         /* Usually this just stores reply data in the pre-allocated ep0 buffer,
1349          * but config change events will also reconfigure hardware. */
1350         switch (ctrl->bRequest) {
1351
1352         case USB_REQ_GET_DESCRIPTOR:
1353                 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1354                                 USB_RECIP_DEVICE))
1355                         break;
1356                 switch (w_value >> 8) {
1357
1358                 case USB_DT_DEVICE:
1359                         VDBG(fsg, "get device descriptor\n");
1360                         value = sizeof device_desc;
1361                         memcpy(req->buf, &device_desc, value);
1362                         break;
1363                 case USB_DT_DEVICE_QUALIFIER:
1364                         VDBG(fsg, "get device qualifier\n");
1365                         if (!gadget_is_dualspeed(fsg->gadget))
1366                                 break;
1367                         value = sizeof dev_qualifier;
1368                         memcpy(req->buf, &dev_qualifier, value);
1369                         break;
1370
1371                 case USB_DT_OTHER_SPEED_CONFIG:
1372                         VDBG(fsg, "get other-speed config descriptor\n");
1373                         if (!gadget_is_dualspeed(fsg->gadget))
1374                                 break;
1375                         goto get_config;
1376                 case USB_DT_CONFIG:
1377                         VDBG(fsg, "get configuration descriptor\n");
1378 get_config:
1379                         value = populate_config_buf(fsg->gadget,
1380                                         req->buf,
1381                                         w_value >> 8,
1382                                         w_value & 0xff);
1383                         break;
1384
1385                 case USB_DT_STRING:
1386                         VDBG(fsg, "get string descriptor\n");
1387
1388                         /* wIndex == language code */
1389                         value = usb_gadget_get_string(&stringtab,
1390                                         w_value & 0xff, req->buf);
1391                         break;
1392                 }
1393                 break;
1394
1395         /* One config, two speeds */
1396         case USB_REQ_SET_CONFIGURATION:
1397                 if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
1398                                 USB_RECIP_DEVICE))
1399                         break;
1400                 VDBG(fsg, "set configuration\n");
1401                 if (w_value == CONFIG_VALUE || w_value == 0) {
1402                         fsg->new_config = w_value;
1403
1404                         /* Raise an exception to wipe out previous transaction
1405                          * state (queued bufs, etc) and set the new config. */
1406                         raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
1407                         value = DELAYED_STATUS;
1408                 }
1409                 break;
1410         case USB_REQ_GET_CONFIGURATION:
1411                 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1412                                 USB_RECIP_DEVICE))
1413                         break;
1414                 VDBG(fsg, "get configuration\n");
1415                 *(u8 *) req->buf = fsg->config;
1416                 value = 1;
1417                 break;
1418
1419         case USB_REQ_SET_INTERFACE:
1420                 if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
1421                                 USB_RECIP_INTERFACE))
1422                         break;
1423                 if (fsg->config && w_index == 0) {
1424
1425                         /* Raise an exception to wipe out previous transaction
1426                          * state (queued bufs, etc) and install the new
1427                          * interface altsetting. */
1428                         raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
1429                         value = DELAYED_STATUS;
1430                 }
1431                 break;
1432         case USB_REQ_GET_INTERFACE:
1433                 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1434                                 USB_RECIP_INTERFACE))
1435                         break;
1436                 if (!fsg->config)
1437                         break;
1438                 if (w_index != 0) {
1439                         value = -EDOM;
1440                         break;
1441                 }
1442                 VDBG(fsg, "get interface\n");
1443                 *(u8 *) req->buf = 0;
1444                 value = 1;
1445                 break;
1446
1447         default:
1448                 VDBG(fsg,
1449                         "unknown control req %02x.%02x v%04x i%04x l%u\n",
1450                         ctrl->bRequestType, ctrl->bRequest,
1451                         w_value, w_index, le16_to_cpu(ctrl->wLength));
1452         }
1453
1454         return value;
1455 }
1456
1457
1458 static int fsg_setup(struct usb_gadget *gadget,
1459                 const struct usb_ctrlrequest *ctrl)
1460 {
1461         struct fsg_dev          *fsg = get_gadget_data(gadget);
1462         int                     rc;
1463         int                     w_length = le16_to_cpu(ctrl->wLength);
1464
1465         ++fsg->ep0_req_tag;             // Record arrival of a new request
1466         fsg->ep0req->context = NULL;
1467         fsg->ep0req->length = 0;
1468         dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
1469
1470         if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
1471                 rc = class_setup_req(fsg, ctrl);
1472         else
1473                 rc = standard_setup_req(fsg, ctrl);
1474
1475         /* Respond with data/status or defer until later? */
1476         if (rc >= 0 && rc != DELAYED_STATUS) {
1477                 rc = min(rc, w_length);
1478                 fsg->ep0req->length = rc;
1479                 fsg->ep0req->zero = rc < w_length;
1480                 fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
1481                                 "ep0-in" : "ep0-out");
1482                 rc = ep0_queue(fsg);
1483         }
1484
1485         /* Device either stalls (rc < 0) or reports success */
1486         return rc;
1487 }
1488
1489
1490 /*-------------------------------------------------------------------------*/
1491
1492 /* All the following routines run in process context */
1493
1494
1495 /* Use this for bulk or interrupt transfers, not ep0 */
1496 static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
1497                 struct usb_request *req, int *pbusy,
1498                 enum fsg_buffer_state *state)
1499 {
1500         int     rc;
1501
1502         if (ep == fsg->bulk_in)
1503                 dump_msg(fsg, "bulk-in", req->buf, req->length);
1504         else if (ep == fsg->intr_in)
1505                 dump_msg(fsg, "intr-in", req->buf, req->length);
1506
1507         spin_lock_irq(&fsg->lock);
1508         *pbusy = 1;
1509         *state = BUF_STATE_BUSY;
1510         spin_unlock_irq(&fsg->lock);
1511         rc = usb_ep_queue(ep, req, GFP_KERNEL);
1512         if (rc != 0) {
1513                 *pbusy = 0;
1514                 *state = BUF_STATE_EMPTY;
1515
1516                 /* We can't do much more than wait for a reset */
1517
1518                 /* Note: currently the net2280 driver fails zero-length
1519                  * submissions if DMA is enabled. */
1520                 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
1521                                                 req->length == 0))
1522                         WARNING(fsg, "error in submission: %s --> %d\n",
1523                                         ep->name, rc);
1524         }
1525 }
1526
1527
1528 static int sleep_thread(struct fsg_dev *fsg)
1529 {
1530         int     rc = 0;
1531
1532         /* Wait until a signal arrives or we are woken up */
1533         for (;;) {
1534                 try_to_freeze();
1535                 set_current_state(TASK_INTERRUPTIBLE);
1536                 if (signal_pending(current)) {
1537                         rc = -EINTR;
1538                         break;
1539                 }
1540                 if (fsg->thread_wakeup_needed)
1541                         break;
1542                 schedule();
1543         }
1544         __set_current_state(TASK_RUNNING);
1545         fsg->thread_wakeup_needed = 0;
1546         return rc;
1547 }
1548
1549
1550 /*-------------------------------------------------------------------------*/
1551
1552 static int do_read(struct fsg_dev *fsg)
1553 {
1554         struct lun              *curlun = fsg->curlun;
1555         u32                     lba;
1556         struct fsg_buffhd       *bh;
1557         int                     rc;
1558         u32                     amount_left;
1559         loff_t                  file_offset, file_offset_tmp;
1560         unsigned int            amount;
1561         unsigned int            partial_page;
1562         ssize_t                 nread;
1563
1564         /* Get the starting Logical Block Address and check that it's
1565          * not too big */
1566         if (fsg->cmnd[0] == SC_READ_6)
1567                 lba = get_unaligned_be24(&fsg->cmnd[1]);
1568         else {
1569                 lba = get_unaligned_be32(&fsg->cmnd[2]);
1570
1571                 /* We allow DPO (Disable Page Out = don't save data in the
1572                  * cache) and FUA (Force Unit Access = don't read from the
1573                  * cache), but we don't implement them. */
1574                 if ((fsg->cmnd[1] & ~0x18) != 0) {
1575                         curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1576                         return -EINVAL;
1577                 }
1578         }
1579         if (lba >= curlun->num_sectors) {
1580                 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1581                 return -EINVAL;
1582         }
1583         file_offset = ((loff_t) lba) << 9;
1584
1585         /* Carry out the file reads */
1586         amount_left = fsg->data_size_from_cmnd;
1587         if (unlikely(amount_left == 0))
1588                 return -EIO;            // No default reply
1589
1590         for (;;) {
1591
1592                 /* Figure out how much we need to read:
1593                  * Try to read the remaining amount.
1594                  * But don't read more than the buffer size.
1595                  * And don't try to read past the end of the file.
1596                  * Finally, if we're not at a page boundary, don't read past
1597                  *      the next page.
1598                  * If this means reading 0 then we were asked to read past
1599                  *      the end of file. */
1600                 amount = min((unsigned int) amount_left, mod_data.buflen);
1601                 amount = min((loff_t) amount,
1602                                 curlun->file_length - file_offset);
1603                 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
1604                 if (partial_page > 0)
1605                         amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
1606                                         partial_page);
1607
1608                 /* Wait for the next buffer to become available */
1609                 bh = fsg->next_buffhd_to_fill;
1610                 while (bh->state != BUF_STATE_EMPTY) {
1611                         rc = sleep_thread(fsg);
1612                         if (rc)
1613                                 return rc;
1614                 }
1615
1616                 /* If we were asked to read past the end of file,
1617                  * end with an empty buffer. */
1618                 if (amount == 0) {
1619                         curlun->sense_data =
1620                                         SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1621                         curlun->sense_data_info = file_offset >> 9;
1622                         curlun->info_valid = 1;
1623                         bh->inreq->length = 0;
1624                         bh->state = BUF_STATE_FULL;
1625                         break;
1626                 }
1627
1628                 /* Perform the read */
1629                 file_offset_tmp = file_offset;
1630                 nread = vfs_read(curlun->filp,
1631                                 (char __user *) bh->buf,
1632                                 amount, &file_offset_tmp);
1633                 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1634                                 (unsigned long long) file_offset,
1635                                 (int) nread);
1636                 if (signal_pending(current))
1637                         return -EINTR;
1638
1639                 if (nread < 0) {
1640                         LDBG(curlun, "error in file read: %d\n",
1641                                         (int) nread);
1642                         nread = 0;
1643                 } else if (nread < amount) {
1644                         LDBG(curlun, "partial file read: %d/%u\n",
1645                                         (int) nread, amount);
1646                         nread -= (nread & 511); // Round down to a block
1647                 }
1648                 file_offset  += nread;
1649                 amount_left  -= nread;
1650                 fsg->residue -= nread;
1651                 bh->inreq->length = nread;
1652                 bh->state = BUF_STATE_FULL;
1653
1654                 /* If an error occurred, report it and its position */
1655                 if (nread < amount) {
1656                         curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1657                         curlun->sense_data_info = file_offset >> 9;
1658                         curlun->info_valid = 1;
1659                         break;
1660                 }
1661
1662                 if (amount_left == 0)
1663                         break;          // No more left to read
1664
1665                 /* Send this buffer and go read some more */
1666                 bh->inreq->zero = 0;
1667                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1668                                 &bh->inreq_busy, &bh->state);
1669                 fsg->next_buffhd_to_fill = bh->next;
1670         }
1671
1672         return -EIO;            // No default reply
1673 }
1674
1675
1676 /*-------------------------------------------------------------------------*/
1677
1678 static int do_write(struct fsg_dev *fsg)
1679 {
1680         struct lun              *curlun = fsg->curlun;
1681         u32                     lba;
1682         struct fsg_buffhd       *bh;
1683         int                     get_some_more;
1684         u32                     amount_left_to_req, amount_left_to_write;
1685         loff_t                  usb_offset, file_offset, file_offset_tmp;
1686         unsigned int            amount;
1687         unsigned int            partial_page;
1688         ssize_t                 nwritten;
1689         int                     rc;
1690
1691         if (curlun->ro) {
1692                 curlun->sense_data = SS_WRITE_PROTECTED;
1693                 return -EINVAL;
1694         }
1695         spin_lock(&curlun->filp->f_lock);
1696         curlun->filp->f_flags &= ~O_SYNC;       // Default is not to wait
1697         spin_unlock(&curlun->filp->f_lock);
1698
1699         /* Get the starting Logical Block Address and check that it's
1700          * not too big */
1701         if (fsg->cmnd[0] == SC_WRITE_6)
1702                 lba = get_unaligned_be24(&fsg->cmnd[1]);
1703         else {
1704                 lba = get_unaligned_be32(&fsg->cmnd[2]);
1705
1706                 /* We allow DPO (Disable Page Out = don't save data in the
1707                  * cache) and FUA (Force Unit Access = write directly to the
1708                  * medium).  We don't implement DPO; we implement FUA by
1709                  * performing synchronous output. */
1710                 if ((fsg->cmnd[1] & ~0x18) != 0) {
1711                         curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1712                         return -EINVAL;
1713                 }
1714                 if (fsg->cmnd[1] & 0x08) {      // FUA
1715                         spin_lock(&curlun->filp->f_lock);
1716                         curlun->filp->f_flags |= O_SYNC;
1717                         spin_unlock(&curlun->filp->f_lock);
1718                 }
1719         }
1720         if (lba >= curlun->num_sectors) {
1721                 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1722                 return -EINVAL;
1723         }
1724
1725         /* Carry out the file writes */
1726         get_some_more = 1;
1727         file_offset = usb_offset = ((loff_t) lba) << 9;
1728         amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
1729
1730         while (amount_left_to_write > 0) {
1731
1732                 /* Queue a request for more data from the host */
1733                 bh = fsg->next_buffhd_to_fill;
1734                 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
1735
1736                         /* Figure out how much we want to get:
1737                          * Try to get the remaining amount.
1738                          * But don't get more than the buffer size.
1739                          * And don't try to go past the end of the file.
1740                          * If we're not at a page boundary,
1741                          *      don't go past the next page.
1742                          * If this means getting 0, then we were asked
1743                          *      to write past the end of file.
1744                          * Finally, round down to a block boundary. */
1745                         amount = min(amount_left_to_req, mod_data.buflen);
1746                         amount = min((loff_t) amount, curlun->file_length -
1747                                         usb_offset);
1748                         partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
1749                         if (partial_page > 0)
1750                                 amount = min(amount,
1751         (unsigned int) PAGE_CACHE_SIZE - partial_page);
1752
1753                         if (amount == 0) {
1754                                 get_some_more = 0;
1755                                 curlun->sense_data =
1756                                         SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1757                                 curlun->sense_data_info = usb_offset >> 9;
1758                                 curlun->info_valid = 1;
1759                                 continue;
1760                         }
1761                         amount -= (amount & 511);
1762                         if (amount == 0) {
1763
1764                                 /* Why were we were asked to transfer a
1765                                  * partial block? */
1766                                 get_some_more = 0;
1767                                 continue;
1768                         }
1769
1770                         /* Get the next buffer */
1771                         usb_offset += amount;
1772                         fsg->usb_amount_left -= amount;
1773                         amount_left_to_req -= amount;
1774                         if (amount_left_to_req == 0)
1775                                 get_some_more = 0;
1776
1777                         /* amount is always divisible by 512, hence by
1778                          * the bulk-out maxpacket size */
1779                         bh->outreq->length = bh->bulk_out_intended_length =
1780                                         amount;
1781                         bh->outreq->short_not_ok = 1;
1782                         start_transfer(fsg, fsg->bulk_out, bh->outreq,
1783                                         &bh->outreq_busy, &bh->state);
1784                         fsg->next_buffhd_to_fill = bh->next;
1785                         continue;
1786                 }
1787
1788                 /* Write the received data to the backing file */
1789                 bh = fsg->next_buffhd_to_drain;
1790                 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
1791                         break;                  // We stopped early
1792                 if (bh->state == BUF_STATE_FULL) {
1793                         smp_rmb();
1794                         fsg->next_buffhd_to_drain = bh->next;
1795                         bh->state = BUF_STATE_EMPTY;
1796
1797                         /* Did something go wrong with the transfer? */
1798                         if (bh->outreq->status != 0) {
1799                                 curlun->sense_data = SS_COMMUNICATION_FAILURE;
1800                                 curlun->sense_data_info = file_offset >> 9;
1801                                 curlun->info_valid = 1;
1802                                 break;
1803                         }
1804
1805                         amount = bh->outreq->actual;
1806                         if (curlun->file_length - file_offset < amount) {
1807                                 LERROR(curlun,
1808         "write %u @ %llu beyond end %llu\n",
1809         amount, (unsigned long long) file_offset,
1810         (unsigned long long) curlun->file_length);
1811                                 amount = curlun->file_length - file_offset;
1812                         }
1813
1814                         /* Perform the write */
1815                         file_offset_tmp = file_offset;
1816                         nwritten = vfs_write(curlun->filp,
1817                                         (char __user *) bh->buf,
1818                                         amount, &file_offset_tmp);
1819                         VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1820                                         (unsigned long long) file_offset,
1821                                         (int) nwritten);
1822                         if (signal_pending(current))
1823                                 return -EINTR;          // Interrupted!
1824
1825                         if (nwritten < 0) {
1826                                 LDBG(curlun, "error in file write: %d\n",
1827                                                 (int) nwritten);
1828                                 nwritten = 0;
1829                         } else if (nwritten < amount) {
1830                                 LDBG(curlun, "partial file write: %d/%u\n",
1831                                                 (int) nwritten, amount);
1832                                 nwritten -= (nwritten & 511);
1833                                                 // Round down to a block
1834                         }
1835                         file_offset += nwritten;
1836                         amount_left_to_write -= nwritten;
1837                         fsg->residue -= nwritten;
1838
1839                         /* If an error occurred, report it and its position */
1840                         if (nwritten < amount) {
1841                                 curlun->sense_data = SS_WRITE_ERROR;
1842                                 curlun->sense_data_info = file_offset >> 9;
1843                                 curlun->info_valid = 1;
1844                                 break;
1845                         }
1846
1847                         /* Did the host decide to stop early? */
1848                         if (bh->outreq->actual != bh->outreq->length) {
1849                                 fsg->short_packet_received = 1;
1850                                 break;
1851                         }
1852                         continue;
1853                 }
1854
1855                 /* Wait for something to happen */
1856                 rc = sleep_thread(fsg);
1857                 if (rc)
1858                         return rc;
1859         }
1860
1861         return -EIO;            // No default reply
1862 }
1863
1864
1865 /*-------------------------------------------------------------------------*/
1866
1867 /* Sync the file data, don't bother with the metadata.
1868  * This code was copied from fs/buffer.c:sys_fdatasync(). */
1869 static int fsync_sub(struct lun *curlun)
1870 {
1871         struct file     *filp = curlun->filp;
1872
1873         if (curlun->ro || !filp)
1874                 return 0;
1875         return vfs_fsync(filp, filp->f_path.dentry, 1);
1876 }
1877
1878 static void fsync_all(struct fsg_dev *fsg)
1879 {
1880         int     i;
1881
1882         for (i = 0; i < fsg->nluns; ++i)
1883                 fsync_sub(&fsg->luns[i]);
1884 }
1885
1886 static int do_synchronize_cache(struct fsg_dev *fsg)
1887 {
1888         struct lun      *curlun = fsg->curlun;
1889         int             rc;
1890
1891         /* We ignore the requested LBA and write out all file's
1892          * dirty data buffers. */
1893         rc = fsync_sub(curlun);
1894         if (rc)
1895                 curlun->sense_data = SS_WRITE_ERROR;
1896         return 0;
1897 }
1898
1899
1900 /*-------------------------------------------------------------------------*/
1901
1902 static void invalidate_sub(struct lun *curlun)
1903 {
1904         struct file     *filp = curlun->filp;
1905         struct inode    *inode = filp->f_path.dentry->d_inode;
1906         unsigned long   rc;
1907
1908         rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
1909         VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc);
1910 }
1911
1912 static int do_verify(struct fsg_dev *fsg)
1913 {
1914         struct lun              *curlun = fsg->curlun;
1915         u32                     lba;
1916         u32                     verification_length;
1917         struct fsg_buffhd       *bh = fsg->next_buffhd_to_fill;
1918         loff_t                  file_offset, file_offset_tmp;
1919         u32                     amount_left;
1920         unsigned int            amount;
1921         ssize_t                 nread;
1922
1923         /* Get the starting Logical Block Address and check that it's
1924          * not too big */
1925         lba = get_unaligned_be32(&fsg->cmnd[2]);
1926         if (lba >= curlun->num_sectors) {
1927                 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1928                 return -EINVAL;
1929         }
1930
1931         /* We allow DPO (Disable Page Out = don't save data in the
1932          * cache) but we don't implement it. */
1933         if ((fsg->cmnd[1] & ~0x10) != 0) {
1934                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1935                 return -EINVAL;
1936         }
1937
1938         verification_length = get_unaligned_be16(&fsg->cmnd[7]);
1939         if (unlikely(verification_length == 0))
1940                 return -EIO;            // No default reply
1941
1942         /* Prepare to carry out the file verify */
1943         amount_left = verification_length << 9;
1944         file_offset = ((loff_t) lba) << 9;
1945
1946         /* Write out all the dirty buffers before invalidating them */
1947         fsync_sub(curlun);
1948         if (signal_pending(current))
1949                 return -EINTR;
1950
1951         invalidate_sub(curlun);
1952         if (signal_pending(current))
1953                 return -EINTR;
1954
1955         /* Just try to read the requested blocks */
1956         while (amount_left > 0) {
1957
1958                 /* Figure out how much we need to read:
1959                  * Try to read the remaining amount, but not more than
1960                  * the buffer size.
1961                  * And don't try to read past the end of the file.
1962                  * If this means reading 0 then we were asked to read
1963                  * past the end of file. */
1964                 amount = min((unsigned int) amount_left, mod_data.buflen);
1965                 amount = min((loff_t) amount,
1966                                 curlun->file_length - file_offset);
1967                 if (amount == 0) {
1968                         curlun->sense_data =
1969                                         SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1970                         curlun->sense_data_info = file_offset >> 9;
1971                         curlun->info_valid = 1;
1972                         break;
1973                 }
1974
1975                 /* Perform the read */
1976                 file_offset_tmp = file_offset;
1977                 nread = vfs_read(curlun->filp,
1978                                 (char __user *) bh->buf,
1979                                 amount, &file_offset_tmp);
1980                 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1981                                 (unsigned long long) file_offset,
1982                                 (int) nread);
1983                 if (signal_pending(current))
1984                         return -EINTR;
1985
1986                 if (nread < 0) {
1987                         LDBG(curlun, "error in file verify: %d\n",
1988                                         (int) nread);
1989                         nread = 0;
1990                 } else if (nread < amount) {
1991                         LDBG(curlun, "partial file verify: %d/%u\n",
1992                                         (int) nread, amount);
1993                         nread -= (nread & 511); // Round down to a sector
1994                 }
1995                 if (nread == 0) {
1996                         curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1997                         curlun->sense_data_info = file_offset >> 9;
1998                         curlun->info_valid = 1;
1999                         break;
2000                 }
2001                 file_offset += nread;
2002                 amount_left -= nread;
2003         }
2004         return 0;
2005 }
2006
2007
2008 /*-------------------------------------------------------------------------*/
2009
2010 static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2011 {
2012         u8      *buf = (u8 *) bh->buf;
2013
2014         static char vendor_id[] = "Linux   ";
2015         static char product_disk_id[] = "File-Stor Gadget";
2016         static char product_cdrom_id[] = "File-CD Gadget  ";
2017
2018         if (!fsg->curlun) {             // Unsupported LUNs are okay
2019                 fsg->bad_lun_okay = 1;
2020                 memset(buf, 0, 36);
2021                 buf[0] = 0x7f;          // Unsupported, no device-type
2022                 buf[4] = 31;            // Additional length
2023                 return 36;
2024         }
2025
2026         memset(buf, 0, 8);
2027         buf[0] = (mod_data.cdrom ? TYPE_CDROM : TYPE_DISK);
2028         if (mod_data.removable)
2029                 buf[1] = 0x80;
2030         buf[2] = 2;             // ANSI SCSI level 2
2031         buf[3] = 2;             // SCSI-2 INQUIRY data format
2032         buf[4] = 31;            // Additional length
2033                                 // No special options
2034         sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
2035                         (mod_data.cdrom ? product_cdrom_id :
2036                                 product_disk_id),
2037                         mod_data.release);
2038         return 36;
2039 }
2040
2041
2042 static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2043 {
2044         struct lun      *curlun = fsg->curlun;
2045         u8              *buf = (u8 *) bh->buf;
2046         u32             sd, sdinfo;
2047         int             valid;
2048
2049         /*
2050          * From the SCSI-2 spec., section 7.9 (Unit attention condition):
2051          *
2052          * If a REQUEST SENSE command is received from an initiator
2053          * with a pending unit attention condition (before the target
2054          * generates the contingent allegiance condition), then the
2055          * target shall either:
2056          *   a) report any pending sense data and preserve the unit
2057          *      attention condition on the logical unit, or,
2058          *   b) report the unit attention condition, may discard any
2059          *      pending sense data, and clear the unit attention
2060          *      condition on the logical unit for that initiator.
2061          *
2062          * FSG normally uses option a); enable this code to use option b).
2063          */
2064 #if 0
2065         if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
2066                 curlun->sense_data = curlun->unit_attention_data;
2067                 curlun->unit_attention_data = SS_NO_SENSE;
2068         }
2069 #endif
2070
2071         if (!curlun) {          // Unsupported LUNs are okay
2072                 fsg->bad_lun_okay = 1;
2073                 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2074                 sdinfo = 0;
2075                 valid = 0;
2076         } else {
2077                 sd = curlun->sense_data;
2078                 sdinfo = curlun->sense_data_info;
2079                 valid = curlun->info_valid << 7;
2080                 curlun->sense_data = SS_NO_SENSE;
2081                 curlun->sense_data_info = 0;
2082                 curlun->info_valid = 0;
2083         }
2084
2085         memset(buf, 0, 18);
2086         buf[0] = valid | 0x70;                  // Valid, current error
2087         buf[2] = SK(sd);
2088         put_unaligned_be32(sdinfo, &buf[3]);    /* Sense information */
2089         buf[7] = 18 - 8;                        // Additional sense length
2090         buf[12] = ASC(sd);
2091         buf[13] = ASCQ(sd);
2092         return 18;
2093 }
2094
2095
2096 static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2097 {
2098         struct lun      *curlun = fsg->curlun;
2099         u32             lba = get_unaligned_be32(&fsg->cmnd[2]);
2100         int             pmi = fsg->cmnd[8];
2101         u8              *buf = (u8 *) bh->buf;
2102
2103         /* Check the PMI and LBA fields */
2104         if (pmi > 1 || (pmi == 0 && lba != 0)) {
2105                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2106                 return -EINVAL;
2107         }
2108
2109         put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
2110                                                 /* Max logical block */
2111         put_unaligned_be32(512, &buf[4]);       /* Block length */
2112         return 8;
2113 }
2114
2115
2116 static void store_cdrom_address(u8 *dest, int msf, u32 addr)
2117 {
2118         if (msf) {
2119                 /* Convert to Minutes-Seconds-Frames */
2120                 addr >>= 2;             /* Convert to 2048-byte frames */
2121                 addr += 2*75;           /* Lead-in occupies 2 seconds */
2122                 dest[3] = addr % 75;    /* Frames */
2123                 addr /= 75;
2124                 dest[2] = addr % 60;    /* Seconds */
2125                 addr /= 60;
2126                 dest[1] = addr;         /* Minutes */
2127                 dest[0] = 0;            /* Reserved */
2128         } else {
2129                 /* Absolute sector */
2130                 put_unaligned_be32(addr, dest);
2131         }
2132 }
2133
2134 static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2135 {
2136         struct lun      *curlun = fsg->curlun;
2137         int             msf = fsg->cmnd[1] & 0x02;
2138         u32             lba = get_unaligned_be32(&fsg->cmnd[2]);
2139         u8              *buf = (u8 *) bh->buf;
2140
2141         if ((fsg->cmnd[1] & ~0x02) != 0) {              /* Mask away MSF */
2142                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2143                 return -EINVAL;
2144         }
2145         if (lba >= curlun->num_sectors) {
2146                 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
2147                 return -EINVAL;
2148         }
2149
2150         memset(buf, 0, 8);
2151         buf[0] = 0x01;          /* 2048 bytes of user data, rest is EC */
2152         store_cdrom_address(&buf[4], msf, lba);
2153         return 8;
2154 }
2155
2156
2157 static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2158 {
2159         struct lun      *curlun = fsg->curlun;
2160         int             msf = fsg->cmnd[1] & 0x02;
2161         int             start_track = fsg->cmnd[6];
2162         u8              *buf = (u8 *) bh->buf;
2163
2164         if ((fsg->cmnd[1] & ~0x02) != 0 ||              /* Mask away MSF */
2165                         start_track > 1) {
2166                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2167                 return -EINVAL;
2168         }
2169
2170         memset(buf, 0, 20);
2171         buf[1] = (20-2);                /* TOC data length */
2172         buf[2] = 1;                     /* First track number */
2173         buf[3] = 1;                     /* Last track number */
2174         buf[5] = 0x16;                  /* Data track, copying allowed */
2175         buf[6] = 0x01;                  /* Only track is number 1 */
2176         store_cdrom_address(&buf[8], msf, 0);
2177
2178         buf[13] = 0x16;                 /* Lead-out track is data */
2179         buf[14] = 0xAA;                 /* Lead-out track number */
2180         store_cdrom_address(&buf[16], msf, curlun->num_sectors);
2181         return 20;
2182 }
2183
2184
2185 static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2186 {
2187         struct lun      *curlun = fsg->curlun;
2188         int             mscmnd = fsg->cmnd[0];
2189         u8              *buf = (u8 *) bh->buf;
2190         u8              *buf0 = buf;
2191         int             pc, page_code;
2192         int             changeable_values, all_pages;
2193         int             valid_page = 0;
2194         int             len, limit;
2195
2196         if ((fsg->cmnd[1] & ~0x08) != 0) {              // Mask away DBD
2197                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2198                 return -EINVAL;
2199         }
2200         pc = fsg->cmnd[2] >> 6;
2201         page_code = fsg->cmnd[2] & 0x3f;
2202         if (pc == 3) {
2203                 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
2204                 return -EINVAL;
2205         }
2206         changeable_values = (pc == 1);
2207         all_pages = (page_code == 0x3f);
2208
2209         /* Write the mode parameter header.  Fixed values are: default
2210          * medium type, no cache control (DPOFUA), and no block descriptors.
2211          * The only variable value is the WriteProtect bit.  We will fill in
2212          * the mode data length later. */
2213         memset(buf, 0, 8);
2214         if (mscmnd == SC_MODE_SENSE_6) {
2215                 buf[2] = (curlun->ro ? 0x80 : 0x00);            // WP, DPOFUA
2216                 buf += 4;
2217                 limit = 255;
2218         } else {                        // SC_MODE_SENSE_10
2219                 buf[3] = (curlun->ro ? 0x80 : 0x00);            // WP, DPOFUA
2220                 buf += 8;
2221                 limit = 65535;          // Should really be mod_data.buflen
2222         }
2223
2224         /* No block descriptors */
2225
2226         /* The mode pages, in numerical order.  The only page we support
2227          * is the Caching page. */
2228         if (page_code == 0x08 || all_pages) {
2229                 valid_page = 1;
2230                 buf[0] = 0x08;          // Page code
2231                 buf[1] = 10;            // Page length
2232                 memset(buf+2, 0, 10);   // None of the fields are changeable
2233
2234                 if (!changeable_values) {
2235                         buf[2] = 0x04;  // Write cache enable,
2236                                         // Read cache not disabled
2237                                         // No cache retention priorities
2238                         put_unaligned_be16(0xffff, &buf[4]);
2239                                         /* Don't disable prefetch */
2240                                         /* Minimum prefetch = 0 */
2241                         put_unaligned_be16(0xffff, &buf[8]);
2242                                         /* Maximum prefetch */
2243                         put_unaligned_be16(0xffff, &buf[10]);
2244                                         /* Maximum prefetch ceiling */
2245                 }
2246                 buf += 12;
2247         }
2248
2249         /* Check that a valid page was requested and the mode data length
2250          * isn't too long. */
2251         len = buf - buf0;
2252         if (!valid_page || len > limit) {
2253                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2254                 return -EINVAL;
2255         }
2256
2257         /*  Store the mode data length */
2258         if (mscmnd == SC_MODE_SENSE_6)
2259                 buf0[0] = len - 1;
2260         else
2261                 put_unaligned_be16(len - 2, buf0);
2262         return len;
2263 }
2264
2265
2266 static int do_start_stop(struct fsg_dev *fsg)
2267 {
2268         struct lun      *curlun = fsg->curlun;
2269         int             loej, start;
2270
2271         if (!mod_data.removable) {
2272                 curlun->sense_data = SS_INVALID_COMMAND;
2273                 return -EINVAL;
2274         }
2275
2276         // int immed = fsg->cmnd[1] & 0x01;
2277         loej = fsg->cmnd[4] & 0x02;
2278         start = fsg->cmnd[4] & 0x01;
2279
2280 #ifdef CONFIG_USB_FILE_STORAGE_TEST
2281         if ((fsg->cmnd[1] & ~0x01) != 0 ||              // Mask away Immed
2282                         (fsg->cmnd[4] & ~0x03) != 0) {  // Mask LoEj, Start
2283                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2284                 return -EINVAL;
2285         }
2286
2287         if (!start) {
2288
2289                 /* Are we allowed to unload the media? */
2290                 if (curlun->prevent_medium_removal) {
2291                         LDBG(curlun, "unload attempt prevented\n");
2292                         curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
2293                         return -EINVAL;
2294                 }
2295                 if (loej) {             // Simulate an unload/eject
2296                         up_read(&fsg->filesem);
2297                         down_write(&fsg->filesem);
2298                         close_backing_file(curlun);
2299                         up_write(&fsg->filesem);
2300                         down_read(&fsg->filesem);
2301                 }
2302         } else {
2303
2304                 /* Our emulation doesn't support mounting; the medium is
2305                  * available for use as soon as it is loaded. */
2306                 if (!backing_file_is_open(curlun)) {
2307                         curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2308                         return -EINVAL;
2309                 }
2310         }
2311 #endif
2312         return 0;
2313 }
2314
2315
2316 static int do_prevent_allow(struct fsg_dev *fsg)
2317 {
2318         struct lun      *curlun = fsg->curlun;
2319         int             prevent;
2320
2321         if (!mod_data.removable) {
2322                 curlun->sense_data = SS_INVALID_COMMAND;
2323                 return -EINVAL;
2324         }
2325
2326         prevent = fsg->cmnd[4] & 0x01;
2327         if ((fsg->cmnd[4] & ~0x01) != 0) {              // Mask away Prevent
2328                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2329                 return -EINVAL;
2330         }
2331
2332         if (curlun->prevent_medium_removal && !prevent)
2333                 fsync_sub(curlun);
2334         curlun->prevent_medium_removal = prevent;
2335         return 0;
2336 }
2337
2338
2339 static int do_read_format_capacities(struct fsg_dev *fsg,
2340                         struct fsg_buffhd *bh)
2341 {
2342         struct lun      *curlun = fsg->curlun;
2343         u8              *buf = (u8 *) bh->buf;
2344
2345         buf[0] = buf[1] = buf[2] = 0;
2346         buf[3] = 8;             // Only the Current/Maximum Capacity Descriptor
2347         buf += 4;
2348
2349         put_unaligned_be32(curlun->num_sectors, &buf[0]);
2350                                                 /* Number of blocks */
2351         put_unaligned_be32(512, &buf[4]);       /* Block length */
2352         buf[4] = 0x02;                          /* Current capacity */
2353         return 12;
2354 }
2355
2356
2357 static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2358 {
2359         struct lun      *curlun = fsg->curlun;
2360
2361         /* We don't support MODE SELECT */
2362         curlun->sense_data = SS_INVALID_COMMAND;
2363         return -EINVAL;
2364 }
2365
2366
2367 /*-------------------------------------------------------------------------*/
2368
2369 static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
2370 {
2371         int     rc;
2372
2373         rc = fsg_set_halt(fsg, fsg->bulk_in);
2374         if (rc == -EAGAIN)
2375                 VDBG(fsg, "delayed bulk-in endpoint halt\n");
2376         while (rc != 0) {
2377                 if (rc != -EAGAIN) {
2378                         WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
2379                         rc = 0;
2380                         break;
2381                 }
2382
2383                 /* Wait for a short time and then try again */
2384                 if (msleep_interruptible(100) != 0)
2385                         return -EINTR;
2386                 rc = usb_ep_set_halt(fsg->bulk_in);
2387         }
2388         return rc;
2389 }
2390
2391 static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
2392 {
2393         int     rc;
2394
2395         DBG(fsg, "bulk-in set wedge\n");
2396         rc = usb_ep_set_wedge(fsg->bulk_in);
2397         if (rc == -EAGAIN)
2398                 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
2399         while (rc != 0) {
2400                 if (rc != -EAGAIN) {
2401                         WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
2402                         rc = 0;
2403                         break;
2404                 }
2405
2406                 /* Wait for a short time and then try again */
2407                 if (msleep_interruptible(100) != 0)
2408                         return -EINTR;
2409                 rc = usb_ep_set_wedge(fsg->bulk_in);
2410         }
2411         return rc;
2412 }
2413
2414 static int pad_with_zeros(struct fsg_dev *fsg)
2415 {
2416         struct fsg_buffhd       *bh = fsg->next_buffhd_to_fill;
2417         u32                     nkeep = bh->inreq->length;
2418         u32                     nsend;
2419         int                     rc;
2420
2421         bh->state = BUF_STATE_EMPTY;            // For the first iteration
2422         fsg->usb_amount_left = nkeep + fsg->residue;
2423         while (fsg->usb_amount_left > 0) {
2424
2425                 /* Wait for the next buffer to be free */
2426                 while (bh->state != BUF_STATE_EMPTY) {
2427                         rc = sleep_thread(fsg);
2428                         if (rc)
2429                                 return rc;
2430                 }
2431
2432                 nsend = min(fsg->usb_amount_left, (u32) mod_data.buflen);
2433                 memset(bh->buf + nkeep, 0, nsend - nkeep);
2434                 bh->inreq->length = nsend;
2435                 bh->inreq->zero = 0;
2436                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2437                                 &bh->inreq_busy, &bh->state);
2438                 bh = fsg->next_buffhd_to_fill = bh->next;
2439                 fsg->usb_amount_left -= nsend;
2440                 nkeep = 0;
2441         }
2442         return 0;
2443 }
2444
2445 static int throw_away_data(struct fsg_dev *fsg)
2446 {
2447         struct fsg_buffhd       *bh;
2448         u32                     amount;
2449         int                     rc;
2450
2451         while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
2452                         fsg->usb_amount_left > 0) {
2453
2454                 /* Throw away the data in a filled buffer */
2455                 if (bh->state == BUF_STATE_FULL) {
2456                         smp_rmb();
2457                         bh->state = BUF_STATE_EMPTY;
2458                         fsg->next_buffhd_to_drain = bh->next;
2459
2460                         /* A short packet or an error ends everything */
2461                         if (bh->outreq->actual != bh->outreq->length ||
2462                                         bh->outreq->status != 0) {
2463                                 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2464                                 return -EINTR;
2465                         }
2466                         continue;
2467                 }
2468
2469                 /* Try to submit another request if we need one */
2470                 bh = fsg->next_buffhd_to_fill;
2471                 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
2472                         amount = min(fsg->usb_amount_left,
2473                                         (u32) mod_data.buflen);
2474
2475                         /* amount is always divisible by 512, hence by
2476                          * the bulk-out maxpacket size */
2477                         bh->outreq->length = bh->bulk_out_intended_length =
2478                                         amount;
2479                         bh->outreq->short_not_ok = 1;
2480                         start_transfer(fsg, fsg->bulk_out, bh->outreq,
2481                                         &bh->outreq_busy, &bh->state);
2482                         fsg->next_buffhd_to_fill = bh->next;
2483                         fsg->usb_amount_left -= amount;
2484                         continue;
2485                 }
2486
2487                 /* Otherwise wait for something to happen */
2488                 rc = sleep_thread(fsg);
2489                 if (rc)
2490                         return rc;
2491         }
2492         return 0;
2493 }
2494
2495
2496 static int finish_reply(struct fsg_dev *fsg)
2497 {
2498         struct fsg_buffhd       *bh = fsg->next_buffhd_to_fill;
2499         int                     rc = 0;
2500
2501         switch (fsg->data_dir) {
2502         case DATA_DIR_NONE:
2503                 break;                  // Nothing to send
2504
2505         /* If we don't know whether the host wants to read or write,
2506          * this must be CB or CBI with an unknown command.  We mustn't
2507          * try to send or receive any data.  So stall both bulk pipes
2508          * if we can and wait for a reset. */
2509         case DATA_DIR_UNKNOWN:
2510                 if (mod_data.can_stall) {
2511                         fsg_set_halt(fsg, fsg->bulk_out);
2512                         rc = halt_bulk_in_endpoint(fsg);
2513                 }
2514                 break;
2515
2516         /* All but the last buffer of data must have already been sent */
2517         case DATA_DIR_TO_HOST:
2518                 if (fsg->data_size == 0)
2519                         ;               // Nothing to send
2520
2521                 /* If there's no residue, simply send the last buffer */
2522                 else if (fsg->residue == 0) {
2523                         bh->inreq->zero = 0;
2524                         start_transfer(fsg, fsg->bulk_in, bh->inreq,
2525                                         &bh->inreq_busy, &bh->state);
2526                         fsg->next_buffhd_to_fill = bh->next;
2527                 }
2528
2529                 /* There is a residue.  For CB and CBI, simply mark the end
2530                  * of the data with a short packet.  However, if we are
2531                  * allowed to stall, there was no data at all (residue ==
2532                  * data_size), and the command failed (invalid LUN or
2533                  * sense data is set), then halt the bulk-in endpoint
2534                  * instead. */
2535                 else if (!transport_is_bbb()) {
2536                         if (mod_data.can_stall &&
2537                                         fsg->residue == fsg->data_size &&
2538         (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
2539                                 bh->state = BUF_STATE_EMPTY;
2540                                 rc = halt_bulk_in_endpoint(fsg);
2541                         } else {
2542                                 bh->inreq->zero = 1;
2543                                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2544                                                 &bh->inreq_busy, &bh->state);
2545                                 fsg->next_buffhd_to_fill = bh->next;
2546                         }
2547                 }
2548
2549                 /* For Bulk-only, if we're allowed to stall then send the
2550                  * short packet and halt the bulk-in endpoint.  If we can't
2551                  * stall, pad out the remaining data with 0's. */
2552                 else {
2553                         if (mod_data.can_stall) {
2554                                 bh->inreq->zero = 1;
2555                                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2556                                                 &bh->inreq_busy, &bh->state);
2557                                 fsg->next_buffhd_to_fill = bh->next;
2558                                 rc = halt_bulk_in_endpoint(fsg);
2559                         } else
2560                                 rc = pad_with_zeros(fsg);
2561                 }
2562                 break;
2563
2564         /* We have processed all we want from the data the host has sent.
2565          * There may still be outstanding bulk-out requests. */
2566         case DATA_DIR_FROM_HOST:
2567                 if (fsg->residue == 0)
2568                         ;               // Nothing to receive
2569
2570                 /* Did the host stop sending unexpectedly early? */
2571                 else if (fsg->short_packet_received) {
2572                         raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2573                         rc = -EINTR;
2574                 }
2575
2576                 /* We haven't processed all the incoming data.  Even though
2577                  * we may be allowed to stall, doing so would cause a race.
2578                  * The controller may already have ACK'ed all the remaining
2579                  * bulk-out packets, in which case the host wouldn't see a
2580                  * STALL.  Not realizing the endpoint was halted, it wouldn't
2581                  * clear the halt -- leading to problems later on. */
2582 #if 0
2583                 else if (mod_data.can_stall) {
2584                         fsg_set_halt(fsg, fsg->bulk_out);
2585                         raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2586                         rc = -EINTR;
2587                 }
2588 #endif
2589
2590                 /* We can't stall.  Read in the excess data and throw it
2591                  * all away. */
2592                 else
2593                         rc = throw_away_data(fsg);
2594                 break;
2595         }
2596         return rc;
2597 }
2598
2599
2600 static int send_status(struct fsg_dev *fsg)
2601 {
2602         struct lun              *curlun = fsg->curlun;
2603         struct fsg_buffhd       *bh;
2604         int                     rc;
2605         u8                      status = USB_STATUS_PASS;
2606         u32                     sd, sdinfo = 0;
2607
2608         /* Wait for the next buffer to become available */
2609         bh = fsg->next_buffhd_to_fill;
2610         while (bh->state != BUF_STATE_EMPTY) {
2611                 rc = sleep_thread(fsg);
2612                 if (rc)
2613                         return rc;
2614         }
2615
2616         if (curlun) {
2617                 sd = curlun->sense_data;
2618                 sdinfo = curlun->sense_data_info;
2619         } else if (fsg->bad_lun_okay)
2620                 sd = SS_NO_SENSE;
2621         else
2622                 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2623
2624         if (fsg->phase_error) {
2625                 DBG(fsg, "sending phase-error status\n");
2626                 status = USB_STATUS_PHASE_ERROR;
2627                 sd = SS_INVALID_COMMAND;
2628         } else if (sd != SS_NO_SENSE) {
2629                 DBG(fsg, "sending command-failure status\n");
2630                 status = USB_STATUS_FAIL;
2631                 VDBG(fsg, "  sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2632                                 "  info x%x\n",
2633                                 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
2634         }
2635
2636         if (transport_is_bbb()) {
2637                 struct bulk_cs_wrap     *csw = bh->buf;
2638
2639                 /* Store and send the Bulk-only CSW */
2640                 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
2641                 csw->Tag = fsg->tag;
2642                 csw->Residue = cpu_to_le32(fsg->residue);
2643                 csw->Status = status;
2644
2645                 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
2646                 bh->inreq->zero = 0;
2647                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2648                                 &bh->inreq_busy, &bh->state);
2649
2650         } else if (mod_data.transport_type == USB_PR_CB) {
2651
2652                 /* Control-Bulk transport has no status phase! */
2653                 return 0;
2654
2655         } else {                        // USB_PR_CBI
2656                 struct interrupt_data   *buf = bh->buf;
2657
2658                 /* Store and send the Interrupt data.  UFI sends the ASC
2659                  * and ASCQ bytes.  Everything else sends a Type (which
2660                  * is always 0) and the status Value. */
2661                 if (mod_data.protocol_type == USB_SC_UFI) {
2662                         buf->bType = ASC(sd);
2663                         buf->bValue = ASCQ(sd);
2664                 } else {
2665                         buf->bType = 0;
2666                         buf->bValue = status;
2667                 }
2668                 fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
2669
2670                 fsg->intr_buffhd = bh;          // Point to the right buffhd
2671                 fsg->intreq->buf = bh->inreq->buf;
2672                 fsg->intreq->context = bh;
2673                 start_transfer(fsg, fsg->intr_in, fsg->intreq,
2674                                 &fsg->intreq_busy, &bh->state);
2675         }
2676
2677         fsg->next_buffhd_to_fill = bh->next;
2678         return 0;
2679 }
2680
2681
2682 /*-------------------------------------------------------------------------*/
2683
2684 /* Check whether the command is properly formed and whether its data size
2685  * and direction agree with the values we already have. */
2686 static int check_command(struct fsg_dev *fsg, int cmnd_size,
2687                 enum data_direction data_dir, unsigned int mask,
2688                 int needs_medium, const char *name)
2689 {
2690         int                     i;
2691         int                     lun = fsg->cmnd[1] >> 5;
2692         static const char       dirletter[4] = {'u', 'o', 'i', 'n'};
2693         char                    hdlen[20];
2694         struct lun              *curlun;
2695
2696         /* Adjust the expected cmnd_size for protocol encapsulation padding.
2697          * Transparent SCSI doesn't pad. */
2698         if (protocol_is_scsi())
2699                 ;
2700
2701         /* There's some disagreement as to whether RBC pads commands or not.
2702          * We'll play it safe and accept either form. */
2703         else if (mod_data.protocol_type == USB_SC_RBC) {
2704                 if (fsg->cmnd_size == 12)
2705                         cmnd_size = 12;
2706
2707         /* All the other protocols pad to 12 bytes */
2708         } else
2709                 cmnd_size = 12;
2710
2711         hdlen[0] = 0;
2712         if (fsg->data_dir != DATA_DIR_UNKNOWN)
2713                 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
2714                                 fsg->data_size);
2715         VDBG(fsg, "SCSI command: %s;  Dc=%d, D%c=%u;  Hc=%d%s\n",
2716                         name, cmnd_size, dirletter[(int) data_dir],
2717                         fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
2718
2719         /* We can't reply at all until we know the correct data direction
2720          * and size. */
2721         if (fsg->data_size_from_cmnd == 0)
2722                 data_dir = DATA_DIR_NONE;
2723         if (fsg->data_dir == DATA_DIR_UNKNOWN) {        // CB or CBI
2724                 fsg->data_dir = data_dir;
2725                 fsg->data_size = fsg->data_size_from_cmnd;
2726
2727         } else {                                        // Bulk-only
2728                 if (fsg->data_size < fsg->data_size_from_cmnd) {
2729
2730                         /* Host data size < Device data size is a phase error.
2731                          * Carry out the command, but only transfer as much
2732                          * as we are allowed. */
2733                         fsg->data_size_from_cmnd = fsg->data_size;
2734                         fsg->phase_error = 1;
2735                 }
2736         }
2737         fsg->residue = fsg->usb_amount_left = fsg->data_size;
2738
2739         /* Conflicting data directions is a phase error */
2740         if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
2741                 fsg->phase_error = 1;
2742                 return -EINVAL;
2743         }
2744
2745         /* Verify the length of the command itself */
2746         if (cmnd_size != fsg->cmnd_size) {
2747
2748                 /* Special case workaround: There are plenty of buggy SCSI
2749                  * implementations. Many have issues with cbw->Length
2750                  * field passing a wrong command size. For those cases we
2751                  * always try to work around the problem by using the length
2752                  * sent by the host side provided it is at least as large
2753                  * as the correct command length.
2754                  * Examples of such cases would be MS-Windows, which issues
2755                  * REQUEST SENSE with cbw->Length == 12 where it should
2756                  * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
2757                  * REQUEST SENSE with cbw->Length == 10 where it should
2758                  * be 6 as well.
2759                  */
2760                 if (cmnd_size <= fsg->cmnd_size) {
2761                         DBG(fsg, "%s is buggy! Expected length %d "
2762                                         "but we got %d\n", name,
2763                                         cmnd_size, fsg->cmnd_size);
2764                         cmnd_size = fsg->cmnd_size;
2765                 } else {
2766                         fsg->phase_error = 1;
2767                         return -EINVAL;
2768                 }
2769         }
2770
2771         /* Check that the LUN values are consistent */
2772         if (transport_is_bbb()) {
2773                 if (fsg->lun != lun)
2774                         DBG(fsg, "using LUN %d from CBW, "
2775                                         "not LUN %d from CDB\n",
2776                                         fsg->lun, lun);
2777         } else
2778                 fsg->lun = lun;         // Use LUN from the command
2779
2780         /* Check the LUN */
2781         if (fsg->lun >= 0 && fsg->lun < fsg->nluns) {
2782                 fsg->curlun = curlun = &fsg->luns[fsg->lun];
2783                 if (fsg->cmnd[0] != SC_REQUEST_SENSE) {
2784                         curlun->sense_data = SS_NO_SENSE;
2785                         curlun->sense_data_info = 0;
2786                         curlun->info_valid = 0;
2787                 }
2788         } else {
2789                 fsg->curlun = curlun = NULL;
2790                 fsg->bad_lun_okay = 0;
2791
2792                 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2793                  * to use unsupported LUNs; all others may not. */
2794                 if (fsg->cmnd[0] != SC_INQUIRY &&
2795                                 fsg->cmnd[0] != SC_REQUEST_SENSE) {
2796                         DBG(fsg, "unsupported LUN %d\n", fsg->lun);
2797                         return -EINVAL;
2798                 }
2799         }
2800
2801         /* If a unit attention condition exists, only INQUIRY and
2802          * REQUEST SENSE commands are allowed; anything else must fail. */
2803         if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
2804                         fsg->cmnd[0] != SC_INQUIRY &&
2805                         fsg->cmnd[0] != SC_REQUEST_SENSE) {
2806                 curlun->sense_data = curlun->unit_attention_data;
2807                 curlun->unit_attention_data = SS_NO_SENSE;
2808                 return -EINVAL;
2809         }
2810
2811         /* Check that only command bytes listed in the mask are non-zero */
2812         fsg->cmnd[1] &= 0x1f;                   // Mask away the LUN
2813         for (i = 1; i < cmnd_size; ++i) {
2814                 if (fsg->cmnd[i] && !(mask & (1 << i))) {
2815                         if (curlun)
2816                                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2817                         return -EINVAL;
2818                 }
2819         }
2820
2821         /* If the medium isn't mounted and the command needs to access
2822          * it, return an error. */
2823         if (curlun && !backing_file_is_open(curlun) && needs_medium) {
2824                 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2825                 return -EINVAL;
2826         }
2827
2828         return 0;
2829 }
2830
2831
2832 static int do_scsi_command(struct fsg_dev *fsg)
2833 {
2834         struct fsg_buffhd       *bh;
2835         int                     rc;
2836         int                     reply = -EINVAL;
2837         int                     i;
2838         static char             unknown[16];
2839
2840         dump_cdb(fsg);
2841
2842         /* Wait for the next buffer to become available for data or status */
2843         bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
2844         while (bh->state != BUF_STATE_EMPTY) {
2845                 rc = sleep_thread(fsg);
2846                 if (rc)
2847                         return rc;
2848         }
2849         fsg->phase_error = 0;
2850         fsg->short_packet_received = 0;
2851
2852         down_read(&fsg->filesem);       // We're using the backing file
2853         switch (fsg->cmnd[0]) {
2854
2855         case SC_INQUIRY:
2856                 fsg->data_size_from_cmnd = fsg->cmnd[4];
2857                 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2858                                 (1<<4), 0,
2859                                 "INQUIRY")) == 0)
2860                         reply = do_inquiry(fsg, bh);
2861                 break;
2862
2863         case SC_MODE_SELECT_6:
2864                 fsg->data_size_from_cmnd = fsg->cmnd[4];
2865                 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2866                                 (1<<1) | (1<<4), 0,
2867                                 "MODE SELECT(6)")) == 0)
2868                         reply = do_mode_select(fsg, bh);
2869                 break;
2870
2871         case SC_MODE_SELECT_10:
2872                 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2873                 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2874                                 (1<<1) | (3<<7), 0,
2875                                 "MODE SELECT(10)")) == 0)
2876                         reply = do_mode_select(fsg, bh);
2877                 break;
2878
2879         case SC_MODE_SENSE_6:
2880                 fsg->data_size_from_cmnd = fsg->cmnd[4];
2881                 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2882                                 (1<<1) | (1<<2) | (1<<4), 0,
2883                                 "MODE SENSE(6)")) == 0)
2884                         reply = do_mode_sense(fsg, bh);
2885                 break;
2886
2887         case SC_MODE_SENSE_10:
2888                 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2889                 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2890                                 (1<<1) | (1<<2) | (3<<7), 0,
2891                                 "MODE SENSE(10)")) == 0)
2892                         reply = do_mode_sense(fsg, bh);
2893                 break;
2894
2895         case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
2896                 fsg->data_size_from_cmnd = 0;
2897                 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2898                                 (1<<4), 0,
2899                                 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
2900                         reply = do_prevent_allow(fsg);
2901                 break;
2902
2903         case SC_READ_6:
2904                 i = fsg->cmnd[4];
2905                 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2906                 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2907                                 (7<<1) | (1<<4), 1,
2908                                 "READ(6)")) == 0)
2909                         reply = do_read(fsg);
2910                 break;
2911
2912         case SC_READ_10:
2913                 fsg->data_size_from_cmnd =
2914                                 get_unaligned_be16(&fsg->cmnd[7]) << 9;
2915                 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2916                                 (1<<1) | (0xf<<2) | (3<<7), 1,
2917                                 "READ(10)")) == 0)
2918                         reply = do_read(fsg);
2919                 break;
2920
2921         case SC_READ_12:
2922                 fsg->data_size_from_cmnd =
2923                                 get_unaligned_be32(&fsg->cmnd[6]) << 9;
2924                 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2925                                 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2926                                 "READ(12)")) == 0)
2927                         reply = do_read(fsg);
2928                 break;
2929
2930         case SC_READ_CAPACITY:
2931                 fsg->data_size_from_cmnd = 8;
2932                 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2933                                 (0xf<<2) | (1<<8), 1,
2934                                 "READ CAPACITY")) == 0)
2935                         reply = do_read_capacity(fsg, bh);
2936                 break;
2937
2938         case SC_READ_HEADER:
2939                 if (!mod_data.cdrom)
2940                         goto unknown_cmnd;
2941                 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2942                 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2943                                 (3<<7) | (0x1f<<1), 1,
2944                                 "READ HEADER")) == 0)
2945                         reply = do_read_header(fsg, bh);
2946                 break;
2947
2948         case SC_READ_TOC:
2949                 if (!mod_data.cdrom)
2950                         goto unknown_cmnd;
2951                 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2952                 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2953                                 (7<<6) | (1<<1), 1,
2954                                 "READ TOC")) == 0)
2955                         reply = do_read_toc(fsg, bh);
2956                 break;
2957
2958         case SC_READ_FORMAT_CAPACITIES:
2959                 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2960                 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2961                                 (3<<7), 1,
2962                                 "READ FORMAT CAPACITIES")) == 0)
2963                         reply = do_read_format_capacities(fsg, bh);
2964                 break;
2965
2966         case SC_REQUEST_SENSE:
2967                 fsg->data_size_from_cmnd = fsg->cmnd[4];
2968                 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2969                                 (1<<4), 0,
2970                                 "REQUEST SENSE")) == 0)
2971                         reply = do_request_sense(fsg, bh);
2972                 break;
2973
2974         case SC_START_STOP_UNIT:
2975                 fsg->data_size_from_cmnd = 0;
2976                 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2977                                 (1<<1) | (1<<4), 0,
2978                                 "START-STOP UNIT")) == 0)
2979                         reply = do_start_stop(fsg);
2980                 break;
2981
2982         case SC_SYNCHRONIZE_CACHE:
2983                 fsg->data_size_from_cmnd = 0;
2984                 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2985                                 (0xf<<2) | (3<<7), 1,
2986                                 "SYNCHRONIZE CACHE")) == 0)
2987                         reply = do_synchronize_cache(fsg);
2988                 break;
2989
2990         case SC_TEST_UNIT_READY:
2991                 fsg->data_size_from_cmnd = 0;
2992                 reply = check_command(fsg, 6, DATA_DIR_NONE,
2993                                 0, 1,
2994                                 "TEST UNIT READY");
2995                 break;
2996
2997         /* Although optional, this command is used by MS-Windows.  We
2998          * support a minimal version: BytChk must be 0. */
2999         case SC_VERIFY:
3000                 fsg->data_size_from_cmnd = 0;
3001                 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
3002                                 (1<<1) | (0xf<<2) | (3<<7), 1,
3003                                 "VERIFY")) == 0)
3004                         reply = do_verify(fsg);
3005                 break;
3006
3007         case SC_WRITE_6:
3008                 i = fsg->cmnd[4];
3009                 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
3010                 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
3011                                 (7<<1) | (1<<4), 1,
3012                                 "WRITE(6)")) == 0)
3013                         reply = do_write(fsg);
3014                 break;
3015
3016         case SC_WRITE_10:
3017                 fsg->data_size_from_cmnd =
3018                                 get_unaligned_be16(&fsg->cmnd[7]) << 9;
3019                 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
3020                                 (1<<1) | (0xf<<2) | (3<<7), 1,
3021                                 "WRITE(10)")) == 0)
3022                         reply = do_write(fsg);
3023                 break;
3024
3025         case SC_WRITE_12:
3026                 fsg->data_size_from_cmnd =
3027                                 get_unaligned_be32(&fsg->cmnd[6]) << 9;
3028                 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
3029                                 (1<<1) | (0xf<<2) | (0xf<<6), 1,
3030                                 "WRITE(12)")) == 0)
3031                         reply = do_write(fsg);
3032                 break;
3033
3034         /* Some mandatory commands that we recognize but don't implement.
3035          * They don't mean much in this setting.  It's left as an exercise
3036          * for anyone interested to implement RESERVE and RELEASE in terms
3037          * of Posix locks. */
3038         case SC_FORMAT_UNIT:
3039         case SC_RELEASE:
3040         case SC_RESERVE:
3041         case SC_SEND_DIAGNOSTIC:
3042                 // Fall through
3043
3044         default:
3045  unknown_cmnd:
3046                 fsg->data_size_from_cmnd = 0;
3047                 sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
3048                 if ((reply = check_command(fsg, fsg->cmnd_size,
3049                                 DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) {
3050                         fsg->curlun->sense_data = SS_INVALID_COMMAND;
3051                         reply = -EINVAL;
3052                 }
3053                 break;
3054         }
3055         up_read(&fsg->filesem);
3056
3057         if (reply == -EINTR || signal_pending(current))
3058                 return -EINTR;
3059
3060         /* Set up the single reply buffer for finish_reply() */
3061         if (reply == -EINVAL)
3062                 reply = 0;              // Error reply length
3063         if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
3064                 reply = min((u32) reply, fsg->data_size_from_cmnd);
3065                 bh->inreq->length = reply;
3066                 bh->state = BUF_STATE_FULL;
3067                 fsg->residue -= reply;
3068         }                               // Otherwise it's already set
3069
3070         return 0;
3071 }
3072
3073
3074 /*-------------------------------------------------------------------------*/
3075
3076 static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
3077 {
3078         struct usb_request      *req = bh->outreq;
3079         struct bulk_cb_wrap     *cbw = req->buf;
3080
3081         /* Was this a real packet?  Should it be ignored? */
3082         if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
3083                 return -EINVAL;
3084
3085         /* Is the CBW valid? */
3086         if (req->actual != USB_BULK_CB_WRAP_LEN ||
3087                         cbw->Signature != cpu_to_le32(
3088                                 USB_BULK_CB_SIG)) {
3089                 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
3090                                 req->actual,
3091                                 le32_to_cpu(cbw->Signature));
3092
3093                 /* The Bulk-only spec says we MUST stall the IN endpoint
3094                  * (6.6.1), so it's unavoidable.  It also says we must
3095                  * retain this state until the next reset, but there's
3096                  * no way to tell the controller driver it should ignore
3097                  * Clear-Feature(HALT) requests.
3098                  *
3099                  * We aren't required to halt the OUT endpoint; instead
3100                  * we can simply accept and discard any data received
3101                  * until the next reset. */
3102                 wedge_bulk_in_endpoint(fsg);
3103                 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
3104                 return -EINVAL;
3105         }
3106
3107         /* Is the CBW meaningful? */
3108         if (cbw->Lun >= MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
3109                         cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
3110                 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
3111                                 "cmdlen %u\n",
3112                                 cbw->Lun, cbw->Flags, cbw->Length);
3113
3114                 /* We can do anything we want here, so let's stall the
3115                  * bulk pipes if we are allowed to. */
3116                 if (mod_data.can_stall) {
3117                         fsg_set_halt(fsg, fsg->bulk_out);
3118                         halt_bulk_in_endpoint(fsg);
3119                 }
3120                 return -EINVAL;
3121         }
3122
3123         /* Save the command for later */
3124         fsg->cmnd_size = cbw->Length;
3125         memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
3126         if (cbw->Flags & USB_BULK_IN_FLAG)
3127                 fsg->data_dir = DATA_DIR_TO_HOST;
3128         else
3129                 fsg->data_dir = DATA_DIR_FROM_HOST;
3130         fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
3131         if (fsg->data_size == 0)
3132                 fsg->data_dir = DATA_DIR_NONE;
3133         fsg->lun = cbw->Lun;
3134         fsg->tag = cbw->Tag;
3135         return 0;
3136 }
3137
3138
3139 static int get_next_command(struct fsg_dev *fsg)
3140 {
3141         struct fsg_buffhd       *bh;
3142         int                     rc = 0;
3143
3144         if (transport_is_bbb()) {
3145
3146                 /* Wait for the next buffer to become available */
3147                 bh = fsg->next_buffhd_to_fill;
3148                 while (bh->state != BUF_STATE_EMPTY) {
3149                         rc = sleep_thread(fsg);
3150                         if (rc)
3151                                 return rc;
3152                 }
3153
3154                 /* Queue a request to read a Bulk-only CBW */
3155                 set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN);
3156                 bh->outreq->short_not_ok = 1;
3157                 start_transfer(fsg, fsg->bulk_out, bh->outreq,
3158                                 &bh->outreq_busy, &bh->state);
3159
3160                 /* We will drain the buffer in software, which means we
3161                  * can reuse it for the next filling.  No need to advance
3162                  * next_buffhd_to_fill. */
3163
3164                 /* Wait for the CBW to arrive */
3165                 while (bh->state != BUF_STATE_FULL) {
3166                         rc = sleep_thread(fsg);
3167                         if (rc)
3168                                 return rc;
3169                 }
3170                 smp_rmb();
3171                 rc = received_cbw(fsg, bh);
3172                 bh->state = BUF_STATE_EMPTY;
3173
3174         } else {                // USB_PR_CB or USB_PR_CBI
3175
3176                 /* Wait for the next command to arrive */
3177                 while (fsg->cbbuf_cmnd_size == 0) {
3178                         rc = sleep_thread(fsg);
3179                         if (rc)
3180                                 return rc;
3181                 }
3182
3183                 /* Is the previous status interrupt request still busy?
3184                  * The host is allowed to skip reading the status,
3185                  * so we must cancel it. */
3186                 if (fsg->intreq_busy)
3187                         usb_ep_dequeue(fsg->intr_in, fsg->intreq);
3188
3189                 /* Copy the command and mark the buffer empty */
3190                 fsg->data_dir = DATA_DIR_UNKNOWN;
3191                 spin_lock_irq(&fsg->lock);
3192                 fsg->cmnd_size = fsg->cbbuf_cmnd_size;
3193                 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
3194                 fsg->cbbuf_cmnd_size = 0;
3195                 spin_unlock_irq(&fsg->lock);
3196         }
3197         return rc;
3198 }
3199
3200
3201 /*-------------------------------------------------------------------------*/
3202
3203 static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
3204                 const struct usb_endpoint_descriptor *d)
3205 {
3206         int     rc;
3207
3208         ep->driver_data = fsg;
3209         rc = usb_ep_enable(ep, d);
3210         if (rc)
3211                 ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
3212         return rc;
3213 }
3214
3215 static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
3216                 struct usb_request **preq)
3217 {
3218         *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
3219         if (*preq)
3220                 return 0;
3221         ERROR(fsg, "can't allocate request for %s\n", ep->name);
3222         return -ENOMEM;
3223 }
3224
3225 /*
3226  * Reset interface setting and re-init endpoint state (toggle etc).
3227  * Call with altsetting < 0 to disable the interface.  The only other
3228  * available altsetting is 0, which enables the interface.
3229  */
3230 static int do_set_interface(struct fsg_dev *fsg, int altsetting)
3231 {
3232         int     rc = 0;
3233         int     i;
3234         const struct usb_endpoint_descriptor    *d;
3235
3236         if (fsg->running)
3237                 DBG(fsg, "reset interface\n");
3238
3239 reset:
3240         /* Deallocate the requests */
3241         for (i = 0; i < NUM_BUFFERS; ++i) {
3242                 struct fsg_buffhd *bh = &fsg->buffhds[i];
3243
3244                 if (bh->inreq) {
3245                         usb_ep_free_request(fsg->bulk_in, bh->inreq);
3246                         bh->inreq = NULL;
3247                 }
3248                 if (bh->outreq) {
3249                         usb_ep_free_request(fsg->bulk_out, bh->outreq);
3250                         bh->outreq = NULL;
3251                 }
3252         }
3253         if (fsg->intreq) {
3254                 usb_ep_free_request(fsg->intr_in, fsg->intreq);
3255                 fsg->intreq = NULL;
3256         }
3257
3258         /* Disable the endpoints */
3259         if (fsg->bulk_in_enabled) {
3260                 usb_ep_disable(fsg->bulk_in);
3261                 fsg->bulk_in_enabled = 0;
3262         }
3263         if (fsg->bulk_out_enabled) {
3264                 usb_ep_disable(fsg->bulk_out);
3265                 fsg->bulk_out_enabled = 0;
3266         }
3267         if (fsg->intr_in_enabled) {
3268                 usb_ep_disable(fsg->intr_in);
3269                 fsg->intr_in_enabled = 0;
3270         }
3271
3272         fsg->running = 0;
3273         if (altsetting < 0 || rc != 0)
3274                 return rc;
3275
3276         DBG(fsg, "set interface %d\n", altsetting);
3277
3278         /* Enable the endpoints */
3279         d = ep_desc(fsg->gadget, &fs_bulk_in_desc, &hs_bulk_in_desc);
3280         if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
3281                 goto reset;
3282         fsg->bulk_in_enabled = 1;
3283
3284         d = ep_desc(fsg->gadget, &fs_bulk_out_desc, &hs_bulk_out_desc);
3285         if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
3286                 goto reset;
3287         fsg->bulk_out_enabled = 1;
3288         fsg->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
3289         clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
3290
3291         if (transport_is_cbi()) {
3292                 d = ep_desc(fsg->gadget, &fs_intr_in_desc, &hs_intr_in_desc);
3293                 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
3294                         goto reset;
3295                 fsg->intr_in_enabled = 1;
3296         }
3297
3298         /* Allocate the requests */
3299         for (i = 0; i < NUM_BUFFERS; ++i) {
3300                 struct fsg_buffhd       *bh = &fsg->buffhds[i];
3301
3302                 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
3303                         goto reset;
3304                 if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
3305                         goto reset;
3306                 bh->inreq->buf = bh->outreq->buf = bh->buf;
3307                 bh->inreq->context = bh->outreq->context = bh;
3308                 bh->inreq->complete = bulk_in_complete;
3309                 bh->outreq->complete = bulk_out_complete;
3310         }
3311         if (transport_is_cbi()) {
3312                 if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
3313                         goto reset;
3314                 fsg->intreq->complete = intr_in_complete;
3315         }
3316
3317         fsg->running = 1;
3318         for (i = 0; i < fsg->nluns; ++i)
3319                 fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
3320         return rc;
3321 }
3322
3323
3324 /*
3325  * Change our operational configuration.  This code must agree with the code
3326  * that returns config descriptors, and with interface altsetting code.
3327  *
3328  * It's also responsible for power management interactions.  Some
3329  * configurations might not work with our current power sources.
3330  * For now we just assume the gadget is always self-powered.
3331  */
3332 static int do_set_config(struct fsg_dev *fsg, u8 new_config)
3333 {
3334         int     rc = 0;
3335
3336         /* Disable the single interface */
3337         if (fsg->config != 0) {
3338                 DBG(fsg, "reset config\n");
3339                 fsg->config = 0;
3340                 rc = do_set_interface(fsg, -1);
3341         }
3342
3343         /* Enable the interface */
3344         if (new_config != 0) {
3345                 fsg->config = new_config;
3346                 if ((rc = do_set_interface(fsg, 0)) != 0)
3347                         fsg->config = 0;        // Reset on errors
3348                 else {
3349                         char *speed;
3350
3351                         switch (fsg->gadget->speed) {
3352                         case USB_SPEED_LOW:     speed = "low";  break;
3353                         case USB_SPEED_FULL:    speed = "full"; break;
3354                         case USB_SPEED_HIGH:    speed = "high"; break;
3355                         default:                speed = "?";    break;
3356                         }
3357                         INFO(fsg, "%s speed config #%d\n", speed, fsg->config);
3358                 }
3359         }
3360         return rc;
3361 }
3362
3363
3364 /*-------------------------------------------------------------------------*/
3365
3366 static void handle_exception(struct fsg_dev *fsg)
3367 {
3368         siginfo_t               info;
3369         int                     sig;
3370         int                     i;
3371         int                     num_active;
3372         struct fsg_buffhd       *bh;
3373         enum fsg_state          old_state;
3374         u8                      new_config;
3375         struct lun              *curlun;
3376         unsigned int            exception_req_tag;
3377         int                     rc;
3378
3379         /* Clear the existing signals.  Anything but SIGUSR1 is converted
3380          * into a high-priority EXIT exception. */
3381         for (;;) {
3382                 sig = dequeue_signal_lock(current, &current->blocked, &info);
3383                 if (!sig)
3384                         break;
3385                 if (sig != SIGUSR1) {
3386                         if (fsg->state < FSG_STATE_EXIT)
3387                                 DBG(fsg, "Main thread exiting on signal\n");
3388                         raise_exception(fsg, FSG_STATE_EXIT);
3389                 }
3390         }
3391
3392         /* Cancel all the pending transfers */
3393         if (fsg->intreq_busy)
3394                 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
3395         for (i = 0; i < NUM_BUFFERS; ++i) {
3396                 bh = &fsg->buffhds[i];
3397                 if (bh->inreq_busy)
3398                         usb_ep_dequeue(fsg->bulk_in, bh->inreq);
3399                 if (bh->outreq_busy)
3400                         usb_ep_dequeue(fsg->bulk_out, bh->outreq);
3401         }
3402
3403         /* Wait until everything is idle */
3404         for (;;) {
3405                 num_active = fsg->intreq_busy;
3406                 for (i = 0; i < NUM_BUFFERS; ++i) {
3407                         bh = &fsg->buffhds[i];
3408                         num_active += bh->inreq_busy + bh->outreq_busy;
3409                 }
3410                 if (num_active == 0)
3411                         break;
3412                 if (sleep_thread(fsg))
3413                         return;
3414         }
3415
3416         /* Clear out the controller's fifos */
3417         if (fsg->bulk_in_enabled)
3418                 usb_ep_fifo_flush(fsg->bulk_in);
3419         if (fsg->bulk_out_enabled)
3420                 usb_ep_fifo_flush(fsg->bulk_out);
3421         if (fsg->intr_in_enabled)
3422                 usb_ep_fifo_flush(fsg->intr_in);
3423
3424         /* Reset the I/O buffer states and pointers, the SCSI
3425          * state, and the exception.  Then invoke the handler. */
3426         spin_lock_irq(&fsg->lock);
3427
3428         for (i = 0; i < NUM_BUFFERS; ++i) {
3429                 bh = &fsg->buffhds[i];
3430                 bh->state = BUF_STATE_EMPTY;
3431         }
3432         fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
3433                         &fsg->buffhds[0];
3434
3435         exception_req_tag = fsg->exception_req_tag;
3436         new_config = fsg->new_config;
3437         old_state = fsg->state;
3438
3439         if (old_state == FSG_STATE_ABORT_BULK_OUT)
3440                 fsg->state = FSG_STATE_STATUS_PHASE;
3441         else {
3442                 for (i = 0; i < fsg->nluns; ++i) {
3443                         curlun = &fsg->luns[i];
3444                         curlun->prevent_medium_removal = 0;
3445                         curlun->sense_data = curlun->unit_attention_data =
3446                                         SS_NO_SENSE;
3447                         curlun->sense_data_info = 0;
3448                         curlun->info_valid = 0;
3449                 }
3450                 fsg->state = FSG_STATE_IDLE;
3451         }
3452         spin_unlock_irq(&fsg->lock);
3453
3454         /* Carry out any extra actions required for the exception */
3455         switch (old_state) {
3456         default:
3457                 break;
3458
3459         case FSG_STATE_ABORT_BULK_OUT:
3460                 send_status(fsg);
3461                 spin_lock_irq(&fsg->lock);
3462                 if (fsg->state == FSG_STATE_STATUS_PHASE)
3463                         fsg->state = FSG_STATE_IDLE;
3464                 spin_unlock_irq(&fsg->lock);
3465                 break;
3466
3467         case FSG_STATE_RESET:
3468                 /* In case we were forced against our will to halt a
3469                  * bulk endpoint, clear the halt now.  (The SuperH UDC
3470                  * requires this.) */
3471                 if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
3472                         usb_ep_clear_halt(fsg->bulk_in);
3473
3474                 if (transport_is_bbb()) {
3475                         if (fsg->ep0_req_tag == exception_req_tag)
3476                                 ep0_queue(fsg); // Complete the status stage
3477
3478                 } else if (transport_is_cbi())
3479                         send_status(fsg);       // Status by interrupt pipe
3480
3481                 /* Technically this should go here, but it would only be
3482                  * a waste of time.  Ditto for the INTERFACE_CHANGE and
3483                  * CONFIG_CHANGE cases. */
3484                 // for (i = 0; i < fsg->nluns; ++i)
3485                 //      fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
3486                 break;
3487
3488         case FSG_STATE_INTERFACE_CHANGE:
3489                 rc = do_set_interface(fsg, 0);
3490                 if (fsg->ep0_req_tag != exception_req_tag)
3491                         break;
3492                 if (rc != 0)                    // STALL on errors
3493                         fsg_set_halt(fsg, fsg->ep0);
3494                 else                            // Complete the status stage
3495                         ep0_queue(fsg);
3496                 break;
3497
3498         case FSG_STATE_CONFIG_CHANGE:
3499                 rc = do_set_config(fsg, new_config);
3500                 if (fsg->ep0_req_tag != exception_req_tag)
3501                         break;
3502                 if (rc != 0)                    // STALL on errors
3503                         fsg_set_halt(fsg,&