blob: 59d97750cecd2d2ed1aefe13219e44ea883f5315 [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;
1138 unsigned int partial_page;
1139 ssize_t nread;
1140
1141 /* Get the starting Logical Block Address and check that it's
1142 * not too big */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001143 if (fsg->cmnd[0] == READ_6)
Alan Stern604eb892009-04-27 13:19:41 -04001144 lba = get_unaligned_be24(&fsg->cmnd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 else {
Alan Stern604eb892009-04-27 13:19:41 -04001146 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* We allow DPO (Disable Page Out = don't save data in the
1149 * cache) and FUA (Force Unit Access = don't read from the
1150 * cache), but we don't implement them. */
1151 if ((fsg->cmnd[1] & ~0x18) != 0) {
1152 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1153 return -EINVAL;
1154 }
1155 }
1156 if (lba >= curlun->num_sectors) {
1157 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1158 return -EINVAL;
1159 }
Peiyu Li3f565a32011-08-17 22:52:59 -07001160 file_offset = ((loff_t) lba) << curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* Carry out the file reads */
1163 amount_left = fsg->data_size_from_cmnd;
1164 if (unlikely(amount_left == 0))
1165 return -EIO; // No default reply
1166
1167 for (;;) {
1168
1169 /* Figure out how much we need to read:
1170 * Try to read the remaining amount.
1171 * But don't read more than the buffer size.
1172 * And don't try to read past the end of the file.
1173 * Finally, if we're not at a page boundary, don't read past
1174 * the next page.
1175 * If this means reading 0 then we were asked to read past
1176 * the end of file. */
1177 amount = min((unsigned int) amount_left, mod_data.buflen);
1178 amount = min((loff_t) amount,
1179 curlun->file_length - file_offset);
1180 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
1181 if (partial_page > 0)
1182 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
1183 partial_page);
1184
1185 /* Wait for the next buffer to become available */
1186 bh = fsg->next_buffhd_to_fill;
1187 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04001188 rc = sleep_thread(fsg);
1189 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return rc;
1191 }
1192
1193 /* If we were asked to read past the end of file,
1194 * end with an empty buffer. */
1195 if (amount == 0) {
1196 curlun->sense_data =
1197 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
Peiyu Li3f565a32011-08-17 22:52:59 -07001198 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001199 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 bh->inreq->length = 0;
1201 bh->state = BUF_STATE_FULL;
1202 break;
1203 }
1204
1205 /* Perform the read */
1206 file_offset_tmp = file_offset;
1207 nread = vfs_read(curlun->filp,
1208 (char __user *) bh->buf,
1209 amount, &file_offset_tmp);
1210 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1211 (unsigned long long) file_offset,
1212 (int) nread);
1213 if (signal_pending(current))
1214 return -EINTR;
1215
1216 if (nread < 0) {
1217 LDBG(curlun, "error in file read: %d\n",
1218 (int) nread);
1219 nread = 0;
1220 } else if (nread < amount) {
1221 LDBG(curlun, "partial file read: %d/%u\n",
1222 (int) nread, amount);
Peiyu Li3f565a32011-08-17 22:52:59 -07001223 nread = round_down(nread, curlun->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 file_offset += nread;
1226 amount_left -= nread;
1227 fsg->residue -= nread;
1228 bh->inreq->length = nread;
1229 bh->state = BUF_STATE_FULL;
1230
1231 /* If an error occurred, report it and its position */
1232 if (nread < amount) {
1233 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
Peiyu Li3f565a32011-08-17 22:52:59 -07001234 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001235 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
1237 }
1238
1239 if (amount_left == 0)
1240 break; // No more left to read
1241
1242 /* Send this buffer and go read some more */
1243 bh->inreq->zero = 0;
1244 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1245 &bh->inreq_busy, &bh->state);
1246 fsg->next_buffhd_to_fill = bh->next;
1247 }
1248
1249 return -EIO; // No default reply
1250}
1251
1252
1253/*-------------------------------------------------------------------------*/
1254
1255static int do_write(struct fsg_dev *fsg)
1256{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001257 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 u32 lba;
1259 struct fsg_buffhd *bh;
1260 int get_some_more;
1261 u32 amount_left_to_req, amount_left_to_write;
1262 loff_t usb_offset, file_offset, file_offset_tmp;
1263 unsigned int amount;
1264 unsigned int partial_page;
1265 ssize_t nwritten;
1266 int rc;
1267
1268 if (curlun->ro) {
1269 curlun->sense_data = SS_WRITE_PROTECTED;
1270 return -EINVAL;
1271 }
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001272 spin_lock(&curlun->filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001274 spin_unlock(&curlun->filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* Get the starting Logical Block Address and check that it's
1277 * not too big */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001278 if (fsg->cmnd[0] == WRITE_6)
Alan Stern604eb892009-04-27 13:19:41 -04001279 lba = get_unaligned_be24(&fsg->cmnd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 else {
Alan Stern604eb892009-04-27 13:19:41 -04001281 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /* We allow DPO (Disable Page Out = don't save data in the
1284 * cache) and FUA (Force Unit Access = write directly to the
1285 * medium). We don't implement DPO; we implement FUA by
1286 * performing synchronous output. */
1287 if ((fsg->cmnd[1] & ~0x18) != 0) {
1288 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1289 return -EINVAL;
1290 }
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03001291 /* FUA */
1292 if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001293 spin_lock(&curlun->filp->f_lock);
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001294 curlun->filp->f_flags |= O_DSYNC;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001295 spin_unlock(&curlun->filp->f_lock);
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 if (lba >= curlun->num_sectors) {
1299 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1300 return -EINVAL;
1301 }
1302
1303 /* Carry out the file writes */
1304 get_some_more = 1;
Peiyu Li3f565a32011-08-17 22:52:59 -07001305 file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
1307
1308 while (amount_left_to_write > 0) {
1309
1310 /* Queue a request for more data from the host */
1311 bh = fsg->next_buffhd_to_fill;
1312 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
1313
1314 /* Figure out how much we want to get:
1315 * Try to get the remaining amount.
1316 * But don't get more than the buffer size.
1317 * And don't try to go past the end of the file.
1318 * If we're not at a page boundary,
1319 * don't go past the next page.
1320 * If this means getting 0, then we were asked
1321 * to write past the end of file.
1322 * Finally, round down to a block boundary. */
1323 amount = min(amount_left_to_req, mod_data.buflen);
1324 amount = min((loff_t) amount, curlun->file_length -
1325 usb_offset);
1326 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
1327 if (partial_page > 0)
1328 amount = min(amount,
1329 (unsigned int) PAGE_CACHE_SIZE - partial_page);
1330
1331 if (amount == 0) {
1332 get_some_more = 0;
1333 curlun->sense_data =
1334 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
Peiyu Li3f565a32011-08-17 22:52:59 -07001335 curlun->sense_data_info = usb_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001336 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 continue;
1338 }
Peiyu Li3f565a32011-08-17 22:52:59 -07001339 amount = round_down(amount, curlun->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (amount == 0) {
1341
1342 /* Why were we were asked to transfer a
1343 * partial block? */
1344 get_some_more = 0;
1345 continue;
1346 }
1347
1348 /* Get the next buffer */
1349 usb_offset += amount;
1350 fsg->usb_amount_left -= amount;
1351 amount_left_to_req -= amount;
1352 if (amount_left_to_req == 0)
1353 get_some_more = 0;
1354
1355 /* amount is always divisible by 512, hence by
1356 * the bulk-out maxpacket size */
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -07001357 bh->outreq->length = bh->bulk_out_intended_length =
1358 amount;
Alan Stern70ffe6e2006-03-23 15:05:16 -05001359 bh->outreq->short_not_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1361 &bh->outreq_busy, &bh->state);
1362 fsg->next_buffhd_to_fill = bh->next;
1363 continue;
1364 }
1365
1366 /* Write the received data to the backing file */
1367 bh = fsg->next_buffhd_to_drain;
1368 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
1369 break; // We stopped early
1370 if (bh->state == BUF_STATE_FULL) {
Alan Sterna21d4fe2005-11-29 12:04:24 -05001371 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 fsg->next_buffhd_to_drain = bh->next;
1373 bh->state = BUF_STATE_EMPTY;
1374
1375 /* Did something go wrong with the transfer? */
1376 if (bh->outreq->status != 0) {
1377 curlun->sense_data = SS_COMMUNICATION_FAILURE;
Peiyu Li3f565a32011-08-17 22:52:59 -07001378 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001379 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
1381 }
1382
1383 amount = bh->outreq->actual;
1384 if (curlun->file_length - file_offset < amount) {
1385 LERROR(curlun,
1386 "write %u @ %llu beyond end %llu\n",
1387 amount, (unsigned long long) file_offset,
1388 (unsigned long long) curlun->file_length);
1389 amount = curlun->file_length - file_offset;
1390 }
1391
1392 /* Perform the write */
1393 file_offset_tmp = file_offset;
1394 nwritten = vfs_write(curlun->filp,
1395 (char __user *) bh->buf,
1396 amount, &file_offset_tmp);
1397 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1398 (unsigned long long) file_offset,
1399 (int) nwritten);
1400 if (signal_pending(current))
1401 return -EINTR; // Interrupted!
1402
1403 if (nwritten < 0) {
1404 LDBG(curlun, "error in file write: %d\n",
1405 (int) nwritten);
1406 nwritten = 0;
1407 } else if (nwritten < amount) {
1408 LDBG(curlun, "partial file write: %d/%u\n",
1409 (int) nwritten, amount);
Peiyu Li3f565a32011-08-17 22:52:59 -07001410 nwritten = round_down(nwritten, curlun->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 file_offset += nwritten;
1413 amount_left_to_write -= nwritten;
1414 fsg->residue -= nwritten;
1415
1416 /* If an error occurred, report it and its position */
1417 if (nwritten < amount) {
1418 curlun->sense_data = SS_WRITE_ERROR;
Peiyu Li3f565a32011-08-17 22:52:59 -07001419 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001420 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 break;
1422 }
1423
1424 /* Did the host decide to stop early? */
1425 if (bh->outreq->actual != bh->outreq->length) {
1426 fsg->short_packet_received = 1;
1427 break;
1428 }
1429 continue;
1430 }
1431
1432 /* Wait for something to happen */
Alan Stern79a7d9e2007-08-08 17:10:11 -04001433 rc = sleep_thread(fsg);
1434 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return rc;
1436 }
1437
1438 return -EIO; // No default reply
1439}
1440
1441
1442/*-------------------------------------------------------------------------*/
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444static int do_synchronize_cache(struct fsg_dev *fsg)
1445{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001446 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int rc;
1448
1449 /* We ignore the requested LBA and write out all file's
1450 * dirty data buffers. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001451 rc = fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (rc)
1453 curlun->sense_data = SS_WRITE_ERROR;
1454 return 0;
1455}
1456
1457
1458/*-------------------------------------------------------------------------*/
1459
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001460static void invalidate_sub(struct fsg_lun *curlun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 struct file *filp = curlun->filp;
Josef Sipek33cb8992006-12-08 02:37:46 -08001463 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 unsigned long rc;
1465
Andrew Mortonfc0ecff2007-02-10 01:45:39 -08001466 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001467 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470static int do_verify(struct fsg_dev *fsg)
1471{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001472 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 u32 lba;
1474 u32 verification_length;
1475 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1476 loff_t file_offset, file_offset_tmp;
1477 u32 amount_left;
1478 unsigned int amount;
1479 ssize_t nread;
1480
1481 /* Get the starting Logical Block Address and check that it's
1482 * not too big */
Alan Stern604eb892009-04-27 13:19:41 -04001483 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (lba >= curlun->num_sectors) {
1485 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1486 return -EINVAL;
1487 }
1488
1489 /* We allow DPO (Disable Page Out = don't save data in the
1490 * cache) but we don't implement it. */
1491 if ((fsg->cmnd[1] & ~0x10) != 0) {
1492 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1493 return -EINVAL;
1494 }
1495
Alan Stern604eb892009-04-27 13:19:41 -04001496 verification_length = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (unlikely(verification_length == 0))
1498 return -EIO; // No default reply
1499
1500 /* Prepare to carry out the file verify */
Peiyu Li3f565a32011-08-17 22:52:59 -07001501 amount_left = verification_length << curlun->blkbits;
1502 file_offset = ((loff_t) lba) << curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 /* Write out all the dirty buffers before invalidating them */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001505 fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (signal_pending(current))
1507 return -EINTR;
1508
1509 invalidate_sub(curlun);
1510 if (signal_pending(current))
1511 return -EINTR;
1512
1513 /* Just try to read the requested blocks */
1514 while (amount_left > 0) {
1515
1516 /* Figure out how much we need to read:
1517 * Try to read the remaining amount, but not more than
1518 * the buffer size.
1519 * And don't try to read past the end of the file.
1520 * If this means reading 0 then we were asked to read
1521 * past the end of file. */
1522 amount = min((unsigned int) amount_left, mod_data.buflen);
1523 amount = min((loff_t) amount,
1524 curlun->file_length - file_offset);
1525 if (amount == 0) {
1526 curlun->sense_data =
1527 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
Peiyu Li3f565a32011-08-17 22:52:59 -07001528 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001529 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 break;
1531 }
1532
1533 /* Perform the read */
1534 file_offset_tmp = file_offset;
1535 nread = vfs_read(curlun->filp,
1536 (char __user *) bh->buf,
1537 amount, &file_offset_tmp);
1538 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1539 (unsigned long long) file_offset,
1540 (int) nread);
1541 if (signal_pending(current))
1542 return -EINTR;
1543
1544 if (nread < 0) {
1545 LDBG(curlun, "error in file verify: %d\n",
1546 (int) nread);
1547 nread = 0;
1548 } else if (nread < amount) {
1549 LDBG(curlun, "partial file verify: %d/%u\n",
1550 (int) nread, amount);
Peiyu Li3f565a32011-08-17 22:52:59 -07001551 nread = round_down(nread, curlun->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 if (nread == 0) {
1554 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
Peiyu Li3f565a32011-08-17 22:52:59 -07001555 curlun->sense_data_info = file_offset >> curlun->blkbits;
Alan Stern6174d0f2006-09-26 14:51:48 -04001556 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 break;
1558 }
1559 file_offset += nread;
1560 amount_left -= nread;
1561 }
1562 return 0;
1563}
1564
1565
1566/*-------------------------------------------------------------------------*/
1567
1568static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1569{
1570 u8 *buf = (u8 *) bh->buf;
1571
1572 static char vendor_id[] = "Linux ";
Alan Stern12aae682008-11-20 14:13:12 -05001573 static char product_disk_id[] = "File-Stor Gadget";
1574 static char product_cdrom_id[] = "File-CD Gadget ";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 if (!fsg->curlun) { // Unsupported LUNs are okay
1577 fsg->bad_lun_okay = 1;
1578 memset(buf, 0, 36);
1579 buf[0] = 0x7f; // Unsupported, no device-type
Alan Stern12aae682008-11-20 14:13:12 -05001580 buf[4] = 31; // Additional length
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return 36;
1582 }
1583
Alan Stern12aae682008-11-20 14:13:12 -05001584 memset(buf, 0, 8);
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001585 buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (mod_data.removable)
1587 buf[1] = 0x80;
1588 buf[2] = 2; // ANSI SCSI level 2
1589 buf[3] = 2; // SCSI-2 INQUIRY data format
1590 buf[4] = 31; // Additional length
1591 // No special options
Alan Stern12aae682008-11-20 14:13:12 -05001592 sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
1593 (mod_data.cdrom ? product_cdrom_id :
1594 product_disk_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 mod_data.release);
1596 return 36;
1597}
1598
1599
1600static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1601{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001602 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 u8 *buf = (u8 *) bh->buf;
1604 u32 sd, sdinfo;
Alan Stern6174d0f2006-09-26 14:51:48 -04001605 int valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /*
1608 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1609 *
1610 * If a REQUEST SENSE command is received from an initiator
1611 * with a pending unit attention condition (before the target
1612 * generates the contingent allegiance condition), then the
1613 * target shall either:
1614 * a) report any pending sense data and preserve the unit
1615 * attention condition on the logical unit, or,
1616 * b) report the unit attention condition, may discard any
1617 * pending sense data, and clear the unit attention
1618 * condition on the logical unit for that initiator.
1619 *
1620 * FSG normally uses option a); enable this code to use option b).
1621 */
1622#if 0
1623 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1624 curlun->sense_data = curlun->unit_attention_data;
1625 curlun->unit_attention_data = SS_NO_SENSE;
1626 }
1627#endif
1628
1629 if (!curlun) { // Unsupported LUNs are okay
1630 fsg->bad_lun_okay = 1;
1631 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1632 sdinfo = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04001633 valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 } else {
1635 sd = curlun->sense_data;
1636 sdinfo = curlun->sense_data_info;
Alan Stern6174d0f2006-09-26 14:51:48 -04001637 valid = curlun->info_valid << 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 curlun->sense_data = SS_NO_SENSE;
1639 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04001640 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642
1643 memset(buf, 0, 18);
Alan Stern6174d0f2006-09-26 14:51:48 -04001644 buf[0] = valid | 0x70; // Valid, current error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 buf[2] = SK(sd);
Alan Stern604eb892009-04-27 13:19:41 -04001646 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 buf[7] = 18 - 8; // Additional sense length
1648 buf[12] = ASC(sd);
1649 buf[13] = ASCQ(sd);
1650 return 18;
1651}
1652
1653
1654static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1655{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001656 struct fsg_lun *curlun = fsg->curlun;
Alan Stern604eb892009-04-27 13:19:41 -04001657 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int pmi = fsg->cmnd[8];
1659 u8 *buf = (u8 *) bh->buf;
1660
1661 /* Check the PMI and LBA fields */
1662 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1663 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1664 return -EINVAL;
1665 }
1666
Alan Stern604eb892009-04-27 13:19:41 -04001667 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1668 /* Max logical block */
Peiyu Li3f565a32011-08-17 22:52:59 -07001669 put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 8;
1671}
1672
1673
Alan Stern12aae682008-11-20 14:13:12 -05001674static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1675{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001676 struct fsg_lun *curlun = fsg->curlun;
Alan Stern12aae682008-11-20 14:13:12 -05001677 int msf = fsg->cmnd[1] & 0x02;
Alan Stern604eb892009-04-27 13:19:41 -04001678 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
Alan Stern12aae682008-11-20 14:13:12 -05001679 u8 *buf = (u8 *) bh->buf;
1680
1681 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
1682 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1683 return -EINVAL;
1684 }
1685 if (lba >= curlun->num_sectors) {
1686 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1687 return -EINVAL;
1688 }
1689
1690 memset(buf, 0, 8);
1691 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1692 store_cdrom_address(&buf[4], msf, lba);
1693 return 8;
1694}
1695
1696
1697static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1698{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001699 struct fsg_lun *curlun = fsg->curlun;
Alan Stern12aae682008-11-20 14:13:12 -05001700 int msf = fsg->cmnd[1] & 0x02;
1701 int start_track = fsg->cmnd[6];
1702 u8 *buf = (u8 *) bh->buf;
1703
1704 if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1705 start_track > 1) {
1706 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1707 return -EINVAL;
1708 }
1709
1710 memset(buf, 0, 20);
1711 buf[1] = (20-2); /* TOC data length */
1712 buf[2] = 1; /* First track number */
1713 buf[3] = 1; /* Last track number */
1714 buf[5] = 0x16; /* Data track, copying allowed */
1715 buf[6] = 0x01; /* Only track is number 1 */
1716 store_cdrom_address(&buf[8], msf, 0);
1717
1718 buf[13] = 0x16; /* Lead-out track is data */
1719 buf[14] = 0xAA; /* Lead-out track number */
1720 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1721 return 20;
1722}
1723
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1726{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001727 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 int mscmnd = fsg->cmnd[0];
1729 u8 *buf = (u8 *) bh->buf;
1730 u8 *buf0 = buf;
1731 int pc, page_code;
1732 int changeable_values, all_pages;
1733 int valid_page = 0;
1734 int len, limit;
1735
1736 if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
1737 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1738 return -EINVAL;
1739 }
1740 pc = fsg->cmnd[2] >> 6;
1741 page_code = fsg->cmnd[2] & 0x3f;
1742 if (pc == 3) {
1743 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1744 return -EINVAL;
1745 }
1746 changeable_values = (pc == 1);
1747 all_pages = (page_code == 0x3f);
1748
1749 /* Write the mode parameter header. Fixed values are: default
1750 * medium type, no cache control (DPOFUA), and no block descriptors.
1751 * The only variable value is the WriteProtect bit. We will fill in
1752 * the mode data length later. */
1753 memset(buf, 0, 8);
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001754 if (mscmnd == MODE_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1756 buf += 4;
1757 limit = 255;
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001758 } else { // MODE_SENSE_10
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1760 buf += 8;
1761 limit = 65535; // Should really be mod_data.buflen
1762 }
1763
1764 /* No block descriptors */
1765
1766 /* The mode pages, in numerical order. The only page we support
1767 * is the Caching page. */
1768 if (page_code == 0x08 || all_pages) {
1769 valid_page = 1;
1770 buf[0] = 0x08; // Page code
1771 buf[1] = 10; // Page length
1772 memset(buf+2, 0, 10); // None of the fields are changeable
1773
1774 if (!changeable_values) {
1775 buf[2] = 0x04; // Write cache enable,
1776 // Read cache not disabled
1777 // No cache retention priorities
Alan Stern604eb892009-04-27 13:19:41 -04001778 put_unaligned_be16(0xffff, &buf[4]);
1779 /* Don't disable prefetch */
1780 /* Minimum prefetch = 0 */
1781 put_unaligned_be16(0xffff, &buf[8]);
1782 /* Maximum prefetch */
1783 put_unaligned_be16(0xffff, &buf[10]);
1784 /* Maximum prefetch ceiling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786 buf += 12;
1787 }
1788
1789 /* Check that a valid page was requested and the mode data length
1790 * isn't too long. */
1791 len = buf - buf0;
1792 if (!valid_page || len > limit) {
1793 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1794 return -EINVAL;
1795 }
1796
1797 /* Store the mode data length */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001798 if (mscmnd == MODE_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 buf0[0] = len - 1;
1800 else
Alan Stern604eb892009-04-27 13:19:41 -04001801 put_unaligned_be16(len - 2, buf0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return len;
1803}
1804
1805
1806static int do_start_stop(struct fsg_dev *fsg)
1807{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001808 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int loej, start;
1810
1811 if (!mod_data.removable) {
1812 curlun->sense_data = SS_INVALID_COMMAND;
1813 return -EINVAL;
1814 }
1815
1816 // int immed = fsg->cmnd[1] & 0x01;
1817 loej = fsg->cmnd[4] & 0x02;
1818 start = fsg->cmnd[4] & 0x01;
1819
1820#ifdef CONFIG_USB_FILE_STORAGE_TEST
1821 if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
1822 (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
1823 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1824 return -EINVAL;
1825 }
1826
1827 if (!start) {
1828
1829 /* Are we allowed to unload the media? */
1830 if (curlun->prevent_medium_removal) {
1831 LDBG(curlun, "unload attempt prevented\n");
1832 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1833 return -EINVAL;
1834 }
1835 if (loej) { // Simulate an unload/eject
1836 up_read(&fsg->filesem);
1837 down_write(&fsg->filesem);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001838 fsg_lun_close(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 up_write(&fsg->filesem);
1840 down_read(&fsg->filesem);
1841 }
1842 } else {
1843
1844 /* Our emulation doesn't support mounting; the medium is
1845 * available for use as soon as it is loaded. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001846 if (!fsg_lun_is_open(curlun)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1848 return -EINVAL;
1849 }
1850 }
1851#endif
1852 return 0;
1853}
1854
1855
1856static int do_prevent_allow(struct fsg_dev *fsg)
1857{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001858 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 int prevent;
1860
1861 if (!mod_data.removable) {
1862 curlun->sense_data = SS_INVALID_COMMAND;
1863 return -EINVAL;
1864 }
1865
1866 prevent = fsg->cmnd[4] & 0x01;
1867 if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
1868 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1869 return -EINVAL;
1870 }
1871
1872 if (curlun->prevent_medium_removal && !prevent)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001873 fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 curlun->prevent_medium_removal = prevent;
1875 return 0;
1876}
1877
1878
1879static int do_read_format_capacities(struct fsg_dev *fsg,
1880 struct fsg_buffhd *bh)
1881{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001882 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 u8 *buf = (u8 *) bh->buf;
1884
1885 buf[0] = buf[1] = buf[2] = 0;
1886 buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
1887 buf += 4;
1888
Alan Stern604eb892009-04-27 13:19:41 -04001889 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1890 /* Number of blocks */
Peiyu Li3f565a32011-08-17 22:52:59 -07001891 put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
Alan Stern604eb892009-04-27 13:19:41 -04001892 buf[4] = 0x02; /* Current capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return 12;
1894}
1895
1896
1897static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1898{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001899 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /* We don't support MODE SELECT */
1902 curlun->sense_data = SS_INVALID_COMMAND;
1903 return -EINVAL;
1904}
1905
1906
1907/*-------------------------------------------------------------------------*/
1908
1909static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1910{
1911 int rc;
1912
1913 rc = fsg_set_halt(fsg, fsg->bulk_in);
1914 if (rc == -EAGAIN)
1915 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1916 while (rc != 0) {
1917 if (rc != -EAGAIN) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001918 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 rc = 0;
1920 break;
1921 }
1922
1923 /* Wait for a short time and then try again */
1924 if (msleep_interruptible(100) != 0)
1925 return -EINTR;
1926 rc = usb_ep_set_halt(fsg->bulk_in);
1927 }
1928 return rc;
1929}
1930
David Lopo62fd2ca2008-04-29 10:14:38 +01001931static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1932{
1933 int rc;
1934
1935 DBG(fsg, "bulk-in set wedge\n");
1936 rc = usb_ep_set_wedge(fsg->bulk_in);
1937 if (rc == -EAGAIN)
1938 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1939 while (rc != 0) {
1940 if (rc != -EAGAIN) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001941 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
David Lopo62fd2ca2008-04-29 10:14:38 +01001942 rc = 0;
1943 break;
1944 }
1945
1946 /* Wait for a short time and then try again */
1947 if (msleep_interruptible(100) != 0)
1948 return -EINTR;
1949 rc = usb_ep_set_wedge(fsg->bulk_in);
1950 }
1951 return rc;
1952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954static int throw_away_data(struct fsg_dev *fsg)
1955{
1956 struct fsg_buffhd *bh;
1957 u32 amount;
1958 int rc;
1959
1960 while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
1961 fsg->usb_amount_left > 0) {
1962
1963 /* Throw away the data in a filled buffer */
1964 if (bh->state == BUF_STATE_FULL) {
Alan Sterna21d4fe2005-11-29 12:04:24 -05001965 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 bh->state = BUF_STATE_EMPTY;
1967 fsg->next_buffhd_to_drain = bh->next;
1968
1969 /* A short packet or an error ends everything */
1970 if (bh->outreq->actual != bh->outreq->length ||
1971 bh->outreq->status != 0) {
1972 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
1973 return -EINTR;
1974 }
1975 continue;
1976 }
1977
1978 /* Try to submit another request if we need one */
1979 bh = fsg->next_buffhd_to_fill;
1980 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
1981 amount = min(fsg->usb_amount_left,
1982 (u32) mod_data.buflen);
1983
1984 /* amount is always divisible by 512, hence by
1985 * the bulk-out maxpacket size */
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -07001986 bh->outreq->length = bh->bulk_out_intended_length =
1987 amount;
Alan Stern70ffe6e2006-03-23 15:05:16 -05001988 bh->outreq->short_not_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1990 &bh->outreq_busy, &bh->state);
1991 fsg->next_buffhd_to_fill = bh->next;
1992 fsg->usb_amount_left -= amount;
1993 continue;
1994 }
1995
1996 /* Otherwise wait for something to happen */
Alan Stern79a7d9e2007-08-08 17:10:11 -04001997 rc = sleep_thread(fsg);
1998 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return rc;
2000 }
2001 return 0;
2002}
2003
2004
2005static int finish_reply(struct fsg_dev *fsg)
2006{
2007 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
2008 int rc = 0;
2009
2010 switch (fsg->data_dir) {
2011 case DATA_DIR_NONE:
2012 break; // Nothing to send
2013
2014 /* If we don't know whether the host wants to read or write,
2015 * this must be CB or CBI with an unknown command. We mustn't
2016 * try to send or receive any data. So stall both bulk pipes
2017 * if we can and wait for a reset. */
2018 case DATA_DIR_UNKNOWN:
2019 if (mod_data.can_stall) {
2020 fsg_set_halt(fsg, fsg->bulk_out);
2021 rc = halt_bulk_in_endpoint(fsg);
2022 }
2023 break;
2024
2025 /* All but the last buffer of data must have already been sent */
2026 case DATA_DIR_TO_HOST:
2027 if (fsg->data_size == 0)
2028 ; // Nothing to send
2029
2030 /* If there's no residue, simply send the last buffer */
2031 else if (fsg->residue == 0) {
2032 bh->inreq->zero = 0;
2033 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2034 &bh->inreq_busy, &bh->state);
2035 fsg->next_buffhd_to_fill = bh->next;
2036 }
2037
2038 /* There is a residue. For CB and CBI, simply mark the end
2039 * of the data with a short packet. However, if we are
2040 * allowed to stall, there was no data at all (residue ==
2041 * data_size), and the command failed (invalid LUN or
2042 * sense data is set), then halt the bulk-in endpoint
2043 * instead. */
2044 else if (!transport_is_bbb()) {
2045 if (mod_data.can_stall &&
2046 fsg->residue == fsg->data_size &&
2047 (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
2048 bh->state = BUF_STATE_EMPTY;
2049 rc = halt_bulk_in_endpoint(fsg);
2050 } else {
2051 bh->inreq->zero = 1;
2052 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2053 &bh->inreq_busy, &bh->state);
2054 fsg->next_buffhd_to_fill = bh->next;
2055 }
2056 }
2057
Alan Sternee81b3e2011-03-25 11:46:27 -04002058 /*
2059 * For Bulk-only, mark the end of the data with a short
2060 * packet. If we are allowed to stall, halt the bulk-in
2061 * endpoint. (Note: This violates the Bulk-Only Transport
2062 * specification, which requires us to pad the data if we
2063 * don't halt the endpoint. Presumably nobody will mind.)
2064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 else {
Alan Sternee81b3e2011-03-25 11:46:27 -04002066 bh->inreq->zero = 1;
2067 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2068 &bh->inreq_busy, &bh->state);
2069 fsg->next_buffhd_to_fill = bh->next;
2070 if (mod_data.can_stall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 rc = halt_bulk_in_endpoint(fsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073 break;
2074
2075 /* We have processed all we want from the data the host has sent.
2076 * There may still be outstanding bulk-out requests. */
2077 case DATA_DIR_FROM_HOST:
2078 if (fsg->residue == 0)
2079 ; // Nothing to receive
2080
2081 /* Did the host stop sending unexpectedly early? */
2082 else if (fsg->short_packet_received) {
2083 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2084 rc = -EINTR;
2085 }
2086
2087 /* We haven't processed all the incoming data. Even though
2088 * we may be allowed to stall, doing so would cause a race.
2089 * The controller may already have ACK'ed all the remaining
2090 * bulk-out packets, in which case the host wouldn't see a
2091 * STALL. Not realizing the endpoint was halted, it wouldn't
2092 * clear the halt -- leading to problems later on. */
2093#if 0
2094 else if (mod_data.can_stall) {
2095 fsg_set_halt(fsg, fsg->bulk_out);
2096 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2097 rc = -EINTR;
2098 }
2099#endif
2100
2101 /* We can't stall. Read in the excess data and throw it
2102 * all away. */
2103 else
2104 rc = throw_away_data(fsg);
2105 break;
2106 }
2107 return rc;
2108}
2109
2110
2111static int send_status(struct fsg_dev *fsg)
2112{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002113 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct fsg_buffhd *bh;
2115 int rc;
2116 u8 status = USB_STATUS_PASS;
2117 u32 sd, sdinfo = 0;
2118
2119 /* Wait for the next buffer to become available */
2120 bh = fsg->next_buffhd_to_fill;
2121 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002122 rc = sleep_thread(fsg);
2123 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return rc;
2125 }
2126
2127 if (curlun) {
2128 sd = curlun->sense_data;
2129 sdinfo = curlun->sense_data_info;
2130 } else if (fsg->bad_lun_okay)
2131 sd = SS_NO_SENSE;
2132 else
2133 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2134
2135 if (fsg->phase_error) {
2136 DBG(fsg, "sending phase-error status\n");
2137 status = USB_STATUS_PHASE_ERROR;
2138 sd = SS_INVALID_COMMAND;
2139 } else if (sd != SS_NO_SENSE) {
2140 DBG(fsg, "sending command-failure status\n");
2141 status = USB_STATUS_FAIL;
2142 VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2143 " info x%x\n",
2144 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
2145 }
2146
2147 if (transport_is_bbb()) {
John Daiker7ca46b82006-12-29 19:02:06 -08002148 struct bulk_cs_wrap *csw = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* Store and send the Bulk-only CSW */
Harvey Harrison551509d2009-02-11 14:11:36 -08002151 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 csw->Tag = fsg->tag;
2153 csw->Residue = cpu_to_le32(fsg->residue);
2154 csw->Status = status;
2155
2156 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
2157 bh->inreq->zero = 0;
2158 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2159 &bh->inreq_busy, &bh->state);
2160
2161 } else if (mod_data.transport_type == USB_PR_CB) {
2162
2163 /* Control-Bulk transport has no status phase! */
2164 return 0;
2165
2166 } else { // USB_PR_CBI
John Daiker7ca46b82006-12-29 19:02:06 -08002167 struct interrupt_data *buf = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 /* Store and send the Interrupt data. UFI sends the ASC
2170 * and ASCQ bytes. Everything else sends a Type (which
2171 * is always 0) and the status Value. */
2172 if (mod_data.protocol_type == USB_SC_UFI) {
2173 buf->bType = ASC(sd);
2174 buf->bValue = ASCQ(sd);
2175 } else {
2176 buf->bType = 0;
2177 buf->bValue = status;
2178 }
2179 fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
2180
2181 fsg->intr_buffhd = bh; // Point to the right buffhd
2182 fsg->intreq->buf = bh->inreq->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 fsg->intreq->context = bh;
2184 start_transfer(fsg, fsg->intr_in, fsg->intreq,
2185 &fsg->intreq_busy, &bh->state);
2186 }
2187
2188 fsg->next_buffhd_to_fill = bh->next;
2189 return 0;
2190}
2191
2192
2193/*-------------------------------------------------------------------------*/
2194
2195/* Check whether the command is properly formed and whether its data size
2196 * and direction agree with the values we already have. */
2197static int check_command(struct fsg_dev *fsg, int cmnd_size,
2198 enum data_direction data_dir, unsigned int mask,
2199 int needs_medium, const char *name)
2200{
2201 int i;
2202 int lun = fsg->cmnd[1] >> 5;
2203 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
2204 char hdlen[20];
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002205 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /* Adjust the expected cmnd_size for protocol encapsulation padding.
2208 * Transparent SCSI doesn't pad. */
2209 if (protocol_is_scsi())
2210 ;
2211
2212 /* There's some disagreement as to whether RBC pads commands or not.
2213 * We'll play it safe and accept either form. */
2214 else if (mod_data.protocol_type == USB_SC_RBC) {
2215 if (fsg->cmnd_size == 12)
2216 cmnd_size = 12;
2217
2218 /* All the other protocols pad to 12 bytes */
2219 } else
2220 cmnd_size = 12;
2221
2222 hdlen[0] = 0;
2223 if (fsg->data_dir != DATA_DIR_UNKNOWN)
2224 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
2225 fsg->data_size);
2226 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
2227 name, cmnd_size, dirletter[(int) data_dir],
2228 fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
2229
2230 /* We can't reply at all until we know the correct data direction
2231 * and size. */
2232 if (fsg->data_size_from_cmnd == 0)
2233 data_dir = DATA_DIR_NONE;
2234 if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
2235 fsg->data_dir = data_dir;
2236 fsg->data_size = fsg->data_size_from_cmnd;
2237
2238 } else { // Bulk-only
2239 if (fsg->data_size < fsg->data_size_from_cmnd) {
2240
2241 /* Host data size < Device data size is a phase error.
2242 * Carry out the command, but only transfer as much
2243 * as we are allowed. */
2244 fsg->data_size_from_cmnd = fsg->data_size;
2245 fsg->phase_error = 1;
2246 }
2247 }
2248 fsg->residue = fsg->usb_amount_left = fsg->data_size;
2249
2250 /* Conflicting data directions is a phase error */
2251 if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
2252 fsg->phase_error = 1;
2253 return -EINVAL;
2254 }
2255
2256 /* Verify the length of the command itself */
2257 if (cmnd_size != fsg->cmnd_size) {
2258
Felipe Balbi549c41e2008-09-19 02:00:02 +03002259 /* Special case workaround: There are plenty of buggy SCSI
2260 * implementations. Many have issues with cbw->Length
2261 * field passing a wrong command size. For those cases we
2262 * always try to work around the problem by using the length
2263 * sent by the host side provided it is at least as large
2264 * as the correct command length.
2265 * Examples of such cases would be MS-Windows, which issues
2266 * REQUEST SENSE with cbw->Length == 12 where it should
2267 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
2268 * REQUEST SENSE with cbw->Length == 10 where it should
2269 * be 6 as well.
2270 */
2271 if (cmnd_size <= fsg->cmnd_size) {
2272 DBG(fsg, "%s is buggy! Expected length %d "
2273 "but we got %d\n", name,
2274 cmnd_size, fsg->cmnd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 cmnd_size = fsg->cmnd_size;
Felipe Balbi549c41e2008-09-19 02:00:02 +03002276 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 fsg->phase_error = 1;
2278 return -EINVAL;
2279 }
2280 }
2281
Alan Sternd794ac72005-04-18 12:43:25 -04002282 /* Check that the LUN values are consistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (transport_is_bbb()) {
2284 if (fsg->lun != lun)
2285 DBG(fsg, "using LUN %d from CBW, "
2286 "not LUN %d from CDB\n",
2287 fsg->lun, lun);
2288 } else
2289 fsg->lun = lun; // Use LUN from the command
2290
2291 /* Check the LUN */
Maxin B John849426c2011-05-08 15:56:17 +03002292 if (fsg->lun < fsg->nluns) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 fsg->curlun = curlun = &fsg->luns[fsg->lun];
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002294 if (fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 curlun->sense_data = SS_NO_SENSE;
2296 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04002297 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 } else {
2300 fsg->curlun = curlun = NULL;
2301 fsg->bad_lun_okay = 0;
2302
2303 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2304 * to use unsupported LUNs; all others may not. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002305 if (fsg->cmnd[0] != INQUIRY &&
2306 fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 DBG(fsg, "unsupported LUN %d\n", fsg->lun);
2308 return -EINVAL;
2309 }
2310 }
2311
2312 /* If a unit attention condition exists, only INQUIRY and
2313 * REQUEST SENSE commands are allowed; anything else must fail. */
2314 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002315 fsg->cmnd[0] != INQUIRY &&
2316 fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 curlun->sense_data = curlun->unit_attention_data;
2318 curlun->unit_attention_data = SS_NO_SENSE;
2319 return -EINVAL;
2320 }
2321
2322 /* Check that only command bytes listed in the mask are non-zero */
2323 fsg->cmnd[1] &= 0x1f; // Mask away the LUN
2324 for (i = 1; i < cmnd_size; ++i) {
2325 if (fsg->cmnd[i] && !(mask & (1 << i))) {
2326 if (curlun)
2327 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2328 return -EINVAL;
2329 }
2330 }
2331
2332 /* If the medium isn't mounted and the command needs to access
2333 * it, return an error. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002334 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2336 return -EINVAL;
2337 }
2338
2339 return 0;
2340}
2341
2342
2343static int do_scsi_command(struct fsg_dev *fsg)
2344{
2345 struct fsg_buffhd *bh;
2346 int rc;
2347 int reply = -EINVAL;
2348 int i;
2349 static char unknown[16];
2350
2351 dump_cdb(fsg);
2352
2353 /* Wait for the next buffer to become available for data or status */
2354 bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
2355 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002356 rc = sleep_thread(fsg);
2357 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 fsg->phase_error = 0;
2361 fsg->short_packet_received = 0;
2362
2363 down_read(&fsg->filesem); // We're using the backing file
2364 switch (fsg->cmnd[0]) {
2365
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002366 case INQUIRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 fsg->data_size_from_cmnd = fsg->cmnd[4];
2368 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2369 (1<<4), 0,
2370 "INQUIRY")) == 0)
2371 reply = do_inquiry(fsg, bh);
2372 break;
2373
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002374 case MODE_SELECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 fsg->data_size_from_cmnd = fsg->cmnd[4];
2376 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2377 (1<<1) | (1<<4), 0,
2378 "MODE SELECT(6)")) == 0)
2379 reply = do_mode_select(fsg, bh);
2380 break;
2381
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002382 case MODE_SELECT_10:
Alan Stern604eb892009-04-27 13:19:41 -04002383 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2385 (1<<1) | (3<<7), 0,
2386 "MODE SELECT(10)")) == 0)
2387 reply = do_mode_select(fsg, bh);
2388 break;
2389
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002390 case MODE_SENSE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 fsg->data_size_from_cmnd = fsg->cmnd[4];
2392 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2393 (1<<1) | (1<<2) | (1<<4), 0,
2394 "MODE SENSE(6)")) == 0)
2395 reply = do_mode_sense(fsg, bh);
2396 break;
2397
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002398 case MODE_SENSE_10:
Alan Stern604eb892009-04-27 13:19:41 -04002399 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2401 (1<<1) | (1<<2) | (3<<7), 0,
2402 "MODE SENSE(10)")) == 0)
2403 reply = do_mode_sense(fsg, bh);
2404 break;
2405
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002406 case ALLOW_MEDIUM_REMOVAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 fsg->data_size_from_cmnd = 0;
2408 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2409 (1<<4), 0,
2410 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
2411 reply = do_prevent_allow(fsg);
2412 break;
2413
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002414 case READ_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 i = fsg->cmnd[4];
Peiyu Li3f565a32011-08-17 22:52:59 -07002416 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2418 (7<<1) | (1<<4), 1,
2419 "READ(6)")) == 0)
2420 reply = do_read(fsg);
2421 break;
2422
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002423 case READ_10:
Alan Stern604eb892009-04-27 13:19:41 -04002424 fsg->data_size_from_cmnd =
Peiyu Li3f565a32011-08-17 22:52:59 -07002425 get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2427 (1<<1) | (0xf<<2) | (3<<7), 1,
2428 "READ(10)")) == 0)
2429 reply = do_read(fsg);
2430 break;
2431
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002432 case READ_12:
Alan Stern604eb892009-04-27 13:19:41 -04002433 fsg->data_size_from_cmnd =
Peiyu Li3f565a32011-08-17 22:52:59 -07002434 get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2436 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2437 "READ(12)")) == 0)
2438 reply = do_read(fsg);
2439 break;
2440
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002441 case READ_CAPACITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 fsg->data_size_from_cmnd = 8;
2443 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2444 (0xf<<2) | (1<<8), 1,
2445 "READ CAPACITY")) == 0)
2446 reply = do_read_capacity(fsg, bh);
2447 break;
2448
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002449 case READ_HEADER:
Alan Stern12aae682008-11-20 14:13:12 -05002450 if (!mod_data.cdrom)
2451 goto unknown_cmnd;
Alan Stern604eb892009-04-27 13:19:41 -04002452 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Alan Stern12aae682008-11-20 14:13:12 -05002453 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2454 (3<<7) | (0x1f<<1), 1,
2455 "READ HEADER")) == 0)
2456 reply = do_read_header(fsg, bh);
2457 break;
2458
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002459 case READ_TOC:
Alan Stern12aae682008-11-20 14:13:12 -05002460 if (!mod_data.cdrom)
2461 goto unknown_cmnd;
Alan Stern604eb892009-04-27 13:19:41 -04002462 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Alan Stern12aae682008-11-20 14:13:12 -05002463 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2464 (7<<6) | (1<<1), 1,
2465 "READ TOC")) == 0)
2466 reply = do_read_toc(fsg, bh);
2467 break;
2468
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002469 case READ_FORMAT_CAPACITIES:
Alan Stern604eb892009-04-27 13:19:41 -04002470 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2472 (3<<7), 1,
2473 "READ FORMAT CAPACITIES")) == 0)
2474 reply = do_read_format_capacities(fsg, bh);
2475 break;
2476
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002477 case REQUEST_SENSE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 fsg->data_size_from_cmnd = fsg->cmnd[4];
2479 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2480 (1<<4), 0,
2481 "REQUEST SENSE")) == 0)
2482 reply = do_request_sense(fsg, bh);
2483 break;
2484
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002485 case START_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 fsg->data_size_from_cmnd = 0;
2487 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2488 (1<<1) | (1<<4), 0,
2489 "START-STOP UNIT")) == 0)
2490 reply = do_start_stop(fsg);
2491 break;
2492
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002493 case SYNCHRONIZE_CACHE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 fsg->data_size_from_cmnd = 0;
2495 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2496 (0xf<<2) | (3<<7), 1,
2497 "SYNCHRONIZE CACHE")) == 0)
2498 reply = do_synchronize_cache(fsg);
2499 break;
2500
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002501 case TEST_UNIT_READY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 fsg->data_size_from_cmnd = 0;
2503 reply = check_command(fsg, 6, DATA_DIR_NONE,
2504 0, 1,
2505 "TEST UNIT READY");
2506 break;
2507
2508 /* Although optional, this command is used by MS-Windows. We
2509 * support a minimal version: BytChk must be 0. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002510 case VERIFY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 fsg->data_size_from_cmnd = 0;
2512 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2513 (1<<1) | (0xf<<2) | (3<<7), 1,
2514 "VERIFY")) == 0)
2515 reply = do_verify(fsg);
2516 break;
2517
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002518 case WRITE_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 i = fsg->cmnd[4];
Peiyu Li3f565a32011-08-17 22:52:59 -07002520 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2522 (7<<1) | (1<<4), 1,
2523 "WRITE(6)")) == 0)
2524 reply = do_write(fsg);
2525 break;
2526
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002527 case WRITE_10:
Alan Stern604eb892009-04-27 13:19:41 -04002528 fsg->data_size_from_cmnd =
Peiyu Li3f565a32011-08-17 22:52:59 -07002529 get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2531 (1<<1) | (0xf<<2) | (3<<7), 1,
2532 "WRITE(10)")) == 0)
2533 reply = do_write(fsg);
2534 break;
2535
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002536 case WRITE_12:
Alan Stern604eb892009-04-27 13:19:41 -04002537 fsg->data_size_from_cmnd =
Peiyu Li3f565a32011-08-17 22:52:59 -07002538 get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
2540 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2541 "WRITE(12)")) == 0)
2542 reply = do_write(fsg);
2543 break;
2544
2545 /* Some mandatory commands that we recognize but don't implement.
2546 * They don't mean much in this setting. It's left as an exercise
2547 * for anyone interested to implement RESERVE and RELEASE in terms
2548 * of Posix locks. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002549 case FORMAT_UNIT:
2550 case RELEASE:
2551 case RESERVE:
2552 case SEND_DIAGNOSTIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 // Fall through
2554
2555 default:
Alan Stern12aae682008-11-20 14:13:12 -05002556 unknown_cmnd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 fsg->data_size_from_cmnd = 0;
2558 sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
2559 if ((reply = check_command(fsg, fsg->cmnd_size,
2560 DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) {
2561 fsg->curlun->sense_data = SS_INVALID_COMMAND;
2562 reply = -EINVAL;
2563 }
2564 break;
2565 }
2566 up_read(&fsg->filesem);
2567
2568 if (reply == -EINTR || signal_pending(current))
2569 return -EINTR;
2570
2571 /* Set up the single reply buffer for finish_reply() */
2572 if (reply == -EINVAL)
2573 reply = 0; // Error reply length
2574 if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
2575 reply = min((u32) reply, fsg->data_size_from_cmnd);
2576 bh->inreq->length = reply;
2577 bh->state = BUF_STATE_FULL;
2578 fsg->residue -= reply;
2579 } // Otherwise it's already set
2580
2581 return 0;
2582}
2583
2584
2585/*-------------------------------------------------------------------------*/
2586
2587static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2588{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002589 struct usb_request *req = bh->outreq;
2590 struct fsg_bulk_cb_wrap *cbw = req->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Alan Sternb950bdb2008-04-14 11:45:29 -04002592 /* Was this a real packet? Should it be ignored? */
2593 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return -EINVAL;
2595
2596 /* Is the CBW valid? */
2597 if (req->actual != USB_BULK_CB_WRAP_LEN ||
Harvey Harrison551509d2009-02-11 14:11:36 -08002598 cbw->Signature != cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 USB_BULK_CB_SIG)) {
2600 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2601 req->actual,
2602 le32_to_cpu(cbw->Signature));
2603
Alan Sternb950bdb2008-04-14 11:45:29 -04002604 /* The Bulk-only spec says we MUST stall the IN endpoint
2605 * (6.6.1), so it's unavoidable. It also says we must
2606 * retain this state until the next reset, but there's
2607 * no way to tell the controller driver it should ignore
2608 * Clear-Feature(HALT) requests.
2609 *
2610 * We aren't required to halt the OUT endpoint; instead
2611 * we can simply accept and discard any data received
2612 * until the next reset. */
David Lopo62fd2ca2008-04-29 10:14:38 +01002613 wedge_bulk_in_endpoint(fsg);
Alan Sternb950bdb2008-04-14 11:45:29 -04002614 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return -EINVAL;
2616 }
2617
2618 /* Is the CBW meaningful? */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002619 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
Alan Stern12943f02007-08-24 16:27:50 -04002620 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2622 "cmdlen %u\n",
2623 cbw->Lun, cbw->Flags, cbw->Length);
2624
2625 /* We can do anything we want here, so let's stall the
2626 * bulk pipes if we are allowed to. */
2627 if (mod_data.can_stall) {
2628 fsg_set_halt(fsg, fsg->bulk_out);
2629 halt_bulk_in_endpoint(fsg);
2630 }
2631 return -EINVAL;
2632 }
2633
2634 /* Save the command for later */
2635 fsg->cmnd_size = cbw->Length;
2636 memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
2637 if (cbw->Flags & USB_BULK_IN_FLAG)
2638 fsg->data_dir = DATA_DIR_TO_HOST;
2639 else
2640 fsg->data_dir = DATA_DIR_FROM_HOST;
2641 fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
2642 if (fsg->data_size == 0)
2643 fsg->data_dir = DATA_DIR_NONE;
2644 fsg->lun = cbw->Lun;
2645 fsg->tag = cbw->Tag;
2646 return 0;
2647}
2648
2649
2650static int get_next_command(struct fsg_dev *fsg)
2651{
2652 struct fsg_buffhd *bh;
2653 int rc = 0;
2654
2655 if (transport_is_bbb()) {
2656
2657 /* Wait for the next buffer to become available */
2658 bh = fsg->next_buffhd_to_fill;
2659 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002660 rc = sleep_thread(fsg);
2661 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 /* Queue a request to read a Bulk-only CBW */
Greg Kroah-Hartman98346f72011-04-14 13:42:46 -07002666 set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN);
2667 bh->outreq->short_not_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 start_transfer(fsg, fsg->bulk_out, bh->outreq,
2669 &bh->outreq_busy, &bh->state);
2670
2671 /* We will drain the buffer in software, which means we
2672 * can reuse it for the next filling. No need to advance
2673 * next_buffhd_to_fill. */
2674
2675 /* Wait for the CBW to arrive */
2676 while (bh->state != BUF_STATE_FULL) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002677 rc = sleep_thread(fsg);
2678 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002680 }
Alan Sterna21d4fe2005-11-29 12:04:24 -05002681 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 rc = received_cbw(fsg, bh);
2683 bh->state = BUF_STATE_EMPTY;
2684
2685 } else { // USB_PR_CB or USB_PR_CBI
2686
2687 /* Wait for the next command to arrive */
2688 while (fsg->cbbuf_cmnd_size == 0) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002689 rc = sleep_thread(fsg);
2690 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 /* Is the previous status interrupt request still busy?
2695 * The host is allowed to skip reading the status,
2696 * so we must cancel it. */
2697 if (fsg->intreq_busy)
2698 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2699
2700 /* Copy the command and mark the buffer empty */
2701 fsg->data_dir = DATA_DIR_UNKNOWN;
2702 spin_lock_irq(&fsg->lock);
2703 fsg->cmnd_size = fsg->cbbuf_cmnd_size;
2704 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
2705 fsg->cbbuf_cmnd_size = 0;
2706 spin_unlock_irq(&fsg->lock);
2707 }
2708 return rc;
2709}
2710
2711
2712/*-------------------------------------------------------------------------*/
2713
2714static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
2715 const struct usb_endpoint_descriptor *d)
2716{
2717 int rc;
2718
2719 ep->driver_data = fsg;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03002720 ep->desc = d;
2721 rc = usb_ep_enable(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (rc)
2723 ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
2724 return rc;
2725}
2726
2727static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
2728 struct usb_request **preq)
2729{
2730 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2731 if (*preq)
2732 return 0;
2733 ERROR(fsg, "can't allocate request for %s\n", ep->name);
2734 return -ENOMEM;
2735}
2736
2737/*
2738 * Reset interface setting and re-init endpoint state (toggle etc).
2739 * Call with altsetting < 0 to disable the interface. The only other
2740 * available altsetting is 0, which enables the interface.
2741 */
2742static int do_set_interface(struct fsg_dev *fsg, int altsetting)
2743{
2744 int rc = 0;
2745 int i;
2746 const struct usb_endpoint_descriptor *d;
2747
2748 if (fsg->running)
2749 DBG(fsg, "reset interface\n");
2750
2751reset:
2752 /* Deallocate the requests */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002753 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 struct fsg_buffhd *bh = &fsg->buffhds[i];
2755
2756 if (bh->inreq) {
2757 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2758 bh->inreq = NULL;
2759 }
2760 if (bh->outreq) {
2761 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2762 bh->outreq = NULL;
2763 }
2764 }
2765 if (fsg->intreq) {
2766 usb_ep_free_request(fsg->intr_in, fsg->intreq);
2767 fsg->intreq = NULL;
2768 }
2769
2770 /* Disable the endpoints */
2771 if (fsg->bulk_in_enabled) {
2772 usb_ep_disable(fsg->bulk_in);
2773 fsg->bulk_in_enabled = 0;
2774 }
2775 if (fsg->bulk_out_enabled) {
2776 usb_ep_disable(fsg->bulk_out);
2777 fsg->bulk_out_enabled = 0;
2778 }
2779 if (fsg->intr_in_enabled) {
2780 usb_ep_disable(fsg->intr_in);
2781 fsg->intr_in_enabled = 0;
2782 }
2783
2784 fsg->running = 0;
2785 if (altsetting < 0 || rc != 0)
2786 return rc;
2787
2788 DBG(fsg, "set interface %d\n", altsetting);
2789
2790 /* Enable the endpoints */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002791 d = fsg_ep_desc(fsg->gadget,
2792 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
2794 goto reset;
2795 fsg->bulk_in_enabled = 1;
2796
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002797 d = fsg_ep_desc(fsg->gadget,
2798 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
2800 goto reset;
2801 fsg->bulk_out_enabled = 1;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002802 fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
Alan Sternb950bdb2008-04-14 11:45:29 -04002803 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 if (transport_is_cbi()) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002806 d = fsg_ep_desc(fsg->gadget,
2807 &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
2809 goto reset;
2810 fsg->intr_in_enabled = 1;
2811 }
2812
2813 /* Allocate the requests */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002814 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 struct fsg_buffhd *bh = &fsg->buffhds[i];
2816
2817 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
2818 goto reset;
2819 if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
2820 goto reset;
2821 bh->inreq->buf = bh->outreq->buf = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 bh->inreq->context = bh->outreq->context = bh;
2823 bh->inreq->complete = bulk_in_complete;
2824 bh->outreq->complete = bulk_out_complete;
2825 }
2826 if (transport_is_cbi()) {
2827 if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
2828 goto reset;
2829 fsg->intreq->complete = intr_in_complete;
2830 }
2831
2832 fsg->running = 1;
2833 for (i = 0; i < fsg->nluns; ++i)
2834 fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2835 return rc;
2836}
2837
2838
2839/*
2840 * Change our operational configuration. This code must agree with the code
2841 * that returns config descriptors, and with interface altsetting code.
2842 *
2843 * It's also responsible for power management interactions. Some
2844 * configurations might not work with our current power sources.
2845 * For now we just assume the gadget is always self-powered.
2846 */
2847static int do_set_config(struct fsg_dev *fsg, u8 new_config)
2848{
2849 int rc = 0;
2850
2851 /* Disable the single interface */
2852 if (fsg->config != 0) {
2853 DBG(fsg, "reset config\n");
2854 fsg->config = 0;
2855 rc = do_set_interface(fsg, -1);
2856 }
2857
2858 /* Enable the interface */
2859 if (new_config != 0) {
2860 fsg->config = new_config;
2861 if ((rc = do_set_interface(fsg, 0)) != 0)
2862 fsg->config = 0; // Reset on errors
2863 else {
2864 char *speed;
2865
2866 switch (fsg->gadget->speed) {
2867 case USB_SPEED_LOW: speed = "low"; break;
2868 case USB_SPEED_FULL: speed = "full"; break;
2869 case USB_SPEED_HIGH: speed = "high"; break;
2870 default: speed = "?"; break;
2871 }
2872 INFO(fsg, "%s speed config #%d\n", speed, fsg->config);
2873 }
2874 }
2875 return rc;
2876}
2877
2878
2879/*-------------------------------------------------------------------------*/
2880
2881static void handle_exception(struct fsg_dev *fsg)
2882{
2883 siginfo_t info;
2884 int sig;
2885 int i;
2886 int num_active;
2887 struct fsg_buffhd *bh;
2888 enum fsg_state old_state;
2889 u8 new_config;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002890 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 unsigned int exception_req_tag;
2892 int rc;
2893
2894 /* Clear the existing signals. Anything but SIGUSR1 is converted
2895 * into a high-priority EXIT exception. */
2896 for (;;) {
Oleg Nesterov8cfbe7e2007-05-30 11:06:33 -04002897 sig = dequeue_signal_lock(current, &current->blocked, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (!sig)
2899 break;
2900 if (sig != SIGUSR1) {
2901 if (fsg->state < FSG_STATE_EXIT)
2902 DBG(fsg, "Main thread exiting on signal\n");
2903 raise_exception(fsg, FSG_STATE_EXIT);
2904 }
2905 }
2906
2907 /* Cancel all the pending transfers */
2908 if (fsg->intreq_busy)
2909 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002910 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 bh = &fsg->buffhds[i];
2912 if (bh->inreq_busy)
2913 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
2914 if (bh->outreq_busy)
2915 usb_ep_dequeue(fsg->bulk_out, bh->outreq);
2916 }
2917
2918 /* Wait until everything is idle */
2919 for (;;) {
2920 num_active = fsg->intreq_busy;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002921 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 bh = &fsg->buffhds[i];
2923 num_active += bh->inreq_busy + bh->outreq_busy;
2924 }
2925 if (num_active == 0)
2926 break;
2927 if (sleep_thread(fsg))
2928 return;
2929 }
2930
2931 /* Clear out the controller's fifos */
2932 if (fsg->bulk_in_enabled)
2933 usb_ep_fifo_flush(fsg->bulk_in);
2934 if (fsg->bulk_out_enabled)
2935 usb_ep_fifo_flush(fsg->bulk_out);
2936 if (fsg->intr_in_enabled)
2937 usb_ep_fifo_flush(fsg->intr_in);
2938
2939 /* Reset the I/O buffer states and pointers, the SCSI
2940 * state, and the exception. Then invoke the handler. */
2941 spin_lock_irq(&fsg->lock);
2942
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002943 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 bh = &fsg->buffhds[i];
2945 bh->state = BUF_STATE_EMPTY;
2946 }
2947 fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
2948 &fsg->buffhds[0];
2949
2950 exception_req_tag = fsg->exception_req_tag;
2951 new_config = fsg->new_config;
2952 old_state = fsg->state;
2953
2954 if (old_state == FSG_STATE_ABORT_BULK_OUT)
2955 fsg->state = FSG_STATE_STATUS_PHASE;
2956 else {
2957 for (i = 0; i < fsg->nluns; ++i) {
2958 curlun = &fsg->luns[i];
2959 curlun->prevent_medium_removal = 0;
2960 curlun->sense_data = curlun->unit_attention_data =
2961 SS_NO_SENSE;
2962 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04002963 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965 fsg->state = FSG_STATE_IDLE;
2966 }
2967 spin_unlock_irq(&fsg->lock);
2968
2969 /* Carry out any extra actions required for the exception */
2970 switch (old_state) {
2971 default:
2972 break;
2973
2974 case FSG_STATE_ABORT_BULK_OUT:
2975 send_status(fsg);
2976 spin_lock_irq(&fsg->lock);
2977 if (fsg->state == FSG_STATE_STATUS_PHASE)
2978 fsg->state = FSG_STATE_IDLE;
2979 spin_unlock_irq(&fsg->lock);
2980 break;
2981
2982 case FSG_STATE_RESET:
2983 /* In case we were forced against our will to halt a
2984 * bulk endpoint, clear the halt now. (The SuperH UDC
2985 * requires this.) */
Alan Sternb950bdb2008-04-14 11:45:29 -04002986 if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 usb_ep_clear_halt(fsg->bulk_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 if (transport_is_bbb()) {
2990 if (fsg->ep0_req_tag == exception_req_tag)
2991 ep0_queue(fsg); // Complete the status stage
2992
2993 } else if (transport_is_cbi())
2994 send_status(fsg); // Status by interrupt pipe
2995
2996 /* Technically this should go here, but it would only be
2997 * a waste of time. Ditto for the INTERFACE_CHANGE and
2998 * CONFIG_CHANGE cases. */
2999 // for (i = 0; i < fsg->nluns; ++i)
3000 // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
3001 break;
3002
3003 case FSG_STATE_INTERFACE_CHANGE:
3004 rc = do_set_interface(fsg, 0);
3005 if (fsg->ep0_req_tag != exception_req_tag)
3006 break;
3007 if (rc != 0) // STALL on errors
3008 fsg_set_halt(fsg, fsg->ep0);
3009 else // Complete the status stage
3010 ep0_queue(fsg);
3011 break;
3012
3013 case FSG_STATE_CONFIG_CHANGE:
3014 rc = do_set_config(fsg, new_config);
3015 if (fsg->ep0_req_tag != exception_req_tag)
3016 break;
3017 if (rc != 0) // STALL on errors
3018 fsg_set_halt(fsg, fsg->ep0);
3019 else // Complete the status stage
3020 ep0_queue(fsg);
3021 break;
3022
3023 case FSG_STATE_DISCONNECT:
Michal Nazarewiczb6058d02009-10-28 16:57:14 +01003024 for (i = 0; i < fsg->nluns; ++i)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003025 fsg_lun_fsync_sub(fsg->luns + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 do_set_config(fsg, 0); // Unconfigured state
3027 break;
3028
3029 case FSG_STATE_EXIT:
3030 case FSG_STATE_TERMINATED:
3031 do_set_config(fsg, 0); // Free resources
3032 spin_lock_irq(&fsg->lock);
3033 fsg->state = FSG_STATE_TERMINATED; // Stop the thread
3034 spin_unlock_irq(&fsg->lock);
3035 break;
3036 }
3037}
3038
3039
3040/*-------------------------------------------------------------------------*/
3041
3042static int fsg_main_thread(void *fsg_)
3043{
John Daiker7ca46b82006-12-29 19:02:06 -08003044 struct fsg_dev *fsg = fsg_;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 /* Allow the thread to be killed by a signal, but set the signal mask
3047 * to block everything but INT, TERM, KILL, and USR1. */
Oleg Nesterov8cfbe7e2007-05-30 11:06:33 -04003048 allow_signal(SIGINT);
3049 allow_signal(SIGTERM);
3050 allow_signal(SIGKILL);
3051 allow_signal(SIGUSR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003053 /* Allow the thread to be frozen */
3054 set_freezable();
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 /* Arrange for userspace references to be interpreted as kernel
3057 * pointers. That way we can pass a kernel pointer to a routine
3058 * that expects a __user pointer and it will work okay. */
3059 set_fs(get_ds());
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 /* The main loop */
3062 while (fsg->state != FSG_STATE_TERMINATED) {
3063 if (exception_in_progress(fsg) || signal_pending(current)) {
3064 handle_exception(fsg);
3065 continue;
3066 }
3067
3068 if (!fsg->running) {
3069 sleep_thread(fsg);
3070 continue;
3071 }
3072
3073 if (get_next_command(fsg))
3074 continue;
3075
3076 spin_lock_irq(&fsg->lock);
3077 if (!exception_in_progress(fsg))
3078 fsg->state = FSG_STATE_DATA_PHASE;
3079 spin_unlock_irq(&fsg->lock);
3080
3081 if (do_scsi_command(fsg) || finish_reply(fsg))
3082 continue;
3083
3084 spin_lock_irq(&fsg->lock);
3085 if (!exception_in_progress(fsg))
3086 fsg->state = FSG_STATE_STATUS_PHASE;
3087 spin_unlock_irq(&fsg->lock);
3088
3089 if (send_status(fsg))
3090 continue;
3091
3092 spin_lock_irq(&fsg->lock);
3093 if (!exception_in_progress(fsg))
3094 fsg->state = FSG_STATE_IDLE;
3095 spin_unlock_irq(&fsg->lock);
3096 }
3097
Alan Stern22efcf42005-09-26 16:12:02 -04003098 spin_lock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 fsg->thread_task = NULL;
Alan Stern22efcf42005-09-26 16:12:02 -04003100 spin_unlock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Alan Stern82a10a82009-04-16 15:37:28 -04003102 /* If we are exiting because of a signal, unregister the
3103 * gadget driver. */
3104 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 usb_gadget_unregister_driver(&fsg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 /* Let the unbind and cleanup routines know the thread has exited */
3108 complete_and_exit(&fsg->thread_notifier, 0);
3109}
3110
3111
3112/*-------------------------------------------------------------------------*/
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115/* The write permissions and store_xxx pointers are set in fsg_bind() */
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003116static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003117static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003118static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120
3121/*-------------------------------------------------------------------------*/
3122
Alan Stern87c42522005-11-09 16:59:56 -05003123static void fsg_release(struct kref *ref)
3124{
3125 struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
3126
3127 kfree(fsg->luns);
3128 kfree(fsg);
3129}
3130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131static void lun_release(struct device *dev)
3132{
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003133 struct rw_semaphore *filesem = dev_get_drvdata(dev);
3134 struct fsg_dev *fsg =
3135 container_of(filesem, struct fsg_dev, filesem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Alan Stern87c42522005-11-09 16:59:56 -05003137 kref_put(&fsg->ref, fsg_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
David Brownella3536782006-07-06 15:48:53 -07003140static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
3142 struct fsg_dev *fsg = get_gadget_data(gadget);
3143 int i;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003144 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 struct usb_request *req = fsg->ep0req;
3146
3147 DBG(fsg, "unbind\n");
3148 clear_bit(REGISTERED, &fsg->atomic_bitflags);
3149
3150 /* Unregister the sysfs attribute files and the LUNs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 for (i = 0; i < fsg->nluns; ++i) {
3152 curlun = &fsg->luns[i];
3153 if (curlun->registered) {
Michal Nazarewicz452ee0e2010-08-12 17:43:50 +02003154 device_remove_file(&curlun->dev, &dev_attr_nofua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 device_remove_file(&curlun->dev, &dev_attr_ro);
3156 device_remove_file(&curlun->dev, &dev_attr_file);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003157 fsg_lun_close(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 device_unregister(&curlun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 curlun->registered = 0;
3160 }
3161 }
3162
3163 /* If the thread isn't already dead, tell it to exit now */
3164 if (fsg->state != FSG_STATE_TERMINATED) {
3165 raise_exception(fsg, FSG_STATE_EXIT);
3166 wait_for_completion(&fsg->thread_notifier);
3167
3168 /* The cleanup routine waits for this completion also */
3169 complete(&fsg->thread_notifier);
3170 }
3171
3172 /* Free the data buffers */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003173 for (i = 0; i < FSG_NUM_BUFFERS; ++i)
David Brownell9d8bab52007-07-01 11:04:54 -07003174 kfree(fsg->buffhds[i].buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /* Free the request and buffer for endpoint 0 */
3177 if (req) {
David Brownell9d8bab52007-07-01 11:04:54 -07003178 kfree(req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 usb_ep_free_request(fsg->ep0, req);
3180 }
3181
3182 set_gadget_data(gadget, NULL);
3183}
3184
3185
3186static int __init check_parameters(struct fsg_dev *fsg)
3187{
3188 int prot;
David Brownell91e79c92005-07-13 15:18:30 -07003189 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 /* Store the default values */
3192 mod_data.transport_type = USB_PR_BULK;
3193 mod_data.transport_name = "Bulk-only";
3194 mod_data.protocol_type = USB_SC_SCSI;
3195 mod_data.protocol_name = "Transparent SCSI";
3196
Alan Sternce459ec2009-02-24 16:19:47 -05003197 /* Some peripheral controllers are known not to be able to
3198 * halt bulk endpoints correctly. If one of them is present,
3199 * disable stalls.
3200 */
Christoph Egger90f79762010-02-05 13:24:12 +01003201 if (gadget_is_at91(fsg->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 mod_data.can_stall = 0;
3203
3204 if (mod_data.release == 0xffff) { // Parameter wasn't set
Christoph Egger90f79762010-02-05 13:24:12 +01003205 gcnum = usb_gadget_controller_number(fsg->gadget);
David Brownell91e79c92005-07-13 15:18:30 -07003206 if (gcnum >= 0)
3207 mod_data.release = 0x0300 + gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 else {
Arjan van de Venb6c63932008-07-25 01:45:52 -07003209 WARNING(fsg, "controller '%s' not recognized\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 fsg->gadget->name);
3211 mod_data.release = 0x0399;
3212 }
3213 }
3214
3215 prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
3216
3217#ifdef CONFIG_USB_FILE_STORAGE_TEST
3218 if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
3219 ; // Use default setting
3220 } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
3221 mod_data.transport_type = USB_PR_CB;
3222 mod_data.transport_name = "Control-Bulk";
3223 } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
3224 mod_data.transport_type = USB_PR_CBI;
3225 mod_data.transport_name = "Control-Bulk-Interrupt";
3226 } else {
3227 ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
3228 return -EINVAL;
3229 }
3230
3231 if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
3232 prot == USB_SC_SCSI) {
3233 ; // Use default setting
3234 } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
3235 prot == USB_SC_RBC) {
3236 mod_data.protocol_type = USB_SC_RBC;
3237 mod_data.protocol_name = "RBC";
3238 } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
3239 strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
3240 prot == USB_SC_8020) {
3241 mod_data.protocol_type = USB_SC_8020;
3242 mod_data.protocol_name = "8020i (ATAPI)";
3243 } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
3244 prot == USB_SC_QIC) {
3245 mod_data.protocol_type = USB_SC_QIC;
3246 mod_data.protocol_name = "QIC-157";
3247 } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
3248 prot == USB_SC_UFI) {
3249 mod_data.protocol_type = USB_SC_UFI;
3250 mod_data.protocol_name = "UFI";
3251 } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
3252 prot == USB_SC_8070) {
3253 mod_data.protocol_type = USB_SC_8070;
3254 mod_data.protocol_name = "8070i";
3255 } else {
3256 ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
3257 return -EINVAL;
3258 }
3259
3260 mod_data.buflen &= PAGE_CACHE_MASK;
3261 if (mod_data.buflen <= 0) {
3262 ERROR(fsg, "invalid buflen\n");
3263 return -ETOOSMALL;
3264 }
Yann Cantin87eb1be2010-06-05 23:06:31 +02003265
Michal Nazarewicz56706492010-07-22 14:16:37 +02003266#endif /* CONFIG_USB_FILE_STORAGE_TEST */
3267
Yann Cantin87eb1be2010-06-05 23:06:31 +02003268 /* Serial string handling.
3269 * On a real device, the serial string would be loaded
3270 * from permanent storage. */
Michal Nazarewicz56706492010-07-22 14:16:37 +02003271 if (mod_data.serial) {
Yann Cantin87eb1be2010-06-05 23:06:31 +02003272 const char *ch;
3273 unsigned len = 0;
3274
3275 /* Sanity check :
3276 * The CB[I] specification limits the serial string to
3277 * 12 uppercase hexadecimal characters.
3278 * BBB need at least 12 uppercase hexadecimal characters,
3279 * with a maximum of 126. */
Michal Nazarewicz56706492010-07-22 14:16:37 +02003280 for (ch = mod_data.serial; *ch; ++ch) {
Yann Cantin87eb1be2010-06-05 23:06:31 +02003281 ++len;
3282 if ((*ch < '0' || *ch > '9') &&
3283 (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
3284 WARNING(fsg,
Alan Sternd8087422010-09-03 11:15:41 -04003285 "Invalid serial string character: %c\n",
Yann Cantin87eb1be2010-06-05 23:06:31 +02003286 *ch);
Alan Sternd8087422010-09-03 11:15:41 -04003287 goto no_serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003288 }
3289 }
3290 if (len > 126 ||
3291 (mod_data.transport_type == USB_PR_BULK && len < 12) ||
3292 (mod_data.transport_type != USB_PR_BULK && len > 12)) {
Alan Sternd8087422010-09-03 11:15:41 -04003293 WARNING(fsg, "Invalid serial string length!\n");
3294 goto no_serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003295 }
Michal Nazarewicz56706492010-07-22 14:16:37 +02003296 fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003297 } else {
Alan Sternd8087422010-09-03 11:15:41 -04003298 WARNING(fsg, "No serial-number string provided!\n");
3299 no_serial:
3300 device_desc.iSerialNumber = 0;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003301 }
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return 0;
3304}
3305
3306
Michal Nazarewicze12995e2010-08-12 17:43:52 +02003307static int __init fsg_bind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308{
3309 struct fsg_dev *fsg = the_fsg;
3310 int rc;
3311 int i;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003312 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct usb_ep *ep;
3314 struct usb_request *req;
3315 char *pathbuf, *p;
3316
3317 fsg->gadget = gadget;
3318 set_gadget_data(gadget, fsg);
3319 fsg->ep0 = gadget->ep0;
3320 fsg->ep0->driver_data = fsg;
3321
3322 if ((rc = check_parameters(fsg)) != 0)
3323 goto out;
3324
3325 if (mod_data.removable) { // Enable the store_xxx attributes
Alan Stern12aae682008-11-20 14:13:12 -05003326 dev_attr_file.attr.mode = 0644;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003327 dev_attr_file.store = fsg_store_file;
Alan Stern12aae682008-11-20 14:13:12 -05003328 if (!mod_data.cdrom) {
3329 dev_attr_ro.attr.mode = 0644;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003330 dev_attr_ro.store = fsg_store_ro;
Alan Stern12aae682008-11-20 14:13:12 -05003331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
3333
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003334 /* Only for removable media? */
3335 dev_attr_nofua.attr.mode = 0644;
3336 dev_attr_nofua.store = fsg_store_nofua;
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 /* Find out how many LUNs there should be */
3339 i = mod_data.nluns;
3340 if (i == 0)
David Brownell2e806f62007-08-02 00:03:39 -07003341 i = max(mod_data.num_filenames, 1u);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003342 if (i > FSG_MAX_LUNS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 ERROR(fsg, "invalid number of LUNs: %d\n", i);
3344 rc = -EINVAL;
3345 goto out;
3346 }
3347
3348 /* Create the LUNs, open their backing files, and register the
3349 * LUN devices in sysfs. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003350 fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 if (!fsg->luns) {
3352 rc = -ENOMEM;
3353 goto out;
3354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 fsg->nluns = i;
3356
3357 for (i = 0; i < fsg->nluns; ++i) {
3358 curlun = &fsg->luns[i];
Michal Nazarewicze909ef52009-10-28 16:57:16 +01003359 curlun->cdrom = !!mod_data.cdrom;
3360 curlun->ro = mod_data.cdrom || mod_data.ro[i];
3361 curlun->initially_ro = curlun->ro;
3362 curlun->removable = mod_data.removable;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003363 curlun->nofua = mod_data.nofua[i];
Alan Stern2de9eae2006-09-25 14:31:15 -04003364 curlun->dev.release = lun_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 curlun->dev.parent = &gadget->dev;
3366 curlun->dev.driver = &fsg_driver.driver;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003367 dev_set_drvdata(&curlun->dev, &fsg->filesem);
Kay Sievers0031a062008-05-02 06:02:41 +02003368 dev_set_name(&curlun->dev,"%s-lun%d",
3369 dev_name(&gadget->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003371 kref_get(&fsg->ref);
3372 rc = device_register(&curlun->dev);
3373 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003375 put_device(&curlun->dev);
Alan Stern2de9eae2006-09-25 14:31:15 -04003376 goto out;
3377 }
3378 curlun->registered = 1;
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003379
3380 rc = device_create_file(&curlun->dev, &dev_attr_ro);
3381 if (rc)
3382 goto out;
3383 rc = device_create_file(&curlun->dev, &dev_attr_nofua);
3384 if (rc)
3385 goto out;
3386 rc = device_create_file(&curlun->dev, &dev_attr_file);
3387 if (rc)
3388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Alan Sternaafe5bd2006-03-31 11:46:43 -05003390 if (mod_data.file[i] && *mod_data.file[i]) {
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003391 rc = fsg_lun_open(curlun, mod_data.file[i]);
3392 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 goto out;
3394 } else if (!mod_data.removable) {
3395 ERROR(fsg, "no file given for LUN%d\n", i);
3396 rc = -EINVAL;
3397 goto out;
3398 }
3399 }
3400
3401 /* Find all the endpoints we will use */
3402 usb_ep_autoconfig_reset(gadget);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003403 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_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->bulk_in = ep;
3408
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003409 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 if (!ep)
3411 goto autoconf_fail;
3412 ep->driver_data = fsg; // claim the endpoint
3413 fsg->bulk_out = ep;
3414
3415 if (transport_is_cbi()) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003416 ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 if (!ep)
3418 goto autoconf_fail;
3419 ep->driver_data = fsg; // claim the endpoint
3420 fsg->intr_in = ep;
3421 }
3422
3423 /* Fix up the descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 device_desc.idVendor = cpu_to_le16(mod_data.vendor);
3425 device_desc.idProduct = cpu_to_le16(mod_data.product);
3426 device_desc.bcdDevice = cpu_to_le16(mod_data.release);
3427
3428 i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003429 fsg_intf_desc.bNumEndpoints = i;
3430 fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
3431 fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
3432 fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
David Brownell2e806f62007-08-02 00:03:39 -07003434 if (gadget_is_dualspeed(gadget)) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003435 fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
David Brownell2e806f62007-08-02 00:03:39 -07003437 /* Assume endpoint addresses are the same for both speeds */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003438 fsg_hs_bulk_in_desc.bEndpointAddress =
3439 fsg_fs_bulk_in_desc.bEndpointAddress;
3440 fsg_hs_bulk_out_desc.bEndpointAddress =
3441 fsg_fs_bulk_out_desc.bEndpointAddress;
3442 fsg_hs_intr_in_desc.bEndpointAddress =
3443 fsg_fs_intr_in_desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445
David Brownell2e806f62007-08-02 00:03:39 -07003446 if (gadget_is_otg(gadget))
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003447 fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
David Brownell2e806f62007-08-02 00:03:39 -07003448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 rc = -ENOMEM;
3450
3451 /* Allocate the request and buffer for endpoint 0 */
3452 fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
3453 if (!req)
3454 goto out;
David Brownell9d8bab52007-07-01 11:04:54 -07003455 req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (!req->buf)
3457 goto out;
3458 req->complete = ep0_complete;
3459
3460 /* Allocate the data buffers */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003461 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 struct fsg_buffhd *bh = &fsg->buffhds[i];
3463
Alan Stern14cd5f82006-03-23 15:07:25 -05003464 /* Allocate for the bulk-in endpoint. We assume that
3465 * the buffer will also work with the bulk-out (and
3466 * interrupt-in) endpoint. */
David Brownell9d8bab52007-07-01 11:04:54 -07003467 bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (!bh->buf)
3469 goto out;
3470 bh->next = bh + 1;
3471 }
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003472 fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474 /* This should reflect the actual gadget power source */
3475 usb_gadget_set_selfpowered(gadget);
3476
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003477 snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
3478 "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07003479 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 gadget->name);
3481
Alan Stern22efcf42005-09-26 16:12:02 -04003482 fsg->thread_task = kthread_create(fsg_main_thread, fsg,
3483 "file-storage-gadget");
3484 if (IS_ERR(fsg->thread_task)) {
3485 rc = PTR_ERR(fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 goto out;
Alan Stern22efcf42005-09-26 16:12:02 -04003487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
3489 INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
Alan Stern664a51a2011-06-15 16:31:37 -04003490 INFO(fsg, "NOTE: This driver is deprecated. "
3491 "Consider using g_mass_storage instead.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
3493
3494 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
3495 for (i = 0; i < fsg->nluns; ++i) {
3496 curlun = &fsg->luns[i];
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003497 if (fsg_lun_is_open(curlun)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 p = NULL;
3499 if (pathbuf) {
Jan Blunckcf28b482008-02-14 19:38:44 -08003500 p = d_path(&curlun->filp->f_path,
3501 pathbuf, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 if (IS_ERR(p))
3503 p = NULL;
3504 }
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003505 LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
3506 curlun->ro, curlun->nofua, (p ? p : "(error)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 }
3508 }
3509 kfree(pathbuf);
3510
3511 DBG(fsg, "transport=%s (x%02x)\n",
3512 mod_data.transport_name, mod_data.transport_type);
3513 DBG(fsg, "protocol=%s (x%02x)\n",
3514 mod_data.protocol_name, mod_data.protocol_type);
3515 DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
3516 mod_data.vendor, mod_data.product, mod_data.release);
Alan Stern12aae682008-11-20 14:13:12 -05003517 DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 mod_data.removable, mod_data.can_stall,
Alan Stern12aae682008-11-20 14:13:12 -05003519 mod_data.cdrom, mod_data.buflen);
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003520 DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
Alan Sterna922c682005-10-06 16:38:45 -04003521
3522 set_bit(REGISTERED, &fsg->atomic_bitflags);
3523
3524 /* Tell the thread to start working */
3525 wake_up_process(fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 return 0;
3527
3528autoconf_fail:
3529 ERROR(fsg, "unable to autoconfigure all endpoints\n");
3530 rc = -ENOTSUPP;
3531
3532out:
3533 fsg->state = FSG_STATE_TERMINATED; // The thread is dead
3534 fsg_unbind(gadget);
Felipe Balbi889394b2008-12-08 13:50:27 +02003535 complete(&fsg->thread_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return rc;
3537}
3538
3539
3540/*-------------------------------------------------------------------------*/
3541
3542static void fsg_suspend(struct usb_gadget *gadget)
3543{
3544 struct fsg_dev *fsg = get_gadget_data(gadget);
3545
3546 DBG(fsg, "suspend\n");
3547 set_bit(SUSPENDED, &fsg->atomic_bitflags);
3548}
3549
3550static void fsg_resume(struct usb_gadget *gadget)
3551{
3552 struct fsg_dev *fsg = get_gadget_data(gadget);
3553
3554 DBG(fsg, "resume\n");
3555 clear_bit(SUSPENDED, &fsg->atomic_bitflags);
3556}
3557
3558
3559/*-------------------------------------------------------------------------*/
3560
3561static struct usb_gadget_driver fsg_driver = {
3562#ifdef CONFIG_USB_GADGET_DUALSPEED
3563 .speed = USB_SPEED_HIGH,
3564#else
3565 .speed = USB_SPEED_FULL,
3566#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003567 .function = (char *) fsg_string_product,
David Brownell6bea4762006-12-05 03:15:33 -08003568 .unbind = fsg_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 .disconnect = fsg_disconnect,
3570 .setup = fsg_setup,
3571 .suspend = fsg_suspend,
3572 .resume = fsg_resume,
3573
3574 .driver = {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003575 .name = DRIVER_NAME,
Ben Dooksd0d50492005-10-10 10:52:33 +01003576 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 // .release = ...
3578 // .suspend = ...
3579 // .resume = ...
3580 },
3581};
3582
3583
3584static int __init fsg_alloc(void)
3585{
3586 struct fsg_dev *fsg;
3587
Alan Sterna922c682005-10-06 16:38:45 -04003588 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 if (!fsg)
3590 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 spin_lock_init(&fsg->lock);
3592 init_rwsem(&fsg->filesem);
Alan Stern87c42522005-11-09 16:59:56 -05003593 kref_init(&fsg->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 init_completion(&fsg->thread_notifier);
3595
3596 the_fsg = fsg;
3597 return 0;
3598}
3599
3600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601static int __init fsg_init(void)
3602{
3603 int rc;
3604 struct fsg_dev *fsg;
3605
3606 if ((rc = fsg_alloc()) != 0)
3607 return rc;
3608 fsg = the_fsg;
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02003609 if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
Alan Stern87c42522005-11-09 16:59:56 -05003610 kref_put(&fsg->ref, fsg_release);
Alan Sterna922c682005-10-06 16:38:45 -04003611 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
3613module_init(fsg_init);
3614
3615
3616static void __exit fsg_cleanup(void)
3617{
3618 struct fsg_dev *fsg = the_fsg;
3619
3620 /* Unregister the driver iff the thread hasn't already done so */
3621 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
3622 usb_gadget_unregister_driver(&fsg_driver);
3623
3624 /* Wait for the thread to finish up */
3625 wait_for_completion(&fsg->thread_notifier);
3626
Alan Stern87c42522005-11-09 16:59:56 -05003627 kref_put(&fsg->ref, fsg_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629module_exit(fsg_cleanup);