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 /*-------------------------------------------------------------------------*/
301 #define LDBG(lun,fmt,args...) \
302 dev_dbg(&(lun)->dev , fmt , ## args)
303 #define MDBG(fmt,args...) \
304 pr_debug(DRIVER_NAME ": " fmt , ## args)
314 #define VLDBG(lun,fmt,args...) \
316 #endif /* VERBOSE_DEBUG */
318 #define LERROR(lun,fmt,args...) \
319 dev_err(&(lun)->dev , fmt , ## args)
320 #define LWARN(lun,fmt,args...) \
321 dev_warn(&(lun)->dev , fmt , ## args)
322 #define LINFO(lun,fmt,args...) \
323 dev_info(&(lun)->dev , fmt , ## args)
325 #define MINFO(fmt,args...) \
326 pr_info(DRIVER_NAME ": " fmt , ## args)
328 #define DBG(d, fmt, args...) \
329 dev_dbg(&(d)->gadget->dev , fmt , ## args)
330 #define VDBG(d, fmt, args...) \
331 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
332 #define ERROR(d, fmt, args...) \
333 dev_err(&(d)->gadget->dev , fmt , ## args)
334 #define WARNING(d, fmt, args...) \
335 dev_warn(&(d)->gadget->dev , fmt , ## args)
336 #define INFO(d, fmt, args...) \
337 dev_info(&(d)->gadget->dev , fmt , ## args)
340 /*-------------------------------------------------------------------------*/
342 /* Encapsulate the module parameter settings */
347 char *file
[MAX_LUNS
];
349 unsigned int num_filenames
;
350 unsigned int num_ros
;
357 char *transport_parm
;
359 unsigned short vendor
;
360 unsigned short product
;
361 unsigned short release
;
365 char *transport_name
;
369 } mod_data
= { // Default values
370 .transport_parm
= "BBB",
371 .protocol_parm
= "SCSI",
375 .vendor
= DRIVER_VENDOR_ID
,
376 .product
= DRIVER_PRODUCT_ID
,
377 .release
= 0xffff, // Use controller chip type
382 module_param_array_named(file
, mod_data
.file
, charp
, &mod_data
.num_filenames
,
384 MODULE_PARM_DESC(file
, "names of backing files or devices");
386 module_param_array_named(ro
, mod_data
.ro
, bool, &mod_data
.num_ros
, S_IRUGO
);
387 MODULE_PARM_DESC(ro
, "true to force read-only");
389 module_param_named(luns
, mod_data
.nluns
, uint
, S_IRUGO
);
390 MODULE_PARM_DESC(luns
, "number of LUNs");
392 module_param_named(removable
, mod_data
.removable
, bool, S_IRUGO
);
393 MODULE_PARM_DESC(removable
, "true to simulate removable media");
395 module_param_named(stall
, mod_data
.can_stall
, bool, S_IRUGO
);
396 MODULE_PARM_DESC(stall
, "false to prevent bulk stalls");
398 module_param_named(cdrom
, mod_data
.cdrom
, bool, S_IRUGO
);
399 MODULE_PARM_DESC(cdrom
, "true to emulate cdrom instead of disk");
402 /* In the non-TEST version, only the module parameters listed above
404 #ifdef CONFIG_USB_FILE_STORAGE_TEST
406 module_param_named(transport
, mod_data
.transport_parm
, charp
, S_IRUGO
);
407 MODULE_PARM_DESC(transport
, "type of transport (BBB, CBI, or CB)");
409 module_param_named(protocol
, mod_data
.protocol_parm
, charp
, S_IRUGO
);
410 MODULE_PARM_DESC(protocol
, "type of protocol (RBC, 8020, QIC, UFI, "
413 module_param_named(vendor
, mod_data
.vendor
, ushort
, S_IRUGO
);
414 MODULE_PARM_DESC(vendor
, "USB Vendor ID");
416 module_param_named(product
, mod_data
.product
, ushort
, S_IRUGO
);
417 MODULE_PARM_DESC(product
, "USB Product ID");
419 module_param_named(release
, mod_data
.release
, ushort
, S_IRUGO
);
420 MODULE_PARM_DESC(release
, "USB release number");
422 module_param_named(buflen
, mod_data
.buflen
, uint
, S_IRUGO
);
423 MODULE_PARM_DESC(buflen
, "I/O buffer size");
425 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
428 /*-------------------------------------------------------------------------*/
430 /* SCSI device types */
431 #define TYPE_DISK 0x00
432 #define TYPE_CDROM 0x05
434 /* USB protocol value = the transport method */
435 #define USB_PR_CBI 0x00 // Control/Bulk/Interrupt
436 #define USB_PR_CB 0x01 // Control/Bulk w/o interrupt
437 #define USB_PR_BULK 0x50 // Bulk-only
439 /* USB subclass value = the protocol encapsulation */
440 #define USB_SC_RBC 0x01 // Reduced Block Commands (flash)
441 #define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM)
442 #define USB_SC_QIC 0x03 // QIC-157 (tape)
443 #define USB_SC_UFI 0x04 // UFI (floppy)
444 #define USB_SC_8070 0x05 // SFF-8070i (removable)
445 #define USB_SC_SCSI 0x06 // Transparent SCSI
447 /* Bulk-only data structures */
449 /* Command Block Wrapper */
450 struct bulk_cb_wrap
{
451 __le32 Signature
; // Contains 'USBC'
452 u32 Tag
; // Unique per command id
453 __le32 DataTransferLength
; // Size of the data
454 u8 Flags
; // Direction in bit 7
455 u8 Lun
; // LUN (normally 0)
456 u8 Length
; // Of the CDB, <= MAX_COMMAND_SIZE
457 u8 CDB
[16]; // Command Data Block
460 #define USB_BULK_CB_WRAP_LEN 31
461 #define USB_BULK_CB_SIG 0x43425355 // Spells out USBC
462 #define USB_BULK_IN_FLAG 0x80
464 /* Command Status Wrapper */
465 struct bulk_cs_wrap
{
466 __le32 Signature
; // Should = 'USBS'
467 u32 Tag
; // Same as original command
468 __le32 Residue
; // Amount not transferred
469 u8 Status
; // See below
472 #define USB_BULK_CS_WRAP_LEN 13
473 #define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS'
474 #define USB_STATUS_PASS 0
475 #define USB_STATUS_FAIL 1
476 #define USB_STATUS_PHASE_ERROR 2
478 /* Bulk-only class specific requests */
479 #define USB_BULK_RESET_REQUEST 0xff
480 #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
483 /* CBI Interrupt data structure */
484 struct interrupt_data
{
489 #define CBI_INTERRUPT_DATA_LEN 2
491 /* CBI Accept Device-Specific Command request */
492 #define USB_CBI_ADSC_REQUEST 0x00
495 #define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
497 /* SCSI commands that we recognize */
498 #define SC_FORMAT_UNIT 0x04
499 #define SC_INQUIRY 0x12
500 #define SC_MODE_SELECT_6 0x15
501 #define SC_MODE_SELECT_10 0x55
502 #define SC_MODE_SENSE_6 0x1a
503 #define SC_MODE_SENSE_10 0x5a
504 #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
505 #define SC_READ_6 0x08
506 #define SC_READ_10 0x28
507 #define SC_READ_12 0xa8
508 #define SC_READ_CAPACITY 0x25
509 #define SC_READ_FORMAT_CAPACITIES 0x23
510 #define SC_READ_HEADER 0x44
511 #define SC_READ_TOC 0x43
512 #define SC_RELEASE 0x17
513 #define SC_REQUEST_SENSE 0x03
514 #define SC_RESERVE 0x16
515 #define SC_SEND_DIAGNOSTIC 0x1d
516 #define SC_START_STOP_UNIT 0x1b
517 #define SC_SYNCHRONIZE_CACHE 0x35
518 #define SC_TEST_UNIT_READY 0x00
519 #define SC_VERIFY 0x2f
520 #define SC_WRITE_6 0x0a
521 #define SC_WRITE_10 0x2a
522 #define SC_WRITE_12 0xaa
524 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
525 #define SS_NO_SENSE 0
526 #define SS_COMMUNICATION_FAILURE 0x040800
527 #define SS_INVALID_COMMAND 0x052000
528 #define SS_INVALID_FIELD_IN_CDB 0x052400
529 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
530 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
531 #define SS_MEDIUM_NOT_PRESENT 0x023a00
532 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
533 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
534 #define SS_RESET_OCCURRED 0x062900
535 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
536 #define SS_UNRECOVERED_READ_ERROR 0x031100
537 #define SS_WRITE_ERROR 0x030c02
538 #define SS_WRITE_PROTECTED 0x072700
540 #define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
541 #define ASC(x) ((u8) ((x) >> 8))
542 #define ASCQ(x) ((u8) (x))
545 /*-------------------------------------------------------------------------*/
548 * These definitions will permit the compiler to avoid generating code for
549 * parts of the driver that aren't used in the non-TEST version. Even gcc
550 * can recognize when a test of a constant expression yields a dead code
554 #ifdef CONFIG_USB_FILE_STORAGE_TEST
556 #define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
557 #define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
558 #define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
562 #define transport_is_bbb() 1
563 #define transport_is_cbi() 0
564 #define protocol_is_scsi() 1
566 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
575 unsigned int prevent_medium_removal
: 1;
576 unsigned int registered
: 1;
577 unsigned int info_valid
: 1;
581 u32 unit_attention_data
;
586 #define backing_file_is_open(curlun) ((curlun)->filp != NULL)
588 static struct lun
*dev_to_lun(struct device
*dev
)
590 return container_of(dev
, struct lun
, dev
);
594 /* Big enough to hold our biggest descriptor */
595 #define EP0_BUFSIZE 256
596 #define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
598 /* Number of buffers we will use. 2 is enough for double-buffering */
599 #define NUM_BUFFERS 2
601 enum fsg_buffer_state
{
609 enum fsg_buffer_state state
;
610 struct fsg_buffhd
*next
;
612 /* The NetChip 2280 is faster, and handles some protocol faults
613 * better, if we don't submit any short bulk-out read requests.
614 * So we will record the intended request length here. */
615 unsigned int bulk_out_intended_length
;
617 struct usb_request
*inreq
;
619 struct usb_request
*outreq
;
624 FSG_STATE_COMMAND_PHASE
= -10, // This one isn't used anywhere
625 FSG_STATE_DATA_PHASE
,
626 FSG_STATE_STATUS_PHASE
,
629 FSG_STATE_ABORT_BULK_OUT
,
631 FSG_STATE_INTERFACE_CHANGE
,
632 FSG_STATE_CONFIG_CHANGE
,
633 FSG_STATE_DISCONNECT
,
638 enum data_direction
{
639 DATA_DIR_UNKNOWN
= 0,
646 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
648 struct usb_gadget
*gadget
;
650 /* filesem protects: backing files in use */
651 struct rw_semaphore filesem
;
653 /* reference counting: wait until all LUNs are released */
656 struct usb_ep
*ep0
; // Handy copy of gadget->ep0
657 struct usb_request
*ep0req
; // For control responses
658 unsigned int ep0_req_tag
;
659 const char *ep0req_name
;
661 struct usb_request
*intreq
; // For interrupt responses
663 struct fsg_buffhd
*intr_buffhd
;
665 unsigned int bulk_out_maxpacket
;
666 enum fsg_state state
; // For exception handling
667 unsigned int exception_req_tag
;
669 u8 config
, new_config
;
671 unsigned int running
: 1;
672 unsigned int bulk_in_enabled
: 1;
673 unsigned int bulk_out_enabled
: 1;
674 unsigned int intr_in_enabled
: 1;
675 unsigned int phase_error
: 1;
676 unsigned int short_packet_received
: 1;
677 unsigned int bad_lun_okay
: 1;
679 unsigned long atomic_bitflags
;
681 #define IGNORE_BULK_OUT 1
684 struct usb_ep
*bulk_in
;
685 struct usb_ep
*bulk_out
;
686 struct usb_ep
*intr_in
;
688 struct fsg_buffhd
*next_buffhd_to_fill
;
689 struct fsg_buffhd
*next_buffhd_to_drain
;
690 struct fsg_buffhd buffhds
[NUM_BUFFERS
];
692 int thread_wakeup_needed
;
693 struct completion thread_notifier
;
694 struct task_struct
*thread_task
;
697 u8 cmnd
[MAX_COMMAND_SIZE
];
698 enum data_direction data_dir
;
700 u32 data_size_from_cmnd
;
706 /* The CB protocol offers no way for a host to know when a command
707 * has completed. As a result the next command may arrive early,
708 * and we will still have to handle it. For that reason we need
709 * a buffer to store new commands when using CB (or CBI, which
710 * does not oblige a host to wait for command completion either). */
712 u8 cbbuf_cmnd
[MAX_COMMAND_SIZE
];
719 typedef void (*fsg_routine_t
)(struct fsg_dev
*);
721 static int exception_in_progress(struct fsg_dev
*fsg
)
723 return (fsg
->state
> FSG_STATE_IDLE
);
726 /* Make bulk-out requests be divisible by the maxpacket size */
727 static void set_bulk_out_req_length(struct fsg_dev
*fsg
,
728 struct fsg_buffhd
*bh
, unsigned int length
)
732 bh
->bulk_out_intended_length
= length
;
733 rem
= length
% fsg
->bulk_out_maxpacket
;
735 length
+= fsg
->bulk_out_maxpacket
- rem
;
736 bh
->outreq
->length
= length
;
739 static struct fsg_dev
*the_fsg
;
740 static struct usb_gadget_driver fsg_driver
;
742 static void close_backing_file(struct lun
*curlun
);
745 /*-------------------------------------------------------------------------*/
749 static void dump_msg(struct fsg_dev
*fsg
, const char *label
,
750 const u8
*buf
, unsigned int length
)
753 DBG(fsg
, "%s, length %u:\n", label
, length
);
754 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
,
755 16, 1, buf
, length
, 0);
759 static void dump_cdb(struct fsg_dev
*fsg
)
764 static void dump_msg(struct fsg_dev
*fsg
, const char *label
,
765 const u8
*buf
, unsigned int length
)
770 static void dump_cdb(struct fsg_dev
*fsg
)
772 print_hex_dump(KERN_DEBUG
, "SCSI CDB: ", DUMP_PREFIX_NONE
,
773 16, 1, fsg
->cmnd
, fsg
->cmnd_size
, 0);
778 static void dump_cdb(struct fsg_dev
*fsg
)
781 #endif /* VERBOSE_DEBUG */
782 #endif /* DUMP_MSGS */
785 static int fsg_set_halt(struct fsg_dev
*fsg
, struct usb_ep
*ep
)
789 if (ep
== fsg
->bulk_in
)
791 else if (ep
== fsg
->bulk_out
)
795 DBG(fsg
, "%s set halt\n", name
);
796 return usb_ep_set_halt(ep
);
800 /*-------------------------------------------------------------------------*/
802 /* Routines for unaligned data access */
804 static u32
get_unaligned_be24(u8
*buf
)
806 return 0xffffff & (u32
) get_unaligned_be32(buf
- 1);
810 /*-------------------------------------------------------------------------*/
813 * DESCRIPTORS ... most are static, but strings and (full) configuration
814 * descriptors are built on demand. Also the (static) config and interface
815 * descriptors are adjusted during fsg_bind().
817 #define STRING_MANUFACTURER 1
818 #define STRING_PRODUCT 2
819 #define STRING_SERIAL 3
820 #define STRING_CONFIG 4
821 #define STRING_INTERFACE 5
823 /* There is only one configuration. */
824 #define CONFIG_VALUE 1
826 static struct usb_device_descriptor
828 .bLength
= sizeof device_desc
,
829 .bDescriptorType
= USB_DT_DEVICE
,
831 .bcdUSB
= cpu_to_le16(0x0200),
832 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
834 /* The next three values can be overridden by module parameters */
835 .idVendor
= cpu_to_le16(DRIVER_VENDOR_ID
),
836 .idProduct
= cpu_to_le16(DRIVER_PRODUCT_ID
),
837 .bcdDevice
= cpu_to_le16(0xffff),
839 .iManufacturer
= STRING_MANUFACTURER
,
840 .iProduct
= STRING_PRODUCT
,
841 .iSerialNumber
= STRING_SERIAL
,
842 .bNumConfigurations
= 1,
845 static struct usb_config_descriptor
847 .bLength
= sizeof config_desc
,
848 .bDescriptorType
= USB_DT_CONFIG
,
850 /* wTotalLength computed by usb_gadget_config_buf() */
852 .bConfigurationValue
= CONFIG_VALUE
,
853 .iConfiguration
= STRING_CONFIG
,
854 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
855 .bMaxPower
= CONFIG_USB_GADGET_VBUS_DRAW
/ 2,
858 static struct usb_otg_descriptor
860 .bLength
= sizeof(otg_desc
),
861 .bDescriptorType
= USB_DT_OTG
,
863 .bmAttributes
= USB_OTG_SRP
,
866 /* There is only one interface. */
868 static struct usb_interface_descriptor
870 .bLength
= sizeof intf_desc
,
871 .bDescriptorType
= USB_DT_INTERFACE
,
873 .bNumEndpoints
= 2, // Adjusted during fsg_bind()
874 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
875 .bInterfaceSubClass
= USB_SC_SCSI
, // Adjusted during fsg_bind()
876 .bInterfaceProtocol
= USB_PR_BULK
, // Adjusted during fsg_bind()
877 .iInterface
= STRING_INTERFACE
,
880 /* Three full-speed endpoint descriptors: bulk-in, bulk-out,
881 * and interrupt-in. */
883 static struct usb_endpoint_descriptor
885 .bLength
= USB_DT_ENDPOINT_SIZE
,
886 .bDescriptorType
= USB_DT_ENDPOINT
,
888 .bEndpointAddress
= USB_DIR_IN
,
889 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
890 /* wMaxPacketSize set by autoconfiguration */
893 static struct usb_endpoint_descriptor
895 .bLength
= USB_DT_ENDPOINT_SIZE
,
896 .bDescriptorType
= USB_DT_ENDPOINT
,
898 .bEndpointAddress
= USB_DIR_OUT
,
899 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
900 /* wMaxPacketSize set by autoconfiguration */
903 static struct usb_endpoint_descriptor
905 .bLength
= USB_DT_ENDPOINT_SIZE
,
906 .bDescriptorType
= USB_DT_ENDPOINT
,
908 .bEndpointAddress
= USB_DIR_IN
,
909 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
910 .wMaxPacketSize
= cpu_to_le16(2),
911 .bInterval
= 32, // frames -> 32 ms
914 static const struct usb_descriptor_header
*fs_function
[] = {
915 (struct usb_descriptor_header
*) &otg_desc
,
916 (struct usb_descriptor_header
*) &intf_desc
,
917 (struct usb_descriptor_header
*) &fs_bulk_in_desc
,
918 (struct usb_descriptor_header
*) &fs_bulk_out_desc
,
919 (struct usb_descriptor_header
*) &fs_intr_in_desc
,
922 #define FS_FUNCTION_PRE_EP_ENTRIES 2
926 * USB 2.0 devices need to expose both high speed and full speed
927 * descriptors, unless they only run at full speed.
929 * That means alternate endpoint descriptors (bigger packets)
930 * and a "device qualifier" ... plus more construction options
931 * for the config descriptor.
933 static struct usb_qualifier_descriptor
935 .bLength
= sizeof dev_qualifier
,
936 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
938 .bcdUSB
= cpu_to_le16(0x0200),
939 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
941 .bNumConfigurations
= 1,
944 static struct usb_endpoint_descriptor
946 .bLength
= USB_DT_ENDPOINT_SIZE
,
947 .bDescriptorType
= USB_DT_ENDPOINT
,
949 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
950 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
951 .wMaxPacketSize
= cpu_to_le16(512),
954 static struct usb_endpoint_descriptor
956 .bLength
= USB_DT_ENDPOINT_SIZE
,
957 .bDescriptorType
= USB_DT_ENDPOINT
,
959 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
960 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
961 .wMaxPacketSize
= cpu_to_le16(512),
962 .bInterval
= 1, // NAK every 1 uframe
965 static struct usb_endpoint_descriptor
967 .bLength
= USB_DT_ENDPOINT_SIZE
,
968 .bDescriptorType
= USB_DT_ENDPOINT
,
970 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
971 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
972 .wMaxPacketSize
= cpu_to_le16(2),
973 .bInterval
= 9, // 2**(9-1) = 256 uframes -> 32 ms
976 static const struct usb_descriptor_header
*hs_function
[] = {
977 (struct usb_descriptor_header
*) &otg_desc
,
978 (struct usb_descriptor_header
*) &intf_desc
,
979 (struct usb_descriptor_header
*) &hs_bulk_in_desc
,
980 (struct usb_descriptor_header
*) &hs_bulk_out_desc
,
981 (struct usb_descriptor_header
*) &hs_intr_in_desc
,
984 #define HS_FUNCTION_PRE_EP_ENTRIES 2
986 /* Maxpacket and other transfer characteristics vary by speed. */
987 static struct usb_endpoint_descriptor
*
988 ep_desc(struct usb_gadget
*g
, struct usb_endpoint_descriptor
*fs
,
989 struct usb_endpoint_descriptor
*hs
)
991 if (gadget_is_dualspeed(g
) && g
->speed
== USB_SPEED_HIGH
)
997 /* The CBI specification limits the serial string to 12 uppercase hexadecimal
999 static char manufacturer
[64];
1000 static char serial
[13];
1002 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
1003 static struct usb_string strings
[] = {
1004 {STRING_MANUFACTURER
, manufacturer
},
1005 {STRING_PRODUCT
, longname
},
1006 {STRING_SERIAL
, serial
},
1007 {STRING_CONFIG
, "Self-powered"},
1008 {STRING_INTERFACE
, "Mass Storage"},
1012 static struct usb_gadget_strings stringtab
= {
1013 .language
= 0x0409, // en-us
1019 * Config descriptors must agree with the code that sets configurations
1020 * and with code managing interfaces and their altsettings. They must
1021 * also handle different speeds and other-speed requests.
1023 static int populate_config_buf(struct usb_gadget
*gadget
,
1024 u8
*buf
, u8 type
, unsigned index
)
1026 enum usb_device_speed speed
= gadget
->speed
;
1028 const struct usb_descriptor_header
**function
;
1033 if (gadget_is_dualspeed(gadget
) && type
== USB_DT_OTHER_SPEED_CONFIG
)
1034 speed
= (USB_SPEED_FULL
+ USB_SPEED_HIGH
) - speed
;
1035 if (gadget_is_dualspeed(gadget
) && speed
== USB_SPEED_HIGH
)
1036 function
= hs_function
;
1038 function
= fs_function
;
1040 /* for now, don't advertise srp-only devices */
1041 if (!gadget_is_otg(gadget
))
1044 len
= usb_gadget_config_buf(&config_desc
, buf
, EP0_BUFSIZE
, function
);
1045 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
1050 /*-------------------------------------------------------------------------*/
1052 /* These routines may be called in process context or in_irq */
1054 /* Caller must hold fsg->lock */
1055 static void wakeup_thread(struct fsg_dev
*fsg
)
1057 /* Tell the main thread that something has happened */
1058 fsg
->thread_wakeup_needed
= 1;
1059 if (fsg
->thread_task
)
1060 wake_up_process(fsg
->thread_task
);
1064 static void raise_exception(struct fsg_dev
*fsg
, enum fsg_state new_state
)
1066 unsigned long flags
;
1068 /* Do nothing if a higher-priority exception is already in progress.
1069 * If a lower-or-equal priority exception is in progress, preempt it
1070 * and notify the main thread by sending it a signal. */
1071 spin_lock_irqsave(&fsg
->lock
, flags
);
1072 if (fsg
->state
<= new_state
) {
1073 fsg
->exception_req_tag
= fsg
->ep0_req_tag
;
1074 fsg
->state
= new_state
;
1075 if (fsg
->thread_task
)
1076 send_sig_info(SIGUSR1
, SEND_SIG_FORCED
,
1079 spin_unlock_irqrestore(&fsg
->lock
, flags
);
1083 /*-------------------------------------------------------------------------*/
1085 /* The disconnect callback and ep0 routines. These always run in_irq,
1086 * except that ep0_queue() is called in the main thread to acknowledge
1087 * completion of various requests: set config, set interface, and
1088 * Bulk-only device reset. */
1090 static void fsg_disconnect(struct usb_gadget
*gadget
)
1092 struct fsg_dev
*fsg
= get_gadget_data(gadget
);
1094 DBG(fsg
, "disconnect or port reset\n");
1095 raise_exception(fsg
, FSG_STATE_DISCONNECT
);
1099 static int ep0_queue(struct fsg_dev
*fsg
)
1103 rc
= usb_ep_queue(fsg
->ep0
, fsg
->ep0req
, GFP_ATOMIC
);
1104 if (rc
!= 0 && rc
!= -ESHUTDOWN
) {
1106 /* We can't do much more than wait for a reset */
1107 WARNING(fsg
, "error in submission: %s --> %d\n",
1108 fsg
->ep0
->name
, rc
);
1113 static void ep0_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1115 struct fsg_dev
*fsg
= ep
->driver_data
;
1117 if (req
->actual
> 0)
1118 dump_msg(fsg
, fsg
->ep0req_name
, req
->buf
, req
->actual
);
1119 if (req
->status
|| req
->actual
!= req
->length
)
1120 DBG(fsg
, "%s --> %d, %u/%u\n", __func__
,
1121 req
->status
, req
->actual
, req
->length
);
1122 if (req
->status
== -ECONNRESET
) // Request was cancelled
1123 usb_ep_fifo_flush(ep
);
1125 if (req
->status
== 0 && req
->context
)
1126 ((fsg_routine_t
) (req
->context
))(fsg
);
1130 /*-------------------------------------------------------------------------*/
1132 /* Bulk and interrupt endpoint completion handlers.
1133 * These always run in_irq. */
1135 static void bulk_in_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1137 struct fsg_dev
*fsg
= ep
->driver_data
;
1138 struct fsg_buffhd
*bh
= req
->context
;
1140 if (req
->status
|| req
->actual
!= req
->length
)
1141 DBG(fsg
, "%s --> %d, %u/%u\n", __func__
,
1142 req
->status
, req
->actual
, req
->length
);
1143 if (req
->status
== -ECONNRESET
) // Request was cancelled
1144 usb_ep_fifo_flush(ep
);
1146 /* Hold the lock while we update the request and buffer states */
1148 spin_lock(&fsg
->lock
);
1150 bh
->state
= BUF_STATE_EMPTY
;
1152 spin_unlock(&fsg
->lock
);
1155 static void bulk_out_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1157 struct fsg_dev
*fsg
= ep
->driver_data
;
1158 struct fsg_buffhd
*bh
= req
->context
;
1160 dump_msg(fsg
, "bulk-out", req
->buf
, req
->actual
);
1161 if (req
->status
|| req
->actual
!= bh
->bulk_out_intended_length
)
1162 DBG(fsg
, "%s --> %d, %u/%u\n", __func__
,
1163 req
->status
, req
->actual
,
1164 bh
->bulk_out_intended_length
);
1165 if (req
->status
== -ECONNRESET
) // Request was cancelled
1166 usb_ep_fifo_flush(ep
);
1168 /* Hold the lock while we update the request and buffer states */
1170 spin_lock(&fsg
->lock
);
1171 bh
->outreq_busy
= 0;
1172 bh
->state
= BUF_STATE_FULL
;
1174 spin_unlock(&fsg
->lock
);
1178 #ifdef CONFIG_USB_FILE_STORAGE_TEST
1179 static void intr_in_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1181 struct fsg_dev
*fsg
= ep
->driver_data
;
1182 struct fsg_buffhd
*bh
= req
->context
;
1184 if (req
->status
|| req
->actual
!= req
->length
)
1185 DBG(fsg
, "%s --> %d, %u/%u\n", __func__
,
1186 req
->status
, req
->actual
, req
->length
);
1187 if (req
->status
== -ECONNRESET
) // Request was cancelled
1188 usb_ep_fifo_flush(ep
);
1190 /* Hold the lock while we update the request and buffer states */
1192 spin_lock(&fsg
->lock
);
1193 fsg
->intreq_busy
= 0;
1194 bh
->state
= BUF_STATE_EMPTY
;
1196 spin_unlock(&fsg
->lock
);
1200 static void intr_in_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1202 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
1205 /*-------------------------------------------------------------------------*/
1207 /* Ep0 class-specific handlers. These always run in_irq. */
1209 #ifdef CONFIG_USB_FILE_STORAGE_TEST
1210 static void received_cbi_adsc(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
1212 struct usb_request
*req
= fsg
->ep0req
;
1213 static u8 cbi_reset_cmnd
[6] = {
1214 SC_SEND_DIAGNOSTIC
, 4, 0xff, 0xff, 0xff, 0xff};
1216 /* Error in command transfer? */
1217 if (req
->status
|| req
->length
!= req
->actual
||
1218 req
->actual
< 6 || req
->actual
> MAX_COMMAND_SIZE
) {
1220 /* Not all controllers allow a protocol stall after
1221 * receiving control-out data, but we'll try anyway. */
1222 fsg_set_halt(fsg
, fsg
->ep0
);
1223 return; // Wait for reset
1226 /* Is it the special reset command? */
1227 if (req
->actual
>= sizeof cbi_reset_cmnd
&&
1228 memcmp(req
->buf
, cbi_reset_cmnd
,
1229 sizeof cbi_reset_cmnd
) == 0) {
1231 /* Raise an exception to stop the current operation
1232 * and reinitialize our state. */
1233 DBG(fsg
, "cbi reset request\n");
1234 raise_exception(fsg
, FSG_STATE_RESET
);
1238 VDBG(fsg
, "CB[I] accept device-specific command\n");
1239 spin_lock(&fsg
->lock
);
1241 /* Save the command for later */
1242 if (fsg
->cbbuf_cmnd_size
)
1243 WARNING(fsg
, "CB[I] overwriting previous command\n");
1244 fsg
->cbbuf_cmnd_size
= req
->actual
;
1245 memcpy(fsg
->cbbuf_cmnd
, req
->buf
, fsg
->cbbuf_cmnd_size
);
1248 spin_unlock(&fsg
->lock
);
1252 static void received_cbi_adsc(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
1254 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
1257 static int class_setup_req(struct fsg_dev
*fsg
,
1258 const struct usb_ctrlrequest
*ctrl
)
1260 struct usb_request
*req
= fsg
->ep0req
;
1261 int value
= -EOPNOTSUPP
;
1262 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1263 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1264 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1269 /* Handle Bulk-only class-specific requests */
1270 if (transport_is_bbb()) {
1271 switch (ctrl
->bRequest
) {
1273 case USB_BULK_RESET_REQUEST
:
1274 if (ctrl
->bRequestType
!= (USB_DIR_OUT
|
1275 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
))
1277 if (w_index
!= 0 || w_value
!= 0) {
1282 /* Raise an exception to stop the current operation
1283 * and reinitialize our state. */
1284 DBG(fsg
, "bulk reset request\n");
1285 raise_exception(fsg
, FSG_STATE_RESET
);
1286 value
= DELAYED_STATUS
;
1289 case USB_BULK_GET_MAX_LUN_REQUEST
:
1290 if (ctrl
->bRequestType
!= (USB_DIR_IN
|
1291 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
))
1293 if (w_index
!= 0 || w_value
!= 0) {
1297 VDBG(fsg
, "get max LUN\n");
1298 *(u8
*) req
->buf
= fsg
->nluns
- 1;
1304 /* Handle CBI class-specific requests */
1306 switch (ctrl
->bRequest
) {
1308 case USB_CBI_ADSC_REQUEST
:
1309 if (ctrl
->bRequestType
!= (USB_DIR_OUT
|
1310 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
))
1312 if (w_index
!= 0 || w_value
!= 0) {
1316 if (w_length
> MAX_COMMAND_SIZE
) {
1321 fsg
->ep0req
->context
= received_cbi_adsc
;
1326 if (value
== -EOPNOTSUPP
)
1328 "unknown class-specific control req "
1329 "%02x.%02x v%04x i%04x l%u\n",
1330 ctrl
->bRequestType
, ctrl
->bRequest
,
1331 le16_to_cpu(ctrl
->wValue
), w_index
, w_length
);
1336 /*-------------------------------------------------------------------------*/
1338 /* Ep0 standard request handlers. These always run in_irq. */
1340 static int standard_setup_req(struct fsg_dev
*fsg
,
1341 const struct usb_ctrlrequest
*ctrl
)
1343 struct usb_request
*req
= fsg
->ep0req
;
1344 int value
= -EOPNOTSUPP
;
1345 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1346 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1348 /* Usually this just stores reply data in the pre-allocated ep0 buffer,
1349 * but config change events will also reconfigure hardware. */
1350 switch (ctrl
->bRequest
) {
1352 case USB_REQ_GET_DESCRIPTOR
:
1353 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_TYPE_STANDARD
|
1356 switch (w_value
>> 8) {
1359 VDBG(fsg
, "get device descriptor\n");
1360 value
= sizeof device_desc
;
1361 memcpy(req
->buf
, &device_desc
, value
);
1363 case USB_DT_DEVICE_QUALIFIER
:
1364 VDBG(fsg
, "get device qualifier\n");
1365 if (!gadget_is_dualspeed(fsg
->gadget
))
1367 value
= sizeof dev_qualifier
;
1368 memcpy(req
->buf
, &dev_qualifier
, value
);
1371 case USB_DT_OTHER_SPEED_CONFIG
:
1372 VDBG(fsg
, "get other-speed config descriptor\n");
1373 if (!gadget_is_dualspeed(fsg
->gadget
))
1377 VDBG(fsg
, "get configuration descriptor\n");
1379 value
= populate_config_buf(fsg
->gadget
,
1386 VDBG(fsg
, "get string descriptor\n");
1388 /* wIndex == language code */
1389 value
= usb_gadget_get_string(&stringtab
,
1390 w_value
& 0xff, req
->buf
);
1395 /* One config, two speeds */
1396 case USB_REQ_SET_CONFIGURATION
:
1397 if (ctrl
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
|
1400 VDBG(fsg
, "set configuration\n");
1401 if (w_value
== CONFIG_VALUE
|| w_value
== 0) {
1402 fsg
->new_config
= w_value
;
1404 /* Raise an exception to wipe out previous transaction
1405 * state (queued bufs, etc) and set the new config. */
1406 raise_exception(fsg
, FSG_STATE_CONFIG_CHANGE
);
1407 value
= DELAYED_STATUS
;
1410 case USB_REQ_GET_CONFIGURATION
:
1411 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_TYPE_STANDARD
|
1414 VDBG(fsg
, "get configuration\n");
1415 *(u8
*) req
->buf
= fsg
->config
;
1419 case USB_REQ_SET_INTERFACE
:
1420 if (ctrl
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
|
1421 USB_RECIP_INTERFACE
))
1423 if (fsg
->config
&& w_index
== 0) {
1425 /* Raise an exception to wipe out previous transaction
1426 * state (queued bufs, etc) and install the new
1427 * interface altsetting. */
1428 raise_exception(fsg
, FSG_STATE_INTERFACE_CHANGE
);
1429 value
= DELAYED_STATUS
;
1432 case USB_REQ_GET_INTERFACE
:
1433 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_TYPE_STANDARD
|
1434 USB_RECIP_INTERFACE
))
1442 VDBG(fsg
, "get interface\n");
1443 *(u8
*) req
->buf
= 0;
1449 "unknown control req %02x.%02x v%04x i%04x l%u\n",
1450 ctrl
->bRequestType
, ctrl
->bRequest
,
1451 w_value
, w_index
, le16_to_cpu(ctrl
->wLength
));
1458 static int fsg_setup(struct usb_gadget
*gadget
,
1459 const struct usb_ctrlrequest
*ctrl
)
1461 struct fsg_dev
*fsg
= get_gadget_data(gadget
);
1463 int w_length
= le16_to_cpu(ctrl
->wLength
);
1465 ++fsg
->ep0_req_tag
; // Record arrival of a new request
1466 fsg
->ep0req
->context
= NULL
;
1467 fsg
->ep0req
->length
= 0;
1468 dump_msg(fsg
, "ep0-setup", (u8
*) ctrl
, sizeof(*ctrl
));
1470 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_CLASS
)
1471 rc
= class_setup_req(fsg
, ctrl
);
1473 rc
= standard_setup_req(fsg
, ctrl
);
1475 /* Respond with data/status or defer until later? */
1476 if (rc
>= 0 && rc
!= DELAYED_STATUS
) {
1477 rc
= min(rc
, w_length
);
1478 fsg
->ep0req
->length
= rc
;
1479 fsg
->ep0req
->zero
= rc
< w_length
;
1480 fsg
->ep0req_name
= (ctrl
->bRequestType
& USB_DIR_IN
?
1481 "ep0-in" : "ep0-out");
1482 rc
= ep0_queue(fsg
);
1485 /* Device either stalls (rc < 0) or reports success */
1490 /*-------------------------------------------------------------------------*/
1492 /* All the following routines run in process context */
1495 /* Use this for bulk or interrupt transfers, not ep0 */
1496 static void start_transfer(struct fsg_dev
*fsg
, struct usb_ep
*ep
,
1497 struct usb_request
*req
, int *pbusy
,
1498 enum fsg_buffer_state
*state
)
1502 if (ep
== fsg
->bulk_in
)
1503 dump_msg(fsg
, "bulk-in", req
->buf
, req
->length
);
1504 else if (ep
== fsg
->intr_in
)
1505 dump_msg(fsg
, "intr-in", req
->buf
, req
->length
);
1507 spin_lock_irq(&fsg
->lock
);
1509 *state
= BUF_STATE_BUSY
;
1510 spin_unlock_irq(&fsg
->lock
);
1511 rc
= usb_ep_queue(ep
, req
, GFP_KERNEL
);
1514 *state
= BUF_STATE_EMPTY
;
1516 /* We can't do much more than wait for a reset */
1518 /* Note: currently the net2280 driver fails zero-length
1519 * submissions if DMA is enabled. */
1520 if (rc
!= -ESHUTDOWN
&& !(rc
== -EOPNOTSUPP
&&
1522 WARNING(fsg
, "error in submission: %s --> %d\n",
1528 static int sleep_thread(struct fsg_dev
*fsg
)
1532 /* Wait until a signal arrives or we are woken up */
1535 set_current_state(TASK_INTERRUPTIBLE
);
1536 if (signal_pending(current
)) {
1540 if (fsg
->thread_wakeup_needed
)
1544 __set_current_state(TASK_RUNNING
);
1545 fsg
->thread_wakeup_needed
= 0;
1550 /*-------------------------------------------------------------------------*/
1552 static int do_read(struct fsg_dev
*fsg
)
1554 struct lun
*curlun
= fsg
->curlun
;
1556 struct fsg_buffhd
*bh
;
1559 loff_t file_offset
, file_offset_tmp
;
1560 unsigned int amount
;
1561 unsigned int partial_page
;
1564 /* Get the starting Logical Block Address and check that it's
1566 if (fsg
->cmnd
[0] == SC_READ_6
)
1567 lba
= get_unaligned_be24(&fsg
->cmnd
[1]);
1569 lba
= get_unaligned_be32(&fsg
->cmnd
[2]);
1571 /* We allow DPO (Disable Page Out = don't save data in the
1572 * cache) and FUA (Force Unit Access = don't read from the
1573 * cache), but we don't implement them. */
1574 if ((fsg
->cmnd
[1] & ~0x18) != 0) {
1575 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1579 if (lba
>= curlun
->num_sectors
) {
1580 curlun
->sense_data
= SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
1583 file_offset
= ((loff_t
) lba
) << 9;
1585 /* Carry out the file reads */
1586 amount_left
= fsg
->data_size_from_cmnd
;
1587 if (unlikely(amount_left
== 0))
1588 return -EIO
; // No default reply
1592 /* Figure out how much we need to read:
1593 * Try to read the remaining amount.
1594 * But don't read more than the buffer size.
1595 * And don't try to read past the end of the file.
1596 * Finally, if we're not at a page boundary, don't read past
1598 * If this means reading 0 then we were asked to read past
1599 * the end of file. */
1600 amount
= min((unsigned int) amount_left
, mod_data
.buflen
);
1601 amount
= min((loff_t
) amount
,
1602 curlun
->file_length
- file_offset
);
1603 partial_page
= file_offset
& (PAGE_CACHE_SIZE
- 1);
1604 if (partial_page
> 0)
1605 amount
= min(amount
, (unsigned int) PAGE_CACHE_SIZE
-
1608 /* Wait for the next buffer to become available */
1609 bh
= fsg
->next_buffhd_to_fill
;
1610 while (bh
->state
!= BUF_STATE_EMPTY
) {
1611 rc
= sleep_thread(fsg
);
1616 /* If we were asked to read past the end of file,
1617 * end with an empty buffer. */
1619 curlun
->sense_data
=
1620 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
1621 curlun
->sense_data_info
= file_offset
>> 9;
1622 curlun
->info_valid
= 1;
1623 bh
->inreq
->length
= 0;
1624 bh
->state
= BUF_STATE_FULL
;
1628 /* Perform the read */
1629 file_offset_tmp
= file_offset
;
1630 nread
= vfs_read(curlun
->filp
,
1631 (char __user
*) bh
->buf
,
1632 amount
, &file_offset_tmp
);
1633 VLDBG(curlun
, "file read %u @ %llu -> %d\n", amount
,
1634 (unsigned long long) file_offset
,
1636 if (signal_pending(current
))
1640 LDBG(curlun
, "error in file read: %d\n",
1643 } else if (nread
< amount
) {
1644 LDBG(curlun
, "partial file read: %d/%u\n",
1645 (int) nread
, amount
);
1646 nread
-= (nread
& 511); // Round down to a block
1648 file_offset
+= nread
;
1649 amount_left
-= nread
;
1650 fsg
->residue
-= nread
;
1651 bh
->inreq
->length
= nread
;
1652 bh
->state
= BUF_STATE_FULL
;
1654 /* If an error occurred, report it and its position */
1655 if (nread
< amount
) {
1656 curlun
->sense_data
= SS_UNRECOVERED_READ_ERROR
;
1657 curlun
->sense_data_info
= file_offset
>> 9;
1658 curlun
->info_valid
= 1;
1662 if (amount_left
== 0)
1663 break; // No more left to read
1665 /* Send this buffer and go read some more */
1666 bh
->inreq
->zero
= 0;
1667 start_transfer(fsg
, fsg
->bulk_in
, bh
->inreq
,
1668 &bh
->inreq_busy
, &bh
->state
);
1669 fsg
->next_buffhd_to_fill
= bh
->next
;
1672 return -EIO
; // No default reply
1676 /*-------------------------------------------------------------------------*/
1678 static int do_write(struct fsg_dev
*fsg
)
1680 struct lun
*curlun
= fsg
->curlun
;
1682 struct fsg_buffhd
*bh
;
1684 u32 amount_left_to_req
, amount_left_to_write
;
1685 loff_t usb_offset
, file_offset
, file_offset_tmp
;
1686 unsigned int amount
;
1687 unsigned int partial_page
;
1692 curlun
->sense_data
= SS_WRITE_PROTECTED
;
1695 spin_lock(&curlun
->filp
->f_lock
);
1696 curlun
->filp
->f_flags
&= ~O_SYNC
; // Default is not to wait
1697 spin_unlock(&curlun
->filp
->f_lock
);
1699 /* Get the starting Logical Block Address and check that it's
1701 if (fsg
->cmnd
[0] == SC_WRITE_6
)
1702 lba
= get_unaligned_be24(&fsg
->cmnd
[1]);
1704 lba
= get_unaligned_be32(&fsg
->cmnd
[2]);
1706 /* We allow DPO (Disable Page Out = don't save data in the
1707 * cache) and FUA (Force Unit Access = write directly to the
1708 * medium). We don't implement DPO; we implement FUA by
1709 * performing synchronous output. */
1710 if ((fsg
->cmnd
[1] & ~0x18) != 0) {
1711 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1714 if (fsg
->cmnd
[1] & 0x08) { // FUA
1715 spin_lock(&curlun
->filp
->f_lock
);
1716 curlun
->filp
->f_flags
|= O_SYNC
;
1717 spin_unlock(&curlun
->filp
->f_lock
);
1720 if (lba
>= curlun
->num_sectors
) {
1721 curlun
->sense_data
= SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
1725 /* Carry out the file writes */
1727 file_offset
= usb_offset
= ((loff_t
) lba
) << 9;
1728 amount_left_to_req
= amount_left_to_write
= fsg
->data_size_from_cmnd
;
1730 while (amount_left_to_write
> 0) {
1732 /* Queue a request for more data from the host */
1733 bh
= fsg
->next_buffhd_to_fill
;
1734 if (bh
->state
== BUF_STATE_EMPTY
&& get_some_more
) {
1736 /* Figure out how much we want to get:
1737 * Try to get the remaining amount.
1738 * But don't get more than the buffer size.
1739 * And don't try to go past the end of the file.
1740 * If we're not at a page boundary,
1741 * don't go past the next page.
1742 * If this means getting 0, then we were asked
1743 * to write past the end of file.
1744 * Finally, round down to a block boundary. */
1745 amount
= min(amount_left_to_req
, mod_data
.buflen
);
1746 amount
= min((loff_t
) amount
, curlun
->file_length
-
1748 partial_page
= usb_offset
& (PAGE_CACHE_SIZE
- 1);
1749 if (partial_page
> 0)
1750 amount
= min(amount
,
1751 (unsigned int) PAGE_CACHE_SIZE
- partial_page
);
1755 curlun
->sense_data
=
1756 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
1757 curlun
->sense_data_info
= usb_offset
>> 9;
1758 curlun
->info_valid
= 1;
1761 amount
-= (amount
& 511);
1764 /* Why were we were asked to transfer a
1770 /* Get the next buffer */
1771 usb_offset
+= amount
;
1772 fsg
->usb_amount_left
-= amount
;
1773 amount_left_to_req
-= amount
;
1774 if (amount_left_to_req
== 0)
1777 /* amount is always divisible by 512, hence by
1778 * the bulk-out maxpacket size */
1779 bh
->outreq
->length
= bh
->bulk_out_intended_length
=
1781 bh
->outreq
->short_not_ok
= 1;
1782 start_transfer(fsg
, fsg
->bulk_out
, bh
->outreq
,
1783 &bh
->outreq_busy
, &bh
->state
);
1784 fsg
->next_buffhd_to_fill
= bh
->next
;
1788 /* Write the received data to the backing file */
1789 bh
= fsg
->next_buffhd_to_drain
;
1790 if (bh
->state
== BUF_STATE_EMPTY
&& !get_some_more
)
1791 break; // We stopped early
1792 if (bh
->state
== BUF_STATE_FULL
) {
1794 fsg
->next_buffhd_to_drain
= bh
->next
;
1795 bh
->state
= BUF_STATE_EMPTY
;
1797 /* Did something go wrong with the transfer? */
1798 if (bh
->outreq
->status
!= 0) {
1799 curlun
->sense_data
= SS_COMMUNICATION_FAILURE
;
1800 curlun
->sense_data_info
= file_offset
>> 9;
1801 curlun
->info_valid
= 1;
1805 amount
= bh
->outreq
->actual
;
1806 if (curlun
->file_length
- file_offset
< amount
) {
1808 "write %u @ %llu beyond end %llu\n",
1809 amount
, (unsigned long long) file_offset
,
1810 (unsigned long long) curlun
->file_length
);
1811 amount
= curlun
->file_length
- file_offset
;
1814 /* Perform the write */
1815 file_offset_tmp
= file_offset
;
1816 nwritten
= vfs_write(curlun
->filp
,
1817 (char __user
*) bh
->buf
,
1818 amount
, &file_offset_tmp
);
1819 VLDBG(curlun
, "file write %u @ %llu -> %d\n", amount
,
1820 (unsigned long long) file_offset
,
1822 if (signal_pending(current
))
1823 return -EINTR
; // Interrupted!
1826 LDBG(curlun
, "error in file write: %d\n",
1829 } else if (nwritten
< amount
) {
1830 LDBG(curlun
, "partial file write: %d/%u\n",
1831 (int) nwritten
, amount
);
1832 nwritten
-= (nwritten
& 511);
1833 // Round down to a block
1835 file_offset
+= nwritten
;
1836 amount_left_to_write
-= nwritten
;
1837 fsg
->residue
-= nwritten
;
1839 /* If an error occurred, report it and its position */
1840 if (nwritten
< amount
) {
1841 curlun
->sense_data
= SS_WRITE_ERROR
;
1842 curlun
->sense_data_info
= file_offset
>> 9;
1843 curlun
->info_valid
= 1;
1847 /* Did the host decide to stop early? */
1848 if (bh
->outreq
->actual
!= bh
->outreq
->length
) {
1849 fsg
->short_packet_received
= 1;
1855 /* Wait for something to happen */
1856 rc
= sleep_thread(fsg
);
1861 return -EIO
; // No default reply
1865 /*-------------------------------------------------------------------------*/
1867 /* Sync the file data, don't bother with the metadata.
1868 * This code was copied from fs/buffer.c:sys_fdatasync(). */
1869 static int fsync_sub(struct lun
*curlun
)
1871 struct file
*filp
= curlun
->filp
;
1873 if (curlun
->ro
|| !filp
)
1875 return vfs_fsync(filp
, filp
->f_path
.dentry
, 1);
1878 static void fsync_all(struct fsg_dev
*fsg
)
1882 for (i
= 0; i
< fsg
->nluns
; ++i
)
1883 fsync_sub(&fsg
->luns
[i
]);
1886 static int do_synchronize_cache(struct fsg_dev
*fsg
)
1888 struct lun
*curlun
= fsg
->curlun
;
1891 /* We ignore the requested LBA and write out all file's
1892 * dirty data buffers. */
1893 rc
= fsync_sub(curlun
);
1895 curlun
->sense_data
= SS_WRITE_ERROR
;
1900 /*-------------------------------------------------------------------------*/
1902 static void invalidate_sub(struct lun
*curlun
)
1904 struct file
*filp
= curlun
->filp
;
1905 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1908 rc
= invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
1909 VLDBG(curlun
, "invalidate_inode_pages -> %ld\n", rc
);
1912 static int do_verify(struct fsg_dev
*fsg
)
1914 struct lun
*curlun
= fsg
->curlun
;
1916 u32 verification_length
;
1917 struct fsg_buffhd
*bh
= fsg
->next_buffhd_to_fill
;
1918 loff_t file_offset
, file_offset_tmp
;
1920 unsigned int amount
;
1923 /* Get the starting Logical Block Address and check that it's
1925 lba
= get_unaligned_be32(&fsg
->cmnd
[2]);
1926 if (lba
>= curlun
->num_sectors
) {
1927 curlun
->sense_data
= SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
1931 /* We allow DPO (Disable Page Out = don't save data in the
1932 * cache) but we don't implement it. */
1933 if ((fsg
->cmnd
[1] & ~0x10) != 0) {
1934 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
1938 verification_length
= get_unaligned_be16(&fsg
->cmnd
[7]);
1939 if (unlikely(verification_length
== 0))
1940 return -EIO
; // No default reply
1942 /* Prepare to carry out the file verify */
1943 amount_left
= verification_length
<< 9;
1944 file_offset
= ((loff_t
) lba
) << 9;
1946 /* Write out all the dirty buffers before invalidating them */
1948 if (signal_pending(current
))
1951 invalidate_sub(curlun
);
1952 if (signal_pending(current
))
1955 /* Just try to read the requested blocks */
1956 while (amount_left
> 0) {
1958 /* Figure out how much we need to read:
1959 * Try to read the remaining amount, but not more than
1961 * And don't try to read past the end of the file.
1962 * If this means reading 0 then we were asked to read
1963 * past the end of file. */
1964 amount
= min((unsigned int) amount_left
, mod_data
.buflen
);
1965 amount
= min((loff_t
) amount
,
1966 curlun
->file_length
- file_offset
);
1968 curlun
->sense_data
=
1969 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
1970 curlun
->sense_data_info
= file_offset
>> 9;
1971 curlun
->info_valid
= 1;
1975 /* Perform the read */
1976 file_offset_tmp
= file_offset
;
1977 nread
= vfs_read(curlun
->filp
,
1978 (char __user
*) bh
->buf
,
1979 amount
, &file_offset_tmp
);
1980 VLDBG(curlun
, "file read %u @ %llu -> %d\n", amount
,
1981 (unsigned long long) file_offset
,
1983 if (signal_pending(current
))
1987 LDBG(curlun
, "error in file verify: %d\n",
1990 } else if (nread
< amount
) {
1991 LDBG(curlun
, "partial file verify: %d/%u\n",
1992 (int) nread
, amount
);
1993 nread
-= (nread
& 511); // Round down to a sector
1996 curlun
->sense_data
= SS_UNRECOVERED_READ_ERROR
;
1997 curlun
->sense_data_info
= file_offset
>> 9;
1998 curlun
->info_valid
= 1;
2001 file_offset
+= nread
;
2002 amount_left
-= nread
;
2008 /*-------------------------------------------------------------------------*/
2010 static int do_inquiry(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
2012 u8
*buf
= (u8
*) bh
->buf
;
2014 static char vendor_id
[] = "Linux ";
2015 static char product_disk_id
[] = "File-Stor Gadget";
2016 static char product_cdrom_id
[] = "File-CD Gadget ";
2018 if (!fsg
->curlun
) { // Unsupported LUNs are okay
2019 fsg
->bad_lun_okay
= 1;
2021 buf
[0] = 0x7f; // Unsupported, no device-type
2022 buf
[4] = 31; // Additional length
2027 buf
[0] = (mod_data
.cdrom
? TYPE_CDROM
: TYPE_DISK
);
2028 if (mod_data
.removable
)
2030 buf
[2] = 2; // ANSI SCSI level 2
2031 buf
[3] = 2; // SCSI-2 INQUIRY data format
2032 buf
[4] = 31; // Additional length
2033 // No special options
2034 sprintf(buf
+ 8, "%-8s%-16s%04x", vendor_id
,
2035 (mod_data
.cdrom
? product_cdrom_id
:
2042 static int do_request_sense(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
2044 struct lun
*curlun
= fsg
->curlun
;
2045 u8
*buf
= (u8
*) bh
->buf
;
2050 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
2052 * If a REQUEST SENSE command is received from an initiator
2053 * with a pending unit attention condition (before the target
2054 * generates the contingent allegiance condition), then the
2055 * target shall either:
2056 * a) report any pending sense data and preserve the unit
2057 * attention condition on the logical unit, or,
2058 * b) report the unit attention condition, may discard any
2059 * pending sense data, and clear the unit attention
2060 * condition on the logical unit for that initiator.
2062 * FSG normally uses option a); enable this code to use option b).
2065 if (curlun
&& curlun
->unit_attention_data
!= SS_NO_SENSE
) {
2066 curlun
->sense_data
= curlun
->unit_attention_data
;
2067 curlun
->unit_attention_data
= SS_NO_SENSE
;
2071 if (!curlun
) { // Unsupported LUNs are okay
2072 fsg
->bad_lun_okay
= 1;
2073 sd
= SS_LOGICAL_UNIT_NOT_SUPPORTED
;
2077 sd
= curlun
->sense_data
;
2078 sdinfo
= curlun
->sense_data_info
;
2079 valid
= curlun
->info_valid
<< 7;
2080 curlun
->sense_data
= SS_NO_SENSE
;
2081 curlun
->sense_data_info
= 0;
2082 curlun
->info_valid
= 0;
2086 buf
[0] = valid
| 0x70; // Valid, current error
2088 put_unaligned_be32(sdinfo
, &buf
[3]); /* Sense information */
2089 buf
[7] = 18 - 8; // Additional sense length
2096 static int do_read_capacity(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
2098 struct lun
*curlun
= fsg
->curlun
;
2099 u32 lba
= get_unaligned_be32(&fsg
->cmnd
[2]);
2100 int pmi
= fsg
->cmnd
[8];
2101 u8
*buf
= (u8
*) bh
->buf
;
2103 /* Check the PMI and LBA fields */
2104 if (pmi
> 1 || (pmi
== 0 && lba
!= 0)) {
2105 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
2109 put_unaligned_be32(curlun
->num_sectors
- 1, &buf
[0]);
2110 /* Max logical block */
2111 put_unaligned_be32(512, &buf
[4]); /* Block length */
2116 static void store_cdrom_address(u8
*dest
, int msf
, u32 addr
)
2119 /* Convert to Minutes-Seconds-Frames */
2120 addr
>>= 2; /* Convert to 2048-byte frames */
2121 addr
+= 2*75; /* Lead-in occupies 2 seconds */
2122 dest
[3] = addr
% 75; /* Frames */
2124 dest
[2] = addr
% 60; /* Seconds */
2126 dest
[1] = addr
; /* Minutes */
2127 dest
[0] = 0; /* Reserved */
2129 /* Absolute sector */
2130 put_unaligned_be32(addr
, dest
);
2134 static int do_read_header(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
2136 struct lun
*curlun
= fsg
->curlun
;
2137 int msf
= fsg
->cmnd
[1] & 0x02;
2138 u32 lba
= get_unaligned_be32(&fsg
->cmnd
[2]);
2139 u8
*buf
= (u8
*) bh
->buf
;
2141 if ((fsg
->cmnd
[1] & ~0x02) != 0) { /* Mask away MSF */
2142 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
2145 if (lba
>= curlun
->num_sectors
) {
2146 curlun
->sense_data
= SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
;
2151 buf
[0] = 0x01; /* 2048 bytes of user data, rest is EC */
2152 store_cdrom_address(&buf
[4], msf
, lba
);
2157 static int do_read_toc(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
2159 struct lun
*curlun
= fsg
->curlun
;
2160 int msf
= fsg
->cmnd
[1] & 0x02;
2161 int start_track
= fsg
->cmnd
[6];
2162 u8
*buf
= (u8
*) bh
->buf
;
2164 if ((fsg
->cmnd
[1] & ~0x02) != 0 || /* Mask away MSF */
2166 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
2171 buf
[1] = (20-2); /* TOC data length */
2172 buf
[2] = 1; /* First track number */
2173 buf
[3] = 1; /* Last track number */
2174 buf
[5] = 0x16; /* Data track, copying allowed */
2175 buf
[6] = 0x01; /* Only track is number 1 */
2176 store_cdrom_address(&buf
[8], msf
, 0);
2178 buf
[13] = 0x16; /* Lead-out track is data */
2179 buf
[14] = 0xAA; /* Lead-out track number */
2180 store_cdrom_address(&buf
[16], msf
, curlun
->num_sectors
);
2185 static int do_mode_sense(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
2187 struct lun
*curlun
= fsg
->curlun
;
2188 int mscmnd
= fsg
->cmnd
[0];
2189 u8
*buf
= (u8
*) bh
->buf
;
2192 int changeable_values
, all_pages
;
2196 if ((fsg
->cmnd
[1] & ~0x08) != 0) { // Mask away DBD
2197 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
2200 pc
= fsg
->cmnd
[2] >> 6;
2201 page_code
= fsg
->cmnd
[2] & 0x3f;
2203 curlun
->sense_data
= SS_SAVING_PARAMETERS_NOT_SUPPORTED
;
2206 changeable_values
= (pc
== 1);
2207 all_pages
= (page_code
== 0x3f);
2209 /* Write the mode parameter header. Fixed values are: default
2210 * medium type, no cache control (DPOFUA), and no block descriptors.
2211 * The only variable value is the WriteProtect bit. We will fill in
2212 * the mode data length later. */
2214 if (mscmnd
== SC_MODE_SENSE_6
) {
2215 buf
[2] = (curlun
->ro
? 0x80 : 0x00); // WP, DPOFUA
2218 } else { // SC_MODE_SENSE_10
2219 buf
[3] = (curlun
->ro
? 0x80 : 0x00); // WP, DPOFUA
2221 limit
= 65535; // Should really be mod_data.buflen
2224 /* No block descriptors */
2226 /* The mode pages, in numerical order. The only page we support
2227 * is the Caching page. */
2228 if (page_code
== 0x08 || all_pages
) {
2230 buf
[0] = 0x08; // Page code
2231 buf
[1] = 10; // Page length
2232 memset(buf
+2, 0, 10); // None of the fields are changeable
2234 if (!changeable_values
) {
2235 buf
[2] = 0x04; // Write cache enable,
2236 // Read cache not disabled
2237 // No cache retention priorities
2238 put_unaligned_be16(0xffff, &buf
[4]);
2239 /* Don't disable prefetch */
2240 /* Minimum prefetch = 0 */
2241 put_unaligned_be16(0xffff, &buf
[8]);
2242 /* Maximum prefetch */
2243 put_unaligned_be16(0xffff, &buf
[10]);
2244 /* Maximum prefetch ceiling */
2249 /* Check that a valid page was requested and the mode data length
2250 * isn't too long. */
2252 if (!valid_page
|| len
> limit
) {
2253 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
2257 /* Store the mode data length */
2258 if (mscmnd
== SC_MODE_SENSE_6
)
2261 put_unaligned_be16(len
- 2, buf0
);
2266 static int do_start_stop(struct fsg_dev
*fsg
)
2268 struct lun
*curlun
= fsg
->curlun
;
2271 if (!mod_data
.removable
) {
2272 curlun
->sense_data
= SS_INVALID_COMMAND
;
2276 // int immed = fsg->cmnd[1] & 0x01;
2277 loej
= fsg
->cmnd
[4] & 0x02;
2278 start
= fsg
->cmnd
[4] & 0x01;
2280 #ifdef CONFIG_USB_FILE_STORAGE_TEST
2281 if ((fsg
->cmnd
[1] & ~0x01) != 0 || // Mask away Immed
2282 (fsg
->cmnd
[4] & ~0x03) != 0) { // Mask LoEj, Start
2283 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
2289 /* Are we allowed to unload the media? */
2290 if (curlun
->prevent_medium_removal
) {
2291 LDBG(curlun
, "unload attempt prevented\n");
2292 curlun
->sense_data
= SS_MEDIUM_REMOVAL_PREVENTED
;
2295 if (loej
) { // Simulate an unload/eject
2296 up_read(&fsg
->filesem
);
2297 down_write(&fsg
->filesem
);
2298 close_backing_file(curlun
);
2299 up_write(&fsg
->filesem
);
2300 down_read(&fsg
->filesem
);
2304 /* Our emulation doesn't support mounting; the medium is
2305 * available for use as soon as it is loaded. */
2306 if (!backing_file_is_open(curlun
)) {
2307 curlun
->sense_data
= SS_MEDIUM_NOT_PRESENT
;
2316 static int do_prevent_allow(struct fsg_dev
*fsg
)
2318 struct lun
*curlun
= fsg
->curlun
;
2321 if (!mod_data
.removable
) {
2322 curlun
->sense_data
= SS_INVALID_COMMAND
;
2326 prevent
= fsg
->cmnd
[4] & 0x01;
2327 if ((fsg
->cmnd
[4] & ~0x01) != 0) { // Mask away Prevent
2328 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
2332 if (curlun
->prevent_medium_removal
&& !prevent
)
2334 curlun
->prevent_medium_removal
= prevent
;
2339 static int do_read_format_capacities(struct fsg_dev
*fsg
,
2340 struct fsg_buffhd
*bh
)
2342 struct lun
*curlun
= fsg
->curlun
;
2343 u8
*buf
= (u8
*) bh
->buf
;
2345 buf
[0] = buf
[1] = buf
[2] = 0;
2346 buf
[3] = 8; // Only the Current/Maximum Capacity Descriptor
2349 put_unaligned_be32(curlun
->num_sectors
, &buf
[0]);
2350 /* Number of blocks */
2351 put_unaligned_be32(512, &buf
[4]); /* Block length */
2352 buf
[4] = 0x02; /* Current capacity */
2357 static int do_mode_select(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
2359 struct lun
*curlun
= fsg
->curlun
;
2361 /* We don't support MODE SELECT */
2362 curlun
->sense_data
= SS_INVALID_COMMAND
;
2367 /*-------------------------------------------------------------------------*/
2369 static int halt_bulk_in_endpoint(struct fsg_dev
*fsg
)
2373 rc
= fsg_set_halt(fsg
, fsg
->bulk_in
);
2375 VDBG(fsg
, "delayed bulk-in endpoint halt\n");
2377 if (rc
!= -EAGAIN
) {
2378 WARNING(fsg
, "usb_ep_set_halt -> %d\n", rc
);
2383 /* Wait for a short time and then try again */
2384 if (msleep_interruptible(100) != 0)
2386 rc
= usb_ep_set_halt(fsg
->bulk_in
);
2391 static int wedge_bulk_in_endpoint(struct fsg_dev
*fsg
)
2395 DBG(fsg
, "bulk-in set wedge\n");
2396 rc
= usb_ep_set_wedge(fsg
->bulk_in
);
2398 VDBG(fsg
, "delayed bulk-in endpoint wedge\n");
2400 if (rc
!= -EAGAIN
) {
2401 WARNING(fsg
, "usb_ep_set_wedge -> %d\n", rc
);
2406 /* Wait for a short time and then try again */
2407 if (msleep_interruptible(100) != 0)
2409 rc
= usb_ep_set_wedge(fsg
->bulk_in
);
2414 static int pad_with_zeros(struct fsg_dev
*fsg
)
2416 struct fsg_buffhd
*bh
= fsg
->next_buffhd_to_fill
;
2417 u32 nkeep
= bh
->inreq
->length
;
2421 bh
->state
= BUF_STATE_EMPTY
; // For the first iteration
2422 fsg
->usb_amount_left
= nkeep
+ fsg
->residue
;
2423 while (fsg
->usb_amount_left
> 0) {
2425 /* Wait for the next buffer to be free */
2426 while (bh
->state
!= BUF_STATE_EMPTY
) {
2427 rc
= sleep_thread(fsg
);
2432 nsend
= min(fsg
->usb_amount_left
, (u32
) mod_data
.buflen
);
2433 memset(bh
->buf
+ nkeep
, 0, nsend
- nkeep
);
2434 bh
->inreq
->length
= nsend
;
2435 bh
->inreq
->zero
= 0;
2436 start_transfer(fsg
, fsg
->bulk_in
, bh
->inreq
,
2437 &bh
->inreq_busy
, &bh
->state
);
2438 bh
= fsg
->next_buffhd_to_fill
= bh
->next
;
2439 fsg
->usb_amount_left
-= nsend
;
2445 static int throw_away_data(struct fsg_dev
*fsg
)
2447 struct fsg_buffhd
*bh
;
2451 while ((bh
= fsg
->next_buffhd_to_drain
)->state
!= BUF_STATE_EMPTY
||
2452 fsg
->usb_amount_left
> 0) {
2454 /* Throw away the data in a filled buffer */
2455 if (bh
->state
== BUF_STATE_FULL
) {
2457 bh
->state
= BUF_STATE_EMPTY
;
2458 fsg
->next_buffhd_to_drain
= bh
->next
;
2460 /* A short packet or an error ends everything */
2461 if (bh
->outreq
->actual
!= bh
->outreq
->length
||
2462 bh
->outreq
->status
!= 0) {
2463 raise_exception(fsg
, FSG_STATE_ABORT_BULK_OUT
);
2469 /* Try to submit another request if we need one */
2470 bh
= fsg
->next_buffhd_to_fill
;
2471 if (bh
->state
== BUF_STATE_EMPTY
&& fsg
->usb_amount_left
> 0) {
2472 amount
= min(fsg
->usb_amount_left
,
2473 (u32
) mod_data
.buflen
);
2475 /* amount is always divisible by 512, hence by
2476 * the bulk-out maxpacket size */
2477 bh
->outreq
->length
= bh
->bulk_out_intended_length
=
2479 bh
->outreq
->short_not_ok
= 1;
2480 start_transfer(fsg
, fsg
->bulk_out
, bh
->outreq
,
2481 &bh
->outreq_busy
, &bh
->state
);
2482 fsg
->next_buffhd_to_fill
= bh
->next
;
2483 fsg
->usb_amount_left
-= amount
;
2487 /* Otherwise wait for something to happen */
2488 rc
= sleep_thread(fsg
);
2496 static int finish_reply(struct fsg_dev
*fsg
)
2498 struct fsg_buffhd
*bh
= fsg
->next_buffhd_to_fill
;
2501 switch (fsg
->data_dir
) {
2503 break; // Nothing to send
2505 /* If we don't know whether the host wants to read or write,
2506 * this must be CB or CBI with an unknown command. We mustn't
2507 * try to send or receive any data. So stall both bulk pipes
2508 * if we can and wait for a reset. */
2509 case DATA_DIR_UNKNOWN
:
2510 if (mod_data
.can_stall
) {
2511 fsg_set_halt(fsg
, fsg
->bulk_out
);
2512 rc
= halt_bulk_in_endpoint(fsg
);
2516 /* All but the last buffer of data must have already been sent */
2517 case DATA_DIR_TO_HOST
:
2518 if (fsg
->data_size
== 0)
2519 ; // Nothing to send
2521 /* If there's no residue, simply send the last buffer */
2522 else if (fsg
->residue
== 0) {
2523 bh
->inreq
->zero
= 0;
2524 start_transfer(fsg
, fsg
->bulk_in
, bh
->inreq
,
2525 &bh
->inreq_busy
, &bh
->state
);
2526 fsg
->next_buffhd_to_fill
= bh
->next
;
2529 /* There is a residue. For CB and CBI, simply mark the end
2530 * of the data with a short packet. However, if we are
2531 * allowed to stall, there was no data at all (residue ==
2532 * data_size), and the command failed (invalid LUN or
2533 * sense data is set), then halt the bulk-in endpoint
2535 else if (!transport_is_bbb()) {
2536 if (mod_data
.can_stall
&&
2537 fsg
->residue
== fsg
->data_size
&&
2538 (!fsg
->curlun
|| fsg
->curlun
->sense_data
!= SS_NO_SENSE
)) {
2539 bh
->state
= BUF_STATE_EMPTY
;
2540 rc
= halt_bulk_in_endpoint(fsg
);
2542 bh
->inreq
->zero
= 1;
2543 start_transfer(fsg
, fsg
->bulk_in
, bh
->inreq
,
2544 &bh
->inreq_busy
, &bh
->state
);
2545 fsg
->next_buffhd_to_fill
= bh
->next
;
2549 /* For Bulk-only, if we're allowed to stall then send the
2550 * short packet and halt the bulk-in endpoint. If we can't
2551 * stall, pad out the remaining data with 0's. */
2553 if (mod_data
.can_stall
) {
2554 bh
->inreq
->zero
= 1;
2555 start_transfer(fsg
, fsg
->bulk_in
, bh
->inreq
,
2556 &bh
->inreq_busy
, &bh
->state
);
2557 fsg
->next_buffhd_to_fill
= bh
->next
;
2558 rc
= halt_bulk_in_endpoint(fsg
);
2560 rc
= pad_with_zeros(fsg
);
2564 /* We have processed all we want from the data the host has sent.
2565 * There may still be outstanding bulk-out requests. */
2566 case DATA_DIR_FROM_HOST
:
2567 if (fsg
->residue
== 0)
2568 ; // Nothing to receive
2570 /* Did the host stop sending unexpectedly early? */
2571 else if (fsg
->short_packet_received
) {
2572 raise_exception(fsg
, FSG_STATE_ABORT_BULK_OUT
);
2576 /* We haven't processed all the incoming data. Even though
2577 * we may be allowed to stall, doing so would cause a race.
2578 * The controller may already have ACK'ed all the remaining
2579 * bulk-out packets, in which case the host wouldn't see a
2580 * STALL. Not realizing the endpoint was halted, it wouldn't
2581 * clear the halt -- leading to problems later on. */
2583 else if (mod_data
.can_stall
) {
2584 fsg_set_halt(fsg
, fsg
->bulk_out
);
2585 raise_exception(fsg
, FSG_STATE_ABORT_BULK_OUT
);
2590 /* We can't stall. Read in the excess data and throw it
2593 rc
= throw_away_data(fsg
);
2600 static int send_status(struct fsg_dev
*fsg
)
2602 struct lun
*curlun
= fsg
->curlun
;
2603 struct fsg_buffhd
*bh
;
2605 u8 status
= USB_STATUS_PASS
;
2608 /* Wait for the next buffer to become available */
2609 bh
= fsg
->next_buffhd_to_fill
;
2610 while (bh
->state
!= BUF_STATE_EMPTY
) {
2611 rc
= sleep_thread(fsg
);
2617 sd
= curlun
->sense_data
;
2618 sdinfo
= curlun
->sense_data_info
;
2619 } else if (fsg
->bad_lun_okay
)
2622 sd
= SS_LOGICAL_UNIT_NOT_SUPPORTED
;
2624 if (fsg
->phase_error
) {
2625 DBG(fsg
, "sending phase-error status\n");
2626 status
= USB_STATUS_PHASE_ERROR
;
2627 sd
= SS_INVALID_COMMAND
;
2628 } else if (sd
!= SS_NO_SENSE
) {
2629 DBG(fsg
, "sending command-failure status\n");
2630 status
= USB_STATUS_FAIL
;
2631 VDBG(fsg
, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2633 SK(sd
), ASC(sd
), ASCQ(sd
), sdinfo
);
2636 if (transport_is_bbb()) {
2637 struct bulk_cs_wrap
*csw
= bh
->buf
;
2639 /* Store and send the Bulk-only CSW */
2640 csw
->Signature
= cpu_to_le32(USB_BULK_CS_SIG
);
2641 csw
->Tag
= fsg
->tag
;
2642 csw
->Residue
= cpu_to_le32(fsg
->residue
);
2643 csw
->Status
= status
;
2645 bh
->inreq
->length
= USB_BULK_CS_WRAP_LEN
;
2646 bh
->inreq
->zero
= 0;
2647 start_transfer(fsg
, fsg
->bulk_in
, bh
->inreq
,
2648 &bh
->inreq_busy
, &bh
->state
);
2650 } else if (mod_data
.transport_type
== USB_PR_CB
) {
2652 /* Control-Bulk transport has no status phase! */
2655 } else { // USB_PR_CBI
2656 struct interrupt_data
*buf
= bh
->buf
;
2658 /* Store and send the Interrupt data. UFI sends the ASC
2659 * and ASCQ bytes. Everything else sends a Type (which
2660 * is always 0) and the status Value. */
2661 if (mod_data
.protocol_type
== USB_SC_UFI
) {
2662 buf
->bType
= ASC(sd
);
2663 buf
->bValue
= ASCQ(sd
);
2666 buf
->bValue
= status
;
2668 fsg
->intreq
->length
= CBI_INTERRUPT_DATA_LEN
;
2670 fsg
->intr_buffhd
= bh
; // Point to the right buffhd
2671 fsg
->intreq
->buf
= bh
->inreq
->buf
;
2672 fsg
->intreq
->context
= bh
;
2673 start_transfer(fsg
, fsg
->intr_in
, fsg
->intreq
,
2674 &fsg
->intreq_busy
, &bh
->state
);
2677 fsg
->next_buffhd_to_fill
= bh
->next
;
2682 /*-------------------------------------------------------------------------*/
2684 /* Check whether the command is properly formed and whether its data size
2685 * and direction agree with the values we already have. */
2686 static int check_command(struct fsg_dev
*fsg
, int cmnd_size
,
2687 enum data_direction data_dir
, unsigned int mask
,
2688 int needs_medium
, const char *name
)
2691 int lun
= fsg
->cmnd
[1] >> 5;
2692 static const char dirletter
[4] = {'u', 'o', 'i', 'n'};
2696 /* Adjust the expected cmnd_size for protocol encapsulation padding.
2697 * Transparent SCSI doesn't pad. */
2698 if (protocol_is_scsi())
2701 /* There's some disagreement as to whether RBC pads commands or not.
2702 * We'll play it safe and accept either form. */
2703 else if (mod_data
.protocol_type
== USB_SC_RBC
) {
2704 if (fsg
->cmnd_size
== 12)
2707 /* All the other protocols pad to 12 bytes */
2712 if (fsg
->data_dir
!= DATA_DIR_UNKNOWN
)
2713 sprintf(hdlen
, ", H%c=%u", dirletter
[(int) fsg
->data_dir
],
2715 VDBG(fsg
, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
2716 name
, cmnd_size
, dirletter
[(int) data_dir
],
2717 fsg
->data_size_from_cmnd
, fsg
->cmnd_size
, hdlen
);
2719 /* We can't reply at all until we know the correct data direction
2721 if (fsg
->data_size_from_cmnd
== 0)
2722 data_dir
= DATA_DIR_NONE
;
2723 if (fsg
->data_dir
== DATA_DIR_UNKNOWN
) { // CB or CBI
2724 fsg
->data_dir
= data_dir
;
2725 fsg
->data_size
= fsg
->data_size_from_cmnd
;
2727 } else { // Bulk-only
2728 if (fsg
->data_size
< fsg
->data_size_from_cmnd
) {
2730 /* Host data size < Device data size is a phase error.
2731 * Carry out the command, but only transfer as much
2732 * as we are allowed. */
2733 fsg
->data_size_from_cmnd
= fsg
->data_size
;
2734 fsg
->phase_error
= 1;
2737 fsg
->residue
= fsg
->usb_amount_left
= fsg
->data_size
;
2739 /* Conflicting data directions is a phase error */
2740 if (fsg
->data_dir
!= data_dir
&& fsg
->data_size_from_cmnd
> 0) {
2741 fsg
->phase_error
= 1;
2745 /* Verify the length of the command itself */
2746 if (cmnd_size
!= fsg
->cmnd_size
) {
2748 /* Special case workaround: There are plenty of buggy SCSI
2749 * implementations. Many have issues with cbw->Length
2750 * field passing a wrong command size. For those cases we
2751 * always try to work around the problem by using the length
2752 * sent by the host side provided it is at least as large
2753 * as the correct command length.
2754 * Examples of such cases would be MS-Windows, which issues
2755 * REQUEST SENSE with cbw->Length == 12 where it should
2756 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
2757 * REQUEST SENSE with cbw->Length == 10 where it should
2760 if (cmnd_size
<= fsg
->cmnd_size
) {
2761 DBG(fsg
, "%s is buggy! Expected length %d "
2762 "but we got %d\n", name
,
2763 cmnd_size
, fsg
->cmnd_size
);
2764 cmnd_size
= fsg
->cmnd_size
;
2766 fsg
->phase_error
= 1;
2771 /* Check that the LUN values are consistent */
2772 if (transport_is_bbb()) {
2773 if (fsg
->lun
!= lun
)
2774 DBG(fsg
, "using LUN %d from CBW, "
2775 "not LUN %d from CDB\n",
2778 fsg
->lun
= lun
; // Use LUN from the command
2781 if (fsg
->lun
>= 0 && fsg
->lun
< fsg
->nluns
) {
2782 fsg
->curlun
= curlun
= &fsg
->luns
[fsg
->lun
];
2783 if (fsg
->cmnd
[0] != SC_REQUEST_SENSE
) {
2784 curlun
->sense_data
= SS_NO_SENSE
;
2785 curlun
->sense_data_info
= 0;
2786 curlun
->info_valid
= 0;
2789 fsg
->curlun
= curlun
= NULL
;
2790 fsg
->bad_lun_okay
= 0;
2792 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2793 * to use unsupported LUNs; all others may not. */
2794 if (fsg
->cmnd
[0] != SC_INQUIRY
&&
2795 fsg
->cmnd
[0] != SC_REQUEST_SENSE
) {
2796 DBG(fsg
, "unsupported LUN %d\n", fsg
->lun
);
2801 /* If a unit attention condition exists, only INQUIRY and
2802 * REQUEST SENSE commands are allowed; anything else must fail. */
2803 if (curlun
&& curlun
->unit_attention_data
!= SS_NO_SENSE
&&
2804 fsg
->cmnd
[0] != SC_INQUIRY
&&
2805 fsg
->cmnd
[0] != SC_REQUEST_SENSE
) {
2806 curlun
->sense_data
= curlun
->unit_attention_data
;
2807 curlun
->unit_attention_data
= SS_NO_SENSE
;
2811 /* Check that only command bytes listed in the mask are non-zero */
2812 fsg
->cmnd
[1] &= 0x1f; // Mask away the LUN
2813 for (i
= 1; i
< cmnd_size
; ++i
) {
2814 if (fsg
->cmnd
[i
] && !(mask
& (1 << i
))) {
2816 curlun
->sense_data
= SS_INVALID_FIELD_IN_CDB
;
2821 /* If the medium isn't mounted and the command needs to access
2822 * it, return an error. */
2823 if (curlun
&& !backing_file_is_open(curlun
) && needs_medium
) {
2824 curlun
->sense_data
= SS_MEDIUM_NOT_PRESENT
;
2832 static int do_scsi_command(struct fsg_dev
*fsg
)
2834 struct fsg_buffhd
*bh
;
2836 int reply
= -EINVAL
;
2838 static char unknown
[16];
2842 /* Wait for the next buffer to become available for data or status */
2843 bh
= fsg
->next_buffhd_to_drain
= fsg
->next_buffhd_to_fill
;
2844 while (bh
->state
!= BUF_STATE_EMPTY
) {
2845 rc
= sleep_thread(fsg
);
2849 fsg
->phase_error
= 0;
2850 fsg
->short_packet_received
= 0;
2852 down_read(&fsg
->filesem
); // We're using the backing file
2853 switch (fsg
->cmnd
[0]) {
2856 fsg
->data_size_from_cmnd
= fsg
->cmnd
[4];
2857 if ((reply
= check_command(fsg
, 6, DATA_DIR_TO_HOST
,
2860 reply
= do_inquiry(fsg
, bh
);
2863 case SC_MODE_SELECT_6
:
2864 fsg
->data_size_from_cmnd
= fsg
->cmnd
[4];
2865 if ((reply
= check_command(fsg
, 6, DATA_DIR_FROM_HOST
,
2867 "MODE SELECT(6)")) == 0)
2868 reply
= do_mode_select(fsg
, bh
);
2871 case SC_MODE_SELECT_10
:
2872 fsg
->data_size_from_cmnd
= get_unaligned_be16(&fsg
->cmnd
[7]);
2873 if ((reply
= check_command(fsg
, 10, DATA_DIR_FROM_HOST
,
2875 "MODE SELECT(10)")) == 0)
2876 reply
= do_mode_select(fsg
, bh
);
2879 case SC_MODE_SENSE_6
:
2880 fsg
->data_size_from_cmnd
= fsg
->cmnd
[4];
2881 if ((reply
= check_command(fsg
, 6, DATA_DIR_TO_HOST
,
2882 (1<<1) | (1<<2) | (1<<4), 0,
2883 "MODE SENSE(6)")) == 0)
2884 reply
= do_mode_sense(fsg
, bh
);
2887 case SC_MODE_SENSE_10
:
2888 fsg
->data_size_from_cmnd
= get_unaligned_be16(&fsg
->cmnd
[7]);
2889 if ((reply
= check_command(fsg
, 10, DATA_DIR_TO_HOST
,
2890 (1<<1) | (1<<2) | (3<<7), 0,
2891 "MODE SENSE(10)")) == 0)
2892 reply
= do_mode_sense(fsg
, bh
);
2895 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL
:
2896 fsg
->data_size_from_cmnd
= 0;
2897 if ((reply
= check_command(fsg
, 6, DATA_DIR_NONE
,
2899 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
2900 reply
= do_prevent_allow(fsg
);
2905 fsg
->data_size_from_cmnd
= (i
== 0 ? 256 : i
) << 9;
2906 if ((reply
= check_command(fsg
, 6, DATA_DIR_TO_HOST
,
2909 reply
= do_read(fsg
);
2913 fsg
->data_size_from_cmnd
=
2914 get_unaligned_be16(&fsg
->cmnd
[7]) << 9;
2915 if ((reply
= check_command(fsg
, 10, DATA_DIR_TO_HOST
,
2916 (1<<1) | (0xf<<2) | (3<<7), 1,
2918 reply
= do_read(fsg
);
2922 fsg
->data_size_from_cmnd
=
2923 get_unaligned_be32(&fsg
->cmnd
[6]) << 9;
2924 if ((reply
= check_command(fsg
, 12, DATA_DIR_TO_HOST
,
2925 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2927 reply
= do_read(fsg
);
2930 case SC_READ_CAPACITY
:
2931 fsg
->data_size_from_cmnd
= 8;
2932 if ((reply
= check_command(fsg
, 10, DATA_DIR_TO_HOST
,
2933 (0xf<<2) | (1<<8), 1,
2934 "READ CAPACITY")) == 0)
2935 reply
= do_read_capacity(fsg
, bh
);
2938 case SC_READ_HEADER
:
2939 if (!mod_data
.cdrom
)
2941 fsg
->data_size_from_cmnd
= get_unaligned_be16(&fsg
->cmnd
[7]);
2942 if ((reply
= check_command(fsg
, 10, DATA_DIR_TO_HOST
,
2943 (3<<7) | (0x1f<<1), 1,
2944 "READ HEADER")) == 0)
2945 reply
= do_read_header(fsg
, bh
);
2949 if (!mod_data
.cdrom
)
2951 fsg
->data_size_from_cmnd
= get_unaligned_be16(&fsg
->cmnd
[7]);
2952 if ((reply
= check_command(fsg
, 10, DATA_DIR_TO_HOST
,
2955 reply
= do_read_toc(fsg
, bh
);
2958 case SC_READ_FORMAT_CAPACITIES
:
2959 fsg
->data_size_from_cmnd
= get_unaligned_be16(&fsg
->cmnd
[7]);
2960 if ((reply
= check_command(fsg
, 10, DATA_DIR_TO_HOST
,
2962 "READ FORMAT CAPACITIES")) == 0)
2963 reply
= do_read_format_capacities(fsg
, bh
);
2966 case SC_REQUEST_SENSE
:
2967 fsg
->data_size_from_cmnd
= fsg
->cmnd
[4];
2968 if ((reply
= check_command(fsg
, 6, DATA_DIR_TO_HOST
,
2970 "REQUEST SENSE")) == 0)
2971 reply
= do_request_sense(fsg
, bh
);
2974 case SC_START_STOP_UNIT
:
2975 fsg
->data_size_from_cmnd
= 0;
2976 if ((reply
= check_command(fsg
, 6, DATA_DIR_NONE
,
2978 "START-STOP UNIT")) == 0)
2979 reply
= do_start_stop(fsg
);
2982 case SC_SYNCHRONIZE_CACHE
:
2983 fsg
->data_size_from_cmnd
= 0;
2984 if ((reply
= check_command(fsg
, 10, DATA_DIR_NONE
,
2985 (0xf<<2) | (3<<7), 1,
2986 "SYNCHRONIZE CACHE")) == 0)
2987 reply
= do_synchronize_cache(fsg
);
2990 case SC_TEST_UNIT_READY
:
2991 fsg
->data_size_from_cmnd
= 0;
2992 reply
= check_command(fsg
, 6, DATA_DIR_NONE
,
2997 /* Although optional, this command is used by MS-Windows. We
2998 * support a minimal version: BytChk must be 0. */
3000 fsg
->data_size_from_cmnd
= 0;
3001 if ((reply
= check_command(fsg
, 10, DATA_DIR_NONE
,
3002 (1<<1) | (0xf<<2) | (3<<7), 1,
3004 reply
= do_verify(fsg
);
3009 fsg
->data_size_from_cmnd
= (i
== 0 ? 256 : i
) << 9;
3010 if ((reply
= check_command(fsg
, 6, DATA_DIR_FROM_HOST
,
3013 reply
= do_write(fsg
);
3017 fsg
->data_size_from_cmnd
=
3018 get_unaligned_be16(&fsg
->cmnd
[7]) << 9;
3019 if ((reply
= check_command(fsg
, 10, DATA_DIR_FROM_HOST
,
3020 (1<<1) | (0xf<<2) | (3<<7), 1,
3022 reply
= do_write(fsg
);
3026 fsg
->data_size_from_cmnd
=
3027 get_unaligned_be32(&fsg
->cmnd
[6]) << 9;
3028 if ((reply
= check_command(fsg
, 12, DATA_DIR_FROM_HOST
,
3029 (1<<1) | (0xf<<2) | (0xf<<6), 1,
3031 reply
= do_write(fsg
);
3034 /* Some mandatory commands that we recognize but don't implement.
3035 * They don't mean much in this setting. It's left as an exercise
3036 * for anyone interested to implement RESERVE and RELEASE in terms
3037 * of Posix locks. */
3038 case SC_FORMAT_UNIT
:
3041 case SC_SEND_DIAGNOSTIC
:
3046 fsg
->data_size_from_cmnd
= 0;
3047 sprintf(unknown
, "Unknown x%02x", fsg
->cmnd
[0]);
3048 if ((reply
= check_command(fsg
, fsg
->cmnd_size
,
3049 DATA_DIR_UNKNOWN
, 0xff, 0, unknown
)) == 0) {
3050 fsg
->curlun
->sense_data
= SS_INVALID_COMMAND
;
3055 up_read(&fsg
->filesem
);
3057 if (reply
== -EINTR
|| signal_pending(current
))
3060 /* Set up the single reply buffer for finish_reply() */
3061 if (reply
== -EINVAL
)
3062 reply
= 0; // Error reply length
3063 if (reply
>= 0 && fsg
->data_dir
== DATA_DIR_TO_HOST
) {
3064 reply
= min((u32
) reply
, fsg
->data_size_from_cmnd
);
3065 bh
->inreq
->length
= reply
;
3066 bh
->state
= BUF_STATE_FULL
;
3067 fsg
->residue
-= reply
;
3068 } // Otherwise it's already set
3074 /*-------------------------------------------------------------------------*/
3076 static int received_cbw(struct fsg_dev
*fsg
, struct fsg_buffhd
*bh
)
3078 struct usb_request
*req
= bh
->outreq
;
3079 struct bulk_cb_wrap
*cbw
= req
->buf
;
3081 /* Was this a real packet? Should it be ignored? */
3082 if (req
->status
|| test_bit(IGNORE_BULK_OUT
, &fsg
->atomic_bitflags
))
3085 /* Is the CBW valid? */
3086 if (req
->actual
!= USB_BULK_CB_WRAP_LEN
||
3087 cbw
->Signature
!= cpu_to_le32(
3089 DBG(fsg
, "invalid CBW: len %u sig 0x%x\n",
3091 le32_to_cpu(cbw
->Signature
));
3093 /* The Bulk-only spec says we MUST stall the IN endpoint
3094 * (6.6.1), so it's unavoidable. It also says we must
3095 * retain this state until the next reset, but there's
3096 * no way to tell the controller driver it should ignore
3097 * Clear-Feature(HALT) requests.
3099 * We aren't required to halt the OUT endpoint; instead
3100 * we can simply accept and discard any data received
3101 * until the next reset. */
3102 wedge_bulk_in_endpoint(fsg
);
3103 set_bit(IGNORE_BULK_OUT
, &fsg
->atomic_bitflags
);
3107 /* Is the CBW meaningful? */
3108 if (cbw
->Lun
>= MAX_LUNS
|| cbw
->Flags
& ~USB_BULK_IN_FLAG
||
3109 cbw
->Length
<= 0 || cbw
->Length
> MAX_COMMAND_SIZE
) {
3110 DBG(fsg
, "non-meaningful CBW: lun = %u, flags = 0x%x, "
3112 cbw
->Lun
, cbw
->Flags
, cbw
->Length
);
3114 /* We can do anything we want here, so let's stall the
3115 * bulk pipes if we are allowed to. */
3116 if (mod_data
.can_stall
) {
3117 fsg_set_halt(fsg
, fsg
->bulk_out
);
3118 halt_bulk_in_endpoint(fsg
);
3123 /* Save the command for later */
3124 fsg
->cmnd_size
= cbw
->Length
;
3125 memcpy(fsg
->cmnd
, cbw
->CDB
, fsg
->cmnd_size
);
3126 if (cbw
->Flags
& USB_BULK_IN_FLAG
)
3127 fsg
->data_dir
= DATA_DIR_TO_HOST
;
3129 fsg
->data_dir
= DATA_DIR_FROM_HOST
;
3130 fsg
->data_size
= le32_to_cpu(cbw
->DataTransferLength
);
3131 if (fsg
->data_size
== 0)
3132 fsg
->data_dir
= DATA_DIR_NONE
;
3133 fsg
->lun
= cbw
->Lun
;
3134 fsg
->tag
= cbw
->Tag
;
3139 static int get_next_command(struct fsg_dev
*fsg
)
3141 struct fsg_buffhd
*bh
;
3144 if (transport_is_bbb()) {
3146 /* Wait for the next buffer to become available */
3147 bh
= fsg
->next_buffhd_to_fill
;
3148 while (bh
->state
!= BUF_STATE_EMPTY
) {
3149 rc
= sleep_thread(fsg
);
3154 /* Queue a request to read a Bulk-only CBW */
3155 set_bulk_out_req_length(fsg
, bh
, USB_BULK_CB_WRAP_LEN
);
3156 bh
->outreq
->short_not_ok
= 1;
3157 start_transfer(fsg
, fsg
->bulk_out
, bh
->outreq
,
3158 &bh
->outreq_busy
, &bh
->state
);
3160 /* We will drain the buffer in software, which means we
3161 * can reuse it for the next filling. No need to advance
3162 * next_buffhd_to_fill. */
3164 /* Wait for the CBW to arrive */
3165 while (bh
->state
!= BUF_STATE_FULL
) {
3166 rc
= sleep_thread(fsg
);
3171 rc
= received_cbw(fsg
, bh
);
3172 bh
->state
= BUF_STATE_EMPTY
;
3174 } else { // USB_PR_CB or USB_PR_CBI
3176 /* Wait for the next command to arrive */
3177 while (fsg
->cbbuf_cmnd_size
== 0) {
3178 rc
= sleep_thread(fsg
);
3183 /* Is the previous status interrupt request still busy?
3184 * The host is allowed to skip reading the status,
3185 * so we must cancel it. */
3186 if (fsg
->intreq_busy
)
3187 usb_ep_dequeue(fsg
->intr_in
, fsg
->intreq
);
3189 /* Copy the command and mark the buffer empty */
3190 fsg
->data_dir
= DATA_DIR_UNKNOWN
;
3191 spin_lock_irq(&fsg
->lock
);
3192 fsg
->cmnd_size
= fsg
->cbbuf_cmnd_size
;
3193 memcpy(fsg
->cmnd
, fsg
->cbbuf_cmnd
, fsg
->cmnd_size
);
3194 fsg
->cbbuf_cmnd_size
= 0;
3195 spin_unlock_irq(&fsg
->lock
);
3201 /*-------------------------------------------------------------------------*/
3203 static int enable_endpoint(struct fsg_dev
*fsg
, struct usb_ep
*ep
,
3204 const struct usb_endpoint_descriptor
*d
)
3208 ep
->driver_data
= fsg
;
3209 rc
= usb_ep_enable(ep
, d
);
3211 ERROR(fsg
, "can't enable %s, result %d\n", ep
->name
, rc
);
3215 static int alloc_request(struct fsg_dev
*fsg
, struct usb_ep
*ep
,
3216 struct usb_request
**preq
)
3218 *preq
= usb_ep_alloc_request(ep
, GFP_ATOMIC
);
3221 ERROR(fsg
, "can't allocate request for %s\n", ep
->name
);
3226 * Reset interface setting and re-init endpoint state (toggle etc).
3227 * Call with altsetting < 0 to disable the interface. The only other
3228 * available altsetting is 0, which enables the interface.
3230 static int do_set_interface(struct fsg_dev
*fsg
, int altsetting
)
3234 const struct usb_endpoint_descriptor
*d
;
3237 DBG(fsg
, "reset interface\n");
3240 /* Deallocate the requests */
3241 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
3242 struct fsg_buffhd
*bh
= &fsg
->buffhds
[i
];
3245 usb_ep_free_request(fsg
->bulk_in
, bh
->inreq
);
3249 usb_ep_free_request(fsg
->bulk_out
, bh
->outreq
);
3254 usb_ep_free_request(fsg
->intr_in
, fsg
->intreq
);
3258 /* Disable the endpoints */
3259 if (fsg
->bulk_in_enabled
) {
3260 usb_ep_disable(fsg
->bulk_in
);
3261 fsg
->bulk_in_enabled
= 0;
3263 if (fsg
->bulk_out_enabled
) {
3264 usb_ep_disable(fsg
->bulk_out
);
3265 fsg
->bulk_out_enabled
= 0;
3267 if (fsg
->intr_in_enabled
) {
3268 usb_ep_disable(fsg
->intr_in
);
3269 fsg
->intr_in_enabled
= 0;
3273 if (altsetting
< 0 || rc
!= 0)
3276 DBG(fsg
, "set interface %d\n", altsetting
);
3278 /* Enable the endpoints */
3279 d
= ep_desc(fsg
->gadget
, &fs_bulk_in_desc
, &hs_bulk_in_desc
);
3280 if ((rc
= enable_endpoint(fsg
, fsg
->bulk_in
, d
)) != 0)
3282 fsg
->bulk_in_enabled
= 1;
3284 d
= ep_desc(fsg
->gadget
, &fs_bulk_out_desc
, &hs_bulk_out_desc
);
3285 if ((rc
= enable_endpoint(fsg
, fsg
->bulk_out
, d
)) != 0)
3287 fsg
->bulk_out_enabled
= 1;
3288 fsg
->bulk_out_maxpacket
= le16_to_cpu(d
->wMaxPacketSize
);
3289 clear_bit(IGNORE_BULK_OUT
, &fsg
->atomic_bitflags
);
3291 if (transport_is_cbi()) {
3292 d
= ep_desc(fsg
->gadget
, &fs_intr_in_desc
, &hs_intr_in_desc
);
3293 if ((rc
= enable_endpoint(fsg
, fsg
->intr_in
, d
)) != 0)
3295 fsg
->intr_in_enabled
= 1;
3298 /* Allocate the requests */
3299 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
3300 struct fsg_buffhd
*bh
= &fsg
->buffhds
[i
];
3302 if ((rc
= alloc_request(fsg
, fsg
->bulk_in
, &bh
->inreq
)) != 0)
3304 if ((rc
= alloc_request(fsg
, fsg
->bulk_out
, &bh
->outreq
)) != 0)
3306 bh
->inreq
->buf
= bh
->outreq
->buf
= bh
->buf
;
3307 bh
->inreq
->context
= bh
->outreq
->context
= bh
;
3308 bh
->inreq
->complete
= bulk_in_complete
;
3309 bh
->outreq
->complete
= bulk_out_complete
;
3311 if (transport_is_cbi()) {
3312 if ((rc
= alloc_request(fsg
, fsg
->intr_in
, &fsg
->intreq
)) != 0)
3314 fsg
->intreq
->complete
= intr_in_complete
;
3318 for (i
= 0; i
< fsg
->nluns
; ++i
)
3319 fsg
->luns
[i
].unit_attention_data
= SS_RESET_OCCURRED
;
3325 * Change our operational configuration. This code must agree with the code
3326 * that returns config descriptors, and with interface altsetting code.
3328 * It's also responsible for power management interactions. Some
3329 * configurations might not work with our current power sources.
3330 * For now we just assume the gadget is always self-powered.
3332 static int do_set_config(struct fsg_dev
*fsg
, u8 new_config
)
3336 /* Disable the single interface */
3337 if (fsg
->config
!= 0) {
3338 DBG(fsg
, "reset config\n");
3340 rc
= do_set_interface(fsg
, -1);
3343 /* Enable the interface */
3344 if (new_config
!= 0) {
3345 fsg
->config
= new_config
;
3346 if ((rc
= do_set_interface(fsg
, 0)) != 0)
3347 fsg
->config
= 0; // Reset on errors
3351 switch (fsg
->gadget
->speed
) {
3352 case USB_SPEED_LOW
: speed
= "low"; break;
3353 case USB_SPEED_FULL
: speed
= "full"; break;
3354 case USB_SPEED_HIGH
: speed
= "high"; break;
3355 default: speed
= "?"; break;
3357 INFO(fsg
, "%s speed config #%d\n", speed
, fsg
->config
);
3364 /*-------------------------------------------------------------------------*/
3366 static void handle_exception(struct fsg_dev
*fsg
)
3372 struct fsg_buffhd
*bh
;
3373 enum fsg_state old_state
;
3376 unsigned int exception_req_tag
;
3379 /* Clear the existing signals. Anything but SIGUSR1 is converted
3380 * into a high-priority EXIT exception. */
3382 sig
= dequeue_signal_lock(current
, ¤t
->blocked
, &info
);
3385 if (sig
!= SIGUSR1
) {
3386 if (fsg
->state
< FSG_STATE_EXIT
)
3387 DBG(fsg
, "Main thread exiting on signal\n");
3388 raise_exception(fsg
, FSG_STATE_EXIT
);
3392 /* Cancel all the pending transfers */
3393 if (fsg
->intreq_busy
)
3394 usb_ep_dequeue(fsg
->intr_in
, fsg
->intreq
);
3395 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
3396 bh
= &fsg
->buffhds
[i
];
3398 usb_ep_dequeue(fsg
->bulk_in
, bh
->inreq
);
3399 if (bh
->outreq_busy
)
3400 usb_ep_dequeue(fsg
->bulk_out
, bh
->outreq
);
3403 /* Wait until everything is idle */
3405 num_active
= fsg
->intreq_busy
;
3406 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
3407 bh
= &fsg
->buffhds
[i
];
3408 num_active
+= bh
->inreq_busy
+ bh
->outreq_busy
;
3410 if (num_active
== 0)
3412 if (sleep_thread(fsg
))
3416 /* Clear out the controller's fifos */
3417 if (fsg
->bulk_in_enabled
)
3418 usb_ep_fifo_flush(fsg
->bulk_in
);
3419 if (fsg
->bulk_out_enabled
)
3420 usb_ep_fifo_flush(fsg
->bulk_out
);
3421 if (fsg
->intr_in_enabled
)
3422 usb_ep_fifo_flush(fsg
->intr_in
);
3424 /* Reset the I/O buffer states and pointers, the SCSI
3425 * state, and the exception. Then invoke the handler. */
3426 spin_lock_irq(&fsg
->lock
);
3428 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
3429 bh
= &fsg
->buffhds
[i
];
3430 bh
->state
= BUF_STATE_EMPTY
;
3432 fsg
->next_buffhd_to_fill
= fsg
->next_buffhd_to_drain
=
3435 exception_req_tag
= fsg
->exception_req_tag
;
3436 new_config
= fsg
->new_config
;
3437 old_state
= fsg
->state
;
3439 if (old_state
== FSG_STATE_ABORT_BULK_OUT
)
3440 fsg
->state
= FSG_STATE_STATUS_PHASE
;
3442 for (i
= 0; i
< fsg
->nluns
; ++i
) {
3443 curlun
= &fsg
->luns
[i
];
3444 curlun
->prevent_medium_removal
= 0;
3445 curlun
->sense_data
= curlun
->unit_attention_data
=
3447 curlun
->sense_data_info
= 0;
3448 curlun
->info_valid
= 0;
3450 fsg
->state
= FSG_STATE_IDLE
;
3452 spin_unlock_irq(&fsg
->lock
);
3454 /* Carry out any extra actions required for the exception */
3455 switch (old_state
) {
3459 case FSG_STATE_ABORT_BULK_OUT
:
3461 spin_lock_irq(&fsg
->lock
);
3462 if (fsg
->state
== FSG_STATE_STATUS_PHASE
)
3463 fsg
->state
= FSG_STATE_IDLE
;
3464 spin_unlock_irq(&fsg
->lock
);
3467 case FSG_STATE_RESET
:
3468 /* In case we were forced against our will to halt a
3469 * bulk endpoint, clear the halt now. (The SuperH UDC
3470 * requires this.) */
3471 if (test_and_clear_bit(IGNORE_BULK_OUT
, &fsg
->atomic_bitflags
))
3472 usb_ep_clear_halt(fsg
->bulk_in
);
3474 if (transport_is_bbb()) {
3475 if (fsg
->ep0_req_tag
== exception_req_tag
)
3476 ep0_queue(fsg
); // Complete the status stage
3478 } else if (transport_is_cbi())
3479 send_status(fsg
); // Status by interrupt pipe
3481 /* Technically this should go here, but it would only be
3482 * a waste of time. Ditto for the INTERFACE_CHANGE and
3483 * CONFIG_CHANGE cases. */
3484 // for (i = 0; i < fsg->nluns; ++i)
3485 // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
3488 case FSG_STATE_INTERFACE_CHANGE
:
3489 rc
= do_set_interface(fsg
, 0);
3490 if (fsg
->ep0_req_tag
!= exception_req_tag
)
3492 if (rc
!= 0) // STALL on errors
3493 fsg_set_halt(fsg
, fsg
->ep0
);
3494 else // Complete the status stage
3498 case FSG_STATE_CONFIG_CHANGE
:
3499 rc
= do_set_config(fsg
, new_config
);
3500 if (fsg
->ep0_req_tag
!= exception_req_tag
)
3502 if (rc
!= 0) // STALL on errors
3503 fsg_set_halt(fsg
, fsg
->ep0
);
3504 else // Complete the status stage
3508 case FSG_STATE_DISCONNECT
:
3510 do_set_config(fsg
, 0); // Unconfigured state
3513 case FSG_STATE_EXIT
:
3514 case FSG_STATE_TERMINATED
:
3515 do_set_config(fsg
, 0); // Free resources
3516 spin_lock_irq(&fsg
->lock
);
3517 fsg
->state
= FSG_STATE_TERMINATED
; // Stop the thread
3518 spin_unlock_irq(&fsg
->lock
);
3524 /*-------------------------------------------------------------------------*/
3526 static int fsg_main_thread(void *fsg_
)
3528 struct fsg_dev
*fsg
= fsg_
;
3530 /* Allow the thread to be killed by a signal, but set the signal mask
3531 * to block everything but INT, TERM, KILL, and USR1. */
3532 allow_signal(SIGINT
);
3533 allow_signal(SIGTERM
);
3534 allow_signal(SIGKILL
);
3535 allow_signal(SIGUSR1
);
3537 /* Allow the thread to be frozen */
3540 /* Arrange for userspace references to be interpreted as kernel
3541 * pointers. That way we can pass a kernel pointer to a routine
3542 * that expects a __user pointer and it will work okay. */
3546 while (fsg
->state
!= FSG_STATE_TERMINATED
) {
3547 if (exception_in_progress(fsg
) || signal_pending(current
)) {
3548 handle_exception(fsg
);
3552 if (!fsg
->running
) {
3557 if (get_next_command(fsg
))
3560 spin_lock_irq(&fsg
->lock
);
3561 if (!exception_in_progress(fsg
))
3562 fsg
->state
= FSG_STATE_DATA_PHASE
;
3563 spin_unlock_irq(&fsg
->lock
);
3565 if (do_scsi_command(fsg
) || finish_reply(fsg
))
3568 spin_lock_irq(&fsg
->lock
);
3569 if (!exception_in_progress(fsg
))
3570 fsg
->state
= FSG_STATE_STATUS_PHASE
;
3571 spin_unlock_irq(&fsg
->lock
);
3573 if (send_status(fsg
))
3576 spin_lock_irq(&fsg
->lock
);
3577 if (!exception_in_progress(fsg
))
3578 fsg
->state
= FSG_STATE_IDLE
;
3579 spin_unlock_irq(&fsg
->lock
);
3582 spin_lock_irq(&fsg
->lock
);
3583 fsg
->thread_task
= NULL
;
3584 spin_unlock_irq(&fsg
->lock
);
3586 /* If we are exiting because of a signal, unregister the
3588 if (test_and_clear_bit(REGISTERED
, &fsg
->atomic_bitflags
))
3589 usb_gadget_unregister_driver(&fsg_driver
);
3591 /* Let the unbind and cleanup routines know the thread has exited */
3592 complete_and_exit(&fsg
->thread_notifier
, 0);
3596 /*-------------------------------------------------------------------------*/
3598 /* If the next two routines are called while the gadget is registered,
3599 * the caller must own fsg->filesem for writing. */
3601 static int open_backing_file(struct lun
*curlun
, const char *filename
)
3604 struct file
*filp
= NULL
;
3606 struct inode
*inode
= NULL
;
3611 /* R/W if we can, R/O if we must */
3614 filp
= filp_open(filename
, O_RDWR
| O_LARGEFILE
, 0);
3615 if (-EROFS
== PTR_ERR(filp
))
3619 filp
= filp_open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
3621 LINFO(curlun
, "unable to open backing file: %s\n", filename
);
3622 return PTR_ERR(filp
);
3625 if (!(filp
->f_mode
& FMODE_WRITE
))
3628 if (filp
->f_path
.dentry
)
3629 inode
= filp
->f_path
.dentry
->d_inode
;
3630 if (inode
&& S_ISBLK(inode
->i_mode
)) {
3631 if (bdev_read_only(inode
->i_bdev
))
3633 } else if (!inode
|| !S_ISREG(inode
->i_mode
)) {
3634 LINFO(curlun
, "invalid file type: %s\n", filename
);
3638 /* If we can't read the file, it's no good.
3639 * If we can't write the file, use it read-only. */
3640 if (!filp
->f_op
|| !(filp
->f_op
->read
|| filp
->f_op
->aio_read
)) {
3641 LINFO(curlun
, "file not readable: %s\n", filename
);
3644 if (!(filp
->f_op
->write
|| filp
->f_op
->aio_write
))
3647 size
= i_size_read(inode
->i_mapping
->host
);
3649 LINFO(curlun
, "unable to find file size: %s\n", filename
);
3653 num_sectors
= size
>> 9; // File size in 512-byte blocks
3655 if (mod_data
.cdrom
) {
3656 num_sectors
&= ~3; // Reduce to a multiple of 2048
3657 min_sectors
= 300*4; // Smallest track is 300 frames
3658 if (num_sectors
>= 256*60*75*4) {
3659 num_sectors
= (256*60*75 - 1) * 4;
3660 LINFO(curlun
, "file too big: %s\n", filename
);
3661 LINFO(curlun
, "using only first %d blocks\n",
3665 if (num_sectors
< min_sectors
) {
3666 LINFO(curlun
, "file too small: %s\n", filename
);
3673 curlun
->filp
= filp
;
3674 curlun
->file_length
= size
;
3675 curlun
->num_sectors
= num_sectors
;
3676 LDBG(curlun
, "open backing file: %s\n", filename
);
3680 filp_close(filp
, current
->files
);
3685 static void close_backing_file(struct lun
*curlun
)
3688 LDBG(curlun
, "close backing file\n");
3690 curlun
->filp
= NULL
;
3695 static ssize_t
show_ro(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
3697 struct lun
*curlun
= dev_to_lun(dev
);
3699 return sprintf(buf
, "%d\n", curlun
->ro
);
3702 static ssize_t
show_file(struct device
*dev
, struct device_attribute
*attr
,
3705 struct lun
*curlun
= dev_to_lun(dev
);
3706 struct fsg_dev
*fsg
= dev_get_drvdata(dev
);
3710 down_read(&fsg
->filesem
);
3711 if (backing_file_is_open(curlun
)) { // Get the complete pathname
3712 p
= d_path(&curlun
->filp
->f_path
, buf
, PAGE_SIZE
- 1);
3717 memmove(buf
, p
, rc
);
3718 buf
[rc
] = '\n'; // Add a newline
3721 } else { // No file, return 0 bytes
3725 up_read(&fsg
->filesem
);
3730 static ssize_t
store_ro(struct device
*dev
, struct device_attribute
*attr
,
3731 const char *buf
, size_t count
)
3734 struct lun
*curlun
= dev_to_lun(dev
);
3735 struct fsg_dev
*fsg
= dev_get_drvdata(dev
);
3738 if (sscanf(buf
, "%d", &i
) != 1)
3741 /* Allow the write-enable status to change only while the backing file
3743 down_read(&fsg
->filesem
);
3744 if (backing_file_is_open(curlun
)) {
3745 LDBG(curlun
, "read-only status change prevented\n");
3749 LDBG(curlun
, "read-only status set to %d\n", curlun
->ro
);
3751 up_read(&fsg
->filesem
);
3755 static ssize_t
store_file(struct device
*dev
, struct device_attribute
*attr
,
3756 const char *buf
, size_t count
)
3758 struct lun
*curlun
= dev_to_lun(dev
);
3759 struct fsg_dev
*fsg
= dev_get_drvdata(dev
);
3762 if (curlun
->prevent_medium_removal
&& backing_file_is_open(curlun
)) {
3763 LDBG(curlun
, "eject attempt prevented\n");
3764 return -EBUSY
; // "Door is locked"
3767 /* Remove a trailing newline */
3768 if (count
> 0 && buf
[count
-1] == '\n')
3769 ((char *) buf
)[count
-1] = 0; // Ugh!
3771 /* Eject current medium */
3772 down_write(&fsg
->filesem
);
3773 if (backing_file_is_open(curlun
)) {
3774 close_backing_file(curlun
);
3775 curlun
->unit_attention_data
= SS_MEDIUM_NOT_PRESENT
;
3778 /* Load new medium */
3779 if (count
> 0 && buf
[0]) {
3780 rc
= open_backing_file(curlun
, buf
);
3782 curlun
->unit_attention_data
=
3783 SS_NOT_READY_TO_READY_TRANSITION
;
3785 up_write(&fsg
->filesem
);
3786 return (rc
< 0 ? rc
: count
);
3790 /* The write permissions and store_xxx pointers are set in fsg_bind() */
3791 static DEVICE_ATTR(ro
, 0444, show_ro
, NULL
);
3792 static DEVICE_ATTR(file
, 0444, show_file
, NULL
);
3795 /*-------------------------------------------------------------------------*/
3797 static void fsg_release(struct kref
*ref
)
3799 struct fsg_dev
*fsg
= container_of(ref
, struct fsg_dev
, ref
);
3805 static void lun_release(struct device
*dev
)
3807 struct fsg_dev
*fsg
= dev_get_drvdata(dev
);
3809 kref_put(&fsg
->ref
, fsg_release
);
3812 static void /* __init_or_exit */ fsg_unbind(struct usb_gadget
*gadget
)
3814 struct fsg_dev
*fsg
= get_gadget_data(gadget
);
3817 struct usb_request
*req
= fsg
->ep0req
;
3819 DBG(fsg
, "unbind\n");
3820 clear_bit(REGISTERED
, &fsg
->atomic_bitflags
);
3822 /* Unregister the sysfs attribute files and the LUNs */
3823 for (i
= 0; i
< fsg
->nluns
; ++i
) {
3824 curlun
= &fsg
->luns
[i
];
3825 if (curlun
->registered
) {
3826 device_remove_file(&curlun
->dev
, &dev_attr_ro
);
3827 device_remove_file(&curlun
->dev
, &dev_attr_file
);
3828 close_backing_file(curlun
);
3829 device_unregister(&curlun
->dev
);
3830 curlun
->registered
= 0;
3834 /* If the thread isn't already dead, tell it to exit now */
3835 if (fsg
->state
!= FSG_STATE_TERMINATED
) {
3836 raise_exception(fsg
, FSG_STATE_EXIT
);
3837 wait_for_completion(&fsg
->thread_notifier
);
3839 /* The cleanup routine waits for this completion also */
3840 complete(&fsg
->thread_notifier
);
3843 /* Free the data buffers */
3844 for (i
= 0; i
< NUM_BUFFERS
; ++i
)
3845 kfree(fsg
->buffhds
[i
].buf
);
3847 /* Free the request and buffer for endpoint 0 */
3850 usb_ep_free_request(fsg
->ep0
, req
);
3853 set_gadget_data(gadget
, NULL
);
3857 static int __init
check_parameters(struct fsg_dev
*fsg
)
3862 /* Store the default values */
3863 mod_data
.transport_type
= USB_PR_BULK
;
3864 mod_data
.transport_name
= "Bulk-only";
3865 mod_data
.protocol_type
= USB_SC_SCSI
;
3866 mod_data
.protocol_name
= "Transparent SCSI";
3868 /* Some peripheral controllers are known not to be able to
3869 * halt bulk endpoints correctly. If one of them is present,
3872 if (gadget_is_sh(fsg
->gadget
) || gadget_is_at91(fsg
->gadget
))
3873 mod_data
.can_stall
= 0;
3875 if (mod_data
.release
== 0xffff) { // Parameter wasn't set
3876 /* The sa1100 controller is not supported */
3877 if (gadget_is_sa1100(fsg
->gadget
))
3880 gcnum
= usb_gadget_controller_number(fsg
->gadget
);
3882 mod_data
.release
= 0x0300 + gcnum
;
3884 WARNING(fsg
, "controller '%s' not recognized\n",
3886 mod_data
.release
= 0x0399;
3890 prot
= simple_strtol(mod_data
.protocol_parm
, NULL
, 0);
3892 #ifdef CONFIG_USB_FILE_STORAGE_TEST
3893 if (strnicmp(mod_data
.transport_parm
, "BBB", 10) == 0) {
3894 ; // Use default setting
3895 } else if (strnicmp(mod_data
.transport_parm
, "CB", 10) == 0) {
3896 mod_data
.transport_type
= USB_PR_CB
;
3897 mod_data
.transport_name
= "Control-Bulk";
3898 } else if (strnicmp(mod_data
.transport_parm
, "CBI", 10) == 0) {
3899 mod_data
.transport_type
= USB_PR_CBI
;
3900 mod_data
.transport_name
= "Control-Bulk-Interrupt";
3902 ERROR(fsg
, "invalid transport: %s\n", mod_data
.transport_parm
);
3906 if (strnicmp(mod_data
.protocol_parm
, "SCSI", 10) == 0 ||
3907 prot
== USB_SC_SCSI
) {
3908 ; // Use default setting
3909 } else if (strnicmp(mod_data
.protocol_parm
, "RBC", 10) == 0 ||
3910 prot
== USB_SC_RBC
) {
3911 mod_data
.protocol_type
= USB_SC_RBC
;
3912 mod_data
.protocol_name
= "RBC";
3913 } else if (strnicmp(mod_data
.protocol_parm
, "8020", 4) == 0 ||
3914 strnicmp(mod_data
.protocol_parm
, "ATAPI", 10) == 0 ||
3915 prot
== USB_SC_8020
) {
3916 mod_data
.protocol_type
= USB_SC_8020
;
3917 mod_data
.protocol_name
= "8020i (ATAPI)";
3918 } else if (strnicmp(mod_data
.protocol_parm
, "QIC", 3) == 0 ||
3919 prot
== USB_SC_QIC
) {
3920 mod_data
.protocol_type
= USB_SC_QIC
;
3921 mod_data
.protocol_name
= "QIC-157";
3922 } else if (strnicmp(mod_data
.protocol_parm
, "UFI", 10) == 0 ||
3923 prot
== USB_SC_UFI
) {
3924 mod_data
.protocol_type
= USB_SC_UFI
;
3925 mod_data
.protocol_name
= "UFI";
3926 } else if (strnicmp(mod_data
.protocol_parm
, "8070", 4) == 0 ||
3927 prot
== USB_SC_8070
) {
3928 mod_data
.protocol_type
= USB_SC_8070
;
3929 mod_data
.protocol_name
= "8070i";
3931 ERROR(fsg
, "invalid protocol: %s\n", mod_data
.protocol_parm
);
3935 mod_data
.buflen
&= PAGE_CACHE_MASK
;
3936 if (mod_data
.buflen
<= 0) {
3937 ERROR(fsg
, "invalid buflen\n");
3940 #endif /* CONFIG_USB_FILE_STORAGE_TEST */
3946 static int __init
fsg_bind(struct usb_gadget
*gadget
)
3948 struct fsg_dev
*fsg
= the_fsg
;
3953 struct usb_request
*req
;
3956 fsg
->gadget
= gadget
;
3957 set_gadget_data(gadget
, fsg
);
3958 fsg
->ep0
= gadget
->ep0
;
3959 fsg
->ep0
->driver_data
= fsg
;
3961 if ((rc
= check_parameters(fsg
)) != 0)
3964 if (mod_data
.removable
) { // Enable the store_xxx attributes
3965 dev_attr_file
.attr
.mode
= 0644;
3966 dev_attr_file
.store
= store_file
;
3967 if (!mod_data
.cdrom
) {
3968 dev_attr_ro
.attr
.mode
= 0644;
3969 dev_attr_ro
.store
= store_ro
;
3973 /* Find out how many LUNs there should be */
3976 i
= max(mod_data
.num_filenames
, 1u);
3978 ERROR(fsg
, "invalid number of LUNs: %d\n", i
);
3983 /* Create the LUNs, open their backing files, and register the
3984 * LUN devices in sysfs. */
3985 fsg
->luns
= kzalloc(i
* sizeof(struct lun
), GFP_KERNEL
);
3992 for (i
= 0; i
< fsg
->nluns
; ++i
) {
3993 curlun
= &fsg
->luns
[i
];
3994 curlun
->ro
= mod_data
.ro
[i
];
3997 curlun
->dev
.release
= lun_release
;
3998 curlun
->dev
.parent
= &gadget
->dev
;
3999 curlun
->dev
.driver
= &fsg_driver
.driver
;
4000 dev_set_drvdata(&curlun
->dev
, fsg
);
4001 dev_set_name(&curlun
->dev
,"%s-lun%d",
4002 dev_name(&gadget
->dev
), i
);
4004 if ((rc
= device_register(&curlun
->dev
)) != 0) {
4005 INFO(fsg
, "failed to register LUN%d: %d\n", i
, rc
);
4008 if ((rc
= device_create_file(&curlun
->dev
,
4009 &dev_attr_ro
)) != 0 ||
4010 (rc
= device_create_file(&curlun
->dev
,
4011 &dev_attr_file
)) != 0) {
4012 device_unregister(&curlun
->dev
);
4015 curlun
->registered
= 1;
4016 kref_get(&fsg
->ref
);
4018 if (mod_data
.file
[i
] && *mod_data
.file
[i
]) {
4019 if ((rc
= open_backing_file(curlun
,
4020 mod_data
.file
[i
])) != 0)
4022 } else if (!mod_data
.removable
) {
4023 ERROR(fsg
, "no file given for LUN%d\n", i
);
4029 /* Find all the endpoints we will use */
4030 usb_ep_autoconfig_reset(gadget
);
4031 ep
= usb_ep_autoconfig(gadget
, &fs_bulk_in_desc
);
4034 ep
->driver_data
= fsg
; // claim the endpoint
4037 ep
= usb_ep_autoconfig(gadget
, &fs_bulk_out_desc
);
4040 ep
->driver_data
= fsg
; // claim the endpoint
4043 if (transport_is_cbi()) {
4044 ep
= usb_ep_autoconfig(gadget
, &fs_intr_in_desc
);
4047 ep
->driver_data
= fsg
; // claim the endpoint
4051 /* Fix up the descriptors */
4052 device_desc
.bMaxPacketSize0
= fsg
->ep0
->maxpacket
;
4053 device_desc
.idVendor
= cpu_to_le16(mod_data
.vendor
);
4054 device_desc
.idProduct
= cpu_to_le16(mod_data
.product
);
4055 device_desc
.bcdDevice
= cpu_to_le16(mod_data
.release
);
4057 i
= (transport_is_cbi() ? 3 : 2); // Number of endpoints
4058 intf_desc
.bNumEndpoints
= i
;
4059 intf_desc
.bInterfaceSubClass
= mod_data
.protocol_type
;
4060 intf_desc
.bInterfaceProtocol
= mod_data
.transport_type
;
4061 fs_function
[i
+ FS_FUNCTION_PRE_EP_ENTRIES
] = NULL
;
4063 if (gadget_is_dualspeed(gadget
)) {
4064 hs_function
[i
+ HS_FUNCTION_PRE_EP_ENTRIES
] = NULL
;
4066 /* Assume ep0 uses the same maxpacket value for both speeds */
4067 dev_qualifier
.bMaxPacketSize0
= fsg
->ep0
->maxpacket
;
4069 /* Assume endpoint addresses are the same for both speeds */
4070 hs_bulk_in_desc
.bEndpointAddress
=
4071 fs_bulk_in_desc
.bEndpointAddress
;
4072 hs_bulk_out_desc
.bEndpointAddress
=
4073 fs_bulk_out_desc
.bEndpointAddress
;
4074 hs_intr_in_desc
.bEndpointAddress
=
4075 fs_intr_in_desc
.bEndpointAddress
;
4078 if (gadget_is_otg(gadget
))
4079 otg_desc
.bmAttributes
|= USB_OTG_HNP
;
4083 /* Allocate the request and buffer for endpoint 0 */
4084 fsg
->ep0req
= req
= usb_ep_alloc_request(fsg
->ep0
, GFP_KERNEL
);
4087 req
->buf
= kmalloc(EP0_BUFSIZE
, GFP_KERNEL
);
4090 req
->complete
= ep0_complete
;
4092 /* Allocate the data buffers */
4093 for (i
= 0; i
< NUM_BUFFERS
; ++i
) {
4094 struct fsg_buffhd
*bh
= &fsg
->buffhds
[i
];
4096 /* Allocate for the bulk-in endpoint. We assume that
4097 * the buffer will also work with the bulk-out (and
4098 * interrupt-in) endpoint. */
4099 bh
->buf
= kmalloc(mod_data
.buflen
, GFP_KERNEL
);
4104 fsg
->buffhds
[NUM_BUFFERS
- 1].next
= &fsg
->buffhds
[0];
4106 /* This should reflect the actual gadget power source */
4107 usb_gadget_set_selfpowered(gadget
);
4109 snprintf(manufacturer
, sizeof manufacturer
, "%s %s with %s",
4110 init_utsname()->sysname
, init_utsname()->release
,
4113 /* On a real device, serial[] would be loaded from permanent
4114 * storage. We just encode it from the driver version string. */
4115 for (i
= 0; i
< sizeof(serial
) - 2; i
+= 2) {
4116 unsigned char c
= DRIVER_VERSION
[i
/ 2];
4120 sprintf(&serial
[i
], "%02X", c
);
4123 fsg
->thread_task
= kthread_create(fsg_main_thread
, fsg
,
4124 "file-storage-gadget");
4125 if (IS_ERR(fsg
->thread_task
)) {
4126 rc
= PTR_ERR(fsg
->thread_task
);
4130 INFO(fsg
, DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
4131 INFO(fsg
, "Number of LUNs=%d\n", fsg
->nluns
);
4133 pathbuf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
4134 for (i
= 0; i
< fsg
->nluns
; ++i
) {
4135 curlun
= &fsg
->luns
[i
];
4136 if (backing_file_is_open(curlun
)) {
4139 p
= d_path(&curlun
->filp
->f_path
,
4144 LINFO(curlun
, "ro=%d, file: %s\n",
4145 curlun
->ro
, (p
? p
: "(error)"));
4150 DBG(fsg
, "transport=%s (x%02x)\n",
4151 mod_data
.transport_name
, mod_data
.transport_type
);
4152 DBG(fsg
, "protocol=%s (x%02x)\n",
4153 mod_data
.protocol_name
, mod_data
.protocol_type
);
4154 DBG(fsg
, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
4155 mod_data
.vendor
, mod_data
.product
, mod_data
.release
);
4156 DBG(fsg
, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
4157 mod_data
.removable
, mod_data
.can_stall
,
4158 mod_data
.cdrom
, mod_data
.buflen
);
4159 DBG(fsg
, "I/O thread pid: %d\n", task_pid_nr(fsg
->thread_task
));
4161 set_bit(REGISTERED
, &fsg
->atomic_bitflags
);
4163 /* Tell the thread to start working */
4164 wake_up_process(fsg
->thread_task
);
4168 ERROR(fsg
, "unable to autoconfigure all endpoints\n");
4172 fsg
->state
= FSG_STATE_TERMINATED
; // The thread is dead
4174 complete(&fsg
->thread_notifier
);
4179 /*-------------------------------------------------------------------------*/
4181 static void fsg_suspend(struct usb_gadget
*gadget
)
4183 struct fsg_dev
*fsg
= get_gadget_data(gadget
);
4185 DBG(fsg
, "suspend\n");
4186 set_bit(SUSPENDED
, &fsg
->atomic_bitflags
);
4189 static void fsg_resume(struct usb_gadget
*gadget
)
4191 struct fsg_dev
*fsg
= get_gadget_data(gadget
);
4193 DBG(fsg
, "resume\n");
4194 clear_bit(SUSPENDED
, &fsg
->atomic_bitflags
);
4198 /*-------------------------------------------------------------------------*/
4200 static struct usb_gadget_driver fsg_driver
= {
4201 #ifdef CONFIG_USB_GADGET_DUALSPEED
4202 .speed
= USB_SPEED_HIGH
,
4204 .speed
= USB_SPEED_FULL
,
4206 .function
= (char *) longname
,
4208 .unbind
= fsg_unbind
,
4209 .disconnect
= fsg_disconnect
,
4211 .suspend
= fsg_suspend
,
4212 .resume
= fsg_resume
,
4215 .name
= (char *) shortname
,
4216 .owner
= THIS_MODULE
,
4224 static int __init
fsg_alloc(void)
4226 struct fsg_dev
*fsg
;
4228 fsg
= kzalloc(sizeof *fsg
, GFP_KERNEL
);
4231 spin_lock_init(&fsg
->lock
);
4232 init_rwsem(&fsg
->filesem
);
4233 kref_init(&fsg
->ref
);
4234 init_completion(&fsg
->thread_notifier
);
4241 static int __init
fsg_init(void)
4244 struct fsg_dev
*fsg
;
4246 if ((rc
= fsg_alloc()) != 0)
4249 if ((rc
= usb_gadget_register_driver(&fsg_driver
)) != 0)
4250 kref_put(&fsg
->ref
, fsg_release
);
4253 module_init(fsg_init
);
4256 static void __exit
fsg_cleanup(void)
4258 struct fsg_dev
*fsg
= the_fsg
;
4260 /* Unregister the driver iff the thread hasn't already done so */
4261 if (test_and_clear_bit(REGISTERED
, &fsg
->atomic_bitflags
))
4262 usb_gadget_unregister_driver(&fsg_driver
);
4264 /* Wait for the thread to finish up */
4265 wait_for_completion(&fsg
->thread_notifier
);
4267 kref_put(&fsg
->ref
, fsg_release
);
4269 module_exit(fsg_cleanup
);