blob: a37640eba4344b144cc6d5747d155f55ec6f8239 [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 *
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100166 *
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100167 * This function is heavily based on "File-backed Storage Gadget" by
168 * Alan Stern which in turn is heavily based on "Gadget Zero" by David
169 * Brownell. The driver's SCSI command interface was based on the
170 * "Information technology - Small Computer System Interface - 2"
171 * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
172 * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
173 * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
174 * was based on the "Universal Serial Bus Mass Storage Class UFI
175 * Command Specification" document, Revision 1.0, December 14, 1998,
176 * available at
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100177 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
178 */
179
180
181/*
182 * Driver Design
183 *
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100184 * The MSF is fairly straightforward. There is a main kernel
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100185 * thread that handles most of the work. Interrupt routines field
186 * callbacks from the controller driver: bulk- and interrupt-request
187 * completion notifications, endpoint-0 events, and disconnect events.
188 * Completion events are passed to the main thread by wakeup calls. Many
189 * ep0 requests are handled at interrupt time, but SetInterface,
190 * SetConfiguration, and device reset requests are forwarded to the
191 * thread in the form of "exceptions" using SIGUSR1 signals (since they
192 * should interrupt any ongoing file I/O operations).
193 *
194 * The thread's main routine implements the standard command/data/status
195 * parts of a SCSI interaction. It and its subroutines are full of tests
196 * for pending signals/exceptions -- all this polling is necessary since
197 * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
198 * indication that the driver really wants to be running in userspace.)
199 * An important point is that so long as the thread is alive it keeps an
200 * open reference to the backing file. This will prevent unmounting
201 * the backing file's underlying filesystem and could cause problems
202 * during system shutdown, for example. To prevent such problems, the
203 * thread catches INT, TERM, and KILL signals and converts them into
204 * an EXIT exception.
205 *
206 * In normal operation the main thread is started during the gadget's
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100207 * fsg_bind() callback and stopped during fsg_unbind(). But it can
208 * also exit when it receives a signal, and there's no point leaving
209 * the gadget running when the thread is dead. At of this moment, MSF
210 * provides no way to deregister the gadget when thread dies -- maybe
211 * a callback functions is needed.
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100212 *
213 * To provide maximum throughput, the driver uses a circular pipeline of
214 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
215 * arbitrarily long; in practice the benefits don't justify having more
216 * than 2 stages (i.e., double buffering). But it helps to think of the
217 * pipeline as being a long one. Each buffer head contains a bulk-in and
218 * a bulk-out request pointer (since the buffer can be used for both
219 * output and input -- directions always are given from the host's
220 * point of view) as well as a pointer to the buffer and various state
221 * variables.
222 *
223 * Use of the pipeline follows a simple protocol. There is a variable
224 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
225 * At any time that buffer head may still be in use from an earlier
226 * request, so each buffer head has a state variable indicating whether
227 * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
228 * buffer head to be EMPTY, filling the buffer either by file I/O or by
229 * USB I/O (during which the buffer head is BUSY), and marking the buffer
230 * head FULL when the I/O is complete. Then the buffer will be emptied
231 * (again possibly by USB I/O, during which it is marked BUSY) and
232 * finally marked EMPTY again (possibly by a completion routine).
233 *
234 * A module parameter tells the driver to avoid stalling the bulk
235 * endpoints wherever the transport specification allows. This is
236 * necessary for some UDCs like the SuperH, which cannot reliably clear a
237 * halt on a bulk endpoint. However, under certain circumstances the
238 * Bulk-only specification requires a stall. In such cases the driver
239 * will halt the endpoint and set a flag indicating that it should clear
240 * the halt in software during the next device reset. Hopefully this
241 * will permit everything to work correctly. Furthermore, although the
242 * specification allows the bulk-out endpoint to halt when the host sends
243 * too much data, implementing this would cause an unavoidable race.
244 * The driver will always use the "no-stall" approach for OUT transfers.
245 *
246 * One subtle point concerns sending status-stage responses for ep0
247 * requests. Some of these requests, such as device reset, can involve
248 * interrupting an ongoing file I/O operation, which might take an
249 * arbitrarily long time. During that delay the host might give up on
250 * the original ep0 request and issue a new one. When that happens the
251 * driver should not notify the host about completion of the original
252 * request, as the host will no longer be waiting for it. So the driver
253 * assigns to each ep0 request a unique tag, and it keeps track of the
254 * tag value of the request associated with a long-running exception
255 * (device-reset, interface-change, or configuration-change). When the
256 * exception handler is finished, the status-stage response is submitted
257 * only if the current ep0 request tag is equal to the exception request
258 * tag. Thus only the most recently received ep0 request will get a
259 * status-stage response.
260 *
261 * Warning: This driver source file is too long. It ought to be split up
262 * into a header file plus about 3 separate .c files, to handle the details
263 * of the Gadget, USB Mass Storage, and SCSI protocols.
264 */
265
266
267/* #define VERBOSE_DEBUG */
268/* #define DUMP_MSGS */
269
270
271#include <linux/blkdev.h>
272#include <linux/completion.h>
273#include <linux/dcache.h>
274#include <linux/delay.h>
275#include <linux/device.h>
276#include <linux/fcntl.h>
277#include <linux/file.h>
278#include <linux/fs.h>
279#include <linux/kref.h>
280#include <linux/kthread.h>
281#include <linux/limits.h>
282#include <linux/rwsem.h>
283#include <linux/slab.h>
284#include <linux/spinlock.h>
285#include <linux/string.h>
286#include <linux/freezer.h>
287#include <linux/utsname.h>
288
289#include <linux/usb/ch9.h>
290#include <linux/usb/gadget.h>
291
292#include "gadget_chips.h"
293
294
295
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100296/*------------------------------------------------------------------------*/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100297
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100298#define FSG_DRIVER_DESC "Mass Storage Function"
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100299#define FSG_DRIVER_VERSION "2009/09/11"
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100300
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100301static const char fsg_string_interface[] = "Mass Storage";
302
303
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100304#define FSG_NO_INTR_EP 1
Michal Nazarewicz606206c22009-10-28 16:57:21 +0100305#define FSG_BUFFHD_STATIC_BUFFER 1
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100306#define FSG_NO_DEVICE_STRINGS 1
307#define FSG_NO_OTG 1
308#define FSG_NO_INTR_EP 1
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100309
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100310#include "storage_common.c"
311
312
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100313/*-------------------------------------------------------------------------*/
314
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100315struct fsg_dev;
316
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100317
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100318/* Data shared by all the FSG instances. */
319struct fsg_common {
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100320 struct usb_gadget *gadget;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100321 struct fsg_dev *fsg;
322 struct fsg_dev *prev_fsg;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100323
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100324 /* filesem protects: backing files in use */
325 struct rw_semaphore filesem;
326
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100327 /* lock protects: state, all the req_busy's */
328 spinlock_t lock;
329
330 struct usb_ep *ep0; /* Copy of gadget->ep0 */
331 struct usb_request *ep0req; /* Copy of cdev->req */
332 unsigned int ep0_req_tag;
333 const char *ep0req_name;
334
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100335 struct fsg_buffhd *next_buffhd_to_fill;
336 struct fsg_buffhd *next_buffhd_to_drain;
337 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
338
339 int cmnd_size;
340 u8 cmnd[MAX_COMMAND_SIZE];
341
342 unsigned int nluns;
343 unsigned int lun;
344 struct fsg_lun *luns;
345 struct fsg_lun *curlun;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100346
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100347 unsigned int bulk_out_maxpacket;
348 enum fsg_state state; /* For exception handling */
349 unsigned int exception_req_tag;
350
351 u8 config, new_config;
352 enum data_direction data_dir;
353 u32 data_size;
354 u32 data_size_from_cmnd;
355 u32 tag;
356 u32 residue;
357 u32 usb_amount_left;
358
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100359 unsigned int can_stall:1;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100360 unsigned int free_storage_on_release:1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100361 unsigned int phase_error:1;
362 unsigned int short_packet_received:1;
363 unsigned int bad_lun_okay:1;
364 unsigned int running:1;
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100365
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100366 int thread_wakeup_needed;
367 struct completion thread_notifier;
368 struct task_struct *thread_task;
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100369
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100370 /* Callback function to call when thread exits. */
371 void (*thread_exits)(struct fsg_common *common);
372 /* Gadget's private data. */
373 void *private_data;
374
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100375 /* Vendor (8 chars), product (16 chars), release (4
376 * hexadecimal digits) and NUL byte */
377 char inquiry_string[8 + 16 + 4 + 1];
378
Michal Nazarewicz9c610212009-10-28 16:57:22 +0100379 struct kref ref;
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100380};
381
382
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100383struct fsg_config {
384 unsigned nluns;
385 struct fsg_lun_config {
386 const char *filename;
387 char ro;
388 char removable;
389 char cdrom;
390 } luns[FSG_MAX_LUNS];
391
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +0100392 const char *lun_name_format;
393 const char *thread_name;
394
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100395 /* Callback function to call when thread exits. */
396 void (*thread_exits)(struct fsg_common *common);
397 /* Gadget's private data. */
398 void *private_data;
399
Michal Nazarewicz481e4922009-11-09 14:15:21 +0100400 const char *vendor_name; /* 8 characters or less */
401 const char *product_name; /* 16 characters or less */
402 u16 release;
403
404 char can_stall;
405};
406
407
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100408struct fsg_dev {
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100409 struct usb_function function;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100410 struct usb_gadget *gadget; /* Copy of cdev->gadget */
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100411 struct fsg_common *common;
412
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100413 u16 interface_number;
414
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100415 unsigned int bulk_in_enabled:1;
416 unsigned int bulk_out_enabled:1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100417
418 unsigned long atomic_bitflags;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100419#define IGNORE_BULK_OUT 0
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100420
421 struct usb_ep *bulk_in;
422 struct usb_ep *bulk_out;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100423};
424
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100425
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100426static inline int __fsg_is_set(struct fsg_common *common,
427 const char *func, unsigned line)
428{
429 if (common->fsg)
430 return 1;
431 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
432 return 0;
433}
434
435#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
436
437
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100438static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
439{
440 return container_of(f, struct fsg_dev, function);
441}
442
443
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100444typedef void (*fsg_routine_t)(struct fsg_dev *);
445
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100446static int exception_in_progress(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100447{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100448 return common->state > FSG_STATE_IDLE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100449}
450
451/* Make bulk-out requests be divisible by the maxpacket size */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100452static void set_bulk_out_req_length(struct fsg_common *common,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100453 struct fsg_buffhd *bh, unsigned int length)
454{
455 unsigned int rem;
456
457 bh->bulk_out_intended_length = length;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100458 rem = length % common->bulk_out_maxpacket;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100459 if (rem > 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100460 length += common->bulk_out_maxpacket - rem;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100461 bh->outreq->length = length;
462}
463
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100464/*-------------------------------------------------------------------------*/
465
466static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
467{
468 const char *name;
469
470 if (ep == fsg->bulk_in)
471 name = "bulk-in";
472 else if (ep == fsg->bulk_out)
473 name = "bulk-out";
474 else
475 name = ep->name;
476 DBG(fsg, "%s set halt\n", name);
477 return usb_ep_set_halt(ep);
478}
479
480
481/*-------------------------------------------------------------------------*/
482
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100483/* These routines may be called in process context or in_irq */
484
485/* Caller must hold fsg->lock */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100486static void wakeup_thread(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100487{
488 /* Tell the main thread that something has happened */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100489 common->thread_wakeup_needed = 1;
490 if (common->thread_task)
491 wake_up_process(common->thread_task);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100492}
493
494
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100495static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100496{
497 unsigned long flags;
498
499 /* Do nothing if a higher-priority exception is already in progress.
500 * If a lower-or-equal priority exception is in progress, preempt it
501 * and notify the main thread by sending it a signal. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100502 spin_lock_irqsave(&common->lock, flags);
503 if (common->state <= new_state) {
504 common->exception_req_tag = common->ep0_req_tag;
505 common->state = new_state;
506 if (common->thread_task)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100507 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100508 common->thread_task);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100509 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100510 spin_unlock_irqrestore(&common->lock, flags);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100511}
512
513
514/*-------------------------------------------------------------------------*/
515
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100516static int ep0_queue(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100517{
518 int rc;
519
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100520 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
521 common->ep0->driver_data = common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100522 if (rc != 0 && rc != -ESHUTDOWN) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100523 /* We can't do much more than wait for a reset */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100524 WARNING(common, "error in submission: %s --> %d\n",
525 common->ep0->name, rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100526 }
527 return rc;
528}
529
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100530/*-------------------------------------------------------------------------*/
531
532/* Bulk and interrupt endpoint completion handlers.
533 * These always run in_irq. */
534
535static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
536{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100537 struct fsg_common *common = ep->driver_data;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100538 struct fsg_buffhd *bh = req->context;
539
540 if (req->status || req->actual != req->length)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100541 DBG(common, "%s --> %d, %u/%u\n", __func__,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100542 req->status, req->actual, req->length);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100543 if (req->status == -ECONNRESET) /* Request was cancelled */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100544 usb_ep_fifo_flush(ep);
545
546 /* Hold the lock while we update the request and buffer states */
547 smp_wmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100548 spin_lock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100549 bh->inreq_busy = 0;
550 bh->state = BUF_STATE_EMPTY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100551 wakeup_thread(common);
552 spin_unlock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100553}
554
555static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
556{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100557 struct fsg_common *common = ep->driver_data;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100558 struct fsg_buffhd *bh = req->context;
559
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100560 dump_msg(common, "bulk-out", req->buf, req->actual);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100561 if (req->status || req->actual != bh->bulk_out_intended_length)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100562 DBG(common, "%s --> %d, %u/%u\n", __func__,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100563 req->status, req->actual,
564 bh->bulk_out_intended_length);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100565 if (req->status == -ECONNRESET) /* Request was cancelled */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100566 usb_ep_fifo_flush(ep);
567
568 /* Hold the lock while we update the request and buffer states */
569 smp_wmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100570 spin_lock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100571 bh->outreq_busy = 0;
572 bh->state = BUF_STATE_FULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100573 wakeup_thread(common);
574 spin_unlock(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100575}
576
577
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100578/*-------------------------------------------------------------------------*/
579
580/* Ep0 class-specific handlers. These always run in_irq. */
581
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100582static int fsg_setup(struct usb_function *f,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100583 const struct usb_ctrlrequest *ctrl)
584{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100585 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100586 struct usb_request *req = fsg->common->ep0req;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100587 u16 w_index = le16_to_cpu(ctrl->wIndex);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100588 u16 w_value = le16_to_cpu(ctrl->wValue);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100589 u16 w_length = le16_to_cpu(ctrl->wLength);
590
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100591 if (!fsg->common->config)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100592 return -EOPNOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100593
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100594 switch (ctrl->bRequest) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100595
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100596 case USB_BULK_RESET_REQUEST:
597 if (ctrl->bRequestType !=
598 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100599 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100600 if (w_index != fsg->interface_number || w_value != 0)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100601 return -EDOM;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100602
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100603 /* Raise an exception to stop the current operation
604 * and reinitialize our state. */
605 DBG(fsg, "bulk reset request\n");
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100606 raise_exception(fsg->common, FSG_STATE_RESET);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100607 return DELAYED_STATUS;
608
609 case USB_BULK_GET_MAX_LUN_REQUEST:
610 if (ctrl->bRequestType !=
611 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100612 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100613 if (w_index != fsg->interface_number || w_value != 0)
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100614 return -EDOM;
615 VDBG(fsg, "get max LUN\n");
Michal Nazarewicza41ae412009-10-28 16:57:20 +0100616 *(u8 *) req->buf = fsg->common->nluns - 1;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100617 return 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100618 }
619
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100620 VDBG(fsg,
621 "unknown class-specific control req "
622 "%02x.%02x v%04x i%04x l%u\n",
623 ctrl->bRequestType, ctrl->bRequest,
624 le16_to_cpu(ctrl->wValue), w_index, w_length);
625 return -EOPNOTSUPP;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100626}
627
628
629/*-------------------------------------------------------------------------*/
630
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100631/* All the following routines run in process context */
632
633
634/* Use this for bulk or interrupt transfers, not ep0 */
635static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
636 struct usb_request *req, int *pbusy,
637 enum fsg_buffer_state *state)
638{
639 int rc;
640
641 if (ep == fsg->bulk_in)
642 dump_msg(fsg, "bulk-in", req->buf, req->length);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100643
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100644 spin_lock_irq(&fsg->common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100645 *pbusy = 1;
646 *state = BUF_STATE_BUSY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100647 spin_unlock_irq(&fsg->common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100648 rc = usb_ep_queue(ep, req, GFP_KERNEL);
649 if (rc != 0) {
650 *pbusy = 0;
651 *state = BUF_STATE_EMPTY;
652
653 /* We can't do much more than wait for a reset */
654
655 /* Note: currently the net2280 driver fails zero-length
656 * submissions if DMA is enabled. */
657 if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
658 req->length == 0))
659 WARNING(fsg, "error in submission: %s --> %d\n",
660 ep->name, rc);
661 }
662}
663
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100664#define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \
665 if (fsg_is_set(common)) \
666 start_transfer((common)->fsg, (common)->fsg->ep_name, \
667 req, pbusy, state); \
668 else
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100669
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100670#define START_TRANSFER(common, ep_name, req, pbusy, state) \
671 START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
672
673
674
675static int sleep_thread(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100676{
677 int rc = 0;
678
679 /* Wait until a signal arrives or we are woken up */
680 for (;;) {
681 try_to_freeze();
682 set_current_state(TASK_INTERRUPTIBLE);
683 if (signal_pending(current)) {
684 rc = -EINTR;
685 break;
686 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100687 if (common->thread_wakeup_needed)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100688 break;
689 schedule();
690 }
691 __set_current_state(TASK_RUNNING);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100692 common->thread_wakeup_needed = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100693 return rc;
694}
695
696
697/*-------------------------------------------------------------------------*/
698
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100699static int do_read(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100700{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100701 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100702 u32 lba;
703 struct fsg_buffhd *bh;
704 int rc;
705 u32 amount_left;
706 loff_t file_offset, file_offset_tmp;
707 unsigned int amount;
708 unsigned int partial_page;
709 ssize_t nread;
710
711 /* Get the starting Logical Block Address and check that it's
712 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100713 if (common->cmnd[0] == SC_READ_6)
714 lba = get_unaligned_be24(&common->cmnd[1]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100715 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100716 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100717
718 /* We allow DPO (Disable Page Out = don't save data in the
719 * cache) and FUA (Force Unit Access = don't read from the
720 * cache), but we don't implement them. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100721 if ((common->cmnd[1] & ~0x18) != 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100722 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
723 return -EINVAL;
724 }
725 }
726 if (lba >= curlun->num_sectors) {
727 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
728 return -EINVAL;
729 }
730 file_offset = ((loff_t) lba) << 9;
731
732 /* Carry out the file reads */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100733 amount_left = common->data_size_from_cmnd;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100734 if (unlikely(amount_left == 0))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100735 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100736
737 for (;;) {
738
739 /* Figure out how much we need to read:
740 * Try to read the remaining amount.
741 * But don't read more than the buffer size.
742 * And don't try to read past the end of the file.
743 * Finally, if we're not at a page boundary, don't read past
744 * the next page.
745 * If this means reading 0 then we were asked to read past
746 * the end of file. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100747 amount = min(amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100748 amount = min((loff_t) amount,
749 curlun->file_length - file_offset);
750 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
751 if (partial_page > 0)
752 amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
753 partial_page);
754
755 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100756 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100757 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100758 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100759 if (rc)
760 return rc;
761 }
762
763 /* If we were asked to read past the end of file,
764 * end with an empty buffer. */
765 if (amount == 0) {
766 curlun->sense_data =
767 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
768 curlun->sense_data_info = file_offset >> 9;
769 curlun->info_valid = 1;
770 bh->inreq->length = 0;
771 bh->state = BUF_STATE_FULL;
772 break;
773 }
774
775 /* Perform the read */
776 file_offset_tmp = file_offset;
777 nread = vfs_read(curlun->filp,
778 (char __user *) bh->buf,
779 amount, &file_offset_tmp);
780 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
781 (unsigned long long) file_offset,
782 (int) nread);
783 if (signal_pending(current))
784 return -EINTR;
785
786 if (nread < 0) {
787 LDBG(curlun, "error in file read: %d\n",
788 (int) nread);
789 nread = 0;
790 } else if (nread < amount) {
791 LDBG(curlun, "partial file read: %d/%u\n",
792 (int) nread, amount);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100793 nread -= (nread & 511); /* Round down to a block */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100794 }
795 file_offset += nread;
796 amount_left -= nread;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100797 common->residue -= nread;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100798 bh->inreq->length = nread;
799 bh->state = BUF_STATE_FULL;
800
801 /* If an error occurred, report it and its position */
802 if (nread < amount) {
803 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
804 curlun->sense_data_info = file_offset >> 9;
805 curlun->info_valid = 1;
806 break;
807 }
808
809 if (amount_left == 0)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100810 break; /* No more left to read */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100811
812 /* Send this buffer and go read some more */
813 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100814 START_TRANSFER_OR(common, bulk_in, bh->inreq,
815 &bh->inreq_busy, &bh->state)
816 /* Don't know what to do if
817 * common->fsg is NULL */
818 return -EIO;
819 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100820 }
821
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100822 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100823}
824
825
826/*-------------------------------------------------------------------------*/
827
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100828static int do_write(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100829{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100830 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100831 u32 lba;
832 struct fsg_buffhd *bh;
833 int get_some_more;
834 u32 amount_left_to_req, amount_left_to_write;
835 loff_t usb_offset, file_offset, file_offset_tmp;
836 unsigned int amount;
837 unsigned int partial_page;
838 ssize_t nwritten;
839 int rc;
840
841 if (curlun->ro) {
842 curlun->sense_data = SS_WRITE_PROTECTED;
843 return -EINVAL;
844 }
845 spin_lock(&curlun->filp->f_lock);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100846 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100847 spin_unlock(&curlun->filp->f_lock);
848
849 /* Get the starting Logical Block Address and check that it's
850 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100851 if (common->cmnd[0] == SC_WRITE_6)
852 lba = get_unaligned_be24(&common->cmnd[1]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100853 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100854 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100855
856 /* We allow DPO (Disable Page Out = don't save data in the
857 * cache) and FUA (Force Unit Access = write directly to the
858 * medium). We don't implement DPO; we implement FUA by
859 * performing synchronous output. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100860 if (common->cmnd[1] & ~0x18) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100861 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
862 return -EINVAL;
863 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100864 if (common->cmnd[1] & 0x08) { /* FUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100865 spin_lock(&curlun->filp->f_lock);
866 curlun->filp->f_flags |= O_SYNC;
867 spin_unlock(&curlun->filp->f_lock);
868 }
869 }
870 if (lba >= curlun->num_sectors) {
871 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
872 return -EINVAL;
873 }
874
875 /* Carry out the file writes */
876 get_some_more = 1;
877 file_offset = usb_offset = ((loff_t) lba) << 9;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100878 amount_left_to_req = common->data_size_from_cmnd;
879 amount_left_to_write = common->data_size_from_cmnd;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100880
881 while (amount_left_to_write > 0) {
882
883 /* Queue a request for more data from the host */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100884 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100885 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
886
887 /* Figure out how much we want to get:
888 * Try to get the remaining amount.
889 * But don't get more than the buffer size.
890 * And don't try to go past the end of the file.
891 * If we're not at a page boundary,
892 * don't go past the next page.
893 * If this means getting 0, then we were asked
894 * to write past the end of file.
895 * Finally, round down to a block boundary. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100896 amount = min(amount_left_to_req, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100897 amount = min((loff_t) amount, curlun->file_length -
898 usb_offset);
899 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
900 if (partial_page > 0)
901 amount = min(amount,
902 (unsigned int) PAGE_CACHE_SIZE - partial_page);
903
904 if (amount == 0) {
905 get_some_more = 0;
906 curlun->sense_data =
907 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
908 curlun->sense_data_info = usb_offset >> 9;
909 curlun->info_valid = 1;
910 continue;
911 }
912 amount -= (amount & 511);
913 if (amount == 0) {
914
915 /* Why were we were asked to transfer a
916 * partial block? */
917 get_some_more = 0;
918 continue;
919 }
920
921 /* Get the next buffer */
922 usb_offset += amount;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100923 common->usb_amount_left -= amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100924 amount_left_to_req -= amount;
925 if (amount_left_to_req == 0)
926 get_some_more = 0;
927
928 /* amount is always divisible by 512, hence by
929 * the bulk-out maxpacket size */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100930 bh->outreq->length = amount;
931 bh->bulk_out_intended_length = amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100932 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100933 START_TRANSFER_OR(common, bulk_out, bh->outreq,
934 &bh->outreq_busy, &bh->state)
935 /* Don't know what to do if
936 * common->fsg is NULL */
937 return -EIO;
938 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100939 continue;
940 }
941
942 /* Write the received data to the backing file */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100943 bh = common->next_buffhd_to_drain;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100944 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100945 break; /* We stopped early */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100946 if (bh->state == BUF_STATE_FULL) {
947 smp_rmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100948 common->next_buffhd_to_drain = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100949 bh->state = BUF_STATE_EMPTY;
950
951 /* Did something go wrong with the transfer? */
952 if (bh->outreq->status != 0) {
953 curlun->sense_data = SS_COMMUNICATION_FAILURE;
954 curlun->sense_data_info = file_offset >> 9;
955 curlun->info_valid = 1;
956 break;
957 }
958
959 amount = bh->outreq->actual;
960 if (curlun->file_length - file_offset < amount) {
961 LERROR(curlun,
962 "write %u @ %llu beyond end %llu\n",
963 amount, (unsigned long long) file_offset,
964 (unsigned long long) curlun->file_length);
965 amount = curlun->file_length - file_offset;
966 }
967
968 /* Perform the write */
969 file_offset_tmp = file_offset;
970 nwritten = vfs_write(curlun->filp,
971 (char __user *) bh->buf,
972 amount, &file_offset_tmp);
973 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
974 (unsigned long long) file_offset,
975 (int) nwritten);
976 if (signal_pending(current))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100977 return -EINTR; /* Interrupted! */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100978
979 if (nwritten < 0) {
980 LDBG(curlun, "error in file write: %d\n",
981 (int) nwritten);
982 nwritten = 0;
983 } else if (nwritten < amount) {
984 LDBG(curlun, "partial file write: %d/%u\n",
985 (int) nwritten, amount);
986 nwritten -= (nwritten & 511);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100987 /* Round down to a block */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100988 }
989 file_offset += nwritten;
990 amount_left_to_write -= nwritten;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +0100991 common->residue -= nwritten;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +0100992
993 /* If an error occurred, report it and its position */
994 if (nwritten < amount) {
995 curlun->sense_data = SS_WRITE_ERROR;
996 curlun->sense_data_info = file_offset >> 9;
997 curlun->info_valid = 1;
998 break;
999 }
1000
1001 /* Did the host decide to stop early? */
1002 if (bh->outreq->actual != bh->outreq->length) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001003 common->short_packet_received = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001004 break;
1005 }
1006 continue;
1007 }
1008
1009 /* Wait for something to happen */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001010 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001011 if (rc)
1012 return rc;
1013 }
1014
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001015 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001016}
1017
1018
1019/*-------------------------------------------------------------------------*/
1020
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001021static int do_synchronize_cache(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001022{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001023 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001024 int rc;
1025
1026 /* We ignore the requested LBA and write out all file's
1027 * dirty data buffers. */
1028 rc = fsg_lun_fsync_sub(curlun);
1029 if (rc)
1030 curlun->sense_data = SS_WRITE_ERROR;
1031 return 0;
1032}
1033
1034
1035/*-------------------------------------------------------------------------*/
1036
1037static void invalidate_sub(struct fsg_lun *curlun)
1038{
1039 struct file *filp = curlun->filp;
1040 struct inode *inode = filp->f_path.dentry->d_inode;
1041 unsigned long rc;
1042
1043 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
1044 VLDBG(curlun, "invalidate_inode_pages -> %ld\n", rc);
1045}
1046
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001047static int do_verify(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 u32 lba;
1051 u32 verification_length;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001052 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001053 loff_t file_offset, file_offset_tmp;
1054 u32 amount_left;
1055 unsigned int amount;
1056 ssize_t nread;
1057
1058 /* Get the starting Logical Block Address and check that it's
1059 * not too big */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001060 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001061 if (lba >= curlun->num_sectors) {
1062 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1063 return -EINVAL;
1064 }
1065
1066 /* We allow DPO (Disable Page Out = don't save data in the
1067 * cache) but we don't implement it. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001068 if (common->cmnd[1] & ~0x10) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001069 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1070 return -EINVAL;
1071 }
1072
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001073 verification_length = get_unaligned_be16(&common->cmnd[7]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001074 if (unlikely(verification_length == 0))
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001075 return -EIO; /* No default reply */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001076
1077 /* Prepare to carry out the file verify */
1078 amount_left = verification_length << 9;
1079 file_offset = ((loff_t) lba) << 9;
1080
1081 /* Write out all the dirty buffers before invalidating them */
1082 fsg_lun_fsync_sub(curlun);
1083 if (signal_pending(current))
1084 return -EINTR;
1085
1086 invalidate_sub(curlun);
1087 if (signal_pending(current))
1088 return -EINTR;
1089
1090 /* Just try to read the requested blocks */
1091 while (amount_left > 0) {
1092
1093 /* Figure out how much we need to read:
1094 * Try to read the remaining amount, but not more than
1095 * the buffer size.
1096 * And don't try to read past the end of the file.
1097 * If this means reading 0 then we were asked to read
1098 * past the end of file. */
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001099 amount = min(amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001100 amount = min((loff_t) amount,
1101 curlun->file_length - file_offset);
1102 if (amount == 0) {
1103 curlun->sense_data =
1104 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1105 curlun->sense_data_info = file_offset >> 9;
1106 curlun->info_valid = 1;
1107 break;
1108 }
1109
1110 /* Perform the read */
1111 file_offset_tmp = file_offset;
1112 nread = vfs_read(curlun->filp,
1113 (char __user *) bh->buf,
1114 amount, &file_offset_tmp);
1115 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1116 (unsigned long long) file_offset,
1117 (int) nread);
1118 if (signal_pending(current))
1119 return -EINTR;
1120
1121 if (nread < 0) {
1122 LDBG(curlun, "error in file verify: %d\n",
1123 (int) nread);
1124 nread = 0;
1125 } else if (nread < amount) {
1126 LDBG(curlun, "partial file verify: %d/%u\n",
1127 (int) nread, amount);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001128 nread -= (nread & 511); /* Round down to a sector */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001129 }
1130 if (nread == 0) {
1131 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1132 curlun->sense_data_info = file_offset >> 9;
1133 curlun->info_valid = 1;
1134 break;
1135 }
1136 file_offset += nread;
1137 amount_left -= nread;
1138 }
1139 return 0;
1140}
1141
1142
1143/*-------------------------------------------------------------------------*/
1144
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001145static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001146{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001147 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001148 u8 *buf = (u8 *) bh->buf;
1149
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001150 if (!curlun) { /* Unsupported LUNs are okay */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001151 common->bad_lun_okay = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001152 memset(buf, 0, 36);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001153 buf[0] = 0x7f; /* Unsupported, no device-type */
1154 buf[4] = 31; /* Additional length */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001155 return 36;
1156 }
1157
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001158 buf[0] = curlun->cdrom ? TYPE_CDROM : TYPE_DISK;
1159 buf[1] = curlun->removable ? 0x80 : 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001160 buf[2] = 2; /* ANSI SCSI level 2 */
1161 buf[3] = 2; /* SCSI-2 INQUIRY data format */
1162 buf[4] = 31; /* Additional length */
1163 buf[5] = 0; /* No special options */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001164 buf[6] = 0;
1165 buf[7] = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001166 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001167 return 36;
1168}
1169
1170
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001171static int do_request_sense(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 u32 sd, sdinfo;
1176 int valid;
1177
1178 /*
1179 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1180 *
1181 * If a REQUEST SENSE command is received from an initiator
1182 * with a pending unit attention condition (before the target
1183 * generates the contingent allegiance condition), then the
1184 * target shall either:
1185 * a) report any pending sense data and preserve the unit
1186 * attention condition on the logical unit, or,
1187 * b) report the unit attention condition, may discard any
1188 * pending sense data, and clear the unit attention
1189 * condition on the logical unit for that initiator.
1190 *
1191 * FSG normally uses option a); enable this code to use option b).
1192 */
1193#if 0
1194 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1195 curlun->sense_data = curlun->unit_attention_data;
1196 curlun->unit_attention_data = SS_NO_SENSE;
1197 }
1198#endif
1199
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001200 if (!curlun) { /* Unsupported LUNs are okay */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001201 common->bad_lun_okay = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001202 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1203 sdinfo = 0;
1204 valid = 0;
1205 } else {
1206 sd = curlun->sense_data;
1207 sdinfo = curlun->sense_data_info;
1208 valid = curlun->info_valid << 7;
1209 curlun->sense_data = SS_NO_SENSE;
1210 curlun->sense_data_info = 0;
1211 curlun->info_valid = 0;
1212 }
1213
1214 memset(buf, 0, 18);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001215 buf[0] = valid | 0x70; /* Valid, current error */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001216 buf[2] = SK(sd);
1217 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001218 buf[7] = 18 - 8; /* Additional sense length */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001219 buf[12] = ASC(sd);
1220 buf[13] = ASCQ(sd);
1221 return 18;
1222}
1223
1224
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001225static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001226{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001227 struct fsg_lun *curlun = common->curlun;
1228 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1229 int pmi = common->cmnd[8];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001230 u8 *buf = (u8 *) bh->buf;
1231
1232 /* Check the PMI and LBA fields */
1233 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1234 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1235 return -EINVAL;
1236 }
1237
1238 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1239 /* Max logical block */
1240 put_unaligned_be32(512, &buf[4]); /* Block length */
1241 return 8;
1242}
1243
1244
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001245static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001246{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001247 struct fsg_lun *curlun = common->curlun;
1248 int msf = common->cmnd[1] & 0x02;
1249 u32 lba = get_unaligned_be32(&common->cmnd[2]);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001250 u8 *buf = (u8 *) bh->buf;
1251
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001252 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001253 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1254 return -EINVAL;
1255 }
1256 if (lba >= curlun->num_sectors) {
1257 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1258 return -EINVAL;
1259 }
1260
1261 memset(buf, 0, 8);
1262 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1263 store_cdrom_address(&buf[4], msf, lba);
1264 return 8;
1265}
1266
1267
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001268static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001269{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001270 struct fsg_lun *curlun = common->curlun;
1271 int msf = common->cmnd[1] & 0x02;
1272 int start_track = common->cmnd[6];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001273 u8 *buf = (u8 *) bh->buf;
1274
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001275 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001276 start_track > 1) {
1277 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1278 return -EINVAL;
1279 }
1280
1281 memset(buf, 0, 20);
1282 buf[1] = (20-2); /* TOC data length */
1283 buf[2] = 1; /* First track number */
1284 buf[3] = 1; /* Last track number */
1285 buf[5] = 0x16; /* Data track, copying allowed */
1286 buf[6] = 0x01; /* Only track is number 1 */
1287 store_cdrom_address(&buf[8], msf, 0);
1288
1289 buf[13] = 0x16; /* Lead-out track is data */
1290 buf[14] = 0xAA; /* Lead-out track number */
1291 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1292 return 20;
1293}
1294
1295
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001296static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001297{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001298 struct fsg_lun *curlun = common->curlun;
1299 int mscmnd = common->cmnd[0];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001300 u8 *buf = (u8 *) bh->buf;
1301 u8 *buf0 = buf;
1302 int pc, page_code;
1303 int changeable_values, all_pages;
1304 int valid_page = 0;
1305 int len, limit;
1306
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001307 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001308 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1309 return -EINVAL;
1310 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001311 pc = common->cmnd[2] >> 6;
1312 page_code = common->cmnd[2] & 0x3f;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001313 if (pc == 3) {
1314 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1315 return -EINVAL;
1316 }
1317 changeable_values = (pc == 1);
1318 all_pages = (page_code == 0x3f);
1319
1320 /* Write the mode parameter header. Fixed values are: default
1321 * medium type, no cache control (DPOFUA), and no block descriptors.
1322 * The only variable value is the WriteProtect bit. We will fill in
1323 * the mode data length later. */
1324 memset(buf, 0, 8);
1325 if (mscmnd == SC_MODE_SENSE_6) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001326 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001327 buf += 4;
1328 limit = 255;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001329 } else { /* SC_MODE_SENSE_10 */
1330 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001331 buf += 8;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001332 limit = 65535; /* Should really be FSG_BUFLEN */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001333 }
1334
1335 /* No block descriptors */
1336
1337 /* The mode pages, in numerical order. The only page we support
1338 * is the Caching page. */
1339 if (page_code == 0x08 || all_pages) {
1340 valid_page = 1;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001341 buf[0] = 0x08; /* Page code */
1342 buf[1] = 10; /* Page length */
1343 memset(buf+2, 0, 10); /* None of the fields are changeable */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001344
1345 if (!changeable_values) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001346 buf[2] = 0x04; /* Write cache enable, */
1347 /* Read cache not disabled */
1348 /* No cache retention priorities */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001349 put_unaligned_be16(0xffff, &buf[4]);
1350 /* Don't disable prefetch */
1351 /* Minimum prefetch = 0 */
1352 put_unaligned_be16(0xffff, &buf[8]);
1353 /* Maximum prefetch */
1354 put_unaligned_be16(0xffff, &buf[10]);
1355 /* Maximum prefetch ceiling */
1356 }
1357 buf += 12;
1358 }
1359
1360 /* Check that a valid page was requested and the mode data length
1361 * isn't too long. */
1362 len = buf - buf0;
1363 if (!valid_page || len > limit) {
1364 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1365 return -EINVAL;
1366 }
1367
1368 /* Store the mode data length */
1369 if (mscmnd == SC_MODE_SENSE_6)
1370 buf0[0] = len - 1;
1371 else
1372 put_unaligned_be16(len - 2, buf0);
1373 return len;
1374}
1375
1376
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001377static int do_start_stop(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001378{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001379 if (!common->curlun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001380 return -EINVAL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001381 } else if (!common->curlun->removable) {
1382 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001383 return -EINVAL;
1384 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001385 return 0;
1386}
1387
1388
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001389static int do_prevent_allow(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001390{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001391 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001392 int prevent;
1393
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001394 if (!common->curlun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01001395 return -EINVAL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001396 } else if (!common->curlun->removable) {
1397 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001398 return -EINVAL;
1399 }
1400
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001401 prevent = common->cmnd[4] & 0x01;
1402 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001403 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1404 return -EINVAL;
1405 }
1406
1407 if (curlun->prevent_medium_removal && !prevent)
1408 fsg_lun_fsync_sub(curlun);
1409 curlun->prevent_medium_removal = prevent;
1410 return 0;
1411}
1412
1413
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001414static int do_read_format_capacities(struct fsg_common *common,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001415 struct fsg_buffhd *bh)
1416{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001417 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001418 u8 *buf = (u8 *) bh->buf;
1419
1420 buf[0] = buf[1] = buf[2] = 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001421 buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001422 buf += 4;
1423
1424 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1425 /* Number of blocks */
1426 put_unaligned_be32(512, &buf[4]); /* Block length */
1427 buf[4] = 0x02; /* Current capacity */
1428 return 12;
1429}
1430
1431
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001432static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001433{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001434 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001435
1436 /* We don't support MODE SELECT */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001437 if (curlun)
1438 curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001439 return -EINVAL;
1440}
1441
1442
1443/*-------------------------------------------------------------------------*/
1444
1445static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1446{
1447 int rc;
1448
1449 rc = fsg_set_halt(fsg, fsg->bulk_in);
1450 if (rc == -EAGAIN)
1451 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1452 while (rc != 0) {
1453 if (rc != -EAGAIN) {
1454 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1455 rc = 0;
1456 break;
1457 }
1458
1459 /* Wait for a short time and then try again */
1460 if (msleep_interruptible(100) != 0)
1461 return -EINTR;
1462 rc = usb_ep_set_halt(fsg->bulk_in);
1463 }
1464 return rc;
1465}
1466
1467static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1468{
1469 int rc;
1470
1471 DBG(fsg, "bulk-in set wedge\n");
1472 rc = usb_ep_set_wedge(fsg->bulk_in);
1473 if (rc == -EAGAIN)
1474 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1475 while (rc != 0) {
1476 if (rc != -EAGAIN) {
1477 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1478 rc = 0;
1479 break;
1480 }
1481
1482 /* Wait for a short time and then try again */
1483 if (msleep_interruptible(100) != 0)
1484 return -EINTR;
1485 rc = usb_ep_set_wedge(fsg->bulk_in);
1486 }
1487 return rc;
1488}
1489
1490static int pad_with_zeros(struct fsg_dev *fsg)
1491{
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001492 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001493 u32 nkeep = bh->inreq->length;
1494 u32 nsend;
1495 int rc;
1496
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001497 bh->state = BUF_STATE_EMPTY; /* For the first iteration */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001498 fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1499 while (fsg->common->usb_amount_left > 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001500
1501 /* Wait for the next buffer to be free */
1502 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001503 rc = sleep_thread(fsg->common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001504 if (rc)
1505 return rc;
1506 }
1507
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001508 nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001509 memset(bh->buf + nkeep, 0, nsend - nkeep);
1510 bh->inreq->length = nsend;
1511 bh->inreq->zero = 0;
1512 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1513 &bh->inreq_busy, &bh->state);
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001514 bh = fsg->common->next_buffhd_to_fill = bh->next;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001515 fsg->common->usb_amount_left -= nsend;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001516 nkeep = 0;
1517 }
1518 return 0;
1519}
1520
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001521static int throw_away_data(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001522{
1523 struct fsg_buffhd *bh;
1524 u32 amount;
1525 int rc;
1526
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001527 for (bh = common->next_buffhd_to_drain;
1528 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1529 bh = common->next_buffhd_to_drain) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001530
1531 /* Throw away the data in a filled buffer */
1532 if (bh->state == BUF_STATE_FULL) {
1533 smp_rmb();
1534 bh->state = BUF_STATE_EMPTY;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001535 common->next_buffhd_to_drain = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001536
1537 /* A short packet or an error ends everything */
1538 if (bh->outreq->actual != bh->outreq->length ||
1539 bh->outreq->status != 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001540 raise_exception(common,
1541 FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001542 return -EINTR;
1543 }
1544 continue;
1545 }
1546
1547 /* Try to submit another request if we need one */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001548 bh = common->next_buffhd_to_fill;
1549 if (bh->state == BUF_STATE_EMPTY
1550 && common->usb_amount_left > 0) {
1551 amount = min(common->usb_amount_left, FSG_BUFLEN);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001552
1553 /* amount is always divisible by 512, hence by
1554 * the bulk-out maxpacket size */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001555 bh->outreq->length = amount;
1556 bh->bulk_out_intended_length = amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001557 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001558 START_TRANSFER_OR(common, bulk_out, bh->outreq,
1559 &bh->outreq_busy, &bh->state)
1560 /* Don't know what to do if
1561 * common->fsg is NULL */
1562 return -EIO;
1563 common->next_buffhd_to_fill = bh->next;
1564 common->usb_amount_left -= amount;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001565 continue;
1566 }
1567
1568 /* Otherwise wait for something to happen */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001569 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001570 if (rc)
1571 return rc;
1572 }
1573 return 0;
1574}
1575
1576
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001577static int finish_reply(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001578{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001579 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001580 int rc = 0;
1581
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001582 switch (common->data_dir) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001583 case DATA_DIR_NONE:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001584 break; /* Nothing to send */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001585
1586 /* If we don't know whether the host wants to read or write,
1587 * this must be CB or CBI with an unknown command. We mustn't
1588 * try to send or receive any data. So stall both bulk pipes
1589 * if we can and wait for a reset. */
1590 case DATA_DIR_UNKNOWN:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001591 if (!common->can_stall) {
1592 /* Nothing */
1593 } else if (fsg_is_set(common)) {
1594 fsg_set_halt(common->fsg, common->fsg->bulk_out);
1595 rc = halt_bulk_in_endpoint(common->fsg);
1596 } else {
1597 /* Don't know what to do if common->fsg is NULL */
1598 rc = -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001599 }
1600 break;
1601
1602 /* All but the last buffer of data must have already been sent */
1603 case DATA_DIR_TO_HOST:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001604 if (common->data_size == 0) {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001605 /* Nothing to send */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001606
1607 /* If there's no residue, simply send the last buffer */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001608 } else if (common->residue == 0) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001609 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001610 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1611 &bh->inreq_busy, &bh->state)
1612 return -EIO;
1613 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001614
1615 /* For Bulk-only, if we're allowed to stall then send the
1616 * short packet and halt the bulk-in endpoint. If we can't
1617 * stall, pad out the remaining data with 0's. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001618 } else if (common->can_stall) {
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001619 bh->inreq->zero = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001620 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1621 &bh->inreq_busy, &bh->state)
1622 /* Don't know what to do if
1623 * common->fsg is NULL */
1624 rc = -EIO;
1625 common->next_buffhd_to_fill = bh->next;
1626 if (common->fsg)
1627 rc = halt_bulk_in_endpoint(common->fsg);
1628 } else if (fsg_is_set(common)) {
1629 rc = pad_with_zeros(common->fsg);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001630 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001631 /* Don't know what to do if common->fsg is NULL */
1632 rc = -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001633 }
1634 break;
1635
1636 /* We have processed all we want from the data the host has sent.
1637 * There may still be outstanding bulk-out requests. */
1638 case DATA_DIR_FROM_HOST:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001639 if (common->residue == 0) {
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001640 /* Nothing to receive */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001641
1642 /* Did the host stop sending unexpectedly early? */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001643 } else if (common->short_packet_received) {
1644 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001645 rc = -EINTR;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001646
1647 /* We haven't processed all the incoming data. Even though
1648 * we may be allowed to stall, doing so would cause a race.
1649 * The controller may already have ACK'ed all the remaining
1650 * bulk-out packets, in which case the host wouldn't see a
1651 * STALL. Not realizing the endpoint was halted, it wouldn't
1652 * clear the halt -- leading to problems later on. */
1653#if 0
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001654 } else if (common->can_stall) {
1655 if (fsg_is_set(common))
1656 fsg_set_halt(common->fsg,
1657 common->fsg->bulk_out);
1658 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001659 rc = -EINTR;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001660#endif
1661
1662 /* We can't stall. Read in the excess data and throw it
1663 * all away. */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001664 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001665 rc = throw_away_data(common);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001666 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001667 break;
1668 }
1669 return rc;
1670}
1671
1672
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001673static int send_status(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001674{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001675 struct fsg_lun *curlun = common->curlun;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001676 struct fsg_buffhd *bh;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001677 struct bulk_cs_wrap *csw;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001678 int rc;
1679 u8 status = USB_STATUS_PASS;
1680 u32 sd, sdinfo = 0;
1681
1682 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001683 bh = common->next_buffhd_to_fill;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001684 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001685 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001686 if (rc)
1687 return rc;
1688 }
1689
1690 if (curlun) {
1691 sd = curlun->sense_data;
1692 sdinfo = curlun->sense_data_info;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001693 } else if (common->bad_lun_okay)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001694 sd = SS_NO_SENSE;
1695 else
1696 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1697
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001698 if (common->phase_error) {
1699 DBG(common, "sending phase-error status\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001700 status = USB_STATUS_PHASE_ERROR;
1701 sd = SS_INVALID_COMMAND;
1702 } else if (sd != SS_NO_SENSE) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001703 DBG(common, "sending command-failure status\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001704 status = USB_STATUS_FAIL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001705 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001706 " info x%x\n",
1707 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1708 }
1709
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001710 /* Store and send the Bulk-only CSW */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001711 csw = (void *)bh->buf;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001712
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001713 csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001714 csw->Tag = common->tag;
1715 csw->Residue = cpu_to_le32(common->residue);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001716 csw->Status = status;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001717
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01001718 bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1719 bh->inreq->zero = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001720 START_TRANSFER_OR(common, bulk_in, bh->inreq,
1721 &bh->inreq_busy, &bh->state)
1722 /* Don't know what to do if common->fsg is NULL */
1723 return -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001724
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001725 common->next_buffhd_to_fill = bh->next;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001726 return 0;
1727}
1728
1729
1730/*-------------------------------------------------------------------------*/
1731
1732/* Check whether the command is properly formed and whether its data size
1733 * and direction agree with the values we already have. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001734static int check_command(struct fsg_common *common, int cmnd_size,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001735 enum data_direction data_dir, unsigned int mask,
1736 int needs_medium, const char *name)
1737{
1738 int i;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001739 int lun = common->cmnd[1] >> 5;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001740 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1741 char hdlen[20];
1742 struct fsg_lun *curlun;
1743
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001744 hdlen[0] = 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001745 if (common->data_dir != DATA_DIR_UNKNOWN)
1746 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1747 common->data_size);
1748 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001749 name, cmnd_size, dirletter[(int) data_dir],
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001750 common->data_size_from_cmnd, common->cmnd_size, hdlen);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001751
1752 /* We can't reply at all until we know the correct data direction
1753 * and size. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001754 if (common->data_size_from_cmnd == 0)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001755 data_dir = DATA_DIR_NONE;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001756 if (common->data_size < common->data_size_from_cmnd) {
1757 /* Host data size < Device data size is a phase error.
1758 * Carry out the command, but only transfer as much as
1759 * we are allowed. */
1760 common->data_size_from_cmnd = common->data_size;
1761 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001762 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001763 common->residue = common->data_size;
1764 common->usb_amount_left = common->data_size;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001765
1766 /* Conflicting data directions is a phase error */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001767 if (common->data_dir != data_dir
1768 && common->data_size_from_cmnd > 0) {
1769 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001770 return -EINVAL;
1771 }
1772
1773 /* Verify the length of the command itself */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001774 if (cmnd_size != common->cmnd_size) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001775
1776 /* Special case workaround: There are plenty of buggy SCSI
1777 * implementations. Many have issues with cbw->Length
1778 * field passing a wrong command size. For those cases we
1779 * always try to work around the problem by using the length
1780 * sent by the host side provided it is at least as large
1781 * as the correct command length.
1782 * Examples of such cases would be MS-Windows, which issues
1783 * REQUEST SENSE with cbw->Length == 12 where it should
1784 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1785 * REQUEST SENSE with cbw->Length == 10 where it should
1786 * be 6 as well.
1787 */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001788 if (cmnd_size <= common->cmnd_size) {
1789 DBG(common, "%s is buggy! Expected length %d "
Michal Nazarewicza41ae412009-10-28 16:57:20 +01001790 "but we got %d\n", name,
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001791 cmnd_size, common->cmnd_size);
1792 cmnd_size = common->cmnd_size;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001793 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001794 common->phase_error = 1;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001795 return -EINVAL;
1796 }
1797 }
1798
1799 /* Check that the LUN values are consistent */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001800 if (common->lun != lun)
1801 DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
1802 common->lun, lun);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001803
1804 /* Check the LUN */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001805 if (common->lun >= 0 && common->lun < common->nluns) {
1806 curlun = &common->luns[common->lun];
1807 common->curlun = curlun;
1808 if (common->cmnd[0] != SC_REQUEST_SENSE) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001809 curlun->sense_data = SS_NO_SENSE;
1810 curlun->sense_data_info = 0;
1811 curlun->info_valid = 0;
1812 }
1813 } else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001814 common->curlun = NULL;
1815 curlun = NULL;
1816 common->bad_lun_okay = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001817
1818 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
1819 * to use unsupported LUNs; all others may not. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001820 if (common->cmnd[0] != SC_INQUIRY &&
1821 common->cmnd[0] != SC_REQUEST_SENSE) {
1822 DBG(common, "unsupported LUN %d\n", common->lun);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001823 return -EINVAL;
1824 }
1825 }
1826
1827 /* If a unit attention condition exists, only INQUIRY and
1828 * REQUEST SENSE commands are allowed; anything else must fail. */
1829 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001830 common->cmnd[0] != SC_INQUIRY &&
1831 common->cmnd[0] != SC_REQUEST_SENSE) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001832 curlun->sense_data = curlun->unit_attention_data;
1833 curlun->unit_attention_data = SS_NO_SENSE;
1834 return -EINVAL;
1835 }
1836
1837 /* Check that only command bytes listed in the mask are non-zero */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001838 common->cmnd[1] &= 0x1f; /* Mask away the LUN */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001839 for (i = 1; i < cmnd_size; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001840 if (common->cmnd[i] && !(mask & (1 << i))) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001841 if (curlun)
1842 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1843 return -EINVAL;
1844 }
1845 }
1846
1847 /* If the medium isn't mounted and the command needs to access
1848 * it, return an error. */
1849 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
1850 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1851 return -EINVAL;
1852 }
1853
1854 return 0;
1855}
1856
1857
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001858static int do_scsi_command(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001859{
1860 struct fsg_buffhd *bh;
1861 int rc;
1862 int reply = -EINVAL;
1863 int i;
1864 static char unknown[16];
1865
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001866 dump_cdb(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001867
1868 /* Wait for the next buffer to become available for data or status */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001869 bh = common->next_buffhd_to_fill;
1870 common->next_buffhd_to_drain = bh;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001871 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001872 rc = sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001873 if (rc)
1874 return rc;
1875 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001876 common->phase_error = 0;
1877 common->short_packet_received = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001878
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001879 down_read(&common->filesem); /* We're using the backing file */
1880 switch (common->cmnd[0]) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001881
1882 case SC_INQUIRY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001883 common->data_size_from_cmnd = common->cmnd[4];
1884 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001885 (1<<4), 0,
1886 "INQUIRY");
1887 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001888 reply = do_inquiry(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001889 break;
1890
1891 case SC_MODE_SELECT_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001892 common->data_size_from_cmnd = common->cmnd[4];
1893 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001894 (1<<1) | (1<<4), 0,
1895 "MODE SELECT(6)");
1896 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001897 reply = do_mode_select(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001898 break;
1899
1900 case SC_MODE_SELECT_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001901 common->data_size_from_cmnd =
1902 get_unaligned_be16(&common->cmnd[7]);
1903 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001904 (1<<1) | (3<<7), 0,
1905 "MODE SELECT(10)");
1906 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001907 reply = do_mode_select(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001908 break;
1909
1910 case SC_MODE_SENSE_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001911 common->data_size_from_cmnd = common->cmnd[4];
1912 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001913 (1<<1) | (1<<2) | (1<<4), 0,
1914 "MODE SENSE(6)");
1915 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001916 reply = do_mode_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001917 break;
1918
1919 case SC_MODE_SENSE_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001920 common->data_size_from_cmnd =
1921 get_unaligned_be16(&common->cmnd[7]);
1922 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001923 (1<<1) | (1<<2) | (3<<7), 0,
1924 "MODE SENSE(10)");
1925 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001926 reply = do_mode_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001927 break;
1928
1929 case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001930 common->data_size_from_cmnd = 0;
1931 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001932 (1<<4), 0,
1933 "PREVENT-ALLOW MEDIUM REMOVAL");
1934 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001935 reply = do_prevent_allow(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001936 break;
1937
1938 case SC_READ_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001939 i = common->cmnd[4];
1940 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1941 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001942 (7<<1) | (1<<4), 1,
1943 "READ(6)");
1944 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001945 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001946 break;
1947
1948 case SC_READ_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001949 common->data_size_from_cmnd =
1950 get_unaligned_be16(&common->cmnd[7]) << 9;
1951 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001952 (1<<1) | (0xf<<2) | (3<<7), 1,
1953 "READ(10)");
1954 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001955 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001956 break;
1957
1958 case SC_READ_12:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001959 common->data_size_from_cmnd =
1960 get_unaligned_be32(&common->cmnd[6]) << 9;
1961 reply = check_command(common, 12, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001962 (1<<1) | (0xf<<2) | (0xf<<6), 1,
1963 "READ(12)");
1964 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001965 reply = do_read(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001966 break;
1967
1968 case SC_READ_CAPACITY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001969 common->data_size_from_cmnd = 8;
1970 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001971 (0xf<<2) | (1<<8), 1,
1972 "READ CAPACITY");
1973 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001974 reply = do_read_capacity(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001975 break;
1976
1977 case SC_READ_HEADER:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001978 if (!common->curlun || !common->curlun->cdrom)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001979 goto unknown_cmnd;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001980 common->data_size_from_cmnd =
1981 get_unaligned_be16(&common->cmnd[7]);
1982 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001983 (3<<7) | (0x1f<<1), 1,
1984 "READ HEADER");
1985 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001986 reply = do_read_header(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001987 break;
1988
1989 case SC_READ_TOC:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001990 if (!common->curlun || !common->curlun->cdrom)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001991 goto unknown_cmnd;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001992 common->data_size_from_cmnd =
1993 get_unaligned_be16(&common->cmnd[7]);
1994 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01001995 (7<<6) | (1<<1), 1,
1996 "READ TOC");
1997 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01001998 reply = do_read_toc(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01001999 break;
2000
2001 case SC_READ_FORMAT_CAPACITIES:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002002 common->data_size_from_cmnd =
2003 get_unaligned_be16(&common->cmnd[7]);
2004 reply = check_command(common, 10, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002005 (3<<7), 1,
2006 "READ FORMAT CAPACITIES");
2007 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002008 reply = do_read_format_capacities(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002009 break;
2010
2011 case SC_REQUEST_SENSE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002012 common->data_size_from_cmnd = common->cmnd[4];
2013 reply = check_command(common, 6, DATA_DIR_TO_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002014 (1<<4), 0,
2015 "REQUEST SENSE");
2016 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002017 reply = do_request_sense(common, bh);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002018 break;
2019
2020 case SC_START_STOP_UNIT:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002021 common->data_size_from_cmnd = 0;
2022 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002023 (1<<1) | (1<<4), 0,
2024 "START-STOP UNIT");
2025 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002026 reply = do_start_stop(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002027 break;
2028
2029 case SC_SYNCHRONIZE_CACHE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002030 common->data_size_from_cmnd = 0;
2031 reply = check_command(common, 10, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002032 (0xf<<2) | (3<<7), 1,
2033 "SYNCHRONIZE CACHE");
2034 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002035 reply = do_synchronize_cache(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002036 break;
2037
2038 case SC_TEST_UNIT_READY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002039 common->data_size_from_cmnd = 0;
2040 reply = check_command(common, 6, DATA_DIR_NONE,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002041 0, 1,
2042 "TEST UNIT READY");
2043 break;
2044
2045 /* Although optional, this command is used by MS-Windows. We
2046 * support a minimal version: BytChk must be 0. */
2047 case SC_VERIFY:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002048 common->data_size_from_cmnd = 0;
2049 reply = check_command(common, 10, DATA_DIR_NONE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002050 (1<<1) | (0xf<<2) | (3<<7), 1,
2051 "VERIFY");
2052 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002053 reply = do_verify(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002054 break;
2055
2056 case SC_WRITE_6:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002057 i = common->cmnd[4];
2058 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2059 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002060 (7<<1) | (1<<4), 1,
2061 "WRITE(6)");
2062 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002063 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002064 break;
2065
2066 case SC_WRITE_10:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002067 common->data_size_from_cmnd =
2068 get_unaligned_be16(&common->cmnd[7]) << 9;
2069 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002070 (1<<1) | (0xf<<2) | (3<<7), 1,
2071 "WRITE(10)");
2072 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002073 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002074 break;
2075
2076 case SC_WRITE_12:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002077 common->data_size_from_cmnd =
2078 get_unaligned_be32(&common->cmnd[6]) << 9;
2079 reply = check_command(common, 12, DATA_DIR_FROM_HOST,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002080 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2081 "WRITE(12)");
2082 if (reply == 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002083 reply = do_write(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002084 break;
2085
2086 /* Some mandatory commands that we recognize but don't implement.
2087 * They don't mean much in this setting. It's left as an exercise
2088 * for anyone interested to implement RESERVE and RELEASE in terms
2089 * of Posix locks. */
2090 case SC_FORMAT_UNIT:
2091 case SC_RELEASE:
2092 case SC_RESERVE:
2093 case SC_SEND_DIAGNOSTIC:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002094 /* Fall through */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002095
2096 default:
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002097unknown_cmnd:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002098 common->data_size_from_cmnd = 0;
2099 sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2100 reply = check_command(common, common->cmnd_size,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002101 DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2102 if (reply == 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002103 common->curlun->sense_data = SS_INVALID_COMMAND;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002104 reply = -EINVAL;
2105 }
2106 break;
2107 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002108 up_read(&common->filesem);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002109
2110 if (reply == -EINTR || signal_pending(current))
2111 return -EINTR;
2112
2113 /* Set up the single reply buffer for finish_reply() */
2114 if (reply == -EINVAL)
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002115 reply = 0; /* Error reply length */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002116 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2117 reply = min((u32) reply, common->data_size_from_cmnd);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002118 bh->inreq->length = reply;
2119 bh->state = BUF_STATE_FULL;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002120 common->residue -= reply;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002121 } /* Otherwise it's already set */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002122
2123 return 0;
2124}
2125
2126
2127/*-------------------------------------------------------------------------*/
2128
2129static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2130{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002131 struct usb_request *req = bh->outreq;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002132 struct fsg_bulk_cb_wrap *cbw = req->buf;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002133 struct fsg_common *common = fsg->common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002134
2135 /* Was this a real packet? Should it be ignored? */
2136 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2137 return -EINVAL;
2138
2139 /* Is the CBW valid? */
2140 if (req->actual != USB_BULK_CB_WRAP_LEN ||
2141 cbw->Signature != cpu_to_le32(
2142 USB_BULK_CB_SIG)) {
2143 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2144 req->actual,
2145 le32_to_cpu(cbw->Signature));
2146
2147 /* The Bulk-only spec says we MUST stall the IN endpoint
2148 * (6.6.1), so it's unavoidable. It also says we must
2149 * retain this state until the next reset, but there's
2150 * no way to tell the controller driver it should ignore
2151 * Clear-Feature(HALT) requests.
2152 *
2153 * We aren't required to halt the OUT endpoint; instead
2154 * we can simply accept and discard any data received
2155 * until the next reset. */
2156 wedge_bulk_in_endpoint(fsg);
2157 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2158 return -EINVAL;
2159 }
2160
2161 /* Is the CBW meaningful? */
2162 if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2163 cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2164 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2165 "cmdlen %u\n",
2166 cbw->Lun, cbw->Flags, cbw->Length);
2167
2168 /* We can do anything we want here, so let's stall the
2169 * bulk pipes if we are allowed to. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002170 if (common->can_stall) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002171 fsg_set_halt(fsg, fsg->bulk_out);
2172 halt_bulk_in_endpoint(fsg);
2173 }
2174 return -EINVAL;
2175 }
2176
2177 /* Save the command for later */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002178 common->cmnd_size = cbw->Length;
2179 memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002180 if (cbw->Flags & USB_BULK_IN_FLAG)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002181 common->data_dir = DATA_DIR_TO_HOST;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002182 else
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002183 common->data_dir = DATA_DIR_FROM_HOST;
2184 common->data_size = le32_to_cpu(cbw->DataTransferLength);
2185 if (common->data_size == 0)
2186 common->data_dir = DATA_DIR_NONE;
2187 common->lun = cbw->Lun;
2188 common->tag = cbw->Tag;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002189 return 0;
2190}
2191
2192
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002193static int get_next_command(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002194{
2195 struct fsg_buffhd *bh;
2196 int rc = 0;
2197
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002198 /* Wait for the next buffer to become available */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002199 bh = common->next_buffhd_to_fill;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002200 while (bh->state != BUF_STATE_EMPTY) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002201 rc = sleep_thread(common);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002202 if (rc)
2203 return rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002204 }
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002205
2206 /* Queue a request to read a Bulk-only CBW */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002207 set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002208 bh->outreq->short_not_ok = 1;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002209 START_TRANSFER_OR(common, bulk_out, bh->outreq,
2210 &bh->outreq_busy, &bh->state)
2211 /* Don't know what to do if common->fsg is NULL */
2212 return -EIO;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002213
2214 /* We will drain the buffer in software, which means we
2215 * can reuse it for the next filling. No need to advance
2216 * next_buffhd_to_fill. */
2217
2218 /* Wait for the CBW to arrive */
2219 while (bh->state != BUF_STATE_FULL) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002220 rc = sleep_thread(common);
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002221 if (rc)
2222 return rc;
2223 }
2224 smp_rmb();
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002225 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
Michal Nazarewicz93bcf122009-10-28 16:57:19 +01002226 bh->state = BUF_STATE_EMPTY;
2227
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002228 return rc;
2229}
2230
2231
2232/*-------------------------------------------------------------------------*/
2233
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002234static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002235 const struct usb_endpoint_descriptor *d)
2236{
2237 int rc;
2238
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002239 ep->driver_data = common;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002240 rc = usb_ep_enable(ep, d);
2241 if (rc)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002242 ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002243 return rc;
2244}
2245
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002246static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002247 struct usb_request **preq)
2248{
2249 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2250 if (*preq)
2251 return 0;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002252 ERROR(common, "can't allocate request for %s\n", ep->name);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002253 return -ENOMEM;
2254}
2255
2256/*
2257 * Reset interface setting and re-init endpoint state (toggle etc).
2258 * Call with altsetting < 0 to disable the interface. The only other
2259 * available altsetting is 0, which enables the interface.
2260 */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002261static int do_set_interface(struct fsg_common *common, int altsetting)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002262{
2263 int rc = 0;
2264 int i;
2265 const struct usb_endpoint_descriptor *d;
2266
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002267 if (common->running)
2268 DBG(common, "reset interface\n");
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002269
2270reset:
2271 /* Deallocate the requests */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002272 if (common->prev_fsg) {
2273 struct fsg_dev *fsg = common->prev_fsg;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002274
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002275 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2276 struct fsg_buffhd *bh = &common->buffhds[i];
2277
2278 if (bh->inreq) {
2279 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2280 bh->inreq = NULL;
2281 }
2282 if (bh->outreq) {
2283 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2284 bh->outreq = NULL;
2285 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002286 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002287
2288 /* Disable the endpoints */
2289 if (fsg->bulk_in_enabled) {
2290 usb_ep_disable(fsg->bulk_in);
2291 fsg->bulk_in_enabled = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002292 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002293 if (fsg->bulk_out_enabled) {
2294 usb_ep_disable(fsg->bulk_out);
2295 fsg->bulk_out_enabled = 0;
2296 }
2297
2298 common->prev_fsg = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002299 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002300
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002301 common->running = 0;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002302 if (altsetting < 0 || rc != 0)
2303 return rc;
2304
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002305 DBG(common, "set interface %d\n", altsetting);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002306
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002307 if (fsg_is_set(common)) {
2308 struct fsg_dev *fsg = common->fsg;
2309 common->prev_fsg = common->fsg;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002310
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002311 /* Enable the endpoints */
2312 d = fsg_ep_desc(common->gadget,
2313 &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2314 rc = enable_endpoint(common, fsg->bulk_in, d);
2315 if (rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002316 goto reset;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002317 fsg->bulk_in_enabled = 1;
2318
2319 d = fsg_ep_desc(common->gadget,
2320 &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2321 rc = enable_endpoint(common, fsg->bulk_out, d);
2322 if (rc)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002323 goto reset;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002324 fsg->bulk_out_enabled = 1;
2325 common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize);
2326 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2327
2328 /* Allocate the requests */
2329 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2330 struct fsg_buffhd *bh = &common->buffhds[i];
2331
2332 rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
2333 if (rc)
2334 goto reset;
2335 rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
2336 if (rc)
2337 goto reset;
2338 bh->inreq->buf = bh->outreq->buf = bh->buf;
2339 bh->inreq->context = bh->outreq->context = bh;
2340 bh->inreq->complete = bulk_in_complete;
2341 bh->outreq->complete = bulk_out_complete;
2342 }
2343
2344 common->running = 1;
2345 for (i = 0; i < common->nluns; ++i)
2346 common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2347 return rc;
2348 } else {
2349 return -EIO;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002350 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002351}
2352
2353
2354/*
2355 * Change our operational configuration. This code must agree with the code
2356 * that returns config descriptors, and with interface altsetting code.
2357 *
2358 * It's also responsible for power management interactions. Some
2359 * configurations might not work with our current power sources.
2360 * For now we just assume the gadget is always self-powered.
2361 */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002362static int do_set_config(struct fsg_common *common, u8 new_config)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002363{
2364 int rc = 0;
2365
2366 /* Disable the single interface */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002367 if (common->config != 0) {
2368 DBG(common, "reset config\n");
2369 common->config = 0;
2370 rc = do_set_interface(common, -1);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002371 }
2372
2373 /* Enable the interface */
2374 if (new_config != 0) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002375 common->config = new_config;
2376 rc = do_set_interface(common, 0);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002377 if (rc != 0)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002378 common->config = 0; /* Reset on errors */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002379 }
2380 return rc;
2381}
2382
2383
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002384/****************************** ALT CONFIGS ******************************/
2385
2386
2387static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2388{
2389 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002390 fsg->common->prev_fsg = fsg->common->fsg;
2391 fsg->common->fsg = fsg;
2392 fsg->common->new_config = 1;
2393 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002394 return 0;
2395}
2396
2397static void fsg_disable(struct usb_function *f)
2398{
2399 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002400 fsg->common->prev_fsg = fsg->common->fsg;
2401 fsg->common->fsg = fsg;
2402 fsg->common->new_config = 0;
2403 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002404}
2405
2406
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002407/*-------------------------------------------------------------------------*/
2408
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002409static void handle_exception(struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002410{
2411 siginfo_t info;
2412 int sig;
2413 int i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002414 struct fsg_buffhd *bh;
2415 enum fsg_state old_state;
2416 u8 new_config;
2417 struct fsg_lun *curlun;
2418 unsigned int exception_req_tag;
2419 int rc;
2420
2421 /* Clear the existing signals. Anything but SIGUSR1 is converted
2422 * into a high-priority EXIT exception. */
2423 for (;;) {
2424 sig = dequeue_signal_lock(current, &current->blocked, &info);
2425 if (!sig)
2426 break;
2427 if (sig != SIGUSR1) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002428 if (common->state < FSG_STATE_EXIT)
2429 DBG(common, "Main thread exiting on signal\n");
2430 raise_exception(common, FSG_STATE_EXIT);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002431 }
2432 }
2433
2434 /* Cancel all the pending transfers */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002435 if (fsg_is_set(common)) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002436 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002437 bh = &common->buffhds[i];
2438 if (bh->inreq_busy)
2439 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2440 if (bh->outreq_busy)
2441 usb_ep_dequeue(common->fsg->bulk_out,
2442 bh->outreq);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002443 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002444
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002445 /* Wait until everything is idle */
2446 for (;;) {
2447 int num_active = 0;
2448 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2449 bh = &common->buffhds[i];
2450 num_active += bh->inreq_busy + bh->outreq_busy;
2451 }
2452 if (num_active == 0)
2453 break;
2454 if (sleep_thread(common))
2455 return;
2456 }
2457
2458 /* Clear out the controller's fifos */
2459 if (common->fsg->bulk_in_enabled)
2460 usb_ep_fifo_flush(common->fsg->bulk_in);
2461 if (common->fsg->bulk_out_enabled)
2462 usb_ep_fifo_flush(common->fsg->bulk_out);
2463 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002464
2465 /* Reset the I/O buffer states and pointers, the SCSI
2466 * state, and the exception. Then invoke the handler. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002467 spin_lock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002468
2469 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002470 bh = &common->buffhds[i];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002471 bh->state = BUF_STATE_EMPTY;
2472 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002473 common->next_buffhd_to_fill = &common->buffhds[0];
2474 common->next_buffhd_to_drain = &common->buffhds[0];
2475 exception_req_tag = common->exception_req_tag;
2476 new_config = common->new_config;
2477 old_state = common->state;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002478
2479 if (old_state == FSG_STATE_ABORT_BULK_OUT)
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002480 common->state = FSG_STATE_STATUS_PHASE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002481 else {
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002482 for (i = 0; i < common->nluns; ++i) {
2483 curlun = &common->luns[i];
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002484 curlun->prevent_medium_removal = 0;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002485 curlun->sense_data = SS_NO_SENSE;
2486 curlun->unit_attention_data = SS_NO_SENSE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002487 curlun->sense_data_info = 0;
2488 curlun->info_valid = 0;
2489 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002490 common->state = FSG_STATE_IDLE;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002491 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002492 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002493
2494 /* Carry out any extra actions required for the exception */
2495 switch (old_state) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002496 case FSG_STATE_ABORT_BULK_OUT:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002497 send_status(common);
2498 spin_lock_irq(&common->lock);
2499 if (common->state == FSG_STATE_STATUS_PHASE)
2500 common->state = FSG_STATE_IDLE;
2501 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002502 break;
2503
2504 case FSG_STATE_RESET:
2505 /* In case we were forced against our will to halt a
2506 * bulk endpoint, clear the halt now. (The SuperH UDC
2507 * requires this.) */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002508 if (!fsg_is_set(common))
2509 break;
2510 if (test_and_clear_bit(IGNORE_BULK_OUT,
2511 &common->fsg->atomic_bitflags))
2512 usb_ep_clear_halt(common->fsg->bulk_in);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002513
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002514 if (common->ep0_req_tag == exception_req_tag)
2515 ep0_queue(common); /* Complete the status stage */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002516
2517 /* Technically this should go here, but it would only be
2518 * a waste of time. Ditto for the INTERFACE_CHANGE and
2519 * CONFIG_CHANGE cases. */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002520 /* for (i = 0; i < common->nluns; ++i) */
2521 /* common->luns[i].unit_attention_data = */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002522 /* SS_RESET_OCCURRED; */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002523 break;
2524
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002525 case FSG_STATE_CONFIG_CHANGE:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002526 rc = do_set_config(common, new_config);
2527 if (common->ep0_req_tag != exception_req_tag)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002528 break;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002529 if (rc != 0) { /* STALL on errors */
2530 DBG(common, "ep0 set halt\n");
2531 usb_ep_set_halt(common->ep0);
2532 } else { /* Complete the status stage */
2533 ep0_queue(common);
2534 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002535 break;
2536
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002537 case FSG_STATE_EXIT:
2538 case FSG_STATE_TERMINATED:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002539 do_set_config(common, 0); /* Free resources */
2540 spin_lock_irq(&common->lock);
2541 common->state = FSG_STATE_TERMINATED; /* Stop the thread */
2542 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002543 break;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002544
2545 case FSG_STATE_INTERFACE_CHANGE:
2546 case FSG_STATE_DISCONNECT:
2547 case FSG_STATE_COMMAND_PHASE:
2548 case FSG_STATE_DATA_PHASE:
2549 case FSG_STATE_STATUS_PHASE:
2550 case FSG_STATE_IDLE:
2551 break;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002552 }
2553}
2554
2555
2556/*-------------------------------------------------------------------------*/
2557
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002558static int fsg_main_thread(void *common_)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002559{
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002560 struct fsg_common *common = common_;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002561
2562 /* Allow the thread to be killed by a signal, but set the signal mask
2563 * to block everything but INT, TERM, KILL, and USR1. */
2564 allow_signal(SIGINT);
2565 allow_signal(SIGTERM);
2566 allow_signal(SIGKILL);
2567 allow_signal(SIGUSR1);
2568
2569 /* Allow the thread to be frozen */
2570 set_freezable();
2571
2572 /* Arrange for userspace references to be interpreted as kernel
2573 * pointers. That way we can pass a kernel pointer to a routine
2574 * that expects a __user pointer and it will work okay. */
2575 set_fs(get_ds());
2576
2577 /* The main loop */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002578 while (common->state != FSG_STATE_TERMINATED) {
2579 if (exception_in_progress(common) || signal_pending(current)) {
2580 handle_exception(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002581 continue;
2582 }
2583
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002584 if (!common->running) {
2585 sleep_thread(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002586 continue;
2587 }
2588
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002589 if (get_next_command(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002590 continue;
2591
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002592 spin_lock_irq(&common->lock);
2593 if (!exception_in_progress(common))
2594 common->state = FSG_STATE_DATA_PHASE;
2595 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002596
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002597 if (do_scsi_command(common) || finish_reply(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002598 continue;
2599
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002600 spin_lock_irq(&common->lock);
2601 if (!exception_in_progress(common))
2602 common->state = FSG_STATE_STATUS_PHASE;
2603 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002604
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002605 if (send_status(common))
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002606 continue;
2607
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002608 spin_lock_irq(&common->lock);
2609 if (!exception_in_progress(common))
2610 common->state = FSG_STATE_IDLE;
2611 spin_unlock_irq(&common->lock);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002612 }
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002613
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002614 spin_lock_irq(&common->lock);
2615 common->thread_task = NULL;
2616 spin_unlock_irq(&common->lock);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002617
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01002618 if (common->thread_exits)
2619 common->thread_exits(common);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002620
2621 /* Let the unbind and cleanup routines know the thread has exited */
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002622 complete_and_exit(&common->thread_notifier, 0);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002623}
2624
2625
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002626/*************************** DEVICE ATTRIBUTES ***************************/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002627
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002628/* Write permission is checked per LUN in store_*() functions. */
2629static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
2630static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002631
2632
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002633/****************************** FSG COMMON ******************************/
2634
2635static void fsg_common_release(struct kref *ref);
2636
2637static void fsg_lun_release(struct device *dev)
2638{
2639 /* Nothing needs to be done */
2640}
2641
2642static inline void fsg_common_get(struct fsg_common *common)
2643{
2644 kref_get(&common->ref);
2645}
2646
2647static inline void fsg_common_put(struct fsg_common *common)
2648{
2649 kref_put(&common->ref, fsg_common_release);
2650}
2651
2652
2653static struct fsg_common *fsg_common_init(struct fsg_common *common,
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002654 struct usb_composite_dev *cdev,
2655 struct fsg_config *cfg)
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002656{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002657 struct usb_gadget *gadget = cdev->gadget;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002658 struct fsg_buffhd *bh;
2659 struct fsg_lun *curlun;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002660 struct fsg_lun_config *lcfg;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002661 int nluns, i, rc;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002662 char *pathbuf;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002663
2664 /* Find out how many LUNs there should be */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002665 nluns = cfg->nluns;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002666 if (nluns < 1 || nluns > FSG_MAX_LUNS) {
2667 dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns);
2668 return ERR_PTR(-EINVAL);
2669 }
2670
2671 /* Allocate? */
2672 if (!common) {
2673 common = kzalloc(sizeof *common, GFP_KERNEL);
2674 if (!common)
2675 return ERR_PTR(-ENOMEM);
2676 common->free_storage_on_release = 1;
2677 } else {
2678 memset(common, 0, sizeof common);
2679 common->free_storage_on_release = 0;
2680 }
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002681
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01002682 common->private_data = cfg->private_data;
2683
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002684 common->gadget = gadget;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002685 common->ep0 = gadget->ep0;
2686 common->ep0req = cdev->req;
2687
2688 /* Maybe allocate device-global string IDs, and patch descriptors */
2689 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2690 rc = usb_string_id(cdev);
2691 if (rc < 0) {
2692 kfree(common);
2693 return ERR_PTR(rc);
2694 }
2695 fsg_strings[FSG_STRING_INTERFACE].id = rc;
2696 fsg_intf_desc.iInterface = rc;
2697 }
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002698
2699 /* Create the LUNs, open their backing files, and register the
2700 * LUN devices in sysfs. */
2701 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
2702 if (!curlun) {
2703 kfree(common);
2704 return ERR_PTR(-ENOMEM);
2705 }
2706 common->luns = curlun;
2707
2708 init_rwsem(&common->filesem);
2709
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002710 for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
2711 curlun->cdrom = !!lcfg->cdrom;
2712 curlun->ro = lcfg->cdrom || lcfg->ro;
2713 curlun->removable = lcfg->removable;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002714 curlun->dev.release = fsg_lun_release;
2715 curlun->dev.parent = &gadget->dev;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002716 /* curlun->dev.driver = &fsg_driver.driver; XXX */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002717 dev_set_drvdata(&curlun->dev, &common->filesem);
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01002718 dev_set_name(&curlun->dev,
2719 cfg->lun_name_format
2720 ? cfg->lun_name_format
2721 : "lun%d",
2722 i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002723
2724 rc = device_register(&curlun->dev);
2725 if (rc) {
2726 INFO(common, "failed to register LUN%d: %d\n", i, rc);
2727 common->nluns = i;
2728 goto error_release;
2729 }
2730
2731 rc = device_create_file(&curlun->dev, &dev_attr_ro);
2732 if (rc)
2733 goto error_luns;
2734 rc = device_create_file(&curlun->dev, &dev_attr_file);
2735 if (rc)
2736 goto error_luns;
2737
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002738 if (lcfg->filename) {
2739 rc = fsg_lun_open(curlun, lcfg->filename);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002740 if (rc)
2741 goto error_luns;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002742 } else if (!curlun->removable) {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002743 ERROR(common, "no file given for LUN%d\n", i);
2744 rc = -EINVAL;
2745 goto error_luns;
2746 }
2747 }
2748 common->nluns = nluns;
2749
2750
2751 /* Data buffers cyclic list */
2752 /* Buffers in buffhds are static -- no need for additional
2753 * allocation. */
2754 bh = common->buffhds;
2755 i = FSG_NUM_BUFFERS - 1;
2756 do {
2757 bh->next = bh + 1;
2758 } while (++bh, --i);
2759 bh->next = common->buffhds;
2760
2761
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002762 /* Prepare inquiryString */
2763 if (cfg->release != 0xffff) {
2764 i = cfg->release;
2765 } else {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002766 /* The sa1100 controller is not supported */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002767 i = gadget_is_sa1100(gadget)
2768 ? -1
2769 : usb_gadget_controller_number(gadget);
2770 if (i >= 0) {
2771 i = 0x0300 + i;
2772 } else {
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002773 WARNING(common, "controller '%s' not recognized\n",
2774 gadget->name);
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002775 i = 0x0399;
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002776 }
2777 }
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002778#define OR(x, y) ((x) ? (x) : (y))
2779 snprintf(common->inquiry_string, sizeof common->inquiry_string,
2780 "%-8s%-16s%04x",
2781 OR(cfg->vendor_name, "Linux "),
2782 /* Assume product name dependent on the first LUN */
2783 OR(cfg->product_name, common->luns->cdrom
2784 ? "File-Stor Gadget"
2785 : "File-CD Gadget "),
2786 i);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002787
2788
2789 /* Some peripheral controllers are known not to be able to
2790 * halt bulk endpoints correctly. If one of them is present,
2791 * disable stalls.
2792 */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002793 common->can_stall = cfg->can_stall &&
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002794 !(gadget_is_sh(common->gadget) ||
2795 gadget_is_at91(common->gadget));
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002796
2797
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002798 spin_lock_init(&common->lock);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002799 kref_init(&common->ref);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002800
2801
2802 /* Tell the thread to start working */
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01002803 common->thread_exits = cfg->thread_exits;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002804 common->thread_task =
2805 kthread_create(fsg_main_thread, common,
2806 OR(cfg->thread_name, "file-storage"));
2807 if (IS_ERR(common->thread_task)) {
2808 rc = PTR_ERR(common->thread_task);
2809 goto error_release;
2810 }
2811 init_completion(&common->thread_notifier);
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01002812#undef OR
2813
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002814
2815 /* Information */
2816 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
2817 INFO(common, "Number of LUNs=%d\n", common->nluns);
2818
2819 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2820 for (i = 0, nluns = common->nluns, curlun = common->luns;
2821 i < nluns;
2822 ++curlun, ++i) {
2823 char *p = "(no medium)";
2824 if (fsg_lun_is_open(curlun)) {
2825 p = "(error)";
2826 if (pathbuf) {
2827 p = d_path(&curlun->filp->f_path,
2828 pathbuf, PATH_MAX);
2829 if (IS_ERR(p))
2830 p = "(error)";
2831 }
2832 }
2833 LINFO(curlun, "LUN: %s%s%sfile: %s\n",
2834 curlun->removable ? "removable " : "",
2835 curlun->ro ? "read only " : "",
2836 curlun->cdrom ? "CD-ROM " : "",
2837 p);
2838 }
2839 kfree(pathbuf);
2840
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002841 DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task));
2842
2843 wake_up_process(common->thread_task);
2844
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002845 return common;
2846
2847
2848error_luns:
2849 common->nluns = i + 1;
2850error_release:
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002851 common->state = FSG_STATE_TERMINATED; /* The thread is dead */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002852 /* Call fsg_common_release() directly, ref might be not
2853 * initialised */
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002854 fsg_common_release(&common->ref);
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002855 complete(&common->thread_notifier);
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002856 return ERR_PTR(rc);
2857}
2858
2859
2860static void fsg_common_release(struct kref *ref)
2861{
2862 struct fsg_common *common =
2863 container_of(ref, struct fsg_common, ref);
2864 unsigned i = common->nluns;
2865 struct fsg_lun *lun = common->luns;
2866
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002867 /* If the thread isn't already dead, tell it to exit now */
2868 if (common->state != FSG_STATE_TERMINATED) {
2869 raise_exception(common, FSG_STATE_EXIT);
2870 wait_for_completion(&common->thread_notifier);
2871
2872 /* The cleanup routine waits for this completion also */
2873 complete(&common->thread_notifier);
2874 }
2875
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002876 /* Beware tempting for -> do-while optimization: when in error
2877 * recovery nluns may be zero. */
2878
2879 for (; i; --i, ++lun) {
2880 device_remove_file(&lun->dev, &dev_attr_ro);
2881 device_remove_file(&lun->dev, &dev_attr_file);
2882 fsg_lun_close(lun);
2883 device_unregister(&lun->dev);
2884 }
2885
2886 kfree(common->luns);
2887 if (common->free_storage_on_release)
2888 kfree(common);
2889}
2890
2891
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002892/*-------------------------------------------------------------------------*/
2893
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002894
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002895static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002896{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002897 struct fsg_dev *fsg = fsg_from_func(f);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002898
2899 DBG(fsg, "unbind\n");
Michal Nazarewicz9c610212009-10-28 16:57:22 +01002900 fsg_common_put(fsg->common);
2901 kfree(fsg);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002902}
2903
2904
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002905static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002906{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002907 struct fsg_dev *fsg = fsg_from_func(f);
2908 struct usb_gadget *gadget = c->cdev->gadget;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002909 int rc;
2910 int i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002911 struct usb_ep *ep;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002912
2913 fsg->gadget = gadget;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002914
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002915 /* New interface */
2916 i = usb_interface_id(c, f);
2917 if (i < 0)
2918 return i;
2919 fsg_intf_desc.bInterfaceNumber = i;
2920 fsg->interface_number = i;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002921
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002922 /* Find all the endpoints we will use */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002923 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2924 if (!ep)
2925 goto autoconf_fail;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002926 ep->driver_data = fsg->common; /* claim the endpoint */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002927 fsg->bulk_in = ep;
2928
2929 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2930 if (!ep)
2931 goto autoconf_fail;
Michal Nazarewicz8ea864c2009-11-09 14:15:24 +01002932 ep->driver_data = fsg->common; /* claim the endpoint */
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002933 fsg->bulk_out = ep;
2934
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002935 if (gadget_is_dualspeed(gadget)) {
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002936 /* Assume endpoint addresses are the same for both speeds */
2937 fsg_hs_bulk_in_desc.bEndpointAddress =
2938 fsg_fs_bulk_in_desc.bEndpointAddress;
2939 fsg_hs_bulk_out_desc.bEndpointAddress =
2940 fsg_fs_bulk_out_desc.bEndpointAddress;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002941 f->hs_descriptors = fsg_hs_function;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002942 }
2943
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002944 return 0;
2945
2946autoconf_fail:
2947 ERROR(fsg, "unable to autoconfigure all endpoints\n");
2948 rc = -ENOTSUPP;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002949 fsg_unbind(c, f);
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002950 return rc;
2951}
2952
2953
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002954/****************************** ADD FUNCTION ******************************/
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002955
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002956static struct usb_gadget_strings *fsg_strings_array[] = {
2957 &fsg_stringtab,
2958 NULL,
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002959};
2960
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002961static int fsg_add(struct usb_composite_dev *cdev,
2962 struct usb_configuration *c,
2963 struct fsg_common *common)
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002964{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002965 struct fsg_dev *fsg;
2966 int rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002967
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002968 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
2969 if (unlikely(!fsg))
2970 return -ENOMEM;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002971
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01002972 fsg->function.name = FSG_DRIVER_DESC;
2973 fsg->function.strings = fsg_strings_array;
2974 fsg->function.descriptors = fsg_fs_function;
2975 fsg->function.bind = fsg_bind;
2976 fsg->function.unbind = fsg_unbind;
2977 fsg->function.setup = fsg_setup;
2978 fsg->function.set_alt = fsg_set_alt;
2979 fsg->function.disable = fsg_disable;
2980
2981 fsg->common = common;
2982 /* Our caller holds a reference to common structure so we
2983 * don't have to be worry about it being freed until we return
2984 * from this function. So instead of incrementing counter now
2985 * and decrement in error recovery we increment it only when
2986 * call to usb_add_function() was successful. */
2987
2988 rc = usb_add_function(c, &fsg->function);
2989
2990 if (likely(rc == 0))
2991 fsg_common_get(fsg->common);
2992 else
2993 kfree(fsg);
2994
2995 return rc;
Michal Nazarewiczd5e2b672009-10-28 16:57:18 +01002996}
Michal Nazarewicz481e4922009-11-09 14:15:21 +01002997
2998
2999
3000/************************* Module parameters *************************/
3001
3002
3003struct fsg_module_parameters {
3004 char *file[FSG_MAX_LUNS];
3005 int ro[FSG_MAX_LUNS];
3006 int removable[FSG_MAX_LUNS];
3007 int cdrom[FSG_MAX_LUNS];
3008
3009 unsigned int file_count, ro_count, removable_count, cdrom_count;
3010 unsigned int luns; /* nluns */
3011 int stall; /* can_stall */
3012};
3013
3014
3015#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
3016 module_param_array_named(prefix ## name, params.name, type, \
3017 &prefix ## params.name ## _count, \
3018 S_IRUGO); \
3019 MODULE_PARM_DESC(prefix ## name, desc)
3020
3021#define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \
3022 module_param_named(prefix ## name, params.name, type, \
3023 S_IRUGO); \
3024 MODULE_PARM_DESC(prefix ## name, desc)
3025
3026#define FSG_MODULE_PARAMETERS(prefix, params) \
3027 _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \
3028 "names of backing files or devices"); \
3029 _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \
3030 "true to force read-only"); \
3031 _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \
3032 "true to simulate removable media"); \
3033 _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
3034 "true to simulate CD-ROM instead of disk"); \
3035 _FSG_MODULE_PARAM(prefix, params, luns, uint, \
3036 "number of LUNs"); \
3037 _FSG_MODULE_PARAM(prefix, params, stall, bool, \
3038 "false to prevent bulk stalls")
3039
3040
3041static void
3042fsg_config_from_params(struct fsg_config *cfg,
3043 const struct fsg_module_parameters *params)
3044{
3045 struct fsg_lun_config *lun;
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003046 unsigned i;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003047
3048 /* Configure LUNs */
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003049 cfg->nluns =
3050 min(params->luns ?: (params->file_count ?: 1u),
3051 (unsigned)FSG_MAX_LUNS);
3052 for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003053 lun->ro = !!params->ro[i];
3054 lun->cdrom = !!params->cdrom[i];
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003055 lun->removable = /* Removable by default */
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003056 params->removable_count <= i || params->removable[i];
3057 lun->filename =
3058 params->file_count > i && params->file[i][0]
3059 ? params->file[i]
3060 : 0;
3061 }
3062
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01003063 /* Let MSF use defaults */
Michal Nazarewicze8b6f8c2009-11-09 14:15:22 +01003064 cfg->lun_name_format = 0;
3065 cfg->thread_name = 0;
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003066 cfg->vendor_name = 0;
3067 cfg->product_name = 0;
3068 cfg->release = 0xffff;
3069
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +01003070 cfg->thread_exits = 0;
3071 cfg->private_data = 0;
3072
Michal Nazarewicz481e4922009-11-09 14:15:21 +01003073 /* Finalise */
3074 cfg->can_stall = params->stall;
3075}
3076
3077static inline struct fsg_common *
3078fsg_common_from_params(struct fsg_common *common,
3079 struct usb_composite_dev *cdev,
3080 const struct fsg_module_parameters *params)
3081 __attribute__((unused));
3082static inline struct fsg_common *
3083fsg_common_from_params(struct fsg_common *common,
3084 struct usb_composite_dev *cdev,
3085 const struct fsg_module_parameters *params)
3086{
3087 struct fsg_config cfg;
3088 fsg_config_from_params(&cfg, params);
3089 return fsg_common_init(common, cdev, &cfg);
3090}
3091