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