blob: c6f96a2b31101e158bf97427cbdec45dac32b373 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * file_storage.c -- File-backed USB Storage Gadget, for USB development
3 *
Alan Stern12aae682008-11-20 14:13:12 -05004 * Copyright (C) 2003-2008 Alan Stern
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The names of the above-listed copyright holders may not be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * ALTERNATIVELY, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") as published by the Free Software
22 * Foundation, either version 2 of that License or (at your option) any
23 * later version.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39/*
40 * The File-backed Storage Gadget acts as a USB Mass Storage device,
Alan Stern12aae682008-11-20 14:13:12 -050041 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
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
Alan Stern12aae682008-11-20 14:13:12 -050049 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
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
Yann Cantin87eb1be2010-06-05 23:06:31 +020059 * Vendor ID, Product ID, release number and serial number can be overridden.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
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
Alan Stern12aae682008-11-20 14:13:12 -050068 * 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
Peiyu Li3f565a32011-08-17 22:52:59 -070072 * need be only one backing file per LUN.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 * Requirements are modest; only a bulk-in and a bulk-out endpoint are
75 * needed (an interrupt-out endpoint is also needed for CBI). The memory
76 * requirement amounts to two 16K buffers, size configurable by a parameter.
77 * Support is included for both full-speed and high-speed operation.
78 *
Alan Stern14cd5f82006-03-23 15:07:25 -050079 * Note that the driver is slightly non-portable in that it assumes a
80 * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
81 * interrupt-in endpoints. With most device controllers this isn't an
82 * issue, but there may be some with hardware restrictions that prevent
83 * a buffer from being used by more than one endpoint.
84 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Module options:
86 *
87 * file=filename[,filename...]
88 * Required if "removable" is not set, names of
89 * the files or block devices used for
90 * backing storage
Alan Sternd8087422010-09-03 11:15:41 -040091 * serial=HHHH... Required serial number (string of hex chars)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ro=b[,b...] Default false, booleans for read-only access
93 * removable Default false, boolean for removable media
94 * luns=N Default N = number of filenames, number of
95 * LUNs to support
Andy Shevchenkoa93917d2010-07-22 17:53:56 +030096 * nofua=b[,b...] Default false, booleans for ignore FUA flag
97 * in SCSI WRITE(10,12) commands
Alan Sternd794ac72005-04-18 12:43:25 -040098 * stall Default determined according to the type of
99 * USB device controller (usually true),
100 * boolean to permit the driver to halt
101 * bulk endpoints
Alan Stern12aae682008-11-20 14:13:12 -0500102 * cdrom Default false, boolean for whether to emulate
103 * a CD-ROM drive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
105 * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
106 * ATAPI, QIC, UFI, 8070, or SCSI;
107 * also 1 - 6)
108 * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
109 * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
110 * release=0xRRRR Override the USB release number (bcdDevice)
111 * buflen=N Default N=16384, buffer size used (will be
112 * rounded down to a multiple of
113 * PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
Alan Sternd8087422010-09-03 11:15:41 -0400115 * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300116 * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
117 * default values are used for everything else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
119 * The pathnames of the backing files and the ro settings are available in
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300120 * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
121 * the gadget's sysfs directory. If the "removable" option is set, writing to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * these files will simulate ejecting/loading the medium (writing an empty
123 * line means eject) and adjusting a write-enable tab. Changes to the ro
Alan Stern12aae682008-11-20 14:13:12 -0500124 * setting are not allowed when the medium is loaded or if CD-ROM emulation
125 * is being used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
127 * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
Alan Sternaafe5bd2006-03-31 11:46:43 -0500128 * The driver's SCSI command interface was based on the "Information
129 * technology - Small Computer System Interface - 2" document from
130 * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
131 * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
132 * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
133 * "Universal Serial Bus Mass Storage Class UFI Command Specification"
134 * document, Revision 1.0, December 14, 1998, available at
135 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
137
138
139/*
140 * Driver Design
141 *
142 * The FSG driver is fairly straightforward. There is a main kernel
143 * thread that handles most of the work. Interrupt routines field
144 * callbacks from the controller driver: bulk- and interrupt-request
145 * completion notifications, endpoint-0 events, and disconnect events.
146 * Completion events are passed to the main thread by wakeup calls. Many
147 * ep0 requests are handled at interrupt time, but SetInterface,
148 * SetConfiguration, and device reset requests are forwarded to the
149 * thread in the form of "exceptions" using SIGUSR1 signals (since they
150 * should interrupt any ongoing file I/O operations).
151 *
152 * The thread's main routine implements the standard command/data/status
153 * parts of a SCSI interaction. It and its subroutines are full of tests
154 * for pending signals/exceptions -- all this polling is necessary since
155 * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
156 * indication that the driver really wants to be running in userspace.)
157 * An important point is that so long as the thread is alive it keeps an
158 * open reference to the backing file. This will prevent unmounting
159 * the backing file's underlying filesystem and could cause problems
160 * during system shutdown, for example. To prevent such problems, the
161 * thread catches INT, TERM, and KILL signals and converts them into
162 * an EXIT exception.
163 *
164 * In normal operation the main thread is started during the gadget's
165 * fsg_bind() callback and stopped during fsg_unbind(). But it can also
166 * exit when it receives a signal, and there's no point leaving the
167 * gadget running when the thread is dead. So just before the thread
168 * exits, it deregisters the gadget driver. This makes things a little
169 * tricky: The driver is deregistered at two places, and the exiting
170 * thread can indirectly call fsg_unbind() which in turn can tell the
171 * thread to exit. The first problem is resolved through the use of the
172 * REGISTERED atomic bitflag; the driver will only be deregistered once.
173 * The second problem is resolved by having fsg_unbind() check
174 * fsg->state; it won't try to stop the thread if the state is already
175 * FSG_STATE_TERMINATED.
176 *
177 * To provide maximum throughput, the driver uses a circular pipeline of
178 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
179 * arbitrarily long; in practice the benefits don't justify having more
180 * than 2 stages (i.e., double buffering). But it helps to think of the
181 * pipeline as being a long one. Each buffer head contains a bulk-in and
182 * a bulk-out request pointer (since the buffer can be used for both
183 * output and input -- directions always are given from the host's
184 * point of view) as well as a pointer to the buffer and various state
185 * variables.
186 *
187 * Use of the pipeline follows a simple protocol. There is a variable
188 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
189 * At any time that buffer head may still be in use from an earlier
190 * request, so each buffer head has a state variable indicating whether
191 * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
192 * buffer head to be EMPTY, filling the buffer either by file I/O or by
193 * USB I/O (during which the buffer head is BUSY), and marking the buffer
194 * head FULL when the I/O is complete. Then the buffer will be emptied
195 * (again possibly by USB I/O, during which it is marked BUSY) and
196 * finally marked EMPTY again (possibly by a completion routine).
197 *
198 * A module parameter tells the driver to avoid stalling the bulk
199 * endpoints wherever the transport specification allows. This is
200 * necessary for some UDCs like the SuperH, which cannot reliably clear a
201 * halt on a bulk endpoint. However, under certain circumstances the
202 * Bulk-only specification requires a stall. In such cases the driver
203 * will halt the endpoint and set a flag indicating that it should clear
204 * the halt in software during the next device reset. Hopefully this
205 * will permit everything to work correctly. Furthermore, although the
206 * specification allows the bulk-out endpoint to halt when the host sends
207 * too much data, implementing this would cause an unavoidable race.
208 * The driver will always use the "no-stall" approach for OUT transfers.
209 *
210 * One subtle point concerns sending status-stage responses for ep0
211 * requests. Some of these requests, such as device reset, can involve
212 * interrupting an ongoing file I/O operation, which might take an
213 * arbitrarily long time. During that delay the host might give up on
214 * the original ep0 request and issue a new one. When that happens the
215 * driver should not notify the host about completion of the original
216 * request, as the host will no longer be waiting for it. So the driver
217 * assigns to each ep0 request a unique tag, and it keeps track of the
218 * tag value of the request associated with a long-running exception
219 * (device-reset, interface-change, or configuration-change). When the
220 * exception handler is finished, the status-stage response is submitted
221 * only if the current ep0 request tag is equal to the exception request
222 * tag. Thus only the most recently received ep0 request will get a
223 * status-stage response.
224 *
225 * Warning: This driver source file is too long. It ought to be split up
226 * into a header file plus about 3 separate .c files, to handle the details
227 * of the Gadget, USB Mass Storage, and SCSI protocols.
228 */
229
230
David Brownell2e806f62007-08-02 00:03:39 -0700231/* #define VERBOSE_DEBUG */
Alan Stern79a7d9e2007-08-08 17:10:11 -0400232/* #define DUMP_MSGS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#include <linux/completion.h>
237#include <linux/dcache.h>
238#include <linux/delay.h>
239#include <linux/device.h>
240#include <linux/fcntl.h>
241#include <linux/file.h>
242#include <linux/fs.h>
Alan Stern87c42522005-11-09 16:59:56 -0500243#include <linux/kref.h>
Alan Stern22efcf42005-09-26 16:12:02 -0400244#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#include <linux/limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#include <linux/slab.h>
248#include <linux/spinlock.h>
249#include <linux/string.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800250#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David Brownell5f848132006-12-16 15:34:53 -0800253#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -0700254#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256#include "gadget_chips.h"
257
258
David Brownell352e2b92008-08-18 17:43:25 -0700259
260/*
261 * Kbuild is not very cooperative with respect to linking separately
262 * compiled library objects into one module. So for now we won't use
263 * separate compilation ... ensuring init/exit sections work to shrink
264 * the runtime footprint, and giving us at least some parts of what
265 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
266 */
267#include "usbstring.c"
268#include "config.c"
269#include "epautoconf.c"
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/*-------------------------------------------------------------------------*/
272
273#define DRIVER_DESC "File-backed Storage Gadget"
274#define DRIVER_NAME "g_file_storage"
Alan Sternd8087422010-09-03 11:15:41 -0400275#define DRIVER_VERSION "1 September 2010"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100277static char fsg_string_manufacturer[64];
278static const char fsg_string_product[] = DRIVER_DESC;
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100279static const char fsg_string_config[] = "Self-powered";
280static const char fsg_string_interface[] = "Mass Storage";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100282
283#include "storage_common.c"
284
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286MODULE_DESCRIPTION(DRIVER_DESC);
287MODULE_AUTHOR("Alan Stern");
288MODULE_LICENSE("Dual BSD/GPL");
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*
291 * This driver assumes self-powered hardware and has no way for users to
292 * trigger remote wakeup. It uses autoconfiguration to select endpoints
293 * and endpoint addresses.
294 */
295
296
297/*-------------------------------------------------------------------------*/
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/* Encapsulate the module parameter settings */
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static struct {
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100303 char *file[FSG_MAX_LUNS];
Alan Sternd8087422010-09-03 11:15:41 -0400304 char *serial;
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100305 int ro[FSG_MAX_LUNS];
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300306 int nofua[FSG_MAX_LUNS];
David Brownell2e806f62007-08-02 00:03:39 -0700307 unsigned int num_filenames;
308 unsigned int num_ros;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300309 unsigned int num_nofuas;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unsigned int nluns;
311
Alan Sternd794ac72005-04-18 12:43:25 -0400312 int removable;
313 int can_stall;
Alan Stern12aae682008-11-20 14:13:12 -0500314 int cdrom;
Alan Sternd794ac72005-04-18 12:43:25 -0400315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 char *transport_parm;
317 char *protocol_parm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned short vendor;
319 unsigned short product;
320 unsigned short release;
321 unsigned int buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 int transport_type;
324 char *transport_name;
325 int protocol_type;
326 char *protocol_name;
327
328} mod_data = { // Default values
329 .transport_parm = "BBB",
330 .protocol_parm = "SCSI",
331 .removable = 0,
Alan Sternd794ac72005-04-18 12:43:25 -0400332 .can_stall = 1,
Alan Stern12aae682008-11-20 14:13:12 -0500333 .cdrom = 0,
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100334 .vendor = FSG_VENDOR_ID,
335 .product = FSG_PRODUCT_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .release = 0xffff, // Use controller chip type
337 .buflen = 16384,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 };
339
340
Alan Sternaafe5bd2006-03-31 11:46:43 -0500341module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
342 S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343MODULE_PARM_DESC(file, "names of backing files or devices");
344
Alan Sternd8087422010-09-03 11:15:41 -0400345module_param_named(serial, mod_data.serial, charp, S_IRUGO);
346MODULE_PARM_DESC(serial, "USB serial number");
347
Alan Sternaafe5bd2006-03-31 11:46:43 -0500348module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349MODULE_PARM_DESC(ro, "true to force read-only");
350
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300351module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
352 S_IRUGO);
353MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
356MODULE_PARM_DESC(luns, "number of LUNs");
357
358module_param_named(removable, mod_data.removable, bool, S_IRUGO);
359MODULE_PARM_DESC(removable, "true to simulate removable media");
360
Alan Sternd794ac72005-04-18 12:43:25 -0400361module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
362MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
363
Alan Stern12aae682008-11-20 14:13:12 -0500364module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
365MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/* In the non-TEST version, only the module parameters listed above
368 * are available. */
369#ifdef CONFIG_USB_FILE_STORAGE_TEST
370
371module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
372MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
373
374module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
375MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
376 "8070, or SCSI)");
377
378module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
379MODULE_PARM_DESC(vendor, "USB Vendor ID");
380
381module_param_named(product, mod_data.product, ushort, S_IRUGO);
382MODULE_PARM_DESC(product, "USB Product ID");
383
384module_param_named(release, mod_data.release, ushort, S_IRUGO);
385MODULE_PARM_DESC(release, "USB release number");
386
387module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
388MODULE_PARM_DESC(buflen, "I/O buffer size");
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#endif /* CONFIG_USB_FILE_STORAGE_TEST */
391
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/*
394 * These definitions will permit the compiler to avoid generating code for
395 * parts of the driver that aren't used in the non-TEST version. Even gcc
396 * can recognize when a test of a constant expression yields a dead code
397 * path.
398 */
399
400#ifdef CONFIG_USB_FILE_STORAGE_TEST
401
402#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
403#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
404#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
405
406#else
407
408#define transport_is_bbb() 1
409#define transport_is_cbi() 0
410#define protocol_is_scsi() 1
411
412#endif /* CONFIG_USB_FILE_STORAGE_TEST */
413
414
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100415/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418struct fsg_dev {
419 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
420 spinlock_t lock;
421 struct usb_gadget *gadget;
422
423 /* filesem protects: backing files in use */
424 struct rw_semaphore filesem;
425
Alan Stern87c42522005-11-09 16:59:56 -0500426 /* reference counting: wait until all LUNs are released */
427 struct kref ref;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct usb_ep *ep0; // Handy copy of gadget->ep0
430 struct usb_request *ep0req; // For control responses
Alan Sterna21d4fe2005-11-29 12:04:24 -0500431 unsigned int ep0_req_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 const char *ep0req_name;
433
434 struct usb_request *intreq; // For interrupt responses
Alan Sterna21d4fe2005-11-29 12:04:24 -0500435 int intreq_busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct fsg_buffhd *intr_buffhd;
437
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100438 unsigned int bulk_out_maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 enum fsg_state state; // For exception handling
440 unsigned int exception_req_tag;
441
442 u8 config, new_config;
443
444 unsigned int running : 1;
445 unsigned int bulk_in_enabled : 1;
446 unsigned int bulk_out_enabled : 1;
447 unsigned int intr_in_enabled : 1;
448 unsigned int phase_error : 1;
449 unsigned int short_packet_received : 1;
450 unsigned int bad_lun_okay : 1;
451
452 unsigned long atomic_bitflags;
453#define REGISTERED 0
Alan Sternb950bdb2008-04-14 11:45:29 -0400454#define IGNORE_BULK_OUT 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#define SUSPENDED 2
456
457 struct usb_ep *bulk_in;
458 struct usb_ep *bulk_out;
459 struct usb_ep *intr_in;
460
461 struct fsg_buffhd *next_buffhd_to_fill;
462 struct fsg_buffhd *next_buffhd_to_drain;
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100463 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int thread_wakeup_needed;
466 struct completion thread_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct task_struct *thread_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 int cmnd_size;
470 u8 cmnd[MAX_COMMAND_SIZE];
471 enum data_direction data_dir;
472 u32 data_size;
473 u32 data_size_from_cmnd;
474 u32 tag;
475 unsigned int lun;
476 u32 residue;
477 u32 usb_amount_left;
478
479 /* The CB protocol offers no way for a host to know when a command
480 * has completed. As a result the next command may arrive early,
481 * and we will still have to handle it. For that reason we need
482 * a buffer to store new commands when using CB (or CBI, which
483 * does not oblige a host to wait for command completion either). */
484 int cbbuf_cmnd_size;
485 u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
486
487 unsigned int nluns;
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100488 struct fsg_lun *luns;
489 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490};
491
492typedef void (*fsg_routine_t)(struct fsg_dev *);
493
Alan Stern79a7d9e2007-08-08 17:10:11 -0400494static int exception_in_progress(struct fsg_dev *fsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 return (fsg->state > FSG_STATE_IDLE);
497}
498
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -0700499/* Make bulk-out requests be divisible by the maxpacket size */
500static void set_bulk_out_req_length(struct fsg_dev *fsg,
501 struct fsg_buffhd *bh, unsigned int length)
502{
503 unsigned int rem;
504
505 bh->bulk_out_intended_length = length;
506 rem = length % fsg->bulk_out_maxpacket;
507 if (rem > 0)
508 length += fsg->bulk_out_maxpacket - rem;
509 bh->outreq->length = length;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static struct fsg_dev *the_fsg;
513static struct usb_gadget_driver fsg_driver;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516/*-------------------------------------------------------------------------*/
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
519{
520 const char *name;
521
522 if (ep == fsg->bulk_in)
523 name = "bulk-in";
524 else if (ep == fsg->bulk_out)
525 name = "bulk-out";
526 else
527 name = ep->name;
528 DBG(fsg, "%s set halt\n", name);
529 return usb_ep_set_halt(ep);
530}
531
532
533/*-------------------------------------------------------------------------*/
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
536 * DESCRIPTORS ... most are static, but strings and (full) configuration
537 * descriptors are built on demand. Also the (static) config and interface
538 * descriptors are adjusted during fsg_bind().
539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541/* There is only one configuration. */
542#define CONFIG_VALUE 1
543
544static struct usb_device_descriptor
545device_desc = {
546 .bLength = sizeof device_desc,
547 .bDescriptorType = USB_DT_DEVICE,
548
Harvey Harrison551509d2009-02-11 14:11:36 -0800549 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 .bDeviceClass = USB_CLASS_PER_INTERFACE,
551
552 /* The next three values can be overridden by module parameters */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100553 .idVendor = cpu_to_le16(FSG_VENDOR_ID),
554 .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
Harvey Harrison551509d2009-02-11 14:11:36 -0800555 .bcdDevice = cpu_to_le16(0xffff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100557 .iManufacturer = FSG_STRING_MANUFACTURER,
558 .iProduct = FSG_STRING_PRODUCT,
559 .iSerialNumber = FSG_STRING_SERIAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 .bNumConfigurations = 1,
561};
562
563static struct usb_config_descriptor
564config_desc = {
565 .bLength = sizeof config_desc,
566 .bDescriptorType = USB_DT_CONFIG,
567
568 /* wTotalLength computed by usb_gadget_config_buf() */
569 .bNumInterfaces = 1,
570 .bConfigurationValue = CONFIG_VALUE,
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100571 .iConfiguration = FSG_STRING_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
David Brownell36e893d2008-09-12 09:39:06 -0700573 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574};
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static struct usb_qualifier_descriptor
578dev_qualifier = {
579 .bLength = sizeof dev_qualifier,
580 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
581
Harvey Harrison551509d2009-02-11 14:11:36 -0800582 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 .bDeviceClass = USB_CLASS_PER_INTERFACE,
584
585 .bNumConfigurations = 1,
586};
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589
590/*
591 * Config descriptors must agree with the code that sets configurations
592 * and with code managing interfaces and their altsettings. They must
593 * also handle different speeds and other-speed requests.
594 */
595static int populate_config_buf(struct usb_gadget *gadget,
596 u8 *buf, u8 type, unsigned index)
597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 enum usb_device_speed speed = gadget->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int len;
600 const struct usb_descriptor_header **function;
601
602 if (index > 0)
603 return -EINVAL;
604
David Brownell2e806f62007-08-02 00:03:39 -0700605 if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100607 function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
608 ? (const struct usb_descriptor_header **)fsg_hs_function
609 : (const struct usb_descriptor_header **)fsg_fs_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* for now, don't advertise srp-only devices */
David Brownell2e806f62007-08-02 00:03:39 -0700612 if (!gadget_is_otg(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 function++;
614
615 len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
616 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
617 return len;
618}
619
620
621/*-------------------------------------------------------------------------*/
622
623/* These routines may be called in process context or in_irq */
624
Alan Sterna21d4fe2005-11-29 12:04:24 -0500625/* Caller must hold fsg->lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static void wakeup_thread(struct fsg_dev *fsg)
627{
628 /* Tell the main thread that something has happened */
629 fsg->thread_wakeup_needed = 1;
Alan Sterna21d4fe2005-11-29 12:04:24 -0500630 if (fsg->thread_task)
631 wake_up_process(fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634
635static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
636{
637 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* Do nothing if a higher-priority exception is already in progress.
640 * If a lower-or-equal priority exception is in progress, preempt it
641 * and notify the main thread by sending it a signal. */
642 spin_lock_irqsave(&fsg->lock, flags);
643 if (fsg->state <= new_state) {
644 fsg->exception_req_tag = fsg->ep0_req_tag;
645 fsg->state = new_state;
Alan Stern22efcf42005-09-26 16:12:02 -0400646 if (fsg->thread_task)
647 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
648 fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 spin_unlock_irqrestore(&fsg->lock, flags);
651}
652
653
654/*-------------------------------------------------------------------------*/
655
656/* The disconnect callback and ep0 routines. These always run in_irq,
657 * except that ep0_queue() is called in the main thread to acknowledge
658 * completion of various requests: set config, set interface, and
659 * Bulk-only device reset. */
660
661static void fsg_disconnect(struct usb_gadget *gadget)
662{
663 struct fsg_dev *fsg = get_gadget_data(gadget);
664
665 DBG(fsg, "disconnect or port reset\n");
666 raise_exception(fsg, FSG_STATE_DISCONNECT);
667}
668
669
670static int ep0_queue(struct fsg_dev *fsg)
671{
672 int rc;
673
674 rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
675 if (rc != 0 && rc != -ESHUTDOWN) {
676
677 /* We can't do much more than wait for a reset */
Arjan van de Venb6c63932008-07-25 01:45:52 -0700678 WARNING(fsg, "error in submission: %s --> %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 fsg->ep0->name, rc);
680 }
681 return rc;
682}
683
684static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
685{
John Daiker7ca46b82006-12-29 19:02:06 -0800686 struct fsg_dev *fsg = ep->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (req->actual > 0)
689 dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
690 if (req->status || req->actual != req->length)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800691 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 req->status, req->actual, req->length);
693 if (req->status == -ECONNRESET) // Request was cancelled
694 usb_ep_fifo_flush(ep);
695
696 if (req->status == 0 && req->context)
697 ((fsg_routine_t) (req->context))(fsg);
698}
699
700
701/*-------------------------------------------------------------------------*/
702
703/* Bulk and interrupt endpoint completion handlers.
704 * These always run in_irq. */
705
706static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
707{
John Daiker7ca46b82006-12-29 19:02:06 -0800708 struct fsg_dev *fsg = ep->driver_data;
709 struct fsg_buffhd *bh = req->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 if (req->status || req->actual != req->length)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800712 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 req->status, req->actual, req->length);
714 if (req->status == -ECONNRESET) // Request was cancelled
715 usb_ep_fifo_flush(ep);
716
717 /* Hold the lock while we update the request and buffer states */
Alan Sterna21d4fe2005-11-29 12:04:24 -0500718 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 spin_lock(&fsg->lock);
720 bh->inreq_busy = 0;
721 bh->state = BUF_STATE_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 wakeup_thread(fsg);
Alan Sterna21d4fe2005-11-29 12:04:24 -0500723 spin_unlock(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
727{
John Daiker7ca46b82006-12-29 19:02:06 -0800728 struct fsg_dev *fsg = ep->driver_data;
729 struct fsg_buffhd *bh = req->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 dump_msg(fsg, "bulk-out", req->buf, req->actual);
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -0700732 if (req->status || req->actual != bh->bulk_out_intended_length)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800733 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -0700734 req->status, req->actual,
735 bh->bulk_out_intended_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (req->status == -ECONNRESET) // Request was cancelled
737 usb_ep_fifo_flush(ep);
738
739 /* Hold the lock while we update the request and buffer states */
Alan Sterna21d4fe2005-11-29 12:04:24 -0500740 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_lock(&fsg->lock);
742 bh->outreq_busy = 0;
743 bh->state = BUF_STATE_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 wakeup_thread(fsg);
Alan Sterna21d4fe2005-11-29 12:04:24 -0500745 spin_unlock(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748
749#ifdef CONFIG_USB_FILE_STORAGE_TEST
750static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
751{
John Daiker7ca46b82006-12-29 19:02:06 -0800752 struct fsg_dev *fsg = ep->driver_data;
753 struct fsg_buffhd *bh = req->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (req->status || req->actual != req->length)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800756 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 req->status, req->actual, req->length);
758 if (req->status == -ECONNRESET) // Request was cancelled
759 usb_ep_fifo_flush(ep);
760
761 /* Hold the lock while we update the request and buffer states */
Alan Sterna21d4fe2005-11-29 12:04:24 -0500762 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 spin_lock(&fsg->lock);
764 fsg->intreq_busy = 0;
765 bh->state = BUF_STATE_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 wakeup_thread(fsg);
Alan Sterna21d4fe2005-11-29 12:04:24 -0500767 spin_unlock(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770#else
771static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
772{}
773#endif /* CONFIG_USB_FILE_STORAGE_TEST */
774
775
776/*-------------------------------------------------------------------------*/
777
778/* Ep0 class-specific handlers. These always run in_irq. */
779
780#ifdef CONFIG_USB_FILE_STORAGE_TEST
781static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
782{
783 struct usb_request *req = fsg->ep0req;
784 static u8 cbi_reset_cmnd[6] = {
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +0200785 SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* Error in command transfer? */
788 if (req->status || req->length != req->actual ||
789 req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
790
791 /* Not all controllers allow a protocol stall after
792 * receiving control-out data, but we'll try anyway. */
793 fsg_set_halt(fsg, fsg->ep0);
794 return; // Wait for reset
795 }
796
797 /* Is it the special reset command? */
798 if (req->actual >= sizeof cbi_reset_cmnd &&
799 memcmp(req->buf, cbi_reset_cmnd,
800 sizeof cbi_reset_cmnd) == 0) {
801
802 /* Raise an exception to stop the current operation
803 * and reinitialize our state. */
804 DBG(fsg, "cbi reset request\n");
805 raise_exception(fsg, FSG_STATE_RESET);
806 return;
807 }
808
809 VDBG(fsg, "CB[I] accept device-specific command\n");
810 spin_lock(&fsg->lock);
811
812 /* Save the command for later */
813 if (fsg->cbbuf_cmnd_size)
Arjan van de Venb6c63932008-07-25 01:45:52 -0700814 WARNING(fsg, "CB[I] overwriting previous command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 fsg->cbbuf_cmnd_size = req->actual;
816 memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 wakeup_thread(fsg);
Alan Sterna21d4fe2005-11-29 12:04:24 -0500819 spin_unlock(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822#else
823static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
824{}
825#endif /* CONFIG_USB_FILE_STORAGE_TEST */
826
827
828static int class_setup_req(struct fsg_dev *fsg,
829 const struct usb_ctrlrequest *ctrl)
830{
831 struct usb_request *req = fsg->ep0req;
832 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -0700833 u16 w_index = le16_to_cpu(ctrl->wIndex);
Luis Lloret88e45db2007-07-26 10:08:47 -0400834 u16 w_value = le16_to_cpu(ctrl->wValue);
David Brownell1bbc1692005-05-07 13:05:13 -0700835 u16 w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (!fsg->config)
838 return value;
839
840 /* Handle Bulk-only class-specific requests */
841 if (transport_is_bbb()) {
842 switch (ctrl->bRequest) {
843
844 case USB_BULK_RESET_REQUEST:
845 if (ctrl->bRequestType != (USB_DIR_OUT |
846 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
847 break;
Luis Lloret88e45db2007-07-26 10:08:47 -0400848 if (w_index != 0 || w_value != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 value = -EDOM;
850 break;
851 }
852
853 /* Raise an exception to stop the current operation
854 * and reinitialize our state. */
855 DBG(fsg, "bulk reset request\n");
856 raise_exception(fsg, FSG_STATE_RESET);
857 value = DELAYED_STATUS;
858 break;
859
860 case USB_BULK_GET_MAX_LUN_REQUEST:
861 if (ctrl->bRequestType != (USB_DIR_IN |
862 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
863 break;
Luis Lloret88e45db2007-07-26 10:08:47 -0400864 if (w_index != 0 || w_value != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 value = -EDOM;
866 break;
867 }
868 VDBG(fsg, "get max LUN\n");
869 *(u8 *) req->buf = fsg->nluns - 1;
Alan Stern76f4af82005-04-05 11:56:54 -0400870 value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
872 }
873 }
874
875 /* Handle CBI class-specific requests */
876 else {
877 switch (ctrl->bRequest) {
878
879 case USB_CBI_ADSC_REQUEST:
880 if (ctrl->bRequestType != (USB_DIR_OUT |
881 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
882 break;
Luis Lloret88e45db2007-07-26 10:08:47 -0400883 if (w_index != 0 || w_value != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 value = -EDOM;
885 break;
886 }
887 if (w_length > MAX_COMMAND_SIZE) {
888 value = -EOVERFLOW;
889 break;
890 }
891 value = w_length;
892 fsg->ep0req->context = received_cbi_adsc;
893 break;
894 }
895 }
896
897 if (value == -EOPNOTSUPP)
898 VDBG(fsg,
899 "unknown class-specific control req "
900 "%02x.%02x v%04x i%04x l%u\n",
901 ctrl->bRequestType, ctrl->bRequest,
David Brownell1bbc1692005-05-07 13:05:13 -0700902 le16_to_cpu(ctrl->wValue), w_index, w_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return value;
904}
905
906
907/*-------------------------------------------------------------------------*/
908
909/* Ep0 standard request handlers. These always run in_irq. */
910
911static int standard_setup_req(struct fsg_dev *fsg,
912 const struct usb_ctrlrequest *ctrl)
913{
914 struct usb_request *req = fsg->ep0req;
915 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -0700916 u16 w_index = le16_to_cpu(ctrl->wIndex);
917 u16 w_value = le16_to_cpu(ctrl->wValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* Usually this just stores reply data in the pre-allocated ep0 buffer,
920 * but config change events will also reconfigure hardware. */
921 switch (ctrl->bRequest) {
922
923 case USB_REQ_GET_DESCRIPTOR:
924 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
925 USB_RECIP_DEVICE))
926 break;
927 switch (w_value >> 8) {
928
929 case USB_DT_DEVICE:
930 VDBG(fsg, "get device descriptor\n");
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200931 device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
Alan Stern76f4af82005-04-05 11:56:54 -0400932 value = sizeof device_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 memcpy(req->buf, &device_desc, value);
934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 case USB_DT_DEVICE_QUALIFIER:
936 VDBG(fsg, "get device qualifier\n");
David Brownell2e806f62007-08-02 00:03:39 -0700937 if (!gadget_is_dualspeed(fsg->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 break;
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200939 /*
940 * Assume ep0 uses the same maxpacket value for both
941 * speeds
942 */
943 dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
Alan Stern76f4af82005-04-05 11:56:54 -0400944 value = sizeof dev_qualifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 memcpy(req->buf, &dev_qualifier, value);
946 break;
947
948 case USB_DT_OTHER_SPEED_CONFIG:
949 VDBG(fsg, "get other-speed config descriptor\n");
David Brownell2e806f62007-08-02 00:03:39 -0700950 if (!gadget_is_dualspeed(fsg->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
952 goto get_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 case USB_DT_CONFIG:
954 VDBG(fsg, "get configuration descriptor\n");
David Brownell2e806f62007-08-02 00:03:39 -0700955get_config:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 value = populate_config_buf(fsg->gadget,
957 req->buf,
958 w_value >> 8,
959 w_value & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 break;
961
962 case USB_DT_STRING:
963 VDBG(fsg, "get string descriptor\n");
964
965 /* wIndex == language code */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100966 value = usb_gadget_get_string(&fsg_stringtab,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 w_value & 0xff, req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 break;
969 }
970 break;
971
972 /* One config, two speeds */
973 case USB_REQ_SET_CONFIGURATION:
974 if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
975 USB_RECIP_DEVICE))
976 break;
977 VDBG(fsg, "set configuration\n");
978 if (w_value == CONFIG_VALUE || w_value == 0) {
979 fsg->new_config = w_value;
980
981 /* Raise an exception to wipe out previous transaction
982 * state (queued bufs, etc) and set the new config. */
983 raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
984 value = DELAYED_STATUS;
985 }
986 break;
987 case USB_REQ_GET_CONFIGURATION:
988 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
989 USB_RECIP_DEVICE))
990 break;
991 VDBG(fsg, "get configuration\n");
992 *(u8 *) req->buf = fsg->config;
Alan Stern76f4af82005-04-05 11:56:54 -0400993 value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995
996 case USB_REQ_SET_INTERFACE:
997 if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
998 USB_RECIP_INTERFACE))
999 break;
1000 if (fsg->config && w_index == 0) {
1001
1002 /* Raise an exception to wipe out previous transaction
1003 * state (queued bufs, etc) and install the new
1004 * interface altsetting. */
1005 raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
1006 value = DELAYED_STATUS;
1007 }
1008 break;
1009 case USB_REQ_GET_INTERFACE:
1010 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
1011 USB_RECIP_INTERFACE))
1012 break;
1013 if (!fsg->config)
1014 break;
1015 if (w_index != 0) {
1016 value = -EDOM;
1017 break;
1018 }
1019 VDBG(fsg, "get interface\n");
1020 *(u8 *) req->buf = 0;
Alan Stern76f4af82005-04-05 11:56:54 -04001021 value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 break;
1023
1024 default:
1025 VDBG(fsg,
1026 "unknown control req %02x.%02x v%04x i%04x l%u\n",
1027 ctrl->bRequestType, ctrl->bRequest,
David Brownell1bbc1692005-05-07 13:05:13 -07001028 w_value, w_index, le16_to_cpu(ctrl->wLength));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030
1031 return value;
1032}
1033
1034
1035static int fsg_setup(struct usb_gadget *gadget,
1036 const struct usb_ctrlrequest *ctrl)
1037{
1038 struct fsg_dev *fsg = get_gadget_data(gadget);
1039 int rc;
David Brownell1bbc1692005-05-07 13:05:13 -07001040 int w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 ++fsg->ep0_req_tag; // Record arrival of a new request
1043 fsg->ep0req->context = NULL;
1044 fsg->ep0req->length = 0;
1045 dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
1046
1047 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
1048 rc = class_setup_req(fsg, ctrl);
1049 else
1050 rc = standard_setup_req(fsg, ctrl);
1051
1052 /* Respond with data/status or defer until later? */
1053 if (rc >= 0 && rc != DELAYED_STATUS) {
Alan Stern76f4af82005-04-05 11:56:54 -04001054 rc = min(rc, w_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 fsg->ep0req->length = rc;
David Brownell1bbc1692005-05-07 13:05:13 -07001056 fsg->ep0req->zero = rc < w_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
1058 "ep0-in" : "ep0-out");
1059 rc = ep0_queue(fsg);
1060 }
1061
1062 /* Device either stalls (rc < 0) or reports success */
1063 return rc;
1064}
1065
1066
1067/*-------------------------------------------------------------------------*/
1068
1069/* All the following routines run in process context */
1070
1071
1072/* Use this for bulk or interrupt transfers, not ep0 */
1073static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
Alan Sterna21d4fe2005-11-29 12:04:24 -05001074 struct usb_request *req, int *pbusy,
1075 enum fsg_buffer_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 int rc;
1078
1079 if (ep == fsg->bulk_in)
1080 dump_msg(fsg, "bulk-in", req->buf, req->length);
1081 else if (ep == fsg->intr_in)
1082 dump_msg(fsg, "intr-in", req->buf, req->length);
Alan Sterna21d4fe2005-11-29 12:04:24 -05001083
1084 spin_lock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *pbusy = 1;
1086 *state = BUF_STATE_BUSY;
Alan Sterna21d4fe2005-11-29 12:04:24 -05001087 spin_unlock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 rc = usb_ep_queue(ep, req, GFP_KERNEL);
1089 if (rc != 0) {
1090 *pbusy = 0;
1091 *state = BUF_STATE_EMPTY;
1092
1093 /* We can't do much more than wait for a reset */
1094
1095 /* Note: currently the net2280 driver fails zero-length
1096 * submissions if DMA is enabled. */
1097 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
1098 req->length == 0))
Arjan van de Venb6c63932008-07-25 01:45:52 -07001099 WARNING(fsg, "error in submission: %s --> %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ep->name, rc);
1101 }
1102}
1103
1104
1105static int sleep_thread(struct fsg_dev *fsg)
1106{
Alan Sterna21d4fe2005-11-29 12:04:24 -05001107 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* Wait until a signal arrives or we are woken up */
Alan Sterna21d4fe2005-11-29 12:04:24 -05001110 for (;;) {
1111 try_to_freeze();
1112 set_current_state(TASK_INTERRUPTIBLE);
1113 if (signal_pending(current)) {
1114 rc = -EINTR;
1115 break;
1116 }
1117 if (fsg->thread_wakeup_needed)
1118 break;
1119 schedule();
1120 }
1121 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 fsg->thread_wakeup_needed = 0;
Alan Sterna21d4fe2005-11-29 12:04:24 -05001123 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126
1127/*-------------------------------------------------------------------------*/
1128
1129static int do_read(struct fsg_dev *fsg)
1130{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001131 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 u32 lba;
1133 struct fsg_buffhd *bh;
1134 int rc;
1135 u32 amount_left;
1136 loff_t file_offset, file_offset_tmp;
1137 unsigned int amount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 ssize_t nread;
1139
1140 /* Get the starting Logical Block Address and check that it's
1141 * not too big */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001142 if (fsg->cmnd[0] == READ_6)
Alan Stern604eb892009-04-27 13:19:41 -04001143 lba = get_unaligned_be24(&fsg->cmnd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 else {
Alan Stern604eb892009-04-27 13:19:41 -04001145 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* We allow DPO (Disable Page Out = don't save data in the
1148 * cache) and FUA (Force Unit Access = don't read from the
1149 * cache), but we don't implement them. */
1150 if ((fsg->cmnd[1] & ~0x18) != 0) {
1151 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1152 return -EINVAL;
1153 }
1154 }
1155 if (lba >= curlun->num_sectors) {
1156 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1157 return -EINVAL;
1158 }
Peiyu Li3f565a32011-08-17 22:52:59 -07001159 file_offset = ((loff_t) lba) << curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /* Carry out the file reads */
1162 amount_left = fsg->data_size_from_cmnd;
1163 if (unlikely(amount_left == 0))
1164 return -EIO; // No default reply
1165
1166 for (;;) {
1167
1168 /* Figure out how much we need to read:
1169 * Try to read the remaining amount.
1170 * But don't read more than the buffer size.
1171 * And don't try to read past the end of the file.
Alan Stern04eee252011-08-18 14:29:00 -04001172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 amount = min((unsigned int) amount_left, mod_data.buflen);
1174 amount = min((loff_t) amount,
1175 curlun->file_length - file_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /* Wait for the next buffer to become available */
1178 bh = fsg->next_buffhd_to_fill;
1179 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04001180 rc = sleep_thread(fsg);
1181 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return rc;
1183 }
1184
1185 /* If we were asked to read past the end of file,
1186 * end with an empty buffer. */
1187 if (amount == 0) {
1188 curlun->sense_data =
1189 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
Peiyu Li3f565a32011-08-17 22:52:59 -07001190 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001191 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 bh->inreq->length = 0;
1193 bh->state = BUF_STATE_FULL;
1194 break;
1195 }
1196
1197 /* Perform the read */
1198 file_offset_tmp = file_offset;
1199 nread = vfs_read(curlun->filp,
1200 (char __user *) bh->buf,
1201 amount, &file_offset_tmp);
1202 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1203 (unsigned long long) file_offset,
1204 (int) nread);
1205 if (signal_pending(current))
1206 return -EINTR;
1207
1208 if (nread < 0) {
1209 LDBG(curlun, "error in file read: %d\n",
1210 (int) nread);
1211 nread = 0;
1212 } else if (nread < amount) {
1213 LDBG(curlun, "partial file read: %d/%u\n",
1214 (int) nread, amount);
Peiyu Li3f565a32011-08-17 22:52:59 -07001215 nread = round_down(nread, curlun->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 file_offset += nread;
1218 amount_left -= nread;
1219 fsg->residue -= nread;
Alan Stern04eee252011-08-18 14:29:00 -04001220
1221 /* Except at the end of the transfer, nread will be
1222 * equal to the buffer size, which is divisible by the
1223 * bulk-in maxpacket size.
1224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 bh->inreq->length = nread;
1226 bh->state = BUF_STATE_FULL;
1227
1228 /* If an error occurred, report it and its position */
1229 if (nread < amount) {
1230 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
Peiyu Li3f565a32011-08-17 22:52:59 -07001231 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001232 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234 }
1235
1236 if (amount_left == 0)
1237 break; // No more left to read
1238
1239 /* Send this buffer and go read some more */
1240 bh->inreq->zero = 0;
1241 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1242 &bh->inreq_busy, &bh->state);
1243 fsg->next_buffhd_to_fill = bh->next;
1244 }
1245
1246 return -EIO; // No default reply
1247}
1248
1249
1250/*-------------------------------------------------------------------------*/
1251
1252static int do_write(struct fsg_dev *fsg)
1253{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001254 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 u32 lba;
1256 struct fsg_buffhd *bh;
1257 int get_some_more;
1258 u32 amount_left_to_req, amount_left_to_write;
1259 loff_t usb_offset, file_offset, file_offset_tmp;
1260 unsigned int amount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 ssize_t nwritten;
1262 int rc;
1263
1264 if (curlun->ro) {
1265 curlun->sense_data = SS_WRITE_PROTECTED;
1266 return -EINVAL;
1267 }
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001268 spin_lock(&curlun->filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001270 spin_unlock(&curlun->filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /* Get the starting Logical Block Address and check that it's
1273 * not too big */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001274 if (fsg->cmnd[0] == WRITE_6)
Alan Stern604eb892009-04-27 13:19:41 -04001275 lba = get_unaligned_be24(&fsg->cmnd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 else {
Alan Stern604eb892009-04-27 13:19:41 -04001277 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /* We allow DPO (Disable Page Out = don't save data in the
1280 * cache) and FUA (Force Unit Access = write directly to the
1281 * medium). We don't implement DPO; we implement FUA by
1282 * performing synchronous output. */
1283 if ((fsg->cmnd[1] & ~0x18) != 0) {
1284 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1285 return -EINVAL;
1286 }
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03001287 /* FUA */
1288 if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001289 spin_lock(&curlun->filp->f_lock);
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001290 curlun->filp->f_flags |= O_DSYNC;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001291 spin_unlock(&curlun->filp->f_lock);
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 if (lba >= curlun->num_sectors) {
1295 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1296 return -EINVAL;
1297 }
1298
1299 /* Carry out the file writes */
1300 get_some_more = 1;
Peiyu Li3f565a32011-08-17 22:52:59 -07001301 file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
1303
1304 while (amount_left_to_write > 0) {
1305
1306 /* Queue a request for more data from the host */
1307 bh = fsg->next_buffhd_to_fill;
1308 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
1309
1310 /* Figure out how much we want to get:
Alan Stern04eee252011-08-18 14:29:00 -04001311 * Try to get the remaining amount,
1312 * but not more than the buffer size.
1313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 amount = min(amount_left_to_req, mod_data.buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Alan Stern04eee252011-08-18 14:29:00 -04001316 /* Beyond the end of the backing file? */
1317 if (usb_offset >= curlun->file_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 get_some_more = 0;
1319 curlun->sense_data =
1320 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
Peiyu Li3f565a32011-08-17 22:52:59 -07001321 curlun->sense_data_info = usb_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001322 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 continue;
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* Get the next buffer */
1327 usb_offset += amount;
1328 fsg->usb_amount_left -= amount;
1329 amount_left_to_req -= amount;
1330 if (amount_left_to_req == 0)
1331 get_some_more = 0;
1332
Alan Stern04eee252011-08-18 14:29:00 -04001333 /* Except at the end of the transfer, amount will be
1334 * equal to the buffer size, which is divisible by
1335 * the bulk-out maxpacket size.
1336 */
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -07001337 bh->outreq->length = bh->bulk_out_intended_length =
1338 amount;
Alan Stern70ffe6e2006-03-23 15:05:16 -05001339 bh->outreq->short_not_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1341 &bh->outreq_busy, &bh->state);
1342 fsg->next_buffhd_to_fill = bh->next;
1343 continue;
1344 }
1345
1346 /* Write the received data to the backing file */
1347 bh = fsg->next_buffhd_to_drain;
1348 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
1349 break; // We stopped early
1350 if (bh->state == BUF_STATE_FULL) {
Alan Sterna21d4fe2005-11-29 12:04:24 -05001351 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 fsg->next_buffhd_to_drain = bh->next;
1353 bh->state = BUF_STATE_EMPTY;
1354
1355 /* Did something go wrong with the transfer? */
1356 if (bh->outreq->status != 0) {
1357 curlun->sense_data = SS_COMMUNICATION_FAILURE;
Peiyu Li3f565a32011-08-17 22:52:59 -07001358 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001359 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
1361 }
1362
1363 amount = bh->outreq->actual;
1364 if (curlun->file_length - file_offset < amount) {
1365 LERROR(curlun,
1366 "write %u @ %llu beyond end %llu\n",
1367 amount, (unsigned long long) file_offset,
1368 (unsigned long long) curlun->file_length);
1369 amount = curlun->file_length - file_offset;
1370 }
1371
Alan Stern04eee252011-08-18 14:29:00 -04001372 /* Don't write a partial block */
1373 amount = round_down(amount, curlun->blksize);
1374 if (amount == 0)
1375 goto empty_write;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /* Perform the write */
1378 file_offset_tmp = file_offset;
1379 nwritten = vfs_write(curlun->filp,
1380 (char __user *) bh->buf,
1381 amount, &file_offset_tmp);
1382 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1383 (unsigned long long) file_offset,
1384 (int) nwritten);
1385 if (signal_pending(current))
1386 return -EINTR; // Interrupted!
1387
1388 if (nwritten < 0) {
1389 LDBG(curlun, "error in file write: %d\n",
1390 (int) nwritten);
1391 nwritten = 0;
1392 } else if (nwritten < amount) {
1393 LDBG(curlun, "partial file write: %d/%u\n",
1394 (int) nwritten, amount);
Peiyu Li3f565a32011-08-17 22:52:59 -07001395 nwritten = round_down(nwritten, curlun->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 file_offset += nwritten;
1398 amount_left_to_write -= nwritten;
1399 fsg->residue -= nwritten;
1400
1401 /* If an error occurred, report it and its position */
1402 if (nwritten < amount) {
1403 curlun->sense_data = SS_WRITE_ERROR;
Peiyu Li3f565a32011-08-17 22:52:59 -07001404 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001405 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 break;
1407 }
1408
Alan Stern04eee252011-08-18 14:29:00 -04001409 empty_write:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /* Did the host decide to stop early? */
1411 if (bh->outreq->actual != bh->outreq->length) {
1412 fsg->short_packet_received = 1;
1413 break;
1414 }
1415 continue;
1416 }
1417
1418 /* Wait for something to happen */
Alan Stern79a7d9e2007-08-08 17:10:11 -04001419 rc = sleep_thread(fsg);
1420 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return rc;
1422 }
1423
1424 return -EIO; // No default reply
1425}
1426
1427
1428/*-------------------------------------------------------------------------*/
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430static int do_synchronize_cache(struct fsg_dev *fsg)
1431{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001432 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int rc;
1434
1435 /* We ignore the requested LBA and write out all file's
1436 * dirty data buffers. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001437 rc = fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (rc)
1439 curlun->sense_data = SS_WRITE_ERROR;
1440 return 0;
1441}
1442
1443
1444/*-------------------------------------------------------------------------*/
1445
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001446static void invalidate_sub(struct fsg_lun *curlun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 struct file *filp = curlun->filp;
Josef Sipek33cb8992006-12-08 02:37:46 -08001449 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 unsigned long rc;
1451
Andrew Mortonfc0ecff2007-02-10 01:45:39 -08001452 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001453 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456static int do_verify(struct fsg_dev *fsg)
1457{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001458 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 u32 lba;
1460 u32 verification_length;
1461 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1462 loff_t file_offset, file_offset_tmp;
1463 u32 amount_left;
1464 unsigned int amount;
1465 ssize_t nread;
1466
1467 /* Get the starting Logical Block Address and check that it's
1468 * not too big */
Alan Stern604eb892009-04-27 13:19:41 -04001469 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (lba >= curlun->num_sectors) {
1471 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1472 return -EINVAL;
1473 }
1474
1475 /* We allow DPO (Disable Page Out = don't save data in the
1476 * cache) but we don't implement it. */
1477 if ((fsg->cmnd[1] & ~0x10) != 0) {
1478 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1479 return -EINVAL;
1480 }
1481
Alan Stern604eb892009-04-27 13:19:41 -04001482 verification_length = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (unlikely(verification_length == 0))
1484 return -EIO; // No default reply
1485
1486 /* Prepare to carry out the file verify */
Peiyu Li3f565a32011-08-17 22:52:59 -07001487 amount_left = verification_length << curlun->blkbits;
1488 file_offset = ((loff_t) lba) << curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 /* Write out all the dirty buffers before invalidating them */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001491 fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (signal_pending(current))
1493 return -EINTR;
1494
1495 invalidate_sub(curlun);
1496 if (signal_pending(current))
1497 return -EINTR;
1498
1499 /* Just try to read the requested blocks */
1500 while (amount_left > 0) {
1501
1502 /* Figure out how much we need to read:
1503 * Try to read the remaining amount, but not more than
1504 * the buffer size.
1505 * And don't try to read past the end of the file.
Alan Stern04eee252011-08-18 14:29:00 -04001506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 amount = min((unsigned int) amount_left, mod_data.buflen);
1508 amount = min((loff_t) amount,
1509 curlun->file_length - file_offset);
1510 if (amount == 0) {
1511 curlun->sense_data =
1512 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
Peiyu Li3f565a32011-08-17 22:52:59 -07001513 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001514 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 break;
1516 }
1517
1518 /* Perform the read */
1519 file_offset_tmp = file_offset;
1520 nread = vfs_read(curlun->filp,
1521 (char __user *) bh->buf,
1522 amount, &file_offset_tmp);
1523 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1524 (unsigned long long) file_offset,
1525 (int) nread);
1526 if (signal_pending(current))
1527 return -EINTR;
1528
1529 if (nread < 0) {
1530 LDBG(curlun, "error in file verify: %d\n",
1531 (int) nread);
1532 nread = 0;
1533 } else if (nread < amount) {
1534 LDBG(curlun, "partial file verify: %d/%u\n",
1535 (int) nread, amount);
Peiyu Li3f565a32011-08-17 22:52:59 -07001536 nread = round_down(nread, curlun->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538 if (nread == 0) {
1539 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
Peiyu Li3f565a32011-08-17 22:52:59 -07001540 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001541 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 }
1544 file_offset += nread;
1545 amount_left -= nread;
1546 }
1547 return 0;
1548}
1549
1550
1551/*-------------------------------------------------------------------------*/
1552
1553static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1554{
1555 u8 *buf = (u8 *) bh->buf;
1556
1557 static char vendor_id[] = "Linux ";
Alan Stern12aae682008-11-20 14:13:12 -05001558 static char product_disk_id[] = "File-Stor Gadget";
1559 static char product_cdrom_id[] = "File-CD Gadget ";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (!fsg->curlun) { // Unsupported LUNs are okay
1562 fsg->bad_lun_okay = 1;
1563 memset(buf, 0, 36);
1564 buf[0] = 0x7f; // Unsupported, no device-type
Alan Stern12aae682008-11-20 14:13:12 -05001565 buf[4] = 31; // Additional length
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return 36;
1567 }
1568
Alan Stern12aae682008-11-20 14:13:12 -05001569 memset(buf, 0, 8);
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001570 buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (mod_data.removable)
1572 buf[1] = 0x80;
1573 buf[2] = 2; // ANSI SCSI level 2
1574 buf[3] = 2; // SCSI-2 INQUIRY data format
1575 buf[4] = 31; // Additional length
1576 // No special options
Alan Stern12aae682008-11-20 14:13:12 -05001577 sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
1578 (mod_data.cdrom ? product_cdrom_id :
1579 product_disk_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 mod_data.release);
1581 return 36;
1582}
1583
1584
1585static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1586{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001587 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 u8 *buf = (u8 *) bh->buf;
1589 u32 sd, sdinfo;
Alan Stern6174d0f2006-09-26 14:51:48 -04001590 int valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
1593 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1594 *
1595 * If a REQUEST SENSE command is received from an initiator
1596 * with a pending unit attention condition (before the target
1597 * generates the contingent allegiance condition), then the
1598 * target shall either:
1599 * a) report any pending sense data and preserve the unit
1600 * attention condition on the logical unit, or,
1601 * b) report the unit attention condition, may discard any
1602 * pending sense data, and clear the unit attention
1603 * condition on the logical unit for that initiator.
1604 *
1605 * FSG normally uses option a); enable this code to use option b).
1606 */
1607#if 0
1608 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1609 curlun->sense_data = curlun->unit_attention_data;
1610 curlun->unit_attention_data = SS_NO_SENSE;
1611 }
1612#endif
1613
1614 if (!curlun) { // Unsupported LUNs are okay
1615 fsg->bad_lun_okay = 1;
1616 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1617 sdinfo = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04001618 valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 } else {
1620 sd = curlun->sense_data;
1621 sdinfo = curlun->sense_data_info;
Alan Stern6174d0f2006-09-26 14:51:48 -04001622 valid = curlun->info_valid << 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 curlun->sense_data = SS_NO_SENSE;
1624 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04001625 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
1628 memset(buf, 0, 18);
Alan Stern6174d0f2006-09-26 14:51:48 -04001629 buf[0] = valid | 0x70; // Valid, current error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 buf[2] = SK(sd);
Alan Stern604eb892009-04-27 13:19:41 -04001631 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 buf[7] = 18 - 8; // Additional sense length
1633 buf[12] = ASC(sd);
1634 buf[13] = ASCQ(sd);
1635 return 18;
1636}
1637
1638
1639static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1640{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001641 struct fsg_lun *curlun = fsg->curlun;
Alan Stern604eb892009-04-27 13:19:41 -04001642 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int pmi = fsg->cmnd[8];
1644 u8 *buf = (u8 *) bh->buf;
1645
1646 /* Check the PMI and LBA fields */
1647 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1648 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1649 return -EINVAL;
1650 }
1651
Alan Stern604eb892009-04-27 13:19:41 -04001652 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1653 /* Max logical block */
Peiyu Li3f565a32011-08-17 22:52:59 -07001654 put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return 8;
1656}
1657
1658
Alan Stern12aae682008-11-20 14:13:12 -05001659static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1660{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001661 struct fsg_lun *curlun = fsg->curlun;
Alan Stern12aae682008-11-20 14:13:12 -05001662 int msf = fsg->cmnd[1] & 0x02;
Alan Stern604eb892009-04-27 13:19:41 -04001663 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
Alan Stern12aae682008-11-20 14:13:12 -05001664 u8 *buf = (u8 *) bh->buf;
1665
1666 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
1667 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1668 return -EINVAL;
1669 }
1670 if (lba >= curlun->num_sectors) {
1671 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1672 return -EINVAL;
1673 }
1674
1675 memset(buf, 0, 8);
1676 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1677 store_cdrom_address(&buf[4], msf, lba);
1678 return 8;
1679}
1680
1681
1682static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1683{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001684 struct fsg_lun *curlun = fsg->curlun;
Alan Stern12aae682008-11-20 14:13:12 -05001685 int msf = fsg->cmnd[1] & 0x02;
1686 int start_track = fsg->cmnd[6];
1687 u8 *buf = (u8 *) bh->buf;
1688
1689 if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1690 start_track > 1) {
1691 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1692 return -EINVAL;
1693 }
1694
1695 memset(buf, 0, 20);
1696 buf[1] = (20-2); /* TOC data length */
1697 buf[2] = 1; /* First track number */
1698 buf[3] = 1; /* Last track number */
1699 buf[5] = 0x16; /* Data track, copying allowed */
1700 buf[6] = 0x01; /* Only track is number 1 */
1701 store_cdrom_address(&buf[8], msf, 0);
1702
1703 buf[13] = 0x16; /* Lead-out track is data */
1704 buf[14] = 0xAA; /* Lead-out track number */
1705 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1706 return 20;
1707}
1708
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1711{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001712 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 int mscmnd = fsg->cmnd[0];
1714 u8 *buf = (u8 *) bh->buf;
1715 u8 *buf0 = buf;
1716 int pc, page_code;
1717 int changeable_values, all_pages;
1718 int valid_page = 0;
1719 int len, limit;
1720
1721 if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
1722 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1723 return -EINVAL;
1724 }
1725 pc = fsg->cmnd[2] >> 6;
1726 page_code = fsg->cmnd[2] & 0x3f;
1727 if (pc == 3) {
1728 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1729 return -EINVAL;
1730 }
1731 changeable_values = (pc == 1);
1732 all_pages = (page_code == 0x3f);
1733
1734 /* Write the mode parameter header. Fixed values are: default
1735 * medium type, no cache control (DPOFUA), and no block descriptors.
1736 * The only variable value is the WriteProtect bit. We will fill in
1737 * the mode data length later. */
1738 memset(buf, 0, 8);
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001739 if (mscmnd == MODE_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1741 buf += 4;
1742 limit = 255;
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001743 } else { // MODE_SENSE_10
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1745 buf += 8;
1746 limit = 65535; // Should really be mod_data.buflen
1747 }
1748
1749 /* No block descriptors */
1750
1751 /* The mode pages, in numerical order. The only page we support
1752 * is the Caching page. */
1753 if (page_code == 0x08 || all_pages) {
1754 valid_page = 1;
1755 buf[0] = 0x08; // Page code
1756 buf[1] = 10; // Page length
1757 memset(buf+2, 0, 10); // None of the fields are changeable
1758
1759 if (!changeable_values) {
1760 buf[2] = 0x04; // Write cache enable,
1761 // Read cache not disabled
1762 // No cache retention priorities
Alan Stern604eb892009-04-27 13:19:41 -04001763 put_unaligned_be16(0xffff, &buf[4]);
1764 /* Don't disable prefetch */
1765 /* Minimum prefetch = 0 */
1766 put_unaligned_be16(0xffff, &buf[8]);
1767 /* Maximum prefetch */
1768 put_unaligned_be16(0xffff, &buf[10]);
1769 /* Maximum prefetch ceiling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771 buf += 12;
1772 }
1773
1774 /* Check that a valid page was requested and the mode data length
1775 * isn't too long. */
1776 len = buf - buf0;
1777 if (!valid_page || len > limit) {
1778 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1779 return -EINVAL;
1780 }
1781
1782 /* Store the mode data length */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001783 if (mscmnd == MODE_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 buf0[0] = len - 1;
1785 else
Alan Stern604eb892009-04-27 13:19:41 -04001786 put_unaligned_be16(len - 2, buf0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return len;
1788}
1789
1790
1791static int do_start_stop(struct fsg_dev *fsg)
1792{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001793 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int loej, start;
1795
1796 if (!mod_data.removable) {
1797 curlun->sense_data = SS_INVALID_COMMAND;
1798 return -EINVAL;
1799 }
1800
1801 // int immed = fsg->cmnd[1] & 0x01;
1802 loej = fsg->cmnd[4] & 0x02;
1803 start = fsg->cmnd[4] & 0x01;
1804
1805#ifdef CONFIG_USB_FILE_STORAGE_TEST
1806 if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
1807 (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
1808 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1809 return -EINVAL;
1810 }
1811
1812 if (!start) {
1813
1814 /* Are we allowed to unload the media? */
1815 if (curlun->prevent_medium_removal) {
1816 LDBG(curlun, "unload attempt prevented\n");
1817 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1818 return -EINVAL;
1819 }
1820 if (loej) { // Simulate an unload/eject
1821 up_read(&fsg->filesem);
1822 down_write(&fsg->filesem);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001823 fsg_lun_close(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 up_write(&fsg->filesem);
1825 down_read(&fsg->filesem);
1826 }
1827 } else {
1828
1829 /* Our emulation doesn't support mounting; the medium is
1830 * available for use as soon as it is loaded. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001831 if (!fsg_lun_is_open(curlun)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1833 return -EINVAL;
1834 }
1835 }
1836#endif
1837 return 0;
1838}
1839
1840
1841static int do_prevent_allow(struct fsg_dev *fsg)
1842{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001843 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 int prevent;
1845
1846 if (!mod_data.removable) {
1847 curlun->sense_data = SS_INVALID_COMMAND;
1848 return -EINVAL;
1849 }
1850
1851 prevent = fsg->cmnd[4] & 0x01;
1852 if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
1853 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1854 return -EINVAL;
1855 }
1856
1857 if (curlun->prevent_medium_removal && !prevent)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001858 fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 curlun->prevent_medium_removal = prevent;
1860 return 0;
1861}
1862
1863
1864static int do_read_format_capacities(struct fsg_dev *fsg,
1865 struct fsg_buffhd *bh)
1866{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001867 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 u8 *buf = (u8 *) bh->buf;
1869
1870 buf[0] = buf[1] = buf[2] = 0;
1871 buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
1872 buf += 4;
1873
Alan Stern604eb892009-04-27 13:19:41 -04001874 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1875 /* Number of blocks */
Peiyu Li3f565a32011-08-17 22:52:59 -07001876 put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
Alan Stern604eb892009-04-27 13:19:41 -04001877 buf[4] = 0x02; /* Current capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return 12;
1879}
1880
1881
1882static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1883{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001884 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /* We don't support MODE SELECT */
1887 curlun->sense_data = SS_INVALID_COMMAND;
1888 return -EINVAL;
1889}
1890
1891
1892/*-------------------------------------------------------------------------*/
1893
1894static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1895{
1896 int rc;
1897
1898 rc = fsg_set_halt(fsg, fsg->bulk_in);
1899 if (rc == -EAGAIN)
1900 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1901 while (rc != 0) {
1902 if (rc != -EAGAIN) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001903 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 rc = 0;
1905 break;
1906 }
1907
1908 /* Wait for a short time and then try again */
1909 if (msleep_interruptible(100) != 0)
1910 return -EINTR;
1911 rc = usb_ep_set_halt(fsg->bulk_in);
1912 }
1913 return rc;
1914}
1915
David Lopo62fd2ca2008-04-29 10:14:38 +01001916static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1917{
1918 int rc;
1919
1920 DBG(fsg, "bulk-in set wedge\n");
1921 rc = usb_ep_set_wedge(fsg->bulk_in);
1922 if (rc == -EAGAIN)
1923 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1924 while (rc != 0) {
1925 if (rc != -EAGAIN) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001926 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
David Lopo62fd2ca2008-04-29 10:14:38 +01001927 rc = 0;
1928 break;
1929 }
1930
1931 /* Wait for a short time and then try again */
1932 if (msleep_interruptible(100) != 0)
1933 return -EINTR;
1934 rc = usb_ep_set_wedge(fsg->bulk_in);
1935 }
1936 return rc;
1937}
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939static int throw_away_data(struct fsg_dev *fsg)
1940{
1941 struct fsg_buffhd *bh;
1942 u32 amount;
1943 int rc;
1944
1945 while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
1946 fsg->usb_amount_left > 0) {
1947
1948 /* Throw away the data in a filled buffer */
1949 if (bh->state == BUF_STATE_FULL) {
Alan Sterna21d4fe2005-11-29 12:04:24 -05001950 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 bh->state = BUF_STATE_EMPTY;
1952 fsg->next_buffhd_to_drain = bh->next;
1953
1954 /* A short packet or an error ends everything */
1955 if (bh->outreq->actual != bh->outreq->length ||
1956 bh->outreq->status != 0) {
1957 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
1958 return -EINTR;
1959 }
1960 continue;
1961 }
1962
1963 /* Try to submit another request if we need one */
1964 bh = fsg->next_buffhd_to_fill;
1965 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
1966 amount = min(fsg->usb_amount_left,
1967 (u32) mod_data.buflen);
1968
Alan Stern04eee252011-08-18 14:29:00 -04001969 /* Except at the end of the transfer, amount will be
1970 * equal to the buffer size, which is divisible by
1971 * the bulk-out maxpacket size.
1972 */
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -07001973 bh->outreq->length = bh->bulk_out_intended_length =
1974 amount;
Alan Stern70ffe6e2006-03-23 15:05:16 -05001975 bh->outreq->short_not_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1977 &bh->outreq_busy, &bh->state);
1978 fsg->next_buffhd_to_fill = bh->next;
1979 fsg->usb_amount_left -= amount;
1980 continue;
1981 }
1982
1983 /* Otherwise wait for something to happen */
Alan Stern79a7d9e2007-08-08 17:10:11 -04001984 rc = sleep_thread(fsg);
1985 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return rc;
1987 }
1988 return 0;
1989}
1990
1991
1992static int finish_reply(struct fsg_dev *fsg)
1993{
1994 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1995 int rc = 0;
1996
1997 switch (fsg->data_dir) {
1998 case DATA_DIR_NONE:
1999 break; // Nothing to send
2000
2001 /* If we don't know whether the host wants to read or write,
2002 * this must be CB or CBI with an unknown command. We mustn't
2003 * try to send or receive any data. So stall both bulk pipes
2004 * if we can and wait for a reset. */
2005 case DATA_DIR_UNKNOWN:
2006 if (mod_data.can_stall) {
2007 fsg_set_halt(fsg, fsg->bulk_out);
2008 rc = halt_bulk_in_endpoint(fsg);
2009 }
2010 break;
2011
2012 /* All but the last buffer of data must have already been sent */
2013 case DATA_DIR_TO_HOST:
2014 if (fsg->data_size == 0)
2015 ; // Nothing to send
2016
2017 /* If there's no residue, simply send the last buffer */
2018 else if (fsg->residue == 0) {
2019 bh->inreq->zero = 0;
2020 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2021 &bh->inreq_busy, &bh->state);
2022 fsg->next_buffhd_to_fill = bh->next;
2023 }
2024
2025 /* There is a residue. For CB and CBI, simply mark the end
2026 * of the data with a short packet. However, if we are
2027 * allowed to stall, there was no data at all (residue ==
2028 * data_size), and the command failed (invalid LUN or
2029 * sense data is set), then halt the bulk-in endpoint
2030 * instead. */
2031 else if (!transport_is_bbb()) {
2032 if (mod_data.can_stall &&
2033 fsg->residue == fsg->data_size &&
2034 (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
2035 bh->state = BUF_STATE_EMPTY;
2036 rc = halt_bulk_in_endpoint(fsg);
2037 } else {
2038 bh->inreq->zero = 1;
2039 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2040 &bh->inreq_busy, &bh->state);
2041 fsg->next_buffhd_to_fill = bh->next;
2042 }
2043 }
2044
Alan Sternee81b3e2011-03-25 11:46:27 -04002045 /*
2046 * For Bulk-only, mark the end of the data with a short
2047 * packet. If we are allowed to stall, halt the bulk-in
2048 * endpoint. (Note: This violates the Bulk-Only Transport
2049 * specification, which requires us to pad the data if we
2050 * don't halt the endpoint. Presumably nobody will mind.)
2051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 else {
Alan Sternee81b3e2011-03-25 11:46:27 -04002053 bh->inreq->zero = 1;
2054 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2055 &bh->inreq_busy, &bh->state);
2056 fsg->next_buffhd_to_fill = bh->next;
2057 if (mod_data.can_stall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 rc = halt_bulk_in_endpoint(fsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 break;
2061
2062 /* We have processed all we want from the data the host has sent.
2063 * There may still be outstanding bulk-out requests. */
2064 case DATA_DIR_FROM_HOST:
2065 if (fsg->residue == 0)
2066 ; // Nothing to receive
2067
2068 /* Did the host stop sending unexpectedly early? */
2069 else if (fsg->short_packet_received) {
2070 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2071 rc = -EINTR;
2072 }
2073
2074 /* We haven't processed all the incoming data. Even though
2075 * we may be allowed to stall, doing so would cause a race.
2076 * The controller may already have ACK'ed all the remaining
2077 * bulk-out packets, in which case the host wouldn't see a
2078 * STALL. Not realizing the endpoint was halted, it wouldn't
2079 * clear the halt -- leading to problems later on. */
2080#if 0
2081 else if (mod_data.can_stall) {
2082 fsg_set_halt(fsg, fsg->bulk_out);
2083 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2084 rc = -EINTR;
2085 }
2086#endif
2087
2088 /* We can't stall. Read in the excess data and throw it
2089 * all away. */
2090 else
2091 rc = throw_away_data(fsg);
2092 break;
2093 }
2094 return rc;
2095}
2096
2097
2098static int send_status(struct fsg_dev *fsg)
2099{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002100 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct fsg_buffhd *bh;
2102 int rc;
2103 u8 status = USB_STATUS_PASS;
2104 u32 sd, sdinfo = 0;
2105
2106 /* Wait for the next buffer to become available */
2107 bh = fsg->next_buffhd_to_fill;
2108 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002109 rc = sleep_thread(fsg);
2110 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return rc;
2112 }
2113
2114 if (curlun) {
2115 sd = curlun->sense_data;
2116 sdinfo = curlun->sense_data_info;
2117 } else if (fsg->bad_lun_okay)
2118 sd = SS_NO_SENSE;
2119 else
2120 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2121
2122 if (fsg->phase_error) {
2123 DBG(fsg, "sending phase-error status\n");
2124 status = USB_STATUS_PHASE_ERROR;
2125 sd = SS_INVALID_COMMAND;
2126 } else if (sd != SS_NO_SENSE) {
2127 DBG(fsg, "sending command-failure status\n");
2128 status = USB_STATUS_FAIL;
2129 VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2130 " info x%x\n",
2131 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
2132 }
2133
2134 if (transport_is_bbb()) {
John Daiker7ca46b82006-12-29 19:02:06 -08002135 struct bulk_cs_wrap *csw = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 /* Store and send the Bulk-only CSW */
Harvey Harrison551509d2009-02-11 14:11:36 -08002138 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 csw->Tag = fsg->tag;
2140 csw->Residue = cpu_to_le32(fsg->residue);
2141 csw->Status = status;
2142
2143 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
2144 bh->inreq->zero = 0;
2145 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2146 &bh->inreq_busy, &bh->state);
2147
2148 } else if (mod_data.transport_type == USB_PR_CB) {
2149
2150 /* Control-Bulk transport has no status phase! */
2151 return 0;
2152
2153 } else { // USB_PR_CBI
John Daiker7ca46b82006-12-29 19:02:06 -08002154 struct interrupt_data *buf = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /* Store and send the Interrupt data. UFI sends the ASC
2157 * and ASCQ bytes. Everything else sends a Type (which
2158 * is always 0) and the status Value. */
2159 if (mod_data.protocol_type == USB_SC_UFI) {
2160 buf->bType = ASC(sd);
2161 buf->bValue = ASCQ(sd);
2162 } else {
2163 buf->bType = 0;
2164 buf->bValue = status;
2165 }
2166 fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
2167
2168 fsg->intr_buffhd = bh; // Point to the right buffhd
2169 fsg->intreq->buf = bh->inreq->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 fsg->intreq->context = bh;
2171 start_transfer(fsg, fsg->intr_in, fsg->intreq,
2172 &fsg->intreq_busy, &bh->state);
2173 }
2174
2175 fsg->next_buffhd_to_fill = bh->next;
2176 return 0;
2177}
2178
2179
2180/*-------------------------------------------------------------------------*/
2181
2182/* Check whether the command is properly formed and whether its data size
2183 * and direction agree with the values we already have. */
2184static int check_command(struct fsg_dev *fsg, int cmnd_size,
2185 enum data_direction data_dir, unsigned int mask,
2186 int needs_medium, const char *name)
2187{
2188 int i;
2189 int lun = fsg->cmnd[1] >> 5;
2190 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
2191 char hdlen[20];
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002192 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 /* Adjust the expected cmnd_size for protocol encapsulation padding.
2195 * Transparent SCSI doesn't pad. */
2196 if (protocol_is_scsi())
2197 ;
2198
2199 /* There's some disagreement as to whether RBC pads commands or not.
2200 * We'll play it safe and accept either form. */
2201 else if (mod_data.protocol_type == USB_SC_RBC) {
2202 if (fsg->cmnd_size == 12)
2203 cmnd_size = 12;
2204
2205 /* All the other protocols pad to 12 bytes */
2206 } else
2207 cmnd_size = 12;
2208
2209 hdlen[0] = 0;
2210 if (fsg->data_dir != DATA_DIR_UNKNOWN)
2211 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
2212 fsg->data_size);
2213 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
2214 name, cmnd_size, dirletter[(int) data_dir],
2215 fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
2216
2217 /* We can't reply at all until we know the correct data direction
2218 * and size. */
2219 if (fsg->data_size_from_cmnd == 0)
2220 data_dir = DATA_DIR_NONE;
2221 if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
2222 fsg->data_dir = data_dir;
2223 fsg->data_size = fsg->data_size_from_cmnd;
2224
2225 } else { // Bulk-only
2226 if (fsg->data_size < fsg->data_size_from_cmnd) {
2227
2228 /* Host data size < Device data size is a phase error.
2229 * Carry out the command, but only transfer as much
2230 * as we are allowed. */
2231 fsg->data_size_from_cmnd = fsg->data_size;
2232 fsg->phase_error = 1;
2233 }
2234 }
2235 fsg->residue = fsg->usb_amount_left = fsg->data_size;
2236
2237 /* Conflicting data directions is a phase error */
2238 if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
2239 fsg->phase_error = 1;
2240 return -EINVAL;
2241 }
2242
2243 /* Verify the length of the command itself */
2244 if (cmnd_size != fsg->cmnd_size) {
2245
Felipe Balbi549c41e2008-09-19 02:00:02 +03002246 /* Special case workaround: There are plenty of buggy SCSI
2247 * implementations. Many have issues with cbw->Length
2248 * field passing a wrong command size. For those cases we
2249 * always try to work around the problem by using the length
2250 * sent by the host side provided it is at least as large
2251 * as the correct command length.
2252 * Examples of such cases would be MS-Windows, which issues
2253 * REQUEST SENSE with cbw->Length == 12 where it should
2254 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
2255 * REQUEST SENSE with cbw->Length == 10 where it should
2256 * be 6 as well.
2257 */
2258 if (cmnd_size <= fsg->cmnd_size) {
2259 DBG(fsg, "%s is buggy! Expected length %d "
2260 "but we got %d\n", name,
2261 cmnd_size, fsg->cmnd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 cmnd_size = fsg->cmnd_size;
Felipe Balbi549c41e2008-09-19 02:00:02 +03002263 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 fsg->phase_error = 1;
2265 return -EINVAL;
2266 }
2267 }
2268
Alan Sternd794ac72005-04-18 12:43:25 -04002269 /* Check that the LUN values are consistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (transport_is_bbb()) {
2271 if (fsg->lun != lun)
2272 DBG(fsg, "using LUN %d from CBW, "
2273 "not LUN %d from CDB\n",
2274 fsg->lun, lun);
2275 } else
2276 fsg->lun = lun; // Use LUN from the command
2277
2278 /* Check the LUN */
Maxin B John849426c2011-05-08 15:56:17 +03002279 if (fsg->lun < fsg->nluns) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 fsg->curlun = curlun = &fsg->luns[fsg->lun];
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002281 if (fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 curlun->sense_data = SS_NO_SENSE;
2283 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04002284 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
2286 } else {
2287 fsg->curlun = curlun = NULL;
2288 fsg->bad_lun_okay = 0;
2289
2290 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2291 * to use unsupported LUNs; all others may not. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002292 if (fsg->cmnd[0] != INQUIRY &&
2293 fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 DBG(fsg, "unsupported LUN %d\n", fsg->lun);
2295 return -EINVAL;
2296 }
2297 }
2298
2299 /* If a unit attention condition exists, only INQUIRY and
2300 * REQUEST SENSE commands are allowed; anything else must fail. */
2301 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002302 fsg->cmnd[0] != INQUIRY &&
2303 fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 curlun->sense_data = curlun->unit_attention_data;
2305 curlun->unit_attention_data = SS_NO_SENSE;
2306 return -EINVAL;
2307 }
2308
2309 /* Check that only command bytes listed in the mask are non-zero */
2310 fsg->cmnd[1] &= 0x1f; // Mask away the LUN
2311 for (i = 1; i < cmnd_size; ++i) {
2312 if (fsg->cmnd[i] && !(mask & (1 << i))) {
2313 if (curlun)
2314 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2315 return -EINVAL;
2316 }
2317 }
2318
2319 /* If the medium isn't mounted and the command needs to access
2320 * it, return an error. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002321 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2323 return -EINVAL;
2324 }
2325
2326 return 0;
2327}
2328
2329
2330static int do_scsi_command(struct fsg_dev *fsg)
2331{
2332 struct fsg_buffhd *bh;
2333 int rc;
2334 int reply = -EINVAL;
2335 int i;
2336 static char unknown[16];
2337
2338 dump_cdb(fsg);
2339
2340 /* Wait for the next buffer to become available for data or status */
2341 bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
2342 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002343 rc = sleep_thread(fsg);
2344 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 fsg->phase_error = 0;
2348 fsg->short_packet_received = 0;
2349
2350 down_read(&fsg->filesem); // We're using the backing file
2351 switch (fsg->cmnd[0]) {
2352
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002353 case INQUIRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 fsg->data_size_from_cmnd = fsg->cmnd[4];
2355 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2356 (1<<4), 0,
2357 "INQUIRY")) == 0)
2358 reply = do_inquiry(fsg, bh);
2359 break;
2360
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002361 case MODE_SELECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 fsg->data_size_from_cmnd = fsg->cmnd[4];
2363 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2364 (1<<1) | (1<<4), 0,
2365 "MODE SELECT(6)")) == 0)
2366 reply = do_mode_select(fsg, bh);
2367 break;
2368
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002369 case MODE_SELECT_10:
Alan Stern604eb892009-04-27 13:19:41 -04002370 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2372 (1<<1) | (3<<7), 0,
2373 "MODE SELECT(10)")) == 0)
2374 reply = do_mode_select(fsg, bh);
2375 break;
2376
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002377 case MODE_SENSE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 fsg->data_size_from_cmnd = fsg->cmnd[4];
2379 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2380 (1<<1) | (1<<2) | (1<<4), 0,
2381 "MODE SENSE(6)")) == 0)
2382 reply = do_mode_sense(fsg, bh);
2383 break;
2384
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002385 case MODE_SENSE_10:
Alan Stern604eb892009-04-27 13:19:41 -04002386 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2388 (1<<1) | (1<<2) | (3<<7), 0,
2389 "MODE SENSE(10)")) == 0)
2390 reply = do_mode_sense(fsg, bh);
2391 break;
2392
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002393 case ALLOW_MEDIUM_REMOVAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 fsg->data_size_from_cmnd = 0;
2395 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2396 (1<<4), 0,
2397 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
2398 reply = do_prevent_allow(fsg);
2399 break;
2400
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002401 case READ_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 i = fsg->cmnd[4];
Peiyu Li3f565a32011-08-17 22:52:59 -07002403 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2405 (7<<1) | (1<<4), 1,
2406 "READ(6)")) == 0)
2407 reply = do_read(fsg);
2408 break;
2409
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002410 case READ_10:
Alan Stern604eb892009-04-27 13:19:41 -04002411 fsg->data_size_from_cmnd =
Peiyu Li3f565a32011-08-17 22:52:59 -07002412 get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2414 (1<<1) | (0xf<<2) | (3<<7), 1,
2415 "READ(10)")) == 0)
2416 reply = do_read(fsg);
2417 break;
2418
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002419 case READ_12:
Alan Stern604eb892009-04-27 13:19:41 -04002420 fsg->data_size_from_cmnd =
Peiyu Li3f565a32011-08-17 22:52:59 -07002421 get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2423 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2424 "READ(12)")) == 0)
2425 reply = do_read(fsg);
2426 break;
2427
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002428 case READ_CAPACITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 fsg->data_size_from_cmnd = 8;
2430 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2431 (0xf<<2) | (1<<8), 1,
2432 "READ CAPACITY")) == 0)
2433 reply = do_read_capacity(fsg, bh);
2434 break;
2435
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002436 case READ_HEADER:
Alan Stern12aae682008-11-20 14:13:12 -05002437 if (!mod_data.cdrom)
2438 goto unknown_cmnd;
Alan Stern604eb892009-04-27 13:19:41 -04002439 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Alan Stern12aae682008-11-20 14:13:12 -05002440 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2441 (3<<7) | (0x1f<<1), 1,
2442 "READ HEADER")) == 0)
2443 reply = do_read_header(fsg, bh);
2444 break;
2445
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002446 case READ_TOC:
Alan Stern12aae682008-11-20 14:13:12 -05002447 if (!mod_data.cdrom)
2448 goto unknown_cmnd;
Alan Stern604eb892009-04-27 13:19:41 -04002449 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Alan Stern12aae682008-11-20 14:13:12 -05002450 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2451 (7<<6) | (1<<1), 1,
2452 "READ TOC")) == 0)
2453 reply = do_read_toc(fsg, bh);
2454 break;
2455
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002456 case READ_FORMAT_CAPACITIES:
Alan Stern604eb892009-04-27 13:19:41 -04002457 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2459 (3<<7), 1,
2460 "READ FORMAT CAPACITIES")) == 0)
2461 reply = do_read_format_capacities(fsg, bh);
2462 break;
2463
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002464 case REQUEST_SENSE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 fsg->data_size_from_cmnd = fsg->cmnd[4];
2466 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2467 (1<<4), 0,
2468 "REQUEST SENSE")) == 0)
2469 reply = do_request_sense(fsg, bh);
2470 break;
2471
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002472 case START_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 fsg->data_size_from_cmnd = 0;
2474 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2475 (1<<1) | (1<<4), 0,
2476 "START-STOP UNIT")) == 0)
2477 reply = do_start_stop(fsg);
2478 break;
2479
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002480 case SYNCHRONIZE_CACHE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 fsg->data_size_from_cmnd = 0;
2482 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2483 (0xf<<2) | (3<<7), 1,
2484 "SYNCHRONIZE CACHE")) == 0)
2485 reply = do_synchronize_cache(fsg);
2486 break;
2487
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002488 case TEST_UNIT_READY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 fsg->data_size_from_cmnd = 0;
2490 reply = check_command(fsg, 6, DATA_DIR_NONE,
2491 0, 1,
2492 "TEST UNIT READY");
2493 break;
2494
2495 /* Although optional, this command is used by MS-Windows. We
2496 * support a minimal version: BytChk must be 0. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002497 case VERIFY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 fsg->data_size_from_cmnd = 0;
2499 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2500 (1<<1) | (0xf<<2) | (3<<7), 1,
2501 "VERIFY")) == 0)
2502 reply = do_verify(fsg);
2503 break;
2504
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002505 case WRITE_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 i = fsg->cmnd[4];
Peiyu Li3f565a32011-08-17 22:52:59 -07002507 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2509 (7<<1) | (1<<4), 1,
2510 "WRITE(6)")) == 0)
2511 reply = do_write(fsg);
2512 break;
2513
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002514 case WRITE_10:
Alan Stern604eb892009-04-27 13:19:41 -04002515 fsg->data_size_from_cmnd =
Peiyu Li3f565a32011-08-17 22:52:59 -07002516 get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2518 (1<<1) | (0xf<<2) | (3<<7), 1,
2519 "WRITE(10)")) == 0)
2520 reply = do_write(fsg);
2521 break;
2522
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002523 case WRITE_12:
Alan Stern604eb892009-04-27 13:19:41 -04002524 fsg->data_size_from_cmnd =
Peiyu Li3f565a32011-08-17 22:52:59 -07002525 get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
2527 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2528 "WRITE(12)")) == 0)
2529 reply = do_write(fsg);
2530 break;
2531
2532 /* Some mandatory commands that we recognize but don't implement.
2533 * They don't mean much in this setting. It's left as an exercise
2534 * for anyone interested to implement RESERVE and RELEASE in terms
2535 * of Posix locks. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002536 case FORMAT_UNIT:
2537 case RELEASE:
2538 case RESERVE:
2539 case SEND_DIAGNOSTIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 // Fall through
2541
2542 default:
Alan Stern12aae682008-11-20 14:13:12 -05002543 unknown_cmnd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 fsg->data_size_from_cmnd = 0;
2545 sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
2546 if ((reply = check_command(fsg, fsg->cmnd_size,
2547 DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) {
2548 fsg->curlun->sense_data = SS_INVALID_COMMAND;
2549 reply = -EINVAL;
2550 }
2551 break;
2552 }
2553 up_read(&fsg->filesem);
2554
2555 if (reply == -EINTR || signal_pending(current))
2556 return -EINTR;
2557
2558 /* Set up the single reply buffer for finish_reply() */
2559 if (reply == -EINVAL)
2560 reply = 0; // Error reply length
2561 if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
2562 reply = min((u32) reply, fsg->data_size_from_cmnd);
2563 bh->inreq->length = reply;
2564 bh->state = BUF_STATE_FULL;
2565 fsg->residue -= reply;
2566 } // Otherwise it's already set
2567
2568 return 0;
2569}
2570
2571
2572/*-------------------------------------------------------------------------*/
2573
2574static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2575{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002576 struct usb_request *req = bh->outreq;
2577 struct fsg_bulk_cb_wrap *cbw = req->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Alan Sternb950bdb2008-04-14 11:45:29 -04002579 /* Was this a real packet? Should it be ignored? */
2580 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return -EINVAL;
2582
2583 /* Is the CBW valid? */
2584 if (req->actual != USB_BULK_CB_WRAP_LEN ||
Harvey Harrison551509d2009-02-11 14:11:36 -08002585 cbw->Signature != cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 USB_BULK_CB_SIG)) {
2587 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2588 req->actual,
2589 le32_to_cpu(cbw->Signature));
2590
Alan Sternb950bdb2008-04-14 11:45:29 -04002591 /* The Bulk-only spec says we MUST stall the IN endpoint
2592 * (6.6.1), so it's unavoidable. It also says we must
2593 * retain this state until the next reset, but there's
2594 * no way to tell the controller driver it should ignore
2595 * Clear-Feature(HALT) requests.
2596 *
2597 * We aren't required to halt the OUT endpoint; instead
2598 * we can simply accept and discard any data received
2599 * until the next reset. */
David Lopo62fd2ca2008-04-29 10:14:38 +01002600 wedge_bulk_in_endpoint(fsg);
Alan Sternb950bdb2008-04-14 11:45:29 -04002601 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return -EINVAL;
2603 }
2604
2605 /* Is the CBW meaningful? */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002606 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
Alan Stern12943f02007-08-24 16:27:50 -04002607 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2609 "cmdlen %u\n",
2610 cbw->Lun, cbw->Flags, cbw->Length);
2611
2612 /* We can do anything we want here, so let's stall the
2613 * bulk pipes if we are allowed to. */
2614 if (mod_data.can_stall) {
2615 fsg_set_halt(fsg, fsg->bulk_out);
2616 halt_bulk_in_endpoint(fsg);
2617 }
2618 return -EINVAL;
2619 }
2620
2621 /* Save the command for later */
2622 fsg->cmnd_size = cbw->Length;
2623 memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
2624 if (cbw->Flags & USB_BULK_IN_FLAG)
2625 fsg->data_dir = DATA_DIR_TO_HOST;
2626 else
2627 fsg->data_dir = DATA_DIR_FROM_HOST;
2628 fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
2629 if (fsg->data_size == 0)
2630 fsg->data_dir = DATA_DIR_NONE;
2631 fsg->lun = cbw->Lun;
2632 fsg->tag = cbw->Tag;
2633 return 0;
2634}
2635
2636
2637static int get_next_command(struct fsg_dev *fsg)
2638{
2639 struct fsg_buffhd *bh;
2640 int rc = 0;
2641
2642 if (transport_is_bbb()) {
2643
2644 /* Wait for the next buffer to become available */
2645 bh = fsg->next_buffhd_to_fill;
2646 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002647 rc = sleep_thread(fsg);
2648 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 /* Queue a request to read a Bulk-only CBW */
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -07002653 set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN);
2654 bh->outreq->short_not_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 start_transfer(fsg, fsg->bulk_out, bh->outreq,
2656 &bh->outreq_busy, &bh->state);
2657
2658 /* We will drain the buffer in software, which means we
2659 * can reuse it for the next filling. No need to advance
2660 * next_buffhd_to_fill. */
2661
2662 /* Wait for the CBW to arrive */
2663 while (bh->state != BUF_STATE_FULL) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002664 rc = sleep_thread(fsg);
2665 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002667 }
Alan Sterna21d4fe2005-11-29 12:04:24 -05002668 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 rc = received_cbw(fsg, bh);
2670 bh->state = BUF_STATE_EMPTY;
2671
2672 } else { // USB_PR_CB or USB_PR_CBI
2673
2674 /* Wait for the next command to arrive */
2675 while (fsg->cbbuf_cmnd_size == 0) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002676 rc = sleep_thread(fsg);
2677 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 /* Is the previous status interrupt request still busy?
2682 * The host is allowed to skip reading the status,
2683 * so we must cancel it. */
2684 if (fsg->intreq_busy)
2685 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2686
2687 /* Copy the command and mark the buffer empty */
2688 fsg->data_dir = DATA_DIR_UNKNOWN;
2689 spin_lock_irq(&fsg->lock);
2690 fsg->cmnd_size = fsg->cbbuf_cmnd_size;
2691 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
2692 fsg->cbbuf_cmnd_size = 0;
2693 spin_unlock_irq(&fsg->lock);
2694 }
2695 return rc;
2696}
2697
2698
2699/*-------------------------------------------------------------------------*/
2700
2701static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
2702 const struct usb_endpoint_descriptor *d)
2703{
2704 int rc;
2705
2706 ep->driver_data = fsg;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03002707 ep->desc = d;
2708 rc = usb_ep_enable(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (rc)
2710 ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
2711 return rc;
2712}
2713
2714static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
2715 struct usb_request **preq)
2716{
2717 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2718 if (*preq)
2719 return 0;
2720 ERROR(fsg, "can't allocate request for %s\n", ep->name);
2721 return -ENOMEM;
2722}
2723
2724/*
2725 * Reset interface setting and re-init endpoint state (toggle etc).
2726 * Call with altsetting < 0 to disable the interface. The only other
2727 * available altsetting is 0, which enables the interface.
2728 */
2729static int do_set_interface(struct fsg_dev *fsg, int altsetting)
2730{
2731 int rc = 0;
2732 int i;
2733 const struct usb_endpoint_descriptor *d;
2734
2735 if (fsg->running)
2736 DBG(fsg, "reset interface\n");
2737
2738reset:
2739 /* Deallocate the requests */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002740 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 struct fsg_buffhd *bh = &fsg->buffhds[i];
2742
2743 if (bh->inreq) {
2744 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2745 bh->inreq = NULL;
2746 }
2747 if (bh->outreq) {
2748 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2749 bh->outreq = NULL;
2750 }
2751 }
2752 if (fsg->intreq) {
2753 usb_ep_free_request(fsg->intr_in, fsg->intreq);
2754 fsg->intreq = NULL;
2755 }
2756
2757 /* Disable the endpoints */
2758 if (fsg->bulk_in_enabled) {
2759 usb_ep_disable(fsg->bulk_in);
2760 fsg->bulk_in_enabled = 0;
2761 }
2762 if (fsg->bulk_out_enabled) {
2763 usb_ep_disable(fsg->bulk_out);
2764 fsg->bulk_out_enabled = 0;
2765 }
2766 if (fsg->intr_in_enabled) {
2767 usb_ep_disable(fsg->intr_in);
2768 fsg->intr_in_enabled = 0;
2769 }
2770
2771 fsg->running = 0;
2772 if (altsetting < 0 || rc != 0)
2773 return rc;
2774
2775 DBG(fsg, "set interface %d\n", altsetting);
2776
2777 /* Enable the endpoints */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002778 d = fsg_ep_desc(fsg->gadget,
2779 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
2781 goto reset;
2782 fsg->bulk_in_enabled = 1;
2783
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002784 d = fsg_ep_desc(fsg->gadget,
2785 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
2787 goto reset;
2788 fsg->bulk_out_enabled = 1;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002789 fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
Alan Sternb950bdb2008-04-14 11:45:29 -04002790 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 if (transport_is_cbi()) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002793 d = fsg_ep_desc(fsg->gadget,
2794 &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
2796 goto reset;
2797 fsg->intr_in_enabled = 1;
2798 }
2799
2800 /* Allocate the requests */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002801 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct fsg_buffhd *bh = &fsg->buffhds[i];
2803
2804 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
2805 goto reset;
2806 if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
2807 goto reset;
2808 bh->inreq->buf = bh->outreq->buf = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 bh->inreq->context = bh->outreq->context = bh;
2810 bh->inreq->complete = bulk_in_complete;
2811 bh->outreq->complete = bulk_out_complete;
2812 }
2813 if (transport_is_cbi()) {
2814 if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
2815 goto reset;
2816 fsg->intreq->complete = intr_in_complete;
2817 }
2818
2819 fsg->running = 1;
2820 for (i = 0; i < fsg->nluns; ++i)
2821 fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2822 return rc;
2823}
2824
2825
2826/*
2827 * Change our operational configuration. This code must agree with the code
2828 * that returns config descriptors, and with interface altsetting code.
2829 *
2830 * It's also responsible for power management interactions. Some
2831 * configurations might not work with our current power sources.
2832 * For now we just assume the gadget is always self-powered.
2833 */
2834static int do_set_config(struct fsg_dev *fsg, u8 new_config)
2835{
2836 int rc = 0;
2837
2838 /* Disable the single interface */
2839 if (fsg->config != 0) {
2840 DBG(fsg, "reset config\n");
2841 fsg->config = 0;
2842 rc = do_set_interface(fsg, -1);
2843 }
2844
2845 /* Enable the interface */
2846 if (new_config != 0) {
2847 fsg->config = new_config;
2848 if ((rc = do_set_interface(fsg, 0)) != 0)
2849 fsg->config = 0; // Reset on errors
2850 else {
2851 char *speed;
2852
2853 switch (fsg->gadget->speed) {
2854 case USB_SPEED_LOW: speed = "low"; break;
2855 case USB_SPEED_FULL: speed = "full"; break;
2856 case USB_SPEED_HIGH: speed = "high"; break;
2857 default: speed = "?"; break;
2858 }
2859 INFO(fsg, "%s speed config #%d\n", speed, fsg->config);
2860 }
2861 }
2862 return rc;
2863}
2864
2865
2866/*-------------------------------------------------------------------------*/
2867
2868static void handle_exception(struct fsg_dev *fsg)
2869{
2870 siginfo_t info;
2871 int sig;
2872 int i;
2873 int num_active;
2874 struct fsg_buffhd *bh;
2875 enum fsg_state old_state;
2876 u8 new_config;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002877 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 unsigned int exception_req_tag;
2879 int rc;
2880
2881 /* Clear the existing signals. Anything but SIGUSR1 is converted
2882 * into a high-priority EXIT exception. */
2883 for (;;) {
Oleg Nesterov8cfbe7e2007-05-30 11:06:33 -04002884 sig = dequeue_signal_lock(current, &current->blocked, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (!sig)
2886 break;
2887 if (sig != SIGUSR1) {
2888 if (fsg->state < FSG_STATE_EXIT)
2889 DBG(fsg, "Main thread exiting on signal\n");
2890 raise_exception(fsg, FSG_STATE_EXIT);
2891 }
2892 }
2893
2894 /* Cancel all the pending transfers */
2895 if (fsg->intreq_busy)
2896 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002897 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 bh = &fsg->buffhds[i];
2899 if (bh->inreq_busy)
2900 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
2901 if (bh->outreq_busy)
2902 usb_ep_dequeue(fsg->bulk_out, bh->outreq);
2903 }
2904
2905 /* Wait until everything is idle */
2906 for (;;) {
2907 num_active = fsg->intreq_busy;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002908 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 bh = &fsg->buffhds[i];
2910 num_active += bh->inreq_busy + bh->outreq_busy;
2911 }
2912 if (num_active == 0)
2913 break;
2914 if (sleep_thread(fsg))
2915 return;
2916 }
2917
2918 /* Clear out the controller's fifos */
2919 if (fsg->bulk_in_enabled)
2920 usb_ep_fifo_flush(fsg->bulk_in);
2921 if (fsg->bulk_out_enabled)
2922 usb_ep_fifo_flush(fsg->bulk_out);
2923 if (fsg->intr_in_enabled)
2924 usb_ep_fifo_flush(fsg->intr_in);
2925
2926 /* Reset the I/O buffer states and pointers, the SCSI
2927 * state, and the exception. Then invoke the handler. */
2928 spin_lock_irq(&fsg->lock);
2929
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002930 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 bh = &fsg->buffhds[i];
2932 bh->state = BUF_STATE_EMPTY;
2933 }
2934 fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
2935 &fsg->buffhds[0];
2936
2937 exception_req_tag = fsg->exception_req_tag;
2938 new_config = fsg->new_config;
2939 old_state = fsg->state;
2940
2941 if (old_state == FSG_STATE_ABORT_BULK_OUT)
2942 fsg->state = FSG_STATE_STATUS_PHASE;
2943 else {
2944 for (i = 0; i < fsg->nluns; ++i) {
2945 curlun = &fsg->luns[i];
2946 curlun->prevent_medium_removal = 0;
2947 curlun->sense_data = curlun->unit_attention_data =
2948 SS_NO_SENSE;
2949 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04002950 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952 fsg->state = FSG_STATE_IDLE;
2953 }
2954 spin_unlock_irq(&fsg->lock);
2955
2956 /* Carry out any extra actions required for the exception */
2957 switch (old_state) {
2958 default:
2959 break;
2960
2961 case FSG_STATE_ABORT_BULK_OUT:
2962 send_status(fsg);
2963 spin_lock_irq(&fsg->lock);
2964 if (fsg->state == FSG_STATE_STATUS_PHASE)
2965 fsg->state = FSG_STATE_IDLE;
2966 spin_unlock_irq(&fsg->lock);
2967 break;
2968
2969 case FSG_STATE_RESET:
2970 /* In case we were forced against our will to halt a
2971 * bulk endpoint, clear the halt now. (The SuperH UDC
2972 * requires this.) */
Alan Sternb950bdb2008-04-14 11:45:29 -04002973 if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 usb_ep_clear_halt(fsg->bulk_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 if (transport_is_bbb()) {
2977 if (fsg->ep0_req_tag == exception_req_tag)
2978 ep0_queue(fsg); // Complete the status stage
2979
2980 } else if (transport_is_cbi())
2981 send_status(fsg); // Status by interrupt pipe
2982
2983 /* Technically this should go here, but it would only be
2984 * a waste of time. Ditto for the INTERFACE_CHANGE and
2985 * CONFIG_CHANGE cases. */
2986 // for (i = 0; i < fsg->nluns; ++i)
2987 // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2988 break;
2989
2990 case FSG_STATE_INTERFACE_CHANGE:
2991 rc = do_set_interface(fsg, 0);
2992 if (fsg->ep0_req_tag != exception_req_tag)
2993 break;
2994 if (rc != 0) // STALL on errors
2995 fsg_set_halt(fsg, fsg->ep0);
2996 else // Complete the status stage
2997 ep0_queue(fsg);
2998 break;
2999
3000 case FSG_STATE_CONFIG_CHANGE:
3001 rc = do_set_config(fsg, new_config);
3002 if (fsg->ep0_req_tag != exception_req_tag)
3003 break;
3004 if (rc != 0) // STALL on errors
3005 fsg_set_halt(fsg, fsg->ep0);
3006 else // Complete the status stage
3007 ep0_queue(fsg);
3008 break;
3009
3010 case FSG_STATE_DISCONNECT:
Michal Nazarewiczb6058d02009-10-28 16:57:14 +01003011 for (i = 0; i < fsg->nluns; ++i)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003012 fsg_lun_fsync_sub(fsg->luns + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 do_set_config(fsg, 0); // Unconfigured state
3014 break;
3015
3016 case FSG_STATE_EXIT:
3017 case FSG_STATE_TERMINATED:
3018 do_set_config(fsg, 0); // Free resources
3019 spin_lock_irq(&fsg->lock);
3020 fsg->state = FSG_STATE_TERMINATED; // Stop the thread
3021 spin_unlock_irq(&fsg->lock);
3022 break;
3023 }
3024}
3025
3026
3027/*-------------------------------------------------------------------------*/
3028
3029static int fsg_main_thread(void *fsg_)
3030{
John Daiker7ca46b82006-12-29 19:02:06 -08003031 struct fsg_dev *fsg = fsg_;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 /* Allow the thread to be killed by a signal, but set the signal mask
3034 * to block everything but INT, TERM, KILL, and USR1. */
Oleg Nesterov8cfbe7e2007-05-30 11:06:33 -04003035 allow_signal(SIGINT);
3036 allow_signal(SIGTERM);
3037 allow_signal(SIGKILL);
3038 allow_signal(SIGUSR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003040 /* Allow the thread to be frozen */
3041 set_freezable();
3042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 /* Arrange for userspace references to be interpreted as kernel
3044 * pointers. That way we can pass a kernel pointer to a routine
3045 * that expects a __user pointer and it will work okay. */
3046 set_fs(get_ds());
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 /* The main loop */
3049 while (fsg->state != FSG_STATE_TERMINATED) {
3050 if (exception_in_progress(fsg) || signal_pending(current)) {
3051 handle_exception(fsg);
3052 continue;
3053 }
3054
3055 if (!fsg->running) {
3056 sleep_thread(fsg);
3057 continue;
3058 }
3059
3060 if (get_next_command(fsg))
3061 continue;
3062
3063 spin_lock_irq(&fsg->lock);
3064 if (!exception_in_progress(fsg))
3065 fsg->state = FSG_STATE_DATA_PHASE;
3066 spin_unlock_irq(&fsg->lock);
3067
3068 if (do_scsi_command(fsg) || finish_reply(fsg))
3069 continue;
3070
3071 spin_lock_irq(&fsg->lock);
3072 if (!exception_in_progress(fsg))
3073 fsg->state = FSG_STATE_STATUS_PHASE;
3074 spin_unlock_irq(&fsg->lock);
3075
3076 if (send_status(fsg))
3077 continue;
3078
3079 spin_lock_irq(&fsg->lock);
3080 if (!exception_in_progress(fsg))
3081 fsg->state = FSG_STATE_IDLE;
3082 spin_unlock_irq(&fsg->lock);
3083 }
3084
Alan Stern22efcf42005-09-26 16:12:02 -04003085 spin_lock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 fsg->thread_task = NULL;
Alan Stern22efcf42005-09-26 16:12:02 -04003087 spin_unlock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Alan Stern82a10a82009-04-16 15:37:28 -04003089 /* If we are exiting because of a signal, unregister the
3090 * gadget driver. */
3091 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 usb_gadget_unregister_driver(&fsg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 /* Let the unbind and cleanup routines know the thread has exited */
3095 complete_and_exit(&fsg->thread_notifier, 0);
3096}
3097
3098
3099/*-------------------------------------------------------------------------*/
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102/* The write permissions and store_xxx pointers are set in fsg_bind() */
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003103static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003104static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003105static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107
3108/*-------------------------------------------------------------------------*/
3109
Alan Stern87c42522005-11-09 16:59:56 -05003110static void fsg_release(struct kref *ref)
3111{
3112 struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
3113
3114 kfree(fsg->luns);
3115 kfree(fsg);
3116}
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118static void lun_release(struct device *dev)
3119{
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003120 struct rw_semaphore *filesem = dev_get_drvdata(dev);
3121 struct fsg_dev *fsg =
3122 container_of(filesem, struct fsg_dev, filesem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Alan Stern87c42522005-11-09 16:59:56 -05003124 kref_put(&fsg->ref, fsg_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
David Brownella3536782006-07-06 15:48:53 -07003127static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
3129 struct fsg_dev *fsg = get_gadget_data(gadget);
3130 int i;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003131 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 struct usb_request *req = fsg->ep0req;
3133
3134 DBG(fsg, "unbind\n");
3135 clear_bit(REGISTERED, &fsg->atomic_bitflags);
3136
3137 /* Unregister the sysfs attribute files and the LUNs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 for (i = 0; i < fsg->nluns; ++i) {
3139 curlun = &fsg->luns[i];
3140 if (curlun->registered) {
Michal Nazarewicz452ee0e2010-08-12 17:43:50 +02003141 device_remove_file(&curlun->dev, &dev_attr_nofua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 device_remove_file(&curlun->dev, &dev_attr_ro);
3143 device_remove_file(&curlun->dev, &dev_attr_file);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003144 fsg_lun_close(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 device_unregister(&curlun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 curlun->registered = 0;
3147 }
3148 }
3149
3150 /* If the thread isn't already dead, tell it to exit now */
3151 if (fsg->state != FSG_STATE_TERMINATED) {
3152 raise_exception(fsg, FSG_STATE_EXIT);
3153 wait_for_completion(&fsg->thread_notifier);
3154
3155 /* The cleanup routine waits for this completion also */
3156 complete(&fsg->thread_notifier);
3157 }
3158
3159 /* Free the data buffers */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003160 for (i = 0; i < FSG_NUM_BUFFERS; ++i)
David Brownell9d8bab52007-07-01 11:04:54 -07003161 kfree(fsg->buffhds[i].buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
3163 /* Free the request and buffer for endpoint 0 */
3164 if (req) {
David Brownell9d8bab52007-07-01 11:04:54 -07003165 kfree(req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 usb_ep_free_request(fsg->ep0, req);
3167 }
3168
3169 set_gadget_data(gadget, NULL);
3170}
3171
3172
3173static int __init check_parameters(struct fsg_dev *fsg)
3174{
3175 int prot;
David Brownell91e79c92005-07-13 15:18:30 -07003176 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 /* Store the default values */
3179 mod_data.transport_type = USB_PR_BULK;
3180 mod_data.transport_name = "Bulk-only";
3181 mod_data.protocol_type = USB_SC_SCSI;
3182 mod_data.protocol_name = "Transparent SCSI";
3183
Alan Sternce459ec2009-02-24 16:19:47 -05003184 /* Some peripheral controllers are known not to be able to
3185 * halt bulk endpoints correctly. If one of them is present,
3186 * disable stalls.
3187 */
Christoph Egger90f79762010-02-05 13:24:12 +01003188 if (gadget_is_at91(fsg->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 mod_data.can_stall = 0;
3190
3191 if (mod_data.release == 0xffff) { // Parameter wasn't set
Christoph Egger90f79762010-02-05 13:24:12 +01003192 gcnum = usb_gadget_controller_number(fsg->gadget);
David Brownell91e79c92005-07-13 15:18:30 -07003193 if (gcnum >= 0)
3194 mod_data.release = 0x0300 + gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 else {
Arjan van de Venb6c63932008-07-25 01:45:52 -07003196 WARNING(fsg, "controller '%s' not recognized\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 fsg->gadget->name);
3198 mod_data.release = 0x0399;
3199 }
3200 }
3201
3202 prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
3203
3204#ifdef CONFIG_USB_FILE_STORAGE_TEST
3205 if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
3206 ; // Use default setting
3207 } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
3208 mod_data.transport_type = USB_PR_CB;
3209 mod_data.transport_name = "Control-Bulk";
3210 } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
3211 mod_data.transport_type = USB_PR_CBI;
3212 mod_data.transport_name = "Control-Bulk-Interrupt";
3213 } else {
3214 ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
3215 return -EINVAL;
3216 }
3217
3218 if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
3219 prot == USB_SC_SCSI) {
3220 ; // Use default setting
3221 } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
3222 prot == USB_SC_RBC) {
3223 mod_data.protocol_type = USB_SC_RBC;
3224 mod_data.protocol_name = "RBC";
3225 } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
3226 strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
3227 prot == USB_SC_8020) {
3228 mod_data.protocol_type = USB_SC_8020;
3229 mod_data.protocol_name = "8020i (ATAPI)";
3230 } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
3231 prot == USB_SC_QIC) {
3232 mod_data.protocol_type = USB_SC_QIC;
3233 mod_data.protocol_name = "QIC-157";
3234 } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
3235 prot == USB_SC_UFI) {
3236 mod_data.protocol_type = USB_SC_UFI;
3237 mod_data.protocol_name = "UFI";
3238 } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
3239 prot == USB_SC_8070) {
3240 mod_data.protocol_type = USB_SC_8070;
3241 mod_data.protocol_name = "8070i";
3242 } else {
3243 ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
3244 return -EINVAL;
3245 }
3246
3247 mod_data.buflen &= PAGE_CACHE_MASK;
3248 if (mod_data.buflen <= 0) {
3249 ERROR(fsg, "invalid buflen\n");
3250 return -ETOOSMALL;
3251 }
Yann Cantin87eb1be2010-06-05 23:06:31 +02003252
Michal Nazarewicz56706492010-07-22 14:16:37 +02003253#endif /* CONFIG_USB_FILE_STORAGE_TEST */
3254
Yann Cantin87eb1be2010-06-05 23:06:31 +02003255 /* Serial string handling.
3256 * On a real device, the serial string would be loaded
3257 * from permanent storage. */
Michal Nazarewicz56706492010-07-22 14:16:37 +02003258 if (mod_data.serial) {
Yann Cantin87eb1be2010-06-05 23:06:31 +02003259 const char *ch;
3260 unsigned len = 0;
3261
3262 /* Sanity check :
3263 * The CB[I] specification limits the serial string to
3264 * 12 uppercase hexadecimal characters.
3265 * BBB need at least 12 uppercase hexadecimal characters,
3266 * with a maximum of 126. */
Michal Nazarewicz56706492010-07-22 14:16:37 +02003267 for (ch = mod_data.serial; *ch; ++ch) {
Yann Cantin87eb1be2010-06-05 23:06:31 +02003268 ++len;
3269 if ((*ch < '0' || *ch > '9') &&
3270 (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
3271 WARNING(fsg,
Alan Sternd8087422010-09-03 11:15:41 -04003272 "Invalid serial string character: %c\n",
Yann Cantin87eb1be2010-06-05 23:06:31 +02003273 *ch);
Alan Sternd8087422010-09-03 11:15:41 -04003274 goto no_serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003275 }
3276 }
3277 if (len > 126 ||
3278 (mod_data.transport_type == USB_PR_BULK && len < 12) ||
3279 (mod_data.transport_type != USB_PR_BULK && len > 12)) {
Alan Sternd8087422010-09-03 11:15:41 -04003280 WARNING(fsg, "Invalid serial string length!\n");
3281 goto no_serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003282 }
Michal Nazarewicz56706492010-07-22 14:16:37 +02003283 fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003284 } else {
Alan Sternd8087422010-09-03 11:15:41 -04003285 WARNING(fsg, "No serial-number string provided!\n");
3286 no_serial:
3287 device_desc.iSerialNumber = 0;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003288 }
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return 0;
3291}
3292
3293
Michal Nazarewicze12995e2010-08-12 17:43:52 +02003294static int __init fsg_bind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
3296 struct fsg_dev *fsg = the_fsg;
3297 int rc;
3298 int i;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003299 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct usb_ep *ep;
3301 struct usb_request *req;
3302 char *pathbuf, *p;
3303
3304 fsg->gadget = gadget;
3305 set_gadget_data(gadget, fsg);
3306 fsg->ep0 = gadget->ep0;
3307 fsg->ep0->driver_data = fsg;
3308
3309 if ((rc = check_parameters(fsg)) != 0)
3310 goto out;
3311
3312 if (mod_data.removable) { // Enable the store_xxx attributes
Alan Stern12aae682008-11-20 14:13:12 -05003313 dev_attr_file.attr.mode = 0644;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003314 dev_attr_file.store = fsg_store_file;
Alan Stern12aae682008-11-20 14:13:12 -05003315 if (!mod_data.cdrom) {
3316 dev_attr_ro.attr.mode = 0644;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003317 dev_attr_ro.store = fsg_store_ro;
Alan Stern12aae682008-11-20 14:13:12 -05003318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003321 /* Only for removable media? */
3322 dev_attr_nofua.attr.mode = 0644;
3323 dev_attr_nofua.store = fsg_store_nofua;
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 /* Find out how many LUNs there should be */
3326 i = mod_data.nluns;
3327 if (i == 0)
David Brownell2e806f62007-08-02 00:03:39 -07003328 i = max(mod_data.num_filenames, 1u);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003329 if (i > FSG_MAX_LUNS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 ERROR(fsg, "invalid number of LUNs: %d\n", i);
3331 rc = -EINVAL;
3332 goto out;
3333 }
3334
3335 /* Create the LUNs, open their backing files, and register the
3336 * LUN devices in sysfs. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003337 fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (!fsg->luns) {
3339 rc = -ENOMEM;
3340 goto out;
3341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 fsg->nluns = i;
3343
3344 for (i = 0; i < fsg->nluns; ++i) {
3345 curlun = &fsg->luns[i];
Michal Nazarewicze909ef52009-10-28 16:57:16 +01003346 curlun->cdrom = !!mod_data.cdrom;
3347 curlun->ro = mod_data.cdrom || mod_data.ro[i];
3348 curlun->initially_ro = curlun->ro;
3349 curlun->removable = mod_data.removable;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003350 curlun->nofua = mod_data.nofua[i];
Alan Stern2de9eae2006-09-25 14:31:15 -04003351 curlun->dev.release = lun_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 curlun->dev.parent = &gadget->dev;
3353 curlun->dev.driver = &fsg_driver.driver;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003354 dev_set_drvdata(&curlun->dev, &fsg->filesem);
Kay Sievers0031a062008-05-02 06:02:41 +02003355 dev_set_name(&curlun->dev,"%s-lun%d",
3356 dev_name(&gadget->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003358 kref_get(&fsg->ref);
3359 rc = device_register(&curlun->dev);
3360 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003362 put_device(&curlun->dev);
Alan Stern2de9eae2006-09-25 14:31:15 -04003363 goto out;
3364 }
3365 curlun->registered = 1;
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003366
3367 rc = device_create_file(&curlun->dev, &dev_attr_ro);
3368 if (rc)
3369 goto out;
3370 rc = device_create_file(&curlun->dev, &dev_attr_nofua);
3371 if (rc)
3372 goto out;
3373 rc = device_create_file(&curlun->dev, &dev_attr_file);
3374 if (rc)
3375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Alan Sternaafe5bd2006-03-31 11:46:43 -05003377 if (mod_data.file[i] && *mod_data.file[i]) {
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003378 rc = fsg_lun_open(curlun, mod_data.file[i]);
3379 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 goto out;
3381 } else if (!mod_data.removable) {
3382 ERROR(fsg, "no file given for LUN%d\n", i);
3383 rc = -EINVAL;
3384 goto out;
3385 }
3386 }
3387
3388 /* Find all the endpoints we will use */
3389 usb_ep_autoconfig_reset(gadget);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003390 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 if (!ep)
3392 goto autoconf_fail;
3393 ep->driver_data = fsg; // claim the endpoint
3394 fsg->bulk_in = ep;
3395
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003396 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 if (!ep)
3398 goto autoconf_fail;
3399 ep->driver_data = fsg; // claim the endpoint
3400 fsg->bulk_out = ep;
3401
3402 if (transport_is_cbi()) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003403 ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if (!ep)
3405 goto autoconf_fail;
3406 ep->driver_data = fsg; // claim the endpoint
3407 fsg->intr_in = ep;
3408 }
3409
3410 /* Fix up the descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 device_desc.idVendor = cpu_to_le16(mod_data.vendor);
3412 device_desc.idProduct = cpu_to_le16(mod_data.product);
3413 device_desc.bcdDevice = cpu_to_le16(mod_data.release);
3414
3415 i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003416 fsg_intf_desc.bNumEndpoints = i;
3417 fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
3418 fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
3419 fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
David Brownell2e806f62007-08-02 00:03:39 -07003421 if (gadget_is_dualspeed(gadget)) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003422 fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
David Brownell2e806f62007-08-02 00:03:39 -07003424 /* Assume endpoint addresses are the same for both speeds */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003425 fsg_hs_bulk_in_desc.bEndpointAddress =
3426 fsg_fs_bulk_in_desc.bEndpointAddress;
3427 fsg_hs_bulk_out_desc.bEndpointAddress =
3428 fsg_fs_bulk_out_desc.bEndpointAddress;
3429 fsg_hs_intr_in_desc.bEndpointAddress =
3430 fsg_fs_intr_in_desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
3432
David Brownell2e806f62007-08-02 00:03:39 -07003433 if (gadget_is_otg(gadget))
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003434 fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
David Brownell2e806f62007-08-02 00:03:39 -07003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 rc = -ENOMEM;
3437
3438 /* Allocate the request and buffer for endpoint 0 */
3439 fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
3440 if (!req)
3441 goto out;
David Brownell9d8bab52007-07-01 11:04:54 -07003442 req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (!req->buf)
3444 goto out;
3445 req->complete = ep0_complete;
3446
3447 /* Allocate the data buffers */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003448 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 struct fsg_buffhd *bh = &fsg->buffhds[i];
3450
Alan Stern14cd5f82006-03-23 15:07:25 -05003451 /* Allocate for the bulk-in endpoint. We assume that
3452 * the buffer will also work with the bulk-out (and
3453 * interrupt-in) endpoint. */
David Brownell9d8bab52007-07-01 11:04:54 -07003454 bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (!bh->buf)
3456 goto out;
3457 bh->next = bh + 1;
3458 }
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003459 fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 /* This should reflect the actual gadget power source */
3462 usb_gadget_set_selfpowered(gadget);
3463
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003464 snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
3465 "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07003466 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 gadget->name);
3468
Alan Stern22efcf42005-09-26 16:12:02 -04003469 fsg->thread_task = kthread_create(fsg_main_thread, fsg,
3470 "file-storage-gadget");
3471 if (IS_ERR(fsg->thread_task)) {
3472 rc = PTR_ERR(fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 goto out;
Alan Stern22efcf42005-09-26 16:12:02 -04003474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
Alan Stern664a51a2011-06-15 16:31:37 -04003477 INFO(fsg, "NOTE: This driver is deprecated. "
3478 "Consider using g_mass_storage instead.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
3480
3481 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
3482 for (i = 0; i < fsg->nluns; ++i) {
3483 curlun = &fsg->luns[i];
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003484 if (fsg_lun_is_open(curlun)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 p = NULL;
3486 if (pathbuf) {
Jan Blunckcf28b482008-02-14 19:38:44 -08003487 p = d_path(&curlun->filp->f_path,
3488 pathbuf, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 if (IS_ERR(p))
3490 p = NULL;
3491 }
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003492 LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
3493 curlun->ro, curlun->nofua, (p ? p : "(error)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
3495 }
3496 kfree(pathbuf);
3497
3498 DBG(fsg, "transport=%s (x%02x)\n",
3499 mod_data.transport_name, mod_data.transport_type);
3500 DBG(fsg, "protocol=%s (x%02x)\n",
3501 mod_data.protocol_name, mod_data.protocol_type);
3502 DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
3503 mod_data.vendor, mod_data.product, mod_data.release);
Alan Stern12aae682008-11-20 14:13:12 -05003504 DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 mod_data.removable, mod_data.can_stall,
Alan Stern12aae682008-11-20 14:13:12 -05003506 mod_data.cdrom, mod_data.buflen);
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003507 DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
Alan Sterna922c682005-10-06 16:38:45 -04003508
3509 set_bit(REGISTERED, &fsg->atomic_bitflags);
3510
3511 /* Tell the thread to start working */
3512 wake_up_process(fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 return 0;
3514
3515autoconf_fail:
3516 ERROR(fsg, "unable to autoconfigure all endpoints\n");
3517 rc = -ENOTSUPP;
3518
3519out:
3520 fsg->state = FSG_STATE_TERMINATED; // The thread is dead
3521 fsg_unbind(gadget);
Felipe Balbi889394b2008-12-08 13:50:27 +02003522 complete(&fsg->thread_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 return rc;
3524}
3525
3526
3527/*-------------------------------------------------------------------------*/
3528
3529static void fsg_suspend(struct usb_gadget *gadget)
3530{
3531 struct fsg_dev *fsg = get_gadget_data(gadget);
3532
3533 DBG(fsg, "suspend\n");
3534 set_bit(SUSPENDED, &fsg->atomic_bitflags);
3535}
3536
3537static void fsg_resume(struct usb_gadget *gadget)
3538{
3539 struct fsg_dev *fsg = get_gadget_data(gadget);
3540
3541 DBG(fsg, "resume\n");
3542 clear_bit(SUSPENDED, &fsg->atomic_bitflags);
3543}
3544
3545
3546/*-------------------------------------------------------------------------*/
3547
3548static struct usb_gadget_driver fsg_driver = {
3549#ifdef CONFIG_USB_GADGET_DUALSPEED
3550 .speed = USB_SPEED_HIGH,
3551#else
3552 .speed = USB_SPEED_FULL,
3553#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003554 .function = (char *) fsg_string_product,
David Brownell6bea4762006-12-05 03:15:33 -08003555 .unbind = fsg_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 .disconnect = fsg_disconnect,
3557 .setup = fsg_setup,
3558 .suspend = fsg_suspend,
3559 .resume = fsg_resume,
3560
3561 .driver = {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003562 .name = DRIVER_NAME,
Ben Dooksd0d50492005-10-10 10:52:33 +01003563 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 // .release = ...
3565 // .suspend = ...
3566 // .resume = ...
3567 },
3568};
3569
3570
3571static int __init fsg_alloc(void)
3572{
3573 struct fsg_dev *fsg;
3574
Alan Sterna922c682005-10-06 16:38:45 -04003575 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 if (!fsg)
3577 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 spin_lock_init(&fsg->lock);
3579 init_rwsem(&fsg->filesem);
Alan Stern87c42522005-11-09 16:59:56 -05003580 kref_init(&fsg->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 init_completion(&fsg->thread_notifier);
3582
3583 the_fsg = fsg;
3584 return 0;
3585}
3586
3587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588static int __init fsg_init(void)
3589{
3590 int rc;
3591 struct fsg_dev *fsg;
3592
3593 if ((rc = fsg_alloc()) != 0)
3594 return rc;
3595 fsg = the_fsg;
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02003596 if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
Alan Stern87c42522005-11-09 16:59:56 -05003597 kref_put(&fsg->ref, fsg_release);
Alan Sterna922c682005-10-06 16:38:45 -04003598 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600module_init(fsg_init);
3601
3602
3603static void __exit fsg_cleanup(void)
3604{
3605 struct fsg_dev *fsg = the_fsg;
3606
3607 /* Unregister the driver iff the thread hasn't already done so */
3608 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
3609 usb_gadget_unregister_driver(&fsg_driver);
3610
3611 /* Wait for the thread to finish up */
3612 wait_for_completion(&fsg->thread_notifier);
3613
Alan Stern87c42522005-11-09 16:59:56 -05003614 kref_put(&fsg->ref, fsg_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616module_exit(fsg_cleanup);