Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * file_storage.c -- File-backed USB Storage Gadget, for USB development |
| 3 | * |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Alan Stern |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * 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 Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 41 | * appearing to the host as a disk drive or as a CD-ROM drive. In addition |
| 42 | * to providing an example of a genuinely useful gadget driver for a USB |
| 43 | * device, it also illustrates a technique of double-buffering for increased |
| 44 | * throughput. Last but not least, it gives an easy way to probe the |
| 45 | * behavior of the Mass Storage drivers in a USB host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * |
| 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 Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 49 | * setting the optional "ro" module parameter. (For CD-ROM emulation, |
| 50 | * access is always read-only.) The gadget will indicate that it has |
| 51 | * removable media if the optional "removable" module parameter is set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * |
| 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 Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 59 | * Vendor ID, Product ID, release number and serial number can be overridden. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * |
| 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 Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 68 | * or empty backing filename means the LUN's medium is not loaded. Ideally |
| 69 | * each LUN would be settable independently as a disk drive or a CD-ROM |
| 70 | * drive, but currently all LUNs have to be the same type. The CD-ROM |
| 71 | * emulation includes a single data track and no audio tracks; hence there |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 72 | * need be only one backing file per LUN. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | * |
| 74 | * Requirements are modest; only a bulk-in and a bulk-out endpoint are |
| 75 | * needed (an interrupt-out endpoint is also needed for CBI). The memory |
| 76 | * requirement amounts to two 16K buffers, size configurable by a parameter. |
| 77 | * Support is included for both full-speed and high-speed operation. |
| 78 | * |
Alan Stern | 14cd5f8 | 2006-03-23 15:07:25 -0500 | [diff] [blame] | 79 | * Note that the driver is slightly non-portable in that it assumes a |
| 80 | * single memory/DMA buffer will be useable for bulk-in, bulk-out, and |
| 81 | * interrupt-in endpoints. With most device controllers this isn't an |
| 82 | * issue, but there may be some with hardware restrictions that prevent |
| 83 | * a buffer from being used by more than one endpoint. |
| 84 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * Module options: |
| 86 | * |
| 87 | * file=filename[,filename...] |
| 88 | * Required if "removable" is not set, names of |
| 89 | * the files or block devices used for |
| 90 | * backing storage |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 91 | * serial=HHHH... Required serial number (string of hex chars) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | * ro=b[,b...] Default false, booleans for read-only access |
| 93 | * removable Default false, boolean for removable media |
| 94 | * luns=N Default N = number of filenames, number of |
| 95 | * LUNs to support |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 96 | * nofua=b[,b...] Default false, booleans for ignore FUA flag |
| 97 | * in SCSI WRITE(10,12) commands |
Alan Stern | d794ac7 | 2005-04-18 12:43:25 -0400 | [diff] [blame] | 98 | * stall Default determined according to the type of |
| 99 | * USB device controller (usually true), |
| 100 | * boolean to permit the driver to halt |
| 101 | * bulk endpoints |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 102 | * cdrom Default false, boolean for whether to emulate |
| 103 | * a CD-ROM drive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * transport=XXX Default BBB, transport name (CB, CBI, or BBB) |
| 105 | * protocol=YYY Default SCSI, protocol name (RBC, 8020 or |
| 106 | * ATAPI, QIC, UFI, 8070, or SCSI; |
| 107 | * also 1 - 6) |
| 108 | * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID |
| 109 | * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID |
| 110 | * release=0xRRRR Override the USB release number (bcdDevice) |
| 111 | * buflen=N Default N=16384, buffer size used (will be |
| 112 | * rounded down to a multiple of |
| 113 | * PAGE_CACHE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 115 | * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro", |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 116 | * "removable", "luns", "nofua", "stall", and "cdrom" options are available; |
| 117 | * default values are used for everything else. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | * |
| 119 | * The pathnames of the backing files and the ro settings are available in |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 120 | * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of |
| 121 | * the gadget's sysfs directory. If the "removable" option is set, writing to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * these files will simulate ejecting/loading the medium (writing an empty |
| 123 | * line means eject) and adjusting a write-enable tab. Changes to the ro |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 124 | * setting are not allowed when the medium is loaded or if CD-ROM emulation |
| 125 | * is being used. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | * |
| 127 | * This gadget driver is heavily based on "Gadget Zero" by David Brownell. |
Alan Stern | aafe5bd | 2006-03-31 11:46:43 -0500 | [diff] [blame] | 128 | * The driver's SCSI command interface was based on the "Information |
| 129 | * technology - Small Computer System Interface - 2" document from |
| 130 | * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at |
| 131 | * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception |
| 132 | * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the |
| 133 | * "Universal Serial Bus Mass Storage Class UFI Command Specification" |
| 134 | * document, Revision 1.0, December 14, 1998, available at |
| 135 | * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | */ |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | * Driver Design |
| 141 | * |
| 142 | * The FSG driver is fairly straightforward. There is a main kernel |
| 143 | * thread that handles most of the work. Interrupt routines field |
| 144 | * callbacks from the controller driver: bulk- and interrupt-request |
| 145 | * completion notifications, endpoint-0 events, and disconnect events. |
| 146 | * Completion events are passed to the main thread by wakeup calls. Many |
| 147 | * ep0 requests are handled at interrupt time, but SetInterface, |
| 148 | * SetConfiguration, and device reset requests are forwarded to the |
| 149 | * thread in the form of "exceptions" using SIGUSR1 signals (since they |
| 150 | * should interrupt any ongoing file I/O operations). |
| 151 | * |
| 152 | * The thread's main routine implements the standard command/data/status |
| 153 | * parts of a SCSI interaction. It and its subroutines are full of tests |
| 154 | * for pending signals/exceptions -- all this polling is necessary since |
| 155 | * the kernel has no setjmp/longjmp equivalents. (Maybe this is an |
| 156 | * indication that the driver really wants to be running in userspace.) |
| 157 | * An important point is that so long as the thread is alive it keeps an |
| 158 | * open reference to the backing file. This will prevent unmounting |
| 159 | * the backing file's underlying filesystem and could cause problems |
| 160 | * during system shutdown, for example. To prevent such problems, the |
| 161 | * thread catches INT, TERM, and KILL signals and converts them into |
| 162 | * an EXIT exception. |
| 163 | * |
| 164 | * In normal operation the main thread is started during the gadget's |
| 165 | * fsg_bind() callback and stopped during fsg_unbind(). But it can also |
| 166 | * exit when it receives a signal, and there's no point leaving the |
| 167 | * gadget running when the thread is dead. So just before the thread |
| 168 | * exits, it deregisters the gadget driver. This makes things a little |
| 169 | * tricky: The driver is deregistered at two places, and the exiting |
| 170 | * thread can indirectly call fsg_unbind() which in turn can tell the |
| 171 | * thread to exit. The first problem is resolved through the use of the |
| 172 | * REGISTERED atomic bitflag; the driver will only be deregistered once. |
| 173 | * The second problem is resolved by having fsg_unbind() check |
| 174 | * fsg->state; it won't try to stop the thread if the state is already |
| 175 | * FSG_STATE_TERMINATED. |
| 176 | * |
| 177 | * To provide maximum throughput, the driver uses a circular pipeline of |
| 178 | * buffer heads (struct fsg_buffhd). In principle the pipeline can be |
| 179 | * arbitrarily long; in practice the benefits don't justify having more |
| 180 | * than 2 stages (i.e., double buffering). But it helps to think of the |
| 181 | * pipeline as being a long one. Each buffer head contains a bulk-in and |
| 182 | * a bulk-out request pointer (since the buffer can be used for both |
| 183 | * output and input -- directions always are given from the host's |
| 184 | * point of view) as well as a pointer to the buffer and various state |
| 185 | * variables. |
| 186 | * |
| 187 | * Use of the pipeline follows a simple protocol. There is a variable |
| 188 | * (fsg->next_buffhd_to_fill) that points to the next buffer head to use. |
| 189 | * At any time that buffer head may still be in use from an earlier |
| 190 | * request, so each buffer head has a state variable indicating whether |
| 191 | * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the |
| 192 | * buffer head to be EMPTY, filling the buffer either by file I/O or by |
| 193 | * USB I/O (during which the buffer head is BUSY), and marking the buffer |
| 194 | * head FULL when the I/O is complete. Then the buffer will be emptied |
| 195 | * (again possibly by USB I/O, during which it is marked BUSY) and |
| 196 | * finally marked EMPTY again (possibly by a completion routine). |
| 197 | * |
| 198 | * A module parameter tells the driver to avoid stalling the bulk |
| 199 | * endpoints wherever the transport specification allows. This is |
| 200 | * necessary for some UDCs like the SuperH, which cannot reliably clear a |
| 201 | * halt on a bulk endpoint. However, under certain circumstances the |
| 202 | * Bulk-only specification requires a stall. In such cases the driver |
| 203 | * will halt the endpoint and set a flag indicating that it should clear |
| 204 | * the halt in software during the next device reset. Hopefully this |
| 205 | * will permit everything to work correctly. Furthermore, although the |
| 206 | * specification allows the bulk-out endpoint to halt when the host sends |
| 207 | * too much data, implementing this would cause an unavoidable race. |
| 208 | * The driver will always use the "no-stall" approach for OUT transfers. |
| 209 | * |
| 210 | * One subtle point concerns sending status-stage responses for ep0 |
| 211 | * requests. Some of these requests, such as device reset, can involve |
| 212 | * interrupting an ongoing file I/O operation, which might take an |
| 213 | * arbitrarily long time. During that delay the host might give up on |
| 214 | * the original ep0 request and issue a new one. When that happens the |
| 215 | * driver should not notify the host about completion of the original |
| 216 | * request, as the host will no longer be waiting for it. So the driver |
| 217 | * assigns to each ep0 request a unique tag, and it keeps track of the |
| 218 | * tag value of the request associated with a long-running exception |
| 219 | * (device-reset, interface-change, or configuration-change). When the |
| 220 | * exception handler is finished, the status-stage response is submitted |
| 221 | * only if the current ep0 request tag is equal to the exception request |
| 222 | * tag. Thus only the most recently received ep0 request will get a |
| 223 | * status-stage response. |
| 224 | * |
| 225 | * Warning: This driver source file is too long. It ought to be split up |
| 226 | * into a header file plus about 3 separate .c files, to handle the details |
| 227 | * of the Gadget, USB Mass Storage, and SCSI protocols. |
| 228 | */ |
| 229 | |
| 230 | |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 231 | /* #define VERBOSE_DEBUG */ |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 232 | /* #define DUMP_MSGS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | #include <linux/completion.h> |
| 237 | #include <linux/dcache.h> |
| 238 | #include <linux/delay.h> |
| 239 | #include <linux/device.h> |
| 240 | #include <linux/fcntl.h> |
| 241 | #include <linux/file.h> |
| 242 | #include <linux/fs.h> |
Alan Stern | 87c4252 | 2005-11-09 16:59:56 -0500 | [diff] [blame] | 243 | #include <linux/kref.h> |
Alan Stern | 22efcf4 | 2005-09-26 16:12:02 -0400 | [diff] [blame] | 244 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | #include <linux/limits.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | #include <linux/rwsem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | #include <linux/slab.h> |
| 248 | #include <linux/spinlock.h> |
| 249 | #include <linux/string.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 250 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | #include <linux/utsname.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
David Brownell | 5f84813 | 2006-12-16 15:34:53 -0800 | [diff] [blame] | 253 | #include <linux/usb/ch9.h> |
David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 254 | #include <linux/usb/gadget.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | #include "gadget_chips.h" |
| 257 | |
| 258 | |
David Brownell | 352e2b9 | 2008-08-18 17:43:25 -0700 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * Kbuild is not very cooperative with respect to linking separately |
| 262 | * compiled library objects into one module. So for now we won't use |
| 263 | * separate compilation ... ensuring init/exit sections work to shrink |
| 264 | * the runtime footprint, and giving us at least some parts of what |
| 265 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 266 | */ |
| 267 | #include "usbstring.c" |
| 268 | #include "config.c" |
| 269 | #include "epautoconf.c" |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | /*-------------------------------------------------------------------------*/ |
| 272 | |
| 273 | #define DRIVER_DESC "File-backed Storage Gadget" |
| 274 | #define DRIVER_NAME "g_file_storage" |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 275 | #define DRIVER_VERSION "1 September 2010" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 277 | static char fsg_string_manufacturer[64]; |
| 278 | static const char fsg_string_product[] = DRIVER_DESC; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 279 | static const char fsg_string_config[] = "Self-powered"; |
| 280 | static const char fsg_string_interface[] = "Mass Storage"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 282 | |
| 283 | #include "storage_common.c" |
| 284 | |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 287 | MODULE_AUTHOR("Alan Stern"); |
| 288 | MODULE_LICENSE("Dual BSD/GPL"); |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | /* |
| 291 | * This driver assumes self-powered hardware and has no way for users to |
| 292 | * trigger remote wakeup. It uses autoconfiguration to select endpoints |
| 293 | * and endpoint addresses. |
| 294 | */ |
| 295 | |
| 296 | |
| 297 | /*-------------------------------------------------------------------------*/ |
| 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /* Encapsulate the module parameter settings */ |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | static struct { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 303 | char *file[FSG_MAX_LUNS]; |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 304 | char *serial; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 305 | int ro[FSG_MAX_LUNS]; |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 306 | int nofua[FSG_MAX_LUNS]; |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 307 | unsigned int num_filenames; |
| 308 | unsigned int num_ros; |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 309 | unsigned int num_nofuas; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | unsigned int nluns; |
| 311 | |
Alan Stern | d794ac7 | 2005-04-18 12:43:25 -0400 | [diff] [blame] | 312 | int removable; |
| 313 | int can_stall; |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 314 | int cdrom; |
Alan Stern | d794ac7 | 2005-04-18 12:43:25 -0400 | [diff] [blame] | 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | char *transport_parm; |
| 317 | char *protocol_parm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | unsigned short vendor; |
| 319 | unsigned short product; |
| 320 | unsigned short release; |
| 321 | unsigned int buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | int transport_type; |
| 324 | char *transport_name; |
| 325 | int protocol_type; |
| 326 | char *protocol_name; |
| 327 | |
| 328 | } mod_data = { // Default values |
| 329 | .transport_parm = "BBB", |
| 330 | .protocol_parm = "SCSI", |
| 331 | .removable = 0, |
Alan Stern | d794ac7 | 2005-04-18 12:43:25 -0400 | [diff] [blame] | 332 | .can_stall = 1, |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 333 | .cdrom = 0, |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 334 | .vendor = FSG_VENDOR_ID, |
| 335 | .product = FSG_PRODUCT_ID, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | .release = 0xffff, // Use controller chip type |
| 337 | .buflen = 16384, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | |
Alan Stern | aafe5bd | 2006-03-31 11:46:43 -0500 | [diff] [blame] | 341 | module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames, |
| 342 | S_IRUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | MODULE_PARM_DESC(file, "names of backing files or devices"); |
| 344 | |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 345 | module_param_named(serial, mod_data.serial, charp, S_IRUGO); |
| 346 | MODULE_PARM_DESC(serial, "USB serial number"); |
| 347 | |
Alan Stern | aafe5bd | 2006-03-31 11:46:43 -0500 | [diff] [blame] | 348 | module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | MODULE_PARM_DESC(ro, "true to force read-only"); |
| 350 | |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 351 | module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas, |
| 352 | S_IRUGO); |
| 353 | MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit"); |
| 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | module_param_named(luns, mod_data.nluns, uint, S_IRUGO); |
| 356 | MODULE_PARM_DESC(luns, "number of LUNs"); |
| 357 | |
| 358 | module_param_named(removable, mod_data.removable, bool, S_IRUGO); |
| 359 | MODULE_PARM_DESC(removable, "true to simulate removable media"); |
| 360 | |
Alan Stern | d794ac7 | 2005-04-18 12:43:25 -0400 | [diff] [blame] | 361 | module_param_named(stall, mod_data.can_stall, bool, S_IRUGO); |
| 362 | MODULE_PARM_DESC(stall, "false to prevent bulk stalls"); |
| 363 | |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 364 | module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO); |
| 365 | MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk"); |
| 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | /* In the non-TEST version, only the module parameters listed above |
| 368 | * are available. */ |
| 369 | #ifdef CONFIG_USB_FILE_STORAGE_TEST |
| 370 | |
| 371 | module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO); |
| 372 | MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)"); |
| 373 | |
| 374 | module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO); |
| 375 | MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, " |
| 376 | "8070, or SCSI)"); |
| 377 | |
| 378 | module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO); |
| 379 | MODULE_PARM_DESC(vendor, "USB Vendor ID"); |
| 380 | |
| 381 | module_param_named(product, mod_data.product, ushort, S_IRUGO); |
| 382 | MODULE_PARM_DESC(product, "USB Product ID"); |
| 383 | |
| 384 | module_param_named(release, mod_data.release, ushort, S_IRUGO); |
| 385 | MODULE_PARM_DESC(release, "USB release number"); |
| 386 | |
| 387 | module_param_named(buflen, mod_data.buflen, uint, S_IRUGO); |
| 388 | MODULE_PARM_DESC(buflen, "I/O buffer size"); |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ |
| 391 | |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /* |
| 394 | * These definitions will permit the compiler to avoid generating code for |
| 395 | * parts of the driver that aren't used in the non-TEST version. Even gcc |
| 396 | * can recognize when a test of a constant expression yields a dead code |
| 397 | * path. |
| 398 | */ |
| 399 | |
| 400 | #ifdef CONFIG_USB_FILE_STORAGE_TEST |
| 401 | |
| 402 | #define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK) |
| 403 | #define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI) |
| 404 | #define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI) |
| 405 | |
| 406 | #else |
| 407 | |
| 408 | #define transport_is_bbb() 1 |
| 409 | #define transport_is_cbi() 0 |
| 410 | #define protocol_is_scsi() 1 |
| 411 | |
| 412 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ |
| 413 | |
| 414 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 415 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | struct fsg_dev { |
| 419 | /* lock protects: state, all the req_busy's, and cbbuf_cmnd */ |
| 420 | spinlock_t lock; |
| 421 | struct usb_gadget *gadget; |
| 422 | |
| 423 | /* filesem protects: backing files in use */ |
| 424 | struct rw_semaphore filesem; |
| 425 | |
Alan Stern | 87c4252 | 2005-11-09 16:59:56 -0500 | [diff] [blame] | 426 | /* reference counting: wait until all LUNs are released */ |
| 427 | struct kref ref; |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | struct usb_ep *ep0; // Handy copy of gadget->ep0 |
| 430 | struct usb_request *ep0req; // For control responses |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 431 | unsigned int ep0_req_tag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | const char *ep0req_name; |
| 433 | |
| 434 | struct usb_request *intreq; // For interrupt responses |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 435 | int intreq_busy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | struct fsg_buffhd *intr_buffhd; |
| 437 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 438 | unsigned int bulk_out_maxpacket; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | enum fsg_state state; // For exception handling |
| 440 | unsigned int exception_req_tag; |
| 441 | |
| 442 | u8 config, new_config; |
| 443 | |
| 444 | unsigned int running : 1; |
| 445 | unsigned int bulk_in_enabled : 1; |
| 446 | unsigned int bulk_out_enabled : 1; |
| 447 | unsigned int intr_in_enabled : 1; |
| 448 | unsigned int phase_error : 1; |
| 449 | unsigned int short_packet_received : 1; |
| 450 | unsigned int bad_lun_okay : 1; |
| 451 | |
| 452 | unsigned long atomic_bitflags; |
| 453 | #define REGISTERED 0 |
Alan Stern | b950bdb | 2008-04-14 11:45:29 -0400 | [diff] [blame] | 454 | #define IGNORE_BULK_OUT 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | #define SUSPENDED 2 |
| 456 | |
| 457 | struct usb_ep *bulk_in; |
| 458 | struct usb_ep *bulk_out; |
| 459 | struct usb_ep *intr_in; |
| 460 | |
| 461 | struct fsg_buffhd *next_buffhd_to_fill; |
| 462 | struct fsg_buffhd *next_buffhd_to_drain; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 463 | struct fsg_buffhd buffhds[FSG_NUM_BUFFERS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | int thread_wakeup_needed; |
| 466 | struct completion thread_notifier; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | struct task_struct *thread_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
| 469 | int cmnd_size; |
| 470 | u8 cmnd[MAX_COMMAND_SIZE]; |
| 471 | enum data_direction data_dir; |
| 472 | u32 data_size; |
| 473 | u32 data_size_from_cmnd; |
| 474 | u32 tag; |
| 475 | unsigned int lun; |
| 476 | u32 residue; |
| 477 | u32 usb_amount_left; |
| 478 | |
| 479 | /* The CB protocol offers no way for a host to know when a command |
| 480 | * has completed. As a result the next command may arrive early, |
| 481 | * and we will still have to handle it. For that reason we need |
| 482 | * a buffer to store new commands when using CB (or CBI, which |
| 483 | * does not oblige a host to wait for command completion either). */ |
| 484 | int cbbuf_cmnd_size; |
| 485 | u8 cbbuf_cmnd[MAX_COMMAND_SIZE]; |
| 486 | |
| 487 | unsigned int nluns; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 488 | struct fsg_lun *luns; |
| 489 | struct fsg_lun *curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | typedef void (*fsg_routine_t)(struct fsg_dev *); |
| 493 | |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 494 | static int exception_in_progress(struct fsg_dev *fsg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
| 496 | return (fsg->state > FSG_STATE_IDLE); |
| 497 | } |
| 498 | |
Greg Kroah-Hartman | 98346f7 | 2011-04-14 13:42:46 -0700 | [diff] [blame] | 499 | /* Make bulk-out requests be divisible by the maxpacket size */ |
| 500 | static void set_bulk_out_req_length(struct fsg_dev *fsg, |
| 501 | struct fsg_buffhd *bh, unsigned int length) |
| 502 | { |
| 503 | unsigned int rem; |
| 504 | |
| 505 | bh->bulk_out_intended_length = length; |
| 506 | rem = length % fsg->bulk_out_maxpacket; |
| 507 | if (rem > 0) |
| 508 | length += fsg->bulk_out_maxpacket - rem; |
| 509 | bh->outreq->length = length; |
| 510 | } |
| 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | static struct fsg_dev *the_fsg; |
| 513 | static struct usb_gadget_driver fsg_driver; |
| 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
| 516 | /*-------------------------------------------------------------------------*/ |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) |
| 519 | { |
| 520 | const char *name; |
| 521 | |
| 522 | if (ep == fsg->bulk_in) |
| 523 | name = "bulk-in"; |
| 524 | else if (ep == fsg->bulk_out) |
| 525 | name = "bulk-out"; |
| 526 | else |
| 527 | name = ep->name; |
| 528 | DBG(fsg, "%s set halt\n", name); |
| 529 | return usb_ep_set_halt(ep); |
| 530 | } |
| 531 | |
| 532 | |
| 533 | /*-------------------------------------------------------------------------*/ |
| 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | /* |
| 536 | * DESCRIPTORS ... most are static, but strings and (full) configuration |
| 537 | * descriptors are built on demand. Also the (static) config and interface |
| 538 | * descriptors are adjusted during fsg_bind(). |
| 539 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
| 541 | /* There is only one configuration. */ |
| 542 | #define CONFIG_VALUE 1 |
| 543 | |
| 544 | static struct usb_device_descriptor |
| 545 | device_desc = { |
| 546 | .bLength = sizeof device_desc, |
| 547 | .bDescriptorType = USB_DT_DEVICE, |
| 548 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 549 | .bcdUSB = cpu_to_le16(0x0200), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 551 | |
| 552 | /* The next three values can be overridden by module parameters */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 553 | .idVendor = cpu_to_le16(FSG_VENDOR_ID), |
| 554 | .idProduct = cpu_to_le16(FSG_PRODUCT_ID), |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 555 | .bcdDevice = cpu_to_le16(0xffff), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 557 | .iManufacturer = FSG_STRING_MANUFACTURER, |
| 558 | .iProduct = FSG_STRING_PRODUCT, |
| 559 | .iSerialNumber = FSG_STRING_SERIAL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | .bNumConfigurations = 1, |
| 561 | }; |
| 562 | |
| 563 | static struct usb_config_descriptor |
| 564 | config_desc = { |
| 565 | .bLength = sizeof config_desc, |
| 566 | .bDescriptorType = USB_DT_CONFIG, |
| 567 | |
| 568 | /* wTotalLength computed by usb_gadget_config_buf() */ |
| 569 | .bNumInterfaces = 1, |
| 570 | .bConfigurationValue = CONFIG_VALUE, |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 571 | .iConfiguration = FSG_STRING_CONFIG, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
David Brownell | 36e893d | 2008-09-12 09:39:06 -0700 | [diff] [blame] | 573 | .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | }; |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | static struct usb_qualifier_descriptor |
| 578 | dev_qualifier = { |
| 579 | .bLength = sizeof dev_qualifier, |
| 580 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, |
| 581 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 582 | .bcdUSB = cpu_to_le16(0x0200), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 584 | |
| 585 | .bNumConfigurations = 1, |
| 586 | }; |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | |
| 590 | /* |
| 591 | * Config descriptors must agree with the code that sets configurations |
| 592 | * and with code managing interfaces and their altsettings. They must |
| 593 | * also handle different speeds and other-speed requests. |
| 594 | */ |
| 595 | static int populate_config_buf(struct usb_gadget *gadget, |
| 596 | u8 *buf, u8 type, unsigned index) |
| 597 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | enum usb_device_speed speed = gadget->speed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | int len; |
| 600 | const struct usb_descriptor_header **function; |
| 601 | |
| 602 | if (index > 0) |
| 603 | return -EINVAL; |
| 604 | |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 605 | if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 607 | function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH |
| 608 | ? (const struct usb_descriptor_header **)fsg_hs_function |
| 609 | : (const struct usb_descriptor_header **)fsg_fs_function; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | /* for now, don't advertise srp-only devices */ |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 612 | if (!gadget_is_otg(gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | function++; |
| 614 | |
| 615 | len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function); |
| 616 | ((struct usb_config_descriptor *) buf)->bDescriptorType = type; |
| 617 | return len; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /*-------------------------------------------------------------------------*/ |
| 622 | |
| 623 | /* These routines may be called in process context or in_irq */ |
| 624 | |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 625 | /* Caller must hold fsg->lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | static void wakeup_thread(struct fsg_dev *fsg) |
| 627 | { |
| 628 | /* Tell the main thread that something has happened */ |
| 629 | fsg->thread_wakeup_needed = 1; |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 630 | if (fsg->thread_task) |
| 631 | wake_up_process(fsg->thread_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | |
| 635 | static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state) |
| 636 | { |
| 637 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | /* Do nothing if a higher-priority exception is already in progress. |
| 640 | * If a lower-or-equal priority exception is in progress, preempt it |
| 641 | * and notify the main thread by sending it a signal. */ |
| 642 | spin_lock_irqsave(&fsg->lock, flags); |
| 643 | if (fsg->state <= new_state) { |
| 644 | fsg->exception_req_tag = fsg->ep0_req_tag; |
| 645 | fsg->state = new_state; |
Alan Stern | 22efcf4 | 2005-09-26 16:12:02 -0400 | [diff] [blame] | 646 | if (fsg->thread_task) |
| 647 | send_sig_info(SIGUSR1, SEND_SIG_FORCED, |
| 648 | fsg->thread_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | spin_unlock_irqrestore(&fsg->lock, flags); |
| 651 | } |
| 652 | |
| 653 | |
| 654 | /*-------------------------------------------------------------------------*/ |
| 655 | |
| 656 | /* The disconnect callback and ep0 routines. These always run in_irq, |
| 657 | * except that ep0_queue() is called in the main thread to acknowledge |
| 658 | * completion of various requests: set config, set interface, and |
| 659 | * Bulk-only device reset. */ |
| 660 | |
| 661 | static void fsg_disconnect(struct usb_gadget *gadget) |
| 662 | { |
| 663 | struct fsg_dev *fsg = get_gadget_data(gadget); |
| 664 | |
| 665 | DBG(fsg, "disconnect or port reset\n"); |
| 666 | raise_exception(fsg, FSG_STATE_DISCONNECT); |
| 667 | } |
| 668 | |
| 669 | |
| 670 | static int ep0_queue(struct fsg_dev *fsg) |
| 671 | { |
| 672 | int rc; |
| 673 | |
| 674 | rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC); |
| 675 | if (rc != 0 && rc != -ESHUTDOWN) { |
| 676 | |
| 677 | /* We can't do much more than wait for a reset */ |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 678 | WARNING(fsg, "error in submission: %s --> %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | fsg->ep0->name, rc); |
| 680 | } |
| 681 | return rc; |
| 682 | } |
| 683 | |
| 684 | static void ep0_complete(struct usb_ep *ep, struct usb_request *req) |
| 685 | { |
John Daiker | 7ca46b8 | 2006-12-29 19:02:06 -0800 | [diff] [blame] | 686 | struct fsg_dev *fsg = ep->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | if (req->actual > 0) |
| 689 | dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual); |
| 690 | if (req->status || req->actual != req->length) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 691 | DBG(fsg, "%s --> %d, %u/%u\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | req->status, req->actual, req->length); |
| 693 | if (req->status == -ECONNRESET) // Request was cancelled |
| 694 | usb_ep_fifo_flush(ep); |
| 695 | |
| 696 | if (req->status == 0 && req->context) |
| 697 | ((fsg_routine_t) (req->context))(fsg); |
| 698 | } |
| 699 | |
| 700 | |
| 701 | /*-------------------------------------------------------------------------*/ |
| 702 | |
| 703 | /* Bulk and interrupt endpoint completion handlers. |
| 704 | * These always run in_irq. */ |
| 705 | |
| 706 | static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req) |
| 707 | { |
John Daiker | 7ca46b8 | 2006-12-29 19:02:06 -0800 | [diff] [blame] | 708 | struct fsg_dev *fsg = ep->driver_data; |
| 709 | struct fsg_buffhd *bh = req->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | if (req->status || req->actual != req->length) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 712 | DBG(fsg, "%s --> %d, %u/%u\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | req->status, req->actual, req->length); |
| 714 | if (req->status == -ECONNRESET) // Request was cancelled |
| 715 | usb_ep_fifo_flush(ep); |
| 716 | |
| 717 | /* Hold the lock while we update the request and buffer states */ |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 718 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | spin_lock(&fsg->lock); |
| 720 | bh->inreq_busy = 0; |
| 721 | bh->state = BUF_STATE_EMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | wakeup_thread(fsg); |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 723 | spin_unlock(&fsg->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req) |
| 727 | { |
John Daiker | 7ca46b8 | 2006-12-29 19:02:06 -0800 | [diff] [blame] | 728 | struct fsg_dev *fsg = ep->driver_data; |
| 729 | struct fsg_buffhd *bh = req->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
| 731 | dump_msg(fsg, "bulk-out", req->buf, req->actual); |
Greg Kroah-Hartman | 98346f7 | 2011-04-14 13:42:46 -0700 | [diff] [blame] | 732 | if (req->status || req->actual != bh->bulk_out_intended_length) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 733 | DBG(fsg, "%s --> %d, %u/%u\n", __func__, |
Greg Kroah-Hartman | 98346f7 | 2011-04-14 13:42:46 -0700 | [diff] [blame] | 734 | req->status, req->actual, |
| 735 | bh->bulk_out_intended_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | if (req->status == -ECONNRESET) // Request was cancelled |
| 737 | usb_ep_fifo_flush(ep); |
| 738 | |
| 739 | /* Hold the lock while we update the request and buffer states */ |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 740 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | spin_lock(&fsg->lock); |
| 742 | bh->outreq_busy = 0; |
| 743 | bh->state = BUF_STATE_FULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | wakeup_thread(fsg); |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 745 | spin_unlock(&fsg->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | |
| 749 | #ifdef CONFIG_USB_FILE_STORAGE_TEST |
| 750 | static void intr_in_complete(struct usb_ep *ep, struct usb_request *req) |
| 751 | { |
John Daiker | 7ca46b8 | 2006-12-29 19:02:06 -0800 | [diff] [blame] | 752 | struct fsg_dev *fsg = ep->driver_data; |
| 753 | struct fsg_buffhd *bh = req->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
| 755 | if (req->status || req->actual != req->length) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 756 | DBG(fsg, "%s --> %d, %u/%u\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | req->status, req->actual, req->length); |
| 758 | if (req->status == -ECONNRESET) // Request was cancelled |
| 759 | usb_ep_fifo_flush(ep); |
| 760 | |
| 761 | /* Hold the lock while we update the request and buffer states */ |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 762 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | spin_lock(&fsg->lock); |
| 764 | fsg->intreq_busy = 0; |
| 765 | bh->state = BUF_STATE_EMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | wakeup_thread(fsg); |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 767 | spin_unlock(&fsg->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | #else |
| 771 | static void intr_in_complete(struct usb_ep *ep, struct usb_request *req) |
| 772 | {} |
| 773 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ |
| 774 | |
| 775 | |
| 776 | /*-------------------------------------------------------------------------*/ |
| 777 | |
| 778 | /* Ep0 class-specific handlers. These always run in_irq. */ |
| 779 | |
| 780 | #ifdef CONFIG_USB_FILE_STORAGE_TEST |
| 781 | static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 782 | { |
| 783 | struct usb_request *req = fsg->ep0req; |
| 784 | static u8 cbi_reset_cmnd[6] = { |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 785 | SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
| 787 | /* Error in command transfer? */ |
| 788 | if (req->status || req->length != req->actual || |
| 789 | req->actual < 6 || req->actual > MAX_COMMAND_SIZE) { |
| 790 | |
| 791 | /* Not all controllers allow a protocol stall after |
| 792 | * receiving control-out data, but we'll try anyway. */ |
| 793 | fsg_set_halt(fsg, fsg->ep0); |
| 794 | return; // Wait for reset |
| 795 | } |
| 796 | |
| 797 | /* Is it the special reset command? */ |
| 798 | if (req->actual >= sizeof cbi_reset_cmnd && |
| 799 | memcmp(req->buf, cbi_reset_cmnd, |
| 800 | sizeof cbi_reset_cmnd) == 0) { |
| 801 | |
| 802 | /* Raise an exception to stop the current operation |
| 803 | * and reinitialize our state. */ |
| 804 | DBG(fsg, "cbi reset request\n"); |
| 805 | raise_exception(fsg, FSG_STATE_RESET); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | VDBG(fsg, "CB[I] accept device-specific command\n"); |
| 810 | spin_lock(&fsg->lock); |
| 811 | |
| 812 | /* Save the command for later */ |
| 813 | if (fsg->cbbuf_cmnd_size) |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 814 | WARNING(fsg, "CB[I] overwriting previous command\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | fsg->cbbuf_cmnd_size = req->actual; |
| 816 | memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size); |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | wakeup_thread(fsg); |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 819 | spin_unlock(&fsg->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | #else |
| 823 | static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 824 | {} |
| 825 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ |
| 826 | |
| 827 | |
| 828 | static int class_setup_req(struct fsg_dev *fsg, |
| 829 | const struct usb_ctrlrequest *ctrl) |
| 830 | { |
| 831 | struct usb_request *req = fsg->ep0req; |
| 832 | int value = -EOPNOTSUPP; |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 833 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
Luis Lloret | 88e45db | 2007-07-26 10:08:47 -0400 | [diff] [blame] | 834 | u16 w_value = le16_to_cpu(ctrl->wValue); |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 835 | u16 w_length = le16_to_cpu(ctrl->wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
| 837 | if (!fsg->config) |
| 838 | return value; |
| 839 | |
| 840 | /* Handle Bulk-only class-specific requests */ |
| 841 | if (transport_is_bbb()) { |
| 842 | switch (ctrl->bRequest) { |
| 843 | |
| 844 | case USB_BULK_RESET_REQUEST: |
| 845 | if (ctrl->bRequestType != (USB_DIR_OUT | |
| 846 | USB_TYPE_CLASS | USB_RECIP_INTERFACE)) |
| 847 | break; |
Luis Lloret | 88e45db | 2007-07-26 10:08:47 -0400 | [diff] [blame] | 848 | if (w_index != 0 || w_value != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | value = -EDOM; |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | /* Raise an exception to stop the current operation |
| 854 | * and reinitialize our state. */ |
| 855 | DBG(fsg, "bulk reset request\n"); |
| 856 | raise_exception(fsg, FSG_STATE_RESET); |
| 857 | value = DELAYED_STATUS; |
| 858 | break; |
| 859 | |
| 860 | case USB_BULK_GET_MAX_LUN_REQUEST: |
| 861 | if (ctrl->bRequestType != (USB_DIR_IN | |
| 862 | USB_TYPE_CLASS | USB_RECIP_INTERFACE)) |
| 863 | break; |
Luis Lloret | 88e45db | 2007-07-26 10:08:47 -0400 | [diff] [blame] | 864 | if (w_index != 0 || w_value != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | value = -EDOM; |
| 866 | break; |
| 867 | } |
| 868 | VDBG(fsg, "get max LUN\n"); |
| 869 | *(u8 *) req->buf = fsg->nluns - 1; |
Alan Stern | 76f4af8 | 2005-04-05 11:56:54 -0400 | [diff] [blame] | 870 | value = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /* Handle CBI class-specific requests */ |
| 876 | else { |
| 877 | switch (ctrl->bRequest) { |
| 878 | |
| 879 | case USB_CBI_ADSC_REQUEST: |
| 880 | if (ctrl->bRequestType != (USB_DIR_OUT | |
| 881 | USB_TYPE_CLASS | USB_RECIP_INTERFACE)) |
| 882 | break; |
Luis Lloret | 88e45db | 2007-07-26 10:08:47 -0400 | [diff] [blame] | 883 | if (w_index != 0 || w_value != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | value = -EDOM; |
| 885 | break; |
| 886 | } |
| 887 | if (w_length > MAX_COMMAND_SIZE) { |
| 888 | value = -EOVERFLOW; |
| 889 | break; |
| 890 | } |
| 891 | value = w_length; |
| 892 | fsg->ep0req->context = received_cbi_adsc; |
| 893 | break; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (value == -EOPNOTSUPP) |
| 898 | VDBG(fsg, |
| 899 | "unknown class-specific control req " |
| 900 | "%02x.%02x v%04x i%04x l%u\n", |
| 901 | ctrl->bRequestType, ctrl->bRequest, |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 902 | le16_to_cpu(ctrl->wValue), w_index, w_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | return value; |
| 904 | } |
| 905 | |
| 906 | |
| 907 | /*-------------------------------------------------------------------------*/ |
| 908 | |
| 909 | /* Ep0 standard request handlers. These always run in_irq. */ |
| 910 | |
| 911 | static int standard_setup_req(struct fsg_dev *fsg, |
| 912 | const struct usb_ctrlrequest *ctrl) |
| 913 | { |
| 914 | struct usb_request *req = fsg->ep0req; |
| 915 | int value = -EOPNOTSUPP; |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 916 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 917 | u16 w_value = le16_to_cpu(ctrl->wValue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
| 919 | /* Usually this just stores reply data in the pre-allocated ep0 buffer, |
| 920 | * but config change events will also reconfigure hardware. */ |
| 921 | switch (ctrl->bRequest) { |
| 922 | |
| 923 | case USB_REQ_GET_DESCRIPTOR: |
| 924 | if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD | |
| 925 | USB_RECIP_DEVICE)) |
| 926 | break; |
| 927 | switch (w_value >> 8) { |
| 928 | |
| 929 | case USB_DT_DEVICE: |
| 930 | VDBG(fsg, "get device descriptor\n"); |
Sebastian Andrzej Siewior | 765f5b8 | 2011-06-23 14:26:11 +0200 | [diff] [blame] | 931 | device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket; |
Alan Stern | 76f4af8 | 2005-04-05 11:56:54 -0400 | [diff] [blame] | 932 | value = sizeof device_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | memcpy(req->buf, &device_desc, value); |
| 934 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | case USB_DT_DEVICE_QUALIFIER: |
| 936 | VDBG(fsg, "get device qualifier\n"); |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 937 | if (!gadget_is_dualspeed(fsg->gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | break; |
Sebastian Andrzej Siewior | 765f5b8 | 2011-06-23 14:26:11 +0200 | [diff] [blame] | 939 | /* |
| 940 | * Assume ep0 uses the same maxpacket value for both |
| 941 | * speeds |
| 942 | */ |
| 943 | dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket; |
Alan Stern | 76f4af8 | 2005-04-05 11:56:54 -0400 | [diff] [blame] | 944 | value = sizeof dev_qualifier; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | memcpy(req->buf, &dev_qualifier, value); |
| 946 | break; |
| 947 | |
| 948 | case USB_DT_OTHER_SPEED_CONFIG: |
| 949 | VDBG(fsg, "get other-speed config descriptor\n"); |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 950 | if (!gadget_is_dualspeed(fsg->gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | break; |
| 952 | goto get_config; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | case USB_DT_CONFIG: |
| 954 | VDBG(fsg, "get configuration descriptor\n"); |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 955 | get_config: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | value = populate_config_buf(fsg->gadget, |
| 957 | req->buf, |
| 958 | w_value >> 8, |
| 959 | w_value & 0xff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | break; |
| 961 | |
| 962 | case USB_DT_STRING: |
| 963 | VDBG(fsg, "get string descriptor\n"); |
| 964 | |
| 965 | /* wIndex == language code */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 966 | value = usb_gadget_get_string(&fsg_stringtab, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | w_value & 0xff, req->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | break; |
| 969 | } |
| 970 | break; |
| 971 | |
| 972 | /* One config, two speeds */ |
| 973 | case USB_REQ_SET_CONFIGURATION: |
| 974 | if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD | |
| 975 | USB_RECIP_DEVICE)) |
| 976 | break; |
| 977 | VDBG(fsg, "set configuration\n"); |
| 978 | if (w_value == CONFIG_VALUE || w_value == 0) { |
| 979 | fsg->new_config = w_value; |
| 980 | |
| 981 | /* Raise an exception to wipe out previous transaction |
| 982 | * state (queued bufs, etc) and set the new config. */ |
| 983 | raise_exception(fsg, FSG_STATE_CONFIG_CHANGE); |
| 984 | value = DELAYED_STATUS; |
| 985 | } |
| 986 | break; |
| 987 | case USB_REQ_GET_CONFIGURATION: |
| 988 | if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD | |
| 989 | USB_RECIP_DEVICE)) |
| 990 | break; |
| 991 | VDBG(fsg, "get configuration\n"); |
| 992 | *(u8 *) req->buf = fsg->config; |
Alan Stern | 76f4af8 | 2005-04-05 11:56:54 -0400 | [diff] [blame] | 993 | value = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | break; |
| 995 | |
| 996 | case USB_REQ_SET_INTERFACE: |
| 997 | if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD | |
| 998 | USB_RECIP_INTERFACE)) |
| 999 | break; |
| 1000 | if (fsg->config && w_index == 0) { |
| 1001 | |
| 1002 | /* Raise an exception to wipe out previous transaction |
| 1003 | * state (queued bufs, etc) and install the new |
| 1004 | * interface altsetting. */ |
| 1005 | raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE); |
| 1006 | value = DELAYED_STATUS; |
| 1007 | } |
| 1008 | break; |
| 1009 | case USB_REQ_GET_INTERFACE: |
| 1010 | if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD | |
| 1011 | USB_RECIP_INTERFACE)) |
| 1012 | break; |
| 1013 | if (!fsg->config) |
| 1014 | break; |
| 1015 | if (w_index != 0) { |
| 1016 | value = -EDOM; |
| 1017 | break; |
| 1018 | } |
| 1019 | VDBG(fsg, "get interface\n"); |
| 1020 | *(u8 *) req->buf = 0; |
Alan Stern | 76f4af8 | 2005-04-05 11:56:54 -0400 | [diff] [blame] | 1021 | value = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | break; |
| 1023 | |
| 1024 | default: |
| 1025 | VDBG(fsg, |
| 1026 | "unknown control req %02x.%02x v%04x i%04x l%u\n", |
| 1027 | ctrl->bRequestType, ctrl->bRequest, |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 1028 | w_value, w_index, le16_to_cpu(ctrl->wLength)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | return value; |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | static int fsg_setup(struct usb_gadget *gadget, |
| 1036 | const struct usb_ctrlrequest *ctrl) |
| 1037 | { |
| 1038 | struct fsg_dev *fsg = get_gadget_data(gadget); |
| 1039 | int rc; |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 1040 | int w_length = le16_to_cpu(ctrl->wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | ++fsg->ep0_req_tag; // Record arrival of a new request |
| 1043 | fsg->ep0req->context = NULL; |
| 1044 | fsg->ep0req->length = 0; |
| 1045 | dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl)); |
| 1046 | |
| 1047 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) |
| 1048 | rc = class_setup_req(fsg, ctrl); |
| 1049 | else |
| 1050 | rc = standard_setup_req(fsg, ctrl); |
| 1051 | |
| 1052 | /* Respond with data/status or defer until later? */ |
| 1053 | if (rc >= 0 && rc != DELAYED_STATUS) { |
Alan Stern | 76f4af8 | 2005-04-05 11:56:54 -0400 | [diff] [blame] | 1054 | rc = min(rc, w_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | fsg->ep0req->length = rc; |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 1056 | fsg->ep0req->zero = rc < w_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ? |
| 1058 | "ep0-in" : "ep0-out"); |
| 1059 | rc = ep0_queue(fsg); |
| 1060 | } |
| 1061 | |
| 1062 | /* Device either stalls (rc < 0) or reports success */ |
| 1063 | return rc; |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | /*-------------------------------------------------------------------------*/ |
| 1068 | |
| 1069 | /* All the following routines run in process context */ |
| 1070 | |
| 1071 | |
| 1072 | /* Use this for bulk or interrupt transfers, not ep0 */ |
| 1073 | static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep, |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 1074 | struct usb_request *req, int *pbusy, |
| 1075 | enum fsg_buffer_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | { |
| 1077 | int rc; |
| 1078 | |
| 1079 | if (ep == fsg->bulk_in) |
| 1080 | dump_msg(fsg, "bulk-in", req->buf, req->length); |
| 1081 | else if (ep == fsg->intr_in) |
| 1082 | dump_msg(fsg, "intr-in", req->buf, req->length); |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 1083 | |
| 1084 | spin_lock_irq(&fsg->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | *pbusy = 1; |
| 1086 | *state = BUF_STATE_BUSY; |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 1087 | spin_unlock_irq(&fsg->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | rc = usb_ep_queue(ep, req, GFP_KERNEL); |
| 1089 | if (rc != 0) { |
| 1090 | *pbusy = 0; |
| 1091 | *state = BUF_STATE_EMPTY; |
| 1092 | |
| 1093 | /* We can't do much more than wait for a reset */ |
| 1094 | |
| 1095 | /* Note: currently the net2280 driver fails zero-length |
| 1096 | * submissions if DMA is enabled. */ |
| 1097 | if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP && |
| 1098 | req->length == 0)) |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 1099 | WARNING(fsg, "error in submission: %s --> %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | ep->name, rc); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | |
| 1105 | static int sleep_thread(struct fsg_dev *fsg) |
| 1106 | { |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 1107 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
| 1109 | /* Wait until a signal arrives or we are woken up */ |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 1110 | for (;;) { |
| 1111 | try_to_freeze(); |
| 1112 | set_current_state(TASK_INTERRUPTIBLE); |
| 1113 | if (signal_pending(current)) { |
| 1114 | rc = -EINTR; |
| 1115 | break; |
| 1116 | } |
| 1117 | if (fsg->thread_wakeup_needed) |
| 1118 | break; |
| 1119 | schedule(); |
| 1120 | } |
| 1121 | __set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | fsg->thread_wakeup_needed = 0; |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 1123 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | |
| 1127 | /*-------------------------------------------------------------------------*/ |
| 1128 | |
| 1129 | static int do_read(struct fsg_dev *fsg) |
| 1130 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1131 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | u32 lba; |
| 1133 | struct fsg_buffhd *bh; |
| 1134 | int rc; |
| 1135 | u32 amount_left; |
| 1136 | loff_t file_offset, file_offset_tmp; |
| 1137 | unsigned int amount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | ssize_t nread; |
| 1139 | |
| 1140 | /* Get the starting Logical Block Address and check that it's |
| 1141 | * not too big */ |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 1142 | if (fsg->cmnd[0] == READ_6) |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1143 | lba = get_unaligned_be24(&fsg->cmnd[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | else { |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1145 | lba = get_unaligned_be32(&fsg->cmnd[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
| 1147 | /* We allow DPO (Disable Page Out = don't save data in the |
| 1148 | * cache) and FUA (Force Unit Access = don't read from the |
| 1149 | * cache), but we don't implement them. */ |
| 1150 | if ((fsg->cmnd[1] & ~0x18) != 0) { |
| 1151 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1152 | return -EINVAL; |
| 1153 | } |
| 1154 | } |
| 1155 | if (lba >= curlun->num_sectors) { |
| 1156 | curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; |
| 1157 | return -EINVAL; |
| 1158 | } |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1159 | file_offset = ((loff_t) lba) << curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
| 1161 | /* Carry out the file reads */ |
| 1162 | amount_left = fsg->data_size_from_cmnd; |
| 1163 | if (unlikely(amount_left == 0)) |
| 1164 | return -EIO; // No default reply |
| 1165 | |
| 1166 | for (;;) { |
| 1167 | |
| 1168 | /* Figure out how much we need to read: |
| 1169 | * Try to read the remaining amount. |
| 1170 | * But don't read more than the buffer size. |
| 1171 | * And don't try to read past the end of the file. |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1172 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | amount = min((unsigned int) amount_left, mod_data.buflen); |
| 1174 | amount = min((loff_t) amount, |
| 1175 | curlun->file_length - file_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
| 1177 | /* Wait for the next buffer to become available */ |
| 1178 | bh = fsg->next_buffhd_to_fill; |
| 1179 | while (bh->state != BUF_STATE_EMPTY) { |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 1180 | rc = sleep_thread(fsg); |
| 1181 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | return rc; |
| 1183 | } |
| 1184 | |
| 1185 | /* If we were asked to read past the end of file, |
| 1186 | * end with an empty buffer. */ |
| 1187 | if (amount == 0) { |
| 1188 | curlun->sense_data = |
| 1189 | SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1190 | curlun->sense_data_info = file_offset >> curlun->blkbits; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1191 | curlun->info_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | bh->inreq->length = 0; |
| 1193 | bh->state = BUF_STATE_FULL; |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | /* Perform the read */ |
| 1198 | file_offset_tmp = file_offset; |
| 1199 | nread = vfs_read(curlun->filp, |
| 1200 | (char __user *) bh->buf, |
| 1201 | amount, &file_offset_tmp); |
| 1202 | VLDBG(curlun, "file read %u @ %llu -> %d\n", amount, |
| 1203 | (unsigned long long) file_offset, |
| 1204 | (int) nread); |
| 1205 | if (signal_pending(current)) |
| 1206 | return -EINTR; |
| 1207 | |
| 1208 | if (nread < 0) { |
| 1209 | LDBG(curlun, "error in file read: %d\n", |
| 1210 | (int) nread); |
| 1211 | nread = 0; |
| 1212 | } else if (nread < amount) { |
| 1213 | LDBG(curlun, "partial file read: %d/%u\n", |
| 1214 | (int) nread, amount); |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1215 | nread = round_down(nread, curlun->blksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | } |
| 1217 | file_offset += nread; |
| 1218 | amount_left -= nread; |
| 1219 | fsg->residue -= nread; |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1220 | |
| 1221 | /* Except at the end of the transfer, nread will be |
| 1222 | * equal to the buffer size, which is divisible by the |
| 1223 | * bulk-in maxpacket size. |
| 1224 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | bh->inreq->length = nread; |
| 1226 | bh->state = BUF_STATE_FULL; |
| 1227 | |
| 1228 | /* If an error occurred, report it and its position */ |
| 1229 | if (nread < amount) { |
| 1230 | curlun->sense_data = SS_UNRECOVERED_READ_ERROR; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1231 | curlun->sense_data_info = file_offset >> curlun->blkbits; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1232 | curlun->info_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | break; |
| 1234 | } |
| 1235 | |
| 1236 | if (amount_left == 0) |
| 1237 | break; // No more left to read |
| 1238 | |
| 1239 | /* Send this buffer and go read some more */ |
| 1240 | bh->inreq->zero = 0; |
| 1241 | start_transfer(fsg, fsg->bulk_in, bh->inreq, |
| 1242 | &bh->inreq_busy, &bh->state); |
| 1243 | fsg->next_buffhd_to_fill = bh->next; |
| 1244 | } |
| 1245 | |
| 1246 | return -EIO; // No default reply |
| 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | /*-------------------------------------------------------------------------*/ |
| 1251 | |
| 1252 | static int do_write(struct fsg_dev *fsg) |
| 1253 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1254 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | u32 lba; |
| 1256 | struct fsg_buffhd *bh; |
| 1257 | int get_some_more; |
| 1258 | u32 amount_left_to_req, amount_left_to_write; |
| 1259 | loff_t usb_offset, file_offset, file_offset_tmp; |
| 1260 | unsigned int amount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | ssize_t nwritten; |
| 1262 | int rc; |
| 1263 | |
| 1264 | if (curlun->ro) { |
| 1265 | curlun->sense_data = SS_WRITE_PROTECTED; |
| 1266 | return -EINVAL; |
| 1267 | } |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 1268 | spin_lock(&curlun->filp->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 1270 | spin_unlock(&curlun->filp->f_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
| 1272 | /* Get the starting Logical Block Address and check that it's |
| 1273 | * not too big */ |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 1274 | if (fsg->cmnd[0] == WRITE_6) |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1275 | lba = get_unaligned_be24(&fsg->cmnd[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | else { |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1277 | lba = get_unaligned_be32(&fsg->cmnd[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
| 1279 | /* We allow DPO (Disable Page Out = don't save data in the |
| 1280 | * cache) and FUA (Force Unit Access = write directly to the |
| 1281 | * medium). We don't implement DPO; we implement FUA by |
| 1282 | * performing synchronous output. */ |
| 1283 | if ((fsg->cmnd[1] & ~0x18) != 0) { |
| 1284 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1285 | return -EINVAL; |
| 1286 | } |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 1287 | /* FUA */ |
| 1288 | if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) { |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 1289 | spin_lock(&curlun->filp->f_lock); |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 1290 | curlun->filp->f_flags |= O_DSYNC; |
Jonathan Corbet | db1dd4d | 2009-02-06 15:25:24 -0700 | [diff] [blame] | 1291 | spin_unlock(&curlun->filp->f_lock); |
| 1292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | } |
| 1294 | if (lba >= curlun->num_sectors) { |
| 1295 | curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; |
| 1296 | return -EINVAL; |
| 1297 | } |
| 1298 | |
| 1299 | /* Carry out the file writes */ |
| 1300 | get_some_more = 1; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1301 | file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd; |
| 1303 | |
| 1304 | while (amount_left_to_write > 0) { |
| 1305 | |
| 1306 | /* Queue a request for more data from the host */ |
| 1307 | bh = fsg->next_buffhd_to_fill; |
| 1308 | if (bh->state == BUF_STATE_EMPTY && get_some_more) { |
| 1309 | |
| 1310 | /* Figure out how much we want to get: |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1311 | * Try to get the remaining amount, |
| 1312 | * but not more than the buffer size. |
| 1313 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | amount = min(amount_left_to_req, mod_data.buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1316 | /* Beyond the end of the backing file? */ |
| 1317 | if (usb_offset >= curlun->file_length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | get_some_more = 0; |
| 1319 | curlun->sense_data = |
| 1320 | SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1321 | curlun->sense_data_info = usb_offset >> curlun->blkbits; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1322 | curlun->info_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | continue; |
| 1324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
| 1326 | /* Get the next buffer */ |
| 1327 | usb_offset += amount; |
| 1328 | fsg->usb_amount_left -= amount; |
| 1329 | amount_left_to_req -= amount; |
| 1330 | if (amount_left_to_req == 0) |
| 1331 | get_some_more = 0; |
| 1332 | |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1333 | /* Except at the end of the transfer, amount will be |
| 1334 | * equal to the buffer size, which is divisible by |
| 1335 | * the bulk-out maxpacket size. |
| 1336 | */ |
Greg Kroah-Hartman | 98346f7 | 2011-04-14 13:42:46 -0700 | [diff] [blame] | 1337 | bh->outreq->length = bh->bulk_out_intended_length = |
| 1338 | amount; |
Alan Stern | 70ffe6e | 2006-03-23 15:05:16 -0500 | [diff] [blame] | 1339 | bh->outreq->short_not_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | 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 Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 1351 | smp_rmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | fsg->next_buffhd_to_drain = bh->next; |
| 1353 | bh->state = BUF_STATE_EMPTY; |
| 1354 | |
| 1355 | /* Did something go wrong with the transfer? */ |
| 1356 | if (bh->outreq->status != 0) { |
| 1357 | curlun->sense_data = SS_COMMUNICATION_FAILURE; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1358 | curlun->sense_data_info = file_offset >> curlun->blkbits; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1359 | curlun->info_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | break; |
| 1361 | } |
| 1362 | |
| 1363 | amount = bh->outreq->actual; |
| 1364 | if (curlun->file_length - file_offset < amount) { |
| 1365 | LERROR(curlun, |
| 1366 | "write %u @ %llu beyond end %llu\n", |
| 1367 | amount, (unsigned long long) file_offset, |
| 1368 | (unsigned long long) curlun->file_length); |
| 1369 | amount = curlun->file_length - file_offset; |
| 1370 | } |
| 1371 | |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1372 | /* Don't write a partial block */ |
| 1373 | amount = round_down(amount, curlun->blksize); |
| 1374 | if (amount == 0) |
| 1375 | goto empty_write; |
| 1376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | /* Perform the write */ |
| 1378 | file_offset_tmp = file_offset; |
| 1379 | nwritten = vfs_write(curlun->filp, |
| 1380 | (char __user *) bh->buf, |
| 1381 | amount, &file_offset_tmp); |
| 1382 | VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, |
| 1383 | (unsigned long long) file_offset, |
| 1384 | (int) nwritten); |
| 1385 | if (signal_pending(current)) |
| 1386 | return -EINTR; // Interrupted! |
| 1387 | |
| 1388 | if (nwritten < 0) { |
| 1389 | LDBG(curlun, "error in file write: %d\n", |
| 1390 | (int) nwritten); |
| 1391 | nwritten = 0; |
| 1392 | } else if (nwritten < amount) { |
| 1393 | LDBG(curlun, "partial file write: %d/%u\n", |
| 1394 | (int) nwritten, amount); |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1395 | nwritten = round_down(nwritten, curlun->blksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | } |
| 1397 | file_offset += nwritten; |
| 1398 | amount_left_to_write -= nwritten; |
| 1399 | fsg->residue -= nwritten; |
| 1400 | |
| 1401 | /* If an error occurred, report it and its position */ |
| 1402 | if (nwritten < amount) { |
| 1403 | curlun->sense_data = SS_WRITE_ERROR; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1404 | curlun->sense_data_info = file_offset >> curlun->blkbits; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1405 | curlun->info_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | break; |
| 1407 | } |
| 1408 | |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1409 | empty_write: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | /* Did the host decide to stop early? */ |
| 1411 | if (bh->outreq->actual != bh->outreq->length) { |
| 1412 | fsg->short_packet_received = 1; |
| 1413 | break; |
| 1414 | } |
| 1415 | continue; |
| 1416 | } |
| 1417 | |
| 1418 | /* Wait for something to happen */ |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 1419 | rc = sleep_thread(fsg); |
| 1420 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | return rc; |
| 1422 | } |
| 1423 | |
| 1424 | return -EIO; // No default reply |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | /*-------------------------------------------------------------------------*/ |
| 1429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | static int do_synchronize_cache(struct fsg_dev *fsg) |
| 1431 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1432 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | int rc; |
| 1434 | |
| 1435 | /* We ignore the requested LBA and write out all file's |
| 1436 | * dirty data buffers. */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1437 | rc = fsg_lun_fsync_sub(curlun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | if (rc) |
| 1439 | curlun->sense_data = SS_WRITE_ERROR; |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
| 1443 | |
| 1444 | /*-------------------------------------------------------------------------*/ |
| 1445 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1446 | static void invalidate_sub(struct fsg_lun *curlun) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | { |
| 1448 | struct file *filp = curlun->filp; |
Josef Sipek | 33cb899 | 2006-12-08 02:37:46 -0800 | [diff] [blame] | 1449 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | unsigned long rc; |
| 1451 | |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 1452 | rc = invalidate_mapping_pages(inode->i_mapping, 0, -1); |
Christoph Hellwig | 2ecdc82 | 2010-01-26 17:27:20 +0100 | [diff] [blame] | 1453 | VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | static int do_verify(struct fsg_dev *fsg) |
| 1457 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1458 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | u32 lba; |
| 1460 | u32 verification_length; |
| 1461 | struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; |
| 1462 | loff_t file_offset, file_offset_tmp; |
| 1463 | u32 amount_left; |
| 1464 | unsigned int amount; |
| 1465 | ssize_t nread; |
| 1466 | |
| 1467 | /* Get the starting Logical Block Address and check that it's |
| 1468 | * not too big */ |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1469 | lba = get_unaligned_be32(&fsg->cmnd[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | if (lba >= curlun->num_sectors) { |
| 1471 | curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; |
| 1472 | return -EINVAL; |
| 1473 | } |
| 1474 | |
| 1475 | /* We allow DPO (Disable Page Out = don't save data in the |
| 1476 | * cache) but we don't implement it. */ |
| 1477 | if ((fsg->cmnd[1] & ~0x10) != 0) { |
| 1478 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1479 | return -EINVAL; |
| 1480 | } |
| 1481 | |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1482 | verification_length = get_unaligned_be16(&fsg->cmnd[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | if (unlikely(verification_length == 0)) |
| 1484 | return -EIO; // No default reply |
| 1485 | |
| 1486 | /* Prepare to carry out the file verify */ |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1487 | amount_left = verification_length << curlun->blkbits; |
| 1488 | file_offset = ((loff_t) lba) << curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | |
| 1490 | /* Write out all the dirty buffers before invalidating them */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1491 | fsg_lun_fsync_sub(curlun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | if (signal_pending(current)) |
| 1493 | return -EINTR; |
| 1494 | |
| 1495 | invalidate_sub(curlun); |
| 1496 | if (signal_pending(current)) |
| 1497 | return -EINTR; |
| 1498 | |
| 1499 | /* Just try to read the requested blocks */ |
| 1500 | while (amount_left > 0) { |
| 1501 | |
| 1502 | /* Figure out how much we need to read: |
| 1503 | * Try to read the remaining amount, but not more than |
| 1504 | * the buffer size. |
| 1505 | * And don't try to read past the end of the file. |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1506 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | amount = min((unsigned int) amount_left, mod_data.buflen); |
| 1508 | amount = min((loff_t) amount, |
| 1509 | curlun->file_length - file_offset); |
| 1510 | if (amount == 0) { |
| 1511 | curlun->sense_data = |
| 1512 | SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1513 | curlun->sense_data_info = file_offset >> curlun->blkbits; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1514 | curlun->info_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | /* Perform the read */ |
| 1519 | file_offset_tmp = file_offset; |
| 1520 | nread = vfs_read(curlun->filp, |
| 1521 | (char __user *) bh->buf, |
| 1522 | amount, &file_offset_tmp); |
| 1523 | VLDBG(curlun, "file read %u @ %llu -> %d\n", amount, |
| 1524 | (unsigned long long) file_offset, |
| 1525 | (int) nread); |
| 1526 | if (signal_pending(current)) |
| 1527 | return -EINTR; |
| 1528 | |
| 1529 | if (nread < 0) { |
| 1530 | LDBG(curlun, "error in file verify: %d\n", |
| 1531 | (int) nread); |
| 1532 | nread = 0; |
| 1533 | } else if (nread < amount) { |
| 1534 | LDBG(curlun, "partial file verify: %d/%u\n", |
| 1535 | (int) nread, amount); |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1536 | nread = round_down(nread, curlun->blksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | } |
| 1538 | if (nread == 0) { |
| 1539 | curlun->sense_data = SS_UNRECOVERED_READ_ERROR; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1540 | curlun->sense_data_info = file_offset >> curlun->blkbits; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1541 | curlun->info_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | break; |
| 1543 | } |
| 1544 | file_offset += nread; |
| 1545 | amount_left -= nread; |
| 1546 | } |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | |
| 1551 | /*-------------------------------------------------------------------------*/ |
| 1552 | |
| 1553 | static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 1554 | { |
| 1555 | u8 *buf = (u8 *) bh->buf; |
| 1556 | |
| 1557 | static char vendor_id[] = "Linux "; |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 1558 | static char product_disk_id[] = "File-Stor Gadget"; |
| 1559 | static char product_cdrom_id[] = "File-CD Gadget "; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
| 1561 | if (!fsg->curlun) { // Unsupported LUNs are okay |
| 1562 | fsg->bad_lun_okay = 1; |
| 1563 | memset(buf, 0, 36); |
| 1564 | buf[0] = 0x7f; // Unsupported, no device-type |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 1565 | buf[4] = 31; // Additional length |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | return 36; |
| 1567 | } |
| 1568 | |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 1569 | memset(buf, 0, 8); |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 1570 | buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | if (mod_data.removable) |
| 1572 | buf[1] = 0x80; |
| 1573 | buf[2] = 2; // ANSI SCSI level 2 |
| 1574 | buf[3] = 2; // SCSI-2 INQUIRY data format |
| 1575 | buf[4] = 31; // Additional length |
| 1576 | // No special options |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 1577 | sprintf(buf + 8, "%-8s%-16s%04x", vendor_id, |
| 1578 | (mod_data.cdrom ? product_cdrom_id : |
| 1579 | product_disk_id), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | mod_data.release); |
| 1581 | return 36; |
| 1582 | } |
| 1583 | |
| 1584 | |
| 1585 | static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 1586 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1587 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | u8 *buf = (u8 *) bh->buf; |
| 1589 | u32 sd, sdinfo; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1590 | int valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
| 1592 | /* |
| 1593 | * From the SCSI-2 spec., section 7.9 (Unit attention condition): |
| 1594 | * |
| 1595 | * If a REQUEST SENSE command is received from an initiator |
| 1596 | * with a pending unit attention condition (before the target |
| 1597 | * generates the contingent allegiance condition), then the |
| 1598 | * target shall either: |
| 1599 | * a) report any pending sense data and preserve the unit |
| 1600 | * attention condition on the logical unit, or, |
| 1601 | * b) report the unit attention condition, may discard any |
| 1602 | * pending sense data, and clear the unit attention |
| 1603 | * condition on the logical unit for that initiator. |
| 1604 | * |
| 1605 | * FSG normally uses option a); enable this code to use option b). |
| 1606 | */ |
| 1607 | #if 0 |
| 1608 | if (curlun && curlun->unit_attention_data != SS_NO_SENSE) { |
| 1609 | curlun->sense_data = curlun->unit_attention_data; |
| 1610 | curlun->unit_attention_data = SS_NO_SENSE; |
| 1611 | } |
| 1612 | #endif |
| 1613 | |
| 1614 | if (!curlun) { // Unsupported LUNs are okay |
| 1615 | fsg->bad_lun_okay = 1; |
| 1616 | sd = SS_LOGICAL_UNIT_NOT_SUPPORTED; |
| 1617 | sdinfo = 0; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1618 | valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | } else { |
| 1620 | sd = curlun->sense_data; |
| 1621 | sdinfo = curlun->sense_data_info; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1622 | valid = curlun->info_valid << 7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | curlun->sense_data = SS_NO_SENSE; |
| 1624 | curlun->sense_data_info = 0; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1625 | curlun->info_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | memset(buf, 0, 18); |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 1629 | buf[0] = valid | 0x70; // Valid, current error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | buf[2] = SK(sd); |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1631 | put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | buf[7] = 18 - 8; // Additional sense length |
| 1633 | buf[12] = ASC(sd); |
| 1634 | buf[13] = ASCQ(sd); |
| 1635 | return 18; |
| 1636 | } |
| 1637 | |
| 1638 | |
| 1639 | static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 1640 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1641 | struct fsg_lun *curlun = fsg->curlun; |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1642 | u32 lba = get_unaligned_be32(&fsg->cmnd[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | int pmi = fsg->cmnd[8]; |
| 1644 | u8 *buf = (u8 *) bh->buf; |
| 1645 | |
| 1646 | /* Check the PMI and LBA fields */ |
| 1647 | if (pmi > 1 || (pmi == 0 && lba != 0)) { |
| 1648 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1649 | return -EINVAL; |
| 1650 | } |
| 1651 | |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1652 | put_unaligned_be32(curlun->num_sectors - 1, &buf[0]); |
| 1653 | /* Max logical block */ |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1654 | put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | return 8; |
| 1656 | } |
| 1657 | |
| 1658 | |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 1659 | static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 1660 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1661 | struct fsg_lun *curlun = fsg->curlun; |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 1662 | int msf = fsg->cmnd[1] & 0x02; |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1663 | u32 lba = get_unaligned_be32(&fsg->cmnd[2]); |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 1664 | u8 *buf = (u8 *) bh->buf; |
| 1665 | |
| 1666 | if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */ |
| 1667 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1668 | return -EINVAL; |
| 1669 | } |
| 1670 | if (lba >= curlun->num_sectors) { |
| 1671 | curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; |
| 1672 | return -EINVAL; |
| 1673 | } |
| 1674 | |
| 1675 | memset(buf, 0, 8); |
| 1676 | buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */ |
| 1677 | store_cdrom_address(&buf[4], msf, lba); |
| 1678 | return 8; |
| 1679 | } |
| 1680 | |
| 1681 | |
| 1682 | static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 1683 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1684 | struct fsg_lun *curlun = fsg->curlun; |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 1685 | int msf = fsg->cmnd[1] & 0x02; |
| 1686 | int start_track = fsg->cmnd[6]; |
| 1687 | u8 *buf = (u8 *) bh->buf; |
| 1688 | |
| 1689 | if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ |
| 1690 | start_track > 1) { |
| 1691 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1692 | return -EINVAL; |
| 1693 | } |
| 1694 | |
| 1695 | memset(buf, 0, 20); |
| 1696 | buf[1] = (20-2); /* TOC data length */ |
| 1697 | buf[2] = 1; /* First track number */ |
| 1698 | buf[3] = 1; /* Last track number */ |
| 1699 | buf[5] = 0x16; /* Data track, copying allowed */ |
| 1700 | buf[6] = 0x01; /* Only track is number 1 */ |
| 1701 | store_cdrom_address(&buf[8], msf, 0); |
| 1702 | |
| 1703 | buf[13] = 0x16; /* Lead-out track is data */ |
| 1704 | buf[14] = 0xAA; /* Lead-out track number */ |
| 1705 | store_cdrom_address(&buf[16], msf, curlun->num_sectors); |
| 1706 | return 20; |
| 1707 | } |
| 1708 | |
| 1709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 1711 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1712 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | int mscmnd = fsg->cmnd[0]; |
| 1714 | u8 *buf = (u8 *) bh->buf; |
| 1715 | u8 *buf0 = buf; |
| 1716 | int pc, page_code; |
| 1717 | int changeable_values, all_pages; |
| 1718 | int valid_page = 0; |
| 1719 | int len, limit; |
| 1720 | |
| 1721 | if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD |
| 1722 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1723 | return -EINVAL; |
| 1724 | } |
| 1725 | pc = fsg->cmnd[2] >> 6; |
| 1726 | page_code = fsg->cmnd[2] & 0x3f; |
| 1727 | if (pc == 3) { |
| 1728 | curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED; |
| 1729 | return -EINVAL; |
| 1730 | } |
| 1731 | changeable_values = (pc == 1); |
| 1732 | all_pages = (page_code == 0x3f); |
| 1733 | |
| 1734 | /* Write the mode parameter header. Fixed values are: default |
| 1735 | * medium type, no cache control (DPOFUA), and no block descriptors. |
| 1736 | * The only variable value is the WriteProtect bit. We will fill in |
| 1737 | * the mode data length later. */ |
| 1738 | memset(buf, 0, 8); |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 1739 | if (mscmnd == MODE_SENSE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA |
| 1741 | buf += 4; |
| 1742 | limit = 255; |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 1743 | } else { // MODE_SENSE_10 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA |
| 1745 | buf += 8; |
| 1746 | limit = 65535; // Should really be mod_data.buflen |
| 1747 | } |
| 1748 | |
| 1749 | /* No block descriptors */ |
| 1750 | |
| 1751 | /* The mode pages, in numerical order. The only page we support |
| 1752 | * is the Caching page. */ |
| 1753 | if (page_code == 0x08 || all_pages) { |
| 1754 | valid_page = 1; |
| 1755 | buf[0] = 0x08; // Page code |
| 1756 | buf[1] = 10; // Page length |
| 1757 | memset(buf+2, 0, 10); // None of the fields are changeable |
| 1758 | |
| 1759 | if (!changeable_values) { |
| 1760 | buf[2] = 0x04; // Write cache enable, |
| 1761 | // Read cache not disabled |
| 1762 | // No cache retention priorities |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1763 | put_unaligned_be16(0xffff, &buf[4]); |
| 1764 | /* Don't disable prefetch */ |
| 1765 | /* Minimum prefetch = 0 */ |
| 1766 | put_unaligned_be16(0xffff, &buf[8]); |
| 1767 | /* Maximum prefetch */ |
| 1768 | put_unaligned_be16(0xffff, &buf[10]); |
| 1769 | /* Maximum prefetch ceiling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | } |
| 1771 | buf += 12; |
| 1772 | } |
| 1773 | |
| 1774 | /* Check that a valid page was requested and the mode data length |
| 1775 | * isn't too long. */ |
| 1776 | len = buf - buf0; |
| 1777 | if (!valid_page || len > limit) { |
| 1778 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1779 | return -EINVAL; |
| 1780 | } |
| 1781 | |
| 1782 | /* Store the mode data length */ |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 1783 | if (mscmnd == MODE_SENSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | buf0[0] = len - 1; |
| 1785 | else |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1786 | put_unaligned_be16(len - 2, buf0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | return len; |
| 1788 | } |
| 1789 | |
| 1790 | |
| 1791 | static int do_start_stop(struct fsg_dev *fsg) |
| 1792 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1793 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | int loej, start; |
| 1795 | |
| 1796 | if (!mod_data.removable) { |
| 1797 | curlun->sense_data = SS_INVALID_COMMAND; |
| 1798 | return -EINVAL; |
| 1799 | } |
| 1800 | |
| 1801 | // int immed = fsg->cmnd[1] & 0x01; |
| 1802 | loej = fsg->cmnd[4] & 0x02; |
| 1803 | start = fsg->cmnd[4] & 0x01; |
| 1804 | |
| 1805 | #ifdef CONFIG_USB_FILE_STORAGE_TEST |
| 1806 | if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed |
| 1807 | (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start |
| 1808 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1809 | return -EINVAL; |
| 1810 | } |
| 1811 | |
| 1812 | if (!start) { |
| 1813 | |
| 1814 | /* Are we allowed to unload the media? */ |
| 1815 | if (curlun->prevent_medium_removal) { |
| 1816 | LDBG(curlun, "unload attempt prevented\n"); |
| 1817 | curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED; |
| 1818 | return -EINVAL; |
| 1819 | } |
| 1820 | if (loej) { // Simulate an unload/eject |
| 1821 | up_read(&fsg->filesem); |
| 1822 | down_write(&fsg->filesem); |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1823 | fsg_lun_close(curlun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | up_write(&fsg->filesem); |
| 1825 | down_read(&fsg->filesem); |
| 1826 | } |
| 1827 | } else { |
| 1828 | |
| 1829 | /* Our emulation doesn't support mounting; the medium is |
| 1830 | * available for use as soon as it is loaded. */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1831 | if (!fsg_lun_is_open(curlun)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | curlun->sense_data = SS_MEDIUM_NOT_PRESENT; |
| 1833 | return -EINVAL; |
| 1834 | } |
| 1835 | } |
| 1836 | #endif |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
| 1840 | |
| 1841 | static int do_prevent_allow(struct fsg_dev *fsg) |
| 1842 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1843 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | int prevent; |
| 1845 | |
| 1846 | if (!mod_data.removable) { |
| 1847 | curlun->sense_data = SS_INVALID_COMMAND; |
| 1848 | return -EINVAL; |
| 1849 | } |
| 1850 | |
| 1851 | prevent = fsg->cmnd[4] & 0x01; |
| 1852 | if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent |
| 1853 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 1854 | return -EINVAL; |
| 1855 | } |
| 1856 | |
| 1857 | if (curlun->prevent_medium_removal && !prevent) |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1858 | fsg_lun_fsync_sub(curlun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | curlun->prevent_medium_removal = prevent; |
| 1860 | return 0; |
| 1861 | } |
| 1862 | |
| 1863 | |
| 1864 | static int do_read_format_capacities(struct fsg_dev *fsg, |
| 1865 | struct fsg_buffhd *bh) |
| 1866 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1867 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | u8 *buf = (u8 *) bh->buf; |
| 1869 | |
| 1870 | buf[0] = buf[1] = buf[2] = 0; |
| 1871 | buf[3] = 8; // Only the Current/Maximum Capacity Descriptor |
| 1872 | buf += 4; |
| 1873 | |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1874 | put_unaligned_be32(curlun->num_sectors, &buf[0]); |
| 1875 | /* Number of blocks */ |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 1876 | put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */ |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 1877 | buf[4] = 0x02; /* Current capacity */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | return 12; |
| 1879 | } |
| 1880 | |
| 1881 | |
| 1882 | static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 1883 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 1884 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | |
| 1886 | /* We don't support MODE SELECT */ |
| 1887 | curlun->sense_data = SS_INVALID_COMMAND; |
| 1888 | return -EINVAL; |
| 1889 | } |
| 1890 | |
| 1891 | |
| 1892 | /*-------------------------------------------------------------------------*/ |
| 1893 | |
| 1894 | static int halt_bulk_in_endpoint(struct fsg_dev *fsg) |
| 1895 | { |
| 1896 | int rc; |
| 1897 | |
| 1898 | rc = fsg_set_halt(fsg, fsg->bulk_in); |
| 1899 | if (rc == -EAGAIN) |
| 1900 | VDBG(fsg, "delayed bulk-in endpoint halt\n"); |
| 1901 | while (rc != 0) { |
| 1902 | if (rc != -EAGAIN) { |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 1903 | WARNING(fsg, "usb_ep_set_halt -> %d\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | rc = 0; |
| 1905 | break; |
| 1906 | } |
| 1907 | |
| 1908 | /* Wait for a short time and then try again */ |
| 1909 | if (msleep_interruptible(100) != 0) |
| 1910 | return -EINTR; |
| 1911 | rc = usb_ep_set_halt(fsg->bulk_in); |
| 1912 | } |
| 1913 | return rc; |
| 1914 | } |
| 1915 | |
David Lopo | 62fd2ca | 2008-04-29 10:14:38 +0100 | [diff] [blame] | 1916 | static int wedge_bulk_in_endpoint(struct fsg_dev *fsg) |
| 1917 | { |
| 1918 | int rc; |
| 1919 | |
| 1920 | DBG(fsg, "bulk-in set wedge\n"); |
| 1921 | rc = usb_ep_set_wedge(fsg->bulk_in); |
| 1922 | if (rc == -EAGAIN) |
| 1923 | VDBG(fsg, "delayed bulk-in endpoint wedge\n"); |
| 1924 | while (rc != 0) { |
| 1925 | if (rc != -EAGAIN) { |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 1926 | WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc); |
David Lopo | 62fd2ca | 2008-04-29 10:14:38 +0100 | [diff] [blame] | 1927 | rc = 0; |
| 1928 | break; |
| 1929 | } |
| 1930 | |
| 1931 | /* Wait for a short time and then try again */ |
| 1932 | if (msleep_interruptible(100) != 0) |
| 1933 | return -EINTR; |
| 1934 | rc = usb_ep_set_wedge(fsg->bulk_in); |
| 1935 | } |
| 1936 | return rc; |
| 1937 | } |
| 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | static int throw_away_data(struct fsg_dev *fsg) |
| 1940 | { |
| 1941 | struct fsg_buffhd *bh; |
| 1942 | u32 amount; |
| 1943 | int rc; |
| 1944 | |
| 1945 | while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY || |
| 1946 | fsg->usb_amount_left > 0) { |
| 1947 | |
| 1948 | /* Throw away the data in a filled buffer */ |
| 1949 | if (bh->state == BUF_STATE_FULL) { |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 1950 | smp_rmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | bh->state = BUF_STATE_EMPTY; |
| 1952 | fsg->next_buffhd_to_drain = bh->next; |
| 1953 | |
| 1954 | /* A short packet or an error ends everything */ |
| 1955 | if (bh->outreq->actual != bh->outreq->length || |
| 1956 | bh->outreq->status != 0) { |
| 1957 | raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); |
| 1958 | return -EINTR; |
| 1959 | } |
| 1960 | continue; |
| 1961 | } |
| 1962 | |
| 1963 | /* Try to submit another request if we need one */ |
| 1964 | bh = fsg->next_buffhd_to_fill; |
| 1965 | if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) { |
| 1966 | amount = min(fsg->usb_amount_left, |
| 1967 | (u32) mod_data.buflen); |
| 1968 | |
Alan Stern | 04eee25 | 2011-08-18 14:29:00 -0400 | [diff] [blame^] | 1969 | /* Except at the end of the transfer, amount will be |
| 1970 | * equal to the buffer size, which is divisible by |
| 1971 | * the bulk-out maxpacket size. |
| 1972 | */ |
Greg Kroah-Hartman | 98346f7 | 2011-04-14 13:42:46 -0700 | [diff] [blame] | 1973 | bh->outreq->length = bh->bulk_out_intended_length = |
| 1974 | amount; |
Alan Stern | 70ffe6e | 2006-03-23 15:05:16 -0500 | [diff] [blame] | 1975 | bh->outreq->short_not_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | start_transfer(fsg, fsg->bulk_out, bh->outreq, |
| 1977 | &bh->outreq_busy, &bh->state); |
| 1978 | fsg->next_buffhd_to_fill = bh->next; |
| 1979 | fsg->usb_amount_left -= amount; |
| 1980 | continue; |
| 1981 | } |
| 1982 | |
| 1983 | /* Otherwise wait for something to happen */ |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 1984 | rc = sleep_thread(fsg); |
| 1985 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | return rc; |
| 1987 | } |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
| 1991 | |
| 1992 | static int finish_reply(struct fsg_dev *fsg) |
| 1993 | { |
| 1994 | struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; |
| 1995 | int rc = 0; |
| 1996 | |
| 1997 | switch (fsg->data_dir) { |
| 1998 | case DATA_DIR_NONE: |
| 1999 | break; // Nothing to send |
| 2000 | |
| 2001 | /* If we don't know whether the host wants to read or write, |
| 2002 | * this must be CB or CBI with an unknown command. We mustn't |
| 2003 | * try to send or receive any data. So stall both bulk pipes |
| 2004 | * if we can and wait for a reset. */ |
| 2005 | case DATA_DIR_UNKNOWN: |
| 2006 | if (mod_data.can_stall) { |
| 2007 | fsg_set_halt(fsg, fsg->bulk_out); |
| 2008 | rc = halt_bulk_in_endpoint(fsg); |
| 2009 | } |
| 2010 | break; |
| 2011 | |
| 2012 | /* All but the last buffer of data must have already been sent */ |
| 2013 | case DATA_DIR_TO_HOST: |
| 2014 | if (fsg->data_size == 0) |
| 2015 | ; // Nothing to send |
| 2016 | |
| 2017 | /* If there's no residue, simply send the last buffer */ |
| 2018 | else if (fsg->residue == 0) { |
| 2019 | bh->inreq->zero = 0; |
| 2020 | start_transfer(fsg, fsg->bulk_in, bh->inreq, |
| 2021 | &bh->inreq_busy, &bh->state); |
| 2022 | fsg->next_buffhd_to_fill = bh->next; |
| 2023 | } |
| 2024 | |
| 2025 | /* There is a residue. For CB and CBI, simply mark the end |
| 2026 | * of the data with a short packet. However, if we are |
| 2027 | * allowed to stall, there was no data at all (residue == |
| 2028 | * data_size), and the command failed (invalid LUN or |
| 2029 | * sense data is set), then halt the bulk-in endpoint |
| 2030 | * instead. */ |
| 2031 | else if (!transport_is_bbb()) { |
| 2032 | if (mod_data.can_stall && |
| 2033 | fsg->residue == fsg->data_size && |
| 2034 | (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) { |
| 2035 | bh->state = BUF_STATE_EMPTY; |
| 2036 | rc = halt_bulk_in_endpoint(fsg); |
| 2037 | } else { |
| 2038 | bh->inreq->zero = 1; |
| 2039 | start_transfer(fsg, fsg->bulk_in, bh->inreq, |
| 2040 | &bh->inreq_busy, &bh->state); |
| 2041 | fsg->next_buffhd_to_fill = bh->next; |
| 2042 | } |
| 2043 | } |
| 2044 | |
Alan Stern | ee81b3e | 2011-03-25 11:46:27 -0400 | [diff] [blame] | 2045 | /* |
| 2046 | * For Bulk-only, mark the end of the data with a short |
| 2047 | * packet. If we are allowed to stall, halt the bulk-in |
| 2048 | * endpoint. (Note: This violates the Bulk-Only Transport |
| 2049 | * specification, which requires us to pad the data if we |
| 2050 | * don't halt the endpoint. Presumably nobody will mind.) |
| 2051 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | else { |
Alan Stern | ee81b3e | 2011-03-25 11:46:27 -0400 | [diff] [blame] | 2053 | bh->inreq->zero = 1; |
| 2054 | start_transfer(fsg, fsg->bulk_in, bh->inreq, |
| 2055 | &bh->inreq_busy, &bh->state); |
| 2056 | fsg->next_buffhd_to_fill = bh->next; |
| 2057 | if (mod_data.can_stall) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | rc = halt_bulk_in_endpoint(fsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | } |
| 2060 | break; |
| 2061 | |
| 2062 | /* We have processed all we want from the data the host has sent. |
| 2063 | * There may still be outstanding bulk-out requests. */ |
| 2064 | case DATA_DIR_FROM_HOST: |
| 2065 | if (fsg->residue == 0) |
| 2066 | ; // Nothing to receive |
| 2067 | |
| 2068 | /* Did the host stop sending unexpectedly early? */ |
| 2069 | else if (fsg->short_packet_received) { |
| 2070 | raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); |
| 2071 | rc = -EINTR; |
| 2072 | } |
| 2073 | |
| 2074 | /* We haven't processed all the incoming data. Even though |
| 2075 | * we may be allowed to stall, doing so would cause a race. |
| 2076 | * The controller may already have ACK'ed all the remaining |
| 2077 | * bulk-out packets, in which case the host wouldn't see a |
| 2078 | * STALL. Not realizing the endpoint was halted, it wouldn't |
| 2079 | * clear the halt -- leading to problems later on. */ |
| 2080 | #if 0 |
| 2081 | else if (mod_data.can_stall) { |
| 2082 | fsg_set_halt(fsg, fsg->bulk_out); |
| 2083 | raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT); |
| 2084 | rc = -EINTR; |
| 2085 | } |
| 2086 | #endif |
| 2087 | |
| 2088 | /* We can't stall. Read in the excess data and throw it |
| 2089 | * all away. */ |
| 2090 | else |
| 2091 | rc = throw_away_data(fsg); |
| 2092 | break; |
| 2093 | } |
| 2094 | return rc; |
| 2095 | } |
| 2096 | |
| 2097 | |
| 2098 | static int send_status(struct fsg_dev *fsg) |
| 2099 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2100 | struct fsg_lun *curlun = fsg->curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | struct fsg_buffhd *bh; |
| 2102 | int rc; |
| 2103 | u8 status = USB_STATUS_PASS; |
| 2104 | u32 sd, sdinfo = 0; |
| 2105 | |
| 2106 | /* Wait for the next buffer to become available */ |
| 2107 | bh = fsg->next_buffhd_to_fill; |
| 2108 | while (bh->state != BUF_STATE_EMPTY) { |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2109 | rc = sleep_thread(fsg); |
| 2110 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | return rc; |
| 2112 | } |
| 2113 | |
| 2114 | if (curlun) { |
| 2115 | sd = curlun->sense_data; |
| 2116 | sdinfo = curlun->sense_data_info; |
| 2117 | } else if (fsg->bad_lun_okay) |
| 2118 | sd = SS_NO_SENSE; |
| 2119 | else |
| 2120 | sd = SS_LOGICAL_UNIT_NOT_SUPPORTED; |
| 2121 | |
| 2122 | if (fsg->phase_error) { |
| 2123 | DBG(fsg, "sending phase-error status\n"); |
| 2124 | status = USB_STATUS_PHASE_ERROR; |
| 2125 | sd = SS_INVALID_COMMAND; |
| 2126 | } else if (sd != SS_NO_SENSE) { |
| 2127 | DBG(fsg, "sending command-failure status\n"); |
| 2128 | status = USB_STATUS_FAIL; |
| 2129 | VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" |
| 2130 | " info x%x\n", |
| 2131 | SK(sd), ASC(sd), ASCQ(sd), sdinfo); |
| 2132 | } |
| 2133 | |
| 2134 | if (transport_is_bbb()) { |
John Daiker | 7ca46b8 | 2006-12-29 19:02:06 -0800 | [diff] [blame] | 2135 | struct bulk_cs_wrap *csw = bh->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | |
| 2137 | /* Store and send the Bulk-only CSW */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 2138 | csw->Signature = cpu_to_le32(USB_BULK_CS_SIG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | csw->Tag = fsg->tag; |
| 2140 | csw->Residue = cpu_to_le32(fsg->residue); |
| 2141 | csw->Status = status; |
| 2142 | |
| 2143 | bh->inreq->length = USB_BULK_CS_WRAP_LEN; |
| 2144 | bh->inreq->zero = 0; |
| 2145 | start_transfer(fsg, fsg->bulk_in, bh->inreq, |
| 2146 | &bh->inreq_busy, &bh->state); |
| 2147 | |
| 2148 | } else if (mod_data.transport_type == USB_PR_CB) { |
| 2149 | |
| 2150 | /* Control-Bulk transport has no status phase! */ |
| 2151 | return 0; |
| 2152 | |
| 2153 | } else { // USB_PR_CBI |
John Daiker | 7ca46b8 | 2006-12-29 19:02:06 -0800 | [diff] [blame] | 2154 | struct interrupt_data *buf = bh->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
| 2156 | /* Store and send the Interrupt data. UFI sends the ASC |
| 2157 | * and ASCQ bytes. Everything else sends a Type (which |
| 2158 | * is always 0) and the status Value. */ |
| 2159 | if (mod_data.protocol_type == USB_SC_UFI) { |
| 2160 | buf->bType = ASC(sd); |
| 2161 | buf->bValue = ASCQ(sd); |
| 2162 | } else { |
| 2163 | buf->bType = 0; |
| 2164 | buf->bValue = status; |
| 2165 | } |
| 2166 | fsg->intreq->length = CBI_INTERRUPT_DATA_LEN; |
| 2167 | |
| 2168 | fsg->intr_buffhd = bh; // Point to the right buffhd |
| 2169 | fsg->intreq->buf = bh->inreq->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | fsg->intreq->context = bh; |
| 2171 | start_transfer(fsg, fsg->intr_in, fsg->intreq, |
| 2172 | &fsg->intreq_busy, &bh->state); |
| 2173 | } |
| 2174 | |
| 2175 | fsg->next_buffhd_to_fill = bh->next; |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
| 2179 | |
| 2180 | /*-------------------------------------------------------------------------*/ |
| 2181 | |
| 2182 | /* Check whether the command is properly formed and whether its data size |
| 2183 | * and direction agree with the values we already have. */ |
| 2184 | static int check_command(struct fsg_dev *fsg, int cmnd_size, |
| 2185 | enum data_direction data_dir, unsigned int mask, |
| 2186 | int needs_medium, const char *name) |
| 2187 | { |
| 2188 | int i; |
| 2189 | int lun = fsg->cmnd[1] >> 5; |
| 2190 | static const char dirletter[4] = {'u', 'o', 'i', 'n'}; |
| 2191 | char hdlen[20]; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2192 | struct fsg_lun *curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | |
| 2194 | /* Adjust the expected cmnd_size for protocol encapsulation padding. |
| 2195 | * Transparent SCSI doesn't pad. */ |
| 2196 | if (protocol_is_scsi()) |
| 2197 | ; |
| 2198 | |
| 2199 | /* There's some disagreement as to whether RBC pads commands or not. |
| 2200 | * We'll play it safe and accept either form. */ |
| 2201 | else if (mod_data.protocol_type == USB_SC_RBC) { |
| 2202 | if (fsg->cmnd_size == 12) |
| 2203 | cmnd_size = 12; |
| 2204 | |
| 2205 | /* All the other protocols pad to 12 bytes */ |
| 2206 | } else |
| 2207 | cmnd_size = 12; |
| 2208 | |
| 2209 | hdlen[0] = 0; |
| 2210 | if (fsg->data_dir != DATA_DIR_UNKNOWN) |
| 2211 | sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir], |
| 2212 | fsg->data_size); |
| 2213 | VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", |
| 2214 | name, cmnd_size, dirletter[(int) data_dir], |
| 2215 | fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen); |
| 2216 | |
| 2217 | /* We can't reply at all until we know the correct data direction |
| 2218 | * and size. */ |
| 2219 | if (fsg->data_size_from_cmnd == 0) |
| 2220 | data_dir = DATA_DIR_NONE; |
| 2221 | if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI |
| 2222 | fsg->data_dir = data_dir; |
| 2223 | fsg->data_size = fsg->data_size_from_cmnd; |
| 2224 | |
| 2225 | } else { // Bulk-only |
| 2226 | if (fsg->data_size < fsg->data_size_from_cmnd) { |
| 2227 | |
| 2228 | /* Host data size < Device data size is a phase error. |
| 2229 | * Carry out the command, but only transfer as much |
| 2230 | * as we are allowed. */ |
| 2231 | fsg->data_size_from_cmnd = fsg->data_size; |
| 2232 | fsg->phase_error = 1; |
| 2233 | } |
| 2234 | } |
| 2235 | fsg->residue = fsg->usb_amount_left = fsg->data_size; |
| 2236 | |
| 2237 | /* Conflicting data directions is a phase error */ |
| 2238 | if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) { |
| 2239 | fsg->phase_error = 1; |
| 2240 | return -EINVAL; |
| 2241 | } |
| 2242 | |
| 2243 | /* Verify the length of the command itself */ |
| 2244 | if (cmnd_size != fsg->cmnd_size) { |
| 2245 | |
Felipe Balbi | 549c41e | 2008-09-19 02:00:02 +0300 | [diff] [blame] | 2246 | /* Special case workaround: There are plenty of buggy SCSI |
| 2247 | * implementations. Many have issues with cbw->Length |
| 2248 | * field passing a wrong command size. For those cases we |
| 2249 | * always try to work around the problem by using the length |
| 2250 | * sent by the host side provided it is at least as large |
| 2251 | * as the correct command length. |
| 2252 | * Examples of such cases would be MS-Windows, which issues |
| 2253 | * REQUEST SENSE with cbw->Length == 12 where it should |
| 2254 | * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and |
| 2255 | * REQUEST SENSE with cbw->Length == 10 where it should |
| 2256 | * be 6 as well. |
| 2257 | */ |
| 2258 | if (cmnd_size <= fsg->cmnd_size) { |
| 2259 | DBG(fsg, "%s is buggy! Expected length %d " |
| 2260 | "but we got %d\n", name, |
| 2261 | cmnd_size, fsg->cmnd_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | cmnd_size = fsg->cmnd_size; |
Felipe Balbi | 549c41e | 2008-09-19 02:00:02 +0300 | [diff] [blame] | 2263 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | fsg->phase_error = 1; |
| 2265 | return -EINVAL; |
| 2266 | } |
| 2267 | } |
| 2268 | |
Alan Stern | d794ac7 | 2005-04-18 12:43:25 -0400 | [diff] [blame] | 2269 | /* Check that the LUN values are consistent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | if (transport_is_bbb()) { |
| 2271 | if (fsg->lun != lun) |
| 2272 | DBG(fsg, "using LUN %d from CBW, " |
| 2273 | "not LUN %d from CDB\n", |
| 2274 | fsg->lun, lun); |
| 2275 | } else |
| 2276 | fsg->lun = lun; // Use LUN from the command |
| 2277 | |
| 2278 | /* Check the LUN */ |
Maxin B John | 849426c | 2011-05-08 15:56:17 +0300 | [diff] [blame] | 2279 | if (fsg->lun < fsg->nluns) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | fsg->curlun = curlun = &fsg->luns[fsg->lun]; |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2281 | if (fsg->cmnd[0] != REQUEST_SENSE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | curlun->sense_data = SS_NO_SENSE; |
| 2283 | curlun->sense_data_info = 0; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 2284 | curlun->info_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | } |
| 2286 | } else { |
| 2287 | fsg->curlun = curlun = NULL; |
| 2288 | fsg->bad_lun_okay = 0; |
| 2289 | |
| 2290 | /* INQUIRY and REQUEST SENSE commands are explicitly allowed |
| 2291 | * to use unsupported LUNs; all others may not. */ |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2292 | if (fsg->cmnd[0] != INQUIRY && |
| 2293 | fsg->cmnd[0] != REQUEST_SENSE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | DBG(fsg, "unsupported LUN %d\n", fsg->lun); |
| 2295 | return -EINVAL; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | /* If a unit attention condition exists, only INQUIRY and |
| 2300 | * REQUEST SENSE commands are allowed; anything else must fail. */ |
| 2301 | if (curlun && curlun->unit_attention_data != SS_NO_SENSE && |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2302 | fsg->cmnd[0] != INQUIRY && |
| 2303 | fsg->cmnd[0] != REQUEST_SENSE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | curlun->sense_data = curlun->unit_attention_data; |
| 2305 | curlun->unit_attention_data = SS_NO_SENSE; |
| 2306 | return -EINVAL; |
| 2307 | } |
| 2308 | |
| 2309 | /* Check that only command bytes listed in the mask are non-zero */ |
| 2310 | fsg->cmnd[1] &= 0x1f; // Mask away the LUN |
| 2311 | for (i = 1; i < cmnd_size; ++i) { |
| 2312 | if (fsg->cmnd[i] && !(mask & (1 << i))) { |
| 2313 | if (curlun) |
| 2314 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; |
| 2315 | return -EINVAL; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | /* If the medium isn't mounted and the command needs to access |
| 2320 | * it, return an error. */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2321 | if (curlun && !fsg_lun_is_open(curlun) && needs_medium) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | curlun->sense_data = SS_MEDIUM_NOT_PRESENT; |
| 2323 | return -EINVAL; |
| 2324 | } |
| 2325 | |
| 2326 | return 0; |
| 2327 | } |
| 2328 | |
| 2329 | |
| 2330 | static int do_scsi_command(struct fsg_dev *fsg) |
| 2331 | { |
| 2332 | struct fsg_buffhd *bh; |
| 2333 | int rc; |
| 2334 | int reply = -EINVAL; |
| 2335 | int i; |
| 2336 | static char unknown[16]; |
| 2337 | |
| 2338 | dump_cdb(fsg); |
| 2339 | |
| 2340 | /* Wait for the next buffer to become available for data or status */ |
| 2341 | bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill; |
| 2342 | while (bh->state != BUF_STATE_EMPTY) { |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2343 | rc = sleep_thread(fsg); |
| 2344 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | return rc; |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | fsg->phase_error = 0; |
| 2348 | fsg->short_packet_received = 0; |
| 2349 | |
| 2350 | down_read(&fsg->filesem); // We're using the backing file |
| 2351 | switch (fsg->cmnd[0]) { |
| 2352 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2353 | case INQUIRY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | fsg->data_size_from_cmnd = fsg->cmnd[4]; |
| 2355 | if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, |
| 2356 | (1<<4), 0, |
| 2357 | "INQUIRY")) == 0) |
| 2358 | reply = do_inquiry(fsg, bh); |
| 2359 | break; |
| 2360 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2361 | case MODE_SELECT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | fsg->data_size_from_cmnd = fsg->cmnd[4]; |
| 2363 | if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, |
| 2364 | (1<<1) | (1<<4), 0, |
| 2365 | "MODE SELECT(6)")) == 0) |
| 2366 | reply = do_mode_select(fsg, bh); |
| 2367 | break; |
| 2368 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2369 | case MODE_SELECT_10: |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2370 | fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, |
| 2372 | (1<<1) | (3<<7), 0, |
| 2373 | "MODE SELECT(10)")) == 0) |
| 2374 | reply = do_mode_select(fsg, bh); |
| 2375 | break; |
| 2376 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2377 | case MODE_SENSE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | fsg->data_size_from_cmnd = fsg->cmnd[4]; |
| 2379 | if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, |
| 2380 | (1<<1) | (1<<2) | (1<<4), 0, |
| 2381 | "MODE SENSE(6)")) == 0) |
| 2382 | reply = do_mode_sense(fsg, bh); |
| 2383 | break; |
| 2384 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2385 | case MODE_SENSE_10: |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2386 | fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, |
| 2388 | (1<<1) | (1<<2) | (3<<7), 0, |
| 2389 | "MODE SENSE(10)")) == 0) |
| 2390 | reply = do_mode_sense(fsg, bh); |
| 2391 | break; |
| 2392 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2393 | case ALLOW_MEDIUM_REMOVAL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | fsg->data_size_from_cmnd = 0; |
| 2395 | if ((reply = check_command(fsg, 6, DATA_DIR_NONE, |
| 2396 | (1<<4), 0, |
| 2397 | "PREVENT-ALLOW MEDIUM REMOVAL")) == 0) |
| 2398 | reply = do_prevent_allow(fsg); |
| 2399 | break; |
| 2400 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2401 | case READ_6: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | i = fsg->cmnd[4]; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 2403 | fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, |
| 2405 | (7<<1) | (1<<4), 1, |
| 2406 | "READ(6)")) == 0) |
| 2407 | reply = do_read(fsg); |
| 2408 | break; |
| 2409 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2410 | case READ_10: |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2411 | fsg->data_size_from_cmnd = |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 2412 | get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, |
| 2414 | (1<<1) | (0xf<<2) | (3<<7), 1, |
| 2415 | "READ(10)")) == 0) |
| 2416 | reply = do_read(fsg); |
| 2417 | break; |
| 2418 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2419 | case READ_12: |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2420 | fsg->data_size_from_cmnd = |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 2421 | get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST, |
| 2423 | (1<<1) | (0xf<<2) | (0xf<<6), 1, |
| 2424 | "READ(12)")) == 0) |
| 2425 | reply = do_read(fsg); |
| 2426 | break; |
| 2427 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2428 | case READ_CAPACITY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | fsg->data_size_from_cmnd = 8; |
| 2430 | if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, |
| 2431 | (0xf<<2) | (1<<8), 1, |
| 2432 | "READ CAPACITY")) == 0) |
| 2433 | reply = do_read_capacity(fsg, bh); |
| 2434 | break; |
| 2435 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2436 | case READ_HEADER: |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 2437 | if (!mod_data.cdrom) |
| 2438 | goto unknown_cmnd; |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2439 | fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 2440 | if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, |
| 2441 | (3<<7) | (0x1f<<1), 1, |
| 2442 | "READ HEADER")) == 0) |
| 2443 | reply = do_read_header(fsg, bh); |
| 2444 | break; |
| 2445 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2446 | case READ_TOC: |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 2447 | if (!mod_data.cdrom) |
| 2448 | goto unknown_cmnd; |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2449 | fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 2450 | if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, |
| 2451 | (7<<6) | (1<<1), 1, |
| 2452 | "READ TOC")) == 0) |
| 2453 | reply = do_read_toc(fsg, bh); |
| 2454 | break; |
| 2455 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2456 | case READ_FORMAT_CAPACITIES: |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2457 | fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, |
| 2459 | (3<<7), 1, |
| 2460 | "READ FORMAT CAPACITIES")) == 0) |
| 2461 | reply = do_read_format_capacities(fsg, bh); |
| 2462 | break; |
| 2463 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2464 | case REQUEST_SENSE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | fsg->data_size_from_cmnd = fsg->cmnd[4]; |
| 2466 | if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, |
| 2467 | (1<<4), 0, |
| 2468 | "REQUEST SENSE")) == 0) |
| 2469 | reply = do_request_sense(fsg, bh); |
| 2470 | break; |
| 2471 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2472 | case START_STOP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | fsg->data_size_from_cmnd = 0; |
| 2474 | if ((reply = check_command(fsg, 6, DATA_DIR_NONE, |
| 2475 | (1<<1) | (1<<4), 0, |
| 2476 | "START-STOP UNIT")) == 0) |
| 2477 | reply = do_start_stop(fsg); |
| 2478 | break; |
| 2479 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2480 | case SYNCHRONIZE_CACHE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | fsg->data_size_from_cmnd = 0; |
| 2482 | if ((reply = check_command(fsg, 10, DATA_DIR_NONE, |
| 2483 | (0xf<<2) | (3<<7), 1, |
| 2484 | "SYNCHRONIZE CACHE")) == 0) |
| 2485 | reply = do_synchronize_cache(fsg); |
| 2486 | break; |
| 2487 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2488 | case TEST_UNIT_READY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | fsg->data_size_from_cmnd = 0; |
| 2490 | reply = check_command(fsg, 6, DATA_DIR_NONE, |
| 2491 | 0, 1, |
| 2492 | "TEST UNIT READY"); |
| 2493 | break; |
| 2494 | |
| 2495 | /* Although optional, this command is used by MS-Windows. We |
| 2496 | * support a minimal version: BytChk must be 0. */ |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2497 | case VERIFY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | fsg->data_size_from_cmnd = 0; |
| 2499 | if ((reply = check_command(fsg, 10, DATA_DIR_NONE, |
| 2500 | (1<<1) | (0xf<<2) | (3<<7), 1, |
| 2501 | "VERIFY")) == 0) |
| 2502 | reply = do_verify(fsg); |
| 2503 | break; |
| 2504 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2505 | case WRITE_6: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | i = fsg->cmnd[4]; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 2507 | fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, |
| 2509 | (7<<1) | (1<<4), 1, |
| 2510 | "WRITE(6)")) == 0) |
| 2511 | reply = do_write(fsg); |
| 2512 | break; |
| 2513 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2514 | case WRITE_10: |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2515 | fsg->data_size_from_cmnd = |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 2516 | get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, |
| 2518 | (1<<1) | (0xf<<2) | (3<<7), 1, |
| 2519 | "WRITE(10)")) == 0) |
| 2520 | reply = do_write(fsg); |
| 2521 | break; |
| 2522 | |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2523 | case WRITE_12: |
Alan Stern | 604eb89 | 2009-04-27 13:19:41 -0400 | [diff] [blame] | 2524 | fsg->data_size_from_cmnd = |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 2525 | get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, |
| 2527 | (1<<1) | (0xf<<2) | (0xf<<6), 1, |
| 2528 | "WRITE(12)")) == 0) |
| 2529 | reply = do_write(fsg); |
| 2530 | break; |
| 2531 | |
| 2532 | /* Some mandatory commands that we recognize but don't implement. |
| 2533 | * They don't mean much in this setting. It's left as an exercise |
| 2534 | * for anyone interested to implement RESERVE and RELEASE in terms |
| 2535 | * of Posix locks. */ |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 2536 | case FORMAT_UNIT: |
| 2537 | case RELEASE: |
| 2538 | case RESERVE: |
| 2539 | case SEND_DIAGNOSTIC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | // Fall through |
| 2541 | |
| 2542 | default: |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 2543 | unknown_cmnd: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | fsg->data_size_from_cmnd = 0; |
| 2545 | sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]); |
| 2546 | if ((reply = check_command(fsg, fsg->cmnd_size, |
| 2547 | DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) { |
| 2548 | fsg->curlun->sense_data = SS_INVALID_COMMAND; |
| 2549 | reply = -EINVAL; |
| 2550 | } |
| 2551 | break; |
| 2552 | } |
| 2553 | up_read(&fsg->filesem); |
| 2554 | |
| 2555 | if (reply == -EINTR || signal_pending(current)) |
| 2556 | return -EINTR; |
| 2557 | |
| 2558 | /* Set up the single reply buffer for finish_reply() */ |
| 2559 | if (reply == -EINVAL) |
| 2560 | reply = 0; // Error reply length |
| 2561 | if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) { |
| 2562 | reply = min((u32) reply, fsg->data_size_from_cmnd); |
| 2563 | bh->inreq->length = reply; |
| 2564 | bh->state = BUF_STATE_FULL; |
| 2565 | fsg->residue -= reply; |
| 2566 | } // Otherwise it's already set |
| 2567 | |
| 2568 | return 0; |
| 2569 | } |
| 2570 | |
| 2571 | |
| 2572 | /*-------------------------------------------------------------------------*/ |
| 2573 | |
| 2574 | static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) |
| 2575 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2576 | struct usb_request *req = bh->outreq; |
| 2577 | struct fsg_bulk_cb_wrap *cbw = req->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | |
Alan Stern | b950bdb | 2008-04-14 11:45:29 -0400 | [diff] [blame] | 2579 | /* Was this a real packet? Should it be ignored? */ |
| 2580 | if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | return -EINVAL; |
| 2582 | |
| 2583 | /* Is the CBW valid? */ |
| 2584 | if (req->actual != USB_BULK_CB_WRAP_LEN || |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 2585 | cbw->Signature != cpu_to_le32( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | USB_BULK_CB_SIG)) { |
| 2587 | DBG(fsg, "invalid CBW: len %u sig 0x%x\n", |
| 2588 | req->actual, |
| 2589 | le32_to_cpu(cbw->Signature)); |
| 2590 | |
Alan Stern | b950bdb | 2008-04-14 11:45:29 -0400 | [diff] [blame] | 2591 | /* The Bulk-only spec says we MUST stall the IN endpoint |
| 2592 | * (6.6.1), so it's unavoidable. It also says we must |
| 2593 | * retain this state until the next reset, but there's |
| 2594 | * no way to tell the controller driver it should ignore |
| 2595 | * Clear-Feature(HALT) requests. |
| 2596 | * |
| 2597 | * We aren't required to halt the OUT endpoint; instead |
| 2598 | * we can simply accept and discard any data received |
| 2599 | * until the next reset. */ |
David Lopo | 62fd2ca | 2008-04-29 10:14:38 +0100 | [diff] [blame] | 2600 | wedge_bulk_in_endpoint(fsg); |
Alan Stern | b950bdb | 2008-04-14 11:45:29 -0400 | [diff] [blame] | 2601 | set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | return -EINVAL; |
| 2603 | } |
| 2604 | |
| 2605 | /* Is the CBW meaningful? */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2606 | if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG || |
Alan Stern | 12943f0 | 2007-08-24 16:27:50 -0400 | [diff] [blame] | 2607 | cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, " |
| 2609 | "cmdlen %u\n", |
| 2610 | cbw->Lun, cbw->Flags, cbw->Length); |
| 2611 | |
| 2612 | /* We can do anything we want here, so let's stall the |
| 2613 | * bulk pipes if we are allowed to. */ |
| 2614 | if (mod_data.can_stall) { |
| 2615 | fsg_set_halt(fsg, fsg->bulk_out); |
| 2616 | halt_bulk_in_endpoint(fsg); |
| 2617 | } |
| 2618 | return -EINVAL; |
| 2619 | } |
| 2620 | |
| 2621 | /* Save the command for later */ |
| 2622 | fsg->cmnd_size = cbw->Length; |
| 2623 | memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size); |
| 2624 | if (cbw->Flags & USB_BULK_IN_FLAG) |
| 2625 | fsg->data_dir = DATA_DIR_TO_HOST; |
| 2626 | else |
| 2627 | fsg->data_dir = DATA_DIR_FROM_HOST; |
| 2628 | fsg->data_size = le32_to_cpu(cbw->DataTransferLength); |
| 2629 | if (fsg->data_size == 0) |
| 2630 | fsg->data_dir = DATA_DIR_NONE; |
| 2631 | fsg->lun = cbw->Lun; |
| 2632 | fsg->tag = cbw->Tag; |
| 2633 | return 0; |
| 2634 | } |
| 2635 | |
| 2636 | |
| 2637 | static int get_next_command(struct fsg_dev *fsg) |
| 2638 | { |
| 2639 | struct fsg_buffhd *bh; |
| 2640 | int rc = 0; |
| 2641 | |
| 2642 | if (transport_is_bbb()) { |
| 2643 | |
| 2644 | /* Wait for the next buffer to become available */ |
| 2645 | bh = fsg->next_buffhd_to_fill; |
| 2646 | while (bh->state != BUF_STATE_EMPTY) { |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2647 | rc = sleep_thread(fsg); |
| 2648 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | return rc; |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | |
| 2652 | /* Queue a request to read a Bulk-only CBW */ |
Greg Kroah-Hartman | 98346f7 | 2011-04-14 13:42:46 -0700 | [diff] [blame] | 2653 | set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN); |
| 2654 | bh->outreq->short_not_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | start_transfer(fsg, fsg->bulk_out, bh->outreq, |
| 2656 | &bh->outreq_busy, &bh->state); |
| 2657 | |
| 2658 | /* We will drain the buffer in software, which means we |
| 2659 | * can reuse it for the next filling. No need to advance |
| 2660 | * next_buffhd_to_fill. */ |
| 2661 | |
| 2662 | /* Wait for the CBW to arrive */ |
| 2663 | while (bh->state != BUF_STATE_FULL) { |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2664 | rc = sleep_thread(fsg); |
| 2665 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | return rc; |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2667 | } |
Alan Stern | a21d4fe | 2005-11-29 12:04:24 -0500 | [diff] [blame] | 2668 | smp_rmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | rc = received_cbw(fsg, bh); |
| 2670 | bh->state = BUF_STATE_EMPTY; |
| 2671 | |
| 2672 | } else { // USB_PR_CB or USB_PR_CBI |
| 2673 | |
| 2674 | /* Wait for the next command to arrive */ |
| 2675 | while (fsg->cbbuf_cmnd_size == 0) { |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2676 | rc = sleep_thread(fsg); |
| 2677 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | return rc; |
Alan Stern | 79a7d9e | 2007-08-08 17:10:11 -0400 | [diff] [blame] | 2679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | |
| 2681 | /* Is the previous status interrupt request still busy? |
| 2682 | * The host is allowed to skip reading the status, |
| 2683 | * so we must cancel it. */ |
| 2684 | if (fsg->intreq_busy) |
| 2685 | usb_ep_dequeue(fsg->intr_in, fsg->intreq); |
| 2686 | |
| 2687 | /* Copy the command and mark the buffer empty */ |
| 2688 | fsg->data_dir = DATA_DIR_UNKNOWN; |
| 2689 | spin_lock_irq(&fsg->lock); |
| 2690 | fsg->cmnd_size = fsg->cbbuf_cmnd_size; |
| 2691 | memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size); |
| 2692 | fsg->cbbuf_cmnd_size = 0; |
| 2693 | spin_unlock_irq(&fsg->lock); |
| 2694 | } |
| 2695 | return rc; |
| 2696 | } |
| 2697 | |
| 2698 | |
| 2699 | /*-------------------------------------------------------------------------*/ |
| 2700 | |
| 2701 | static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep, |
| 2702 | const struct usb_endpoint_descriptor *d) |
| 2703 | { |
| 2704 | int rc; |
| 2705 | |
| 2706 | ep->driver_data = fsg; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 2707 | ep->desc = d; |
| 2708 | rc = usb_ep_enable(ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | if (rc) |
| 2710 | ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc); |
| 2711 | return rc; |
| 2712 | } |
| 2713 | |
| 2714 | static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep, |
| 2715 | struct usb_request **preq) |
| 2716 | { |
| 2717 | *preq = usb_ep_alloc_request(ep, GFP_ATOMIC); |
| 2718 | if (*preq) |
| 2719 | return 0; |
| 2720 | ERROR(fsg, "can't allocate request for %s\n", ep->name); |
| 2721 | return -ENOMEM; |
| 2722 | } |
| 2723 | |
| 2724 | /* |
| 2725 | * Reset interface setting and re-init endpoint state (toggle etc). |
| 2726 | * Call with altsetting < 0 to disable the interface. The only other |
| 2727 | * available altsetting is 0, which enables the interface. |
| 2728 | */ |
| 2729 | static int do_set_interface(struct fsg_dev *fsg, int altsetting) |
| 2730 | { |
| 2731 | int rc = 0; |
| 2732 | int i; |
| 2733 | const struct usb_endpoint_descriptor *d; |
| 2734 | |
| 2735 | if (fsg->running) |
| 2736 | DBG(fsg, "reset interface\n"); |
| 2737 | |
| 2738 | reset: |
| 2739 | /* Deallocate the requests */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2740 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | struct fsg_buffhd *bh = &fsg->buffhds[i]; |
| 2742 | |
| 2743 | if (bh->inreq) { |
| 2744 | usb_ep_free_request(fsg->bulk_in, bh->inreq); |
| 2745 | bh->inreq = NULL; |
| 2746 | } |
| 2747 | if (bh->outreq) { |
| 2748 | usb_ep_free_request(fsg->bulk_out, bh->outreq); |
| 2749 | bh->outreq = NULL; |
| 2750 | } |
| 2751 | } |
| 2752 | if (fsg->intreq) { |
| 2753 | usb_ep_free_request(fsg->intr_in, fsg->intreq); |
| 2754 | fsg->intreq = NULL; |
| 2755 | } |
| 2756 | |
| 2757 | /* Disable the endpoints */ |
| 2758 | if (fsg->bulk_in_enabled) { |
| 2759 | usb_ep_disable(fsg->bulk_in); |
| 2760 | fsg->bulk_in_enabled = 0; |
| 2761 | } |
| 2762 | if (fsg->bulk_out_enabled) { |
| 2763 | usb_ep_disable(fsg->bulk_out); |
| 2764 | fsg->bulk_out_enabled = 0; |
| 2765 | } |
| 2766 | if (fsg->intr_in_enabled) { |
| 2767 | usb_ep_disable(fsg->intr_in); |
| 2768 | fsg->intr_in_enabled = 0; |
| 2769 | } |
| 2770 | |
| 2771 | fsg->running = 0; |
| 2772 | if (altsetting < 0 || rc != 0) |
| 2773 | return rc; |
| 2774 | |
| 2775 | DBG(fsg, "set interface %d\n", altsetting); |
| 2776 | |
| 2777 | /* Enable the endpoints */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2778 | d = fsg_ep_desc(fsg->gadget, |
| 2779 | &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0) |
| 2781 | goto reset; |
| 2782 | fsg->bulk_in_enabled = 1; |
| 2783 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2784 | d = fsg_ep_desc(fsg->gadget, |
| 2785 | &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0) |
| 2787 | goto reset; |
| 2788 | fsg->bulk_out_enabled = 1; |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 2789 | fsg->bulk_out_maxpacket = usb_endpoint_maxp(d); |
Alan Stern | b950bdb | 2008-04-14 11:45:29 -0400 | [diff] [blame] | 2790 | clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | |
| 2792 | if (transport_is_cbi()) { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2793 | d = fsg_ep_desc(fsg->gadget, |
| 2794 | &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0) |
| 2796 | goto reset; |
| 2797 | fsg->intr_in_enabled = 1; |
| 2798 | } |
| 2799 | |
| 2800 | /* Allocate the requests */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2801 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | struct fsg_buffhd *bh = &fsg->buffhds[i]; |
| 2803 | |
| 2804 | if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0) |
| 2805 | goto reset; |
| 2806 | if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0) |
| 2807 | goto reset; |
| 2808 | bh->inreq->buf = bh->outreq->buf = bh->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | bh->inreq->context = bh->outreq->context = bh; |
| 2810 | bh->inreq->complete = bulk_in_complete; |
| 2811 | bh->outreq->complete = bulk_out_complete; |
| 2812 | } |
| 2813 | if (transport_is_cbi()) { |
| 2814 | if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0) |
| 2815 | goto reset; |
| 2816 | fsg->intreq->complete = intr_in_complete; |
| 2817 | } |
| 2818 | |
| 2819 | fsg->running = 1; |
| 2820 | for (i = 0; i < fsg->nluns; ++i) |
| 2821 | fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED; |
| 2822 | return rc; |
| 2823 | } |
| 2824 | |
| 2825 | |
| 2826 | /* |
| 2827 | * Change our operational configuration. This code must agree with the code |
| 2828 | * that returns config descriptors, and with interface altsetting code. |
| 2829 | * |
| 2830 | * It's also responsible for power management interactions. Some |
| 2831 | * configurations might not work with our current power sources. |
| 2832 | * For now we just assume the gadget is always self-powered. |
| 2833 | */ |
| 2834 | static int do_set_config(struct fsg_dev *fsg, u8 new_config) |
| 2835 | { |
| 2836 | int rc = 0; |
| 2837 | |
| 2838 | /* Disable the single interface */ |
| 2839 | if (fsg->config != 0) { |
| 2840 | DBG(fsg, "reset config\n"); |
| 2841 | fsg->config = 0; |
| 2842 | rc = do_set_interface(fsg, -1); |
| 2843 | } |
| 2844 | |
| 2845 | /* Enable the interface */ |
| 2846 | if (new_config != 0) { |
| 2847 | fsg->config = new_config; |
| 2848 | if ((rc = do_set_interface(fsg, 0)) != 0) |
| 2849 | fsg->config = 0; // Reset on errors |
| 2850 | else { |
| 2851 | char *speed; |
| 2852 | |
| 2853 | switch (fsg->gadget->speed) { |
| 2854 | case USB_SPEED_LOW: speed = "low"; break; |
| 2855 | case USB_SPEED_FULL: speed = "full"; break; |
| 2856 | case USB_SPEED_HIGH: speed = "high"; break; |
| 2857 | default: speed = "?"; break; |
| 2858 | } |
| 2859 | INFO(fsg, "%s speed config #%d\n", speed, fsg->config); |
| 2860 | } |
| 2861 | } |
| 2862 | return rc; |
| 2863 | } |
| 2864 | |
| 2865 | |
| 2866 | /*-------------------------------------------------------------------------*/ |
| 2867 | |
| 2868 | static void handle_exception(struct fsg_dev *fsg) |
| 2869 | { |
| 2870 | siginfo_t info; |
| 2871 | int sig; |
| 2872 | int i; |
| 2873 | int num_active; |
| 2874 | struct fsg_buffhd *bh; |
| 2875 | enum fsg_state old_state; |
| 2876 | u8 new_config; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2877 | struct fsg_lun *curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | unsigned int exception_req_tag; |
| 2879 | int rc; |
| 2880 | |
| 2881 | /* Clear the existing signals. Anything but SIGUSR1 is converted |
| 2882 | * into a high-priority EXIT exception. */ |
| 2883 | for (;;) { |
Oleg Nesterov | 8cfbe7e | 2007-05-30 11:06:33 -0400 | [diff] [blame] | 2884 | sig = dequeue_signal_lock(current, ¤t->blocked, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | if (!sig) |
| 2886 | break; |
| 2887 | if (sig != SIGUSR1) { |
| 2888 | if (fsg->state < FSG_STATE_EXIT) |
| 2889 | DBG(fsg, "Main thread exiting on signal\n"); |
| 2890 | raise_exception(fsg, FSG_STATE_EXIT); |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | /* Cancel all the pending transfers */ |
| 2895 | if (fsg->intreq_busy) |
| 2896 | usb_ep_dequeue(fsg->intr_in, fsg->intreq); |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2897 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | bh = &fsg->buffhds[i]; |
| 2899 | if (bh->inreq_busy) |
| 2900 | usb_ep_dequeue(fsg->bulk_in, bh->inreq); |
| 2901 | if (bh->outreq_busy) |
| 2902 | usb_ep_dequeue(fsg->bulk_out, bh->outreq); |
| 2903 | } |
| 2904 | |
| 2905 | /* Wait until everything is idle */ |
| 2906 | for (;;) { |
| 2907 | num_active = fsg->intreq_busy; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2908 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | bh = &fsg->buffhds[i]; |
| 2910 | num_active += bh->inreq_busy + bh->outreq_busy; |
| 2911 | } |
| 2912 | if (num_active == 0) |
| 2913 | break; |
| 2914 | if (sleep_thread(fsg)) |
| 2915 | return; |
| 2916 | } |
| 2917 | |
| 2918 | /* Clear out the controller's fifos */ |
| 2919 | if (fsg->bulk_in_enabled) |
| 2920 | usb_ep_fifo_flush(fsg->bulk_in); |
| 2921 | if (fsg->bulk_out_enabled) |
| 2922 | usb_ep_fifo_flush(fsg->bulk_out); |
| 2923 | if (fsg->intr_in_enabled) |
| 2924 | usb_ep_fifo_flush(fsg->intr_in); |
| 2925 | |
| 2926 | /* Reset the I/O buffer states and pointers, the SCSI |
| 2927 | * state, and the exception. Then invoke the handler. */ |
| 2928 | spin_lock_irq(&fsg->lock); |
| 2929 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 2930 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | bh = &fsg->buffhds[i]; |
| 2932 | bh->state = BUF_STATE_EMPTY; |
| 2933 | } |
| 2934 | fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain = |
| 2935 | &fsg->buffhds[0]; |
| 2936 | |
| 2937 | exception_req_tag = fsg->exception_req_tag; |
| 2938 | new_config = fsg->new_config; |
| 2939 | old_state = fsg->state; |
| 2940 | |
| 2941 | if (old_state == FSG_STATE_ABORT_BULK_OUT) |
| 2942 | fsg->state = FSG_STATE_STATUS_PHASE; |
| 2943 | else { |
| 2944 | for (i = 0; i < fsg->nluns; ++i) { |
| 2945 | curlun = &fsg->luns[i]; |
| 2946 | curlun->prevent_medium_removal = 0; |
| 2947 | curlun->sense_data = curlun->unit_attention_data = |
| 2948 | SS_NO_SENSE; |
| 2949 | curlun->sense_data_info = 0; |
Alan Stern | 6174d0f | 2006-09-26 14:51:48 -0400 | [diff] [blame] | 2950 | curlun->info_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | } |
| 2952 | fsg->state = FSG_STATE_IDLE; |
| 2953 | } |
| 2954 | spin_unlock_irq(&fsg->lock); |
| 2955 | |
| 2956 | /* Carry out any extra actions required for the exception */ |
| 2957 | switch (old_state) { |
| 2958 | default: |
| 2959 | break; |
| 2960 | |
| 2961 | case FSG_STATE_ABORT_BULK_OUT: |
| 2962 | send_status(fsg); |
| 2963 | spin_lock_irq(&fsg->lock); |
| 2964 | if (fsg->state == FSG_STATE_STATUS_PHASE) |
| 2965 | fsg->state = FSG_STATE_IDLE; |
| 2966 | spin_unlock_irq(&fsg->lock); |
| 2967 | break; |
| 2968 | |
| 2969 | case FSG_STATE_RESET: |
| 2970 | /* In case we were forced against our will to halt a |
| 2971 | * bulk endpoint, clear the halt now. (The SuperH UDC |
| 2972 | * requires this.) */ |
Alan Stern | b950bdb | 2008-04-14 11:45:29 -0400 | [diff] [blame] | 2973 | if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | usb_ep_clear_halt(fsg->bulk_in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | |
| 2976 | if (transport_is_bbb()) { |
| 2977 | if (fsg->ep0_req_tag == exception_req_tag) |
| 2978 | ep0_queue(fsg); // Complete the status stage |
| 2979 | |
| 2980 | } else if (transport_is_cbi()) |
| 2981 | send_status(fsg); // Status by interrupt pipe |
| 2982 | |
| 2983 | /* Technically this should go here, but it would only be |
| 2984 | * a waste of time. Ditto for the INTERFACE_CHANGE and |
| 2985 | * CONFIG_CHANGE cases. */ |
| 2986 | // for (i = 0; i < fsg->nluns; ++i) |
| 2987 | // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED; |
| 2988 | break; |
| 2989 | |
| 2990 | case FSG_STATE_INTERFACE_CHANGE: |
| 2991 | rc = do_set_interface(fsg, 0); |
| 2992 | if (fsg->ep0_req_tag != exception_req_tag) |
| 2993 | break; |
| 2994 | if (rc != 0) // STALL on errors |
| 2995 | fsg_set_halt(fsg, fsg->ep0); |
| 2996 | else // Complete the status stage |
| 2997 | ep0_queue(fsg); |
| 2998 | break; |
| 2999 | |
| 3000 | case FSG_STATE_CONFIG_CHANGE: |
| 3001 | rc = do_set_config(fsg, new_config); |
| 3002 | if (fsg->ep0_req_tag != exception_req_tag) |
| 3003 | break; |
| 3004 | if (rc != 0) // STALL on errors |
| 3005 | fsg_set_halt(fsg, fsg->ep0); |
| 3006 | else // Complete the status stage |
| 3007 | ep0_queue(fsg); |
| 3008 | break; |
| 3009 | |
| 3010 | case FSG_STATE_DISCONNECT: |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 3011 | for (i = 0; i < fsg->nluns; ++i) |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3012 | fsg_lun_fsync_sub(fsg->luns + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | do_set_config(fsg, 0); // Unconfigured state |
| 3014 | break; |
| 3015 | |
| 3016 | case FSG_STATE_EXIT: |
| 3017 | case FSG_STATE_TERMINATED: |
| 3018 | do_set_config(fsg, 0); // Free resources |
| 3019 | spin_lock_irq(&fsg->lock); |
| 3020 | fsg->state = FSG_STATE_TERMINATED; // Stop the thread |
| 3021 | spin_unlock_irq(&fsg->lock); |
| 3022 | break; |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | |
| 3027 | /*-------------------------------------------------------------------------*/ |
| 3028 | |
| 3029 | static int fsg_main_thread(void *fsg_) |
| 3030 | { |
John Daiker | 7ca46b8 | 2006-12-29 19:02:06 -0800 | [diff] [blame] | 3031 | struct fsg_dev *fsg = fsg_; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | /* Allow the thread to be killed by a signal, but set the signal mask |
| 3034 | * to block everything but INT, TERM, KILL, and USR1. */ |
Oleg Nesterov | 8cfbe7e | 2007-05-30 11:06:33 -0400 | [diff] [blame] | 3035 | allow_signal(SIGINT); |
| 3036 | allow_signal(SIGTERM); |
| 3037 | allow_signal(SIGKILL); |
| 3038 | allow_signal(SIGUSR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 3040 | /* Allow the thread to be frozen */ |
| 3041 | set_freezable(); |
| 3042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | /* Arrange for userspace references to be interpreted as kernel |
| 3044 | * pointers. That way we can pass a kernel pointer to a routine |
| 3045 | * that expects a __user pointer and it will work okay. */ |
| 3046 | set_fs(get_ds()); |
| 3047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | /* The main loop */ |
| 3049 | while (fsg->state != FSG_STATE_TERMINATED) { |
| 3050 | if (exception_in_progress(fsg) || signal_pending(current)) { |
| 3051 | handle_exception(fsg); |
| 3052 | continue; |
| 3053 | } |
| 3054 | |
| 3055 | if (!fsg->running) { |
| 3056 | sleep_thread(fsg); |
| 3057 | continue; |
| 3058 | } |
| 3059 | |
| 3060 | if (get_next_command(fsg)) |
| 3061 | continue; |
| 3062 | |
| 3063 | spin_lock_irq(&fsg->lock); |
| 3064 | if (!exception_in_progress(fsg)) |
| 3065 | fsg->state = FSG_STATE_DATA_PHASE; |
| 3066 | spin_unlock_irq(&fsg->lock); |
| 3067 | |
| 3068 | if (do_scsi_command(fsg) || finish_reply(fsg)) |
| 3069 | continue; |
| 3070 | |
| 3071 | spin_lock_irq(&fsg->lock); |
| 3072 | if (!exception_in_progress(fsg)) |
| 3073 | fsg->state = FSG_STATE_STATUS_PHASE; |
| 3074 | spin_unlock_irq(&fsg->lock); |
| 3075 | |
| 3076 | if (send_status(fsg)) |
| 3077 | continue; |
| 3078 | |
| 3079 | spin_lock_irq(&fsg->lock); |
| 3080 | if (!exception_in_progress(fsg)) |
| 3081 | fsg->state = FSG_STATE_IDLE; |
| 3082 | spin_unlock_irq(&fsg->lock); |
| 3083 | } |
| 3084 | |
Alan Stern | 22efcf4 | 2005-09-26 16:12:02 -0400 | [diff] [blame] | 3085 | spin_lock_irq(&fsg->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | fsg->thread_task = NULL; |
Alan Stern | 22efcf4 | 2005-09-26 16:12:02 -0400 | [diff] [blame] | 3087 | spin_unlock_irq(&fsg->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | |
Alan Stern | 82a10a8 | 2009-04-16 15:37:28 -0400 | [diff] [blame] | 3089 | /* If we are exiting because of a signal, unregister the |
| 3090 | * gadget driver. */ |
| 3091 | if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | usb_gadget_unregister_driver(&fsg_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | |
| 3094 | /* Let the unbind and cleanup routines know the thread has exited */ |
| 3095 | complete_and_exit(&fsg->thread_notifier, 0); |
| 3096 | } |
| 3097 | |
| 3098 | |
| 3099 | /*-------------------------------------------------------------------------*/ |
| 3100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | |
| 3102 | /* The write permissions and store_xxx pointers are set in fsg_bind() */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 3103 | static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL); |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 3104 | static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL); |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 3105 | static DEVICE_ATTR(file, 0444, fsg_show_file, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
| 3107 | |
| 3108 | /*-------------------------------------------------------------------------*/ |
| 3109 | |
Alan Stern | 87c4252 | 2005-11-09 16:59:56 -0500 | [diff] [blame] | 3110 | static void fsg_release(struct kref *ref) |
| 3111 | { |
| 3112 | struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref); |
| 3113 | |
| 3114 | kfree(fsg->luns); |
| 3115 | kfree(fsg); |
| 3116 | } |
| 3117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | static void lun_release(struct device *dev) |
| 3119 | { |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 3120 | struct rw_semaphore *filesem = dev_get_drvdata(dev); |
| 3121 | struct fsg_dev *fsg = |
| 3122 | container_of(filesem, struct fsg_dev, filesem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | |
Alan Stern | 87c4252 | 2005-11-09 16:59:56 -0500 | [diff] [blame] | 3124 | kref_put(&fsg->ref, fsg_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
David Brownell | a353678 | 2006-07-06 15:48:53 -0700 | [diff] [blame] | 3127 | static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | { |
| 3129 | struct fsg_dev *fsg = get_gadget_data(gadget); |
| 3130 | int i; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3131 | struct fsg_lun *curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | struct usb_request *req = fsg->ep0req; |
| 3133 | |
| 3134 | DBG(fsg, "unbind\n"); |
| 3135 | clear_bit(REGISTERED, &fsg->atomic_bitflags); |
| 3136 | |
| 3137 | /* Unregister the sysfs attribute files and the LUNs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | for (i = 0; i < fsg->nluns; ++i) { |
| 3139 | curlun = &fsg->luns[i]; |
| 3140 | if (curlun->registered) { |
Michal Nazarewicz | 452ee0e | 2010-08-12 17:43:50 +0200 | [diff] [blame] | 3141 | device_remove_file(&curlun->dev, &dev_attr_nofua); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | device_remove_file(&curlun->dev, &dev_attr_ro); |
| 3143 | device_remove_file(&curlun->dev, &dev_attr_file); |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3144 | fsg_lun_close(curlun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | device_unregister(&curlun->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | curlun->registered = 0; |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | /* If the thread isn't already dead, tell it to exit now */ |
| 3151 | if (fsg->state != FSG_STATE_TERMINATED) { |
| 3152 | raise_exception(fsg, FSG_STATE_EXIT); |
| 3153 | wait_for_completion(&fsg->thread_notifier); |
| 3154 | |
| 3155 | /* The cleanup routine waits for this completion also */ |
| 3156 | complete(&fsg->thread_notifier); |
| 3157 | } |
| 3158 | |
| 3159 | /* Free the data buffers */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3160 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) |
David Brownell | 9d8bab5 | 2007-07-01 11:04:54 -0700 | [diff] [blame] | 3161 | kfree(fsg->buffhds[i].buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | |
| 3163 | /* Free the request and buffer for endpoint 0 */ |
| 3164 | if (req) { |
David Brownell | 9d8bab5 | 2007-07-01 11:04:54 -0700 | [diff] [blame] | 3165 | kfree(req->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | usb_ep_free_request(fsg->ep0, req); |
| 3167 | } |
| 3168 | |
| 3169 | set_gadget_data(gadget, NULL); |
| 3170 | } |
| 3171 | |
| 3172 | |
| 3173 | static int __init check_parameters(struct fsg_dev *fsg) |
| 3174 | { |
| 3175 | int prot; |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 3176 | int gcnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | |
| 3178 | /* Store the default values */ |
| 3179 | mod_data.transport_type = USB_PR_BULK; |
| 3180 | mod_data.transport_name = "Bulk-only"; |
| 3181 | mod_data.protocol_type = USB_SC_SCSI; |
| 3182 | mod_data.protocol_name = "Transparent SCSI"; |
| 3183 | |
Alan Stern | ce459ec | 2009-02-24 16:19:47 -0500 | [diff] [blame] | 3184 | /* Some peripheral controllers are known not to be able to |
| 3185 | * halt bulk endpoints correctly. If one of them is present, |
| 3186 | * disable stalls. |
| 3187 | */ |
Christoph Egger | 90f7976 | 2010-02-05 13:24:12 +0100 | [diff] [blame] | 3188 | if (gadget_is_at91(fsg->gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | mod_data.can_stall = 0; |
| 3190 | |
| 3191 | if (mod_data.release == 0xffff) { // Parameter wasn't set |
Christoph Egger | 90f7976 | 2010-02-05 13:24:12 +0100 | [diff] [blame] | 3192 | gcnum = usb_gadget_controller_number(fsg->gadget); |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 3193 | if (gcnum >= 0) |
| 3194 | mod_data.release = 0x0300 + gcnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | else { |
Arjan van de Ven | b6c6393 | 2008-07-25 01:45:52 -0700 | [diff] [blame] | 3196 | WARNING(fsg, "controller '%s' not recognized\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | fsg->gadget->name); |
| 3198 | mod_data.release = 0x0399; |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | prot = simple_strtol(mod_data.protocol_parm, NULL, 0); |
| 3203 | |
| 3204 | #ifdef CONFIG_USB_FILE_STORAGE_TEST |
| 3205 | if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) { |
| 3206 | ; // Use default setting |
| 3207 | } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) { |
| 3208 | mod_data.transport_type = USB_PR_CB; |
| 3209 | mod_data.transport_name = "Control-Bulk"; |
| 3210 | } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) { |
| 3211 | mod_data.transport_type = USB_PR_CBI; |
| 3212 | mod_data.transport_name = "Control-Bulk-Interrupt"; |
| 3213 | } else { |
| 3214 | ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm); |
| 3215 | return -EINVAL; |
| 3216 | } |
| 3217 | |
| 3218 | if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 || |
| 3219 | prot == USB_SC_SCSI) { |
| 3220 | ; // Use default setting |
| 3221 | } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 || |
| 3222 | prot == USB_SC_RBC) { |
| 3223 | mod_data.protocol_type = USB_SC_RBC; |
| 3224 | mod_data.protocol_name = "RBC"; |
| 3225 | } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 || |
| 3226 | strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 || |
| 3227 | prot == USB_SC_8020) { |
| 3228 | mod_data.protocol_type = USB_SC_8020; |
| 3229 | mod_data.protocol_name = "8020i (ATAPI)"; |
| 3230 | } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 || |
| 3231 | prot == USB_SC_QIC) { |
| 3232 | mod_data.protocol_type = USB_SC_QIC; |
| 3233 | mod_data.protocol_name = "QIC-157"; |
| 3234 | } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 || |
| 3235 | prot == USB_SC_UFI) { |
| 3236 | mod_data.protocol_type = USB_SC_UFI; |
| 3237 | mod_data.protocol_name = "UFI"; |
| 3238 | } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 || |
| 3239 | prot == USB_SC_8070) { |
| 3240 | mod_data.protocol_type = USB_SC_8070; |
| 3241 | mod_data.protocol_name = "8070i"; |
| 3242 | } else { |
| 3243 | ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm); |
| 3244 | return -EINVAL; |
| 3245 | } |
| 3246 | |
| 3247 | mod_data.buflen &= PAGE_CACHE_MASK; |
| 3248 | if (mod_data.buflen <= 0) { |
| 3249 | ERROR(fsg, "invalid buflen\n"); |
| 3250 | return -ETOOSMALL; |
| 3251 | } |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3252 | |
Michal Nazarewicz | 5670649 | 2010-07-22 14:16:37 +0200 | [diff] [blame] | 3253 | #endif /* CONFIG_USB_FILE_STORAGE_TEST */ |
| 3254 | |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3255 | /* Serial string handling. |
| 3256 | * On a real device, the serial string would be loaded |
| 3257 | * from permanent storage. */ |
Michal Nazarewicz | 5670649 | 2010-07-22 14:16:37 +0200 | [diff] [blame] | 3258 | if (mod_data.serial) { |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3259 | const char *ch; |
| 3260 | unsigned len = 0; |
| 3261 | |
| 3262 | /* Sanity check : |
| 3263 | * The CB[I] specification limits the serial string to |
| 3264 | * 12 uppercase hexadecimal characters. |
| 3265 | * BBB need at least 12 uppercase hexadecimal characters, |
| 3266 | * with a maximum of 126. */ |
Michal Nazarewicz | 5670649 | 2010-07-22 14:16:37 +0200 | [diff] [blame] | 3267 | for (ch = mod_data.serial; *ch; ++ch) { |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3268 | ++len; |
| 3269 | if ((*ch < '0' || *ch > '9') && |
| 3270 | (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */ |
| 3271 | WARNING(fsg, |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 3272 | "Invalid serial string character: %c\n", |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3273 | *ch); |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 3274 | goto no_serial; |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3275 | } |
| 3276 | } |
| 3277 | if (len > 126 || |
| 3278 | (mod_data.transport_type == USB_PR_BULK && len < 12) || |
| 3279 | (mod_data.transport_type != USB_PR_BULK && len > 12)) { |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 3280 | WARNING(fsg, "Invalid serial string length!\n"); |
| 3281 | goto no_serial; |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3282 | } |
Michal Nazarewicz | 5670649 | 2010-07-22 14:16:37 +0200 | [diff] [blame] | 3283 | fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial; |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3284 | } else { |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 3285 | WARNING(fsg, "No serial-number string provided!\n"); |
| 3286 | no_serial: |
| 3287 | device_desc.iSerialNumber = 0; |
Yann Cantin | 87eb1be | 2010-06-05 23:06:31 +0200 | [diff] [blame] | 3288 | } |
| 3289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | return 0; |
| 3291 | } |
| 3292 | |
| 3293 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 3294 | static int __init fsg_bind(struct usb_gadget *gadget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | { |
| 3296 | struct fsg_dev *fsg = the_fsg; |
| 3297 | int rc; |
| 3298 | int i; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3299 | struct fsg_lun *curlun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | struct usb_ep *ep; |
| 3301 | struct usb_request *req; |
| 3302 | char *pathbuf, *p; |
| 3303 | |
| 3304 | fsg->gadget = gadget; |
| 3305 | set_gadget_data(gadget, fsg); |
| 3306 | fsg->ep0 = gadget->ep0; |
| 3307 | fsg->ep0->driver_data = fsg; |
| 3308 | |
| 3309 | if ((rc = check_parameters(fsg)) != 0) |
| 3310 | goto out; |
| 3311 | |
| 3312 | if (mod_data.removable) { // Enable the store_xxx attributes |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 3313 | dev_attr_file.attr.mode = 0644; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 3314 | dev_attr_file.store = fsg_store_file; |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 3315 | if (!mod_data.cdrom) { |
| 3316 | dev_attr_ro.attr.mode = 0644; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 3317 | dev_attr_ro.store = fsg_store_ro; |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 3318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | } |
| 3320 | |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 3321 | /* Only for removable media? */ |
| 3322 | dev_attr_nofua.attr.mode = 0644; |
| 3323 | dev_attr_nofua.store = fsg_store_nofua; |
| 3324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | /* Find out how many LUNs there should be */ |
| 3326 | i = mod_data.nluns; |
| 3327 | if (i == 0) |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 3328 | i = max(mod_data.num_filenames, 1u); |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3329 | if (i > FSG_MAX_LUNS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | ERROR(fsg, "invalid number of LUNs: %d\n", i); |
| 3331 | rc = -EINVAL; |
| 3332 | goto out; |
| 3333 | } |
| 3334 | |
| 3335 | /* Create the LUNs, open their backing files, and register the |
| 3336 | * LUN devices in sysfs. */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3337 | fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | if (!fsg->luns) { |
| 3339 | rc = -ENOMEM; |
| 3340 | goto out; |
| 3341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | fsg->nluns = i; |
| 3343 | |
| 3344 | for (i = 0; i < fsg->nluns; ++i) { |
| 3345 | curlun = &fsg->luns[i]; |
Michal Nazarewicz | e909ef5 | 2009-10-28 16:57:16 +0100 | [diff] [blame] | 3346 | curlun->cdrom = !!mod_data.cdrom; |
| 3347 | curlun->ro = mod_data.cdrom || mod_data.ro[i]; |
| 3348 | curlun->initially_ro = curlun->ro; |
| 3349 | curlun->removable = mod_data.removable; |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 3350 | curlun->nofua = mod_data.nofua[i]; |
Alan Stern | 2de9eae | 2006-09-25 14:31:15 -0400 | [diff] [blame] | 3351 | curlun->dev.release = lun_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | curlun->dev.parent = &gadget->dev; |
| 3353 | curlun->dev.driver = &fsg_driver.driver; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 3354 | dev_set_drvdata(&curlun->dev, &fsg->filesem); |
Kay Sievers | 0031a06 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 3355 | dev_set_name(&curlun->dev,"%s-lun%d", |
| 3356 | dev_name(&gadget->dev), i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | |
Michal Nazarewicz | d9385b6 | 2010-10-28 17:31:18 +0200 | [diff] [blame] | 3358 | kref_get(&fsg->ref); |
| 3359 | rc = device_register(&curlun->dev); |
| 3360 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | INFO(fsg, "failed to register LUN%d: %d\n", i, rc); |
Michal Nazarewicz | d9385b6 | 2010-10-28 17:31:18 +0200 | [diff] [blame] | 3362 | put_device(&curlun->dev); |
Alan Stern | 2de9eae | 2006-09-25 14:31:15 -0400 | [diff] [blame] | 3363 | goto out; |
| 3364 | } |
| 3365 | curlun->registered = 1; |
Michal Nazarewicz | d9385b6 | 2010-10-28 17:31:18 +0200 | [diff] [blame] | 3366 | |
| 3367 | rc = device_create_file(&curlun->dev, &dev_attr_ro); |
| 3368 | if (rc) |
| 3369 | goto out; |
| 3370 | rc = device_create_file(&curlun->dev, &dev_attr_nofua); |
| 3371 | if (rc) |
| 3372 | goto out; |
| 3373 | rc = device_create_file(&curlun->dev, &dev_attr_file); |
| 3374 | if (rc) |
| 3375 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | |
Alan Stern | aafe5bd | 2006-03-31 11:46:43 -0500 | [diff] [blame] | 3377 | if (mod_data.file[i] && *mod_data.file[i]) { |
Michal Nazarewicz | d9385b6 | 2010-10-28 17:31:18 +0200 | [diff] [blame] | 3378 | rc = fsg_lun_open(curlun, mod_data.file[i]); |
| 3379 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | goto out; |
| 3381 | } else if (!mod_data.removable) { |
| 3382 | ERROR(fsg, "no file given for LUN%d\n", i); |
| 3383 | rc = -EINVAL; |
| 3384 | goto out; |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | /* Find all the endpoints we will use */ |
| 3389 | usb_ep_autoconfig_reset(gadget); |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3390 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | if (!ep) |
| 3392 | goto autoconf_fail; |
| 3393 | ep->driver_data = fsg; // claim the endpoint |
| 3394 | fsg->bulk_in = ep; |
| 3395 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3396 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | if (!ep) |
| 3398 | goto autoconf_fail; |
| 3399 | ep->driver_data = fsg; // claim the endpoint |
| 3400 | fsg->bulk_out = ep; |
| 3401 | |
| 3402 | if (transport_is_cbi()) { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3403 | ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | if (!ep) |
| 3405 | goto autoconf_fail; |
| 3406 | ep->driver_data = fsg; // claim the endpoint |
| 3407 | fsg->intr_in = ep; |
| 3408 | } |
| 3409 | |
| 3410 | /* Fix up the descriptors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | device_desc.idVendor = cpu_to_le16(mod_data.vendor); |
| 3412 | device_desc.idProduct = cpu_to_le16(mod_data.product); |
| 3413 | device_desc.bcdDevice = cpu_to_le16(mod_data.release); |
| 3414 | |
| 3415 | i = (transport_is_cbi() ? 3 : 2); // Number of endpoints |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3416 | fsg_intf_desc.bNumEndpoints = i; |
| 3417 | fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type; |
| 3418 | fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type; |
| 3419 | fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 3421 | if (gadget_is_dualspeed(gadget)) { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3422 | fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 3424 | /* Assume endpoint addresses are the same for both speeds */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3425 | fsg_hs_bulk_in_desc.bEndpointAddress = |
| 3426 | fsg_fs_bulk_in_desc.bEndpointAddress; |
| 3427 | fsg_hs_bulk_out_desc.bEndpointAddress = |
| 3428 | fsg_fs_bulk_out_desc.bEndpointAddress; |
| 3429 | fsg_hs_intr_in_desc.bEndpointAddress = |
| 3430 | fsg_fs_intr_in_desc.bEndpointAddress; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | } |
| 3432 | |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 3433 | if (gadget_is_otg(gadget)) |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3434 | fsg_otg_desc.bmAttributes |= USB_OTG_HNP; |
David Brownell | 2e806f6 | 2007-08-02 00:03:39 -0700 | [diff] [blame] | 3435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | rc = -ENOMEM; |
| 3437 | |
| 3438 | /* Allocate the request and buffer for endpoint 0 */ |
| 3439 | fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL); |
| 3440 | if (!req) |
| 3441 | goto out; |
David Brownell | 9d8bab5 | 2007-07-01 11:04:54 -0700 | [diff] [blame] | 3442 | req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | if (!req->buf) |
| 3444 | goto out; |
| 3445 | req->complete = ep0_complete; |
| 3446 | |
| 3447 | /* Allocate the data buffers */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3448 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | struct fsg_buffhd *bh = &fsg->buffhds[i]; |
| 3450 | |
Alan Stern | 14cd5f8 | 2006-03-23 15:07:25 -0500 | [diff] [blame] | 3451 | /* Allocate for the bulk-in endpoint. We assume that |
| 3452 | * the buffer will also work with the bulk-out (and |
| 3453 | * interrupt-in) endpoint. */ |
David Brownell | 9d8bab5 | 2007-07-01 11:04:54 -0700 | [diff] [blame] | 3454 | bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | if (!bh->buf) |
| 3456 | goto out; |
| 3457 | bh->next = bh + 1; |
| 3458 | } |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3459 | fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | |
| 3461 | /* This should reflect the actual gadget power source */ |
| 3462 | usb_gadget_set_selfpowered(gadget); |
| 3463 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3464 | snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer, |
| 3465 | "%s %s with %s", |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 3466 | init_utsname()->sysname, init_utsname()->release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | gadget->name); |
| 3468 | |
Alan Stern | 22efcf4 | 2005-09-26 16:12:02 -0400 | [diff] [blame] | 3469 | fsg->thread_task = kthread_create(fsg_main_thread, fsg, |
| 3470 | "file-storage-gadget"); |
| 3471 | if (IS_ERR(fsg->thread_task)) { |
| 3472 | rc = PTR_ERR(fsg->thread_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | goto out; |
Alan Stern | 22efcf4 | 2005-09-26 16:12:02 -0400 | [diff] [blame] | 3474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | |
| 3476 | INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); |
Alan Stern | 664a51a | 2011-06-15 16:31:37 -0400 | [diff] [blame] | 3477 | INFO(fsg, "NOTE: This driver is deprecated. " |
| 3478 | "Consider using g_mass_storage instead.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | INFO(fsg, "Number of LUNs=%d\n", fsg->nluns); |
| 3480 | |
| 3481 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); |
| 3482 | for (i = 0; i < fsg->nluns; ++i) { |
| 3483 | curlun = &fsg->luns[i]; |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3484 | if (fsg_lun_is_open(curlun)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | p = NULL; |
| 3486 | if (pathbuf) { |
Jan Blunck | cf28b48 | 2008-02-14 19:38:44 -0800 | [diff] [blame] | 3487 | p = d_path(&curlun->filp->f_path, |
| 3488 | pathbuf, PATH_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | if (IS_ERR(p)) |
| 3490 | p = NULL; |
| 3491 | } |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 3492 | LINFO(curlun, "ro=%d, nofua=%d, file: %s\n", |
| 3493 | curlun->ro, curlun->nofua, (p ? p : "(error)")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | } |
| 3495 | } |
| 3496 | kfree(pathbuf); |
| 3497 | |
| 3498 | DBG(fsg, "transport=%s (x%02x)\n", |
| 3499 | mod_data.transport_name, mod_data.transport_type); |
| 3500 | DBG(fsg, "protocol=%s (x%02x)\n", |
| 3501 | mod_data.protocol_name, mod_data.protocol_type); |
| 3502 | DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n", |
| 3503 | mod_data.vendor, mod_data.product, mod_data.release); |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 3504 | DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | mod_data.removable, mod_data.can_stall, |
Alan Stern | 12aae68 | 2008-11-20 14:13:12 -0500 | [diff] [blame] | 3506 | mod_data.cdrom, mod_data.buflen); |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3507 | DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task)); |
Alan Stern | a922c68 | 2005-10-06 16:38:45 -0400 | [diff] [blame] | 3508 | |
| 3509 | set_bit(REGISTERED, &fsg->atomic_bitflags); |
| 3510 | |
| 3511 | /* Tell the thread to start working */ |
| 3512 | wake_up_process(fsg->thread_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | return 0; |
| 3514 | |
| 3515 | autoconf_fail: |
| 3516 | ERROR(fsg, "unable to autoconfigure all endpoints\n"); |
| 3517 | rc = -ENOTSUPP; |
| 3518 | |
| 3519 | out: |
| 3520 | fsg->state = FSG_STATE_TERMINATED; // The thread is dead |
| 3521 | fsg_unbind(gadget); |
Felipe Balbi | 889394b | 2008-12-08 13:50:27 +0200 | [diff] [blame] | 3522 | complete(&fsg->thread_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | return rc; |
| 3524 | } |
| 3525 | |
| 3526 | |
| 3527 | /*-------------------------------------------------------------------------*/ |
| 3528 | |
| 3529 | static void fsg_suspend(struct usb_gadget *gadget) |
| 3530 | { |
| 3531 | struct fsg_dev *fsg = get_gadget_data(gadget); |
| 3532 | |
| 3533 | DBG(fsg, "suspend\n"); |
| 3534 | set_bit(SUSPENDED, &fsg->atomic_bitflags); |
| 3535 | } |
| 3536 | |
| 3537 | static void fsg_resume(struct usb_gadget *gadget) |
| 3538 | { |
| 3539 | struct fsg_dev *fsg = get_gadget_data(gadget); |
| 3540 | |
| 3541 | DBG(fsg, "resume\n"); |
| 3542 | clear_bit(SUSPENDED, &fsg->atomic_bitflags); |
| 3543 | } |
| 3544 | |
| 3545 | |
| 3546 | /*-------------------------------------------------------------------------*/ |
| 3547 | |
| 3548 | static struct usb_gadget_driver fsg_driver = { |
| 3549 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
| 3550 | .speed = USB_SPEED_HIGH, |
| 3551 | #else |
| 3552 | .speed = USB_SPEED_FULL, |
| 3553 | #endif |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3554 | .function = (char *) fsg_string_product, |
David Brownell | 6bea476 | 2006-12-05 03:15:33 -0800 | [diff] [blame] | 3555 | .unbind = fsg_unbind, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | .disconnect = fsg_disconnect, |
| 3557 | .setup = fsg_setup, |
| 3558 | .suspend = fsg_suspend, |
| 3559 | .resume = fsg_resume, |
| 3560 | |
| 3561 | .driver = { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 3562 | .name = DRIVER_NAME, |
Ben Dooks | d0d5049 | 2005-10-10 10:52:33 +0100 | [diff] [blame] | 3563 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | // .release = ... |
| 3565 | // .suspend = ... |
| 3566 | // .resume = ... |
| 3567 | }, |
| 3568 | }; |
| 3569 | |
| 3570 | |
| 3571 | static int __init fsg_alloc(void) |
| 3572 | { |
| 3573 | struct fsg_dev *fsg; |
| 3574 | |
Alan Stern | a922c68 | 2005-10-06 16:38:45 -0400 | [diff] [blame] | 3575 | fsg = kzalloc(sizeof *fsg, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | if (!fsg) |
| 3577 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | spin_lock_init(&fsg->lock); |
| 3579 | init_rwsem(&fsg->filesem); |
Alan Stern | 87c4252 | 2005-11-09 16:59:56 -0500 | [diff] [blame] | 3580 | kref_init(&fsg->ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | init_completion(&fsg->thread_notifier); |
| 3582 | |
| 3583 | the_fsg = fsg; |
| 3584 | return 0; |
| 3585 | } |
| 3586 | |
| 3587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | static int __init fsg_init(void) |
| 3589 | { |
| 3590 | int rc; |
| 3591 | struct fsg_dev *fsg; |
| 3592 | |
| 3593 | if ((rc = fsg_alloc()) != 0) |
| 3594 | return rc; |
| 3595 | fsg = the_fsg; |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 3596 | if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0) |
Alan Stern | 87c4252 | 2005-11-09 16:59:56 -0500 | [diff] [blame] | 3597 | kref_put(&fsg->ref, fsg_release); |
Alan Stern | a922c68 | 2005-10-06 16:38:45 -0400 | [diff] [blame] | 3598 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | } |
| 3600 | module_init(fsg_init); |
| 3601 | |
| 3602 | |
| 3603 | static void __exit fsg_cleanup(void) |
| 3604 | { |
| 3605 | struct fsg_dev *fsg = the_fsg; |
| 3606 | |
| 3607 | /* Unregister the driver iff the thread hasn't already done so */ |
| 3608 | if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags)) |
| 3609 | usb_gadget_unregister_driver(&fsg_driver); |
| 3610 | |
| 3611 | /* Wait for the thread to finish up */ |
| 3612 | wait_for_completion(&fsg->thread_notifier); |
| 3613 | |
Alan Stern | 87c4252 | 2005-11-09 16:59:56 -0500 | [diff] [blame] | 3614 | kref_put(&fsg->ref, fsg_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | } |
| 3616 | module_exit(fsg_cleanup); |