]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/gadget/function/f_mass_storage.c
usb: gadget: udc: renesas_usb3: make const array max_packet_array static
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / gadget / function / f_mass_storage.c
CommitLineData
d5e2b67a 1/*
d26a6aa0 2 * f_mass_storage.c -- Mass Storage USB Composite Function
d5e2b67a
MN
3 *
4 * Copyright (C) 2003-2008 Alan Stern
d26a6aa0 5 * Copyright (C) 2009 Samsung Electronics
54b8360f 6 * Author: Michal Nazarewicz <mina86@mina86.com>
d5e2b67a
MN
7 * 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
d5e2b67a 40/*
d26a6aa0
MN
41 * The Mass Storage Function acts as a USB Mass Storage device,
42 * appearing to the host as a disk drive or as a CD-ROM drive. In
43 * addition to providing an example of a genuinely useful composite
44 * function for a USB device, it also illustrates a technique of
45 * double-buffering for increased throughput.
d5e2b67a 46 *
a8287a4e
MN
47 * For more information about MSF and in particular its module
48 * parameters and sysfs interface read the
49 * <Documentation/usb/mass-storage.txt> file.
50 */
51
52/*
d26a6aa0
MN
53 * MSF is configured by specifying a fsg_config structure. It has the
54 * following fields:
55 *
56 * nluns Number of LUNs function have (anywhere from 1
75ddead2 57 * to FSG_MAX_LUNS).
d26a6aa0
MN
58 * luns An array of LUN configuration values. This
59 * should be filled for each LUN that
60 * function will include (ie. for "nluns"
61 * LUNs). Each element of the array has
62 * the following fields:
63 * ->filename The path to the backing file for the LUN.
64 * Required if LUN is not marked as
65 * removable.
66 * ->ro Flag specifying access to the LUN shall be
67 * read-only. This is implied if CD-ROM
68 * emulation is enabled as well as when
69 * it was impossible to open "filename"
70 * in R/W mode.
71 * ->removable Flag specifying that LUN shall be indicated as
72 * being removable.
73 * ->cdrom Flag specifying that LUN shall be reported as
74 * being a CD-ROM.
9cfe745e
MN
75 * ->nofua Flag specifying that FUA flag in SCSI WRITE(10,12)
76 * commands for this LUN shall be ignored.
d26a6aa0 77 *
d26a6aa0
MN
78 * vendor_name
79 * product_name
80 * release Information used as a reply to INQUIRY
81 * request. To use default set to NULL,
82 * NULL, 0xffff respectively. The first
83 * field should be 8 and the second 16
84 * characters or less.
85 *
86 * can_stall Set to permit function to halt bulk endpoints.
87 * Disabled on some USB devices known not
88 * to work correctly. You should set it
89 * to true.
90 *
91 * If "removable" is not set for a LUN then a backing file must be
92 * specified. If it is set, then NULL filename means the LUN's medium
93 * is not loaded (an empty string as "filename" in the fsg_config
94 * structure causes error). The CD-ROM emulation includes a single
95 * data track and no audio tracks; hence there need be only one
3f565a36 96 * backing file per LUN.
d26a6aa0 97 *
d26a6aa0
MN
98 * This function is heavily based on "File-backed Storage Gadget" by
99 * Alan Stern which in turn is heavily based on "Gadget Zero" by David
100 * Brownell. The driver's SCSI command interface was based on the
101 * "Information technology - Small Computer System Interface - 2"
102 * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
103 * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
104 * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
105 * was based on the "Universal Serial Bus Mass Storage Class UFI
106 * Command Specification" document, Revision 1.0, December 14, 1998,
107 * available at
d5e2b67a
MN
108 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
109 */
110
d5e2b67a
MN
111/*
112 * Driver Design
113 *
d26a6aa0 114 * The MSF is fairly straightforward. There is a main kernel
d5e2b67a
MN
115 * thread that handles most of the work. Interrupt routines field
116 * callbacks from the controller driver: bulk- and interrupt-request
117 * completion notifications, endpoint-0 events, and disconnect events.
118 * Completion events are passed to the main thread by wakeup calls. Many
119 * ep0 requests are handled at interrupt time, but SetInterface,
120 * SetConfiguration, and device reset requests are forwarded to the
121 * thread in the form of "exceptions" using SIGUSR1 signals (since they
122 * should interrupt any ongoing file I/O operations).
123 *
124 * The thread's main routine implements the standard command/data/status
125 * parts of a SCSI interaction. It and its subroutines are full of tests
126 * for pending signals/exceptions -- all this polling is necessary since
127 * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
128 * indication that the driver really wants to be running in userspace.)
129 * An important point is that so long as the thread is alive it keeps an
130 * open reference to the backing file. This will prevent unmounting
131 * the backing file's underlying filesystem and could cause problems
132 * during system shutdown, for example. To prevent such problems, the
133 * thread catches INT, TERM, and KILL signals and converts them into
134 * an EXIT exception.
135 *
136 * In normal operation the main thread is started during the gadget's
d26a6aa0
MN
137 * fsg_bind() callback and stopped during fsg_unbind(). But it can
138 * also exit when it receives a signal, and there's no point leaving
a8287a4e 139 * the gadget running when the thread is dead. As of this moment, MSF
d26a6aa0
MN
140 * provides no way to deregister the gadget when thread dies -- maybe
141 * a callback functions is needed.
d5e2b67a
MN
142 *
143 * To provide maximum throughput, the driver uses a circular pipeline of
144 * buffer heads (struct fsg_buffhd). In principle the pipeline can be
145 * arbitrarily long; in practice the benefits don't justify having more
146 * than 2 stages (i.e., double buffering). But it helps to think of the
147 * pipeline as being a long one. Each buffer head contains a bulk-in and
148 * a bulk-out request pointer (since the buffer can be used for both
149 * output and input -- directions always are given from the host's
150 * point of view) as well as a pointer to the buffer and various state
151 * variables.
152 *
153 * Use of the pipeline follows a simple protocol. There is a variable
154 * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
155 * At any time that buffer head may still be in use from an earlier
156 * request, so each buffer head has a state variable indicating whether
157 * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
158 * buffer head to be EMPTY, filling the buffer either by file I/O or by
159 * USB I/O (during which the buffer head is BUSY), and marking the buffer
160 * head FULL when the I/O is complete. Then the buffer will be emptied
161 * (again possibly by USB I/O, during which it is marked BUSY) and
162 * finally marked EMPTY again (possibly by a completion routine).
163 *
164 * A module parameter tells the driver to avoid stalling the bulk
165 * endpoints wherever the transport specification allows. This is
166 * necessary for some UDCs like the SuperH, which cannot reliably clear a
167 * halt on a bulk endpoint. However, under certain circumstances the
168 * Bulk-only specification requires a stall. In such cases the driver
169 * will halt the endpoint and set a flag indicating that it should clear
170 * the halt in software during the next device reset. Hopefully this
171 * will permit everything to work correctly. Furthermore, although the
172 * specification allows the bulk-out endpoint to halt when the host sends
173 * too much data, implementing this would cause an unavoidable race.
174 * The driver will always use the "no-stall" approach for OUT transfers.
175 *
176 * One subtle point concerns sending status-stage responses for ep0
177 * requests. Some of these requests, such as device reset, can involve
178 * interrupting an ongoing file I/O operation, which might take an
179 * arbitrarily long time. During that delay the host might give up on
180 * the original ep0 request and issue a new one. When that happens the
181 * driver should not notify the host about completion of the original
182 * request, as the host will no longer be waiting for it. So the driver
183 * assigns to each ep0 request a unique tag, and it keeps track of the
184 * tag value of the request associated with a long-running exception
185 * (device-reset, interface-change, or configuration-change). When the
186 * exception handler is finished, the status-stage response is submitted
187 * only if the current ep0 request tag is equal to the exception request
188 * tag. Thus only the most recently received ep0 request will get a
189 * status-stage response.
190 *
191 * Warning: This driver source file is too long. It ought to be split up
192 * into a header file plus about 3 separate .c files, to handle the details
193 * of the Gadget, USB Mass Storage, and SCSI protocols.
194 */
195
196
197/* #define VERBOSE_DEBUG */
198/* #define DUMP_MSGS */
199
d5e2b67a
MN
200#include <linux/blkdev.h>
201#include <linux/completion.h>
202#include <linux/dcache.h>
203#include <linux/delay.h>
204#include <linux/device.h>
205#include <linux/fcntl.h>
206#include <linux/file.h>
207#include <linux/fs.h>
208#include <linux/kref.h>
209#include <linux/kthread.h>
3f07c014 210#include <linux/sched/signal.h>
d5e2b67a
MN
211#include <linux/limits.h>
212#include <linux/rwsem.h>
213#include <linux/slab.h>
214#include <linux/spinlock.h>
215#include <linux/string.h>
216#include <linux/freezer.h>
e5eaa0dc 217#include <linux/module.h>
4a1e9211 218#include <linux/uaccess.h>
d5e2b67a
MN
219
220#include <linux/usb/ch9.h>
221#include <linux/usb/gadget.h>
a283c03a 222#include <linux/usb/composite.h>
d5e2b67a 223
ef0aa4b9 224#include "configfs.h"
d5e2b67a
MN
225
226
e8b6f8c5 227/*------------------------------------------------------------------------*/
d5e2b67a 228
d23b0f08 229#define FSG_DRIVER_DESC "Mass Storage Function"
d26a6aa0 230#define FSG_DRIVER_VERSION "2009/09/11"
d5e2b67a 231
d5e2b67a
MN
232static const char fsg_string_interface[] = "Mass Storage";
233
6fdc5dd2 234#include "storage_common.h"
f3fed367 235#include "f_mass_storage.h"
d5e2b67a 236
6fdc5dd2
AP
237/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
238static struct usb_string fsg_strings[] = {
239 {FSG_STRING_INTERFACE, fsg_string_interface},
240 {}
241};
242
243static struct usb_gadget_strings fsg_stringtab = {
244 .language = 0x0409, /* en-us */
245 .strings = fsg_strings,
246};
d5e2b67a 247
4610f19b
AP
248static struct usb_gadget_strings *fsg_strings_array[] = {
249 &fsg_stringtab,
250 NULL,
251};
252
d5e2b67a
MN
253/*-------------------------------------------------------------------------*/
254
8ea864cf 255struct fsg_dev;
8876f5e7
MN
256struct fsg_common;
257
a41ae418
MN
258/* Data shared by all the FSG instances. */
259struct fsg_common {
9c610213 260 struct usb_gadget *gadget;
95ed3236 261 struct usb_composite_dev *cdev;
b894f60a 262 struct fsg_dev *fsg, *new_fsg;
225785ae 263 wait_queue_head_t io_wait;
b894f60a 264 wait_queue_head_t fsg_wait;
9c610213 265
a41ae418
MN
266 /* filesem protects: backing files in use */
267 struct rw_semaphore filesem;
268
225785ae 269 /* lock protects: state and thread_task */
8ea864cf
MN
270 spinlock_t lock;
271
272 struct usb_ep *ep0; /* Copy of gadget->ep0 */
273 struct usb_request *ep0req; /* Copy of cdev->req */
274 unsigned int ep0_req_tag;
8ea864cf 275
a41ae418
MN
276 struct fsg_buffhd *next_buffhd_to_fill;
277 struct fsg_buffhd *next_buffhd_to_drain;
6532c7fd 278 struct fsg_buffhd *buffhds;
6fdc5dd2 279 unsigned int fsg_num_buffers;
a41ae418
MN
280
281 int cmnd_size;
282 u8 cmnd[MAX_COMMAND_SIZE];
283
a41ae418 284 unsigned int lun;
dd02ea5a 285 struct fsg_lun *luns[FSG_MAX_LUNS];
a41ae418 286 struct fsg_lun *curlun;
9c610213 287
8ea864cf
MN
288 unsigned int bulk_out_maxpacket;
289 enum fsg_state state; /* For exception handling */
290 unsigned int exception_req_tag;
291
8ea864cf
MN
292 enum data_direction data_dir;
293 u32 data_size;
294 u32 data_size_from_cmnd;
295 u32 tag;
296 u32 residue;
297 u32 usb_amount_left;
298
481e4929 299 unsigned int can_stall:1;
9c610213 300 unsigned int free_storage_on_release:1;
8ea864cf
MN
301 unsigned int phase_error:1;
302 unsigned int short_packet_received:1;
303 unsigned int bad_lun_okay:1;
304 unsigned int running:1;
bd528d4e 305 unsigned int sysfs:1;
9c610213 306
8ea864cf
MN
307 struct completion thread_notifier;
308 struct task_struct *thread_task;
e8b6f8c5 309
c85efcb9
MN
310 /* Gadget's private data. */
311 void *private_data;
312
6ac47090 313 char inquiry_string[INQUIRY_STRING_LEN];
481e4929 314
9c610213 315 struct kref ref;
a41ae418
MN
316};
317
d5e2b67a 318struct fsg_dev {
d23b0f08 319 struct usb_function function;
d23b0f08 320 struct usb_gadget *gadget; /* Copy of cdev->gadget */
a41ae418
MN
321 struct fsg_common *common;
322
d23b0f08
MN
323 u16 interface_number;
324
d26a6aa0
MN
325 unsigned int bulk_in_enabled:1;
326 unsigned int bulk_out_enabled:1;
d5e2b67a
MN
327
328 unsigned long atomic_bitflags;
8ea864cf 329#define IGNORE_BULK_OUT 0
d5e2b67a
MN
330
331 struct usb_ep *bulk_in;
332 struct usb_ep *bulk_out;
8ea864cf 333};
d5e2b67a 334
8ea864cf
MN
335static inline int __fsg_is_set(struct fsg_common *common,
336 const char *func, unsigned line)
337{
338 if (common->fsg)
339 return 1;
340 ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
8876f5e7 341 WARN_ON(1);
8ea864cf
MN
342 return 0;
343}
344
345#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
d5e2b67a 346
d23b0f08
MN
347static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
348{
349 return container_of(f, struct fsg_dev, function);
350}
351
d5e2b67a
MN
352typedef void (*fsg_routine_t)(struct fsg_dev *);
353
8ea864cf 354static int exception_in_progress(struct fsg_common *common)
d5e2b67a 355{
78db441d 356 return common->state > FSG_STATE_NORMAL;
d5e2b67a
MN
357}
358
98346f7d
GKH
359/* Make bulk-out requests be divisible by the maxpacket size */
360static void set_bulk_out_req_length(struct fsg_common *common,
361 struct fsg_buffhd *bh, unsigned int length)
362{
363 unsigned int rem;
364
365 bh->bulk_out_intended_length = length;
366 rem = length % common->bulk_out_maxpacket;
367 if (rem > 0)
368 length += common->bulk_out_maxpacket - rem;
369 bh->outreq->length = length;
370}
371
372
d5e2b67a
MN
373/*-------------------------------------------------------------------------*/
374
375static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
376{
377 const char *name;
378
379 if (ep == fsg->bulk_in)
380 name = "bulk-in";
381 else if (ep == fsg->bulk_out)
382 name = "bulk-out";
383 else
384 name = ep->name;
385 DBG(fsg, "%s set halt\n", name);
386 return usb_ep_set_halt(ep);
387}
388
389
d5e2b67a
MN
390/*-------------------------------------------------------------------------*/
391
392/* These routines may be called in process context or in_irq */
393
8ea864cf 394static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
d5e2b67a
MN
395{
396 unsigned long flags;
397
b73af61e
MN
398 /*
399 * Do nothing if a higher-priority exception is already in progress.
d5e2b67a 400 * If a lower-or-equal priority exception is in progress, preempt it
b73af61e
MN
401 * and notify the main thread by sending it a signal.
402 */
8ea864cf
MN
403 spin_lock_irqsave(&common->lock, flags);
404 if (common->state <= new_state) {
405 common->exception_req_tag = common->ep0_req_tag;
406 common->state = new_state;
407 if (common->thread_task)
d5e2b67a 408 send_sig_info(SIGUSR1, SEND_SIG_FORCED,
8ea864cf 409 common->thread_task);
d5e2b67a 410 }
8ea864cf 411 spin_unlock_irqrestore(&common->lock, flags);
d5e2b67a
MN
412}
413
414
415/*-------------------------------------------------------------------------*/
416
8ea864cf 417static int ep0_queue(struct fsg_common *common)
d5e2b67a
MN
418{
419 int rc;
420
8ea864cf
MN
421 rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
422 common->ep0->driver_data = common;
d5e2b67a 423 if (rc != 0 && rc != -ESHUTDOWN) {
d5e2b67a 424 /* We can't do much more than wait for a reset */
8ea864cf
MN
425 WARNING(common, "error in submission: %s --> %d\n",
426 common->ep0->name, rc);
d5e2b67a
MN
427 }
428 return rc;
429}
430
b73af61e 431
d5e2b67a
MN
432/*-------------------------------------------------------------------------*/
433
b73af61e 434/* Completion handlers. These always run in_irq. */
d5e2b67a
MN
435
436static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
437{
8ea864cf 438 struct fsg_common *common = ep->driver_data;
d5e2b67a
MN
439 struct fsg_buffhd *bh = req->context;
440
441 if (req->status || req->actual != req->length)
8ea864cf 442 DBG(common, "%s --> %d, %u/%u\n", __func__,
b73af61e 443 req->status, req->actual, req->length);
d26a6aa0 444 if (req->status == -ECONNRESET) /* Request was cancelled */
d5e2b67a
MN
445 usb_ep_fifo_flush(ep);
446
225785ae
AS
447 /* Synchronize with the smp_load_acquire() in sleep_thread() */
448 smp_store_release(&bh->state, BUF_STATE_EMPTY);
449 wake_up(&common->io_wait);
d5e2b67a
MN
450}
451
452static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
453{
8ea864cf 454 struct fsg_common *common = ep->driver_data;
d5e2b67a
MN
455 struct fsg_buffhd *bh = req->context;
456
8ea864cf 457 dump_msg(common, "bulk-out", req->buf, req->actual);
98346f7d 458 if (req->status || req->actual != bh->bulk_out_intended_length)
8ea864cf 459 DBG(common, "%s --> %d, %u/%u\n", __func__,
98346f7d 460 req->status, req->actual, bh->bulk_out_intended_length);
d26a6aa0 461 if (req->status == -ECONNRESET) /* Request was cancelled */
d5e2b67a
MN
462 usb_ep_fifo_flush(ep);
463
225785ae
AS
464 /* Synchronize with the smp_load_acquire() in sleep_thread() */
465 smp_store_release(&bh->state, BUF_STATE_FULL);
466 wake_up(&common->io_wait);
d5e2b67a
MN
467}
468
dd02ea5a
KO
469static int _fsg_common_get_max_lun(struct fsg_common *common)
470{
471 int i = ARRAY_SIZE(common->luns) - 1;
472
473 while (i >= 0 && !common->luns[i])
474 --i;
475
476 return i;
477}
478
d23b0f08 479static int fsg_setup(struct usb_function *f,
b73af61e 480 const struct usb_ctrlrequest *ctrl)
d5e2b67a 481{
d23b0f08 482 struct fsg_dev *fsg = fsg_from_func(f);
8ea864cf 483 struct usb_request *req = fsg->common->ep0req;
d5e2b67a 484 u16 w_index = le16_to_cpu(ctrl->wIndex);
93bcf12e 485 u16 w_value = le16_to_cpu(ctrl->wValue);
d5e2b67a
MN
486 u16 w_length = le16_to_cpu(ctrl->wLength);
487
b894f60a 488 if (!fsg_is_set(fsg->common))
93bcf12e 489 return -EOPNOTSUPP;
d5e2b67a 490
73ee4da9
RQ
491 ++fsg->common->ep0_req_tag; /* Record arrival of a new request */
492 req->context = NULL;
493 req->length = 0;
494 dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
495
93bcf12e 496 switch (ctrl->bRequest) {
d5e2b67a 497
775dafdc 498 case US_BULK_RESET_REQUEST:
93bcf12e
MN
499 if (ctrl->bRequestType !=
500 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
d5e2b67a 501 break;
ce7b6121
PZ
502 if (w_index != fsg->interface_number || w_value != 0 ||
503 w_length != 0)
93bcf12e 504 return -EDOM;
d5e2b67a 505
b73af61e
MN
506 /*
507 * Raise an exception to stop the current operation
508 * and reinitialize our state.
509 */
93bcf12e 510 DBG(fsg, "bulk reset request\n");
78db441d 511 raise_exception(fsg->common, FSG_STATE_PROTOCOL_RESET);
655403c2 512 return USB_GADGET_DELAYED_STATUS;
d5e2b67a 513
775dafdc 514 case US_BULK_GET_MAX_LUN:
93bcf12e
MN
515 if (ctrl->bRequestType !=
516 (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
d5e2b67a 517 break;
db332bc9
PZ
518 if (w_index != fsg->interface_number || w_value != 0 ||
519 w_length != 1)
93bcf12e
MN
520 return -EDOM;
521 VDBG(fsg, "get max LUN\n");
dd02ea5a 522 *(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
b00ce11f
MN
523
524 /* Respond with data/status */
d7e18a9f 525 req->length = min((u16)1, w_length);
b00ce11f 526 return ep0_queue(fsg->common);
93bcf12e
MN
527 }
528
529 VDBG(fsg,
b73af61e 530 "unknown class-specific control req %02x.%02x v%04x i%04x l%u\n",
93bcf12e
MN
531 ctrl->bRequestType, ctrl->bRequest,
532 le16_to_cpu(ctrl->wValue), w_index, w_length);
533 return -EOPNOTSUPP;
d5e2b67a
MN
534}
535
536
d5e2b67a
MN
537/*-------------------------------------------------------------------------*/
538
539/* All the following routines run in process context */
540
d5e2b67a 541/* Use this for bulk or interrupt transfers, not ep0 */
225785ae
AS
542static int start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
543 struct usb_request *req)
d5e2b67a
MN
544{
545 int rc;
546
547 if (ep == fsg->bulk_in)
548 dump_msg(fsg, "bulk-in", req->buf, req->length);
d5e2b67a 549
d5e2b67a 550 rc = usb_ep_queue(ep, req, GFP_KERNEL);
225785ae 551 if (rc) {
d5e2b67a 552
225785ae
AS
553 /* We can't do much more than wait for a reset */
554 req->status = rc;
4953ef65 555
225785ae
AS
556 /*
557 * Note: currently the net2280 driver fails zero-length
558 * submissions if DMA is enabled.
559 */
560 if (rc != -ESHUTDOWN &&
561 !(rc == -EOPNOTSUPP && req->length == 0))
562 WARNING(fsg, "error in submission: %s --> %d\n",
563 ep->name, rc);
564 }
565 return rc;
d5e2b67a
MN
566}
567
fe52f792
MN
568static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
569{
570 if (!fsg_is_set(common))
571 return false;
225785ae
AS
572 bh->state = BUF_STATE_SENDING;
573 if (start_transfer(common->fsg, common->fsg->bulk_in, bh->inreq))
574 bh->state = BUF_STATE_EMPTY;
fe52f792
MN
575 return true;
576}
8ea864cf 577
fe52f792
MN
578static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
579{
580 if (!fsg_is_set(common))
581 return false;
225785ae
AS
582 bh->state = BUF_STATE_RECEIVING;
583 if (start_transfer(common->fsg, common->fsg->bulk_out, bh->outreq))
584 bh->state = BUF_STATE_FULL;
fe52f792
MN
585 return true;
586}
d5e2b67a 587
225785ae
AS
588static int sleep_thread(struct fsg_common *common, bool can_freeze,
589 struct fsg_buffhd *bh)
d5e2b67a 590{
225785ae 591 int rc;
d5e2b67a 592
225785ae
AS
593 /* Wait until a signal arrives or bh is no longer busy */
594 if (can_freeze)
595 /*
596 * synchronize with the smp_store_release(&bh->state) in
597 * bulk_in_complete() or bulk_out_complete()
598 */
599 rc = wait_event_freezable(common->io_wait,
600 bh && smp_load_acquire(&bh->state) >=
601 BUF_STATE_EMPTY);
602 else
603 rc = wait_event_interruptible(common->io_wait,
604 bh && smp_load_acquire(&bh->state) >=
605 BUF_STATE_EMPTY);
606 return rc ? -EINTR : 0;
d5e2b67a
MN
607}
608
609
610/*-------------------------------------------------------------------------*/
611
8ea864cf 612static int do_read(struct fsg_common *common)
d5e2b67a 613{
8ea864cf 614 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
615 u32 lba;
616 struct fsg_buffhd *bh;
617 int rc;
618 u32 amount_left;
619 loff_t file_offset, file_offset_tmp;
620 unsigned int amount;
d5e2b67a
MN
621 ssize_t nread;
622
b73af61e
MN
623 /*
624 * Get the starting Logical Block Address and check that it's
625 * not too big.
626 */
0a6a717c 627 if (common->cmnd[0] == READ_6)
8ea864cf 628 lba = get_unaligned_be24(&common->cmnd[1]);
d5e2b67a 629 else {
8ea864cf 630 lba = get_unaligned_be32(&common->cmnd[2]);
d5e2b67a 631
b73af61e
MN
632 /*
633 * We allow DPO (Disable Page Out = don't save data in the
d5e2b67a 634 * cache) and FUA (Force Unit Access = don't read from the
b73af61e
MN
635 * cache), but we don't implement them.
636 */
8ea864cf 637 if ((common->cmnd[1] & ~0x18) != 0) {
d5e2b67a
MN
638 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
639 return -EINVAL;
640 }
641 }
642 if (lba >= curlun->num_sectors) {
643 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
644 return -EINVAL;
645 }
3f565a36 646 file_offset = ((loff_t) lba) << curlun->blkbits;
d5e2b67a
MN
647
648 /* Carry out the file reads */
8ea864cf 649 amount_left = common->data_size_from_cmnd;
d5e2b67a 650 if (unlikely(amount_left == 0))
d26a6aa0 651 return -EIO; /* No default reply */
d5e2b67a
MN
652
653 for (;;) {
b73af61e
MN
654 /*
655 * Figure out how much we need to read:
d5e2b67a
MN
656 * Try to read the remaining amount.
657 * But don't read more than the buffer size.
658 * And don't try to read past the end of the file.
b73af61e 659 */
93bcf12e 660 amount = min(amount_left, FSG_BUFLEN);
b73af61e
MN
661 amount = min((loff_t)amount,
662 curlun->file_length - file_offset);
d5e2b67a
MN
663
664 /* Wait for the next buffer to become available */
8ea864cf 665 bh = common->next_buffhd_to_fill;
225785ae
AS
666 rc = sleep_thread(common, false, bh);
667 if (rc)
668 return rc;
d5e2b67a 669
b73af61e
MN
670 /*
671 * If we were asked to read past the end of file,
672 * end with an empty buffer.
673 */
d5e2b67a
MN
674 if (amount == 0) {
675 curlun->sense_data =
676 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
3f565a36
PL
677 curlun->sense_data_info =
678 file_offset >> curlun->blkbits;
d5e2b67a
MN
679 curlun->info_valid = 1;
680 bh->inreq->length = 0;
681 bh->state = BUF_STATE_FULL;
682 break;
683 }
684
685 /* Perform the read */
686 file_offset_tmp = file_offset;
05a4a33b
CH
687 nread = kernel_read(curlun->filp, bh->buf, amount,
688 &file_offset_tmp);
d5e2b67a 689 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
b73af61e 690 (unsigned long long)file_offset, (int)nread);
d5e2b67a
MN
691 if (signal_pending(current))
692 return -EINTR;
693
694 if (nread < 0) {
b73af61e 695 LDBG(curlun, "error in file read: %d\n", (int)nread);
d5e2b67a
MN
696 nread = 0;
697 } else if (nread < amount) {
698 LDBG(curlun, "partial file read: %d/%u\n",
b73af61e 699 (int)nread, amount);
3f565a36 700 nread = round_down(nread, curlun->blksize);
d5e2b67a
MN
701 }
702 file_offset += nread;
703 amount_left -= nread;
8ea864cf 704 common->residue -= nread;
04eee25b
AS
705
706 /*
707 * Except at the end of the transfer, nread will be
708 * equal to the buffer size, which is divisible by the
709 * bulk-in maxpacket size.
710 */
d5e2b67a
MN
711 bh->inreq->length = nread;
712 bh->state = BUF_STATE_FULL;
713
714 /* If an error occurred, report it and its position */
715 if (nread < amount) {
716 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
3f565a36
PL
717 curlun->sense_data_info =
718 file_offset >> curlun->blkbits;
d5e2b67a
MN
719 curlun->info_valid = 1;
720 break;
721 }
722
723 if (amount_left == 0)
d26a6aa0 724 break; /* No more left to read */
d5e2b67a
MN
725
726 /* Send this buffer and go read some more */
727 bh->inreq->zero = 0;
fe52f792
MN
728 if (!start_in_transfer(common, bh))
729 /* Don't know what to do if common->fsg is NULL */
8ea864cf
MN
730 return -EIO;
731 common->next_buffhd_to_fill = bh->next;
d5e2b67a
MN
732 }
733
d26a6aa0 734 return -EIO; /* No default reply */
d5e2b67a
MN
735}
736
737
738/*-------------------------------------------------------------------------*/
739
8ea864cf 740static int do_write(struct fsg_common *common)
d5e2b67a 741{
8ea864cf 742 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
743 u32 lba;
744 struct fsg_buffhd *bh;
745 int get_some_more;
746 u32 amount_left_to_req, amount_left_to_write;
747 loff_t usb_offset, file_offset, file_offset_tmp;
748 unsigned int amount;
d5e2b67a
MN
749 ssize_t nwritten;
750 int rc;
751
752 if (curlun->ro) {
753 curlun->sense_data = SS_WRITE_PROTECTED;
754 return -EINVAL;
755 }
756 spin_lock(&curlun->filp->f_lock);
d26a6aa0 757 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
d5e2b67a
MN
758 spin_unlock(&curlun->filp->f_lock);
759
b73af61e
MN
760 /*
761 * Get the starting Logical Block Address and check that it's
762 * not too big
763 */
0a6a717c 764 if (common->cmnd[0] == WRITE_6)
8ea864cf 765 lba = get_unaligned_be24(&common->cmnd[1]);
d5e2b67a 766 else {
8ea864cf 767 lba = get_unaligned_be32(&common->cmnd[2]);
d5e2b67a 768
b73af61e
MN
769 /*
770 * We allow DPO (Disable Page Out = don't save data in the
d5e2b67a
MN
771 * cache) and FUA (Force Unit Access = write directly to the
772 * medium). We don't implement DPO; we implement FUA by
b73af61e
MN
773 * performing synchronous output.
774 */
8ea864cf 775 if (common->cmnd[1] & ~0x18) {
d5e2b67a
MN
776 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
777 return -EINVAL;
778 }
9cfe745e 779 if (!curlun->nofua && (common->cmnd[1] & 0x08)) { /* FUA */
d5e2b67a
MN
780 spin_lock(&curlun->filp->f_lock);
781 curlun->filp->f_flags |= O_SYNC;
782 spin_unlock(&curlun->filp->f_lock);
783 }
784 }
785 if (lba >= curlun->num_sectors) {
786 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
787 return -EINVAL;
788 }
789
790 /* Carry out the file writes */
791 get_some_more = 1;
3f565a36 792 file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
8ea864cf
MN
793 amount_left_to_req = common->data_size_from_cmnd;
794 amount_left_to_write = common->data_size_from_cmnd;
d5e2b67a
MN
795
796 while (amount_left_to_write > 0) {
797
798 /* Queue a request for more data from the host */
8ea864cf 799 bh = common->next_buffhd_to_fill;
d5e2b67a
MN
800 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
801
b73af61e
MN
802 /*
803 * Figure out how much we want to get:
04eee25b
AS
804 * Try to get the remaining amount,
805 * but not more than the buffer size.
b73af61e 806 */
93bcf12e 807 amount = min(amount_left_to_req, FSG_BUFLEN);
04eee25b
AS
808
809 /* Beyond the end of the backing file? */
810 if (usb_offset >= curlun->file_length) {
d5e2b67a
MN
811 get_some_more = 0;
812 curlun->sense_data =
813 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
3f565a36
PL
814 curlun->sense_data_info =
815 usb_offset >> curlun->blkbits;
d5e2b67a
MN
816 curlun->info_valid = 1;
817 continue;
818 }
d5e2b67a
MN
819
820 /* Get the next buffer */
821 usb_offset += amount;
8ea864cf 822 common->usb_amount_left -= amount;
d5e2b67a
MN
823 amount_left_to_req -= amount;
824 if (amount_left_to_req == 0)
825 get_some_more = 0;
826
b73af61e 827 /*
04eee25b
AS
828 * Except at the end of the transfer, amount will be
829 * equal to the buffer size, which is divisible by
830 * the bulk-out maxpacket size.
b73af61e 831 */
fe696765 832 set_bulk_out_req_length(common, bh, amount);
fe52f792 833 if (!start_out_transfer(common, bh))
b73af61e 834 /* Dunno what to do if common->fsg is NULL */
8ea864cf
MN
835 return -EIO;
836 common->next_buffhd_to_fill = bh->next;
d5e2b67a
MN
837 continue;
838 }
839
840 /* Write the received data to the backing file */
8ea864cf 841 bh = common->next_buffhd_to_drain;
d5e2b67a 842 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
d26a6aa0 843 break; /* We stopped early */
225785ae
AS
844
845 /* Wait for the data to be received */
846 rc = sleep_thread(common, false, bh);
847 if (rc)
848 return rc;
849
850 common->next_buffhd_to_drain = bh->next;
851 bh->state = BUF_STATE_EMPTY;
852
853 /* Did something go wrong with the transfer? */
854 if (bh->outreq->status != 0) {
855 curlun->sense_data = SS_COMMUNICATION_FAILURE;
856 curlun->sense_data_info =
3f565a36 857 file_offset >> curlun->blkbits;
225785ae
AS
858 curlun->info_valid = 1;
859 break;
860 }
d5e2b67a 861
225785ae
AS
862 amount = bh->outreq->actual;
863 if (curlun->file_length - file_offset < amount) {
864 LERROR(curlun, "write %u @ %llu beyond end %llu\n",
b73af61e
MN
865 amount, (unsigned long long)file_offset,
866 (unsigned long long)curlun->file_length);
225785ae
AS
867 amount = curlun->file_length - file_offset;
868 }
d5e2b67a 869
225785ae
AS
870 /*
871 * Don't accept excess data. The spec doesn't say
872 * what to do in this case. We'll ignore the error.
873 */
874 amount = min(amount, bh->bulk_out_intended_length);
d5e2b67a 875
225785ae
AS
876 /* Don't write a partial block */
877 amount = round_down(amount, curlun->blksize);
878 if (amount == 0)
879 goto empty_write;
880
881 /* Perform the write */
882 file_offset_tmp = file_offset;
05a4a33b
CH
883 nwritten = kernel_write(curlun->filp, bh->buf, amount,
884 &file_offset_tmp);
225785ae
AS
885 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
886 (unsigned long long)file_offset, (int)nwritten);
887 if (signal_pending(current))
888 return -EINTR; /* Interrupted! */
889
890 if (nwritten < 0) {
891 LDBG(curlun, "error in file write: %d\n",
892 (int) nwritten);
893 nwritten = 0;
894 } else if (nwritten < amount) {
895 LDBG(curlun, "partial file write: %d/%u\n",
896 (int) nwritten, amount);
897 nwritten = round_down(nwritten, curlun->blksize);
898 }
899 file_offset += nwritten;
900 amount_left_to_write -= nwritten;
901 common->residue -= nwritten;
902
903 /* If an error occurred, report it and its position */
904 if (nwritten < amount) {
905 curlun->sense_data = SS_WRITE_ERROR;
906 curlun->sense_data_info =
3f565a36 907 file_offset >> curlun->blkbits;
225785ae
AS
908 curlun->info_valid = 1;
909 break;
910 }
d5e2b67a 911
04eee25b 912 empty_write:
225785ae
AS
913 /* Did the host decide to stop early? */
914 if (bh->outreq->actual < bh->bulk_out_intended_length) {
915 common->short_packet_received = 1;
916 break;
d5e2b67a 917 }
d5e2b67a
MN
918 }
919
d26a6aa0 920 return -EIO; /* No default reply */
d5e2b67a
MN
921}
922
923
924/*-------------------------------------------------------------------------*/
925
8ea864cf 926static int do_synchronize_cache(struct fsg_common *common)
d5e2b67a 927{
8ea864cf 928 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
929 int rc;
930
931 /* We ignore the requested LBA and write out all file's
932 * dirty data buffers. */
933 rc = fsg_lun_fsync_sub(curlun);
934 if (rc)
935 curlun->sense_data = SS_WRITE_ERROR;
936 return 0;
937}
938
939
940/*-------------------------------------------------------------------------*/
941
942static void invalidate_sub(struct fsg_lun *curlun)
943{
944 struct file *filp = curlun->filp;
496ad9aa 945 struct inode *inode = file_inode(filp);
d5e2b67a
MN
946 unsigned long rc;
947
948 rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
2ecdc82e 949 VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
d5e2b67a
MN
950}
951
8ea864cf 952static int do_verify(struct fsg_common *common)
d5e2b67a 953{
8ea864cf 954 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
955 u32 lba;
956 u32 verification_length;
8ea864cf 957 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
d5e2b67a
MN
958 loff_t file_offset, file_offset_tmp;
959 u32 amount_left;
960 unsigned int amount;
961 ssize_t nread;
962
b73af61e
MN
963 /*
964 * Get the starting Logical Block Address and check that it's
965 * not too big.
966 */
8ea864cf 967 lba = get_unaligned_be32(&common->cmnd[2]);
d5e2b67a
MN
968 if (lba >= curlun->num_sectors) {
969 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
970 return -EINVAL;
971 }
972
b73af61e
MN
973 /*
974 * We allow DPO (Disable Page Out = don't save data in the
975 * cache) but we don't implement it.
976 */
8ea864cf 977 if (common->cmnd[1] & ~0x10) {
d5e2b67a
MN
978 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
979 return -EINVAL;
980 }
981
8ea864cf 982 verification_length = get_unaligned_be16(&common->cmnd[7]);
d5e2b67a 983 if (unlikely(verification_length == 0))
d26a6aa0 984 return -EIO; /* No default reply */
d5e2b67a
MN
985
986 /* Prepare to carry out the file verify */
3f565a36
PL
987 amount_left = verification_length << curlun->blkbits;
988 file_offset = ((loff_t) lba) << curlun->blkbits;
d5e2b67a
MN
989
990 /* Write out all the dirty buffers before invalidating them */
991 fsg_lun_fsync_sub(curlun);
992 if (signal_pending(current))
993 return -EINTR;
994
995 invalidate_sub(curlun);
996 if (signal_pending(current))
997 return -EINTR;
998
999 /* Just try to read the requested blocks */
1000 while (amount_left > 0) {
b73af61e
MN
1001 /*
1002 * Figure out how much we need to read:
d5e2b67a
MN
1003 * Try to read the remaining amount, but not more than
1004 * the buffer size.
1005 * And don't try to read past the end of the file.
b73af61e 1006 */
93bcf12e 1007 amount = min(amount_left, FSG_BUFLEN);
b73af61e
MN
1008 amount = min((loff_t)amount,
1009 curlun->file_length - file_offset);
d5e2b67a
MN
1010 if (amount == 0) {
1011 curlun->sense_data =
1012 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
3f565a36
PL
1013 curlun->sense_data_info =
1014 file_offset >> curlun->blkbits;
d5e2b67a
MN
1015 curlun->info_valid = 1;
1016 break;
1017 }
1018
1019 /* Perform the read */
1020 file_offset_tmp = file_offset;
05a4a33b
CH
1021 nread = kernel_read(curlun->filp, bh->buf, amount,
1022 &file_offset_tmp);
d5e2b67a
MN
1023 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1024 (unsigned long long) file_offset,
1025 (int) nread);
1026 if (signal_pending(current))
1027 return -EINTR;
1028
1029 if (nread < 0) {
b73af61e 1030 LDBG(curlun, "error in file verify: %d\n", (int)nread);
d5e2b67a
MN
1031 nread = 0;
1032 } else if (nread < amount) {
1033 LDBG(curlun, "partial file verify: %d/%u\n",
b73af61e 1034 (int)nread, amount);
3f565a36 1035 nread = round_down(nread, curlun->blksize);
d5e2b67a
MN
1036 }
1037 if (nread == 0) {
1038 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
3f565a36
PL
1039 curlun->sense_data_info =
1040 file_offset >> curlun->blkbits;
d5e2b67a
MN
1041 curlun->info_valid = 1;
1042 break;
1043 }
1044 file_offset += nread;
1045 amount_left -= nread;
1046 }
1047 return 0;
1048}
1049
1050
1051/*-------------------------------------------------------------------------*/
1052
8ea864cf 1053static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
d5e2b67a 1054{
8ea864cf 1055 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
1056 u8 *buf = (u8 *) bh->buf;
1057
481e4929 1058 if (!curlun) { /* Unsupported LUNs are okay */
8ea864cf 1059 common->bad_lun_okay = 1;
d5e2b67a 1060 memset(buf, 0, 36);
fddc26f5 1061 buf[0] = TYPE_NO_LUN; /* Unsupported, no device-type */
d26a6aa0 1062 buf[4] = 31; /* Additional length */
d5e2b67a
MN
1063 return 36;
1064 }
1065
0a6a717c 1066 buf[0] = curlun->cdrom ? TYPE_ROM : TYPE_DISK;
481e4929 1067 buf[1] = curlun->removable ? 0x80 : 0;
d26a6aa0
MN
1068 buf[2] = 2; /* ANSI SCSI level 2 */
1069 buf[3] = 2; /* SCSI-2 INQUIRY data format */
1070 buf[4] = 31; /* Additional length */
1071 buf[5] = 0; /* No special options */
481e4929
MN
1072 buf[6] = 0;
1073 buf[7] = 0;
6ac47090
PG
1074 if (curlun->inquiry_string[0])
1075 memcpy(buf + 8, curlun->inquiry_string,
1076 sizeof(curlun->inquiry_string));
1077 else
1078 memcpy(buf + 8, common->inquiry_string,
1079 sizeof(common->inquiry_string));
d5e2b67a
MN
1080 return 36;
1081}
1082
8ea864cf 1083static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
d5e2b67a 1084{
8ea864cf 1085 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
1086 u8 *buf = (u8 *) bh->buf;
1087 u32 sd, sdinfo;
1088 int valid;
1089
1090 /*
1091 * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1092 *
1093 * If a REQUEST SENSE command is received from an initiator
1094 * with a pending unit attention condition (before the target
1095 * generates the contingent allegiance condition), then the
1096 * target shall either:
1097 * a) report any pending sense data and preserve the unit
1098 * attention condition on the logical unit, or,
1099 * b) report the unit attention condition, may discard any
1100 * pending sense data, and clear the unit attention
1101 * condition on the logical unit for that initiator.
1102 *
1103 * FSG normally uses option a); enable this code to use option b).
1104 */
1105#if 0
1106 if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1107 curlun->sense_data = curlun->unit_attention_data;
1108 curlun->unit_attention_data = SS_NO_SENSE;
1109 }
1110#endif
1111
d26a6aa0 1112 if (!curlun) { /* Unsupported LUNs are okay */
8ea864cf 1113 common->bad_lun_okay = 1;
d5e2b67a
MN
1114 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1115 sdinfo = 0;
1116 valid = 0;
1117 } else {
1118 sd = curlun->sense_data;
1119 sdinfo = curlun->sense_data_info;
1120 valid = curlun->info_valid << 7;
1121 curlun->sense_data = SS_NO_SENSE;
1122 curlun->sense_data_info = 0;
1123 curlun->info_valid = 0;
1124 }
1125
1126 memset(buf, 0, 18);
d26a6aa0 1127 buf[0] = valid | 0x70; /* Valid, current error */
d5e2b67a
MN
1128 buf[2] = SK(sd);
1129 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
d26a6aa0 1130 buf[7] = 18 - 8; /* Additional sense length */
d5e2b67a
MN
1131 buf[12] = ASC(sd);
1132 buf[13] = ASCQ(sd);
1133 return 18;
1134}
1135
8ea864cf 1136static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
d5e2b67a 1137{
8ea864cf
MN
1138 struct fsg_lun *curlun = common->curlun;
1139 u32 lba = get_unaligned_be32(&common->cmnd[2]);
1140 int pmi = common->cmnd[8];
b73af61e 1141 u8 *buf = (u8 *)bh->buf;
d5e2b67a
MN
1142
1143 /* Check the PMI and LBA fields */
1144 if (pmi > 1 || (pmi == 0 && lba != 0)) {
1145 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1146 return -EINVAL;
1147 }
1148
1149 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1150 /* Max logical block */
3f565a36 1151 put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
d5e2b67a
MN
1152 return 8;
1153}
1154
8ea864cf 1155static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
d5e2b67a 1156{
8ea864cf
MN
1157 struct fsg_lun *curlun = common->curlun;
1158 int msf = common->cmnd[1] & 0x02;
1159 u32 lba = get_unaligned_be32(&common->cmnd[2]);
b73af61e 1160 u8 *buf = (u8 *)bh->buf;
d5e2b67a 1161
8ea864cf 1162 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
d5e2b67a
MN
1163 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1164 return -EINVAL;
1165 }
1166 if (lba >= curlun->num_sectors) {
1167 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1168 return -EINVAL;
1169 }
1170
1171 memset(buf, 0, 8);
1172 buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1173 store_cdrom_address(&buf[4], msf, lba);
1174 return 8;
1175}
1176
8ea864cf 1177static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
d5e2b67a 1178{
8ea864cf
MN
1179 struct fsg_lun *curlun = common->curlun;
1180 int msf = common->cmnd[1] & 0x02;
1181 int start_track = common->cmnd[6];
b73af61e 1182 u8 *buf = (u8 *)bh->buf;
d5e2b67a 1183
8ea864cf 1184 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
d5e2b67a
MN
1185 start_track > 1) {
1186 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1187 return -EINVAL;
1188 }
1189
1190 memset(buf, 0, 20);
1191 buf[1] = (20-2); /* TOC data length */
1192 buf[2] = 1; /* First track number */
1193 buf[3] = 1; /* Last track number */
1194 buf[5] = 0x16; /* Data track, copying allowed */
1195 buf[6] = 0x01; /* Only track is number 1 */
1196 store_cdrom_address(&buf[8], msf, 0);
1197
1198 buf[13] = 0x16; /* Lead-out track is data */
1199 buf[14] = 0xAA; /* Lead-out track number */
1200 store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1201 return 20;
1202}
1203
8ea864cf 1204static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
d5e2b67a 1205{
8ea864cf
MN
1206 struct fsg_lun *curlun = common->curlun;
1207 int mscmnd = common->cmnd[0];
d5e2b67a
MN
1208 u8 *buf = (u8 *) bh->buf;
1209 u8 *buf0 = buf;
1210 int pc, page_code;
1211 int changeable_values, all_pages;
1212 int valid_page = 0;
1213 int len, limit;
1214
8ea864cf 1215 if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
d5e2b67a
MN
1216 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1217 return -EINVAL;
1218 }
8ea864cf
MN
1219 pc = common->cmnd[2] >> 6;
1220 page_code = common->cmnd[2] & 0x3f;
d5e2b67a
MN
1221 if (pc == 3) {
1222 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1223 return -EINVAL;
1224 }
1225 changeable_values = (pc == 1);
1226 all_pages = (page_code == 0x3f);
1227
b73af61e
MN
1228 /*
1229 * Write the mode parameter header. Fixed values are: default
d5e2b67a
MN
1230 * medium type, no cache control (DPOFUA), and no block descriptors.
1231 * The only variable value is the WriteProtect bit. We will fill in
b73af61e
MN
1232 * the mode data length later.
1233 */
d5e2b67a 1234 memset(buf, 0, 8);
0a6a717c 1235 if (mscmnd == MODE_SENSE) {
d26a6aa0 1236 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
d5e2b67a
MN
1237 buf += 4;
1238 limit = 255;
0a6a717c 1239 } else { /* MODE_SENSE_10 */
d26a6aa0 1240 buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
d5e2b67a 1241 buf += 8;
d26a6aa0 1242 limit = 65535; /* Should really be FSG_BUFLEN */
d5e2b67a
MN
1243 }
1244
1245 /* No block descriptors */
1246
b73af61e
MN
1247 /*
1248 * The mode pages, in numerical order. The only page we support
1249 * is the Caching page.
1250 */
d5e2b67a
MN
1251 if (page_code == 0x08 || all_pages) {
1252 valid_page = 1;
d26a6aa0
MN
1253 buf[0] = 0x08; /* Page code */
1254 buf[1] = 10; /* Page length */
1255 memset(buf+2, 0, 10); /* None of the fields are changeable */
d5e2b67a
MN
1256
1257 if (!changeable_values) {
d26a6aa0
MN
1258 buf[2] = 0x04; /* Write cache enable, */
1259 /* Read cache not disabled */
1260 /* No cache retention priorities */
d5e2b67a
MN
1261 put_unaligned_be16(0xffff, &buf[4]);
1262 /* Don't disable prefetch */
1263 /* Minimum prefetch = 0 */
1264 put_unaligned_be16(0xffff, &buf[8]);
1265 /* Maximum prefetch */
1266 put_unaligned_be16(0xffff, &buf[10]);
1267 /* Maximum prefetch ceiling */
1268 }
1269 buf += 12;
1270 }
1271
b73af61e
MN
1272 /*
1273 * Check that a valid page was requested and the mode data length
1274 * isn't too long.
1275 */
d5e2b67a
MN
1276 len = buf - buf0;
1277 if (!valid_page || len > limit) {
1278 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1279 return -EINVAL;
1280 }
1281
1282 /* Store the mode data length */
0a6a717c 1283 if (mscmnd == MODE_SENSE)
d5e2b67a
MN
1284 buf0[0] = len - 1;
1285 else
1286 put_unaligned_be16(len - 2, buf0);
1287 return len;
1288}
1289
8ea864cf 1290static int do_start_stop(struct fsg_common *common)
d5e2b67a 1291{
31436a1a
FC
1292 struct fsg_lun *curlun = common->curlun;
1293 int loej, start;
1294
1295 if (!curlun) {
481e4929 1296 return -EINVAL;
31436a1a
FC
1297 } else if (!curlun->removable) {
1298 curlun->sense_data = SS_INVALID_COMMAND;
d5e2b67a 1299 return -EINVAL;
8876f5e7
MN
1300 } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
1301 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
31436a1a
FC
1302 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1303 return -EINVAL;
1304 }
1305
8876f5e7
MN
1306 loej = common->cmnd[4] & 0x02;
1307 start = common->cmnd[4] & 0x01;
31436a1a 1308
b73af61e
MN
1309 /*
1310 * Our emulation doesn't support mounting; the medium is
1311 * available for use as soon as it is loaded.
1312 */
8876f5e7 1313 if (start) {
31436a1a
FC
1314 if (!fsg_lun_is_open(curlun)) {
1315 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1316 return -EINVAL;
1317 }
8876f5e7 1318 return 0;
31436a1a 1319 }
8876f5e7
MN
1320
1321 /* Are we allowed to unload the media? */
1322 if (curlun->prevent_medium_removal) {
1323 LDBG(curlun, "unload attempt prevented\n");
1324 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1325 return -EINVAL;
1326 }
1327
1328 if (!loej)
1329 return 0;
1330
8876f5e7
MN
1331 up_read(&common->filesem);
1332 down_write(&common->filesem);
1333 fsg_lun_close(curlun);
1334 up_write(&common->filesem);
1335 down_read(&common->filesem);
1336
2b508002 1337 return 0;
d5e2b67a
MN
1338}
1339
8ea864cf 1340static int do_prevent_allow(struct fsg_common *common)
d5e2b67a 1341{
8ea864cf 1342 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
1343 int prevent;
1344
8ea864cf 1345 if (!common->curlun) {
481e4929 1346 return -EINVAL;
8ea864cf
MN
1347 } else if (!common->curlun->removable) {
1348 common->curlun->sense_data = SS_INVALID_COMMAND;
d5e2b67a
MN
1349 return -EINVAL;
1350 }
1351
8ea864cf
MN
1352 prevent = common->cmnd[4] & 0x01;
1353 if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
d5e2b67a
MN
1354 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1355 return -EINVAL;
1356 }
1357
1358 if (curlun->prevent_medium_removal && !prevent)
1359 fsg_lun_fsync_sub(curlun);
1360 curlun->prevent_medium_removal = prevent;
1361 return 0;
1362}
1363
8ea864cf 1364static int do_read_format_capacities(struct fsg_common *common,
d5e2b67a
MN
1365 struct fsg_buffhd *bh)
1366{
8ea864cf 1367 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
1368 u8 *buf = (u8 *) bh->buf;
1369
1370 buf[0] = buf[1] = buf[2] = 0;
d26a6aa0 1371 buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
d5e2b67a
MN
1372 buf += 4;
1373
1374 put_unaligned_be32(curlun->num_sectors, &buf[0]);
1375 /* Number of blocks */
3f565a36 1376 put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
d5e2b67a
MN
1377 buf[4] = 0x02; /* Current capacity */
1378 return 12;
1379}
1380
8ea864cf 1381static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
d5e2b67a 1382{
8ea864cf 1383 struct fsg_lun *curlun = common->curlun;
d5e2b67a
MN
1384
1385 /* We don't support MODE SELECT */
8ea864cf
MN
1386 if (curlun)
1387 curlun->sense_data = SS_INVALID_COMMAND;
d5e2b67a
MN
1388 return -EINVAL;
1389}
1390
1391
1392/*-------------------------------------------------------------------------*/
1393
1394static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1395{
1396 int rc;
1397
1398 rc = fsg_set_halt(fsg, fsg->bulk_in);
1399 if (rc == -EAGAIN)
1400 VDBG(fsg, "delayed bulk-in endpoint halt\n");
1401 while (rc != 0) {
1402 if (rc != -EAGAIN) {
1403 WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1404 rc = 0;
1405 break;
1406 }
1407
1408 /* Wait for a short time and then try again */
1409 if (msleep_interruptible(100) != 0)
1410 return -EINTR;
1411 rc = usb_ep_set_halt(fsg->bulk_in);
1412 }
1413 return rc;
1414}
1415
1416static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1417{
1418 int rc;
1419
1420 DBG(fsg, "bulk-in set wedge\n");
1421 rc = usb_ep_set_wedge(fsg->bulk_in);
1422 if (rc == -EAGAIN)
1423 VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1424 while (rc != 0) {
1425 if (rc != -EAGAIN) {
1426 WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1427 rc = 0;
1428 break;
1429 }
1430
1431 /* Wait for a short time and then try again */
1432 if (msleep_interruptible(100) != 0)
1433 return -EINTR;
1434 rc = usb_ep_set_wedge(fsg->bulk_in);
1435 }
1436 return rc;
1437}
1438
8ea864cf 1439static int throw_away_data(struct fsg_common *common)
d5e2b67a 1440{
225785ae 1441 struct fsg_buffhd *bh, *bh2;
d5e2b67a
MN
1442 u32 amount;
1443 int rc;
1444
8ea864cf
MN
1445 for (bh = common->next_buffhd_to_drain;
1446 bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1447 bh = common->next_buffhd_to_drain) {
d5e2b67a 1448
d5e2b67a 1449 /* Try to submit another request if we need one */
225785ae
AS
1450 bh2 = common->next_buffhd_to_fill;
1451 if (bh2->state == BUF_STATE_EMPTY &&
1452 common->usb_amount_left > 0) {
8ea864cf 1453 amount = min(common->usb_amount_left, FSG_BUFLEN);
d5e2b67a 1454
b73af61e 1455 /*
04eee25b
AS
1456 * Except at the end of the transfer, amount will be
1457 * equal to the buffer size, which is divisible by
b73af61e
MN
1458 * the bulk-out maxpacket size.
1459 */
225785ae
AS
1460 set_bulk_out_req_length(common, bh2, amount);
1461 if (!start_out_transfer(common, bh2))
b73af61e 1462 /* Dunno what to do if common->fsg is NULL */
8ea864cf 1463 return -EIO;
225785ae 1464 common->next_buffhd_to_fill = bh2->next;
8ea864cf 1465 common->usb_amount_left -= amount;
d5e2b67a
MN
1466 continue;
1467 }
1468
225785ae
AS
1469 /* Wait for the data to be received */
1470 rc = sleep_thread(common, false, bh);
d5e2b67a
MN
1471 if (rc)
1472 return rc;
225785ae
AS
1473
1474 /* Throw away the data in a filled buffer */
1475 bh->state = BUF_STATE_EMPTY;
1476 common->next_buffhd_to_drain = bh->next;
1477
1478 /* A short packet or an error ends everything */
1479 if (bh->outreq->actual < bh->bulk_out_intended_length ||
1480 bh->outreq->status != 0) {
1481 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1482 return -EINTR;
1483 }
d5e2b67a
MN
1484 }
1485 return 0;
1486}
1487
8ea864cf 1488static int finish_reply(struct fsg_common *common)
d5e2b67a 1489{
8ea864cf 1490 struct fsg_buffhd *bh = common->next_buffhd_to_fill;
d5e2b67a
MN
1491 int rc = 0;
1492
8ea864cf 1493 switch (common->data_dir) {
d5e2b67a 1494 case DATA_DIR_NONE:
d26a6aa0 1495 break; /* Nothing to send */
d5e2b67a 1496
b73af61e
MN
1497 /*
1498 * If we don't know whether the host wants to read or write,
d5e2b67a
MN
1499 * this must be CB or CBI with an unknown command. We mustn't
1500 * try to send or receive any data. So stall both bulk pipes
b73af61e
MN
1501 * if we can and wait for a reset.
1502 */
d5e2b67a 1503 case DATA_DIR_UNKNOWN:
8ea864cf
MN
1504 if (!common->can_stall) {
1505 /* Nothing */
1506 } else if (fsg_is_set(common)) {
1507 fsg_set_halt(common->fsg, common->fsg->bulk_out);
1508 rc = halt_bulk_in_endpoint(common->fsg);
1509 } else {
1510 /* Don't know what to do if common->fsg is NULL */
1511 rc = -EIO;
d5e2b67a
MN
1512 }
1513 break;
1514
1515 /* All but the last buffer of data must have already been sent */
1516 case DATA_DIR_TO_HOST:
8ea864cf 1517 if (common->data_size == 0) {
93bcf12e 1518 /* Nothing to send */
d5e2b67a 1519
ee81b3e0
AS
1520 /* Don't know what to do if common->fsg is NULL */
1521 } else if (!fsg_is_set(common)) {
1522 rc = -EIO;
1523
d5e2b67a 1524 /* If there's no residue, simply send the last buffer */
8ea864cf 1525 } else if (common->residue == 0) {
d5e2b67a 1526 bh->inreq->zero = 0;
fe52f792 1527 if (!start_in_transfer(common, bh))
8ea864cf
MN
1528 return -EIO;
1529 common->next_buffhd_to_fill = bh->next;
d5e2b67a 1530
b73af61e 1531 /*
ee81b3e0
AS
1532 * For Bulk-only, mark the end of the data with a short
1533 * packet. If we are allowed to stall, halt the bulk-in
1534 * endpoint. (Note: This violates the Bulk-Only Transport
1535 * specification, which requires us to pad the data if we
1536 * don't halt the endpoint. Presumably nobody will mind.)
b73af61e 1537 */
ee81b3e0 1538 } else {
93bcf12e 1539 bh->inreq->zero = 1;
fe52f792 1540 if (!start_in_transfer(common, bh))
8ea864cf
MN
1541 rc = -EIO;
1542 common->next_buffhd_to_fill = bh->next;
ee81b3e0 1543 if (common->can_stall)
8ea864cf 1544 rc = halt_bulk_in_endpoint(common->fsg);
d5e2b67a
MN
1545 }
1546 break;
1547
b73af61e
MN
1548 /*
1549 * We have processed all we want from the data the host has sent.
1550 * There may still be outstanding bulk-out requests.
1551 */
d5e2b67a 1552 case DATA_DIR_FROM_HOST:
8ea864cf 1553 if (common->residue == 0) {
d26a6aa0 1554 /* Nothing to receive */
d5e2b67a
MN
1555
1556 /* Did the host stop sending unexpectedly early? */
8ea864cf
MN
1557 } else if (common->short_packet_received) {
1558 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
d5e2b67a 1559 rc = -EINTR;
d5e2b67a 1560
b73af61e
MN
1561 /*
1562 * We haven't processed all the incoming data. Even though
d5e2b67a
MN
1563 * we may be allowed to stall, doing so would cause a race.
1564 * The controller may already have ACK'ed all the remaining
1565 * bulk-out packets, in which case the host wouldn't see a
1566 * STALL. Not realizing the endpoint was halted, it wouldn't
b73af61e
MN
1567 * clear the halt -- leading to problems later on.
1568 */
d5e2b67a 1569#if 0
8ea864cf
MN
1570 } else if (common->can_stall) {
1571 if (fsg_is_set(common))
1572 fsg_set_halt(common->fsg,
1573 common->fsg->bulk_out);
1574 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
d5e2b67a 1575 rc = -EINTR;
d5e2b67a
MN
1576#endif
1577
b73af61e
MN
1578 /*
1579 * We can't stall. Read in the excess data and throw it
1580 * all away.
1581 */
d26a6aa0 1582 } else {
8ea864cf 1583 rc = throw_away_data(common);
d26a6aa0 1584 }
d5e2b67a
MN
1585 break;
1586 }
1587 return rc;
1588}
1589
78db441d 1590static void send_status(struct fsg_common *common)
d5e2b67a 1591{
8ea864cf 1592 struct fsg_lun *curlun = common->curlun;
d5e2b67a 1593 struct fsg_buffhd *bh;
93bcf12e 1594 struct bulk_cs_wrap *csw;
d5e2b67a 1595 int rc;
775dafdc 1596 u8 status = US_BULK_STAT_OK;
d5e2b67a
MN
1597 u32 sd, sdinfo = 0;
1598
1599 /* Wait for the next buffer to become available */
8ea864cf 1600 bh = common->next_buffhd_to_fill;
225785ae
AS
1601 rc = sleep_thread(common, false, bh);
1602 if (rc)
1603 return;
d5e2b67a
MN
1604
1605 if (curlun) {
1606 sd = curlun->sense_data;
1607 sdinfo = curlun->sense_data_info;
8ea864cf 1608 } else if (common->bad_lun_okay)
d5e2b67a
MN
1609 sd = SS_NO_SENSE;
1610 else
1611 sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1612
8ea864cf
MN
1613 if (common->phase_error) {
1614 DBG(common, "sending phase-error status\n");
775dafdc 1615 status = US_BULK_STAT_PHASE;
d5e2b67a
MN
1616 sd = SS_INVALID_COMMAND;
1617 } else if (sd != SS_NO_SENSE) {
8ea864cf 1618 DBG(common, "sending command-failure status\n");
775dafdc 1619 status = US_BULK_STAT_FAIL;
8ea864cf 1620 VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
d5e2b67a
MN
1621 " info x%x\n",
1622 SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1623 }
1624
93bcf12e 1625 /* Store and send the Bulk-only CSW */
d26a6aa0 1626 csw = (void *)bh->buf;
d5e2b67a 1627
775dafdc 1628 csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
8ea864cf
MN
1629 csw->Tag = common->tag;
1630 csw->Residue = cpu_to_le32(common->residue);
93bcf12e 1631 csw->Status = status;
d5e2b67a 1632
775dafdc 1633 bh->inreq->length = US_BULK_CS_WRAP_LEN;
93bcf12e 1634 bh->inreq->zero = 0;
fe52f792 1635 if (!start_in_transfer(common, bh))
8ea864cf 1636 /* Don't know what to do if common->fsg is NULL */
78db441d 1637 return;
d5e2b67a 1638
8ea864cf 1639 common->next_buffhd_to_fill = bh->next;
78db441d 1640 return;
d5e2b67a
MN
1641}
1642
1643
1644/*-------------------------------------------------------------------------*/
1645
b73af61e
MN
1646/*
1647 * Check whether the command is properly formed and whether its data size
1648 * and direction agree with the values we already have.
1649 */
8ea864cf 1650static int check_command(struct fsg_common *common, int cmnd_size,
b73af61e
MN
1651 enum data_direction data_dir, unsigned int mask,
1652 int needs_medium, const char *name)
d5e2b67a
MN
1653{
1654 int i;
98f3a1b9 1655 unsigned int lun = common->cmnd[1] >> 5;
d5e2b67a
MN
1656 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1657 char hdlen[20];
1658 struct fsg_lun *curlun;
1659
d5e2b67a 1660 hdlen[0] = 0;
8ea864cf
MN
1661 if (common->data_dir != DATA_DIR_UNKNOWN)
1662 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
b73af61e 1663 common->data_size);
8ea864cf 1664 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
d26a6aa0 1665 name, cmnd_size, dirletter[(int) data_dir],
8ea864cf 1666 common->data_size_from_cmnd, common->cmnd_size, hdlen);
d5e2b67a 1667
b73af61e
MN
1668 /*
1669 * We can't reply at all until we know the correct data direction
1670 * and size.
1671 */
8ea864cf 1672 if (common->data_size_from_cmnd == 0)
d5e2b67a 1673 data_dir = DATA_DIR_NONE;
8ea864cf 1674 if (common->data_size < common->data_size_from_cmnd) {
b73af61e
MN
1675 /*
1676 * Host data size < Device data size is a phase error.
8ea864cf 1677 * Carry out the command, but only transfer as much as
b73af61e
MN
1678 * we are allowed.
1679 */
8ea864cf
MN
1680 common->data_size_from_cmnd = common->data_size;
1681 common->phase_error = 1;
d5e2b67a 1682 }
8ea864cf
MN
1683 common->residue = common->data_size;
1684 common->usb_amount_left = common->data_size;
d5e2b67a
MN
1685
1686 /* Conflicting data directions is a phase error */
b73af61e 1687 if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) {
8ea864cf 1688 common->phase_error = 1;
d5e2b67a
MN
1689 return -EINVAL;
1690 }
1691
1692 /* Verify the length of the command itself */
8ea864cf 1693 if (cmnd_size != common->cmnd_size) {
d5e2b67a 1694
b73af61e
MN
1695 /*
1696 * Special case workaround: There are plenty of buggy SCSI
d5e2b67a
MN
1697 * implementations. Many have issues with cbw->Length
1698 * field passing a wrong command size. For those cases we
1699 * always try to work around the problem by using the length
1700 * sent by the host side provided it is at least as large
1701 * as the correct command length.
1702 * Examples of such cases would be MS-Windows, which issues
1703 * REQUEST SENSE with cbw->Length == 12 where it should
1704 * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1705 * REQUEST SENSE with cbw->Length == 10 where it should
1706 * be 6 as well.
1707 */
8ea864cf
MN
1708 if (cmnd_size <= common->cmnd_size) {
1709 DBG(common, "%s is buggy! Expected length %d "
a41ae418 1710 "but we got %d\n", name,
8ea864cf
MN
1711 cmnd_size, common->cmnd_size);
1712 cmnd_size = common->cmnd_size;
d5e2b67a 1713 } else {
8ea864cf 1714 common->phase_error = 1;
d5e2b67a
MN
1715 return -EINVAL;
1716 }
1717 }
1718
1719 /* Check that the LUN values are consistent */
8ea864cf 1720 if (common->lun != lun)
98f3a1b9 1721 DBG(common, "using LUN %u from CBW, not LUN %u from CDB\n",
8ea864cf 1722 common->lun, lun);
d5e2b67a
MN
1723
1724 /* Check the LUN */
144974e7
YL
1725 curlun = common->curlun;
1726 if (curlun) {
0a6a717c 1727 if (common->cmnd[0] != REQUEST_SENSE) {
d5e2b67a
MN
1728 curlun->sense_data = SS_NO_SENSE;
1729 curlun->sense_data_info = 0;
1730 curlun->info_valid = 0;
1731 }
1732 } else {
8ea864cf 1733 common->bad_lun_okay = 0;
d5e2b67a 1734
b73af61e
MN
1735 /*
1736 * INQUIRY and REQUEST SENSE commands are explicitly allowed
1737 * to use unsupported LUNs; all others may not.
1738 */
0a6a717c
MN
1739 if (common->cmnd[0] != INQUIRY &&
1740 common->cmnd[0] != REQUEST_SENSE) {
98f3a1b9 1741 DBG(common, "unsupported LUN %u\n", common->lun);
d5e2b67a
MN
1742 return -EINVAL;
1743 }
1744 }
1745
b73af61e
MN
1746 /*
1747 * If a unit attention condition exists, only INQUIRY and
1748 * REQUEST SENSE commands are allowed; anything else must fail.
1749 */
d5e2b67a 1750 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
b73af61e
MN
1751 common->cmnd[0] != INQUIRY &&
1752 common->cmnd[0] != REQUEST_SENSE) {
d5e2b67a
MN
1753 curlun->sense_data = curlun->unit_attention_data;
1754 curlun->unit_attention_data = SS_NO_SENSE;
1755 return -EINVAL;
1756 }
1757
1758 /* Check that only command bytes listed in the mask are non-zero */
8ea864cf 1759 common->cmnd[1] &= 0x1f; /* Mask away the LUN */
d5e2b67a 1760 for (i = 1; i < cmnd_size; ++i) {
8ea864cf 1761 if (common->cmnd[i] && !(mask & (1 << i))) {
d5e2b67a
MN
1762 if (curlun)
1763 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1764 return -EINVAL;
1765 }
1766 }
1767
1768 /* If the medium isn't mounted and the command needs to access
1769 * it, return an error. */
1770 if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
1771 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1772 return -EINVAL;
1773 }
1774
1775 return 0;
1776}
1777
144974e7
YL
1778/* wrapper of check_command for data size in blocks handling */
1779static int check_command_size_in_blocks(struct fsg_common *common,
1780 int cmnd_size, enum data_direction data_dir,
1781 unsigned int mask, int needs_medium, const char *name)
1782{
1783 if (common->curlun)
1784 common->data_size_from_cmnd <<= common->curlun->blkbits;
1785 return check_command(common, cmnd_size, data_dir,
1786 mask, needs_medium, name);
1787}
1788
8ea864cf 1789static int do_scsi_command(struct fsg_common *common)
d5e2b67a
MN
1790{
1791 struct fsg_buffhd *bh;
1792 int rc;
1793 int reply = -EINVAL;
1794 int i;
1795 static char unknown[16];
1796
8ea864cf 1797 dump_cdb(common);
d5e2b67a
MN
1798
1799 /* Wait for the next buffer to become available for data or status */
8ea864cf
MN
1800 bh = common->next_buffhd_to_fill;
1801 common->next_buffhd_to_drain = bh;
225785ae
AS
1802 rc = sleep_thread(common, false, bh);
1803 if (rc)
1804 return rc;
1805
8ea864cf
MN
1806 common->phase_error = 0;
1807 common->short_packet_received = 0;
d5e2b67a 1808
8ea864cf
MN
1809 down_read(&common->filesem); /* We're using the backing file */
1810 switch (common->cmnd[0]) {
d5e2b67a 1811
0a6a717c 1812 case INQUIRY:
8ea864cf
MN
1813 common->data_size_from_cmnd = common->cmnd[4];
1814 reply = check_command(common, 6, DATA_DIR_TO_HOST,
d26a6aa0
MN
1815 (1<<4), 0,
1816 "INQUIRY");
1817 if (reply == 0)
8ea864cf 1818 reply = do_inquiry(common, bh);
d5e2b67a
MN
1819 break;
1820
0a6a717c 1821 case MODE_SELECT:
8ea864cf
MN
1822 common->data_size_from_cmnd = common->cmnd[4];
1823 reply = check_command(common, 6, DATA_DIR_FROM_HOST,
d26a6aa0
MN
1824 (1<<1) | (1<<4), 0,
1825 "MODE SELECT(6)");
1826 if (reply == 0)
8ea864cf 1827 reply = do_mode_select(common, bh);
d5e2b67a
MN
1828 break;
1829
0a6a717c 1830 case MODE_SELECT_10:
8ea864cf
MN
1831 common->data_size_from_cmnd =
1832 get_unaligned_be16(&common->cmnd[7]);
1833 reply = check_command(common, 10, DATA_DIR_FROM_HOST,
d26a6aa0
MN
1834 (1<<1) | (3<<7), 0,
1835 "MODE SELECT(10)");
1836 if (reply == 0)
8ea864cf 1837 reply = do_mode_select(common, bh);
d5e2b67a
MN
1838 break;
1839
0a6a717c 1840 case MODE_SENSE:
8ea864cf
MN
1841 common->data_size_from_cmnd = common->cmnd[4];
1842 reply = check_command(common, 6, DATA_DIR_TO_HOST,
d26a6aa0
MN
1843 (1<<1) | (1<<2) | (1<<4), 0,
1844 "MODE SENSE(6)");
1845 if (reply == 0)
8ea864cf 1846 reply = do_mode_sense(common, bh);
d5e2b67a
MN
1847 break;
1848
0a6a717c 1849 case MODE_SENSE_10:
8ea864cf
MN
1850 common->data_size_from_cmnd =
1851 get_unaligned_be16(&common->cmnd[7]);
1852 reply = check_command(common, 10, DATA_DIR_TO_HOST,
d26a6aa0
MN
1853 (1<<1) | (1<<2) | (3<<7), 0,
1854 "MODE SENSE(10)");
1855 if (reply == 0)
8ea864cf 1856 reply = do_mode_sense(common, bh);
d5e2b67a
MN
1857 break;
1858
0a6a717c 1859 case ALLOW_MEDIUM_REMOVAL:
8ea864cf
MN
1860 common->data_size_from_cmnd = 0;
1861 reply = check_command(common, 6, DATA_DIR_NONE,
d26a6aa0
MN
1862 (1<<4), 0,
1863 "PREVENT-ALLOW MEDIUM REMOVAL");
1864 if (reply == 0)
8ea864cf 1865 reply = do_prevent_allow(common);
d5e2b67a
MN
1866 break;
1867
0a6a717c 1868 case READ_6:
8ea864cf 1869 i = common->cmnd[4];
144974e7
YL
1870 common->data_size_from_cmnd = (i == 0) ? 256 : i;
1871 reply = check_command_size_in_blocks(common, 6,
1872 DATA_DIR_TO_HOST,
d26a6aa0
MN
1873 (7<<1) | (1<<4), 1,
1874 "READ(6)");
1875 if (reply == 0)
8ea864cf 1876 reply = do_read(common);
d5e2b67a
MN
1877 break;
1878
0a6a717c 1879 case READ_10:
8ea864cf 1880 common->data_size_from_cmnd =
144974e7
YL
1881 get_unaligned_be16(&common->cmnd[7]);
1882 reply = check_command_size_in_blocks(common, 10,
1883 DATA_DIR_TO_HOST,
d26a6aa0
MN
1884 (1<<1) | (0xf<<2) | (3<<7), 1,
1885 "READ(10)");
1886 if (reply == 0)
8ea864cf 1887 reply = do_read(common);
d5e2b67a
MN
1888 break;
1889
0a6a717c 1890 case READ_12:
8ea864cf 1891 common->data_size_from_cmnd =
144974e7
YL
1892 get_unaligned_be32(&common->cmnd[6]);
1893 reply = check_command_size_in_blocks(common, 12,
1894 DATA_DIR_TO_HOST,
d26a6aa0
MN
1895 (1<<1) | (0xf<<2) | (0xf<<6), 1,
1896 "READ(12)");
1897 if (reply == 0)
8ea864cf 1898 reply = do_read(common);
d5e2b67a
MN
1899 break;
1900
0a6a717c 1901 case READ_CAPACITY:
8ea864cf
MN
1902 common->data_size_from_cmnd = 8;
1903 reply = check_command(common, 10, DATA_DIR_TO_HOST,
d26a6aa0
MN
1904 (0xf<<2) | (1<<8), 1,
1905 "READ CAPACITY");
1906 if (reply == 0)
8ea864cf 1907 reply = do_read_capacity(common, bh);
d5e2b67a
MN
1908 break;
1909
0a6a717c 1910 case READ_HEADER:
8ea864cf 1911 if (!common->curlun || !common->curlun->cdrom)
d5e2b67a 1912 goto unknown_cmnd;
8ea864cf
MN
1913 common->data_size_from_cmnd =
1914 get_unaligned_be16(&common->cmnd[7]);
1915 reply = check_command(common, 10, DATA_DIR_TO_HOST,
d26a6aa0
MN
1916 (3<<7) | (0x1f<<1), 1,
1917 "READ HEADER");
1918 if (reply == 0)
8ea864cf 1919 reply = do_read_header(common, bh);
d5e2b67a
MN
1920 break;
1921
0a6a717c 1922 case READ_TOC:
8ea864cf 1923 if (!common->curlun || !common->curlun->cdrom)
d5e2b67a 1924 goto unknown_cmnd;
8ea864cf
MN
1925 common->data_size_from_cmnd =
1926 get_unaligned_be16(&common->cmnd[7]);
1927 reply = check_command(common, 10, DATA_DIR_TO_HOST,
d26a6aa0
MN
1928 (7<<6) | (1<<1), 1,
1929 "READ TOC");
1930 if (reply == 0)
8ea864cf 1931 reply = do_read_toc(common, bh);
d5e2b67a
MN
1932 break;
1933
0a6a717c 1934 case READ_FORMAT_CAPACITIES:
8ea864cf
MN
1935 common->data_size_from_cmnd =
1936 get_unaligned_be16(&common->cmnd[7]);
1937 reply = check_command(common, 10, DATA_DIR_TO_HOST,
d26a6aa0
MN
1938 (3<<7), 1,
1939 "READ FORMAT CAPACITIES");
1940 if (reply == 0)
8ea864cf 1941 reply = do_read_format_capacities(common, bh);
d5e2b67a
MN
1942 break;
1943
0a6a717c 1944 case REQUEST_SENSE:
8ea864cf
MN
1945 common->data_size_from_cmnd = common->cmnd[4];
1946 reply = check_command(common, 6, DATA_DIR_TO_HOST,
d26a6aa0
MN
1947 (1<<4), 0,
1948 "REQUEST SENSE");
1949 if (reply == 0)
8ea864cf 1950 reply = do_request_sense(common, bh);
d5e2b67a
MN
1951 break;
1952
0a6a717c 1953 case START_STOP:
8ea864cf
MN
1954 common->data_size_from_cmnd = 0;
1955 reply = check_command(common, 6, DATA_DIR_NONE,
d26a6aa0
MN
1956 (1<<1) | (1<<4), 0,
1957 "START-STOP UNIT");
1958 if (reply == 0)
8ea864cf 1959 reply = do_start_stop(common);
d5e2b67a
MN
1960 break;
1961
0a6a717c 1962 case SYNCHRONIZE_CACHE:
8ea864cf
MN
1963 common->data_size_from_cmnd = 0;
1964 reply = check_command(common, 10, DATA_DIR_NONE,
d26a6aa0
MN
1965 (0xf<<2) | (3<<7), 1,
1966 "SYNCHRONIZE CACHE");
1967 if (reply == 0)
8ea864cf 1968 reply = do_synchronize_cache(common);
d5e2b67a
MN
1969 break;
1970
0a6a717c 1971 case TEST_UNIT_READY:
8ea864cf
MN
1972 common->data_size_from_cmnd = 0;
1973 reply = check_command(common, 6, DATA_DIR_NONE,
d5e2b67a
MN
1974 0, 1,
1975 "TEST UNIT READY");
1976 break;
1977
b73af61e
MN
1978 /*
1979 * Although optional, this command is used by MS-Windows. We
1980 * support a minimal version: BytChk must be 0.
1981 */
0a6a717c 1982 case VERIFY:
8ea864cf
MN
1983 common->data_size_from_cmnd = 0;
1984 reply = check_command(common, 10, DATA_DIR_NONE,
d26a6aa0
MN
1985 (1<<1) | (0xf<<2) | (3<<7), 1,
1986 "VERIFY");
1987 if (reply == 0)
8ea864cf 1988 reply = do_verify(common);
d5e2b67a
MN
1989 break;
1990
0a6a717c 1991 case WRITE_6:
8ea864cf 1992 i = common->cmnd[4];
144974e7
YL
1993 common->data_size_from_cmnd = (i == 0) ? 256 : i;
1994 reply = check_command_size_in_blocks(common, 6,
1995 DATA_DIR_FROM_HOST,
d26a6aa0
MN
1996 (7<<1) | (1<<4), 1,
1997 "WRITE(6)");
1998 if (reply == 0)
8ea864cf 1999 reply = do_write(common);
d5e2b67a
MN
2000 break;
2001
0a6a717c 2002 case WRITE_10:
8ea864cf 2003 common->data_size_from_cmnd =
144974e7
YL
2004 get_unaligned_be16(&common->cmnd[7]);
2005 reply = check_command_size_in_blocks(common, 10,
2006 DATA_DIR_FROM_HOST,
d26a6aa0
MN
2007 (1<<1) | (0xf<<2) | (3<<7), 1,
2008 "WRITE(10)");
2009 if (reply == 0)
8ea864cf 2010 reply = do_write(common);
d5e2b67a
MN
2011 break;
2012
0a6a717c 2013 case WRITE_12:
8ea864cf 2014 common->data_size_from_cmnd =
144974e7
YL
2015 get_unaligned_be32(&common->cmnd[6]);
2016 reply = check_command_size_in_blocks(common, 12,
2017 DATA_DIR_FROM_HOST,
d26a6aa0
MN
2018 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2019 "WRITE(12)");
2020 if (reply == 0)
8ea864cf 2021 reply = do_write(common);
d5e2b67a
MN
2022 break;
2023
b73af61e
MN
2024 /*
2025 * Some mandatory commands that we recognize but don't implement.
d5e2b67a
MN
2026 * They don't mean much in this setting. It's left as an exercise
2027 * for anyone interested to implement RESERVE and RELEASE in terms
b73af61e
MN
2028 * of Posix locks.
2029 */
0a6a717c
MN
2030 case FORMAT_UNIT:
2031 case RELEASE:
2032 case RESERVE:
2033 case SEND_DIAGNOSTIC:
d26a6aa0 2034 /* Fall through */
d5e2b67a
MN
2035
2036 default:
d26a6aa0 2037unknown_cmnd:
8ea864cf
MN
2038 common->data_size_from_cmnd = 0;
2039 sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2040 reply = check_command(common, common->cmnd_size,
c85dcdac 2041 DATA_DIR_UNKNOWN, ~0, 0, unknown);
d26a6aa0 2042 if (reply == 0) {
8ea864cf 2043 common->curlun->sense_data = SS_INVALID_COMMAND;
d5e2b67a
MN
2044 reply = -EINVAL;
2045 }
2046 break;
2047 }
8ea864cf 2048 up_read(&common->filesem);
d5e2b67a
MN
2049
2050 if (reply == -EINTR || signal_pending(current))
2051 return -EINTR;
2052
2053 /* Set up the single reply buffer for finish_reply() */
2054 if (reply == -EINVAL)
d26a6aa0 2055 reply = 0; /* Error reply length */
8ea864cf 2056 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
b73af61e 2057 reply = min((u32)reply, common->data_size_from_cmnd);
d5e2b67a
MN
2058 bh->inreq->length = reply;
2059 bh->state = BUF_STATE_FULL;
8ea864cf 2060 common->residue -= reply;
d26a6aa0 2061 } /* Otherwise it's already set */
d5e2b67a
MN
2062
2063 return 0;
2064}
2065
2066
2067/*-------------------------------------------------------------------------*/
2068
2069static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2070{
8ea864cf 2071 struct usb_request *req = bh->outreq;
7ac4704c 2072 struct bulk_cb_wrap *cbw = req->buf;
8ea864cf 2073 struct fsg_common *common = fsg->common;
d5e2b67a
MN
2074
2075 /* Was this a real packet? Should it be ignored? */
2076 if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2077 return -EINVAL;
2078
2079 /* Is the CBW valid? */
775dafdc 2080 if (req->actual != US_BULK_CB_WRAP_LEN ||
d5e2b67a 2081 cbw->Signature != cpu_to_le32(
775dafdc 2082 US_BULK_CB_SIGN)) {
d5e2b67a
MN
2083 DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2084 req->actual,
2085 le32_to_cpu(cbw->Signature));
2086
b73af61e
MN
2087 /*
2088 * The Bulk-only spec says we MUST stall the IN endpoint
d5e2b67a
MN
2089 * (6.6.1), so it's unavoidable. It also says we must
2090 * retain this state until the next reset, but there's
2091 * no way to tell the controller driver it should ignore
2092 * Clear-Feature(HALT) requests.
2093 *
2094 * We aren't required to halt the OUT endpoint; instead
2095 * we can simply accept and discard any data received
b73af61e
MN
2096 * until the next reset.
2097 */
d5e2b67a
MN
2098 wedge_bulk_in_endpoint(fsg);
2099 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2100 return -EINVAL;
2101 }
2102
2103 /* Is the CBW meaningful? */
dd02ea5a
KO
2104 if (cbw->Lun >= ARRAY_SIZE(common->luns) ||
2105 cbw->Flags & ~US_BULK_FLAG_IN || cbw->Length <= 0 ||
2106 cbw->Length > MAX_COMMAND_SIZE) {
d5e2b67a
MN
2107 DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2108 "cmdlen %u\n",
2109 cbw->Lun, cbw->Flags, cbw->Length);
2110
b73af61e
MN
2111 /*
2112 * We can do anything we want here, so let's stall the
2113 * bulk pipes if we are allowed to.
2114 */
8ea864cf 2115 if (common->can_stall) {
d5e2b67a
MN
2116 fsg_set_halt(fsg, fsg->bulk_out);
2117 halt_bulk_in_endpoint(fsg);
2118 }
2119 return -EINVAL;
2120 }
2121
2122 /* Save the command for later */
8ea864cf
MN
2123 common->cmnd_size = cbw->Length;
2124 memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
775dafdc 2125 if (cbw->Flags & US_BULK_FLAG_IN)
8ea864cf 2126 common->data_dir = DATA_DIR_TO_HOST;
d5e2b67a 2127 else
8ea864cf
MN
2128 common->data_dir = DATA_DIR_FROM_HOST;
2129 common->data_size = le32_to_cpu(cbw->DataTransferLength);
2130 if (common->data_size == 0)
2131 common->data_dir = DATA_DIR_NONE;
2132 common->lun = cbw->Lun;
dd02ea5a 2133 if (common->lun < ARRAY_SIZE(common->luns))
8b903fd7 2134 common->curlun = common->luns[common->lun];
144974e7
YL
2135 else
2136 common->curlun = NULL;
8ea864cf 2137 common->tag = cbw->Tag;
d5e2b67a
MN
2138 return 0;
2139}
2140
8ea864cf 2141static int get_next_command(struct fsg_common *common)
d5e2b67a
MN
2142{
2143 struct fsg_buffhd *bh;
2144 int rc = 0;
2145
93bcf12e 2146 /* Wait for the next buffer to become available */
8ea864cf 2147 bh = common->next_buffhd_to_fill;
225785ae
AS
2148 rc = sleep_thread(common, true, bh);
2149 if (rc)
2150 return rc;
d5e2b67a 2151
93bcf12e 2152 /* Queue a request to read a Bulk-only CBW */
775dafdc 2153 set_bulk_out_req_length(common, bh, US_BULK_CB_WRAP_LEN);
fe52f792 2154 if (!start_out_transfer(common, bh))
8ea864cf
MN
2155 /* Don't know what to do if common->fsg is NULL */
2156 return -EIO;
d5e2b67a 2157
b73af61e
MN
2158 /*
2159 * We will drain the buffer in software, which means we
93bcf12e 2160 * can reuse it for the next filling. No need to advance
b73af61e
MN
2161 * next_buffhd_to_fill.
2162 */
d5e2b67a 2163
93bcf12e 2164 /* Wait for the CBW to arrive */
225785ae
AS
2165 rc = sleep_thread(common, true, bh);
2166 if (rc)
2167 return rc;
2168
8ea864cf 2169 rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
93bcf12e
MN
2170 bh->state = BUF_STATE_EMPTY;
2171
d5e2b67a
MN
2172 return rc;
2173}
2174
2175
2176/*-------------------------------------------------------------------------*/
2177
8ea864cf 2178static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
d5e2b67a
MN
2179 struct usb_request **preq)
2180{
2181 *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2182 if (*preq)
2183 return 0;
8ea864cf 2184 ERROR(common, "can't allocate request for %s\n", ep->name);
d5e2b67a
MN
2185 return -ENOMEM;
2186}
2187
b894f60a
MN
2188/* Reset interface setting and re-init endpoint state (toggle etc). */
2189static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg)
d5e2b67a 2190{
b894f60a
MN
2191 struct fsg_dev *fsg;
2192 int i, rc = 0;
d5e2b67a 2193
8ea864cf
MN
2194 if (common->running)
2195 DBG(common, "reset interface\n");
d5e2b67a
MN
2196
2197reset:
2198 /* Deallocate the requests */
b894f60a
MN
2199 if (common->fsg) {
2200 fsg = common->fsg;
8ea864cf 2201
6fdc5dd2 2202 for (i = 0; i < common->fsg_num_buffers; ++i) {
8ea864cf 2203 struct fsg_buffhd *bh = &common->buffhds[i];
d5e2b67a 2204
8ea864cf
MN
2205 if (bh->inreq) {
2206 usb_ep_free_request(fsg->bulk_in, bh->inreq);
2207 bh->inreq = NULL;
2208 }
2209 if (bh->outreq) {
2210 usb_ep_free_request(fsg->bulk_out, bh->outreq);
2211 bh->outreq = NULL;
2212 }
d5e2b67a 2213 }
8ea864cf
MN
2214
2215 /* Disable the endpoints */
2216 if (fsg->bulk_in_enabled) {
2217 usb_ep_disable(fsg->bulk_in);
2218 fsg->bulk_in_enabled = 0;
2219 }
2220 if (fsg->bulk_out_enabled) {
2221 usb_ep_disable(fsg->bulk_out);
2222 fsg->bulk_out_enabled = 0;
d5e2b67a 2223 }
d5e2b67a 2224
b894f60a
MN
2225 common->fsg = NULL;
2226 wake_up(&common->fsg_wait);
d5e2b67a 2227 }
d5e2b67a 2228
8ea864cf 2229 common->running = 0;
b894f60a 2230 if (!new_fsg || rc)
d5e2b67a
MN
2231 return rc;
2232
b894f60a
MN
2233 common->fsg = new_fsg;
2234 fsg = common->fsg;
d5e2b67a 2235
b894f60a 2236 /* Enable the endpoints */
ea2a1df7
TB
2237 rc = config_ep_by_speed(common->gadget, &(fsg->function), fsg->bulk_in);
2238 if (rc)
2239 goto reset;
2240 rc = usb_ep_enable(fsg->bulk_in);
b894f60a
MN
2241 if (rc)
2242 goto reset;
ea2a1df7 2243 fsg->bulk_in->driver_data = common;
b894f60a 2244 fsg->bulk_in_enabled = 1;
8ea864cf 2245
ea2a1df7
TB
2246 rc = config_ep_by_speed(common->gadget, &(fsg->function),
2247 fsg->bulk_out);
2248 if (rc)
2249 goto reset;
2250 rc = usb_ep_enable(fsg->bulk_out);
b894f60a
MN
2251 if (rc)
2252 goto reset;
ea2a1df7 2253 fsg->bulk_out->driver_data = common;
b894f60a 2254 fsg->bulk_out_enabled = 1;
29cc8897 2255 common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc);
b894f60a
MN
2256 clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2257
2258 /* Allocate the requests */
6fdc5dd2 2259 for (i = 0; i < common->fsg_num_buffers; ++i) {
b894f60a
MN
2260 struct fsg_buffhd *bh = &common->buffhds[i];
2261
2262 rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
8ea864cf 2263 if (rc)
d5e2b67a 2264 goto reset;
b894f60a 2265 rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
8ea864cf 2266 if (rc)
d5e2b67a 2267 goto reset;
b894f60a
MN
2268 bh->inreq->buf = bh->outreq->buf = bh->buf;
2269 bh->inreq->context = bh->outreq->context = bh;
2270 bh->inreq->complete = bulk_in_complete;
2271 bh->outreq->complete = bulk_out_complete;
8ea864cf 2272 }
d5e2b67a 2273
b894f60a 2274 common->running = 1;
dd02ea5a 2275 for (i = 0; i < ARRAY_SIZE(common->luns); ++i)
8b903fd7
AP
2276 if (common->luns[i])
2277 common->luns[i]->unit_attention_data =
2278 SS_RESET_OCCURRED;
d5e2b67a
MN
2279 return rc;
2280}
2281
2282
d23b0f08
MN
2283/****************************** ALT CONFIGS ******************************/
2284
d23b0f08
MN
2285static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2286{
2287 struct fsg_dev *fsg = fsg_from_func(f);
b894f60a 2288 fsg->common->new_fsg = fsg;
8ea864cf 2289 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
95ed3236 2290 return USB_GADGET_DELAYED_STATUS;
d23b0f08
MN
2291}
2292
2293static void fsg_disable(struct usb_function *f)
2294{
2295 struct fsg_dev *fsg = fsg_from_func(f);
b894f60a 2296 fsg->common->new_fsg = NULL;
8ea864cf 2297 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
d23b0f08
MN
2298}
2299
2300
d5e2b67a
MN
2301/*-------------------------------------------------------------------------*/
2302
8ea864cf 2303static void handle_exception(struct fsg_common *common)
d5e2b67a 2304{
d5e2b67a 2305 int i;
d5e2b67a
MN
2306 struct fsg_buffhd *bh;
2307 enum fsg_state old_state;
d5e2b67a
MN
2308 struct fsg_lun *curlun;
2309 unsigned int exception_req_tag;
d5e2b67a 2310
b73af61e
MN
2311 /*
2312 * Clear the existing signals. Anything but SIGUSR1 is converted
2313 * into a high-priority EXIT exception.
2314 */
d5e2b67a 2315 for (;;) {
be0e6f29 2316 int sig = kernel_dequeue_signal(NULL);
d5e2b67a
MN
2317 if (!sig)
2318 break;
2319 if (sig != SIGUSR1) {
78db441d 2320 spin_lock_irq(&common->lock);
8ea864cf
MN
2321 if (common->state < FSG_STATE_EXIT)
2322 DBG(common, "Main thread exiting on signal\n");
78db441d
AS
2323 common->state = FSG_STATE_EXIT;
2324 spin_unlock_irq(&common->lock);
d5e2b67a
MN
2325 }
2326 }
2327
2328 /* Cancel all the pending transfers */
b894f60a 2329 if (likely(common->fsg)) {
6fdc5dd2 2330 for (i = 0; i < common->fsg_num_buffers; ++i) {
8ea864cf 2331 bh = &common->buffhds[i];
225785ae 2332 if (bh->state == BUF_STATE_SENDING)
8ea864cf 2333 usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
225785ae 2334 if (bh->state == BUF_STATE_RECEIVING)
8ea864cf
MN
2335 usb_ep_dequeue(common->fsg->bulk_out,
2336 bh->outreq);
d5e2b67a 2337
225785ae
AS
2338 /* Wait for a transfer to become idle */
2339 if (sleep_thread(common, false, bh))
8ea864cf
MN
2340 return;
2341 }
2342
2343 /* Clear out the controller's fifos */
2344 if (common->fsg->bulk_in_enabled)
2345 usb_ep_fifo_flush(common->fsg->bulk_in);
2346 if (common->fsg->bulk_out_enabled)
2347 usb_ep_fifo_flush(common->fsg->bulk_out);
2348 }
d5e2b67a 2349
b73af61e
MN
2350 /*
2351 * Reset the I/O buffer states and pointers, the SCSI
2352 * state, and the exception. Then invoke the handler.
2353 */
8ea864cf 2354 spin_lock_irq(&common->lock);
d5e2b67a 2355
6fdc5dd2 2356 for (i = 0; i < common->fsg_num_buffers; ++i) {
8ea864cf 2357 bh = &common->buffhds[i];
d5e2b67a
MN
2358 bh->state = BUF_STATE_EMPTY;
2359 }
8ea864cf
MN
2360 common->next_buffhd_to_fill = &common->buffhds[0];
2361 common->next_buffhd_to_drain = &common->buffhds[0];
2362 exception_req_tag = common->exception_req_tag;
8ea864cf 2363 old_state = common->state;
78db441d 2364 common->state = FSG_STATE_NORMAL;
d5e2b67a 2365
78db441d 2366 if (old_state != FSG_STATE_ABORT_BULK_OUT) {
dd02ea5a 2367 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) {
8b903fd7
AP
2368 curlun = common->luns[i];
2369 if (!curlun)
2370 continue;
d5e2b67a 2371 curlun->prevent_medium_removal = 0;
d26a6aa0
MN
2372 curlun->sense_data = SS_NO_SENSE;
2373 curlun->unit_attention_data = SS_NO_SENSE;
d5e2b67a
MN
2374 curlun->sense_data_info = 0;
2375 curlun->info_valid = 0;
2376 }
d5e2b67a 2377 }
8ea864cf 2378 spin_unlock_irq(&common->lock);
d5e2b67a
MN
2379
2380 /* Carry out any extra actions required for the exception */
2381 switch (old_state) {
78db441d
AS
2382 case FSG_STATE_NORMAL:
2383 break;
2384
d5e2b67a 2385 case FSG_STATE_ABORT_BULK_OUT:
8ea864cf 2386 send_status(common);
d5e2b67a
MN
2387 break;
2388
78db441d 2389 case FSG_STATE_PROTOCOL_RESET:
b73af61e
MN
2390 /*
2391 * In case we were forced against our will to halt a
d5e2b67a 2392 * bulk endpoint, clear the halt now. (The SuperH UDC
b73af61e
MN
2393 * requires this.)
2394 */
8ea864cf
MN
2395 if (!fsg_is_set(common))
2396 break;
2397 if (test_and_clear_bit(IGNORE_BULK_OUT,
2398 &common->fsg->atomic_bitflags))
2399 usb_ep_clear_halt(common->fsg->bulk_in);
d5e2b67a 2400
8ea864cf
MN
2401 if (common->ep0_req_tag == exception_req_tag)
2402 ep0_queue(common); /* Complete the status stage */
d5e2b67a 2403
b73af61e
MN
2404 /*
2405 * Technically this should go here, but it would only be
d5e2b67a 2406 * a waste of time. Ditto for the INTERFACE_CHANGE and
b73af61e
MN
2407 * CONFIG_CHANGE cases.
2408 */
dd02ea5a 2409 /* for (i = 0; i < common->ARRAY_SIZE(common->luns); ++i) */
8b903fd7
AP
2410 /* if (common->luns[i]) */
2411 /* common->luns[i]->unit_attention_data = */
2412 /* SS_RESET_OCCURRED; */
d5e2b67a
MN
2413 break;
2414
d5e2b67a 2415 case FSG_STATE_CONFIG_CHANGE:
b894f60a 2416 do_set_interface(common, common->new_fsg);
95ed3236
RQ
2417 if (common->new_fsg)
2418 usb_composite_setup_continue(common->cdev);
d5e2b67a
MN
2419 break;
2420
d5e2b67a 2421 case FSG_STATE_EXIT:
b894f60a 2422 do_set_interface(common, NULL); /* Free resources */
8ea864cf
MN
2423 spin_lock_irq(&common->lock);
2424 common->state = FSG_STATE_TERMINATED; /* Stop the thread */
2425 spin_unlock_irq(&common->lock);
d5e2b67a 2426 break;
d23b0f08 2427
78db441d 2428 case FSG_STATE_TERMINATED:
d23b0f08 2429 break;
d5e2b67a
MN
2430 }
2431}
2432
2433
2434/*-------------------------------------------------------------------------*/
2435
8ea864cf 2436static int fsg_main_thread(void *common_)
d5e2b67a 2437{
8ea864cf 2438 struct fsg_common *common = common_;
1fbbb78f 2439 int i;
d5e2b67a 2440
b73af61e
MN
2441 /*
2442 * Allow the thread to be killed by a signal, but set the signal mask
2443 * to block everything but INT, TERM, KILL, and USR1.
2444 */
d5e2b67a
MN
2445 allow_signal(SIGINT);
2446 allow_signal(SIGTERM);
2447 allow_signal(SIGKILL);
2448 allow_signal(SIGUSR1);
2449
2450 /* Allow the thread to be frozen */
2451 set_freezable();
2452
d5e2b67a 2453 /* The main loop */
8ea864cf
MN
2454 while (common->state != FSG_STATE_TERMINATED) {
2455 if (exception_in_progress(common) || signal_pending(current)) {
2456 handle_exception(common);
d5e2b67a
MN
2457 continue;
2458 }
2459
8ea864cf 2460 if (!common->running) {
225785ae 2461 sleep_thread(common, true, NULL);
d5e2b67a
MN
2462 continue;
2463 }
2464
78db441d 2465 if (get_next_command(common) || exception_in_progress(common))
d5e2b67a 2466 continue;
78db441d 2467 if (do_scsi_command(common) || exception_in_progress(common))
d5e2b67a 2468 continue;
78db441d 2469 if (finish_reply(common) || exception_in_progress(common))
d5e2b67a 2470 continue;
78db441d 2471 send_status(common);
d23b0f08 2472 }
d5e2b67a 2473
8ea864cf
MN
2474 spin_lock_irq(&common->lock);
2475 common->thread_task = NULL;
2476 spin_unlock_irq(&common->lock);
d5e2b67a 2477
1fbbb78f 2478 /* Eject media from all LUNs */
7f1ee826 2479
1fbbb78f
AS
2480 down_write(&common->filesem);
2481 for (i = 0; i < ARRAY_SIZE(common->luns); i++) {
2482 struct fsg_lun *curlun = common->luns[i];
7f1ee826 2483
1fbbb78f 2484 if (curlun && fsg_lun_is_open(curlun))
7f1ee826 2485 fsg_lun_close(curlun);
7f1ee826 2486 }
1fbbb78f 2487 up_write(&common->filesem);
d5e2b67a 2488
00cb636e 2489 /* Let fsg_unbind() know the thread has exited */
8ea864cf 2490 complete_and_exit(&common->thread_notifier, 0);
d5e2b67a
MN
2491}
2492
2493
9c610213 2494/*************************** DEVICE ATTRIBUTES ***************************/
d5e2b67a 2495
6fdc5dd2
AP
2496static ssize_t ro_show(struct device *dev, struct device_attribute *attr, char *buf)
2497{
77850ae2
AP
2498 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
2499
2500 return fsg_show_ro(curlun, buf);
6fdc5dd2
AP
2501}
2502
2503static ssize_t nofua_show(struct device *dev, struct device_attribute *attr,
2504 char *buf)
2505{
77850ae2
AP
2506 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
2507
2508 return fsg_show_nofua(curlun, buf);
6fdc5dd2
AP
2509}
2510
2511static ssize_t file_show(struct device *dev, struct device_attribute *attr,
2512 char *buf)
2513{
77850ae2
AP
2514 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
2515 struct rw_semaphore *filesem = dev_get_drvdata(dev);
2516
2517 return fsg_show_file(curlun, filesem, buf);
6fdc5dd2
AP
2518}
2519
2520static ssize_t ro_store(struct device *dev, struct device_attribute *attr,
2521 const char *buf, size_t count)
2522{
77850ae2
AP
2523 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
2524 struct rw_semaphore *filesem = dev_get_drvdata(dev);
2525
2526 return fsg_store_ro(curlun, filesem, buf, count);
6fdc5dd2
AP
2527}
2528
2529static ssize_t nofua_store(struct device *dev, struct device_attribute *attr,
2530 const char *buf, size_t count)
2531{
77850ae2
AP
2532 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
2533
2534 return fsg_store_nofua(curlun, buf, count);
6fdc5dd2
AP
2535}
2536
2537static ssize_t file_store(struct device *dev, struct device_attribute *attr,
2538 const char *buf, size_t count)
2539{
77850ae2
AP
2540 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
2541 struct rw_semaphore *filesem = dev_get_drvdata(dev);
2542
2543 return fsg_store_file(curlun, filesem, buf, count);
6fdc5dd2
AP
2544}
2545
ce26bd23 2546static DEVICE_ATTR_RW(nofua);
c7b364f7
TI
2547/* mode wil be set in fsg_lun_attr_is_visible() */
2548static DEVICE_ATTR(ro, 0, ro_show, ro_store);
2549static DEVICE_ATTR(file, 0, file_show, file_store);
d5e2b67a 2550
9c610213
MN
2551/****************************** FSG COMMON ******************************/
2552
2553static void fsg_common_release(struct kref *ref);
d5e2b67a 2554
9c610213 2555static void fsg_lun_release(struct device *dev)
d5e2b67a 2556{
9c610213 2557 /* Nothing needs to be done */
d5e2b67a
MN
2558}
2559
f3fed367 2560void fsg_common_get(struct fsg_common *common)
d5e2b67a 2561{
9c610213 2562 kref_get(&common->ref);
d5e2b67a 2563}
7a93d040 2564EXPORT_SYMBOL_GPL(fsg_common_get);
d5e2b67a 2565
f3fed367 2566void fsg_common_put(struct fsg_common *common)
9c610213
MN
2567{
2568 kref_put(&common->ref, fsg_common_release);
2569}
7a93d040 2570EXPORT_SYMBOL_GPL(fsg_common_put);
9c610213 2571
7a93d040 2572static struct fsg_common *fsg_common_setup(struct fsg_common *common)
b24650df
AP
2573{
2574 if (!common) {
2575 common = kzalloc(sizeof(*common), GFP_KERNEL);
2576 if (!common)
2577 return ERR_PTR(-ENOMEM);
2578 common->free_storage_on_release = 1;
2579 } else {
b24650df
AP
2580 common->free_storage_on_release = 0;
2581 }
2582 init_rwsem(&common->filesem);
2583 spin_lock_init(&common->lock);
2584 kref_init(&common->ref);
2585 init_completion(&common->thread_notifier);
225785ae 2586 init_waitqueue_head(&common->io_wait);
b24650df
AP
2587 init_waitqueue_head(&common->fsg_wait);
2588 common->state = FSG_STATE_TERMINATED;
dd02ea5a 2589 memset(common->luns, 0, sizeof(common->luns));
b24650df
AP
2590
2591 return common;
2592}
2593
bd528d4e
AP
2594void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs)
2595{
2596 common->sysfs = sysfs;
2597}
7a93d040 2598EXPORT_SYMBOL_GPL(fsg_common_set_sysfs);
bd528d4e 2599
8502d66d
AP
2600static void _fsg_common_free_buffers(struct fsg_buffhd *buffhds, unsigned n)
2601{
2602 if (buffhds) {
2603 struct fsg_buffhd *bh = buffhds;
2604 while (n--) {
2605 kfree(bh->buf);
2606 ++bh;
2607 }
2608 kfree(buffhds);
2609 }
2610}
2611
6313caac
AP
2612int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
2613{
2614 struct fsg_buffhd *bh, *buffhds;
fe5a6c48 2615 int i;
6313caac
AP
2616
2617 buffhds = kcalloc(n, sizeof(*buffhds), GFP_KERNEL);
2618 if (!buffhds)
2619 return -ENOMEM;
2620
2621 /* Data buffers cyclic list */
2622 bh = buffhds;
2623 i = n;
2624 goto buffhds_first_it;
2625 do {
2626 bh->next = bh + 1;
2627 ++bh;
2628buffhds_first_it:
2629 bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
2630 if (unlikely(!bh->buf))
2631 goto error_release;
2632 } while (--i);
2633 bh->next = buffhds;
2634
2635 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
2636 common->fsg_num_buffers = n;
2637 common->buffhds = buffhds;
2638
2639 return 0;
2640
2641error_release:
2642 /*
2643 * "buf"s pointed to by heads after n - i are NULL
2644 * so releasing them won't hurt
2645 */
2646 _fsg_common_free_buffers(buffhds, n);
2647
2648 return -ENOMEM;
2649}
7a93d040 2650EXPORT_SYMBOL_GPL(fsg_common_set_num_buffers);
6313caac 2651
5542f58c 2652void fsg_common_remove_lun(struct fsg_lun *lun)
70634170 2653{
5542f58c 2654 if (device_is_registered(&lun->dev))
70634170 2655 device_unregister(&lun->dev);
70634170
AP
2656 fsg_lun_close(lun);
2657 kfree(lun);
2658}
7a93d040 2659EXPORT_SYMBOL_GPL(fsg_common_remove_lun);
70634170
AP
2660
2661static void _fsg_common_remove_luns(struct fsg_common *common, int n)
2662{
2663 int i;
2664
2665 for (i = 0; i < n; ++i)
2666 if (common->luns[i]) {
5542f58c 2667 fsg_common_remove_lun(common->luns[i]);
70634170
AP
2668 common->luns[i] = NULL;
2669 }
2670}
2671
2672void fsg_common_remove_luns(struct fsg_common *common)
2673{
dd02ea5a 2674 _fsg_common_remove_luns(common, ARRAY_SIZE(common->luns));
70634170 2675}
bab7a1f1 2676EXPORT_SYMBOL_GPL(fsg_common_remove_luns);
70634170 2677
e5eaa0dc
AP
2678void fsg_common_free_buffers(struct fsg_common *common)
2679{
2680 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
2681 common->buffhds = NULL;
2682}
7a93d040 2683EXPORT_SYMBOL_GPL(fsg_common_free_buffers);
70634170 2684
a891d7a3
AP
2685int fsg_common_set_cdev(struct fsg_common *common,
2686 struct usb_composite_dev *cdev, bool can_stall)
2687{
2688 struct usb_string *us;
2689
2690 common->gadget = cdev->gadget;
2691 common->ep0 = cdev->gadget->ep0;
2692 common->ep0req = cdev->req;
2693 common->cdev = cdev;
2694
2695 us = usb_gstrings_attach(cdev, fsg_strings_array,
2696 ARRAY_SIZE(fsg_strings));
2697 if (IS_ERR(us))
2698 return PTR_ERR(us);
2699
2700 fsg_intf_desc.iInterface = us[FSG_STRING_INTERFACE].id;
2701
2702 /*
2703 * Some peripheral controllers are known not to be able to
2704 * halt bulk endpoints correctly. If one of them is present,
2705 * disable stalls.
2706 */
a4cc4215
RB
2707 common->can_stall = can_stall &&
2708 gadget_is_stall_supported(common->gadget);
a891d7a3
AP
2709
2710 return 0;
2711}
7a93d040 2712EXPORT_SYMBOL_GPL(fsg_common_set_cdev);
a891d7a3 2713
c7b364f7
TI
2714static struct attribute *fsg_lun_dev_attrs[] = {
2715 &dev_attr_ro.attr,
2716 &dev_attr_file.attr,
2717 &dev_attr_nofua.attr,
2718 NULL
2719};
b27c08c9 2720
c7b364f7
TI
2721static umode_t fsg_lun_dev_is_visible(struct kobject *kobj,
2722 struct attribute *attr, int idx)
2723{
2724 struct device *dev = kobj_to_dev(kobj);
2725 struct fsg_lun *lun = fsg_lun_from_dev(dev);
b27c08c9 2726
c7b364f7
TI
2727 if (attr == &dev_attr_ro.attr)
2728 return lun->cdrom ? S_IRUGO : (S_IWUSR | S_IRUGO);
2729 if (attr == &dev_attr_file.attr)
2730 return lun->removable ? (S_IWUSR | S_IRUGO) : S_IRUGO;
2731 return attr->mode;
2732}
b27c08c9 2733
c7b364f7
TI
2734static const struct attribute_group fsg_lun_dev_group = {
2735 .attrs = fsg_lun_dev_attrs,
2736 .is_visible = fsg_lun_dev_is_visible,
2737};
b27c08c9 2738
c7b364f7
TI
2739static const struct attribute_group *fsg_lun_dev_groups[] = {
2740 &fsg_lun_dev_group,
2741 NULL
2742};
b27c08c9
AP
2743
2744int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
2745 unsigned int id, const char *name,
2746 const char **name_pfx)
2747{
2748 struct fsg_lun *lun;
2749 char *pathbuf, *p;
2750 int rc = -ENOMEM;
2751
dd02ea5a 2752 if (id >= ARRAY_SIZE(common->luns))
b27c08c9
AP
2753 return -ENODEV;
2754
2755 if (common->luns[id])
2756 return -EBUSY;
2757
2758 if (!cfg->filename && !cfg->removable) {
2759 pr_err("no file given for LUN%d\n", id);
2760 return -EINVAL;
2761 }
2762
2763 lun = kzalloc(sizeof(*lun), GFP_KERNEL);
2764 if (!lun)
2765 return -ENOMEM;
2766
2767 lun->name_pfx = name_pfx;
2768
2769 lun->cdrom = !!cfg->cdrom;
2770 lun->ro = cfg->cdrom || cfg->ro;
2771 lun->initially_ro = lun->ro;
2772 lun->removable = !!cfg->removable;
2773
2774 if (!common->sysfs) {
2775 /* we DON'T own the name!*/
2776 lun->name = name;
2777 } else {
2778 lun->dev.release = fsg_lun_release;
2779 lun->dev.parent = &common->gadget->dev;
c7b364f7 2780 lun->dev.groups = fsg_lun_dev_groups;
b27c08c9 2781 dev_set_drvdata(&lun->dev, &common->filesem);
a1924d8a 2782 dev_set_name(&lun->dev, "%s", name);
b27c08c9
AP
2783 lun->name = dev_name(&lun->dev);
2784
c7b364f7 2785 rc = device_register(&lun->dev);
b27c08c9
AP
2786 if (rc) {
2787 pr_info("failed to register LUN%d: %d\n", id, rc);
c7b364f7 2788 put_device(&lun->dev);
b27c08c9
AP
2789 goto error_sysfs;
2790 }
2791 }
2792
2793 common->luns[id] = lun;
2794
2795 if (cfg->filename) {
2796 rc = fsg_lun_open(lun, cfg->filename);
2797 if (rc)
2798 goto error_lun;
2799 }
2800
2801 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
2802 p = "(no medium)";
2803 if (fsg_lun_is_open(lun)) {
2804 p = "(error)";
2805 if (pathbuf) {
9bf39ab2 2806 p = file_path(lun->filp, pathbuf, PATH_MAX);
b27c08c9
AP
2807 if (IS_ERR(p))
2808 p = "(error)";
2809 }
2810 }
2811 pr_info("LUN: %s%s%sfile: %s\n",
2812 lun->removable ? "removable " : "",
2813 lun->ro ? "read only " : "",
2814 lun->cdrom ? "CD-ROM " : "",
2815 p);
2816 kfree(pathbuf);
2817
2818 return 0;
2819
2820error_lun:
5542f58c 2821 if (device_is_registered(&lun->dev))
b27c08c9 2822 device_unregister(&lun->dev);
b27c08c9
AP
2823 fsg_lun_close(lun);
2824 common->luns[id] = NULL;
2825error_sysfs:
2826 kfree(lun);
2827 return rc;
2828}
7a93d040 2829EXPORT_SYMBOL_GPL(fsg_common_create_lun);
b27c08c9
AP
2830
2831int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg)
2832{
2833 char buf[8]; /* enough for 100000000 different numbers, decimal */
2834 int i, rc;
2835
dd02ea5a
KO
2836 fsg_common_remove_luns(common);
2837
2838 for (i = 0; i < cfg->nluns; ++i) {
b27c08c9
AP
2839 snprintf(buf, sizeof(buf), "lun%d", i);
2840 rc = fsg_common_create_lun(common, &cfg->luns[i], i, buf, NULL);
2841 if (rc)
2842 goto fail;
2843 }
2844
dd02ea5a 2845 pr_info("Number of LUNs=%d\n", cfg->nluns);
b27c08c9
AP
2846
2847 return 0;
2848
2849fail:
2850 _fsg_common_remove_luns(common, i);
2851 return rc;
2852}
7a93d040 2853EXPORT_SYMBOL_GPL(fsg_common_create_luns);
b27c08c9 2854
23682e3c
AP
2855void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
2856 const char *pn)
2857{
2858 int i;
2859
2860 /* Prepare inquiryString */
2861 i = get_default_bcdDevice();
2862 snprintf(common->inquiry_string, sizeof(common->inquiry_string),
2863 "%-8s%-16s%04x", vn ?: "Linux",
2864 /* Assume product name dependent on the first LUN */
2865 pn ?: ((*common->luns)->cdrom
2866 ? "File-CD Gadget"
2867 : "File-Stor Gadget"),
2868 i);
2869}
7a93d040 2870EXPORT_SYMBOL_GPL(fsg_common_set_inquiry_string);
23682e3c 2871
9c610213
MN
2872static void fsg_common_release(struct kref *ref)
2873{
b9e00088 2874 struct fsg_common *common = container_of(ref, struct fsg_common, ref);
dd02ea5a 2875 int i;
9c610213 2876
8ea864cf
MN
2877 /* If the thread isn't already dead, tell it to exit now */
2878 if (common->state != FSG_STATE_TERMINATED) {
2879 raise_exception(common, FSG_STATE_EXIT);
2880 wait_for_completion(&common->thread_notifier);
8ea864cf
MN
2881 }
2882
dd02ea5a
KO
2883 for (i = 0; i < ARRAY_SIZE(common->luns); ++i) {
2884 struct fsg_lun *lun = common->luns[i];
2885 if (!lun)
2886 continue;
2887 fsg_lun_close(lun);
5542f58c 2888 if (device_is_registered(&lun->dev))
dd02ea5a
KO
2889 device_unregister(&lun->dev);
2890 kfree(lun);
9c610213
MN
2891 }
2892
8502d66d 2893 _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
9c610213
MN
2894 if (common->free_storage_on_release)
2895 kfree(common);
2896}
2897
2898
2899/*-------------------------------------------------------------------------*/
2900
28824b18 2901static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
d5e2b67a 2902{
d23b0f08 2903 struct fsg_dev *fsg = fsg_from_func(f);
dd02ea5a 2904 struct fsg_common *common = fsg->common;
d23b0f08 2905 struct usb_gadget *gadget = c->cdev->gadget;
d5e2b67a 2906 int i;
d5e2b67a 2907 struct usb_ep *ep;
10287bae
SAS
2908 unsigned max_burst;
2909 int ret;
e5eaa0dc 2910 struct fsg_opts *opts;
7a93d040 2911
dd02ea5a
KO
2912 /* Don't allow to bind if we don't have at least one LUN */
2913 ret = _fsg_common_get_max_lun(common);
2914 if (ret < 0) {
2915 pr_err("There should be at least one LUN.\n");
2916 return -EINVAL;
2917 }
2918
e5eaa0dc
AP
2919 opts = fsg_opts_from_func_inst(f->fi);
2920 if (!opts->no_configfs) {
2921 ret = fsg_common_set_cdev(fsg->common, c->cdev,
2922 fsg->common->can_stall);
2923 if (ret)
2924 return ret;
f91d2f43 2925 fsg_common_set_inquiry_string(fsg->common, NULL, NULL);
f78bbcae
MN
2926 }
2927
2928 if (!common->thread_task) {
78db441d 2929 common->state = FSG_STATE_NORMAL;
f78bbcae
MN
2930 common->thread_task =
2931 kthread_create(fsg_main_thread, common, "file-storage");
2932 if (IS_ERR(common->thread_task)) {
78db441d 2933 ret = PTR_ERR(common->thread_task);
f78bbcae
MN
2934 common->thread_task = NULL;
2935 common->state = FSG_STATE_TERMINATED;
e5eaa0dc 2936 return ret;
f78bbcae
MN
2937 }
2938 DBG(common, "I/O thread pid: %d\n",
2939 task_pid_nr(common->thread_task));
2940 wake_up_process(common->thread_task);
e5eaa0dc 2941 }
e5eaa0dc 2942
d5e2b67a 2943 fsg->gadget = gadget;
d5e2b67a 2944
d23b0f08
MN
2945 /* New interface */
2946 i = usb_interface_id(c, f);
2947 if (i < 0)
8078f314 2948 goto fail;
d23b0f08
MN
2949 fsg_intf_desc.bInterfaceNumber = i;
2950 fsg->interface_number = i;
d5e2b67a 2951
d5e2b67a 2952 /* Find all the endpoints we will use */
d5e2b67a
MN
2953 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2954 if (!ep)
2955 goto autoconf_fail;
d5e2b67a
MN
2956 fsg->bulk_in = ep;
2957
2958 ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2959 if (!ep)
2960 goto autoconf_fail;
d5e2b67a
MN
2961 fsg->bulk_out = ep;
2962
10287bae
SAS
2963 /* Assume endpoint addresses are the same for both speeds */
2964 fsg_hs_bulk_in_desc.bEndpointAddress =
2965 fsg_fs_bulk_in_desc.bEndpointAddress;
2966 fsg_hs_bulk_out_desc.bEndpointAddress =
2967 fsg_fs_bulk_out_desc.bEndpointAddress;
4bb99b7c 2968
10287bae
SAS
2969 /* Calculate bMaxBurst, we know packet size is 1024 */
2970 max_burst = min_t(unsigned, FSG_BUFLEN / 1024, 15);
4bb99b7c 2971
10287bae
SAS
2972 fsg_ss_bulk_in_desc.bEndpointAddress =
2973 fsg_fs_bulk_in_desc.bEndpointAddress;
2974 fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
4bb99b7c 2975
10287bae
SAS
2976 fsg_ss_bulk_out_desc.bEndpointAddress =
2977 fsg_fs_bulk_out_desc.bEndpointAddress;
2978 fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
4bb99b7c 2979
10287bae 2980 ret = usb_assign_descriptors(f, fsg_fs_function, fsg_hs_function,
59a3cedf 2981 fsg_ss_function, fsg_ss_function);
10287bae
SAS
2982 if (ret)
2983 goto autoconf_fail;
4bb99b7c 2984
d5e2b67a
MN
2985 return 0;
2986
2987autoconf_fail:
2988 ERROR(fsg, "unable to autoconfigure all endpoints\n");
8078f314
SSR
2989 i = -ENOTSUPP;
2990fail:
2991 /* terminate the thread */
2992 if (fsg->common->state != FSG_STATE_TERMINATED) {
2993 raise_exception(fsg->common, FSG_STATE_EXIT);
2994 wait_for_completion(&fsg->common->thread_notifier);
2995 }
2996 return i;
d5e2b67a
MN
2997}
2998
e5eaa0dc
AP
2999/****************************** ALLOCATE FUNCTION *************************/
3000
3001static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
3002{
3003 struct fsg_dev *fsg = fsg_from_func(f);
3004 struct fsg_common *common = fsg->common;
3005
3006 DBG(fsg, "unbind\n");
3007 if (fsg->common->fsg == fsg) {
3008 fsg->common->new_fsg = NULL;
3009 raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
3010 /* FIXME: make interruptible or killable somehow? */
3011 wait_event(common->fsg_wait, common->fsg != fsg);
3012 }
3013
e5eaa0dc 3014 usb_free_all_descriptors(&fsg->function);
d5e2b67a 3015}
481e4929 3016
ef0aa4b9
AP
3017static inline struct fsg_lun_opts *to_fsg_lun_opts(struct config_item *item)
3018{
3019 return container_of(to_config_group(item), struct fsg_lun_opts, group);
3020}
3021
3022static inline struct fsg_opts *to_fsg_opts(struct config_item *item)
3023{
3024 return container_of(to_config_group(item), struct fsg_opts,
3025 func_inst.group);
3026}
3027
ef0aa4b9
AP
3028static void fsg_lun_attr_release(struct config_item *item)
3029{
3030 struct fsg_lun_opts *lun_opts;
3031
3032 lun_opts = to_fsg_lun_opts(item);
3033 kfree(lun_opts);
3034}
3035
3036static struct configfs_item_operations fsg_lun_item_ops = {
5bb7289d 3037 .release = fsg_lun_attr_release,
ef0aa4b9
AP
3038};
3039
4a90cb20 3040static ssize_t fsg_lun_opts_file_show(struct config_item *item, char *page)
ef0aa4b9 3041{
4a90cb20
CH
3042 struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
3043 struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
ef0aa4b9
AP
3044
3045 return fsg_show_file(opts->lun, &fsg_opts->common->filesem, page);
3046}
3047
4a90cb20 3048static ssize_t fsg_lun_opts_file_store(struct config_item *item,
ef0aa4b9
AP
3049 const char *page, size_t len)
3050{
4a90cb20
CH
3051 struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
3052 struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
ef0aa4b9
AP
3053
3054 return fsg_store_file(opts->lun, &fsg_opts->common->filesem, page, len);
3055}
3056
4a90cb20 3057CONFIGFS_ATTR(fsg_lun_opts_, file);
ef0aa4b9 3058
4a90cb20 3059static ssize_t fsg_lun_opts_ro_show(struct config_item *item, char *page)
ef0aa4b9 3060{
4a90cb20 3061 return fsg_show_ro(to_fsg_lun_opts(item)->lun, page);
ef0aa4b9
AP
3062}
3063
4a90cb20 3064static ssize_t fsg_lun_opts_ro_store(struct config_item *item,
ef0aa4b9
AP
3065 const char *page, size_t len)
3066{
4a90cb20
CH
3067 struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
3068 struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
ef0aa4b9
AP
3069
3070 return fsg_store_ro(opts->lun, &fsg_opts->common->filesem, page, len);
3071}
3072
4a90cb20 3073CONFIGFS_ATTR(fsg_lun_opts_, ro);
ef0aa4b9 3074
4a90cb20 3075static ssize_t fsg_lun_opts_removable_show(struct config_item *item,
ef0aa4b9
AP
3076 char *page)
3077{
4a90cb20 3078 return fsg_show_removable(to_fsg_lun_opts(item)->lun, page);
ef0aa4b9
AP
3079}
3080
4a90cb20 3081static ssize_t fsg_lun_opts_removable_store(struct config_item *item,
ef0aa4b9
AP
3082 const char *page, size_t len)
3083{
4a90cb20 3084 return fsg_store_removable(to_fsg_lun_opts(item)->lun, page, len);
ef0aa4b9
AP
3085}
3086
4a90cb20 3087CONFIGFS_ATTR(fsg_lun_opts_, removable);
ef0aa4b9 3088
4a90cb20 3089static ssize_t fsg_lun_opts_cdrom_show(struct config_item *item, char *page)
ef0aa4b9 3090{
4a90cb20 3091 return fsg_show_cdrom(to_fsg_lun_opts(item)->lun, page);
ef0aa4b9
AP
3092}
3093
4a90cb20 3094static ssize_t fsg_lun_opts_cdrom_store(struct config_item *item,
ef0aa4b9
AP
3095 const char *page, size_t len)
3096{
4a90cb20
CH
3097 struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
3098 struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
b8798636
AP
3099
3100 return fsg_store_cdrom(opts->lun, &fsg_opts->common->filesem, page,
3101 len);
ef0aa4b9
AP
3102}
3103
4a90cb20 3104CONFIGFS_ATTR(fsg_lun_opts_, cdrom);
ef0aa4b9 3105
4a90cb20 3106static ssize_t fsg_lun_opts_nofua_show(struct config_item *item, char *page)
ef0aa4b9 3107{
4a90cb20 3108 return fsg_show_nofua(to_fsg_lun_opts(item)->lun, page);
ef0aa4b9
AP
3109}
3110
4a90cb20 3111static ssize_t fsg_lun_opts_nofua_store(struct config_item *item,
ef0aa4b9
AP
3112 const char *page, size_t len)
3113{
4a90cb20 3114 return fsg_store_nofua(to_fsg_lun_opts(item)->lun, page, len);
ef0aa4b9
AP
3115}
3116
4a90cb20 3117CONFIGFS_ATTR(fsg_lun_opts_, nofua);
ef0aa4b9 3118
6ac47090
PG
3119static ssize_t fsg_lun_opts_inquiry_string_show(struct config_item *item,
3120 char *page)
3121{
3122 return fsg_show_inquiry_string(to_fsg_lun_opts(item)->lun, page);
3123}
3124
3125static ssize_t fsg_lun_opts_inquiry_string_store(struct config_item *item,
3126 const char *page, size_t len)
3127{
3128 return fsg_store_inquiry_string(to_fsg_lun_opts(item)->lun, page, len);
3129}
3130
3131CONFIGFS_ATTR(fsg_lun_opts_, inquiry_string);
3132
ef0aa4b9 3133static struct configfs_attribute *fsg_lun_attrs[] = {
4a90cb20
CH
3134 &fsg_lun_opts_attr_file,
3135 &fsg_lun_opts_attr_ro,
3136 &fsg_lun_opts_attr_removable,
3137 &fsg_lun_opts_attr_cdrom,
3138 &fsg_lun_opts_attr_nofua,
6ac47090 3139 &fsg_lun_opts_attr_inquiry_string,
ef0aa4b9
AP
3140 NULL,
3141};
3142
3143static struct config_item_type fsg_lun_type = {
3144 .ct_item_ops = &fsg_lun_item_ops,
3145 .ct_attrs = fsg_lun_attrs,
3146 .ct_owner = THIS_MODULE,
3147};
3148
ef0aa4b9
AP
3149static struct config_group *fsg_lun_make(struct config_group *group,
3150 const char *name)
3151{
3152 struct fsg_lun_opts *opts;
3153 struct fsg_opts *fsg_opts;
3154 struct fsg_lun_config config;
3155 char *num_str;
3156 u8 num;
3157 int ret;
3158
3159 num_str = strchr(name, '.');
3160 if (!num_str) {
3161 pr_err("Unable to locate . in LUN.NUMBER\n");
3162 return ERR_PTR(-EINVAL);
3163 }
3164 num_str++;
3165
3166 ret = kstrtou8(num_str, 0, &num);
3167 if (ret)
3168 return ERR_PTR(ret);
3169
3170 fsg_opts = to_fsg_opts(&group->cg_item);
3171 if (num >= FSG_MAX_LUNS)
5bb7289d
AP
3172 return ERR_PTR(-ERANGE);
3173
ef0aa4b9
AP
3174 mutex_lock(&fsg_opts->lock);
3175 if (fsg_opts->refcnt || fsg_opts->common->luns[num]) {
3176 ret = -EBUSY;
3177 goto out;
3178 }
3179
3180 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
3181 if (!opts) {
3182 ret = -ENOMEM;
3183 goto out;
3184 }
3185
3186 memset(&config, 0, sizeof(config));
3187 config.removable = true;
3188
ef0aa4b9
AP
3189 ret = fsg_common_create_lun(fsg_opts->common, &config, num, name,
3190 (const char **)&group->cg_item.ci_name);
3191 if (ret) {
3192 kfree(opts);
3193 goto out;
3194 }
3195 opts->lun = fsg_opts->common->luns[num];
3196 opts->lun_id = num;
3197 mutex_unlock(&fsg_opts->lock);
3198
3199 config_group_init_type_name(&opts->group, name, &fsg_lun_type);
3200
3201 return &opts->group;
3202out:
3203 mutex_unlock(&fsg_opts->lock);
3204 return ERR_PTR(ret);
3205}
3206
3207static void fsg_lun_drop(struct config_group *group, struct config_item *item)
3208{
3209 struct fsg_lun_opts *lun_opts;
3210 struct fsg_opts *fsg_opts;
3211
3212 lun_opts = to_fsg_lun_opts(item);
3213 fsg_opts = to_fsg_opts(&group->cg_item);
3214
3215 mutex_lock(&fsg_opts->lock);
3216 if (fsg_opts->refcnt) {
3217 struct config_item *gadget;
3218
3219 gadget = group->cg_item.ci_parent->ci_parent;
3220 unregister_gadget_item(gadget);
3221 }
3222
5542f58c 3223 fsg_common_remove_lun(lun_opts->lun);
ef0aa4b9
AP
3224 fsg_opts->common->luns[lun_opts->lun_id] = NULL;
3225 lun_opts->lun_id = 0;
3226 mutex_unlock(&fsg_opts->lock);
3227
3228 config_item_put(item);
3229}
3230
ef0aa4b9
AP
3231static void fsg_attr_release(struct config_item *item)
3232{
3233 struct fsg_opts *opts = to_fsg_opts(item);
3234
3235 usb_put_function_instance(&opts->func_inst);
3236}
3237
3238static struct configfs_item_operations fsg_item_ops = {
5bb7289d 3239 .release = fsg_attr_release,
ef0aa4b9
AP
3240};
3241
4a90cb20 3242static ssize_t fsg_opts_stall_show(struct config_item *item, char *page)
ef0aa4b9 3243{
4a90cb20 3244 struct fsg_opts *opts = to_fsg_opts(item);
ef0aa4b9
AP
3245 int result;
3246
3247 mutex_lock(&opts->lock);
3248 result = sprintf(page, "%d", opts->common->can_stall);
3249 mutex_unlock(&opts->lock);
3250
3251 return result;
3252}
3253
4a90cb20 3254static ssize_t fsg_opts_stall_store(struct config_item *item, const char *page,
ef0aa4b9
AP
3255 size_t len)
3256{
4a90cb20 3257 struct fsg_opts *opts = to_fsg_opts(item);
ef0aa4b9 3258 int ret;
5bb7289d 3259 bool stall;
ef0aa4b9
AP
3260
3261 mutex_lock(&opts->lock);
5bb7289d 3262
ef0aa4b9 3263 if (opts->refcnt) {
5bb7289d
AP
3264 mutex_unlock(&opts->lock);
3265 return -EBUSY;
ef0aa4b9 3266 }
ef0aa4b9 3267
5bb7289d
AP
3268 ret = strtobool(page, &stall);
3269 if (!ret) {
3270 opts->common->can_stall = stall;
3271 ret = len;
3272 }
ef0aa4b9 3273
ef0aa4b9 3274 mutex_unlock(&opts->lock);
5bb7289d 3275
ef0aa4b9
AP
3276 return ret;
3277}
3278
4a90cb20 3279CONFIGFS_ATTR(fsg_opts_, stall);
ef0aa4b9
AP
3280
3281#ifdef CONFIG_USB_GADGET_DEBUG_FILES
4a90cb20 3282static ssize_t fsg_opts_num_buffers_show(struct config_item *item, char *page)
ef0aa4b9 3283{
4a90cb20 3284 struct fsg_opts *opts = to_fsg_opts(item);
ef0aa4b9
AP
3285 int result;
3286
3287 mutex_lock(&opts->lock);
3288 result = sprintf(page, "%d", opts->common->fsg_num_buffers);
3289 mutex_unlock(&opts->lock);
3290
3291 return result;
3292}
3293
4a90cb20 3294static ssize_t fsg_opts_num_buffers_store(struct config_item *item,
ef0aa4b9
AP
3295 const char *page, size_t len)
3296{
4a90cb20 3297 struct fsg_opts *opts = to_fsg_opts(item);
ef0aa4b9
AP
3298 int ret;
3299 u8 num;
3300
3301 mutex_lock(&opts->lock);
3302 if (opts->refcnt) {
3303 ret = -EBUSY;
3304 goto end;
3305 }
3306 ret = kstrtou8(page, 0, &num);
3307 if (ret)
3308 goto end;
3309
ef0aa4b9
AP
3310 fsg_common_set_num_buffers(opts->common, num);
3311 ret = len;
3312
3313end:
3314 mutex_unlock(&opts->lock);
3315 return ret;
3316}
3317
4a90cb20 3318CONFIGFS_ATTR(fsg_opts_, num_buffers);
ef0aa4b9
AP
3319#endif
3320
3321static struct configfs_attribute *fsg_attrs[] = {
4a90cb20 3322 &fsg_opts_attr_stall,
ef0aa4b9 3323#ifdef CONFIG_USB_GADGET_DEBUG_FILES
4a90cb20 3324 &fsg_opts_attr_num_buffers,
ef0aa4b9
AP
3325#endif
3326 NULL,
3327};
3328
3329static struct configfs_group_operations fsg_group_ops = {
3330 .make_group = fsg_lun_make,
3331 .drop_item = fsg_lun_drop,
3332};
3333
3334static struct config_item_type fsg_func_type = {
3335 .ct_item_ops = &fsg_item_ops,
3336 .ct_group_ops = &fsg_group_ops,
3337 .ct_attrs = fsg_attrs,
3338 .ct_owner = THIS_MODULE,
3339};
3340
e5eaa0dc
AP
3341static void fsg_free_inst(struct usb_function_instance *fi)
3342{
3343 struct fsg_opts *opts;
3344
3345 opts = fsg_opts_from_func_inst(fi);
3346 fsg_common_put(opts->common);
3347 kfree(opts);
3348}
3349
3350static struct usb_function_instance *fsg_alloc_inst(void)
3351{
3352 struct fsg_opts *opts;
ef0aa4b9 3353 struct fsg_lun_config config;
e5eaa0dc
AP
3354 int rc;
3355
3356 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
3357 if (!opts)
3358 return ERR_PTR(-ENOMEM);
ef0aa4b9 3359 mutex_init(&opts->lock);
e5eaa0dc 3360 opts->func_inst.free_func_inst = fsg_free_inst;
7a93d040 3361 opts->common = fsg_common_setup(opts->common);
e5eaa0dc
AP
3362 if (IS_ERR(opts->common)) {
3363 rc = PTR_ERR(opts->common);
3364 goto release_opts;
3365 }
e5eaa0dc
AP
3366
3367 rc = fsg_common_set_num_buffers(opts->common,
3368 CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS);
3369 if (rc)
dd02ea5a 3370 goto release_opts;
e5eaa0dc
AP
3371
3372 pr_info(FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
3373
ef0aa4b9
AP
3374 memset(&config, 0, sizeof(config));
3375 config.removable = true;
3376 rc = fsg_common_create_lun(opts->common, &config, 0, "lun.0",
3377 (const char **)&opts->func_inst.group.cg_item.ci_name);
903588a9
KO
3378 if (rc)
3379 goto release_buffers;
3380
ef0aa4b9
AP
3381 opts->lun0.lun = opts->common->luns[0];
3382 opts->lun0.lun_id = 0;
ef0aa4b9
AP
3383
3384 config_group_init_type_name(&opts->func_inst.group, "", &fsg_func_type);
3385
1ae1602d
CH
3386 config_group_init_type_name(&opts->lun0.group, "lun.0", &fsg_lun_type);
3387 configfs_add_default_group(&opts->lun0.group, &opts->func_inst.group);
3388
e5eaa0dc
AP
3389 return &opts->func_inst;
3390
903588a9
KO
3391release_buffers:
3392 fsg_common_free_buffers(opts->common);
e5eaa0dc
AP
3393release_opts:
3394 kfree(opts);
3395 return ERR_PTR(rc);
3396}
3397
3398static void fsg_free(struct usb_function *f)
3399{
3400 struct fsg_dev *fsg;
ef0aa4b9 3401 struct fsg_opts *opts;
e5eaa0dc
AP
3402
3403 fsg = container_of(f, struct fsg_dev, function);
ef0aa4b9
AP
3404 opts = container_of(f->fi, struct fsg_opts, func_inst);
3405
3406 mutex_lock(&opts->lock);
3407 opts->refcnt--;
3408 mutex_unlock(&opts->lock);
e5eaa0dc
AP
3409
3410 kfree(fsg);
3411}
3412
3413static struct usb_function *fsg_alloc(struct usb_function_instance *fi)
3414{
3415 struct fsg_opts *opts = fsg_opts_from_func_inst(fi);
3416 struct fsg_common *common = opts->common;
3417 struct fsg_dev *fsg;
3418
3419 fsg = kzalloc(sizeof(*fsg), GFP_KERNEL);
3420 if (unlikely(!fsg))
3421 return ERR_PTR(-ENOMEM);
3422
ef0aa4b9
AP
3423 mutex_lock(&opts->lock);
3424 opts->refcnt++;
3425 mutex_unlock(&opts->lock);
8515bac0 3426
e5eaa0dc
AP
3427 fsg->function.name = FSG_DRIVER_DESC;
3428 fsg->function.bind = fsg_bind;
3429 fsg->function.unbind = fsg_unbind;
3430 fsg->function.setup = fsg_setup;
3431 fsg->function.set_alt = fsg_set_alt;
3432 fsg->function.disable = fsg_disable;
3433 fsg->function.free_func = fsg_free;
3434
3435 fsg->common = common;
3436
3437 return &fsg->function;
3438}
3439
3440DECLARE_USB_FUNCTION_INIT(mass_storage, fsg_alloc_inst, fsg_alloc);
3441MODULE_LICENSE("GPL");
3442MODULE_AUTHOR("Michal Nazarewicz");
3443
481e4929
MN
3444/************************* Module parameters *************************/
3445
6fdc5dd2 3446
f3fed367 3447void fsg_config_from_params(struct fsg_config *cfg,
6fdc5dd2
AP
3448 const struct fsg_module_parameters *params,
3449 unsigned int fsg_num_buffers)
481e4929
MN
3450{
3451 struct fsg_lun_config *lun;
d26a6aa0 3452 unsigned i;
481e4929
MN
3453
3454 /* Configure LUNs */
d26a6aa0
MN
3455 cfg->nluns =
3456 min(params->luns ?: (params->file_count ?: 1u),
3457 (unsigned)FSG_MAX_LUNS);
3458 for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
481e4929
MN
3459 lun->ro = !!params->ro[i];
3460 lun->cdrom = !!params->cdrom[i];
fa84c575 3461 lun->removable = !!params->removable[i];
481e4929
MN
3462 lun->filename =
3463 params->file_count > i && params->file[i][0]
3464 ? params->file[i]
136c489b 3465 : NULL;
481e4929
MN
3466 }
3467
d26a6aa0 3468 /* Let MSF use defaults */
136c489b
JH
3469 cfg->vendor_name = NULL;
3470 cfg->product_name = NULL;
481e4929 3471
8876f5e7
MN
3472 cfg->ops = NULL;
3473 cfg->private_data = NULL;
c85efcb9 3474
481e4929
MN
3475 /* Finalise */
3476 cfg->can_stall = params->stall;
6fdc5dd2 3477 cfg->fsg_num_buffers = fsg_num_buffers;
481e4929 3478}
7a93d040 3479EXPORT_SYMBOL_GPL(fsg_config_from_params);