blob: aebfb81f3ba4e644b809805df810d45985c1af03 [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
72 * need be only one backing file per LUN. Note also that the CD-ROM block
73 * length is set to 512 rather than the more common value 2048.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 * Requirements are modest; only a bulk-in and a bulk-out endpoint are
76 * needed (an interrupt-out endpoint is also needed for CBI). The memory
77 * requirement amounts to two 16K buffers, size configurable by a parameter.
78 * Support is included for both full-speed and high-speed operation.
79 *
Alan Stern14cd5f82006-03-23 15:07:25 -050080 * Note that the driver is slightly non-portable in that it assumes a
81 * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
82 * interrupt-in endpoints. With most device controllers this isn't an
83 * issue, but there may be some with hardware restrictions that prevent
84 * a buffer from being used by more than one endpoint.
85 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * Module options:
87 *
88 * file=filename[,filename...]
89 * Required if "removable" is not set, names of
90 * the files or block devices used for
91 * backing storage
Alan Sternd8087422010-09-03 11:15:41 -040092 * serial=HHHH... Required serial number (string of hex chars)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * ro=b[,b...] Default false, booleans for read-only access
94 * removable Default false, boolean for removable media
95 * luns=N Default N = number of filenames, number of
96 * LUNs to support
Andy Shevchenkoa93917d2010-07-22 17:53:56 +030097 * nofua=b[,b...] Default false, booleans for ignore FUA flag
98 * in SCSI WRITE(10,12) commands
Alan Sternd794ac72005-04-18 12:43:25 -040099 * stall Default determined according to the type of
100 * USB device controller (usually true),
101 * boolean to permit the driver to halt
102 * bulk endpoints
Alan Stern12aae682008-11-20 14:13:12 -0500103 * cdrom Default false, boolean for whether to emulate
104 * a CD-ROM drive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
106 * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
107 * ATAPI, QIC, UFI, 8070, or SCSI;
108 * also 1 - 6)
109 * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
110 * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
111 * release=0xRRRR Override the USB release number (bcdDevice)
112 * buflen=N Default N=16384, buffer size used (will be
113 * rounded down to a multiple of
114 * PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
Alan Sternd8087422010-09-03 11:15:41 -0400116 * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300117 * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
118 * default values are used for everything else.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *
120 * The pathnames of the backing files and the ro settings are available in
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300121 * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
122 * the gadget's sysfs directory. If the "removable" option is set, writing to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * these files will simulate ejecting/loading the medium (writing an empty
124 * line means eject) and adjusting a write-enable tab. Changes to the ro
Alan Stern12aae682008-11-20 14:13:12 -0500125 * setting are not allowed when the medium is loaded or if CD-ROM emulation
126 * is being used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
128 * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
Alan Sternaafe5bd2006-03-31 11:46:43 -0500129 * The driver's SCSI command interface was based on the "Information
130 * technology - Small Computer System Interface - 2" document from
131 * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
132 * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
133 * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
134 * "Universal Serial Bus Mass Storage Class UFI Command Specification"
135 * document, Revision 1.0, December 14, 1998, available at
136 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
138
139
140/*
141 * Driver Design
142 *
143 * The FSG driver is fairly straightforward. There is a main kernel
144 * thread that handles most of the work. Interrupt routines field
145 * callbacks from the controller driver: bulk- and interrupt-request
146 * completion notifications, endpoint-0 events, and disconnect events.
147 * Completion events are passed to the main thread by wakeup calls. Many
148 * ep0 requests are handled at interrupt time, but SetInterface,
149 * SetConfiguration, and device reset requests are forwarded to the
150 * thread in the form of "exceptions" using SIGUSR1 signals (since they
151 * should interrupt any ongoing file I/O operations).
152 *
153 * The thread's main routine implements the standard command/data/status
154 * parts of a SCSI interaction. It and its subroutines are full of tests
155 * for pending signals/exceptions -- all this polling is necessary since
156 * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
157 * indication that the driver really wants to be running in userspace.)
158 * An important point is that so long as the thread is alive it keeps an
159 * open reference to the backing file. This will prevent unmounting
160 * the backing file's underlying filesystem and could cause problems
161 * during system shutdown, for example. To prevent such problems, the
162 * thread catches INT, TERM, and KILL signals and converts them into
163 * an EXIT exception.
164 *
165 * In normal operation the main thread is started during the gadget's
166 * fsg_bind() callback and stopped during fsg_unbind(). But it can also
167 * exit when it receives a signal, and there's no point leaving the
168 * gadget running when the thread is dead. So just before the thread
169 * exits, it deregisters the gadget driver. This makes things a little
170 * tricky: The driver is deregistered at two places, and the exiting
171 * thread can indirectly call fsg_unbind() which in turn can tell the
172 * thread to exit. The first problem is resolved through the use of the
173 * REGISTERED atomic bitflag; the driver will only be deregistered once.
174 * The second problem is resolved by having fsg_unbind() check
175 * fsg->state; it won't try to stop the thread if the state is already
176 * FSG_STATE_TERMINATED.
177 *
178 * To provide maximum throughput, the driver uses a circular pipeline of
179 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
180 * arbitrarily long; in practice the benefits don't justify having more
181 * than 2 stages (i.e., double buffering). But it helps to think of the
182 * pipeline as being a long one. Each buffer head contains a bulk-in and
183 * a bulk-out request pointer (since the buffer can be used for both
184 * output and input -- directions always are given from the host's
185 * point of view) as well as a pointer to the buffer and various state
186 * variables.
187 *
188 * Use of the pipeline follows a simple protocol. There is a variable
189 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
190 * At any time that buffer head may still be in use from an earlier
191 * request, so each buffer head has a state variable indicating whether
192 * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
193 * buffer head to be EMPTY, filling the buffer either by file I/O or by
194 * USB I/O (during which the buffer head is BUSY), and marking the buffer
195 * head FULL when the I/O is complete. Then the buffer will be emptied
196 * (again possibly by USB I/O, during which it is marked BUSY) and
197 * finally marked EMPTY again (possibly by a completion routine).
198 *
199 * A module parameter tells the driver to avoid stalling the bulk
200 * endpoints wherever the transport specification allows. This is
201 * necessary for some UDCs like the SuperH, which cannot reliably clear a
202 * halt on a bulk endpoint. However, under certain circumstances the
203 * Bulk-only specification requires a stall. In such cases the driver
204 * will halt the endpoint and set a flag indicating that it should clear
205 * the halt in software during the next device reset. Hopefully this
206 * will permit everything to work correctly. Furthermore, although the
207 * specification allows the bulk-out endpoint to halt when the host sends
208 * too much data, implementing this would cause an unavoidable race.
209 * The driver will always use the "no-stall" approach for OUT transfers.
210 *
211 * One subtle point concerns sending status-stage responses for ep0
212 * requests. Some of these requests, such as device reset, can involve
213 * interrupting an ongoing file I/O operation, which might take an
214 * arbitrarily long time. During that delay the host might give up on
215 * the original ep0 request and issue a new one. When that happens the
216 * driver should not notify the host about completion of the original
217 * request, as the host will no longer be waiting for it. So the driver
218 * assigns to each ep0 request a unique tag, and it keeps track of the
219 * tag value of the request associated with a long-running exception
220 * (device-reset, interface-change, or configuration-change). When the
221 * exception handler is finished, the status-stage response is submitted
222 * only if the current ep0 request tag is equal to the exception request
223 * tag. Thus only the most recently received ep0 request will get a
224 * status-stage response.
225 *
226 * Warning: This driver source file is too long. It ought to be split up
227 * into a header file plus about 3 separate .c files, to handle the details
228 * of the Gadget, USB Mass Storage, and SCSI protocols.
229 */
230
231
David Brownell2e806f62007-08-02 00:03:39 -0700232/* #define VERBOSE_DEBUG */
Alan Stern79a7d9e2007-08-08 17:10:11 -0400233/* #define DUMP_MSGS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#include <linux/completion.h>
238#include <linux/dcache.h>
239#include <linux/delay.h>
240#include <linux/device.h>
241#include <linux/fcntl.h>
242#include <linux/file.h>
243#include <linux/fs.h>
Alan Stern87c42522005-11-09 16:59:56 -0500244#include <linux/kref.h>
Alan Stern22efcf42005-09-26 16:12:02 -0400245#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#include <linux/limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#include <linux/slab.h>
249#include <linux/spinlock.h>
250#include <linux/string.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -0800251#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
David Brownell5f848132006-12-16 15:34:53 -0800254#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -0700255#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257#include "gadget_chips.h"
258
259
David Brownell352e2b92008-08-18 17:43:25 -0700260
261/*
262 * Kbuild is not very cooperative with respect to linking separately
263 * compiled library objects into one module. So for now we won't use
264 * separate compilation ... ensuring init/exit sections work to shrink
265 * the runtime footprint, and giving us at least some parts of what
266 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
267 */
268#include "usbstring.c"
269#include "config.c"
270#include "epautoconf.c"
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*-------------------------------------------------------------------------*/
273
274#define DRIVER_DESC "File-backed Storage Gadget"
275#define DRIVER_NAME "g_file_storage"
Alan Sternd8087422010-09-03 11:15:41 -0400276#define DRIVER_VERSION "1 September 2010"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100278static char fsg_string_manufacturer[64];
279static const char fsg_string_product[] = DRIVER_DESC;
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100280static const char fsg_string_config[] = "Self-powered";
281static const char fsg_string_interface[] = "Mass Storage";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100283
284#include "storage_common.c"
285
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287MODULE_DESCRIPTION(DRIVER_DESC);
288MODULE_AUTHOR("Alan Stern");
289MODULE_LICENSE("Dual BSD/GPL");
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/*
292 * This driver assumes self-powered hardware and has no way for users to
293 * trigger remote wakeup. It uses autoconfiguration to select endpoints
294 * and endpoint addresses.
295 */
296
297
298/*-------------------------------------------------------------------------*/
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/* Encapsulate the module parameter settings */
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static struct {
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100304 char *file[FSG_MAX_LUNS];
Alan Sternd8087422010-09-03 11:15:41 -0400305 char *serial;
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100306 int ro[FSG_MAX_LUNS];
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300307 int nofua[FSG_MAX_LUNS];
David Brownell2e806f62007-08-02 00:03:39 -0700308 unsigned int num_filenames;
309 unsigned int num_ros;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300310 unsigned int num_nofuas;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unsigned int nluns;
312
Alan Sternd794ac72005-04-18 12:43:25 -0400313 int removable;
314 int can_stall;
Alan Stern12aae682008-11-20 14:13:12 -0500315 int cdrom;
Alan Sternd794ac72005-04-18 12:43:25 -0400316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 char *transport_parm;
318 char *protocol_parm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 unsigned short vendor;
320 unsigned short product;
321 unsigned short release;
322 unsigned int buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 int transport_type;
325 char *transport_name;
326 int protocol_type;
327 char *protocol_name;
328
329} mod_data = { // Default values
330 .transport_parm = "BBB",
331 .protocol_parm = "SCSI",
332 .removable = 0,
Alan Sternd794ac72005-04-18 12:43:25 -0400333 .can_stall = 1,
Alan Stern12aae682008-11-20 14:13:12 -0500334 .cdrom = 0,
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100335 .vendor = FSG_VENDOR_ID,
336 .product = FSG_PRODUCT_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .release = 0xffff, // Use controller chip type
338 .buflen = 16384,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 };
340
341
Alan Sternaafe5bd2006-03-31 11:46:43 -0500342module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
343 S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344MODULE_PARM_DESC(file, "names of backing files or devices");
345
Alan Sternd8087422010-09-03 11:15:41 -0400346module_param_named(serial, mod_data.serial, charp, S_IRUGO);
347MODULE_PARM_DESC(serial, "USB serial number");
348
Alan Sternaafe5bd2006-03-31 11:46:43 -0500349module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350MODULE_PARM_DESC(ro, "true to force read-only");
351
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300352module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
353 S_IRUGO);
354MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
357MODULE_PARM_DESC(luns, "number of LUNs");
358
359module_param_named(removable, mod_data.removable, bool, S_IRUGO);
360MODULE_PARM_DESC(removable, "true to simulate removable media");
361
Alan Sternd794ac72005-04-18 12:43:25 -0400362module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
363MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
364
Alan Stern12aae682008-11-20 14:13:12 -0500365module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
366MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/* In the non-TEST version, only the module parameters listed above
369 * are available. */
370#ifdef CONFIG_USB_FILE_STORAGE_TEST
371
372module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
373MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
374
375module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
376MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
377 "8070, or SCSI)");
378
379module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
380MODULE_PARM_DESC(vendor, "USB Vendor ID");
381
382module_param_named(product, mod_data.product, ushort, S_IRUGO);
383MODULE_PARM_DESC(product, "USB Product ID");
384
385module_param_named(release, mod_data.release, ushort, S_IRUGO);
386MODULE_PARM_DESC(release, "USB release number");
387
388module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
389MODULE_PARM_DESC(buflen, "I/O buffer size");
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#endif /* CONFIG_USB_FILE_STORAGE_TEST */
392
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/*
395 * These definitions will permit the compiler to avoid generating code for
396 * parts of the driver that aren't used in the non-TEST version. Even gcc
397 * can recognize when a test of a constant expression yields a dead code
398 * path.
399 */
400
401#ifdef CONFIG_USB_FILE_STORAGE_TEST
402
403#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
404#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
405#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
406
407#else
408
409#define transport_is_bbb() 1
410#define transport_is_cbi() 0
411#define protocol_is_scsi() 1
412
413#endif /* CONFIG_USB_FILE_STORAGE_TEST */
414
415
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100416/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419struct fsg_dev {
420 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
421 spinlock_t lock;
422 struct usb_gadget *gadget;
423
424 /* filesem protects: backing files in use */
425 struct rw_semaphore filesem;
426
Alan Stern87c42522005-11-09 16:59:56 -0500427 /* reference counting: wait until all LUNs are released */
428 struct kref ref;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct usb_ep *ep0; // Handy copy of gadget->ep0
431 struct usb_request *ep0req; // For control responses
Alan Sterna21d4fe2005-11-29 12:04:24 -0500432 unsigned int ep0_req_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 const char *ep0req_name;
434
435 struct usb_request *intreq; // For interrupt responses
Alan Sterna21d4fe2005-11-29 12:04:24 -0500436 int intreq_busy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct fsg_buffhd *intr_buffhd;
438
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100439 unsigned int bulk_out_maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 enum fsg_state state; // For exception handling
441 unsigned int exception_req_tag;
442
443 u8 config, new_config;
444
445 unsigned int running : 1;
446 unsigned int bulk_in_enabled : 1;
447 unsigned int bulk_out_enabled : 1;
448 unsigned int intr_in_enabled : 1;
449 unsigned int phase_error : 1;
450 unsigned int short_packet_received : 1;
451 unsigned int bad_lun_okay : 1;
452
453 unsigned long atomic_bitflags;
454#define REGISTERED 0
Alan Sternb950bdb2008-04-14 11:45:29 -0400455#define IGNORE_BULK_OUT 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#define SUSPENDED 2
457
458 struct usb_ep *bulk_in;
459 struct usb_ep *bulk_out;
460 struct usb_ep *intr_in;
461
462 struct fsg_buffhd *next_buffhd_to_fill;
463 struct fsg_buffhd *next_buffhd_to_drain;
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100464 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int thread_wakeup_needed;
467 struct completion thread_notifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct task_struct *thread_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 int cmnd_size;
471 u8 cmnd[MAX_COMMAND_SIZE];
472 enum data_direction data_dir;
473 u32 data_size;
474 u32 data_size_from_cmnd;
475 u32 tag;
476 unsigned int lun;
477 u32 residue;
478 u32 usb_amount_left;
479
480 /* The CB protocol offers no way for a host to know when a command
481 * has completed. As a result the next command may arrive early,
482 * and we will still have to handle it. For that reason we need
483 * a buffer to store new commands when using CB (or CBI, which
484 * does not oblige a host to wait for command completion either). */
485 int cbbuf_cmnd_size;
486 u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
487
488 unsigned int nluns;
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100489 struct fsg_lun *luns;
490 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491};
492
493typedef void (*fsg_routine_t)(struct fsg_dev *);
494
Alan Stern79a7d9e2007-08-08 17:10:11 -0400495static int exception_in_progress(struct fsg_dev *fsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 return (fsg->state > FSG_STATE_IDLE);
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static struct fsg_dev *the_fsg;
501static struct usb_gadget_driver fsg_driver;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504/*-------------------------------------------------------------------------*/
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
507{
508 const char *name;
509
510 if (ep == fsg->bulk_in)
511 name = "bulk-in";
512 else if (ep == fsg->bulk_out)
513 name = "bulk-out";
514 else
515 name = ep->name;
516 DBG(fsg, "%s set halt\n", name);
517 return usb_ep_set_halt(ep);
518}
519
520
521/*-------------------------------------------------------------------------*/
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*
524 * DESCRIPTORS ... most are static, but strings and (full) configuration
525 * descriptors are built on demand. Also the (static) config and interface
526 * descriptors are adjusted during fsg_bind().
527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529/* There is only one configuration. */
530#define CONFIG_VALUE 1
531
532static struct usb_device_descriptor
533device_desc = {
534 .bLength = sizeof device_desc,
535 .bDescriptorType = USB_DT_DEVICE,
536
Harvey Harrison551509d2009-02-11 14:11:36 -0800537 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 .bDeviceClass = USB_CLASS_PER_INTERFACE,
539
540 /* The next three values can be overridden by module parameters */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100541 .idVendor = cpu_to_le16(FSG_VENDOR_ID),
542 .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
Harvey Harrison551509d2009-02-11 14:11:36 -0800543 .bcdDevice = cpu_to_le16(0xffff),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100545 .iManufacturer = FSG_STRING_MANUFACTURER,
546 .iProduct = FSG_STRING_PRODUCT,
547 .iSerialNumber = FSG_STRING_SERIAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .bNumConfigurations = 1,
549};
550
551static struct usb_config_descriptor
552config_desc = {
553 .bLength = sizeof config_desc,
554 .bDescriptorType = USB_DT_CONFIG,
555
556 /* wTotalLength computed by usb_gadget_config_buf() */
557 .bNumInterfaces = 1,
558 .bConfigurationValue = CONFIG_VALUE,
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100559 .iConfiguration = FSG_STRING_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
David Brownell36e893d2008-09-12 09:39:06 -0700561 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562};
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static struct usb_qualifier_descriptor
566dev_qualifier = {
567 .bLength = sizeof dev_qualifier,
568 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
569
Harvey Harrison551509d2009-02-11 14:11:36 -0800570 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 .bDeviceClass = USB_CLASS_PER_INTERFACE,
572
573 .bNumConfigurations = 1,
574};
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577
578/*
579 * Config descriptors must agree with the code that sets configurations
580 * and with code managing interfaces and their altsettings. They must
581 * also handle different speeds and other-speed requests.
582 */
583static int populate_config_buf(struct usb_gadget *gadget,
584 u8 *buf, u8 type, unsigned index)
585{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 enum usb_device_speed speed = gadget->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int len;
588 const struct usb_descriptor_header **function;
589
590 if (index > 0)
591 return -EINVAL;
592
David Brownell2e806f62007-08-02 00:03:39 -0700593 if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100595 function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
596 ? (const struct usb_descriptor_header **)fsg_hs_function
597 : (const struct usb_descriptor_header **)fsg_fs_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* for now, don't advertise srp-only devices */
David Brownell2e806f62007-08-02 00:03:39 -0700600 if (!gadget_is_otg(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 function++;
602
603 len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
604 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
605 return len;
606}
607
608
609/*-------------------------------------------------------------------------*/
610
611/* These routines may be called in process context or in_irq */
612
Alan Sterna21d4fe2005-11-29 12:04:24 -0500613/* Caller must hold fsg->lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static void wakeup_thread(struct fsg_dev *fsg)
615{
616 /* Tell the main thread that something has happened */
617 fsg->thread_wakeup_needed = 1;
Alan Sterna21d4fe2005-11-29 12:04:24 -0500618 if (fsg->thread_task)
619 wake_up_process(fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622
623static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
624{
625 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* Do nothing if a higher-priority exception is already in progress.
628 * If a lower-or-equal priority exception is in progress, preempt it
629 * and notify the main thread by sending it a signal. */
630 spin_lock_irqsave(&fsg->lock, flags);
631 if (fsg->state <= new_state) {
632 fsg->exception_req_tag = fsg->ep0_req_tag;
633 fsg->state = new_state;
Alan Stern22efcf42005-09-26 16:12:02 -0400634 if (fsg->thread_task)
635 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
636 fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 spin_unlock_irqrestore(&fsg->lock, flags);
639}
640
641
642/*-------------------------------------------------------------------------*/
643
644/* The disconnect callback and ep0 routines. These always run in_irq,
645 * except that ep0_queue() is called in the main thread to acknowledge
646 * completion of various requests: set config, set interface, and
647 * Bulk-only device reset. */
648
649static void fsg_disconnect(struct usb_gadget *gadget)
650{
651 struct fsg_dev *fsg = get_gadget_data(gadget);
652
653 DBG(fsg, "disconnect or port reset\n");
654 raise_exception(fsg, FSG_STATE_DISCONNECT);
655}
656
657
658static int ep0_queue(struct fsg_dev *fsg)
659{
660 int rc;
661
662 rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
663 if (rc != 0 && rc != -ESHUTDOWN) {
664
665 /* We can't do much more than wait for a reset */
Arjan van de Venb6c63932008-07-25 01:45:52 -0700666 WARNING(fsg, "error in submission: %s --> %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 fsg->ep0->name, rc);
668 }
669 return rc;
670}
671
672static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
673{
John Daiker7ca46b82006-12-29 19:02:06 -0800674 struct fsg_dev *fsg = ep->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 if (req->actual > 0)
677 dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
678 if (req->status || req->actual != req->length)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800679 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 req->status, req->actual, req->length);
681 if (req->status == -ECONNRESET) // Request was cancelled
682 usb_ep_fifo_flush(ep);
683
684 if (req->status == 0 && req->context)
685 ((fsg_routine_t) (req->context))(fsg);
686}
687
688
689/*-------------------------------------------------------------------------*/
690
691/* Bulk and interrupt endpoint completion handlers.
692 * These always run in_irq. */
693
694static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
695{
John Daiker7ca46b82006-12-29 19:02:06 -0800696 struct fsg_dev *fsg = ep->driver_data;
697 struct fsg_buffhd *bh = req->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (req->status || req->actual != req->length)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800700 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 req->status, req->actual, req->length);
702 if (req->status == -ECONNRESET) // Request was cancelled
703 usb_ep_fifo_flush(ep);
704
705 /* Hold the lock while we update the request and buffer states */
Alan Sterna21d4fe2005-11-29 12:04:24 -0500706 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 spin_lock(&fsg->lock);
708 bh->inreq_busy = 0;
709 bh->state = BUF_STATE_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 wakeup_thread(fsg);
Alan Sterna21d4fe2005-11-29 12:04:24 -0500711 spin_unlock(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
715{
John Daiker7ca46b82006-12-29 19:02:06 -0800716 struct fsg_dev *fsg = ep->driver_data;
717 struct fsg_buffhd *bh = req->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 dump_msg(fsg, "bulk-out", req->buf, req->actual);
Mian Yousaf Kaukab806e8f82011-03-24 12:20:13 +0100720 if (req->status || req->actual != req->length)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800721 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
Mian Yousaf Kaukab806e8f82011-03-24 12:20:13 +0100722 req->status, req->actual, req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (req->status == -ECONNRESET) // Request was cancelled
724 usb_ep_fifo_flush(ep);
725
726 /* Hold the lock while we update the request and buffer states */
Alan Sterna21d4fe2005-11-29 12:04:24 -0500727 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 spin_lock(&fsg->lock);
729 bh->outreq_busy = 0;
730 bh->state = BUF_STATE_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 wakeup_thread(fsg);
Alan Sterna21d4fe2005-11-29 12:04:24 -0500732 spin_unlock(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735
736#ifdef CONFIG_USB_FILE_STORAGE_TEST
737static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
738{
John Daiker7ca46b82006-12-29 19:02:06 -0800739 struct fsg_dev *fsg = ep->driver_data;
740 struct fsg_buffhd *bh = req->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (req->status || req->actual != req->length)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800743 DBG(fsg, "%s --> %d, %u/%u\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 req->status, req->actual, req->length);
745 if (req->status == -ECONNRESET) // Request was cancelled
746 usb_ep_fifo_flush(ep);
747
748 /* Hold the lock while we update the request and buffer states */
Alan Sterna21d4fe2005-11-29 12:04:24 -0500749 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 spin_lock(&fsg->lock);
751 fsg->intreq_busy = 0;
752 bh->state = BUF_STATE_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 wakeup_thread(fsg);
Alan Sterna21d4fe2005-11-29 12:04:24 -0500754 spin_unlock(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757#else
758static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
759{}
760#endif /* CONFIG_USB_FILE_STORAGE_TEST */
761
762
763/*-------------------------------------------------------------------------*/
764
765/* Ep0 class-specific handlers. These always run in_irq. */
766
767#ifdef CONFIG_USB_FILE_STORAGE_TEST
768static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
769{
770 struct usb_request *req = fsg->ep0req;
771 static u8 cbi_reset_cmnd[6] = {
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +0200772 SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* Error in command transfer? */
775 if (req->status || req->length != req->actual ||
776 req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
777
778 /* Not all controllers allow a protocol stall after
779 * receiving control-out data, but we'll try anyway. */
780 fsg_set_halt(fsg, fsg->ep0);
781 return; // Wait for reset
782 }
783
784 /* Is it the special reset command? */
785 if (req->actual >= sizeof cbi_reset_cmnd &&
786 memcmp(req->buf, cbi_reset_cmnd,
787 sizeof cbi_reset_cmnd) == 0) {
788
789 /* Raise an exception to stop the current operation
790 * and reinitialize our state. */
791 DBG(fsg, "cbi reset request\n");
792 raise_exception(fsg, FSG_STATE_RESET);
793 return;
794 }
795
796 VDBG(fsg, "CB[I] accept device-specific command\n");
797 spin_lock(&fsg->lock);
798
799 /* Save the command for later */
800 if (fsg->cbbuf_cmnd_size)
Arjan van de Venb6c63932008-07-25 01:45:52 -0700801 WARNING(fsg, "CB[I] overwriting previous command\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 fsg->cbbuf_cmnd_size = req->actual;
803 memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 wakeup_thread(fsg);
Alan Sterna21d4fe2005-11-29 12:04:24 -0500806 spin_unlock(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809#else
810static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
811{}
812#endif /* CONFIG_USB_FILE_STORAGE_TEST */
813
814
815static int class_setup_req(struct fsg_dev *fsg,
816 const struct usb_ctrlrequest *ctrl)
817{
818 struct usb_request *req = fsg->ep0req;
819 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -0700820 u16 w_index = le16_to_cpu(ctrl->wIndex);
Luis Lloret88e45db2007-07-26 10:08:47 -0400821 u16 w_value = le16_to_cpu(ctrl->wValue);
David Brownell1bbc1692005-05-07 13:05:13 -0700822 u16 w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (!fsg->config)
825 return value;
826
827 /* Handle Bulk-only class-specific requests */
828 if (transport_is_bbb()) {
829 switch (ctrl->bRequest) {
830
831 case USB_BULK_RESET_REQUEST:
832 if (ctrl->bRequestType != (USB_DIR_OUT |
833 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
834 break;
Luis Lloret88e45db2007-07-26 10:08:47 -0400835 if (w_index != 0 || w_value != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 value = -EDOM;
837 break;
838 }
839
840 /* Raise an exception to stop the current operation
841 * and reinitialize our state. */
842 DBG(fsg, "bulk reset request\n");
843 raise_exception(fsg, FSG_STATE_RESET);
844 value = DELAYED_STATUS;
845 break;
846
847 case USB_BULK_GET_MAX_LUN_REQUEST:
848 if (ctrl->bRequestType != (USB_DIR_IN |
849 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
850 break;
Luis Lloret88e45db2007-07-26 10:08:47 -0400851 if (w_index != 0 || w_value != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 value = -EDOM;
853 break;
854 }
855 VDBG(fsg, "get max LUN\n");
856 *(u8 *) req->buf = fsg->nluns - 1;
Alan Stern76f4af82005-04-05 11:56:54 -0400857 value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 }
860 }
861
862 /* Handle CBI class-specific requests */
863 else {
864 switch (ctrl->bRequest) {
865
866 case USB_CBI_ADSC_REQUEST:
867 if (ctrl->bRequestType != (USB_DIR_OUT |
868 USB_TYPE_CLASS | USB_RECIP_INTERFACE))
869 break;
Luis Lloret88e45db2007-07-26 10:08:47 -0400870 if (w_index != 0 || w_value != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 value = -EDOM;
872 break;
873 }
874 if (w_length > MAX_COMMAND_SIZE) {
875 value = -EOVERFLOW;
876 break;
877 }
878 value = w_length;
879 fsg->ep0req->context = received_cbi_adsc;
880 break;
881 }
882 }
883
884 if (value == -EOPNOTSUPP)
885 VDBG(fsg,
886 "unknown class-specific control req "
887 "%02x.%02x v%04x i%04x l%u\n",
888 ctrl->bRequestType, ctrl->bRequest,
David Brownell1bbc1692005-05-07 13:05:13 -0700889 le16_to_cpu(ctrl->wValue), w_index, w_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return value;
891}
892
893
894/*-------------------------------------------------------------------------*/
895
896/* Ep0 standard request handlers. These always run in_irq. */
897
898static int standard_setup_req(struct fsg_dev *fsg,
899 const struct usb_ctrlrequest *ctrl)
900{
901 struct usb_request *req = fsg->ep0req;
902 int value = -EOPNOTSUPP;
David Brownell1bbc1692005-05-07 13:05:13 -0700903 u16 w_index = le16_to_cpu(ctrl->wIndex);
904 u16 w_value = le16_to_cpu(ctrl->wValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* Usually this just stores reply data in the pre-allocated ep0 buffer,
907 * but config change events will also reconfigure hardware. */
908 switch (ctrl->bRequest) {
909
910 case USB_REQ_GET_DESCRIPTOR:
911 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
912 USB_RECIP_DEVICE))
913 break;
914 switch (w_value >> 8) {
915
916 case USB_DT_DEVICE:
917 VDBG(fsg, "get device descriptor\n");
Alan Stern76f4af82005-04-05 11:56:54 -0400918 value = sizeof device_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 memcpy(req->buf, &device_desc, value);
920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 case USB_DT_DEVICE_QUALIFIER:
922 VDBG(fsg, "get device qualifier\n");
David Brownell2e806f62007-08-02 00:03:39 -0700923 if (!gadget_is_dualspeed(fsg->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
Alan Stern76f4af82005-04-05 11:56:54 -0400925 value = sizeof dev_qualifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 memcpy(req->buf, &dev_qualifier, value);
927 break;
928
929 case USB_DT_OTHER_SPEED_CONFIG:
930 VDBG(fsg, "get other-speed config descriptor\n");
David Brownell2e806f62007-08-02 00:03:39 -0700931 if (!gadget_is_dualspeed(fsg->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933 goto get_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 case USB_DT_CONFIG:
935 VDBG(fsg, "get configuration descriptor\n");
David Brownell2e806f62007-08-02 00:03:39 -0700936get_config:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 value = populate_config_buf(fsg->gadget,
938 req->buf,
939 w_value >> 8,
940 w_value & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942
943 case USB_DT_STRING:
944 VDBG(fsg, "get string descriptor\n");
945
946 /* wIndex == language code */
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100947 value = usb_gadget_get_string(&fsg_stringtab,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 w_value & 0xff, req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950 }
951 break;
952
953 /* One config, two speeds */
954 case USB_REQ_SET_CONFIGURATION:
955 if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
956 USB_RECIP_DEVICE))
957 break;
958 VDBG(fsg, "set configuration\n");
959 if (w_value == CONFIG_VALUE || w_value == 0) {
960 fsg->new_config = w_value;
961
962 /* Raise an exception to wipe out previous transaction
963 * state (queued bufs, etc) and set the new config. */
964 raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
965 value = DELAYED_STATUS;
966 }
967 break;
968 case USB_REQ_GET_CONFIGURATION:
969 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
970 USB_RECIP_DEVICE))
971 break;
972 VDBG(fsg, "get configuration\n");
973 *(u8 *) req->buf = fsg->config;
Alan Stern76f4af82005-04-05 11:56:54 -0400974 value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976
977 case USB_REQ_SET_INTERFACE:
978 if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
979 USB_RECIP_INTERFACE))
980 break;
981 if (fsg->config && w_index == 0) {
982
983 /* Raise an exception to wipe out previous transaction
984 * state (queued bufs, etc) and install the new
985 * interface altsetting. */
986 raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
987 value = DELAYED_STATUS;
988 }
989 break;
990 case USB_REQ_GET_INTERFACE:
991 if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
992 USB_RECIP_INTERFACE))
993 break;
994 if (!fsg->config)
995 break;
996 if (w_index != 0) {
997 value = -EDOM;
998 break;
999 }
1000 VDBG(fsg, "get interface\n");
1001 *(u8 *) req->buf = 0;
Alan Stern76f4af82005-04-05 11:56:54 -04001002 value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004
1005 default:
1006 VDBG(fsg,
1007 "unknown control req %02x.%02x v%04x i%04x l%u\n",
1008 ctrl->bRequestType, ctrl->bRequest,
David Brownell1bbc1692005-05-07 13:05:13 -07001009 w_value, w_index, le16_to_cpu(ctrl->wLength));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
1012 return value;
1013}
1014
1015
1016static int fsg_setup(struct usb_gadget *gadget,
1017 const struct usb_ctrlrequest *ctrl)
1018{
1019 struct fsg_dev *fsg = get_gadget_data(gadget);
1020 int rc;
David Brownell1bbc1692005-05-07 13:05:13 -07001021 int w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 ++fsg->ep0_req_tag; // Record arrival of a new request
1024 fsg->ep0req->context = NULL;
1025 fsg->ep0req->length = 0;
1026 dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
1027
1028 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
1029 rc = class_setup_req(fsg, ctrl);
1030 else
1031 rc = standard_setup_req(fsg, ctrl);
1032
1033 /* Respond with data/status or defer until later? */
1034 if (rc >= 0 && rc != DELAYED_STATUS) {
Alan Stern76f4af82005-04-05 11:56:54 -04001035 rc = min(rc, w_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 fsg->ep0req->length = rc;
David Brownell1bbc1692005-05-07 13:05:13 -07001037 fsg->ep0req->zero = rc < w_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
1039 "ep0-in" : "ep0-out");
1040 rc = ep0_queue(fsg);
1041 }
1042
1043 /* Device either stalls (rc < 0) or reports success */
1044 return rc;
1045}
1046
1047
1048/*-------------------------------------------------------------------------*/
1049
1050/* All the following routines run in process context */
1051
1052
1053/* Use this for bulk or interrupt transfers, not ep0 */
1054static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
Alan Sterna21d4fe2005-11-29 12:04:24 -05001055 struct usb_request *req, int *pbusy,
1056 enum fsg_buffer_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 int rc;
1059
1060 if (ep == fsg->bulk_in)
1061 dump_msg(fsg, "bulk-in", req->buf, req->length);
1062 else if (ep == fsg->intr_in)
1063 dump_msg(fsg, "intr-in", req->buf, req->length);
Alan Sterna21d4fe2005-11-29 12:04:24 -05001064
1065 spin_lock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 *pbusy = 1;
1067 *state = BUF_STATE_BUSY;
Alan Sterna21d4fe2005-11-29 12:04:24 -05001068 spin_unlock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 rc = usb_ep_queue(ep, req, GFP_KERNEL);
1070 if (rc != 0) {
1071 *pbusy = 0;
1072 *state = BUF_STATE_EMPTY;
1073
1074 /* We can't do much more than wait for a reset */
1075
1076 /* Note: currently the net2280 driver fails zero-length
1077 * submissions if DMA is enabled. */
1078 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
1079 req->length == 0))
Arjan van de Venb6c63932008-07-25 01:45:52 -07001080 WARNING(fsg, "error in submission: %s --> %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ep->name, rc);
1082 }
1083}
1084
1085
1086static int sleep_thread(struct fsg_dev *fsg)
1087{
Alan Sterna21d4fe2005-11-29 12:04:24 -05001088 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 /* Wait until a signal arrives or we are woken up */
Alan Sterna21d4fe2005-11-29 12:04:24 -05001091 for (;;) {
1092 try_to_freeze();
1093 set_current_state(TASK_INTERRUPTIBLE);
1094 if (signal_pending(current)) {
1095 rc = -EINTR;
1096 break;
1097 }
1098 if (fsg->thread_wakeup_needed)
1099 break;
1100 schedule();
1101 }
1102 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 fsg->thread_wakeup_needed = 0;
Alan Sterna21d4fe2005-11-29 12:04:24 -05001104 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107
1108/*-------------------------------------------------------------------------*/
1109
1110static int do_read(struct fsg_dev *fsg)
1111{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001112 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 u32 lba;
1114 struct fsg_buffhd *bh;
1115 int rc;
1116 u32 amount_left;
1117 loff_t file_offset, file_offset_tmp;
1118 unsigned int amount;
1119 unsigned int partial_page;
1120 ssize_t nread;
1121
1122 /* Get the starting Logical Block Address and check that it's
1123 * not too big */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001124 if (fsg->cmnd[0] == READ_6)
Alan Stern604eb892009-04-27 13:19:41 -04001125 lba = get_unaligned_be24(&fsg->cmnd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 else {
Alan Stern604eb892009-04-27 13:19:41 -04001127 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 /* We allow DPO (Disable Page Out = don't save data in the
1130 * cache) and FUA (Force Unit Access = don't read from the
1131 * cache), but we don't implement them. */
1132 if ((fsg->cmnd[1] & ~0x18) != 0) {
1133 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1134 return -EINVAL;
1135 }
1136 }
1137 if (lba >= curlun->num_sectors) {
1138 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1139 return -EINVAL;
1140 }
1141 file_offset = ((loff_t) lba) << 9;
1142
1143 /* Carry out the file reads */
1144 amount_left = fsg->data_size_from_cmnd;
1145 if (unlikely(amount_left == 0))
1146 return -EIO; // No default reply
1147
1148 for (;;) {
1149
1150 /* Figure out how much we need to read:
1151 * Try to read the remaining amount.
1152 * But don't read more than the buffer size.
1153 * And don't try to read past the end of the file.
1154 * Finally, if we're not at a page boundary, don't read past
1155 * the next page.
1156 * If this means reading 0 then we were asked to read past
1157 * the end of file. */
1158 amount = min((unsigned int) amount_left, mod_data.buflen);
1159 amount = min((loff_t) amount,
1160 curlun->file_length - file_offset);
1161 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
1162 if (partial_page > 0)
1163 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
1164 partial_page);
1165
1166 /* Wait for the next buffer to become available */
1167 bh = fsg->next_buffhd_to_fill;
1168 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04001169 rc = sleep_thread(fsg);
1170 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return rc;
1172 }
1173
1174 /* If we were asked to read past the end of file,
1175 * end with an empty buffer. */
1176 if (amount == 0) {
1177 curlun->sense_data =
1178 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1179 curlun->sense_data_info = file_offset >> 9;
Alan Stern6174d0f2006-09-26 14:51:48 -04001180 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 bh->inreq->length = 0;
1182 bh->state = BUF_STATE_FULL;
1183 break;
1184 }
1185
1186 /* Perform the read */
1187 file_offset_tmp = file_offset;
1188 nread = vfs_read(curlun->filp,
1189 (char __user *) bh->buf,
1190 amount, &file_offset_tmp);
1191 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1192 (unsigned long long) file_offset,
1193 (int) nread);
1194 if (signal_pending(current))
1195 return -EINTR;
1196
1197 if (nread < 0) {
1198 LDBG(curlun, "error in file read: %d\n",
1199 (int) nread);
1200 nread = 0;
1201 } else if (nread < amount) {
1202 LDBG(curlun, "partial file read: %d/%u\n",
1203 (int) nread, amount);
1204 nread -= (nread & 511); // Round down to a block
1205 }
1206 file_offset += nread;
1207 amount_left -= nread;
1208 fsg->residue -= nread;
1209 bh->inreq->length = nread;
1210 bh->state = BUF_STATE_FULL;
1211
1212 /* If an error occurred, report it and its position */
1213 if (nread < amount) {
1214 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1215 curlun->sense_data_info = file_offset >> 9;
Alan Stern6174d0f2006-09-26 14:51:48 -04001216 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
1218 }
1219
1220 if (amount_left == 0)
1221 break; // No more left to read
1222
1223 /* Send this buffer and go read some more */
1224 bh->inreq->zero = 0;
1225 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1226 &bh->inreq_busy, &bh->state);
1227 fsg->next_buffhd_to_fill = bh->next;
1228 }
1229
1230 return -EIO; // No default reply
1231}
1232
1233
1234/*-------------------------------------------------------------------------*/
1235
1236static int do_write(struct fsg_dev *fsg)
1237{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001238 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 u32 lba;
1240 struct fsg_buffhd *bh;
1241 int get_some_more;
1242 u32 amount_left_to_req, amount_left_to_write;
1243 loff_t usb_offset, file_offset, file_offset_tmp;
1244 unsigned int amount;
1245 unsigned int partial_page;
1246 ssize_t nwritten;
1247 int rc;
1248
1249 if (curlun->ro) {
1250 curlun->sense_data = SS_WRITE_PROTECTED;
1251 return -EINVAL;
1252 }
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001253 spin_lock(&curlun->filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001255 spin_unlock(&curlun->filp->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* Get the starting Logical Block Address and check that it's
1258 * not too big */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001259 if (fsg->cmnd[0] == WRITE_6)
Alan Stern604eb892009-04-27 13:19:41 -04001260 lba = get_unaligned_be24(&fsg->cmnd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 else {
Alan Stern604eb892009-04-27 13:19:41 -04001262 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* We allow DPO (Disable Page Out = don't save data in the
1265 * cache) and FUA (Force Unit Access = write directly to the
1266 * medium). We don't implement DPO; we implement FUA by
1267 * performing synchronous output. */
1268 if ((fsg->cmnd[1] & ~0x18) != 0) {
1269 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1270 return -EINVAL;
1271 }
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03001272 /* FUA */
1273 if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001274 spin_lock(&curlun->filp->f_lock);
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001275 curlun->filp->f_flags |= O_DSYNC;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07001276 spin_unlock(&curlun->filp->f_lock);
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279 if (lba >= curlun->num_sectors) {
1280 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1281 return -EINVAL;
1282 }
1283
1284 /* Carry out the file writes */
1285 get_some_more = 1;
1286 file_offset = usb_offset = ((loff_t) lba) << 9;
1287 amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
1288
1289 while (amount_left_to_write > 0) {
1290
1291 /* Queue a request for more data from the host */
1292 bh = fsg->next_buffhd_to_fill;
1293 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
1294
1295 /* Figure out how much we want to get:
1296 * Try to get the remaining amount.
1297 * But don't get more than the buffer size.
1298 * And don't try to go past the end of the file.
1299 * If we're not at a page boundary,
1300 * don't go past the next page.
1301 * If this means getting 0, then we were asked
1302 * to write past the end of file.
1303 * Finally, round down to a block boundary. */
1304 amount = min(amount_left_to_req, mod_data.buflen);
1305 amount = min((loff_t) amount, curlun->file_length -
1306 usb_offset);
1307 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
1308 if (partial_page > 0)
1309 amount = min(amount,
1310 (unsigned int) PAGE_CACHE_SIZE - partial_page);
1311
1312 if (amount == 0) {
1313 get_some_more = 0;
1314 curlun->sense_data =
1315 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1316 curlun->sense_data_info = usb_offset >> 9;
Alan Stern6174d0f2006-09-26 14:51:48 -04001317 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 continue;
1319 }
1320 amount -= (amount & 511);
1321 if (amount == 0) {
1322
1323 /* Why were we were asked to transfer a
1324 * partial block? */
1325 get_some_more = 0;
1326 continue;
1327 }
1328
1329 /* Get the next buffer */
1330 usb_offset += amount;
1331 fsg->usb_amount_left -= amount;
1332 amount_left_to_req -= amount;
1333 if (amount_left_to_req == 0)
1334 get_some_more = 0;
1335
1336 /* amount is always divisible by 512, hence by
1337 * the bulk-out maxpacket size */
Mian Yousaf Kaukab806e8f82011-03-24 12:20:13 +01001338 bh->outreq->length = amount;
Alan Stern70ffe6e2006-03-23 15:05:16 -05001339 bh->outreq->short_not_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1341 &bh->outreq_busy, &bh->state);
1342 fsg->next_buffhd_to_fill = bh->next;
1343 continue;
1344 }
1345
1346 /* Write the received data to the backing file */
1347 bh = fsg->next_buffhd_to_drain;
1348 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
1349 break; // We stopped early
1350 if (bh->state == BUF_STATE_FULL) {
Alan Sterna21d4fe2005-11-29 12:04:24 -05001351 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 fsg->next_buffhd_to_drain = bh->next;
1353 bh->state = BUF_STATE_EMPTY;
1354
1355 /* Did something go wrong with the transfer? */
1356 if (bh->outreq->status != 0) {
1357 curlun->sense_data = SS_COMMUNICATION_FAILURE;
1358 curlun->sense_data_info = file_offset >> 9;
Alan Stern6174d0f2006-09-26 14:51:48 -04001359 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
1361 }
1362
1363 amount = bh->outreq->actual;
1364 if (curlun->file_length - file_offset < amount) {
1365 LERROR(curlun,
1366 "write %u @ %llu beyond end %llu\n",
1367 amount, (unsigned long long) file_offset,
1368 (unsigned long long) curlun->file_length);
1369 amount = curlun->file_length - file_offset;
1370 }
1371
1372 /* Perform the write */
1373 file_offset_tmp = file_offset;
1374 nwritten = vfs_write(curlun->filp,
1375 (char __user *) bh->buf,
1376 amount, &file_offset_tmp);
1377 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1378 (unsigned long long) file_offset,
1379 (int) nwritten);
1380 if (signal_pending(current))
1381 return -EINTR; // Interrupted!
1382
1383 if (nwritten < 0) {
1384 LDBG(curlun, "error in file write: %d\n",
1385 (int) nwritten);
1386 nwritten = 0;
1387 } else if (nwritten < amount) {
1388 LDBG(curlun, "partial file write: %d/%u\n",
1389 (int) nwritten, amount);
1390 nwritten -= (nwritten & 511);
1391 // Round down to a block
1392 }
1393 file_offset += nwritten;
1394 amount_left_to_write -= nwritten;
1395 fsg->residue -= nwritten;
1396
1397 /* If an error occurred, report it and its position */
1398 if (nwritten < amount) {
1399 curlun->sense_data = SS_WRITE_ERROR;
1400 curlun->sense_data_info = file_offset >> 9;
Alan Stern6174d0f2006-09-26 14:51:48 -04001401 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
1403 }
1404
1405 /* Did the host decide to stop early? */
1406 if (bh->outreq->actual != bh->outreq->length) {
1407 fsg->short_packet_received = 1;
1408 break;
1409 }
1410 continue;
1411 }
1412
1413 /* Wait for something to happen */
Alan Stern79a7d9e2007-08-08 17:10:11 -04001414 rc = sleep_thread(fsg);
1415 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return rc;
1417 }
1418
1419 return -EIO; // No default reply
1420}
1421
1422
1423/*-------------------------------------------------------------------------*/
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425static int do_synchronize_cache(struct fsg_dev *fsg)
1426{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001427 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int rc;
1429
1430 /* We ignore the requested LBA and write out all file's
1431 * dirty data buffers. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001432 rc = fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (rc)
1434 curlun->sense_data = SS_WRITE_ERROR;
1435 return 0;
1436}
1437
1438
1439/*-------------------------------------------------------------------------*/
1440
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001441static void invalidate_sub(struct fsg_lun *curlun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct file *filp = curlun->filp;
Josef Sipek33cb8992006-12-08 02:37:46 -08001444 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 unsigned long rc;
1446
Andrew Mortonfc0ecff2007-02-10 01:45:39 -08001447 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001448 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451static int do_verify(struct fsg_dev *fsg)
1452{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001453 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 u32 lba;
1455 u32 verification_length;
1456 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1457 loff_t file_offset, file_offset_tmp;
1458 u32 amount_left;
1459 unsigned int amount;
1460 ssize_t nread;
1461
1462 /* Get the starting Logical Block Address and check that it's
1463 * not too big */
Alan Stern604eb892009-04-27 13:19:41 -04001464 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (lba >= curlun->num_sectors) {
1466 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1467 return -EINVAL;
1468 }
1469
1470 /* We allow DPO (Disable Page Out = don't save data in the
1471 * cache) but we don't implement it. */
1472 if ((fsg->cmnd[1] & ~0x10) != 0) {
1473 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1474 return -EINVAL;
1475 }
1476
Alan Stern604eb892009-04-27 13:19:41 -04001477 verification_length = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (unlikely(verification_length == 0))
1479 return -EIO; // No default reply
1480
1481 /* Prepare to carry out the file verify */
1482 amount_left = verification_length << 9;
1483 file_offset = ((loff_t) lba) << 9;
1484
1485 /* Write out all the dirty buffers before invalidating them */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001486 fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (signal_pending(current))
1488 return -EINTR;
1489
1490 invalidate_sub(curlun);
1491 if (signal_pending(current))
1492 return -EINTR;
1493
1494 /* Just try to read the requested blocks */
1495 while (amount_left > 0) {
1496
1497 /* Figure out how much we need to read:
1498 * Try to read the remaining amount, but not more than
1499 * the buffer size.
1500 * And don't try to read past the end of the file.
1501 * If this means reading 0 then we were asked to read
1502 * past the end of file. */
1503 amount = min((unsigned int) amount_left, mod_data.buflen);
1504 amount = min((loff_t) amount,
1505 curlun->file_length - file_offset);
1506 if (amount == 0) {
1507 curlun->sense_data =
1508 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1509 curlun->sense_data_info = file_offset >> 9;
Alan Stern6174d0f2006-09-26 14:51:48 -04001510 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
1512 }
1513
1514 /* Perform the read */
1515 file_offset_tmp = file_offset;
1516 nread = vfs_read(curlun->filp,
1517 (char __user *) bh->buf,
1518 amount, &file_offset_tmp);
1519 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1520 (unsigned long long) file_offset,
1521 (int) nread);
1522 if (signal_pending(current))
1523 return -EINTR;
1524
1525 if (nread < 0) {
1526 LDBG(curlun, "error in file verify: %d\n",
1527 (int) nread);
1528 nread = 0;
1529 } else if (nread < amount) {
1530 LDBG(curlun, "partial file verify: %d/%u\n",
1531 (int) nread, amount);
1532 nread -= (nread & 511); // Round down to a sector
1533 }
1534 if (nread == 0) {
1535 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1536 curlun->sense_data_info = file_offset >> 9;
Alan Stern6174d0f2006-09-26 14:51:48 -04001537 curlun->info_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
1539 }
1540 file_offset += nread;
1541 amount_left -= nread;
1542 }
1543 return 0;
1544}
1545
1546
1547/*-------------------------------------------------------------------------*/
1548
1549static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1550{
1551 u8 *buf = (u8 *) bh->buf;
1552
1553 static char vendor_id[] = "Linux ";
Alan Stern12aae682008-11-20 14:13:12 -05001554 static char product_disk_id[] = "File-Stor Gadget";
1555 static char product_cdrom_id[] = "File-CD Gadget ";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 if (!fsg->curlun) { // Unsupported LUNs are okay
1558 fsg->bad_lun_okay = 1;
1559 memset(buf, 0, 36);
1560 buf[0] = 0x7f; // Unsupported, no device-type
Alan Stern12aae682008-11-20 14:13:12 -05001561 buf[4] = 31; // Additional length
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return 36;
1563 }
1564
Alan Stern12aae682008-11-20 14:13:12 -05001565 memset(buf, 0, 8);
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001566 buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (mod_data.removable)
1568 buf[1] = 0x80;
1569 buf[2] = 2; // ANSI SCSI level 2
1570 buf[3] = 2; // SCSI-2 INQUIRY data format
1571 buf[4] = 31; // Additional length
1572 // No special options
Alan Stern12aae682008-11-20 14:13:12 -05001573 sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
1574 (mod_data.cdrom ? product_cdrom_id :
1575 product_disk_id),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 mod_data.release);
1577 return 36;
1578}
1579
1580
1581static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1582{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001583 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 u8 *buf = (u8 *) bh->buf;
1585 u32 sd, sdinfo;
Alan Stern6174d0f2006-09-26 14:51:48 -04001586 int valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
1589 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1590 *
1591 * If a REQUEST SENSE command is received from an initiator
1592 * with a pending unit attention condition (before the target
1593 * generates the contingent allegiance condition), then the
1594 * target shall either:
1595 * a) report any pending sense data and preserve the unit
1596 * attention condition on the logical unit, or,
1597 * b) report the unit attention condition, may discard any
1598 * pending sense data, and clear the unit attention
1599 * condition on the logical unit for that initiator.
1600 *
1601 * FSG normally uses option a); enable this code to use option b).
1602 */
1603#if 0
1604 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1605 curlun->sense_data = curlun->unit_attention_data;
1606 curlun->unit_attention_data = SS_NO_SENSE;
1607 }
1608#endif
1609
1610 if (!curlun) { // Unsupported LUNs are okay
1611 fsg->bad_lun_okay = 1;
1612 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1613 sdinfo = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04001614 valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 } else {
1616 sd = curlun->sense_data;
1617 sdinfo = curlun->sense_data_info;
Alan Stern6174d0f2006-09-26 14:51:48 -04001618 valid = curlun->info_valid << 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 curlun->sense_data = SS_NO_SENSE;
1620 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04001621 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
1624 memset(buf, 0, 18);
Alan Stern6174d0f2006-09-26 14:51:48 -04001625 buf[0] = valid | 0x70; // Valid, current error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 buf[2] = SK(sd);
Alan Stern604eb892009-04-27 13:19:41 -04001627 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 buf[7] = 18 - 8; // Additional sense length
1629 buf[12] = ASC(sd);
1630 buf[13] = ASCQ(sd);
1631 return 18;
1632}
1633
1634
1635static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1636{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001637 struct fsg_lun *curlun = fsg->curlun;
Alan Stern604eb892009-04-27 13:19:41 -04001638 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 int pmi = fsg->cmnd[8];
1640 u8 *buf = (u8 *) bh->buf;
1641
1642 /* Check the PMI and LBA fields */
1643 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1644 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1645 return -EINVAL;
1646 }
1647
Alan Stern604eb892009-04-27 13:19:41 -04001648 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1649 /* Max logical block */
1650 put_unaligned_be32(512, &buf[4]); /* Block length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return 8;
1652}
1653
1654
Alan Stern12aae682008-11-20 14:13:12 -05001655static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1656{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001657 struct fsg_lun *curlun = fsg->curlun;
Alan Stern12aae682008-11-20 14:13:12 -05001658 int msf = fsg->cmnd[1] & 0x02;
Alan Stern604eb892009-04-27 13:19:41 -04001659 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
Alan Stern12aae682008-11-20 14:13:12 -05001660 u8 *buf = (u8 *) bh->buf;
1661
1662 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
1663 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1664 return -EINVAL;
1665 }
1666 if (lba >= curlun->num_sectors) {
1667 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1668 return -EINVAL;
1669 }
1670
1671 memset(buf, 0, 8);
1672 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1673 store_cdrom_address(&buf[4], msf, lba);
1674 return 8;
1675}
1676
1677
1678static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1679{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001680 struct fsg_lun *curlun = fsg->curlun;
Alan Stern12aae682008-11-20 14:13:12 -05001681 int msf = fsg->cmnd[1] & 0x02;
1682 int start_track = fsg->cmnd[6];
1683 u8 *buf = (u8 *) bh->buf;
1684
1685 if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1686 start_track > 1) {
1687 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1688 return -EINVAL;
1689 }
1690
1691 memset(buf, 0, 20);
1692 buf[1] = (20-2); /* TOC data length */
1693 buf[2] = 1; /* First track number */
1694 buf[3] = 1; /* Last track number */
1695 buf[5] = 0x16; /* Data track, copying allowed */
1696 buf[6] = 0x01; /* Only track is number 1 */
1697 store_cdrom_address(&buf[8], msf, 0);
1698
1699 buf[13] = 0x16; /* Lead-out track is data */
1700 buf[14] = 0xAA; /* Lead-out track number */
1701 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1702 return 20;
1703}
1704
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1707{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001708 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 int mscmnd = fsg->cmnd[0];
1710 u8 *buf = (u8 *) bh->buf;
1711 u8 *buf0 = buf;
1712 int pc, page_code;
1713 int changeable_values, all_pages;
1714 int valid_page = 0;
1715 int len, limit;
1716
1717 if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
1718 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1719 return -EINVAL;
1720 }
1721 pc = fsg->cmnd[2] >> 6;
1722 page_code = fsg->cmnd[2] & 0x3f;
1723 if (pc == 3) {
1724 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1725 return -EINVAL;
1726 }
1727 changeable_values = (pc == 1);
1728 all_pages = (page_code == 0x3f);
1729
1730 /* Write the mode parameter header. Fixed values are: default
1731 * medium type, no cache control (DPOFUA), and no block descriptors.
1732 * The only variable value is the WriteProtect bit. We will fill in
1733 * the mode data length later. */
1734 memset(buf, 0, 8);
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001735 if (mscmnd == MODE_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1737 buf += 4;
1738 limit = 255;
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001739 } else { // MODE_SENSE_10
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
1741 buf += 8;
1742 limit = 65535; // Should really be mod_data.buflen
1743 }
1744
1745 /* No block descriptors */
1746
1747 /* The mode pages, in numerical order. The only page we support
1748 * is the Caching page. */
1749 if (page_code == 0x08 || all_pages) {
1750 valid_page = 1;
1751 buf[0] = 0x08; // Page code
1752 buf[1] = 10; // Page length
1753 memset(buf+2, 0, 10); // None of the fields are changeable
1754
1755 if (!changeable_values) {
1756 buf[2] = 0x04; // Write cache enable,
1757 // Read cache not disabled
1758 // No cache retention priorities
Alan Stern604eb892009-04-27 13:19:41 -04001759 put_unaligned_be16(0xffff, &buf[4]);
1760 /* Don't disable prefetch */
1761 /* Minimum prefetch = 0 */
1762 put_unaligned_be16(0xffff, &buf[8]);
1763 /* Maximum prefetch */
1764 put_unaligned_be16(0xffff, &buf[10]);
1765 /* Maximum prefetch ceiling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767 buf += 12;
1768 }
1769
1770 /* Check that a valid page was requested and the mode data length
1771 * isn't too long. */
1772 len = buf - buf0;
1773 if (!valid_page || len > limit) {
1774 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1775 return -EINVAL;
1776 }
1777
1778 /* Store the mode data length */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02001779 if (mscmnd == MODE_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 buf0[0] = len - 1;
1781 else
Alan Stern604eb892009-04-27 13:19:41 -04001782 put_unaligned_be16(len - 2, buf0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return len;
1784}
1785
1786
1787static int do_start_stop(struct fsg_dev *fsg)
1788{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001789 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 int loej, start;
1791
1792 if (!mod_data.removable) {
1793 curlun->sense_data = SS_INVALID_COMMAND;
1794 return -EINVAL;
1795 }
1796
1797 // int immed = fsg->cmnd[1] & 0x01;
1798 loej = fsg->cmnd[4] & 0x02;
1799 start = fsg->cmnd[4] & 0x01;
1800
1801#ifdef CONFIG_USB_FILE_STORAGE_TEST
1802 if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
1803 (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
1804 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1805 return -EINVAL;
1806 }
1807
1808 if (!start) {
1809
1810 /* Are we allowed to unload the media? */
1811 if (curlun->prevent_medium_removal) {
1812 LDBG(curlun, "unload attempt prevented\n");
1813 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1814 return -EINVAL;
1815 }
1816 if (loej) { // Simulate an unload/eject
1817 up_read(&fsg->filesem);
1818 down_write(&fsg->filesem);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001819 fsg_lun_close(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 up_write(&fsg->filesem);
1821 down_read(&fsg->filesem);
1822 }
1823 } else {
1824
1825 /* Our emulation doesn't support mounting; the medium is
1826 * available for use as soon as it is loaded. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001827 if (!fsg_lun_is_open(curlun)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1829 return -EINVAL;
1830 }
1831 }
1832#endif
1833 return 0;
1834}
1835
1836
1837static int do_prevent_allow(struct fsg_dev *fsg)
1838{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001839 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 int prevent;
1841
1842 if (!mod_data.removable) {
1843 curlun->sense_data = SS_INVALID_COMMAND;
1844 return -EINVAL;
1845 }
1846
1847 prevent = fsg->cmnd[4] & 0x01;
1848 if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
1849 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1850 return -EINVAL;
1851 }
1852
1853 if (curlun->prevent_medium_removal && !prevent)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001854 fsg_lun_fsync_sub(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 curlun->prevent_medium_removal = prevent;
1856 return 0;
1857}
1858
1859
1860static int do_read_format_capacities(struct fsg_dev *fsg,
1861 struct fsg_buffhd *bh)
1862{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001863 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 u8 *buf = (u8 *) bh->buf;
1865
1866 buf[0] = buf[1] = buf[2] = 0;
1867 buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
1868 buf += 4;
1869
Alan Stern604eb892009-04-27 13:19:41 -04001870 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1871 /* Number of blocks */
1872 put_unaligned_be32(512, &buf[4]); /* Block length */
1873 buf[4] = 0x02; /* Current capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return 12;
1875}
1876
1877
1878static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1879{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01001880 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 /* We don't support MODE SELECT */
1883 curlun->sense_data = SS_INVALID_COMMAND;
1884 return -EINVAL;
1885}
1886
1887
1888/*-------------------------------------------------------------------------*/
1889
1890static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1891{
1892 int rc;
1893
1894 rc = fsg_set_halt(fsg, fsg->bulk_in);
1895 if (rc == -EAGAIN)
1896 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1897 while (rc != 0) {
1898 if (rc != -EAGAIN) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001899 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 rc = 0;
1901 break;
1902 }
1903
1904 /* Wait for a short time and then try again */
1905 if (msleep_interruptible(100) != 0)
1906 return -EINTR;
1907 rc = usb_ep_set_halt(fsg->bulk_in);
1908 }
1909 return rc;
1910}
1911
David Lopo62fd2ca2008-04-29 10:14:38 +01001912static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1913{
1914 int rc;
1915
1916 DBG(fsg, "bulk-in set wedge\n");
1917 rc = usb_ep_set_wedge(fsg->bulk_in);
1918 if (rc == -EAGAIN)
1919 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1920 while (rc != 0) {
1921 if (rc != -EAGAIN) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001922 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
David Lopo62fd2ca2008-04-29 10:14:38 +01001923 rc = 0;
1924 break;
1925 }
1926
1927 /* Wait for a short time and then try again */
1928 if (msleep_interruptible(100) != 0)
1929 return -EINTR;
1930 rc = usb_ep_set_wedge(fsg->bulk_in);
1931 }
1932 return rc;
1933}
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935static int throw_away_data(struct fsg_dev *fsg)
1936{
1937 struct fsg_buffhd *bh;
1938 u32 amount;
1939 int rc;
1940
1941 while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
1942 fsg->usb_amount_left > 0) {
1943
1944 /* Throw away the data in a filled buffer */
1945 if (bh->state == BUF_STATE_FULL) {
Alan Sterna21d4fe2005-11-29 12:04:24 -05001946 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 bh->state = BUF_STATE_EMPTY;
1948 fsg->next_buffhd_to_drain = bh->next;
1949
1950 /* A short packet or an error ends everything */
1951 if (bh->outreq->actual != bh->outreq->length ||
1952 bh->outreq->status != 0) {
1953 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
1954 return -EINTR;
1955 }
1956 continue;
1957 }
1958
1959 /* Try to submit another request if we need one */
1960 bh = fsg->next_buffhd_to_fill;
1961 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
1962 amount = min(fsg->usb_amount_left,
1963 (u32) mod_data.buflen);
1964
1965 /* amount is always divisible by 512, hence by
1966 * the bulk-out maxpacket size */
Mian Yousaf Kaukab806e8f82011-03-24 12:20:13 +01001967 bh->outreq->length = amount;
Alan Stern70ffe6e2006-03-23 15:05:16 -05001968 bh->outreq->short_not_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1970 &bh->outreq_busy, &bh->state);
1971 fsg->next_buffhd_to_fill = bh->next;
1972 fsg->usb_amount_left -= amount;
1973 continue;
1974 }
1975
1976 /* Otherwise wait for something to happen */
Alan Stern79a7d9e2007-08-08 17:10:11 -04001977 rc = sleep_thread(fsg);
1978 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return rc;
1980 }
1981 return 0;
1982}
1983
1984
1985static int finish_reply(struct fsg_dev *fsg)
1986{
1987 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
1988 int rc = 0;
1989
1990 switch (fsg->data_dir) {
1991 case DATA_DIR_NONE:
1992 break; // Nothing to send
1993
1994 /* If we don't know whether the host wants to read or write,
1995 * this must be CB or CBI with an unknown command. We mustn't
1996 * try to send or receive any data. So stall both bulk pipes
1997 * if we can and wait for a reset. */
1998 case DATA_DIR_UNKNOWN:
1999 if (mod_data.can_stall) {
2000 fsg_set_halt(fsg, fsg->bulk_out);
2001 rc = halt_bulk_in_endpoint(fsg);
2002 }
2003 break;
2004
2005 /* All but the last buffer of data must have already been sent */
2006 case DATA_DIR_TO_HOST:
2007 if (fsg->data_size == 0)
2008 ; // Nothing to send
2009
2010 /* If there's no residue, simply send the last buffer */
2011 else if (fsg->residue == 0) {
2012 bh->inreq->zero = 0;
2013 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2014 &bh->inreq_busy, &bh->state);
2015 fsg->next_buffhd_to_fill = bh->next;
2016 }
2017
2018 /* There is a residue. For CB and CBI, simply mark the end
2019 * of the data with a short packet. However, if we are
2020 * allowed to stall, there was no data at all (residue ==
2021 * data_size), and the command failed (invalid LUN or
2022 * sense data is set), then halt the bulk-in endpoint
2023 * instead. */
2024 else if (!transport_is_bbb()) {
2025 if (mod_data.can_stall &&
2026 fsg->residue == fsg->data_size &&
2027 (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
2028 bh->state = BUF_STATE_EMPTY;
2029 rc = halt_bulk_in_endpoint(fsg);
2030 } else {
2031 bh->inreq->zero = 1;
2032 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2033 &bh->inreq_busy, &bh->state);
2034 fsg->next_buffhd_to_fill = bh->next;
2035 }
2036 }
2037
Alan Sternee81b3e2011-03-25 11:46:27 -04002038 /*
2039 * For Bulk-only, mark the end of the data with a short
2040 * packet. If we are allowed to stall, halt the bulk-in
2041 * endpoint. (Note: This violates the Bulk-Only Transport
2042 * specification, which requires us to pad the data if we
2043 * don't halt the endpoint. Presumably nobody will mind.)
2044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 else {
Alan Sternee81b3e2011-03-25 11:46:27 -04002046 bh->inreq->zero = 1;
2047 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2048 &bh->inreq_busy, &bh->state);
2049 fsg->next_buffhd_to_fill = bh->next;
2050 if (mod_data.can_stall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 rc = halt_bulk_in_endpoint(fsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053 break;
2054
2055 /* We have processed all we want from the data the host has sent.
2056 * There may still be outstanding bulk-out requests. */
2057 case DATA_DIR_FROM_HOST:
2058 if (fsg->residue == 0)
2059 ; // Nothing to receive
2060
2061 /* Did the host stop sending unexpectedly early? */
2062 else if (fsg->short_packet_received) {
2063 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2064 rc = -EINTR;
2065 }
2066
2067 /* We haven't processed all the incoming data. Even though
2068 * we may be allowed to stall, doing so would cause a race.
2069 * The controller may already have ACK'ed all the remaining
2070 * bulk-out packets, in which case the host wouldn't see a
2071 * STALL. Not realizing the endpoint was halted, it wouldn't
2072 * clear the halt -- leading to problems later on. */
2073#if 0
2074 else if (mod_data.can_stall) {
2075 fsg_set_halt(fsg, fsg->bulk_out);
2076 raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
2077 rc = -EINTR;
2078 }
2079#endif
2080
2081 /* We can't stall. Read in the excess data and throw it
2082 * all away. */
2083 else
2084 rc = throw_away_data(fsg);
2085 break;
2086 }
2087 return rc;
2088}
2089
2090
2091static int send_status(struct fsg_dev *fsg)
2092{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002093 struct fsg_lun *curlun = fsg->curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 struct fsg_buffhd *bh;
2095 int rc;
2096 u8 status = USB_STATUS_PASS;
2097 u32 sd, sdinfo = 0;
2098
2099 /* Wait for the next buffer to become available */
2100 bh = fsg->next_buffhd_to_fill;
2101 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002102 rc = sleep_thread(fsg);
2103 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return rc;
2105 }
2106
2107 if (curlun) {
2108 sd = curlun->sense_data;
2109 sdinfo = curlun->sense_data_info;
2110 } else if (fsg->bad_lun_okay)
2111 sd = SS_NO_SENSE;
2112 else
2113 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
2114
2115 if (fsg->phase_error) {
2116 DBG(fsg, "sending phase-error status\n");
2117 status = USB_STATUS_PHASE_ERROR;
2118 sd = SS_INVALID_COMMAND;
2119 } else if (sd != SS_NO_SENSE) {
2120 DBG(fsg, "sending command-failure status\n");
2121 status = USB_STATUS_FAIL;
2122 VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
2123 " info x%x\n",
2124 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
2125 }
2126
2127 if (transport_is_bbb()) {
John Daiker7ca46b82006-12-29 19:02:06 -08002128 struct bulk_cs_wrap *csw = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /* Store and send the Bulk-only CSW */
Harvey Harrison551509d2009-02-11 14:11:36 -08002131 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 csw->Tag = fsg->tag;
2133 csw->Residue = cpu_to_le32(fsg->residue);
2134 csw->Status = status;
2135
2136 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
2137 bh->inreq->zero = 0;
2138 start_transfer(fsg, fsg->bulk_in, bh->inreq,
2139 &bh->inreq_busy, &bh->state);
2140
2141 } else if (mod_data.transport_type == USB_PR_CB) {
2142
2143 /* Control-Bulk transport has no status phase! */
2144 return 0;
2145
2146 } else { // USB_PR_CBI
John Daiker7ca46b82006-12-29 19:02:06 -08002147 struct interrupt_data *buf = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /* Store and send the Interrupt data. UFI sends the ASC
2150 * and ASCQ bytes. Everything else sends a Type (which
2151 * is always 0) and the status Value. */
2152 if (mod_data.protocol_type == USB_SC_UFI) {
2153 buf->bType = ASC(sd);
2154 buf->bValue = ASCQ(sd);
2155 } else {
2156 buf->bType = 0;
2157 buf->bValue = status;
2158 }
2159 fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
2160
2161 fsg->intr_buffhd = bh; // Point to the right buffhd
2162 fsg->intreq->buf = bh->inreq->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 fsg->intreq->context = bh;
2164 start_transfer(fsg, fsg->intr_in, fsg->intreq,
2165 &fsg->intreq_busy, &bh->state);
2166 }
2167
2168 fsg->next_buffhd_to_fill = bh->next;
2169 return 0;
2170}
2171
2172
2173/*-------------------------------------------------------------------------*/
2174
2175/* Check whether the command is properly formed and whether its data size
2176 * and direction agree with the values we already have. */
2177static int check_command(struct fsg_dev *fsg, int cmnd_size,
2178 enum data_direction data_dir, unsigned int mask,
2179 int needs_medium, const char *name)
2180{
2181 int i;
2182 int lun = fsg->cmnd[1] >> 5;
2183 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
2184 char hdlen[20];
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002185 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 /* Adjust the expected cmnd_size for protocol encapsulation padding.
2188 * Transparent SCSI doesn't pad. */
2189 if (protocol_is_scsi())
2190 ;
2191
2192 /* There's some disagreement as to whether RBC pads commands or not.
2193 * We'll play it safe and accept either form. */
2194 else if (mod_data.protocol_type == USB_SC_RBC) {
2195 if (fsg->cmnd_size == 12)
2196 cmnd_size = 12;
2197
2198 /* All the other protocols pad to 12 bytes */
2199 } else
2200 cmnd_size = 12;
2201
2202 hdlen[0] = 0;
2203 if (fsg->data_dir != DATA_DIR_UNKNOWN)
2204 sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
2205 fsg->data_size);
2206 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
2207 name, cmnd_size, dirletter[(int) data_dir],
2208 fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
2209
2210 /* We can't reply at all until we know the correct data direction
2211 * and size. */
2212 if (fsg->data_size_from_cmnd == 0)
2213 data_dir = DATA_DIR_NONE;
2214 if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
2215 fsg->data_dir = data_dir;
2216 fsg->data_size = fsg->data_size_from_cmnd;
2217
2218 } else { // Bulk-only
2219 if (fsg->data_size < fsg->data_size_from_cmnd) {
2220
2221 /* Host data size < Device data size is a phase error.
2222 * Carry out the command, but only transfer as much
2223 * as we are allowed. */
2224 fsg->data_size_from_cmnd = fsg->data_size;
2225 fsg->phase_error = 1;
2226 }
2227 }
2228 fsg->residue = fsg->usb_amount_left = fsg->data_size;
2229
2230 /* Conflicting data directions is a phase error */
2231 if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
2232 fsg->phase_error = 1;
2233 return -EINVAL;
2234 }
2235
2236 /* Verify the length of the command itself */
2237 if (cmnd_size != fsg->cmnd_size) {
2238
Felipe Balbi549c41e2008-09-19 02:00:02 +03002239 /* Special case workaround: There are plenty of buggy SCSI
2240 * implementations. Many have issues with cbw->Length
2241 * field passing a wrong command size. For those cases we
2242 * always try to work around the problem by using the length
2243 * sent by the host side provided it is at least as large
2244 * as the correct command length.
2245 * Examples of such cases would be MS-Windows, which issues
2246 * REQUEST SENSE with cbw->Length == 12 where it should
2247 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
2248 * REQUEST SENSE with cbw->Length == 10 where it should
2249 * be 6 as well.
2250 */
2251 if (cmnd_size <= fsg->cmnd_size) {
2252 DBG(fsg, "%s is buggy! Expected length %d "
2253 "but we got %d\n", name,
2254 cmnd_size, fsg->cmnd_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 cmnd_size = fsg->cmnd_size;
Felipe Balbi549c41e2008-09-19 02:00:02 +03002256 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 fsg->phase_error = 1;
2258 return -EINVAL;
2259 }
2260 }
2261
Alan Sternd794ac72005-04-18 12:43:25 -04002262 /* Check that the LUN values are consistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (transport_is_bbb()) {
2264 if (fsg->lun != lun)
2265 DBG(fsg, "using LUN %d from CBW, "
2266 "not LUN %d from CDB\n",
2267 fsg->lun, lun);
2268 } else
2269 fsg->lun = lun; // Use LUN from the command
2270
2271 /* Check the LUN */
2272 if (fsg->lun >= 0 && fsg->lun < fsg->nluns) {
2273 fsg->curlun = curlun = &fsg->luns[fsg->lun];
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002274 if (fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 curlun->sense_data = SS_NO_SENSE;
2276 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04002277 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279 } else {
2280 fsg->curlun = curlun = NULL;
2281 fsg->bad_lun_okay = 0;
2282
2283 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2284 * to use unsupported LUNs; all others may not. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002285 if (fsg->cmnd[0] != INQUIRY &&
2286 fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 DBG(fsg, "unsupported LUN %d\n", fsg->lun);
2288 return -EINVAL;
2289 }
2290 }
2291
2292 /* If a unit attention condition exists, only INQUIRY and
2293 * REQUEST SENSE commands are allowed; anything else must fail. */
2294 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002295 fsg->cmnd[0] != INQUIRY &&
2296 fsg->cmnd[0] != REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 curlun->sense_data = curlun->unit_attention_data;
2298 curlun->unit_attention_data = SS_NO_SENSE;
2299 return -EINVAL;
2300 }
2301
2302 /* Check that only command bytes listed in the mask are non-zero */
2303 fsg->cmnd[1] &= 0x1f; // Mask away the LUN
2304 for (i = 1; i < cmnd_size; ++i) {
2305 if (fsg->cmnd[i] && !(mask & (1 << i))) {
2306 if (curlun)
2307 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2308 return -EINVAL;
2309 }
2310 }
2311
2312 /* If the medium isn't mounted and the command needs to access
2313 * it, return an error. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002314 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
2316 return -EINVAL;
2317 }
2318
2319 return 0;
2320}
2321
2322
2323static int do_scsi_command(struct fsg_dev *fsg)
2324{
2325 struct fsg_buffhd *bh;
2326 int rc;
2327 int reply = -EINVAL;
2328 int i;
2329 static char unknown[16];
2330
2331 dump_cdb(fsg);
2332
2333 /* Wait for the next buffer to become available for data or status */
2334 bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
2335 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002336 rc = sleep_thread(fsg);
2337 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 fsg->phase_error = 0;
2341 fsg->short_packet_received = 0;
2342
2343 down_read(&fsg->filesem); // We're using the backing file
2344 switch (fsg->cmnd[0]) {
2345
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002346 case INQUIRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 fsg->data_size_from_cmnd = fsg->cmnd[4];
2348 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2349 (1<<4), 0,
2350 "INQUIRY")) == 0)
2351 reply = do_inquiry(fsg, bh);
2352 break;
2353
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002354 case MODE_SELECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 fsg->data_size_from_cmnd = fsg->cmnd[4];
2356 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2357 (1<<1) | (1<<4), 0,
2358 "MODE SELECT(6)")) == 0)
2359 reply = do_mode_select(fsg, bh);
2360 break;
2361
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002362 case MODE_SELECT_10:
Alan Stern604eb892009-04-27 13:19:41 -04002363 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2365 (1<<1) | (3<<7), 0,
2366 "MODE SELECT(10)")) == 0)
2367 reply = do_mode_select(fsg, bh);
2368 break;
2369
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002370 case MODE_SENSE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 fsg->data_size_from_cmnd = fsg->cmnd[4];
2372 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2373 (1<<1) | (1<<2) | (1<<4), 0,
2374 "MODE SENSE(6)")) == 0)
2375 reply = do_mode_sense(fsg, bh);
2376 break;
2377
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002378 case MODE_SENSE_10:
Alan Stern604eb892009-04-27 13:19:41 -04002379 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2381 (1<<1) | (1<<2) | (3<<7), 0,
2382 "MODE SENSE(10)")) == 0)
2383 reply = do_mode_sense(fsg, bh);
2384 break;
2385
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002386 case ALLOW_MEDIUM_REMOVAL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 fsg->data_size_from_cmnd = 0;
2388 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2389 (1<<4), 0,
2390 "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
2391 reply = do_prevent_allow(fsg);
2392 break;
2393
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002394 case READ_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 i = fsg->cmnd[4];
2396 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2397 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2398 (7<<1) | (1<<4), 1,
2399 "READ(6)")) == 0)
2400 reply = do_read(fsg);
2401 break;
2402
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002403 case READ_10:
Alan Stern604eb892009-04-27 13:19:41 -04002404 fsg->data_size_from_cmnd =
2405 get_unaligned_be16(&fsg->cmnd[7]) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2407 (1<<1) | (0xf<<2) | (3<<7), 1,
2408 "READ(10)")) == 0)
2409 reply = do_read(fsg);
2410 break;
2411
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002412 case READ_12:
Alan Stern604eb892009-04-27 13:19:41 -04002413 fsg->data_size_from_cmnd =
2414 get_unaligned_be32(&fsg->cmnd[6]) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2416 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2417 "READ(12)")) == 0)
2418 reply = do_read(fsg);
2419 break;
2420
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002421 case READ_CAPACITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 fsg->data_size_from_cmnd = 8;
2423 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2424 (0xf<<2) | (1<<8), 1,
2425 "READ CAPACITY")) == 0)
2426 reply = do_read_capacity(fsg, bh);
2427 break;
2428
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002429 case READ_HEADER:
Alan Stern12aae682008-11-20 14:13:12 -05002430 if (!mod_data.cdrom)
2431 goto unknown_cmnd;
Alan Stern604eb892009-04-27 13:19:41 -04002432 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Alan Stern12aae682008-11-20 14:13:12 -05002433 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2434 (3<<7) | (0x1f<<1), 1,
2435 "READ HEADER")) == 0)
2436 reply = do_read_header(fsg, bh);
2437 break;
2438
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002439 case READ_TOC:
Alan Stern12aae682008-11-20 14:13:12 -05002440 if (!mod_data.cdrom)
2441 goto unknown_cmnd;
Alan Stern604eb892009-04-27 13:19:41 -04002442 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Alan Stern12aae682008-11-20 14:13:12 -05002443 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2444 (7<<6) | (1<<1), 1,
2445 "READ TOC")) == 0)
2446 reply = do_read_toc(fsg, bh);
2447 break;
2448
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002449 case READ_FORMAT_CAPACITIES:
Alan Stern604eb892009-04-27 13:19:41 -04002450 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2452 (3<<7), 1,
2453 "READ FORMAT CAPACITIES")) == 0)
2454 reply = do_read_format_capacities(fsg, bh);
2455 break;
2456
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002457 case REQUEST_SENSE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 fsg->data_size_from_cmnd = fsg->cmnd[4];
2459 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2460 (1<<4), 0,
2461 "REQUEST SENSE")) == 0)
2462 reply = do_request_sense(fsg, bh);
2463 break;
2464
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002465 case START_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 fsg->data_size_from_cmnd = 0;
2467 if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
2468 (1<<1) | (1<<4), 0,
2469 "START-STOP UNIT")) == 0)
2470 reply = do_start_stop(fsg);
2471 break;
2472
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002473 case SYNCHRONIZE_CACHE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 fsg->data_size_from_cmnd = 0;
2475 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2476 (0xf<<2) | (3<<7), 1,
2477 "SYNCHRONIZE CACHE")) == 0)
2478 reply = do_synchronize_cache(fsg);
2479 break;
2480
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002481 case TEST_UNIT_READY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 fsg->data_size_from_cmnd = 0;
2483 reply = check_command(fsg, 6, DATA_DIR_NONE,
2484 0, 1,
2485 "TEST UNIT READY");
2486 break;
2487
2488 /* Although optional, this command is used by MS-Windows. We
2489 * support a minimal version: BytChk must be 0. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002490 case VERIFY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 fsg->data_size_from_cmnd = 0;
2492 if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
2493 (1<<1) | (0xf<<2) | (3<<7), 1,
2494 "VERIFY")) == 0)
2495 reply = do_verify(fsg);
2496 break;
2497
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002498 case WRITE_6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 i = fsg->cmnd[4];
2500 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2501 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2502 (7<<1) | (1<<4), 1,
2503 "WRITE(6)")) == 0)
2504 reply = do_write(fsg);
2505 break;
2506
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002507 case WRITE_10:
Alan Stern604eb892009-04-27 13:19:41 -04002508 fsg->data_size_from_cmnd =
2509 get_unaligned_be16(&fsg->cmnd[7]) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2511 (1<<1) | (0xf<<2) | (3<<7), 1,
2512 "WRITE(10)")) == 0)
2513 reply = do_write(fsg);
2514 break;
2515
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002516 case WRITE_12:
Alan Stern604eb892009-04-27 13:19:41 -04002517 fsg->data_size_from_cmnd =
2518 get_unaligned_be32(&fsg->cmnd[6]) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
2520 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2521 "WRITE(12)")) == 0)
2522 reply = do_write(fsg);
2523 break;
2524
2525 /* Some mandatory commands that we recognize but don't implement.
2526 * They don't mean much in this setting. It's left as an exercise
2527 * for anyone interested to implement RESERVE and RELEASE in terms
2528 * of Posix locks. */
Michal Nazarewicz0a6a7172010-10-07 14:46:15 +02002529 case FORMAT_UNIT:
2530 case RELEASE:
2531 case RESERVE:
2532 case SEND_DIAGNOSTIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 // Fall through
2534
2535 default:
Alan Stern12aae682008-11-20 14:13:12 -05002536 unknown_cmnd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 fsg->data_size_from_cmnd = 0;
2538 sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
2539 if ((reply = check_command(fsg, fsg->cmnd_size,
2540 DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) {
2541 fsg->curlun->sense_data = SS_INVALID_COMMAND;
2542 reply = -EINVAL;
2543 }
2544 break;
2545 }
2546 up_read(&fsg->filesem);
2547
2548 if (reply == -EINTR || signal_pending(current))
2549 return -EINTR;
2550
2551 /* Set up the single reply buffer for finish_reply() */
2552 if (reply == -EINVAL)
2553 reply = 0; // Error reply length
2554 if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
2555 reply = min((u32) reply, fsg->data_size_from_cmnd);
2556 bh->inreq->length = reply;
2557 bh->state = BUF_STATE_FULL;
2558 fsg->residue -= reply;
2559 } // Otherwise it's already set
2560
2561 return 0;
2562}
2563
2564
2565/*-------------------------------------------------------------------------*/
2566
2567static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2568{
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002569 struct usb_request *req = bh->outreq;
2570 struct fsg_bulk_cb_wrap *cbw = req->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Alan Sternb950bdb2008-04-14 11:45:29 -04002572 /* Was this a real packet? Should it be ignored? */
2573 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return -EINVAL;
2575
2576 /* Is the CBW valid? */
2577 if (req->actual != USB_BULK_CB_WRAP_LEN ||
Harvey Harrison551509d2009-02-11 14:11:36 -08002578 cbw->Signature != cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 USB_BULK_CB_SIG)) {
2580 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2581 req->actual,
2582 le32_to_cpu(cbw->Signature));
2583
Alan Sternb950bdb2008-04-14 11:45:29 -04002584 /* The Bulk-only spec says we MUST stall the IN endpoint
2585 * (6.6.1), so it's unavoidable. It also says we must
2586 * retain this state until the next reset, but there's
2587 * no way to tell the controller driver it should ignore
2588 * Clear-Feature(HALT) requests.
2589 *
2590 * We aren't required to halt the OUT endpoint; instead
2591 * we can simply accept and discard any data received
2592 * until the next reset. */
David Lopo62fd2ca2008-04-29 10:14:38 +01002593 wedge_bulk_in_endpoint(fsg);
Alan Sternb950bdb2008-04-14 11:45:29 -04002594 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return -EINVAL;
2596 }
2597
2598 /* Is the CBW meaningful? */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002599 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
Alan Stern12943f02007-08-24 16:27:50 -04002600 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2602 "cmdlen %u\n",
2603 cbw->Lun, cbw->Flags, cbw->Length);
2604
2605 /* We can do anything we want here, so let's stall the
2606 * bulk pipes if we are allowed to. */
2607 if (mod_data.can_stall) {
2608 fsg_set_halt(fsg, fsg->bulk_out);
2609 halt_bulk_in_endpoint(fsg);
2610 }
2611 return -EINVAL;
2612 }
2613
2614 /* Save the command for later */
2615 fsg->cmnd_size = cbw->Length;
2616 memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
2617 if (cbw->Flags & USB_BULK_IN_FLAG)
2618 fsg->data_dir = DATA_DIR_TO_HOST;
2619 else
2620 fsg->data_dir = DATA_DIR_FROM_HOST;
2621 fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
2622 if (fsg->data_size == 0)
2623 fsg->data_dir = DATA_DIR_NONE;
2624 fsg->lun = cbw->Lun;
2625 fsg->tag = cbw->Tag;
2626 return 0;
2627}
2628
2629
2630static int get_next_command(struct fsg_dev *fsg)
2631{
2632 struct fsg_buffhd *bh;
2633 int rc = 0;
2634
2635 if (transport_is_bbb()) {
2636
2637 /* Wait for the next buffer to become available */
2638 bh = fsg->next_buffhd_to_fill;
2639 while (bh->state != BUF_STATE_EMPTY) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002640 rc = sleep_thread(fsg);
2641 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
2645 /* Queue a request to read a Bulk-only CBW */
Mian Yousaf Kaukab806e8f82011-03-24 12:20:13 +01002646 bh->outreq->length = USB_BULK_CB_WRAP_LEN;
2647 bh->outreq->short_not_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 start_transfer(fsg, fsg->bulk_out, bh->outreq,
2649 &bh->outreq_busy, &bh->state);
2650
2651 /* We will drain the buffer in software, which means we
2652 * can reuse it for the next filling. No need to advance
2653 * next_buffhd_to_fill. */
2654
2655 /* Wait for the CBW to arrive */
2656 while (bh->state != BUF_STATE_FULL) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002657 rc = sleep_thread(fsg);
2658 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002660 }
Alan Sterna21d4fe2005-11-29 12:04:24 -05002661 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 rc = received_cbw(fsg, bh);
2663 bh->state = BUF_STATE_EMPTY;
2664
2665 } else { // USB_PR_CB or USB_PR_CBI
2666
2667 /* Wait for the next command to arrive */
2668 while (fsg->cbbuf_cmnd_size == 0) {
Alan Stern79a7d9e2007-08-08 17:10:11 -04002669 rc = sleep_thread(fsg);
2670 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return rc;
Alan Stern79a7d9e2007-08-08 17:10:11 -04002672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 /* Is the previous status interrupt request still busy?
2675 * The host is allowed to skip reading the status,
2676 * so we must cancel it. */
2677 if (fsg->intreq_busy)
2678 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
2679
2680 /* Copy the command and mark the buffer empty */
2681 fsg->data_dir = DATA_DIR_UNKNOWN;
2682 spin_lock_irq(&fsg->lock);
2683 fsg->cmnd_size = fsg->cbbuf_cmnd_size;
2684 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
2685 fsg->cbbuf_cmnd_size = 0;
2686 spin_unlock_irq(&fsg->lock);
2687 }
2688 return rc;
2689}
2690
2691
2692/*-------------------------------------------------------------------------*/
2693
2694static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
2695 const struct usb_endpoint_descriptor *d)
2696{
2697 int rc;
2698
2699 ep->driver_data = fsg;
2700 rc = usb_ep_enable(ep, d);
2701 if (rc)
2702 ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
2703 return rc;
2704}
2705
2706static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
2707 struct usb_request **preq)
2708{
2709 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2710 if (*preq)
2711 return 0;
2712 ERROR(fsg, "can't allocate request for %s\n", ep->name);
2713 return -ENOMEM;
2714}
2715
2716/*
2717 * Reset interface setting and re-init endpoint state (toggle etc).
2718 * Call with altsetting < 0 to disable the interface. The only other
2719 * available altsetting is 0, which enables the interface.
2720 */
2721static int do_set_interface(struct fsg_dev *fsg, int altsetting)
2722{
2723 int rc = 0;
2724 int i;
2725 const struct usb_endpoint_descriptor *d;
2726
2727 if (fsg->running)
2728 DBG(fsg, "reset interface\n");
2729
2730reset:
2731 /* Deallocate the requests */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002732 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 struct fsg_buffhd *bh = &fsg->buffhds[i];
2734
2735 if (bh->inreq) {
2736 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2737 bh->inreq = NULL;
2738 }
2739 if (bh->outreq) {
2740 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2741 bh->outreq = NULL;
2742 }
2743 }
2744 if (fsg->intreq) {
2745 usb_ep_free_request(fsg->intr_in, fsg->intreq);
2746 fsg->intreq = NULL;
2747 }
2748
2749 /* Disable the endpoints */
2750 if (fsg->bulk_in_enabled) {
2751 usb_ep_disable(fsg->bulk_in);
2752 fsg->bulk_in_enabled = 0;
2753 }
2754 if (fsg->bulk_out_enabled) {
2755 usb_ep_disable(fsg->bulk_out);
2756 fsg->bulk_out_enabled = 0;
2757 }
2758 if (fsg->intr_in_enabled) {
2759 usb_ep_disable(fsg->intr_in);
2760 fsg->intr_in_enabled = 0;
2761 }
2762
2763 fsg->running = 0;
2764 if (altsetting < 0 || rc != 0)
2765 return rc;
2766
2767 DBG(fsg, "set interface %d\n", altsetting);
2768
2769 /* Enable the endpoints */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002770 d = fsg_ep_desc(fsg->gadget,
2771 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
2773 goto reset;
2774 fsg->bulk_in_enabled = 1;
2775
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002776 d = fsg_ep_desc(fsg->gadget,
2777 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
2779 goto reset;
2780 fsg->bulk_out_enabled = 1;
2781 fsg->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
Alan Sternb950bdb2008-04-14 11:45:29 -04002782 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 if (transport_is_cbi()) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002785 d = fsg_ep_desc(fsg->gadget,
2786 &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
2788 goto reset;
2789 fsg->intr_in_enabled = 1;
2790 }
2791
2792 /* Allocate the requests */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002793 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct fsg_buffhd *bh = &fsg->buffhds[i];
2795
2796 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
2797 goto reset;
2798 if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
2799 goto reset;
2800 bh->inreq->buf = bh->outreq->buf = bh->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 bh->inreq->context = bh->outreq->context = bh;
2802 bh->inreq->complete = bulk_in_complete;
2803 bh->outreq->complete = bulk_out_complete;
2804 }
2805 if (transport_is_cbi()) {
2806 if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
2807 goto reset;
2808 fsg->intreq->complete = intr_in_complete;
2809 }
2810
2811 fsg->running = 1;
2812 for (i = 0; i < fsg->nluns; ++i)
2813 fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2814 return rc;
2815}
2816
2817
2818/*
2819 * Change our operational configuration. This code must agree with the code
2820 * that returns config descriptors, and with interface altsetting code.
2821 *
2822 * It's also responsible for power management interactions. Some
2823 * configurations might not work with our current power sources.
2824 * For now we just assume the gadget is always self-powered.
2825 */
2826static int do_set_config(struct fsg_dev *fsg, u8 new_config)
2827{
2828 int rc = 0;
2829
2830 /* Disable the single interface */
2831 if (fsg->config != 0) {
2832 DBG(fsg, "reset config\n");
2833 fsg->config = 0;
2834 rc = do_set_interface(fsg, -1);
2835 }
2836
2837 /* Enable the interface */
2838 if (new_config != 0) {
2839 fsg->config = new_config;
2840 if ((rc = do_set_interface(fsg, 0)) != 0)
2841 fsg->config = 0; // Reset on errors
2842 else {
2843 char *speed;
2844
2845 switch (fsg->gadget->speed) {
2846 case USB_SPEED_LOW: speed = "low"; break;
2847 case USB_SPEED_FULL: speed = "full"; break;
2848 case USB_SPEED_HIGH: speed = "high"; break;
2849 default: speed = "?"; break;
2850 }
2851 INFO(fsg, "%s speed config #%d\n", speed, fsg->config);
2852 }
2853 }
2854 return rc;
2855}
2856
2857
2858/*-------------------------------------------------------------------------*/
2859
2860static void handle_exception(struct fsg_dev *fsg)
2861{
2862 siginfo_t info;
2863 int sig;
2864 int i;
2865 int num_active;
2866 struct fsg_buffhd *bh;
2867 enum fsg_state old_state;
2868 u8 new_config;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002869 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 unsigned int exception_req_tag;
2871 int rc;
2872
2873 /* Clear the existing signals. Anything but SIGUSR1 is converted
2874 * into a high-priority EXIT exception. */
2875 for (;;) {
Oleg Nesterov8cfbe7e2007-05-30 11:06:33 -04002876 sig = dequeue_signal_lock(current, &current->blocked, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (!sig)
2878 break;
2879 if (sig != SIGUSR1) {
2880 if (fsg->state < FSG_STATE_EXIT)
2881 DBG(fsg, "Main thread exiting on signal\n");
2882 raise_exception(fsg, FSG_STATE_EXIT);
2883 }
2884 }
2885
2886 /* Cancel all the pending transfers */
2887 if (fsg->intreq_busy)
2888 usb_ep_dequeue(fsg->intr_in, fsg->intreq);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002889 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 bh = &fsg->buffhds[i];
2891 if (bh->inreq_busy)
2892 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
2893 if (bh->outreq_busy)
2894 usb_ep_dequeue(fsg->bulk_out, bh->outreq);
2895 }
2896
2897 /* Wait until everything is idle */
2898 for (;;) {
2899 num_active = fsg->intreq_busy;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002900 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 bh = &fsg->buffhds[i];
2902 num_active += bh->inreq_busy + bh->outreq_busy;
2903 }
2904 if (num_active == 0)
2905 break;
2906 if (sleep_thread(fsg))
2907 return;
2908 }
2909
2910 /* Clear out the controller's fifos */
2911 if (fsg->bulk_in_enabled)
2912 usb_ep_fifo_flush(fsg->bulk_in);
2913 if (fsg->bulk_out_enabled)
2914 usb_ep_fifo_flush(fsg->bulk_out);
2915 if (fsg->intr_in_enabled)
2916 usb_ep_fifo_flush(fsg->intr_in);
2917
2918 /* Reset the I/O buffer states and pointers, the SCSI
2919 * state, and the exception. Then invoke the handler. */
2920 spin_lock_irq(&fsg->lock);
2921
Michal Nazarewiczd6181702009-10-28 16:57:15 +01002922 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 bh = &fsg->buffhds[i];
2924 bh->state = BUF_STATE_EMPTY;
2925 }
2926 fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
2927 &fsg->buffhds[0];
2928
2929 exception_req_tag = fsg->exception_req_tag;
2930 new_config = fsg->new_config;
2931 old_state = fsg->state;
2932
2933 if (old_state == FSG_STATE_ABORT_BULK_OUT)
2934 fsg->state = FSG_STATE_STATUS_PHASE;
2935 else {
2936 for (i = 0; i < fsg->nluns; ++i) {
2937 curlun = &fsg->luns[i];
2938 curlun->prevent_medium_removal = 0;
2939 curlun->sense_data = curlun->unit_attention_data =
2940 SS_NO_SENSE;
2941 curlun->sense_data_info = 0;
Alan Stern6174d0f2006-09-26 14:51:48 -04002942 curlun->info_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
2944 fsg->state = FSG_STATE_IDLE;
2945 }
2946 spin_unlock_irq(&fsg->lock);
2947
2948 /* Carry out any extra actions required for the exception */
2949 switch (old_state) {
2950 default:
2951 break;
2952
2953 case FSG_STATE_ABORT_BULK_OUT:
2954 send_status(fsg);
2955 spin_lock_irq(&fsg->lock);
2956 if (fsg->state == FSG_STATE_STATUS_PHASE)
2957 fsg->state = FSG_STATE_IDLE;
2958 spin_unlock_irq(&fsg->lock);
2959 break;
2960
2961 case FSG_STATE_RESET:
2962 /* In case we were forced against our will to halt a
2963 * bulk endpoint, clear the halt now. (The SuperH UDC
2964 * requires this.) */
Alan Sternb950bdb2008-04-14 11:45:29 -04002965 if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 usb_ep_clear_halt(fsg->bulk_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 if (transport_is_bbb()) {
2969 if (fsg->ep0_req_tag == exception_req_tag)
2970 ep0_queue(fsg); // Complete the status stage
2971
2972 } else if (transport_is_cbi())
2973 send_status(fsg); // Status by interrupt pipe
2974
2975 /* Technically this should go here, but it would only be
2976 * a waste of time. Ditto for the INTERFACE_CHANGE and
2977 * CONFIG_CHANGE cases. */
2978 // for (i = 0; i < fsg->nluns; ++i)
2979 // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2980 break;
2981
2982 case FSG_STATE_INTERFACE_CHANGE:
2983 rc = do_set_interface(fsg, 0);
2984 if (fsg->ep0_req_tag != exception_req_tag)
2985 break;
2986 if (rc != 0) // STALL on errors
2987 fsg_set_halt(fsg, fsg->ep0);
2988 else // Complete the status stage
2989 ep0_queue(fsg);
2990 break;
2991
2992 case FSG_STATE_CONFIG_CHANGE:
2993 rc = do_set_config(fsg, new_config);
2994 if (fsg->ep0_req_tag != exception_req_tag)
2995 break;
2996 if (rc != 0) // STALL on errors
2997 fsg_set_halt(fsg, fsg->ep0);
2998 else // Complete the status stage
2999 ep0_queue(fsg);
3000 break;
3001
3002 case FSG_STATE_DISCONNECT:
Michal Nazarewiczb6058d02009-10-28 16:57:14 +01003003 for (i = 0; i < fsg->nluns; ++i)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003004 fsg_lun_fsync_sub(fsg->luns + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 do_set_config(fsg, 0); // Unconfigured state
3006 break;
3007
3008 case FSG_STATE_EXIT:
3009 case FSG_STATE_TERMINATED:
3010 do_set_config(fsg, 0); // Free resources
3011 spin_lock_irq(&fsg->lock);
3012 fsg->state = FSG_STATE_TERMINATED; // Stop the thread
3013 spin_unlock_irq(&fsg->lock);
3014 break;
3015 }
3016}
3017
3018
3019/*-------------------------------------------------------------------------*/
3020
3021static int fsg_main_thread(void *fsg_)
3022{
John Daiker7ca46b82006-12-29 19:02:06 -08003023 struct fsg_dev *fsg = fsg_;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 /* Allow the thread to be killed by a signal, but set the signal mask
3026 * to block everything but INT, TERM, KILL, and USR1. */
Oleg Nesterov8cfbe7e2007-05-30 11:06:33 -04003027 allow_signal(SIGINT);
3028 allow_signal(SIGTERM);
3029 allow_signal(SIGKILL);
3030 allow_signal(SIGUSR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003032 /* Allow the thread to be frozen */
3033 set_freezable();
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /* Arrange for userspace references to be interpreted as kernel
3036 * pointers. That way we can pass a kernel pointer to a routine
3037 * that expects a __user pointer and it will work okay. */
3038 set_fs(get_ds());
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 /* The main loop */
3041 while (fsg->state != FSG_STATE_TERMINATED) {
3042 if (exception_in_progress(fsg) || signal_pending(current)) {
3043 handle_exception(fsg);
3044 continue;
3045 }
3046
3047 if (!fsg->running) {
3048 sleep_thread(fsg);
3049 continue;
3050 }
3051
3052 if (get_next_command(fsg))
3053 continue;
3054
3055 spin_lock_irq(&fsg->lock);
3056 if (!exception_in_progress(fsg))
3057 fsg->state = FSG_STATE_DATA_PHASE;
3058 spin_unlock_irq(&fsg->lock);
3059
3060 if (do_scsi_command(fsg) || finish_reply(fsg))
3061 continue;
3062
3063 spin_lock_irq(&fsg->lock);
3064 if (!exception_in_progress(fsg))
3065 fsg->state = FSG_STATE_STATUS_PHASE;
3066 spin_unlock_irq(&fsg->lock);
3067
3068 if (send_status(fsg))
3069 continue;
3070
3071 spin_lock_irq(&fsg->lock);
3072 if (!exception_in_progress(fsg))
3073 fsg->state = FSG_STATE_IDLE;
3074 spin_unlock_irq(&fsg->lock);
3075 }
3076
Alan Stern22efcf42005-09-26 16:12:02 -04003077 spin_lock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 fsg->thread_task = NULL;
Alan Stern22efcf42005-09-26 16:12:02 -04003079 spin_unlock_irq(&fsg->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Alan Stern82a10a82009-04-16 15:37:28 -04003081 /* If we are exiting because of a signal, unregister the
3082 * gadget driver. */
3083 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 usb_gadget_unregister_driver(&fsg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 /* Let the unbind and cleanup routines know the thread has exited */
3087 complete_and_exit(&fsg->thread_notifier, 0);
3088}
3089
3090
3091/*-------------------------------------------------------------------------*/
3092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094/* The write permissions and store_xxx pointers are set in fsg_bind() */
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003095static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003096static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003097static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099
3100/*-------------------------------------------------------------------------*/
3101
Alan Stern87c42522005-11-09 16:59:56 -05003102static void fsg_release(struct kref *ref)
3103{
3104 struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
3105
3106 kfree(fsg->luns);
3107 kfree(fsg);
3108}
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110static void lun_release(struct device *dev)
3111{
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003112 struct rw_semaphore *filesem = dev_get_drvdata(dev);
3113 struct fsg_dev *fsg =
3114 container_of(filesem, struct fsg_dev, filesem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Alan Stern87c42522005-11-09 16:59:56 -05003116 kref_put(&fsg->ref, fsg_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
David Brownella3536782006-07-06 15:48:53 -07003119static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
3121 struct fsg_dev *fsg = get_gadget_data(gadget);
3122 int i;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003123 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct usb_request *req = fsg->ep0req;
3125
3126 DBG(fsg, "unbind\n");
3127 clear_bit(REGISTERED, &fsg->atomic_bitflags);
3128
3129 /* Unregister the sysfs attribute files and the LUNs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 for (i = 0; i < fsg->nluns; ++i) {
3131 curlun = &fsg->luns[i];
3132 if (curlun->registered) {
Michal Nazarewicz452ee0e2010-08-12 17:43:50 +02003133 device_remove_file(&curlun->dev, &dev_attr_nofua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 device_remove_file(&curlun->dev, &dev_attr_ro);
3135 device_remove_file(&curlun->dev, &dev_attr_file);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003136 fsg_lun_close(curlun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 device_unregister(&curlun->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 curlun->registered = 0;
3139 }
3140 }
3141
3142 /* If the thread isn't already dead, tell it to exit now */
3143 if (fsg->state != FSG_STATE_TERMINATED) {
3144 raise_exception(fsg, FSG_STATE_EXIT);
3145 wait_for_completion(&fsg->thread_notifier);
3146
3147 /* The cleanup routine waits for this completion also */
3148 complete(&fsg->thread_notifier);
3149 }
3150
3151 /* Free the data buffers */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003152 for (i = 0; i < FSG_NUM_BUFFERS; ++i)
David Brownell9d8bab52007-07-01 11:04:54 -07003153 kfree(fsg->buffhds[i].buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 /* Free the request and buffer for endpoint 0 */
3156 if (req) {
David Brownell9d8bab52007-07-01 11:04:54 -07003157 kfree(req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 usb_ep_free_request(fsg->ep0, req);
3159 }
3160
3161 set_gadget_data(gadget, NULL);
3162}
3163
3164
3165static int __init check_parameters(struct fsg_dev *fsg)
3166{
3167 int prot;
David Brownell91e79c92005-07-13 15:18:30 -07003168 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 /* Store the default values */
3171 mod_data.transport_type = USB_PR_BULK;
3172 mod_data.transport_name = "Bulk-only";
3173 mod_data.protocol_type = USB_SC_SCSI;
3174 mod_data.protocol_name = "Transparent SCSI";
3175
Alan Sternce459ec2009-02-24 16:19:47 -05003176 /* Some peripheral controllers are known not to be able to
3177 * halt bulk endpoints correctly. If one of them is present,
3178 * disable stalls.
3179 */
Christoph Egger90f79762010-02-05 13:24:12 +01003180 if (gadget_is_at91(fsg->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 mod_data.can_stall = 0;
3182
3183 if (mod_data.release == 0xffff) { // Parameter wasn't set
Christoph Egger90f79762010-02-05 13:24:12 +01003184 gcnum = usb_gadget_controller_number(fsg->gadget);
David Brownell91e79c92005-07-13 15:18:30 -07003185 if (gcnum >= 0)
3186 mod_data.release = 0x0300 + gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 else {
Arjan van de Venb6c63932008-07-25 01:45:52 -07003188 WARNING(fsg, "controller '%s' not recognized\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 fsg->gadget->name);
3190 mod_data.release = 0x0399;
3191 }
3192 }
3193
3194 prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
3195
3196#ifdef CONFIG_USB_FILE_STORAGE_TEST
3197 if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
3198 ; // Use default setting
3199 } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
3200 mod_data.transport_type = USB_PR_CB;
3201 mod_data.transport_name = "Control-Bulk";
3202 } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
3203 mod_data.transport_type = USB_PR_CBI;
3204 mod_data.transport_name = "Control-Bulk-Interrupt";
3205 } else {
3206 ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
3207 return -EINVAL;
3208 }
3209
3210 if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
3211 prot == USB_SC_SCSI) {
3212 ; // Use default setting
3213 } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
3214 prot == USB_SC_RBC) {
3215 mod_data.protocol_type = USB_SC_RBC;
3216 mod_data.protocol_name = "RBC";
3217 } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
3218 strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
3219 prot == USB_SC_8020) {
3220 mod_data.protocol_type = USB_SC_8020;
3221 mod_data.protocol_name = "8020i (ATAPI)";
3222 } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
3223 prot == USB_SC_QIC) {
3224 mod_data.protocol_type = USB_SC_QIC;
3225 mod_data.protocol_name = "QIC-157";
3226 } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
3227 prot == USB_SC_UFI) {
3228 mod_data.protocol_type = USB_SC_UFI;
3229 mod_data.protocol_name = "UFI";
3230 } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
3231 prot == USB_SC_8070) {
3232 mod_data.protocol_type = USB_SC_8070;
3233 mod_data.protocol_name = "8070i";
3234 } else {
3235 ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
3236 return -EINVAL;
3237 }
3238
3239 mod_data.buflen &= PAGE_CACHE_MASK;
3240 if (mod_data.buflen <= 0) {
3241 ERROR(fsg, "invalid buflen\n");
3242 return -ETOOSMALL;
3243 }
Yann Cantin87eb1be2010-06-05 23:06:31 +02003244
Michal Nazarewicz56706492010-07-22 14:16:37 +02003245#endif /* CONFIG_USB_FILE_STORAGE_TEST */
3246
Yann Cantin87eb1be2010-06-05 23:06:31 +02003247 /* Serial string handling.
3248 * On a real device, the serial string would be loaded
3249 * from permanent storage. */
Michal Nazarewicz56706492010-07-22 14:16:37 +02003250 if (mod_data.serial) {
Yann Cantin87eb1be2010-06-05 23:06:31 +02003251 const char *ch;
3252 unsigned len = 0;
3253
3254 /* Sanity check :
3255 * The CB[I] specification limits the serial string to
3256 * 12 uppercase hexadecimal characters.
3257 * BBB need at least 12 uppercase hexadecimal characters,
3258 * with a maximum of 126. */
Michal Nazarewicz56706492010-07-22 14:16:37 +02003259 for (ch = mod_data.serial; *ch; ++ch) {
Yann Cantin87eb1be2010-06-05 23:06:31 +02003260 ++len;
3261 if ((*ch < '0' || *ch > '9') &&
3262 (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
3263 WARNING(fsg,
Alan Sternd8087422010-09-03 11:15:41 -04003264 "Invalid serial string character: %c\n",
Yann Cantin87eb1be2010-06-05 23:06:31 +02003265 *ch);
Alan Sternd8087422010-09-03 11:15:41 -04003266 goto no_serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003267 }
3268 }
3269 if (len > 126 ||
3270 (mod_data.transport_type == USB_PR_BULK && len < 12) ||
3271 (mod_data.transport_type != USB_PR_BULK && len > 12)) {
Alan Sternd8087422010-09-03 11:15:41 -04003272 WARNING(fsg, "Invalid serial string length!\n");
3273 goto no_serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003274 }
Michal Nazarewicz56706492010-07-22 14:16:37 +02003275 fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003276 } else {
Alan Sternd8087422010-09-03 11:15:41 -04003277 WARNING(fsg, "No serial-number string provided!\n");
3278 no_serial:
3279 device_desc.iSerialNumber = 0;
Yann Cantin87eb1be2010-06-05 23:06:31 +02003280 }
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return 0;
3283}
3284
3285
Michal Nazarewicze12995e2010-08-12 17:43:52 +02003286static int __init fsg_bind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
3288 struct fsg_dev *fsg = the_fsg;
3289 int rc;
3290 int i;
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003291 struct fsg_lun *curlun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 struct usb_ep *ep;
3293 struct usb_request *req;
3294 char *pathbuf, *p;
3295
3296 fsg->gadget = gadget;
3297 set_gadget_data(gadget, fsg);
3298 fsg->ep0 = gadget->ep0;
3299 fsg->ep0->driver_data = fsg;
3300
3301 if ((rc = check_parameters(fsg)) != 0)
3302 goto out;
3303
3304 if (mod_data.removable) { // Enable the store_xxx attributes
Alan Stern12aae682008-11-20 14:13:12 -05003305 dev_attr_file.attr.mode = 0644;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003306 dev_attr_file.store = fsg_store_file;
Alan Stern12aae682008-11-20 14:13:12 -05003307 if (!mod_data.cdrom) {
3308 dev_attr_ro.attr.mode = 0644;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003309 dev_attr_ro.store = fsg_store_ro;
Alan Stern12aae682008-11-20 14:13:12 -05003310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
3312
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003313 /* Only for removable media? */
3314 dev_attr_nofua.attr.mode = 0644;
3315 dev_attr_nofua.store = fsg_store_nofua;
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 /* Find out how many LUNs there should be */
3318 i = mod_data.nluns;
3319 if (i == 0)
David Brownell2e806f62007-08-02 00:03:39 -07003320 i = max(mod_data.num_filenames, 1u);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003321 if (i > FSG_MAX_LUNS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 ERROR(fsg, "invalid number of LUNs: %d\n", i);
3323 rc = -EINVAL;
3324 goto out;
3325 }
3326
3327 /* Create the LUNs, open their backing files, and register the
3328 * LUN devices in sysfs. */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003329 fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (!fsg->luns) {
3331 rc = -ENOMEM;
3332 goto out;
3333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 fsg->nluns = i;
3335
3336 for (i = 0; i < fsg->nluns; ++i) {
3337 curlun = &fsg->luns[i];
Michal Nazarewicze909ef52009-10-28 16:57:16 +01003338 curlun->cdrom = !!mod_data.cdrom;
3339 curlun->ro = mod_data.cdrom || mod_data.ro[i];
3340 curlun->initially_ro = curlun->ro;
3341 curlun->removable = mod_data.removable;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003342 curlun->nofua = mod_data.nofua[i];
Alan Stern2de9eae2006-09-25 14:31:15 -04003343 curlun->dev.release = lun_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 curlun->dev.parent = &gadget->dev;
3345 curlun->dev.driver = &fsg_driver.driver;
Michal Nazarewicz93f93742009-10-28 16:57:17 +01003346 dev_set_drvdata(&curlun->dev, &fsg->filesem);
Kay Sievers0031a062008-05-02 06:02:41 +02003347 dev_set_name(&curlun->dev,"%s-lun%d",
3348 dev_name(&gadget->dev), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003350 kref_get(&fsg->ref);
3351 rc = device_register(&curlun->dev);
3352 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003354 put_device(&curlun->dev);
Alan Stern2de9eae2006-09-25 14:31:15 -04003355 goto out;
3356 }
3357 curlun->registered = 1;
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003358
3359 rc = device_create_file(&curlun->dev, &dev_attr_ro);
3360 if (rc)
3361 goto out;
3362 rc = device_create_file(&curlun->dev, &dev_attr_nofua);
3363 if (rc)
3364 goto out;
3365 rc = device_create_file(&curlun->dev, &dev_attr_file);
3366 if (rc)
3367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Alan Sternaafe5bd2006-03-31 11:46:43 -05003369 if (mod_data.file[i] && *mod_data.file[i]) {
Michal Nazarewiczd9385b62010-10-28 17:31:18 +02003370 rc = fsg_lun_open(curlun, mod_data.file[i]);
3371 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 goto out;
3373 } else if (!mod_data.removable) {
3374 ERROR(fsg, "no file given for LUN%d\n", i);
3375 rc = -EINVAL;
3376 goto out;
3377 }
3378 }
3379
3380 /* Find all the endpoints we will use */
3381 usb_ep_autoconfig_reset(gadget);
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003382 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 if (!ep)
3384 goto autoconf_fail;
3385 ep->driver_data = fsg; // claim the endpoint
3386 fsg->bulk_in = ep;
3387
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003388 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (!ep)
3390 goto autoconf_fail;
3391 ep->driver_data = fsg; // claim the endpoint
3392 fsg->bulk_out = ep;
3393
3394 if (transport_is_cbi()) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003395 ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 if (!ep)
3397 goto autoconf_fail;
3398 ep->driver_data = fsg; // claim the endpoint
3399 fsg->intr_in = ep;
3400 }
3401
3402 /* Fix up the descriptors */
3403 device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
3404 device_desc.idVendor = cpu_to_le16(mod_data.vendor);
3405 device_desc.idProduct = cpu_to_le16(mod_data.product);
3406 device_desc.bcdDevice = cpu_to_le16(mod_data.release);
3407
3408 i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003409 fsg_intf_desc.bNumEndpoints = i;
3410 fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
3411 fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
3412 fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
David Brownell2e806f62007-08-02 00:03:39 -07003414 if (gadget_is_dualspeed(gadget)) {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003415 fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
David Brownell2e806f62007-08-02 00:03:39 -07003417 /* Assume ep0 uses the same maxpacket value for both speeds */
3418 dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
David Brownell2e806f62007-08-02 00:03:39 -07003420 /* Assume endpoint addresses are the same for both speeds */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003421 fsg_hs_bulk_in_desc.bEndpointAddress =
3422 fsg_fs_bulk_in_desc.bEndpointAddress;
3423 fsg_hs_bulk_out_desc.bEndpointAddress =
3424 fsg_fs_bulk_out_desc.bEndpointAddress;
3425 fsg_hs_intr_in_desc.bEndpointAddress =
3426 fsg_fs_intr_in_desc.bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 }
3428
David Brownell2e806f62007-08-02 00:03:39 -07003429 if (gadget_is_otg(gadget))
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003430 fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
David Brownell2e806f62007-08-02 00:03:39 -07003431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 rc = -ENOMEM;
3433
3434 /* Allocate the request and buffer for endpoint 0 */
3435 fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
3436 if (!req)
3437 goto out;
David Brownell9d8bab52007-07-01 11:04:54 -07003438 req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 if (!req->buf)
3440 goto out;
3441 req->complete = ep0_complete;
3442
3443 /* Allocate the data buffers */
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003444 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 struct fsg_buffhd *bh = &fsg->buffhds[i];
3446
Alan Stern14cd5f82006-03-23 15:07:25 -05003447 /* Allocate for the bulk-in endpoint. We assume that
3448 * the buffer will also work with the bulk-out (and
3449 * interrupt-in) endpoint. */
David Brownell9d8bab52007-07-01 11:04:54 -07003450 bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 if (!bh->buf)
3452 goto out;
3453 bh->next = bh + 1;
3454 }
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003455 fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 /* This should reflect the actual gadget power source */
3458 usb_gadget_set_selfpowered(gadget);
3459
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003460 snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
3461 "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07003462 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 gadget->name);
3464
Alan Stern22efcf42005-09-26 16:12:02 -04003465 fsg->thread_task = kthread_create(fsg_main_thread, fsg,
3466 "file-storage-gadget");
3467 if (IS_ERR(fsg->thread_task)) {
3468 rc = PTR_ERR(fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 goto out;
Alan Stern22efcf42005-09-26 16:12:02 -04003470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
3473 INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
3474
3475 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
3476 for (i = 0; i < fsg->nluns; ++i) {
3477 curlun = &fsg->luns[i];
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003478 if (fsg_lun_is_open(curlun)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 p = NULL;
3480 if (pathbuf) {
Jan Blunckcf28b482008-02-14 19:38:44 -08003481 p = d_path(&curlun->filp->f_path,
3482 pathbuf, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 if (IS_ERR(p))
3484 p = NULL;
3485 }
Andy Shevchenkoa93917d2010-07-22 17:53:56 +03003486 LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
3487 curlun->ro, curlun->nofua, (p ? p : "(error)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
3489 }
3490 kfree(pathbuf);
3491
3492 DBG(fsg, "transport=%s (x%02x)\n",
3493 mod_data.transport_name, mod_data.transport_type);
3494 DBG(fsg, "protocol=%s (x%02x)\n",
3495 mod_data.protocol_name, mod_data.protocol_type);
3496 DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
3497 mod_data.vendor, mod_data.product, mod_data.release);
Alan Stern12aae682008-11-20 14:13:12 -05003498 DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 mod_data.removable, mod_data.can_stall,
Alan Stern12aae682008-11-20 14:13:12 -05003500 mod_data.cdrom, mod_data.buflen);
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003501 DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
Alan Sterna922c682005-10-06 16:38:45 -04003502
3503 set_bit(REGISTERED, &fsg->atomic_bitflags);
3504
3505 /* Tell the thread to start working */
3506 wake_up_process(fsg->thread_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return 0;
3508
3509autoconf_fail:
3510 ERROR(fsg, "unable to autoconfigure all endpoints\n");
3511 rc = -ENOTSUPP;
3512
3513out:
3514 fsg->state = FSG_STATE_TERMINATED; // The thread is dead
3515 fsg_unbind(gadget);
Felipe Balbi889394b2008-12-08 13:50:27 +02003516 complete(&fsg->thread_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return rc;
3518}
3519
3520
3521/*-------------------------------------------------------------------------*/
3522
3523static void fsg_suspend(struct usb_gadget *gadget)
3524{
3525 struct fsg_dev *fsg = get_gadget_data(gadget);
3526
3527 DBG(fsg, "suspend\n");
3528 set_bit(SUSPENDED, &fsg->atomic_bitflags);
3529}
3530
3531static void fsg_resume(struct usb_gadget *gadget)
3532{
3533 struct fsg_dev *fsg = get_gadget_data(gadget);
3534
3535 DBG(fsg, "resume\n");
3536 clear_bit(SUSPENDED, &fsg->atomic_bitflags);
3537}
3538
3539
3540/*-------------------------------------------------------------------------*/
3541
3542static struct usb_gadget_driver fsg_driver = {
3543#ifdef CONFIG_USB_GADGET_DUALSPEED
3544 .speed = USB_SPEED_HIGH,
3545#else
3546 .speed = USB_SPEED_FULL,
3547#endif
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003548 .function = (char *) fsg_string_product,
David Brownell6bea4762006-12-05 03:15:33 -08003549 .unbind = fsg_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 .disconnect = fsg_disconnect,
3551 .setup = fsg_setup,
3552 .suspend = fsg_suspend,
3553 .resume = fsg_resume,
3554
3555 .driver = {
Michal Nazarewiczd6181702009-10-28 16:57:15 +01003556 .name = DRIVER_NAME,
Ben Dooksd0d50492005-10-10 10:52:33 +01003557 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 // .release = ...
3559 // .suspend = ...
3560 // .resume = ...
3561 },
3562};
3563
3564
3565static int __init fsg_alloc(void)
3566{
3567 struct fsg_dev *fsg;
3568
Alan Sterna922c682005-10-06 16:38:45 -04003569 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (!fsg)
3571 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 spin_lock_init(&fsg->lock);
3573 init_rwsem(&fsg->filesem);
Alan Stern87c42522005-11-09 16:59:56 -05003574 kref_init(&fsg->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 init_completion(&fsg->thread_notifier);
3576
3577 the_fsg = fsg;
3578 return 0;
3579}
3580
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582static int __init fsg_init(void)
3583{
3584 int rc;
3585 struct fsg_dev *fsg;
3586
3587 if ((rc = fsg_alloc()) != 0)
3588 return rc;
3589 fsg = the_fsg;
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02003590 if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
Alan Stern87c42522005-11-09 16:59:56 -05003591 kref_put(&fsg->ref, fsg_release);
Alan Sterna922c682005-10-06 16:38:45 -04003592 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594module_init(fsg_init);
3595
3596
3597static void __exit fsg_cleanup(void)
3598{
3599 struct fsg_dev *fsg = the_fsg;
3600
3601 /* Unregister the driver iff the thread hasn't already done so */
3602 if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
3603 usb_gadget_unregister_driver(&fsg_driver);
3604
3605 /* Wait for the thread to finish up */
3606 wait_for_completion(&fsg->thread_notifier);
3607
Alan Stern87c42522005-11-09 16:59:56 -05003608 kref_put(&fsg->ref, fsg_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610module_exit(fsg_cleanup);