blob: 32cce029f65c8ea366d1789e54067948fea6f509 [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;
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +0200319struct fsg_common;
320
321/* FSF callback functions */
322struct fsg_operations {
323 /* Callback function to call when thread exits. If no
324 * callback is set or it returns value lower then zero MSF
325 * will force eject all LUNs it operates on (including those
326 * marked as non-removable or with prevent_medium_removal flag
327 * set). */
328 int (*thread_exits)(struct fsg_common *common);
329
330 /* Called prior to ejection. Negative return means error,
331 * zero means to continue with ejection, positive means not to
332 * eject. */
333 int (*pre_eject)(struct fsg_common *common,
334 struct fsg_lun *lun, int num);
335 /* Called after ejection. Negative return means error, zero
336 * or positive is just a success. */
337 int (*post_eject)(struct fsg_common *common,
338 struct fsg_lun *lun, int num);
339};
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100340
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100341
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100342/* Data shared by all the FSG instances. */
343struct fsg_common {
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100344 struct usb_gadget *gadget;
Michal Nazarewiczb894f602010-06-25 16:29:28 +0200345 struct fsg_dev *fsg, *new_fsg;
346 wait_queue_head_t fsg_wait;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100347
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100348 /* filesem protects: backing files in use */
349 struct rw_semaphore filesem;
350
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100351 /* lock protects: state, all the req_busy's */
352 spinlock_t lock;
353
354 struct usb_ep *ep0; /* Copy of gadget->ep0 */
355 struct usb_request *ep0req; /* Copy of cdev->req */
356 unsigned int ep0_req_tag;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100357
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100358 struct fsg_buffhd *next_buffhd_to_fill;
359 struct fsg_buffhd *next_buffhd_to_drain;
360 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
361
362 int cmnd_size;
363 u8 cmnd[MAX_COMMAND_SIZE];
364
365 unsigned int nluns;
366 unsigned int lun;
367 struct fsg_lun *luns;
368 struct fsg_lun *curlun;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100369
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100370 unsigned int bulk_out_maxpacket;
371 enum fsg_state state; /* For exception handling */
372 unsigned int exception_req_tag;
373
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100374 enum data_direction data_dir;
375 u32 data_size;
376 u32 data_size_from_cmnd;
377 u32 tag;
378 u32 residue;
379 u32 usb_amount_left;
380
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100381 unsigned int can_stall:1;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100382 unsigned int free_storage_on_release:1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100383 unsigned int phase_error:1;
384 unsigned int short_packet_received:1;
385 unsigned int bad_lun_okay:1;
386 unsigned int running:1;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100387
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100388 int thread_wakeup_needed;
389 struct completion thread_notifier;
390 struct task_struct *thread_task;
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100391
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +0200392 /* Callback functions. */
393 const struct fsg_operations *ops;
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100394 /* Gadget's private data. */
395 void *private_data;
396
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100397 /* Vendor (8 chars), product (16 chars), release (4
398 * hexadecimal digits) and NUL byte */
399 char inquiry_string[8 + 16 + 4 + 1];
400
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100401 struct kref ref;
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100402};
403
404
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100405struct fsg_config {
406 unsigned nluns;
407 struct fsg_lun_config {
408 const char *filename;
409 char ro;
410 char removable;
411 char cdrom;
412 } luns[FSG_MAX_LUNS];
413
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100414 const char *lun_name_format;
415 const char *thread_name;
416
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +0200417 /* Callback functions. */
418 const struct fsg_operations *ops;
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100419 /* Gadget's private data. */
420 void *private_data;
421
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100422 const char *vendor_name; /* 8 characters or less */
423 const char *product_name; /* 16 characters or less */
424 u16 release;
425
426 char can_stall;
427};
428
429
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100430struct fsg_dev {
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100431 struct usb_function function;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100432 struct usb_gadget *gadget; /* Copy of cdev->gadget */
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100433 struct fsg_common *common;
434
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100435 u16 interface_number;
436
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100437 unsigned int bulk_in_enabled:1;
438 unsigned int bulk_out_enabled:1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100439
440 unsigned long atomic_bitflags;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100441#define IGNORE_BULK_OUT 0
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100442
443 struct usb_ep *bulk_in;
444 struct usb_ep *bulk_out;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100445};
446
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100447
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100448static inline int __fsg_is_set(struct fsg_common *common,
449 const char *func, unsigned line)
450{
451 if (common->fsg)
452 return 1;
453 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +0200454 WARN_ON(1);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100455 return 0;
456}
457
458#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
459
460
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100461static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
462{
463 return container_of(f, struct fsg_dev, function);
464}
465
466
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100467typedef void (*fsg_routine_t)(struct fsg_dev *);
468
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100469static int exception_in_progress(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100470{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100471 return common->state > FSG_STATE_IDLE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100472}
473
474/* Make bulk-out requests be divisible by the maxpacket size */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100475static void set_bulk_out_req_length(struct fsg_common *common,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100476 struct fsg_buffhd *bh, unsigned int length)
477{
478 unsigned int rem;
479
480 bh->bulk_out_intended_length = length;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100481 rem = length % common->bulk_out_maxpacket;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100482 if (rem > 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100483 length += common->bulk_out_maxpacket - rem;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100484 bh->outreq->length = length;
485}
486
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100487/*-------------------------------------------------------------------------*/
488
489static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
490{
491 const char *name;
492
493 if (ep == fsg->bulk_in)
494 name = "bulk-in";
495 else if (ep == fsg->bulk_out)
496 name = "bulk-out";
497 else
498 name = ep->name;
499 DBG(fsg, "%s set halt\n", name);
500 return usb_ep_set_halt(ep);
501}
502
503
504/*-------------------------------------------------------------------------*/
505
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100506/* These routines may be called in process context or in_irq */
507
508/* Caller must hold fsg->lock */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100509static void wakeup_thread(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100510{
511 /* Tell the main thread that something has happened */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100512 common->thread_wakeup_needed = 1;
513 if (common->thread_task)
514 wake_up_process(common->thread_task);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100515}
516
517
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100518static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100519{
520 unsigned long flags;
521
522 /* Do nothing if a higher-priority exception is already in progress.
523 * If a lower-or-equal priority exception is in progress, preempt it
524 * and notify the main thread by sending it a signal. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100525 spin_lock_irqsave(&common->lock, flags);
526 if (common->state <= new_state) {
527 common->exception_req_tag = common->ep0_req_tag;
528 common->state = new_state;
529 if (common->thread_task)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100530 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100531 common->thread_task);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100532 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100533 spin_unlock_irqrestore(&common->lock, flags);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100534}
535
536
537/*-------------------------------------------------------------------------*/
538
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100539static int ep0_queue(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100540{
541 int rc;
542
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100543 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
544 common->ep0->driver_data = common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100545 if (rc != 0 && rc != -ESHUTDOWN) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100546 /* We can't do much more than wait for a reset */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100547 WARNING(common, "error in submission: %s --> %d\n",
548 common->ep0->name, rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100549 }
550 return rc;
551}
552
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100553/*-------------------------------------------------------------------------*/
554
555/* Bulk and interrupt endpoint completion handlers.
556 * These always run in_irq. */
557
558static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
559{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100560 struct fsg_common *common = ep->driver_data;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100561 struct fsg_buffhd *bh = req->context;
562
563 if (req->status || req->actual != req->length)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100564 DBG(common, "%s --> %d, %u/%u\n", __func__,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100565 req->status, req->actual, req->length);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100566 if (req->status == -ECONNRESET) /* Request was cancelled */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100567 usb_ep_fifo_flush(ep);
568
569 /* Hold the lock while we update the request and buffer states */
570 smp_wmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100571 spin_lock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100572 bh->inreq_busy = 0;
573 bh->state = BUF_STATE_EMPTY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100574 wakeup_thread(common);
575 spin_unlock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100576}
577
578static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
579{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100580 struct fsg_common *common = ep->driver_data;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100581 struct fsg_buffhd *bh = req->context;
582
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100583 dump_msg(common, "bulk-out", req->buf, req->actual);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100584 if (req->status || req->actual != bh->bulk_out_intended_length)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100585 DBG(common, "%s --> %d, %u/%u\n", __func__,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100586 req->status, req->actual,
587 bh->bulk_out_intended_length);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100588 if (req->status == -ECONNRESET) /* Request was cancelled */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100589 usb_ep_fifo_flush(ep);
590
591 /* Hold the lock while we update the request and buffer states */
592 smp_wmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100593 spin_lock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100594 bh->outreq_busy = 0;
595 bh->state = BUF_STATE_FULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100596 wakeup_thread(common);
597 spin_unlock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100598}
599
600
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100601/*-------------------------------------------------------------------------*/
602
603/* Ep0 class-specific handlers. These always run in_irq. */
604
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100605static int fsg_setup(struct usb_function *f,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100606 const struct usb_ctrlrequest *ctrl)
607{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100608 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100609 struct usb_request *req = fsg->common->ep0req;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100610 u16 w_index = le16_to_cpu(ctrl->wIndex);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100611 u16 w_value = le16_to_cpu(ctrl->wValue);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100612 u16 w_length = le16_to_cpu(ctrl->wLength);
613
Michal Nazarewiczb894f602010-06-25 16:29:28 +0200614 if (!fsg_is_set(fsg->common))
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100615 return -EOPNOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100616
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100617 switch (ctrl->bRequest) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100618
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100619 case USB_BULK_RESET_REQUEST:
620 if (ctrl->bRequestType !=
621 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100622 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100623 if (w_index != fsg->interface_number || w_value != 0)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100624 return -EDOM;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100625
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100626 /* Raise an exception to stop the current operation
627 * and reinitialize our state. */
628 DBG(fsg, "bulk reset request\n");
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100629 raise_exception(fsg->common, FSG_STATE_RESET);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100630 return DELAYED_STATUS;
631
632 case USB_BULK_GET_MAX_LUN_REQUEST:
633 if (ctrl->bRequestType !=
634 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100635 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100636 if (w_index != fsg->interface_number || w_value != 0)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100637 return -EDOM;
638 VDBG(fsg, "get max LUN\n");
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100639 *(u8 *) req->buf = fsg->common->nluns - 1;
Michal Nazarewiczb00ce112010-01-27 11:14:28 +0100640
641 /* Respond with data/status */
Michal Nazarewiczd7e18a92010-02-03 11:37:17 +0100642 req->length = min((u16)1, w_length);
Michal Nazarewiczb00ce112010-01-27 11:14:28 +0100643 return ep0_queue(fsg->common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100644 }
645
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100646 VDBG(fsg,
647 "unknown class-specific control req "
648 "%02x.%02x v%04x i%04x l%u\n",
649 ctrl->bRequestType, ctrl->bRequest,
650 le16_to_cpu(ctrl->wValue), w_index, w_length);
651 return -EOPNOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100652}
653
654
655/*-------------------------------------------------------------------------*/
656
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100657/* All the following routines run in process context */
658
659
660/* Use this for bulk or interrupt transfers, not ep0 */
661static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
662 struct usb_request *req, int *pbusy,
663 enum fsg_buffer_state *state)
664{
665 int rc;
666
667 if (ep == fsg->bulk_in)
668 dump_msg(fsg, "bulk-in", req->buf, req->length);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100669
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100670 spin_lock_irq(&fsg->common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100671 *pbusy = 1;
672 *state = BUF_STATE_BUSY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100673 spin_unlock_irq(&fsg->common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100674 rc = usb_ep_queue(ep, req, GFP_KERNEL);
675 if (rc != 0) {
676 *pbusy = 0;
677 *state = BUF_STATE_EMPTY;
678
679 /* We can't do much more than wait for a reset */
680
681 /* Note: currently the net2280 driver fails zero-length
682 * submissions if DMA is enabled. */
683 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
684 req->length == 0))
685 WARNING(fsg, "error in submission: %s --> %d\n",
686 ep->name, rc);
687 }
688}
689
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100690#define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \
691 if (fsg_is_set(common)) \
692 start_transfer((common)->fsg, (common)->fsg->ep_name, \
693 req, pbusy, state); \
694 else
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100695
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100696#define START_TRANSFER(common, ep_name, req, pbusy, state) \
697 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
698
699
700
701static int sleep_thread(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100702{
703 int rc = 0;
704
705 /* Wait until a signal arrives or we are woken up */
706 for (;;) {
707 try_to_freeze();
708 set_current_state(TASK_INTERRUPTIBLE);
709 if (signal_pending(current)) {
710 rc = -EINTR;
711 break;
712 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100713 if (common->thread_wakeup_needed)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100714 break;
715 schedule();
716 }
717 __set_current_state(TASK_RUNNING);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100718 common->thread_wakeup_needed = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100719 return rc;
720}
721
722
723/*-------------------------------------------------------------------------*/
724
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100725static int do_read(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100726{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100727 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100728 u32 lba;
729 struct fsg_buffhd *bh;
730 int rc;
731 u32 amount_left;
732 loff_t file_offset, file_offset_tmp;
733 unsigned int amount;
734 unsigned int partial_page;
735 ssize_t nread;
736
737 /* Get the starting Logical Block Address and check that it's
738 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100739 if (common->cmnd[0] == SC_READ_6)
740 lba = get_unaligned_be24(&common->cmnd[1]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100741 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100742 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100743
744 /* We allow DPO (Disable Page Out = don't save data in the
745 * cache) and FUA (Force Unit Access = don't read from the
746 * cache), but we don't implement them. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100747 if ((common->cmnd[1] & ~0x18) != 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100748 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
749 return -EINVAL;
750 }
751 }
752 if (lba >= curlun->num_sectors) {
753 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
754 return -EINVAL;
755 }
756 file_offset = ((loff_t) lba) << 9;
757
758 /* Carry out the file reads */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100759 amount_left = common->data_size_from_cmnd;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100760 if (unlikely(amount_left == 0))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100761 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100762
763 for (;;) {
764
765 /* Figure out how much we need to read:
766 * Try to read the remaining amount.
767 * But don't read more than the buffer size.
768 * And don't try to read past the end of the file.
769 * Finally, if we're not at a page boundary, don't read past
770 * the next page.
771 * If this means reading 0 then we were asked to read past
772 * the end of file. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100773 amount = min(amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100774 amount = min((loff_t) amount,
775 curlun->file_length - file_offset);
776 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
777 if (partial_page > 0)
778 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
779 partial_page);
780
781 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100782 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100783 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100784 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100785 if (rc)
786 return rc;
787 }
788
789 /* If we were asked to read past the end of file,
790 * end with an empty buffer. */
791 if (amount == 0) {
792 curlun->sense_data =
793 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
794 curlun->sense_data_info = file_offset >> 9;
795 curlun->info_valid = 1;
796 bh->inreq->length = 0;
797 bh->state = BUF_STATE_FULL;
798 break;
799 }
800
801 /* Perform the read */
802 file_offset_tmp = file_offset;
803 nread = vfs_read(curlun->filp,
804 (char __user *) bh->buf,
805 amount, &file_offset_tmp);
806 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
807 (unsigned long long) file_offset,
808 (int) nread);
809 if (signal_pending(current))
810 return -EINTR;
811
812 if (nread < 0) {
813 LDBG(curlun, "error in file read: %d\n",
814 (int) nread);
815 nread = 0;
816 } else if (nread < amount) {
817 LDBG(curlun, "partial file read: %d/%u\n",
818 (int) nread, amount);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100819 nread -= (nread & 511); /* Round down to a block */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100820 }
821 file_offset += nread;
822 amount_left -= nread;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100823 common->residue -= nread;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100824 bh->inreq->length = nread;
825 bh->state = BUF_STATE_FULL;
826
827 /* If an error occurred, report it and its position */
828 if (nread < amount) {
829 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
830 curlun->sense_data_info = file_offset >> 9;
831 curlun->info_valid = 1;
832 break;
833 }
834
835 if (amount_left == 0)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100836 break; /* No more left to read */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100837
838 /* Send this buffer and go read some more */
839 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100840 START_TRANSFER_OR(common, bulk_in, bh->inreq,
841 &bh->inreq_busy, &bh->state)
842 /* Don't know what to do if
843 * common->fsg is NULL */
844 return -EIO;
845 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100846 }
847
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100848 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100849}
850
851
852/*-------------------------------------------------------------------------*/
853
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100854static int do_write(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100855{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100856 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100857 u32 lba;
858 struct fsg_buffhd *bh;
859 int get_some_more;
860 u32 amount_left_to_req, amount_left_to_write;
861 loff_t usb_offset, file_offset, file_offset_tmp;
862 unsigned int amount;
863 unsigned int partial_page;
864 ssize_t nwritten;
865 int rc;
866
867 if (curlun->ro) {
868 curlun->sense_data = SS_WRITE_PROTECTED;
869 return -EINVAL;
870 }
871 spin_lock(&curlun->filp->f_lock);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100872 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100873 spin_unlock(&curlun->filp->f_lock);
874
875 /* Get the starting Logical Block Address and check that it's
876 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100877 if (common->cmnd[0] == SC_WRITE_6)
878 lba = get_unaligned_be24(&common->cmnd[1]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100879 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100880 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100881
882 /* We allow DPO (Disable Page Out = don't save data in the
883 * cache) and FUA (Force Unit Access = write directly to the
884 * medium). We don't implement DPO; we implement FUA by
885 * performing synchronous output. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100886 if (common->cmnd[1] & ~0x18) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100887 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
888 return -EINVAL;
889 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100890 if (common->cmnd[1] & 0x08) { /* FUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100891 spin_lock(&curlun->filp->f_lock);
892 curlun->filp->f_flags |= O_SYNC;
893 spin_unlock(&curlun->filp->f_lock);
894 }
895 }
896 if (lba >= curlun->num_sectors) {
897 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
898 return -EINVAL;
899 }
900
901 /* Carry out the file writes */
902 get_some_more = 1;
903 file_offset = usb_offset = ((loff_t) lba) << 9;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100904 amount_left_to_req = common->data_size_from_cmnd;
905 amount_left_to_write = common->data_size_from_cmnd;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100906
907 while (amount_left_to_write > 0) {
908
909 /* Queue a request for more data from the host */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100910 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100911 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
912
913 /* Figure out how much we want to get:
914 * Try to get the remaining amount.
915 * But don't get more than the buffer size.
916 * And don't try to go past the end of the file.
917 * If we're not at a page boundary,
918 * don't go past the next page.
919 * If this means getting 0, then we were asked
920 * to write past the end of file.
921 * Finally, round down to a block boundary. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100922 amount = min(amount_left_to_req, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100923 amount = min((loff_t) amount, curlun->file_length -
924 usb_offset);
925 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
926 if (partial_page > 0)
927 amount = min(amount,
928 (unsigned int) PAGE_CACHE_SIZE - partial_page);
929
930 if (amount == 0) {
931 get_some_more = 0;
932 curlun->sense_data =
933 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
934 curlun->sense_data_info = usb_offset >> 9;
935 curlun->info_valid = 1;
936 continue;
937 }
938 amount -= (amount & 511);
939 if (amount == 0) {
940
941 /* Why were we were asked to transfer a
942 * partial block? */
943 get_some_more = 0;
944 continue;
945 }
946
947 /* Get the next buffer */
948 usb_offset += amount;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100949 common->usb_amount_left -= amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100950 amount_left_to_req -= amount;
951 if (amount_left_to_req == 0)
952 get_some_more = 0;
953
954 /* amount is always divisible by 512, hence by
955 * the bulk-out maxpacket size */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100956 bh->outreq->length = amount;
957 bh->bulk_out_intended_length = amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100958 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100959 START_TRANSFER_OR(common, bulk_out, bh->outreq,
960 &bh->outreq_busy, &bh->state)
961 /* Don't know what to do if
962 * common->fsg is NULL */
963 return -EIO;
964 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100965 continue;
966 }
967
968 /* Write the received data to the backing file */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100969 bh = common->next_buffhd_to_drain;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100970 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100971 break; /* We stopped early */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100972 if (bh->state == BUF_STATE_FULL) {
973 smp_rmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100974 common->next_buffhd_to_drain = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100975 bh->state = BUF_STATE_EMPTY;
976
977 /* Did something go wrong with the transfer? */
978 if (bh->outreq->status != 0) {
979 curlun->sense_data = SS_COMMUNICATION_FAILURE;
980 curlun->sense_data_info = file_offset >> 9;
981 curlun->info_valid = 1;
982 break;
983 }
984
985 amount = bh->outreq->actual;
986 if (curlun->file_length - file_offset < amount) {
987 LERROR(curlun,
988 "write %u @ %llu beyond end %llu\n",
989 amount, (unsigned long long) file_offset,
990 (unsigned long long) curlun->file_length);
991 amount = curlun->file_length - file_offset;
992 }
993
994 /* Perform the write */
995 file_offset_tmp = file_offset;
996 nwritten = vfs_write(curlun->filp,
997 (char __user *) bh->buf,
998 amount, &file_offset_tmp);
999 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
1000 (unsigned long long) file_offset,
1001 (int) nwritten);
1002 if (signal_pending(current))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001003 return -EINTR; /* Interrupted! */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001004
1005 if (nwritten < 0) {
1006 LDBG(curlun, "error in file write: %d\n",
1007 (int) nwritten);
1008 nwritten = 0;
1009 } else if (nwritten < amount) {
1010 LDBG(curlun, "partial file write: %d/%u\n",
1011 (int) nwritten, amount);
1012 nwritten -= (nwritten & 511);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001013 /* Round down to a block */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001014 }
1015 file_offset += nwritten;
1016 amount_left_to_write -= nwritten;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001017 common->residue -= nwritten;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001018
1019 /* If an error occurred, report it and its position */
1020 if (nwritten < amount) {
1021 curlun->sense_data = SS_WRITE_ERROR;
1022 curlun->sense_data_info = file_offset >> 9;
1023 curlun->info_valid = 1;
1024 break;
1025 }
1026
1027 /* Did the host decide to stop early? */
1028 if (bh->outreq->actual != bh->outreq->length) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001029 common->short_packet_received = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001030 break;
1031 }
1032 continue;
1033 }
1034
1035 /* Wait for something to happen */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001036 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001037 if (rc)
1038 return rc;
1039 }
1040
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001041 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001042}
1043
1044
1045/*-------------------------------------------------------------------------*/
1046
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001047static int do_synchronize_cache(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001048{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001049 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001050 int rc;
1051
1052 /* We ignore the requested LBA and write out all file's
1053 * dirty data buffers. */
1054 rc = fsg_lun_fsync_sub(curlun);
1055 if (rc)
1056 curlun->sense_data = SS_WRITE_ERROR;
1057 return 0;
1058}
1059
1060
1061/*-------------------------------------------------------------------------*/
1062
1063static void invalidate_sub(struct fsg_lun *curlun)
1064{
1065 struct file *filp = curlun->filp;
1066 struct inode *inode = filp->f_path.dentry->d_inode;
1067 unsigned long rc;
1068
1069 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
Christoph Hellwig2ecdc822010-01-26 17:27:20 +01001070 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001071}
1072
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001073static int do_verify(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001074{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001075 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001076 u32 lba;
1077 u32 verification_length;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001078 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001079 loff_t file_offset, file_offset_tmp;
1080 u32 amount_left;
1081 unsigned int amount;
1082 ssize_t nread;
1083
1084 /* Get the starting Logical Block Address and check that it's
1085 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001086 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001087 if (lba >= curlun->num_sectors) {
1088 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1089 return -EINVAL;
1090 }
1091
1092 /* We allow DPO (Disable Page Out = don't save data in the
1093 * cache) but we don't implement it. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001094 if (common->cmnd[1] & ~0x10) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001095 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1096 return -EINVAL;
1097 }
1098
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001099 verification_length = get_unaligned_be16(&common->cmnd[7]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001100 if (unlikely(verification_length == 0))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001101 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001102
1103 /* Prepare to carry out the file verify */
1104 amount_left = verification_length << 9;
1105 file_offset = ((loff_t) lba) << 9;
1106
1107 /* Write out all the dirty buffers before invalidating them */
1108 fsg_lun_fsync_sub(curlun);
1109 if (signal_pending(current))
1110 return -EINTR;
1111
1112 invalidate_sub(curlun);
1113 if (signal_pending(current))
1114 return -EINTR;
1115
1116 /* Just try to read the requested blocks */
1117 while (amount_left > 0) {
1118
1119 /* Figure out how much we need to read:
1120 * Try to read the remaining amount, but not more than
1121 * the buffer size.
1122 * And don't try to read past the end of the file.
1123 * If this means reading 0 then we were asked to read
1124 * past the end of file. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001125 amount = min(amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001126 amount = min((loff_t) amount,
1127 curlun->file_length - file_offset);
1128 if (amount == 0) {
1129 curlun->sense_data =
1130 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1131 curlun->sense_data_info = file_offset >> 9;
1132 curlun->info_valid = 1;
1133 break;
1134 }
1135
1136 /* Perform the read */
1137 file_offset_tmp = file_offset;
1138 nread = vfs_read(curlun->filp,
1139 (char __user *) bh->buf,
1140 amount, &file_offset_tmp);
1141 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1142 (unsigned long long) file_offset,
1143 (int) nread);
1144 if (signal_pending(current))
1145 return -EINTR;
1146
1147 if (nread < 0) {
1148 LDBG(curlun, "error in file verify: %d\n",
1149 (int) nread);
1150 nread = 0;
1151 } else if (nread < amount) {
1152 LDBG(curlun, "partial file verify: %d/%u\n",
1153 (int) nread, amount);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001154 nread -= (nread & 511); /* Round down to a sector */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001155 }
1156 if (nread == 0) {
1157 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1158 curlun->sense_data_info = file_offset >> 9;
1159 curlun->info_valid = 1;
1160 break;
1161 }
1162 file_offset += nread;
1163 amount_left -= nread;
1164 }
1165 return 0;
1166}
1167
1168
1169/*-------------------------------------------------------------------------*/
1170
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001171static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001172{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001173 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001174 u8 *buf = (u8 *) bh->buf;
1175
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001176 if (!curlun) { /* Unsupported LUNs are okay */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001177 common->bad_lun_okay = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001178 memset(buf, 0, 36);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001179 buf[0] = 0x7f; /* Unsupported, no device-type */
1180 buf[4] = 31; /* Additional length */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001181 return 36;
1182 }
1183
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001184 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK;
1185 buf[1] = curlun->removable ? 0x80 : 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001186 buf[2] = 2; /* ANSI SCSI level 2 */
1187 buf[3] = 2; /* SCSI-2 INQUIRY data format */
1188 buf[4] = 31; /* Additional length */
1189 buf[5] = 0; /* No special options */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001190 buf[6] = 0;
1191 buf[7] = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001192 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001193 return 36;
1194}
1195
1196
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001197static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001198{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001199 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001200 u8 *buf = (u8 *) bh->buf;
1201 u32 sd, sdinfo;
1202 int valid;
1203
1204 /*
1205 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1206 *
1207 * If a REQUEST SENSE command is received from an initiator
1208 * with a pending unit attention condition (before the target
1209 * generates the contingent allegiance condition), then the
1210 * target shall either:
1211 * a) report any pending sense data and preserve the unit
1212 * attention condition on the logical unit, or,
1213 * b) report the unit attention condition, may discard any
1214 * pending sense data, and clear the unit attention
1215 * condition on the logical unit for that initiator.
1216 *
1217 * FSG normally uses option a); enable this code to use option b).
1218 */
1219#if 0
1220 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1221 curlun->sense_data = curlun->unit_attention_data;
1222 curlun->unit_attention_data = SS_NO_SENSE;
1223 }
1224#endif
1225
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001226 if (!curlun) { /* Unsupported LUNs are okay */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001227 common->bad_lun_okay = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001228 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1229 sdinfo = 0;
1230 valid = 0;
1231 } else {
1232 sd = curlun->sense_data;
1233 sdinfo = curlun->sense_data_info;
1234 valid = curlun->info_valid << 7;
1235 curlun->sense_data = SS_NO_SENSE;
1236 curlun->sense_data_info = 0;
1237 curlun->info_valid = 0;
1238 }
1239
1240 memset(buf, 0, 18);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001241 buf[0] = valid | 0x70; /* Valid, current error */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001242 buf[2] = SK(sd);
1243 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001244 buf[7] = 18 - 8; /* Additional sense length */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001245 buf[12] = ASC(sd);
1246 buf[13] = ASCQ(sd);
1247 return 18;
1248}
1249
1250
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001251static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001252{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001253 struct fsg_lun *curlun = common->curlun;
1254 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1255 int pmi = common->cmnd[8];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001256 u8 *buf = (u8 *) bh->buf;
1257
1258 /* Check the PMI and LBA fields */
1259 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1260 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1261 return -EINVAL;
1262 }
1263
1264 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1265 /* Max logical block */
1266 put_unaligned_be32(512, &buf[4]); /* Block length */
1267 return 8;
1268}
1269
1270
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001271static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001272{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001273 struct fsg_lun *curlun = common->curlun;
1274 int msf = common->cmnd[1] & 0x02;
1275 u32 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001276 u8 *buf = (u8 *) bh->buf;
1277
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001278 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001279 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1280 return -EINVAL;
1281 }
1282 if (lba >= curlun->num_sectors) {
1283 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1284 return -EINVAL;
1285 }
1286
1287 memset(buf, 0, 8);
1288 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1289 store_cdrom_address(&buf[4], msf, lba);
1290 return 8;
1291}
1292
1293
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001294static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001295{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001296 struct fsg_lun *curlun = common->curlun;
1297 int msf = common->cmnd[1] & 0x02;
1298 int start_track = common->cmnd[6];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001299 u8 *buf = (u8 *) bh->buf;
1300
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001301 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001302 start_track > 1) {
1303 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1304 return -EINVAL;
1305 }
1306
1307 memset(buf, 0, 20);
1308 buf[1] = (20-2); /* TOC data length */
1309 buf[2] = 1; /* First track number */
1310 buf[3] = 1; /* Last track number */
1311 buf[5] = 0x16; /* Data track, copying allowed */
1312 buf[6] = 0x01; /* Only track is number 1 */
1313 store_cdrom_address(&buf[8], msf, 0);
1314
1315 buf[13] = 0x16; /* Lead-out track is data */
1316 buf[14] = 0xAA; /* Lead-out track number */
1317 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1318 return 20;
1319}
1320
1321
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001322static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001323{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001324 struct fsg_lun *curlun = common->curlun;
1325 int mscmnd = common->cmnd[0];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001326 u8 *buf = (u8 *) bh->buf;
1327 u8 *buf0 = buf;
1328 int pc, page_code;
1329 int changeable_values, all_pages;
1330 int valid_page = 0;
1331 int len, limit;
1332
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001333 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001334 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1335 return -EINVAL;
1336 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001337 pc = common->cmnd[2] >> 6;
1338 page_code = common->cmnd[2] & 0x3f;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001339 if (pc == 3) {
1340 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1341 return -EINVAL;
1342 }
1343 changeable_values = (pc == 1);
1344 all_pages = (page_code == 0x3f);
1345
1346 /* Write the mode parameter header. Fixed values are: default
1347 * medium type, no cache control (DPOFUA), and no block descriptors.
1348 * The only variable value is the WriteProtect bit. We will fill in
1349 * the mode data length later. */
1350 memset(buf, 0, 8);
1351 if (mscmnd == SC_MODE_SENSE_6) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001352 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001353 buf += 4;
1354 limit = 255;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001355 } else { /* SC_MODE_SENSE_10 */
1356 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001357 buf += 8;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001358 limit = 65535; /* Should really be FSG_BUFLEN */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001359 }
1360
1361 /* No block descriptors */
1362
1363 /* The mode pages, in numerical order. The only page we support
1364 * is the Caching page. */
1365 if (page_code == 0x08 || all_pages) {
1366 valid_page = 1;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001367 buf[0] = 0x08; /* Page code */
1368 buf[1] = 10; /* Page length */
1369 memset(buf+2, 0, 10); /* None of the fields are changeable */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001370
1371 if (!changeable_values) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001372 buf[2] = 0x04; /* Write cache enable, */
1373 /* Read cache not disabled */
1374 /* No cache retention priorities */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001375 put_unaligned_be16(0xffff, &buf[4]);
1376 /* Don't disable prefetch */
1377 /* Minimum prefetch = 0 */
1378 put_unaligned_be16(0xffff, &buf[8]);
1379 /* Maximum prefetch */
1380 put_unaligned_be16(0xffff, &buf[10]);
1381 /* Maximum prefetch ceiling */
1382 }
1383 buf += 12;
1384 }
1385
1386 /* Check that a valid page was requested and the mode data length
1387 * isn't too long. */
1388 len = buf - buf0;
1389 if (!valid_page || len > limit) {
1390 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1391 return -EINVAL;
1392 }
1393
1394 /* Store the mode data length */
1395 if (mscmnd == SC_MODE_SENSE_6)
1396 buf0[0] = len - 1;
1397 else
1398 put_unaligned_be16(len - 2, buf0);
1399 return len;
1400}
1401
1402
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001403static int do_start_stop(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001404{
Fabien Chouteau31436a12010-04-26 12:34:54 +02001405 struct fsg_lun *curlun = common->curlun;
1406 int loej, start;
1407
1408 if (!curlun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001409 return -EINVAL;
Fabien Chouteau31436a12010-04-26 12:34:54 +02001410 } else if (!curlun->removable) {
1411 curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001412 return -EINVAL;
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +02001413 } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
1414 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
Fabien Chouteau31436a12010-04-26 12:34:54 +02001415 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1416 return -EINVAL;
1417 }
1418
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +02001419 loej = common->cmnd[4] & 0x02;
1420 start = common->cmnd[4] & 0x01;
Fabien Chouteau31436a12010-04-26 12:34:54 +02001421
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +02001422 /* Our emulation doesn't support mounting; the medium is
1423 * available for use as soon as it is loaded. */
1424 if (start) {
Fabien Chouteau31436a12010-04-26 12:34:54 +02001425 if (!fsg_lun_is_open(curlun)) {
1426 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1427 return -EINVAL;
1428 }
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +02001429 return 0;
Fabien Chouteau31436a12010-04-26 12:34:54 +02001430 }
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +02001431
1432 /* Are we allowed to unload the media? */
1433 if (curlun->prevent_medium_removal) {
1434 LDBG(curlun, "unload attempt prevented\n");
1435 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1436 return -EINVAL;
1437 }
1438
1439 if (!loej)
1440 return 0;
1441
1442 /* Simulate an unload/eject */
1443 if (common->ops && common->ops->pre_eject) {
1444 int r = common->ops->pre_eject(common, curlun,
1445 curlun - common->luns);
1446 if (unlikely(r < 0))
1447 return r;
1448 else if (r)
1449 return 0;
1450 }
1451
1452 up_read(&common->filesem);
1453 down_write(&common->filesem);
1454 fsg_lun_close(curlun);
1455 up_write(&common->filesem);
1456 down_read(&common->filesem);
1457
1458 return common->ops && common->ops->post_eject
1459 ? min(0, common->ops->post_eject(common, curlun,
1460 curlun - common->luns))
1461 : 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001462}
1463
1464
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001465static int do_prevent_allow(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001466{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001467 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001468 int prevent;
1469
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001470 if (!common->curlun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001471 return -EINVAL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001472 } else if (!common->curlun->removable) {
1473 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001474 return -EINVAL;
1475 }
1476
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001477 prevent = common->cmnd[4] & 0x01;
1478 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001479 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1480 return -EINVAL;
1481 }
1482
1483 if (curlun->prevent_medium_removal && !prevent)
1484 fsg_lun_fsync_sub(curlun);
1485 curlun->prevent_medium_removal = prevent;
1486 return 0;
1487}
1488
1489
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001490static int do_read_format_capacities(struct fsg_common *common,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001491 struct fsg_buffhd *bh)
1492{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001493 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001494 u8 *buf = (u8 *) bh->buf;
1495
1496 buf[0] = buf[1] = buf[2] = 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001497 buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001498 buf += 4;
1499
1500 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1501 /* Number of blocks */
1502 put_unaligned_be32(512, &buf[4]); /* Block length */
1503 buf[4] = 0x02; /* Current capacity */
1504 return 12;
1505}
1506
1507
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001508static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001509{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001510 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001511
1512 /* We don't support MODE SELECT */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001513 if (curlun)
1514 curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001515 return -EINVAL;
1516}
1517
1518
1519/*-------------------------------------------------------------------------*/
1520
1521static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1522{
1523 int rc;
1524
1525 rc = fsg_set_halt(fsg, fsg->bulk_in);
1526 if (rc == -EAGAIN)
1527 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1528 while (rc != 0) {
1529 if (rc != -EAGAIN) {
1530 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1531 rc = 0;
1532 break;
1533 }
1534
1535 /* Wait for a short time and then try again */
1536 if (msleep_interruptible(100) != 0)
1537 return -EINTR;
1538 rc = usb_ep_set_halt(fsg->bulk_in);
1539 }
1540 return rc;
1541}
1542
1543static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1544{
1545 int rc;
1546
1547 DBG(fsg, "bulk-in set wedge\n");
1548 rc = usb_ep_set_wedge(fsg->bulk_in);
1549 if (rc == -EAGAIN)
1550 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1551 while (rc != 0) {
1552 if (rc != -EAGAIN) {
1553 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1554 rc = 0;
1555 break;
1556 }
1557
1558 /* Wait for a short time and then try again */
1559 if (msleep_interruptible(100) != 0)
1560 return -EINTR;
1561 rc = usb_ep_set_wedge(fsg->bulk_in);
1562 }
1563 return rc;
1564}
1565
1566static int pad_with_zeros(struct fsg_dev *fsg)
1567{
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001568 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001569 u32 nkeep = bh->inreq->length;
1570 u32 nsend;
1571 int rc;
1572
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001573 bh->state = BUF_STATE_EMPTY; /* For the first iteration */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001574 fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1575 while (fsg->common->usb_amount_left > 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001576
1577 /* Wait for the next buffer to be free */
1578 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001579 rc = sleep_thread(fsg->common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001580 if (rc)
1581 return rc;
1582 }
1583
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001584 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001585 memset(bh->buf + nkeep, 0, nsend - nkeep);
1586 bh->inreq->length = nsend;
1587 bh->inreq->zero = 0;
1588 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1589 &bh->inreq_busy, &bh->state);
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001590 bh = fsg->common->next_buffhd_to_fill = bh->next;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001591 fsg->common->usb_amount_left -= nsend;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001592 nkeep = 0;
1593 }
1594 return 0;
1595}
1596
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001597static int throw_away_data(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001598{
1599 struct fsg_buffhd *bh;
1600 u32 amount;
1601 int rc;
1602
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001603 for (bh = common->next_buffhd_to_drain;
1604 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1605 bh = common->next_buffhd_to_drain) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001606
1607 /* Throw away the data in a filled buffer */
1608 if (bh->state == BUF_STATE_FULL) {
1609 smp_rmb();
1610 bh->state = BUF_STATE_EMPTY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001611 common->next_buffhd_to_drain = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001612
1613 /* A short packet or an error ends everything */
1614 if (bh->outreq->actual != bh->outreq->length ||
1615 bh->outreq->status != 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001616 raise_exception(common,
1617 FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001618 return -EINTR;
1619 }
1620 continue;
1621 }
1622
1623 /* Try to submit another request if we need one */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001624 bh = common->next_buffhd_to_fill;
1625 if (bh->state == BUF_STATE_EMPTY
1626 && common->usb_amount_left > 0) {
1627 amount = min(common->usb_amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001628
1629 /* amount is always divisible by 512, hence by
1630 * the bulk-out maxpacket size */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001631 bh->outreq->length = amount;
1632 bh->bulk_out_intended_length = amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001633 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001634 START_TRANSFER_OR(common, bulk_out, bh->outreq,
1635 &bh->outreq_busy, &bh->state)
1636 /* Don't know what to do if
1637 * common->fsg is NULL */
1638 return -EIO;
1639 common->next_buffhd_to_fill = bh->next;
1640 common->usb_amount_left -= amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001641 continue;
1642 }
1643
1644 /* Otherwise wait for something to happen */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001645 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001646 if (rc)
1647 return rc;
1648 }
1649 return 0;
1650}
1651
1652
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001653static int finish_reply(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001654{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001655 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001656 int rc = 0;
1657
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001658 switch (common->data_dir) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001659 case DATA_DIR_NONE:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001660 break; /* Nothing to send */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001661
1662 /* If we don't know whether the host wants to read or write,
1663 * this must be CB or CBI with an unknown command. We mustn't
1664 * try to send or receive any data. So stall both bulk pipes
1665 * if we can and wait for a reset. */
1666 case DATA_DIR_UNKNOWN:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001667 if (!common->can_stall) {
1668 /* Nothing */
1669 } else if (fsg_is_set(common)) {
1670 fsg_set_halt(common->fsg, common->fsg->bulk_out);
1671 rc = halt_bulk_in_endpoint(common->fsg);
1672 } else {
1673 /* Don't know what to do if common->fsg is NULL */
1674 rc = -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001675 }
1676 break;
1677
1678 /* All but the last buffer of data must have already been sent */
1679 case DATA_DIR_TO_HOST:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001680 if (common->data_size == 0) {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001681 /* Nothing to send */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001682
1683 /* If there's no residue, simply send the last buffer */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001684 } else if (common->residue == 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001685 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001686 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1687 &bh->inreq_busy, &bh->state)
1688 return -EIO;
1689 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001690
1691 /* For Bulk-only, if we're allowed to stall then send the
1692 * short packet and halt the bulk-in endpoint. If we can't
1693 * stall, pad out the remaining data with 0's. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001694 } else if (common->can_stall) {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001695 bh->inreq->zero = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001696 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1697 &bh->inreq_busy, &bh->state)
1698 /* Don't know what to do if
1699 * common->fsg is NULL */
1700 rc = -EIO;
1701 common->next_buffhd_to_fill = bh->next;
1702 if (common->fsg)
1703 rc = halt_bulk_in_endpoint(common->fsg);
1704 } else if (fsg_is_set(common)) {
1705 rc = pad_with_zeros(common->fsg);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001706 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001707 /* Don't know what to do if common->fsg is NULL */
1708 rc = -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001709 }
1710 break;
1711
1712 /* We have processed all we want from the data the host has sent.
1713 * There may still be outstanding bulk-out requests. */
1714 case DATA_DIR_FROM_HOST:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001715 if (common->residue == 0) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001716 /* Nothing to receive */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001717
1718 /* Did the host stop sending unexpectedly early? */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001719 } else if (common->short_packet_received) {
1720 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001721 rc = -EINTR;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001722
1723 /* We haven't processed all the incoming data. Even though
1724 * we may be allowed to stall, doing so would cause a race.
1725 * The controller may already have ACK'ed all the remaining
1726 * bulk-out packets, in which case the host wouldn't see a
1727 * STALL. Not realizing the endpoint was halted, it wouldn't
1728 * clear the halt -- leading to problems later on. */
1729#if 0
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001730 } else if (common->can_stall) {
1731 if (fsg_is_set(common))
1732 fsg_set_halt(common->fsg,
1733 common->fsg->bulk_out);
1734 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001735 rc = -EINTR;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001736#endif
1737
1738 /* We can't stall. Read in the excess data and throw it
1739 * all away. */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001740 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001741 rc = throw_away_data(common);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001742 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001743 break;
1744 }
1745 return rc;
1746}
1747
1748
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001749static int send_status(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001750{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001751 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001752 struct fsg_buffhd *bh;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001753 struct bulk_cs_wrap *csw;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001754 int rc;
1755 u8 status = USB_STATUS_PASS;
1756 u32 sd, sdinfo = 0;
1757
1758 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001759 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001760 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001761 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001762 if (rc)
1763 return rc;
1764 }
1765
1766 if (curlun) {
1767 sd = curlun->sense_data;
1768 sdinfo = curlun->sense_data_info;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001769 } else if (common->bad_lun_okay)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001770 sd = SS_NO_SENSE;
1771 else
1772 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1773
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001774 if (common->phase_error) {
1775 DBG(common, "sending phase-error status\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001776 status = USB_STATUS_PHASE_ERROR;
1777 sd = SS_INVALID_COMMAND;
1778 } else if (sd != SS_NO_SENSE) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001779 DBG(common, "sending command-failure status\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001780 status = USB_STATUS_FAIL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001781 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001782 " info x%x\n",
1783 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1784 }
1785
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001786 /* Store and send the Bulk-only CSW */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001787 csw = (void *)bh->buf;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001788
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001789 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001790 csw->Tag = common->tag;
1791 csw->Residue = cpu_to_le32(common->residue);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001792 csw->Status = status;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001793
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001794 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1795 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001796 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1797 &bh->inreq_busy, &bh->state)
1798 /* Don't know what to do if common->fsg is NULL */
1799 return -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001800
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001801 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001802 return 0;
1803}
1804
1805
1806/*-------------------------------------------------------------------------*/
1807
1808/* Check whether the command is properly formed and whether its data size
1809 * and direction agree with the values we already have. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001810static int check_command(struct fsg_common *common, int cmnd_size,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001811 enum data_direction data_dir, unsigned int mask,
1812 int needs_medium, const char *name)
1813{
1814 int i;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001815 int lun = common->cmnd[1] >> 5;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001816 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1817 char hdlen[20];
1818 struct fsg_lun *curlun;
1819
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001820 hdlen[0] = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001821 if (common->data_dir != DATA_DIR_UNKNOWN)
1822 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1823 common->data_size);
1824 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001825 name, cmnd_size, dirletter[(int) data_dir],
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001826 common->data_size_from_cmnd, common->cmnd_size, hdlen);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001827
1828 /* We can't reply at all until we know the correct data direction
1829 * and size. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001830 if (common->data_size_from_cmnd == 0)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001831 data_dir = DATA_DIR_NONE;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001832 if (common->data_size < common->data_size_from_cmnd) {
1833 /* Host data size < Device data size is a phase error.
1834 * Carry out the command, but only transfer as much as
1835 * we are allowed. */
1836 common->data_size_from_cmnd = common->data_size;
1837 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001838 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001839 common->residue = common->data_size;
1840 common->usb_amount_left = common->data_size;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001841
1842 /* Conflicting data directions is a phase error */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001843 if (common->data_dir != data_dir
1844 && common->data_size_from_cmnd > 0) {
1845 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001846 return -EINVAL;
1847 }
1848
1849 /* Verify the length of the command itself */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001850 if (cmnd_size != common->cmnd_size) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001851
1852 /* Special case workaround: There are plenty of buggy SCSI
1853 * implementations. Many have issues with cbw->Length
1854 * field passing a wrong command size. For those cases we
1855 * always try to work around the problem by using the length
1856 * sent by the host side provided it is at least as large
1857 * as the correct command length.
1858 * Examples of such cases would be MS-Windows, which issues
1859 * REQUEST SENSE with cbw->Length == 12 where it should
1860 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1861 * REQUEST SENSE with cbw->Length == 10 where it should
1862 * be 6 as well.
1863 */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001864 if (cmnd_size <= common->cmnd_size) {
1865 DBG(common, "%s is buggy! Expected length %d "
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001866 "but we got %d\n", name,
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001867 cmnd_size, common->cmnd_size);
1868 cmnd_size = common->cmnd_size;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001869 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001870 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001871 return -EINVAL;
1872 }
1873 }
1874
1875 /* Check that the LUN values are consistent */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001876 if (common->lun != lun)
1877 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
1878 common->lun, lun);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001879
1880 /* Check the LUN */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001881 if (common->lun >= 0 && common->lun < common->nluns) {
1882 curlun = &common->luns[common->lun];
1883 common->curlun = curlun;
1884 if (common->cmnd[0] != SC_REQUEST_SENSE) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001885 curlun->sense_data = SS_NO_SENSE;
1886 curlun->sense_data_info = 0;
1887 curlun->info_valid = 0;
1888 }
1889 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001890 common->curlun = NULL;
1891 curlun = NULL;
1892 common->bad_lun_okay = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001893
1894 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
1895 * to use unsupported LUNs; all others may not. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001896 if (common->cmnd[0] != SC_INQUIRY &&
1897 common->cmnd[0] != SC_REQUEST_SENSE) {
1898 DBG(common, "unsupported LUN %d\n", common->lun);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001899 return -EINVAL;
1900 }
1901 }
1902
1903 /* If a unit attention condition exists, only INQUIRY and
1904 * REQUEST SENSE commands are allowed; anything else must fail. */
1905 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001906 common->cmnd[0] != SC_INQUIRY &&
1907 common->cmnd[0] != SC_REQUEST_SENSE) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001908 curlun->sense_data = curlun->unit_attention_data;
1909 curlun->unit_attention_data = SS_NO_SENSE;
1910 return -EINVAL;
1911 }
1912
1913 /* Check that only command bytes listed in the mask are non-zero */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001914 common->cmnd[1] &= 0x1f; /* Mask away the LUN */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001915 for (i = 1; i < cmnd_size; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001916 if (common->cmnd[i] && !(mask & (1 << i))) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001917 if (curlun)
1918 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1919 return -EINVAL;
1920 }
1921 }
1922
1923 /* If the medium isn't mounted and the command needs to access
1924 * it, return an error. */
1925 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
1926 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1927 return -EINVAL;
1928 }
1929
1930 return 0;
1931}
1932
1933
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001934static int do_scsi_command(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001935{
1936 struct fsg_buffhd *bh;
1937 int rc;
1938 int reply = -EINVAL;
1939 int i;
1940 static char unknown[16];
1941
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001942 dump_cdb(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001943
1944 /* Wait for the next buffer to become available for data or status */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001945 bh = common->next_buffhd_to_fill;
1946 common->next_buffhd_to_drain = bh;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001947 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001948 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001949 if (rc)
1950 return rc;
1951 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001952 common->phase_error = 0;
1953 common->short_packet_received = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001954
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001955 down_read(&common->filesem); /* We're using the backing file */
1956 switch (common->cmnd[0]) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001957
1958 case SC_INQUIRY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001959 common->data_size_from_cmnd = common->cmnd[4];
1960 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001961 (1<<4), 0,
1962 "INQUIRY");
1963 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001964 reply = do_inquiry(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001965 break;
1966
1967 case SC_MODE_SELECT_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001968 common->data_size_from_cmnd = common->cmnd[4];
1969 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001970 (1<<1) | (1<<4), 0,
1971 "MODE SELECT(6)");
1972 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001973 reply = do_mode_select(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001974 break;
1975
1976 case SC_MODE_SELECT_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001977 common->data_size_from_cmnd =
1978 get_unaligned_be16(&common->cmnd[7]);
1979 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001980 (1<<1) | (3<<7), 0,
1981 "MODE SELECT(10)");
1982 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001983 reply = do_mode_select(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001984 break;
1985
1986 case SC_MODE_SENSE_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001987 common->data_size_from_cmnd = common->cmnd[4];
1988 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001989 (1<<1) | (1<<2) | (1<<4), 0,
1990 "MODE SENSE(6)");
1991 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001992 reply = do_mode_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001993 break;
1994
1995 case SC_MODE_SENSE_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001996 common->data_size_from_cmnd =
1997 get_unaligned_be16(&common->cmnd[7]);
1998 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001999 (1<<1) | (1<<2) | (3<<7), 0,
2000 "MODE SENSE(10)");
2001 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002002 reply = do_mode_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002003 break;
2004
2005 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002006 common->data_size_from_cmnd = 0;
2007 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002008 (1<<4), 0,
2009 "PREVENT-ALLOW MEDIUM REMOVAL");
2010 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002011 reply = do_prevent_allow(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002012 break;
2013
2014 case SC_READ_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002015 i = common->cmnd[4];
2016 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2017 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002018 (7<<1) | (1<<4), 1,
2019 "READ(6)");
2020 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002021 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002022 break;
2023
2024 case SC_READ_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002025 common->data_size_from_cmnd =
2026 get_unaligned_be16(&common->cmnd[7]) << 9;
2027 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002028 (1<<1) | (0xf<<2) | (3<<7), 1,
2029 "READ(10)");
2030 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002031 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002032 break;
2033
2034 case SC_READ_12:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002035 common->data_size_from_cmnd =
2036 get_unaligned_be32(&common->cmnd[6]) << 9;
2037 reply = check_command(common, 12, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002038 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2039 "READ(12)");
2040 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002041 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002042 break;
2043
2044 case SC_READ_CAPACITY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002045 common->data_size_from_cmnd = 8;
2046 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002047 (0xf<<2) | (1<<8), 1,
2048 "READ CAPACITY");
2049 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002050 reply = do_read_capacity(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002051 break;
2052
2053 case SC_READ_HEADER:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002054 if (!common->curlun || !common->curlun->cdrom)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002055 goto unknown_cmnd;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002056 common->data_size_from_cmnd =
2057 get_unaligned_be16(&common->cmnd[7]);
2058 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002059 (3<<7) | (0x1f<<1), 1,
2060 "READ HEADER");
2061 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002062 reply = do_read_header(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002063 break;
2064
2065 case SC_READ_TOC:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002066 if (!common->curlun || !common->curlun->cdrom)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002067 goto unknown_cmnd;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002068 common->data_size_from_cmnd =
2069 get_unaligned_be16(&common->cmnd[7]);
2070 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002071 (7<<6) | (1<<1), 1,
2072 "READ TOC");
2073 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002074 reply = do_read_toc(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002075 break;
2076
2077 case SC_READ_FORMAT_CAPACITIES:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002078 common->data_size_from_cmnd =
2079 get_unaligned_be16(&common->cmnd[7]);
2080 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002081 (3<<7), 1,
2082 "READ FORMAT CAPACITIES");
2083 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002084 reply = do_read_format_capacities(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002085 break;
2086
2087 case SC_REQUEST_SENSE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002088 common->data_size_from_cmnd = common->cmnd[4];
2089 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002090 (1<<4), 0,
2091 "REQUEST SENSE");
2092 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002093 reply = do_request_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002094 break;
2095
2096 case SC_START_STOP_UNIT:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002097 common->data_size_from_cmnd = 0;
2098 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002099 (1<<1) | (1<<4), 0,
2100 "START-STOP UNIT");
2101 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002102 reply = do_start_stop(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002103 break;
2104
2105 case SC_SYNCHRONIZE_CACHE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002106 common->data_size_from_cmnd = 0;
2107 reply = check_command(common, 10, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002108 (0xf<<2) | (3<<7), 1,
2109 "SYNCHRONIZE CACHE");
2110 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002111 reply = do_synchronize_cache(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002112 break;
2113
2114 case SC_TEST_UNIT_READY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002115 common->data_size_from_cmnd = 0;
2116 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002117 0, 1,
2118 "TEST UNIT READY");
2119 break;
2120
2121 /* Although optional, this command is used by MS-Windows. We
2122 * support a minimal version: BytChk must be 0. */
2123 case SC_VERIFY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002124 common->data_size_from_cmnd = 0;
2125 reply = check_command(common, 10, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002126 (1<<1) | (0xf<<2) | (3<<7), 1,
2127 "VERIFY");
2128 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002129 reply = do_verify(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002130 break;
2131
2132 case SC_WRITE_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002133 i = common->cmnd[4];
2134 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2135 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002136 (7<<1) | (1<<4), 1,
2137 "WRITE(6)");
2138 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002139 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002140 break;
2141
2142 case SC_WRITE_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002143 common->data_size_from_cmnd =
2144 get_unaligned_be16(&common->cmnd[7]) << 9;
2145 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002146 (1<<1) | (0xf<<2) | (3<<7), 1,
2147 "WRITE(10)");
2148 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002149 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002150 break;
2151
2152 case SC_WRITE_12:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002153 common->data_size_from_cmnd =
2154 get_unaligned_be32(&common->cmnd[6]) << 9;
2155 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002156 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2157 "WRITE(12)");
2158 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002159 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002160 break;
2161
2162 /* Some mandatory commands that we recognize but don't implement.
2163 * They don't mean much in this setting. It's left as an exercise
2164 * for anyone interested to implement RESERVE and RELEASE in terms
2165 * of Posix locks. */
2166 case SC_FORMAT_UNIT:
2167 case SC_RELEASE:
2168 case SC_RESERVE:
2169 case SC_SEND_DIAGNOSTIC:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002170 /* Fall through */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002171
2172 default:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002173unknown_cmnd:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002174 common->data_size_from_cmnd = 0;
2175 sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2176 reply = check_command(common, common->cmnd_size,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002177 DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2178 if (reply == 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002179 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002180 reply = -EINVAL;
2181 }
2182 break;
2183 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002184 up_read(&common->filesem);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002185
2186 if (reply == -EINTR || signal_pending(current))
2187 return -EINTR;
2188
2189 /* Set up the single reply buffer for finish_reply() */
2190 if (reply == -EINVAL)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002191 reply = 0; /* Error reply length */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002192 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2193 reply = min((u32) reply, common->data_size_from_cmnd);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002194 bh->inreq->length = reply;
2195 bh->state = BUF_STATE_FULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002196 common->residue -= reply;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002197 } /* Otherwise it's already set */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002198
2199 return 0;
2200}
2201
2202
2203/*-------------------------------------------------------------------------*/
2204
2205static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2206{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002207 struct usb_request *req = bh->outreq;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002208 struct fsg_bulk_cb_wrap *cbw = req->buf;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002209 struct fsg_common *common = fsg->common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002210
2211 /* Was this a real packet? Should it be ignored? */
2212 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2213 return -EINVAL;
2214
2215 /* Is the CBW valid? */
2216 if (req->actual != USB_BULK_CB_WRAP_LEN ||
2217 cbw->Signature != cpu_to_le32(
2218 USB_BULK_CB_SIG)) {
2219 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2220 req->actual,
2221 le32_to_cpu(cbw->Signature));
2222
2223 /* The Bulk-only spec says we MUST stall the IN endpoint
2224 * (6.6.1), so it's unavoidable. It also says we must
2225 * retain this state until the next reset, but there's
2226 * no way to tell the controller driver it should ignore
2227 * Clear-Feature(HALT) requests.
2228 *
2229 * We aren't required to halt the OUT endpoint; instead
2230 * we can simply accept and discard any data received
2231 * until the next reset. */
2232 wedge_bulk_in_endpoint(fsg);
2233 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2234 return -EINVAL;
2235 }
2236
2237 /* Is the CBW meaningful? */
2238 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2239 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2240 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2241 "cmdlen %u\n",
2242 cbw->Lun, cbw->Flags, cbw->Length);
2243
2244 /* We can do anything we want here, so let's stall the
2245 * bulk pipes if we are allowed to. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002246 if (common->can_stall) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002247 fsg_set_halt(fsg, fsg->bulk_out);
2248 halt_bulk_in_endpoint(fsg);
2249 }
2250 return -EINVAL;
2251 }
2252
2253 /* Save the command for later */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002254 common->cmnd_size = cbw->Length;
2255 memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002256 if (cbw->Flags & USB_BULK_IN_FLAG)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002257 common->data_dir = DATA_DIR_TO_HOST;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002258 else
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002259 common->data_dir = DATA_DIR_FROM_HOST;
2260 common->data_size = le32_to_cpu(cbw->DataTransferLength);
2261 if (common->data_size == 0)
2262 common->data_dir = DATA_DIR_NONE;
2263 common->lun = cbw->Lun;
2264 common->tag = cbw->Tag;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002265 return 0;
2266}
2267
2268
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002269static int get_next_command(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002270{
2271 struct fsg_buffhd *bh;
2272 int rc = 0;
2273
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002274 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002275 bh = common->next_buffhd_to_fill;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002276 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002277 rc = sleep_thread(common);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002278 if (rc)
2279 return rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002280 }
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002281
2282 /* Queue a request to read a Bulk-only CBW */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002283 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002284 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002285 START_TRANSFER_OR(common, bulk_out, bh->outreq,
2286 &bh->outreq_busy, &bh->state)
2287 /* Don't know what to do if common->fsg is NULL */
2288 return -EIO;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002289
2290 /* We will drain the buffer in software, which means we
2291 * can reuse it for the next filling. No need to advance
2292 * next_buffhd_to_fill. */
2293
2294 /* Wait for the CBW to arrive */
2295 while (bh->state != BUF_STATE_FULL) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002296 rc = sleep_thread(common);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002297 if (rc)
2298 return rc;
2299 }
2300 smp_rmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002301 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002302 bh->state = BUF_STATE_EMPTY;
2303
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002304 return rc;
2305}
2306
2307
2308/*-------------------------------------------------------------------------*/
2309
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002310static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002311 const struct usb_endpoint_descriptor *d)
2312{
2313 int rc;
2314
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002315 ep->driver_data = common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002316 rc = usb_ep_enable(ep, d);
2317 if (rc)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002318 ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002319 return rc;
2320}
2321
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002322static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002323 struct usb_request **preq)
2324{
2325 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2326 if (*preq)
2327 return 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002328 ERROR(common, "can't allocate request for %s\n", ep->name);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002329 return -ENOMEM;
2330}
2331
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002332/* Reset interface setting and re-init endpoint state (toggle etc). */
2333static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002334{
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002335 const struct usb_endpoint_descriptor *d;
2336 struct fsg_dev *fsg;
2337 int i, rc = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002338
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002339 if (common->running)
2340 DBG(common, "reset interface\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002341
2342reset:
2343 /* Deallocate the requests */
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002344 if (common->fsg) {
2345 fsg = common->fsg;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002346
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002347 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2348 struct fsg_buffhd *bh = &common->buffhds[i];
2349
2350 if (bh->inreq) {
2351 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2352 bh->inreq = NULL;
2353 }
2354 if (bh->outreq) {
2355 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2356 bh->outreq = NULL;
2357 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002358 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002359
2360 /* Disable the endpoints */
2361 if (fsg->bulk_in_enabled) {
2362 usb_ep_disable(fsg->bulk_in);
2363 fsg->bulk_in_enabled = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002364 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002365 if (fsg->bulk_out_enabled) {
2366 usb_ep_disable(fsg->bulk_out);
2367 fsg->bulk_out_enabled = 0;
2368 }
2369
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002370 common->fsg = NULL;
2371 wake_up(&common->fsg_wait);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002372 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002373
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002374 common->running = 0;
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002375 if (!new_fsg || rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002376 return rc;
2377
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002378 common->fsg = new_fsg;
2379 fsg = common->fsg;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002380
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002381 /* Enable the endpoints */
2382 d = fsg_ep_desc(common->gadget,
2383 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2384 rc = enable_endpoint(common, fsg->bulk_in, d);
2385 if (rc)
2386 goto reset;
2387 fsg->bulk_in_enabled = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002388
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002389 d = fsg_ep_desc(common->gadget,
2390 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2391 rc = enable_endpoint(common, fsg->bulk_out, d);
2392 if (rc)
2393 goto reset;
2394 fsg->bulk_out_enabled = 1;
2395 common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2396 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2397
2398 /* Allocate the requests */
2399 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2400 struct fsg_buffhd *bh = &common->buffhds[i];
2401
2402 rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002403 if (rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002404 goto reset;
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002405 rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002406 if (rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002407 goto reset;
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002408 bh->inreq->buf = bh->outreq->buf = bh->buf;
2409 bh->inreq->context = bh->outreq->context = bh;
2410 bh->inreq->complete = bulk_in_complete;
2411 bh->outreq->complete = bulk_out_complete;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002412 }
2413
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002414 common->running = 1;
2415 for (i = 0; i < common->nluns; ++i)
2416 common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002417 return rc;
2418}
2419
2420
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002421/****************************** ALT CONFIGS ******************************/
2422
2423
2424static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2425{
2426 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002427 fsg->common->new_fsg = fsg;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002428 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002429 return 0;
2430}
2431
2432static void fsg_disable(struct usb_function *f)
2433{
2434 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002435 fsg->common->new_fsg = NULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002436 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002437}
2438
2439
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002440/*-------------------------------------------------------------------------*/
2441
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002442static void handle_exception(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002443{
2444 siginfo_t info;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002445 int i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002446 struct fsg_buffhd *bh;
2447 enum fsg_state old_state;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002448 struct fsg_lun *curlun;
2449 unsigned int exception_req_tag;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002450
2451 /* Clear the existing signals. Anything but SIGUSR1 is converted
2452 * into a high-priority EXIT exception. */
2453 for (;;) {
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002454 int sig =
2455 dequeue_signal_lock(current, &current->blocked, &info);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002456 if (!sig)
2457 break;
2458 if (sig != SIGUSR1) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002459 if (common->state < FSG_STATE_EXIT)
2460 DBG(common, "Main thread exiting on signal\n");
2461 raise_exception(common, FSG_STATE_EXIT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002462 }
2463 }
2464
2465 /* Cancel all the pending transfers */
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002466 if (likely(common->fsg)) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002467 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002468 bh = &common->buffhds[i];
2469 if (bh->inreq_busy)
2470 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2471 if (bh->outreq_busy)
2472 usb_ep_dequeue(common->fsg->bulk_out,
2473 bh->outreq);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002474 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002475
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002476 /* Wait until everything is idle */
2477 for (;;) {
2478 int num_active = 0;
2479 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2480 bh = &common->buffhds[i];
2481 num_active += bh->inreq_busy + bh->outreq_busy;
2482 }
2483 if (num_active == 0)
2484 break;
2485 if (sleep_thread(common))
2486 return;
2487 }
2488
2489 /* Clear out the controller's fifos */
2490 if (common->fsg->bulk_in_enabled)
2491 usb_ep_fifo_flush(common->fsg->bulk_in);
2492 if (common->fsg->bulk_out_enabled)
2493 usb_ep_fifo_flush(common->fsg->bulk_out);
2494 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002495
2496 /* Reset the I/O buffer states and pointers, the SCSI
2497 * state, and the exception. Then invoke the handler. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002498 spin_lock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002499
2500 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002501 bh = &common->buffhds[i];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002502 bh->state = BUF_STATE_EMPTY;
2503 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002504 common->next_buffhd_to_fill = &common->buffhds[0];
2505 common->next_buffhd_to_drain = &common->buffhds[0];
2506 exception_req_tag = common->exception_req_tag;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002507 old_state = common->state;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002508
2509 if (old_state == FSG_STATE_ABORT_BULK_OUT)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002510 common->state = FSG_STATE_STATUS_PHASE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002511 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002512 for (i = 0; i < common->nluns; ++i) {
2513 curlun = &common->luns[i];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002514 curlun->prevent_medium_removal = 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002515 curlun->sense_data = SS_NO_SENSE;
2516 curlun->unit_attention_data = SS_NO_SENSE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002517 curlun->sense_data_info = 0;
2518 curlun->info_valid = 0;
2519 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002520 common->state = FSG_STATE_IDLE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002521 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002522 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002523
2524 /* Carry out any extra actions required for the exception */
2525 switch (old_state) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002526 case FSG_STATE_ABORT_BULK_OUT:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002527 send_status(common);
2528 spin_lock_irq(&common->lock);
2529 if (common->state == FSG_STATE_STATUS_PHASE)
2530 common->state = FSG_STATE_IDLE;
2531 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002532 break;
2533
2534 case FSG_STATE_RESET:
2535 /* In case we were forced against our will to halt a
2536 * bulk endpoint, clear the halt now. (The SuperH UDC
2537 * requires this.) */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002538 if (!fsg_is_set(common))
2539 break;
2540 if (test_and_clear_bit(IGNORE_BULK_OUT,
2541 &common->fsg->atomic_bitflags))
2542 usb_ep_clear_halt(common->fsg->bulk_in);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002543
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002544 if (common->ep0_req_tag == exception_req_tag)
2545 ep0_queue(common); /* Complete the status stage */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002546
2547 /* Technically this should go here, but it would only be
2548 * a waste of time. Ditto for the INTERFACE_CHANGE and
2549 * CONFIG_CHANGE cases. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002550 /* for (i = 0; i < common->nluns; ++i) */
2551 /* common->luns[i].unit_attention_data = */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002552 /* SS_RESET_OCCURRED; */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002553 break;
2554
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002555 case FSG_STATE_CONFIG_CHANGE:
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002556 do_set_interface(common, common->new_fsg);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002557 break;
2558
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002559 case FSG_STATE_EXIT:
2560 case FSG_STATE_TERMINATED:
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002561 do_set_interface(common, NULL); /* Free resources */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002562 spin_lock_irq(&common->lock);
2563 common->state = FSG_STATE_TERMINATED; /* Stop the thread */
2564 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002565 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002566
2567 case FSG_STATE_INTERFACE_CHANGE:
2568 case FSG_STATE_DISCONNECT:
2569 case FSG_STATE_COMMAND_PHASE:
2570 case FSG_STATE_DATA_PHASE:
2571 case FSG_STATE_STATUS_PHASE:
2572 case FSG_STATE_IDLE:
2573 break;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002574 }
2575}
2576
2577
2578/*-------------------------------------------------------------------------*/
2579
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002580static int fsg_main_thread(void *common_)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002581{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002582 struct fsg_common *common = common_;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002583
2584 /* Allow the thread to be killed by a signal, but set the signal mask
2585 * to block everything but INT, TERM, KILL, and USR1. */
2586 allow_signal(SIGINT);
2587 allow_signal(SIGTERM);
2588 allow_signal(SIGKILL);
2589 allow_signal(SIGUSR1);
2590
2591 /* Allow the thread to be frozen */
2592 set_freezable();
2593
2594 /* Arrange for userspace references to be interpreted as kernel
2595 * pointers. That way we can pass a kernel pointer to a routine
2596 * that expects a __user pointer and it will work okay. */
2597 set_fs(get_ds());
2598
2599 /* The main loop */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002600 while (common->state != FSG_STATE_TERMINATED) {
2601 if (exception_in_progress(common) || signal_pending(current)) {
2602 handle_exception(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002603 continue;
2604 }
2605
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002606 if (!common->running) {
2607 sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002608 continue;
2609 }
2610
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002611 if (get_next_command(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002612 continue;
2613
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002614 spin_lock_irq(&common->lock);
2615 if (!exception_in_progress(common))
2616 common->state = FSG_STATE_DATA_PHASE;
2617 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002618
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002619 if (do_scsi_command(common) || finish_reply(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002620 continue;
2621
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002622 spin_lock_irq(&common->lock);
2623 if (!exception_in_progress(common))
2624 common->state = FSG_STATE_STATUS_PHASE;
2625 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002626
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002627 if (send_status(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002628 continue;
2629
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002630 spin_lock_irq(&common->lock);
2631 if (!exception_in_progress(common))
2632 common->state = FSG_STATE_IDLE;
2633 spin_unlock_irq(&common->lock);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002634 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002635
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002636 spin_lock_irq(&common->lock);
2637 common->thread_task = NULL;
2638 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002639
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +02002640 if (!common->ops || !common->ops->thread_exits
2641 || common->ops->thread_exits(common) < 0) {
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +01002642 struct fsg_lun *curlun = common->luns;
2643 unsigned i = common->nluns;
2644
2645 down_write(&common->filesem);
2646 for (; i--; ++curlun) {
2647 if (!fsg_lun_is_open(curlun))
2648 continue;
2649
2650 fsg_lun_close(curlun);
2651 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
2652 }
2653 up_write(&common->filesem);
2654 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002655
2656 /* Let the unbind and cleanup routines know the thread has exited */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002657 complete_and_exit(&common->thread_notifier, 0);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002658}
2659
2660
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002661/*************************** DEVICE ATTRIBUTES ***************************/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002662
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002663/* Write permission is checked per LUN in store_*() functions. */
2664static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
2665static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002666
2667
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002668/****************************** FSG COMMON ******************************/
2669
2670static void fsg_common_release(struct kref *ref);
2671
2672static void fsg_lun_release(struct device *dev)
2673{
2674 /* Nothing needs to be done */
2675}
2676
2677static inline void fsg_common_get(struct fsg_common *common)
2678{
2679 kref_get(&common->ref);
2680}
2681
2682static inline void fsg_common_put(struct fsg_common *common)
2683{
2684 kref_put(&common->ref, fsg_common_release);
2685}
2686
2687
2688static struct fsg_common *fsg_common_init(struct fsg_common *common,
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002689 struct usb_composite_dev *cdev,
2690 struct fsg_config *cfg)
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002691{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002692 struct usb_gadget *gadget = cdev->gadget;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002693 struct fsg_buffhd *bh;
2694 struct fsg_lun *curlun;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002695 struct fsg_lun_config *lcfg;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002696 int nluns, i, rc;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002697 char *pathbuf;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002698
2699 /* Find out how many LUNs there should be */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002700 nluns = cfg->nluns;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002701 if (nluns < 1 || nluns > FSG_MAX_LUNS) {
2702 dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns);
2703 return ERR_PTR(-EINVAL);
2704 }
2705
2706 /* Allocate? */
2707 if (!common) {
2708 common = kzalloc(sizeof *common, GFP_KERNEL);
2709 if (!common)
2710 return ERR_PTR(-ENOMEM);
2711 common->free_storage_on_release = 1;
2712 } else {
2713 memset(common, 0, sizeof common);
2714 common->free_storage_on_release = 0;
2715 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002716
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +02002717 common->ops = cfg->ops;
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01002718 common->private_data = cfg->private_data;
2719
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002720 common->gadget = gadget;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002721 common->ep0 = gadget->ep0;
2722 common->ep0req = cdev->req;
2723
2724 /* Maybe allocate device-global string IDs, and patch descriptors */
2725 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2726 rc = usb_string_id(cdev);
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002727 if (unlikely(rc < 0))
2728 goto error_release;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002729 fsg_strings[FSG_STRING_INTERFACE].id = rc;
2730 fsg_intf_desc.iInterface = rc;
2731 }
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002732
2733 /* Create the LUNs, open their backing files, and register the
2734 * LUN devices in sysfs. */
2735 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002736 if (unlikely(!curlun)) {
2737 rc = -ENOMEM;
2738 goto error_release;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002739 }
2740 common->luns = curlun;
2741
2742 init_rwsem(&common->filesem);
2743
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002744 for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
2745 curlun->cdrom = !!lcfg->cdrom;
2746 curlun->ro = lcfg->cdrom || lcfg->ro;
2747 curlun->removable = lcfg->removable;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002748 curlun->dev.release = fsg_lun_release;
2749 curlun->dev.parent = &gadget->dev;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002750 /* curlun->dev.driver = &fsg_driver.driver; XXX */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002751 dev_set_drvdata(&curlun->dev, &common->filesem);
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01002752 dev_set_name(&curlun->dev,
2753 cfg->lun_name_format
2754 ? cfg->lun_name_format
2755 : "lun%d",
2756 i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002757
2758 rc = device_register(&curlun->dev);
2759 if (rc) {
2760 INFO(common, "failed to register LUN%d: %d\n", i, rc);
2761 common->nluns = i;
2762 goto error_release;
2763 }
2764
2765 rc = device_create_file(&curlun->dev, &dev_attr_ro);
2766 if (rc)
2767 goto error_luns;
2768 rc = device_create_file(&curlun->dev, &dev_attr_file);
2769 if (rc)
2770 goto error_luns;
2771
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002772 if (lcfg->filename) {
2773 rc = fsg_lun_open(curlun, lcfg->filename);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002774 if (rc)
2775 goto error_luns;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002776 } else if (!curlun->removable) {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002777 ERROR(common, "no file given for LUN%d\n", i);
2778 rc = -EINVAL;
2779 goto error_luns;
2780 }
2781 }
2782 common->nluns = nluns;
2783
2784
2785 /* Data buffers cyclic list */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002786 bh = common->buffhds;
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002787 i = FSG_NUM_BUFFERS;
2788 goto buffhds_first_it;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002789 do {
2790 bh->next = bh + 1;
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002791 ++bh;
2792buffhds_first_it:
2793 bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
2794 if (unlikely(!bh->buf)) {
2795 rc = -ENOMEM;
2796 goto error_release;
2797 }
2798 } while (--i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002799 bh->next = common->buffhds;
2800
2801
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002802 /* Prepare inquiryString */
2803 if (cfg->release != 0xffff) {
2804 i = cfg->release;
2805 } else {
Christoph Egger90f79762010-02-05 13:24:12 +01002806 i = usb_gadget_controller_number(gadget);
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002807 if (i >= 0) {
2808 i = 0x0300 + i;
2809 } else {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002810 WARNING(common, "controller '%s' not recognized\n",
2811 gadget->name);
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002812 i = 0x0399;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002813 }
2814 }
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002815#define OR(x, y) ((x) ? (x) : (y))
2816 snprintf(common->inquiry_string, sizeof common->inquiry_string,
2817 "%-8s%-16s%04x",
2818 OR(cfg->vendor_name, "Linux "),
2819 /* Assume product name dependent on the first LUN */
2820 OR(cfg->product_name, common->luns->cdrom
2821 ? "File-Stor Gadget"
2822 : "File-CD Gadget "),
2823 i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002824
2825
2826 /* Some peripheral controllers are known not to be able to
2827 * halt bulk endpoints correctly. If one of them is present,
2828 * disable stalls.
2829 */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002830 common->can_stall = cfg->can_stall &&
Christoph Egger90f79762010-02-05 13:24:12 +01002831 !(gadget_is_at91(common->gadget));
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002832
2833
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002834 spin_lock_init(&common->lock);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002835 kref_init(&common->ref);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002836
2837
2838 /* Tell the thread to start working */
2839 common->thread_task =
2840 kthread_create(fsg_main_thread, common,
2841 OR(cfg->thread_name, "file-storage"));
2842 if (IS_ERR(common->thread_task)) {
2843 rc = PTR_ERR(common->thread_task);
2844 goto error_release;
2845 }
2846 init_completion(&common->thread_notifier);
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002847 init_waitqueue_head(&common->fsg_wait);
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01002848#undef OR
2849
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002850
2851 /* Information */
2852 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
2853 INFO(common, "Number of LUNs=%d\n", common->nluns);
2854
2855 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2856 for (i = 0, nluns = common->nluns, curlun = common->luns;
2857 i < nluns;
2858 ++curlun, ++i) {
2859 char *p = "(no medium)";
2860 if (fsg_lun_is_open(curlun)) {
2861 p = "(error)";
2862 if (pathbuf) {
2863 p = d_path(&curlun->filp->f_path,
2864 pathbuf, PATH_MAX);
2865 if (IS_ERR(p))
2866 p = "(error)";
2867 }
2868 }
2869 LINFO(curlun, "LUN: %s%s%sfile: %s\n",
2870 curlun->removable ? "removable " : "",
2871 curlun->ro ? "read only " : "",
2872 curlun->cdrom ? "CD-ROM " : "",
2873 p);
2874 }
2875 kfree(pathbuf);
2876
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002877 DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task));
2878
2879 wake_up_process(common->thread_task);
2880
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002881 return common;
2882
2883
2884error_luns:
2885 common->nluns = i + 1;
2886error_release:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002887 common->state = FSG_STATE_TERMINATED; /* The thread is dead */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002888 /* Call fsg_common_release() directly, ref might be not
2889 * initialised */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002890 fsg_common_release(&common->ref);
2891 return ERR_PTR(rc);
2892}
2893
2894
2895static void fsg_common_release(struct kref *ref)
2896{
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002897 struct fsg_common *common = container_of(ref, struct fsg_common, ref);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002898
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002899 /* If the thread isn't already dead, tell it to exit now */
2900 if (common->state != FSG_STATE_TERMINATED) {
2901 raise_exception(common, FSG_STATE_EXIT);
2902 wait_for_completion(&common->thread_notifier);
2903
2904 /* The cleanup routine waits for this completion also */
2905 complete(&common->thread_notifier);
2906 }
2907
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002908 if (likely(common->luns)) {
2909 struct fsg_lun *lun = common->luns;
2910 unsigned i = common->nluns;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002911
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002912 /* In error recovery common->nluns may be zero. */
2913 for (; i; --i, ++lun) {
2914 device_remove_file(&lun->dev, &dev_attr_ro);
2915 device_remove_file(&lun->dev, &dev_attr_file);
2916 fsg_lun_close(lun);
2917 device_unregister(&lun->dev);
2918 }
2919
2920 kfree(common->luns);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002921 }
2922
Michal Nazarewiczb9e00082010-05-12 12:51:13 +02002923 {
2924 struct fsg_buffhd *bh = common->buffhds;
2925 unsigned i = FSG_NUM_BUFFERS;
2926 do {
2927 kfree(bh->buf);
2928 } while (++bh, --i);
2929 }
Michal Nazarewiczaae86e82010-03-15 21:38:31 +01002930
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002931 if (common->free_storage_on_release)
2932 kfree(common);
2933}
2934
2935
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002936/*-------------------------------------------------------------------------*/
2937
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002938
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002939static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002940{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002941 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002942 struct fsg_common *common = fsg->common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002943
2944 DBG(fsg, "unbind\n");
Michal Nazarewiczb894f602010-06-25 16:29:28 +02002945 if (fsg->common->fsg == fsg) {
2946 fsg->common->new_fsg = NULL;
2947 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2948 /* FIXME: make interruptible or killable somehow? */
2949 wait_event(common->fsg_wait, common->fsg != fsg);
2950 }
2951
2952 fsg_common_put(common);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02002953 usb_free_descriptors(fsg->function.descriptors);
2954 usb_free_descriptors(fsg->function.hs_descriptors);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002955 kfree(fsg);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002956}
2957
2958
Michal Nazarewicz28824b12010-05-05 12:53:13 +02002959static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002960{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002961 struct fsg_dev *fsg = fsg_from_func(f);
2962 struct usb_gadget *gadget = c->cdev->gadget;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002963 int i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002964 struct usb_ep *ep;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002965
2966 fsg->gadget = gadget;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002967
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002968 /* New interface */
2969 i = usb_interface_id(c, f);
2970 if (i < 0)
2971 return i;
2972 fsg_intf_desc.bInterfaceNumber = i;
2973 fsg->interface_number = i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002974
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002975 /* Find all the endpoints we will use */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002976 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2977 if (!ep)
2978 goto autoconf_fail;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002979 ep->driver_data = fsg->common; /* claim the endpoint */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002980 fsg->bulk_in = ep;
2981
2982 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2983 if (!ep)
2984 goto autoconf_fail;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002985 ep->driver_data = fsg->common; /* claim the endpoint */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002986 fsg->bulk_out = ep;
2987
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02002988 /* Copy descriptors */
2989 f->descriptors = usb_copy_descriptors(fsg_fs_function);
2990 if (unlikely(!f->descriptors))
2991 return -ENOMEM;
2992
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002993 if (gadget_is_dualspeed(gadget)) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002994 /* Assume endpoint addresses are the same for both speeds */
2995 fsg_hs_bulk_in_desc.bEndpointAddress =
2996 fsg_fs_bulk_in_desc.bEndpointAddress;
2997 fsg_hs_bulk_out_desc.bEndpointAddress =
2998 fsg_fs_bulk_out_desc.bEndpointAddress;
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02002999 f->hs_descriptors = usb_copy_descriptors(fsg_hs_function);
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003000 if (unlikely(!f->hs_descriptors)) {
3001 usb_free_descriptors(f->descriptors);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02003002 return -ENOMEM;
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003003 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003004 }
3005
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003006 return 0;
3007
3008autoconf_fail:
3009 ERROR(fsg, "unable to autoconfigure all endpoints\n");
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003010 return -ENOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003011}
3012
3013
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003014/****************************** ADD FUNCTION ******************************/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003015
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003016static struct usb_gadget_strings *fsg_strings_array[] = {
3017 &fsg_stringtab,
3018 NULL,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003019};
3020
Michal Nazarewicz1dc90982010-06-16 12:07:57 +02003021static int fsg_bind_config(struct usb_composite_dev *cdev,
3022 struct usb_configuration *c,
3023 struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003024{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003025 struct fsg_dev *fsg;
3026 int rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003027
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003028 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
3029 if (unlikely(!fsg))
3030 return -ENOMEM;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003031
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003032 fsg->function.name = FSG_DRIVER_DESC;
3033 fsg->function.strings = fsg_strings_array;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003034 fsg->function.bind = fsg_bind;
3035 fsg->function.unbind = fsg_unbind;
3036 fsg->function.setup = fsg_setup;
3037 fsg->function.set_alt = fsg_set_alt;
3038 fsg->function.disable = fsg_disable;
3039
3040 fsg->common = common;
3041 /* Our caller holds a reference to common structure so we
3042 * don't have to be worry about it being freed until we return
3043 * from this function. So instead of incrementing counter now
3044 * and decrement in error recovery we increment it only when
3045 * call to usb_add_function() was successful. */
3046
3047 rc = usb_add_function(c, &fsg->function);
Michal Nazarewicz0fb2c2a2010-03-29 14:01:32 +02003048 if (unlikely(rc))
Michal Nazarewicze5fd39d2010-06-25 16:29:26 +02003049 kfree(fsg);
3050 else
3051 fsg_common_get(fsg->common);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003052 return rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01003053}
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003054
Michal Nazarewicz1dc90982010-06-16 12:07:57 +02003055static inline int __deprecated __maybe_unused
3056fsg_add(struct usb_composite_dev *cdev,
3057 struct usb_configuration *c,
3058 struct fsg_common *common)
3059{
3060 return fsg_bind_config(cdev, c, common);
3061}
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003062
3063
3064/************************* Module parameters *************************/
3065
3066
3067struct fsg_module_parameters {
3068 char *file[FSG_MAX_LUNS];
3069 int ro[FSG_MAX_LUNS];
3070 int removable[FSG_MAX_LUNS];
3071 int cdrom[FSG_MAX_LUNS];
3072
3073 unsigned int file_count, ro_count, removable_count, cdrom_count;
3074 unsigned int luns; /* nluns */
3075 int stall; /* can_stall */
3076};
3077
3078
3079#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
3080 module_param_array_named(prefix ## name, params.name, type, \
3081 &prefix ## params.name ## _count, \
3082 S_IRUGO); \
3083 MODULE_PARM_DESC(prefix ## name, desc)
3084
3085#define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \
3086 module_param_named(prefix ## name, params.name, type, \
3087 S_IRUGO); \
3088 MODULE_PARM_DESC(prefix ## name, desc)
3089
3090#define FSG_MODULE_PARAMETERS(prefix, params) \
3091 _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \
3092 "names of backing files or devices"); \
3093 _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \
3094 "true to force read-only"); \
3095 _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \
3096 "true to simulate removable media"); \
3097 _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
3098 "true to simulate CD-ROM instead of disk"); \
3099 _FSG_MODULE_PARAM(prefix, params, luns, uint, \
3100 "number of LUNs"); \
3101 _FSG_MODULE_PARAM(prefix, params, stall, bool, \
3102 "false to prevent bulk stalls")
3103
3104
3105static void
3106fsg_config_from_params(struct fsg_config *cfg,
3107 const struct fsg_module_parameters *params)
3108{
3109 struct fsg_lun_config *lun;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003110 unsigned i;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003111
3112 /* Configure LUNs */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003113 cfg->nluns =
3114 min(params->luns ?: (params->file_count ?: 1u),
3115 (unsigned)FSG_MAX_LUNS);
3116 for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003117 lun->ro = !!params->ro[i];
3118 lun->cdrom = !!params->cdrom[i];
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003119 lun->removable = /* Removable by default */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003120 params->removable_count <= i || params->removable[i];
3121 lun->filename =
3122 params->file_count > i && params->file[i][0]
3123 ? params->file[i]
3124 : 0;
3125 }
3126
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003127 /* Let MSF use defaults */
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01003128 cfg->lun_name_format = 0;
3129 cfg->thread_name = 0;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003130 cfg->vendor_name = 0;
3131 cfg->product_name = 0;
3132 cfg->release = 0xffff;
3133
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +02003134 cfg->ops = NULL;
3135 cfg->private_data = NULL;
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01003136
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003137 /* Finalise */
3138 cfg->can_stall = params->stall;
3139}
3140
3141static inline struct fsg_common *
3142fsg_common_from_params(struct fsg_common *common,
3143 struct usb_composite_dev *cdev,
3144 const struct fsg_module_parameters *params)
3145 __attribute__((unused));
3146static inline struct fsg_common *
3147fsg_common_from_params(struct fsg_common *common,
3148 struct usb_composite_dev *cdev,
3149 const struct fsg_module_parameters *params)
3150{
3151 struct fsg_config cfg;
3152 fsg_config_from_params(&cfg, params);
3153 return fsg_common_init(common, cdev, &cfg);
3154}
3155