15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
200a2430fSAndrzej Pietrasiewicz /*
300a2430fSAndrzej Pietrasiewicz * f_mass_storage.c -- Mass Storage USB Composite Function
400a2430fSAndrzej Pietrasiewicz *
500a2430fSAndrzej Pietrasiewicz * Copyright (C) 2003-2008 Alan Stern
600a2430fSAndrzej Pietrasiewicz * Copyright (C) 2009 Samsung Electronics
700a2430fSAndrzej Pietrasiewicz * Author: Michal Nazarewicz <mina86@mina86.com>
800a2430fSAndrzej Pietrasiewicz * All rights reserved.
900a2430fSAndrzej Pietrasiewicz */
1000a2430fSAndrzej Pietrasiewicz
1100a2430fSAndrzej Pietrasiewicz /*
1200a2430fSAndrzej Pietrasiewicz * The Mass Storage Function acts as a USB Mass Storage device,
1300a2430fSAndrzej Pietrasiewicz * appearing to the host as a disk drive or as a CD-ROM drive. In
1400a2430fSAndrzej Pietrasiewicz * addition to providing an example of a genuinely useful composite
1500a2430fSAndrzej Pietrasiewicz * function for a USB device, it also illustrates a technique of
1600a2430fSAndrzej Pietrasiewicz * double-buffering for increased throughput.
1700a2430fSAndrzej Pietrasiewicz *
1800a2430fSAndrzej Pietrasiewicz * For more information about MSF and in particular its module
1900a2430fSAndrzej Pietrasiewicz * parameters and sysfs interface read the
20ecefae6dSMauro Carvalho Chehab * <Documentation/usb/mass-storage.rst> file.
2100a2430fSAndrzej Pietrasiewicz */
2200a2430fSAndrzej Pietrasiewicz
2300a2430fSAndrzej Pietrasiewicz /*
2400a2430fSAndrzej Pietrasiewicz * MSF is configured by specifying a fsg_config structure. It has the
2500a2430fSAndrzej Pietrasiewicz * following fields:
2600a2430fSAndrzej Pietrasiewicz *
2700a2430fSAndrzej Pietrasiewicz * nluns Number of LUNs function have (anywhere from 1
2875ddead2SKrzysztof Opasiak * to FSG_MAX_LUNS).
2900a2430fSAndrzej Pietrasiewicz * luns An array of LUN configuration values. This
3000a2430fSAndrzej Pietrasiewicz * should be filled for each LUN that
3100a2430fSAndrzej Pietrasiewicz * function will include (ie. for "nluns"
3200a2430fSAndrzej Pietrasiewicz * LUNs). Each element of the array has
3300a2430fSAndrzej Pietrasiewicz * the following fields:
3400a2430fSAndrzej Pietrasiewicz * ->filename The path to the backing file for the LUN.
3500a2430fSAndrzej Pietrasiewicz * Required if LUN is not marked as
3600a2430fSAndrzej Pietrasiewicz * removable.
3700a2430fSAndrzej Pietrasiewicz * ->ro Flag specifying access to the LUN shall be
3800a2430fSAndrzej Pietrasiewicz * read-only. This is implied if CD-ROM
3900a2430fSAndrzej Pietrasiewicz * emulation is enabled as well as when
4000a2430fSAndrzej Pietrasiewicz * it was impossible to open "filename"
4100a2430fSAndrzej Pietrasiewicz * in R/W mode.
4200a2430fSAndrzej Pietrasiewicz * ->removable Flag specifying that LUN shall be indicated as
4300a2430fSAndrzej Pietrasiewicz * being removable.
4400a2430fSAndrzej Pietrasiewicz * ->cdrom Flag specifying that LUN shall be reported as
4500a2430fSAndrzej Pietrasiewicz * being a CD-ROM.
4600a2430fSAndrzej Pietrasiewicz * ->nofua Flag specifying that FUA flag in SCSI WRITE(10,12)
4700a2430fSAndrzej Pietrasiewicz * commands for this LUN shall be ignored.
4800a2430fSAndrzej Pietrasiewicz *
4900a2430fSAndrzej Pietrasiewicz * vendor_name
5000a2430fSAndrzej Pietrasiewicz * product_name
5100a2430fSAndrzej Pietrasiewicz * release Information used as a reply to INQUIRY
5200a2430fSAndrzej Pietrasiewicz * request. To use default set to NULL,
5300a2430fSAndrzej Pietrasiewicz * NULL, 0xffff respectively. The first
5400a2430fSAndrzej Pietrasiewicz * field should be 8 and the second 16
5500a2430fSAndrzej Pietrasiewicz * characters or less.
5600a2430fSAndrzej Pietrasiewicz *
5700a2430fSAndrzej Pietrasiewicz * can_stall Set to permit function to halt bulk endpoints.
5800a2430fSAndrzej Pietrasiewicz * Disabled on some USB devices known not
5900a2430fSAndrzej Pietrasiewicz * to work correctly. You should set it
6000a2430fSAndrzej Pietrasiewicz * to true.
6100a2430fSAndrzej Pietrasiewicz *
6200a2430fSAndrzej Pietrasiewicz * If "removable" is not set for a LUN then a backing file must be
6300a2430fSAndrzej Pietrasiewicz * specified. If it is set, then NULL filename means the LUN's medium
6400a2430fSAndrzej Pietrasiewicz * is not loaded (an empty string as "filename" in the fsg_config
6500a2430fSAndrzej Pietrasiewicz * structure causes error). The CD-ROM emulation includes a single
6600a2430fSAndrzej Pietrasiewicz * data track and no audio tracks; hence there need be only one
6700a2430fSAndrzej Pietrasiewicz * backing file per LUN.
6800a2430fSAndrzej Pietrasiewicz *
6900a2430fSAndrzej Pietrasiewicz * This function is heavily based on "File-backed Storage Gadget" by
7000a2430fSAndrzej Pietrasiewicz * Alan Stern which in turn is heavily based on "Gadget Zero" by David
7100a2430fSAndrzej Pietrasiewicz * Brownell. The driver's SCSI command interface was based on the
7200a2430fSAndrzej Pietrasiewicz * "Information technology - Small Computer System Interface - 2"
7300a2430fSAndrzej Pietrasiewicz * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
7400a2430fSAndrzej Pietrasiewicz * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
7500a2430fSAndrzej Pietrasiewicz * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
7600a2430fSAndrzej Pietrasiewicz * was based on the "Universal Serial Bus Mass Storage Class UFI
7700a2430fSAndrzej Pietrasiewicz * Command Specification" document, Revision 1.0, December 14, 1998,
7800a2430fSAndrzej Pietrasiewicz * available at
7900a2430fSAndrzej Pietrasiewicz * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
8000a2430fSAndrzej Pietrasiewicz */
8100a2430fSAndrzej Pietrasiewicz
8200a2430fSAndrzej Pietrasiewicz /*
8300a2430fSAndrzej Pietrasiewicz * Driver Design
8400a2430fSAndrzej Pietrasiewicz *
8500a2430fSAndrzej Pietrasiewicz * The MSF is fairly straightforward. There is a main kernel
8600a2430fSAndrzej Pietrasiewicz * thread that handles most of the work. Interrupt routines field
8700a2430fSAndrzej Pietrasiewicz * callbacks from the controller driver: bulk- and interrupt-request
8800a2430fSAndrzej Pietrasiewicz * completion notifications, endpoint-0 events, and disconnect events.
8900a2430fSAndrzej Pietrasiewicz * Completion events are passed to the main thread by wakeup calls. Many
9000a2430fSAndrzej Pietrasiewicz * ep0 requests are handled at interrupt time, but SetInterface,
9100a2430fSAndrzej Pietrasiewicz * SetConfiguration, and device reset requests are forwarded to the
9200a2430fSAndrzej Pietrasiewicz * thread in the form of "exceptions" using SIGUSR1 signals (since they
9300a2430fSAndrzej Pietrasiewicz * should interrupt any ongoing file I/O operations).
9400a2430fSAndrzej Pietrasiewicz *
9500a2430fSAndrzej Pietrasiewicz * The thread's main routine implements the standard command/data/status
9600a2430fSAndrzej Pietrasiewicz * parts of a SCSI interaction. It and its subroutines are full of tests
9700a2430fSAndrzej Pietrasiewicz * for pending signals/exceptions -- all this polling is necessary since
9800a2430fSAndrzej Pietrasiewicz * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
9900a2430fSAndrzej Pietrasiewicz * indication that the driver really wants to be running in userspace.)
10000a2430fSAndrzej Pietrasiewicz * An important point is that so long as the thread is alive it keeps an
10100a2430fSAndrzej Pietrasiewicz * open reference to the backing file. This will prevent unmounting
10200a2430fSAndrzej Pietrasiewicz * the backing file's underlying filesystem and could cause problems
10300a2430fSAndrzej Pietrasiewicz * during system shutdown, for example. To prevent such problems, the
10400a2430fSAndrzej Pietrasiewicz * thread catches INT, TERM, and KILL signals and converts them into
10500a2430fSAndrzej Pietrasiewicz * an EXIT exception.
10600a2430fSAndrzej Pietrasiewicz *
10700a2430fSAndrzej Pietrasiewicz * In normal operation the main thread is started during the gadget's
10800a2430fSAndrzej Pietrasiewicz * fsg_bind() callback and stopped during fsg_unbind(). But it can
10900a2430fSAndrzej Pietrasiewicz * also exit when it receives a signal, and there's no point leaving
11000a2430fSAndrzej Pietrasiewicz * the gadget running when the thread is dead. As of this moment, MSF
11100a2430fSAndrzej Pietrasiewicz * provides no way to deregister the gadget when thread dies -- maybe
11200a2430fSAndrzej Pietrasiewicz * a callback functions is needed.
11300a2430fSAndrzej Pietrasiewicz *
11400a2430fSAndrzej Pietrasiewicz * To provide maximum throughput, the driver uses a circular pipeline of
11500a2430fSAndrzej Pietrasiewicz * buffer heads (struct fsg_buffhd). In principle the pipeline can be
11600a2430fSAndrzej Pietrasiewicz * arbitrarily long; in practice the benefits don't justify having more
11700a2430fSAndrzej Pietrasiewicz * than 2 stages (i.e., double buffering). But it helps to think of the
11800a2430fSAndrzej Pietrasiewicz * pipeline as being a long one. Each buffer head contains a bulk-in and
11900a2430fSAndrzej Pietrasiewicz * a bulk-out request pointer (since the buffer can be used for both
12000a2430fSAndrzej Pietrasiewicz * output and input -- directions always are given from the host's
12100a2430fSAndrzej Pietrasiewicz * point of view) as well as a pointer to the buffer and various state
12200a2430fSAndrzej Pietrasiewicz * variables.
12300a2430fSAndrzej Pietrasiewicz *
12400a2430fSAndrzej Pietrasiewicz * Use of the pipeline follows a simple protocol. There is a variable
12500a2430fSAndrzej Pietrasiewicz * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
12600a2430fSAndrzej Pietrasiewicz * At any time that buffer head may still be in use from an earlier
12700a2430fSAndrzej Pietrasiewicz * request, so each buffer head has a state variable indicating whether
12800a2430fSAndrzej Pietrasiewicz * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
12900a2430fSAndrzej Pietrasiewicz * buffer head to be EMPTY, filling the buffer either by file I/O or by
13000a2430fSAndrzej Pietrasiewicz * USB I/O (during which the buffer head is BUSY), and marking the buffer
13100a2430fSAndrzej Pietrasiewicz * head FULL when the I/O is complete. Then the buffer will be emptied
13200a2430fSAndrzej Pietrasiewicz * (again possibly by USB I/O, during which it is marked BUSY) and
13300a2430fSAndrzej Pietrasiewicz * finally marked EMPTY again (possibly by a completion routine).
13400a2430fSAndrzej Pietrasiewicz *
13500a2430fSAndrzej Pietrasiewicz * A module parameter tells the driver to avoid stalling the bulk
13600a2430fSAndrzej Pietrasiewicz * endpoints wherever the transport specification allows. This is
13700a2430fSAndrzej Pietrasiewicz * necessary for some UDCs like the SuperH, which cannot reliably clear a
13800a2430fSAndrzej Pietrasiewicz * halt on a bulk endpoint. However, under certain circumstances the
13900a2430fSAndrzej Pietrasiewicz * Bulk-only specification requires a stall. In such cases the driver
14000a2430fSAndrzej Pietrasiewicz * will halt the endpoint and set a flag indicating that it should clear
14100a2430fSAndrzej Pietrasiewicz * the halt in software during the next device reset. Hopefully this
14200a2430fSAndrzej Pietrasiewicz * will permit everything to work correctly. Furthermore, although the
14300a2430fSAndrzej Pietrasiewicz * specification allows the bulk-out endpoint to halt when the host sends
14400a2430fSAndrzej Pietrasiewicz * too much data, implementing this would cause an unavoidable race.
14500a2430fSAndrzej Pietrasiewicz * The driver will always use the "no-stall" approach for OUT transfers.
14600a2430fSAndrzej Pietrasiewicz *
14700a2430fSAndrzej Pietrasiewicz * One subtle point concerns sending status-stage responses for ep0
14800a2430fSAndrzej Pietrasiewicz * requests. Some of these requests, such as device reset, can involve
14900a2430fSAndrzej Pietrasiewicz * interrupting an ongoing file I/O operation, which might take an
15000a2430fSAndrzej Pietrasiewicz * arbitrarily long time. During that delay the host might give up on
15100a2430fSAndrzej Pietrasiewicz * the original ep0 request and issue a new one. When that happens the
15200a2430fSAndrzej Pietrasiewicz * driver should not notify the host about completion of the original
15300a2430fSAndrzej Pietrasiewicz * request, as the host will no longer be waiting for it. So the driver
15400a2430fSAndrzej Pietrasiewicz * assigns to each ep0 request a unique tag, and it keeps track of the
15500a2430fSAndrzej Pietrasiewicz * tag value of the request associated with a long-running exception
15600a2430fSAndrzej Pietrasiewicz * (device-reset, interface-change, or configuration-change). When the
15700a2430fSAndrzej Pietrasiewicz * exception handler is finished, the status-stage response is submitted
15800a2430fSAndrzej Pietrasiewicz * only if the current ep0 request tag is equal to the exception request
15900a2430fSAndrzej Pietrasiewicz * tag. Thus only the most recently received ep0 request will get a
16000a2430fSAndrzej Pietrasiewicz * status-stage response.
16100a2430fSAndrzej Pietrasiewicz *
16200a2430fSAndrzej Pietrasiewicz * Warning: This driver source file is too long. It ought to be split up
16300a2430fSAndrzej Pietrasiewicz * into a header file plus about 3 separate .c files, to handle the details
16400a2430fSAndrzej Pietrasiewicz * of the Gadget, USB Mass Storage, and SCSI protocols.
16500a2430fSAndrzej Pietrasiewicz */
16600a2430fSAndrzej Pietrasiewicz
16700a2430fSAndrzej Pietrasiewicz
16800a2430fSAndrzej Pietrasiewicz /* #define VERBOSE_DEBUG */
16900a2430fSAndrzej Pietrasiewicz /* #define DUMP_MSGS */
17000a2430fSAndrzej Pietrasiewicz
17100a2430fSAndrzej Pietrasiewicz #include <linux/blkdev.h>
17200a2430fSAndrzej Pietrasiewicz #include <linux/completion.h>
17300a2430fSAndrzej Pietrasiewicz #include <linux/dcache.h>
17400a2430fSAndrzej Pietrasiewicz #include <linux/delay.h>
17500a2430fSAndrzej Pietrasiewicz #include <linux/device.h>
17600a2430fSAndrzej Pietrasiewicz #include <linux/fcntl.h>
17700a2430fSAndrzej Pietrasiewicz #include <linux/file.h>
17800a2430fSAndrzej Pietrasiewicz #include <linux/fs.h>
179a8bc8cc1SChristophe JAILLET #include <linux/kstrtox.h>
18000a2430fSAndrzej Pietrasiewicz #include <linux/kthread.h>
1813f07c014SIngo Molnar #include <linux/sched/signal.h>
18200a2430fSAndrzej Pietrasiewicz #include <linux/limits.h>
183cbcc268bSMatthew Wilcox (Oracle) #include <linux/pagemap.h>
18400a2430fSAndrzej Pietrasiewicz #include <linux/rwsem.h>
18500a2430fSAndrzej Pietrasiewicz #include <linux/slab.h>
18600a2430fSAndrzej Pietrasiewicz #include <linux/spinlock.h>
18700a2430fSAndrzej Pietrasiewicz #include <linux/string.h>
18800a2430fSAndrzej Pietrasiewicz #include <linux/freezer.h>
18900a2430fSAndrzej Pietrasiewicz #include <linux/module.h>
1904a1e9211SFelipe Balbi #include <linux/uaccess.h>
191a7afff31SBart Van Assche #include <asm/unaligned.h>
19200a2430fSAndrzej Pietrasiewicz
19300a2430fSAndrzej Pietrasiewicz #include <linux/usb/ch9.h>
19400a2430fSAndrzej Pietrasiewicz #include <linux/usb/gadget.h>
19500a2430fSAndrzej Pietrasiewicz #include <linux/usb/composite.h>
19600a2430fSAndrzej Pietrasiewicz
1979ae24af3SGustavo A. R. Silva #include <linux/nospec.h>
1989ae24af3SGustavo A. R. Silva
19900a2430fSAndrzej Pietrasiewicz #include "configfs.h"
20000a2430fSAndrzej Pietrasiewicz
20100a2430fSAndrzej Pietrasiewicz
20200a2430fSAndrzej Pietrasiewicz /*------------------------------------------------------------------------*/
20300a2430fSAndrzej Pietrasiewicz
20400a2430fSAndrzej Pietrasiewicz #define FSG_DRIVER_DESC "Mass Storage Function"
20500a2430fSAndrzej Pietrasiewicz #define FSG_DRIVER_VERSION "2009/09/11"
20600a2430fSAndrzej Pietrasiewicz
20700a2430fSAndrzej Pietrasiewicz static const char fsg_string_interface[] = "Mass Storage";
20800a2430fSAndrzej Pietrasiewicz
20900a2430fSAndrzej Pietrasiewicz #include "storage_common.h"
21000a2430fSAndrzej Pietrasiewicz #include "f_mass_storage.h"
21100a2430fSAndrzej Pietrasiewicz
21200a2430fSAndrzej Pietrasiewicz /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
21300a2430fSAndrzej Pietrasiewicz static struct usb_string fsg_strings[] = {
21400a2430fSAndrzej Pietrasiewicz {FSG_STRING_INTERFACE, fsg_string_interface},
21500a2430fSAndrzej Pietrasiewicz {}
21600a2430fSAndrzej Pietrasiewicz };
21700a2430fSAndrzej Pietrasiewicz
21800a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings fsg_stringtab = {
21900a2430fSAndrzej Pietrasiewicz .language = 0x0409, /* en-us */
22000a2430fSAndrzej Pietrasiewicz .strings = fsg_strings,
22100a2430fSAndrzej Pietrasiewicz };
22200a2430fSAndrzej Pietrasiewicz
22300a2430fSAndrzej Pietrasiewicz static struct usb_gadget_strings *fsg_strings_array[] = {
22400a2430fSAndrzej Pietrasiewicz &fsg_stringtab,
22500a2430fSAndrzej Pietrasiewicz NULL,
22600a2430fSAndrzej Pietrasiewicz };
22700a2430fSAndrzej Pietrasiewicz
22800a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
22900a2430fSAndrzej Pietrasiewicz
23000a2430fSAndrzej Pietrasiewicz struct fsg_dev;
23100a2430fSAndrzej Pietrasiewicz struct fsg_common;
23200a2430fSAndrzej Pietrasiewicz
23300a2430fSAndrzej Pietrasiewicz /* Data shared by all the FSG instances. */
23400a2430fSAndrzej Pietrasiewicz struct fsg_common {
23500a2430fSAndrzej Pietrasiewicz struct usb_gadget *gadget;
23600a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev;
2374a56a478SBenjamin Herrenschmidt struct fsg_dev *fsg;
238225785aeSAlan Stern wait_queue_head_t io_wait;
23900a2430fSAndrzej Pietrasiewicz wait_queue_head_t fsg_wait;
24000a2430fSAndrzej Pietrasiewicz
24100a2430fSAndrzej Pietrasiewicz /* filesem protects: backing files in use */
24200a2430fSAndrzej Pietrasiewicz struct rw_semaphore filesem;
24300a2430fSAndrzej Pietrasiewicz
244225785aeSAlan Stern /* lock protects: state and thread_task */
24500a2430fSAndrzej Pietrasiewicz spinlock_t lock;
24600a2430fSAndrzej Pietrasiewicz
24700a2430fSAndrzej Pietrasiewicz struct usb_ep *ep0; /* Copy of gadget->ep0 */
24800a2430fSAndrzej Pietrasiewicz struct usb_request *ep0req; /* Copy of cdev->req */
24900a2430fSAndrzej Pietrasiewicz unsigned int ep0_req_tag;
25000a2430fSAndrzej Pietrasiewicz
25100a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *next_buffhd_to_fill;
25200a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *next_buffhd_to_drain;
25300a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *buffhds;
25400a2430fSAndrzej Pietrasiewicz unsigned int fsg_num_buffers;
25500a2430fSAndrzej Pietrasiewicz
25600a2430fSAndrzej Pietrasiewicz int cmnd_size;
25700a2430fSAndrzej Pietrasiewicz u8 cmnd[MAX_COMMAND_SIZE];
25800a2430fSAndrzej Pietrasiewicz
25900a2430fSAndrzej Pietrasiewicz unsigned int lun;
260dd02ea5aSKrzysztof Opasiak struct fsg_lun *luns[FSG_MAX_LUNS];
26100a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun;
26200a2430fSAndrzej Pietrasiewicz
26300a2430fSAndrzej Pietrasiewicz unsigned int bulk_out_maxpacket;
26400a2430fSAndrzej Pietrasiewicz enum fsg_state state; /* For exception handling */
26500a2430fSAndrzej Pietrasiewicz unsigned int exception_req_tag;
2664a56a478SBenjamin Herrenschmidt void *exception_arg;
26700a2430fSAndrzej Pietrasiewicz
26800a2430fSAndrzej Pietrasiewicz enum data_direction data_dir;
26900a2430fSAndrzej Pietrasiewicz u32 data_size;
27000a2430fSAndrzej Pietrasiewicz u32 data_size_from_cmnd;
27100a2430fSAndrzej Pietrasiewicz u32 tag;
27200a2430fSAndrzej Pietrasiewicz u32 residue;
27300a2430fSAndrzej Pietrasiewicz u32 usb_amount_left;
27400a2430fSAndrzej Pietrasiewicz
27500a2430fSAndrzej Pietrasiewicz unsigned int can_stall:1;
27600a2430fSAndrzej Pietrasiewicz unsigned int free_storage_on_release:1;
27700a2430fSAndrzej Pietrasiewicz unsigned int phase_error:1;
27800a2430fSAndrzej Pietrasiewicz unsigned int short_packet_received:1;
27900a2430fSAndrzej Pietrasiewicz unsigned int bad_lun_okay:1;
28000a2430fSAndrzej Pietrasiewicz unsigned int running:1;
28100a2430fSAndrzej Pietrasiewicz unsigned int sysfs:1;
28200a2430fSAndrzej Pietrasiewicz
28300a2430fSAndrzej Pietrasiewicz struct completion thread_notifier;
28400a2430fSAndrzej Pietrasiewicz struct task_struct *thread_task;
28500a2430fSAndrzej Pietrasiewicz
28600a2430fSAndrzej Pietrasiewicz /* Gadget's private data. */
28700a2430fSAndrzej Pietrasiewicz void *private_data;
28800a2430fSAndrzej Pietrasiewicz
2896ac47090SPhilipp Gesang char inquiry_string[INQUIRY_STRING_LEN];
29000a2430fSAndrzej Pietrasiewicz };
29100a2430fSAndrzej Pietrasiewicz
29200a2430fSAndrzej Pietrasiewicz struct fsg_dev {
29300a2430fSAndrzej Pietrasiewicz struct usb_function function;
29400a2430fSAndrzej Pietrasiewicz struct usb_gadget *gadget; /* Copy of cdev->gadget */
29500a2430fSAndrzej Pietrasiewicz struct fsg_common *common;
29600a2430fSAndrzej Pietrasiewicz
29700a2430fSAndrzej Pietrasiewicz u16 interface_number;
29800a2430fSAndrzej Pietrasiewicz
29900a2430fSAndrzej Pietrasiewicz unsigned int bulk_in_enabled:1;
30000a2430fSAndrzej Pietrasiewicz unsigned int bulk_out_enabled:1;
30100a2430fSAndrzej Pietrasiewicz
30200a2430fSAndrzej Pietrasiewicz unsigned long atomic_bitflags;
30300a2430fSAndrzej Pietrasiewicz #define IGNORE_BULK_OUT 0
30400a2430fSAndrzej Pietrasiewicz
30500a2430fSAndrzej Pietrasiewicz struct usb_ep *bulk_in;
30600a2430fSAndrzej Pietrasiewicz struct usb_ep *bulk_out;
30700a2430fSAndrzej Pietrasiewicz };
30800a2430fSAndrzej Pietrasiewicz
__fsg_is_set(struct fsg_common * common,const char * func,unsigned line)30900a2430fSAndrzej Pietrasiewicz static inline int __fsg_is_set(struct fsg_common *common,
31000a2430fSAndrzej Pietrasiewicz const char *func, unsigned line)
31100a2430fSAndrzej Pietrasiewicz {
31200a2430fSAndrzej Pietrasiewicz if (common->fsg)
31300a2430fSAndrzej Pietrasiewicz return 1;
31400a2430fSAndrzej Pietrasiewicz ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
31500a2430fSAndrzej Pietrasiewicz WARN_ON(1);
31600a2430fSAndrzej Pietrasiewicz return 0;
31700a2430fSAndrzej Pietrasiewicz }
31800a2430fSAndrzej Pietrasiewicz
31900a2430fSAndrzej Pietrasiewicz #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
32000a2430fSAndrzej Pietrasiewicz
fsg_from_func(struct usb_function * f)32100a2430fSAndrzej Pietrasiewicz static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
32200a2430fSAndrzej Pietrasiewicz {
32300a2430fSAndrzej Pietrasiewicz return container_of(f, struct fsg_dev, function);
32400a2430fSAndrzej Pietrasiewicz }
32500a2430fSAndrzej Pietrasiewicz
exception_in_progress(struct fsg_common * common)32600a2430fSAndrzej Pietrasiewicz static int exception_in_progress(struct fsg_common *common)
32700a2430fSAndrzej Pietrasiewicz {
32878db441dSAlan Stern return common->state > FSG_STATE_NORMAL;
32900a2430fSAndrzej Pietrasiewicz }
33000a2430fSAndrzej Pietrasiewicz
33100a2430fSAndrzej Pietrasiewicz /* Make bulk-out requests be divisible by the maxpacket size */
set_bulk_out_req_length(struct fsg_common * common,struct fsg_buffhd * bh,unsigned int length)33200a2430fSAndrzej Pietrasiewicz static void set_bulk_out_req_length(struct fsg_common *common,
33300a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh, unsigned int length)
33400a2430fSAndrzej Pietrasiewicz {
33500a2430fSAndrzej Pietrasiewicz unsigned int rem;
33600a2430fSAndrzej Pietrasiewicz
33700a2430fSAndrzej Pietrasiewicz bh->bulk_out_intended_length = length;
33800a2430fSAndrzej Pietrasiewicz rem = length % common->bulk_out_maxpacket;
33900a2430fSAndrzej Pietrasiewicz if (rem > 0)
34000a2430fSAndrzej Pietrasiewicz length += common->bulk_out_maxpacket - rem;
34100a2430fSAndrzej Pietrasiewicz bh->outreq->length = length;
34200a2430fSAndrzej Pietrasiewicz }
34300a2430fSAndrzej Pietrasiewicz
34400a2430fSAndrzej Pietrasiewicz
34500a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
34600a2430fSAndrzej Pietrasiewicz
fsg_set_halt(struct fsg_dev * fsg,struct usb_ep * ep)34700a2430fSAndrzej Pietrasiewicz static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
34800a2430fSAndrzej Pietrasiewicz {
34900a2430fSAndrzej Pietrasiewicz const char *name;
35000a2430fSAndrzej Pietrasiewicz
35100a2430fSAndrzej Pietrasiewicz if (ep == fsg->bulk_in)
35200a2430fSAndrzej Pietrasiewicz name = "bulk-in";
35300a2430fSAndrzej Pietrasiewicz else if (ep == fsg->bulk_out)
35400a2430fSAndrzej Pietrasiewicz name = "bulk-out";
35500a2430fSAndrzej Pietrasiewicz else
35600a2430fSAndrzej Pietrasiewicz name = ep->name;
35700a2430fSAndrzej Pietrasiewicz DBG(fsg, "%s set halt\n", name);
35800a2430fSAndrzej Pietrasiewicz return usb_ep_set_halt(ep);
35900a2430fSAndrzej Pietrasiewicz }
36000a2430fSAndrzej Pietrasiewicz
36100a2430fSAndrzej Pietrasiewicz
36200a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
36300a2430fSAndrzej Pietrasiewicz
36400a2430fSAndrzej Pietrasiewicz /* These routines may be called in process context or in_irq */
36500a2430fSAndrzej Pietrasiewicz
__raise_exception(struct fsg_common * common,enum fsg_state new_state,void * arg)3664a56a478SBenjamin Herrenschmidt static void __raise_exception(struct fsg_common *common, enum fsg_state new_state,
3674a56a478SBenjamin Herrenschmidt void *arg)
36800a2430fSAndrzej Pietrasiewicz {
36900a2430fSAndrzej Pietrasiewicz unsigned long flags;
37000a2430fSAndrzej Pietrasiewicz
37100a2430fSAndrzej Pietrasiewicz /*
37200a2430fSAndrzej Pietrasiewicz * Do nothing if a higher-priority exception is already in progress.
37300a2430fSAndrzej Pietrasiewicz * If a lower-or-equal priority exception is in progress, preempt it
37400a2430fSAndrzej Pietrasiewicz * and notify the main thread by sending it a signal.
37500a2430fSAndrzej Pietrasiewicz */
37600a2430fSAndrzej Pietrasiewicz spin_lock_irqsave(&common->lock, flags);
37700a2430fSAndrzej Pietrasiewicz if (common->state <= new_state) {
37800a2430fSAndrzej Pietrasiewicz common->exception_req_tag = common->ep0_req_tag;
37900a2430fSAndrzej Pietrasiewicz common->state = new_state;
3804a56a478SBenjamin Herrenschmidt common->exception_arg = arg;
38100a2430fSAndrzej Pietrasiewicz if (common->thread_task)
38203515054SEric W. Biederman send_sig_info(SIGUSR1, SEND_SIG_PRIV,
38300a2430fSAndrzej Pietrasiewicz common->thread_task);
38400a2430fSAndrzej Pietrasiewicz }
38500a2430fSAndrzej Pietrasiewicz spin_unlock_irqrestore(&common->lock, flags);
38600a2430fSAndrzej Pietrasiewicz }
38700a2430fSAndrzej Pietrasiewicz
raise_exception(struct fsg_common * common,enum fsg_state new_state)3884a56a478SBenjamin Herrenschmidt static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
3894a56a478SBenjamin Herrenschmidt {
3904a56a478SBenjamin Herrenschmidt __raise_exception(common, new_state, NULL);
3914a56a478SBenjamin Herrenschmidt }
39200a2430fSAndrzej Pietrasiewicz
39300a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
39400a2430fSAndrzej Pietrasiewicz
ep0_queue(struct fsg_common * common)39500a2430fSAndrzej Pietrasiewicz static int ep0_queue(struct fsg_common *common)
39600a2430fSAndrzej Pietrasiewicz {
39700a2430fSAndrzej Pietrasiewicz int rc;
39800a2430fSAndrzej Pietrasiewicz
39900a2430fSAndrzej Pietrasiewicz rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
40000a2430fSAndrzej Pietrasiewicz common->ep0->driver_data = common;
40100a2430fSAndrzej Pietrasiewicz if (rc != 0 && rc != -ESHUTDOWN) {
40200a2430fSAndrzej Pietrasiewicz /* We can't do much more than wait for a reset */
40300a2430fSAndrzej Pietrasiewicz WARNING(common, "error in submission: %s --> %d\n",
40400a2430fSAndrzej Pietrasiewicz common->ep0->name, rc);
40500a2430fSAndrzej Pietrasiewicz }
40600a2430fSAndrzej Pietrasiewicz return rc;
40700a2430fSAndrzej Pietrasiewicz }
40800a2430fSAndrzej Pietrasiewicz
40900a2430fSAndrzej Pietrasiewicz
41000a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
41100a2430fSAndrzej Pietrasiewicz
41200a2430fSAndrzej Pietrasiewicz /* Completion handlers. These always run in_irq. */
41300a2430fSAndrzej Pietrasiewicz
bulk_in_complete(struct usb_ep * ep,struct usb_request * req)41400a2430fSAndrzej Pietrasiewicz static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
41500a2430fSAndrzej Pietrasiewicz {
41600a2430fSAndrzej Pietrasiewicz struct fsg_common *common = ep->driver_data;
41700a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh = req->context;
41800a2430fSAndrzej Pietrasiewicz
41900a2430fSAndrzej Pietrasiewicz if (req->status || req->actual != req->length)
42000a2430fSAndrzej Pietrasiewicz DBG(common, "%s --> %d, %u/%u\n", __func__,
42100a2430fSAndrzej Pietrasiewicz req->status, req->actual, req->length);
42200a2430fSAndrzej Pietrasiewicz if (req->status == -ECONNRESET) /* Request was cancelled */
42300a2430fSAndrzej Pietrasiewicz usb_ep_fifo_flush(ep);
42400a2430fSAndrzej Pietrasiewicz
425225785aeSAlan Stern /* Synchronize with the smp_load_acquire() in sleep_thread() */
426225785aeSAlan Stern smp_store_release(&bh->state, BUF_STATE_EMPTY);
427225785aeSAlan Stern wake_up(&common->io_wait);
42800a2430fSAndrzej Pietrasiewicz }
42900a2430fSAndrzej Pietrasiewicz
bulk_out_complete(struct usb_ep * ep,struct usb_request * req)43000a2430fSAndrzej Pietrasiewicz static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
43100a2430fSAndrzej Pietrasiewicz {
43200a2430fSAndrzej Pietrasiewicz struct fsg_common *common = ep->driver_data;
43300a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh = req->context;
43400a2430fSAndrzej Pietrasiewicz
43500a2430fSAndrzej Pietrasiewicz dump_msg(common, "bulk-out", req->buf, req->actual);
43600a2430fSAndrzej Pietrasiewicz if (req->status || req->actual != bh->bulk_out_intended_length)
43700a2430fSAndrzej Pietrasiewicz DBG(common, "%s --> %d, %u/%u\n", __func__,
43800a2430fSAndrzej Pietrasiewicz req->status, req->actual, bh->bulk_out_intended_length);
43900a2430fSAndrzej Pietrasiewicz if (req->status == -ECONNRESET) /* Request was cancelled */
44000a2430fSAndrzej Pietrasiewicz usb_ep_fifo_flush(ep);
44100a2430fSAndrzej Pietrasiewicz
442225785aeSAlan Stern /* Synchronize with the smp_load_acquire() in sleep_thread() */
443225785aeSAlan Stern smp_store_release(&bh->state, BUF_STATE_FULL);
444225785aeSAlan Stern wake_up(&common->io_wait);
44500a2430fSAndrzej Pietrasiewicz }
44600a2430fSAndrzej Pietrasiewicz
_fsg_common_get_max_lun(struct fsg_common * common)447dd02ea5aSKrzysztof Opasiak static int _fsg_common_get_max_lun(struct fsg_common *common)
448dd02ea5aSKrzysztof Opasiak {
449dd02ea5aSKrzysztof Opasiak int i = ARRAY_SIZE(common->luns) - 1;
450dd02ea5aSKrzysztof Opasiak
451dd02ea5aSKrzysztof Opasiak while (i >= 0 && !common->luns[i])
452dd02ea5aSKrzysztof Opasiak --i;
453dd02ea5aSKrzysztof Opasiak
454dd02ea5aSKrzysztof Opasiak return i;
455dd02ea5aSKrzysztof Opasiak }
456dd02ea5aSKrzysztof Opasiak
fsg_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)45700a2430fSAndrzej Pietrasiewicz static int fsg_setup(struct usb_function *f,
45800a2430fSAndrzej Pietrasiewicz const struct usb_ctrlrequest *ctrl)
45900a2430fSAndrzej Pietrasiewicz {
46000a2430fSAndrzej Pietrasiewicz struct fsg_dev *fsg = fsg_from_func(f);
46100a2430fSAndrzej Pietrasiewicz struct usb_request *req = fsg->common->ep0req;
46200a2430fSAndrzej Pietrasiewicz u16 w_index = le16_to_cpu(ctrl->wIndex);
46300a2430fSAndrzej Pietrasiewicz u16 w_value = le16_to_cpu(ctrl->wValue);
46400a2430fSAndrzej Pietrasiewicz u16 w_length = le16_to_cpu(ctrl->wLength);
46500a2430fSAndrzej Pietrasiewicz
46600a2430fSAndrzej Pietrasiewicz if (!fsg_is_set(fsg->common))
46700a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP;
46800a2430fSAndrzej Pietrasiewicz
46900a2430fSAndrzej Pietrasiewicz ++fsg->common->ep0_req_tag; /* Record arrival of a new request */
47000a2430fSAndrzej Pietrasiewicz req->context = NULL;
47100a2430fSAndrzej Pietrasiewicz req->length = 0;
47200a2430fSAndrzej Pietrasiewicz dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
47300a2430fSAndrzej Pietrasiewicz
47400a2430fSAndrzej Pietrasiewicz switch (ctrl->bRequest) {
47500a2430fSAndrzej Pietrasiewicz
47600a2430fSAndrzej Pietrasiewicz case US_BULK_RESET_REQUEST:
47700a2430fSAndrzej Pietrasiewicz if (ctrl->bRequestType !=
47800a2430fSAndrzej Pietrasiewicz (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
47900a2430fSAndrzej Pietrasiewicz break;
48000a2430fSAndrzej Pietrasiewicz if (w_index != fsg->interface_number || w_value != 0 ||
48100a2430fSAndrzej Pietrasiewicz w_length != 0)
48200a2430fSAndrzej Pietrasiewicz return -EDOM;
48300a2430fSAndrzej Pietrasiewicz
48400a2430fSAndrzej Pietrasiewicz /*
48500a2430fSAndrzej Pietrasiewicz * Raise an exception to stop the current operation
48600a2430fSAndrzej Pietrasiewicz * and reinitialize our state.
48700a2430fSAndrzej Pietrasiewicz */
48800a2430fSAndrzej Pietrasiewicz DBG(fsg, "bulk reset request\n");
48978db441dSAlan Stern raise_exception(fsg->common, FSG_STATE_PROTOCOL_RESET);
49000a2430fSAndrzej Pietrasiewicz return USB_GADGET_DELAYED_STATUS;
49100a2430fSAndrzej Pietrasiewicz
49200a2430fSAndrzej Pietrasiewicz case US_BULK_GET_MAX_LUN:
49300a2430fSAndrzej Pietrasiewicz if (ctrl->bRequestType !=
49400a2430fSAndrzej Pietrasiewicz (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
49500a2430fSAndrzej Pietrasiewicz break;
49600a2430fSAndrzej Pietrasiewicz if (w_index != fsg->interface_number || w_value != 0 ||
49700a2430fSAndrzej Pietrasiewicz w_length != 1)
49800a2430fSAndrzej Pietrasiewicz return -EDOM;
49900a2430fSAndrzej Pietrasiewicz VDBG(fsg, "get max LUN\n");
500dd02ea5aSKrzysztof Opasiak *(u8 *)req->buf = _fsg_common_get_max_lun(fsg->common);
50100a2430fSAndrzej Pietrasiewicz
50200a2430fSAndrzej Pietrasiewicz /* Respond with data/status */
50300a2430fSAndrzej Pietrasiewicz req->length = min((u16)1, w_length);
50400a2430fSAndrzej Pietrasiewicz return ep0_queue(fsg->common);
50500a2430fSAndrzej Pietrasiewicz }
50600a2430fSAndrzej Pietrasiewicz
50700a2430fSAndrzej Pietrasiewicz VDBG(fsg,
50800a2430fSAndrzej Pietrasiewicz "unknown class-specific control req %02x.%02x v%04x i%04x l%u\n",
50900a2430fSAndrzej Pietrasiewicz ctrl->bRequestType, ctrl->bRequest,
51000a2430fSAndrzej Pietrasiewicz le16_to_cpu(ctrl->wValue), w_index, w_length);
51100a2430fSAndrzej Pietrasiewicz return -EOPNOTSUPP;
51200a2430fSAndrzej Pietrasiewicz }
51300a2430fSAndrzej Pietrasiewicz
51400a2430fSAndrzej Pietrasiewicz
51500a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
51600a2430fSAndrzej Pietrasiewicz
51700a2430fSAndrzej Pietrasiewicz /* All the following routines run in process context */
51800a2430fSAndrzej Pietrasiewicz
51900a2430fSAndrzej Pietrasiewicz /* Use this for bulk or interrupt transfers, not ep0 */
start_transfer(struct fsg_dev * fsg,struct usb_ep * ep,struct usb_request * req)520225785aeSAlan Stern static int start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
521225785aeSAlan Stern struct usb_request *req)
52200a2430fSAndrzej Pietrasiewicz {
52300a2430fSAndrzej Pietrasiewicz int rc;
52400a2430fSAndrzej Pietrasiewicz
52500a2430fSAndrzej Pietrasiewicz if (ep == fsg->bulk_in)
52600a2430fSAndrzej Pietrasiewicz dump_msg(fsg, "bulk-in", req->buf, req->length);
52700a2430fSAndrzej Pietrasiewicz
52800a2430fSAndrzej Pietrasiewicz rc = usb_ep_queue(ep, req, GFP_KERNEL);
529225785aeSAlan Stern if (rc) {
53000a2430fSAndrzej Pietrasiewicz
53100a2430fSAndrzej Pietrasiewicz /* We can't do much more than wait for a reset */
532225785aeSAlan Stern req->status = rc;
53300a2430fSAndrzej Pietrasiewicz
53400a2430fSAndrzej Pietrasiewicz /*
53500a2430fSAndrzej Pietrasiewicz * Note: currently the net2280 driver fails zero-length
53600a2430fSAndrzej Pietrasiewicz * submissions if DMA is enabled.
53700a2430fSAndrzej Pietrasiewicz */
538225785aeSAlan Stern if (rc != -ESHUTDOWN &&
539225785aeSAlan Stern !(rc == -EOPNOTSUPP && req->length == 0))
540225785aeSAlan Stern WARNING(fsg, "error in submission: %s --> %d\n",
541225785aeSAlan Stern ep->name, rc);
542225785aeSAlan Stern }
543225785aeSAlan Stern return rc;
54400a2430fSAndrzej Pietrasiewicz }
54500a2430fSAndrzej Pietrasiewicz
start_in_transfer(struct fsg_common * common,struct fsg_buffhd * bh)54600a2430fSAndrzej Pietrasiewicz static bool start_in_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
54700a2430fSAndrzej Pietrasiewicz {
548*0d179c57Syuan linyu int rc;
549*0d179c57Syuan linyu
55000a2430fSAndrzej Pietrasiewicz if (!fsg_is_set(common))
55100a2430fSAndrzej Pietrasiewicz return false;
552225785aeSAlan Stern bh->state = BUF_STATE_SENDING;
553*0d179c57Syuan linyu rc = start_transfer(common->fsg, common->fsg->bulk_in, bh->inreq);
554*0d179c57Syuan linyu if (rc) {
555225785aeSAlan Stern bh->state = BUF_STATE_EMPTY;
556*0d179c57Syuan linyu if (rc == -ESHUTDOWN) {
557*0d179c57Syuan linyu common->running = 0;
558*0d179c57Syuan linyu return false;
559*0d179c57Syuan linyu }
560*0d179c57Syuan linyu }
56100a2430fSAndrzej Pietrasiewicz return true;
56200a2430fSAndrzej Pietrasiewicz }
56300a2430fSAndrzej Pietrasiewicz
start_out_transfer(struct fsg_common * common,struct fsg_buffhd * bh)56400a2430fSAndrzej Pietrasiewicz static bool start_out_transfer(struct fsg_common *common, struct fsg_buffhd *bh)
56500a2430fSAndrzej Pietrasiewicz {
566*0d179c57Syuan linyu int rc;
567*0d179c57Syuan linyu
56800a2430fSAndrzej Pietrasiewicz if (!fsg_is_set(common))
56900a2430fSAndrzej Pietrasiewicz return false;
570225785aeSAlan Stern bh->state = BUF_STATE_RECEIVING;
571*0d179c57Syuan linyu rc = start_transfer(common->fsg, common->fsg->bulk_out, bh->outreq);
572*0d179c57Syuan linyu if (rc) {
573225785aeSAlan Stern bh->state = BUF_STATE_FULL;
574*0d179c57Syuan linyu if (rc == -ESHUTDOWN) {
575*0d179c57Syuan linyu common->running = 0;
576*0d179c57Syuan linyu return false;
577*0d179c57Syuan linyu }
578*0d179c57Syuan linyu }
57900a2430fSAndrzej Pietrasiewicz return true;
58000a2430fSAndrzej Pietrasiewicz }
58100a2430fSAndrzej Pietrasiewicz
sleep_thread(struct fsg_common * common,bool can_freeze,struct fsg_buffhd * bh)582225785aeSAlan Stern static int sleep_thread(struct fsg_common *common, bool can_freeze,
583225785aeSAlan Stern struct fsg_buffhd *bh)
58400a2430fSAndrzej Pietrasiewicz {
585225785aeSAlan Stern int rc;
58600a2430fSAndrzej Pietrasiewicz
587225785aeSAlan Stern /* Wait until a signal arrives or bh is no longer busy */
58800a2430fSAndrzej Pietrasiewicz if (can_freeze)
589225785aeSAlan Stern /*
590225785aeSAlan Stern * synchronize with the smp_store_release(&bh->state) in
591225785aeSAlan Stern * bulk_in_complete() or bulk_out_complete()
592225785aeSAlan Stern */
593225785aeSAlan Stern rc = wait_event_freezable(common->io_wait,
594225785aeSAlan Stern bh && smp_load_acquire(&bh->state) >=
595225785aeSAlan Stern BUF_STATE_EMPTY);
596225785aeSAlan Stern else
597225785aeSAlan Stern rc = wait_event_interruptible(common->io_wait,
598225785aeSAlan Stern bh && smp_load_acquire(&bh->state) >=
599225785aeSAlan Stern BUF_STATE_EMPTY);
600225785aeSAlan Stern return rc ? -EINTR : 0;
60100a2430fSAndrzej Pietrasiewicz }
60200a2430fSAndrzej Pietrasiewicz
60300a2430fSAndrzej Pietrasiewicz
60400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
60500a2430fSAndrzej Pietrasiewicz
do_read(struct fsg_common * common)60600a2430fSAndrzej Pietrasiewicz static int do_read(struct fsg_common *common)
60700a2430fSAndrzej Pietrasiewicz {
60800a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
609bedbac5fSNikita Yushchenko u64 lba;
61000a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh;
61100a2430fSAndrzej Pietrasiewicz int rc;
61200a2430fSAndrzej Pietrasiewicz u32 amount_left;
61300a2430fSAndrzej Pietrasiewicz loff_t file_offset, file_offset_tmp;
61400a2430fSAndrzej Pietrasiewicz unsigned int amount;
61500a2430fSAndrzej Pietrasiewicz ssize_t nread;
61600a2430fSAndrzej Pietrasiewicz
61700a2430fSAndrzej Pietrasiewicz /*
61800a2430fSAndrzej Pietrasiewicz * Get the starting Logical Block Address and check that it's
61900a2430fSAndrzej Pietrasiewicz * not too big.
62000a2430fSAndrzej Pietrasiewicz */
62100a2430fSAndrzej Pietrasiewicz if (common->cmnd[0] == READ_6)
62200a2430fSAndrzej Pietrasiewicz lba = get_unaligned_be24(&common->cmnd[1]);
62300a2430fSAndrzej Pietrasiewicz else {
624bedbac5fSNikita Yushchenko if (common->cmnd[0] == READ_16)
625bedbac5fSNikita Yushchenko lba = get_unaligned_be64(&common->cmnd[2]);
626bedbac5fSNikita Yushchenko else /* READ_10 or READ_12 */
62700a2430fSAndrzej Pietrasiewicz lba = get_unaligned_be32(&common->cmnd[2]);
62800a2430fSAndrzej Pietrasiewicz
62900a2430fSAndrzej Pietrasiewicz /*
63000a2430fSAndrzej Pietrasiewicz * We allow DPO (Disable Page Out = don't save data in the
63100a2430fSAndrzej Pietrasiewicz * cache) and FUA (Force Unit Access = don't read from the
63200a2430fSAndrzej Pietrasiewicz * cache), but we don't implement them.
63300a2430fSAndrzej Pietrasiewicz */
63400a2430fSAndrzej Pietrasiewicz if ((common->cmnd[1] & ~0x18) != 0) {
63500a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
63600a2430fSAndrzej Pietrasiewicz return -EINVAL;
63700a2430fSAndrzej Pietrasiewicz }
63800a2430fSAndrzej Pietrasiewicz }
63900a2430fSAndrzej Pietrasiewicz if (lba >= curlun->num_sectors) {
64000a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
64100a2430fSAndrzej Pietrasiewicz return -EINVAL;
64200a2430fSAndrzej Pietrasiewicz }
64300a2430fSAndrzej Pietrasiewicz file_offset = ((loff_t) lba) << curlun->blkbits;
64400a2430fSAndrzej Pietrasiewicz
64500a2430fSAndrzej Pietrasiewicz /* Carry out the file reads */
64600a2430fSAndrzej Pietrasiewicz amount_left = common->data_size_from_cmnd;
64700a2430fSAndrzej Pietrasiewicz if (unlikely(amount_left == 0))
64800a2430fSAndrzej Pietrasiewicz return -EIO; /* No default reply */
64900a2430fSAndrzej Pietrasiewicz
65000a2430fSAndrzej Pietrasiewicz for (;;) {
65100a2430fSAndrzej Pietrasiewicz /*
65200a2430fSAndrzej Pietrasiewicz * Figure out how much we need to read:
65300a2430fSAndrzej Pietrasiewicz * Try to read the remaining amount.
65400a2430fSAndrzej Pietrasiewicz * But don't read more than the buffer size.
65500a2430fSAndrzej Pietrasiewicz * And don't try to read past the end of the file.
65600a2430fSAndrzej Pietrasiewicz */
65700a2430fSAndrzej Pietrasiewicz amount = min(amount_left, FSG_BUFLEN);
65800a2430fSAndrzej Pietrasiewicz amount = min((loff_t)amount,
65900a2430fSAndrzej Pietrasiewicz curlun->file_length - file_offset);
66000a2430fSAndrzej Pietrasiewicz
66100a2430fSAndrzej Pietrasiewicz /* Wait for the next buffer to become available */
66200a2430fSAndrzej Pietrasiewicz bh = common->next_buffhd_to_fill;
663225785aeSAlan Stern rc = sleep_thread(common, false, bh);
66400a2430fSAndrzej Pietrasiewicz if (rc)
66500a2430fSAndrzej Pietrasiewicz return rc;
66600a2430fSAndrzej Pietrasiewicz
66700a2430fSAndrzej Pietrasiewicz /*
66800a2430fSAndrzej Pietrasiewicz * If we were asked to read past the end of file,
66900a2430fSAndrzej Pietrasiewicz * end with an empty buffer.
67000a2430fSAndrzej Pietrasiewicz */
67100a2430fSAndrzej Pietrasiewicz if (amount == 0) {
67200a2430fSAndrzej Pietrasiewicz curlun->sense_data =
67300a2430fSAndrzej Pietrasiewicz SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
67400a2430fSAndrzej Pietrasiewicz curlun->sense_data_info =
67500a2430fSAndrzej Pietrasiewicz file_offset >> curlun->blkbits;
67600a2430fSAndrzej Pietrasiewicz curlun->info_valid = 1;
67700a2430fSAndrzej Pietrasiewicz bh->inreq->length = 0;
67800a2430fSAndrzej Pietrasiewicz bh->state = BUF_STATE_FULL;
67900a2430fSAndrzej Pietrasiewicz break;
68000a2430fSAndrzej Pietrasiewicz }
68100a2430fSAndrzej Pietrasiewicz
68200a2430fSAndrzej Pietrasiewicz /* Perform the read */
68300a2430fSAndrzej Pietrasiewicz file_offset_tmp = file_offset;
68405a4a33bSChristoph Hellwig nread = kernel_read(curlun->filp, bh->buf, amount,
68505a4a33bSChristoph Hellwig &file_offset_tmp);
68600a2430fSAndrzej Pietrasiewicz VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
68700a2430fSAndrzej Pietrasiewicz (unsigned long long)file_offset, (int)nread);
68800a2430fSAndrzej Pietrasiewicz if (signal_pending(current))
68900a2430fSAndrzej Pietrasiewicz return -EINTR;
69000a2430fSAndrzej Pietrasiewicz
69100a2430fSAndrzej Pietrasiewicz if (nread < 0) {
69200a2430fSAndrzej Pietrasiewicz LDBG(curlun, "error in file read: %d\n", (int)nread);
69300a2430fSAndrzej Pietrasiewicz nread = 0;
69400a2430fSAndrzej Pietrasiewicz } else if (nread < amount) {
69500a2430fSAndrzej Pietrasiewicz LDBG(curlun, "partial file read: %d/%u\n",
69600a2430fSAndrzej Pietrasiewicz (int)nread, amount);
69700a2430fSAndrzej Pietrasiewicz nread = round_down(nread, curlun->blksize);
69800a2430fSAndrzej Pietrasiewicz }
69900a2430fSAndrzej Pietrasiewicz file_offset += nread;
70000a2430fSAndrzej Pietrasiewicz amount_left -= nread;
70100a2430fSAndrzej Pietrasiewicz common->residue -= nread;
70200a2430fSAndrzej Pietrasiewicz
70300a2430fSAndrzej Pietrasiewicz /*
70400a2430fSAndrzej Pietrasiewicz * Except at the end of the transfer, nread will be
70500a2430fSAndrzej Pietrasiewicz * equal to the buffer size, which is divisible by the
70600a2430fSAndrzej Pietrasiewicz * bulk-in maxpacket size.
70700a2430fSAndrzej Pietrasiewicz */
70800a2430fSAndrzej Pietrasiewicz bh->inreq->length = nread;
70900a2430fSAndrzej Pietrasiewicz bh->state = BUF_STATE_FULL;
71000a2430fSAndrzej Pietrasiewicz
71100a2430fSAndrzej Pietrasiewicz /* If an error occurred, report it and its position */
71200a2430fSAndrzej Pietrasiewicz if (nread < amount) {
71300a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
71400a2430fSAndrzej Pietrasiewicz curlun->sense_data_info =
71500a2430fSAndrzej Pietrasiewicz file_offset >> curlun->blkbits;
71600a2430fSAndrzej Pietrasiewicz curlun->info_valid = 1;
71700a2430fSAndrzej Pietrasiewicz break;
71800a2430fSAndrzej Pietrasiewicz }
71900a2430fSAndrzej Pietrasiewicz
72000a2430fSAndrzej Pietrasiewicz if (amount_left == 0)
72100a2430fSAndrzej Pietrasiewicz break; /* No more left to read */
72200a2430fSAndrzej Pietrasiewicz
72300a2430fSAndrzej Pietrasiewicz /* Send this buffer and go read some more */
72400a2430fSAndrzej Pietrasiewicz bh->inreq->zero = 0;
72500a2430fSAndrzej Pietrasiewicz if (!start_in_transfer(common, bh))
72600a2430fSAndrzej Pietrasiewicz /* Don't know what to do if common->fsg is NULL */
72700a2430fSAndrzej Pietrasiewicz return -EIO;
72800a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_fill = bh->next;
72900a2430fSAndrzej Pietrasiewicz }
73000a2430fSAndrzej Pietrasiewicz
73100a2430fSAndrzej Pietrasiewicz return -EIO; /* No default reply */
73200a2430fSAndrzej Pietrasiewicz }
73300a2430fSAndrzej Pietrasiewicz
73400a2430fSAndrzej Pietrasiewicz
73500a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
73600a2430fSAndrzej Pietrasiewicz
do_write(struct fsg_common * common)73700a2430fSAndrzej Pietrasiewicz static int do_write(struct fsg_common *common)
73800a2430fSAndrzej Pietrasiewicz {
73900a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
740bedbac5fSNikita Yushchenko u64 lba;
74100a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh;
74200a2430fSAndrzej Pietrasiewicz int get_some_more;
74300a2430fSAndrzej Pietrasiewicz u32 amount_left_to_req, amount_left_to_write;
74400a2430fSAndrzej Pietrasiewicz loff_t usb_offset, file_offset, file_offset_tmp;
74500a2430fSAndrzej Pietrasiewicz unsigned int amount;
74600a2430fSAndrzej Pietrasiewicz ssize_t nwritten;
74700a2430fSAndrzej Pietrasiewicz int rc;
74800a2430fSAndrzej Pietrasiewicz
74900a2430fSAndrzej Pietrasiewicz if (curlun->ro) {
75000a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_WRITE_PROTECTED;
75100a2430fSAndrzej Pietrasiewicz return -EINVAL;
75200a2430fSAndrzej Pietrasiewicz }
75300a2430fSAndrzej Pietrasiewicz spin_lock(&curlun->filp->f_lock);
75400a2430fSAndrzej Pietrasiewicz curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */
75500a2430fSAndrzej Pietrasiewicz spin_unlock(&curlun->filp->f_lock);
75600a2430fSAndrzej Pietrasiewicz
75700a2430fSAndrzej Pietrasiewicz /*
75800a2430fSAndrzej Pietrasiewicz * Get the starting Logical Block Address and check that it's
75900a2430fSAndrzej Pietrasiewicz * not too big
76000a2430fSAndrzej Pietrasiewicz */
76100a2430fSAndrzej Pietrasiewicz if (common->cmnd[0] == WRITE_6)
76200a2430fSAndrzej Pietrasiewicz lba = get_unaligned_be24(&common->cmnd[1]);
76300a2430fSAndrzej Pietrasiewicz else {
764bedbac5fSNikita Yushchenko if (common->cmnd[0] == WRITE_16)
765bedbac5fSNikita Yushchenko lba = get_unaligned_be64(&common->cmnd[2]);
766bedbac5fSNikita Yushchenko else /* WRITE_10 or WRITE_12 */
76700a2430fSAndrzej Pietrasiewicz lba = get_unaligned_be32(&common->cmnd[2]);
76800a2430fSAndrzej Pietrasiewicz
76900a2430fSAndrzej Pietrasiewicz /*
77000a2430fSAndrzej Pietrasiewicz * We allow DPO (Disable Page Out = don't save data in the
77100a2430fSAndrzej Pietrasiewicz * cache) and FUA (Force Unit Access = write directly to the
77200a2430fSAndrzej Pietrasiewicz * medium). We don't implement DPO; we implement FUA by
77300a2430fSAndrzej Pietrasiewicz * performing synchronous output.
77400a2430fSAndrzej Pietrasiewicz */
77500a2430fSAndrzej Pietrasiewicz if (common->cmnd[1] & ~0x18) {
77600a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
77700a2430fSAndrzej Pietrasiewicz return -EINVAL;
77800a2430fSAndrzej Pietrasiewicz }
77900a2430fSAndrzej Pietrasiewicz if (!curlun->nofua && (common->cmnd[1] & 0x08)) { /* FUA */
78000a2430fSAndrzej Pietrasiewicz spin_lock(&curlun->filp->f_lock);
78100a2430fSAndrzej Pietrasiewicz curlun->filp->f_flags |= O_SYNC;
78200a2430fSAndrzej Pietrasiewicz spin_unlock(&curlun->filp->f_lock);
78300a2430fSAndrzej Pietrasiewicz }
78400a2430fSAndrzej Pietrasiewicz }
78500a2430fSAndrzej Pietrasiewicz if (lba >= curlun->num_sectors) {
78600a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
78700a2430fSAndrzej Pietrasiewicz return -EINVAL;
78800a2430fSAndrzej Pietrasiewicz }
78900a2430fSAndrzej Pietrasiewicz
79000a2430fSAndrzej Pietrasiewicz /* Carry out the file writes */
79100a2430fSAndrzej Pietrasiewicz get_some_more = 1;
79200a2430fSAndrzej Pietrasiewicz file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
79300a2430fSAndrzej Pietrasiewicz amount_left_to_req = common->data_size_from_cmnd;
79400a2430fSAndrzej Pietrasiewicz amount_left_to_write = common->data_size_from_cmnd;
79500a2430fSAndrzej Pietrasiewicz
79600a2430fSAndrzej Pietrasiewicz while (amount_left_to_write > 0) {
79700a2430fSAndrzej Pietrasiewicz
79800a2430fSAndrzej Pietrasiewicz /* Queue a request for more data from the host */
79900a2430fSAndrzej Pietrasiewicz bh = common->next_buffhd_to_fill;
80000a2430fSAndrzej Pietrasiewicz if (bh->state == BUF_STATE_EMPTY && get_some_more) {
80100a2430fSAndrzej Pietrasiewicz
80200a2430fSAndrzej Pietrasiewicz /*
80300a2430fSAndrzej Pietrasiewicz * Figure out how much we want to get:
80400a2430fSAndrzej Pietrasiewicz * Try to get the remaining amount,
80500a2430fSAndrzej Pietrasiewicz * but not more than the buffer size.
80600a2430fSAndrzej Pietrasiewicz */
80700a2430fSAndrzej Pietrasiewicz amount = min(amount_left_to_req, FSG_BUFLEN);
80800a2430fSAndrzej Pietrasiewicz
80900a2430fSAndrzej Pietrasiewicz /* Beyond the end of the backing file? */
81000a2430fSAndrzej Pietrasiewicz if (usb_offset >= curlun->file_length) {
81100a2430fSAndrzej Pietrasiewicz get_some_more = 0;
81200a2430fSAndrzej Pietrasiewicz curlun->sense_data =
81300a2430fSAndrzej Pietrasiewicz SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
81400a2430fSAndrzej Pietrasiewicz curlun->sense_data_info =
81500a2430fSAndrzej Pietrasiewicz usb_offset >> curlun->blkbits;
81600a2430fSAndrzej Pietrasiewicz curlun->info_valid = 1;
81700a2430fSAndrzej Pietrasiewicz continue;
81800a2430fSAndrzej Pietrasiewicz }
81900a2430fSAndrzej Pietrasiewicz
82000a2430fSAndrzej Pietrasiewicz /* Get the next buffer */
82100a2430fSAndrzej Pietrasiewicz usb_offset += amount;
82200a2430fSAndrzej Pietrasiewicz common->usb_amount_left -= amount;
82300a2430fSAndrzej Pietrasiewicz amount_left_to_req -= amount;
82400a2430fSAndrzej Pietrasiewicz if (amount_left_to_req == 0)
82500a2430fSAndrzej Pietrasiewicz get_some_more = 0;
82600a2430fSAndrzej Pietrasiewicz
82700a2430fSAndrzej Pietrasiewicz /*
82800a2430fSAndrzej Pietrasiewicz * Except at the end of the transfer, amount will be
82900a2430fSAndrzej Pietrasiewicz * equal to the buffer size, which is divisible by
83000a2430fSAndrzej Pietrasiewicz * the bulk-out maxpacket size.
83100a2430fSAndrzej Pietrasiewicz */
83200a2430fSAndrzej Pietrasiewicz set_bulk_out_req_length(common, bh, amount);
83300a2430fSAndrzej Pietrasiewicz if (!start_out_transfer(common, bh))
83400a2430fSAndrzej Pietrasiewicz /* Dunno what to do if common->fsg is NULL */
83500a2430fSAndrzej Pietrasiewicz return -EIO;
83600a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_fill = bh->next;
83700a2430fSAndrzej Pietrasiewicz continue;
83800a2430fSAndrzej Pietrasiewicz }
83900a2430fSAndrzej Pietrasiewicz
84000a2430fSAndrzej Pietrasiewicz /* Write the received data to the backing file */
84100a2430fSAndrzej Pietrasiewicz bh = common->next_buffhd_to_drain;
84200a2430fSAndrzej Pietrasiewicz if (bh->state == BUF_STATE_EMPTY && !get_some_more)
84300a2430fSAndrzej Pietrasiewicz break; /* We stopped early */
844225785aeSAlan Stern
845225785aeSAlan Stern /* Wait for the data to be received */
846225785aeSAlan Stern rc = sleep_thread(common, false, bh);
847225785aeSAlan Stern if (rc)
848225785aeSAlan Stern return rc;
849225785aeSAlan Stern
85000a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_drain = bh->next;
85100a2430fSAndrzej Pietrasiewicz bh->state = BUF_STATE_EMPTY;
85200a2430fSAndrzej Pietrasiewicz
85300a2430fSAndrzej Pietrasiewicz /* Did something go wrong with the transfer? */
85400a2430fSAndrzej Pietrasiewicz if (bh->outreq->status != 0) {
85500a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_COMMUNICATION_FAILURE;
85600a2430fSAndrzej Pietrasiewicz curlun->sense_data_info =
85700a2430fSAndrzej Pietrasiewicz file_offset >> curlun->blkbits;
85800a2430fSAndrzej Pietrasiewicz curlun->info_valid = 1;
85900a2430fSAndrzej Pietrasiewicz break;
86000a2430fSAndrzej Pietrasiewicz }
86100a2430fSAndrzej Pietrasiewicz
86200a2430fSAndrzej Pietrasiewicz amount = bh->outreq->actual;
86300a2430fSAndrzej Pietrasiewicz if (curlun->file_length - file_offset < amount) {
864225785aeSAlan Stern LERROR(curlun, "write %u @ %llu beyond end %llu\n",
86500a2430fSAndrzej Pietrasiewicz amount, (unsigned long long)file_offset,
86600a2430fSAndrzej Pietrasiewicz (unsigned long long)curlun->file_length);
86700a2430fSAndrzej Pietrasiewicz amount = curlun->file_length - file_offset;
86800a2430fSAndrzej Pietrasiewicz }
86900a2430fSAndrzej Pietrasiewicz
870225785aeSAlan Stern /*
871225785aeSAlan Stern * Don't accept excess data. The spec doesn't say
87200a2430fSAndrzej Pietrasiewicz * what to do in this case. We'll ignore the error.
87300a2430fSAndrzej Pietrasiewicz */
87400a2430fSAndrzej Pietrasiewicz amount = min(amount, bh->bulk_out_intended_length);
87500a2430fSAndrzej Pietrasiewicz
87600a2430fSAndrzej Pietrasiewicz /* Don't write a partial block */
87700a2430fSAndrzej Pietrasiewicz amount = round_down(amount, curlun->blksize);
87800a2430fSAndrzej Pietrasiewicz if (amount == 0)
87900a2430fSAndrzej Pietrasiewicz goto empty_write;
88000a2430fSAndrzej Pietrasiewicz
88100a2430fSAndrzej Pietrasiewicz /* Perform the write */
88200a2430fSAndrzej Pietrasiewicz file_offset_tmp = file_offset;
88305a4a33bSChristoph Hellwig nwritten = kernel_write(curlun->filp, bh->buf, amount,
88405a4a33bSChristoph Hellwig &file_offset_tmp);
88500a2430fSAndrzej Pietrasiewicz VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
88600a2430fSAndrzej Pietrasiewicz (unsigned long long)file_offset, (int)nwritten);
88700a2430fSAndrzej Pietrasiewicz if (signal_pending(current))
88800a2430fSAndrzej Pietrasiewicz return -EINTR; /* Interrupted! */
88900a2430fSAndrzej Pietrasiewicz
89000a2430fSAndrzej Pietrasiewicz if (nwritten < 0) {
89100a2430fSAndrzej Pietrasiewicz LDBG(curlun, "error in file write: %d\n",
89200a2430fSAndrzej Pietrasiewicz (int) nwritten);
89300a2430fSAndrzej Pietrasiewicz nwritten = 0;
89400a2430fSAndrzej Pietrasiewicz } else if (nwritten < amount) {
89500a2430fSAndrzej Pietrasiewicz LDBG(curlun, "partial file write: %d/%u\n",
89600a2430fSAndrzej Pietrasiewicz (int) nwritten, amount);
89700a2430fSAndrzej Pietrasiewicz nwritten = round_down(nwritten, curlun->blksize);
89800a2430fSAndrzej Pietrasiewicz }
89900a2430fSAndrzej Pietrasiewicz file_offset += nwritten;
90000a2430fSAndrzej Pietrasiewicz amount_left_to_write -= nwritten;
90100a2430fSAndrzej Pietrasiewicz common->residue -= nwritten;
90200a2430fSAndrzej Pietrasiewicz
90300a2430fSAndrzej Pietrasiewicz /* If an error occurred, report it and its position */
90400a2430fSAndrzej Pietrasiewicz if (nwritten < amount) {
90500a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_WRITE_ERROR;
90600a2430fSAndrzej Pietrasiewicz curlun->sense_data_info =
90700a2430fSAndrzej Pietrasiewicz file_offset >> curlun->blkbits;
90800a2430fSAndrzej Pietrasiewicz curlun->info_valid = 1;
90900a2430fSAndrzej Pietrasiewicz break;
91000a2430fSAndrzej Pietrasiewicz }
91100a2430fSAndrzej Pietrasiewicz
91200a2430fSAndrzej Pietrasiewicz empty_write:
91300a2430fSAndrzej Pietrasiewicz /* Did the host decide to stop early? */
91400a2430fSAndrzej Pietrasiewicz if (bh->outreq->actual < bh->bulk_out_intended_length) {
91500a2430fSAndrzej Pietrasiewicz common->short_packet_received = 1;
91600a2430fSAndrzej Pietrasiewicz break;
91700a2430fSAndrzej Pietrasiewicz }
91800a2430fSAndrzej Pietrasiewicz }
91900a2430fSAndrzej Pietrasiewicz
92000a2430fSAndrzej Pietrasiewicz return -EIO; /* No default reply */
92100a2430fSAndrzej Pietrasiewicz }
92200a2430fSAndrzej Pietrasiewicz
92300a2430fSAndrzej Pietrasiewicz
92400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
92500a2430fSAndrzej Pietrasiewicz
do_synchronize_cache(struct fsg_common * common)92600a2430fSAndrzej Pietrasiewicz static int do_synchronize_cache(struct fsg_common *common)
92700a2430fSAndrzej Pietrasiewicz {
92800a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
92900a2430fSAndrzej Pietrasiewicz int rc;
93000a2430fSAndrzej Pietrasiewicz
93100a2430fSAndrzej Pietrasiewicz /* We ignore the requested LBA and write out all file's
93200a2430fSAndrzej Pietrasiewicz * dirty data buffers. */
93300a2430fSAndrzej Pietrasiewicz rc = fsg_lun_fsync_sub(curlun);
93400a2430fSAndrzej Pietrasiewicz if (rc)
93500a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_WRITE_ERROR;
93600a2430fSAndrzej Pietrasiewicz return 0;
93700a2430fSAndrzej Pietrasiewicz }
93800a2430fSAndrzej Pietrasiewicz
93900a2430fSAndrzej Pietrasiewicz
94000a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
94100a2430fSAndrzej Pietrasiewicz
invalidate_sub(struct fsg_lun * curlun)94200a2430fSAndrzej Pietrasiewicz static void invalidate_sub(struct fsg_lun *curlun)
94300a2430fSAndrzej Pietrasiewicz {
94400a2430fSAndrzej Pietrasiewicz struct file *filp = curlun->filp;
94500a2430fSAndrzej Pietrasiewicz struct inode *inode = file_inode(filp);
94655c3e571SAlan Stern unsigned long __maybe_unused rc;
94700a2430fSAndrzej Pietrasiewicz
94800a2430fSAndrzej Pietrasiewicz rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
94900a2430fSAndrzej Pietrasiewicz VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
95000a2430fSAndrzej Pietrasiewicz }
95100a2430fSAndrzej Pietrasiewicz
do_verify(struct fsg_common * common)95200a2430fSAndrzej Pietrasiewicz static int do_verify(struct fsg_common *common)
95300a2430fSAndrzej Pietrasiewicz {
95400a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
95500a2430fSAndrzej Pietrasiewicz u32 lba;
95600a2430fSAndrzej Pietrasiewicz u32 verification_length;
95700a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh = common->next_buffhd_to_fill;
95800a2430fSAndrzej Pietrasiewicz loff_t file_offset, file_offset_tmp;
95900a2430fSAndrzej Pietrasiewicz u32 amount_left;
96000a2430fSAndrzej Pietrasiewicz unsigned int amount;
96100a2430fSAndrzej Pietrasiewicz ssize_t nread;
96200a2430fSAndrzej Pietrasiewicz
96300a2430fSAndrzej Pietrasiewicz /*
96400a2430fSAndrzej Pietrasiewicz * Get the starting Logical Block Address and check that it's
96500a2430fSAndrzej Pietrasiewicz * not too big.
96600a2430fSAndrzej Pietrasiewicz */
96700a2430fSAndrzej Pietrasiewicz lba = get_unaligned_be32(&common->cmnd[2]);
96800a2430fSAndrzej Pietrasiewicz if (lba >= curlun->num_sectors) {
96900a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
97000a2430fSAndrzej Pietrasiewicz return -EINVAL;
97100a2430fSAndrzej Pietrasiewicz }
97200a2430fSAndrzej Pietrasiewicz
97300a2430fSAndrzej Pietrasiewicz /*
97400a2430fSAndrzej Pietrasiewicz * We allow DPO (Disable Page Out = don't save data in the
97500a2430fSAndrzej Pietrasiewicz * cache) but we don't implement it.
97600a2430fSAndrzej Pietrasiewicz */
97700a2430fSAndrzej Pietrasiewicz if (common->cmnd[1] & ~0x10) {
97800a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
97900a2430fSAndrzej Pietrasiewicz return -EINVAL;
98000a2430fSAndrzej Pietrasiewicz }
98100a2430fSAndrzej Pietrasiewicz
98200a2430fSAndrzej Pietrasiewicz verification_length = get_unaligned_be16(&common->cmnd[7]);
98300a2430fSAndrzej Pietrasiewicz if (unlikely(verification_length == 0))
98400a2430fSAndrzej Pietrasiewicz return -EIO; /* No default reply */
98500a2430fSAndrzej Pietrasiewicz
98600a2430fSAndrzej Pietrasiewicz /* Prepare to carry out the file verify */
98700a2430fSAndrzej Pietrasiewicz amount_left = verification_length << curlun->blkbits;
98800a2430fSAndrzej Pietrasiewicz file_offset = ((loff_t) lba) << curlun->blkbits;
98900a2430fSAndrzej Pietrasiewicz
99000a2430fSAndrzej Pietrasiewicz /* Write out all the dirty buffers before invalidating them */
99100a2430fSAndrzej Pietrasiewicz fsg_lun_fsync_sub(curlun);
99200a2430fSAndrzej Pietrasiewicz if (signal_pending(current))
99300a2430fSAndrzej Pietrasiewicz return -EINTR;
99400a2430fSAndrzej Pietrasiewicz
99500a2430fSAndrzej Pietrasiewicz invalidate_sub(curlun);
99600a2430fSAndrzej Pietrasiewicz if (signal_pending(current))
99700a2430fSAndrzej Pietrasiewicz return -EINTR;
99800a2430fSAndrzej Pietrasiewicz
99900a2430fSAndrzej Pietrasiewicz /* Just try to read the requested blocks */
100000a2430fSAndrzej Pietrasiewicz while (amount_left > 0) {
100100a2430fSAndrzej Pietrasiewicz /*
100200a2430fSAndrzej Pietrasiewicz * Figure out how much we need to read:
100300a2430fSAndrzej Pietrasiewicz * Try to read the remaining amount, but not more than
100400a2430fSAndrzej Pietrasiewicz * the buffer size.
100500a2430fSAndrzej Pietrasiewicz * And don't try to read past the end of the file.
100600a2430fSAndrzej Pietrasiewicz */
100700a2430fSAndrzej Pietrasiewicz amount = min(amount_left, FSG_BUFLEN);
100800a2430fSAndrzej Pietrasiewicz amount = min((loff_t)amount,
100900a2430fSAndrzej Pietrasiewicz curlun->file_length - file_offset);
101000a2430fSAndrzej Pietrasiewicz if (amount == 0) {
101100a2430fSAndrzej Pietrasiewicz curlun->sense_data =
101200a2430fSAndrzej Pietrasiewicz SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
101300a2430fSAndrzej Pietrasiewicz curlun->sense_data_info =
101400a2430fSAndrzej Pietrasiewicz file_offset >> curlun->blkbits;
101500a2430fSAndrzej Pietrasiewicz curlun->info_valid = 1;
101600a2430fSAndrzej Pietrasiewicz break;
101700a2430fSAndrzej Pietrasiewicz }
101800a2430fSAndrzej Pietrasiewicz
101900a2430fSAndrzej Pietrasiewicz /* Perform the read */
102000a2430fSAndrzej Pietrasiewicz file_offset_tmp = file_offset;
102105a4a33bSChristoph Hellwig nread = kernel_read(curlun->filp, bh->buf, amount,
102205a4a33bSChristoph Hellwig &file_offset_tmp);
102300a2430fSAndrzej Pietrasiewicz VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
102400a2430fSAndrzej Pietrasiewicz (unsigned long long) file_offset,
102500a2430fSAndrzej Pietrasiewicz (int) nread);
102600a2430fSAndrzej Pietrasiewicz if (signal_pending(current))
102700a2430fSAndrzej Pietrasiewicz return -EINTR;
102800a2430fSAndrzej Pietrasiewicz
102900a2430fSAndrzej Pietrasiewicz if (nread < 0) {
103000a2430fSAndrzej Pietrasiewicz LDBG(curlun, "error in file verify: %d\n", (int)nread);
103100a2430fSAndrzej Pietrasiewicz nread = 0;
103200a2430fSAndrzej Pietrasiewicz } else if (nread < amount) {
103300a2430fSAndrzej Pietrasiewicz LDBG(curlun, "partial file verify: %d/%u\n",
103400a2430fSAndrzej Pietrasiewicz (int)nread, amount);
103500a2430fSAndrzej Pietrasiewicz nread = round_down(nread, curlun->blksize);
103600a2430fSAndrzej Pietrasiewicz }
103700a2430fSAndrzej Pietrasiewicz if (nread == 0) {
103800a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
103900a2430fSAndrzej Pietrasiewicz curlun->sense_data_info =
104000a2430fSAndrzej Pietrasiewicz file_offset >> curlun->blkbits;
104100a2430fSAndrzej Pietrasiewicz curlun->info_valid = 1;
104200a2430fSAndrzej Pietrasiewicz break;
104300a2430fSAndrzej Pietrasiewicz }
104400a2430fSAndrzej Pietrasiewicz file_offset += nread;
104500a2430fSAndrzej Pietrasiewicz amount_left -= nread;
104600a2430fSAndrzej Pietrasiewicz }
104700a2430fSAndrzej Pietrasiewicz return 0;
104800a2430fSAndrzej Pietrasiewicz }
104900a2430fSAndrzej Pietrasiewicz
105000a2430fSAndrzej Pietrasiewicz
105100a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
105200a2430fSAndrzej Pietrasiewicz
do_inquiry(struct fsg_common * common,struct fsg_buffhd * bh)105300a2430fSAndrzej Pietrasiewicz static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
105400a2430fSAndrzej Pietrasiewicz {
105500a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
105600a2430fSAndrzej Pietrasiewicz u8 *buf = (u8 *) bh->buf;
105700a2430fSAndrzej Pietrasiewicz
105800a2430fSAndrzej Pietrasiewicz if (!curlun) { /* Unsupported LUNs are okay */
105900a2430fSAndrzej Pietrasiewicz common->bad_lun_okay = 1;
106000a2430fSAndrzej Pietrasiewicz memset(buf, 0, 36);
1061fddc26f5STal Shorer buf[0] = TYPE_NO_LUN; /* Unsupported, no device-type */
106200a2430fSAndrzej Pietrasiewicz buf[4] = 31; /* Additional length */
106300a2430fSAndrzej Pietrasiewicz return 36;
106400a2430fSAndrzej Pietrasiewicz }
106500a2430fSAndrzej Pietrasiewicz
106600a2430fSAndrzej Pietrasiewicz buf[0] = curlun->cdrom ? TYPE_ROM : TYPE_DISK;
106700a2430fSAndrzej Pietrasiewicz buf[1] = curlun->removable ? 0x80 : 0;
106800a2430fSAndrzej Pietrasiewicz buf[2] = 2; /* ANSI SCSI level 2 */
106900a2430fSAndrzej Pietrasiewicz buf[3] = 2; /* SCSI-2 INQUIRY data format */
107000a2430fSAndrzej Pietrasiewicz buf[4] = 31; /* Additional length */
107100a2430fSAndrzej Pietrasiewicz buf[5] = 0; /* No special options */
107200a2430fSAndrzej Pietrasiewicz buf[6] = 0;
107300a2430fSAndrzej Pietrasiewicz buf[7] = 0;
10746ac47090SPhilipp Gesang if (curlun->inquiry_string[0])
10756ac47090SPhilipp Gesang memcpy(buf + 8, curlun->inquiry_string,
10766ac47090SPhilipp Gesang sizeof(curlun->inquiry_string));
10776ac47090SPhilipp Gesang else
10786ac47090SPhilipp Gesang memcpy(buf + 8, common->inquiry_string,
10796ac47090SPhilipp Gesang sizeof(common->inquiry_string));
108000a2430fSAndrzej Pietrasiewicz return 36;
108100a2430fSAndrzej Pietrasiewicz }
108200a2430fSAndrzej Pietrasiewicz
do_request_sense(struct fsg_common * common,struct fsg_buffhd * bh)108300a2430fSAndrzej Pietrasiewicz static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
108400a2430fSAndrzej Pietrasiewicz {
108500a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
108600a2430fSAndrzej Pietrasiewicz u8 *buf = (u8 *) bh->buf;
108700a2430fSAndrzej Pietrasiewicz u32 sd, sdinfo;
108800a2430fSAndrzej Pietrasiewicz int valid;
108900a2430fSAndrzej Pietrasiewicz
109000a2430fSAndrzej Pietrasiewicz /*
109100a2430fSAndrzej Pietrasiewicz * From the SCSI-2 spec., section 7.9 (Unit attention condition):
109200a2430fSAndrzej Pietrasiewicz *
109300a2430fSAndrzej Pietrasiewicz * If a REQUEST SENSE command is received from an initiator
109400a2430fSAndrzej Pietrasiewicz * with a pending unit attention condition (before the target
109500a2430fSAndrzej Pietrasiewicz * generates the contingent allegiance condition), then the
109600a2430fSAndrzej Pietrasiewicz * target shall either:
109700a2430fSAndrzej Pietrasiewicz * a) report any pending sense data and preserve the unit
109800a2430fSAndrzej Pietrasiewicz * attention condition on the logical unit, or,
109900a2430fSAndrzej Pietrasiewicz * b) report the unit attention condition, may discard any
110000a2430fSAndrzej Pietrasiewicz * pending sense data, and clear the unit attention
110100a2430fSAndrzej Pietrasiewicz * condition on the logical unit for that initiator.
110200a2430fSAndrzej Pietrasiewicz *
110300a2430fSAndrzej Pietrasiewicz * FSG normally uses option a); enable this code to use option b).
110400a2430fSAndrzej Pietrasiewicz */
110500a2430fSAndrzej Pietrasiewicz #if 0
110600a2430fSAndrzej Pietrasiewicz if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
110700a2430fSAndrzej Pietrasiewicz curlun->sense_data = curlun->unit_attention_data;
110800a2430fSAndrzej Pietrasiewicz curlun->unit_attention_data = SS_NO_SENSE;
110900a2430fSAndrzej Pietrasiewicz }
111000a2430fSAndrzej Pietrasiewicz #endif
111100a2430fSAndrzej Pietrasiewicz
111200a2430fSAndrzej Pietrasiewicz if (!curlun) { /* Unsupported LUNs are okay */
111300a2430fSAndrzej Pietrasiewicz common->bad_lun_okay = 1;
111400a2430fSAndrzej Pietrasiewicz sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
111500a2430fSAndrzej Pietrasiewicz sdinfo = 0;
111600a2430fSAndrzej Pietrasiewicz valid = 0;
111700a2430fSAndrzej Pietrasiewicz } else {
111800a2430fSAndrzej Pietrasiewicz sd = curlun->sense_data;
111900a2430fSAndrzej Pietrasiewicz sdinfo = curlun->sense_data_info;
112000a2430fSAndrzej Pietrasiewicz valid = curlun->info_valid << 7;
112100a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_NO_SENSE;
112200a2430fSAndrzej Pietrasiewicz curlun->sense_data_info = 0;
112300a2430fSAndrzej Pietrasiewicz curlun->info_valid = 0;
112400a2430fSAndrzej Pietrasiewicz }
112500a2430fSAndrzej Pietrasiewicz
112600a2430fSAndrzej Pietrasiewicz memset(buf, 0, 18);
112700a2430fSAndrzej Pietrasiewicz buf[0] = valid | 0x70; /* Valid, current error */
112800a2430fSAndrzej Pietrasiewicz buf[2] = SK(sd);
112900a2430fSAndrzej Pietrasiewicz put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
113000a2430fSAndrzej Pietrasiewicz buf[7] = 18 - 8; /* Additional sense length */
113100a2430fSAndrzej Pietrasiewicz buf[12] = ASC(sd);
113200a2430fSAndrzej Pietrasiewicz buf[13] = ASCQ(sd);
113300a2430fSAndrzej Pietrasiewicz return 18;
113400a2430fSAndrzej Pietrasiewicz }
113500a2430fSAndrzej Pietrasiewicz
do_read_capacity(struct fsg_common * common,struct fsg_buffhd * bh)113600a2430fSAndrzej Pietrasiewicz static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
113700a2430fSAndrzej Pietrasiewicz {
113800a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
113900a2430fSAndrzej Pietrasiewicz u32 lba = get_unaligned_be32(&common->cmnd[2]);
114000a2430fSAndrzej Pietrasiewicz int pmi = common->cmnd[8];
114100a2430fSAndrzej Pietrasiewicz u8 *buf = (u8 *)bh->buf;
1142bedbac5fSNikita Yushchenko u32 max_lba;
114300a2430fSAndrzej Pietrasiewicz
114400a2430fSAndrzej Pietrasiewicz /* Check the PMI and LBA fields */
114500a2430fSAndrzej Pietrasiewicz if (pmi > 1 || (pmi == 0 && lba != 0)) {
114600a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
114700a2430fSAndrzej Pietrasiewicz return -EINVAL;
114800a2430fSAndrzej Pietrasiewicz }
114900a2430fSAndrzej Pietrasiewicz
1150bedbac5fSNikita Yushchenko if (curlun->num_sectors < 0x100000000ULL)
1151bedbac5fSNikita Yushchenko max_lba = curlun->num_sectors - 1;
1152bedbac5fSNikita Yushchenko else
1153bedbac5fSNikita Yushchenko max_lba = 0xffffffff;
1154bedbac5fSNikita Yushchenko put_unaligned_be32(max_lba, &buf[0]); /* Max logical block */
115500a2430fSAndrzej Pietrasiewicz put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
115600a2430fSAndrzej Pietrasiewicz return 8;
115700a2430fSAndrzej Pietrasiewicz }
115800a2430fSAndrzej Pietrasiewicz
do_read_capacity_16(struct fsg_common * common,struct fsg_buffhd * bh)1159bedbac5fSNikita Yushchenko static int do_read_capacity_16(struct fsg_common *common, struct fsg_buffhd *bh)
1160bedbac5fSNikita Yushchenko {
1161bedbac5fSNikita Yushchenko struct fsg_lun *curlun = common->curlun;
1162bedbac5fSNikita Yushchenko u64 lba = get_unaligned_be64(&common->cmnd[2]);
1163bedbac5fSNikita Yushchenko int pmi = common->cmnd[14];
1164bedbac5fSNikita Yushchenko u8 *buf = (u8 *)bh->buf;
1165bedbac5fSNikita Yushchenko
1166bedbac5fSNikita Yushchenko /* Check the PMI and LBA fields */
1167bedbac5fSNikita Yushchenko if (pmi > 1 || (pmi == 0 && lba != 0)) {
1168bedbac5fSNikita Yushchenko curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1169bedbac5fSNikita Yushchenko return -EINVAL;
1170bedbac5fSNikita Yushchenko }
1171bedbac5fSNikita Yushchenko
1172bedbac5fSNikita Yushchenko put_unaligned_be64(curlun->num_sectors - 1, &buf[0]);
1173bedbac5fSNikita Yushchenko /* Max logical block */
1174bedbac5fSNikita Yushchenko put_unaligned_be32(curlun->blksize, &buf[8]); /* Block length */
1175bedbac5fSNikita Yushchenko
1176bedbac5fSNikita Yushchenko /* It is safe to keep other fields zeroed */
1177bedbac5fSNikita Yushchenko memset(&buf[12], 0, 32 - 12);
1178bedbac5fSNikita Yushchenko return 32;
1179bedbac5fSNikita Yushchenko }
1180bedbac5fSNikita Yushchenko
do_read_header(struct fsg_common * common,struct fsg_buffhd * bh)118100a2430fSAndrzej Pietrasiewicz static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
118200a2430fSAndrzej Pietrasiewicz {
118300a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
118400a2430fSAndrzej Pietrasiewicz int msf = common->cmnd[1] & 0x02;
118500a2430fSAndrzej Pietrasiewicz u32 lba = get_unaligned_be32(&common->cmnd[2]);
118600a2430fSAndrzej Pietrasiewicz u8 *buf = (u8 *)bh->buf;
118700a2430fSAndrzej Pietrasiewicz
118800a2430fSAndrzej Pietrasiewicz if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
118900a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
119000a2430fSAndrzej Pietrasiewicz return -EINVAL;
119100a2430fSAndrzej Pietrasiewicz }
119200a2430fSAndrzej Pietrasiewicz if (lba >= curlun->num_sectors) {
119300a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
119400a2430fSAndrzej Pietrasiewicz return -EINVAL;
119500a2430fSAndrzej Pietrasiewicz }
119600a2430fSAndrzej Pietrasiewicz
119700a2430fSAndrzej Pietrasiewicz memset(buf, 0, 8);
119800a2430fSAndrzej Pietrasiewicz buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
119900a2430fSAndrzej Pietrasiewicz store_cdrom_address(&buf[4], msf, lba);
120000a2430fSAndrzej Pietrasiewicz return 8;
120100a2430fSAndrzej Pietrasiewicz }
120200a2430fSAndrzej Pietrasiewicz
do_read_toc(struct fsg_common * common,struct fsg_buffhd * bh)120300a2430fSAndrzej Pietrasiewicz static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
120400a2430fSAndrzej Pietrasiewicz {
120500a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
120600a2430fSAndrzej Pietrasiewicz int msf = common->cmnd[1] & 0x02;
120700a2430fSAndrzej Pietrasiewicz int start_track = common->cmnd[6];
120800a2430fSAndrzej Pietrasiewicz u8 *buf = (u8 *)bh->buf;
120989ada0feSRoger Quadros u8 format;
121089ada0feSRoger Quadros int i, len;
121100a2430fSAndrzej Pietrasiewicz
12123b91edd6SNeal Liu format = common->cmnd[2] & 0xf;
12133b91edd6SNeal Liu
121400a2430fSAndrzej Pietrasiewicz if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
12153b91edd6SNeal Liu (start_track > 1 && format != 0x1)) {
121600a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
121700a2430fSAndrzej Pietrasiewicz return -EINVAL;
121800a2430fSAndrzej Pietrasiewicz }
121900a2430fSAndrzej Pietrasiewicz
122089ada0feSRoger Quadros /*
122189ada0feSRoger Quadros * Check if CDB is old style SFF-8020i
122289ada0feSRoger Quadros * i.e. format is in 2 MSBs of byte 9
122389ada0feSRoger Quadros * Mac OS-X host sends us this.
122489ada0feSRoger Quadros */
122589ada0feSRoger Quadros if (format == 0)
122689ada0feSRoger Quadros format = (common->cmnd[9] >> 6) & 0x3;
122789ada0feSRoger Quadros
122889ada0feSRoger Quadros switch (format) {
12293b91edd6SNeal Liu case 0: /* Formatted TOC */
12303b91edd6SNeal Liu case 1: /* Multi-session info */
123189ada0feSRoger Quadros len = 4 + 2*8; /* 4 byte header + 2 descriptors */
123289ada0feSRoger Quadros memset(buf, 0, len);
123389ada0feSRoger Quadros buf[1] = len - 2; /* TOC Length excludes length field */
123400a2430fSAndrzej Pietrasiewicz buf[2] = 1; /* First track number */
123500a2430fSAndrzej Pietrasiewicz buf[3] = 1; /* Last track number */
123600a2430fSAndrzej Pietrasiewicz buf[5] = 0x16; /* Data track, copying allowed */
123700a2430fSAndrzej Pietrasiewicz buf[6] = 0x01; /* Only track is number 1 */
123800a2430fSAndrzej Pietrasiewicz store_cdrom_address(&buf[8], msf, 0);
123900a2430fSAndrzej Pietrasiewicz
124000a2430fSAndrzej Pietrasiewicz buf[13] = 0x16; /* Lead-out track is data */
124100a2430fSAndrzej Pietrasiewicz buf[14] = 0xAA; /* Lead-out track number */
124200a2430fSAndrzej Pietrasiewicz store_cdrom_address(&buf[16], msf, curlun->num_sectors);
124389ada0feSRoger Quadros return len;
124489ada0feSRoger Quadros
124589ada0feSRoger Quadros case 2:
124689ada0feSRoger Quadros /* Raw TOC */
124789ada0feSRoger Quadros len = 4 + 3*11; /* 4 byte header + 3 descriptors */
124889ada0feSRoger Quadros memset(buf, 0, len); /* Header + A0, A1 & A2 descriptors */
124989ada0feSRoger Quadros buf[1] = len - 2; /* TOC Length excludes length field */
125089ada0feSRoger Quadros buf[2] = 1; /* First complete session */
125189ada0feSRoger Quadros buf[3] = 1; /* Last complete session */
125289ada0feSRoger Quadros
125389ada0feSRoger Quadros buf += 4;
125489ada0feSRoger Quadros /* fill in A0, A1 and A2 points */
125589ada0feSRoger Quadros for (i = 0; i < 3; i++) {
125689ada0feSRoger Quadros buf[0] = 1; /* Session number */
125789ada0feSRoger Quadros buf[1] = 0x16; /* Data track, copying allowed */
125889ada0feSRoger Quadros /* 2 - Track number 0 -> TOC */
125989ada0feSRoger Quadros buf[3] = 0xA0 + i; /* A0, A1, A2 point */
126089ada0feSRoger Quadros /* 4, 5, 6 - Min, sec, frame is zero */
126189ada0feSRoger Quadros buf[8] = 1; /* Pmin: last track number */
126289ada0feSRoger Quadros buf += 11; /* go to next track descriptor */
126389ada0feSRoger Quadros }
126489ada0feSRoger Quadros buf -= 11; /* go back to A2 descriptor */
126589ada0feSRoger Quadros
126689ada0feSRoger Quadros /* For A2, 7, 8, 9, 10 - zero, Pmin, Psec, Pframe of Lead out */
126789ada0feSRoger Quadros store_cdrom_address(&buf[7], msf, curlun->num_sectors);
126889ada0feSRoger Quadros return len;
126989ada0feSRoger Quadros
127089ada0feSRoger Quadros default:
12713b91edd6SNeal Liu /* PMA, ATIP, CD-TEXT not supported/required */
127289ada0feSRoger Quadros curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
127389ada0feSRoger Quadros return -EINVAL;
127489ada0feSRoger Quadros }
127500a2430fSAndrzej Pietrasiewicz }
127600a2430fSAndrzej Pietrasiewicz
do_mode_sense(struct fsg_common * common,struct fsg_buffhd * bh)127700a2430fSAndrzej Pietrasiewicz static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
127800a2430fSAndrzej Pietrasiewicz {
127900a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
128000a2430fSAndrzej Pietrasiewicz int mscmnd = common->cmnd[0];
128100a2430fSAndrzej Pietrasiewicz u8 *buf = (u8 *) bh->buf;
128200a2430fSAndrzej Pietrasiewicz u8 *buf0 = buf;
128300a2430fSAndrzej Pietrasiewicz int pc, page_code;
128400a2430fSAndrzej Pietrasiewicz int changeable_values, all_pages;
128500a2430fSAndrzej Pietrasiewicz int valid_page = 0;
128600a2430fSAndrzej Pietrasiewicz int len, limit;
128700a2430fSAndrzej Pietrasiewicz
128800a2430fSAndrzej Pietrasiewicz if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
128900a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
129000a2430fSAndrzej Pietrasiewicz return -EINVAL;
129100a2430fSAndrzej Pietrasiewicz }
129200a2430fSAndrzej Pietrasiewicz pc = common->cmnd[2] >> 6;
129300a2430fSAndrzej Pietrasiewicz page_code = common->cmnd[2] & 0x3f;
129400a2430fSAndrzej Pietrasiewicz if (pc == 3) {
129500a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
129600a2430fSAndrzej Pietrasiewicz return -EINVAL;
129700a2430fSAndrzej Pietrasiewicz }
129800a2430fSAndrzej Pietrasiewicz changeable_values = (pc == 1);
129900a2430fSAndrzej Pietrasiewicz all_pages = (page_code == 0x3f);
130000a2430fSAndrzej Pietrasiewicz
130100a2430fSAndrzej Pietrasiewicz /*
130200a2430fSAndrzej Pietrasiewicz * Write the mode parameter header. Fixed values are: default
130300a2430fSAndrzej Pietrasiewicz * medium type, no cache control (DPOFUA), and no block descriptors.
130400a2430fSAndrzej Pietrasiewicz * The only variable value is the WriteProtect bit. We will fill in
130500a2430fSAndrzej Pietrasiewicz * the mode data length later.
130600a2430fSAndrzej Pietrasiewicz */
130700a2430fSAndrzej Pietrasiewicz memset(buf, 0, 8);
130800a2430fSAndrzej Pietrasiewicz if (mscmnd == MODE_SENSE) {
130900a2430fSAndrzej Pietrasiewicz buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
131000a2430fSAndrzej Pietrasiewicz buf += 4;
131100a2430fSAndrzej Pietrasiewicz limit = 255;
131200a2430fSAndrzej Pietrasiewicz } else { /* MODE_SENSE_10 */
131300a2430fSAndrzej Pietrasiewicz buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
131400a2430fSAndrzej Pietrasiewicz buf += 8;
131500a2430fSAndrzej Pietrasiewicz limit = 65535; /* Should really be FSG_BUFLEN */
131600a2430fSAndrzej Pietrasiewicz }
131700a2430fSAndrzej Pietrasiewicz
131800a2430fSAndrzej Pietrasiewicz /* No block descriptors */
131900a2430fSAndrzej Pietrasiewicz
132000a2430fSAndrzej Pietrasiewicz /*
132100a2430fSAndrzej Pietrasiewicz * The mode pages, in numerical order. The only page we support
132200a2430fSAndrzej Pietrasiewicz * is the Caching page.
132300a2430fSAndrzej Pietrasiewicz */
132400a2430fSAndrzej Pietrasiewicz if (page_code == 0x08 || all_pages) {
132500a2430fSAndrzej Pietrasiewicz valid_page = 1;
132600a2430fSAndrzej Pietrasiewicz buf[0] = 0x08; /* Page code */
132700a2430fSAndrzej Pietrasiewicz buf[1] = 10; /* Page length */
132800a2430fSAndrzej Pietrasiewicz memset(buf+2, 0, 10); /* None of the fields are changeable */
132900a2430fSAndrzej Pietrasiewicz
133000a2430fSAndrzej Pietrasiewicz if (!changeable_values) {
133100a2430fSAndrzej Pietrasiewicz buf[2] = 0x04; /* Write cache enable, */
133200a2430fSAndrzej Pietrasiewicz /* Read cache not disabled */
133300a2430fSAndrzej Pietrasiewicz /* No cache retention priorities */
133400a2430fSAndrzej Pietrasiewicz put_unaligned_be16(0xffff, &buf[4]);
133500a2430fSAndrzej Pietrasiewicz /* Don't disable prefetch */
133600a2430fSAndrzej Pietrasiewicz /* Minimum prefetch = 0 */
133700a2430fSAndrzej Pietrasiewicz put_unaligned_be16(0xffff, &buf[8]);
133800a2430fSAndrzej Pietrasiewicz /* Maximum prefetch */
133900a2430fSAndrzej Pietrasiewicz put_unaligned_be16(0xffff, &buf[10]);
134000a2430fSAndrzej Pietrasiewicz /* Maximum prefetch ceiling */
134100a2430fSAndrzej Pietrasiewicz }
134200a2430fSAndrzej Pietrasiewicz buf += 12;
134300a2430fSAndrzej Pietrasiewicz }
134400a2430fSAndrzej Pietrasiewicz
134500a2430fSAndrzej Pietrasiewicz /*
134600a2430fSAndrzej Pietrasiewicz * Check that a valid page was requested and the mode data length
134700a2430fSAndrzej Pietrasiewicz * isn't too long.
134800a2430fSAndrzej Pietrasiewicz */
134900a2430fSAndrzej Pietrasiewicz len = buf - buf0;
135000a2430fSAndrzej Pietrasiewicz if (!valid_page || len > limit) {
135100a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
135200a2430fSAndrzej Pietrasiewicz return -EINVAL;
135300a2430fSAndrzej Pietrasiewicz }
135400a2430fSAndrzej Pietrasiewicz
135500a2430fSAndrzej Pietrasiewicz /* Store the mode data length */
135600a2430fSAndrzej Pietrasiewicz if (mscmnd == MODE_SENSE)
135700a2430fSAndrzej Pietrasiewicz buf0[0] = len - 1;
135800a2430fSAndrzej Pietrasiewicz else
135900a2430fSAndrzej Pietrasiewicz put_unaligned_be16(len - 2, buf0);
136000a2430fSAndrzej Pietrasiewicz return len;
136100a2430fSAndrzej Pietrasiewicz }
136200a2430fSAndrzej Pietrasiewicz
do_start_stop(struct fsg_common * common)136300a2430fSAndrzej Pietrasiewicz static int do_start_stop(struct fsg_common *common)
136400a2430fSAndrzej Pietrasiewicz {
136500a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
136600a2430fSAndrzej Pietrasiewicz int loej, start;
136700a2430fSAndrzej Pietrasiewicz
136800a2430fSAndrzej Pietrasiewicz if (!curlun) {
136900a2430fSAndrzej Pietrasiewicz return -EINVAL;
137000a2430fSAndrzej Pietrasiewicz } else if (!curlun->removable) {
137100a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_COMMAND;
137200a2430fSAndrzej Pietrasiewicz return -EINVAL;
137300a2430fSAndrzej Pietrasiewicz } else if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
137400a2430fSAndrzej Pietrasiewicz (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
137500a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
137600a2430fSAndrzej Pietrasiewicz return -EINVAL;
137700a2430fSAndrzej Pietrasiewicz }
137800a2430fSAndrzej Pietrasiewicz
137900a2430fSAndrzej Pietrasiewicz loej = common->cmnd[4] & 0x02;
138000a2430fSAndrzej Pietrasiewicz start = common->cmnd[4] & 0x01;
138100a2430fSAndrzej Pietrasiewicz
138200a2430fSAndrzej Pietrasiewicz /*
138300a2430fSAndrzej Pietrasiewicz * Our emulation doesn't support mounting; the medium is
138400a2430fSAndrzej Pietrasiewicz * available for use as soon as it is loaded.
138500a2430fSAndrzej Pietrasiewicz */
138600a2430fSAndrzej Pietrasiewicz if (start) {
138700a2430fSAndrzej Pietrasiewicz if (!fsg_lun_is_open(curlun)) {
138800a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
138900a2430fSAndrzej Pietrasiewicz return -EINVAL;
139000a2430fSAndrzej Pietrasiewicz }
139100a2430fSAndrzej Pietrasiewicz return 0;
139200a2430fSAndrzej Pietrasiewicz }
139300a2430fSAndrzej Pietrasiewicz
139400a2430fSAndrzej Pietrasiewicz /* Are we allowed to unload the media? */
139500a2430fSAndrzej Pietrasiewicz if (curlun->prevent_medium_removal) {
139600a2430fSAndrzej Pietrasiewicz LDBG(curlun, "unload attempt prevented\n");
139700a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
139800a2430fSAndrzej Pietrasiewicz return -EINVAL;
139900a2430fSAndrzej Pietrasiewicz }
140000a2430fSAndrzej Pietrasiewicz
140100a2430fSAndrzej Pietrasiewicz if (!loej)
140200a2430fSAndrzej Pietrasiewicz return 0;
140300a2430fSAndrzej Pietrasiewicz
140400a2430fSAndrzej Pietrasiewicz up_read(&common->filesem);
140500a2430fSAndrzej Pietrasiewicz down_write(&common->filesem);
140600a2430fSAndrzej Pietrasiewicz fsg_lun_close(curlun);
140700a2430fSAndrzej Pietrasiewicz up_write(&common->filesem);
140800a2430fSAndrzej Pietrasiewicz down_read(&common->filesem);
140900a2430fSAndrzej Pietrasiewicz
141000a2430fSAndrzej Pietrasiewicz return 0;
141100a2430fSAndrzej Pietrasiewicz }
141200a2430fSAndrzej Pietrasiewicz
do_prevent_allow(struct fsg_common * common)141300a2430fSAndrzej Pietrasiewicz static int do_prevent_allow(struct fsg_common *common)
141400a2430fSAndrzej Pietrasiewicz {
141500a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
141600a2430fSAndrzej Pietrasiewicz int prevent;
141700a2430fSAndrzej Pietrasiewicz
141800a2430fSAndrzej Pietrasiewicz if (!common->curlun) {
141900a2430fSAndrzej Pietrasiewicz return -EINVAL;
142000a2430fSAndrzej Pietrasiewicz } else if (!common->curlun->removable) {
142100a2430fSAndrzej Pietrasiewicz common->curlun->sense_data = SS_INVALID_COMMAND;
142200a2430fSAndrzej Pietrasiewicz return -EINVAL;
142300a2430fSAndrzej Pietrasiewicz }
142400a2430fSAndrzej Pietrasiewicz
142500a2430fSAndrzej Pietrasiewicz prevent = common->cmnd[4] & 0x01;
142600a2430fSAndrzej Pietrasiewicz if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
142700a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
142800a2430fSAndrzej Pietrasiewicz return -EINVAL;
142900a2430fSAndrzej Pietrasiewicz }
143000a2430fSAndrzej Pietrasiewicz
143100a2430fSAndrzej Pietrasiewicz if (curlun->prevent_medium_removal && !prevent)
143200a2430fSAndrzej Pietrasiewicz fsg_lun_fsync_sub(curlun);
143300a2430fSAndrzej Pietrasiewicz curlun->prevent_medium_removal = prevent;
143400a2430fSAndrzej Pietrasiewicz return 0;
143500a2430fSAndrzej Pietrasiewicz }
143600a2430fSAndrzej Pietrasiewicz
do_read_format_capacities(struct fsg_common * common,struct fsg_buffhd * bh)143700a2430fSAndrzej Pietrasiewicz static int do_read_format_capacities(struct fsg_common *common,
143800a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh)
143900a2430fSAndrzej Pietrasiewicz {
144000a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
144100a2430fSAndrzej Pietrasiewicz u8 *buf = (u8 *) bh->buf;
144200a2430fSAndrzej Pietrasiewicz
144300a2430fSAndrzej Pietrasiewicz buf[0] = buf[1] = buf[2] = 0;
144400a2430fSAndrzej Pietrasiewicz buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
144500a2430fSAndrzej Pietrasiewicz buf += 4;
144600a2430fSAndrzej Pietrasiewicz
144700a2430fSAndrzej Pietrasiewicz put_unaligned_be32(curlun->num_sectors, &buf[0]);
144800a2430fSAndrzej Pietrasiewicz /* Number of blocks */
144900a2430fSAndrzej Pietrasiewicz put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
145000a2430fSAndrzej Pietrasiewicz buf[4] = 0x02; /* Current capacity */
145100a2430fSAndrzej Pietrasiewicz return 12;
145200a2430fSAndrzej Pietrasiewicz }
145300a2430fSAndrzej Pietrasiewicz
do_mode_select(struct fsg_common * common,struct fsg_buffhd * bh)145400a2430fSAndrzej Pietrasiewicz static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
145500a2430fSAndrzej Pietrasiewicz {
145600a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
145700a2430fSAndrzej Pietrasiewicz
145800a2430fSAndrzej Pietrasiewicz /* We don't support MODE SELECT */
145900a2430fSAndrzej Pietrasiewicz if (curlun)
146000a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_COMMAND;
146100a2430fSAndrzej Pietrasiewicz return -EINVAL;
146200a2430fSAndrzej Pietrasiewicz }
146300a2430fSAndrzej Pietrasiewicz
146400a2430fSAndrzej Pietrasiewicz
146500a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
146600a2430fSAndrzej Pietrasiewicz
halt_bulk_in_endpoint(struct fsg_dev * fsg)146700a2430fSAndrzej Pietrasiewicz static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
146800a2430fSAndrzej Pietrasiewicz {
146900a2430fSAndrzej Pietrasiewicz int rc;
147000a2430fSAndrzej Pietrasiewicz
147100a2430fSAndrzej Pietrasiewicz rc = fsg_set_halt(fsg, fsg->bulk_in);
147200a2430fSAndrzej Pietrasiewicz if (rc == -EAGAIN)
147300a2430fSAndrzej Pietrasiewicz VDBG(fsg, "delayed bulk-in endpoint halt\n");
147400a2430fSAndrzej Pietrasiewicz while (rc != 0) {
147500a2430fSAndrzej Pietrasiewicz if (rc != -EAGAIN) {
147600a2430fSAndrzej Pietrasiewicz WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
147700a2430fSAndrzej Pietrasiewicz rc = 0;
147800a2430fSAndrzej Pietrasiewicz break;
147900a2430fSAndrzej Pietrasiewicz }
148000a2430fSAndrzej Pietrasiewicz
148100a2430fSAndrzej Pietrasiewicz /* Wait for a short time and then try again */
148200a2430fSAndrzej Pietrasiewicz if (msleep_interruptible(100) != 0)
148300a2430fSAndrzej Pietrasiewicz return -EINTR;
148400a2430fSAndrzej Pietrasiewicz rc = usb_ep_set_halt(fsg->bulk_in);
148500a2430fSAndrzej Pietrasiewicz }
148600a2430fSAndrzej Pietrasiewicz return rc;
148700a2430fSAndrzej Pietrasiewicz }
148800a2430fSAndrzej Pietrasiewicz
wedge_bulk_in_endpoint(struct fsg_dev * fsg)148900a2430fSAndrzej Pietrasiewicz static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
149000a2430fSAndrzej Pietrasiewicz {
149100a2430fSAndrzej Pietrasiewicz int rc;
149200a2430fSAndrzej Pietrasiewicz
149300a2430fSAndrzej Pietrasiewicz DBG(fsg, "bulk-in set wedge\n");
149400a2430fSAndrzej Pietrasiewicz rc = usb_ep_set_wedge(fsg->bulk_in);
149500a2430fSAndrzej Pietrasiewicz if (rc == -EAGAIN)
149600a2430fSAndrzej Pietrasiewicz VDBG(fsg, "delayed bulk-in endpoint wedge\n");
149700a2430fSAndrzej Pietrasiewicz while (rc != 0) {
149800a2430fSAndrzej Pietrasiewicz if (rc != -EAGAIN) {
149900a2430fSAndrzej Pietrasiewicz WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
150000a2430fSAndrzej Pietrasiewicz rc = 0;
150100a2430fSAndrzej Pietrasiewicz break;
150200a2430fSAndrzej Pietrasiewicz }
150300a2430fSAndrzej Pietrasiewicz
150400a2430fSAndrzej Pietrasiewicz /* Wait for a short time and then try again */
150500a2430fSAndrzej Pietrasiewicz if (msleep_interruptible(100) != 0)
150600a2430fSAndrzej Pietrasiewicz return -EINTR;
150700a2430fSAndrzej Pietrasiewicz rc = usb_ep_set_wedge(fsg->bulk_in);
150800a2430fSAndrzej Pietrasiewicz }
150900a2430fSAndrzej Pietrasiewicz return rc;
151000a2430fSAndrzej Pietrasiewicz }
151100a2430fSAndrzej Pietrasiewicz
throw_away_data(struct fsg_common * common)151200a2430fSAndrzej Pietrasiewicz static int throw_away_data(struct fsg_common *common)
151300a2430fSAndrzej Pietrasiewicz {
1514225785aeSAlan Stern struct fsg_buffhd *bh, *bh2;
151500a2430fSAndrzej Pietrasiewicz u32 amount;
151600a2430fSAndrzej Pietrasiewicz int rc;
151700a2430fSAndrzej Pietrasiewicz
151800a2430fSAndrzej Pietrasiewicz for (bh = common->next_buffhd_to_drain;
151900a2430fSAndrzej Pietrasiewicz bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
152000a2430fSAndrzej Pietrasiewicz bh = common->next_buffhd_to_drain) {
152100a2430fSAndrzej Pietrasiewicz
152200a2430fSAndrzej Pietrasiewicz /* Try to submit another request if we need one */
1523225785aeSAlan Stern bh2 = common->next_buffhd_to_fill;
1524225785aeSAlan Stern if (bh2->state == BUF_STATE_EMPTY &&
1525225785aeSAlan Stern common->usb_amount_left > 0) {
152600a2430fSAndrzej Pietrasiewicz amount = min(common->usb_amount_left, FSG_BUFLEN);
152700a2430fSAndrzej Pietrasiewicz
152800a2430fSAndrzej Pietrasiewicz /*
152900a2430fSAndrzej Pietrasiewicz * Except at the end of the transfer, amount will be
153000a2430fSAndrzej Pietrasiewicz * equal to the buffer size, which is divisible by
153100a2430fSAndrzej Pietrasiewicz * the bulk-out maxpacket size.
153200a2430fSAndrzej Pietrasiewicz */
1533225785aeSAlan Stern set_bulk_out_req_length(common, bh2, amount);
1534225785aeSAlan Stern if (!start_out_transfer(common, bh2))
153500a2430fSAndrzej Pietrasiewicz /* Dunno what to do if common->fsg is NULL */
153600a2430fSAndrzej Pietrasiewicz return -EIO;
1537225785aeSAlan Stern common->next_buffhd_to_fill = bh2->next;
153800a2430fSAndrzej Pietrasiewicz common->usb_amount_left -= amount;
153900a2430fSAndrzej Pietrasiewicz continue;
154000a2430fSAndrzej Pietrasiewicz }
154100a2430fSAndrzej Pietrasiewicz
1542225785aeSAlan Stern /* Wait for the data to be received */
1543225785aeSAlan Stern rc = sleep_thread(common, false, bh);
154400a2430fSAndrzej Pietrasiewicz if (rc)
154500a2430fSAndrzej Pietrasiewicz return rc;
1546225785aeSAlan Stern
1547225785aeSAlan Stern /* Throw away the data in a filled buffer */
1548225785aeSAlan Stern bh->state = BUF_STATE_EMPTY;
1549225785aeSAlan Stern common->next_buffhd_to_drain = bh->next;
1550225785aeSAlan Stern
1551225785aeSAlan Stern /* A short packet or an error ends everything */
1552225785aeSAlan Stern if (bh->outreq->actual < bh->bulk_out_intended_length ||
1553225785aeSAlan Stern bh->outreq->status != 0) {
1554225785aeSAlan Stern raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1555225785aeSAlan Stern return -EINTR;
1556225785aeSAlan Stern }
155700a2430fSAndrzej Pietrasiewicz }
155800a2430fSAndrzej Pietrasiewicz return 0;
155900a2430fSAndrzej Pietrasiewicz }
156000a2430fSAndrzej Pietrasiewicz
finish_reply(struct fsg_common * common)156100a2430fSAndrzej Pietrasiewicz static int finish_reply(struct fsg_common *common)
156200a2430fSAndrzej Pietrasiewicz {
156300a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh = common->next_buffhd_to_fill;
156400a2430fSAndrzej Pietrasiewicz int rc = 0;
156500a2430fSAndrzej Pietrasiewicz
156600a2430fSAndrzej Pietrasiewicz switch (common->data_dir) {
156700a2430fSAndrzej Pietrasiewicz case DATA_DIR_NONE:
156800a2430fSAndrzej Pietrasiewicz break; /* Nothing to send */
156900a2430fSAndrzej Pietrasiewicz
157000a2430fSAndrzej Pietrasiewicz /*
157100a2430fSAndrzej Pietrasiewicz * If we don't know whether the host wants to read or write,
157200a2430fSAndrzej Pietrasiewicz * this must be CB or CBI with an unknown command. We mustn't
157300a2430fSAndrzej Pietrasiewicz * try to send or receive any data. So stall both bulk pipes
157400a2430fSAndrzej Pietrasiewicz * if we can and wait for a reset.
157500a2430fSAndrzej Pietrasiewicz */
157600a2430fSAndrzej Pietrasiewicz case DATA_DIR_UNKNOWN:
157700a2430fSAndrzej Pietrasiewicz if (!common->can_stall) {
157800a2430fSAndrzej Pietrasiewicz /* Nothing */
157900a2430fSAndrzej Pietrasiewicz } else if (fsg_is_set(common)) {
158000a2430fSAndrzej Pietrasiewicz fsg_set_halt(common->fsg, common->fsg->bulk_out);
158100a2430fSAndrzej Pietrasiewicz rc = halt_bulk_in_endpoint(common->fsg);
158200a2430fSAndrzej Pietrasiewicz } else {
158300a2430fSAndrzej Pietrasiewicz /* Don't know what to do if common->fsg is NULL */
158400a2430fSAndrzej Pietrasiewicz rc = -EIO;
158500a2430fSAndrzej Pietrasiewicz }
158600a2430fSAndrzej Pietrasiewicz break;
158700a2430fSAndrzej Pietrasiewicz
158800a2430fSAndrzej Pietrasiewicz /* All but the last buffer of data must have already been sent */
158900a2430fSAndrzej Pietrasiewicz case DATA_DIR_TO_HOST:
159000a2430fSAndrzej Pietrasiewicz if (common->data_size == 0) {
159100a2430fSAndrzej Pietrasiewicz /* Nothing to send */
159200a2430fSAndrzej Pietrasiewicz
159300a2430fSAndrzej Pietrasiewicz /* Don't know what to do if common->fsg is NULL */
159400a2430fSAndrzej Pietrasiewicz } else if (!fsg_is_set(common)) {
159500a2430fSAndrzej Pietrasiewicz rc = -EIO;
159600a2430fSAndrzej Pietrasiewicz
159700a2430fSAndrzej Pietrasiewicz /* If there's no residue, simply send the last buffer */
159800a2430fSAndrzej Pietrasiewicz } else if (common->residue == 0) {
159900a2430fSAndrzej Pietrasiewicz bh->inreq->zero = 0;
160000a2430fSAndrzej Pietrasiewicz if (!start_in_transfer(common, bh))
160100a2430fSAndrzej Pietrasiewicz return -EIO;
160200a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_fill = bh->next;
160300a2430fSAndrzej Pietrasiewicz
160400a2430fSAndrzej Pietrasiewicz /*
160500a2430fSAndrzej Pietrasiewicz * For Bulk-only, mark the end of the data with a short
160600a2430fSAndrzej Pietrasiewicz * packet. If we are allowed to stall, halt the bulk-in
160700a2430fSAndrzej Pietrasiewicz * endpoint. (Note: This violates the Bulk-Only Transport
160800a2430fSAndrzej Pietrasiewicz * specification, which requires us to pad the data if we
160900a2430fSAndrzej Pietrasiewicz * don't halt the endpoint. Presumably nobody will mind.)
161000a2430fSAndrzej Pietrasiewicz */
161100a2430fSAndrzej Pietrasiewicz } else {
161200a2430fSAndrzej Pietrasiewicz bh->inreq->zero = 1;
161300a2430fSAndrzej Pietrasiewicz if (!start_in_transfer(common, bh))
161400a2430fSAndrzej Pietrasiewicz rc = -EIO;
161500a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_fill = bh->next;
161600a2430fSAndrzej Pietrasiewicz if (common->can_stall)
161700a2430fSAndrzej Pietrasiewicz rc = halt_bulk_in_endpoint(common->fsg);
161800a2430fSAndrzej Pietrasiewicz }
161900a2430fSAndrzej Pietrasiewicz break;
162000a2430fSAndrzej Pietrasiewicz
162100a2430fSAndrzej Pietrasiewicz /*
162200a2430fSAndrzej Pietrasiewicz * We have processed all we want from the data the host has sent.
162300a2430fSAndrzej Pietrasiewicz * There may still be outstanding bulk-out requests.
162400a2430fSAndrzej Pietrasiewicz */
162500a2430fSAndrzej Pietrasiewicz case DATA_DIR_FROM_HOST:
162600a2430fSAndrzej Pietrasiewicz if (common->residue == 0) {
162700a2430fSAndrzej Pietrasiewicz /* Nothing to receive */
162800a2430fSAndrzej Pietrasiewicz
162900a2430fSAndrzej Pietrasiewicz /* Did the host stop sending unexpectedly early? */
163000a2430fSAndrzej Pietrasiewicz } else if (common->short_packet_received) {
163100a2430fSAndrzej Pietrasiewicz raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
163200a2430fSAndrzej Pietrasiewicz rc = -EINTR;
163300a2430fSAndrzej Pietrasiewicz
163400a2430fSAndrzej Pietrasiewicz /*
163500a2430fSAndrzej Pietrasiewicz * We haven't processed all the incoming data. Even though
163600a2430fSAndrzej Pietrasiewicz * we may be allowed to stall, doing so would cause a race.
163700a2430fSAndrzej Pietrasiewicz * The controller may already have ACK'ed all the remaining
163800a2430fSAndrzej Pietrasiewicz * bulk-out packets, in which case the host wouldn't see a
163900a2430fSAndrzej Pietrasiewicz * STALL. Not realizing the endpoint was halted, it wouldn't
164000a2430fSAndrzej Pietrasiewicz * clear the halt -- leading to problems later on.
164100a2430fSAndrzej Pietrasiewicz */
164200a2430fSAndrzej Pietrasiewicz #if 0
164300a2430fSAndrzej Pietrasiewicz } else if (common->can_stall) {
164400a2430fSAndrzej Pietrasiewicz if (fsg_is_set(common))
164500a2430fSAndrzej Pietrasiewicz fsg_set_halt(common->fsg,
164600a2430fSAndrzej Pietrasiewicz common->fsg->bulk_out);
164700a2430fSAndrzej Pietrasiewicz raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
164800a2430fSAndrzej Pietrasiewicz rc = -EINTR;
164900a2430fSAndrzej Pietrasiewicz #endif
165000a2430fSAndrzej Pietrasiewicz
165100a2430fSAndrzej Pietrasiewicz /*
165200a2430fSAndrzej Pietrasiewicz * We can't stall. Read in the excess data and throw it
165300a2430fSAndrzej Pietrasiewicz * all away.
165400a2430fSAndrzej Pietrasiewicz */
165500a2430fSAndrzej Pietrasiewicz } else {
165600a2430fSAndrzej Pietrasiewicz rc = throw_away_data(common);
165700a2430fSAndrzej Pietrasiewicz }
165800a2430fSAndrzej Pietrasiewicz break;
165900a2430fSAndrzej Pietrasiewicz }
166000a2430fSAndrzej Pietrasiewicz return rc;
166100a2430fSAndrzej Pietrasiewicz }
166200a2430fSAndrzej Pietrasiewicz
send_status(struct fsg_common * common)166378db441dSAlan Stern static void send_status(struct fsg_common *common)
166400a2430fSAndrzej Pietrasiewicz {
166500a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = common->curlun;
166600a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh;
166700a2430fSAndrzej Pietrasiewicz struct bulk_cs_wrap *csw;
166800a2430fSAndrzej Pietrasiewicz int rc;
166900a2430fSAndrzej Pietrasiewicz u8 status = US_BULK_STAT_OK;
167000a2430fSAndrzej Pietrasiewicz u32 sd, sdinfo = 0;
167100a2430fSAndrzej Pietrasiewicz
167200a2430fSAndrzej Pietrasiewicz /* Wait for the next buffer to become available */
167300a2430fSAndrzej Pietrasiewicz bh = common->next_buffhd_to_fill;
1674225785aeSAlan Stern rc = sleep_thread(common, false, bh);
167500a2430fSAndrzej Pietrasiewicz if (rc)
167678db441dSAlan Stern return;
167700a2430fSAndrzej Pietrasiewicz
167800a2430fSAndrzej Pietrasiewicz if (curlun) {
167900a2430fSAndrzej Pietrasiewicz sd = curlun->sense_data;
168000a2430fSAndrzej Pietrasiewicz sdinfo = curlun->sense_data_info;
168100a2430fSAndrzej Pietrasiewicz } else if (common->bad_lun_okay)
168200a2430fSAndrzej Pietrasiewicz sd = SS_NO_SENSE;
168300a2430fSAndrzej Pietrasiewicz else
168400a2430fSAndrzej Pietrasiewicz sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
168500a2430fSAndrzej Pietrasiewicz
168600a2430fSAndrzej Pietrasiewicz if (common->phase_error) {
168700a2430fSAndrzej Pietrasiewicz DBG(common, "sending phase-error status\n");
168800a2430fSAndrzej Pietrasiewicz status = US_BULK_STAT_PHASE;
168900a2430fSAndrzej Pietrasiewicz sd = SS_INVALID_COMMAND;
169000a2430fSAndrzej Pietrasiewicz } else if (sd != SS_NO_SENSE) {
169100a2430fSAndrzej Pietrasiewicz DBG(common, "sending command-failure status\n");
169200a2430fSAndrzej Pietrasiewicz status = US_BULK_STAT_FAIL;
169300a2430fSAndrzej Pietrasiewicz VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
169400a2430fSAndrzej Pietrasiewicz " info x%x\n",
169500a2430fSAndrzej Pietrasiewicz SK(sd), ASC(sd), ASCQ(sd), sdinfo);
169600a2430fSAndrzej Pietrasiewicz }
169700a2430fSAndrzej Pietrasiewicz
169800a2430fSAndrzej Pietrasiewicz /* Store and send the Bulk-only CSW */
169900a2430fSAndrzej Pietrasiewicz csw = (void *)bh->buf;
170000a2430fSAndrzej Pietrasiewicz
170100a2430fSAndrzej Pietrasiewicz csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
170200a2430fSAndrzej Pietrasiewicz csw->Tag = common->tag;
170300a2430fSAndrzej Pietrasiewicz csw->Residue = cpu_to_le32(common->residue);
170400a2430fSAndrzej Pietrasiewicz csw->Status = status;
170500a2430fSAndrzej Pietrasiewicz
170600a2430fSAndrzej Pietrasiewicz bh->inreq->length = US_BULK_CS_WRAP_LEN;
170700a2430fSAndrzej Pietrasiewicz bh->inreq->zero = 0;
170800a2430fSAndrzej Pietrasiewicz if (!start_in_transfer(common, bh))
170900a2430fSAndrzej Pietrasiewicz /* Don't know what to do if common->fsg is NULL */
171078db441dSAlan Stern return;
171100a2430fSAndrzej Pietrasiewicz
171200a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_fill = bh->next;
171378db441dSAlan Stern return;
171400a2430fSAndrzej Pietrasiewicz }
171500a2430fSAndrzej Pietrasiewicz
171600a2430fSAndrzej Pietrasiewicz
171700a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
171800a2430fSAndrzej Pietrasiewicz
171900a2430fSAndrzej Pietrasiewicz /*
172000a2430fSAndrzej Pietrasiewicz * Check whether the command is properly formed and whether its data size
172100a2430fSAndrzej Pietrasiewicz * and direction agree with the values we already have.
172200a2430fSAndrzej Pietrasiewicz */
check_command(struct fsg_common * common,int cmnd_size,enum data_direction data_dir,unsigned int mask,int needs_medium,const char * name)172300a2430fSAndrzej Pietrasiewicz static int check_command(struct fsg_common *common, int cmnd_size,
172400a2430fSAndrzej Pietrasiewicz enum data_direction data_dir, unsigned int mask,
172500a2430fSAndrzej Pietrasiewicz int needs_medium, const char *name)
172600a2430fSAndrzej Pietrasiewicz {
172700a2430fSAndrzej Pietrasiewicz int i;
172800a2430fSAndrzej Pietrasiewicz unsigned int lun = common->cmnd[1] >> 5;
172900a2430fSAndrzej Pietrasiewicz static const char dirletter[4] = {'u', 'o', 'i', 'n'};
173000a2430fSAndrzej Pietrasiewicz char hdlen[20];
173100a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun;
173200a2430fSAndrzej Pietrasiewicz
173300a2430fSAndrzej Pietrasiewicz hdlen[0] = 0;
173400a2430fSAndrzej Pietrasiewicz if (common->data_dir != DATA_DIR_UNKNOWN)
173500a2430fSAndrzej Pietrasiewicz sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
173600a2430fSAndrzej Pietrasiewicz common->data_size);
173700a2430fSAndrzej Pietrasiewicz VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
173800a2430fSAndrzej Pietrasiewicz name, cmnd_size, dirletter[(int) data_dir],
173900a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd, common->cmnd_size, hdlen);
174000a2430fSAndrzej Pietrasiewicz
174100a2430fSAndrzej Pietrasiewicz /*
174200a2430fSAndrzej Pietrasiewicz * We can't reply at all until we know the correct data direction
174300a2430fSAndrzej Pietrasiewicz * and size.
174400a2430fSAndrzej Pietrasiewicz */
174500a2430fSAndrzej Pietrasiewicz if (common->data_size_from_cmnd == 0)
174600a2430fSAndrzej Pietrasiewicz data_dir = DATA_DIR_NONE;
174700a2430fSAndrzej Pietrasiewicz if (common->data_size < common->data_size_from_cmnd) {
174800a2430fSAndrzej Pietrasiewicz /*
174900a2430fSAndrzej Pietrasiewicz * Host data size < Device data size is a phase error.
175000a2430fSAndrzej Pietrasiewicz * Carry out the command, but only transfer as much as
175100a2430fSAndrzej Pietrasiewicz * we are allowed.
175200a2430fSAndrzej Pietrasiewicz */
175300a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = common->data_size;
175400a2430fSAndrzej Pietrasiewicz common->phase_error = 1;
175500a2430fSAndrzej Pietrasiewicz }
175600a2430fSAndrzej Pietrasiewicz common->residue = common->data_size;
175700a2430fSAndrzej Pietrasiewicz common->usb_amount_left = common->data_size;
175800a2430fSAndrzej Pietrasiewicz
175900a2430fSAndrzej Pietrasiewicz /* Conflicting data directions is a phase error */
176000a2430fSAndrzej Pietrasiewicz if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) {
176100a2430fSAndrzej Pietrasiewicz common->phase_error = 1;
176200a2430fSAndrzej Pietrasiewicz return -EINVAL;
176300a2430fSAndrzej Pietrasiewicz }
176400a2430fSAndrzej Pietrasiewicz
176500a2430fSAndrzej Pietrasiewicz /* Verify the length of the command itself */
176600a2430fSAndrzej Pietrasiewicz if (cmnd_size != common->cmnd_size) {
176700a2430fSAndrzej Pietrasiewicz
176800a2430fSAndrzej Pietrasiewicz /*
176900a2430fSAndrzej Pietrasiewicz * Special case workaround: There are plenty of buggy SCSI
177000a2430fSAndrzej Pietrasiewicz * implementations. Many have issues with cbw->Length
177100a2430fSAndrzej Pietrasiewicz * field passing a wrong command size. For those cases we
177200a2430fSAndrzej Pietrasiewicz * always try to work around the problem by using the length
177300a2430fSAndrzej Pietrasiewicz * sent by the host side provided it is at least as large
177400a2430fSAndrzej Pietrasiewicz * as the correct command length.
177500a2430fSAndrzej Pietrasiewicz * Examples of such cases would be MS-Windows, which issues
177600a2430fSAndrzej Pietrasiewicz * REQUEST SENSE with cbw->Length == 12 where it should
177700a2430fSAndrzej Pietrasiewicz * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
177800a2430fSAndrzej Pietrasiewicz * REQUEST SENSE with cbw->Length == 10 where it should
177900a2430fSAndrzej Pietrasiewicz * be 6 as well.
178000a2430fSAndrzej Pietrasiewicz */
178100a2430fSAndrzej Pietrasiewicz if (cmnd_size <= common->cmnd_size) {
178200a2430fSAndrzej Pietrasiewicz DBG(common, "%s is buggy! Expected length %d "
178300a2430fSAndrzej Pietrasiewicz "but we got %d\n", name,
178400a2430fSAndrzej Pietrasiewicz cmnd_size, common->cmnd_size);
178500a2430fSAndrzej Pietrasiewicz cmnd_size = common->cmnd_size;
178600a2430fSAndrzej Pietrasiewicz } else {
178700a2430fSAndrzej Pietrasiewicz common->phase_error = 1;
178800a2430fSAndrzej Pietrasiewicz return -EINVAL;
178900a2430fSAndrzej Pietrasiewicz }
179000a2430fSAndrzej Pietrasiewicz }
179100a2430fSAndrzej Pietrasiewicz
179200a2430fSAndrzej Pietrasiewicz /* Check that the LUN values are consistent */
179300a2430fSAndrzej Pietrasiewicz if (common->lun != lun)
179400a2430fSAndrzej Pietrasiewicz DBG(common, "using LUN %u from CBW, not LUN %u from CDB\n",
179500a2430fSAndrzej Pietrasiewicz common->lun, lun);
179600a2430fSAndrzej Pietrasiewicz
179700a2430fSAndrzej Pietrasiewicz /* Check the LUN */
179800a2430fSAndrzej Pietrasiewicz curlun = common->curlun;
179900a2430fSAndrzej Pietrasiewicz if (curlun) {
180000a2430fSAndrzej Pietrasiewicz if (common->cmnd[0] != REQUEST_SENSE) {
180100a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_NO_SENSE;
180200a2430fSAndrzej Pietrasiewicz curlun->sense_data_info = 0;
180300a2430fSAndrzej Pietrasiewicz curlun->info_valid = 0;
180400a2430fSAndrzej Pietrasiewicz }
180500a2430fSAndrzej Pietrasiewicz } else {
180600a2430fSAndrzej Pietrasiewicz common->bad_lun_okay = 0;
180700a2430fSAndrzej Pietrasiewicz
180800a2430fSAndrzej Pietrasiewicz /*
180900a2430fSAndrzej Pietrasiewicz * INQUIRY and REQUEST SENSE commands are explicitly allowed
181000a2430fSAndrzej Pietrasiewicz * to use unsupported LUNs; all others may not.
181100a2430fSAndrzej Pietrasiewicz */
181200a2430fSAndrzej Pietrasiewicz if (common->cmnd[0] != INQUIRY &&
181300a2430fSAndrzej Pietrasiewicz common->cmnd[0] != REQUEST_SENSE) {
181400a2430fSAndrzej Pietrasiewicz DBG(common, "unsupported LUN %u\n", common->lun);
181500a2430fSAndrzej Pietrasiewicz return -EINVAL;
181600a2430fSAndrzej Pietrasiewicz }
181700a2430fSAndrzej Pietrasiewicz }
181800a2430fSAndrzej Pietrasiewicz
181900a2430fSAndrzej Pietrasiewicz /*
182000a2430fSAndrzej Pietrasiewicz * If a unit attention condition exists, only INQUIRY and
182100a2430fSAndrzej Pietrasiewicz * REQUEST SENSE commands are allowed; anything else must fail.
182200a2430fSAndrzej Pietrasiewicz */
182300a2430fSAndrzej Pietrasiewicz if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
182400a2430fSAndrzej Pietrasiewicz common->cmnd[0] != INQUIRY &&
182500a2430fSAndrzej Pietrasiewicz common->cmnd[0] != REQUEST_SENSE) {
182600a2430fSAndrzej Pietrasiewicz curlun->sense_data = curlun->unit_attention_data;
182700a2430fSAndrzej Pietrasiewicz curlun->unit_attention_data = SS_NO_SENSE;
182800a2430fSAndrzej Pietrasiewicz return -EINVAL;
182900a2430fSAndrzej Pietrasiewicz }
183000a2430fSAndrzej Pietrasiewicz
183100a2430fSAndrzej Pietrasiewicz /* Check that only command bytes listed in the mask are non-zero */
183200a2430fSAndrzej Pietrasiewicz common->cmnd[1] &= 0x1f; /* Mask away the LUN */
183300a2430fSAndrzej Pietrasiewicz for (i = 1; i < cmnd_size; ++i) {
183400a2430fSAndrzej Pietrasiewicz if (common->cmnd[i] && !(mask & (1 << i))) {
183500a2430fSAndrzej Pietrasiewicz if (curlun)
183600a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
183700a2430fSAndrzej Pietrasiewicz return -EINVAL;
183800a2430fSAndrzej Pietrasiewicz }
183900a2430fSAndrzej Pietrasiewicz }
184000a2430fSAndrzej Pietrasiewicz
184100a2430fSAndrzej Pietrasiewicz /* If the medium isn't mounted and the command needs to access
184200a2430fSAndrzej Pietrasiewicz * it, return an error. */
184300a2430fSAndrzej Pietrasiewicz if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
184400a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
184500a2430fSAndrzej Pietrasiewicz return -EINVAL;
184600a2430fSAndrzej Pietrasiewicz }
184700a2430fSAndrzej Pietrasiewicz
184800a2430fSAndrzej Pietrasiewicz return 0;
184900a2430fSAndrzej Pietrasiewicz }
185000a2430fSAndrzej Pietrasiewicz
185100a2430fSAndrzej Pietrasiewicz /* wrapper of check_command for data size in blocks handling */
check_command_size_in_blocks(struct fsg_common * common,int cmnd_size,enum data_direction data_dir,unsigned int mask,int needs_medium,const char * name)185200a2430fSAndrzej Pietrasiewicz static int check_command_size_in_blocks(struct fsg_common *common,
185300a2430fSAndrzej Pietrasiewicz int cmnd_size, enum data_direction data_dir,
185400a2430fSAndrzej Pietrasiewicz unsigned int mask, int needs_medium, const char *name)
185500a2430fSAndrzej Pietrasiewicz {
185600a2430fSAndrzej Pietrasiewicz if (common->curlun)
185700a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd <<= common->curlun->blkbits;
185800a2430fSAndrzej Pietrasiewicz return check_command(common, cmnd_size, data_dir,
185900a2430fSAndrzej Pietrasiewicz mask, needs_medium, name);
186000a2430fSAndrzej Pietrasiewicz }
186100a2430fSAndrzej Pietrasiewicz
do_scsi_command(struct fsg_common * common)186200a2430fSAndrzej Pietrasiewicz static int do_scsi_command(struct fsg_common *common)
186300a2430fSAndrzej Pietrasiewicz {
186400a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh;
186500a2430fSAndrzej Pietrasiewicz int rc;
186600a2430fSAndrzej Pietrasiewicz int reply = -EINVAL;
186700a2430fSAndrzej Pietrasiewicz int i;
186800a2430fSAndrzej Pietrasiewicz static char unknown[16];
186900a2430fSAndrzej Pietrasiewicz
187000a2430fSAndrzej Pietrasiewicz dump_cdb(common);
187100a2430fSAndrzej Pietrasiewicz
187200a2430fSAndrzej Pietrasiewicz /* Wait for the next buffer to become available for data or status */
187300a2430fSAndrzej Pietrasiewicz bh = common->next_buffhd_to_fill;
187400a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_drain = bh;
1875225785aeSAlan Stern rc = sleep_thread(common, false, bh);
187600a2430fSAndrzej Pietrasiewicz if (rc)
187700a2430fSAndrzej Pietrasiewicz return rc;
1878225785aeSAlan Stern
187900a2430fSAndrzej Pietrasiewicz common->phase_error = 0;
188000a2430fSAndrzej Pietrasiewicz common->short_packet_received = 0;
188100a2430fSAndrzej Pietrasiewicz
188200a2430fSAndrzej Pietrasiewicz down_read(&common->filesem); /* We're using the backing file */
188300a2430fSAndrzej Pietrasiewicz switch (common->cmnd[0]) {
188400a2430fSAndrzej Pietrasiewicz
188500a2430fSAndrzej Pietrasiewicz case INQUIRY:
188600a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = common->cmnd[4];
188700a2430fSAndrzej Pietrasiewicz reply = check_command(common, 6, DATA_DIR_TO_HOST,
188800a2430fSAndrzej Pietrasiewicz (1<<4), 0,
188900a2430fSAndrzej Pietrasiewicz "INQUIRY");
189000a2430fSAndrzej Pietrasiewicz if (reply == 0)
189100a2430fSAndrzej Pietrasiewicz reply = do_inquiry(common, bh);
189200a2430fSAndrzej Pietrasiewicz break;
189300a2430fSAndrzej Pietrasiewicz
189400a2430fSAndrzej Pietrasiewicz case MODE_SELECT:
189500a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = common->cmnd[4];
189600a2430fSAndrzej Pietrasiewicz reply = check_command(common, 6, DATA_DIR_FROM_HOST,
189700a2430fSAndrzej Pietrasiewicz (1<<1) | (1<<4), 0,
189800a2430fSAndrzej Pietrasiewicz "MODE SELECT(6)");
189900a2430fSAndrzej Pietrasiewicz if (reply == 0)
190000a2430fSAndrzej Pietrasiewicz reply = do_mode_select(common, bh);
190100a2430fSAndrzej Pietrasiewicz break;
190200a2430fSAndrzej Pietrasiewicz
190300a2430fSAndrzej Pietrasiewicz case MODE_SELECT_10:
190400a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
190500a2430fSAndrzej Pietrasiewicz get_unaligned_be16(&common->cmnd[7]);
190600a2430fSAndrzej Pietrasiewicz reply = check_command(common, 10, DATA_DIR_FROM_HOST,
190700a2430fSAndrzej Pietrasiewicz (1<<1) | (3<<7), 0,
190800a2430fSAndrzej Pietrasiewicz "MODE SELECT(10)");
190900a2430fSAndrzej Pietrasiewicz if (reply == 0)
191000a2430fSAndrzej Pietrasiewicz reply = do_mode_select(common, bh);
191100a2430fSAndrzej Pietrasiewicz break;
191200a2430fSAndrzej Pietrasiewicz
191300a2430fSAndrzej Pietrasiewicz case MODE_SENSE:
191400a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = common->cmnd[4];
191500a2430fSAndrzej Pietrasiewicz reply = check_command(common, 6, DATA_DIR_TO_HOST,
191600a2430fSAndrzej Pietrasiewicz (1<<1) | (1<<2) | (1<<4), 0,
191700a2430fSAndrzej Pietrasiewicz "MODE SENSE(6)");
191800a2430fSAndrzej Pietrasiewicz if (reply == 0)
191900a2430fSAndrzej Pietrasiewicz reply = do_mode_sense(common, bh);
192000a2430fSAndrzej Pietrasiewicz break;
192100a2430fSAndrzej Pietrasiewicz
192200a2430fSAndrzej Pietrasiewicz case MODE_SENSE_10:
192300a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
192400a2430fSAndrzej Pietrasiewicz get_unaligned_be16(&common->cmnd[7]);
192500a2430fSAndrzej Pietrasiewicz reply = check_command(common, 10, DATA_DIR_TO_HOST,
192600a2430fSAndrzej Pietrasiewicz (1<<1) | (1<<2) | (3<<7), 0,
192700a2430fSAndrzej Pietrasiewicz "MODE SENSE(10)");
192800a2430fSAndrzej Pietrasiewicz if (reply == 0)
192900a2430fSAndrzej Pietrasiewicz reply = do_mode_sense(common, bh);
193000a2430fSAndrzej Pietrasiewicz break;
193100a2430fSAndrzej Pietrasiewicz
193200a2430fSAndrzej Pietrasiewicz case ALLOW_MEDIUM_REMOVAL:
193300a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = 0;
193400a2430fSAndrzej Pietrasiewicz reply = check_command(common, 6, DATA_DIR_NONE,
193500a2430fSAndrzej Pietrasiewicz (1<<4), 0,
193600a2430fSAndrzej Pietrasiewicz "PREVENT-ALLOW MEDIUM REMOVAL");
193700a2430fSAndrzej Pietrasiewicz if (reply == 0)
193800a2430fSAndrzej Pietrasiewicz reply = do_prevent_allow(common);
193900a2430fSAndrzej Pietrasiewicz break;
194000a2430fSAndrzej Pietrasiewicz
194100a2430fSAndrzej Pietrasiewicz case READ_6:
194200a2430fSAndrzej Pietrasiewicz i = common->cmnd[4];
194300a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = (i == 0) ? 256 : i;
194400a2430fSAndrzej Pietrasiewicz reply = check_command_size_in_blocks(common, 6,
194500a2430fSAndrzej Pietrasiewicz DATA_DIR_TO_HOST,
194600a2430fSAndrzej Pietrasiewicz (7<<1) | (1<<4), 1,
194700a2430fSAndrzej Pietrasiewicz "READ(6)");
194800a2430fSAndrzej Pietrasiewicz if (reply == 0)
194900a2430fSAndrzej Pietrasiewicz reply = do_read(common);
195000a2430fSAndrzej Pietrasiewicz break;
195100a2430fSAndrzej Pietrasiewicz
195200a2430fSAndrzej Pietrasiewicz case READ_10:
195300a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
195400a2430fSAndrzej Pietrasiewicz get_unaligned_be16(&common->cmnd[7]);
195500a2430fSAndrzej Pietrasiewicz reply = check_command_size_in_blocks(common, 10,
195600a2430fSAndrzej Pietrasiewicz DATA_DIR_TO_HOST,
195700a2430fSAndrzej Pietrasiewicz (1<<1) | (0xf<<2) | (3<<7), 1,
195800a2430fSAndrzej Pietrasiewicz "READ(10)");
195900a2430fSAndrzej Pietrasiewicz if (reply == 0)
196000a2430fSAndrzej Pietrasiewicz reply = do_read(common);
196100a2430fSAndrzej Pietrasiewicz break;
196200a2430fSAndrzej Pietrasiewicz
196300a2430fSAndrzej Pietrasiewicz case READ_12:
196400a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
196500a2430fSAndrzej Pietrasiewicz get_unaligned_be32(&common->cmnd[6]);
196600a2430fSAndrzej Pietrasiewicz reply = check_command_size_in_blocks(common, 12,
196700a2430fSAndrzej Pietrasiewicz DATA_DIR_TO_HOST,
196800a2430fSAndrzej Pietrasiewicz (1<<1) | (0xf<<2) | (0xf<<6), 1,
196900a2430fSAndrzej Pietrasiewicz "READ(12)");
197000a2430fSAndrzej Pietrasiewicz if (reply == 0)
197100a2430fSAndrzej Pietrasiewicz reply = do_read(common);
197200a2430fSAndrzej Pietrasiewicz break;
197300a2430fSAndrzej Pietrasiewicz
1974bedbac5fSNikita Yushchenko case READ_16:
1975bedbac5fSNikita Yushchenko common->data_size_from_cmnd =
1976bedbac5fSNikita Yushchenko get_unaligned_be32(&common->cmnd[10]);
1977bedbac5fSNikita Yushchenko reply = check_command_size_in_blocks(common, 16,
1978bedbac5fSNikita Yushchenko DATA_DIR_TO_HOST,
1979bedbac5fSNikita Yushchenko (1<<1) | (0xff<<2) | (0xf<<10), 1,
1980bedbac5fSNikita Yushchenko "READ(16)");
1981bedbac5fSNikita Yushchenko if (reply == 0)
1982bedbac5fSNikita Yushchenko reply = do_read(common);
1983bedbac5fSNikita Yushchenko break;
1984bedbac5fSNikita Yushchenko
198500a2430fSAndrzej Pietrasiewicz case READ_CAPACITY:
198600a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = 8;
198700a2430fSAndrzej Pietrasiewicz reply = check_command(common, 10, DATA_DIR_TO_HOST,
198800a2430fSAndrzej Pietrasiewicz (0xf<<2) | (1<<8), 1,
198900a2430fSAndrzej Pietrasiewicz "READ CAPACITY");
199000a2430fSAndrzej Pietrasiewicz if (reply == 0)
199100a2430fSAndrzej Pietrasiewicz reply = do_read_capacity(common, bh);
199200a2430fSAndrzej Pietrasiewicz break;
199300a2430fSAndrzej Pietrasiewicz
199400a2430fSAndrzej Pietrasiewicz case READ_HEADER:
199500a2430fSAndrzej Pietrasiewicz if (!common->curlun || !common->curlun->cdrom)
199600a2430fSAndrzej Pietrasiewicz goto unknown_cmnd;
199700a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
199800a2430fSAndrzej Pietrasiewicz get_unaligned_be16(&common->cmnd[7]);
199900a2430fSAndrzej Pietrasiewicz reply = check_command(common, 10, DATA_DIR_TO_HOST,
200000a2430fSAndrzej Pietrasiewicz (3<<7) | (0x1f<<1), 1,
200100a2430fSAndrzej Pietrasiewicz "READ HEADER");
200200a2430fSAndrzej Pietrasiewicz if (reply == 0)
200300a2430fSAndrzej Pietrasiewicz reply = do_read_header(common, bh);
200400a2430fSAndrzej Pietrasiewicz break;
200500a2430fSAndrzej Pietrasiewicz
200600a2430fSAndrzej Pietrasiewicz case READ_TOC:
200700a2430fSAndrzej Pietrasiewicz if (!common->curlun || !common->curlun->cdrom)
200800a2430fSAndrzej Pietrasiewicz goto unknown_cmnd;
200900a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
201000a2430fSAndrzej Pietrasiewicz get_unaligned_be16(&common->cmnd[7]);
201100a2430fSAndrzej Pietrasiewicz reply = check_command(common, 10, DATA_DIR_TO_HOST,
201289ada0feSRoger Quadros (0xf<<6) | (3<<1), 1,
201300a2430fSAndrzej Pietrasiewicz "READ TOC");
201400a2430fSAndrzej Pietrasiewicz if (reply == 0)
201500a2430fSAndrzej Pietrasiewicz reply = do_read_toc(common, bh);
201600a2430fSAndrzej Pietrasiewicz break;
201700a2430fSAndrzej Pietrasiewicz
201800a2430fSAndrzej Pietrasiewicz case READ_FORMAT_CAPACITIES:
201900a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
202000a2430fSAndrzej Pietrasiewicz get_unaligned_be16(&common->cmnd[7]);
202100a2430fSAndrzej Pietrasiewicz reply = check_command(common, 10, DATA_DIR_TO_HOST,
202200a2430fSAndrzej Pietrasiewicz (3<<7), 1,
202300a2430fSAndrzej Pietrasiewicz "READ FORMAT CAPACITIES");
202400a2430fSAndrzej Pietrasiewicz if (reply == 0)
202500a2430fSAndrzej Pietrasiewicz reply = do_read_format_capacities(common, bh);
202600a2430fSAndrzej Pietrasiewicz break;
202700a2430fSAndrzej Pietrasiewicz
202800a2430fSAndrzej Pietrasiewicz case REQUEST_SENSE:
202900a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = common->cmnd[4];
203000a2430fSAndrzej Pietrasiewicz reply = check_command(common, 6, DATA_DIR_TO_HOST,
203100a2430fSAndrzej Pietrasiewicz (1<<4), 0,
203200a2430fSAndrzej Pietrasiewicz "REQUEST SENSE");
203300a2430fSAndrzej Pietrasiewicz if (reply == 0)
203400a2430fSAndrzej Pietrasiewicz reply = do_request_sense(common, bh);
203500a2430fSAndrzej Pietrasiewicz break;
203600a2430fSAndrzej Pietrasiewicz
2037bedbac5fSNikita Yushchenko case SERVICE_ACTION_IN_16:
2038bedbac5fSNikita Yushchenko switch (common->cmnd[1] & 0x1f) {
2039bedbac5fSNikita Yushchenko
2040bedbac5fSNikita Yushchenko case SAI_READ_CAPACITY_16:
2041bedbac5fSNikita Yushchenko common->data_size_from_cmnd =
2042bedbac5fSNikita Yushchenko get_unaligned_be32(&common->cmnd[10]);
2043bedbac5fSNikita Yushchenko reply = check_command(common, 16, DATA_DIR_TO_HOST,
2044bedbac5fSNikita Yushchenko (1<<1) | (0xff<<2) | (0xf<<10) |
2045bedbac5fSNikita Yushchenko (1<<14), 1,
2046bedbac5fSNikita Yushchenko "READ CAPACITY(16)");
2047bedbac5fSNikita Yushchenko if (reply == 0)
2048bedbac5fSNikita Yushchenko reply = do_read_capacity_16(common, bh);
2049bedbac5fSNikita Yushchenko break;
2050bedbac5fSNikita Yushchenko
2051bedbac5fSNikita Yushchenko default:
2052bedbac5fSNikita Yushchenko goto unknown_cmnd;
2053bedbac5fSNikita Yushchenko }
2054bedbac5fSNikita Yushchenko break;
2055bedbac5fSNikita Yushchenko
205600a2430fSAndrzej Pietrasiewicz case START_STOP:
205700a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = 0;
205800a2430fSAndrzej Pietrasiewicz reply = check_command(common, 6, DATA_DIR_NONE,
205900a2430fSAndrzej Pietrasiewicz (1<<1) | (1<<4), 0,
206000a2430fSAndrzej Pietrasiewicz "START-STOP UNIT");
206100a2430fSAndrzej Pietrasiewicz if (reply == 0)
206200a2430fSAndrzej Pietrasiewicz reply = do_start_stop(common);
206300a2430fSAndrzej Pietrasiewicz break;
206400a2430fSAndrzej Pietrasiewicz
206500a2430fSAndrzej Pietrasiewicz case SYNCHRONIZE_CACHE:
206600a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = 0;
206700a2430fSAndrzej Pietrasiewicz reply = check_command(common, 10, DATA_DIR_NONE,
206800a2430fSAndrzej Pietrasiewicz (0xf<<2) | (3<<7), 1,
206900a2430fSAndrzej Pietrasiewicz "SYNCHRONIZE CACHE");
207000a2430fSAndrzej Pietrasiewicz if (reply == 0)
207100a2430fSAndrzej Pietrasiewicz reply = do_synchronize_cache(common);
207200a2430fSAndrzej Pietrasiewicz break;
207300a2430fSAndrzej Pietrasiewicz
207400a2430fSAndrzej Pietrasiewicz case TEST_UNIT_READY:
207500a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = 0;
207600a2430fSAndrzej Pietrasiewicz reply = check_command(common, 6, DATA_DIR_NONE,
207700a2430fSAndrzej Pietrasiewicz 0, 1,
207800a2430fSAndrzej Pietrasiewicz "TEST UNIT READY");
207900a2430fSAndrzej Pietrasiewicz break;
208000a2430fSAndrzej Pietrasiewicz
208100a2430fSAndrzej Pietrasiewicz /*
208200a2430fSAndrzej Pietrasiewicz * Although optional, this command is used by MS-Windows. We
208300a2430fSAndrzej Pietrasiewicz * support a minimal version: BytChk must be 0.
208400a2430fSAndrzej Pietrasiewicz */
208500a2430fSAndrzej Pietrasiewicz case VERIFY:
208600a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = 0;
208700a2430fSAndrzej Pietrasiewicz reply = check_command(common, 10, DATA_DIR_NONE,
208800a2430fSAndrzej Pietrasiewicz (1<<1) | (0xf<<2) | (3<<7), 1,
208900a2430fSAndrzej Pietrasiewicz "VERIFY");
209000a2430fSAndrzej Pietrasiewicz if (reply == 0)
209100a2430fSAndrzej Pietrasiewicz reply = do_verify(common);
209200a2430fSAndrzej Pietrasiewicz break;
209300a2430fSAndrzej Pietrasiewicz
209400a2430fSAndrzej Pietrasiewicz case WRITE_6:
209500a2430fSAndrzej Pietrasiewicz i = common->cmnd[4];
209600a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = (i == 0) ? 256 : i;
209700a2430fSAndrzej Pietrasiewicz reply = check_command_size_in_blocks(common, 6,
209800a2430fSAndrzej Pietrasiewicz DATA_DIR_FROM_HOST,
209900a2430fSAndrzej Pietrasiewicz (7<<1) | (1<<4), 1,
210000a2430fSAndrzej Pietrasiewicz "WRITE(6)");
210100a2430fSAndrzej Pietrasiewicz if (reply == 0)
210200a2430fSAndrzej Pietrasiewicz reply = do_write(common);
210300a2430fSAndrzej Pietrasiewicz break;
210400a2430fSAndrzej Pietrasiewicz
210500a2430fSAndrzej Pietrasiewicz case WRITE_10:
210600a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
210700a2430fSAndrzej Pietrasiewicz get_unaligned_be16(&common->cmnd[7]);
210800a2430fSAndrzej Pietrasiewicz reply = check_command_size_in_blocks(common, 10,
210900a2430fSAndrzej Pietrasiewicz DATA_DIR_FROM_HOST,
211000a2430fSAndrzej Pietrasiewicz (1<<1) | (0xf<<2) | (3<<7), 1,
211100a2430fSAndrzej Pietrasiewicz "WRITE(10)");
211200a2430fSAndrzej Pietrasiewicz if (reply == 0)
211300a2430fSAndrzej Pietrasiewicz reply = do_write(common);
211400a2430fSAndrzej Pietrasiewicz break;
211500a2430fSAndrzej Pietrasiewicz
211600a2430fSAndrzej Pietrasiewicz case WRITE_12:
211700a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd =
211800a2430fSAndrzej Pietrasiewicz get_unaligned_be32(&common->cmnd[6]);
211900a2430fSAndrzej Pietrasiewicz reply = check_command_size_in_blocks(common, 12,
212000a2430fSAndrzej Pietrasiewicz DATA_DIR_FROM_HOST,
212100a2430fSAndrzej Pietrasiewicz (1<<1) | (0xf<<2) | (0xf<<6), 1,
212200a2430fSAndrzej Pietrasiewicz "WRITE(12)");
212300a2430fSAndrzej Pietrasiewicz if (reply == 0)
212400a2430fSAndrzej Pietrasiewicz reply = do_write(common);
212500a2430fSAndrzej Pietrasiewicz break;
212600a2430fSAndrzej Pietrasiewicz
2127bedbac5fSNikita Yushchenko case WRITE_16:
2128bedbac5fSNikita Yushchenko common->data_size_from_cmnd =
2129bedbac5fSNikita Yushchenko get_unaligned_be32(&common->cmnd[10]);
2130bedbac5fSNikita Yushchenko reply = check_command_size_in_blocks(common, 16,
2131bedbac5fSNikita Yushchenko DATA_DIR_FROM_HOST,
2132bedbac5fSNikita Yushchenko (1<<1) | (0xff<<2) | (0xf<<10), 1,
2133bedbac5fSNikita Yushchenko "WRITE(16)");
2134bedbac5fSNikita Yushchenko if (reply == 0)
2135bedbac5fSNikita Yushchenko reply = do_write(common);
2136bedbac5fSNikita Yushchenko break;
2137bedbac5fSNikita Yushchenko
213800a2430fSAndrzej Pietrasiewicz /*
213900a2430fSAndrzej Pietrasiewicz * Some mandatory commands that we recognize but don't implement.
214000a2430fSAndrzej Pietrasiewicz * They don't mean much in this setting. It's left as an exercise
214100a2430fSAndrzej Pietrasiewicz * for anyone interested to implement RESERVE and RELEASE in terms
214200a2430fSAndrzej Pietrasiewicz * of Posix locks.
214300a2430fSAndrzej Pietrasiewicz */
214400a2430fSAndrzej Pietrasiewicz case FORMAT_UNIT:
214500a2430fSAndrzej Pietrasiewicz case RELEASE:
214600a2430fSAndrzej Pietrasiewicz case RESERVE:
214700a2430fSAndrzej Pietrasiewicz case SEND_DIAGNOSTIC:
214800a2430fSAndrzej Pietrasiewicz
214900a2430fSAndrzej Pietrasiewicz default:
215000a2430fSAndrzej Pietrasiewicz unknown_cmnd:
215100a2430fSAndrzej Pietrasiewicz common->data_size_from_cmnd = 0;
215200a2430fSAndrzej Pietrasiewicz sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
215300a2430fSAndrzej Pietrasiewicz reply = check_command(common, common->cmnd_size,
215400a2430fSAndrzej Pietrasiewicz DATA_DIR_UNKNOWN, ~0, 0, unknown);
215500a2430fSAndrzej Pietrasiewicz if (reply == 0) {
215600a2430fSAndrzej Pietrasiewicz common->curlun->sense_data = SS_INVALID_COMMAND;
215700a2430fSAndrzej Pietrasiewicz reply = -EINVAL;
215800a2430fSAndrzej Pietrasiewicz }
215900a2430fSAndrzej Pietrasiewicz break;
216000a2430fSAndrzej Pietrasiewicz }
216100a2430fSAndrzej Pietrasiewicz up_read(&common->filesem);
216200a2430fSAndrzej Pietrasiewicz
216300a2430fSAndrzej Pietrasiewicz if (reply == -EINTR || signal_pending(current))
216400a2430fSAndrzej Pietrasiewicz return -EINTR;
216500a2430fSAndrzej Pietrasiewicz
216600a2430fSAndrzej Pietrasiewicz /* Set up the single reply buffer for finish_reply() */
216700a2430fSAndrzej Pietrasiewicz if (reply == -EINVAL)
216800a2430fSAndrzej Pietrasiewicz reply = 0; /* Error reply length */
216900a2430fSAndrzej Pietrasiewicz if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
217000a2430fSAndrzej Pietrasiewicz reply = min((u32)reply, common->data_size_from_cmnd);
217100a2430fSAndrzej Pietrasiewicz bh->inreq->length = reply;
217200a2430fSAndrzej Pietrasiewicz bh->state = BUF_STATE_FULL;
217300a2430fSAndrzej Pietrasiewicz common->residue -= reply;
217400a2430fSAndrzej Pietrasiewicz } /* Otherwise it's already set */
217500a2430fSAndrzej Pietrasiewicz
217600a2430fSAndrzej Pietrasiewicz return 0;
217700a2430fSAndrzej Pietrasiewicz }
217800a2430fSAndrzej Pietrasiewicz
217900a2430fSAndrzej Pietrasiewicz
218000a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
218100a2430fSAndrzej Pietrasiewicz
received_cbw(struct fsg_dev * fsg,struct fsg_buffhd * bh)218200a2430fSAndrzej Pietrasiewicz static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
218300a2430fSAndrzej Pietrasiewicz {
218400a2430fSAndrzej Pietrasiewicz struct usb_request *req = bh->outreq;
218500a2430fSAndrzej Pietrasiewicz struct bulk_cb_wrap *cbw = req->buf;
218600a2430fSAndrzej Pietrasiewicz struct fsg_common *common = fsg->common;
218700a2430fSAndrzej Pietrasiewicz
218800a2430fSAndrzej Pietrasiewicz /* Was this a real packet? Should it be ignored? */
218900a2430fSAndrzej Pietrasiewicz if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
219000a2430fSAndrzej Pietrasiewicz return -EINVAL;
219100a2430fSAndrzej Pietrasiewicz
219200a2430fSAndrzej Pietrasiewicz /* Is the CBW valid? */
219300a2430fSAndrzej Pietrasiewicz if (req->actual != US_BULK_CB_WRAP_LEN ||
219400a2430fSAndrzej Pietrasiewicz cbw->Signature != cpu_to_le32(
219500a2430fSAndrzej Pietrasiewicz US_BULK_CB_SIGN)) {
219600a2430fSAndrzej Pietrasiewicz DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
219700a2430fSAndrzej Pietrasiewicz req->actual,
219800a2430fSAndrzej Pietrasiewicz le32_to_cpu(cbw->Signature));
219900a2430fSAndrzej Pietrasiewicz
220000a2430fSAndrzej Pietrasiewicz /*
220100a2430fSAndrzej Pietrasiewicz * The Bulk-only spec says we MUST stall the IN endpoint
220200a2430fSAndrzej Pietrasiewicz * (6.6.1), so it's unavoidable. It also says we must
220300a2430fSAndrzej Pietrasiewicz * retain this state until the next reset, but there's
220400a2430fSAndrzej Pietrasiewicz * no way to tell the controller driver it should ignore
220500a2430fSAndrzej Pietrasiewicz * Clear-Feature(HALT) requests.
220600a2430fSAndrzej Pietrasiewicz *
220700a2430fSAndrzej Pietrasiewicz * We aren't required to halt the OUT endpoint; instead
220800a2430fSAndrzej Pietrasiewicz * we can simply accept and discard any data received
220900a2430fSAndrzej Pietrasiewicz * until the next reset.
221000a2430fSAndrzej Pietrasiewicz */
221100a2430fSAndrzej Pietrasiewicz wedge_bulk_in_endpoint(fsg);
221200a2430fSAndrzej Pietrasiewicz set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
221300a2430fSAndrzej Pietrasiewicz return -EINVAL;
221400a2430fSAndrzej Pietrasiewicz }
221500a2430fSAndrzej Pietrasiewicz
221600a2430fSAndrzej Pietrasiewicz /* Is the CBW meaningful? */
2217dd02ea5aSKrzysztof Opasiak if (cbw->Lun >= ARRAY_SIZE(common->luns) ||
2218dd02ea5aSKrzysztof Opasiak cbw->Flags & ~US_BULK_FLAG_IN || cbw->Length <= 0 ||
2219dd02ea5aSKrzysztof Opasiak cbw->Length > MAX_COMMAND_SIZE) {
222000a2430fSAndrzej Pietrasiewicz DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
222100a2430fSAndrzej Pietrasiewicz "cmdlen %u\n",
222200a2430fSAndrzej Pietrasiewicz cbw->Lun, cbw->Flags, cbw->Length);
222300a2430fSAndrzej Pietrasiewicz
222400a2430fSAndrzej Pietrasiewicz /*
222500a2430fSAndrzej Pietrasiewicz * We can do anything we want here, so let's stall the
222600a2430fSAndrzej Pietrasiewicz * bulk pipes if we are allowed to.
222700a2430fSAndrzej Pietrasiewicz */
222800a2430fSAndrzej Pietrasiewicz if (common->can_stall) {
222900a2430fSAndrzej Pietrasiewicz fsg_set_halt(fsg, fsg->bulk_out);
223000a2430fSAndrzej Pietrasiewicz halt_bulk_in_endpoint(fsg);
223100a2430fSAndrzej Pietrasiewicz }
223200a2430fSAndrzej Pietrasiewicz return -EINVAL;
223300a2430fSAndrzej Pietrasiewicz }
223400a2430fSAndrzej Pietrasiewicz
223500a2430fSAndrzej Pietrasiewicz /* Save the command for later */
223600a2430fSAndrzej Pietrasiewicz common->cmnd_size = cbw->Length;
223700a2430fSAndrzej Pietrasiewicz memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
223800a2430fSAndrzej Pietrasiewicz if (cbw->Flags & US_BULK_FLAG_IN)
223900a2430fSAndrzej Pietrasiewicz common->data_dir = DATA_DIR_TO_HOST;
224000a2430fSAndrzej Pietrasiewicz else
224100a2430fSAndrzej Pietrasiewicz common->data_dir = DATA_DIR_FROM_HOST;
224200a2430fSAndrzej Pietrasiewicz common->data_size = le32_to_cpu(cbw->DataTransferLength);
224300a2430fSAndrzej Pietrasiewicz if (common->data_size == 0)
224400a2430fSAndrzej Pietrasiewicz common->data_dir = DATA_DIR_NONE;
224500a2430fSAndrzej Pietrasiewicz common->lun = cbw->Lun;
2246dd02ea5aSKrzysztof Opasiak if (common->lun < ARRAY_SIZE(common->luns))
224700a2430fSAndrzej Pietrasiewicz common->curlun = common->luns[common->lun];
224800a2430fSAndrzej Pietrasiewicz else
224900a2430fSAndrzej Pietrasiewicz common->curlun = NULL;
225000a2430fSAndrzej Pietrasiewicz common->tag = cbw->Tag;
225100a2430fSAndrzej Pietrasiewicz return 0;
225200a2430fSAndrzej Pietrasiewicz }
225300a2430fSAndrzej Pietrasiewicz
get_next_command(struct fsg_common * common)225400a2430fSAndrzej Pietrasiewicz static int get_next_command(struct fsg_common *common)
225500a2430fSAndrzej Pietrasiewicz {
225600a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh;
225700a2430fSAndrzej Pietrasiewicz int rc = 0;
225800a2430fSAndrzej Pietrasiewicz
225900a2430fSAndrzej Pietrasiewicz /* Wait for the next buffer to become available */
226000a2430fSAndrzej Pietrasiewicz bh = common->next_buffhd_to_fill;
2261225785aeSAlan Stern rc = sleep_thread(common, true, bh);
226200a2430fSAndrzej Pietrasiewicz if (rc)
226300a2430fSAndrzej Pietrasiewicz return rc;
226400a2430fSAndrzej Pietrasiewicz
226500a2430fSAndrzej Pietrasiewicz /* Queue a request to read a Bulk-only CBW */
226600a2430fSAndrzej Pietrasiewicz set_bulk_out_req_length(common, bh, US_BULK_CB_WRAP_LEN);
226700a2430fSAndrzej Pietrasiewicz if (!start_out_transfer(common, bh))
226800a2430fSAndrzej Pietrasiewicz /* Don't know what to do if common->fsg is NULL */
226900a2430fSAndrzej Pietrasiewicz return -EIO;
227000a2430fSAndrzej Pietrasiewicz
227100a2430fSAndrzej Pietrasiewicz /*
227200a2430fSAndrzej Pietrasiewicz * We will drain the buffer in software, which means we
227300a2430fSAndrzej Pietrasiewicz * can reuse it for the next filling. No need to advance
227400a2430fSAndrzej Pietrasiewicz * next_buffhd_to_fill.
227500a2430fSAndrzej Pietrasiewicz */
227600a2430fSAndrzej Pietrasiewicz
227700a2430fSAndrzej Pietrasiewicz /* Wait for the CBW to arrive */
2278225785aeSAlan Stern rc = sleep_thread(common, true, bh);
227900a2430fSAndrzej Pietrasiewicz if (rc)
228000a2430fSAndrzej Pietrasiewicz return rc;
2281225785aeSAlan Stern
228200a2430fSAndrzej Pietrasiewicz rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
228300a2430fSAndrzej Pietrasiewicz bh->state = BUF_STATE_EMPTY;
228400a2430fSAndrzej Pietrasiewicz
228500a2430fSAndrzej Pietrasiewicz return rc;
228600a2430fSAndrzej Pietrasiewicz }
228700a2430fSAndrzej Pietrasiewicz
228800a2430fSAndrzej Pietrasiewicz
228900a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
229000a2430fSAndrzej Pietrasiewicz
alloc_request(struct fsg_common * common,struct usb_ep * ep,struct usb_request ** preq)229100a2430fSAndrzej Pietrasiewicz static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
229200a2430fSAndrzej Pietrasiewicz struct usb_request **preq)
229300a2430fSAndrzej Pietrasiewicz {
229400a2430fSAndrzej Pietrasiewicz *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
229500a2430fSAndrzej Pietrasiewicz if (*preq)
229600a2430fSAndrzej Pietrasiewicz return 0;
229700a2430fSAndrzej Pietrasiewicz ERROR(common, "can't allocate request for %s\n", ep->name);
229800a2430fSAndrzej Pietrasiewicz return -ENOMEM;
229900a2430fSAndrzej Pietrasiewicz }
230000a2430fSAndrzej Pietrasiewicz
230100a2430fSAndrzej Pietrasiewicz /* Reset interface setting and re-init endpoint state (toggle etc). */
do_set_interface(struct fsg_common * common,struct fsg_dev * new_fsg)230200a2430fSAndrzej Pietrasiewicz static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg)
230300a2430fSAndrzej Pietrasiewicz {
230400a2430fSAndrzej Pietrasiewicz struct fsg_dev *fsg;
230500a2430fSAndrzej Pietrasiewicz int i, rc = 0;
230600a2430fSAndrzej Pietrasiewicz
230700a2430fSAndrzej Pietrasiewicz if (common->running)
230800a2430fSAndrzej Pietrasiewicz DBG(common, "reset interface\n");
230900a2430fSAndrzej Pietrasiewicz
231000a2430fSAndrzej Pietrasiewicz reset:
231100a2430fSAndrzej Pietrasiewicz /* Deallocate the requests */
231200a2430fSAndrzej Pietrasiewicz if (common->fsg) {
231300a2430fSAndrzej Pietrasiewicz fsg = common->fsg;
231400a2430fSAndrzej Pietrasiewicz
231500a2430fSAndrzej Pietrasiewicz for (i = 0; i < common->fsg_num_buffers; ++i) {
231600a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh = &common->buffhds[i];
231700a2430fSAndrzej Pietrasiewicz
231800a2430fSAndrzej Pietrasiewicz if (bh->inreq) {
231900a2430fSAndrzej Pietrasiewicz usb_ep_free_request(fsg->bulk_in, bh->inreq);
232000a2430fSAndrzej Pietrasiewicz bh->inreq = NULL;
232100a2430fSAndrzej Pietrasiewicz }
232200a2430fSAndrzej Pietrasiewicz if (bh->outreq) {
232300a2430fSAndrzej Pietrasiewicz usb_ep_free_request(fsg->bulk_out, bh->outreq);
232400a2430fSAndrzej Pietrasiewicz bh->outreq = NULL;
232500a2430fSAndrzej Pietrasiewicz }
232600a2430fSAndrzej Pietrasiewicz }
232700a2430fSAndrzej Pietrasiewicz
232800a2430fSAndrzej Pietrasiewicz /* Disable the endpoints */
232900a2430fSAndrzej Pietrasiewicz if (fsg->bulk_in_enabled) {
233000a2430fSAndrzej Pietrasiewicz usb_ep_disable(fsg->bulk_in);
233100a2430fSAndrzej Pietrasiewicz fsg->bulk_in_enabled = 0;
233200a2430fSAndrzej Pietrasiewicz }
233300a2430fSAndrzej Pietrasiewicz if (fsg->bulk_out_enabled) {
233400a2430fSAndrzej Pietrasiewicz usb_ep_disable(fsg->bulk_out);
233500a2430fSAndrzej Pietrasiewicz fsg->bulk_out_enabled = 0;
233600a2430fSAndrzej Pietrasiewicz }
233700a2430fSAndrzej Pietrasiewicz
233800a2430fSAndrzej Pietrasiewicz common->fsg = NULL;
233900a2430fSAndrzej Pietrasiewicz wake_up(&common->fsg_wait);
234000a2430fSAndrzej Pietrasiewicz }
234100a2430fSAndrzej Pietrasiewicz
234200a2430fSAndrzej Pietrasiewicz common->running = 0;
234300a2430fSAndrzej Pietrasiewicz if (!new_fsg || rc)
234400a2430fSAndrzej Pietrasiewicz return rc;
234500a2430fSAndrzej Pietrasiewicz
234600a2430fSAndrzej Pietrasiewicz common->fsg = new_fsg;
234700a2430fSAndrzej Pietrasiewicz fsg = common->fsg;
234800a2430fSAndrzej Pietrasiewicz
234900a2430fSAndrzej Pietrasiewicz /* Enable the endpoints */
235000a2430fSAndrzej Pietrasiewicz rc = config_ep_by_speed(common->gadget, &(fsg->function), fsg->bulk_in);
235100a2430fSAndrzej Pietrasiewicz if (rc)
235200a2430fSAndrzej Pietrasiewicz goto reset;
235300a2430fSAndrzej Pietrasiewicz rc = usb_ep_enable(fsg->bulk_in);
235400a2430fSAndrzej Pietrasiewicz if (rc)
235500a2430fSAndrzej Pietrasiewicz goto reset;
235600a2430fSAndrzej Pietrasiewicz fsg->bulk_in->driver_data = common;
235700a2430fSAndrzej Pietrasiewicz fsg->bulk_in_enabled = 1;
235800a2430fSAndrzej Pietrasiewicz
235900a2430fSAndrzej Pietrasiewicz rc = config_ep_by_speed(common->gadget, &(fsg->function),
236000a2430fSAndrzej Pietrasiewicz fsg->bulk_out);
236100a2430fSAndrzej Pietrasiewicz if (rc)
236200a2430fSAndrzej Pietrasiewicz goto reset;
236300a2430fSAndrzej Pietrasiewicz rc = usb_ep_enable(fsg->bulk_out);
236400a2430fSAndrzej Pietrasiewicz if (rc)
236500a2430fSAndrzej Pietrasiewicz goto reset;
236600a2430fSAndrzej Pietrasiewicz fsg->bulk_out->driver_data = common;
236700a2430fSAndrzej Pietrasiewicz fsg->bulk_out_enabled = 1;
236800a2430fSAndrzej Pietrasiewicz common->bulk_out_maxpacket = usb_endpoint_maxp(fsg->bulk_out->desc);
236900a2430fSAndrzej Pietrasiewicz clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
237000a2430fSAndrzej Pietrasiewicz
237100a2430fSAndrzej Pietrasiewicz /* Allocate the requests */
237200a2430fSAndrzej Pietrasiewicz for (i = 0; i < common->fsg_num_buffers; ++i) {
237300a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh = &common->buffhds[i];
237400a2430fSAndrzej Pietrasiewicz
237500a2430fSAndrzej Pietrasiewicz rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
237600a2430fSAndrzej Pietrasiewicz if (rc)
237700a2430fSAndrzej Pietrasiewicz goto reset;
237800a2430fSAndrzej Pietrasiewicz rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
237900a2430fSAndrzej Pietrasiewicz if (rc)
238000a2430fSAndrzej Pietrasiewicz goto reset;
238100a2430fSAndrzej Pietrasiewicz bh->inreq->buf = bh->outreq->buf = bh->buf;
238200a2430fSAndrzej Pietrasiewicz bh->inreq->context = bh->outreq->context = bh;
238300a2430fSAndrzej Pietrasiewicz bh->inreq->complete = bulk_in_complete;
238400a2430fSAndrzej Pietrasiewicz bh->outreq->complete = bulk_out_complete;
238500a2430fSAndrzej Pietrasiewicz }
238600a2430fSAndrzej Pietrasiewicz
238700a2430fSAndrzej Pietrasiewicz common->running = 1;
2388dd02ea5aSKrzysztof Opasiak for (i = 0; i < ARRAY_SIZE(common->luns); ++i)
238900a2430fSAndrzej Pietrasiewicz if (common->luns[i])
239000a2430fSAndrzej Pietrasiewicz common->luns[i]->unit_attention_data =
239100a2430fSAndrzej Pietrasiewicz SS_RESET_OCCURRED;
239200a2430fSAndrzej Pietrasiewicz return rc;
239300a2430fSAndrzej Pietrasiewicz }
239400a2430fSAndrzej Pietrasiewicz
239500a2430fSAndrzej Pietrasiewicz
239600a2430fSAndrzej Pietrasiewicz /****************************** ALT CONFIGS ******************************/
239700a2430fSAndrzej Pietrasiewicz
fsg_set_alt(struct usb_function * f,unsigned intf,unsigned alt)239800a2430fSAndrzej Pietrasiewicz static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
239900a2430fSAndrzej Pietrasiewicz {
240000a2430fSAndrzej Pietrasiewicz struct fsg_dev *fsg = fsg_from_func(f);
24014a56a478SBenjamin Herrenschmidt
24024a56a478SBenjamin Herrenschmidt __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, fsg);
240300a2430fSAndrzej Pietrasiewicz return USB_GADGET_DELAYED_STATUS;
240400a2430fSAndrzej Pietrasiewicz }
240500a2430fSAndrzej Pietrasiewicz
fsg_disable(struct usb_function * f)240600a2430fSAndrzej Pietrasiewicz static void fsg_disable(struct usb_function *f)
240700a2430fSAndrzej Pietrasiewicz {
240800a2430fSAndrzej Pietrasiewicz struct fsg_dev *fsg = fsg_from_func(f);
24094a56a478SBenjamin Herrenschmidt
24109fff139aSWesley Cheng /* Disable the endpoints */
24119fff139aSWesley Cheng if (fsg->bulk_in_enabled) {
24129fff139aSWesley Cheng usb_ep_disable(fsg->bulk_in);
24139fff139aSWesley Cheng fsg->bulk_in_enabled = 0;
24149fff139aSWesley Cheng }
24159fff139aSWesley Cheng if (fsg->bulk_out_enabled) {
24169fff139aSWesley Cheng usb_ep_disable(fsg->bulk_out);
24179fff139aSWesley Cheng fsg->bulk_out_enabled = 0;
24189fff139aSWesley Cheng }
24199fff139aSWesley Cheng
24204a56a478SBenjamin Herrenschmidt __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, NULL);
242100a2430fSAndrzej Pietrasiewicz }
242200a2430fSAndrzej Pietrasiewicz
242300a2430fSAndrzej Pietrasiewicz
242400a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
242500a2430fSAndrzej Pietrasiewicz
handle_exception(struct fsg_common * common)242600a2430fSAndrzej Pietrasiewicz static void handle_exception(struct fsg_common *common)
242700a2430fSAndrzej Pietrasiewicz {
242800a2430fSAndrzej Pietrasiewicz int i;
242900a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh;
243000a2430fSAndrzej Pietrasiewicz enum fsg_state old_state;
243100a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun;
243200a2430fSAndrzej Pietrasiewicz unsigned int exception_req_tag;
24334a56a478SBenjamin Herrenschmidt struct fsg_dev *new_fsg;
243400a2430fSAndrzej Pietrasiewicz
243500a2430fSAndrzej Pietrasiewicz /*
243600a2430fSAndrzej Pietrasiewicz * Clear the existing signals. Anything but SIGUSR1 is converted
243700a2430fSAndrzej Pietrasiewicz * into a high-priority EXIT exception.
243800a2430fSAndrzej Pietrasiewicz */
243900a2430fSAndrzej Pietrasiewicz for (;;) {
2440961366a0SEric W. Biederman int sig = kernel_dequeue_signal();
244100a2430fSAndrzej Pietrasiewicz if (!sig)
244200a2430fSAndrzej Pietrasiewicz break;
244300a2430fSAndrzej Pietrasiewicz if (sig != SIGUSR1) {
244478db441dSAlan Stern spin_lock_irq(&common->lock);
244500a2430fSAndrzej Pietrasiewicz if (common->state < FSG_STATE_EXIT)
244600a2430fSAndrzej Pietrasiewicz DBG(common, "Main thread exiting on signal\n");
244778db441dSAlan Stern common->state = FSG_STATE_EXIT;
244878db441dSAlan Stern spin_unlock_irq(&common->lock);
244900a2430fSAndrzej Pietrasiewicz }
245000a2430fSAndrzej Pietrasiewicz }
245100a2430fSAndrzej Pietrasiewicz
245200a2430fSAndrzej Pietrasiewicz /* Cancel all the pending transfers */
245300a2430fSAndrzej Pietrasiewicz if (likely(common->fsg)) {
245400a2430fSAndrzej Pietrasiewicz for (i = 0; i < common->fsg_num_buffers; ++i) {
245500a2430fSAndrzej Pietrasiewicz bh = &common->buffhds[i];
2456225785aeSAlan Stern if (bh->state == BUF_STATE_SENDING)
245700a2430fSAndrzej Pietrasiewicz usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2458225785aeSAlan Stern if (bh->state == BUF_STATE_RECEIVING)
245900a2430fSAndrzej Pietrasiewicz usb_ep_dequeue(common->fsg->bulk_out,
246000a2430fSAndrzej Pietrasiewicz bh->outreq);
246100a2430fSAndrzej Pietrasiewicz
2462225785aeSAlan Stern /* Wait for a transfer to become idle */
2463225785aeSAlan Stern if (sleep_thread(common, false, bh))
246400a2430fSAndrzej Pietrasiewicz return;
246500a2430fSAndrzej Pietrasiewicz }
246600a2430fSAndrzej Pietrasiewicz
246700a2430fSAndrzej Pietrasiewicz /* Clear out the controller's fifos */
246800a2430fSAndrzej Pietrasiewicz if (common->fsg->bulk_in_enabled)
246900a2430fSAndrzej Pietrasiewicz usb_ep_fifo_flush(common->fsg->bulk_in);
247000a2430fSAndrzej Pietrasiewicz if (common->fsg->bulk_out_enabled)
247100a2430fSAndrzej Pietrasiewicz usb_ep_fifo_flush(common->fsg->bulk_out);
247200a2430fSAndrzej Pietrasiewicz }
247300a2430fSAndrzej Pietrasiewicz
247400a2430fSAndrzej Pietrasiewicz /*
247500a2430fSAndrzej Pietrasiewicz * Reset the I/O buffer states and pointers, the SCSI
247600a2430fSAndrzej Pietrasiewicz * state, and the exception. Then invoke the handler.
247700a2430fSAndrzej Pietrasiewicz */
247800a2430fSAndrzej Pietrasiewicz spin_lock_irq(&common->lock);
247900a2430fSAndrzej Pietrasiewicz
248000a2430fSAndrzej Pietrasiewicz for (i = 0; i < common->fsg_num_buffers; ++i) {
248100a2430fSAndrzej Pietrasiewicz bh = &common->buffhds[i];
248200a2430fSAndrzej Pietrasiewicz bh->state = BUF_STATE_EMPTY;
248300a2430fSAndrzej Pietrasiewicz }
248400a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_fill = &common->buffhds[0];
248500a2430fSAndrzej Pietrasiewicz common->next_buffhd_to_drain = &common->buffhds[0];
248600a2430fSAndrzej Pietrasiewicz exception_req_tag = common->exception_req_tag;
24874a56a478SBenjamin Herrenschmidt new_fsg = common->exception_arg;
248800a2430fSAndrzej Pietrasiewicz old_state = common->state;
248978db441dSAlan Stern common->state = FSG_STATE_NORMAL;
249000a2430fSAndrzej Pietrasiewicz
249178db441dSAlan Stern if (old_state != FSG_STATE_ABORT_BULK_OUT) {
2492dd02ea5aSKrzysztof Opasiak for (i = 0; i < ARRAY_SIZE(common->luns); ++i) {
249300a2430fSAndrzej Pietrasiewicz curlun = common->luns[i];
249400a2430fSAndrzej Pietrasiewicz if (!curlun)
249500a2430fSAndrzej Pietrasiewicz continue;
249600a2430fSAndrzej Pietrasiewicz curlun->prevent_medium_removal = 0;
249700a2430fSAndrzej Pietrasiewicz curlun->sense_data = SS_NO_SENSE;
249800a2430fSAndrzej Pietrasiewicz curlun->unit_attention_data = SS_NO_SENSE;
249900a2430fSAndrzej Pietrasiewicz curlun->sense_data_info = 0;
250000a2430fSAndrzej Pietrasiewicz curlun->info_valid = 0;
250100a2430fSAndrzej Pietrasiewicz }
250200a2430fSAndrzej Pietrasiewicz }
250300a2430fSAndrzej Pietrasiewicz spin_unlock_irq(&common->lock);
250400a2430fSAndrzej Pietrasiewicz
250500a2430fSAndrzej Pietrasiewicz /* Carry out any extra actions required for the exception */
250600a2430fSAndrzej Pietrasiewicz switch (old_state) {
250778db441dSAlan Stern case FSG_STATE_NORMAL:
250800a2430fSAndrzej Pietrasiewicz break;
250900a2430fSAndrzej Pietrasiewicz
251078db441dSAlan Stern case FSG_STATE_ABORT_BULK_OUT:
251178db441dSAlan Stern send_status(common);
251278db441dSAlan Stern break;
251378db441dSAlan Stern
251478db441dSAlan Stern case FSG_STATE_PROTOCOL_RESET:
251500a2430fSAndrzej Pietrasiewicz /*
251600a2430fSAndrzej Pietrasiewicz * In case we were forced against our will to halt a
251700a2430fSAndrzej Pietrasiewicz * bulk endpoint, clear the halt now. (The SuperH UDC
251800a2430fSAndrzej Pietrasiewicz * requires this.)
251900a2430fSAndrzej Pietrasiewicz */
252000a2430fSAndrzej Pietrasiewicz if (!fsg_is_set(common))
252100a2430fSAndrzej Pietrasiewicz break;
252200a2430fSAndrzej Pietrasiewicz if (test_and_clear_bit(IGNORE_BULK_OUT,
252300a2430fSAndrzej Pietrasiewicz &common->fsg->atomic_bitflags))
252400a2430fSAndrzej Pietrasiewicz usb_ep_clear_halt(common->fsg->bulk_in);
252500a2430fSAndrzej Pietrasiewicz
252600a2430fSAndrzej Pietrasiewicz if (common->ep0_req_tag == exception_req_tag)
252700a2430fSAndrzej Pietrasiewicz ep0_queue(common); /* Complete the status stage */
252800a2430fSAndrzej Pietrasiewicz
252900a2430fSAndrzej Pietrasiewicz /*
253000a2430fSAndrzej Pietrasiewicz * Technically this should go here, but it would only be
253100a2430fSAndrzej Pietrasiewicz * a waste of time. Ditto for the INTERFACE_CHANGE and
253200a2430fSAndrzej Pietrasiewicz * CONFIG_CHANGE cases.
253300a2430fSAndrzej Pietrasiewicz */
2534dd02ea5aSKrzysztof Opasiak /* for (i = 0; i < common->ARRAY_SIZE(common->luns); ++i) */
253500a2430fSAndrzej Pietrasiewicz /* if (common->luns[i]) */
253600a2430fSAndrzej Pietrasiewicz /* common->luns[i]->unit_attention_data = */
253700a2430fSAndrzej Pietrasiewicz /* SS_RESET_OCCURRED; */
253800a2430fSAndrzej Pietrasiewicz break;
253900a2430fSAndrzej Pietrasiewicz
254000a2430fSAndrzej Pietrasiewicz case FSG_STATE_CONFIG_CHANGE:
25414a56a478SBenjamin Herrenschmidt do_set_interface(common, new_fsg);
25424a56a478SBenjamin Herrenschmidt if (new_fsg)
254300a2430fSAndrzej Pietrasiewicz usb_composite_setup_continue(common->cdev);
254400a2430fSAndrzej Pietrasiewicz break;
254500a2430fSAndrzej Pietrasiewicz
254600a2430fSAndrzej Pietrasiewicz case FSG_STATE_EXIT:
254700a2430fSAndrzej Pietrasiewicz do_set_interface(common, NULL); /* Free resources */
254800a2430fSAndrzej Pietrasiewicz spin_lock_irq(&common->lock);
254900a2430fSAndrzej Pietrasiewicz common->state = FSG_STATE_TERMINATED; /* Stop the thread */
255000a2430fSAndrzej Pietrasiewicz spin_unlock_irq(&common->lock);
255100a2430fSAndrzej Pietrasiewicz break;
255200a2430fSAndrzej Pietrasiewicz
255378db441dSAlan Stern case FSG_STATE_TERMINATED:
255400a2430fSAndrzej Pietrasiewicz break;
255500a2430fSAndrzej Pietrasiewicz }
255600a2430fSAndrzej Pietrasiewicz }
255700a2430fSAndrzej Pietrasiewicz
255800a2430fSAndrzej Pietrasiewicz
255900a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
256000a2430fSAndrzej Pietrasiewicz
fsg_main_thread(void * common_)256100a2430fSAndrzej Pietrasiewicz static int fsg_main_thread(void *common_)
256200a2430fSAndrzej Pietrasiewicz {
256300a2430fSAndrzej Pietrasiewicz struct fsg_common *common = common_;
25641fbbb78fSAlan Stern int i;
256500a2430fSAndrzej Pietrasiewicz
256600a2430fSAndrzej Pietrasiewicz /*
256700a2430fSAndrzej Pietrasiewicz * Allow the thread to be killed by a signal, but set the signal mask
256800a2430fSAndrzej Pietrasiewicz * to block everything but INT, TERM, KILL, and USR1.
256900a2430fSAndrzej Pietrasiewicz */
257000a2430fSAndrzej Pietrasiewicz allow_signal(SIGINT);
257100a2430fSAndrzej Pietrasiewicz allow_signal(SIGTERM);
257200a2430fSAndrzej Pietrasiewicz allow_signal(SIGKILL);
257300a2430fSAndrzej Pietrasiewicz allow_signal(SIGUSR1);
257400a2430fSAndrzej Pietrasiewicz
257500a2430fSAndrzej Pietrasiewicz /* Allow the thread to be frozen */
257600a2430fSAndrzej Pietrasiewicz set_freezable();
257700a2430fSAndrzej Pietrasiewicz
257800a2430fSAndrzej Pietrasiewicz /* The main loop */
257900a2430fSAndrzej Pietrasiewicz while (common->state != FSG_STATE_TERMINATED) {
258000a2430fSAndrzej Pietrasiewicz if (exception_in_progress(common) || signal_pending(current)) {
258100a2430fSAndrzej Pietrasiewicz handle_exception(common);
258200a2430fSAndrzej Pietrasiewicz continue;
258300a2430fSAndrzej Pietrasiewicz }
258400a2430fSAndrzej Pietrasiewicz
258500a2430fSAndrzej Pietrasiewicz if (!common->running) {
2586225785aeSAlan Stern sleep_thread(common, true, NULL);
258700a2430fSAndrzej Pietrasiewicz continue;
258800a2430fSAndrzej Pietrasiewicz }
258900a2430fSAndrzej Pietrasiewicz
259078db441dSAlan Stern if (get_next_command(common) || exception_in_progress(common))
259100a2430fSAndrzej Pietrasiewicz continue;
259278db441dSAlan Stern if (do_scsi_command(common) || exception_in_progress(common))
259300a2430fSAndrzej Pietrasiewicz continue;
259478db441dSAlan Stern if (finish_reply(common) || exception_in_progress(common))
259500a2430fSAndrzej Pietrasiewicz continue;
259678db441dSAlan Stern send_status(common);
259700a2430fSAndrzej Pietrasiewicz }
259800a2430fSAndrzej Pietrasiewicz
259900a2430fSAndrzej Pietrasiewicz spin_lock_irq(&common->lock);
260000a2430fSAndrzej Pietrasiewicz common->thread_task = NULL;
260100a2430fSAndrzej Pietrasiewicz spin_unlock_irq(&common->lock);
260200a2430fSAndrzej Pietrasiewicz
26031fbbb78fSAlan Stern /* Eject media from all LUNs */
260400a2430fSAndrzej Pietrasiewicz
260500a2430fSAndrzej Pietrasiewicz down_write(&common->filesem);
2606bee91869SAxel Lin for (i = 0; i < ARRAY_SIZE(common->luns); i++) {
2607dd02ea5aSKrzysztof Opasiak struct fsg_lun *curlun = common->luns[i];
260800a2430fSAndrzej Pietrasiewicz
26091fbbb78fSAlan Stern if (curlun && fsg_lun_is_open(curlun))
261000a2430fSAndrzej Pietrasiewicz fsg_lun_close(curlun);
261100a2430fSAndrzej Pietrasiewicz }
261200a2430fSAndrzej Pietrasiewicz up_write(&common->filesem);
261300a2430fSAndrzej Pietrasiewicz
261400a2430fSAndrzej Pietrasiewicz /* Let fsg_unbind() know the thread has exited */
2615cead1855SEric W. Biederman kthread_complete_and_exit(&common->thread_notifier, 0);
261600a2430fSAndrzej Pietrasiewicz }
261700a2430fSAndrzej Pietrasiewicz
261800a2430fSAndrzej Pietrasiewicz
261900a2430fSAndrzej Pietrasiewicz /*************************** DEVICE ATTRIBUTES ***************************/
262000a2430fSAndrzej Pietrasiewicz
ro_show(struct device * dev,struct device_attribute * attr,char * buf)262100a2430fSAndrzej Pietrasiewicz static ssize_t ro_show(struct device *dev, struct device_attribute *attr, char *buf)
262200a2430fSAndrzej Pietrasiewicz {
262300a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = fsg_lun_from_dev(dev);
262400a2430fSAndrzej Pietrasiewicz
262500a2430fSAndrzej Pietrasiewicz return fsg_show_ro(curlun, buf);
262600a2430fSAndrzej Pietrasiewicz }
262700a2430fSAndrzej Pietrasiewicz
nofua_show(struct device * dev,struct device_attribute * attr,char * buf)262800a2430fSAndrzej Pietrasiewicz static ssize_t nofua_show(struct device *dev, struct device_attribute *attr,
262900a2430fSAndrzej Pietrasiewicz char *buf)
263000a2430fSAndrzej Pietrasiewicz {
263100a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = fsg_lun_from_dev(dev);
263200a2430fSAndrzej Pietrasiewicz
263300a2430fSAndrzej Pietrasiewicz return fsg_show_nofua(curlun, buf);
263400a2430fSAndrzej Pietrasiewicz }
263500a2430fSAndrzej Pietrasiewicz
file_show(struct device * dev,struct device_attribute * attr,char * buf)263600a2430fSAndrzej Pietrasiewicz static ssize_t file_show(struct device *dev, struct device_attribute *attr,
263700a2430fSAndrzej Pietrasiewicz char *buf)
263800a2430fSAndrzej Pietrasiewicz {
263900a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = fsg_lun_from_dev(dev);
264000a2430fSAndrzej Pietrasiewicz struct rw_semaphore *filesem = dev_get_drvdata(dev);
264100a2430fSAndrzej Pietrasiewicz
264200a2430fSAndrzej Pietrasiewicz return fsg_show_file(curlun, filesem, buf);
264300a2430fSAndrzej Pietrasiewicz }
264400a2430fSAndrzej Pietrasiewicz
ro_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)264500a2430fSAndrzej Pietrasiewicz static ssize_t ro_store(struct device *dev, struct device_attribute *attr,
264600a2430fSAndrzej Pietrasiewicz const char *buf, size_t count)
264700a2430fSAndrzej Pietrasiewicz {
264800a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = fsg_lun_from_dev(dev);
264900a2430fSAndrzej Pietrasiewicz struct rw_semaphore *filesem = dev_get_drvdata(dev);
265000a2430fSAndrzej Pietrasiewicz
265100a2430fSAndrzej Pietrasiewicz return fsg_store_ro(curlun, filesem, buf, count);
265200a2430fSAndrzej Pietrasiewicz }
265300a2430fSAndrzej Pietrasiewicz
nofua_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)265400a2430fSAndrzej Pietrasiewicz static ssize_t nofua_store(struct device *dev, struct device_attribute *attr,
265500a2430fSAndrzej Pietrasiewicz const char *buf, size_t count)
265600a2430fSAndrzej Pietrasiewicz {
265700a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = fsg_lun_from_dev(dev);
265800a2430fSAndrzej Pietrasiewicz
265900a2430fSAndrzej Pietrasiewicz return fsg_store_nofua(curlun, buf, count);
266000a2430fSAndrzej Pietrasiewicz }
266100a2430fSAndrzej Pietrasiewicz
file_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)266200a2430fSAndrzej Pietrasiewicz static ssize_t file_store(struct device *dev, struct device_attribute *attr,
266300a2430fSAndrzej Pietrasiewicz const char *buf, size_t count)
266400a2430fSAndrzej Pietrasiewicz {
266500a2430fSAndrzej Pietrasiewicz struct fsg_lun *curlun = fsg_lun_from_dev(dev);
266600a2430fSAndrzej Pietrasiewicz struct rw_semaphore *filesem = dev_get_drvdata(dev);
266700a2430fSAndrzej Pietrasiewicz
266800a2430fSAndrzej Pietrasiewicz return fsg_store_file(curlun, filesem, buf, count);
266900a2430fSAndrzej Pietrasiewicz }
267000a2430fSAndrzej Pietrasiewicz
forced_eject_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2671421c8d9aSMaxim Devaev static ssize_t forced_eject_store(struct device *dev,
2672421c8d9aSMaxim Devaev struct device_attribute *attr,
2673421c8d9aSMaxim Devaev const char *buf, size_t count)
2674421c8d9aSMaxim Devaev {
2675421c8d9aSMaxim Devaev struct fsg_lun *curlun = fsg_lun_from_dev(dev);
2676421c8d9aSMaxim Devaev struct rw_semaphore *filesem = dev_get_drvdata(dev);
2677421c8d9aSMaxim Devaev
2678421c8d9aSMaxim Devaev return fsg_store_forced_eject(curlun, filesem, buf, count);
2679421c8d9aSMaxim Devaev }
2680421c8d9aSMaxim Devaev
268100a2430fSAndrzej Pietrasiewicz static DEVICE_ATTR_RW(nofua);
2682421c8d9aSMaxim Devaev static DEVICE_ATTR_WO(forced_eject);
268300a2430fSAndrzej Pietrasiewicz
2684c4c2fac9SGreg Kroah-Hartman /*
2685c4c2fac9SGreg Kroah-Hartman * Mode of the ro and file attribute files will be overridden in
2686c4c2fac9SGreg Kroah-Hartman * fsg_lun_dev_is_visible() depending on if this is a cdrom, or if it is a
2687c4c2fac9SGreg Kroah-Hartman * removable device.
2688c4c2fac9SGreg Kroah-Hartman */
2689c4c2fac9SGreg Kroah-Hartman static DEVICE_ATTR_RW(ro);
2690c4c2fac9SGreg Kroah-Hartman static DEVICE_ATTR_RW(file);
2691c4c2fac9SGreg Kroah-Hartman
269200a2430fSAndrzej Pietrasiewicz /****************************** FSG COMMON ******************************/
269300a2430fSAndrzej Pietrasiewicz
fsg_lun_release(struct device * dev)269400a2430fSAndrzej Pietrasiewicz static void fsg_lun_release(struct device *dev)
269500a2430fSAndrzej Pietrasiewicz {
269600a2430fSAndrzej Pietrasiewicz /* Nothing needs to be done */
269700a2430fSAndrzej Pietrasiewicz }
269800a2430fSAndrzej Pietrasiewicz
fsg_common_setup(struct fsg_common * common)269900a2430fSAndrzej Pietrasiewicz static struct fsg_common *fsg_common_setup(struct fsg_common *common)
270000a2430fSAndrzej Pietrasiewicz {
270100a2430fSAndrzej Pietrasiewicz if (!common) {
270200a2430fSAndrzej Pietrasiewicz common = kzalloc(sizeof(*common), GFP_KERNEL);
270300a2430fSAndrzej Pietrasiewicz if (!common)
270400a2430fSAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM);
270500a2430fSAndrzej Pietrasiewicz common->free_storage_on_release = 1;
270600a2430fSAndrzej Pietrasiewicz } else {
270700a2430fSAndrzej Pietrasiewicz common->free_storage_on_release = 0;
270800a2430fSAndrzej Pietrasiewicz }
270900a2430fSAndrzej Pietrasiewicz init_rwsem(&common->filesem);
271000a2430fSAndrzej Pietrasiewicz spin_lock_init(&common->lock);
271100a2430fSAndrzej Pietrasiewicz init_completion(&common->thread_notifier);
2712225785aeSAlan Stern init_waitqueue_head(&common->io_wait);
271300a2430fSAndrzej Pietrasiewicz init_waitqueue_head(&common->fsg_wait);
271400a2430fSAndrzej Pietrasiewicz common->state = FSG_STATE_TERMINATED;
2715dd02ea5aSKrzysztof Opasiak memset(common->luns, 0, sizeof(common->luns));
271600a2430fSAndrzej Pietrasiewicz
271700a2430fSAndrzej Pietrasiewicz return common;
271800a2430fSAndrzej Pietrasiewicz }
271900a2430fSAndrzej Pietrasiewicz
fsg_common_set_sysfs(struct fsg_common * common,bool sysfs)272000a2430fSAndrzej Pietrasiewicz void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs)
272100a2430fSAndrzej Pietrasiewicz {
272200a2430fSAndrzej Pietrasiewicz common->sysfs = sysfs;
272300a2430fSAndrzej Pietrasiewicz }
272400a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_common_set_sysfs);
272500a2430fSAndrzej Pietrasiewicz
_fsg_common_free_buffers(struct fsg_buffhd * buffhds,unsigned n)272600a2430fSAndrzej Pietrasiewicz static void _fsg_common_free_buffers(struct fsg_buffhd *buffhds, unsigned n)
272700a2430fSAndrzej Pietrasiewicz {
272800a2430fSAndrzej Pietrasiewicz if (buffhds) {
272900a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh = buffhds;
273000a2430fSAndrzej Pietrasiewicz while (n--) {
273100a2430fSAndrzej Pietrasiewicz kfree(bh->buf);
273200a2430fSAndrzej Pietrasiewicz ++bh;
273300a2430fSAndrzej Pietrasiewicz }
273400a2430fSAndrzej Pietrasiewicz kfree(buffhds);
273500a2430fSAndrzej Pietrasiewicz }
273600a2430fSAndrzej Pietrasiewicz }
273700a2430fSAndrzej Pietrasiewicz
fsg_common_set_num_buffers(struct fsg_common * common,unsigned int n)273800a2430fSAndrzej Pietrasiewicz int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
273900a2430fSAndrzej Pietrasiewicz {
274000a2430fSAndrzej Pietrasiewicz struct fsg_buffhd *bh, *buffhds;
2741fe5a6c48SFelipe Balbi int i;
274200a2430fSAndrzej Pietrasiewicz
274300a2430fSAndrzej Pietrasiewicz buffhds = kcalloc(n, sizeof(*buffhds), GFP_KERNEL);
274400a2430fSAndrzej Pietrasiewicz if (!buffhds)
274500a2430fSAndrzej Pietrasiewicz return -ENOMEM;
274600a2430fSAndrzej Pietrasiewicz
274700a2430fSAndrzej Pietrasiewicz /* Data buffers cyclic list */
274800a2430fSAndrzej Pietrasiewicz bh = buffhds;
274900a2430fSAndrzej Pietrasiewicz i = n;
275000a2430fSAndrzej Pietrasiewicz goto buffhds_first_it;
275100a2430fSAndrzej Pietrasiewicz do {
275200a2430fSAndrzej Pietrasiewicz bh->next = bh + 1;
275300a2430fSAndrzej Pietrasiewicz ++bh;
275400a2430fSAndrzej Pietrasiewicz buffhds_first_it:
275500a2430fSAndrzej Pietrasiewicz bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
275600a2430fSAndrzej Pietrasiewicz if (unlikely(!bh->buf))
275700a2430fSAndrzej Pietrasiewicz goto error_release;
275800a2430fSAndrzej Pietrasiewicz } while (--i);
275900a2430fSAndrzej Pietrasiewicz bh->next = buffhds;
276000a2430fSAndrzej Pietrasiewicz
276100a2430fSAndrzej Pietrasiewicz _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
276200a2430fSAndrzej Pietrasiewicz common->fsg_num_buffers = n;
276300a2430fSAndrzej Pietrasiewicz common->buffhds = buffhds;
276400a2430fSAndrzej Pietrasiewicz
276500a2430fSAndrzej Pietrasiewicz return 0;
276600a2430fSAndrzej Pietrasiewicz
276700a2430fSAndrzej Pietrasiewicz error_release:
276800a2430fSAndrzej Pietrasiewicz /*
276900a2430fSAndrzej Pietrasiewicz * "buf"s pointed to by heads after n - i are NULL
277000a2430fSAndrzej Pietrasiewicz * so releasing them won't hurt
277100a2430fSAndrzej Pietrasiewicz */
277200a2430fSAndrzej Pietrasiewicz _fsg_common_free_buffers(buffhds, n);
277300a2430fSAndrzej Pietrasiewicz
277400a2430fSAndrzej Pietrasiewicz return -ENOMEM;
277500a2430fSAndrzej Pietrasiewicz }
277600a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_common_set_num_buffers);
277700a2430fSAndrzej Pietrasiewicz
fsg_common_remove_lun(struct fsg_lun * lun)27785542f58cSKrzysztof Opasiak void fsg_common_remove_lun(struct fsg_lun *lun)
277900a2430fSAndrzej Pietrasiewicz {
27805542f58cSKrzysztof Opasiak if (device_is_registered(&lun->dev))
278100a2430fSAndrzej Pietrasiewicz device_unregister(&lun->dev);
278200a2430fSAndrzej Pietrasiewicz fsg_lun_close(lun);
278300a2430fSAndrzej Pietrasiewicz kfree(lun);
278400a2430fSAndrzej Pietrasiewicz }
278500a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_common_remove_lun);
278600a2430fSAndrzej Pietrasiewicz
_fsg_common_remove_luns(struct fsg_common * common,int n)278700a2430fSAndrzej Pietrasiewicz static void _fsg_common_remove_luns(struct fsg_common *common, int n)
278800a2430fSAndrzej Pietrasiewicz {
278900a2430fSAndrzej Pietrasiewicz int i;
279000a2430fSAndrzej Pietrasiewicz
279100a2430fSAndrzej Pietrasiewicz for (i = 0; i < n; ++i)
279200a2430fSAndrzej Pietrasiewicz if (common->luns[i]) {
27935542f58cSKrzysztof Opasiak fsg_common_remove_lun(common->luns[i]);
279400a2430fSAndrzej Pietrasiewicz common->luns[i] = NULL;
279500a2430fSAndrzej Pietrasiewicz }
279600a2430fSAndrzej Pietrasiewicz }
279700a2430fSAndrzej Pietrasiewicz
fsg_common_remove_luns(struct fsg_common * common)279800a2430fSAndrzej Pietrasiewicz void fsg_common_remove_luns(struct fsg_common *common)
279900a2430fSAndrzej Pietrasiewicz {
2800dd02ea5aSKrzysztof Opasiak _fsg_common_remove_luns(common, ARRAY_SIZE(common->luns));
280100a2430fSAndrzej Pietrasiewicz }
2802bab7a1f1SKrzysztof Opasiak EXPORT_SYMBOL_GPL(fsg_common_remove_luns);
280300a2430fSAndrzej Pietrasiewicz
fsg_common_free_buffers(struct fsg_common * common)280400a2430fSAndrzej Pietrasiewicz void fsg_common_free_buffers(struct fsg_common *common)
280500a2430fSAndrzej Pietrasiewicz {
280600a2430fSAndrzej Pietrasiewicz _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
280700a2430fSAndrzej Pietrasiewicz common->buffhds = NULL;
280800a2430fSAndrzej Pietrasiewicz }
280900a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_common_free_buffers);
281000a2430fSAndrzej Pietrasiewicz
fsg_common_set_cdev(struct fsg_common * common,struct usb_composite_dev * cdev,bool can_stall)281100a2430fSAndrzej Pietrasiewicz int fsg_common_set_cdev(struct fsg_common *common,
281200a2430fSAndrzej Pietrasiewicz struct usb_composite_dev *cdev, bool can_stall)
281300a2430fSAndrzej Pietrasiewicz {
281400a2430fSAndrzej Pietrasiewicz struct usb_string *us;
281500a2430fSAndrzej Pietrasiewicz
281600a2430fSAndrzej Pietrasiewicz common->gadget = cdev->gadget;
281700a2430fSAndrzej Pietrasiewicz common->ep0 = cdev->gadget->ep0;
281800a2430fSAndrzej Pietrasiewicz common->ep0req = cdev->req;
281900a2430fSAndrzej Pietrasiewicz common->cdev = cdev;
282000a2430fSAndrzej Pietrasiewicz
282100a2430fSAndrzej Pietrasiewicz us = usb_gstrings_attach(cdev, fsg_strings_array,
282200a2430fSAndrzej Pietrasiewicz ARRAY_SIZE(fsg_strings));
282300a2430fSAndrzej Pietrasiewicz if (IS_ERR(us))
282400a2430fSAndrzej Pietrasiewicz return PTR_ERR(us);
282500a2430fSAndrzej Pietrasiewicz
282600a2430fSAndrzej Pietrasiewicz fsg_intf_desc.iInterface = us[FSG_STRING_INTERFACE].id;
282700a2430fSAndrzej Pietrasiewicz
282800a2430fSAndrzej Pietrasiewicz /*
282900a2430fSAndrzej Pietrasiewicz * Some peripheral controllers are known not to be able to
283000a2430fSAndrzej Pietrasiewicz * halt bulk endpoints correctly. If one of them is present,
283100a2430fSAndrzej Pietrasiewicz * disable stalls.
283200a2430fSAndrzej Pietrasiewicz */
2833a4cc4215SRobert Baldyga common->can_stall = can_stall &&
2834a4cc4215SRobert Baldyga gadget_is_stall_supported(common->gadget);
283500a2430fSAndrzej Pietrasiewicz
283600a2430fSAndrzej Pietrasiewicz return 0;
283700a2430fSAndrzej Pietrasiewicz }
283800a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_common_set_cdev);
283900a2430fSAndrzej Pietrasiewicz
2840c7b364f7STakashi Iwai static struct attribute *fsg_lun_dev_attrs[] = {
2841c7b364f7STakashi Iwai &dev_attr_ro.attr,
2842c7b364f7STakashi Iwai &dev_attr_file.attr,
2843c7b364f7STakashi Iwai &dev_attr_nofua.attr,
2844421c8d9aSMaxim Devaev &dev_attr_forced_eject.attr,
2845c7b364f7STakashi Iwai NULL
2846c7b364f7STakashi Iwai };
2847c7b364f7STakashi Iwai
fsg_lun_dev_is_visible(struct kobject * kobj,struct attribute * attr,int idx)2848c7b364f7STakashi Iwai static umode_t fsg_lun_dev_is_visible(struct kobject *kobj,
2849c7b364f7STakashi Iwai struct attribute *attr, int idx)
285000a2430fSAndrzej Pietrasiewicz {
2851c7b364f7STakashi Iwai struct device *dev = kobj_to_dev(kobj);
2852c7b364f7STakashi Iwai struct fsg_lun *lun = fsg_lun_from_dev(dev);
285300a2430fSAndrzej Pietrasiewicz
2854c7b364f7STakashi Iwai if (attr == &dev_attr_ro.attr)
2855c7b364f7STakashi Iwai return lun->cdrom ? S_IRUGO : (S_IWUSR | S_IRUGO);
2856c7b364f7STakashi Iwai if (attr == &dev_attr_file.attr)
2857c7b364f7STakashi Iwai return lun->removable ? (S_IWUSR | S_IRUGO) : S_IRUGO;
2858c7b364f7STakashi Iwai return attr->mode;
285900a2430fSAndrzej Pietrasiewicz }
286000a2430fSAndrzej Pietrasiewicz
2861c7b364f7STakashi Iwai static const struct attribute_group fsg_lun_dev_group = {
2862c7b364f7STakashi Iwai .attrs = fsg_lun_dev_attrs,
2863c7b364f7STakashi Iwai .is_visible = fsg_lun_dev_is_visible,
2864c7b364f7STakashi Iwai };
286500a2430fSAndrzej Pietrasiewicz
2866c7b364f7STakashi Iwai static const struct attribute_group *fsg_lun_dev_groups[] = {
2867c7b364f7STakashi Iwai &fsg_lun_dev_group,
2868c7b364f7STakashi Iwai NULL
2869c7b364f7STakashi Iwai };
287000a2430fSAndrzej Pietrasiewicz
fsg_common_create_lun(struct fsg_common * common,struct fsg_lun_config * cfg,unsigned int id,const char * name,const char ** name_pfx)287100a2430fSAndrzej Pietrasiewicz int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
287200a2430fSAndrzej Pietrasiewicz unsigned int id, const char *name,
287300a2430fSAndrzej Pietrasiewicz const char **name_pfx)
287400a2430fSAndrzej Pietrasiewicz {
287500a2430fSAndrzej Pietrasiewicz struct fsg_lun *lun;
28766b394dbbSDavid Disseldorp char *pathbuf = NULL, *p = "(no medium)";
287700a2430fSAndrzej Pietrasiewicz int rc = -ENOMEM;
287800a2430fSAndrzej Pietrasiewicz
2879dd02ea5aSKrzysztof Opasiak if (id >= ARRAY_SIZE(common->luns))
288000a2430fSAndrzej Pietrasiewicz return -ENODEV;
288100a2430fSAndrzej Pietrasiewicz
288200a2430fSAndrzej Pietrasiewicz if (common->luns[id])
288300a2430fSAndrzej Pietrasiewicz return -EBUSY;
288400a2430fSAndrzej Pietrasiewicz
288500a2430fSAndrzej Pietrasiewicz if (!cfg->filename && !cfg->removable) {
288600a2430fSAndrzej Pietrasiewicz pr_err("no file given for LUN%d\n", id);
288700a2430fSAndrzej Pietrasiewicz return -EINVAL;
288800a2430fSAndrzej Pietrasiewicz }
288900a2430fSAndrzej Pietrasiewicz
289000a2430fSAndrzej Pietrasiewicz lun = kzalloc(sizeof(*lun), GFP_KERNEL);
289100a2430fSAndrzej Pietrasiewicz if (!lun)
289200a2430fSAndrzej Pietrasiewicz return -ENOMEM;
289300a2430fSAndrzej Pietrasiewicz
289400a2430fSAndrzej Pietrasiewicz lun->name_pfx = name_pfx;
289500a2430fSAndrzej Pietrasiewicz
289600a2430fSAndrzej Pietrasiewicz lun->cdrom = !!cfg->cdrom;
289700a2430fSAndrzej Pietrasiewicz lun->ro = cfg->cdrom || cfg->ro;
289800a2430fSAndrzej Pietrasiewicz lun->initially_ro = lun->ro;
289900a2430fSAndrzej Pietrasiewicz lun->removable = !!cfg->removable;
290000a2430fSAndrzej Pietrasiewicz
290100a2430fSAndrzej Pietrasiewicz if (!common->sysfs) {
290200a2430fSAndrzej Pietrasiewicz /* we DON'T own the name!*/
290300a2430fSAndrzej Pietrasiewicz lun->name = name;
290400a2430fSAndrzej Pietrasiewicz } else {
290500a2430fSAndrzej Pietrasiewicz lun->dev.release = fsg_lun_release;
290600a2430fSAndrzej Pietrasiewicz lun->dev.parent = &common->gadget->dev;
2907c7b364f7STakashi Iwai lun->dev.groups = fsg_lun_dev_groups;
290800a2430fSAndrzej Pietrasiewicz dev_set_drvdata(&lun->dev, &common->filesem);
290900a2430fSAndrzej Pietrasiewicz dev_set_name(&lun->dev, "%s", name);
291000a2430fSAndrzej Pietrasiewicz lun->name = dev_name(&lun->dev);
291100a2430fSAndrzej Pietrasiewicz
2912c7b364f7STakashi Iwai rc = device_register(&lun->dev);
291300a2430fSAndrzej Pietrasiewicz if (rc) {
291400a2430fSAndrzej Pietrasiewicz pr_info("failed to register LUN%d: %d\n", id, rc);
2915c7b364f7STakashi Iwai put_device(&lun->dev);
291600a2430fSAndrzej Pietrasiewicz goto error_sysfs;
291700a2430fSAndrzej Pietrasiewicz }
291800a2430fSAndrzej Pietrasiewicz }
291900a2430fSAndrzej Pietrasiewicz
292000a2430fSAndrzej Pietrasiewicz common->luns[id] = lun;
292100a2430fSAndrzej Pietrasiewicz
292200a2430fSAndrzej Pietrasiewicz if (cfg->filename) {
292300a2430fSAndrzej Pietrasiewicz rc = fsg_lun_open(lun, cfg->filename);
292400a2430fSAndrzej Pietrasiewicz if (rc)
292500a2430fSAndrzej Pietrasiewicz goto error_lun;
292600a2430fSAndrzej Pietrasiewicz
292700a2430fSAndrzej Pietrasiewicz p = "(error)";
29286b394dbbSDavid Disseldorp pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
292900a2430fSAndrzej Pietrasiewicz if (pathbuf) {
29309bf39ab2SMiklos Szeredi p = file_path(lun->filp, pathbuf, PATH_MAX);
293100a2430fSAndrzej Pietrasiewicz if (IS_ERR(p))
293200a2430fSAndrzej Pietrasiewicz p = "(error)";
293300a2430fSAndrzej Pietrasiewicz }
293400a2430fSAndrzej Pietrasiewicz }
293500a2430fSAndrzej Pietrasiewicz pr_info("LUN: %s%s%sfile: %s\n",
293600a2430fSAndrzej Pietrasiewicz lun->removable ? "removable " : "",
293700a2430fSAndrzej Pietrasiewicz lun->ro ? "read only " : "",
293800a2430fSAndrzej Pietrasiewicz lun->cdrom ? "CD-ROM " : "",
293900a2430fSAndrzej Pietrasiewicz p);
294000a2430fSAndrzej Pietrasiewicz kfree(pathbuf);
294100a2430fSAndrzej Pietrasiewicz
294200a2430fSAndrzej Pietrasiewicz return 0;
294300a2430fSAndrzej Pietrasiewicz
294400a2430fSAndrzej Pietrasiewicz error_lun:
29455542f58cSKrzysztof Opasiak if (device_is_registered(&lun->dev))
294600a2430fSAndrzej Pietrasiewicz device_unregister(&lun->dev);
294700a2430fSAndrzej Pietrasiewicz common->luns[id] = NULL;
294800a2430fSAndrzej Pietrasiewicz error_sysfs:
294900a2430fSAndrzej Pietrasiewicz kfree(lun);
295000a2430fSAndrzej Pietrasiewicz return rc;
295100a2430fSAndrzej Pietrasiewicz }
295200a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_common_create_lun);
295300a2430fSAndrzej Pietrasiewicz
fsg_common_create_luns(struct fsg_common * common,struct fsg_config * cfg)295400a2430fSAndrzej Pietrasiewicz int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg)
295500a2430fSAndrzej Pietrasiewicz {
295600a2430fSAndrzej Pietrasiewicz char buf[8]; /* enough for 100000000 different numbers, decimal */
295700a2430fSAndrzej Pietrasiewicz int i, rc;
295800a2430fSAndrzej Pietrasiewicz
2959dd02ea5aSKrzysztof Opasiak fsg_common_remove_luns(common);
2960dd02ea5aSKrzysztof Opasiak
2961dd02ea5aSKrzysztof Opasiak for (i = 0; i < cfg->nluns; ++i) {
296200a2430fSAndrzej Pietrasiewicz snprintf(buf, sizeof(buf), "lun%d", i);
296300a2430fSAndrzej Pietrasiewicz rc = fsg_common_create_lun(common, &cfg->luns[i], i, buf, NULL);
296400a2430fSAndrzej Pietrasiewicz if (rc)
296500a2430fSAndrzej Pietrasiewicz goto fail;
296600a2430fSAndrzej Pietrasiewicz }
296700a2430fSAndrzej Pietrasiewicz
2968dd02ea5aSKrzysztof Opasiak pr_info("Number of LUNs=%d\n", cfg->nluns);
296900a2430fSAndrzej Pietrasiewicz
297000a2430fSAndrzej Pietrasiewicz return 0;
297100a2430fSAndrzej Pietrasiewicz
297200a2430fSAndrzej Pietrasiewicz fail:
297300a2430fSAndrzej Pietrasiewicz _fsg_common_remove_luns(common, i);
297400a2430fSAndrzej Pietrasiewicz return rc;
297500a2430fSAndrzej Pietrasiewicz }
297600a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_common_create_luns);
297700a2430fSAndrzej Pietrasiewicz
fsg_common_set_inquiry_string(struct fsg_common * common,const char * vn,const char * pn)297800a2430fSAndrzej Pietrasiewicz void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
297900a2430fSAndrzej Pietrasiewicz const char *pn)
298000a2430fSAndrzej Pietrasiewicz {
298100a2430fSAndrzej Pietrasiewicz int i;
298200a2430fSAndrzej Pietrasiewicz
298300a2430fSAndrzej Pietrasiewicz /* Prepare inquiryString */
298400a2430fSAndrzej Pietrasiewicz i = get_default_bcdDevice();
298500a2430fSAndrzej Pietrasiewicz snprintf(common->inquiry_string, sizeof(common->inquiry_string),
298600a2430fSAndrzej Pietrasiewicz "%-8s%-16s%04x", vn ?: "Linux",
298700a2430fSAndrzej Pietrasiewicz /* Assume product name dependent on the first LUN */
298800a2430fSAndrzej Pietrasiewicz pn ?: ((*common->luns)->cdrom
298900a2430fSAndrzej Pietrasiewicz ? "File-CD Gadget"
299000a2430fSAndrzej Pietrasiewicz : "File-Stor Gadget"),
299100a2430fSAndrzej Pietrasiewicz i);
299200a2430fSAndrzej Pietrasiewicz }
299300a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_common_set_inquiry_string);
299400a2430fSAndrzej Pietrasiewicz
fsg_common_release(struct fsg_common * common)29951fcba97eSJaejoong Kim static void fsg_common_release(struct fsg_common *common)
299600a2430fSAndrzej Pietrasiewicz {
2997dd02ea5aSKrzysztof Opasiak int i;
299800a2430fSAndrzej Pietrasiewicz
299900a2430fSAndrzej Pietrasiewicz /* If the thread isn't already dead, tell it to exit now */
300000a2430fSAndrzej Pietrasiewicz if (common->state != FSG_STATE_TERMINATED) {
300100a2430fSAndrzej Pietrasiewicz raise_exception(common, FSG_STATE_EXIT);
300200a2430fSAndrzej Pietrasiewicz wait_for_completion(&common->thread_notifier);
300300a2430fSAndrzej Pietrasiewicz }
300400a2430fSAndrzej Pietrasiewicz
3005dd02ea5aSKrzysztof Opasiak for (i = 0; i < ARRAY_SIZE(common->luns); ++i) {
3006dd02ea5aSKrzysztof Opasiak struct fsg_lun *lun = common->luns[i];
300700a2430fSAndrzej Pietrasiewicz if (!lun)
300800a2430fSAndrzej Pietrasiewicz continue;
300900a2430fSAndrzej Pietrasiewicz fsg_lun_close(lun);
30105542f58cSKrzysztof Opasiak if (device_is_registered(&lun->dev))
301100a2430fSAndrzej Pietrasiewicz device_unregister(&lun->dev);
301200a2430fSAndrzej Pietrasiewicz kfree(lun);
301300a2430fSAndrzej Pietrasiewicz }
301400a2430fSAndrzej Pietrasiewicz
301500a2430fSAndrzej Pietrasiewicz _fsg_common_free_buffers(common->buffhds, common->fsg_num_buffers);
301600a2430fSAndrzej Pietrasiewicz if (common->free_storage_on_release)
301700a2430fSAndrzej Pietrasiewicz kfree(common);
301800a2430fSAndrzej Pietrasiewicz }
301900a2430fSAndrzej Pietrasiewicz
302000a2430fSAndrzej Pietrasiewicz
302100a2430fSAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
302200a2430fSAndrzej Pietrasiewicz
fsg_bind(struct usb_configuration * c,struct usb_function * f)302300a2430fSAndrzej Pietrasiewicz static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
302400a2430fSAndrzej Pietrasiewicz {
302500a2430fSAndrzej Pietrasiewicz struct fsg_dev *fsg = fsg_from_func(f);
3026dd02ea5aSKrzysztof Opasiak struct fsg_common *common = fsg->common;
302700a2430fSAndrzej Pietrasiewicz struct usb_gadget *gadget = c->cdev->gadget;
302800a2430fSAndrzej Pietrasiewicz int i;
302900a2430fSAndrzej Pietrasiewicz struct usb_ep *ep;
303000a2430fSAndrzej Pietrasiewicz unsigned max_burst;
303100a2430fSAndrzej Pietrasiewicz int ret;
303200a2430fSAndrzej Pietrasiewicz struct fsg_opts *opts;
303300a2430fSAndrzej Pietrasiewicz
3034dd02ea5aSKrzysztof Opasiak /* Don't allow to bind if we don't have at least one LUN */
3035dd02ea5aSKrzysztof Opasiak ret = _fsg_common_get_max_lun(common);
3036dd02ea5aSKrzysztof Opasiak if (ret < 0) {
3037dd02ea5aSKrzysztof Opasiak pr_err("There should be at least one LUN.\n");
3038dd02ea5aSKrzysztof Opasiak return -EINVAL;
3039dd02ea5aSKrzysztof Opasiak }
3040dd02ea5aSKrzysztof Opasiak
304100a2430fSAndrzej Pietrasiewicz opts = fsg_opts_from_func_inst(f->fi);
304200a2430fSAndrzej Pietrasiewicz if (!opts->no_configfs) {
304300a2430fSAndrzej Pietrasiewicz ret = fsg_common_set_cdev(fsg->common, c->cdev,
304400a2430fSAndrzej Pietrasiewicz fsg->common->can_stall);
304500a2430fSAndrzej Pietrasiewicz if (ret)
304600a2430fSAndrzej Pietrasiewicz return ret;
304700a2430fSAndrzej Pietrasiewicz fsg_common_set_inquiry_string(fsg->common, NULL, NULL);
3048f78bbcaeSMichal Nazarewicz }
3049f78bbcaeSMichal Nazarewicz
3050f78bbcaeSMichal Nazarewicz if (!common->thread_task) {
305178db441dSAlan Stern common->state = FSG_STATE_NORMAL;
3052f78bbcaeSMichal Nazarewicz common->thread_task =
3053f78bbcaeSMichal Nazarewicz kthread_create(fsg_main_thread, common, "file-storage");
3054f78bbcaeSMichal Nazarewicz if (IS_ERR(common->thread_task)) {
305578db441dSAlan Stern ret = PTR_ERR(common->thread_task);
3056f78bbcaeSMichal Nazarewicz common->thread_task = NULL;
3057f78bbcaeSMichal Nazarewicz common->state = FSG_STATE_TERMINATED;
305800a2430fSAndrzej Pietrasiewicz return ret;
305900a2430fSAndrzej Pietrasiewicz }
3060f78bbcaeSMichal Nazarewicz DBG(common, "I/O thread pid: %d\n",
3061f78bbcaeSMichal Nazarewicz task_pid_nr(common->thread_task));
3062f78bbcaeSMichal Nazarewicz wake_up_process(common->thread_task);
3063f78bbcaeSMichal Nazarewicz }
306400a2430fSAndrzej Pietrasiewicz
306500a2430fSAndrzej Pietrasiewicz fsg->gadget = gadget;
306600a2430fSAndrzej Pietrasiewicz
306700a2430fSAndrzej Pietrasiewicz /* New interface */
306800a2430fSAndrzej Pietrasiewicz i = usb_interface_id(c, f);
306900a2430fSAndrzej Pietrasiewicz if (i < 0)
30708078f314SSanjay Singh Rawat goto fail;
307100a2430fSAndrzej Pietrasiewicz fsg_intf_desc.bInterfaceNumber = i;
307200a2430fSAndrzej Pietrasiewicz fsg->interface_number = i;
307300a2430fSAndrzej Pietrasiewicz
307400a2430fSAndrzej Pietrasiewicz /* Find all the endpoints we will use */
307500a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
307600a2430fSAndrzej Pietrasiewicz if (!ep)
307700a2430fSAndrzej Pietrasiewicz goto autoconf_fail;
307800a2430fSAndrzej Pietrasiewicz fsg->bulk_in = ep;
307900a2430fSAndrzej Pietrasiewicz
308000a2430fSAndrzej Pietrasiewicz ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
308100a2430fSAndrzej Pietrasiewicz if (!ep)
308200a2430fSAndrzej Pietrasiewicz goto autoconf_fail;
308300a2430fSAndrzej Pietrasiewicz fsg->bulk_out = ep;
308400a2430fSAndrzej Pietrasiewicz
308500a2430fSAndrzej Pietrasiewicz /* Assume endpoint addresses are the same for both speeds */
308600a2430fSAndrzej Pietrasiewicz fsg_hs_bulk_in_desc.bEndpointAddress =
308700a2430fSAndrzej Pietrasiewicz fsg_fs_bulk_in_desc.bEndpointAddress;
308800a2430fSAndrzej Pietrasiewicz fsg_hs_bulk_out_desc.bEndpointAddress =
308900a2430fSAndrzej Pietrasiewicz fsg_fs_bulk_out_desc.bEndpointAddress;
309000a2430fSAndrzej Pietrasiewicz
309100a2430fSAndrzej Pietrasiewicz /* Calculate bMaxBurst, we know packet size is 1024 */
309200a2430fSAndrzej Pietrasiewicz max_burst = min_t(unsigned, FSG_BUFLEN / 1024, 15);
309300a2430fSAndrzej Pietrasiewicz
309400a2430fSAndrzej Pietrasiewicz fsg_ss_bulk_in_desc.bEndpointAddress =
309500a2430fSAndrzej Pietrasiewicz fsg_fs_bulk_in_desc.bEndpointAddress;
309600a2430fSAndrzej Pietrasiewicz fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
309700a2430fSAndrzej Pietrasiewicz
309800a2430fSAndrzej Pietrasiewicz fsg_ss_bulk_out_desc.bEndpointAddress =
309900a2430fSAndrzej Pietrasiewicz fsg_fs_bulk_out_desc.bEndpointAddress;
310000a2430fSAndrzej Pietrasiewicz fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
310100a2430fSAndrzej Pietrasiewicz
310200a2430fSAndrzej Pietrasiewicz ret = usb_assign_descriptors(f, fsg_fs_function, fsg_hs_function,
310359a3cedfSJohn Youn fsg_ss_function, fsg_ss_function);
310400a2430fSAndrzej Pietrasiewicz if (ret)
310500a2430fSAndrzej Pietrasiewicz goto autoconf_fail;
310600a2430fSAndrzej Pietrasiewicz
310700a2430fSAndrzej Pietrasiewicz return 0;
310800a2430fSAndrzej Pietrasiewicz
310900a2430fSAndrzej Pietrasiewicz autoconf_fail:
311000a2430fSAndrzej Pietrasiewicz ERROR(fsg, "unable to autoconfigure all endpoints\n");
31118078f314SSanjay Singh Rawat i = -ENOTSUPP;
31128078f314SSanjay Singh Rawat fail:
31138078f314SSanjay Singh Rawat /* terminate the thread */
31148078f314SSanjay Singh Rawat if (fsg->common->state != FSG_STATE_TERMINATED) {
31158078f314SSanjay Singh Rawat raise_exception(fsg->common, FSG_STATE_EXIT);
31168078f314SSanjay Singh Rawat wait_for_completion(&fsg->common->thread_notifier);
31178078f314SSanjay Singh Rawat }
31188078f314SSanjay Singh Rawat return i;
311900a2430fSAndrzej Pietrasiewicz }
312000a2430fSAndrzej Pietrasiewicz
312100a2430fSAndrzej Pietrasiewicz /****************************** ALLOCATE FUNCTION *************************/
312200a2430fSAndrzej Pietrasiewicz
fsg_unbind(struct usb_configuration * c,struct usb_function * f)312300a2430fSAndrzej Pietrasiewicz static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
312400a2430fSAndrzej Pietrasiewicz {
312500a2430fSAndrzej Pietrasiewicz struct fsg_dev *fsg = fsg_from_func(f);
312600a2430fSAndrzej Pietrasiewicz struct fsg_common *common = fsg->common;
312700a2430fSAndrzej Pietrasiewicz
312800a2430fSAndrzej Pietrasiewicz DBG(fsg, "unbind\n");
312900a2430fSAndrzej Pietrasiewicz if (fsg->common->fsg == fsg) {
31304a56a478SBenjamin Herrenschmidt __raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE, NULL);
313100a2430fSAndrzej Pietrasiewicz /* FIXME: make interruptible or killable somehow? */
313200a2430fSAndrzej Pietrasiewicz wait_event(common->fsg_wait, common->fsg != fsg);
313300a2430fSAndrzej Pietrasiewicz }
313400a2430fSAndrzej Pietrasiewicz
313500a2430fSAndrzej Pietrasiewicz usb_free_all_descriptors(&fsg->function);
313600a2430fSAndrzej Pietrasiewicz }
313700a2430fSAndrzej Pietrasiewicz
to_fsg_lun_opts(struct config_item * item)313800a2430fSAndrzej Pietrasiewicz static inline struct fsg_lun_opts *to_fsg_lun_opts(struct config_item *item)
313900a2430fSAndrzej Pietrasiewicz {
314000a2430fSAndrzej Pietrasiewicz return container_of(to_config_group(item), struct fsg_lun_opts, group);
314100a2430fSAndrzej Pietrasiewicz }
314200a2430fSAndrzej Pietrasiewicz
to_fsg_opts(struct config_item * item)314300a2430fSAndrzej Pietrasiewicz static inline struct fsg_opts *to_fsg_opts(struct config_item *item)
314400a2430fSAndrzej Pietrasiewicz {
314500a2430fSAndrzej Pietrasiewicz return container_of(to_config_group(item), struct fsg_opts,
314600a2430fSAndrzej Pietrasiewicz func_inst.group);
314700a2430fSAndrzej Pietrasiewicz }
314800a2430fSAndrzej Pietrasiewicz
fsg_lun_attr_release(struct config_item * item)314900a2430fSAndrzej Pietrasiewicz static void fsg_lun_attr_release(struct config_item *item)
315000a2430fSAndrzej Pietrasiewicz {
315100a2430fSAndrzej Pietrasiewicz struct fsg_lun_opts *lun_opts;
315200a2430fSAndrzej Pietrasiewicz
315300a2430fSAndrzej Pietrasiewicz lun_opts = to_fsg_lun_opts(item);
315400a2430fSAndrzej Pietrasiewicz kfree(lun_opts);
315500a2430fSAndrzej Pietrasiewicz }
315600a2430fSAndrzej Pietrasiewicz
315700a2430fSAndrzej Pietrasiewicz static struct configfs_item_operations fsg_lun_item_ops = {
315800a2430fSAndrzej Pietrasiewicz .release = fsg_lun_attr_release,
315900a2430fSAndrzej Pietrasiewicz };
316000a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_file_show(struct config_item * item,char * page)31614a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_file_show(struct config_item *item, char *page)
316200a2430fSAndrzej Pietrasiewicz {
31634a90cb20SChristoph Hellwig struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
31644a90cb20SChristoph Hellwig struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
316500a2430fSAndrzej Pietrasiewicz
316600a2430fSAndrzej Pietrasiewicz return fsg_show_file(opts->lun, &fsg_opts->common->filesem, page);
316700a2430fSAndrzej Pietrasiewicz }
316800a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_file_store(struct config_item * item,const char * page,size_t len)31694a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_file_store(struct config_item *item,
317000a2430fSAndrzej Pietrasiewicz const char *page, size_t len)
317100a2430fSAndrzej Pietrasiewicz {
31724a90cb20SChristoph Hellwig struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
31734a90cb20SChristoph Hellwig struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
317400a2430fSAndrzej Pietrasiewicz
317500a2430fSAndrzej Pietrasiewicz return fsg_store_file(opts->lun, &fsg_opts->common->filesem, page, len);
317600a2430fSAndrzej Pietrasiewicz }
317700a2430fSAndrzej Pietrasiewicz
31784a90cb20SChristoph Hellwig CONFIGFS_ATTR(fsg_lun_opts_, file);
317900a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_ro_show(struct config_item * item,char * page)31804a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_ro_show(struct config_item *item, char *page)
318100a2430fSAndrzej Pietrasiewicz {
31824a90cb20SChristoph Hellwig return fsg_show_ro(to_fsg_lun_opts(item)->lun, page);
318300a2430fSAndrzej Pietrasiewicz }
318400a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_ro_store(struct config_item * item,const char * page,size_t len)31854a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_ro_store(struct config_item *item,
318600a2430fSAndrzej Pietrasiewicz const char *page, size_t len)
318700a2430fSAndrzej Pietrasiewicz {
31884a90cb20SChristoph Hellwig struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
31894a90cb20SChristoph Hellwig struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
319000a2430fSAndrzej Pietrasiewicz
319100a2430fSAndrzej Pietrasiewicz return fsg_store_ro(opts->lun, &fsg_opts->common->filesem, page, len);
319200a2430fSAndrzej Pietrasiewicz }
319300a2430fSAndrzej Pietrasiewicz
31944a90cb20SChristoph Hellwig CONFIGFS_ATTR(fsg_lun_opts_, ro);
319500a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_removable_show(struct config_item * item,char * page)31964a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_removable_show(struct config_item *item,
319700a2430fSAndrzej Pietrasiewicz char *page)
319800a2430fSAndrzej Pietrasiewicz {
31994a90cb20SChristoph Hellwig return fsg_show_removable(to_fsg_lun_opts(item)->lun, page);
320000a2430fSAndrzej Pietrasiewicz }
320100a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_removable_store(struct config_item * item,const char * page,size_t len)32024a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_removable_store(struct config_item *item,
320300a2430fSAndrzej Pietrasiewicz const char *page, size_t len)
320400a2430fSAndrzej Pietrasiewicz {
32054a90cb20SChristoph Hellwig return fsg_store_removable(to_fsg_lun_opts(item)->lun, page, len);
320600a2430fSAndrzej Pietrasiewicz }
320700a2430fSAndrzej Pietrasiewicz
32084a90cb20SChristoph Hellwig CONFIGFS_ATTR(fsg_lun_opts_, removable);
320900a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_cdrom_show(struct config_item * item,char * page)32104a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_cdrom_show(struct config_item *item, char *page)
321100a2430fSAndrzej Pietrasiewicz {
32124a90cb20SChristoph Hellwig return fsg_show_cdrom(to_fsg_lun_opts(item)->lun, page);
321300a2430fSAndrzej Pietrasiewicz }
321400a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_cdrom_store(struct config_item * item,const char * page,size_t len)32154a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_cdrom_store(struct config_item *item,
321600a2430fSAndrzej Pietrasiewicz const char *page, size_t len)
321700a2430fSAndrzej Pietrasiewicz {
32184a90cb20SChristoph Hellwig struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
32194a90cb20SChristoph Hellwig struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
322000a2430fSAndrzej Pietrasiewicz
322100a2430fSAndrzej Pietrasiewicz return fsg_store_cdrom(opts->lun, &fsg_opts->common->filesem, page,
322200a2430fSAndrzej Pietrasiewicz len);
322300a2430fSAndrzej Pietrasiewicz }
322400a2430fSAndrzej Pietrasiewicz
32254a90cb20SChristoph Hellwig CONFIGFS_ATTR(fsg_lun_opts_, cdrom);
322600a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_nofua_show(struct config_item * item,char * page)32274a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_nofua_show(struct config_item *item, char *page)
322800a2430fSAndrzej Pietrasiewicz {
32294a90cb20SChristoph Hellwig return fsg_show_nofua(to_fsg_lun_opts(item)->lun, page);
323000a2430fSAndrzej Pietrasiewicz }
323100a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_nofua_store(struct config_item * item,const char * page,size_t len)32324a90cb20SChristoph Hellwig static ssize_t fsg_lun_opts_nofua_store(struct config_item *item,
323300a2430fSAndrzej Pietrasiewicz const char *page, size_t len)
323400a2430fSAndrzej Pietrasiewicz {
32354a90cb20SChristoph Hellwig return fsg_store_nofua(to_fsg_lun_opts(item)->lun, page, len);
323600a2430fSAndrzej Pietrasiewicz }
323700a2430fSAndrzej Pietrasiewicz
32384a90cb20SChristoph Hellwig CONFIGFS_ATTR(fsg_lun_opts_, nofua);
323900a2430fSAndrzej Pietrasiewicz
fsg_lun_opts_inquiry_string_show(struct config_item * item,char * page)32406ac47090SPhilipp Gesang static ssize_t fsg_lun_opts_inquiry_string_show(struct config_item *item,
32416ac47090SPhilipp Gesang char *page)
32426ac47090SPhilipp Gesang {
32436ac47090SPhilipp Gesang return fsg_show_inquiry_string(to_fsg_lun_opts(item)->lun, page);
32446ac47090SPhilipp Gesang }
32456ac47090SPhilipp Gesang
fsg_lun_opts_inquiry_string_store(struct config_item * item,const char * page,size_t len)32466ac47090SPhilipp Gesang static ssize_t fsg_lun_opts_inquiry_string_store(struct config_item *item,
32476ac47090SPhilipp Gesang const char *page, size_t len)
32486ac47090SPhilipp Gesang {
32496ac47090SPhilipp Gesang return fsg_store_inquiry_string(to_fsg_lun_opts(item)->lun, page, len);
32506ac47090SPhilipp Gesang }
32516ac47090SPhilipp Gesang
32526ac47090SPhilipp Gesang CONFIGFS_ATTR(fsg_lun_opts_, inquiry_string);
32536ac47090SPhilipp Gesang
fsg_lun_opts_forced_eject_store(struct config_item * item,const char * page,size_t len)3254421c8d9aSMaxim Devaev static ssize_t fsg_lun_opts_forced_eject_store(struct config_item *item,
3255421c8d9aSMaxim Devaev const char *page, size_t len)
3256421c8d9aSMaxim Devaev {
3257421c8d9aSMaxim Devaev struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
3258421c8d9aSMaxim Devaev struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
3259421c8d9aSMaxim Devaev
3260421c8d9aSMaxim Devaev return fsg_store_forced_eject(opts->lun, &fsg_opts->common->filesem,
3261421c8d9aSMaxim Devaev page, len);
3262421c8d9aSMaxim Devaev }
3263421c8d9aSMaxim Devaev
3264421c8d9aSMaxim Devaev CONFIGFS_ATTR_WO(fsg_lun_opts_, forced_eject);
3265421c8d9aSMaxim Devaev
326600a2430fSAndrzej Pietrasiewicz static struct configfs_attribute *fsg_lun_attrs[] = {
32674a90cb20SChristoph Hellwig &fsg_lun_opts_attr_file,
32684a90cb20SChristoph Hellwig &fsg_lun_opts_attr_ro,
32694a90cb20SChristoph Hellwig &fsg_lun_opts_attr_removable,
32704a90cb20SChristoph Hellwig &fsg_lun_opts_attr_cdrom,
32714a90cb20SChristoph Hellwig &fsg_lun_opts_attr_nofua,
32726ac47090SPhilipp Gesang &fsg_lun_opts_attr_inquiry_string,
3273421c8d9aSMaxim Devaev &fsg_lun_opts_attr_forced_eject,
327400a2430fSAndrzej Pietrasiewicz NULL,
327500a2430fSAndrzej Pietrasiewicz };
327600a2430fSAndrzej Pietrasiewicz
327797363902SBhumika Goyal static const struct config_item_type fsg_lun_type = {
327800a2430fSAndrzej Pietrasiewicz .ct_item_ops = &fsg_lun_item_ops,
327900a2430fSAndrzej Pietrasiewicz .ct_attrs = fsg_lun_attrs,
328000a2430fSAndrzej Pietrasiewicz .ct_owner = THIS_MODULE,
328100a2430fSAndrzej Pietrasiewicz };
328200a2430fSAndrzej Pietrasiewicz
fsg_lun_make(struct config_group * group,const char * name)328300a2430fSAndrzej Pietrasiewicz static struct config_group *fsg_lun_make(struct config_group *group,
328400a2430fSAndrzej Pietrasiewicz const char *name)
328500a2430fSAndrzej Pietrasiewicz {
328600a2430fSAndrzej Pietrasiewicz struct fsg_lun_opts *opts;
328700a2430fSAndrzej Pietrasiewicz struct fsg_opts *fsg_opts;
328800a2430fSAndrzej Pietrasiewicz struct fsg_lun_config config;
328900a2430fSAndrzej Pietrasiewicz char *num_str;
329000a2430fSAndrzej Pietrasiewicz u8 num;
329100a2430fSAndrzej Pietrasiewicz int ret;
329200a2430fSAndrzej Pietrasiewicz
329300a2430fSAndrzej Pietrasiewicz num_str = strchr(name, '.');
329400a2430fSAndrzej Pietrasiewicz if (!num_str) {
329500a2430fSAndrzej Pietrasiewicz pr_err("Unable to locate . in LUN.NUMBER\n");
329600a2430fSAndrzej Pietrasiewicz return ERR_PTR(-EINVAL);
329700a2430fSAndrzej Pietrasiewicz }
329800a2430fSAndrzej Pietrasiewicz num_str++;
329900a2430fSAndrzej Pietrasiewicz
330000a2430fSAndrzej Pietrasiewicz ret = kstrtou8(num_str, 0, &num);
330100a2430fSAndrzej Pietrasiewicz if (ret)
330200a2430fSAndrzej Pietrasiewicz return ERR_PTR(ret);
330300a2430fSAndrzej Pietrasiewicz
330400a2430fSAndrzej Pietrasiewicz fsg_opts = to_fsg_opts(&group->cg_item);
330500a2430fSAndrzej Pietrasiewicz if (num >= FSG_MAX_LUNS)
330600a2430fSAndrzej Pietrasiewicz return ERR_PTR(-ERANGE);
33079ae24af3SGustavo A. R. Silva num = array_index_nospec(num, FSG_MAX_LUNS);
330800a2430fSAndrzej Pietrasiewicz
330900a2430fSAndrzej Pietrasiewicz mutex_lock(&fsg_opts->lock);
331000a2430fSAndrzej Pietrasiewicz if (fsg_opts->refcnt || fsg_opts->common->luns[num]) {
331100a2430fSAndrzej Pietrasiewicz ret = -EBUSY;
331200a2430fSAndrzej Pietrasiewicz goto out;
331300a2430fSAndrzej Pietrasiewicz }
331400a2430fSAndrzej Pietrasiewicz
331500a2430fSAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL);
331600a2430fSAndrzej Pietrasiewicz if (!opts) {
331700a2430fSAndrzej Pietrasiewicz ret = -ENOMEM;
331800a2430fSAndrzej Pietrasiewicz goto out;
331900a2430fSAndrzej Pietrasiewicz }
332000a2430fSAndrzej Pietrasiewicz
332100a2430fSAndrzej Pietrasiewicz memset(&config, 0, sizeof(config));
332200a2430fSAndrzej Pietrasiewicz config.removable = true;
332300a2430fSAndrzej Pietrasiewicz
332400a2430fSAndrzej Pietrasiewicz ret = fsg_common_create_lun(fsg_opts->common, &config, num, name,
332500a2430fSAndrzej Pietrasiewicz (const char **)&group->cg_item.ci_name);
332600a2430fSAndrzej Pietrasiewicz if (ret) {
332700a2430fSAndrzej Pietrasiewicz kfree(opts);
332800a2430fSAndrzej Pietrasiewicz goto out;
332900a2430fSAndrzej Pietrasiewicz }
333000a2430fSAndrzej Pietrasiewicz opts->lun = fsg_opts->common->luns[num];
333100a2430fSAndrzej Pietrasiewicz opts->lun_id = num;
333200a2430fSAndrzej Pietrasiewicz mutex_unlock(&fsg_opts->lock);
333300a2430fSAndrzej Pietrasiewicz
333400a2430fSAndrzej Pietrasiewicz config_group_init_type_name(&opts->group, name, &fsg_lun_type);
333500a2430fSAndrzej Pietrasiewicz
333600a2430fSAndrzej Pietrasiewicz return &opts->group;
333700a2430fSAndrzej Pietrasiewicz out:
333800a2430fSAndrzej Pietrasiewicz mutex_unlock(&fsg_opts->lock);
333900a2430fSAndrzej Pietrasiewicz return ERR_PTR(ret);
334000a2430fSAndrzej Pietrasiewicz }
334100a2430fSAndrzej Pietrasiewicz
fsg_lun_drop(struct config_group * group,struct config_item * item)334200a2430fSAndrzej Pietrasiewicz static void fsg_lun_drop(struct config_group *group, struct config_item *item)
334300a2430fSAndrzej Pietrasiewicz {
334400a2430fSAndrzej Pietrasiewicz struct fsg_lun_opts *lun_opts;
334500a2430fSAndrzej Pietrasiewicz struct fsg_opts *fsg_opts;
334600a2430fSAndrzej Pietrasiewicz
334700a2430fSAndrzej Pietrasiewicz lun_opts = to_fsg_lun_opts(item);
334800a2430fSAndrzej Pietrasiewicz fsg_opts = to_fsg_opts(&group->cg_item);
334900a2430fSAndrzej Pietrasiewicz
335000a2430fSAndrzej Pietrasiewicz mutex_lock(&fsg_opts->lock);
335100a2430fSAndrzej Pietrasiewicz if (fsg_opts->refcnt) {
335200a2430fSAndrzej Pietrasiewicz struct config_item *gadget;
335300a2430fSAndrzej Pietrasiewicz
335400a2430fSAndrzej Pietrasiewicz gadget = group->cg_item.ci_parent->ci_parent;
335500a2430fSAndrzej Pietrasiewicz unregister_gadget_item(gadget);
335600a2430fSAndrzej Pietrasiewicz }
335700a2430fSAndrzej Pietrasiewicz
33585542f58cSKrzysztof Opasiak fsg_common_remove_lun(lun_opts->lun);
335900a2430fSAndrzej Pietrasiewicz fsg_opts->common->luns[lun_opts->lun_id] = NULL;
336000a2430fSAndrzej Pietrasiewicz lun_opts->lun_id = 0;
336100a2430fSAndrzej Pietrasiewicz mutex_unlock(&fsg_opts->lock);
336200a2430fSAndrzej Pietrasiewicz
336300a2430fSAndrzej Pietrasiewicz config_item_put(item);
336400a2430fSAndrzej Pietrasiewicz }
336500a2430fSAndrzej Pietrasiewicz
fsg_attr_release(struct config_item * item)336600a2430fSAndrzej Pietrasiewicz static void fsg_attr_release(struct config_item *item)
336700a2430fSAndrzej Pietrasiewicz {
336800a2430fSAndrzej Pietrasiewicz struct fsg_opts *opts = to_fsg_opts(item);
336900a2430fSAndrzej Pietrasiewicz
337000a2430fSAndrzej Pietrasiewicz usb_put_function_instance(&opts->func_inst);
337100a2430fSAndrzej Pietrasiewicz }
337200a2430fSAndrzej Pietrasiewicz
337300a2430fSAndrzej Pietrasiewicz static struct configfs_item_operations fsg_item_ops = {
337400a2430fSAndrzej Pietrasiewicz .release = fsg_attr_release,
337500a2430fSAndrzej Pietrasiewicz };
337600a2430fSAndrzej Pietrasiewicz
fsg_opts_stall_show(struct config_item * item,char * page)33774a90cb20SChristoph Hellwig static ssize_t fsg_opts_stall_show(struct config_item *item, char *page)
337800a2430fSAndrzej Pietrasiewicz {
33794a90cb20SChristoph Hellwig struct fsg_opts *opts = to_fsg_opts(item);
338000a2430fSAndrzej Pietrasiewicz int result;
338100a2430fSAndrzej Pietrasiewicz
338200a2430fSAndrzej Pietrasiewicz mutex_lock(&opts->lock);
338300a2430fSAndrzej Pietrasiewicz result = sprintf(page, "%d", opts->common->can_stall);
338400a2430fSAndrzej Pietrasiewicz mutex_unlock(&opts->lock);
338500a2430fSAndrzej Pietrasiewicz
338600a2430fSAndrzej Pietrasiewicz return result;
338700a2430fSAndrzej Pietrasiewicz }
338800a2430fSAndrzej Pietrasiewicz
fsg_opts_stall_store(struct config_item * item,const char * page,size_t len)33894a90cb20SChristoph Hellwig static ssize_t fsg_opts_stall_store(struct config_item *item, const char *page,
339000a2430fSAndrzej Pietrasiewicz size_t len)
339100a2430fSAndrzej Pietrasiewicz {
33924a90cb20SChristoph Hellwig struct fsg_opts *opts = to_fsg_opts(item);
339300a2430fSAndrzej Pietrasiewicz int ret;
339400a2430fSAndrzej Pietrasiewicz bool stall;
339500a2430fSAndrzej Pietrasiewicz
339600a2430fSAndrzej Pietrasiewicz mutex_lock(&opts->lock);
339700a2430fSAndrzej Pietrasiewicz
339800a2430fSAndrzej Pietrasiewicz if (opts->refcnt) {
339900a2430fSAndrzej Pietrasiewicz mutex_unlock(&opts->lock);
340000a2430fSAndrzej Pietrasiewicz return -EBUSY;
340100a2430fSAndrzej Pietrasiewicz }
340200a2430fSAndrzej Pietrasiewicz
3403a8bc8cc1SChristophe JAILLET ret = kstrtobool(page, &stall);
340400a2430fSAndrzej Pietrasiewicz if (!ret) {
340500a2430fSAndrzej Pietrasiewicz opts->common->can_stall = stall;
340600a2430fSAndrzej Pietrasiewicz ret = len;
340700a2430fSAndrzej Pietrasiewicz }
340800a2430fSAndrzej Pietrasiewicz
340900a2430fSAndrzej Pietrasiewicz mutex_unlock(&opts->lock);
341000a2430fSAndrzej Pietrasiewicz
341100a2430fSAndrzej Pietrasiewicz return ret;
341200a2430fSAndrzej Pietrasiewicz }
341300a2430fSAndrzej Pietrasiewicz
34144a90cb20SChristoph Hellwig CONFIGFS_ATTR(fsg_opts_, stall);
341500a2430fSAndrzej Pietrasiewicz
341600a2430fSAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES
fsg_opts_num_buffers_show(struct config_item * item,char * page)34174a90cb20SChristoph Hellwig static ssize_t fsg_opts_num_buffers_show(struct config_item *item, char *page)
341800a2430fSAndrzej Pietrasiewicz {
34194a90cb20SChristoph Hellwig struct fsg_opts *opts = to_fsg_opts(item);
342000a2430fSAndrzej Pietrasiewicz int result;
342100a2430fSAndrzej Pietrasiewicz
342200a2430fSAndrzej Pietrasiewicz mutex_lock(&opts->lock);
342300a2430fSAndrzej Pietrasiewicz result = sprintf(page, "%d", opts->common->fsg_num_buffers);
342400a2430fSAndrzej Pietrasiewicz mutex_unlock(&opts->lock);
342500a2430fSAndrzej Pietrasiewicz
342600a2430fSAndrzej Pietrasiewicz return result;
342700a2430fSAndrzej Pietrasiewicz }
342800a2430fSAndrzej Pietrasiewicz
fsg_opts_num_buffers_store(struct config_item * item,const char * page,size_t len)34294a90cb20SChristoph Hellwig static ssize_t fsg_opts_num_buffers_store(struct config_item *item,
343000a2430fSAndrzej Pietrasiewicz const char *page, size_t len)
343100a2430fSAndrzej Pietrasiewicz {
34324a90cb20SChristoph Hellwig struct fsg_opts *opts = to_fsg_opts(item);
343300a2430fSAndrzej Pietrasiewicz int ret;
343400a2430fSAndrzej Pietrasiewicz u8 num;
343500a2430fSAndrzej Pietrasiewicz
343600a2430fSAndrzej Pietrasiewicz mutex_lock(&opts->lock);
343700a2430fSAndrzej Pietrasiewicz if (opts->refcnt) {
343800a2430fSAndrzej Pietrasiewicz ret = -EBUSY;
343900a2430fSAndrzej Pietrasiewicz goto end;
344000a2430fSAndrzej Pietrasiewicz }
344100a2430fSAndrzej Pietrasiewicz ret = kstrtou8(page, 0, &num);
344200a2430fSAndrzej Pietrasiewicz if (ret)
344300a2430fSAndrzej Pietrasiewicz goto end;
344400a2430fSAndrzej Pietrasiewicz
34457a051e8dSJaejoong Kim ret = fsg_common_set_num_buffers(opts->common, num);
34467a051e8dSJaejoong Kim if (ret)
34477a051e8dSJaejoong Kim goto end;
344800a2430fSAndrzej Pietrasiewicz ret = len;
344900a2430fSAndrzej Pietrasiewicz
345000a2430fSAndrzej Pietrasiewicz end:
345100a2430fSAndrzej Pietrasiewicz mutex_unlock(&opts->lock);
345200a2430fSAndrzej Pietrasiewicz return ret;
345300a2430fSAndrzej Pietrasiewicz }
345400a2430fSAndrzej Pietrasiewicz
34554a90cb20SChristoph Hellwig CONFIGFS_ATTR(fsg_opts_, num_buffers);
345600a2430fSAndrzej Pietrasiewicz #endif
345700a2430fSAndrzej Pietrasiewicz
345800a2430fSAndrzej Pietrasiewicz static struct configfs_attribute *fsg_attrs[] = {
34594a90cb20SChristoph Hellwig &fsg_opts_attr_stall,
346000a2430fSAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES
34614a90cb20SChristoph Hellwig &fsg_opts_attr_num_buffers,
346200a2430fSAndrzej Pietrasiewicz #endif
346300a2430fSAndrzej Pietrasiewicz NULL,
346400a2430fSAndrzej Pietrasiewicz };
346500a2430fSAndrzej Pietrasiewicz
346600a2430fSAndrzej Pietrasiewicz static struct configfs_group_operations fsg_group_ops = {
346700a2430fSAndrzej Pietrasiewicz .make_group = fsg_lun_make,
346800a2430fSAndrzej Pietrasiewicz .drop_item = fsg_lun_drop,
346900a2430fSAndrzej Pietrasiewicz };
347000a2430fSAndrzej Pietrasiewicz
347197363902SBhumika Goyal static const struct config_item_type fsg_func_type = {
347200a2430fSAndrzej Pietrasiewicz .ct_item_ops = &fsg_item_ops,
347300a2430fSAndrzej Pietrasiewicz .ct_group_ops = &fsg_group_ops,
347400a2430fSAndrzej Pietrasiewicz .ct_attrs = fsg_attrs,
347500a2430fSAndrzej Pietrasiewicz .ct_owner = THIS_MODULE,
347600a2430fSAndrzej Pietrasiewicz };
347700a2430fSAndrzej Pietrasiewicz
fsg_free_inst(struct usb_function_instance * fi)347800a2430fSAndrzej Pietrasiewicz static void fsg_free_inst(struct usb_function_instance *fi)
347900a2430fSAndrzej Pietrasiewicz {
348000a2430fSAndrzej Pietrasiewicz struct fsg_opts *opts;
348100a2430fSAndrzej Pietrasiewicz
348200a2430fSAndrzej Pietrasiewicz opts = fsg_opts_from_func_inst(fi);
34831fcba97eSJaejoong Kim fsg_common_release(opts->common);
348400a2430fSAndrzej Pietrasiewicz kfree(opts);
348500a2430fSAndrzej Pietrasiewicz }
348600a2430fSAndrzej Pietrasiewicz
fsg_alloc_inst(void)348700a2430fSAndrzej Pietrasiewicz static struct usb_function_instance *fsg_alloc_inst(void)
348800a2430fSAndrzej Pietrasiewicz {
348900a2430fSAndrzej Pietrasiewicz struct fsg_opts *opts;
349000a2430fSAndrzej Pietrasiewicz struct fsg_lun_config config;
349100a2430fSAndrzej Pietrasiewicz int rc;
349200a2430fSAndrzej Pietrasiewicz
349300a2430fSAndrzej Pietrasiewicz opts = kzalloc(sizeof(*opts), GFP_KERNEL);
349400a2430fSAndrzej Pietrasiewicz if (!opts)
349500a2430fSAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM);
349600a2430fSAndrzej Pietrasiewicz mutex_init(&opts->lock);
349700a2430fSAndrzej Pietrasiewicz opts->func_inst.free_func_inst = fsg_free_inst;
349800a2430fSAndrzej Pietrasiewicz opts->common = fsg_common_setup(opts->common);
349900a2430fSAndrzej Pietrasiewicz if (IS_ERR(opts->common)) {
350000a2430fSAndrzej Pietrasiewicz rc = PTR_ERR(opts->common);
350100a2430fSAndrzej Pietrasiewicz goto release_opts;
350200a2430fSAndrzej Pietrasiewicz }
350300a2430fSAndrzej Pietrasiewicz
350400a2430fSAndrzej Pietrasiewicz rc = fsg_common_set_num_buffers(opts->common,
350500a2430fSAndrzej Pietrasiewicz CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS);
350600a2430fSAndrzej Pietrasiewicz if (rc)
35071fcba97eSJaejoong Kim goto release_common;
350800a2430fSAndrzej Pietrasiewicz
350900a2430fSAndrzej Pietrasiewicz pr_info(FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
351000a2430fSAndrzej Pietrasiewicz
351100a2430fSAndrzej Pietrasiewicz memset(&config, 0, sizeof(config));
351200a2430fSAndrzej Pietrasiewicz config.removable = true;
351300a2430fSAndrzej Pietrasiewicz rc = fsg_common_create_lun(opts->common, &config, 0, "lun.0",
351400a2430fSAndrzej Pietrasiewicz (const char **)&opts->func_inst.group.cg_item.ci_name);
3515903588a9SKrzysztof Opasiak if (rc)
3516903588a9SKrzysztof Opasiak goto release_buffers;
3517903588a9SKrzysztof Opasiak
351800a2430fSAndrzej Pietrasiewicz opts->lun0.lun = opts->common->luns[0];
351900a2430fSAndrzej Pietrasiewicz opts->lun0.lun_id = 0;
352000a2430fSAndrzej Pietrasiewicz
352100a2430fSAndrzej Pietrasiewicz config_group_init_type_name(&opts->func_inst.group, "", &fsg_func_type);
352200a2430fSAndrzej Pietrasiewicz
35231ae1602dSChristoph Hellwig config_group_init_type_name(&opts->lun0.group, "lun.0", &fsg_lun_type);
35241ae1602dSChristoph Hellwig configfs_add_default_group(&opts->lun0.group, &opts->func_inst.group);
35251ae1602dSChristoph Hellwig
352600a2430fSAndrzej Pietrasiewicz return &opts->func_inst;
352700a2430fSAndrzej Pietrasiewicz
3528903588a9SKrzysztof Opasiak release_buffers:
3529903588a9SKrzysztof Opasiak fsg_common_free_buffers(opts->common);
35301fcba97eSJaejoong Kim release_common:
35311fcba97eSJaejoong Kim kfree(opts->common);
353200a2430fSAndrzej Pietrasiewicz release_opts:
353300a2430fSAndrzej Pietrasiewicz kfree(opts);
353400a2430fSAndrzej Pietrasiewicz return ERR_PTR(rc);
353500a2430fSAndrzej Pietrasiewicz }
353600a2430fSAndrzej Pietrasiewicz
fsg_free(struct usb_function * f)353700a2430fSAndrzej Pietrasiewicz static void fsg_free(struct usb_function *f)
353800a2430fSAndrzej Pietrasiewicz {
353900a2430fSAndrzej Pietrasiewicz struct fsg_dev *fsg;
354000a2430fSAndrzej Pietrasiewicz struct fsg_opts *opts;
354100a2430fSAndrzej Pietrasiewicz
354200a2430fSAndrzej Pietrasiewicz fsg = container_of(f, struct fsg_dev, function);
354300a2430fSAndrzej Pietrasiewicz opts = container_of(f->fi, struct fsg_opts, func_inst);
354400a2430fSAndrzej Pietrasiewicz
354500a2430fSAndrzej Pietrasiewicz mutex_lock(&opts->lock);
354600a2430fSAndrzej Pietrasiewicz opts->refcnt--;
354700a2430fSAndrzej Pietrasiewicz mutex_unlock(&opts->lock);
354800a2430fSAndrzej Pietrasiewicz
354900a2430fSAndrzej Pietrasiewicz kfree(fsg);
355000a2430fSAndrzej Pietrasiewicz }
355100a2430fSAndrzej Pietrasiewicz
fsg_alloc(struct usb_function_instance * fi)355200a2430fSAndrzej Pietrasiewicz static struct usb_function *fsg_alloc(struct usb_function_instance *fi)
355300a2430fSAndrzej Pietrasiewicz {
355400a2430fSAndrzej Pietrasiewicz struct fsg_opts *opts = fsg_opts_from_func_inst(fi);
355500a2430fSAndrzej Pietrasiewicz struct fsg_common *common = opts->common;
355600a2430fSAndrzej Pietrasiewicz struct fsg_dev *fsg;
355700a2430fSAndrzej Pietrasiewicz
355800a2430fSAndrzej Pietrasiewicz fsg = kzalloc(sizeof(*fsg), GFP_KERNEL);
355900a2430fSAndrzej Pietrasiewicz if (unlikely(!fsg))
356000a2430fSAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM);
356100a2430fSAndrzej Pietrasiewicz
356200a2430fSAndrzej Pietrasiewicz mutex_lock(&opts->lock);
356300a2430fSAndrzej Pietrasiewicz opts->refcnt++;
356400a2430fSAndrzej Pietrasiewicz mutex_unlock(&opts->lock);
35658515bac0SMichal Nazarewicz
356600a2430fSAndrzej Pietrasiewicz fsg->function.name = FSG_DRIVER_DESC;
356700a2430fSAndrzej Pietrasiewicz fsg->function.bind = fsg_bind;
356800a2430fSAndrzej Pietrasiewicz fsg->function.unbind = fsg_unbind;
356900a2430fSAndrzej Pietrasiewicz fsg->function.setup = fsg_setup;
357000a2430fSAndrzej Pietrasiewicz fsg->function.set_alt = fsg_set_alt;
357100a2430fSAndrzej Pietrasiewicz fsg->function.disable = fsg_disable;
357200a2430fSAndrzej Pietrasiewicz fsg->function.free_func = fsg_free;
357300a2430fSAndrzej Pietrasiewicz
357400a2430fSAndrzej Pietrasiewicz fsg->common = common;
357500a2430fSAndrzej Pietrasiewicz
357600a2430fSAndrzej Pietrasiewicz return &fsg->function;
357700a2430fSAndrzej Pietrasiewicz }
357800a2430fSAndrzej Pietrasiewicz
357900a2430fSAndrzej Pietrasiewicz DECLARE_USB_FUNCTION_INIT(mass_storage, fsg_alloc_inst, fsg_alloc);
358000a2430fSAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
358100a2430fSAndrzej Pietrasiewicz MODULE_AUTHOR("Michal Nazarewicz");
358200a2430fSAndrzej Pietrasiewicz
358300a2430fSAndrzej Pietrasiewicz /************************* Module parameters *************************/
358400a2430fSAndrzej Pietrasiewicz
358500a2430fSAndrzej Pietrasiewicz
fsg_config_from_params(struct fsg_config * cfg,const struct fsg_module_parameters * params,unsigned int fsg_num_buffers)358600a2430fSAndrzej Pietrasiewicz void fsg_config_from_params(struct fsg_config *cfg,
358700a2430fSAndrzej Pietrasiewicz const struct fsg_module_parameters *params,
358800a2430fSAndrzej Pietrasiewicz unsigned int fsg_num_buffers)
358900a2430fSAndrzej Pietrasiewicz {
359000a2430fSAndrzej Pietrasiewicz struct fsg_lun_config *lun;
359100a2430fSAndrzej Pietrasiewicz unsigned i;
359200a2430fSAndrzej Pietrasiewicz
359300a2430fSAndrzej Pietrasiewicz /* Configure LUNs */
359400a2430fSAndrzej Pietrasiewicz cfg->nluns =
359500a2430fSAndrzej Pietrasiewicz min(params->luns ?: (params->file_count ?: 1u),
359600a2430fSAndrzej Pietrasiewicz (unsigned)FSG_MAX_LUNS);
359700a2430fSAndrzej Pietrasiewicz for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) {
359800a2430fSAndrzej Pietrasiewicz lun->ro = !!params->ro[i];
359900a2430fSAndrzej Pietrasiewicz lun->cdrom = !!params->cdrom[i];
360000a2430fSAndrzej Pietrasiewicz lun->removable = !!params->removable[i];
360100a2430fSAndrzej Pietrasiewicz lun->filename =
360200a2430fSAndrzej Pietrasiewicz params->file_count > i && params->file[i][0]
360300a2430fSAndrzej Pietrasiewicz ? params->file[i]
360400a2430fSAndrzej Pietrasiewicz : NULL;
360500a2430fSAndrzej Pietrasiewicz }
360600a2430fSAndrzej Pietrasiewicz
360700a2430fSAndrzej Pietrasiewicz /* Let MSF use defaults */
360800a2430fSAndrzej Pietrasiewicz cfg->vendor_name = NULL;
360900a2430fSAndrzej Pietrasiewicz cfg->product_name = NULL;
361000a2430fSAndrzej Pietrasiewicz
361100a2430fSAndrzej Pietrasiewicz cfg->ops = NULL;
361200a2430fSAndrzej Pietrasiewicz cfg->private_data = NULL;
361300a2430fSAndrzej Pietrasiewicz
361400a2430fSAndrzej Pietrasiewicz /* Finalise */
361500a2430fSAndrzej Pietrasiewicz cfg->can_stall = params->stall;
361600a2430fSAndrzej Pietrasiewicz cfg->fsg_num_buffers = fsg_num_buffers;
361700a2430fSAndrzej Pietrasiewicz }
361800a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(fsg_config_from_params);
3619