blob: 8a95ebc7e3d3e88230e39448bef900b3afea8bf4 [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 Nazarewiczb894f602010-06-25 16:29:28 +0200324 struct fsg_dev *fsg, *new_fsg;
325 wait_queue_head_t fsg_wait;
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
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100354 enum data_direction data_dir;
355 u32 data_size;
356 u32 data_size_from_cmnd;
357 u32 tag;
358 u32 residue;
359 u32 usb_amount_left;
360
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100361 unsigned int can_stall:1;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100362 unsigned int free_storage_on_release:1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100363 unsigned int phase_error:1;
364 unsigned int short_packet_received:1;
365 unsigned int bad_lun_okay:1;
366 unsigned int running:1;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100367
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100368 int thread_wakeup_needed;
369 struct completion thread_notifier;
370 struct task_struct *thread_task;
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100371
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100372 /* Callback function to call when thread exits. */
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +0100373 int (*thread_exits)(struct fsg_common *common);
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100374 /* Gadget's private data. */
375 void *private_data;
376
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100377 /* Vendor (8 chars), product (16 chars), release (4
378 * hexadecimal digits) and NUL byte */
379 char inquiry_string[8 + 16 + 4 + 1];
380
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100381 struct kref ref;
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100382};
383
384
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100385struct fsg_config {
386 unsigned nluns;
387 struct fsg_lun_config {
388 const char *filename;
389 char ro;
390 char removable;
391 char cdrom;
392 } luns[FSG_MAX_LUNS];
393
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100394 const char *lun_name_format;
395 const char *thread_name;
396
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +0100397 /* Callback function to call when thread exits. If no
398 * callback is set or it returns value lower then zero MSF
399 * will force eject all LUNs it operates on (including those
400 * marked as non-removable or with prevent_medium_removal flag
401 * set). */
402 int (*thread_exits)(struct fsg_common *common);
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100403 /* Gadget's private data. */
404 void *private_data;
405
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100406 const char *vendor_name; /* 8 characters or less */
407 const char *product_name; /* 16 characters or less */
408 u16 release;
409
410 char can_stall;
411};
412
413
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100414struct fsg_dev {
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100415 struct usb_function function;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100416 struct usb_gadget *gadget; /* Copy of cdev->gadget */
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100417 struct fsg_common *common;
418
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100419 u16 interface_number;
420
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100421 unsigned int bulk_in_enabled:1;
422 unsigned int bulk_out_enabled:1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100423
424 unsigned long atomic_bitflags;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100425#define IGNORE_BULK_OUT 0
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100426
427 struct usb_ep *bulk_in;
428 struct usb_ep *bulk_out;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100429};
430
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100431
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100432static inline int __fsg_is_set(struct fsg_common *common,
433 const char *func, unsigned line)
434{
435 if (common->fsg)
436 return 1;
437 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
438 return 0;
439}
440
441#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
442
443
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100444static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
445{
446 return container_of(f, struct fsg_dev, function);
447}
448
449
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100450typedef void (*fsg_routine_t)(struct fsg_dev *);
451
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100452static int exception_in_progress(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100453{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100454 return common->state > FSG_STATE_IDLE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100455}
456
457/* Make bulk-out requests be divisible by the maxpacket size */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100458static void set_bulk_out_req_length(struct fsg_common *common,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100459 struct fsg_buffhd *bh, unsigned int length)
460{
461 unsigned int rem;
462
463 bh->bulk_out_intended_length = length;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100464 rem = length % common->bulk_out_maxpacket;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100465 if (rem > 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100466 length += common->bulk_out_maxpacket - rem;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100467 bh->outreq->length = length;
468}
469
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100470/*-------------------------------------------------------------------------*/
471
472static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
473{
474 const char *name;
475
476 if (ep == fsg->bulk_in)
477 name = "bulk-in";
478 else if (ep == fsg->bulk_out)
479 name = "bulk-out";
480 else
481 name = ep->name;
482 DBG(fsg, "%s set halt\n", name);
483 return usb_ep_set_halt(ep);
484}
485
486
487/*-------------------------------------------------------------------------*/
488
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100489/* These routines may be called in process context or in_irq */
490
491/* Caller must hold fsg->lock */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100492static void wakeup_thread(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100493{
494 /* Tell the main thread that something has happened */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100495 common->thread_wakeup_needed = 1;
496 if (common->thread_task)
497 wake_up_process(common->thread_task);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100498}
499
500
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100501static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100502{
503 unsigned long flags;
504
505 /* Do nothing if a higher-priority exception is already in progress.
506 * If a lower-or-equal priority exception is in progress, preempt it
507 * and notify the main thread by sending it a signal. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100508 spin_lock_irqsave(&common->lock, flags);
509 if (common->state <= new_state) {
510 common->exception_req_tag = common->ep0_req_tag;
511 common->state = new_state;
512 if (common->thread_task)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100513 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100514 common->thread_task);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100515 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100516 spin_unlock_irqrestore(&common->lock, flags);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100517}
518
519
520/*-------------------------------------------------------------------------*/
521
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100522static int ep0_queue(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100523{
524 int rc;
525
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100526 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
527 common->ep0->driver_data = common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100528 if (rc != 0 && rc != -ESHUTDOWN) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100529 /* We can't do much more than wait for a reset */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100530 WARNING(common, "error in submission: %s --> %d\n",
531 common->ep0->name, rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100532 }
533 return rc;
534}
535
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100536/*-------------------------------------------------------------------------*/
537
538/* Bulk and interrupt endpoint completion handlers.
539 * These always run in_irq. */
540
541static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
542{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100543 struct fsg_common *common = ep->driver_data;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100544 struct fsg_buffhd *bh = req->context;
545
546 if (req->status || req->actual != req->length)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100547 DBG(common, "%s --> %d, %u/%u\n", __func__,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100548 req->status, req->actual, req->length);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100549 if (req->status == -ECONNRESET) /* Request was cancelled */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100550 usb_ep_fifo_flush(ep);
551
552 /* Hold the lock while we update the request and buffer states */
553 smp_wmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100554 spin_lock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100555 bh->inreq_busy = 0;
556 bh->state = BUF_STATE_EMPTY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100557 wakeup_thread(common);
558 spin_unlock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100559}
560
561static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
562{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100563 struct fsg_common *common = ep->driver_data;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100564 struct fsg_buffhd *bh = req->context;
565
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100566 dump_msg(common, "bulk-out", req->buf, req->actual);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100567 if (req->status || req->actual != bh->bulk_out_intended_length)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100568 DBG(common, "%s --> %d, %u/%u\n", __func__,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100569 req->status, req->actual,
570 bh->bulk_out_intended_length);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100571 if (req->status == -ECONNRESET) /* Request was cancelled */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100572 usb_ep_fifo_flush(ep);
573
574 /* Hold the lock while we update the request and buffer states */
575 smp_wmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100576 spin_lock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100577 bh->outreq_busy = 0;
578 bh->state = BUF_STATE_FULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100579 wakeup_thread(common);
580 spin_unlock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100581}
582
583
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100584/*-------------------------------------------------------------------------*/
585
586/* Ep0 class-specific handlers. These always run in_irq. */
587
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100588static int fsg_setup(struct usb_function *f,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100589 const struct usb_ctrlrequest *ctrl)
590{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100591 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100592 struct usb_request *req = fsg->common->ep0req;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100593 u16 w_index = le16_to_cpu(ctrl->wIndex);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100594 u16 w_value = le16_to_cpu(ctrl->wValue);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100595 u16 w_length = le16_to_cpu(ctrl->wLength);
596
Michal Nazarewiczb894f602010-06-25 16:29:28 +0200597 if (!fsg_is_set(fsg->common))
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100598 return -EOPNOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100599
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100600 switch (ctrl->bRequest) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100601
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100602 case USB_BULK_RESET_REQUEST:
603 if (ctrl->bRequestType !=
604 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100605 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100606 if (w_index != fsg->interface_number || w_value != 0)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100607 return -EDOM;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100608
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100609 /* Raise an exception to stop the current operation
610 * and reinitialize our state. */
611 DBG(fsg, "bulk reset request\n");
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100612 raise_exception(fsg->common, FSG_STATE_RESET);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100613 return DELAYED_STATUS;
614
615 case USB_BULK_GET_MAX_LUN_REQUEST:
616 if (ctrl->bRequestType !=
617 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100618 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100619 if (w_index != fsg->interface_number || w_value != 0)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100620 return -EDOM;
621 VDBG(fsg, "get max LUN\n");
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100622 *(u8 *) req->buf = fsg->common->nluns - 1;
Michal Nazarewiczb00ce112010-01-27 11:14:28 +0100623
624 /* Respond with data/status */
Michal Nazarewiczd7e18a92010-02-03 11:37:17 +0100625 req->length = min((u16)1, w_length);
Michal Nazarewiczb00ce112010-01-27 11:14:28 +0100626 fsg->common->ep0req_name =
627 ctrl->bRequestType & USB_DIR_IN ? "ep0-in" : "ep0-out";
628 return ep0_queue(fsg->common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100629 }
630
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100631 VDBG(fsg,
632 "unknown class-specific control req "
633 "%02x.%02x v%04x i%04x l%u\n",
634 ctrl->bRequestType, ctrl->bRequest,
635 le16_to_cpu(ctrl->wValue), w_index, w_length);
636 return -EOPNOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100637}
638
639
640/*-------------------------------------------------------------------------*/
641
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100642/* All the following routines run in process context */
643
644
645/* Use this for bulk or interrupt transfers, not ep0 */
646static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
647 struct usb_request *req, int *pbusy,
648 enum fsg_buffer_state *state)
649{
650 int rc;
651
652 if (ep == fsg->bulk_in)
653 dump_msg(fsg, "bulk-in", req->buf, req->length);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100654
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100655 spin_lock_irq(&fsg->common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100656 *pbusy = 1;
657 *state = BUF_STATE_BUSY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100658 spin_unlock_irq(&fsg->common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100659 rc = usb_ep_queue(ep, req, GFP_KERNEL);
660 if (rc != 0) {
661 *pbusy = 0;
662 *state = BUF_STATE_EMPTY;
663
664 /* We can't do much more than wait for a reset */
665
666 /* Note: currently the net2280 driver fails zero-length
667 * submissions if DMA is enabled. */
668 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
669 req->length == 0))
670 WARNING(fsg, "error in submission: %s --> %d\n",
671 ep->name, rc);
672 }
673}
674
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100675#define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \
676 if (fsg_is_set(common)) \
677 start_transfer((common)->fsg, (common)->fsg->ep_name, \
678 req, pbusy, state); \
679 else
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100680
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100681#define START_TRANSFER(common, ep_name, req, pbusy, state) \
682 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
683
684
685
686static int sleep_thread(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100687{
688 int rc = 0;
689
690 /* Wait until a signal arrives or we are woken up */
691 for (;;) {
692 try_to_freeze();
693 set_current_state(TASK_INTERRUPTIBLE);
694 if (signal_pending(current)) {
695 rc = -EINTR;
696 break;
697 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100698 if (common->thread_wakeup_needed)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100699 break;
700 schedule();
701 }
702 __set_current_state(TASK_RUNNING);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100703 common->thread_wakeup_needed = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100704 return rc;
705}
706
707
708/*-------------------------------------------------------------------------*/
709
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100710static int do_read(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100711{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100712 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100713 u32 lba;
714 struct fsg_buffhd *bh;
715 int rc;
716 u32 amount_left;
717 loff_t file_offset, file_offset_tmp;
718 unsigned int amount;
719 unsigned int partial_page;
720 ssize_t nread;
721
722 /* Get the starting Logical Block Address and check that it's
723 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100724 if (common->cmnd[0] == SC_READ_6)
725 lba = get_unaligned_be24(&common->cmnd[1]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100726 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100727 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100728
729 /* We allow DPO (Disable Page Out = don't save data in the
730 * cache) and FUA (Force Unit Access = don't read from the
731 * cache), but we don't implement them. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100732 if ((common->cmnd[1] & ~0x18) != 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100733 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
734 return -EINVAL;
735 }
736 }
737 if (lba >= curlun->num_sectors) {
738 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
739 return -EINVAL;
740 }
741 file_offset = ((loff_t) lba) << 9;
742
743 /* Carry out the file reads */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100744 amount_left = common->data_size_from_cmnd;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100745 if (unlikely(amount_left == 0))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100746 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100747
748 for (;;) {
749
750 /* Figure out how much we need to read:
751 * Try to read the remaining amount.
752 * But don't read more than the buffer size.
753 * And don't try to read past the end of the file.
754 * Finally, if we're not at a page boundary, don't read past
755 * the next page.
756 * If this means reading 0 then we were asked to read past
757 * the end of file. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100758 amount = min(amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100759 amount = min((loff_t) amount,
760 curlun->file_length - file_offset);
761 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
762 if (partial_page > 0)
763 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
764 partial_page);
765
766 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100767 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100768 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100769 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100770 if (rc)
771 return rc;
772 }
773
774 /* If we were asked to read past the end of file,
775 * end with an empty buffer. */
776 if (amount == 0) {
777 curlun->sense_data =
778 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
779 curlun->sense_data_info = file_offset >> 9;
780 curlun->info_valid = 1;
781 bh->inreq->length = 0;
782 bh->state = BUF_STATE_FULL;
783 break;
784 }
785
786 /* Perform the read */
787 file_offset_tmp = file_offset;
788 nread = vfs_read(curlun->filp,
789 (char __user *) bh->buf,
790 amount, &file_offset_tmp);
791 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
792 (unsigned long long) file_offset,
793 (int) nread);
794 if (signal_pending(current))
795 return -EINTR;
796
797 if (nread < 0) {
798 LDBG(curlun, "error in file read: %d\n",
799 (int) nread);
800 nread = 0;
801 } else if (nread < amount) {
802 LDBG(curlun, "partial file read: %d/%u\n",
803 (int) nread, amount);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100804 nread -= (nread & 511); /* Round down to a block */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100805 }
806 file_offset += nread;
807 amount_left -= nread;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100808 common->residue -= nread;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100809 bh->inreq->length = nread;
810 bh->state = BUF_STATE_FULL;
811
812 /* If an error occurred, report it and its position */
813 if (nread < amount) {
814 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
815 curlun->sense_data_info = file_offset >> 9;
816 curlun->info_valid = 1;
817 break;
818 }
819
820 if (amount_left == 0)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100821 break; /* No more left to read */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100822
823 /* Send this buffer and go read some more */
824 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100825 START_TRANSFER_OR(common, bulk_in, bh->inreq,
826 &bh->inreq_busy, &bh->state)
827 /* Don't know what to do if
828 * common->fsg is NULL */
829 return -EIO;
830 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100831 }
832
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100833 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100834}
835
836
837/*-------------------------------------------------------------------------*/
838
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100839static int do_write(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100840{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100841 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100842 u32 lba;
843 struct fsg_buffhd *bh;
844 int get_some_more;
845 u32 amount_left_to_req, amount_left_to_write;
846 loff_t usb_offset, file_offset, file_offset_tmp;
847 unsigned int amount;
848 unsigned int partial_page;
849 ssize_t nwritten;
850 int rc;
851
852 if (curlun->ro) {
853 curlun->sense_data = SS_WRITE_PROTECTED;
854 return -EINVAL;
855 }
856 spin_lock(&curlun->filp->f_lock);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100857 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100858 spin_unlock(&curlun->filp->f_lock);
859
860 /* Get the starting Logical Block Address and check that it's
861 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100862 if (common->cmnd[0] == SC_WRITE_6)
863 lba = get_unaligned_be24(&common->cmnd[1]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100864 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100865 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100866
867 /* We allow DPO (Disable Page Out = don't save data in the
868 * cache) and FUA (Force Unit Access = write directly to the
869 * medium). We don't implement DPO; we implement FUA by
870 * performing synchronous output. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100871 if (common->cmnd[1] & ~0x18) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100872 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
873 return -EINVAL;
874 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100875 if (common->cmnd[1] & 0x08) { /* FUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100876 spin_lock(&curlun->filp->f_lock);
877 curlun->filp->f_flags |= O_SYNC;
878 spin_unlock(&curlun->filp->f_lock);
879 }
880 }
881 if (lba >= curlun->num_sectors) {
882 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
883 return -EINVAL;
884 }
885
886 /* Carry out the file writes */
887 get_some_more = 1;
888 file_offset = usb_offset = ((loff_t) lba) << 9;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100889 amount_left_to_req = common->data_size_from_cmnd;
890 amount_left_to_write = common->data_size_from_cmnd;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100891
892 while (amount_left_to_write > 0) {
893
894 /* Queue a request for more data from the host */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100895 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100896 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
897
898 /* Figure out how much we want to get:
899 * Try to get the remaining amount.
900 * But don't get more than the buffer size.
901 * And don't try to go past the end of the file.
902 * If we're not at a page boundary,
903 * don't go past the next page.
904 * If this means getting 0, then we were asked
905 * to write past the end of file.
906 * Finally, round down to a block boundary. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100907 amount = min(amount_left_to_req, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100908 amount = min((loff_t) amount, curlun->file_length -
909 usb_offset);
910 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
911 if (partial_page > 0)
912 amount = min(amount,
913 (unsigned int) PAGE_CACHE_SIZE - partial_page);
914
915 if (amount == 0) {
916 get_some_more = 0;
917 curlun->sense_data =
918 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
919 curlun->sense_data_info = usb_offset >> 9;
920 curlun->info_valid = 1;
921 continue;
922 }
923 amount -= (amount & 511);
924 if (amount == 0) {
925
926 /* Why were we were asked to transfer a
927 * partial block? */
928 get_some_more = 0;
929 continue;
930 }
931
932 /* Get the next buffer */
933 usb_offset += amount;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100934 common->usb_amount_left -= amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100935 amount_left_to_req -= amount;
936 if (amount_left_to_req == 0)
937 get_some_more = 0;
938
939 /* amount is always divisible by 512, hence by
940 * the bulk-out maxpacket size */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100941 bh->outreq->length = amount;
942 bh->bulk_out_intended_length = amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100943 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100944 START_TRANSFER_OR(common, bulk_out, bh->outreq,
945 &bh->outreq_busy, &bh->state)
946 /* Don't know what to do if
947 * common->fsg is NULL */
948 return -EIO;
949 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100950 continue;
951 }
952
953 /* Write the received data to the backing file */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100954 bh = common->next_buffhd_to_drain;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100955 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100956 break; /* We stopped early */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100957 if (bh->state == BUF_STATE_FULL) {
958 smp_rmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100959 common->next_buffhd_to_drain = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100960 bh->state = BUF_STATE_EMPTY;
961
962 /* Did something go wrong with the transfer? */
963 if (bh->outreq->status != 0) {
964 curlun->sense_data = SS_COMMUNICATION_FAILURE;
965 curlun->sense_data_info = file_offset >> 9;
966 curlun->info_valid = 1;
967 break;
968 }
969
970 amount = bh->outreq->actual;
971 if (curlun->file_length - file_offset < amount) {
972 LERROR(curlun,
973 "write %u @ %llu beyond end %llu\n",
974 amount, (unsigned long long) file_offset,
975 (unsigned long long) curlun->file_length);
976 amount = curlun->file_length - file_offset;
977 }
978
979 /* Perform the write */
980 file_offset_tmp = file_offset;
981 nwritten = vfs_write(curlun->filp,
982 (char __user *) bh->buf,
983 amount, &file_offset_tmp);
984 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
985 (unsigned long long) file_offset,
986 (int) nwritten);
987 if (signal_pending(current))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100988 return -EINTR; /* Interrupted! */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100989
990 if (nwritten < 0) {
991 LDBG(curlun, "error in file write: %d\n",
992 (int) nwritten);
993 nwritten = 0;
994 } else if (nwritten < amount) {
995 LDBG(curlun, "partial file write: %d/%u\n",
996 (int) nwritten, amount);
997 nwritten -= (nwritten & 511);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100998 /* Round down to a block */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100999 }
1000 file_offset += nwritten;
1001 amount_left_to_write -= nwritten;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001002 common->residue -= nwritten;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001003
1004 /* If an error occurred, report it and its position */
1005 if (nwritten < amount) {
1006 curlun->sense_data = SS_WRITE_ERROR;
1007 curlun->sense_data_info = file_offset >> 9;
1008 curlun->info_valid = 1;
1009 break;
1010 }
1011
1012 /* Did the host decide to stop early? */
1013 if (bh->outreq->actual != bh->outreq->length) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001014 common->short_packet_received = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001015 break;
1016 }
1017 continue;
1018 }
1019
1020 /* Wait for something to happen */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001021 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001022 if (rc)
1023 return rc;
1024 }
1025
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001026 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001027}
1028
1029
1030/*-------------------------------------------------------------------------*/
1031
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001032static int do_synchronize_cache(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001033{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001034 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001035 int rc;
1036
1037 /* We ignore the requested LBA and write out all file's
1038 * dirty data buffers. */
1039 rc = fsg_lun_fsync_sub(curlun);
1040 if (rc)
1041 curlun->sense_data = SS_WRITE_ERROR;
1042 return 0;
1043}
1044
1045
1046/*-------------------------------------------------------------------------*/
1047
1048static void invalidate_sub(struct fsg_lun *curlun)
1049{
1050 struct file *filp = curlun->filp;
1051 struct inode *inode = filp->f_path.dentry->d_inode;
1052 unsigned long rc;
1053
1054 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001055 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001056}
1057
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001058static int do_verify(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001059{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001060 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001061 u32 lba;
1062 u32 verification_length;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001063 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001064 loff_t file_offset, file_offset_tmp;
1065 u32 amount_left;
1066 unsigned int amount;
1067 ssize_t nread;
1068
1069 /* Get the starting Logical Block Address and check that it's
1070 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001071 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001072 if (lba >= curlun->num_sectors) {
1073 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1074 return -EINVAL;
1075 }
1076
1077 /* We allow DPO (Disable Page Out = don't save data in the
1078 * cache) but we don't implement it. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001079 if (common->cmnd[1] & ~0x10) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001080 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1081 return -EINVAL;
1082 }
1083
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001084 verification_length = get_unaligned_be16(&common->cmnd[7]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001085 if (unlikely(verification_length == 0))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001086 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001087
1088 /* Prepare to carry out the file verify */
1089 amount_left = verification_length << 9;
1090 file_offset = ((loff_t) lba) << 9;
1091
1092 /* Write out all the dirty buffers before invalidating them */
1093 fsg_lun_fsync_sub(curlun);
1094 if (signal_pending(current))
1095 return -EINTR;
1096
1097 invalidate_sub(curlun);
1098 if (signal_pending(current))
1099 return -EINTR;
1100
1101 /* Just try to read the requested blocks */
1102 while (amount_left > 0) {
1103
1104 /* Figure out how much we need to read:
1105 * Try to read the remaining amount, but not more than
1106 * the buffer size.
1107 * And don't try to read past the end of the file.
1108 * If this means reading 0 then we were asked to read
1109 * past the end of file. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001110 amount = min(amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001111 amount = min((loff_t) amount,
1112 curlun->file_length - file_offset);
1113 if (amount == 0) {
1114 curlun->sense_data =
1115 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1116 curlun->sense_data_info = file_offset >> 9;
1117 curlun->info_valid = 1;
1118 break;
1119 }
1120
1121 /* Perform the read */
1122 file_offset_tmp = file_offset;
1123 nread = vfs_read(curlun->filp,
1124 (char __user *) bh->buf,
1125 amount, &file_offset_tmp);
1126 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1127 (unsigned long long) file_offset,
1128 (int) nread);
1129 if (signal_pending(current))
1130 return -EINTR;
1131
1132 if (nread < 0) {
1133 LDBG(curlun, "error in file verify: %d\n",
1134 (int) nread);
1135 nread = 0;
1136 } else if (nread < amount) {
1137 LDBG(curlun, "partial file verify: %d/%u\n",
1138 (int) nread, amount);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001139 nread -= (nread & 511); /* Round down to a sector */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001140 }
1141 if (nread == 0) {
1142 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1143 curlun->sense_data_info = file_offset >> 9;
1144 curlun->info_valid = 1;
1145 break;
1146 }
1147 file_offset += nread;
1148 amount_left -= nread;
1149 }
1150 return 0;
1151}
1152
1153
1154/*-------------------------------------------------------------------------*/
1155
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001156static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001157{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001158 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001159 u8 *buf = (u8 *) bh->buf;
1160
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001161 if (!curlun) { /* Unsupported LUNs are okay */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001162 common->bad_lun_okay = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001163 memset(buf, 0, 36);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001164 buf[0] = 0x7f; /* Unsupported, no device-type */
1165 buf[4] = 31; /* Additional length */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001166 return 36;
1167 }
1168
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001169 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK;
1170 buf[1] = curlun->removable ? 0x80 : 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001171 buf[2] = 2; /* ANSI SCSI level 2 */
1172 buf[3] = 2; /* SCSI-2 INQUIRY data format */
1173 buf[4] = 31; /* Additional length */
1174 buf[5] = 0; /* No special options */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001175 buf[6] = 0;
1176 buf[7] = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001177 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001178 return 36;
1179}
1180
1181
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001182static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001183{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001184 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001185 u8 *buf = (u8 *) bh->buf;
1186 u32 sd, sdinfo;
1187 int valid;
1188
1189 /*
1190 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1191 *
1192 * If a REQUEST SENSE command is received from an initiator
1193 * with a pending unit attention condition (before the target
1194 * generates the contingent allegiance condition), then the
1195 * target shall either:
1196 * a) report any pending sense data and preserve the unit
1197 * attention condition on the logical unit, or,
1198 * b) report the unit attention condition, may discard any
1199 * pending sense data, and clear the unit attention
1200 * condition on the logical unit for that initiator.
1201 *
1202 * FSG normally uses option a); enable this code to use option b).
1203 */
1204#if 0
1205 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1206 curlun->sense_data = curlun->unit_attention_data;
1207 curlun->unit_attention_data = SS_NO_SENSE;
1208 }
1209#endif
1210
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001211 if (!curlun) { /* Unsupported LUNs are okay */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001212 common->bad_lun_okay = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001213 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1214 sdinfo = 0;
1215 valid = 0;
1216 } else {
1217 sd = curlun->sense_data;
1218 sdinfo = curlun->sense_data_info;
1219 valid = curlun->info_valid << 7;
1220 curlun->sense_data = SS_NO_SENSE;
1221 curlun->sense_data_info = 0;
1222 curlun->info_valid = 0;
1223 }
1224
1225 memset(buf, 0, 18);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001226 buf[0] = valid | 0x70; /* Valid, current error */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001227 buf[2] = SK(sd);
1228 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001229 buf[7] = 18 - 8; /* Additional sense length */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001230 buf[12] = ASC(sd);
1231 buf[13] = ASCQ(sd);
1232 return 18;
1233}
1234
1235
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001236static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001237{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001238 struct fsg_lun *curlun = common->curlun;
1239 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1240 int pmi = common->cmnd[8];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001241 u8 *buf = (u8 *) bh->buf;
1242
1243 /* Check the PMI and LBA fields */
1244 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1245 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1246 return -EINVAL;
1247 }
1248
1249 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1250 /* Max logical block */
1251 put_unaligned_be32(512, &buf[4]); /* Block length */
1252 return 8;
1253}
1254
1255
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001256static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001257{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001258 struct fsg_lun *curlun = common->curlun;
1259 int msf = common->cmnd[1] & 0x02;
1260 u32 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001261 u8 *buf = (u8 *) bh->buf;
1262
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001263 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001264 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1265 return -EINVAL;
1266 }
1267 if (lba >= curlun->num_sectors) {
1268 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1269 return -EINVAL;
1270 }
1271
1272 memset(buf, 0, 8);
1273 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1274 store_cdrom_address(&buf[4], msf, lba);
1275 return 8;
1276}
1277
1278
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001279static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001280{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001281 struct fsg_lun *curlun = common->curlun;
1282 int msf = common->cmnd[1] & 0x02;
1283 int start_track = common->cmnd[6];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001284 u8 *buf = (u8 *) bh->buf;
1285
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001286 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001287 start_track > 1) {
1288 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1289 return -EINVAL;
1290 }
1291
1292 memset(buf, 0, 20);
1293 buf[1] = (20-2); /* TOC data length */
1294 buf[2] = 1; /* First track number */
1295 buf[3] = 1; /* Last track number */
1296 buf[5] = 0x16; /* Data track, copying allowed */
1297 buf[6] = 0x01; /* Only track is number 1 */
1298 store_cdrom_address(&buf[8], msf, 0);
1299
1300 buf[13] = 0x16; /* Lead-out track is data */
1301 buf[14] = 0xAA; /* Lead-out track number */
1302 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1303 return 20;
1304}
1305
1306
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001307static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001308{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001309 struct fsg_lun *curlun = common->curlun;
1310 int mscmnd = common->cmnd[0];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001311 u8 *buf = (u8 *) bh->buf;
1312 u8 *buf0 = buf;
1313 int pc, page_code;
1314 int changeable_values, all_pages;
1315 int valid_page = 0;
1316 int len, limit;
1317
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001318 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001319 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1320 return -EINVAL;
1321 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001322 pc = common->cmnd[2] >> 6;
1323 page_code = common->cmnd[2] & 0x3f;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001324 if (pc == 3) {
1325 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1326 return -EINVAL;
1327 }
1328 changeable_values = (pc == 1);
1329 all_pages = (page_code == 0x3f);
1330
1331 /* Write the mode parameter header. Fixed values are: default
1332 * medium type, no cache control (DPOFUA), and no block descriptors.
1333 * The only variable value is the WriteProtect bit. We will fill in
1334 * the mode data length later. */
1335 memset(buf, 0, 8);
1336 if (mscmnd == SC_MODE_SENSE_6) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001337 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001338 buf += 4;
1339 limit = 255;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001340 } else { /* SC_MODE_SENSE_10 */
1341 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001342 buf += 8;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001343 limit = 65535; /* Should really be FSG_BUFLEN */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001344 }
1345
1346 /* No block descriptors */
1347
1348 /* The mode pages, in numerical order. The only page we support
1349 * is the Caching page. */
1350 if (page_code == 0x08 || all_pages) {
1351 valid_page = 1;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001352 buf[0] = 0x08; /* Page code */
1353 buf[1] = 10; /* Page length */
1354 memset(buf+2, 0, 10); /* None of the fields are changeable */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001355
1356 if (!changeable_values) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001357 buf[2] = 0x04; /* Write cache enable, */
1358 /* Read cache not disabled */
1359 /* No cache retention priorities */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001360 put_unaligned_be16(0xffff, &buf[4]);
1361 /* Don't disable prefetch */
1362 /* Minimum prefetch = 0 */
1363 put_unaligned_be16(0xffff, &buf[8]);
1364 /* Maximum prefetch */
1365 put_unaligned_be16(0xffff, &buf[10]);
1366 /* Maximum prefetch ceiling */
1367 }
1368 buf += 12;
1369 }
1370
1371 /* Check that a valid page was requested and the mode data length
1372 * isn't too long. */
1373 len = buf - buf0;
1374 if (!valid_page || len > limit) {
1375 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1376 return -EINVAL;
1377 }
1378
1379 /* Store the mode data length */
1380 if (mscmnd == SC_MODE_SENSE_6)
1381 buf0[0] = len - 1;
1382 else
1383 put_unaligned_be16(len - 2, buf0);
1384 return len;
1385}
1386
1387
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001388static int do_start_stop(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001389{
Fabien Chouteau31436a12010-04-26 12:34:54 +02001390 struct fsg_lun *curlun = common->curlun;
1391 int loej, start;
1392
1393 if (!curlun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001394 return -EINVAL;
Fabien Chouteau31436a12010-04-26 12:34:54 +02001395 } else if (!curlun->removable) {
1396 curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001397 return -EINVAL;
1398 }
Fabien Chouteau31436a12010-04-26 12:34:54 +02001399
1400 loej = common->cmnd[4] & 0x02;
1401 start = common->cmnd[4] & 0x01;
1402
1403 /* eject code from file_storage.c:do_start_stop() */
1404
1405 if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
1406 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
1407 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1408 return -EINVAL;
1409 }
1410
1411 if (!start) {
1412 /* Are we allowed to unload the media? */
1413 if (curlun->prevent_medium_removal) {
1414 LDBG(curlun, "unload attempt prevented\n");
1415 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1416 return -EINVAL;
1417 }
1418 if (loej) { /* Simulate an unload/eject */
1419 up_read(&common->filesem);
1420 down_write(&common->filesem);
1421 fsg_lun_close(curlun);
1422 up_write(&common->filesem);
1423 down_read(&common->filesem);
1424 }
1425 } else {
1426
1427 /* Our emulation doesn't support mounting; the medium is
1428 * available for use as soon as it is loaded. */
1429 if (!fsg_lun_is_open(curlun)) {
1430 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1431 return -EINVAL;
1432 }
1433 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001434 return 0;
1435}
1436
1437
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001438static int do_prevent_allow(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001439{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001440 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001441 int prevent;
1442
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001443 if (!common->curlun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001444 return -EINVAL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001445 } else if (!common->curlun->removable) {
1446 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001447 return -EINVAL;
1448 }
1449
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001450 prevent = common->cmnd[4] & 0x01;
1451 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001452 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1453 return -EINVAL;
1454 }
1455
1456 if (curlun->prevent_medium_removal && !prevent)
1457 fsg_lun_fsync_sub(curlun);
1458 curlun->prevent_medium_removal = prevent;
1459 return 0;
1460}
1461
1462
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001463static int do_read_format_capacities(struct fsg_common *common,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001464 struct fsg_buffhd *bh)
1465{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001466 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001467 u8 *buf = (u8 *) bh->buf;
1468
1469 buf[0] = buf[1] = buf[2] = 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001470 buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001471 buf += 4;
1472
1473 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1474 /* Number of blocks */
1475 put_unaligned_be32(512, &buf[4]); /* Block length */
1476 buf[4] = 0x02; /* Current capacity */
1477 return 12;
1478}
1479
1480
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001481static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001482{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001483 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001484
1485 /* We don't support MODE SELECT */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001486 if (curlun)
1487 curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001488 return -EINVAL;
1489}
1490
1491
1492/*-------------------------------------------------------------------------*/
1493
1494static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1495{
1496 int rc;
1497
1498 rc = fsg_set_halt(fsg, fsg->bulk_in);
1499 if (rc == -EAGAIN)
1500 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1501 while (rc != 0) {
1502 if (rc != -EAGAIN) {
1503 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1504 rc = 0;
1505 break;
1506 }
1507
1508 /* Wait for a short time and then try again */
1509 if (msleep_interruptible(100) != 0)
1510 return -EINTR;
1511 rc = usb_ep_set_halt(fsg->bulk_in);
1512 }
1513 return rc;
1514}
1515
1516static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1517{
1518 int rc;
1519
1520 DBG(fsg, "bulk-in set wedge\n");
1521 rc = usb_ep_set_wedge(fsg->bulk_in);
1522 if (rc == -EAGAIN)
1523 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1524 while (rc != 0) {
1525 if (rc != -EAGAIN) {
1526 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1527 rc = 0;
1528 break;
1529 }
1530
1531 /* Wait for a short time and then try again */
1532 if (msleep_interruptible(100) != 0)
1533 return -EINTR;
1534 rc = usb_ep_set_wedge(fsg->bulk_in);
1535 }
1536 return rc;
1537}
1538
1539static int pad_with_zeros(struct fsg_dev *fsg)
1540{
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001541 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001542 u32 nkeep = bh->inreq->length;
1543 u32 nsend;
1544 int rc;
1545
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001546 bh->state = BUF_STATE_EMPTY; /* For the first iteration */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001547 fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1548 while (fsg->common->usb_amount_left > 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001549
1550 /* Wait for the next buffer to be free */
1551 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001552 rc = sleep_thread(fsg->common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001553 if (rc)
1554 return rc;
1555 }
1556
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001557 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001558 memset(bh->buf + nkeep, 0, nsend - nkeep);
1559 bh->inreq->length = nsend;
1560 bh->inreq->zero = 0;
1561 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1562 &bh->inreq_busy, &bh->state);
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001563 bh = fsg->common->next_buffhd_to_fill = bh->next;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001564 fsg->common->usb_amount_left -= nsend;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001565 nkeep = 0;
1566 }
1567 return 0;
1568}
1569
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001570static int throw_away_data(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001571{
1572 struct fsg_buffhd *bh;
1573 u32 amount;
1574 int rc;
1575
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001576 for (bh = common->next_buffhd_to_drain;
1577 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1578 bh = common->next_buffhd_to_drain) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001579
1580 /* Throw away the data in a filled buffer */
1581 if (bh->state == BUF_STATE_FULL) {
1582 smp_rmb();
1583 bh->state = BUF_STATE_EMPTY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001584 common->next_buffhd_to_drain = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001585
1586 /* A short packet or an error ends everything */
1587 if (bh->outreq->actual != bh->outreq->length ||
1588 bh->outreq->status != 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001589 raise_exception(common,
1590 FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001591 return -EINTR;
1592 }
1593 continue;
1594 }
1595
1596 /* Try to submit another request if we need one */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001597 bh = common->next_buffhd_to_fill;
1598 if (bh->state == BUF_STATE_EMPTY
1599 && common->usb_amount_left > 0) {
1600 amount = min(common->usb_amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001601
1602 /* amount is always divisible by 512, hence by
1603 * the bulk-out maxpacket size */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001604 bh->outreq->length = amount;
1605 bh->bulk_out_intended_length = amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001606 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001607 START_TRANSFER_OR(common, bulk_out, bh->outreq,
1608 &bh->outreq_busy, &bh->state)
1609 /* Don't know what to do if
1610 * common->fsg is NULL */
1611 return -EIO;
1612 common->next_buffhd_to_fill = bh->next;
1613 common->usb_amount_left -= amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001614 continue;
1615 }
1616
1617 /* Otherwise wait for something to happen */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001618 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001619 if (rc)
1620 return rc;
1621 }
1622 return 0;
1623}
1624
1625
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001626static int finish_reply(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001627{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001628 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001629 int rc = 0;
1630
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001631 switch (common->data_dir) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001632 case DATA_DIR_NONE:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001633 break; /* Nothing to send */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001634
1635 /* If we don't know whether the host wants to read or write,
1636 * this must be CB or CBI with an unknown command. We mustn't
1637 * try to send or receive any data. So stall both bulk pipes
1638 * if we can and wait for a reset. */
1639 case DATA_DIR_UNKNOWN:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001640 if (!common->can_stall) {
1641 /* Nothing */
1642 } else if (fsg_is_set(common)) {
1643 fsg_set_halt(common->fsg, common->fsg->bulk_out);
1644 rc = halt_bulk_in_endpoint(common->fsg);
1645 } else {
1646 /* Don't know what to do if common->fsg is NULL */
1647 rc = -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001648 }
1649 break;
1650
1651 /* All but the last buffer of data must have already been sent */
1652 case DATA_DIR_TO_HOST:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001653 if (common->data_size == 0) {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001654 /* Nothing to send */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001655
1656 /* If there's no residue, simply send the last buffer */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001657 } else if (common->residue == 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001658 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001659 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1660 &bh->inreq_busy, &bh->state)
1661 return -EIO;
1662 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001663
1664 /* For Bulk-only, if we're allowed to stall then send the
1665 * short packet and halt the bulk-in endpoint. If we can't
1666 * stall, pad out the remaining data with 0's. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001667 } else if (common->can_stall) {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001668 bh->inreq->zero = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001669 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1670 &bh->inreq_busy, &bh->state)
1671 /* Don't know what to do if
1672 * common->fsg is NULL */
1673 rc = -EIO;
1674 common->next_buffhd_to_fill = bh->next;
1675 if (common->fsg)
1676 rc = halt_bulk_in_endpoint(common->fsg);
1677 } else if (fsg_is_set(common)) {
1678 rc = pad_with_zeros(common->fsg);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001679 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001680 /* Don't know what to do if common->fsg is NULL */
1681 rc = -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001682 }
1683 break;
1684
1685 /* We have processed all we want from the data the host has sent.
1686 * There may still be outstanding bulk-out requests. */
1687 case DATA_DIR_FROM_HOST:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001688 if (common->residue == 0) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001689 /* Nothing to receive */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001690
1691 /* Did the host stop sending unexpectedly early? */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001692 } else if (common->short_packet_received) {
1693 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001694 rc = -EINTR;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001695
1696 /* We haven't processed all the incoming data. Even though
1697 * we may be allowed to stall, doing so would cause a race.
1698 * The controller may already have ACK'ed all the remaining
1699 * bulk-out packets, in which case the host wouldn't see a
1700 * STALL. Not realizing the endpoint was halted, it wouldn't
1701 * clear the halt -- leading to problems later on. */
1702#if 0
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001703 } else if (common->can_stall) {
1704 if (fsg_is_set(common))
1705 fsg_set_halt(common->fsg,
1706 common->fsg->bulk_out);
1707 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001708 rc = -EINTR;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001709#endif
1710
1711 /* We can't stall. Read in the excess data and throw it
1712 * all away. */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001713 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001714 rc = throw_away_data(common);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001715 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001716 break;
1717 }
1718 return rc;
1719}
1720
1721
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001722static int send_status(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001723{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001724 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001725 struct fsg_buffhd *bh;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001726 struct bulk_cs_wrap *csw;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001727 int rc;
1728 u8 status = USB_STATUS_PASS;
1729 u32 sd, sdinfo = 0;
1730
1731 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001732 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001733 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001734 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001735 if (rc)
1736 return rc;
1737 }
1738
1739 if (curlun) {
1740 sd = curlun->sense_data;
1741 sdinfo = curlun->sense_data_info;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001742 } else if (common->bad_lun_okay)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001743 sd = SS_NO_SENSE;
1744 else
1745 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1746
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001747 if (common->phase_error) {
1748 DBG(common, "sending phase-error status\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001749 status = USB_STATUS_PHASE_ERROR;
1750 sd = SS_INVALID_COMMAND;
1751 } else if (sd != SS_NO_SENSE) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001752 DBG(common, "sending command-failure status\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001753 status = USB_STATUS_FAIL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001754 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001755 " info x%x\n",
1756 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1757 }
1758
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001759 /* Store and send the Bulk-only CSW */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001760 csw = (void *)bh->buf;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001761
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001762 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001763 csw->Tag = common->tag;
1764 csw->Residue = cpu_to_le32(common->residue);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001765 csw->Status = status;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001766
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001767 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1768 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001769 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1770 &bh->inreq_busy, &bh->state)
1771 /* Don't know what to do if common->fsg is NULL */
1772 return -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001773
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001774 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001775 return 0;
1776}
1777
1778
1779/*-------------------------------------------------------------------------*/
1780
1781/* Check whether the command is properly formed and whether its data size
1782 * and direction agree with the values we already have. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001783static int check_command(struct fsg_common *common, int cmnd_size,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001784 enum data_direction data_dir, unsigned int mask,
1785 int needs_medium, const char *name)
1786{
1787 int i;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001788 int lun = common->cmnd[1] >> 5;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001789 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1790 char hdlen[20];
1791 struct fsg_lun *curlun;
1792
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001793 hdlen[0] = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001794 if (common->data_dir != DATA_DIR_UNKNOWN)
1795 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1796 common->data_size);
1797 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001798 name, cmnd_size, dirletter[(int) data_dir],
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001799 common->data_size_from_cmnd, common->cmnd_size, hdlen);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001800
1801 /* We can't reply at all until we know the correct data direction
1802 * and size. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001803 if (common->data_size_from_cmnd == 0)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001804 data_dir = DATA_DIR_NONE;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001805 if (common->data_size < common->data_size_from_cmnd) {
1806 /* Host data size < Device data size is a phase error.
1807 * Carry out the command, but only transfer as much as
1808 * we are allowed. */
1809 common->data_size_from_cmnd = common->data_size;
1810 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001811 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001812 common->residue = common->data_size;
1813 common->usb_amount_left = common->data_size;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001814
1815 /* Conflicting data directions is a phase error */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001816 if (common->data_dir != data_dir
1817 && common->data_size_from_cmnd > 0) {
1818 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001819 return -EINVAL;
1820 }
1821
1822 /* Verify the length of the command itself */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001823 if (cmnd_size != common->cmnd_size) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001824
1825 /* Special case workaround: There are plenty of buggy SCSI
1826 * implementations. Many have issues with cbw->Length
1827 * field passing a wrong command size. For those cases we
1828 * always try to work around the problem by using the length
1829 * sent by the host side provided it is at least as large
1830 * as the correct command length.
1831 * Examples of such cases would be MS-Windows, which issues
1832 * REQUEST SENSE with cbw->Length == 12 where it should
1833 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1834 * REQUEST SENSE with cbw->Length == 10 where it should
1835 * be 6 as well.
1836 */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001837 if (cmnd_size <= common->cmnd_size) {
1838 DBG(common, "%s is buggy! Expected length %d "
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001839 "but we got %d\n", name,
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001840 cmnd_size, common->cmnd_size);
1841 cmnd_size = common->cmnd_size;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001842 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001843 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001844 return -EINVAL;
1845 }
1846 }
1847
1848 /* Check that the LUN values are consistent */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001849 if (common->lun != lun)
1850 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
1851 common->lun, lun);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001852
1853 /* Check the LUN */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001854 if (common->lun >= 0 && common->lun < common->nluns) {
1855 curlun = &common->luns[common->lun];
1856 common->curlun = curlun;
1857 if (common->cmnd[0] != SC_REQUEST_SENSE) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001858 curlun->sense_data = SS_NO_SENSE;
1859 curlun->sense_data_info = 0;
1860 curlun->info_valid = 0;
1861 }
1862 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001863 common->curlun = NULL;
1864 curlun = NULL;
1865 common->bad_lun_okay = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001866
1867 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
1868 * to use unsupported LUNs; all others may not. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001869 if (common->cmnd[0] != SC_INQUIRY &&
1870 common->cmnd[0] != SC_REQUEST_SENSE) {
1871 DBG(common, "unsupported LUN %d\n", common->lun);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001872 return -EINVAL;
1873 }
1874 }
1875
1876 /* If a unit attention condition exists, only INQUIRY and
1877 * REQUEST SENSE commands are allowed; anything else must fail. */
1878 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001879 common->cmnd[0] != SC_INQUIRY &&
1880 common->cmnd[0] != SC_REQUEST_SENSE) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001881 curlun->sense_data = curlun->unit_attention_data;
1882 curlun->unit_attention_data = SS_NO_SENSE;
1883 return -EINVAL;
1884 }
1885
1886 /* Check that only command bytes listed in the mask are non-zero */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001887 common->cmnd[1] &= 0x1f; /* Mask away the LUN */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001888 for (i = 1; i < cmnd_size; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001889 if (common->cmnd[i] && !(mask & (1 << i))) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001890 if (curlun)
1891 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1892 return -EINVAL;
1893 }
1894 }
1895
1896 /* If the medium isn't mounted and the command needs to access
1897 * it, return an error. */
1898 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
1899 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1900 return -EINVAL;
1901 }
1902
1903 return 0;
1904}
1905
1906
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001907static int do_scsi_command(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001908{
1909 struct fsg_buffhd *bh;
1910 int rc;
1911 int reply = -EINVAL;
1912 int i;
1913 static char unknown[16];
1914
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001915 dump_cdb(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001916
1917 /* Wait for the next buffer to become available for data or status */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001918 bh = common->next_buffhd_to_fill;
1919 common->next_buffhd_to_drain = bh;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001920 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001921 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001922 if (rc)
1923 return rc;
1924 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001925 common->phase_error = 0;
1926 common->short_packet_received = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001927
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001928 down_read(&common->filesem); /* We're using the backing file */
1929 switch (common->cmnd[0]) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001930
1931 case SC_INQUIRY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001932 common->data_size_from_cmnd = common->cmnd[4];
1933 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001934 (1<<4), 0,
1935 "INQUIRY");
1936 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001937 reply = do_inquiry(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001938 break;
1939
1940 case SC_MODE_SELECT_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001941 common->data_size_from_cmnd = common->cmnd[4];
1942 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001943 (1<<1) | (1<<4), 0,
1944 "MODE SELECT(6)");
1945 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001946 reply = do_mode_select(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001947 break;
1948
1949 case SC_MODE_SELECT_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001950 common->data_size_from_cmnd =
1951 get_unaligned_be16(&common->cmnd[7]);
1952 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001953 (1<<1) | (3<<7), 0,
1954 "MODE SELECT(10)");
1955 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001956 reply = do_mode_select(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001957 break;
1958
1959 case SC_MODE_SENSE_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001960 common->data_size_from_cmnd = common->cmnd[4];
1961 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001962 (1<<1) | (1<<2) | (1<<4), 0,
1963 "MODE SENSE(6)");
1964 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001965 reply = do_mode_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001966 break;
1967
1968 case SC_MODE_SENSE_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001969 common->data_size_from_cmnd =
1970 get_unaligned_be16(&common->cmnd[7]);
1971 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001972 (1<<1) | (1<<2) | (3<<7), 0,
1973 "MODE SENSE(10)");
1974 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001975 reply = do_mode_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001976 break;
1977
1978 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001979 common->data_size_from_cmnd = 0;
1980 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001981 (1<<4), 0,
1982 "PREVENT-ALLOW MEDIUM REMOVAL");
1983 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001984 reply = do_prevent_allow(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001985 break;
1986
1987 case SC_READ_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001988 i = common->cmnd[4];
1989 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1990 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001991 (7<<1) | (1<<4), 1,
1992 "READ(6)");
1993 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001994 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001995 break;
1996
1997 case SC_READ_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001998 common->data_size_from_cmnd =
1999 get_unaligned_be16(&common->cmnd[7]) << 9;
2000 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002001 (1<<1) | (0xf<<2) | (3<<7), 1,
2002 "READ(10)");
2003 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002004 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002005 break;
2006
2007 case SC_READ_12:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002008 common->data_size_from_cmnd =
2009 get_unaligned_be32(&common->cmnd[6]) << 9;
2010 reply = check_command(common, 12, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002011 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2012 "READ(12)");
2013 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002014 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002015 break;
2016
2017 case SC_READ_CAPACITY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002018 common->data_size_from_cmnd = 8;
2019 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002020 (0xf<<2) | (1<<8), 1,
2021 "READ CAPACITY");
2022 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002023 reply = do_read_capacity(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002024 break;
2025
2026 case SC_READ_HEADER:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002027 if (!common->curlun || !common->curlun->cdrom)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002028 goto unknown_cmnd;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002029 common->data_size_from_cmnd =
2030 get_unaligned_be16(&common->cmnd[7]);
2031 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002032 (3<<7) | (0x1f<<1), 1,
2033 "READ HEADER");
2034 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002035 reply = do_read_header(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002036 break;
2037
2038 case SC_READ_TOC:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002039 if (!common->curlun || !common->curlun->cdrom)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002040 goto unknown_cmnd;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002041 common->data_size_from_cmnd =
2042 get_unaligned_be16(&common->cmnd[7]);
2043 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002044 (7<<6) | (1<<1), 1,
2045 "READ TOC");
2046 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002047 reply = do_read_toc(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002048 break;
2049
2050 case SC_READ_FORMAT_CAPACITIES:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002051 common->data_size_from_cmnd =
2052 get_unaligned_be16(&common->cmnd[7]);
2053 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002054 (3<<7), 1,
2055 "READ FORMAT CAPACITIES");
2056 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002057 reply = do_read_format_capacities(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002058 break;
2059
2060 case SC_REQUEST_SENSE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002061 common->data_size_from_cmnd = common->cmnd[4];
2062 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002063 (1<<4), 0,
2064 "REQUEST SENSE");
2065 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002066 reply = do_request_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002067 break;
2068
2069 case SC_START_STOP_UNIT:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002070 common->data_size_from_cmnd = 0;
2071 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002072 (1<<1) | (1<<4), 0,
2073 "START-STOP UNIT");
2074 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002075 reply = do_start_stop(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002076 break;
2077
2078 case SC_SYNCHRONIZE_CACHE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002079 common->data_size_from_cmnd = 0;
2080 reply = check_command(common, 10, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002081 (0xf<<2) | (3<<7), 1,
2082 "SYNCHRONIZE CACHE");
2083 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002084 reply = do_synchronize_cache(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002085 break;
2086
2087 case SC_TEST_UNIT_READY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002088 common->data_size_from_cmnd = 0;
2089 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002090 0, 1,
2091 "TEST UNIT READY");
2092 break;
2093
2094 /* Although optional, this command is used by MS-Windows. We
2095 * support a minimal version: BytChk must be 0. */
2096 case SC_VERIFY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002097 common->data_size_from_cmnd = 0;
2098 reply = check_command(common, 10, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002099 (1<<1) | (0xf<<2) | (3<<7), 1,
2100 "VERIFY");
2101 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002102 reply = do_verify(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002103 break;
2104
2105 case SC_WRITE_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002106 i = common->cmnd[4];
2107 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2108 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002109 (7<<1) | (1<<4), 1,
2110 "WRITE(6)");
2111 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002112 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002113 break;
2114
2115 case SC_WRITE_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002116 common->data_size_from_cmnd =
2117 get_unaligned_be16(&common->cmnd[7]) << 9;
2118 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002119 (1<<1) | (0xf<<2) | (3<<7), 1,
2120 "WRITE(10)");
2121 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002122 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002123 break;
2124
2125 case SC_WRITE_12:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002126 common->data_size_from_cmnd =
2127 get_unaligned_be32(&common->cmnd[6]) << 9;
2128 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002129 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2130 "WRITE(12)");
2131 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002132 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002133 break;
2134
2135 /* Some mandatory commands that we recognize but don't implement.
2136 * They don't mean much in this setting. It's left as an exercise
2137 * for anyone interested to implement RESERVE and RELEASE in terms
2138 * of Posix locks. */
2139 case SC_FORMAT_UNIT:
2140 case SC_RELEASE:
2141 case SC_RESERVE:
2142 case SC_SEND_DIAGNOSTIC:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002143 /* Fall through */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002144
2145 default:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002146unknown_cmnd:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002147 common->data_size_from_cmnd = 0;
2148 sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2149 reply = check_command(common, common->cmnd_size,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002150 DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2151 if (reply == 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002152 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002153 reply = -EINVAL;
2154 }
2155 break;
2156 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002157 up_read(&common->filesem);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002158
2159 if (reply == -EINTR || signal_pending(current))
2160 return -EINTR;
2161
2162 /* Set up the single reply buffer for finish_reply() */
2163 if (reply == -EINVAL)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002164 reply = 0; /* Error reply length */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002165 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2166 reply = min((u32) reply, common->data_size_from_cmnd);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002167 bh->inreq->length = reply;
2168 bh->state = BUF_STATE_FULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002169 common->residue -= reply;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002170 } /* Otherwise it's already set */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002171
2172 return 0;
2173}
2174
2175
2176/*-------------------------------------------------------------------------*/
2177
2178static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2179{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002180 struct usb_request *req = bh->outreq;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002181 struct fsg_bulk_cb_wrap *cbw = req->buf;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002182 struct fsg_common *common = fsg->common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002183
2184 /* Was this a real packet? Should it be ignored? */
2185 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2186 return -EINVAL;
2187
2188 /* Is the CBW valid? */
2189 if (req->actual != USB_BULK_CB_WRAP_LEN ||
2190 cbw->Signature != cpu_to_le32(
2191 USB_BULK_CB_SIG)) {
2192 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2193 req->actual,
2194 le32_to_cpu(cbw->Signature));
2195
2196 /* The Bulk-only spec says we MUST stall the IN endpoint
2197 * (6.6.1), so it's unavoidable. It also says we must
2198 * retain this state until the next reset, but there's
2199 * no way to tell the controller driver it should ignore
2200 * Clear-Feature(HALT) requests.
2201 *
2202 * We aren't required to halt the OUT endpoint; instead
2203 * we can simply accept and discard any data received
2204 * until the next reset. */
2205 wedge_bulk_in_endpoint(fsg);
2206 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2207 return -EINVAL;
2208 }
2209
2210 /* Is the CBW meaningful? */
2211 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2212 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2213 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2214 "cmdlen %u\n",
2215 cbw->Lun, cbw->Flags, cbw->Length);
2216
2217 /* We can do anything we want here, so let's stall the
2218 * bulk pipes if we are allowed to. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002219 if (common->can_stall) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002220 fsg_set_halt(fsg, fsg->bulk_out);
2221 halt_bulk_in_endpoint(fsg);
2222 }
2223 return -EINVAL;
2224 }
2225
2226 /* Save the command for later */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002227 common->cmnd_size = cbw->Length;
2228 memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002229 if (cbw->Flags & USB_BULK_IN_FLAG)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002230 common->data_dir = DATA_DIR_TO_HOST;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002231 else
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002232 common->data_dir = DATA_DIR_FROM_HOST;
2233 common->data_size = le32_to_cpu(cbw->DataTransferLength);
2234 if (common->data_size == 0)
2235 common->data_dir = DATA_DIR_NONE;
2236 common->lun = cbw->Lun;
2237 common->tag = cbw->Tag;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002238 return 0;
2239}
2240
2241
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002242static int get_next_command(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002243{
2244 struct fsg_buffhd *bh;
2245 int rc = 0;
2246
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002247 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002248 bh = common->next_buffhd_to_fill;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002249 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002250 rc = sleep_thread(common);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002251 if (rc)
2252 return rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002253 }
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002254
2255 /* Queue a request to read a Bulk-only CBW */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002256 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002257 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002258 START_TRANSFER_OR(common, bulk_out, bh->outreq,
2259 &bh->outreq_busy, &bh->state)
2260 /* Don't know what to do if common->fsg is NULL */
2261 return -EIO;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002262
2263 /* We will drain the buffer in software, which means we
2264 * can reuse it for the next filling. No need to advance
2265 * next_buffhd_to_fill. */
2266
2267 /* Wait for the CBW to arrive */
2268 while (bh->state != BUF_STATE_FULL) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002269 rc = sleep_thread(common);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002270 if (rc)
2271 return rc;
2272 }
2273 smp_rmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002274 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002275 bh->state = BUF_STATE_EMPTY;
2276
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002277 return rc;
2278}
2279
2280
2281/*-------------------------------------------------------------------------*/
2282
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002283static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002284 const struct usb_endpoint_descriptor *d)
2285{
2286 int rc;
2287
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002288 ep->driver_data = common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002289 rc = usb_ep_enable(ep, d);
2290 if (rc)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002291 ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002292 return rc;
2293}
2294
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002295static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002296 struct usb_request **preq)
2297{
2298 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2299 if (*preq)
2300 return 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002301 ERROR(common, "can't allocate request for %s\n", ep->name);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002302 return -ENOMEM;
2303}
2304
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002305/* Reset interface setting and re-init endpoint state (toggle etc). */
2306static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002307{
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002308 const struct usb_endpoint_descriptor *d;
2309 struct fsg_dev *fsg;
2310 int i, rc = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002311
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002312 if (common->running)
2313 DBG(common, "reset interface\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002314
2315reset:
2316 /* Deallocate the requests */
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002317 if (common->fsg) {
2318 fsg = common->fsg;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002319
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002320 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2321 struct fsg_buffhd *bh = &common->buffhds[i];
2322
2323 if (bh->inreq) {
2324 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2325 bh->inreq = NULL;
2326 }
2327 if (bh->outreq) {
2328 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2329 bh->outreq = NULL;
2330 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002331 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002332
2333 /* Disable the endpoints */
2334 if (fsg->bulk_in_enabled) {
2335 usb_ep_disable(fsg->bulk_in);
2336 fsg->bulk_in_enabled = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002337 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002338 if (fsg->bulk_out_enabled) {
2339 usb_ep_disable(fsg->bulk_out);
2340 fsg->bulk_out_enabled = 0;
2341 }
2342
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002343 common->fsg = NULL;
2344 wake_up(&common->fsg_wait);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002345 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002346
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002347 common->running = 0;
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002348 if (!new_fsg || rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002349 return rc;
2350
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002351 common->fsg = new_fsg;
2352 fsg = common->fsg;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002353
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002354 /* Enable the endpoints */
2355 d = fsg_ep_desc(common->gadget,
2356 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2357 rc = enable_endpoint(common, fsg->bulk_in, d);
2358 if (rc)
2359 goto reset;
2360 fsg->bulk_in_enabled = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002361
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002362 d = fsg_ep_desc(common->gadget,
2363 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2364 rc = enable_endpoint(common, fsg->bulk_out, d);
2365 if (rc)
2366 goto reset;
2367 fsg->bulk_out_enabled = 1;
2368 common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2369 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2370
2371 /* Allocate the requests */
2372 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2373 struct fsg_buffhd *bh = &common->buffhds[i];
2374
2375 rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002376 if (rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002377 goto reset;
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002378 rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002379 if (rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002380 goto reset;
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002381 bh->inreq->buf = bh->outreq->buf = bh->buf;
2382 bh->inreq->context = bh->outreq->context = bh;
2383 bh->inreq->complete = bulk_in_complete;
2384 bh->outreq->complete = bulk_out_complete;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002385 }
2386
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002387 common->running = 1;
2388 for (i = 0; i < common->nluns; ++i)
2389 common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002390 return rc;
2391}
2392
2393
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002394/****************************** ALT CONFIGS ******************************/
2395
2396
2397static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2398{
2399 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002400 fsg->common->new_fsg = fsg;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002401 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002402 return 0;
2403}
2404
2405static void fsg_disable(struct usb_function *f)
2406{
2407 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002408 fsg->common->new_fsg = NULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002409 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002410}
2411
2412
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002413/*-------------------------------------------------------------------------*/
2414
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002415static void handle_exception(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002416{
2417 siginfo_t info;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002418 int i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002419 struct fsg_buffhd *bh;
2420 enum fsg_state old_state;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002421 struct fsg_lun *curlun;
2422 unsigned int exception_req_tag;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002423
2424 /* Clear the existing signals. Anything but SIGUSR1 is converted
2425 * into a high-priority EXIT exception. */
2426 for (;;) {
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002427 int sig =
2428 dequeue_signal_lock(current, &current->blocked, &info);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002429 if (!sig)
2430 break;
2431 if (sig != SIGUSR1) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002432 if (common->state < FSG_STATE_EXIT)
2433 DBG(common, "Main thread exiting on signal\n");
2434 raise_exception(common, FSG_STATE_EXIT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002435 }
2436 }
2437
2438 /* Cancel all the pending transfers */
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002439 if (likely(common->fsg)) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002440 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002441 bh = &common->buffhds[i];
2442 if (bh->inreq_busy)
2443 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2444 if (bh->outreq_busy)
2445 usb_ep_dequeue(common->fsg->bulk_out,
2446 bh->outreq);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002447 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002448
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002449 /* Wait until everything is idle */
2450 for (;;) {
2451 int num_active = 0;
2452 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2453 bh = &common->buffhds[i];
2454 num_active += bh->inreq_busy + bh->outreq_busy;
2455 }
2456 if (num_active == 0)
2457 break;
2458 if (sleep_thread(common))
2459 return;
2460 }
2461
2462 /* Clear out the controller's fifos */
2463 if (common->fsg->bulk_in_enabled)
2464 usb_ep_fifo_flush(common->fsg->bulk_in);
2465 if (common->fsg->bulk_out_enabled)
2466 usb_ep_fifo_flush(common->fsg->bulk_out);
2467 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002468
2469 /* Reset the I/O buffer states and pointers, the SCSI
2470 * state, and the exception. Then invoke the handler. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002471 spin_lock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002472
2473 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002474 bh = &common->buffhds[i];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002475 bh->state = BUF_STATE_EMPTY;
2476 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002477 common->next_buffhd_to_fill = &common->buffhds[0];
2478 common->next_buffhd_to_drain = &common->buffhds[0];
2479 exception_req_tag = common->exception_req_tag;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002480 old_state = common->state;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002481
2482 if (old_state == FSG_STATE_ABORT_BULK_OUT)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002483 common->state = FSG_STATE_STATUS_PHASE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002484 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002485 for (i = 0; i < common->nluns; ++i) {
2486 curlun = &common->luns[i];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002487 curlun->prevent_medium_removal = 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002488 curlun->sense_data = SS_NO_SENSE;
2489 curlun->unit_attention_data = SS_NO_SENSE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002490 curlun->sense_data_info = 0;
2491 curlun->info_valid = 0;
2492 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002493 common->state = FSG_STATE_IDLE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002494 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002495 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002496
2497 /* Carry out any extra actions required for the exception */
2498 switch (old_state) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002499 case FSG_STATE_ABORT_BULK_OUT:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002500 send_status(common);
2501 spin_lock_irq(&common->lock);
2502 if (common->state == FSG_STATE_STATUS_PHASE)
2503 common->state = FSG_STATE_IDLE;
2504 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002505 break;
2506
2507 case FSG_STATE_RESET:
2508 /* In case we were forced against our will to halt a
2509 * bulk endpoint, clear the halt now. (The SuperH UDC
2510 * requires this.) */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002511 if (!fsg_is_set(common))
2512 break;
2513 if (test_and_clear_bit(IGNORE_BULK_OUT,
2514 &common->fsg->atomic_bitflags))
2515 usb_ep_clear_halt(common->fsg->bulk_in);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002516
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002517 if (common->ep0_req_tag == exception_req_tag)
2518 ep0_queue(common); /* Complete the status stage */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002519
2520 /* Technically this should go here, but it would only be
2521 * a waste of time. Ditto for the INTERFACE_CHANGE and
2522 * CONFIG_CHANGE cases. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002523 /* for (i = 0; i < common->nluns; ++i) */
2524 /* common->luns[i].unit_attention_data = */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002525 /* SS_RESET_OCCURRED; */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002526 break;
2527
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002528 case FSG_STATE_CONFIG_CHANGE:
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002529 do_set_interface(common, common->new_fsg);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002530 break;
2531
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002532 case FSG_STATE_EXIT:
2533 case FSG_STATE_TERMINATED:
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002534 do_set_interface(common, NULL); /* Free resources */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002535 spin_lock_irq(&common->lock);
2536 common->state = FSG_STATE_TERMINATED; /* Stop the thread */
2537 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002538 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002539
2540 case FSG_STATE_INTERFACE_CHANGE:
2541 case FSG_STATE_DISCONNECT:
2542 case FSG_STATE_COMMAND_PHASE:
2543 case FSG_STATE_DATA_PHASE:
2544 case FSG_STATE_STATUS_PHASE:
2545 case FSG_STATE_IDLE:
2546 break;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002547 }
2548}
2549
2550
2551/*-------------------------------------------------------------------------*/
2552
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002553static int fsg_main_thread(void *common_)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002554{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002555 struct fsg_common *common = common_;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002556
2557 /* Allow the thread to be killed by a signal, but set the signal mask
2558 * to block everything but INT, TERM, KILL, and USR1. */
2559 allow_signal(SIGINT);
2560 allow_signal(SIGTERM);
2561 allow_signal(SIGKILL);
2562 allow_signal(SIGUSR1);
2563
2564 /* Allow the thread to be frozen */
2565 set_freezable();
2566
2567 /* Arrange for userspace references to be interpreted as kernel
2568 * pointers. That way we can pass a kernel pointer to a routine
2569 * that expects a __user pointer and it will work okay. */
2570 set_fs(get_ds());
2571
2572 /* The main loop */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002573 while (common->state != FSG_STATE_TERMINATED) {
2574 if (exception_in_progress(common) || signal_pending(current)) {
2575 handle_exception(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002576 continue;
2577 }
2578
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002579 if (!common->running) {
2580 sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002581 continue;
2582 }
2583
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002584 if (get_next_command(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002585 continue;
2586
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002587 spin_lock_irq(&common->lock);
2588 if (!exception_in_progress(common))
2589 common->state = FSG_STATE_DATA_PHASE;
2590 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002591
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002592 if (do_scsi_command(common) || finish_reply(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002593 continue;
2594
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002595 spin_lock_irq(&common->lock);
2596 if (!exception_in_progress(common))
2597 common->state = FSG_STATE_STATUS_PHASE;
2598 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002599
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002600 if (send_status(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002601 continue;
2602
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002603 spin_lock_irq(&common->lock);
2604 if (!exception_in_progress(common))
2605 common->state = FSG_STATE_IDLE;
2606 spin_unlock_irq(&common->lock);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002607 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002608
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002609 spin_lock_irq(&common->lock);
2610 common->thread_task = NULL;
2611 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002612
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +01002613 if (!common->thread_exits || common->thread_exits(common) < 0) {
2614 struct fsg_lun *curlun = common->luns;
2615 unsigned i = common->nluns;
2616
2617 down_write(&common->filesem);
2618 for (; i--; ++curlun) {
2619 if (!fsg_lun_is_open(curlun))
2620 continue;
2621
2622 fsg_lun_close(curlun);
2623 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
2624 }
2625 up_write(&common->filesem);
2626 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002627
2628 /* Let the unbind and cleanup routines know the thread has exited */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002629 complete_and_exit(&common->thread_notifier, 0);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002630}
2631
2632
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002633/*************************** DEVICE ATTRIBUTES ***************************/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002634
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002635/* Write permission is checked per LUN in store_*() functions. */
2636static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
2637static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002638
2639
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002640/****************************** FSG COMMON ******************************/
2641
2642static void fsg_common_release(struct kref *ref);
2643
2644static void fsg_lun_release(struct device *dev)
2645{
2646 /* Nothing needs to be done */
2647}
2648
2649static inline void fsg_common_get(struct fsg_common *common)
2650{
2651 kref_get(&common->ref);
2652}
2653
2654static inline void fsg_common_put(struct fsg_common *common)
2655{
2656 kref_put(&common->ref, fsg_common_release);
2657}
2658
2659
2660static struct fsg_common *fsg_common_init(struct fsg_common *common,
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002661 struct usb_composite_dev *cdev,
2662 struct fsg_config *cfg)
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002663{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002664 struct usb_gadget *gadget = cdev->gadget;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002665 struct fsg_buffhd *bh;
2666 struct fsg_lun *curlun;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002667 struct fsg_lun_config *lcfg;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002668 int nluns, i, rc;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002669 char *pathbuf;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002670
2671 /* Find out how many LUNs there should be */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002672 nluns = cfg->nluns;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002673 if (nluns < 1 || nluns > FSG_MAX_LUNS) {
2674 dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns);
2675 return ERR_PTR(-EINVAL);
2676 }
2677
2678 /* Allocate? */
2679 if (!common) {
2680 common = kzalloc(sizeof *common, GFP_KERNEL);
2681 if (!common)
2682 return ERR_PTR(-ENOMEM);
2683 common->free_storage_on_release = 1;
2684 } else {
2685 memset(common, 0, sizeof common);
2686 common->free_storage_on_release = 0;
2687 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002688
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01002689 common->private_data = cfg->private_data;
2690
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002691 common->gadget = gadget;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002692 common->ep0 = gadget->ep0;
2693 common->ep0req = cdev->req;
2694
2695 /* Maybe allocate device-global string IDs, and patch descriptors */
2696 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2697 rc = usb_string_id(cdev);
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002698 if (unlikely(rc < 0))
2699 goto error_release;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002700 fsg_strings[FSG_STRING_INTERFACE].id = rc;
2701 fsg_intf_desc.iInterface = rc;
2702 }
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002703
2704 /* Create the LUNs, open their backing files, and register the
2705 * LUN devices in sysfs. */
2706 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002707 if (unlikely(!curlun)) {
2708 rc = -ENOMEM;
2709 goto error_release;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002710 }
2711 common->luns = curlun;
2712
2713 init_rwsem(&common->filesem);
2714
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002715 for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
2716 curlun->cdrom = !!lcfg->cdrom;
2717 curlun->ro = lcfg->cdrom || lcfg->ro;
2718 curlun->removable = lcfg->removable;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002719 curlun->dev.release = fsg_lun_release;
2720 curlun->dev.parent = &gadget->dev;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002721 /* curlun->dev.driver = &fsg_driver.driver; XXX */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002722 dev_set_drvdata(&curlun->dev, &common->filesem);
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01002723 dev_set_name(&curlun->dev,
2724 cfg->lun_name_format
2725 ? cfg->lun_name_format
2726 : "lun%d",
2727 i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002728
2729 rc = device_register(&curlun->dev);
2730 if (rc) {
2731 INFO(common, "failed to register LUN%d: %d\n", i, rc);
2732 common->nluns = i;
2733 goto error_release;
2734 }
2735
2736 rc = device_create_file(&curlun->dev, &dev_attr_ro);
2737 if (rc)
2738 goto error_luns;
2739 rc = device_create_file(&curlun->dev, &dev_attr_file);
2740 if (rc)
2741 goto error_luns;
2742
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002743 if (lcfg->filename) {
2744 rc = fsg_lun_open(curlun, lcfg->filename);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002745 if (rc)
2746 goto error_luns;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002747 } else if (!curlun->removable) {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002748 ERROR(common, "no file given for LUN%d\n", i);
2749 rc = -EINVAL;
2750 goto error_luns;
2751 }
2752 }
2753 common->nluns = nluns;
2754
2755
2756 /* Data buffers cyclic list */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002757 bh = common->buffhds;
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002758 i = FSG_NUM_BUFFERS;
2759 goto buffhds_first_it;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002760 do {
2761 bh->next = bh + 1;
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002762 ++bh;
2763buffhds_first_it:
2764 bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
2765 if (unlikely(!bh->buf)) {
2766 rc = -ENOMEM;
2767 goto error_release;
2768 }
2769 } while (--i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002770 bh->next = common->buffhds;
2771
2772
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002773 /* Prepare inquiryString */
2774 if (cfg->release != 0xffff) {
2775 i = cfg->release;
2776 } else {
Christoph Egger90f79762010-02-05 13:24:12 +01002777 i = usb_gadget_controller_number(gadget);
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002778 if (i >= 0) {
2779 i = 0x0300 + i;
2780 } else {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002781 WARNING(common, "controller '%s' not recognized\n",
2782 gadget->name);
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002783 i = 0x0399;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002784 }
2785 }
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002786#define OR(x, y) ((x) ? (x) : (y))
2787 snprintf(common->inquiry_string, sizeof common->inquiry_string,
2788 "%-8s%-16s%04x",
2789 OR(cfg->vendor_name, "Linux "),
2790 /* Assume product name dependent on the first LUN */
2791 OR(cfg->product_name, common->luns->cdrom
2792 ? "File-Stor Gadget"
2793 : "File-CD Gadget "),
2794 i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002795
2796
2797 /* Some peripheral controllers are known not to be able to
2798 * halt bulk endpoints correctly. If one of them is present,
2799 * disable stalls.
2800 */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002801 common->can_stall = cfg->can_stall &&
Christoph Egger90f79762010-02-05 13:24:12 +01002802 !(gadget_is_at91(common->gadget));
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002803
2804
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002805 spin_lock_init(&common->lock);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002806 kref_init(&common->ref);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002807
2808
2809 /* Tell the thread to start working */
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01002810 common->thread_exits = cfg->thread_exits;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002811 common->thread_task =
2812 kthread_create(fsg_main_thread, common,
2813 OR(cfg->thread_name, "file-storage"));
2814 if (IS_ERR(common->thread_task)) {
2815 rc = PTR_ERR(common->thread_task);
2816 goto error_release;
2817 }
2818 init_completion(&common->thread_notifier);
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002819 init_waitqueue_head(&common->fsg_wait);
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01002820#undef OR
2821
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002822
2823 /* Information */
2824 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
2825 INFO(common, "Number of LUNs=%d\n", common->nluns);
2826
2827 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2828 for (i = 0, nluns = common->nluns, curlun = common->luns;
2829 i < nluns;
2830 ++curlun, ++i) {
2831 char *p = "(no medium)";
2832 if (fsg_lun_is_open(curlun)) {
2833 p = "(error)";
2834 if (pathbuf) {
2835 p = d_path(&curlun->filp->f_path,
2836 pathbuf, PATH_MAX);
2837 if (IS_ERR(p))
2838 p = "(error)";
2839 }
2840 }
2841 LINFO(curlun, "LUN: %s%s%sfile: %s\n",
2842 curlun->removable ? "removable " : "",
2843 curlun->ro ? "read only " : "",
2844 curlun->cdrom ? "CD-ROM " : "",
2845 p);
2846 }
2847 kfree(pathbuf);
2848
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002849 DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task));
2850
2851 wake_up_process(common->thread_task);
2852
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002853 return common;
2854
2855
2856error_luns:
2857 common->nluns = i + 1;
2858error_release:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002859 common->state = FSG_STATE_TERMINATED; /* The thread is dead */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002860 /* Call fsg_common_release() directly, ref might be not
2861 * initialised */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002862 fsg_common_release(&common->ref);
2863 return ERR_PTR(rc);
2864}
2865
2866
2867static void fsg_common_release(struct kref *ref)
2868{
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002869 struct fsg_common *common = container_of(ref, struct fsg_common, ref);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002870
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002871 /* If the thread isn't already dead, tell it to exit now */
2872 if (common->state != FSG_STATE_TERMINATED) {
2873 raise_exception(common, FSG_STATE_EXIT);
2874 wait_for_completion(&common->thread_notifier);
2875
2876 /* The cleanup routine waits for this completion also */
2877 complete(&common->thread_notifier);
2878 }
2879
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002880 if (likely(common->luns)) {
2881 struct fsg_lun *lun = common->luns;
2882 unsigned i = common->nluns;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002883
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002884 /* In error recovery common->nluns may be zero. */
2885 for (; i; --i, ++lun) {
2886 device_remove_file(&lun->dev, &dev_attr_ro);
2887 device_remove_file(&lun->dev, &dev_attr_file);
2888 fsg_lun_close(lun);
2889 device_unregister(&lun->dev);
2890 }
2891
2892 kfree(common->luns);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002893 }
2894
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002895 {
2896 struct fsg_buffhd *bh = common->buffhds;
2897 unsigned i = FSG_NUM_BUFFERS;
2898 do {
2899 kfree(bh->buf);
2900 } while (++bh, --i);
2901 }
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002902
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002903 if (common->free_storage_on_release)
2904 kfree(common);
2905}
2906
2907
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002908/*-------------------------------------------------------------------------*/
2909
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002910
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002911static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002912{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002913 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002914 struct fsg_common *common = fsg->common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002915
2916 DBG(fsg, "unbind\n");
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002917 if (fsg->common->fsg == fsg) {
2918 fsg->common->new_fsg = NULL;
2919 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2920 /* FIXME: make interruptible or killable somehow? */
2921 wait_event(common->fsg_wait, common->fsg != fsg);
2922 }
2923
2924 fsg_common_put(common);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02002925 usb_free_descriptors(fsg->function.descriptors);
2926 usb_free_descriptors(fsg->function.hs_descriptors);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002927 kfree(fsg);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002928}
2929
2930
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002931static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002932{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002933 struct fsg_dev *fsg = fsg_from_func(f);
2934 struct usb_gadget *gadget = c->cdev->gadget;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002935 int i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002936 struct usb_ep *ep;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002937
2938 fsg->gadget = gadget;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002939
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002940 /* New interface */
2941 i = usb_interface_id(c, f);
2942 if (i < 0)
2943 return i;
2944 fsg_intf_desc.bInterfaceNumber = i;
2945 fsg->interface_number = i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002946
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002947 /* Find all the endpoints we will use */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002948 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2949 if (!ep)
2950 goto autoconf_fail;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002951 ep->driver_data = fsg->common; /* claim the endpoint */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002952 fsg->bulk_in = ep;
2953
2954 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2955 if (!ep)
2956 goto autoconf_fail;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002957 ep->driver_data = fsg->common; /* claim the endpoint */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002958 fsg->bulk_out = ep;
2959
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02002960 /* Copy descriptors */
2961 f->descriptors = usb_copy_descriptors(fsg_fs_function);
2962 if (unlikely(!f->descriptors))
2963 return -ENOMEM;
2964
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002965 if (gadget_is_dualspeed(gadget)) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002966 /* Assume endpoint addresses are the same for both speeds */
2967 fsg_hs_bulk_in_desc.bEndpointAddress =
2968 fsg_fs_bulk_in_desc.bEndpointAddress;
2969 fsg_hs_bulk_out_desc.bEndpointAddress =
2970 fsg_fs_bulk_out_desc.bEndpointAddress;
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02002971 f->hs_descriptors = usb_copy_descriptors(fsg_hs_function);
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02002972 if (unlikely(!f->hs_descriptors)) {
2973 usb_free_descriptors(f->descriptors);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02002974 return -ENOMEM;
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02002975 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002976 }
2977
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002978 return 0;
2979
2980autoconf_fail:
2981 ERROR(fsg, "unable to autoconfigure all endpoints\n");
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02002982 return -ENOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002983}
2984
2985
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002986/****************************** ADD FUNCTION ******************************/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002987
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002988static struct usb_gadget_strings *fsg_strings_array[] = {
2989 &fsg_stringtab,
2990 NULL,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002991};
2992
Michal Nazarewicz1dc90982010-06-16 12:07:57 +02002993static int fsg_bind_config(struct usb_composite_dev *cdev,
2994 struct usb_configuration *c,
2995 struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002996{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002997 struct fsg_dev *fsg;
2998 int rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002999
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003000 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
3001 if (unlikely(!fsg))
3002 return -ENOMEM;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003003
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003004 fsg->function.name = FSG_DRIVER_DESC;
3005 fsg->function.strings = fsg_strings_array;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003006 fsg->function.bind = fsg_bind;
3007 fsg->function.unbind = fsg_unbind;
3008 fsg->function.setup = fsg_setup;
3009 fsg->function.set_alt = fsg_set_alt;
3010 fsg->function.disable = fsg_disable;
3011
3012 fsg->common = common;
3013 /* Our caller holds a reference to common structure so we
3014 * don't have to be worry about it being freed until we return
3015 * from this function. So instead of incrementing counter now
3016 * and decrement in error recovery we increment it only when
3017 * call to usb_add_function() was successful. */
3018
3019 rc = usb_add_function(c, &fsg->function);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02003020 if (unlikely(rc))
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003021 kfree(fsg);
3022 else
3023 fsg_common_get(fsg->common);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003024 return rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003025}
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003026
Michal Nazarewicz1dc90982010-06-16 12:07:57 +02003027static inline int __deprecated __maybe_unused
3028fsg_add(struct usb_composite_dev *cdev,
3029 struct usb_configuration *c,
3030 struct fsg_common *common)
3031{
3032 return fsg_bind_config(cdev, c, common);
3033}
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003034
3035
3036/************************* Module parameters *************************/
3037
3038
3039struct fsg_module_parameters {
3040 char *file[FSG_MAX_LUNS];
3041 int ro[FSG_MAX_LUNS];
3042 int removable[FSG_MAX_LUNS];
3043 int cdrom[FSG_MAX_LUNS];
3044
3045 unsigned int file_count, ro_count, removable_count, cdrom_count;
3046 unsigned int luns; /* nluns */
3047 int stall; /* can_stall */
3048};
3049
3050
3051#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
3052 module_param_array_named(prefix ## name, params.name, type, \
3053 &prefix ## params.name ## _count, \
3054 S_IRUGO); \
3055 MODULE_PARM_DESC(prefix ## name, desc)
3056
3057#define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \
3058 module_param_named(prefix ## name, params.name, type, \
3059 S_IRUGO); \
3060 MODULE_PARM_DESC(prefix ## name, desc)
3061
3062#define FSG_MODULE_PARAMETERS(prefix, params) \
3063 _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \
3064 "names of backing files or devices"); \
3065 _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \
3066 "true to force read-only"); \
3067 _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \
3068 "true to simulate removable media"); \
3069 _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
3070 "true to simulate CD-ROM instead of disk"); \
3071 _FSG_MODULE_PARAM(prefix, params, luns, uint, \
3072 "number of LUNs"); \
3073 _FSG_MODULE_PARAM(prefix, params, stall, bool, \
3074 "false to prevent bulk stalls")
3075
3076
3077static void
3078fsg_config_from_params(struct fsg_config *cfg,
3079 const struct fsg_module_parameters *params)
3080{
3081 struct fsg_lun_config *lun;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003082 unsigned i;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003083
3084 /* Configure LUNs */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003085 cfg->nluns =
3086 min(params->luns ?: (params->file_count ?: 1u),
3087 (unsigned)FSG_MAX_LUNS);
3088 for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003089 lun->ro = !!params->ro[i];
3090 lun->cdrom = !!params->cdrom[i];
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003091 lun->removable = /* Removable by default */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003092 params->removable_count <= i || params->removable[i];
3093 lun->filename =
3094 params->file_count > i && params->file[i][0]
3095 ? params->file[i]
3096 : 0;
3097 }
3098
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003099 /* Let MSF use defaults */
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01003100 cfg->lun_name_format = 0;
3101 cfg->thread_name = 0;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003102 cfg->vendor_name = 0;
3103 cfg->product_name = 0;
3104 cfg->release = 0xffff;
3105
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01003106 cfg->thread_exits = 0;
3107 cfg->private_data = 0;
3108
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003109 /* Finalise */
3110 cfg->can_stall = params->stall;
3111}
3112
3113static inline struct fsg_common *
3114fsg_common_from_params(struct fsg_common *common,
3115 struct usb_composite_dev *cdev,
3116 const struct fsg_module_parameters *params)
3117 __attribute__((unused));
3118static inline struct fsg_common *
3119fsg_common_from_params(struct fsg_common *common,
3120 struct usb_composite_dev *cdev,
3121 const struct fsg_module_parameters *params)
3122{
3123 struct fsg_config cfg;
3124 fsg_config_from_params(&cfg, params);
3125 return fsg_common_init(common, cdev, &cfg);
3126}
3127