2 * file_storage.c -- File-backed USB Storage Gadget, for USB development
4 * Copyright (C) 2003-2008 Alan Stern
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The names of the above-listed copyright holders may not be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * ALTERNATIVELY, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") as published by the Free Software
22 * Foundation, either version 2 of that License or (at your option) any
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * The File-backed Storage Gadget acts as a USB Mass Storage device,
41 * appearing to the host as a disk drive or as a CD-ROM drive. In addition
42 * to providing an example of a genuinely useful gadget driver for a USB
43 * device, it also illustrates a technique of double-buffering for increased
44 * throughput. Last but not least, it gives an easy way to probe the
45 * behavior of the Mass Storage drivers in a USB host.
47 * Backing storage is provided by a regular file or a block device, specified
48 * by the "file" module parameter. Access can be limited to read-only by
49 * setting the optional "ro" module parameter. (For CD-ROM emulation,
50 * access is always read-only.) The gadget will indicate that it has
51 * removable media if the optional "removable" module parameter is set.
53 * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
54 * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
55 * by the optional "transport" module parameter. It also supports the
56 * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
57 * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
58 * the optional "protocol" module parameter. In addition, the default
59 * Vendor ID, Product ID, and release number can be overridden.
61 * There is support for multiple logical units (LUNs), each of which has
62 * its own backing file. The number of LUNs can be set using the optional
63 * "luns" module parameter (anywhere from 1 to 8), and the corresponding
64 * files are specified using comma-separated lists for "file" and "ro".
65 * The default number of LUNs is taken from the number of "file" elements;
66 * it is 1 if "file" is not given. If "removable" is not set then a backing
67 * file must be specified for each LUN. If it is set, then an unspecified
68 * or empty backing filename means the LUN's medium is not loaded. Ideally
69 * each LUN would be settable independently as a disk drive or a CD-ROM
70 * drive, but currently all LUNs have to be the same type. The CD-ROM
71 * emulation includes a single data track and no audio tracks; hence there
72 * need be only one backing file per LUN. Note also that the CD-ROM block
73 * length is set to 512 rather than the more common value 2048.
75 * Requirements are modest; only a bulk-in and a bulk-out endpoint are
76 * needed (an interrupt-out endpoint is also needed for CBI). The memory
77 * requirement amounts to two 16K buffers, size configurable by a parameter.
78 * Support is included for both full-speed and high-speed operation.
80 * Note that the driver is slightly non-portable in that it assumes a
81 * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
82 * interrupt-in endpoints. With most device controllers this isn't an
83 * issue, but there may be some with hardware restrictions that prevent
84 * a buffer from being used by more than one endpoint.
88 * file=filename[,filename...]
89 * Required if "removable" is not set, names of
90 * the files or block devices used for
92 * ro=b[,b...] Default false, booleans for read-only access
93 * removable Default false, boolean for removable media
94 * luns=N Default N = number of filenames, number of
96 * stall Default determined according to the type of
97 * USB device controller (usually true),
98 * boolean to permit the driver to halt
100 * cdrom Default false, boolean for whether to emulate
102 * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
103 * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
104 * ATAPI, QIC, UFI, 8070, or SCSI;
106 * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
107 * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
108 * release=0xRRRR Override the USB release number (bcdDevice)
109 * buflen=N Default N=16384, buffer size used (will be
110 * rounded down to a multiple of
113 * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "ro",
114 * "removable", "luns", "stall", and "cdrom" options are available; default
115 * values are used for everything else.
117 * The pathnames of the backing files and the ro settings are available in
118 * the attribute files "file" and "ro" in the lun<n> subdirectory of the
119 * gadget's sysfs directory. If the "removable" option is set, writing to
120 * these files will simulate ejecting/loading the medium (writing an empty
121 * line means eject) and adjusting a write-enable tab. Changes to the ro
122 * setting are not allowed when the medium is loaded or if CD-ROM emulation
125 * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
126 * The driver's SCSI command interface was based on the "Information
127 * technology - Small Computer System Interface - 2" document from
128 * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
129 * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
130 * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
131 * "Universal Serial Bus Mass Storage Class UFI Command Specification"
132 * document, Revision 1.0, December 14, 1998, available at
133 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
140 * The FSG driver is fairly straightforward. There is a main kernel
141 * thread that handles most of the work. Interrupt routines field
142 * callbacks from the controller driver: bulk- and interrupt-request
143 * completion notifications, endpoint-0 events, and disconnect events.
144 * Completion events are passed to the main thread by wakeup calls. Many
145 * ep0 requests are handled at interrupt time, but SetInterface,
146 * SetConfiguration, and device reset requests are forwarded to the
147 * thread in the form of "exceptions" using SIGUSR1 signals (since they
148 * should interrupt any ongoing file I/O operations).
150 * The thread's main routine implements the standard command/data/status
151 * parts of a SCSI interaction. It and its subroutines are full of tests
152 * for pending signals/exceptions -- all this polling is necessary since
153 * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
154 * indication that the driver really wants to be running in userspace.)
155 * An important point is that so long as the thread is alive it keeps an
156 * open reference to the backing file. This will prevent unmounting
157 * the backing file's underlying filesystem and could cause problems
158 * during system shutdown, for example. To prevent such problems, the
159 * thread catches INT, TERM, and KILL signals and converts them into
162 * In normal operation the main thread is started during the gadget's
163 * fsg_bind() callback and stopped during fsg_unbind(). But it can also
164 * exit when it receives a signal, and there's no point leaving the
165 * gadget running when the thread is dead. So just before the thread
166 * exits, it deregisters the gadget driver. This makes things a little
167 * tricky: The driver is deregistered at two places, and the exiting
168 * thread can indirectly call fsg_unbind() which in turn can tell the
169 * thread to exit. The first problem is resolved through the use of the
170 * REGISTERED atomic bitflag; the driver will only be deregistered once.
171 * The second problem is resolved by having fsg_unbind() check
172 * fsg->state; it won't try to stop the thread if the state is already
173 * FSG_STATE_TERMINATED.
175 * To provide maximum throughput, the driver uses a circular pipeline of
176 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
177 * arbitrarily long; in practice the benefits don't justify having more
178 * than 2 stages (i.e., double buffering). But it helps to think of the
179 * pipeline as being a long one. Each buffer head contains a bulk-in and
180 * a bulk-out request pointer (since the buffer can be used for both
181 * output and input -- directions always are given from the host's
182 * point of view) as well as a pointer to the buffer and various state
185 * Use of the pipeline follows a simple protocol. There is a variable
186 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
187 * At any time that buffer head may still be in use from an earlier
188 * request, so each buffer head has a state variable indicating whether
189 * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
190 * buffer head to be EMPTY, filling the buffer either by file I/O or by
191 * USB I/O (during which the buffer head is BUSY), and marking the buffer
192 * head FULL when the I/O is complete. Then the buffer will be emptied
193 * (again possibly by USB I/O, during which it is marked BUSY) and
194 * finally marked EMPTY again (possibly by a completion routine).
196 * A module parameter tells the driver to avoid stalling the bulk
197 * endpoints wherever the transport specification allows. This is
198 * necessary for some UDCs like the SuperH, which cannot reliably clear a
199 * halt on a bulk endpoint. However, under certain circumstances the
200 * Bulk-only specification requires a stall. In such cases the driver
201 * will halt the endpoint and set a flag indicating that it should clear
202 * the halt in software during the next device reset. Hopefully this
203 * will permit everything to work correctly. Furthermore, although the
204 * specification allows the bulk-out endpoint to halt when the host sends
205 * too much data, implementing this would cause an unavoidable race.
206 * The driver will always use the "no-stall" approach for OUT transfers.
208 * One subtle point concerns sending status-stage responses for ep0
209 * requests. Some of these requests, such as device reset, can involve
210 * interrupting an ongoing file I/O operation, which might take an
211 * arbitrarily long time. During that delay the host might give up on
212 * the original ep0 request and issue a new one. When that happens the
213 * driver should not notify the host about completion of the original
214 * request, as the host will no longer be waiting for it. So the driver
215 * assigns to each ep0 request a unique tag, and it keeps track of the
216 * tag value of the request associated with a long-running exception
217 * (device-reset, interface-change, or configuration-change). When the
218 * exception handler is finished, the status-stage response is submitted
219 * only if the current ep0 request tag is equal to the exception request
220 * tag. Thus only the most recently received ep0 request will get a
221 * status-stage response.
223 * Warning: This driver source file is too long. It ought to be split up
224 * into a header file plus about 3 separate .c files, to handle the details
225 * of the Gadget, USB Mass Storage, and SCSI protocols.
229 /* #define VERBOSE_DEBUG */
230 /* #define DUMP_MSGS */
233 #include <linux/blkdev.h>
234 #include <linux/completion.h>
235 #include <linux/dcache.h>
236 #include <linux/delay.h>
237 #include <linux/device.h>
238 #include <linux/fcntl.h>
239 #include <linux/file.h>
240 #include <linux/fs.h>
241 #include <linux/kref.h>
242 #include <linux/kthread.h>
243 #include <linux/limits.h>
244 #include <linux/rwsem.h>
245 #include <linux/slab.h>
246 #include <linux/spinlock.h>
247 #include <linux/string.h>
248 #include <linux/freezer.h>
249 #include <linux/utsname.h>
251 #include <asm/unaligned.h>
253 #include <linux/usb/ch9.h>
254 #include <linux/usb/gadget.h>
256 #include "gadget_chips.h"
261 * Kbuild is not very cooperative with respect to linking separately
262 * compiled library objects into one module. So for now we won't use
263 * separate compilation ... ensuring init/exit sections work to shrink
264 * the runtime footprint, and giving us at least some parts of what
265 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
267 #include "usbstring.c"
269 #include "epautoconf.c"
271 /*-------------------------------------------------------------------------*/
273 #define DRIVER_DESC "File-backed Storage Gadget"
274 #define DRIVER_NAME "g_file_storage"
275 #define DRIVER_VERSION "20 November 2008"
277 static const char longname[] = DRIVER_DESC;
278 static const char shortname[] = DRIVER_NAME;
280 MODULE_DESCRIPTION(DRIVER_DESC);
281 MODULE_AUTHOR("Alan Stern");
282 MODULE_LICENSE("Dual BSD/GPL");
284 /* Thanks to NetChip Technologies for donating this product ID.
286 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
287 * Instead: allocate your own, using normal USB-IF procedures. */
288 #define DRIVER_VENDOR_ID 0x0525 // NetChip
289 #define DRIVER_PRODUCT_ID 0xa4a5 // Linux-USB File-backed Storage Gadget
293 * This driver assumes self-powered hardware and has no way for users to
294 * trigger remote wakeup. It uses autoconfiguration to select endpoints
295 * and endpoint addresses.
299 /*-------------------------------------------------------------------------*/
302 /* Encapsulate the module parameter settings */
307 char *file[MAX_LUNS];
309 unsigned int num_filenames;
310 unsigned int num_ros;
317 char *transport_parm;
319 unsigned short vendor;
320 unsigned short product;
321 unsigned short release;
325 char *transport_name;
329 } mod_data = { // Default values
330 .transport_parm = "BBB",
331 .protocol_parm = "SCSI",
335 .vendor = DRIVER_VENDOR_ID,
336 .product = DRIVER_PRODUCT_ID,
337 .release = 0xffff, // Use controller chip type
342 module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
344 MODULE_PARM_DESC(file, "names of backing files or devices");
346 module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
347 MODULE_PARM_DESC(ro, "true to force read-only");
349 module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
350 MODULE_PARM_DESC(luns, "number of LUNs");
352 module_param_named(removable, mod_data.removable, bool, S_IRUGO);
353 MODULE_PARM_DESC(removable, "true to simulate removable media");
355 module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
356 MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
358 module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
359 MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
362 /* In the non-TEST version, only the module parameters listed above
364 #ifdef CONFIG_USB_FILE_STORAGE_TEST
366 module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
367 MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
369 module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
370 MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
373 module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
374 MODULE_PARM_DESC(vendor, "USB Vendor ID");
376 module_param_named(product, mod_data.product, ushort, S_IRUGO);
377 MODULE_PARM_DESC(product, "USB Product ID");
379 module_param_named(release, mod_data.release, ushort, S_IRUGO);
380 MODULE_PARM_DESC(release, "USB release number");
382 module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
383 MODULE_PARM_DESC(buflen, "I/O buffer size");
385 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
389 * These definitions will permit the compiler to avoid generating code for
390 * parts of the driver that aren't used in the non-TEST version. Even gcc
391 * can recognize when a test of a constant expression yields a dead code
395 #ifdef CONFIG_USB_FILE_STORAGE_TEST
397 #define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
398 #define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
399 #define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
403 #define transport_is_bbb() 1
404 #define transport_is_cbi() 0
405 #define protocol_is_scsi() 1
407 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
410 /*-------------------------------------------------------------------------*/
412 #include "storage_common.c"
414 /*-------------------------------------------------------------------------*/
418 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
420 struct usb_gadget *gadget;
422 /* filesem protects: backing files in use */
423 struct rw_semaphore filesem;
425 /* reference counting: wait until all LUNs are released */
428 struct usb_ep *ep0; // Handy copy of gadget->ep0
429 struct usb_request *ep0req; // For control responses
430 unsigned int ep0_req_tag;
431 const char *ep0req_name;
433 struct usb_request *intreq; // For interrupt responses
435 struct fsg_buffhd *intr_buffhd;
437 unsigned int bulk_out_maxpacket;
438 enum fsg_state state; // For exception handling
439 unsigned int exception_req_tag;
441 u8 config, new_config;
443 unsigned int running : 1;
444 unsigned int bulk_in_enabled : 1;
445 unsigned int bulk_out_enabled : 1;
446 unsigned int intr_in_enabled : 1;
447 unsigned int phase_error : 1;
448 unsigned int short_packet_received : 1;
449 unsigned int bad_lun_okay : 1;
451 unsigned long atomic_bitflags;
453 #define IGNORE_BULK_OUT 1
456 struct usb_ep *bulk_in;
457 struct usb_ep *bulk_out;
458 struct usb_ep *intr_in;
460 struct fsg_buffhd *next_buffhd_to_fill;
461 struct fsg_buffhd *next_buffhd_to_drain;
462 struct fsg_buffhd buffhds[NUM_BUFFERS];
464 int thread_wakeup_needed;
465 struct completion thread_notifier;
466 struct task_struct *thread_task;
469 u8 cmnd[MAX_COMMAND_SIZE];
470 enum data_direction data_dir;
472 u32 data_size_from_cmnd;
478 /* The CB protocol offers no way for a host to know when a command
479 * has completed. As a result the next command may arrive early,
480 * and we will still have to handle it. For that reason we need
481 * a buffer to store new commands when using CB (or CBI, which
482 * does not oblige a host to wait for command completion either). */
484 u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
491 typedef void (*fsg_routine_t)(struct fsg_dev *);
493 static int exception_in_progress(struct fsg_dev *fsg)
495 return (fsg->state > FSG_STATE_IDLE);
498 /* Make bulk-out requests be divisible by the maxpacket size */
499 static void set_bulk_out_req_length(struct fsg_dev *fsg,
500 struct fsg_buffhd *bh, unsigned int length)
504 bh->bulk_out_intended_length = length;
505 rem = length % fsg->bulk_out_maxpacket;
507 length += fsg->bulk_out_maxpacket - rem;
508 bh->outreq->length = length;
511 static struct fsg_dev *the_fsg;
512 static struct usb_gadget_driver fsg_driver;
515 /*-------------------------------------------------------------------------*/
517 static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
521 if (ep == fsg->bulk_in)
523 else if (ep == fsg->bulk_out)
527 DBG(fsg, "%s set halt\n", name);
528 return usb_ep_set_halt(ep);
532 /*-------------------------------------------------------------------------*/
535 * DESCRIPTORS ... most are static, but strings and (full) configuration
536 * descriptors are built on demand. Also the (static) config and interface
537 * descriptors are adjusted during fsg_bind().
540 /* There is only one configuration. */
541 #define CONFIG_VALUE 1
543 static struct usb_device_descriptor
545 .bLength = sizeof device_desc,
546 .bDescriptorType = USB_DT_DEVICE,
548 .bcdUSB = cpu_to_le16(0x0200),
549 .bDeviceClass = USB_CLASS_PER_INTERFACE,
551 /* The next three values can be overridden by module parameters */
552 .idVendor = cpu_to_le16(DRIVER_VENDOR_ID),
553 .idProduct = cpu_to_le16(DRIVER_PRODUCT_ID),
554 .bcdDevice = cpu_to_le16(0xffff),
556 .iManufacturer = STRING_MANUFACTURER,
557 .iProduct = STRING_PRODUCT,
558 .iSerialNumber = STRING_SERIAL,
559 .bNumConfigurations = 1,
562 static struct usb_config_descriptor
564 .bLength = sizeof config_desc,
565 .bDescriptorType = USB_DT_CONFIG,
567 /* wTotalLength computed by usb_gadget_config_buf() */
569 .bConfigurationValue = CONFIG_VALUE,
570 .iConfiguration = STRING_CONFIG,
571 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
572 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
576 static struct usb_qualifier_descriptor
578 .bLength = sizeof dev_qualifier,
579 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
581 .bcdUSB = cpu_to_le16(0x0200),
582 .bDeviceClass = USB_CLASS_PER_INTERFACE,
584 .bNumConfigurations = 1,
590 * Config descriptors must agree with the code that sets configurations
591 * and with code managing interfaces and their altsettings. They must
592 * also handle different speeds and other-speed requests.
594 static int populate_config_buf(struct usb_gadget *gadget,
595 u8 *buf, u8 type, unsigned index)
597 enum usb_device_speed speed = gadget->speed;
599 const struct usb_descriptor_header **function;
604 if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
605 speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
606 if (gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH)
607 function = hs_function;
609 function = fs_function;
611 /* for now, don't advertise srp-only devices */
612 if (!gadget_is_otg(gadget))
615 len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
616 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
621 /*-------------------------------------------------------------------------*/
623 /* These routines may be called in process context or in_irq */
625 /* Caller must hold fsg->lock */
626 static void wakeup_thread(struct fsg_dev *fsg)
628 /* Tell the main thread that something has happened */
629 fsg->thread_wakeup_needed = 1;
630 if (fsg->thread_task)
631 wake_up_process(fsg->thread_task);
635 static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
639 /* Do nothing if a higher-priority exception is already in progress.
640 * If a lower-or-equal priority exception is in progress, preempt it
641 * and notify the main thread by sending it a signal. */
642 spin_lock_irqsave(&fsg->lock, flags);
643 if (fsg->state <= new_state) {
644 fsg->exception_req_tag = fsg->ep0_req_tag;
645 fsg->state = new_state;
646 if (fsg->thread_task)
647 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
650 spin_unlock_irqrestore(&fsg->lock, flags);
654 /*-------------------------------------------------------------------------*/
656 /* The disconnect callback and ep0 routines. These always run in_irq,
657 * except that ep0_queue() is called in the main thread to acknowledge
658 * completion of various requests: set config, set interface, and
659 * Bulk-only device reset. */
661 static void fsg_disconnect(struct usb_gadget *gadget)
663 struct fsg_dev *fsg = get_gadget_data(gadget);
665 DBG(fsg, "disconnect or port reset\n");
666 raise_exception(fsg, FSG_STATE_DISCONNECT);
670 static int ep0_queue(struct fsg_dev *fsg)
674 rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
675 if (rc != 0 && rc != -ESHUTDOWN) {
677 /* We can't do much more than wait for a reset */
678 WARNING(fsg, "error in submission: %s --> %d\n",
684 static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
686 struct fsg_dev *fsg = ep->driver_data;
689 dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
690 if (req->status || req->actual != req->length)
691 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
692 req->status, req->actual, req->length);
693 if (req->status == -ECONNRESET) // Request was cancelled
694 usb_ep_fifo_flush(ep);
696 if (req->status == 0 && req->context)
697 ((fsg_routine_t) (req->context))(fsg);
701 /*-------------------------------------------------------------------------*/
703 /* Bulk and interrupt endpoint completion handlers.
704 * These always run in_irq. */
706 static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
708 struct fsg_dev *fsg = ep->driver_data;
709 struct fsg_buffhd *bh = req->context;
711 if (req->status || req->actual != req->length)
712 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
713 req->status, req->actual, req->length);
714 if (req->status == -ECONNRESET) // Request was cancelled
715 usb_ep_fifo_flush(ep);
717 /* Hold the lock while we update the request and buffer states */
719 spin_lock(&fsg->lock);
721 bh->state = BUF_STATE_EMPTY;
723 spin_unlock(&fsg->lock);
726 static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
728 struct fsg_dev *fsg = ep->driver_data;
729 struct fsg_buffhd *bh = req->context;
731 dump_msg(fsg, "bulk-out", req->buf, req->actual);
732 if (req->status || req->actual != bh->bulk_out_intended_length)
733 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
734 req->status, req->actual,
735 bh->bulk_out_intended_length);
736 if (req->status == -ECONNRESET) // Request was cancelled
737 usb_ep_fifo_flush(ep);
739 /* Hold the lock while we update the request and buffer states */
741 spin_lock(&fsg->lock);
743 bh->state = BUF_STATE_FULL;
745 spin_unlock(&fsg->lock);
749 #ifdef CONFIG_USB_FILE_STORAGE_TEST
750 static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
752 struct fsg_dev *fsg = ep->driver_data;
753 struct fsg_buffhd *bh = req->context;
755 if (req->status || req->actual != req->length)
756 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
757 req->status, req->actual, req->length);
758 if (req->status == -ECONNRESET) // Request was cancelled
759 usb_ep_fifo_flush(ep);
761 /* Hold the lock while we update the request and buffer states */
763 spin_lock(&fsg->lock);
764 fsg->intreq_busy = 0;
765 bh->state = BUF_STATE_EMPTY;
767 spin_unlock(&fsg->lock);
771 static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
773 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
776 /*-------------------------------------------------------------------------*/
778 /* Ep0 class-specific handlers. These always run in_irq. */
780 #ifdef CONFIG_USB_FILE_STORAGE_TEST
781 static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
783 struct usb_request *req = fsg->ep0req;
784 static u8 cbi_reset_cmnd[6] = {
785 SC_SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
787 /* Error in command transfer? */
788 if (req->status || req->length != req->actual ||
789 req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
791 /* Not all controllers allow a protocol stall after
792 * receiving control-out data, but we'll try anyway. */
793 fsg_set_halt(fsg, fsg->ep0);
794 return; // Wait for reset
797 /* Is it the special reset command? */
798 if (req->actual >= sizeof cbi_reset_cmnd &&
799 memcmp(req->buf, cbi_reset_cmnd,
800 sizeof cbi_reset_cmnd) == 0) {
802 /* Raise an exception to stop the current operation
803 * and reinitialize our state. */
804 DBG(fsg, "cbi reset request\n");
805 raise_exception(fsg, FSG_STATE_RESET);
809 VDBG(fsg, "CB[I] accept device-specific command\n");
810 spin_lock(&fsg->lock);
812 /* Save the command for later */
813 if (fsg->cbbuf_cmnd_size)
814 WARNING(fsg, "CB[I] overwriting previous command\n");
815 fsg->cbbuf_cmnd_size = req->actual;
816 memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
819 spin_unlock(&fsg->lock);
823 static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
825 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
828 static int class_setup_req(struct fsg_dev *fsg,
829 const struct usb_ctrlrequest *ctrl)
831 struct usb_request *req = fsg->ep0req;
832 int value = -EOPNOTSUPP;
833 u16 w_index = le16_to_cpu(ctrl->wIndex);
834 u16 w_value = le16_to_cpu(ctrl->wValue);
835 u16 w_length = le16_to_cpu(ctrl->wLength);
840 /* Handle Bulk-only class-specific requests */
841 if (transport_is_bbb()) {
842 switch (ctrl->bRequest) {
844 case USB_BULK_RESET_REQUEST:
845 if (ctrl->bRequestType != (USB_DIR_OUT |
846 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
848 if (w_index != 0 || w_value != 0) {
853 /* Raise an exception to stop the current operation
854 * and reinitialize our state. */
855 DBG(fsg, "bulk reset request\n");
856 raise_exception(fsg, FSG_STATE_RESET);
857 value = DELAYED_STATUS;
860 case USB_BULK_GET_MAX_LUN_REQUEST:
861 if (ctrl->bRequestType != (USB_DIR_IN |
862 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
864 if (w_index != 0 || w_value != 0) {
868 VDBG(fsg, "get max LUN\n");
869 *(u8 *) req->buf = fsg->nluns - 1;
875 /* Handle CBI class-specific requests */
877 switch (ctrl->bRequest) {
879 case USB_CBI_ADSC_REQUEST:
880 if (ctrl->bRequestType != (USB_DIR_OUT |
881 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
883 if (w_index != 0 || w_value != 0) {
887 if (w_length > MAX_COMMAND_SIZE) {
892 fsg->ep0req->context = received_cbi_adsc;
897 if (value == -EOPNOTSUPP)
899 "unknown class-specific control req "
900 "%02x.%02x v%04x i%04x l%u\n",
901 ctrl->bRequestType, ctrl->bRequest,
902 le16_to_cpu(ctrl->wValue), w_index, w_length);
907 /*-------------------------------------------------------------------------*/
909 /* Ep0 standard request handlers. These always run in_irq. */
911 static int standard_setup_req(struct fsg_dev *fsg,
912 const struct usb_ctrlrequest *ctrl)
914 struct usb_request *req = fsg->ep0req;
915 int value = -EOPNOTSUPP;
916 u16 w_index = le16_to_cpu(ctrl->wIndex);
917 u16 w_value = le16_to_cpu(ctrl->wValue);
919 /* Usually this just stores reply data in the pre-allocated ep0 buffer,
920 * but config change events will also reconfigure hardware. */
921 switch (ctrl->bRequest) {
923 case USB_REQ_GET_DESCRIPTOR:
924 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
927 switch (w_value >> 8) {
930 VDBG(fsg, "get device descriptor\n");
931 value = sizeof device_desc;
932 memcpy(req->buf, &device_desc, value);
934 case USB_DT_DEVICE_QUALIFIER:
935 VDBG(fsg, "get device qualifier\n");
936 if (!gadget_is_dualspeed(fsg->gadget))
938 value = sizeof dev_qualifier;
939 memcpy(req->buf, &dev_qualifier, value);
942 case USB_DT_OTHER_SPEED_CONFIG:
943 VDBG(fsg, "get other-speed config descriptor\n");
944 if (!gadget_is_dualspeed(fsg->gadget))
948 VDBG(fsg, "get configuration descriptor\n");
950 value = populate_config_buf(fsg->gadget,
957 VDBG(fsg, "get string descriptor\n");
959 /* wIndex == language code */
960 value = usb_gadget_get_string(&stringtab,
961 w_value & 0xff, req->buf);
966 /* One config, two speeds */
967 case USB_REQ_SET_CONFIGURATION:
968 if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
971 VDBG(fsg, "set configuration\n");
972 if (w_value == CONFIG_VALUE || w_value == 0) {
973 fsg->new_config = w_value;
975 /* Raise an exception to wipe out previous transaction
976 * state (queued bufs, etc) and set the new config. */
977 raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
978 value = DELAYED_STATUS;
981 case USB_REQ_GET_CONFIGURATION:
982 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
985 VDBG(fsg, "get configuration\n");
986 *(u8 *) req->buf = fsg->config;
990 case USB_REQ_SET_INTERFACE:
991 if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
992 USB_RECIP_INTERFACE))
994 if (fsg->config && w_index == 0) {
996 /* Raise an exception to wipe out previous transaction
997 * state (queued bufs, etc) and install the new
998 * interface altsetting. */
999 raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
1000 value = DELAYED_STATUS;
1003 case USB_REQ_GET_INTERFACE:
1004 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1005 USB_RECIP_INTERFACE))
1013 VDBG(fsg, "get interface\n");
1014 *(u8 *) req->buf = 0;
1020 "unknown control req %02x.%02x v%04x i%04x l%u\n",
1021 ctrl->bRequestType, ctrl->bRequest,
1022 w_value, w_index, le16_to_cpu(ctrl->wLength));
1029 static int fsg_setup(struct usb_gadget *gadget,
1030 const struct usb_ctrlrequest *ctrl)
1032 struct fsg_dev *fsg = get_gadget_data(gadget);
1034 int w_length = le16_to_cpu(ctrl->wLength);
1036 ++fsg->ep0_req_tag; // Record arrival of a new request
1037 fsg->ep0req->context = NULL;
1038 fsg->ep0req->length = 0;
1039 dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
1041 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
1042 rc = class_setup_req(fsg, ctrl);
1044 rc = standard_setup_req(fsg, ctrl);
1046 /* Respond with data/status or defer until later? */
1047 if (rc >= 0 && rc != DELAYED_STATUS) {
1048 rc = min(rc, w_length);
1049 fsg->ep0req->length = rc;
1050 fsg->ep0req->zero = rc < w_length;
1051 fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
1052 "ep0-in" : "ep0-out");
1053 rc = ep0_queue(fsg);
1056 /* Device either stalls (rc < 0) or reports success */
1061 /*-------------------------------------------------------------------------*/
1063 /* All the following routines run in process context */
1066 /* Use this for bulk or interrupt transfers, not ep0 */
1067 static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
1068 struct usb_request *req, int *pbusy,
1069 enum fsg_buffer_state *state)
1073 if (ep == fsg->bulk_in)
1074 dump_msg(fsg, "bulk-in", req->buf, req->length);
1075 else if (ep == fsg->intr_in)
1076 dump_msg(fsg, "intr-in", req->buf, req->length);
1078 spin_lock_irq(&fsg->lock);
1080 *state = BUF_STATE_BUSY;
1081 spin_unlock_irq(&fsg->lock);
1082 rc = usb_ep_queue(ep, req, GFP_KERNEL);
1085 *state = BUF_STATE_EMPTY;
1087 /* We can't do much more than wait for a reset */
1089 /* Note: currently the net2280 driver fails zero-length
1090 * submissions if DMA is enabled. */
1091 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
1093 WARNING(fsg, "error in submission: %s --> %d\n",
1099 static int sleep_thread(struct fsg_dev *fsg)
1103 /* Wait until a signal arrives or we are woken up */
1106 set_current_state(TASK_INTERRUPTIBLE);
1107 if (signal_pending(current)) {
1111 if (fsg->thread_wakeup_needed)
1115 __set_current_state(TASK_RUNNING);
1116 fsg->thread_wakeup_needed = 0;
1121 /*-------------------------------------------------------------------------*/
1123 static int do_read(struct fsg_dev *fsg)
1125 struct lun *curlun = fsg->curlun;
1127 struct fsg_buffhd *bh;
1130 loff_t file_offset, file_offset_tmp;
1131 unsigned int amount;
1132 unsigned int partial_page;
1135 /* Get the starting Logical Block Address and check that it's
1137 if (fsg->cmnd[0] == SC_READ_6)
1138 lba = get_unaligned_be24(&fsg->cmnd[1]);
1140 lba = get_unaligned_be32(&fsg->cmnd[2]);
1142 /* We allow DPO (Disable Page Out = don't save data in the
1143 * cache) and FUA (Force Unit Access = don't read from the
1144 * cache), but we don't implement them. */
1145 if ((fsg->cmnd[1] & ~0x18) != 0) {
1146 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1150 if (lba >= curlun->num_sectors) {
1151 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1154 file_offset = ((loff_t) lba) << 9;
1156 /* Carry out the file reads */
1157 amount_left = fsg->data_size_from_cmnd;
1158 if (unlikely(amount_left == 0))
1159 return -EIO; // No default reply
1163 /* Figure out how much we need to read:
1164 * Try to read the remaining amount.
1165 * But don't read more than the buffer size.
1166 * And don't try to read past the end of the file.
1167 * Finally, if we're not at a page boundary, don't read past
1169 * If this means reading 0 then we were asked to read past
1170 * the end of file. */
1171 amount = min((unsigned int) amount_left, mod_data.buflen);
1172 amount = min((loff_t) amount,
1173 curlun->file_length - file_offset);
1174 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
1175 if (partial_page > 0)
1176 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
1179 /* Wait for the next buffer to become available */
1180 bh = fsg->next_buffhd_to_fill;
1181 while (bh->state != BUF_STATE_EMPTY) {
1182 rc = sleep_thread(fsg);
1187 /* If we were asked to read past the end of file,
1188 * end with an empty buffer. */
1190 curlun->sense_data =
1191 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1192 curlun->sense_data_info = file_offset >> 9;
1193 curlun->info_valid = 1;
1194 bh->inreq->length = 0;
1195 bh->state = BUF_STATE_FULL;
1199 /* Perform the read */
1200 file_offset_tmp = file_offset;
1201 nread = vfs_read(curlun->filp,
1202 (char __user *) bh->buf,
1203 amount, &file_offset_tmp);
1204 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1205 (unsigned long long) file_offset,
1207 if (signal_pending(current))
1211 LDBG(curlun, "error in file read: %d\n",
1214 } else if (nread < amount) {
1215 LDBG(curlun, "partial file read: %d/%u\n",
1216 (int) nread, amount);
1217 nread -= (nread & 511); // Round down to a block
1219 file_offset += nread;
1220 amount_left -= nread;
1221 fsg->residue -= nread;
1222 bh->inreq->length = nread;
1223 bh->state = BUF_STATE_FULL;
1225 /* If an error occurred, report it and its position */
1226 if (nread < amount) {
1227 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1228 curlun->sense_data_info = file_offset >> 9;
1229 curlun->info_valid = 1;
1233 if (amount_left == 0)
1234 break; // No more left to read
1236 /* Send this buffer and go read some more */
1237 bh->inreq->zero = 0;
1238 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1239 &bh->inreq_busy, &bh->state);
1240 fsg->next_buffhd_to_fill = bh->next;
1243 return -EIO; // No default reply
1247 /*-------------------------------------------------------------------------*/
1249 static int do_write(struct fsg_dev *fsg)
1251 struct lun *curlun = fsg->curlun;
1253 struct fsg_buffhd *bh;
1255 u32 amount_left_to_req, amount_left_to_write;
1256 loff_t usb_offset, file_offset, file_offset_tmp;
1257 unsigned int amount;
1258 unsigned int partial_page;
1263 curlun->sense_data = SS_WRITE_PROTECTED;
1266 spin_lock(&curlun->filp->f_lock);
1267 curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
1268 spin_unlock(&curlun->filp->f_lock);
1270 /* Get the starting Logical Block Address and check that it's
1272 if (fsg->cmnd[0] == SC_WRITE_6)
1273 lba = get_unaligned_be24(&fsg->cmnd[1]);
1275 lba = get_unaligned_be32(&fsg->cmnd[2]);
1277 /* We allow DPO (Disable Page Out = don't save data in the
1278 * cache) and FUA (Force Unit Access = write directly to the
1279 * medium). We don't implement DPO; we implement FUA by
1280 * performing synchronous output. */
1281 if ((fsg->cmnd[1] & ~0x18) != 0) {
1282 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1285 if (fsg->cmnd[1] & 0x08) { // FUA
1286 spin_lock(&curlun->filp->f_lock);
1287 curlun->filp->f_flags |= O_SYNC;
1288 spin_unlock(&curlun->filp->f_lock);
1291 if (lba >= curlun->num_sectors) {
1292 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1296 /* Carry out the file writes */
1298 file_offset = usb_offset = ((loff_t) lba) << 9;
1299 amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
1301 while (amount_left_to_write > 0) {
1303 /* Queue a request for more data from the host */
1304 bh = fsg->next_buffhd_to_fill;
1305 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
1307 /* Figure out how much we want to get:
1308 * Try to get the remaining amount.
1309 * But don't get more than the buffer size.
1310 * And don't try to go past the end of the file.
1311 * If we're not at a page boundary,
1312 * don't go past the next page.
1313 * If this means getting 0, then we were asked
1314 * to write past the end of file.
1315 * Finally, round down to a block boundary. */
1316 amount = min(amount_left_to_req, mod_data.buflen);
1317 amount = min((loff_t) amount, curlun->file_length -
1319 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
1320 if (partial_page > 0)
1321 amount = min(amount,
1322 (unsigned int) PAGE_CACHE_SIZE - partial_page);
1326 curlun->sense_data =
1327 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1328 curlun->sense_data_info = usb_offset >> 9;
1329 curlun->info_valid = 1;
1332 amount -= (amount & 511);
1335 /* Why were we were asked to transfer a
1341 /* Get the next buffer */
1342 usb_offset += amount;
1343 fsg->usb_amount_left -= amount;
1344 amount_left_to_req -= amount;
1345 if (amount_left_to_req == 0)
1348 /* amount is always divisible by 512, hence by
1349 * the bulk-out maxpacket size */
1350 bh->outreq->length = bh->bulk_out_intended_length =
1352 bh->outreq->short_not_ok = 1;
1353 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1354 &bh->outreq_busy, &bh->state);
1355 fsg->next_buffhd_to_fill = bh->next;
1359 /* Write the received data to the backing file */
1360 bh = fsg->next_buffhd_to_drain;
1361 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
1362 break; // We stopped early
1363 if (bh->state == BUF_STATE_FULL) {
1365 fsg->next_buffhd_to_drain = bh->next;
1366 bh->state = BUF_STATE_EMPTY;
1368 /* Did something go wrong with the transfer? */
1369 if (bh->outreq->status != 0) {
1370 curlun->sense_data = SS_COMMUNICATION_FAILURE;
1371 curlun->sense_data_info = file_offset >> 9;
1372 curlun->info_valid = 1;
1376 amount = bh->outreq->actual;
1377 if (curlun->file_length - file_offset < amount) {
1379 "write %u @ %llu beyond end %llu\n",
1380 amount, (unsigned long long) file_offset,
1381 (unsigned long long) curlun->file_length);
1382 amount = curlun->file_length - file_offset;
1385 /* Perform the write */
1386 file_offset_tmp = file_offset;
1387 nwritten = vfs_write(curlun->filp,
1388 (char __user *) bh->buf,
1389 amount, &file_offset_tmp);
1390 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1391 (unsigned long long) file_offset,
1393 if (signal_pending(current))
1394 return -EINTR; // Interrupted!
1397 LDBG(curlun, "error in file write: %d\n",
1400 } else if (nwritten < amount) {
1401 LDBG(curlun, "partial file write: %d/%u\n",
1402 (int) nwritten, amount);
1403 nwritten -= (nwritten & 511);
1404 // Round down to a block
1406 file_offset += nwritten;
1407 amount_left_to_write -= nwritten;
1408 fsg->residue -= nwritten;
1410 /* If an error occurred, report it and its position */
1411 if (nwritten < amount) {
1412 curlun->sense_data = SS_WRITE_ERROR;
1413 curlun->sense_data_info = file_offset >> 9;
1414 curlun->info_valid = 1;
1418 /* Did the host decide to stop early? */
1419 if (bh->outreq->actual != bh->outreq->length) {
1420 fsg->short_packet_received = 1;
1426 /* Wait for something to happen */
1427 rc = sleep_thread(fsg);
1432 return -EIO; // No default reply
1436 /*-------------------------------------------------------------------------*/
1438 static int do_synchronize_cache(struct fsg_dev *fsg)
1440 struct lun *curlun = fsg->curlun;
1443 /* We ignore the requested LBA and write out all file's
1444 * dirty data buffers. */
1445 rc = fsync_sub(curlun);
1447 curlun->sense_data = SS_WRITE_ERROR;
1452 /*-------------------------------------------------------------------------*/
1454 static void invalidate_sub(struct lun *curlun)
1456 struct file *filp = curlun->filp;
1457 struct inode *inode = filp->f_path.dentry->d_inode;
1460 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
1461 VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc);
1464 static int do_verify(struct fsg_dev *fsg)
1466 struct lun *curlun = fsg->curlun;
1468 u32 verification_length;
1469 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1470 loff_t file_offset, file_offset_tmp;
1472 unsigned int amount;
1475 /* Get the starting Logical Block Address and check that it's
1477 lba = get_unaligned_be32(&fsg->cmnd[2]);
1478 if (lba >= curlun->num_sectors) {
1479 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1483 /* We allow DPO (Disable Page Out = don't save data in the
1484 * cache) but we don't implement it. */
1485 if ((fsg->cmnd[1] & ~0x10) != 0) {
1486 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1490 verification_length = get_unaligned_be16(&fsg->cmnd[7]);
1491 if (unlikely(verification_length == 0))
1492 return -EIO; // No default reply
1494 /* Prepare to carry out the file verify */
1495 amount_left = verification_length << 9;
1496 file_offset = ((loff_t) lba) << 9;
1498 /* Write out all the dirty buffers before invalidating them */
1500 if (signal_pending(current))
1503 invalidate_sub(curlun);
1504 if (signal_pending(current))
1507 /* Just try to read the requested blocks */
1508 while (amount_left > 0) {
1510 /* Figure out how much we need to read:
1511 * Try to read the remaining amount, but not more than
1513 * And don't try to read past the end of the file.
1514 * If this means reading 0 then we were asked to read
1515 * past the end of file. */
1516 amount = min((unsigned int) amount_left, mod_data.buflen);
1517 amount = min((loff_t) amount,
1518 curlun->file_length - file_offset);
1520 curlun->sense_data =
1521 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1522 curlun->sense_data_info = file_offset >> 9;
1523 curlun->info_valid = 1;
1527 /* Perform the read */
1528 file_offset_tmp = file_offset;
1529 nread = vfs_read(curlun->filp,
1530 (char __user *) bh->buf,
1531 amount, &file_offset_tmp);
1532 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1533 (unsigned long long) file_offset,
1535 if (signal_pending(current))
1539 LDBG(curlun, "error in file verify: %d\n",
1542 } else if (nread < amount) {
1543 LDBG(curlun, "partial file verify: %d/%u\n",
1544 (int) nread, amount);
1545 nread -= (nread & 511); // Round down to a sector
1548 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1549 curlun->sense_data_info = file_offset >> 9;
1550 curlun->info_valid = 1;
1553 file_offset += nread;
1554 amount_left -= nread;
1560 /*-------------------------------------------------------------------------*/
1562 static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1564 u8 *buf = (u8 *) bh->buf;
1566 static char vendor_id[] = "Linux ";
1567 static char product_disk_id[] = "File-Stor Gadget";
1568 static char product_cdrom_id[] = "File-CD Gadget ";
1570 if (!fsg->curlun) { // Unsupported LUNs are okay
1571 fsg->bad_lun_okay = 1;
1573 buf[0] = 0x7f; // Unsupported, no device-type
1574 buf[4] = 31; // Additional length
1579 buf[0] = (mod_data.cdrom ? TYPE_CDROM : TYPE_DISK);
1580 if (mod_data.removable)
1582 buf[2] = 2; // ANSI SCSI level 2
1583 buf[3] = 2; // SCSI-2 INQUIRY data format
1584 buf[4] = 31; // Additional length
1585 // No special options
1586 sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
1587 (mod_data.cdrom ? product_cdrom_id :
1594 static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1596 struct lun *curlun = fsg->curlun;
1597 u8 *buf = (u8 *) bh->buf;
1602 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1604 * If a REQUEST SENSE command is received from an initiator
1605 * with a pending unit attention condition (before the target
1606 * generates the contingent allegiance condition), then the
1607 * target shall either:
1608 * a) report any pending sense data and preserve the unit
1609 * attention condition on the logical unit, or,
1610 * b) report the unit attention condition, may discard any
1611 * pending sense data, and clear the unit attention
1612 * condition on the logical unit for that initiator.
1614 * FSG normally uses option a); enable this code to use option b).
1617 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1618 curlun->sense_data = curlun->unit_attention_data;
1619 curlun->unit_attention_data = SS_NO_SENSE;
1623 if (!curlun) { // Unsupported LUNs are okay
1624 fsg->bad_lun_okay = 1;
1625 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1629 sd = curlun->sense_data;
1630 sdinfo = curlun->sense_data_info;
1631 valid = curlun->info_valid << 7;
1632 curlun->sense_data = SS_NO_SENSE;
1633 curlun->sense_data_info = 0;
1634 curlun->info_valid = 0;
1638 buf[0] = valid | 0x70; // Valid, current error
1640 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
1641 buf[7] = 18 - 8; // Additional sense length
1648 static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1650 struct lun *curlun = fsg->curlun;
1651 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
1652 int pmi = fsg->cmnd[8];
1653 u8 *buf = (u8 *) bh->buf;
1655 /* Check the PMI and LBA fields */
1656 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1657 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1661 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1662 /* Max logical block */
1663 put_unaligned_be32(512, &buf[4]); /* Block length */
1668 static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1670 struct lun *curlun = fsg->curlun;
1671 int msf = fsg->cmnd[1] & 0x02;
1672 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
1673 u8 *buf = (u8 *) bh->buf;
1675 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
1676 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1679 if (lba >= curlun->num_sectors) {
1680 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1685 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1686 store_cdrom_address(&buf[4], msf, lba);
1691 static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1693 struct lun *curlun = fsg->curlun;
1694 int msf = fsg->cmnd[1] & 0x02;
1695 int start_track = fsg->cmnd[6];
1696 u8 *buf = (u8 *) bh->buf;
1698 if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1700 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1705 buf[1] = (20-2); /* TOC data length */
1706 buf[2] = 1; /* First track number */
1707 buf[3] = 1; /* Last track number */
1708 buf[5] = 0x16; /* Data track, copying allowed */
1709 buf[6] = 0x01; /* Only track is number 1 */
1710 store_cdrom_address(&buf[8], msf, 0);
1712 buf[13] = 0x16; /* Lead-out track is data */
1713 buf[14] = 0xAA; /* Lead-out track number */
1714 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1719 static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1721 struct lun *curlun = fsg->curlun;
1722 int mscmnd = fsg->cmnd[0];
1723 u8 *buf = (u8 *) bh->buf;
1726 int changeable_values, all_pages;
1730 if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
1731 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1734 pc = fsg->cmnd[2] >> 6;
1735 page_code = fsg->cmnd[2] & 0x3f;
1737 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1740 changeable_values = (pc == 1);
1741 all_pages = (page_code == 0x3f);
1743 /* Write the mode parameter header. Fixed values are: default
1744 * medium type, no cache control (DPOFUA), and no block descriptors.
1745 * The only variable value is the WriteProtect bit. We will fill in
1746 * the mode data length later. */
1748 if (mscmnd == SC_MODE_SENSE_6) {
1749 buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1752 } else { // SC_MODE_SENSE_10
1753 buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1755 limit = 65535; // Should really be mod_data.buflen
1758 /* No block descriptors */
1760 /* The mode pages, in numerical order. The only page we support
1761 * is the Caching page. */
1762 if (page_code == 0x08 || all_pages) {
1764 buf[0] = 0x08; // Page code
1765 buf[1] = 10; // Page length
1766 memset(buf+2, 0, 10); // None of the fields are changeable
1768 if (!changeable_values) {
1769 buf[2] = 0x04; // Write cache enable,
1770 // Read cache not disabled
1771 // No cache retention priorities
1772 put_unaligned_be16(0xffff, &buf[4]);
1773 /* Don't disable prefetch */
1774 /* Minimum prefetch = 0 */
1775 put_unaligned_be16(0xffff, &buf[8]);
1776 /* Maximum prefetch */
1777 put_unaligned_be16(0xffff, &buf[10]);
1778 /* Maximum prefetch ceiling */
1783 /* Check that a valid page was requested and the mode data length
1784 * isn't too long. */
1786 if (!valid_page || len > limit) {
1787 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1791 /* Store the mode data length */
1792 if (mscmnd == SC_MODE_SENSE_6)
1795 put_unaligned_be16(len - 2, buf0);
1800 static int do_start_stop(struct fsg_dev *fsg)
1802 struct lun *curlun = fsg->curlun;
1805 if (!mod_data.removable) {
1806 curlun->sense_data = SS_INVALID_COMMAND;
1810 // int immed = fsg->cmnd[1] & 0x01;
1811 loej = fsg->cmnd[4] & 0x02;
1812 start = fsg->cmnd[4] & 0x01;
1814 #ifdef CONFIG_USB_FILE_STORAGE_TEST
1815 if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
1816 (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
1817 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1823 /* Are we allowed to unload the media? */
1824 if (curlun->prevent_medium_removal) {
1825 LDBG(curlun, "unload attempt prevented\n");
1826 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1829 if (loej) { // Simulate an unload/eject
1830 up_read(&fsg->filesem);
1831 down_write(&fsg->filesem);
1832 close_backing_file(curlun);
1833 up_write(&fsg->filesem);
1834 down_read(&fsg->filesem);
1838 /* Our emulation doesn't support mounting; the medium is
1839 * available for use as soon as it is loaded. */
1840 if (!backing_file_is_open(curlun)) {
1841 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1850 static int do_prevent_allow(struct fsg_dev *fsg)
1852 struct lun *curlun = fsg->curlun;
1855 if (!mod_data.removable) {
1856 curlun->sense_data = SS_INVALID_COMMAND;
1860 prevent = fsg->cmnd[4] & 0x01;
1861 if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
1862 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1866 if (curlun->prevent_medium_removal && !prevent)
1868 curlun->prevent_medium_removal = prevent;
1873 static int do_read_format_capacities(struct fsg_dev *fsg,
1874 struct fsg_buffhd *bh)
1876 struct lun *curlun = fsg->curlun;
1877 u8 *buf = (u8 *) bh->buf;
1879 buf[0] = buf[1] = buf[2] = 0;
1880 buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
1883 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1884 /* Number of blocks */
1885 put_unaligned_be32(512, &buf[4]); /* Block length */
1886 buf[4] = 0x02; /* Current capacity */
1891 static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1893 struct lun *curlun = fsg->curlun;
1895 /* We don't support MODE SELECT */
1896 curlun->sense_data = SS_INVALID_COMMAND;
1901 /*-------------------------------------------------------------------------*/
1903 static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1907 rc = fsg_set_halt(fsg, fsg->bulk_in);
1909 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1911 if (rc != -EAGAIN) {
1912 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1917 /* Wait for a short time and then try again */
1918 if (msleep_interruptible(100) != 0)
1920 rc = usb_ep_set_halt(fsg->bulk_in);
1925 static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1929 DBG(fsg, "bulk-in set wedge\n");
1930 rc = usb_ep_set_wedge(fsg->bulk_in);
1932 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1934 if (rc != -EAGAIN) {
1935 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1940 /* Wait for a short time and then try again */
1941 if (msleep_interruptible(100) != 0)
1943 rc = usb_ep_set_wedge(fsg->bulk_in);
1948 static int pad_with_zeros(struct fsg_dev *fsg)
1950 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1951 u32 nkeep = bh->inreq->length;
1955 bh->state = BUF_STATE_EMPTY; // For the first iteration
1956 fsg->usb_amount_left = nkeep + fsg->residue;
1957 while (fsg->usb_amount_left > 0) {
1959 /* Wait for the next buffer to be free */
1960 while (bh->state != BUF_STATE_EMPTY) {
1961 rc = sleep_thread(fsg);
1966 nsend = min(fsg->usb_amount_left, (u32) mod_data.buflen);
1967 memset(bh->buf + nkeep, 0, nsend - nkeep);
1968 bh->inreq->length = nsend;
1969 bh->inreq->zero = 0;
1970 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1971 &bh->inreq_busy, &bh->state);
1972 bh = fsg->next_buffhd_to_fill = bh->next;
1973 fsg->usb_amount_left -= nsend;
1979 static int throw_away_data(struct fsg_dev *fsg)
1981 struct fsg_buffhd *bh;
1985 while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
1986 fsg->usb_amount_left > 0) {
1988 /* Throw away the data in a filled buffer */
1989 if (bh->state == BUF_STATE_FULL) {
1991 bh->state = BUF_STATE_EMPTY;
1992 fsg->next_buffhd_to_drain = bh->next;
1994 /* A short packet or an error ends everything */
1995 if (bh->outreq->actual != bh->outreq->length ||
1996 bh->outreq->status != 0) {
1997 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2003 /* Try to submit another request if we need one */
2004 bh = fsg->next_buffhd_to_fill;
2005 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
2006 amount = min(fsg->usb_amount_left,
2007 (u32) mod_data.buflen);
2009 /* amount is always divisible by 512, hence by
2010 * the bulk-out maxpacket size */
2011 bh->outreq->length = bh->bulk_out_intended_length =
2013 bh->outreq->short_not_ok = 1;
2014 start_transfer(fsg, fsg->bulk_out, bh->outreq,
2015 &bh->outreq_busy, &bh->state);
2016 fsg->next_buffhd_to_fill = bh->next;
2017 fsg->usb_amount_left -= amount;
2021 /* Otherwise wait for something to happen */
2022 rc = sleep_thread(fsg);
2030 static int finish_reply(struct fsg_dev *fsg)
2032 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
2035 switch (fsg->data_dir) {
2037 break; // Nothing to send
2039 /* If we don't know whether the host wants to read or write,
2040 * this must be CB or CBI with an unknown command. We mustn't
2041 * try to send or receive any data. So stall both bulk pipes
2042 * if we can and wait for a reset. */
2043 case DATA_DIR_UNKNOWN:
2044 if (mod_data.can_stall) {
2045 fsg_set_halt(fsg, fsg->bulk_out);
2046 rc = halt_bulk_in_endpoint(fsg);
2050 /* All but the last buffer of data must have already been sent */
2051 case DATA_DIR_TO_HOST:
2052 if (fsg->data_size == 0)
2053 ; // Nothing to send
2055 /* If there's no residue, simply send the last buffer */
2056 else if (fsg->residue == 0) {
2057 bh->inreq->zero = 0;
2058 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2059 &bh->inreq_busy, &bh->state);
2060 fsg->next_buffhd_to_fill = bh->next;
2063 /* There is a residue. For CB and CBI, simply mark the end
2064 * of the data with a short packet. However, if we are
2065 * allowed to stall, there was no data at all (residue ==
2066 * data_size), and the command failed (invalid LUN or
2067 * sense data is set), then halt the bulk-in endpoint
2069 else if (!transport_is_bbb()) {
2070 if (mod_data.can_stall &&
2071 fsg->residue == fsg->data_size &&
2072 (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
2073 bh->state = BUF_STATE_EMPTY;
2074 rc = halt_bulk_in_endpoint(fsg);
2076 bh->inreq->zero = 1;
2077 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2078 &bh->inreq_busy, &bh->state);
2079 fsg->next_buffhd_to_fill = bh->next;
2083 /* For Bulk-only, if we're allowed to stall then send the
2084 * short packet and halt the bulk-in endpoint. If we can't
2085 * stall, pad out the remaining data with 0's. */
2087 if (mod_data.can_stall) {
2088 bh->inreq->zero = 1;
2089 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2090 &bh->inreq_busy, &bh->state);
2091 fsg->next_buffhd_to_fill = bh->next;
2092 rc = halt_bulk_in_endpoint(fsg);
2094 rc = pad_with_zeros(fsg);
2098 /* We have processed all we want from the data the host has sent.
2099 * There may still be outstanding bulk-out requests. */
2100 case DATA_DIR_FROM_HOST:
2101 if (fsg->residue == 0)
2102 ; // Nothing to receive
2104 /* Did the host stop sending unexpectedly early? */
2105 else if (fsg->short_packet_received) {
2106 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2110 /* We haven't processed all the incoming data. Even though
2111 * we may be allowed to stall, doing so would cause a race.
2112 * The controller may already have ACK'ed all the remaining
2113 * bulk-out packets, in which case the host wouldn't see a
2114 * STALL. Not realizing the endpoint was halted, it wouldn't
2115 * clear the halt -- leading to problems later on. */
2117 else if (mod_data.can_stall) {
2118 fsg_set_halt(fsg, fsg->bulk_out);
2119 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2124 /* We can't stall. Read in the excess data and throw it
2127 rc = throw_away_data(fsg);
2134 static int send_status(struct fsg_dev *fsg)
2136 struct lun *curlun = fsg->curlun;
2137 struct fsg_buffhd *bh;
2139 u8 status = USB_STATUS_PASS;
2142 /* Wait for the next buffer to become available */
2143 bh = fsg->next_buffhd_to_fill;
2144 while (bh->state != BUF_STATE_EMPTY) {
2145 rc = sleep_thread(fsg);
2151 sd = curlun->sense_data;
2152 sdinfo = curlun->sense_data_info;
2153 } else if (fsg->bad_lun_okay)
2156 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2158 if (fsg->phase_error) {
2159 DBG(fsg, "sending phase-error status\n");
2160 status = USB_STATUS_PHASE_ERROR;
2161 sd = SS_INVALID_COMMAND;
2162 } else if (sd != SS_NO_SENSE) {
2163 DBG(fsg, "sending command-failure status\n");
2164 status = USB_STATUS_FAIL;
2165 VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2167 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
2170 if (transport_is_bbb()) {
2171 struct bulk_cs_wrap *csw = bh->buf;
2173 /* Store and send the Bulk-only CSW */
2174 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
2175 csw->Tag = fsg->tag;
2176 csw->Residue = cpu_to_le32(fsg->residue);
2177 csw->Status = status;
2179 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
2180 bh->inreq->zero = 0;
2181 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2182 &bh->inreq_busy, &bh->state);
2184 } else if (mod_data.transport_type == USB_PR_CB) {
2186 /* Control-Bulk transport has no status phase! */
2189 } else { // USB_PR_CBI
2190 struct interrupt_data *buf = bh->buf;
2192 /* Store and send the Interrupt data. UFI sends the ASC
2193 * and ASCQ bytes. Everything else sends a Type (which
2194 * is always 0) and the status Value. */
2195 if (mod_data.protocol_type == USB_SC_UFI) {
2196 buf->bType = ASC(sd);
2197 buf->bValue = ASCQ(sd);
2200 buf->bValue = status;
2202 fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
2204 fsg->intr_buffhd = bh; // Point to the right buffhd
2205 fsg->intreq->buf = bh->inreq->buf;
2206 fsg->intreq->context = bh;
2207 start_transfer(fsg, fsg->intr_in, fsg->intreq,
2208 &fsg->intreq_busy, &bh->state);
2211 fsg->next_buffhd_to_fill = bh->next;
2216 /*-------------------------------------------------------------------------*/
2218 /* Check whether the command is properly formed and whether its data size
2219 * and direction agree with the values we already have. */
2220 static int check_command(struct fsg_dev *fsg, int cmnd_size,
2221 enum data_direction data_dir, unsigned int mask,
2222 int needs_medium, const char *name)
2225 int lun = fsg->cmnd[1] >> 5;
2226 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
2230 /* Adjust the expected cmnd_size for protocol encapsulation padding.
2231 * Transparent SCSI doesn't pad. */
2232 if (protocol_is_scsi())
2235 /* There's some disagreement as to whether RBC pads commands or not.
2236 * We'll play it safe and accept either form. */
2237 else if (mod_data.protocol_type == USB_SC_RBC) {
2238 if (fsg->cmnd_size == 12)
2241 /* All the other protocols pad to 12 bytes */
2246 if (fsg->data_dir != DATA_DIR_UNKNOWN)
2247 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
2249 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
2250 name, cmnd_size, dirletter[(int) data_dir],
2251 fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
2253 /* We can't reply at all until we know the correct data direction
2255 if (fsg->data_size_from_cmnd == 0)
2256 data_dir = DATA_DIR_NONE;
2257 if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
2258 fsg->data_dir = data_dir;
2259 fsg->data_size = fsg->data_size_from_cmnd;
2261 } else { // Bulk-only
2262 if (fsg->data_size < fsg->data_size_from_cmnd) {
2264 /* Host data size < Device data size is a phase error.
2265 * Carry out the command, but only transfer as much
2266 * as we are allowed. */
2267 fsg->data_size_from_cmnd = fsg->data_size;
2268 fsg->phase_error = 1;
2271 fsg->residue = fsg->usb_amount_left = fsg->data_size;
2273 /* Conflicting data directions is a phase error */
2274 if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
2275 fsg->phase_error = 1;
2279 /* Verify the length of the command itself */
2280 if (cmnd_size != fsg->cmnd_size) {
2282 /* Special case workaround: There are plenty of buggy SCSI
2283 * implementations. Many have issues with cbw->Length
2284 * field passing a wrong command size. For those cases we
2285 * always try to work around the problem by using the length
2286 * sent by the host side provided it is at least as large
2287 * as the correct command length.
2288 * Examples of such cases would be MS-Windows, which issues
2289 * REQUEST SENSE with cbw->Length == 12 where it should
2290 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
2291 * REQUEST SENSE with cbw->Length == 10 where it should
2294 if (cmnd_size <= fsg->cmnd_size) {
2295 DBG(fsg, "%s is buggy! Expected length %d "
2296 "but we got %d\n", name,
2297 cmnd_size, fsg->cmnd_size);
2298 cmnd_size = fsg->cmnd_size;
2300 fsg->phase_error = 1;
2305 /* Check that the LUN values are consistent */
2306 if (transport_is_bbb()) {
2307 if (fsg->lun != lun)
2308 DBG(fsg, "using LUN %d from CBW, "
2309 "not LUN %d from CDB\n",
2312 fsg->lun = lun; // Use LUN from the command
2315 if (fsg->lun >= 0 && fsg->lun < fsg->nluns) {
2316 fsg->curlun = curlun = &fsg->luns[fsg->lun];
2317 if (fsg->cmnd[0] != SC_REQUEST_SENSE) {
2318 curlun->sense_data = SS_NO_SENSE;
2319 curlun->sense_data_info = 0;
2320 curlun->info_valid = 0;
2323 fsg->curlun = curlun = NULL;
2324 fsg->bad_lun_okay = 0;
2326 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2327 * to use unsupported LUNs; all others may not. */
2328 if (fsg->cmnd[0] != SC_INQUIRY &&
2329 fsg->cmnd[0] != SC_REQUEST_SENSE) {
2330 DBG(fsg, "unsupported LUN %d\n", fsg->lun);
2335 /* If a unit attention condition exists, only INQUIRY and
2336 * REQUEST SENSE commands are allowed; anything else must fail. */
2337 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
2338 fsg->cmnd[0] != SC_INQUIRY &&
2339 fsg->cmnd[0] != SC_REQUEST_SENSE) {
2340 curlun->sense_data = curlun->unit_attention_data;
2341 curlun->unit_attention_data = SS_NO_SENSE;
2345 /* Check that only command bytes listed in the mask are non-zero */
2346 fsg->cmnd[1] &= 0x1f; // Mask away the LUN
2347 for (i = 1; i < cmnd_size; ++i) {
2348 if (fsg->cmnd[i] && !(mask & (1 << i))) {
2350 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2355 /* If the medium isn't mounted and the command needs to access
2356 * it, return an error. */
2357 if (curlun && !backing_file_is_open(curlun) && needs_medium) {
2358 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2366 static int do_scsi_command(struct fsg_dev *fsg)
2368 struct fsg_buffhd *bh;
2370 int reply = -EINVAL;
2372 static char unknown[16];
2376 /* Wait for the next buffer to become available for data or status */
2377 bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
2378 while (bh->state != BUF_STATE_EMPTY) {
2379 rc = sleep_thread(fsg);
2383 fsg->phase_error = 0;
2384 fsg->short_packet_received = 0;
2386 down_read(&fsg->filesem); // We're using the backing file
2387 switch (fsg->cmnd[0]) {
2390 fsg->data_size_from_cmnd = fsg->cmnd[4];
2391 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2394 reply = do_inquiry(fsg, bh);
2397 case SC_MODE_SELECT_6:
2398 fsg->data_size_from_cmnd = fsg->cmnd[4];
2399 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2401 "MODE SELECT(6)")) == 0)
2402 reply = do_mode_select(fsg, bh);
2405 case SC_MODE_SELECT_10:
2406 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2407 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2409 "MODE SELECT(10)")) == 0)
2410 reply = do_mode_select(fsg, bh);
2413 case SC_MODE_SENSE_6:
2414 fsg->data_size_from_cmnd = fsg->cmnd[4];
2415 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2416 (1<<1) | (1<<2) | (1<<4), 0,
2417 "MODE SENSE(6)")) == 0)
2418 reply = do_mode_sense(fsg, bh);
2421 case SC_MODE_SENSE_10:
2422 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2423 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2424 (1<<1) | (1<<2) | (3<<7), 0,
2425 "MODE SENSE(10)")) == 0)
2426 reply = do_mode_sense(fsg, bh);
2429 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
2430 fsg->data_size_from_cmnd = 0;
2431 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2433 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
2434 reply = do_prevent_allow(fsg);
2439 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2440 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2443 reply = do_read(fsg);
2447 fsg->data_size_from_cmnd =
2448 get_unaligned_be16(&fsg->cmnd[7]) << 9;
2449 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2450 (1<<1) | (0xf<<2) | (3<<7), 1,
2452 reply = do_read(fsg);
2456 fsg->data_size_from_cmnd =
2457 get_unaligned_be32(&fsg->cmnd[6]) << 9;
2458 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2459 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2461 reply = do_read(fsg);
2464 case SC_READ_CAPACITY:
2465 fsg->data_size_from_cmnd = 8;
2466 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2467 (0xf<<2) | (1<<8), 1,
2468 "READ CAPACITY")) == 0)
2469 reply = do_read_capacity(fsg, bh);
2472 case SC_READ_HEADER:
2473 if (!mod_data.cdrom)
2475 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2476 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2477 (3<<7) | (0x1f<<1), 1,
2478 "READ HEADER")) == 0)
2479 reply = do_read_header(fsg, bh);
2483 if (!mod_data.cdrom)
2485 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2486 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2489 reply = do_read_toc(fsg, bh);
2492 case SC_READ_FORMAT_CAPACITIES:
2493 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2494 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2496 "READ FORMAT CAPACITIES")) == 0)
2497 reply = do_read_format_capacities(fsg, bh);
2500 case SC_REQUEST_SENSE:
2501 fsg->data_size_from_cmnd = fsg->cmnd[4];
2502 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2504 "REQUEST SENSE")) == 0)
2505 reply = do_request_sense(fsg, bh);
2508 case SC_START_STOP_UNIT:
2509 fsg->data_size_from_cmnd = 0;
2510 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2512 "START-STOP UNIT")) == 0)
2513 reply = do_start_stop(fsg);
2516 case SC_SYNCHRONIZE_CACHE:
2517 fsg->data_size_from_cmnd = 0;
2518 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2519 (0xf<<2) | (3<<7), 1,
2520 "SYNCHRONIZE CACHE")) == 0)
2521 reply = do_synchronize_cache(fsg);
2524 case SC_TEST_UNIT_READY:
2525 fsg->data_size_from_cmnd = 0;
2526 reply = check_command(fsg, 6, DATA_DIR_NONE,
2531 /* Although optional, this command is used by MS-Windows. We
2532 * support a minimal version: BytChk must be 0. */
2534 fsg->data_size_from_cmnd = 0;
2535 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2536 (1<<1) | (0xf<<2) | (3<<7), 1,
2538 reply = do_verify(fsg);
2543 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2544 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2547 reply = do_write(fsg);
2551 fsg->data_size_from_cmnd =
2552 get_unaligned_be16(&fsg->cmnd[7]) << 9;
2553 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2554 (1<<1) | (0xf<<2) | (3<<7), 1,
2556 reply = do_write(fsg);
2560 fsg->data_size_from_cmnd =
2561 get_unaligned_be32(&fsg->cmnd[6]) << 9;
2562 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
2563 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2565 reply = do_write(fsg);
2568 /* Some mandatory commands that we recognize but don't implement.
2569 * They don't mean much in this setting. It's left as an exercise
2570 * for anyone interested to implement RESERVE and RELEASE in terms
2571 * of Posix locks. */
2572 case SC_FORMAT_UNIT:
2575 case SC_SEND_DIAGNOSTIC:
2580 fsg->data_size_from_cmnd = 0;
2581 sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
2582 if ((reply = check_command(fsg, fsg->cmnd_size,
2583 DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) {
2584 fsg->curlun->sense_data = SS_INVALID_COMMAND;
2589 up_read(&fsg->filesem);
2591 if (reply == -EINTR || signal_pending(current))
2594 /* Set up the single reply buffer for finish_reply() */
2595 if (reply == -EINVAL)
2596 reply = 0; // Error reply length
2597 if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
2598 reply = min((u32) reply, fsg->data_size_from_cmnd);
2599 bh->inreq->length = reply;
2600 bh->state = BUF_STATE_FULL;
2601 fsg->residue -= reply;
2602 } // Otherwise it's already set
2608 /*-------------------------------------------------------------------------*/
2610 static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2612 struct usb_request *req = bh->outreq;
2613 struct bulk_cb_wrap *cbw = req->buf;
2615 /* Was this a real packet? Should it be ignored? */
2616 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2619 /* Is the CBW valid? */
2620 if (req->actual != USB_BULK_CB_WRAP_LEN ||
2621 cbw->Signature != cpu_to_le32(
2623 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2625 le32_to_cpu(cbw->Signature));
2627 /* The Bulk-only spec says we MUST stall the IN endpoint
2628 * (6.6.1), so it's unavoidable. It also says we must
2629 * retain this state until the next reset, but there's
2630 * no way to tell the controller driver it should ignore
2631 * Clear-Feature(HALT) requests.
2633 * We aren't required to halt the OUT endpoint; instead
2634 * we can simply accept and discard any data received
2635 * until the next reset. */
2636 wedge_bulk_in_endpoint(fsg);
2637 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2641 /* Is the CBW meaningful? */
2642 if (cbw->Lun >= MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2643 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2644 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2646 cbw->Lun, cbw->Flags, cbw->Length);
2648 /* We can do anything we want here, so let's stall the
2649 * bulk pipes if we are allowed to. */
2650 if (mod_data.can_stall) {
2651 fsg_set_halt(fsg, fsg->bulk_out);
2652 halt_bulk_in_endpoint(fsg);
2657 /* Save the command for later */
2658 fsg->cmnd_size = cbw->Length;
2659 memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
2660 if (cbw->Flags & USB_BULK_IN_FLAG)
2661 fsg->data_dir = DATA_DIR_TO_HOST;
2663 fsg->data_dir = DATA_DIR_FROM_HOST;
2664 fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
2665 if (fsg->data_size == 0)
2666 fsg->data_dir = DATA_DIR_NONE;
2667 fsg->lun = cbw->Lun;
2668 fsg->tag = cbw->Tag;
2673 static int get_next_command(struct fsg_dev *fsg)
2675 struct fsg_buffhd *bh;
2678 if (transport_is_bbb()) {
2680 /* Wait for the next buffer to become available */
2681 bh = fsg->next_buffhd_to_fill;
2682 while (bh->state != BUF_STATE_EMPTY) {
2683 rc = sleep_thread(fsg);
2688 /* Queue a request to read a Bulk-only CBW */
2689 set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN);
2690 bh->outreq->short_not_ok = 1;
2691 start_transfer(fsg, fsg->bulk_out, bh->outreq,
2692 &bh->outreq_busy, &bh->state);
2694 /* We will drain the buffer in software, which means we
2695 * can reuse it for the next filling. No need to advance
2696 * next_buffhd_to_fill. */
2698 /* Wait for the CBW to arrive */
2699 while (bh->state != BUF_STATE_FULL) {
2700 rc = sleep_thread(fsg);
2705 rc = received_cbw(fsg, bh);
2706 bh->state = BUF_STATE_EMPTY;
2708 } else { // USB_PR_CB or USB_PR_CBI
2710 /* Wait for the next command to arrive */
2711 while (fsg->cbbuf_cmnd_size == 0) {
2712 rc = sleep_thread(fsg);
2717 /* Is the previous status interrupt request still busy?
2718 * The host is allowed to skip reading the status,
2719 * so we must cancel it. */
2720 if (fsg->intreq_busy)
2721 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2723 /* Copy the command and mark the buffer empty */
2724 fsg->data_dir = DATA_DIR_UNKNOWN;
2725 spin_lock_irq(&fsg->lock);
2726 fsg->cmnd_size = fsg->cbbuf_cmnd_size;
2727 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
2728 fsg->cbbuf_cmnd_size = 0;
2729 spin_unlock_irq(&fsg->lock);
2735 /*-------------------------------------------------------------------------*/
2737 static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
2738 const struct usb_endpoint_descriptor *d)
2742 ep->driver_data = fsg;
2743 rc = usb_ep_enable(ep, d);
2745 ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
2749 static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
2750 struct usb_request **preq)
2752 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2755 ERROR(fsg, "can't allocate request for %s\n", ep->name);
2760 * Reset interface setting and re-init endpoint state (toggle etc).
2761 * Call with altsetting < 0 to disable the interface. The only other
2762 * available altsetting is 0, which enables the interface.
2764 static int do_set_interface(struct fsg_dev *fsg, int altsetting)
2768 const struct usb_endpoint_descriptor *d;
2771 DBG(fsg, "reset interface\n");
2774 /* Deallocate the requests */
2775 for (i = 0; i < NUM_BUFFERS; ++i) {
2776 struct fsg_buffhd *bh = &fsg->buffhds[i];
2779 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2783 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2788 usb_ep_free_request(fsg->intr_in, fsg->intreq);
2792 /* Disable the endpoints */
2793 if (fsg->bulk_in_enabled) {
2794 usb_ep_disable(fsg->bulk_in);
2795 fsg->bulk_in_enabled = 0;
2797 if (fsg->bulk_out_enabled) {
2798 usb_ep_disable(fsg->bulk_out);
2799 fsg->bulk_out_enabled = 0;
2801 if (fsg->intr_in_enabled) {
2802 usb_ep_disable(fsg->intr_in);
2803 fsg->intr_in_enabled = 0;
2807 if (altsetting < 0 || rc != 0)
2810 DBG(fsg, "set interface %d\n", altsetting);
2812 /* Enable the endpoints */
2813 d = ep_desc(fsg->gadget, &fs_bulk_in_desc, &hs_bulk_in_desc);
2814 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
2816 fsg->bulk_in_enabled = 1;
2818 d = ep_desc(fsg->gadget, &fs_bulk_out_desc, &hs_bulk_out_desc);
2819 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
2821 fsg->bulk_out_enabled = 1;
2822 fsg->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2823 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2825 if (transport_is_cbi()) {
2826 d = ep_desc(fsg->gadget, &fs_intr_in_desc, &hs_intr_in_desc);
2827 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
2829 fsg->intr_in_enabled = 1;
2832 /* Allocate the requests */
2833 for (i = 0; i < NUM_BUFFERS; ++i) {
2834 struct fsg_buffhd *bh = &fsg->buffhds[i];
2836 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
2838 if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
2840 bh->inreq->buf = bh->outreq->buf = bh->buf;
2841 bh->inreq->context = bh->outreq->context = bh;
2842 bh->inreq->complete = bulk_in_complete;
2843 bh->outreq->complete = bulk_out_complete;
2845 if (transport_is_cbi()) {
2846 if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
2848 fsg->intreq->complete = intr_in_complete;
2852 for (i = 0; i < fsg->nluns; ++i)
2853 fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2859 * Change our operational configuration. This code must agree with the code
2860 * that returns config descriptors, and with interface altsetting code.
2862 * It's also responsible for power management interactions. Some
2863 * configurations might not work with our current power sources.
2864 * For now we just assume the gadget is always self-powered.
2866 static int do_set_config(struct fsg_dev *fsg, u8 new_config)
2870 /* Disable the single interface */
2871 if (fsg->config != 0) {
2872 DBG(fsg, "reset config\n");
2874 rc = do_set_interface(fsg, -1);
2877 /* Enable the interface */
2878 if (new_config != 0) {
2879 fsg->config = new_config;
2880 if ((rc = do_set_interface(fsg, 0)) != 0)
2881 fsg->config = 0; // Reset on errors
2885 switch (fsg->gadget->speed) {
2886 case USB_SPEED_LOW: speed = "low"; break;
2887 case USB_SPEED_FULL: speed = "full"; break;
2888 case USB_SPEED_HIGH: speed = "high"; break;
2889 default: speed = "?"; break;
2891 INFO(fsg, "%s speed config #%d\n", speed, fsg->config);
2898 /*-------------------------------------------------------------------------*/
2900 static void handle_exception(struct fsg_dev *fsg)
2906 struct fsg_buffhd *bh;
2907 enum fsg_state old_state;
2910 unsigned int exception_req_tag;
2913 /* Clear the existing signals. Anything but SIGUSR1 is converted
2914 * into a high-priority EXIT exception. */
2916 sig = dequeue_signal_lock(current, ¤t->blocked, &info);
2919 if (sig != SIGUSR1) {
2920 if (fsg->state < FSG_STATE_EXIT)
2921 DBG(fsg, "Main thread exiting on signal\n");
2922 raise_exception(fsg, FSG_STATE_EXIT);
2926 /* Cancel all the pending transfers */
2927 if (fsg->intreq_busy)
2928 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2929 for (i = 0; i < NUM_BUFFERS; ++i) {
2930 bh = &fsg->buffhds[i];
2932 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
2933 if (bh->outreq_busy)
2934 usb_ep_dequeue(fsg->bulk_out, bh->outreq);
2937 /* Wait until everything is idle */
2939 num_active = fsg->intreq_busy;
2940 for (i = 0; i < NUM_BUFFERS; ++i) {
2941 bh = &fsg->buffhds[i];
2942 num_active += bh->inreq_busy + bh->outreq_busy;
2944 if (num_active == 0)
2946 if (sleep_thread(fsg))
2950 /* Clear out the controller's fifos */
2951 if (fsg->bulk_in_enabled)
2952 usb_ep_fifo_flush(fsg->bulk_in);
2953 if (fsg->bulk_out_enabled)
2954 usb_ep_fifo_flush(fsg->bulk_out);
2955 if (fsg->intr_in_enabled)
2956 usb_ep_fifo_flush(fsg->intr_in);
2958 /* Reset the I/O buffer states and pointers, the SCSI
2959 * state, and the exception. Then invoke the handler. */
2960 spin_lock_irq(&fsg->lock);
2962 for (i = 0; i < NUM_BUFFERS; ++i) {
2963 bh = &fsg->buffhds[i];
2964 bh->state = BUF_STATE_EMPTY;
2966 fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
2969 exception_req_tag = fsg->exception_req_tag;
2970 new_config = fsg->new_config;
2971 old_state = fsg->state;
2973 if (old_state == FSG_STATE_ABORT_BULK_OUT)
2974 fsg->state = FSG_STATE_STATUS_PHASE;
2976 for (i = 0; i < fsg->nluns; ++i) {
2977 curlun = &fsg->luns[i];
2978 curlun->prevent_medium_removal = 0;
2979 curlun->sense_data = curlun->unit_attention_data =
2981 curlun->sense_data_info = 0;
2982 curlun->info_valid = 0;
2984 fsg->state = FSG_STATE_IDLE;
2986 spin_unlock_irq(&fsg->lock);
2988 /* Carry out any extra actions required for the exception */
2989 switch (old_state) {
2993 case FSG_STATE_ABORT_BULK_OUT:
2995 spin_lock_irq(&fsg->lock);
2996 if (fsg->state == FSG_STATE_STATUS_PHASE)
2997 fsg->state = FSG_STATE_IDLE;
2998 spin_unlock_irq(&fsg->lock);
3001 case FSG_STATE_RESET:
3002 /* In case we were forced against our will to halt a
3003 * bulk endpoint, clear the halt now. (The SuperH UDC
3004 * requires this.) */
3005 if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
3006 usb_ep_clear_halt(fsg->bulk_in);
3008 if (transport_is_bbb()) {
3009 if (fsg->ep0_req_tag == exception_req_tag)
3010 ep0_queue(fsg); // Complete the status stage