blob: f866b7e72a285c1c8b85f11d9d0d236cb4018c99 [file] [log] [blame]
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001/*
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002 * f_mass_storage.c -- Mass Storage USB Composite Function
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003 *
4 * Copyright (C) 2003-2008 Alan Stern
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01005 * Copyright (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01007 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") as published by the Free Software
24 * Foundation, either version 2 of that License or (at your option) any
25 * later version.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40
41/*
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010042 * The Mass Storage Function acts as a USB Mass Storage device,
43 * appearing to the host as a disk drive or as a CD-ROM drive. In
44 * addition to providing an example of a genuinely useful composite
45 * function for a USB device, it also illustrates a technique of
46 * double-buffering for increased throughput.
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +010047 *
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010048 * Function supports multiple logical units (LUNs). Backing storage
49 * for each LUN is provided by a regular file or a block device.
50 * Access for each LUN can be limited to read-only. Moreover, the
51 * function can indicate that LUN is removable and/or CD-ROM. (The
52 * later implies read-only access.)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +010053 *
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010054 * MSF is configured by specifying a fsg_config structure. It has the
55 * following fields:
56 *
57 * nluns Number of LUNs function have (anywhere from 1
58 * to FSG_MAX_LUNS which is 8).
59 * luns An array of LUN configuration values. This
60 * should be filled for each LUN that
61 * function will include (ie. for "nluns"
62 * LUNs). Each element of the array has
63 * the following fields:
64 * ->filename The path to the backing file for the LUN.
65 * Required if LUN is not marked as
66 * removable.
67 * ->ro Flag specifying access to the LUN shall be
68 * read-only. This is implied if CD-ROM
69 * emulation is enabled as well as when
70 * it was impossible to open "filename"
71 * in R/W mode.
72 * ->removable Flag specifying that LUN shall be indicated as
73 * being removable.
74 * ->cdrom Flag specifying that LUN shall be reported as
75 * being a CD-ROM.
76 *
77 * lun_name_format A printf-like format for names of the LUN
78 * devices. This determines how the
79 * directory in sysfs will be named.
80 * Unless you are using several MSFs in
81 * a single gadget (as opposed to single
82 * MSF in many configurations) you may
83 * leave it as NULL (in which case
84 * "lun%d" will be used). In the format
85 * you can use "%d" to index LUNs for
86 * MSF's with more than one LUN. (Beware
87 * that there is only one integer given
88 * as an argument for the format and
89 * specifying invalid format may cause
90 * unspecified behaviour.)
91 * thread_name Name of the kernel thread process used by the
92 * MSF. You can safely set it to NULL
93 * (in which case default "file-storage"
94 * will be used).
95 *
96 * vendor_name
97 * product_name
98 * release Information used as a reply to INQUIRY
99 * request. To use default set to NULL,
100 * NULL, 0xffff respectively. The first
101 * field should be 8 and the second 16
102 * characters or less.
103 *
104 * can_stall Set to permit function to halt bulk endpoints.
105 * Disabled on some USB devices known not
106 * to work correctly. You should set it
107 * to true.
108 *
109 * If "removable" is not set for a LUN then a backing file must be
110 * specified. If it is set, then NULL filename means the LUN's medium
111 * is not loaded (an empty string as "filename" in the fsg_config
112 * structure causes error). The CD-ROM emulation includes a single
113 * data track and no audio tracks; hence there need be only one
114 * backing file per LUN. Note also that the CD-ROM block length is
115 * set to 512 rather than the more common value 2048.
116 *
117 *
118 * MSF includes support for module parameters. If gadget using it
119 * decides to use it, the following module parameters will be
120 * available:
121 *
122 * file=filename[,filename...]
123 * Names of the files or block devices used for
124 * backing storage.
125 * ro=b[,b...] Default false, boolean for read-only access.
126 * removable=b[,b...]
127 * Default true, boolean for removable media.
128 * cdrom=b[,b...] Default false, boolean for whether to emulate
129 * a CD-ROM drive.
130 * luns=N Default N = number of filenames, number of
131 * LUNs to support.
132 * stall Default determined according to the type of
133 * USB device controller (usually true),
134 * boolean to permit the driver to halt
135 * bulk endpoints.
136 *
137 * The module parameters may be prefixed with some string. You need
138 * to consult gadget's documentation or source to verify whether it is
139 * using those module parameters and if it does what are the prefixes
140 * (look for FSG_MODULE_PARAMETERS() macro usage, what's inside it is
141 * the prefix).
142 *
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100143 *
144 * Requirements are modest; only a bulk-in and a bulk-out endpoint are
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100145 * needed. The memory requirement amounts to two 16K buffers, size
146 * configurable by a parameter. Support is included for both
147 * full-speed and high-speed operation.
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100148 *
149 * Note that the driver is slightly non-portable in that it assumes a
150 * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
151 * interrupt-in endpoints. With most device controllers this isn't an
152 * issue, but there may be some with hardware restrictions that prevent
153 * a buffer from being used by more than one endpoint.
154 *
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100155 *
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100156 * The pathnames of the backing files and the ro settings are
157 * available in the attribute files "file" and "ro" in the lun<n> (or
158 * to be more precise in a directory which name comes from
159 * "lun_name_format" option!) subdirectory of the gadget's sysfs
160 * directory. If the "removable" option is set, writing to these
161 * files will simulate ejecting/loading the medium (writing an empty
162 * line means eject) and adjusting a write-enable tab. Changes to the
163 * ro setting are not allowed when the medium is loaded or if CD-ROM
164 * emulation is being used.
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100165 *
Fabien Chouteau31436a12010-04-26 12:34:54 +0200166 * When a LUN receive an "eject" SCSI request (Start/Stop Unit),
167 * if the LUN is removable, the backing file is released to simulate
168 * ejection.
169 *
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100170 *
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100171 * This function is heavily based on "File-backed Storage Gadget" by
172 * Alan Stern which in turn is heavily based on "Gadget Zero" by David
173 * Brownell. The driver's SCSI command interface was based on the
174 * "Information technology - Small Computer System Interface - 2"
175 * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
176 * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
177 * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
178 * was based on the "Universal Serial Bus Mass Storage Class UFI
179 * Command Specification" document, Revision 1.0, December 14, 1998,
180 * available at
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100181 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
182 */
183
184
185/*
186 * Driver Design
187 *
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100188 * The MSF is fairly straightforward. There is a main kernel
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100189 * thread that handles most of the work. Interrupt routines field
190 * callbacks from the controller driver: bulk- and interrupt-request
191 * completion notifications, endpoint-0 events, and disconnect events.
192 * Completion events are passed to the main thread by wakeup calls. Many
193 * ep0 requests are handled at interrupt time, but SetInterface,
194 * SetConfiguration, and device reset requests are forwarded to the
195 * thread in the form of "exceptions" using SIGUSR1 signals (since they
196 * should interrupt any ongoing file I/O operations).
197 *
198 * The thread's main routine implements the standard command/data/status
199 * parts of a SCSI interaction. It and its subroutines are full of tests
200 * for pending signals/exceptions -- all this polling is necessary since
201 * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
202 * indication that the driver really wants to be running in userspace.)
203 * An important point is that so long as the thread is alive it keeps an
204 * open reference to the backing file. This will prevent unmounting
205 * the backing file's underlying filesystem and could cause problems
206 * during system shutdown, for example. To prevent such problems, the
207 * thread catches INT, TERM, and KILL signals and converts them into
208 * an EXIT exception.
209 *
210 * In normal operation the main thread is started during the gadget's
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100211 * fsg_bind() callback and stopped during fsg_unbind(). But it can
212 * also exit when it receives a signal, and there's no point leaving
213 * the gadget running when the thread is dead. At of this moment, MSF
214 * provides no way to deregister the gadget when thread dies -- maybe
215 * a callback functions is needed.
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100216 *
217 * To provide maximum throughput, the driver uses a circular pipeline of
218 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
219 * arbitrarily long; in practice the benefits don't justify having more
220 * than 2 stages (i.e., double buffering). But it helps to think of the
221 * pipeline as being a long one. Each buffer head contains a bulk-in and
222 * a bulk-out request pointer (since the buffer can be used for both
223 * output and input -- directions always are given from the host's
224 * point of view) as well as a pointer to the buffer and various state
225 * variables.
226 *
227 * Use of the pipeline follows a simple protocol. There is a variable
228 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
229 * At any time that buffer head may still be in use from an earlier
230 * request, so each buffer head has a state variable indicating whether
231 * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
232 * buffer head to be EMPTY, filling the buffer either by file I/O or by
233 * USB I/O (during which the buffer head is BUSY), and marking the buffer
234 * head FULL when the I/O is complete. Then the buffer will be emptied
235 * (again possibly by USB I/O, during which it is marked BUSY) and
236 * finally marked EMPTY again (possibly by a completion routine).
237 *
238 * A module parameter tells the driver to avoid stalling the bulk
239 * endpoints wherever the transport specification allows. This is
240 * necessary for some UDCs like the SuperH, which cannot reliably clear a
241 * halt on a bulk endpoint. However, under certain circumstances the
242 * Bulk-only specification requires a stall. In such cases the driver
243 * will halt the endpoint and set a flag indicating that it should clear
244 * the halt in software during the next device reset. Hopefully this
245 * will permit everything to work correctly. Furthermore, although the
246 * specification allows the bulk-out endpoint to halt when the host sends
247 * too much data, implementing this would cause an unavoidable race.
248 * The driver will always use the "no-stall" approach for OUT transfers.
249 *
250 * One subtle point concerns sending status-stage responses for ep0
251 * requests. Some of these requests, such as device reset, can involve
252 * interrupting an ongoing file I/O operation, which might take an
253 * arbitrarily long time. During that delay the host might give up on
254 * the original ep0 request and issue a new one. When that happens the
255 * driver should not notify the host about completion of the original
256 * request, as the host will no longer be waiting for it. So the driver
257 * assigns to each ep0 request a unique tag, and it keeps track of the
258 * tag value of the request associated with a long-running exception
259 * (device-reset, interface-change, or configuration-change). When the
260 * exception handler is finished, the status-stage response is submitted
261 * only if the current ep0 request tag is equal to the exception request
262 * tag. Thus only the most recently received ep0 request will get a
263 * status-stage response.
264 *
265 * Warning: This driver source file is too long. It ought to be split up
266 * into a header file plus about 3 separate .c files, to handle the details
267 * of the Gadget, USB Mass Storage, and SCSI protocols.
268 */
269
270
271/* #define VERBOSE_DEBUG */
272/* #define DUMP_MSGS */
273
274
275#include <linux/blkdev.h>
276#include <linux/completion.h>
277#include <linux/dcache.h>
278#include <linux/delay.h>
279#include <linux/device.h>
280#include <linux/fcntl.h>
281#include <linux/file.h>
282#include <linux/fs.h>
283#include <linux/kref.h>
284#include <linux/kthread.h>
285#include <linux/limits.h>
286#include <linux/rwsem.h>
287#include <linux/slab.h>
288#include <linux/spinlock.h>
289#include <linux/string.h>
290#include <linux/freezer.h>
291#include <linux/utsname.h>
292
293#include <linux/usb/ch9.h>
294#include <linux/usb/gadget.h>
295
296#include "gadget_chips.h"
297
298
299
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100300/*------------------------------------------------------------------------*/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100301
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100302#define FSG_DRIVER_DESC "Mass Storage Function"
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100303#define FSG_DRIVER_VERSION "2009/09/11"
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100304
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100305static const char fsg_string_interface[] = "Mass Storage";
306
307
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100308#define FSG_NO_INTR_EP 1
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100309#define FSG_NO_DEVICE_STRINGS 1
310#define FSG_NO_OTG 1
311#define FSG_NO_INTR_EP 1
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100312
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100313#include "storage_common.c"
314
315
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100316/*-------------------------------------------------------------------------*/
317
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100318struct fsg_dev;
319
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100320
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100321/* Data shared by all the FSG instances. */
322struct fsg_common {
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100323 struct usb_gadget *gadget;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100324 struct fsg_dev *fsg;
325 struct fsg_dev *prev_fsg;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100326
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100327 /* filesem protects: backing files in use */
328 struct rw_semaphore filesem;
329
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100330 /* lock protects: state, all the req_busy's */
331 spinlock_t lock;
332
333 struct usb_ep *ep0; /* Copy of gadget->ep0 */
334 struct usb_request *ep0req; /* Copy of cdev->req */
335 unsigned int ep0_req_tag;
336 const char *ep0req_name;
337
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100338 struct fsg_buffhd *next_buffhd_to_fill;
339 struct fsg_buffhd *next_buffhd_to_drain;
340 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
341
342 int cmnd_size;
343 u8 cmnd[MAX_COMMAND_SIZE];
344
345 unsigned int nluns;
346 unsigned int lun;
347 struct fsg_lun *luns;
348 struct fsg_lun *curlun;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100349
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100350 unsigned int bulk_out_maxpacket;
351 enum fsg_state state; /* For exception handling */
352 unsigned int exception_req_tag;
353
354 u8 config, new_config;
355 enum data_direction data_dir;
356 u32 data_size;
357 u32 data_size_from_cmnd;
358 u32 tag;
359 u32 residue;
360 u32 usb_amount_left;
361
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100362 unsigned int can_stall:1;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100363 unsigned int free_storage_on_release:1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100364 unsigned int phase_error:1;
365 unsigned int short_packet_received:1;
366 unsigned int bad_lun_okay:1;
367 unsigned int running:1;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100368
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100369 int thread_wakeup_needed;
370 struct completion thread_notifier;
371 struct task_struct *thread_task;
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100372
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100373 /* Callback function to call when thread exits. */
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +0100374 int (*thread_exits)(struct fsg_common *common);
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100375 /* Gadget's private data. */
376 void *private_data;
377
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100378 /* Vendor (8 chars), product (16 chars), release (4
379 * hexadecimal digits) and NUL byte */
380 char inquiry_string[8 + 16 + 4 + 1];
381
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100382 struct kref ref;
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100383};
384
385
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100386struct fsg_config {
387 unsigned nluns;
388 struct fsg_lun_config {
389 const char *filename;
390 char ro;
391 char removable;
392 char cdrom;
393 } luns[FSG_MAX_LUNS];
394
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100395 const char *lun_name_format;
396 const char *thread_name;
397
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +0100398 /* Callback function to call when thread exits. If no
399 * callback is set or it returns value lower then zero MSF
400 * will force eject all LUNs it operates on (including those
401 * marked as non-removable or with prevent_medium_removal flag
402 * set). */
403 int (*thread_exits)(struct fsg_common *common);
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100404 /* Gadget's private data. */
405 void *private_data;
406
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100407 const char *vendor_name; /* 8 characters or less */
408 const char *product_name; /* 16 characters or less */
409 u16 release;
410
411 char can_stall;
412};
413
414
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100415struct fsg_dev {
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100416 struct usb_function function;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100417 struct usb_gadget *gadget; /* Copy of cdev->gadget */
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100418 struct fsg_common *common;
419
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100420 u16 interface_number;
421
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100422 unsigned int bulk_in_enabled:1;
423 unsigned int bulk_out_enabled:1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100424
425 unsigned long atomic_bitflags;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100426#define IGNORE_BULK_OUT 0
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100427
428 struct usb_ep *bulk_in;
429 struct usb_ep *bulk_out;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100430};
431
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100432
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100433static inline int __fsg_is_set(struct fsg_common *common,
434 const char *func, unsigned line)
435{
436 if (common->fsg)
437 return 1;
438 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
439 return 0;
440}
441
442#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
443
444
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100445static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
446{
447 return container_of(f, struct fsg_dev, function);
448}
449
450
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100451typedef void (*fsg_routine_t)(struct fsg_dev *);
452
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100453static int exception_in_progress(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100454{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100455 return common->state > FSG_STATE_IDLE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100456}
457
458/* Make bulk-out requests be divisible by the maxpacket size */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100459static void set_bulk_out_req_length(struct fsg_common *common,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100460 struct fsg_buffhd *bh, unsigned int length)
461{
462 unsigned int rem;
463
464 bh->bulk_out_intended_length = length;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100465 rem = length % common->bulk_out_maxpacket;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100466 if (rem > 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100467 length += common->bulk_out_maxpacket - rem;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100468 bh->outreq->length = length;
469}
470
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100471/*-------------------------------------------------------------------------*/
472
473static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
474{
475 const char *name;
476
477 if (ep == fsg->bulk_in)
478 name = "bulk-in";
479 else if (ep == fsg->bulk_out)
480 name = "bulk-out";
481 else
482 name = ep->name;
483 DBG(fsg, "%s set halt\n", name);
484 return usb_ep_set_halt(ep);
485}
486
487
488/*-------------------------------------------------------------------------*/
489
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100490/* These routines may be called in process context or in_irq */
491
492/* Caller must hold fsg->lock */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100493static void wakeup_thread(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100494{
495 /* Tell the main thread that something has happened */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100496 common->thread_wakeup_needed = 1;
497 if (common->thread_task)
498 wake_up_process(common->thread_task);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100499}
500
501
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100502static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100503{
504 unsigned long flags;
505
506 /* Do nothing if a higher-priority exception is already in progress.
507 * If a lower-or-equal priority exception is in progress, preempt it
508 * and notify the main thread by sending it a signal. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100509 spin_lock_irqsave(&common->lock, flags);
510 if (common->state <= new_state) {
511 common->exception_req_tag = common->ep0_req_tag;
512 common->state = new_state;
513 if (common->thread_task)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100514 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100515 common->thread_task);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100516 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100517 spin_unlock_irqrestore(&common->lock, flags);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100518}
519
520
521/*-------------------------------------------------------------------------*/
522
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100523static int ep0_queue(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100524{
525 int rc;
526
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100527 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
528 common->ep0->driver_data = common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100529 if (rc != 0 && rc != -ESHUTDOWN) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100530 /* We can't do much more than wait for a reset */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100531 WARNING(common, "error in submission: %s --> %d\n",
532 common->ep0->name, rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100533 }
534 return rc;
535}
536
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100537/*-------------------------------------------------------------------------*/
538
539/* Bulk and interrupt endpoint completion handlers.
540 * These always run in_irq. */
541
542static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
543{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100544 struct fsg_common *common = ep->driver_data;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100545 struct fsg_buffhd *bh = req->context;
546
547 if (req->status || req->actual != req->length)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100548 DBG(common, "%s --> %d, %u/%u\n", __func__,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100549 req->status, req->actual, req->length);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100550 if (req->status == -ECONNRESET) /* Request was cancelled */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100551 usb_ep_fifo_flush(ep);
552
553 /* Hold the lock while we update the request and buffer states */
554 smp_wmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100555 spin_lock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100556 bh->inreq_busy = 0;
557 bh->state = BUF_STATE_EMPTY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100558 wakeup_thread(common);
559 spin_unlock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100560}
561
562static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
563{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100564 struct fsg_common *common = ep->driver_data;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100565 struct fsg_buffhd *bh = req->context;
566
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100567 dump_msg(common, "bulk-out", req->buf, req->actual);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100568 if (req->status || req->actual != bh->bulk_out_intended_length)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100569 DBG(common, "%s --> %d, %u/%u\n", __func__,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100570 req->status, req->actual,
571 bh->bulk_out_intended_length);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100572 if (req->status == -ECONNRESET) /* Request was cancelled */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100573 usb_ep_fifo_flush(ep);
574
575 /* Hold the lock while we update the request and buffer states */
576 smp_wmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100577 spin_lock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100578 bh->outreq_busy = 0;
579 bh->state = BUF_STATE_FULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100580 wakeup_thread(common);
581 spin_unlock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100582}
583
584
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100585/*-------------------------------------------------------------------------*/
586
587/* Ep0 class-specific handlers. These always run in_irq. */
588
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100589static int fsg_setup(struct usb_function *f,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100590 const struct usb_ctrlrequest *ctrl)
591{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100592 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100593 struct usb_request *req = fsg->common->ep0req;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100594 u16 w_index = le16_to_cpu(ctrl->wIndex);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100595 u16 w_value = le16_to_cpu(ctrl->wValue);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100596 u16 w_length = le16_to_cpu(ctrl->wLength);
597
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100598 if (!fsg->common->config)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100599 return -EOPNOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100600
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100601 switch (ctrl->bRequest) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100602
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100603 case USB_BULK_RESET_REQUEST:
604 if (ctrl->bRequestType !=
605 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100606 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100607 if (w_index != fsg->interface_number || w_value != 0)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100608 return -EDOM;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100609
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100610 /* Raise an exception to stop the current operation
611 * and reinitialize our state. */
612 DBG(fsg, "bulk reset request\n");
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100613 raise_exception(fsg->common, FSG_STATE_RESET);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100614 return DELAYED_STATUS;
615
616 case USB_BULK_GET_MAX_LUN_REQUEST:
617 if (ctrl->bRequestType !=
618 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100619 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100620 if (w_index != fsg->interface_number || w_value != 0)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100621 return -EDOM;
622 VDBG(fsg, "get max LUN\n");
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100623 *(u8 *) req->buf = fsg->common->nluns - 1;
Michal Nazarewiczb00ce112010-01-27 11:14:28 +0100624
625 /* Respond with data/status */
Michal Nazarewiczd7e18a92010-02-03 11:37:17 +0100626 req->length = min((u16)1, w_length);
Michal Nazarewiczb00ce112010-01-27 11:14:28 +0100627 fsg->common->ep0req_name =
628 ctrl->bRequestType & USB_DIR_IN ? "ep0-in" : "ep0-out";
629 return ep0_queue(fsg->common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100630 }
631
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100632 VDBG(fsg,
633 "unknown class-specific control req "
634 "%02x.%02x v%04x i%04x l%u\n",
635 ctrl->bRequestType, ctrl->bRequest,
636 le16_to_cpu(ctrl->wValue), w_index, w_length);
637 return -EOPNOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100638}
639
640
641/*-------------------------------------------------------------------------*/
642
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100643/* All the following routines run in process context */
644
645
646/* Use this for bulk or interrupt transfers, not ep0 */
647static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
648 struct usb_request *req, int *pbusy,
649 enum fsg_buffer_state *state)
650{
651 int rc;
652
653 if (ep == fsg->bulk_in)
654 dump_msg(fsg, "bulk-in", req->buf, req->length);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100655
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100656 spin_lock_irq(&fsg->common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100657 *pbusy = 1;
658 *state = BUF_STATE_BUSY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100659 spin_unlock_irq(&fsg->common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100660 rc = usb_ep_queue(ep, req, GFP_KERNEL);
661 if (rc != 0) {
662 *pbusy = 0;
663 *state = BUF_STATE_EMPTY;
664
665 /* We can't do much more than wait for a reset */
666
667 /* Note: currently the net2280 driver fails zero-length
668 * submissions if DMA is enabled. */
669 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
670 req->length == 0))
671 WARNING(fsg, "error in submission: %s --> %d\n",
672 ep->name, rc);
673 }
674}
675
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100676#define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \
677 if (fsg_is_set(common)) \
678 start_transfer((common)->fsg, (common)->fsg->ep_name, \
679 req, pbusy, state); \
680 else
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100681
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100682#define START_TRANSFER(common, ep_name, req, pbusy, state) \
683 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
684
685
686
687static int sleep_thread(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100688{
689 int rc = 0;
690
691 /* Wait until a signal arrives or we are woken up */
692 for (;;) {
693 try_to_freeze();
694 set_current_state(TASK_INTERRUPTIBLE);
695 if (signal_pending(current)) {
696 rc = -EINTR;
697 break;
698 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100699 if (common->thread_wakeup_needed)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100700 break;
701 schedule();
702 }
703 __set_current_state(TASK_RUNNING);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100704 common->thread_wakeup_needed = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100705 return rc;
706}
707
708
709/*-------------------------------------------------------------------------*/
710
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100711static int do_read(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100712{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100713 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100714 u32 lba;
715 struct fsg_buffhd *bh;
716 int rc;
717 u32 amount_left;
718 loff_t file_offset, file_offset_tmp;
719 unsigned int amount;
720 unsigned int partial_page;
721 ssize_t nread;
722
723 /* Get the starting Logical Block Address and check that it's
724 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100725 if (common->cmnd[0] == SC_READ_6)
726 lba = get_unaligned_be24(&common->cmnd[1]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100727 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100728 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100729
730 /* We allow DPO (Disable Page Out = don't save data in the
731 * cache) and FUA (Force Unit Access = don't read from the
732 * cache), but we don't implement them. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100733 if ((common->cmnd[1] & ~0x18) != 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100734 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
735 return -EINVAL;
736 }
737 }
738 if (lba >= curlun->num_sectors) {
739 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
740 return -EINVAL;
741 }
742 file_offset = ((loff_t) lba) << 9;
743
744 /* Carry out the file reads */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100745 amount_left = common->data_size_from_cmnd;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100746 if (unlikely(amount_left == 0))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100747 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100748
749 for (;;) {
750
751 /* Figure out how much we need to read:
752 * Try to read the remaining amount.
753 * But don't read more than the buffer size.
754 * And don't try to read past the end of the file.
755 * Finally, if we're not at a page boundary, don't read past
756 * the next page.
757 * If this means reading 0 then we were asked to read past
758 * the end of file. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100759 amount = min(amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100760 amount = min((loff_t) amount,
761 curlun->file_length - file_offset);
762 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
763 if (partial_page > 0)
764 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
765 partial_page);
766
767 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100768 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100769 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100770 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100771 if (rc)
772 return rc;
773 }
774
775 /* If we were asked to read past the end of file,
776 * end with an empty buffer. */
777 if (amount == 0) {
778 curlun->sense_data =
779 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
780 curlun->sense_data_info = file_offset >> 9;
781 curlun->info_valid = 1;
782 bh->inreq->length = 0;
783 bh->state = BUF_STATE_FULL;
784 break;
785 }
786
787 /* Perform the read */
788 file_offset_tmp = file_offset;
789 nread = vfs_read(curlun->filp,
790 (char __user *) bh->buf,
791 amount, &file_offset_tmp);
792 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
793 (unsigned long long) file_offset,
794 (int) nread);
795 if (signal_pending(current))
796 return -EINTR;
797
798 if (nread < 0) {
799 LDBG(curlun, "error in file read: %d\n",
800 (int) nread);
801 nread = 0;
802 } else if (nread < amount) {
803 LDBG(curlun, "partial file read: %d/%u\n",
804 (int) nread, amount);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100805 nread -= (nread & 511); /* Round down to a block */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100806 }
807 file_offset += nread;
808 amount_left -= nread;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100809 common->residue -= nread;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100810 bh->inreq->length = nread;
811 bh->state = BUF_STATE_FULL;
812
813 /* If an error occurred, report it and its position */
814 if (nread < amount) {
815 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
816 curlun->sense_data_info = file_offset >> 9;
817 curlun->info_valid = 1;
818 break;
819 }
820
821 if (amount_left == 0)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100822 break; /* No more left to read */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100823
824 /* Send this buffer and go read some more */
825 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100826 START_TRANSFER_OR(common, bulk_in, bh->inreq,
827 &bh->inreq_busy, &bh->state)
828 /* Don't know what to do if
829 * common->fsg is NULL */
830 return -EIO;
831 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100832 }
833
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100834 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100835}
836
837
838/*-------------------------------------------------------------------------*/
839
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100840static int do_write(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100841{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100842 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100843 u32 lba;
844 struct fsg_buffhd *bh;
845 int get_some_more;
846 u32 amount_left_to_req, amount_left_to_write;
847 loff_t usb_offset, file_offset, file_offset_tmp;
848 unsigned int amount;
849 unsigned int partial_page;
850 ssize_t nwritten;
851 int rc;
852
853 if (curlun->ro) {
854 curlun->sense_data = SS_WRITE_PROTECTED;
855 return -EINVAL;
856 }
857 spin_lock(&curlun->filp->f_lock);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100858 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100859 spin_unlock(&curlun->filp->f_lock);
860
861 /* Get the starting Logical Block Address and check that it's
862 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100863 if (common->cmnd[0] == SC_WRITE_6)
864 lba = get_unaligned_be24(&common->cmnd[1]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100865 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100866 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100867
868 /* We allow DPO (Disable Page Out = don't save data in the
869 * cache) and FUA (Force Unit Access = write directly to the
870 * medium). We don't implement DPO; we implement FUA by
871 * performing synchronous output. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100872 if (common->cmnd[1] & ~0x18) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100873 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
874 return -EINVAL;
875 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100876 if (common->cmnd[1] & 0x08) { /* FUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100877 spin_lock(&curlun->filp->f_lock);
878 curlun->filp->f_flags |= O_SYNC;
879 spin_unlock(&curlun->filp->f_lock);
880 }
881 }
882 if (lba >= curlun->num_sectors) {
883 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
884 return -EINVAL;
885 }
886
887 /* Carry out the file writes */
888 get_some_more = 1;
889 file_offset = usb_offset = ((loff_t) lba) << 9;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100890 amount_left_to_req = common->data_size_from_cmnd;
891 amount_left_to_write = common->data_size_from_cmnd;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100892
893 while (amount_left_to_write > 0) {
894
895 /* Queue a request for more data from the host */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100896 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100897 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
898
899 /* Figure out how much we want to get:
900 * Try to get the remaining amount.
901 * But don't get more than the buffer size.
902 * And don't try to go past the end of the file.
903 * If we're not at a page boundary,
904 * don't go past the next page.
905 * If this means getting 0, then we were asked
906 * to write past the end of file.
907 * Finally, round down to a block boundary. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100908 amount = min(amount_left_to_req, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100909 amount = min((loff_t) amount, curlun->file_length -
910 usb_offset);
911 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
912 if (partial_page > 0)
913 amount = min(amount,
914 (unsigned int) PAGE_CACHE_SIZE - partial_page);
915
916 if (amount == 0) {
917 get_some_more = 0;
918 curlun->sense_data =
919 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
920 curlun->sense_data_info = usb_offset >> 9;
921 curlun->info_valid = 1;
922 continue;
923 }
924 amount -= (amount & 511);
925 if (amount == 0) {
926
927 /* Why were we were asked to transfer a
928 * partial block? */
929 get_some_more = 0;
930 continue;
931 }
932
933 /* Get the next buffer */
934 usb_offset += amount;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100935 common->usb_amount_left -= amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100936 amount_left_to_req -= amount;
937 if (amount_left_to_req == 0)
938 get_some_more = 0;
939
940 /* amount is always divisible by 512, hence by
941 * the bulk-out maxpacket size */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100942 bh->outreq->length = amount;
943 bh->bulk_out_intended_length = amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100944 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100945 START_TRANSFER_OR(common, bulk_out, bh->outreq,
946 &bh->outreq_busy, &bh->state)
947 /* Don't know what to do if
948 * common->fsg is NULL */
949 return -EIO;
950 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100951 continue;
952 }
953
954 /* Write the received data to the backing file */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100955 bh = common->next_buffhd_to_drain;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100956 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100957 break; /* We stopped early */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100958 if (bh->state == BUF_STATE_FULL) {
959 smp_rmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100960 common->next_buffhd_to_drain = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100961 bh->state = BUF_STATE_EMPTY;
962
963 /* Did something go wrong with the transfer? */
964 if (bh->outreq->status != 0) {
965 curlun->sense_data = SS_COMMUNICATION_FAILURE;
966 curlun->sense_data_info = file_offset >> 9;
967 curlun->info_valid = 1;
968 break;
969 }
970
971 amount = bh->outreq->actual;
972 if (curlun->file_length - file_offset < amount) {
973 LERROR(curlun,
974 "write %u @ %llu beyond end %llu\n",
975 amount, (unsigned long long) file_offset,
976 (unsigned long long) curlun->file_length);
977 amount = curlun->file_length - file_offset;
978 }
979
980 /* Perform the write */
981 file_offset_tmp = file_offset;
982 nwritten = vfs_write(curlun->filp,
983 (char __user *) bh->buf,
984 amount, &file_offset_tmp);
985 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
986 (unsigned long long) file_offset,
987 (int) nwritten);
988 if (signal_pending(current))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100989 return -EINTR; /* Interrupted! */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100990
991 if (nwritten < 0) {
992 LDBG(curlun, "error in file write: %d\n",
993 (int) nwritten);
994 nwritten = 0;
995 } else if (nwritten < amount) {
996 LDBG(curlun, "partial file write: %d/%u\n",
997 (int) nwritten, amount);
998 nwritten -= (nwritten & 511);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100999 /* Round down to a block */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001000 }
1001 file_offset += nwritten;
1002 amount_left_to_write -= nwritten;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001003 common->residue -= nwritten;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001004
1005 /* If an error occurred, report it and its position */
1006 if (nwritten < amount) {
1007 curlun->sense_data = SS_WRITE_ERROR;
1008 curlun->sense_data_info = file_offset >> 9;
1009 curlun->info_valid = 1;
1010 break;
1011 }
1012
1013 /* Did the host decide to stop early? */
1014 if (bh->outreq->actual != bh->outreq->length) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001015 common->short_packet_received = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001016 break;
1017 }
1018 continue;
1019 }
1020
1021 /* Wait for something to happen */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001022 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001023 if (rc)
1024 return rc;
1025 }
1026
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001027 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001028}
1029
1030
1031/*-------------------------------------------------------------------------*/
1032
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001033static int do_synchronize_cache(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001034{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001035 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001036 int rc;
1037
1038 /* We ignore the requested LBA and write out all file's
1039 * dirty data buffers. */
1040 rc = fsg_lun_fsync_sub(curlun);
1041 if (rc)
1042 curlun->sense_data = SS_WRITE_ERROR;
1043 return 0;
1044}
1045
1046
1047/*-------------------------------------------------------------------------*/
1048
1049static void invalidate_sub(struct fsg_lun *curlun)
1050{
1051 struct file *filp = curlun->filp;
1052 struct inode *inode = filp->f_path.dentry->d_inode;
1053 unsigned long rc;
1054
1055 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001056 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001057}
1058
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001059static int do_verify(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001060{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001061 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001062 u32 lba;
1063 u32 verification_length;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001064 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001065 loff_t file_offset, file_offset_tmp;
1066 u32 amount_left;
1067 unsigned int amount;
1068 ssize_t nread;
1069
1070 /* Get the starting Logical Block Address and check that it's
1071 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001072 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001073 if (lba >= curlun->num_sectors) {
1074 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1075 return -EINVAL;
1076 }
1077
1078 /* We allow DPO (Disable Page Out = don't save data in the
1079 * cache) but we don't implement it. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001080 if (common->cmnd[1] & ~0x10) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001081 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1082 return -EINVAL;
1083 }
1084
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001085 verification_length = get_unaligned_be16(&common->cmnd[7]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001086 if (unlikely(verification_length == 0))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001087 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001088
1089 /* Prepare to carry out the file verify */
1090 amount_left = verification_length << 9;
1091 file_offset = ((loff_t) lba) << 9;
1092
1093 /* Write out all the dirty buffers before invalidating them */
1094 fsg_lun_fsync_sub(curlun);
1095 if (signal_pending(current))
1096 return -EINTR;
1097
1098 invalidate_sub(curlun);
1099 if (signal_pending(current))
1100 return -EINTR;
1101
1102 /* Just try to read the requested blocks */
1103 while (amount_left > 0) {
1104
1105 /* Figure out how much we need to read:
1106 * Try to read the remaining amount, but not more than
1107 * the buffer size.
1108 * And don't try to read past the end of the file.
1109 * If this means reading 0 then we were asked to read
1110 * past the end of file. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001111 amount = min(amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001112 amount = min((loff_t) amount,
1113 curlun->file_length - file_offset);
1114 if (amount == 0) {
1115 curlun->sense_data =
1116 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1117 curlun->sense_data_info = file_offset >> 9;
1118 curlun->info_valid = 1;
1119 break;
1120 }
1121
1122 /* Perform the read */
1123 file_offset_tmp = file_offset;
1124 nread = vfs_read(curlun->filp,
1125 (char __user *) bh->buf,
1126 amount, &file_offset_tmp);
1127 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1128 (unsigned long long) file_offset,
1129 (int) nread);
1130 if (signal_pending(current))
1131 return -EINTR;
1132
1133 if (nread < 0) {
1134 LDBG(curlun, "error in file verify: %d\n",
1135 (int) nread);
1136 nread = 0;
1137 } else if (nread < amount) {
1138 LDBG(curlun, "partial file verify: %d/%u\n",
1139 (int) nread, amount);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001140 nread -= (nread & 511); /* Round down to a sector */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001141 }
1142 if (nread == 0) {
1143 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1144 curlun->sense_data_info = file_offset >> 9;
1145 curlun->info_valid = 1;
1146 break;
1147 }
1148 file_offset += nread;
1149 amount_left -= nread;
1150 }
1151 return 0;
1152}
1153
1154
1155/*-------------------------------------------------------------------------*/
1156
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001157static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001158{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001159 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001160 u8 *buf = (u8 *) bh->buf;
1161
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001162 if (!curlun) { /* Unsupported LUNs are okay */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001163 common->bad_lun_okay = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001164 memset(buf, 0, 36);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001165 buf[0] = 0x7f; /* Unsupported, no device-type */
1166 buf[4] = 31; /* Additional length */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001167 return 36;
1168 }
1169
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001170 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK;
1171 buf[1] = curlun->removable ? 0x80 : 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001172 buf[2] = 2; /* ANSI SCSI level 2 */
1173 buf[3] = 2; /* SCSI-2 INQUIRY data format */
1174 buf[4] = 31; /* Additional length */
1175 buf[5] = 0; /* No special options */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001176 buf[6] = 0;
1177 buf[7] = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001178 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001179 return 36;
1180}
1181
1182
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001183static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001184{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001185 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001186 u8 *buf = (u8 *) bh->buf;
1187 u32 sd, sdinfo;
1188 int valid;
1189
1190 /*
1191 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1192 *
1193 * If a REQUEST SENSE command is received from an initiator
1194 * with a pending unit attention condition (before the target
1195 * generates the contingent allegiance condition), then the
1196 * target shall either:
1197 * a) report any pending sense data and preserve the unit
1198 * attention condition on the logical unit, or,
1199 * b) report the unit attention condition, may discard any
1200 * pending sense data, and clear the unit attention
1201 * condition on the logical unit for that initiator.
1202 *
1203 * FSG normally uses option a); enable this code to use option b).
1204 */
1205#if 0
1206 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1207 curlun->sense_data = curlun->unit_attention_data;
1208 curlun->unit_attention_data = SS_NO_SENSE;
1209 }
1210#endif
1211
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001212 if (!curlun) { /* Unsupported LUNs are okay */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001213 common->bad_lun_okay = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001214 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1215 sdinfo = 0;
1216 valid = 0;
1217 } else {
1218 sd = curlun->sense_data;
1219 sdinfo = curlun->sense_data_info;
1220 valid = curlun->info_valid << 7;
1221 curlun->sense_data = SS_NO_SENSE;
1222 curlun->sense_data_info = 0;
1223 curlun->info_valid = 0;
1224 }
1225
1226 memset(buf, 0, 18);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001227 buf[0] = valid | 0x70; /* Valid, current error */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001228 buf[2] = SK(sd);
1229 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001230 buf[7] = 18 - 8; /* Additional sense length */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001231 buf[12] = ASC(sd);
1232 buf[13] = ASCQ(sd);
1233 return 18;
1234}
1235
1236
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001237static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001238{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001239 struct fsg_lun *curlun = common->curlun;
1240 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1241 int pmi = common->cmnd[8];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001242 u8 *buf = (u8 *) bh->buf;
1243
1244 /* Check the PMI and LBA fields */
1245 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1246 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1247 return -EINVAL;
1248 }
1249
1250 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1251 /* Max logical block */
1252 put_unaligned_be32(512, &buf[4]); /* Block length */
1253 return 8;
1254}
1255
1256
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001257static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001258{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001259 struct fsg_lun *curlun = common->curlun;
1260 int msf = common->cmnd[1] & 0x02;
1261 u32 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001262 u8 *buf = (u8 *) bh->buf;
1263
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001264 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001265 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1266 return -EINVAL;
1267 }
1268 if (lba >= curlun->num_sectors) {
1269 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1270 return -EINVAL;
1271 }
1272
1273 memset(buf, 0, 8);
1274 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1275 store_cdrom_address(&buf[4], msf, lba);
1276 return 8;
1277}
1278
1279
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001280static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001281{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001282 struct fsg_lun *curlun = common->curlun;
1283 int msf = common->cmnd[1] & 0x02;
1284 int start_track = common->cmnd[6];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001285 u8 *buf = (u8 *) bh->buf;
1286
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001287 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001288 start_track > 1) {
1289 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1290 return -EINVAL;
1291 }
1292
1293 memset(buf, 0, 20);
1294 buf[1] = (20-2); /* TOC data length */
1295 buf[2] = 1; /* First track number */
1296 buf[3] = 1; /* Last track number */
1297 buf[5] = 0x16; /* Data track, copying allowed */
1298 buf[6] = 0x01; /* Only track is number 1 */
1299 store_cdrom_address(&buf[8], msf, 0);
1300
1301 buf[13] = 0x16; /* Lead-out track is data */
1302 buf[14] = 0xAA; /* Lead-out track number */
1303 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1304 return 20;
1305}
1306
1307
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001308static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001309{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001310 struct fsg_lun *curlun = common->curlun;
1311 int mscmnd = common->cmnd[0];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001312 u8 *buf = (u8 *) bh->buf;
1313 u8 *buf0 = buf;
1314 int pc, page_code;
1315 int changeable_values, all_pages;
1316 int valid_page = 0;
1317 int len, limit;
1318
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001319 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001320 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1321 return -EINVAL;
1322 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001323 pc = common->cmnd[2] >> 6;
1324 page_code = common->cmnd[2] & 0x3f;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001325 if (pc == 3) {
1326 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1327 return -EINVAL;
1328 }
1329 changeable_values = (pc == 1);
1330 all_pages = (page_code == 0x3f);
1331
1332 /* Write the mode parameter header. Fixed values are: default
1333 * medium type, no cache control (DPOFUA), and no block descriptors.
1334 * The only variable value is the WriteProtect bit. We will fill in
1335 * the mode data length later. */
1336 memset(buf, 0, 8);
1337 if (mscmnd == SC_MODE_SENSE_6) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001338 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001339 buf += 4;
1340 limit = 255;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001341 } else { /* SC_MODE_SENSE_10 */
1342 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001343 buf += 8;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001344 limit = 65535; /* Should really be FSG_BUFLEN */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001345 }
1346
1347 /* No block descriptors */
1348
1349 /* The mode pages, in numerical order. The only page we support
1350 * is the Caching page. */
1351 if (page_code == 0x08 || all_pages) {
1352 valid_page = 1;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001353 buf[0] = 0x08; /* Page code */
1354 buf[1] = 10; /* Page length */
1355 memset(buf+2, 0, 10); /* None of the fields are changeable */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001356
1357 if (!changeable_values) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001358 buf[2] = 0x04; /* Write cache enable, */
1359 /* Read cache not disabled */
1360 /* No cache retention priorities */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001361 put_unaligned_be16(0xffff, &buf[4]);
1362 /* Don't disable prefetch */
1363 /* Minimum prefetch = 0 */
1364 put_unaligned_be16(0xffff, &buf[8]);
1365 /* Maximum prefetch */
1366 put_unaligned_be16(0xffff, &buf[10]);
1367 /* Maximum prefetch ceiling */
1368 }
1369 buf += 12;
1370 }
1371
1372 /* Check that a valid page was requested and the mode data length
1373 * isn't too long. */
1374 len = buf - buf0;
1375 if (!valid_page || len > limit) {
1376 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1377 return -EINVAL;
1378 }
1379
1380 /* Store the mode data length */
1381 if (mscmnd == SC_MODE_SENSE_6)
1382 buf0[0] = len - 1;
1383 else
1384 put_unaligned_be16(len - 2, buf0);
1385 return len;
1386}
1387
1388
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001389static int do_start_stop(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001390{
Fabien Chouteau31436a12010-04-26 12:34:54 +02001391 struct fsg_lun *curlun = common->curlun;
1392 int loej, start;
1393
1394 if (!curlun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001395 return -EINVAL;
Fabien Chouteau31436a12010-04-26 12:34:54 +02001396 } else if (!curlun->removable) {
1397 curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001398 return -EINVAL;
1399 }
Fabien Chouteau31436a12010-04-26 12:34:54 +02001400
1401 loej = common->cmnd[4] & 0x02;
1402 start = common->cmnd[4] & 0x01;
1403
1404 /* eject code from file_storage.c:do_start_stop() */
1405
1406 if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
1407 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
1408 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1409 return -EINVAL;
1410 }
1411
1412 if (!start) {
1413 /* Are we allowed to unload the media? */
1414 if (curlun->prevent_medium_removal) {
1415 LDBG(curlun, "unload attempt prevented\n");
1416 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1417 return -EINVAL;
1418 }
1419 if (loej) { /* Simulate an unload/eject */
1420 up_read(&common->filesem);
1421 down_write(&common->filesem);
1422 fsg_lun_close(curlun);
1423 up_write(&common->filesem);
1424 down_read(&common->filesem);
1425 }
1426 } else {
1427
1428 /* Our emulation doesn't support mounting; the medium is
1429 * available for use as soon as it is loaded. */
1430 if (!fsg_lun_is_open(curlun)) {
1431 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1432 return -EINVAL;
1433 }
1434 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001435 return 0;
1436}
1437
1438
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001439static int do_prevent_allow(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001440{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001441 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001442 int prevent;
1443
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001444 if (!common->curlun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001445 return -EINVAL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001446 } else if (!common->curlun->removable) {
1447 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001448 return -EINVAL;
1449 }
1450
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001451 prevent = common->cmnd[4] & 0x01;
1452 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001453 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1454 return -EINVAL;
1455 }
1456
1457 if (curlun->prevent_medium_removal && !prevent)
1458 fsg_lun_fsync_sub(curlun);
1459 curlun->prevent_medium_removal = prevent;
1460 return 0;
1461}
1462
1463
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001464static int do_read_format_capacities(struct fsg_common *common,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001465 struct fsg_buffhd *bh)
1466{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001467 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001468 u8 *buf = (u8 *) bh->buf;
1469
1470 buf[0] = buf[1] = buf[2] = 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001471 buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001472 buf += 4;
1473
1474 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1475 /* Number of blocks */
1476 put_unaligned_be32(512, &buf[4]); /* Block length */
1477 buf[4] = 0x02; /* Current capacity */
1478 return 12;
1479}
1480
1481
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001482static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001483{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001484 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001485
1486 /* We don't support MODE SELECT */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001487 if (curlun)
1488 curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001489 return -EINVAL;
1490}
1491
1492
1493/*-------------------------------------------------------------------------*/
1494
1495static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1496{
1497 int rc;
1498
1499 rc = fsg_set_halt(fsg, fsg->bulk_in);
1500 if (rc == -EAGAIN)
1501 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1502 while (rc != 0) {
1503 if (rc != -EAGAIN) {
1504 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1505 rc = 0;
1506 break;
1507 }
1508
1509 /* Wait for a short time and then try again */
1510 if (msleep_interruptible(100) != 0)
1511 return -EINTR;
1512 rc = usb_ep_set_halt(fsg->bulk_in);
1513 }
1514 return rc;
1515}
1516
1517static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1518{
1519 int rc;
1520
1521 DBG(fsg, "bulk-in set wedge\n");
1522 rc = usb_ep_set_wedge(fsg->bulk_in);
1523 if (rc == -EAGAIN)
1524 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1525 while (rc != 0) {
1526 if (rc != -EAGAIN) {
1527 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1528 rc = 0;
1529 break;
1530 }
1531
1532 /* Wait for a short time and then try again */
1533 if (msleep_interruptible(100) != 0)
1534 return -EINTR;
1535 rc = usb_ep_set_wedge(fsg->bulk_in);
1536 }
1537 return rc;
1538}
1539
1540static int pad_with_zeros(struct fsg_dev *fsg)
1541{
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001542 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001543 u32 nkeep = bh->inreq->length;
1544 u32 nsend;
1545 int rc;
1546
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001547 bh->state = BUF_STATE_EMPTY; /* For the first iteration */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001548 fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1549 while (fsg->common->usb_amount_left > 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001550
1551 /* Wait for the next buffer to be free */
1552 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001553 rc = sleep_thread(fsg->common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001554 if (rc)
1555 return rc;
1556 }
1557
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001558 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001559 memset(bh->buf + nkeep, 0, nsend - nkeep);
1560 bh->inreq->length = nsend;
1561 bh->inreq->zero = 0;
1562 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1563 &bh->inreq_busy, &bh->state);
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001564 bh = fsg->common->next_buffhd_to_fill = bh->next;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001565 fsg->common->usb_amount_left -= nsend;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001566 nkeep = 0;
1567 }
1568 return 0;
1569}
1570
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001571static int throw_away_data(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001572{
1573 struct fsg_buffhd *bh;
1574 u32 amount;
1575 int rc;
1576
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001577 for (bh = common->next_buffhd_to_drain;
1578 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1579 bh = common->next_buffhd_to_drain) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001580
1581 /* Throw away the data in a filled buffer */
1582 if (bh->state == BUF_STATE_FULL) {
1583 smp_rmb();
1584 bh->state = BUF_STATE_EMPTY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001585 common->next_buffhd_to_drain = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001586
1587 /* A short packet or an error ends everything */
1588 if (bh->outreq->actual != bh->outreq->length ||
1589 bh->outreq->status != 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001590 raise_exception(common,
1591 FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001592 return -EINTR;
1593 }
1594 continue;
1595 }
1596
1597 /* Try to submit another request if we need one */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001598 bh = common->next_buffhd_to_fill;
1599 if (bh->state == BUF_STATE_EMPTY
1600 && common->usb_amount_left > 0) {
1601 amount = min(common->usb_amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001602
1603 /* amount is always divisible by 512, hence by
1604 * the bulk-out maxpacket size */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001605 bh->outreq->length = amount;
1606 bh->bulk_out_intended_length = amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001607 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001608 START_TRANSFER_OR(common, bulk_out, bh->outreq,
1609 &bh->outreq_busy, &bh->state)
1610 /* Don't know what to do if
1611 * common->fsg is NULL */
1612 return -EIO;
1613 common->next_buffhd_to_fill = bh->next;
1614 common->usb_amount_left -= amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001615 continue;
1616 }
1617
1618 /* Otherwise wait for something to happen */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001619 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001620 if (rc)
1621 return rc;
1622 }
1623 return 0;
1624}
1625
1626
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001627static int finish_reply(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001628{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001629 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001630 int rc = 0;
1631
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001632 switch (common->data_dir) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001633 case DATA_DIR_NONE:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001634 break; /* Nothing to send */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001635
1636 /* If we don't know whether the host wants to read or write,
1637 * this must be CB or CBI with an unknown command. We mustn't
1638 * try to send or receive any data. So stall both bulk pipes
1639 * if we can and wait for a reset. */
1640 case DATA_DIR_UNKNOWN:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001641 if (!common->can_stall) {
1642 /* Nothing */
1643 } else if (fsg_is_set(common)) {
1644 fsg_set_halt(common->fsg, common->fsg->bulk_out);
1645 rc = halt_bulk_in_endpoint(common->fsg);
1646 } else {
1647 /* Don't know what to do if common->fsg is NULL */
1648 rc = -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001649 }
1650 break;
1651
1652 /* All but the last buffer of data must have already been sent */
1653 case DATA_DIR_TO_HOST:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001654 if (common->data_size == 0) {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001655 /* Nothing to send */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001656
1657 /* If there's no residue, simply send the last buffer */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001658 } else if (common->residue == 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001659 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001660 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1661 &bh->inreq_busy, &bh->state)
1662 return -EIO;
1663 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001664
1665 /* For Bulk-only, if we're allowed to stall then send the
1666 * short packet and halt the bulk-in endpoint. If we can't
1667 * stall, pad out the remaining data with 0's. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001668 } else if (common->can_stall) {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001669 bh->inreq->zero = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001670 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1671 &bh->inreq_busy, &bh->state)
1672 /* Don't know what to do if
1673 * common->fsg is NULL */
1674 rc = -EIO;
1675 common->next_buffhd_to_fill = bh->next;
1676 if (common->fsg)
1677 rc = halt_bulk_in_endpoint(common->fsg);
1678 } else if (fsg_is_set(common)) {
1679 rc = pad_with_zeros(common->fsg);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001680 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001681 /* Don't know what to do if common->fsg is NULL */
1682 rc = -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001683 }
1684 break;
1685
1686 /* We have processed all we want from the data the host has sent.
1687 * There may still be outstanding bulk-out requests. */
1688 case DATA_DIR_FROM_HOST:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001689 if (common->residue == 0) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001690 /* Nothing to receive */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001691
1692 /* Did the host stop sending unexpectedly early? */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001693 } else if (common->short_packet_received) {
1694 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001695 rc = -EINTR;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001696
1697 /* We haven't processed all the incoming data. Even though
1698 * we may be allowed to stall, doing so would cause a race.
1699 * The controller may already have ACK'ed all the remaining
1700 * bulk-out packets, in which case the host wouldn't see a
1701 * STALL. Not realizing the endpoint was halted, it wouldn't
1702 * clear the halt -- leading to problems later on. */
1703#if 0
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001704 } else if (common->can_stall) {
1705 if (fsg_is_set(common))
1706 fsg_set_halt(common->fsg,
1707 common->fsg->bulk_out);
1708 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001709 rc = -EINTR;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001710#endif
1711
1712 /* We can't stall. Read in the excess data and throw it
1713 * all away. */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001714 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001715 rc = throw_away_data(common);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001716 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001717 break;
1718 }
1719 return rc;
1720}
1721
1722
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001723static int send_status(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001724{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001725 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001726 struct fsg_buffhd *bh;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001727 struct bulk_cs_wrap *csw;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001728 int rc;
1729 u8 status = USB_STATUS_PASS;
1730 u32 sd, sdinfo = 0;
1731
1732 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001733 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001734 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001735 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001736 if (rc)
1737 return rc;
1738 }
1739
1740 if (curlun) {
1741 sd = curlun->sense_data;
1742 sdinfo = curlun->sense_data_info;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001743 } else if (common->bad_lun_okay)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001744 sd = SS_NO_SENSE;
1745 else
1746 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1747
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001748 if (common->phase_error) {
1749 DBG(common, "sending phase-error status\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001750 status = USB_STATUS_PHASE_ERROR;
1751 sd = SS_INVALID_COMMAND;
1752 } else if (sd != SS_NO_SENSE) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001753 DBG(common, "sending command-failure status\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001754 status = USB_STATUS_FAIL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001755 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001756 " info x%x\n",
1757 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1758 }
1759
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001760 /* Store and send the Bulk-only CSW */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001761 csw = (void *)bh->buf;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001762
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001763 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001764 csw->Tag = common->tag;
1765 csw->Residue = cpu_to_le32(common->residue);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001766 csw->Status = status;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001767
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001768 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1769 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001770 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1771 &bh->inreq_busy, &bh->state)
1772 /* Don't know what to do if common->fsg is NULL */
1773 return -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001774
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001775 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001776 return 0;
1777}
1778
1779
1780/*-------------------------------------------------------------------------*/
1781
1782/* Check whether the command is properly formed and whether its data size
1783 * and direction agree with the values we already have. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001784static int check_command(struct fsg_common *common, int cmnd_size,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001785 enum data_direction data_dir, unsigned int mask,
1786 int needs_medium, const char *name)
1787{
1788 int i;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001789 int lun = common->cmnd[1] >> 5;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001790 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1791 char hdlen[20];
1792 struct fsg_lun *curlun;
1793
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001794 hdlen[0] = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001795 if (common->data_dir != DATA_DIR_UNKNOWN)
1796 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1797 common->data_size);
1798 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001799 name, cmnd_size, dirletter[(int) data_dir],
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001800 common->data_size_from_cmnd, common->cmnd_size, hdlen);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001801
1802 /* We can't reply at all until we know the correct data direction
1803 * and size. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001804 if (common->data_size_from_cmnd == 0)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001805 data_dir = DATA_DIR_NONE;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001806 if (common->data_size < common->data_size_from_cmnd) {
1807 /* Host data size < Device data size is a phase error.
1808 * Carry out the command, but only transfer as much as
1809 * we are allowed. */
1810 common->data_size_from_cmnd = common->data_size;
1811 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001812 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001813 common->residue = common->data_size;
1814 common->usb_amount_left = common->data_size;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001815
1816 /* Conflicting data directions is a phase error */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001817 if (common->data_dir != data_dir
1818 && common->data_size_from_cmnd > 0) {
1819 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001820 return -EINVAL;
1821 }
1822
1823 /* Verify the length of the command itself */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001824 if (cmnd_size != common->cmnd_size) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001825
1826 /* Special case workaround: There are plenty of buggy SCSI
1827 * implementations. Many have issues with cbw->Length
1828 * field passing a wrong command size. For those cases we
1829 * always try to work around the problem by using the length
1830 * sent by the host side provided it is at least as large
1831 * as the correct command length.
1832 * Examples of such cases would be MS-Windows, which issues
1833 * REQUEST SENSE with cbw->Length == 12 where it should
1834 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1835 * REQUEST SENSE with cbw->Length == 10 where it should
1836 * be 6 as well.
1837 */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001838 if (cmnd_size <= common->cmnd_size) {
1839 DBG(common, "%s is buggy! Expected length %d "
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001840 "but we got %d\n", name,
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001841 cmnd_size, common->cmnd_size);
1842 cmnd_size = common->cmnd_size;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001843 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001844 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001845 return -EINVAL;
1846 }
1847 }
1848
1849 /* Check that the LUN values are consistent */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001850 if (common->lun != lun)
1851 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
1852 common->lun, lun);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001853
1854 /* Check the LUN */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001855 if (common->lun >= 0 && common->lun < common->nluns) {
1856 curlun = &common->luns[common->lun];
1857 common->curlun = curlun;
1858 if (common->cmnd[0] != SC_REQUEST_SENSE) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001859 curlun->sense_data = SS_NO_SENSE;
1860 curlun->sense_data_info = 0;
1861 curlun->info_valid = 0;
1862 }
1863 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001864 common->curlun = NULL;
1865 curlun = NULL;
1866 common->bad_lun_okay = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001867
1868 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
1869 * to use unsupported LUNs; all others may not. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001870 if (common->cmnd[0] != SC_INQUIRY &&
1871 common->cmnd[0] != SC_REQUEST_SENSE) {
1872 DBG(common, "unsupported LUN %d\n", common->lun);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001873 return -EINVAL;
1874 }
1875 }
1876
1877 /* If a unit attention condition exists, only INQUIRY and
1878 * REQUEST SENSE commands are allowed; anything else must fail. */
1879 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001880 common->cmnd[0] != SC_INQUIRY &&
1881 common->cmnd[0] != SC_REQUEST_SENSE) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001882 curlun->sense_data = curlun->unit_attention_data;
1883 curlun->unit_attention_data = SS_NO_SENSE;
1884 return -EINVAL;
1885 }
1886
1887 /* Check that only command bytes listed in the mask are non-zero */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001888 common->cmnd[1] &= 0x1f; /* Mask away the LUN */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001889 for (i = 1; i < cmnd_size; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001890 if (common->cmnd[i] && !(mask & (1 << i))) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001891 if (curlun)
1892 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1893 return -EINVAL;
1894 }
1895 }
1896
1897 /* If the medium isn't mounted and the command needs to access
1898 * it, return an error. */
1899 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
1900 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1901 return -EINVAL;
1902 }
1903
1904 return 0;
1905}
1906
1907
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001908static int do_scsi_command(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001909{
1910 struct fsg_buffhd *bh;
1911 int rc;
1912 int reply = -EINVAL;
1913 int i;
1914 static char unknown[16];
1915
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001916 dump_cdb(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001917
1918 /* Wait for the next buffer to become available for data or status */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001919 bh = common->next_buffhd_to_fill;
1920 common->next_buffhd_to_drain = bh;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001921 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001922 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001923 if (rc)
1924 return rc;
1925 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001926 common->phase_error = 0;
1927 common->short_packet_received = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001928
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001929 down_read(&common->filesem); /* We're using the backing file */
1930 switch (common->cmnd[0]) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001931
1932 case SC_INQUIRY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001933 common->data_size_from_cmnd = common->cmnd[4];
1934 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001935 (1<<4), 0,
1936 "INQUIRY");
1937 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001938 reply = do_inquiry(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001939 break;
1940
1941 case SC_MODE_SELECT_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001942 common->data_size_from_cmnd = common->cmnd[4];
1943 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001944 (1<<1) | (1<<4), 0,
1945 "MODE SELECT(6)");
1946 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001947 reply = do_mode_select(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001948 break;
1949
1950 case SC_MODE_SELECT_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001951 common->data_size_from_cmnd =
1952 get_unaligned_be16(&common->cmnd[7]);
1953 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001954 (1<<1) | (3<<7), 0,
1955 "MODE SELECT(10)");
1956 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001957 reply = do_mode_select(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001958 break;
1959
1960 case SC_MODE_SENSE_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001961 common->data_size_from_cmnd = common->cmnd[4];
1962 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001963 (1<<1) | (1<<2) | (1<<4), 0,
1964 "MODE SENSE(6)");
1965 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001966 reply = do_mode_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001967 break;
1968
1969 case SC_MODE_SENSE_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001970 common->data_size_from_cmnd =
1971 get_unaligned_be16(&common->cmnd[7]);
1972 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001973 (1<<1) | (1<<2) | (3<<7), 0,
1974 "MODE SENSE(10)");
1975 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001976 reply = do_mode_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001977 break;
1978
1979 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001980 common->data_size_from_cmnd = 0;
1981 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001982 (1<<4), 0,
1983 "PREVENT-ALLOW MEDIUM REMOVAL");
1984 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001985 reply = do_prevent_allow(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001986 break;
1987
1988 case SC_READ_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001989 i = common->cmnd[4];
1990 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1991 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001992 (7<<1) | (1<<4), 1,
1993 "READ(6)");
1994 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001995 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001996 break;
1997
1998 case SC_READ_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001999 common->data_size_from_cmnd =
2000 get_unaligned_be16(&common->cmnd[7]) << 9;
2001 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002002 (1<<1) | (0xf<<2) | (3<<7), 1,
2003 "READ(10)");
2004 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002005 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002006 break;
2007
2008 case SC_READ_12:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002009 common->data_size_from_cmnd =
2010 get_unaligned_be32(&common->cmnd[6]) << 9;
2011 reply = check_command(common, 12, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002012 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2013 "READ(12)");
2014 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002015 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002016 break;
2017
2018 case SC_READ_CAPACITY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002019 common->data_size_from_cmnd = 8;
2020 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002021 (0xf<<2) | (1<<8), 1,
2022 "READ CAPACITY");
2023 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002024 reply = do_read_capacity(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002025 break;
2026
2027 case SC_READ_HEADER:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002028 if (!common->curlun || !common->curlun->cdrom)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002029 goto unknown_cmnd;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002030 common->data_size_from_cmnd =
2031 get_unaligned_be16(&common->cmnd[7]);
2032 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002033 (3<<7) | (0x1f<<1), 1,
2034 "READ HEADER");
2035 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002036 reply = do_read_header(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002037 break;
2038
2039 case SC_READ_TOC:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002040 if (!common->curlun || !common->curlun->cdrom)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002041 goto unknown_cmnd;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002042 common->data_size_from_cmnd =
2043 get_unaligned_be16(&common->cmnd[7]);
2044 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002045 (7<<6) | (1<<1), 1,
2046 "READ TOC");
2047 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002048 reply = do_read_toc(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002049 break;
2050
2051 case SC_READ_FORMAT_CAPACITIES:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002052 common->data_size_from_cmnd =
2053 get_unaligned_be16(&common->cmnd[7]);
2054 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002055 (3<<7), 1,
2056 "READ FORMAT CAPACITIES");
2057 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002058 reply = do_read_format_capacities(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002059 break;
2060
2061 case SC_REQUEST_SENSE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002062 common->data_size_from_cmnd = common->cmnd[4];
2063 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002064 (1<<4), 0,
2065 "REQUEST SENSE");
2066 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002067 reply = do_request_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002068 break;
2069
2070 case SC_START_STOP_UNIT:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002071 common->data_size_from_cmnd = 0;
2072 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002073 (1<<1) | (1<<4), 0,
2074 "START-STOP UNIT");
2075 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002076 reply = do_start_stop(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002077 break;
2078
2079 case SC_SYNCHRONIZE_CACHE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002080 common->data_size_from_cmnd = 0;
2081 reply = check_command(common, 10, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002082 (0xf<<2) | (3<<7), 1,
2083 "SYNCHRONIZE CACHE");
2084 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002085 reply = do_synchronize_cache(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002086 break;
2087
2088 case SC_TEST_UNIT_READY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002089 common->data_size_from_cmnd = 0;
2090 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002091 0, 1,
2092 "TEST UNIT READY");
2093 break;
2094
2095 /* Although optional, this command is used by MS-Windows. We
2096 * support a minimal version: BytChk must be 0. */
2097 case SC_VERIFY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002098 common->data_size_from_cmnd = 0;
2099 reply = check_command(common, 10, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002100 (1<<1) | (0xf<<2) | (3<<7), 1,
2101 "VERIFY");
2102 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002103 reply = do_verify(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002104 break;
2105
2106 case SC_WRITE_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002107 i = common->cmnd[4];
2108 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2109 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002110 (7<<1) | (1<<4), 1,
2111 "WRITE(6)");
2112 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002113 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002114 break;
2115
2116 case SC_WRITE_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002117 common->data_size_from_cmnd =
2118 get_unaligned_be16(&common->cmnd[7]) << 9;
2119 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002120 (1<<1) | (0xf<<2) | (3<<7), 1,
2121 "WRITE(10)");
2122 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002123 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002124 break;
2125
2126 case SC_WRITE_12:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002127 common->data_size_from_cmnd =
2128 get_unaligned_be32(&common->cmnd[6]) << 9;
2129 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002130 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2131 "WRITE(12)");
2132 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002133 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002134 break;
2135
2136 /* Some mandatory commands that we recognize but don't implement.
2137 * They don't mean much in this setting. It's left as an exercise
2138 * for anyone interested to implement RESERVE and RELEASE in terms
2139 * of Posix locks. */
2140 case SC_FORMAT_UNIT:
2141 case SC_RELEASE:
2142 case SC_RESERVE:
2143 case SC_SEND_DIAGNOSTIC:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002144 /* Fall through */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002145
2146 default:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002147unknown_cmnd:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002148 common->data_size_from_cmnd = 0;
2149 sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2150 reply = check_command(common, common->cmnd_size,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002151 DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2152 if (reply == 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002153 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002154 reply = -EINVAL;
2155 }
2156 break;
2157 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002158 up_read(&common->filesem);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002159
2160 if (reply == -EINTR || signal_pending(current))
2161 return -EINTR;
2162
2163 /* Set up the single reply buffer for finish_reply() */
2164 if (reply == -EINVAL)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002165 reply = 0; /* Error reply length */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002166 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2167 reply = min((u32) reply, common->data_size_from_cmnd);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002168 bh->inreq->length = reply;
2169 bh->state = BUF_STATE_FULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002170 common->residue -= reply;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002171 } /* Otherwise it's already set */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002172
2173 return 0;
2174}
2175
2176
2177/*-------------------------------------------------------------------------*/
2178
2179static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2180{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002181 struct usb_request *req = bh->outreq;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002182 struct fsg_bulk_cb_wrap *cbw = req->buf;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002183 struct fsg_common *common = fsg->common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002184
2185 /* Was this a real packet? Should it be ignored? */
2186 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2187 return -EINVAL;
2188
2189 /* Is the CBW valid? */
2190 if (req->actual != USB_BULK_CB_WRAP_LEN ||
2191 cbw->Signature != cpu_to_le32(
2192 USB_BULK_CB_SIG)) {
2193 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2194 req->actual,
2195 le32_to_cpu(cbw->Signature));
2196
2197 /* The Bulk-only spec says we MUST stall the IN endpoint
2198 * (6.6.1), so it's unavoidable. It also says we must
2199 * retain this state until the next reset, but there's
2200 * no way to tell the controller driver it should ignore
2201 * Clear-Feature(HALT) requests.
2202 *
2203 * We aren't required to halt the OUT endpoint; instead
2204 * we can simply accept and discard any data received
2205 * until the next reset. */
2206 wedge_bulk_in_endpoint(fsg);
2207 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2208 return -EINVAL;
2209 }
2210
2211 /* Is the CBW meaningful? */
2212 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2213 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2214 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2215 "cmdlen %u\n",
2216 cbw->Lun, cbw->Flags, cbw->Length);
2217
2218 /* We can do anything we want here, so let's stall the
2219 * bulk pipes if we are allowed to. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002220 if (common->can_stall) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002221 fsg_set_halt(fsg, fsg->bulk_out);
2222 halt_bulk_in_endpoint(fsg);
2223 }
2224 return -EINVAL;
2225 }
2226
2227 /* Save the command for later */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002228 common->cmnd_size = cbw->Length;
2229 memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002230 if (cbw->Flags & USB_BULK_IN_FLAG)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002231 common->data_dir = DATA_DIR_TO_HOST;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002232 else
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002233 common->data_dir = DATA_DIR_FROM_HOST;
2234 common->data_size = le32_to_cpu(cbw->DataTransferLength);
2235 if (common->data_size == 0)
2236 common->data_dir = DATA_DIR_NONE;
2237 common->lun = cbw->Lun;
2238 common->tag = cbw->Tag;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002239 return 0;
2240}
2241
2242
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002243static int get_next_command(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002244{
2245 struct fsg_buffhd *bh;
2246 int rc = 0;
2247
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002248 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002249 bh = common->next_buffhd_to_fill;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002250 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002251 rc = sleep_thread(common);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002252 if (rc)
2253 return rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002254 }
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002255
2256 /* Queue a request to read a Bulk-only CBW */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002257 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002258 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002259 START_TRANSFER_OR(common, bulk_out, bh->outreq,
2260 &bh->outreq_busy, &bh->state)
2261 /* Don't know what to do if common->fsg is NULL */
2262 return -EIO;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002263
2264 /* We will drain the buffer in software, which means we
2265 * can reuse it for the next filling. No need to advance
2266 * next_buffhd_to_fill. */
2267
2268 /* Wait for the CBW to arrive */
2269 while (bh->state != BUF_STATE_FULL) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002270 rc = sleep_thread(common);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002271 if (rc)
2272 return rc;
2273 }
2274 smp_rmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002275 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002276 bh->state = BUF_STATE_EMPTY;
2277
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002278 return rc;
2279}
2280
2281
2282/*-------------------------------------------------------------------------*/
2283
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002284static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002285 const struct usb_endpoint_descriptor *d)
2286{
2287 int rc;
2288
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002289 ep->driver_data = common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002290 rc = usb_ep_enable(ep, d);
2291 if (rc)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002292 ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002293 return rc;
2294}
2295
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002296static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002297 struct usb_request **preq)
2298{
2299 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2300 if (*preq)
2301 return 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002302 ERROR(common, "can't allocate request for %s\n", ep->name);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002303 return -ENOMEM;
2304}
2305
2306/*
2307 * Reset interface setting and re-init endpoint state (toggle etc).
2308 * Call with altsetting < 0 to disable the interface. The only other
2309 * available altsetting is 0, which enables the interface.
2310 */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002311static int do_set_interface(struct fsg_common *common, int altsetting)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002312{
2313 int rc = 0;
2314 int i;
2315 const struct usb_endpoint_descriptor *d;
2316
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002317 if (common->running)
2318 DBG(common, "reset interface\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002319
2320reset:
2321 /* Deallocate the requests */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002322 if (common->prev_fsg) {
2323 struct fsg_dev *fsg = common->prev_fsg;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002324
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002325 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2326 struct fsg_buffhd *bh = &common->buffhds[i];
2327
2328 if (bh->inreq) {
2329 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2330 bh->inreq = NULL;
2331 }
2332 if (bh->outreq) {
2333 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2334 bh->outreq = NULL;
2335 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002336 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002337
2338 /* Disable the endpoints */
2339 if (fsg->bulk_in_enabled) {
2340 usb_ep_disable(fsg->bulk_in);
2341 fsg->bulk_in_enabled = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002342 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002343 if (fsg->bulk_out_enabled) {
2344 usb_ep_disable(fsg->bulk_out);
2345 fsg->bulk_out_enabled = 0;
2346 }
2347
2348 common->prev_fsg = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002349 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002350
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002351 common->running = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002352 if (altsetting < 0 || rc != 0)
2353 return rc;
2354
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002355 DBG(common, "set interface %d\n", altsetting);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002356
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002357 if (fsg_is_set(common)) {
2358 struct fsg_dev *fsg = common->fsg;
2359 common->prev_fsg = common->fsg;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002360
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002361 /* Enable the endpoints */
2362 d = fsg_ep_desc(common->gadget,
2363 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2364 rc = enable_endpoint(common, fsg->bulk_in, d);
2365 if (rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002366 goto reset;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002367 fsg->bulk_in_enabled = 1;
2368
2369 d = fsg_ep_desc(common->gadget,
2370 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2371 rc = enable_endpoint(common, fsg->bulk_out, d);
2372 if (rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002373 goto reset;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002374 fsg->bulk_out_enabled = 1;
2375 common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2376 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2377
2378 /* Allocate the requests */
2379 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2380 struct fsg_buffhd *bh = &common->buffhds[i];
2381
2382 rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
2383 if (rc)
2384 goto reset;
2385 rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
2386 if (rc)
2387 goto reset;
2388 bh->inreq->buf = bh->outreq->buf = bh->buf;
2389 bh->inreq->context = bh->outreq->context = bh;
2390 bh->inreq->complete = bulk_in_complete;
2391 bh->outreq->complete = bulk_out_complete;
2392 }
2393
2394 common->running = 1;
2395 for (i = 0; i < common->nluns; ++i)
2396 common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2397 return rc;
2398 } else {
2399 return -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002400 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002401}
2402
2403
2404/*
2405 * Change our operational configuration. This code must agree with the code
2406 * that returns config descriptors, and with interface altsetting code.
2407 *
2408 * It's also responsible for power management interactions. Some
2409 * configurations might not work with our current power sources.
2410 * For now we just assume the gadget is always self-powered.
2411 */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002412static int do_set_config(struct fsg_common *common, u8 new_config)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002413{
2414 int rc = 0;
2415
2416 /* Disable the single interface */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002417 if (common->config != 0) {
2418 DBG(common, "reset config\n");
2419 common->config = 0;
2420 rc = do_set_interface(common, -1);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002421 }
2422
2423 /* Enable the interface */
2424 if (new_config != 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002425 common->config = new_config;
2426 rc = do_set_interface(common, 0);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002427 if (rc != 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002428 common->config = 0; /* Reset on errors */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002429 }
2430 return rc;
2431}
2432
2433
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002434/****************************** ALT CONFIGS ******************************/
2435
2436
2437static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2438{
2439 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002440 fsg->common->prev_fsg = fsg->common->fsg;
2441 fsg->common->fsg = fsg;
2442 fsg->common->new_config = 1;
2443 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002444 return 0;
2445}
2446
2447static void fsg_disable(struct usb_function *f)
2448{
2449 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002450 fsg->common->prev_fsg = fsg->common->fsg;
2451 fsg->common->fsg = fsg;
2452 fsg->common->new_config = 0;
2453 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002454}
2455
2456
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002457/*-------------------------------------------------------------------------*/
2458
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002459static void handle_exception(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002460{
2461 siginfo_t info;
2462 int sig;
2463 int i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002464 struct fsg_buffhd *bh;
2465 enum fsg_state old_state;
2466 u8 new_config;
2467 struct fsg_lun *curlun;
2468 unsigned int exception_req_tag;
2469 int rc;
2470
2471 /* Clear the existing signals. Anything but SIGUSR1 is converted
2472 * into a high-priority EXIT exception. */
2473 for (;;) {
2474 sig = dequeue_signal_lock(current, &current->blocked, &info);
2475 if (!sig)
2476 break;
2477 if (sig != SIGUSR1) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002478 if (common->state < FSG_STATE_EXIT)
2479 DBG(common, "Main thread exiting on signal\n");
2480 raise_exception(common, FSG_STATE_EXIT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002481 }
2482 }
2483
2484 /* Cancel all the pending transfers */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002485 if (fsg_is_set(common)) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002486 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002487 bh = &common->buffhds[i];
2488 if (bh->inreq_busy)
2489 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2490 if (bh->outreq_busy)
2491 usb_ep_dequeue(common->fsg->bulk_out,
2492 bh->outreq);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002493 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002494
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002495 /* Wait until everything is idle */
2496 for (;;) {
2497 int num_active = 0;
2498 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2499 bh = &common->buffhds[i];
2500 num_active += bh->inreq_busy + bh->outreq_busy;
2501 }
2502 if (num_active == 0)
2503 break;
2504 if (sleep_thread(common))
2505 return;
2506 }
2507
2508 /* Clear out the controller's fifos */
2509 if (common->fsg->bulk_in_enabled)
2510 usb_ep_fifo_flush(common->fsg->bulk_in);
2511 if (common->fsg->bulk_out_enabled)
2512 usb_ep_fifo_flush(common->fsg->bulk_out);
2513 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002514
2515 /* Reset the I/O buffer states and pointers, the SCSI
2516 * state, and the exception. Then invoke the handler. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002517 spin_lock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002518
2519 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002520 bh = &common->buffhds[i];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002521 bh->state = BUF_STATE_EMPTY;
2522 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002523 common->next_buffhd_to_fill = &common->buffhds[0];
2524 common->next_buffhd_to_drain = &common->buffhds[0];
2525 exception_req_tag = common->exception_req_tag;
2526 new_config = common->new_config;
2527 old_state = common->state;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002528
2529 if (old_state == FSG_STATE_ABORT_BULK_OUT)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002530 common->state = FSG_STATE_STATUS_PHASE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002531 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002532 for (i = 0; i < common->nluns; ++i) {
2533 curlun = &common->luns[i];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002534 curlun->prevent_medium_removal = 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002535 curlun->sense_data = SS_NO_SENSE;
2536 curlun->unit_attention_data = SS_NO_SENSE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002537 curlun->sense_data_info = 0;
2538 curlun->info_valid = 0;
2539 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002540 common->state = FSG_STATE_IDLE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002541 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002542 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002543
2544 /* Carry out any extra actions required for the exception */
2545 switch (old_state) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002546 case FSG_STATE_ABORT_BULK_OUT:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002547 send_status(common);
2548 spin_lock_irq(&common->lock);
2549 if (common->state == FSG_STATE_STATUS_PHASE)
2550 common->state = FSG_STATE_IDLE;
2551 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002552 break;
2553
2554 case FSG_STATE_RESET:
2555 /* In case we were forced against our will to halt a
2556 * bulk endpoint, clear the halt now. (The SuperH UDC
2557 * requires this.) */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002558 if (!fsg_is_set(common))
2559 break;
2560 if (test_and_clear_bit(IGNORE_BULK_OUT,
2561 &common->fsg->atomic_bitflags))
2562 usb_ep_clear_halt(common->fsg->bulk_in);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002563
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002564 if (common->ep0_req_tag == exception_req_tag)
2565 ep0_queue(common); /* Complete the status stage */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002566
2567 /* Technically this should go here, but it would only be
2568 * a waste of time. Ditto for the INTERFACE_CHANGE and
2569 * CONFIG_CHANGE cases. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002570 /* for (i = 0; i < common->nluns; ++i) */
2571 /* common->luns[i].unit_attention_data = */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002572 /* SS_RESET_OCCURRED; */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002573 break;
2574
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002575 case FSG_STATE_CONFIG_CHANGE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002576 rc = do_set_config(common, new_config);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002577 break;
2578
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002579 case FSG_STATE_EXIT:
2580 case FSG_STATE_TERMINATED:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002581 do_set_config(common, 0); /* Free resources */
2582 spin_lock_irq(&common->lock);
2583 common->state = FSG_STATE_TERMINATED; /* Stop the thread */
2584 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002585 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002586
2587 case FSG_STATE_INTERFACE_CHANGE:
2588 case FSG_STATE_DISCONNECT:
2589 case FSG_STATE_COMMAND_PHASE:
2590 case FSG_STATE_DATA_PHASE:
2591 case FSG_STATE_STATUS_PHASE:
2592 case FSG_STATE_IDLE:
2593 break;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002594 }
2595}
2596
2597
2598/*-------------------------------------------------------------------------*/
2599
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002600static int fsg_main_thread(void *common_)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002601{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002602 struct fsg_common *common = common_;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002603
2604 /* Allow the thread to be killed by a signal, but set the signal mask
2605 * to block everything but INT, TERM, KILL, and USR1. */
2606 allow_signal(SIGINT);
2607 allow_signal(SIGTERM);
2608 allow_signal(SIGKILL);
2609 allow_signal(SIGUSR1);
2610
2611 /* Allow the thread to be frozen */
2612 set_freezable();
2613
2614 /* Arrange for userspace references to be interpreted as kernel
2615 * pointers. That way we can pass a kernel pointer to a routine
2616 * that expects a __user pointer and it will work okay. */
2617 set_fs(get_ds());
2618
2619 /* The main loop */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002620 while (common->state != FSG_STATE_TERMINATED) {
2621 if (exception_in_progress(common) || signal_pending(current)) {
2622 handle_exception(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002623 continue;
2624 }
2625
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002626 if (!common->running) {
2627 sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002628 continue;
2629 }
2630
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002631 if (get_next_command(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002632 continue;
2633
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002634 spin_lock_irq(&common->lock);
2635 if (!exception_in_progress(common))
2636 common->state = FSG_STATE_DATA_PHASE;
2637 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002638
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002639 if (do_scsi_command(common) || finish_reply(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002640 continue;
2641
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002642 spin_lock_irq(&common->lock);
2643 if (!exception_in_progress(common))
2644 common->state = FSG_STATE_STATUS_PHASE;
2645 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002646
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002647 if (send_status(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002648 continue;
2649
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002650 spin_lock_irq(&common->lock);
2651 if (!exception_in_progress(common))
2652 common->state = FSG_STATE_IDLE;
2653 spin_unlock_irq(&common->lock);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002654 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002655
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002656 spin_lock_irq(&common->lock);
2657 common->thread_task = NULL;
2658 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002659
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +01002660 if (!common->thread_exits || common->thread_exits(common) < 0) {
2661 struct fsg_lun *curlun = common->luns;
2662 unsigned i = common->nluns;
2663
2664 down_write(&common->filesem);
2665 for (; i--; ++curlun) {
2666 if (!fsg_lun_is_open(curlun))
2667 continue;
2668
2669 fsg_lun_close(curlun);
2670 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
2671 }
2672 up_write(&common->filesem);
2673 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002674
2675 /* Let the unbind and cleanup routines know the thread has exited */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002676 complete_and_exit(&common->thread_notifier, 0);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002677}
2678
2679
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002680/*************************** DEVICE ATTRIBUTES ***************************/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002681
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002682/* Write permission is checked per LUN in store_*() functions. */
2683static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
2684static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002685
2686
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002687/****************************** FSG COMMON ******************************/
2688
2689static void fsg_common_release(struct kref *ref);
2690
2691static void fsg_lun_release(struct device *dev)
2692{
2693 /* Nothing needs to be done */
2694}
2695
2696static inline void fsg_common_get(struct fsg_common *common)
2697{
2698 kref_get(&common->ref);
2699}
2700
2701static inline void fsg_common_put(struct fsg_common *common)
2702{
2703 kref_put(&common->ref, fsg_common_release);
2704}
2705
2706
2707static struct fsg_common *fsg_common_init(struct fsg_common *common,
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002708 struct usb_composite_dev *cdev,
2709 struct fsg_config *cfg)
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002710{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002711 struct usb_gadget *gadget = cdev->gadget;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002712 struct fsg_buffhd *bh;
2713 struct fsg_lun *curlun;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002714 struct fsg_lun_config *lcfg;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002715 int nluns, i, rc;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002716 char *pathbuf;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002717
2718 /* Find out how many LUNs there should be */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002719 nluns = cfg->nluns;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002720 if (nluns < 1 || nluns > FSG_MAX_LUNS) {
2721 dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns);
2722 return ERR_PTR(-EINVAL);
2723 }
2724
2725 /* Allocate? */
2726 if (!common) {
2727 common = kzalloc(sizeof *common, GFP_KERNEL);
2728 if (!common)
2729 return ERR_PTR(-ENOMEM);
2730 common->free_storage_on_release = 1;
2731 } else {
2732 memset(common, 0, sizeof common);
2733 common->free_storage_on_release = 0;
2734 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002735
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01002736 common->private_data = cfg->private_data;
2737
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002738 common->gadget = gadget;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002739 common->ep0 = gadget->ep0;
2740 common->ep0req = cdev->req;
2741
2742 /* Maybe allocate device-global string IDs, and patch descriptors */
2743 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2744 rc = usb_string_id(cdev);
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002745 if (unlikely(rc < 0))
2746 goto error_release;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002747 fsg_strings[FSG_STRING_INTERFACE].id = rc;
2748 fsg_intf_desc.iInterface = rc;
2749 }
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002750
2751 /* Create the LUNs, open their backing files, and register the
2752 * LUN devices in sysfs. */
2753 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002754 if (unlikely(!curlun)) {
2755 rc = -ENOMEM;
2756 goto error_release;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002757 }
2758 common->luns = curlun;
2759
2760 init_rwsem(&common->filesem);
2761
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002762 for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
2763 curlun->cdrom = !!lcfg->cdrom;
2764 curlun->ro = lcfg->cdrom || lcfg->ro;
2765 curlun->removable = lcfg->removable;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002766 curlun->dev.release = fsg_lun_release;
2767 curlun->dev.parent = &gadget->dev;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002768 /* curlun->dev.driver = &fsg_driver.driver; XXX */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002769 dev_set_drvdata(&curlun->dev, &common->filesem);
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01002770 dev_set_name(&curlun->dev,
2771 cfg->lun_name_format
2772 ? cfg->lun_name_format
2773 : "lun%d",
2774 i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002775
2776 rc = device_register(&curlun->dev);
2777 if (rc) {
2778 INFO(common, "failed to register LUN%d: %d\n", i, rc);
2779 common->nluns = i;
2780 goto error_release;
2781 }
2782
2783 rc = device_create_file(&curlun->dev, &dev_attr_ro);
2784 if (rc)
2785 goto error_luns;
2786 rc = device_create_file(&curlun->dev, &dev_attr_file);
2787 if (rc)
2788 goto error_luns;
2789
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002790 if (lcfg->filename) {
2791 rc = fsg_lun_open(curlun, lcfg->filename);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002792 if (rc)
2793 goto error_luns;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002794 } else if (!curlun->removable) {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002795 ERROR(common, "no file given for LUN%d\n", i);
2796 rc = -EINVAL;
2797 goto error_luns;
2798 }
2799 }
2800 common->nluns = nluns;
2801
2802
2803 /* Data buffers cyclic list */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002804 bh = common->buffhds;
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002805 i = FSG_NUM_BUFFERS;
2806 goto buffhds_first_it;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002807 do {
2808 bh->next = bh + 1;
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002809 ++bh;
2810buffhds_first_it:
2811 bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
2812 if (unlikely(!bh->buf)) {
2813 rc = -ENOMEM;
2814 goto error_release;
2815 }
2816 } while (--i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002817 bh->next = common->buffhds;
2818
2819
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002820 /* Prepare inquiryString */
2821 if (cfg->release != 0xffff) {
2822 i = cfg->release;
2823 } else {
Christoph Egger90f79762010-02-05 13:24:12 +01002824 i = usb_gadget_controller_number(gadget);
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002825 if (i >= 0) {
2826 i = 0x0300 + i;
2827 } else {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002828 WARNING(common, "controller '%s' not recognized\n",
2829 gadget->name);
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002830 i = 0x0399;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002831 }
2832 }
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002833#define OR(x, y) ((x) ? (x) : (y))
2834 snprintf(common->inquiry_string, sizeof common->inquiry_string,
2835 "%-8s%-16s%04x",
2836 OR(cfg->vendor_name, "Linux "),
2837 /* Assume product name dependent on the first LUN */
2838 OR(cfg->product_name, common->luns->cdrom
2839 ? "File-Stor Gadget"
2840 : "File-CD Gadget "),
2841 i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002842
2843
2844 /* Some peripheral controllers are known not to be able to
2845 * halt bulk endpoints correctly. If one of them is present,
2846 * disable stalls.
2847 */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002848 common->can_stall = cfg->can_stall &&
Christoph Egger90f79762010-02-05 13:24:12 +01002849 !(gadget_is_at91(common->gadget));
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002850
2851
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002852 spin_lock_init(&common->lock);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002853 kref_init(&common->ref);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002854
2855
2856 /* Tell the thread to start working */
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01002857 common->thread_exits = cfg->thread_exits;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002858 common->thread_task =
2859 kthread_create(fsg_main_thread, common,
2860 OR(cfg->thread_name, "file-storage"));
2861 if (IS_ERR(common->thread_task)) {
2862 rc = PTR_ERR(common->thread_task);
2863 goto error_release;
2864 }
2865 init_completion(&common->thread_notifier);
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01002866#undef OR
2867
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002868
2869 /* Information */
2870 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
2871 INFO(common, "Number of LUNs=%d\n", common->nluns);
2872
2873 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2874 for (i = 0, nluns = common->nluns, curlun = common->luns;
2875 i < nluns;
2876 ++curlun, ++i) {
2877 char *p = "(no medium)";
2878 if (fsg_lun_is_open(curlun)) {
2879 p = "(error)";
2880 if (pathbuf) {
2881 p = d_path(&curlun->filp->f_path,
2882 pathbuf, PATH_MAX);
2883 if (IS_ERR(p))
2884 p = "(error)";
2885 }
2886 }
2887 LINFO(curlun, "LUN: %s%s%sfile: %s\n",
2888 curlun->removable ? "removable " : "",
2889 curlun->ro ? "read only " : "",
2890 curlun->cdrom ? "CD-ROM " : "",
2891 p);
2892 }
2893 kfree(pathbuf);
2894
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002895 DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task));
2896
2897 wake_up_process(common->thread_task);
2898
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002899 return common;
2900
2901
2902error_luns:
2903 common->nluns = i + 1;
2904error_release:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002905 common->state = FSG_STATE_TERMINATED; /* The thread is dead */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002906 /* Call fsg_common_release() directly, ref might be not
2907 * initialised */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002908 fsg_common_release(&common->ref);
2909 return ERR_PTR(rc);
2910}
2911
2912
2913static void fsg_common_release(struct kref *ref)
2914{
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002915 struct fsg_common *common = container_of(ref, struct fsg_common, ref);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002916
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002917 /* If the thread isn't already dead, tell it to exit now */
2918 if (common->state != FSG_STATE_TERMINATED) {
2919 raise_exception(common, FSG_STATE_EXIT);
2920 wait_for_completion(&common->thread_notifier);
2921
2922 /* The cleanup routine waits for this completion also */
2923 complete(&common->thread_notifier);
2924 }
2925
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002926 if (likely(common->luns)) {
2927 struct fsg_lun *lun = common->luns;
2928 unsigned i = common->nluns;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002929
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002930 /* In error recovery common->nluns may be zero. */
2931 for (; i; --i, ++lun) {
2932 device_remove_file(&lun->dev, &dev_attr_ro);
2933 device_remove_file(&lun->dev, &dev_attr_file);
2934 fsg_lun_close(lun);
2935 device_unregister(&lun->dev);
2936 }
2937
2938 kfree(common->luns);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002939 }
2940
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002941 {
2942 struct fsg_buffhd *bh = common->buffhds;
2943 unsigned i = FSG_NUM_BUFFERS;
2944 do {
2945 kfree(bh->buf);
2946 } while (++bh, --i);
2947 }
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002948
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002949 if (common->free_storage_on_release)
2950 kfree(common);
2951}
2952
2953
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002954/*-------------------------------------------------------------------------*/
2955
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002956
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002957static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002958{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002959 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002960
2961 DBG(fsg, "unbind\n");
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002962 fsg_common_put(fsg->common);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02002963 usb_free_descriptors(fsg->function.descriptors);
2964 usb_free_descriptors(fsg->function.hs_descriptors);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002965 kfree(fsg);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002966}
2967
2968
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002969static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002970{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002971 struct fsg_dev *fsg = fsg_from_func(f);
2972 struct usb_gadget *gadget = c->cdev->gadget;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002973 int i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002974 struct usb_ep *ep;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002975
2976 fsg->gadget = gadget;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002977
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002978 /* New interface */
2979 i = usb_interface_id(c, f);
2980 if (i < 0)
2981 return i;
2982 fsg_intf_desc.bInterfaceNumber = i;
2983 fsg->interface_number = i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002984
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002985 /* Find all the endpoints we will use */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002986 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2987 if (!ep)
2988 goto autoconf_fail;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002989 ep->driver_data = fsg->common; /* claim the endpoint */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002990 fsg->bulk_in = ep;
2991
2992 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2993 if (!ep)
2994 goto autoconf_fail;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002995 ep->driver_data = fsg->common; /* claim the endpoint */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002996 fsg->bulk_out = ep;
2997
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02002998 /* Copy descriptors */
2999 f->descriptors = usb_copy_descriptors(fsg_fs_function);
3000 if (unlikely(!f->descriptors))
3001 return -ENOMEM;
3002
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003003 if (gadget_is_dualspeed(gadget)) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003004 /* Assume endpoint addresses are the same for both speeds */
3005 fsg_hs_bulk_in_desc.bEndpointAddress =
3006 fsg_fs_bulk_in_desc.bEndpointAddress;
3007 fsg_hs_bulk_out_desc.bEndpointAddress =
3008 fsg_fs_bulk_out_desc.bEndpointAddress;
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02003009 f->hs_descriptors = usb_copy_descriptors(fsg_hs_function);
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003010 if (unlikely(!f->hs_descriptors)) {
3011 usb_free_descriptors(f->descriptors);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02003012 return -ENOMEM;
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003013 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003014 }
3015
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003016 return 0;
3017
3018autoconf_fail:
3019 ERROR(fsg, "unable to autoconfigure all endpoints\n");
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003020 return -ENOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003021}
3022
3023
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003024/****************************** ADD FUNCTION ******************************/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003025
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003026static struct usb_gadget_strings *fsg_strings_array[] = {
3027 &fsg_stringtab,
3028 NULL,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003029};
3030
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003031static int fsg_add(struct usb_composite_dev *cdev,
3032 struct usb_configuration *c,
3033 struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003034{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003035 struct fsg_dev *fsg;
3036 int rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003037
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003038 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
3039 if (unlikely(!fsg))
3040 return -ENOMEM;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003041
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003042 fsg->function.name = FSG_DRIVER_DESC;
3043 fsg->function.strings = fsg_strings_array;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003044 fsg->function.bind = fsg_bind;
3045 fsg->function.unbind = fsg_unbind;
3046 fsg->function.setup = fsg_setup;
3047 fsg->function.set_alt = fsg_set_alt;
3048 fsg->function.disable = fsg_disable;
3049
3050 fsg->common = common;
3051 /* Our caller holds a reference to common structure so we
3052 * don't have to be worry about it being freed until we return
3053 * from this function. So instead of incrementing counter now
3054 * and decrement in error recovery we increment it only when
3055 * call to usb_add_function() was successful. */
3056
3057 rc = usb_add_function(c, &fsg->function);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02003058 if (unlikely(rc))
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003059 kfree(fsg);
3060 else
3061 fsg_common_get(fsg->common);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003062 return rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003063}
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003064
3065
3066
3067/************************* Module parameters *************************/
3068
3069
3070struct fsg_module_parameters {
3071 char *file[FSG_MAX_LUNS];
3072 int ro[FSG_MAX_LUNS];
3073 int removable[FSG_MAX_LUNS];
3074 int cdrom[FSG_MAX_LUNS];
3075
3076 unsigned int file_count, ro_count, removable_count, cdrom_count;
3077 unsigned int luns; /* nluns */
3078 int stall; /* can_stall */
3079};
3080
3081
3082#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
3083 module_param_array_named(prefix ## name, params.name, type, \
3084 &prefix ## params.name ## _count, \
3085 S_IRUGO); \
3086 MODULE_PARM_DESC(prefix ## name, desc)
3087
3088#define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \
3089 module_param_named(prefix ## name, params.name, type, \
3090 S_IRUGO); \
3091 MODULE_PARM_DESC(prefix ## name, desc)
3092
3093#define FSG_MODULE_PARAMETERS(prefix, params) \
3094 _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \
3095 "names of backing files or devices"); \
3096 _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \
3097 "true to force read-only"); \
3098 _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \
3099 "true to simulate removable media"); \
3100 _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
3101 "true to simulate CD-ROM instead of disk"); \
3102 _FSG_MODULE_PARAM(prefix, params, luns, uint, \
3103 "number of LUNs"); \
3104 _FSG_MODULE_PARAM(prefix, params, stall, bool, \
3105 "false to prevent bulk stalls")
3106
3107
3108static void
3109fsg_config_from_params(struct fsg_config *cfg,
3110 const struct fsg_module_parameters *params)
3111{
3112 struct fsg_lun_config *lun;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003113 unsigned i;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003114
3115 /* Configure LUNs */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003116 cfg->nluns =
3117 min(params->luns ?: (params->file_count ?: 1u),
3118 (unsigned)FSG_MAX_LUNS);
3119 for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003120 lun->ro = !!params->ro[i];
3121 lun->cdrom = !!params->cdrom[i];
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003122 lun->removable = /* Removable by default */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003123 params->removable_count <= i || params->removable[i];
3124 lun->filename =
3125 params->file_count > i && params->file[i][0]
3126 ? params->file[i]
3127 : 0;
3128 }
3129
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003130 /* Let MSF use defaults */
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01003131 cfg->lun_name_format = 0;
3132 cfg->thread_name = 0;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003133 cfg->vendor_name = 0;
3134 cfg->product_name = 0;
3135 cfg->release = 0xffff;
3136
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01003137 cfg->thread_exits = 0;
3138 cfg->private_data = 0;
3139
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003140 /* Finalise */
3141 cfg->can_stall = params->stall;
3142}
3143
3144static inline struct fsg_common *
3145fsg_common_from_params(struct fsg_common *common,
3146 struct usb_composite_dev *cdev,
3147 const struct fsg_module_parameters *params)
3148 __attribute__((unused));
3149static inline struct fsg_common *
3150fsg_common_from_params(struct fsg_common *common,
3151 struct usb_composite_dev *cdev,
3152 const struct fsg_module_parameters *params)
3153{
3154 struct fsg_config cfg;
3155 fsg_config_from_params(&cfg, params);
3156 return fsg_common_init(common, cdev, &cfg);
3157}
3158