USB: g_mass_storage: superfluous and missing packets fixed
[linux-2.6.git] / drivers / usb / gadget / f_mass_storage.c
1 /*
2  * f_mass_storage.c -- Mass Storage USB Composite Function
3  *
4  * Copyright (C) 2003-2008 Alan Stern
5  * Copyright (C) 2009 Samsung Electronics
6  *                    Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The names of the above-listed copyright holders may not be used
19  *    to endorse or promote products derived from this software without
20  *    specific prior written permission.
21  *
22  * ALTERNATIVELY, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") as published by the Free Software
24  * Foundation, either version 2 of that License or (at your option) any
25  * later version.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  */
39
40
41 /*
42  * The Mass Storage Function acts as a USB Mass Storage device,
43  * appearing to the host as a disk drive or as a CD-ROM drive.  In
44  * addition to providing an example of a genuinely useful composite
45  * function for a USB device, it also illustrates a technique of
46  * double-buffering for increased throughput.
47  *
48  * Function supports multiple logical units (LUNs).  Backing storage
49  * for each LUN is provided by a regular file or a block device.
50  * Access for each LUN can be limited to read-only.  Moreover, the
51  * function can indicate that LUN is removable and/or CD-ROM.  (The
52  * later implies read-only access.)
53  *
54  * MSF is configured by specifying a fsg_config structure.  It has the
55  * following fields:
56  *
57  *      nluns           Number of LUNs function have (anywhere from 1
58  *                              to FSG_MAX_LUNS which is 8).
59  *      luns            An array of LUN configuration values.  This
60  *                              should be filled for each LUN that
61  *                              function will include (ie. for "nluns"
62  *                              LUNs).  Each element of the array has
63  *                              the following fields:
64  *      ->filename      The path to the backing file for the LUN.
65  *                              Required if LUN is not marked as
66  *                              removable.
67  *      ->ro            Flag specifying access to the LUN shall be
68  *                              read-only.  This is implied if CD-ROM
69  *                              emulation is enabled as well as when
70  *                              it was impossible to open "filename"
71  *                              in R/W mode.
72  *      ->removable     Flag specifying that LUN shall be indicated as
73  *                              being removable.
74  *      ->cdrom         Flag specifying that LUN shall be reported as
75  *                              being a CD-ROM.
76  *
77  *      lun_name_format A printf-like format for names of the LUN
78  *                              devices.  This determines how the
79  *                              directory in sysfs will be named.
80  *                              Unless you are using several MSFs in
81  *                              a single gadget (as opposed to single
82  *                              MSF in many configurations) you may
83  *                              leave it as NULL (in which case
84  *                              "lun%d" will be used).  In the format
85  *                              you can use "%d" to index LUNs for
86  *                              MSF's with more than one LUN.  (Beware
87  *                              that there is only one integer given
88  *                              as an argument for the format and
89  *                              specifying invalid format may cause
90  *                              unspecified behaviour.)
91  *      thread_name     Name of the kernel thread process used by the
92  *                              MSF.  You can safely set it to NULL
93  *                              (in which case default "file-storage"
94  *                              will be used).
95  *
96  *      vendor_name
97  *      product_name
98  *      release         Information used as a reply to INQUIRY
99  *                              request.  To use default set to NULL,
100  *                              NULL, 0xffff respectively.  The first
101  *                              field should be 8 and the second 16
102  *                              characters or less.
103  *
104  *      can_stall       Set to permit function to halt bulk endpoints.
105  *                              Disabled on some USB devices known not
106  *                              to work correctly.  You should set it
107  *                              to true.
108  *
109  * If "removable" is not set for a LUN then a backing file must be
110  * specified.  If it is set, then NULL filename means the LUN's medium
111  * is not loaded (an empty string as "filename" in the fsg_config
112  * structure causes error).  The CD-ROM emulation includes a single
113  * data track and no audio tracks; hence there need be only one
114  * backing file per LUN.  Note also that the CD-ROM block length is
115  * set to 512 rather than the more common value 2048.
116  *
117  *
118  * MSF includes support for module parameters.  If gadget using it
119  * decides to use it, the following module parameters will be
120  * available:
121  *
122  *      file=filename[,filename...]
123  *                      Names of the files or block devices used for
124  *                              backing storage.
125  *      ro=b[,b...]     Default false, boolean for read-only access.
126  *      removable=b[,b...]
127  *                      Default true, boolean for removable media.
128  *      cdrom=b[,b...]  Default false, boolean for whether to emulate
129  *                              a CD-ROM drive.
130  *      luns=N          Default N = number of filenames, number of
131  *                              LUNs to support.
132  *      stall           Default determined according to the type of
133  *                              USB device controller (usually true),
134  *                              boolean to permit the driver to halt
135  *                              bulk endpoints.
136  *
137  * The module parameters may be prefixed with some string.  You need
138  * to consult gadget's documentation or source to verify whether it is
139  * using those module parameters and if it does what are the prefixes
140  * (look for FSG_MODULE_PARAMETERS() macro usage, what's inside it is
141  * the prefix).
142  *
143  *
144  * Requirements are modest; only a bulk-in and a bulk-out endpoint are
145  * needed.  The memory requirement amounts to two 16K buffers, size
146  * configurable by a parameter.  Support is included for both
147  * full-speed and high-speed operation.
148  *
149  * Note that the driver is slightly non-portable in that it assumes a
150  * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
151  * interrupt-in endpoints.  With most device controllers this isn't an
152  * issue, but there may be some with hardware restrictions that prevent
153  * a buffer from being used by more than one endpoint.
154  *
155  *
156  * The pathnames of the backing files and the ro settings are
157  * available in the attribute files "file" and "ro" in the lun<n> (or
158  * to be more precise in a directory which name comes from
159  * "lun_name_format" option!) subdirectory of the gadget's sysfs
160  * directory.  If the "removable" option is set, writing to these
161  * files will simulate ejecting/loading the medium (writing an empty
162  * line means eject) and adjusting a write-enable tab.  Changes to the
163  * ro setting are not allowed when the medium is loaded or if CD-ROM
164  * emulation is being used.
165  *
166  *
167  * This function is heavily based on "File-backed Storage Gadget" by
168  * Alan Stern which in turn is heavily based on "Gadget Zero" by David
169  * Brownell.  The driver's SCSI command interface was based on the
170  * "Information technology - Small Computer System Interface - 2"
171  * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
172  * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
173  * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
174  * was based on the "Universal Serial Bus Mass Storage Class UFI
175  * Command Specification" document, Revision 1.0, December 14, 1998,
176  * available at
177  * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
178  */
179
180
181 /*
182  *                              Driver Design
183  *
184  * The MSF is fairly straightforward.  There is a main kernel
185  * thread that handles most of the work.  Interrupt routines field
186  * callbacks from the controller driver: bulk- and interrupt-request
187  * completion notifications, endpoint-0 events, and disconnect events.
188  * Completion events are passed to the main thread by wakeup calls.  Many
189  * ep0 requests are handled at interrupt time, but SetInterface,
190  * SetConfiguration, and device reset requests are forwarded to the
191  * thread in the form of "exceptions" using SIGUSR1 signals (since they
192  * should interrupt any ongoing file I/O operations).
193  *
194  * The thread's main routine implements the standard command/data/status
195  * parts of a SCSI interaction.  It and its subroutines are full of tests
196  * for pending signals/exceptions -- all this polling is necessary since
197  * the kernel has no setjmp/longjmp equivalents.  (Maybe this is an
198  * indication that the driver really wants to be running in userspace.)
199  * An important point is that so long as the thread is alive it keeps an
200  * open reference to the backing file.  This will prevent unmounting
201  * the backing file's underlying filesystem and could cause problems
202  * during system shutdown, for example.  To prevent such problems, the
203  * thread catches INT, TERM, and KILL signals and converts them into
204  * an EXIT exception.
205  *
206  * In normal operation the main thread is started during the gadget's
207  * fsg_bind() callback and stopped during fsg_unbind().  But it can
208  * also exit when it receives a signal, and there's no point leaving
209  * the gadget running when the thread is dead.  At of this moment, MSF
210  * provides no way to deregister the gadget when thread dies -- maybe
211  * a callback functions is needed.
212  *
213  * To provide maximum throughput, the driver uses a circular pipeline of
214  * buffer heads (struct fsg_buffhd).  In principle the pipeline can be
215  * arbitrarily long; in practice the benefits don't justify having more
216  * than 2 stages (i.e., double buffering).  But it helps to think of the
217  * pipeline as being a long one.  Each buffer head contains a bulk-in and
218  * a bulk-out request pointer (since the buffer can be used for both
219  * output and input -- directions always are given from the host's
220  * point of view) as well as a pointer to the buffer and various state
221  * variables.
222  *
223  * Use of the pipeline follows a simple protocol.  There is a variable
224  * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
225  * At any time that buffer head may still be in use from an earlier
226  * request, so each buffer head has a state variable indicating whether
227  * it is EMPTY, FULL, or BUSY.  Typical use involves waiting for the
228  * buffer head to be EMPTY, filling the buffer either by file I/O or by
229  * USB I/O (during which the buffer head is BUSY), and marking the buffer
230  * head FULL when the I/O is complete.  Then the buffer will be emptied
231  * (again possibly by USB I/O, during which it is marked BUSY) and
232  * finally marked EMPTY again (possibly by a completion routine).
233  *
234  * A module parameter tells the driver to avoid stalling the bulk
235  * endpoints wherever the transport specification allows.  This is
236  * necessary for some UDCs like the SuperH, which cannot reliably clear a
237  * halt on a bulk endpoint.  However, under certain circumstances the
238  * Bulk-only specification requires a stall.  In such cases the driver
239  * will halt the endpoint and set a flag indicating that it should clear
240  * the halt in software during the next device reset.  Hopefully this
241  * will permit everything to work correctly.  Furthermore, although the
242  * specification allows the bulk-out endpoint to halt when the host sends
243  * too much data, implementing this would cause an unavoidable race.
244  * The driver will always use the "no-stall" approach for OUT transfers.
245  *
246  * One subtle point concerns sending status-stage responses for ep0
247  * requests.  Some of these requests, such as device reset, can involve
248  * interrupting an ongoing file I/O operation, which might take an
249  * arbitrarily long time.  During that delay the host might give up on
250  * the original ep0 request and issue a new one.  When that happens the
251  * driver should not notify the host about completion of the original
252  * request, as the host will no longer be waiting for it.  So the driver
253  * assigns to each ep0 request a unique tag, and it keeps track of the
254  * tag value of the request associated with a long-running exception
255  * (device-reset, interface-change, or configuration-change).  When the
256  * exception handler is finished, the status-stage response is submitted
257  * only if the current ep0 request tag is equal to the exception request
258  * tag.  Thus only the most recently received ep0 request will get a
259  * status-stage response.
260  *
261  * Warning: This driver source file is too long.  It ought to be split up
262  * into a header file plus about 3 separate .c files, to handle the details
263  * of the Gadget, USB Mass Storage, and SCSI protocols.
264  */
265
266
267 /* #define VERBOSE_DEBUG */
268 /* #define DUMP_MSGS */
269
270
271 #include <linux/blkdev.h>
272 #include <linux/completion.h>
273 #include <linux/dcache.h>
274 #include <linux/delay.h>
275 #include <linux/device.h>
276 #include <linux/fcntl.h>
277 #include <linux/file.h>
278 #include <linux/fs.h>
279 #include <linux/kref.h>
280 #include <linux/kthread.h>
281 #include <linux/limits.h>
282 #include <linux/rwsem.h>
283 #include <linux/slab.h>
284 #include <linux/spinlock.h>
285 #include <linux/string.h>
286 #include <linux/freezer.h>
287 #include <linux/utsname.h>
288
289 #include <linux/usb/ch9.h>
290 #include <linux/usb/gadget.h>
291
292 #include "gadget_chips.h"
293
294
295
296 /*------------------------------------------------------------------------*/
297
298 #define FSG_DRIVER_DESC         "Mass Storage Function"
299 #define FSG_DRIVER_VERSION      "2009/09/11"
300
301 static const char fsg_string_interface[] = "Mass Storage";
302
303
304 #define FSG_NO_INTR_EP 1
305 #define FSG_BUFFHD_STATIC_BUFFER 1
306 #define FSG_NO_DEVICE_STRINGS    1
307 #define FSG_NO_OTG               1
308 #define FSG_NO_INTR_EP           1
309
310 #include "storage_common.c"
311
312
313 /*-------------------------------------------------------------------------*/
314
315 struct fsg_dev;
316
317
318 /* Data shared by all the FSG instances. */
319 struct fsg_common {
320         struct usb_gadget       *gadget;
321         struct fsg_dev          *fsg;
322         struct fsg_dev          *prev_fsg;
323
324         /* filesem protects: backing files in use */
325         struct rw_semaphore     filesem;
326
327         /* lock protects: state, all the req_busy's */
328         spinlock_t              lock;
329
330         struct usb_ep           *ep0;           /* Copy of gadget->ep0 */
331         struct usb_request      *ep0req;        /* Copy of cdev->req */
332         unsigned int            ep0_req_tag;
333         const char              *ep0req_name;
334
335         struct fsg_buffhd       *next_buffhd_to_fill;
336         struct fsg_buffhd       *next_buffhd_to_drain;
337         struct fsg_buffhd       buffhds[FSG_NUM_BUFFERS];
338
339         int                     cmnd_size;
340         u8                      cmnd[MAX_COMMAND_SIZE];
341
342         unsigned int            nluns;
343         unsigned int            lun;
344         struct fsg_lun          *luns;
345         struct fsg_lun          *curlun;
346
347         unsigned int            bulk_out_maxpacket;
348         enum fsg_state          state;          /* For exception handling */
349         unsigned int            exception_req_tag;
350
351         u8                      config, new_config;
352         enum data_direction     data_dir;
353         u32                     data_size;
354         u32                     data_size_from_cmnd;
355         u32                     tag;
356         u32                     residue;
357         u32                     usb_amount_left;
358
359         unsigned int            can_stall:1;
360         unsigned int            free_storage_on_release:1;
361         unsigned int            phase_error:1;
362         unsigned int            short_packet_received:1;
363         unsigned int            bad_lun_okay:1;
364         unsigned int            running:1;
365
366         int                     thread_wakeup_needed;
367         struct completion       thread_notifier;
368         struct task_struct      *thread_task;
369
370         /* Callback function to call when thread exits. */
371         int                     (*thread_exits)(struct fsg_common *common);
372         /* Gadget's private data. */
373         void                    *private_data;
374
375         /* Vendor (8 chars), product (16 chars), release (4
376          * hexadecimal digits) and NUL byte */
377         char inquiry_string[8 + 16 + 4 + 1];
378
379         struct kref             ref;
380 };
381
382
383 struct fsg_config {
384         unsigned nluns;
385         struct fsg_lun_config {
386                 const char *filename;
387                 char ro;
388                 char removable;
389                 char cdrom;
390         } luns[FSG_MAX_LUNS];
391
392         const char              *lun_name_format;
393         const char              *thread_name;
394
395         /* Callback function to call when thread exits.  If no
396          * callback is set or it returns value lower then zero MSF
397          * will force eject all LUNs it operates on (including those
398          * marked as non-removable or with prevent_medium_removal flag
399          * set). */
400         int                     (*thread_exits)(struct fsg_common *common);
401         /* Gadget's private data. */
402         void                    *private_data;
403
404         const char *vendor_name;                /*  8 characters or less */
405         const char *product_name;               /* 16 characters or less */
406         u16 release;
407
408         char                    can_stall;
409 };
410
411
412 struct fsg_dev {
413         struct usb_function     function;
414         struct usb_gadget       *gadget;        /* Copy of cdev->gadget */
415         struct fsg_common       *common;
416
417         u16                     interface_number;
418
419         unsigned int            bulk_in_enabled:1;
420         unsigned int            bulk_out_enabled:1;
421
422         unsigned long           atomic_bitflags;
423 #define IGNORE_BULK_OUT         0
424
425         struct usb_ep           *bulk_in;
426         struct usb_ep           *bulk_out;
427 };
428
429
430 static inline int __fsg_is_set(struct fsg_common *common,
431                                const char *func, unsigned line)
432 {
433         if (common->fsg)
434                 return 1;
435         ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
436         return 0;
437 }
438
439 #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
440
441
442 static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
443 {
444         return container_of(f, struct fsg_dev, function);
445 }
446
447
448 typedef void (*fsg_routine_t)(struct fsg_dev *);
449
450 static int exception_in_progress(struct fsg_common *common)
451 {
452         return common->state > FSG_STATE_IDLE;
453 }
454
455 /* Make bulk-out requests be divisible by the maxpacket size */
456 static void set_bulk_out_req_length(struct fsg_common *common,
457                 struct fsg_buffhd *bh, unsigned int length)
458 {
459         unsigned int    rem;
460
461         bh->bulk_out_intended_length = length;
462         rem = length % common->bulk_out_maxpacket;
463         if (rem > 0)
464                 length += common->bulk_out_maxpacket - rem;
465         bh->outreq->length = length;
466 }
467
468 /*-------------------------------------------------------------------------*/
469
470 static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
471 {
472         const char      *name;
473
474         if (ep == fsg->bulk_in)
475                 name = "bulk-in";
476         else if (ep == fsg->bulk_out)
477                 name = "bulk-out";
478         else
479                 name = ep->name;
480         DBG(fsg, "%s set halt\n", name);
481         return usb_ep_set_halt(ep);
482 }
483
484
485 /*-------------------------------------------------------------------------*/
486
487 /* These routines may be called in process context or in_irq */
488
489 /* Caller must hold fsg->lock */
490 static void wakeup_thread(struct fsg_common *common)
491 {
492         /* Tell the main thread that something has happened */
493         common->thread_wakeup_needed = 1;
494         if (common->thread_task)
495                 wake_up_process(common->thread_task);
496 }
497
498
499 static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
500 {
501         unsigned long           flags;
502
503         /* Do nothing if a higher-priority exception is already in progress.
504          * If a lower-or-equal priority exception is in progress, preempt it
505          * and notify the main thread by sending it a signal. */
506         spin_lock_irqsave(&common->lock, flags);
507         if (common->state <= new_state) {
508                 common->exception_req_tag = common->ep0_req_tag;
509                 common->state = new_state;
510                 if (common->thread_task)
511                         send_sig_info(SIGUSR1, SEND_SIG_FORCED,
512                                       common->thread_task);
513         }
514         spin_unlock_irqrestore(&common->lock, flags);
515 }
516
517
518 /*-------------------------------------------------------------------------*/
519
520 static int ep0_queue(struct fsg_common *common)
521 {
522         int     rc;
523
524         rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
525         common->ep0->driver_data = common;
526         if (rc != 0 && rc != -ESHUTDOWN) {
527                 /* We can't do much more than wait for a reset */
528                 WARNING(common, "error in submission: %s --> %d\n",
529                         common->ep0->name, rc);
530         }
531         return rc;
532 }
533
534 /*-------------------------------------------------------------------------*/
535
536 /* Bulk and interrupt endpoint completion handlers.
537  * These always run in_irq. */
538
539 static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
540 {
541         struct fsg_common       *common = ep->driver_data;
542         struct fsg_buffhd       *bh = req->context;
543
544         if (req->status || req->actual != req->length)
545                 DBG(common, "%s --> %d, %u/%u\n", __func__,
546                                 req->status, req->actual, req->length);
547         if (req->status == -ECONNRESET)         /* Request was cancelled */
548                 usb_ep_fifo_flush(ep);
549
550         /* Hold the lock while we update the request and buffer states */
551         smp_wmb();
552         spin_lock(&common->lock);
553         bh->inreq_busy = 0;
554         bh->state = BUF_STATE_EMPTY;
555         wakeup_thread(common);
556         spin_unlock(&common->lock);
557 }
558
559 static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
560 {
561         struct fsg_common       *common = ep->driver_data;
562         struct fsg_buffhd       *bh = req->context;
563
564         dump_msg(common, "bulk-out", req->buf, req->actual);
565         if (req->status || req->actual != bh->bulk_out_intended_length)
566                 DBG(common, "%s --> %d, %u/%u\n", __func__,
567                                 req->status, req->actual,
568                                 bh->bulk_out_intended_length);
569         if (req->status == -ECONNRESET)         /* Request was cancelled */
570                 usb_ep_fifo_flush(ep);
571
572         /* Hold the lock while we update the request and buffer states */
573         smp_wmb();
574         spin_lock(&common->lock);
575         bh->outreq_busy = 0;
576         bh->state = BUF_STATE_FULL;
577         wakeup_thread(common);
578         spin_unlock(&common->lock);
579 }
580
581
582 /*-------------------------------------------------------------------------*/
583
584 /* Ep0 class-specific handlers.  These always run in_irq. */
585
586 static int fsg_setup(struct usb_function *f,
587                 const struct usb_ctrlrequest *ctrl)
588 {
589         struct fsg_dev          *fsg = fsg_from_func(f);
590         struct usb_request      *req = fsg->common->ep0req;
591         u16                     w_index = le16_to_cpu(ctrl->wIndex);
592         u16                     w_value = le16_to_cpu(ctrl->wValue);
593         u16                     w_length = le16_to_cpu(ctrl->wLength);
594
595         if (!fsg->common->config)
596                 return -EOPNOTSUPP;
597
598         switch (ctrl->bRequest) {
599
600         case USB_BULK_RESET_REQUEST:
601                 if (ctrl->bRequestType !=
602                     (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
603                         break;
604                 if (w_index != fsg->interface_number || w_value != 0)
605                         return -EDOM;
606
607                 /* Raise an exception to stop the current operation
608                  * and reinitialize our state. */
609                 DBG(fsg, "bulk reset request\n");
610                 raise_exception(fsg->common, FSG_STATE_RESET);
611                 return DELAYED_STATUS;
612
613         case USB_BULK_GET_MAX_LUN_REQUEST:
614                 if (ctrl->bRequestType !=
615                     (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
616                         break;
617                 if (w_index != fsg->interface_number || w_value != 0)
618                         return -EDOM;
619                 VDBG(fsg, "get max LUN\n");
620                 *(u8 *) req->buf = fsg->common->nluns - 1;
621
622                 /* Respond with data/status */
623                 req->length = min(1, w_length);
624                 fsg->common->ep0req_name =
625                         ctrl->bRequestType & USB_DIR_IN ? "ep0-in" : "ep0-out";
626                 return ep0_queue(fsg->common);
627         }
628
629         VDBG(fsg,
630              "unknown class-specific control req "
631              "%02x.%02x v%04x i%04x l%u\n",
632              ctrl->bRequestType, ctrl->bRequest,
633              le16_to_cpu(ctrl->wValue), w_index, w_length);
634         return -EOPNOTSUPP;
635 }
636
637
638 /*-------------------------------------------------------------------------*/
639
640 /* All the following routines run in process context */
641
642
643 /* Use this for bulk or interrupt transfers, not ep0 */
644 static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
645                 struct usb_request *req, int *pbusy,
646                 enum fsg_buffer_state *state)
647 {
648         int     rc;
649
650         if (ep == fsg->bulk_in)
651                 dump_msg(fsg, "bulk-in", req->buf, req->length);
652
653         spin_lock_irq(&fsg->common->lock);
654         *pbusy = 1;
655         *state = BUF_STATE_BUSY;
656         spin_unlock_irq(&fsg->common->lock);
657         rc = usb_ep_queue(ep, req, GFP_KERNEL);
658         if (rc != 0) {
659                 *pbusy = 0;
660                 *state = BUF_STATE_EMPTY;
661
662                 /* We can't do much more than wait for a reset */
663
664                 /* Note: currently the net2280 driver fails zero-length
665                  * submissions if DMA is enabled. */
666                 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
667                                                 req->length == 0))
668                         WARNING(fsg, "error in submission: %s --> %d\n",
669                                         ep->name, rc);
670         }
671 }
672
673 #define START_TRANSFER_OR(common, ep_name, req, pbusy, state)           \
674         if (fsg_is_set(common))                                         \
675                 start_transfer((common)->fsg, (common)->fsg->ep_name,   \
676                                req, pbusy, state);                      \
677         else
678
679 #define START_TRANSFER(common, ep_name, req, pbusy, state)              \
680         START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
681
682
683
684 static int sleep_thread(struct fsg_common *common)
685 {
686         int     rc = 0;
687
688         /* Wait until a signal arrives or we are woken up */
689         for (;;) {
690                 try_to_freeze();
691                 set_current_state(TASK_INTERRUPTIBLE);
692                 if (signal_pending(current)) {
693                         rc = -EINTR;
694                         break;
695                 }
696                 if (common->thread_wakeup_needed)
697                         break;
698                 schedule();
699         }
700         __set_current_state(TASK_RUNNING);
701         common->thread_wakeup_needed = 0;
702         return rc;
703 }
704
705
706 /*-------------------------------------------------------------------------*/
707
708 static int do_read(struct fsg_common *common)
709 {
710         struct fsg_lun          *curlun = common->curlun;
711         u32                     lba;
712         struct fsg_buffhd       *bh;
713         int                     rc;
714         u32                     amount_left;
715         loff_t                  file_offset, file_offset_tmp;
716         unsigned int            amount;
717         unsigned int            partial_page;
718         ssize_t                 nread;
719
720         /* Get the starting Logical Block Address and check that it's
721          * not too big */
722         if (common->cmnd[0] == SC_READ_6)
723                 lba = get_unaligned_be24(&common->cmnd[1]);
724         else {
725                 lba = get_unaligned_be32(&common->cmnd[2]);
726
727                 /* We allow DPO (Disable Page Out = don't save data in the
728                  * cache) and FUA (Force Unit Access = don't read from the
729                  * cache), but we don't implement them. */
730                 if ((common->cmnd[1] & ~0x18) != 0) {
731                         curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
732                         return -EINVAL;
733                 }
734         }
735         if (lba >= curlun->num_sectors) {
736                 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
737                 return -EINVAL;
738         }
739         file_offset = ((loff_t) lba) << 9;
740
741         /* Carry out the file reads */
742         amount_left = common->data_size_from_cmnd;
743         if (unlikely(amount_left == 0))
744                 return -EIO;            /* No default reply */
745
746         for (;;) {
747
748                 /* Figure out how much we need to read:
749                  * Try to read the remaining amount.
750                  * But don't read more than the buffer size.
751                  * And don't try to read past the end of the file.
752                  * Finally, if we're not at a page boundary, don't read past
753                  *      the next page.
754                  * If this means reading 0 then we were asked to read past
755                  *      the end of file. */
756                 amount = min(amount_left, FSG_BUFLEN);
757                 amount = min((loff_t) amount,
758                                 curlun->file_length - file_offset);
759                 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
760                 if (partial_page > 0)
761                         amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
762                                         partial_page);
763
764                 /* Wait for the next buffer to become available */
765                 bh = common->next_buffhd_to_fill;
766                 while (bh->state != BUF_STATE_EMPTY) {
767                         rc = sleep_thread(common);
768                         if (rc)
769                                 return rc;
770                 }
771
772                 /* If we were asked to read past the end of file,
773                  * end with an empty buffer. */
774                 if (amount == 0) {
775                         curlun->sense_data =
776                                         SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
777                         curlun->sense_data_info = file_offset >> 9;
778                         curlun->info_valid = 1;
779                         bh->inreq->length = 0;
780                         bh->state = BUF_STATE_FULL;
781                         break;
782                 }
783
784                 /* Perform the read */
785                 file_offset_tmp = file_offset;
786                 nread = vfs_read(curlun->filp,
787                                 (char __user *) bh->buf,
788                                 amount, &file_offset_tmp);
789                 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
790                                 (unsigned long long) file_offset,
791                                 (int) nread);
792                 if (signal_pending(current))
793                         return -EINTR;
794
795                 if (nread < 0) {
796                         LDBG(curlun, "error in file read: %d\n",
797                                         (int) nread);
798                         nread = 0;
799                 } else if (nread < amount) {
800                         LDBG(curlun, "partial file read: %d/%u\n",
801                                         (int) nread, amount);
802                         nread -= (nread & 511); /* Round down to a block */
803                 }
804                 file_offset  += nread;
805                 amount_left  -= nread;
806                 common->residue -= nread;
807                 bh->inreq->length = nread;
808                 bh->state = BUF_STATE_FULL;
809
810                 /* If an error occurred, report it and its position */
811                 if (nread < amount) {
812                         curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
813                         curlun->sense_data_info = file_offset >> 9;
814                         curlun->info_valid = 1;
815                         break;
816                 }
817
818                 if (amount_left == 0)
819                         break;          /* No more left to read */
820
821                 /* Send this buffer and go read some more */
822                 bh->inreq->zero = 0;
823                 START_TRANSFER_OR(common, bulk_in, bh->inreq,
824                                &bh->inreq_busy, &bh->state)
825                         /* Don't know what to do if
826                          * common->fsg is NULL */
827                         return -EIO;
828                 common->next_buffhd_to_fill = bh->next;
829         }
830
831         return -EIO;            /* No default reply */
832 }
833
834
835 /*-------------------------------------------------------------------------*/
836
837 static int do_write(struct fsg_common *common)
838 {
839         struct fsg_lun          *curlun = common->curlun;
840         u32                     lba;
841         struct fsg_buffhd       *bh;
842         int                     get_some_more;
843         u32                     amount_left_to_req, amount_left_to_write;
844         loff_t                  usb_offset, file_offset, file_offset_tmp;
845         unsigned int            amount;
846         unsigned int            partial_page;
847         ssize_t                 nwritten;
848         int                     rc;
849
850         if (curlun->ro) {
851                 curlun->sense_data = SS_WRITE_PROTECTED;
852                 return -EINVAL;
853         }
854         spin_lock(&curlun->filp->f_lock);
855         curlun->filp->f_flags &= ~O_SYNC;       /* Default is not to wait */
856         spin_unlock(&curlun->filp->f_lock);
857
858         /* Get the starting Logical Block Address and check that it's
859          * not too big */
860         if (common->cmnd[0] == SC_WRITE_6)
861                 lba = get_unaligned_be24(&common->cmnd[1]);
862         else {
863                 lba = get_unaligned_be32(&common->cmnd[2]);
864
865                 /* We allow DPO (Disable Page Out = don't save data in the
866                  * cache) and FUA (Force Unit Access = write directly to the
867                  * medium).  We don't implement DPO; we implement FUA by
868                  * performing synchronous output. */
869                 if (common->cmnd[1] & ~0x18) {
870                         curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
871                         return -EINVAL;
872                 }
873                 if (common->cmnd[1] & 0x08) {   /* FUA */
874                         spin_lock(&curlun->filp->f_lock);
875                         curlun->filp->f_flags |= O_SYNC;
876                         spin_unlock(&curlun->filp->f_lock);
877                 }
878         }
879         if (lba >= curlun->num_sectors) {
880                 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
881                 return -EINVAL;
882         }
883
884         /* Carry out the file writes */
885         get_some_more = 1;
886         file_offset = usb_offset = ((loff_t) lba) << 9;
887         amount_left_to_req = common->data_size_from_cmnd;
888         amount_left_to_write = common->data_size_from_cmnd;
889
890         while (amount_left_to_write > 0) {
891
892                 /* Queue a request for more data from the host */
893                 bh = common->next_buffhd_to_fill;
894                 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
895
896                         /* Figure out how much we want to get:
897                          * Try to get the remaining amount.
898                          * But don't get more than the buffer size.
899                          * And don't try to go past the end of the file.
900                          * If we're not at a page boundary,
901                          *      don't go past the next page.
902                          * If this means getting 0, then we were asked
903                          *      to write past the end of file.
904                          * Finally, round down to a block boundary. */
905                         amount = min(amount_left_to_req, FSG_BUFLEN);
906                         amount = min((loff_t) amount, curlun->file_length -
907                                         usb_offset);
908                         partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
909                         if (partial_page > 0)
910                                 amount = min(amount,
911         (unsigned int) PAGE_CACHE_SIZE - partial_page);
912
913                         if (amount == 0) {
914                                 get_some_more = 0;
915                                 curlun->sense_data =
916                                         SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
917                                 curlun->sense_data_info = usb_offset >> 9;
918                                 curlun->info_valid = 1;
919                                 continue;
920                         }
921                         amount -= (amount & 511);
922                         if (amount == 0) {
923
924                                 /* Why were we were asked to transfer a
925                                  * partial block? */
926                                 get_some_more = 0;
927                                 continue;
928                         }
929
930                         /* Get the next buffer */
931                         usb_offset += amount;
932                         common->usb_amount_left -= amount;
933                         amount_left_to_req -= amount;
934                         if (amount_left_to_req == 0)
935                                 get_some_more = 0;
936
937                         /* amount is always divisible by 512, hence by
938                          * the bulk-out maxpacket size */
939                         bh->outreq->length = amount;
940                         bh->bulk_out_intended_length = amount;
941                         bh->outreq->short_not_ok = 1;
942                         START_TRANSFER_OR(common, bulk_out, bh->outreq,
943                                           &bh->outreq_busy, &bh->state)
944                                 /* Don't know what to do if
945                                  * common->fsg is NULL */
946                                 return -EIO;
947                         common->next_buffhd_to_fill = bh->next;
948                         continue;
949                 }
950
951                 /* Write the received data to the backing file */
952                 bh = common->next_buffhd_to_drain;
953                 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
954                         break;                  /* We stopped early */
955                 if (bh->state == BUF_STATE_FULL) {
956                         smp_rmb();
957                         common->next_buffhd_to_drain = bh->next;
958                         bh->state = BUF_STATE_EMPTY;
959
960                         /* Did something go wrong with the transfer? */
961                         if (bh->outreq->status != 0) {
962                                 curlun->sense_data = SS_COMMUNICATION_FAILURE;
963                                 curlun->sense_data_info = file_offset >> 9;
964                                 curlun->info_valid = 1;
965                                 break;
966                         }
967
968                         amount = bh->outreq->actual;
969                         if (curlun->file_length - file_offset < amount) {
970                                 LERROR(curlun,
971         "write %u @ %llu beyond end %llu\n",
972         amount, (unsigned long long) file_offset,
973         (unsigned long long) curlun->file_length);
974                                 amount = curlun->file_length - file_offset;
975                         }
976
977                         /* Perform the write */
978                         file_offset_tmp = file_offset;
979                         nwritten = vfs_write(curlun->filp,
980                                         (char __user *) bh->buf,
981                                         amount, &file_offset_tmp);
982                         VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
983                                         (unsigned long long) file_offset,
984                                         (int) nwritten);
985                         if (signal_pending(current))
986                                 return -EINTR;          /* Interrupted! */
987
988                         if (nwritten < 0) {
989                                 LDBG(curlun, "error in file write: %d\n",
990                                                 (int) nwritten);
991                                 nwritten = 0;
992                         } else if (nwritten < amount) {
993                                 LDBG(curlun, "partial file write: %d/%u\n",
994                                                 (int) nwritten, amount);
995                                 nwritten -= (nwritten & 511);
996                                 /* Round down to a block */
997                         }
998                         file_offset += nwritten;
999                         amount_left_to_write -= nwritten;
1000                         common->residue -= nwritten;
1001
1002                         /* If an error occurred, report it and its position */
1003                         if (nwritten < amount) {
1004                                 curlun->sense_data = SS_WRITE_ERROR;
1005                                 curlun->sense_data_info = file_offset >> 9;
1006                                 curlun->info_valid = 1;
1007                                 break;
1008                         }
1009
1010                         /* Did the host decide to stop early? */
1011                         if (bh->outreq->actual != bh->outreq->length) {
1012                                 common->short_packet_received = 1;
1013                                 break;
1014                         }
1015                         continue;
1016                 }
1017
1018                 /* Wait for something to happen */
1019                 rc = sleep_thread(common);
1020                 if (rc)
1021                         return rc;
1022         }
1023
1024         return -EIO;            /* No default reply */
1025 }
1026
1027
1028 /*-------------------------------------------------------------------------*/
1029
1030 static int do_synchronize_cache(struct fsg_common *common)
1031 {
1032         struct fsg_lun  *curlun = common->curlun;
1033         int             rc;
1034
1035         /* We ignore the requested LBA and write out all file's
1036          * dirty data buffers. */
1037         rc = fsg_lun_fsync_sub(curlun);
1038         if (rc)
1039                 curlun->sense_data = SS_WRITE_ERROR;
1040         return 0;
1041 }
1042
1043
1044 /*-------------------------------------------------------------------------*/
1045
1046 static void invalidate_sub(struct fsg_lun *curlun)
1047 {
1048         struct file     *filp = curlun->filp;
1049         struct inode    *inode = filp->f_path.dentry->d_inode;
1050         unsigned long   rc;
1051
1052         rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
1053         VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc);
1054 }
1055
1056 static int do_verify(struct fsg_common *common)
1057 {
1058         struct fsg_lun          *curlun = common->curlun;
1059         u32                     lba;
1060         u32                     verification_length;
1061         struct fsg_buffhd       *bh = common->next_buffhd_to_fill;
1062         loff_t                  file_offset, file_offset_tmp;
1063         u32                     amount_left;
1064         unsigned int            amount;
1065         ssize_t                 nread;
1066
1067         /* Get the starting Logical Block Address and check that it's
1068          * not too big */
1069         lba = get_unaligned_be32(&common->cmnd[2]);
1070         if (lba >= curlun->num_sectors) {
1071                 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1072                 return -EINVAL;
1073         }
1074
1075         /* We allow DPO (Disable Page Out = don't save data in the
1076          * cache) but we don't implement it. */
1077         if (common->cmnd[1] & ~0x10) {
1078                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1079                 return -EINVAL;
1080         }
1081
1082         verification_length = get_unaligned_be16(&common->cmnd[7]);
1083         if (unlikely(verification_length == 0))
1084                 return -EIO;            /* No default reply */
1085
1086         /* Prepare to carry out the file verify */
1087         amount_left = verification_length << 9;
1088         file_offset = ((loff_t) lba) << 9;
1089
1090         /* Write out all the dirty buffers before invalidating them */
1091         fsg_lun_fsync_sub(curlun);
1092         if (signal_pending(current))
1093                 return -EINTR;
1094
1095         invalidate_sub(curlun);
1096         if (signal_pending(current))
1097                 return -EINTR;
1098
1099         /* Just try to read the requested blocks */
1100         while (amount_left > 0) {
1101
1102                 /* Figure out how much we need to read:
1103                  * Try to read the remaining amount, but not more than
1104                  * the buffer size.
1105                  * And don't try to read past the end of the file.
1106                  * If this means reading 0 then we were asked to read
1107                  * past the end of file. */
1108                 amount = min(amount_left, FSG_BUFLEN);
1109                 amount = min((loff_t) amount,
1110                                 curlun->file_length - file_offset);
1111                 if (amount == 0) {
1112                         curlun->sense_data =
1113                                         SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1114                         curlun->sense_data_info = file_offset >> 9;
1115                         curlun->info_valid = 1;
1116                         break;
1117                 }
1118
1119                 /* Perform the read */
1120                 file_offset_tmp = file_offset;
1121                 nread = vfs_read(curlun->filp,
1122                                 (char __user *) bh->buf,
1123                                 amount, &file_offset_tmp);
1124                 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1125                                 (unsigned long long) file_offset,
1126                                 (int) nread);
1127                 if (signal_pending(current))
1128                         return -EINTR;
1129
1130                 if (nread < 0) {
1131                         LDBG(curlun, "error in file verify: %d\n",
1132                                         (int) nread);
1133                         nread = 0;
1134                 } else if (nread < amount) {
1135                         LDBG(curlun, "partial file verify: %d/%u\n",
1136                                         (int) nread, amount);
1137                         nread -= (nread & 511); /* Round down to a sector */
1138                 }
1139                 if (nread == 0) {
1140                         curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1141                         curlun->sense_data_info = file_offset >> 9;
1142                         curlun->info_valid = 1;
1143                         break;
1144                 }
1145                 file_offset += nread;
1146                 amount_left -= nread;
1147         }
1148         return 0;
1149 }
1150
1151
1152 /*-------------------------------------------------------------------------*/
1153
1154 static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1155 {
1156         struct fsg_lun *curlun = common->curlun;
1157         u8      *buf = (u8 *) bh->buf;
1158
1159         if (!curlun) {          /* Unsupported LUNs are okay */
1160                 common->bad_lun_okay = 1;
1161                 memset(buf, 0, 36);
1162                 buf[0] = 0x7f;          /* Unsupported, no device-type */
1163                 buf[4] = 31;            /* Additional length */
1164                 return 36;
1165         }
1166
1167         buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK;
1168         buf[1] = curlun->removable ? 0x80 : 0;
1169         buf[2] = 2;             /* ANSI SCSI level 2 */
1170         buf[3] = 2;             /* SCSI-2 INQUIRY data format */
1171         buf[4] = 31;            /* Additional length */
1172         buf[5] = 0;             /* No special options */
1173         buf[6] = 0;
1174         buf[7] = 0;
1175         memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);
1176         return 36;
1177 }
1178
1179
1180 static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1181 {
1182         struct fsg_lun  *curlun = common->curlun;
1183         u8              *buf = (u8 *) bh->buf;
1184         u32             sd, sdinfo;
1185         int             valid;
1186
1187         /*
1188          * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1189          *
1190          * If a REQUEST SENSE command is received from an initiator
1191          * with a pending unit attention condition (before the target
1192          * generates the contingent allegiance condition), then the
1193          * target shall either:
1194          *   a) report any pending sense data and preserve the unit
1195          *      attention condition on the logical unit, or,
1196          *   b) report the unit attention condition, may discard any
1197          *      pending sense data, and clear the unit attention
1198          *      condition on the logical unit for that initiator.
1199          *
1200          * FSG normally uses option a); enable this code to use option b).
1201          */
1202 #if 0
1203         if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1204                 curlun->sense_data = curlun->unit_attention_data;
1205                 curlun->unit_attention_data = SS_NO_SENSE;
1206         }
1207 #endif
1208
1209         if (!curlun) {          /* Unsupported LUNs are okay */
1210                 common->bad_lun_okay = 1;
1211                 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1212                 sdinfo = 0;
1213                 valid = 0;
1214         } else {
1215                 sd = curlun->sense_data;
1216                 sdinfo = curlun->sense_data_info;
1217                 valid = curlun->info_valid << 7;
1218                 curlun->sense_data = SS_NO_SENSE;
1219                 curlun->sense_data_info = 0;
1220                 curlun->info_valid = 0;
1221         }
1222
1223         memset(buf, 0, 18);
1224         buf[0] = valid | 0x70;                  /* Valid, current error */
1225         buf[2] = SK(sd);
1226         put_unaligned_be32(sdinfo, &buf[3]);    /* Sense information */
1227         buf[7] = 18 - 8;                        /* Additional sense length */
1228         buf[12] = ASC(sd);
1229         buf[13] = ASCQ(sd);
1230         return 18;
1231 }
1232
1233
1234 static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1235 {
1236         struct fsg_lun  *curlun = common->curlun;
1237         u32             lba = get_unaligned_be32(&common->cmnd[2]);
1238         int             pmi = common->cmnd[8];
1239         u8              *buf = (u8 *) bh->buf;
1240
1241         /* Check the PMI and LBA fields */
1242         if (pmi > 1 || (pmi == 0 && lba != 0)) {
1243                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1244                 return -EINVAL;
1245         }
1246
1247         put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1248                                                 /* Max logical block */
1249         put_unaligned_be32(512, &buf[4]);       /* Block length */
1250         return 8;
1251 }
1252
1253
1254 static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
1255 {
1256         struct fsg_lun  *curlun = common->curlun;
1257         int             msf = common->cmnd[1] & 0x02;
1258         u32             lba = get_unaligned_be32(&common->cmnd[2]);
1259         u8              *buf = (u8 *) bh->buf;
1260
1261         if (common->cmnd[1] & ~0x02) {          /* Mask away MSF */
1262                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1263                 return -EINVAL;
1264         }
1265         if (lba >= curlun->num_sectors) {
1266                 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1267                 return -EINVAL;
1268         }
1269
1270         memset(buf, 0, 8);
1271         buf[0] = 0x01;          /* 2048 bytes of user data, rest is EC */
1272         store_cdrom_address(&buf[4], msf, lba);
1273         return 8;
1274 }
1275
1276
1277 static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
1278 {
1279         struct fsg_lun  *curlun = common->curlun;
1280         int             msf = common->cmnd[1] & 0x02;
1281         int             start_track = common->cmnd[6];
1282         u8              *buf = (u8 *) bh->buf;
1283
1284         if ((common->cmnd[1] & ~0x02) != 0 ||   /* Mask away MSF */
1285                         start_track > 1) {
1286                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1287                 return -EINVAL;
1288         }
1289
1290         memset(buf, 0, 20);
1291         buf[1] = (20-2);                /* TOC data length */
1292         buf[2] = 1;                     /* First track number */
1293         buf[3] = 1;                     /* Last track number */
1294         buf[5] = 0x16;                  /* Data track, copying allowed */
1295         buf[6] = 0x01;                  /* Only track is number 1 */
1296         store_cdrom_address(&buf[8], msf, 0);
1297
1298         buf[13] = 0x16;                 /* Lead-out track is data */
1299         buf[14] = 0xAA;                 /* Lead-out track number */
1300         store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1301         return 20;
1302 }
1303
1304
1305 static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1306 {
1307         struct fsg_lun  *curlun = common->curlun;
1308         int             mscmnd = common->cmnd[0];
1309         u8              *buf = (u8 *) bh->buf;
1310         u8              *buf0 = buf;
1311         int             pc, page_code;
1312         int             changeable_values, all_pages;
1313         int             valid_page = 0;
1314         int             len, limit;
1315
1316         if ((common->cmnd[1] & ~0x08) != 0) {   /* Mask away DBD */
1317                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1318                 return -EINVAL;
1319         }
1320         pc = common->cmnd[2] >> 6;
1321         page_code = common->cmnd[2] & 0x3f;
1322         if (pc == 3) {
1323                 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1324                 return -EINVAL;
1325         }
1326         changeable_values = (pc == 1);
1327         all_pages = (page_code == 0x3f);
1328
1329         /* Write the mode parameter header.  Fixed values are: default
1330          * medium type, no cache control (DPOFUA), and no block descriptors.
1331          * The only variable value is the WriteProtect bit.  We will fill in
1332          * the mode data length later. */
1333         memset(buf, 0, 8);
1334         if (mscmnd == SC_MODE_SENSE_6) {
1335                 buf[2] = (curlun->ro ? 0x80 : 0x00);            /* WP, DPOFUA */
1336                 buf += 4;
1337                 limit = 255;
1338         } else {                        /* SC_MODE_SENSE_10 */
1339                 buf[3] = (curlun->ro ? 0x80 : 0x00);            /* WP, DPOFUA */
1340                 buf += 8;
1341                 limit = 65535;          /* Should really be FSG_BUFLEN */
1342         }
1343
1344         /* No block descriptors */
1345
1346         /* The mode pages, in numerical order.  The only page we support
1347          * is the Caching page. */
1348         if (page_code == 0x08 || all_pages) {
1349                 valid_page = 1;
1350                 buf[0] = 0x08;          /* Page code */
1351                 buf[1] = 10;            /* Page length */
1352                 memset(buf+2, 0, 10);   /* None of the fields are changeable */
1353
1354                 if (!changeable_values) {
1355                         buf[2] = 0x04;  /* Write cache enable, */
1356                                         /* Read cache not disabled */
1357                                         /* No cache retention priorities */
1358                         put_unaligned_be16(0xffff, &buf[4]);
1359                                         /* Don't disable prefetch */
1360                                         /* Minimum prefetch = 0 */
1361                         put_unaligned_be16(0xffff, &buf[8]);
1362                                         /* Maximum prefetch */
1363                         put_unaligned_be16(0xffff, &buf[10]);
1364                                         /* Maximum prefetch ceiling */
1365                 }
1366                 buf += 12;
1367         }
1368
1369         /* Check that a valid page was requested and the mode data length
1370          * isn't too long. */
1371         len = buf - buf0;
1372         if (!valid_page || len > limit) {
1373                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1374                 return -EINVAL;
1375         }
1376
1377         /*  Store the mode data length */
1378         if (mscmnd == SC_MODE_SENSE_6)
1379                 buf0[0] = len - 1;
1380         else
1381                 put_unaligned_be16(len - 2, buf0);
1382         return len;
1383 }
1384
1385
1386 static int do_start_stop(struct fsg_common *common)
1387 {
1388         if (!common->curlun) {
1389                 return -EINVAL;
1390         } else if (!common->curlun->removable) {
1391                 common->curlun->sense_data = SS_INVALID_COMMAND;
1392                 return -EINVAL;
1393         }
1394         return 0;
1395 }
1396
1397
1398 static int do_prevent_allow(struct fsg_common *common)
1399 {
1400         struct fsg_lun  *curlun = common->curlun;
1401         int             prevent;
1402
1403         if (!common->curlun) {
1404                 return -EINVAL;
1405         } else if (!common->curlun->removable) {
1406                 common->curlun->sense_data = SS_INVALID_COMMAND;
1407                 return -EINVAL;
1408         }
1409
1410         prevent = common->cmnd[4] & 0x01;
1411         if ((common->cmnd[4] & ~0x01) != 0) {   /* Mask away Prevent */
1412                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1413                 return -EINVAL;
1414         }
1415
1416         if (curlun->prevent_medium_removal && !prevent)
1417                 fsg_lun_fsync_sub(curlun);
1418         curlun->prevent_medium_removal = prevent;
1419         return 0;
1420 }
1421
1422
1423 static int do_read_format_capacities(struct fsg_common *common,
1424                         struct fsg_buffhd *bh)
1425 {
1426         struct fsg_lun  *curlun = common->curlun;
1427         u8              *buf = (u8 *) bh->buf;
1428
1429         buf[0] = buf[1] = buf[2] = 0;
1430         buf[3] = 8;     /* Only the Current/Maximum Capacity Descriptor */
1431         buf += 4;
1432
1433         put_unaligned_be32(curlun->num_sectors, &buf[0]);
1434                                                 /* Number of blocks */
1435         put_unaligned_be32(512, &buf[4]);       /* Block length */
1436         buf[4] = 0x02;                          /* Current capacity */
1437         return 12;
1438 }
1439
1440
1441 static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
1442 {
1443         struct fsg_lun  *curlun = common->curlun;
1444
1445         /* We don't support MODE SELECT */
1446         if (curlun)
1447                 curlun->sense_data = SS_INVALID_COMMAND;
1448         return -EINVAL;
1449 }
1450
1451
1452 /*-------------------------------------------------------------------------*/
1453
1454 static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1455 {
1456         int     rc;
1457
1458         rc = fsg_set_halt(fsg, fsg->bulk_in);
1459         if (rc == -EAGAIN)
1460                 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1461         while (rc != 0) {
1462                 if (rc != -EAGAIN) {
1463                         WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1464                         rc = 0;
1465                         break;
1466                 }
1467
1468                 /* Wait for a short time and then try again */
1469                 if (msleep_interruptible(100) != 0)
1470                         return -EINTR;
1471                 rc = usb_ep_set_halt(fsg->bulk_in);
1472         }
1473         return rc;
1474 }
1475
1476 static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1477 {
1478         int     rc;
1479
1480         DBG(fsg, "bulk-in set wedge\n");
1481         rc = usb_ep_set_wedge(fsg->bulk_in);
1482         if (rc == -EAGAIN)
1483                 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1484         while (rc != 0) {
1485                 if (rc != -EAGAIN) {
1486                         WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1487                         rc = 0;
1488                         break;
1489                 }
1490
1491                 /* Wait for a short time and then try again */
1492                 if (msleep_interruptible(100) != 0)
1493                         return -EINTR;
1494                 rc = usb_ep_set_wedge(fsg->bulk_in);
1495         }
1496         return rc;
1497 }
1498
1499 static int pad_with_zeros(struct fsg_dev *fsg)
1500 {
1501         struct fsg_buffhd       *bh = fsg->common->next_buffhd_to_fill;
1502         u32                     nkeep = bh->inreq->length;
1503         u32                     nsend;
1504         int                     rc;
1505
1506         bh->state = BUF_STATE_EMPTY;            /* For the first iteration */
1507         fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1508         while (fsg->common->usb_amount_left > 0) {
1509
1510                 /* Wait for the next buffer to be free */
1511                 while (bh->state != BUF_STATE_EMPTY) {
1512                         rc = sleep_thread(fsg->common);
1513                         if (rc)
1514                                 return rc;
1515                 }
1516
1517                 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
1518                 memset(bh->buf + nkeep, 0, nsend - nkeep);
1519                 bh->inreq->length = nsend;
1520                 bh->inreq->zero = 0;
1521                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1522                                 &bh->inreq_busy, &bh->state);
1523                 bh = fsg->common->next_buffhd_to_fill = bh->next;
1524                 fsg->common->usb_amount_left -= nsend;
1525                 nkeep = 0;
1526         }
1527         return 0;
1528 }
1529
1530 static int throw_away_data(struct fsg_common *common)
1531 {
1532         struct fsg_buffhd       *bh;
1533         u32                     amount;
1534         int                     rc;
1535
1536         for (bh = common->next_buffhd_to_drain;
1537              bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1538              bh = common->next_buffhd_to_drain) {
1539
1540                 /* Throw away the data in a filled buffer */
1541                 if (bh->state == BUF_STATE_FULL) {
1542                         smp_rmb();
1543                         bh->state = BUF_STATE_EMPTY;
1544                         common->next_buffhd_to_drain = bh->next;
1545
1546                         /* A short packet or an error ends everything */
1547                         if (bh->outreq->actual != bh->outreq->length ||
1548                                         bh->outreq->status != 0) {
1549                                 raise_exception(common,
1550                                                 FSG_STATE_ABORT_BULK_OUT);
1551                                 return -EINTR;
1552                         }
1553                         continue;
1554                 }
1555
1556                 /* Try to submit another request if we need one */
1557                 bh = common->next_buffhd_to_fill;
1558                 if (bh->state == BUF_STATE_EMPTY
1559                  && common->usb_amount_left > 0) {
1560                         amount = min(common->usb_amount_left, FSG_BUFLEN);
1561
1562                         /* amount is always divisible by 512, hence by
1563                          * the bulk-out maxpacket size */
1564                         bh->outreq->length = amount;
1565                         bh->bulk_out_intended_length = amount;
1566                         bh->outreq->short_not_ok = 1;
1567                         START_TRANSFER_OR(common, bulk_out, bh->outreq,
1568                                           &bh->outreq_busy, &bh->state)
1569                                 /* Don't know what to do if
1570                                  * common->fsg is NULL */
1571                                 return -EIO;
1572                         common->next_buffhd_to_fill = bh->next;
1573                         common->usb_amount_left -= amount;
1574                         continue;
1575                 }
1576
1577                 /* Otherwise wait for something to happen */
1578                 rc = sleep_thread(common);
1579                 if (rc)
1580                         return rc;
1581         }
1582         return 0;
1583 }
1584
1585
1586 static int finish_reply(struct fsg_common *common)
1587 {
1588         struct fsg_buffhd       *bh = common->next_buffhd_to_fill;
1589         int                     rc = 0;
1590
1591         switch (common->data_dir) {
1592         case DATA_DIR_NONE:
1593                 break;                  /* Nothing to send */
1594
1595         /* If we don't know whether the host wants to read or write,
1596          * this must be CB or CBI with an unknown command.  We mustn't
1597          * try to send or receive any data.  So stall both bulk pipes
1598          * if we can and wait for a reset. */
1599         case DATA_DIR_UNKNOWN:
1600                 if (!common->can_stall) {
1601                         /* Nothing */
1602                 } else if (fsg_is_set(common)) {
1603                         fsg_set_halt(common->fsg, common->fsg->bulk_out);
1604                         rc = halt_bulk_in_endpoint(common->fsg);
1605                 } else {
1606                         /* Don't know what to do if common->fsg is NULL */
1607                         rc = -EIO;
1608                 }
1609                 break;
1610
1611         /* All but the last buffer of data must have already been sent */
1612         case DATA_DIR_TO_HOST:
1613                 if (common->data_size == 0) {
1614                         /* Nothing to send */
1615
1616                 /* If there's no residue, simply send the last buffer */
1617                 } else if (common->residue == 0) {
1618                         bh->inreq->zero = 0;
1619                         START_TRANSFER_OR(common, bulk_in, bh->inreq,
1620                                           &bh->inreq_busy, &bh->state)
1621                                 return -EIO;
1622                         common->next_buffhd_to_fill = bh->next;
1623
1624                 /* For Bulk-only, if we're allowed to stall then send the
1625                  * short packet and halt the bulk-in endpoint.  If we can't
1626                  * stall, pad out the remaining data with 0's. */
1627                 } else if (common->can_stall) {
1628                         bh->inreq->zero = 1;
1629                         START_TRANSFER_OR(common, bulk_in, bh->inreq,
1630                                           &bh->inreq_busy, &bh->state)
1631                                 /* Don't know what to do if
1632                                  * common->fsg is NULL */
1633                                 rc = -EIO;
1634                         common->next_buffhd_to_fill = bh->next;
1635                         if (common->fsg)
1636                                 rc = halt_bulk_in_endpoint(common->fsg);
1637                 } else if (fsg_is_set(common)) {
1638                         rc = pad_with_zeros(common->fsg);
1639                 } else {
1640                         /* Don't know what to do if common->fsg is NULL */
1641                         rc = -EIO;
1642                 }
1643                 break;
1644
1645         /* We have processed all we want from the data the host has sent.
1646          * There may still be outstanding bulk-out requests. */
1647         case DATA_DIR_FROM_HOST:
1648                 if (common->residue == 0) {
1649                         /* Nothing to receive */
1650
1651                 /* Did the host stop sending unexpectedly early? */
1652                 } else if (common->short_packet_received) {
1653                         raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1654                         rc = -EINTR;
1655
1656                 /* We haven't processed all the incoming data.  Even though
1657                  * we may be allowed to stall, doing so would cause a race.
1658                  * The controller may already have ACK'ed all the remaining
1659                  * bulk-out packets, in which case the host wouldn't see a
1660                  * STALL.  Not realizing the endpoint was halted, it wouldn't
1661                  * clear the halt -- leading to problems later on. */
1662 #if 0
1663                 } else if (common->can_stall) {
1664                         if (fsg_is_set(common))
1665                                 fsg_set_halt(common->fsg,
1666                                              common->fsg->bulk_out);
1667                         raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1668                         rc = -EINTR;
1669 #endif
1670
1671                 /* We can't stall.  Read in the excess data and throw it
1672                  * all away. */
1673                 } else {
1674                         rc = throw_away_data(common);
1675                 }
1676                 break;
1677         }
1678         return rc;
1679 }
1680
1681
1682 static int send_status(struct fsg_common *common)
1683 {
1684         struct fsg_lun          *curlun = common->curlun;
1685         struct fsg_buffhd       *bh;
1686         struct bulk_cs_wrap     *csw;
1687         int                     rc;
1688         u8                      status = USB_STATUS_PASS;
1689         u32                     sd, sdinfo = 0;
1690
1691         /* Wait for the next buffer to become available */
1692         bh = common->next_buffhd_to_fill;
1693         while (bh->state != BUF_STATE_EMPTY) {
1694                 rc = sleep_thread(common);
1695                 if (rc)
1696                         return rc;
1697         }
1698
1699         if (curlun) {
1700                 sd = curlun->sense_data;
1701                 sdinfo = curlun->sense_data_info;
1702         } else if (common->bad_lun_okay)
1703                 sd = SS_NO_SENSE;
1704         else
1705                 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1706
1707         if (common->phase_error) {
1708                 DBG(common, "sending phase-error status\n");
1709                 status = USB_STATUS_PHASE_ERROR;
1710                 sd = SS_INVALID_COMMAND;
1711         } else if (sd != SS_NO_SENSE) {
1712                 DBG(common, "sending command-failure status\n");
1713                 status = USB_STATUS_FAIL;
1714                 VDBG(common, "  sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
1715                                 "  info x%x\n",
1716                                 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1717         }
1718
1719         /* Store and send the Bulk-only CSW */
1720         csw = (void *)bh->buf;
1721
1722         csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
1723         csw->Tag = common->tag;
1724         csw->Residue = cpu_to_le32(common->residue);
1725         csw->Status = status;
1726
1727         bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1728         bh->inreq->zero = 0;
1729         START_TRANSFER_OR(common, bulk_in, bh->inreq,
1730                           &bh->inreq_busy, &bh->state)
1731                 /* Don't know what to do if common->fsg is NULL */
1732                 return -EIO;
1733
1734         common->next_buffhd_to_fill = bh->next;
1735         return 0;
1736 }
1737
1738
1739 /*-------------------------------------------------------------------------*/
1740
1741 /* Check whether the command is properly formed and whether its data size
1742  * and direction agree with the values we already have. */
1743 static int check_command(struct fsg_common *common, int cmnd_size,
1744                 enum data_direction data_dir, unsigned int mask,
1745                 int needs_medium, const char *name)
1746 {
1747         int                     i;
1748         int                     lun = common->cmnd[1] >> 5;
1749         static const char       dirletter[4] = {'u', 'o', 'i', 'n'};
1750         char                    hdlen[20];
1751         struct fsg_lun          *curlun;
1752
1753         hdlen[0] = 0;
1754         if (common->data_dir != DATA_DIR_UNKNOWN)
1755                 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1756                                 common->data_size);
1757         VDBG(common, "SCSI command: %s;  Dc=%d, D%c=%u;  Hc=%d%s\n",
1758              name, cmnd_size, dirletter[(int) data_dir],
1759              common->data_size_from_cmnd, common->cmnd_size, hdlen);
1760
1761         /* We can't reply at all until we know the correct data direction
1762          * and size. */
1763         if (common->data_size_from_cmnd == 0)
1764                 data_dir = DATA_DIR_NONE;
1765         if (common->data_size < common->data_size_from_cmnd) {
1766                 /* Host data size < Device data size is a phase error.
1767                  * Carry out the command, but only transfer as much as
1768                  * we are allowed. */
1769                 common->data_size_from_cmnd = common->data_size;
1770                 common->phase_error = 1;
1771         }
1772         common->residue = common->data_size;
1773         common->usb_amount_left = common->data_size;
1774
1775         /* Conflicting data directions is a phase error */
1776         if (common->data_dir != data_dir
1777          && common->data_size_from_cmnd > 0) {
1778                 common->phase_error = 1;
1779                 return -EINVAL;
1780         }
1781
1782         /* Verify the length of the command itself */
1783         if (cmnd_size != common->cmnd_size) {
1784
1785                 /* Special case workaround: There are plenty of buggy SCSI
1786                  * implementations. Many have issues with cbw->Length
1787                  * field passing a wrong command size. For those cases we
1788                  * always try to work around the problem by using the length
1789                  * sent by the host side provided it is at least as large
1790                  * as the correct command length.
1791                  * Examples of such cases would be MS-Windows, which issues
1792                  * REQUEST SENSE with cbw->Length == 12 where it should
1793                  * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1794                  * REQUEST SENSE with cbw->Length == 10 where it should
1795                  * be 6 as well.
1796                  */
1797                 if (cmnd_size <= common->cmnd_size) {
1798                         DBG(common, "%s is buggy! Expected length %d "
1799                             "but we got %d\n", name,
1800                             cmnd_size, common->cmnd_size);
1801                         cmnd_size = common->cmnd_size;
1802                 } else {
1803                         common->phase_error = 1;
1804                         return -EINVAL;
1805                 }
1806         }
1807
1808         /* Check that the LUN values are consistent */
1809         if (common->lun != lun)
1810                 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
1811                     common->lun, lun);
1812
1813         /* Check the LUN */
1814         if (common->lun >= 0 && common->lun < common->nluns) {
1815                 curlun = &common->luns[common->lun];
1816                 common->curlun = curlun;
1817                 if (common->cmnd[0] != SC_REQUEST_SENSE) {
1818                         curlun->sense_data = SS_NO_SENSE;
1819                         curlun->sense_data_info = 0;
1820                         curlun->info_valid = 0;
1821                 }
1822         } else {
1823                 common->curlun = NULL;
1824                 curlun = NULL;
1825                 common->bad_lun_okay = 0;
1826
1827                 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
1828                  * to use unsupported LUNs; all others may not. */
1829                 if (common->cmnd[0] != SC_INQUIRY &&
1830                     common->cmnd[0] != SC_REQUEST_SENSE) {
1831                         DBG(common, "unsupported LUN %d\n", common->lun);
1832                         return -EINVAL;
1833                 }
1834         }
1835
1836         /* If a unit attention condition exists, only INQUIRY and
1837          * REQUEST SENSE commands are allowed; anything else must fail. */
1838         if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
1839                         common->cmnd[0] != SC_INQUIRY &&
1840                         common->cmnd[0] != SC_REQUEST_SENSE) {
1841                 curlun->sense_data = curlun->unit_attention_data;
1842                 curlun->unit_attention_data = SS_NO_SENSE;
1843                 return -EINVAL;
1844         }
1845
1846         /* Check that only command bytes listed in the mask are non-zero */
1847         common->cmnd[1] &= 0x1f;                        /* Mask away the LUN */
1848         for (i = 1; i < cmnd_size; ++i) {
1849                 if (common->cmnd[i] && !(mask & (1 << i))) {
1850                         if (curlun)
1851                                 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1852                         return -EINVAL;
1853                 }
1854         }
1855
1856         /* If the medium isn't mounted and the command needs to access
1857          * it, return an error. */
1858         if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
1859                 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1860                 return -EINVAL;
1861         }
1862
1863         return 0;
1864 }
1865
1866
1867 static int do_scsi_command(struct fsg_common *common)
1868 {
1869         struct fsg_buffhd       *bh;
1870         int                     rc;
1871         int                     reply = -EINVAL;
1872         int                     i;
1873         static char             unknown[16];
1874
1875         dump_cdb(common);
1876
1877         /* Wait for the next buffer to become available for data or status */
1878         bh = common->next_buffhd_to_fill;
1879         common->next_buffhd_to_drain = bh;
1880         while (bh->state != BUF_STATE_EMPTY) {
1881                 rc = sleep_thread(common);
1882                 if (rc)
1883                         return rc;
1884         }
1885         common->phase_error = 0;
1886         common->short_packet_received = 0;
1887
1888         down_read(&common->filesem);    /* We're using the backing file */
1889         switch (common->cmnd[0]) {
1890
1891         case SC_INQUIRY:
1892                 common->data_size_from_cmnd = common->cmnd[4];
1893                 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1894                                       (1<<4), 0,
1895                                       "INQUIRY");
1896                 if (reply == 0)
1897                         reply = do_inquiry(common, bh);
1898                 break;
1899
1900         case SC_MODE_SELECT_6:
1901                 common->data_size_from_cmnd = common->cmnd[4];
1902                 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
1903                                       (1<<1) | (1<<4), 0,
1904                                       "MODE SELECT(6)");
1905                 if (reply == 0)
1906                         reply = do_mode_select(common, bh);
1907                 break;
1908
1909         case SC_MODE_SELECT_10:
1910                 common->data_size_from_cmnd =
1911                         get_unaligned_be16(&common->cmnd[7]);
1912                 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
1913                                       (1<<1) | (3<<7), 0,
1914                                       "MODE SELECT(10)");
1915                 if (reply == 0)
1916                         reply = do_mode_select(common, bh);
1917                 break;
1918
1919         case SC_MODE_SENSE_6:
1920                 common->data_size_from_cmnd = common->cmnd[4];
1921                 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1922                                       (1<<1) | (1<<2) | (1<<4), 0,
1923                                       "MODE SENSE(6)");
1924                 if (reply == 0)
1925                         reply = do_mode_sense(common, bh);
1926                 break;
1927
1928         case SC_MODE_SENSE_10:
1929                 common->data_size_from_cmnd =
1930                         get_unaligned_be16(&common->cmnd[7]);
1931                 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1932                                       (1<<1) | (1<<2) | (3<<7), 0,
1933                                       "MODE SENSE(10)");
1934                 if (reply == 0)
1935                         reply = do_mode_sense(common, bh);
1936                 break;
1937
1938         case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1939                 common->data_size_from_cmnd = 0;
1940                 reply = check_command(common, 6, DATA_DIR_NONE,
1941                                       (1<<4), 0,
1942                                       "PREVENT-ALLOW MEDIUM REMOVAL");
1943                 if (reply == 0)
1944                         reply = do_prevent_allow(common);
1945                 break;
1946
1947         case SC_READ_6:
1948                 i = common->cmnd[4];
1949                 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1950                 reply = check_command(common, 6, DATA_DIR_TO_HOST,
1951                                       (7<<1) | (1<<4), 1,
1952                                       "READ(6)");
1953                 if (reply == 0)
1954                         reply = do_read(common);
1955                 break;
1956
1957         case SC_READ_10:
1958                 common->data_size_from_cmnd =
1959                                 get_unaligned_be16(&common->cmnd[7]) << 9;
1960                 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1961                                       (1<<1) | (0xf<<2) | (3<<7), 1,
1962                                       "READ(10)");
1963                 if (reply == 0)
1964                         reply = do_read(common);
1965                 break;
1966
1967         case SC_READ_12:
1968                 common->data_size_from_cmnd =
1969                                 get_unaligned_be32(&common->cmnd[6]) << 9;
1970                 reply = check_command(common, 12, DATA_DIR_TO_HOST,
1971                                       (1<<1) | (0xf<<2) | (0xf<<6), 1,
1972                                       "READ(12)");
1973                 if (reply == 0)
1974                         reply = do_read(common);
1975                 break;
1976
1977         case SC_READ_CAPACITY:
1978                 common->data_size_from_cmnd = 8;
1979                 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1980                                       (0xf<<2) | (1<<8), 1,
1981                                       "READ CAPACITY");
1982                 if (reply == 0)
1983                         reply = do_read_capacity(common, bh);
1984                 break;
1985
1986         case SC_READ_HEADER:
1987                 if (!common->curlun || !common->curlun->cdrom)
1988                         goto unknown_cmnd;
1989                 common->data_size_from_cmnd =
1990                         get_unaligned_be16(&common->cmnd[7]);
1991                 reply = check_command(common, 10, DATA_DIR_TO_HOST,
1992                                       (3<<7) | (0x1f<<1), 1,
1993                                       "READ HEADER");
1994                 if (reply == 0)
1995                         reply = do_read_header(common, bh);
1996                 break;
1997
1998         case SC_READ_TOC:
1999                 if (!common->curlun || !common->curlun->cdrom)
2000                         goto unknown_cmnd;
2001                 common->data_size_from_cmnd =
2002                         get_unaligned_be16(&common->cmnd[7]);
2003                 reply = check_command(common, 10, DATA_DIR_TO_HOST,
2004                                       (7<<6) | (1<<1), 1,
2005                                       "READ TOC");
2006                 if (reply == 0)
2007                         reply = do_read_toc(common, bh);
2008                 break;
2009
2010         case SC_READ_FORMAT_CAPACITIES:
2011                 common->data_size_from_cmnd =
2012                         get_unaligned_be16(&common->cmnd[7]);
2013                 reply = check_command(common, 10, DATA_DIR_TO_HOST,
2014                                       (3<<7), 1,
2015                                       "READ FORMAT CAPACITIES");
2016                 if (reply == 0)
2017                         reply = do_read_format_capacities(common, bh);
2018                 break;
2019
2020         case SC_REQUEST_SENSE:
2021                 common->data_size_from_cmnd = common->cmnd[4];
2022                 reply = check_command(common, 6, DATA_DIR_TO_HOST,
2023                                       (1<<4), 0,
2024                                       "REQUEST SENSE");
2025                 if (reply == 0)
2026                         reply = do_request_sense(common, bh);
2027                 break;
2028
2029         case SC_START_STOP_UNIT:
2030                 common->data_size_from_cmnd = 0;
2031                 reply = check_command(common, 6, DATA_DIR_NONE,
2032                                       (1<<1) | (1<<4), 0,
2033                                       "START-STOP UNIT");
2034                 if (reply == 0)
2035                         reply = do_start_stop(common);
2036                 break;
2037
2038         case SC_SYNCHRONIZE_CACHE:
2039                 common->data_size_from_cmnd = 0;
2040                 reply = check_command(common, 10, DATA_DIR_NONE,
2041                                       (0xf<<2) | (3<<7), 1,
2042                                       "SYNCHRONIZE CACHE");
2043                 if (reply == 0)
2044                         reply = do_synchronize_cache(common);
2045                 break;
2046
2047         case SC_TEST_UNIT_READY:
2048                 common->data_size_from_cmnd = 0;
2049                 reply = check_command(common, 6, DATA_DIR_NONE,
2050                                 0, 1,
2051                                 "TEST UNIT READY");
2052                 break;
2053
2054         /* Although optional, this command is used by MS-Windows.  We
2055          * support a minimal version: BytChk must be 0. */
2056         case SC_VERIFY:
2057                 common->data_size_from_cmnd = 0;
2058                 reply = check_command(common, 10, DATA_DIR_NONE,
2059                                       (1<<1) | (0xf<<2) | (3<<7), 1,
2060                                       "VERIFY");
2061                 if (reply == 0)
2062                         reply = do_verify(common);
2063                 break;
2064
2065         case SC_WRITE_6:
2066                 i = common->cmnd[4];
2067                 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2068                 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
2069                                       (7<<1) | (1<<4), 1,
2070                                       "WRITE(6)");
2071                 if (reply == 0)
2072                         reply = do_write(common);
2073                 break;
2074
2075         case SC_WRITE_10:
2076                 common->data_size_from_cmnd =
2077                                 get_unaligned_be16(&common->cmnd[7]) << 9;
2078                 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
2079                                       (1<<1) | (0xf<<2) | (3<<7), 1,
2080                                       "WRITE(10)");
2081                 if (reply == 0)
2082                         reply = do_write(common);
2083                 break;
2084
2085         case SC_WRITE_12:
2086                 common->data_size_from_cmnd =
2087                                 get_unaligned_be32(&common->cmnd[6]) << 9;
2088                 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
2089                                       (1<<1) | (0xf<<2) | (0xf<<6), 1,
2090                                       "WRITE(12)");
2091                 if (reply == 0)
2092                         reply = do_write(common);
2093                 break;
2094
2095         /* Some mandatory commands that we recognize but don't implement.
2096          * They don't mean much in this setting.  It's left as an exercise
2097          * for anyone interested to implement RESERVE and RELEASE in terms
2098          * of Posix locks. */
2099         case SC_FORMAT_UNIT:
2100         case SC_RELEASE:
2101         case SC_RESERVE:
2102         case SC_SEND_DIAGNOSTIC:
2103                 /* Fall through */
2104
2105         default:
2106 unknown_cmnd:
2107                 common->data_size_from_cmnd = 0;
2108                 sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2109                 reply = check_command(common, common->cmnd_size,
2110                                       DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2111                 if (reply == 0) {
2112                         common->curlun->sense_data = SS_INVALID_COMMAND;
2113                         reply = -EINVAL;
2114                 }
2115                 break;
2116         }
2117         up_read(&common->filesem);
2118
2119         if (reply == -EINTR || signal_pending(current))
2120                 return -EINTR;
2121
2122         /* Set up the single reply buffer for finish_reply() */
2123         if (reply == -EINVAL)
2124                 reply = 0;              /* Error reply length */
2125         if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2126                 reply = min((u32) reply, common->data_size_from_cmnd);
2127                 bh->inreq->length = reply;
2128                 bh->state = BUF_STATE_FULL;
2129                 common->residue -= reply;
2130         }                               /* Otherwise it's already set */
2131
2132         return 0;
2133 }
2134
2135
2136 /*-------------------------------------------------------------------------*/
2137
2138 static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2139 {
2140         struct usb_request      *req = bh->outreq;
2141         struct fsg_bulk_cb_wrap *cbw = req->buf;
2142         struct fsg_common       *common = fsg->common;
2143
2144         /* Was this a real packet?  Should it be ignored? */
2145         if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2146                 return -EINVAL;
2147
2148         /* Is the CBW valid? */
2149         if (req->actual != USB_BULK_CB_WRAP_LEN ||
2150                         cbw->Signature != cpu_to_le32(
2151                                 USB_BULK_CB_SIG)) {
2152                 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2153                                 req->actual,
2154                                 le32_to_cpu(cbw->Signature));
2155
2156                 /* The Bulk-only spec says we MUST stall the IN endpoint
2157                  * (6.6.1), so it's unavoidable.  It also says we must
2158                  * retain this state until the next reset, but there's
2159                  * no way to tell the controller driver it should ignore
2160                  * Clear-Feature(HALT) requests.
2161                  *
2162                  * We aren't required to halt the OUT endpoint; instead
2163                  * we can simply accept and discard any data received
2164                  * until the next reset. */
2165                 wedge_bulk_in_endpoint(fsg);
2166                 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2167                 return -EINVAL;
2168         }
2169
2170         /* Is the CBW meaningful? */
2171         if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2172                         cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2173                 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2174                                 "cmdlen %u\n",
2175                                 cbw->Lun, cbw->Flags, cbw->Length);
2176
2177                 /* We can do anything we want here, so let's stall the
2178                  * bulk pipes if we are allowed to. */
2179                 if (common->can_stall) {
2180                         fsg_set_halt(fsg, fsg->bulk_out);
2181                         halt_bulk_in_endpoint(fsg);
2182                 }
2183                 return -EINVAL;
2184         }
2185
2186         /* Save the command for later */
2187         common->cmnd_size = cbw->Length;
2188         memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
2189         if (cbw->Flags & USB_BULK_IN_FLAG)
2190                 common->data_dir = DATA_DIR_TO_HOST;
2191         else
2192                 common->data_dir = DATA_DIR_FROM_HOST;
2193         common->data_size = le32_to_cpu(cbw->DataTransferLength);
2194         if (common->data_size == 0)
2195                 common->data_dir = DATA_DIR_NONE;
2196         common->lun = cbw->Lun;
2197         common->tag = cbw->Tag;
2198         return 0;
2199 }
2200
2201
2202 static int get_next_command(struct fsg_common *common)
2203 {
2204         struct fsg_buffhd       *bh;
2205         int                     rc = 0;
2206
2207         /* Wait for the next buffer to become available */
2208         bh = common->next_buffhd_to_fill;
2209         while (bh->state != BUF_STATE_EMPTY) {
2210                 rc = sleep_thread(common);
2211                 if (rc)
2212                         return rc;
2213         }
2214
2215         /* Queue a request to read a Bulk-only CBW */
2216         set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
2217         bh->outreq->short_not_ok = 1;
2218         START_TRANSFER_OR(common, bulk_out, bh->outreq,
2219                           &bh->outreq_busy, &bh->state)
2220                 /* Don't know what to do if common->fsg is NULL */
2221                 return -EIO;
2222
2223         /* We will drain the buffer in software, which means we
2224          * can reuse it for the next filling.  No need to advance
2225          * next_buffhd_to_fill. */
2226
2227         /* Wait for the CBW to arrive */
2228         while (bh->state != BUF_STATE_FULL) {
2229                 rc = sleep_thread(common);
2230                 if (rc)
2231                         return rc;
2232         }
2233         smp_rmb();
2234         rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
2235         bh->state = BUF_STATE_EMPTY;
2236
2237         return rc;
2238 }
2239
2240
2241 /*-------------------------------------------------------------------------*/
2242
2243 static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
2244                 const struct usb_endpoint_descriptor *d)
2245 {
2246         int     rc;
2247
2248         ep->driver_data = common;
2249         rc = usb_ep_enable(ep, d);
2250         if (rc)
2251                 ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
2252         return rc;
2253 }
2254
2255 static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
2256                 struct usb_request **preq)
2257 {
2258         *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2259         if (*preq)
2260                 return 0;
2261         ERROR(common, "can't allocate request for %s\n", ep->name);
2262         return -ENOMEM;
2263 }
2264
2265 /*
2266  * Reset interface setting and re-init endpoint state (toggle etc).
2267  * Call with altsetting < 0 to disable the interface.  The only other
2268  * available altsetting is 0, which enables the interface.
2269  */
2270 static int do_set_interface(struct fsg_common *common, int altsetting)
2271 {
2272         int     rc = 0;
2273         int     i;
2274         const struct usb_endpoint_descriptor    *d;
2275
2276         if (common->running)
2277                 DBG(common, "reset interface\n");
2278
2279 reset:
2280         /* Deallocate the requests */
2281         if (common->prev_fsg) {
2282                 struct fsg_dev *fsg = common->prev_fsg;
2283
2284                 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2285                         struct fsg_buffhd *bh = &common->buffhds[i];
2286
2287                         if (bh->inreq) {
2288                                 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2289                                 bh->inreq = NULL;
2290                         }
2291                         if (bh->outreq) {
2292                                 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2293                                 bh->outreq = NULL;
2294                         }
2295                 }
2296
2297                 /* Disable the endpoints */
2298                 if (fsg->bulk_in_enabled) {
2299                         usb_ep_disable(fsg->bulk_in);
2300                         fsg->bulk_in_enabled = 0;
2301                 }
2302                 if (fsg->bulk_out_enabled) {
2303                         usb_ep_disable(fsg->bulk_out);
2304                         fsg->bulk_out_enabled = 0;
2305                 }
2306
2307                 common->prev_fsg = 0;
2308         }
2309
2310         common->running = 0;
2311         if (altsetting < 0 || rc != 0)
2312                 return rc;
2313
2314         DBG(common, "set interface %d\n", altsetting);
2315
2316         if (fsg_is_set(common)) {
2317                 struct fsg_dev *fsg = common->fsg;
2318                 common->prev_fsg = common->fsg;
2319
2320                 /* Enable the endpoints */
2321                 d = fsg_ep_desc(common->gadget,
2322                                 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2323                 rc = enable_endpoint(common, fsg->bulk_in, d);
2324                 if (rc)
2325                         goto reset;
2326                 fsg->bulk_in_enabled = 1;
2327
2328                 d = fsg_ep_desc(common->gadget,
2329                                 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2330                 rc = enable_endpoint(common, fsg->bulk_out, d);
2331                 if (rc)
2332                         goto reset;
2333                 fsg->bulk_out_enabled = 1;
2334                 common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2335                 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2336
2337                 /* Allocate the requests */
2338                 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2339                         struct fsg_buffhd       *bh = &common->buffhds[i];
2340
2341                         rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
2342                         if (rc)
2343                                 goto reset;
2344                         rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
2345                         if (rc)
2346                                 goto reset;
2347                         bh->inreq->buf = bh->outreq->buf = bh->buf;
2348                         bh->inreq->context = bh->outreq->context = bh;
2349                         bh->inreq->complete = bulk_in_complete;
2350                         bh->outreq->complete = bulk_out_complete;
2351                 }
2352
2353                 common->running = 1;
2354                 for (i = 0; i < common->nluns; ++i)
2355                         common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2356                 return rc;
2357         } else {
2358                 return -EIO;
2359         }
2360 }
2361
2362
2363 /*
2364  * Change our operational configuration.  This code must agree with the code
2365  * that returns config descriptors, and with interface altsetting code.
2366  *
2367  * It's also responsible for power management interactions.  Some
2368  * configurations might not work with our current power sources.
2369  * For now we just assume the gadget is always self-powered.
2370  */
2371 static int do_set_config(struct fsg_common *common, u8 new_config)
2372 {
2373         int     rc = 0;
2374
2375         /* Disable the single interface */
2376         if (common->config != 0) {
2377                 DBG(common, "reset config\n");
2378                 common->config = 0;
2379                 rc = do_set_interface(common, -1);
2380         }
2381
2382         /* Enable the interface */
2383         if (new_config != 0) {
2384                 common->config = new_config;
2385                 rc = do_set_interface(common, 0);
2386                 if (rc != 0)
2387                         common->config = 0;     /* Reset on errors */
2388         }
2389         return rc;
2390 }
2391
2392
2393 /****************************** ALT CONFIGS ******************************/
2394
2395
2396 static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2397 {
2398         struct fsg_dev *fsg = fsg_from_func(f);
2399         fsg->common->prev_fsg = fsg->common->fsg;
2400         fsg->common->fsg = fsg;
2401         fsg->common->new_config = 1;
2402         raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2403         return 0;
2404 }
2405
2406 static void fsg_disable(struct usb_function *f)
2407 {
2408         struct fsg_dev *fsg = fsg_from_func(f);
2409         fsg->common->prev_fsg = fsg->common->fsg;
2410         fsg->common->fsg = fsg;
2411         fsg->common->new_config = 0;
2412         raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2413 }
2414
2415
2416 /*-------------------------------------------------------------------------*/
2417
2418 static void handle_exception(struct fsg_common *common)
2419 {
2420         siginfo_t               info;
2421         int                     sig;
2422         int                     i;
2423         struct fsg_buffhd       *bh;
2424         enum fsg_state          old_state;
2425         u8                      new_config;
2426         struct fsg_lun          *curlun;
2427         unsigned int            exception_req_tag;
2428         int                     rc;
2429
2430         /* Clear the existing signals.  Anything but SIGUSR1 is converted
2431          * into a high-priority EXIT exception. */
2432         for (;;) {
2433                 sig = dequeue_signal_lock(current, &current->blocked, &info);
2434                 if (!sig)
2435                         break;
2436                 if (sig != SIGUSR1) {
2437                         if (common->state < FSG_STATE_EXIT)
2438                                 DBG(common, "Main thread exiting on signal\n");
2439                         raise_exception(common, FSG_STATE_EXIT);
2440                 }
2441         }
2442
2443         /* Cancel all the pending transfers */
2444         if (fsg_is_set(common)) {
2445                 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2446                         bh = &common->buffhds[i];
2447                         if (bh->inreq_busy)
2448                                 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2449                         if (bh->outreq_busy)
2450                                 usb_ep_dequeue(common->fsg->bulk_out,
2451                                                bh->outreq);
2452                 }
2453
2454                 /* Wait until everything is idle */
2455                 for (;;) {
2456                         int num_active = 0;
2457                         for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2458                                 bh = &common->buffhds[i];
2459                                 num_active += bh->inreq_busy + bh->outreq_busy;
2460                         }
2461                         if (num_active == 0)
2462                                 break;
2463                         if (sleep_thread(common))
2464                                 return;
2465                 }
2466
2467                 /* Clear out the controller's fifos */
2468                 if (common->fsg->bulk_in_enabled)
2469                         usb_ep_fifo_flush(common->fsg->bulk_in);
2470                 if (common->fsg->bulk_out_enabled)
2471                         usb_ep_fifo_flush(common->fsg->bulk_out);
2472         }
2473
2474         /* Reset the I/O buffer states and pointers, the SCSI
2475          * state, and the exception.  Then invoke the handler. */
2476         spin_lock_irq(&common->lock);
2477
2478         for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2479                 bh = &common->buffhds[i];
2480                 bh->state = BUF_STATE_EMPTY;
2481         }
2482         common->next_buffhd_to_fill = &common->buffhds[0];
2483         common->next_buffhd_to_drain = &common->buffhds[0];
2484         exception_req_tag = common->exception_req_tag;
2485         new_config = common->new_config;
2486         old_state = common->state;
2487
2488         if (old_state == FSG_STATE_ABORT_BULK_OUT)
2489                 common->state = FSG_STATE_STATUS_PHASE;
2490         else {
2491                 for (i = 0; i < common->nluns; ++i) {
2492                         curlun = &common->luns[i];
2493                         curlun->prevent_medium_removal = 0;
2494                         curlun->sense_data = SS_NO_SENSE;
2495                         curlun->unit_attention_data = SS_NO_SENSE;
2496                         curlun->sense_data_info = 0;
2497                         curlun->info_valid = 0;
2498                 }
2499                 common->state = FSG_STATE_IDLE;
2500         }
2501         spin_unlock_irq(&common->lock);
2502
2503         /* Carry out any extra actions required for the exception */
2504         switch (old_state) {
2505         case FSG_STATE_ABORT_BULK_OUT:
2506                 send_status(common);
2507                 spin_lock_irq(&common->lock);
2508                 if (common->state == FSG_STATE_STATUS_PHASE)
2509                         common->state = FSG_STATE_IDLE;
2510                 spin_unlock_irq(&common->lock);
2511                 break;
2512
2513         case FSG_STATE_RESET:
2514                 /* In case we were forced against our will to halt a
2515                  * bulk endpoint, clear the halt now.  (The SuperH UDC
2516                  * requires this.) */
2517                 if (!fsg_is_set(common))
2518                         break;
2519                 if (test_and_clear_bit(IGNORE_BULK_OUT,
2520                                        &common->fsg->atomic_bitflags))
2521                         usb_ep_clear_halt(common->fsg->bulk_in);
2522
2523                 if (common->ep0_req_tag == exception_req_tag)
2524                         ep0_queue(common);      /* Complete the status stage */
2525
2526                 /* Technically this should go here, but it would only be
2527                  * a waste of time.  Ditto for the INTERFACE_CHANGE and
2528                  * CONFIG_CHANGE cases. */
2529                 /* for (i = 0; i < common->nluns; ++i) */
2530                 /*      common->luns[i].unit_attention_data = */
2531                 /*              SS_RESET_OCCURRED;  */
2532                 break;
2533
2534         case FSG_STATE_CONFIG_CHANGE:
2535                 rc = do_set_config(common, new_config);
2536                 break;
2537
2538         case FSG_STATE_EXIT:
2539         case FSG_STATE_TERMINATED:
2540                 do_set_config(common, 0);               /* Free resources */
2541                 spin_lock_irq(&common->lock);
2542                 common->state = FSG_STATE_TERMINATED;   /* Stop the thread */
2543                 spin_unlock_irq(&common->lock);
2544                 break;
2545
2546         case FSG_STATE_INTERFACE_CHANGE:
2547         case FSG_STATE_DISCONNECT:
2548         case FSG_STATE_COMMAND_PHASE:
2549         case FSG_STATE_DATA_PHASE:
2550         case FSG_STATE_STATUS_PHASE:
2551         case FSG_STATE_IDLE:
2552                 break;
2553         }
2554 }
2555
2556
2557 /*-------------------------------------------------------------------------*/
2558
2559 static int fsg_main_thread(void *common_)
2560 {
2561         struct fsg_common       *common = common_;
2562
2563         /* Allow the thread to be killed by a signal, but set the signal mask
2564          * to block everything but INT, TERM, KILL, and USR1. */
2565         allow_signal(SIGINT);
2566         allow_signal(SIGTERM);
2567         allow_signal(SIGKILL);
2568         allow_signal(SIGUSR1);
2569
2570         /* Allow the thread to be frozen */
2571         set_freezable();
2572
2573         /* Arrange for userspace references to be interpreted as kernel
2574          * pointers.  That way we can pass a kernel pointer to a routine
2575          * that expects a __user pointer and it will work okay. */
2576         set_fs(get_ds());
2577
2578         /* The main loop */
2579         while (common->state != FSG_STATE_TERMINATED) {
2580                 if (exception_in_progress(common) || signal_pending(current)) {
2581                         handle_exception(common);
2582                         continue;
2583                 }
2584
2585                 if (!common->running) {
2586                         sleep_thread(common);
2587                         continue;
2588                 }
2589
2590                 if (get_next_command(common))
2591                         continue;
2592
2593                 spin_lock_irq(&common->lock);
2594                 if (!exception_in_progress(common))
2595                         common->state = FSG_STATE_DATA_PHASE;
2596                 spin_unlock_irq(&common->lock);
2597
2598                 if (do_scsi_command(common) || finish_reply(common))
2599                         continue;
2600
2601                 spin_lock_irq(&common->lock);
2602                 if (!exception_in_progress(common))
2603                         common->state = FSG_STATE_STATUS_PHASE;
2604                 spin_unlock_irq(&common->lock);
2605
2606                 if (send_status(common))
2607                         continue;
2608
2609                 spin_lock_irq(&common->lock);
2610                 if (!exception_in_progress(common))
2611                         common->state = FSG_STATE_IDLE;
2612                 spin_unlock_irq(&common->lock);
2613         }
2614
2615         spin_lock_irq(&common->lock);
2616         common->thread_task = NULL;
2617         spin_unlock_irq(&common->lock);
2618
2619         if (!common->thread_exits || common->thread_exits(common) < 0) {
2620                 struct fsg_lun *curlun = common->luns;
2621                 unsigned i = common->nluns;
2622
2623                 down_write(&common->filesem);
2624                 for (; i--; ++curlun) {
2625                         if (!fsg_lun_is_open(curlun))
2626                                 continue;
2627
2628                         fsg_lun_close(curlun);
2629                         curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
2630                 }
2631                 up_write(&common->filesem);
2632         }
2633
2634         /* Let the unbind and cleanup routines know the thread has exited */
2635         complete_and_exit(&common->thread_notifier, 0);
2636 }
2637
2638
2639 /*************************** DEVICE ATTRIBUTES ***************************/
2640
2641 /* Write permission is checked per LUN in store_*() functions. */
2642 static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
2643 static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
2644
2645
2646 /****************************** FSG COMMON ******************************/
2647
2648 static void fsg_common_release(struct kref *ref);
2649
2650 static void fsg_lun_release(struct device *dev)
2651 {
2652         /* Nothing needs to be done */
2653 }
2654
2655 static inline void fsg_common_get(struct fsg_common *common)
2656 {
2657         kref_get(&common->ref);
2658 }
2659
2660 static inline void fsg_common_put(struct fsg_common *common)
2661 {
2662         kref_put(&common->ref, fsg_common_release);
2663 }
2664
2665
2666 static struct fsg_common *fsg_common_init(struct fsg_common *common,
2667                                           struct usb_composite_dev *cdev,
2668                                           struct fsg_config *cfg)
2669 {
2670         struct usb_gadget *gadget = cdev->gadget;
2671         struct fsg_buffhd *bh;
2672         struct fsg_lun *curlun;
2673         struct fsg_lun_config *lcfg;
2674         int nluns, i, rc;
2675         char *pathbuf;
2676
2677         /* Find out how many LUNs there should be */
2678         nluns = cfg->nluns;
2679         if (nluns < 1 || nluns > FSG_MAX_LUNS) {
2680                 dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns);
2681                 return ERR_PTR(-EINVAL);
2682         }
2683
2684         /* Allocate? */
2685         if (!common) {
2686                 common = kzalloc(sizeof *common, GFP_KERNEL);
2687                 if (!common)
2688                         return ERR_PTR(-ENOMEM);
2689                 common->free_storage_on_release = 1;
2690         } else {
2691                 memset(common, 0, sizeof common);
2692                 common->free_storage_on_release = 0;
2693         }
2694
2695         common->private_data = cfg->private_data;
2696
2697         common->gadget = gadget;
2698         common->ep0 = gadget->ep0;
2699         common->ep0req = cdev->req;
2700
2701         /* Maybe allocate device-global string IDs, and patch descriptors */
2702         if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2703                 rc = usb_string_id(cdev);
2704                 if (rc < 0) {
2705                         kfree(common);
2706                         return ERR_PTR(rc);
2707                 }
2708                 fsg_strings[FSG_STRING_INTERFACE].id = rc;
2709                 fsg_intf_desc.iInterface = rc;
2710         }
2711
2712         /* Create the LUNs, open their backing files, and register the
2713          * LUN devices in sysfs. */
2714         curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
2715         if (!curlun) {
2716                 kfree(common);
2717                 return ERR_PTR(-ENOMEM);
2718         }
2719         common->luns = curlun;
2720
2721         init_rwsem(&common->filesem);
2722
2723         for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
2724                 curlun->cdrom = !!lcfg->cdrom;
2725                 curlun->ro = lcfg->cdrom || lcfg->ro;
2726                 curlun->removable = lcfg->removable;
2727                 curlun->dev.release = fsg_lun_release;
2728                 curlun->dev.parent = &gadget->dev;
2729                 /* curlun->dev.driver = &fsg_driver.driver; XXX */
2730                 dev_set_drvdata(&curlun->dev, &common->filesem);
2731                 dev_set_name(&curlun->dev,
2732                              cfg->lun_name_format
2733                            ? cfg->lun_name_format
2734                            : "lun%d",
2735                              i);
2736
2737                 rc = device_register(&curlun->dev);
2738                 if (rc) {
2739                         INFO(common, "failed to register LUN%d: %d\n", i, rc);
2740                         common->nluns = i;
2741                         goto error_release;
2742                 }
2743
2744                 rc = device_create_file(&curlun->dev, &dev_attr_ro);
2745                 if (rc)
2746                         goto error_luns;
2747                 rc = device_create_file(&curlun->dev, &dev_attr_file);
2748                 if (rc)
2749                         goto error_luns;
2750
2751                 if (lcfg->filename) {
2752                         rc = fsg_lun_open(curlun, lcfg->filename);
2753                         if (rc)
2754                                 goto error_luns;
2755                 } else if (!curlun->removable) {
2756                         ERROR(common, "no file given for LUN%d\n", i);
2757                         rc = -EINVAL;
2758                         goto error_luns;
2759                 }
2760         }
2761         common->nluns = nluns;
2762
2763
2764         /* Data buffers cyclic list */
2765         /* Buffers in buffhds are static -- no need for additional
2766          * allocation. */
2767         bh = common->buffhds;
2768         i = FSG_NUM_BUFFERS - 1;
2769         do {
2770                 bh->next = bh + 1;
2771         } while (++bh, --i);
2772         bh->next = common->buffhds;
2773
2774
2775         /* Prepare inquiryString */
2776         if (cfg->release != 0xffff) {
2777                 i = cfg->release;
2778         } else {
2779                 /* The sa1100 controller is not supported */
2780                 i = gadget_is_sa1100(gadget)
2781                         ? -1
2782                         : usb_gadget_controller_number(gadget);
2783                 if (i >= 0) {
2784                         i = 0x0300 + i;
2785                 } else {
2786                         WARNING(common, "controller '%s' not recognized\n",
2787                                 gadget->name);
2788                         i = 0x0399;
2789                 }
2790         }
2791 #define OR(x, y) ((x) ? (x) : (y))
2792         snprintf(common->inquiry_string, sizeof common->inquiry_string,
2793                  "%-8s%-16s%04x",
2794                  OR(cfg->vendor_name, "Linux   "),
2795                  /* Assume product name dependent on the first LUN */
2796                  OR(cfg->product_name, common->luns->cdrom
2797                                      ? "File-Stor Gadget"
2798                                      : "File-CD Gadget  "),
2799                  i);
2800
2801
2802         /* Some peripheral controllers are known not to be able to
2803          * halt bulk endpoints correctly.  If one of them is present,
2804          * disable stalls.
2805          */
2806         common->can_stall = cfg->can_stall &&
2807                 !(gadget_is_sh(common->gadget) ||
2808                   gadget_is_at91(common->gadget));
2809
2810
2811         spin_lock_init(&common->lock);
2812         kref_init(&common->ref);
2813
2814
2815         /* Tell the thread to start working */
2816         common->thread_exits = cfg->thread_exits;
2817         common->thread_task =
2818                 kthread_create(fsg_main_thread, common,
2819                                OR(cfg->thread_name, "file-storage"));
2820         if (IS_ERR(common->thread_task)) {
2821                 rc = PTR_ERR(common->thread_task);
2822                 goto error_release;
2823         }
2824         init_completion(&common->thread_notifier);
2825 #undef OR
2826
2827
2828         /* Information */
2829         INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
2830         INFO(common, "Number of LUNs=%d\n", common->nluns);
2831
2832         pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2833         for (i = 0, nluns = common->nluns, curlun = common->luns;
2834              i < nluns;
2835              ++curlun, ++i) {
2836                 char *p = "(no medium)";
2837                 if (fsg_lun_is_open(curlun)) {
2838                         p = "(error)";
2839                         if (pathbuf) {
2840                                 p = d_path(&curlun->filp->f_path,
2841                                            pathbuf, PATH_MAX);
2842                                 if (IS_ERR(p))
2843                                         p = "(error)";
2844                         }
2845                 }
2846                 LINFO(curlun, "LUN: %s%s%sfile: %s\n",
2847                       curlun->removable ? "removable " : "",
2848                       curlun->ro ? "read only " : "",
2849                       curlun->cdrom ? "CD-ROM " : "",
2850                       p);
2851         }
2852         kfree(pathbuf);
2853
2854         DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task));
2855
2856         wake_up_process(common->thread_task);
2857
2858         return common;
2859
2860
2861 error_luns:
2862         common->nluns = i + 1;
2863 error_release:
2864         common->state = FSG_STATE_TERMINATED;   /* The thread is dead */
2865         /* Call fsg_common_release() directly, ref might be not
2866          * initialised */
2867         fsg_common_release(&common->ref);
2868         complete(&common->thread_notifier);
2869         return ERR_PTR(rc);
2870 }
2871
2872
2873 static void fsg_common_release(struct kref *ref)
2874 {
2875         struct fsg_common *common =
2876                 container_of(ref, struct fsg_common, ref);
2877         unsigned i = common->nluns;
2878         struct fsg_lun *lun = common->luns;
2879
2880         /* If the thread isn't already dead, tell it to exit now */
2881         if (common->state != FSG_STATE_TERMINATED) {
2882                 raise_exception(common, FSG_STATE_EXIT);
2883                 wait_for_completion(&common->thread_notifier);
2884
2885                 /* The cleanup routine waits for this completion also */
2886                 complete(&common->thread_notifier);
2887         }
2888
2889         /* Beware tempting for -> do-while optimization: when in error
2890          * recovery nluns may be zero. */
2891
2892         for (; i; --i, ++lun) {
2893                 device_remove_file(&lun->dev, &dev_attr_ro);
2894                 device_remove_file(&lun->dev, &dev_attr_file);
2895                 fsg_lun_close(lun);
2896                 device_unregister(&lun->dev);
2897         }
2898
2899         kfree(common->luns);
2900         if (common->free_storage_on_release)
2901                 kfree(common);
2902 }
2903
2904
2905 /*-------------------------------------------------------------------------*/
2906
2907
2908 static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2909 {
2910         struct fsg_dev          *fsg = fsg_from_func(f);
2911
2912         DBG(fsg, "unbind\n");
2913         fsg_common_put(fsg->common);
2914         kfree(fsg);
2915 }
2916
2917
2918 static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2919 {
2920         struct fsg_dev          *fsg = fsg_from_func(f);
2921         struct usb_gadget       *gadget = c->cdev->gadget;
2922         int                     rc;
2923         int                     i;
2924         struct usb_ep           *ep;
2925
2926         fsg->gadget = gadget;
2927
2928         /* New interface */
2929         i = usb_interface_id(c, f);
2930         if (i < 0)
2931                 return i;
2932         fsg_intf_desc.bInterfaceNumber = i;
2933         fsg->interface_number = i;
2934
2935         /* Find all the endpoints we will use */
2936         ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2937         if (!ep)
2938                 goto autoconf_fail;
2939         ep->driver_data = fsg->common;  /* claim the endpoint */
2940         fsg->bulk_in = ep;
2941
2942         ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2943         if (!ep)
2944                 goto autoconf_fail;
2945         ep->driver_data = fsg->common;  /* claim the endpoint */
2946         fsg->bulk_out = ep;
2947
2948         if (gadget_is_dualspeed(gadget)) {
2949                 /* Assume endpoint addresses are the same for both speeds */
2950                 fsg_hs_bulk_in_desc.bEndpointAddress =
2951                         fsg_fs_bulk_in_desc.bEndpointAddress;
2952                 fsg_hs_bulk_out_desc.bEndpointAddress =
2953                         fsg_fs_bulk_out_desc.bEndpointAddress;
2954                 f->hs_descriptors = fsg_hs_function;
2955         }
2956
2957         return 0;
2958
2959 autoconf_fail:
2960         ERROR(fsg, "unable to autoconfigure all endpoints\n");
2961         rc = -ENOTSUPP;
2962         fsg_unbind(c, f);
2963         return rc;
2964 }
2965
2966
2967 /****************************** ADD FUNCTION ******************************/
2968
2969 static struct usb_gadget_strings *fsg_strings_array[] = {
2970         &fsg_stringtab,
2971         NULL,
2972 };
2973
2974 static int fsg_add(struct usb_composite_dev *cdev,
2975                    struct usb_configuration *c,
2976                    struct fsg_common *common)
2977 {
2978         struct fsg_dev *fsg;
2979         int rc;
2980
2981         fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
2982         if (unlikely(!fsg))
2983                 return -ENOMEM;
2984
2985         fsg->function.name        = FSG_DRIVER_DESC;
2986         fsg->function.strings     = fsg_strings_array;
2987         fsg->function.descriptors = fsg_fs_function;
2988         fsg->function.bind        = fsg_bind;
2989         fsg->function.unbind      = fsg_unbind;
2990         fsg->function.setup       = fsg_setup;
2991         fsg->function.set_alt     = fsg_set_alt;
2992         fsg->function.disable     = fsg_disable;
2993
2994         fsg->common               = common;
2995         /* Our caller holds a reference to common structure so we
2996          * don't have to be worry about it being freed until we return
2997          * from this function.  So instead of incrementing counter now
2998          * and decrement in error recovery we increment it only when
2999          * call to usb_add_function() was successful. */
3000
3001         rc = usb_add_function(c, &fsg->function);
3002
3003         if (likely(rc == 0))
3004                 fsg_common_get(fsg->common);
3005         else
3006                 kfree(fsg);
3007
3008         return rc;
3009 }
3010
3011
3012
3013 /************************* Module parameters *************************/
3014
3015
3016 struct fsg_module_parameters {
3017         char            *file[FSG_MAX_LUNS];
3018         int             ro[FSG_MAX_LUNS];
3019         int             removable[FSG_MAX_LUNS];
3020         int             cdrom[FSG_MAX_LUNS];
3021
3022         unsigned int    file_count, ro_count, removable_count, cdrom_count;
3023         unsigned int    luns;   /* nluns */
3024         int             stall;  /* can_stall */
3025 };
3026
3027
3028 #define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc)       \
3029         module_param_array_named(prefix ## name, params.name, type,     \
3030                                  &prefix ## params.name ## _count,      \
3031                                  S_IRUGO);                              \
3032         MODULE_PARM_DESC(prefix ## name, desc)
3033
3034 #define _FSG_MODULE_PARAM(prefix, params, name, type, desc)             \
3035         module_param_named(prefix ## name, params.name, type,           \
3036                            S_IRUGO);                                    \
3037         MODULE_PARM_DESC(prefix ## name, desc)
3038
3039 #define FSG_MODULE_PARAMETERS(prefix, params)                           \
3040         _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp,            \
3041                                 "names of backing files or devices");   \
3042         _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool,               \
3043                                 "true to force read-only");             \
3044         _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool,        \
3045                                 "true to simulate removable media");    \
3046         _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool,            \
3047                                 "true to simulate CD-ROM instead of disk"); \
3048         _FSG_MODULE_PARAM(prefix, params, luns, uint,                   \
3049                           "number of LUNs");                            \
3050         _FSG_MODULE_PARAM(prefix, params, stall, bool,                  \
3051                           "false to prevent bulk stalls")
3052
3053
3054 static void
3055 fsg_config_from_params(struct fsg_config *cfg,
3056                        const struct fsg_module_parameters *params)
3057 {
3058         struct fsg_lun_config *lun;
3059         unsigned i;
3060
3061         /* Configure LUNs */
3062         cfg->nluns =
3063                 min(params->luns ?: (params->file_count ?: 1u),
3064                     (unsigned)FSG_MAX_LUNS);
3065         for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
3066                 lun->ro = !!params->ro[i];
3067                 lun->cdrom = !!params->cdrom[i];
3068                 lun->removable = /* Removable by default */
3069                         params->removable_count <= i || params->removable[i];
3070                 lun->filename =
3071                         params->file_count > i && params->file[i][0]
3072                         ? params->file[i]
3073                         : 0;
3074         }
3075
3076         /* Let MSF use defaults */
3077         cfg->lun_name_format = 0;
3078         cfg->thread_name = 0;
3079         cfg->vendor_name = 0;
3080         cfg->product_name = 0;
3081         cfg->release = 0xffff;
3082
3083         cfg->thread_exits = 0;
3084         cfg->private_data = 0;
3085
3086         /* Finalise */
3087         cfg->can_stall = params->stall;
3088 }
3089
3090 static inline struct fsg_common *
3091 fsg_common_from_params(struct fsg_common *common,
3092                        struct usb_composite_dev *cdev,
3093                        const struct fsg_module_parameters *params)
3094         __attribute__((unused));
3095 static inline struct fsg_common *
3096 fsg_common_from_params(struct fsg_common *common,
3097                        struct usb_composite_dev *cdev,
3098                        const struct fsg_module_parameters *params)
3099 {
3100         struct fsg_config cfg;
3101         fsg_config_from_params(&cfg, params);
3102         return fsg_common_init(common, cdev, &cfg);
3103 }
3104