14549e789STom Rini // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2b4d36f68SPiotr Wilczek /*
3b4d36f68SPiotr Wilczek * f_mass_storage.c -- Mass Storage USB Composite Function
4b4d36f68SPiotr Wilczek *
5b4d36f68SPiotr Wilczek * Copyright (C) 2003-2008 Alan Stern
6b4d36f68SPiotr Wilczek * Copyright (C) 2009 Samsung Electronics
7b4d36f68SPiotr Wilczek * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
8b4d36f68SPiotr Wilczek * All rights reserved.
9b4d36f68SPiotr Wilczek */
10b4d36f68SPiotr Wilczek
11b4d36f68SPiotr Wilczek /*
12b4d36f68SPiotr Wilczek * The Mass Storage Function acts as a USB Mass Storage device,
13b4d36f68SPiotr Wilczek * appearing to the host as a disk drive or as a CD-ROM drive. In
14b4d36f68SPiotr Wilczek * addition to providing an example of a genuinely useful composite
15b4d36f68SPiotr Wilczek * function for a USB device, it also illustrates a technique of
16b4d36f68SPiotr Wilczek * double-buffering for increased throughput.
17b4d36f68SPiotr Wilczek *
18b4d36f68SPiotr Wilczek * Function supports multiple logical units (LUNs). Backing storage
19b4d36f68SPiotr Wilczek * for each LUN is provided by a regular file or a block device.
20b4d36f68SPiotr Wilczek * Access for each LUN can be limited to read-only. Moreover, the
21b4d36f68SPiotr Wilczek * function can indicate that LUN is removable and/or CD-ROM. (The
22b4d36f68SPiotr Wilczek * later implies read-only access.)
23b4d36f68SPiotr Wilczek *
24b4d36f68SPiotr Wilczek * MSF is configured by specifying a fsg_config structure. It has the
25b4d36f68SPiotr Wilczek * following fields:
26b4d36f68SPiotr Wilczek *
27b4d36f68SPiotr Wilczek * nluns Number of LUNs function have (anywhere from 1
28b4d36f68SPiotr Wilczek * to FSG_MAX_LUNS which is 8).
29b4d36f68SPiotr Wilczek * luns An array of LUN configuration values. This
30b4d36f68SPiotr Wilczek * should be filled for each LUN that
31b4d36f68SPiotr Wilczek * function will include (ie. for "nluns"
32b4d36f68SPiotr Wilczek * LUNs). Each element of the array has
33b4d36f68SPiotr Wilczek * the following fields:
34b4d36f68SPiotr Wilczek * ->filename The path to the backing file for the LUN.
35b4d36f68SPiotr Wilczek * Required if LUN is not marked as
36b4d36f68SPiotr Wilczek * removable.
37b4d36f68SPiotr Wilczek * ->ro Flag specifying access to the LUN shall be
38b4d36f68SPiotr Wilczek * read-only. This is implied if CD-ROM
39b4d36f68SPiotr Wilczek * emulation is enabled as well as when
40b4d36f68SPiotr Wilczek * it was impossible to open "filename"
41b4d36f68SPiotr Wilczek * in R/W mode.
42b4d36f68SPiotr Wilczek * ->removable Flag specifying that LUN shall be indicated as
43b4d36f68SPiotr Wilczek * being removable.
44b4d36f68SPiotr Wilczek * ->cdrom Flag specifying that LUN shall be reported as
45b4d36f68SPiotr Wilczek * being a CD-ROM.
46b4d36f68SPiotr Wilczek *
47b4d36f68SPiotr Wilczek * lun_name_format A printf-like format for names of the LUN
48b4d36f68SPiotr Wilczek * devices. This determines how the
49b4d36f68SPiotr Wilczek * directory in sysfs will be named.
50b4d36f68SPiotr Wilczek * Unless you are using several MSFs in
51b4d36f68SPiotr Wilczek * a single gadget (as opposed to single
52b4d36f68SPiotr Wilczek * MSF in many configurations) you may
53b4d36f68SPiotr Wilczek * leave it as NULL (in which case
54b4d36f68SPiotr Wilczek * "lun%d" will be used). In the format
55b4d36f68SPiotr Wilczek * you can use "%d" to index LUNs for
56b4d36f68SPiotr Wilczek * MSF's with more than one LUN. (Beware
57b4d36f68SPiotr Wilczek * that there is only one integer given
58b4d36f68SPiotr Wilczek * as an argument for the format and
59b4d36f68SPiotr Wilczek * specifying invalid format may cause
60b4d36f68SPiotr Wilczek * unspecified behaviour.)
61b4d36f68SPiotr Wilczek * thread_name Name of the kernel thread process used by the
62b4d36f68SPiotr Wilczek * MSF. You can safely set it to NULL
63b4d36f68SPiotr Wilczek * (in which case default "file-storage"
64b4d36f68SPiotr Wilczek * will be used).
65b4d36f68SPiotr Wilczek *
66b4d36f68SPiotr Wilczek * vendor_name
67b4d36f68SPiotr Wilczek * product_name
68b4d36f68SPiotr Wilczek * release Information used as a reply to INQUIRY
69b4d36f68SPiotr Wilczek * request. To use default set to NULL,
70b4d36f68SPiotr Wilczek * NULL, 0xffff respectively. The first
71b4d36f68SPiotr Wilczek * field should be 8 and the second 16
72b4d36f68SPiotr Wilczek * characters or less.
73b4d36f68SPiotr Wilczek *
74b4d36f68SPiotr Wilczek * can_stall Set to permit function to halt bulk endpoints.
75b4d36f68SPiotr Wilczek * Disabled on some USB devices known not
76b4d36f68SPiotr Wilczek * to work correctly. You should set it
77b4d36f68SPiotr Wilczek * to true.
78b4d36f68SPiotr Wilczek *
79b4d36f68SPiotr Wilczek * If "removable" is not set for a LUN then a backing file must be
80b4d36f68SPiotr Wilczek * specified. If it is set, then NULL filename means the LUN's medium
81b4d36f68SPiotr Wilczek * is not loaded (an empty string as "filename" in the fsg_config
82b4d36f68SPiotr Wilczek * structure causes error). The CD-ROM emulation includes a single
83b4d36f68SPiotr Wilczek * data track and no audio tracks; hence there need be only one
84b4d36f68SPiotr Wilczek * backing file per LUN. Note also that the CD-ROM block length is
85b4d36f68SPiotr Wilczek * set to 512 rather than the more common value 2048.
86b4d36f68SPiotr Wilczek *
87b4d36f68SPiotr Wilczek *
88b4d36f68SPiotr Wilczek * MSF includes support for module parameters. If gadget using it
89b4d36f68SPiotr Wilczek * decides to use it, the following module parameters will be
90b4d36f68SPiotr Wilczek * available:
91b4d36f68SPiotr Wilczek *
92b4d36f68SPiotr Wilczek * file=filename[,filename...]
93b4d36f68SPiotr Wilczek * Names of the files or block devices used for
94b4d36f68SPiotr Wilczek * backing storage.
95b4d36f68SPiotr Wilczek * ro=b[,b...] Default false, boolean for read-only access.
96b4d36f68SPiotr Wilczek * removable=b[,b...]
97b4d36f68SPiotr Wilczek * Default true, boolean for removable media.
98b4d36f68SPiotr Wilczek * cdrom=b[,b...] Default false, boolean for whether to emulate
99b4d36f68SPiotr Wilczek * a CD-ROM drive.
100b4d36f68SPiotr Wilczek * luns=N Default N = number of filenames, number of
101b4d36f68SPiotr Wilczek * LUNs to support.
102b4d36f68SPiotr Wilczek * stall Default determined according to the type of
103b4d36f68SPiotr Wilczek * USB device controller (usually true),
104b4d36f68SPiotr Wilczek * boolean to permit the driver to halt
105b4d36f68SPiotr Wilczek * bulk endpoints.
106b4d36f68SPiotr Wilczek *
107b4d36f68SPiotr Wilczek * The module parameters may be prefixed with some string. You need
108b4d36f68SPiotr Wilczek * to consult gadget's documentation or source to verify whether it is
109b4d36f68SPiotr Wilczek * using those module parameters and if it does what are the prefixes
110b4d36f68SPiotr Wilczek * (look for FSG_MODULE_PARAMETERS() macro usage, what's inside it is
111b4d36f68SPiotr Wilczek * the prefix).
112b4d36f68SPiotr Wilczek *
113b4d36f68SPiotr Wilczek *
114b4d36f68SPiotr Wilczek * Requirements are modest; only a bulk-in and a bulk-out endpoint are
115b4d36f68SPiotr Wilczek * needed. The memory requirement amounts to two 16K buffers, size
116b4d36f68SPiotr Wilczek * configurable by a parameter. Support is included for both
117b4d36f68SPiotr Wilczek * full-speed and high-speed operation.
118b4d36f68SPiotr Wilczek *
119b4d36f68SPiotr Wilczek * Note that the driver is slightly non-portable in that it assumes a
120b4d36f68SPiotr Wilczek * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
121b4d36f68SPiotr Wilczek * interrupt-in endpoints. With most device controllers this isn't an
122b4d36f68SPiotr Wilczek * issue, but there may be some with hardware restrictions that prevent
123b4d36f68SPiotr Wilczek * a buffer from being used by more than one endpoint.
124b4d36f68SPiotr Wilczek *
125b4d36f68SPiotr Wilczek *
126b4d36f68SPiotr Wilczek * The pathnames of the backing files and the ro settings are
127b4d36f68SPiotr Wilczek * available in the attribute files "file" and "ro" in the lun<n> (or
128b4d36f68SPiotr Wilczek * to be more precise in a directory which name comes from
129b4d36f68SPiotr Wilczek * "lun_name_format" option!) subdirectory of the gadget's sysfs
130b4d36f68SPiotr Wilczek * directory. If the "removable" option is set, writing to these
131b4d36f68SPiotr Wilczek * files will simulate ejecting/loading the medium (writing an empty
132b4d36f68SPiotr Wilczek * line means eject) and adjusting a write-enable tab. Changes to the
133b4d36f68SPiotr Wilczek * ro setting are not allowed when the medium is loaded or if CD-ROM
134b4d36f68SPiotr Wilczek * emulation is being used.
135b4d36f68SPiotr Wilczek *
136b4d36f68SPiotr Wilczek * When a LUN receive an "eject" SCSI request (Start/Stop Unit),
137b4d36f68SPiotr Wilczek * if the LUN is removable, the backing file is released to simulate
138b4d36f68SPiotr Wilczek * ejection.
139b4d36f68SPiotr Wilczek *
140b4d36f68SPiotr Wilczek *
141b4d36f68SPiotr Wilczek * This function is heavily based on "File-backed Storage Gadget" by
142b4d36f68SPiotr Wilczek * Alan Stern which in turn is heavily based on "Gadget Zero" by David
143b4d36f68SPiotr Wilczek * Brownell. The driver's SCSI command interface was based on the
144b4d36f68SPiotr Wilczek * "Information technology - Small Computer System Interface - 2"
145b4d36f68SPiotr Wilczek * document from X3T9.2 Project 375D, Revision 10L, 7-SEP-93,
146b4d36f68SPiotr Wilczek * available at <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>.
147b4d36f68SPiotr Wilczek * The single exception is opcode 0x23 (READ FORMAT CAPACITIES), which
148b4d36f68SPiotr Wilczek * was based on the "Universal Serial Bus Mass Storage Class UFI
149b4d36f68SPiotr Wilczek * Command Specification" document, Revision 1.0, December 14, 1998,
150b4d36f68SPiotr Wilczek * available at
151b4d36f68SPiotr Wilczek * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
152b4d36f68SPiotr Wilczek */
153b4d36f68SPiotr Wilczek
154b4d36f68SPiotr Wilczek /*
155b4d36f68SPiotr Wilczek * Driver Design
156b4d36f68SPiotr Wilczek *
157b4d36f68SPiotr Wilczek * The MSF is fairly straightforward. There is a main kernel
158b4d36f68SPiotr Wilczek * thread that handles most of the work. Interrupt routines field
159b4d36f68SPiotr Wilczek * callbacks from the controller driver: bulk- and interrupt-request
160b4d36f68SPiotr Wilczek * completion notifications, endpoint-0 events, and disconnect events.
161b4d36f68SPiotr Wilczek * Completion events are passed to the main thread by wakeup calls. Many
162b4d36f68SPiotr Wilczek * ep0 requests are handled at interrupt time, but SetInterface,
163b4d36f68SPiotr Wilczek * SetConfiguration, and device reset requests are forwarded to the
164b4d36f68SPiotr Wilczek * thread in the form of "exceptions" using SIGUSR1 signals (since they
165b4d36f68SPiotr Wilczek * should interrupt any ongoing file I/O operations).
166b4d36f68SPiotr Wilczek *
167b4d36f68SPiotr Wilczek * The thread's main routine implements the standard command/data/status
168b4d36f68SPiotr Wilczek * parts of a SCSI interaction. It and its subroutines are full of tests
169b4d36f68SPiotr Wilczek * for pending signals/exceptions -- all this polling is necessary since
170b4d36f68SPiotr Wilczek * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
171b4d36f68SPiotr Wilczek * indication that the driver really wants to be running in userspace.)
172b4d36f68SPiotr Wilczek * An important point is that so long as the thread is alive it keeps an
173b4d36f68SPiotr Wilczek * open reference to the backing file. This will prevent unmounting
174b4d36f68SPiotr Wilczek * the backing file's underlying filesystem and could cause problems
175b4d36f68SPiotr Wilczek * during system shutdown, for example. To prevent such problems, the
176b4d36f68SPiotr Wilczek * thread catches INT, TERM, and KILL signals and converts them into
177b4d36f68SPiotr Wilczek * an EXIT exception.
178b4d36f68SPiotr Wilczek *
179b4d36f68SPiotr Wilczek * In normal operation the main thread is started during the gadget's
180b4d36f68SPiotr Wilczek * fsg_bind() callback and stopped during fsg_unbind(). But it can
181b4d36f68SPiotr Wilczek * also exit when it receives a signal, and there's no point leaving
182b4d36f68SPiotr Wilczek * the gadget running when the thread is dead. At of this moment, MSF
183b4d36f68SPiotr Wilczek * provides no way to deregister the gadget when thread dies -- maybe
184b4d36f68SPiotr Wilczek * a callback functions is needed.
185b4d36f68SPiotr Wilczek *
186b4d36f68SPiotr Wilczek * To provide maximum throughput, the driver uses a circular pipeline of
187b4d36f68SPiotr Wilczek * buffer heads (struct fsg_buffhd). In principle the pipeline can be
188b4d36f68SPiotr Wilczek * arbitrarily long; in practice the benefits don't justify having more
189b4d36f68SPiotr Wilczek * than 2 stages (i.e., double buffering). But it helps to think of the
190b4d36f68SPiotr Wilczek * pipeline as being a long one. Each buffer head contains a bulk-in and
191b4d36f68SPiotr Wilczek * a bulk-out request pointer (since the buffer can be used for both
192b4d36f68SPiotr Wilczek * output and input -- directions always are given from the host's
193b4d36f68SPiotr Wilczek * point of view) as well as a pointer to the buffer and various state
194b4d36f68SPiotr Wilczek * variables.
195b4d36f68SPiotr Wilczek *
196b4d36f68SPiotr Wilczek * Use of the pipeline follows a simple protocol. There is a variable
197b4d36f68SPiotr Wilczek * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
198b4d36f68SPiotr Wilczek * At any time that buffer head may still be in use from an earlier
199b4d36f68SPiotr Wilczek * request, so each buffer head has a state variable indicating whether
200b4d36f68SPiotr Wilczek * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
201b4d36f68SPiotr Wilczek * buffer head to be EMPTY, filling the buffer either by file I/O or by
202b4d36f68SPiotr Wilczek * USB I/O (during which the buffer head is BUSY), and marking the buffer
203b4d36f68SPiotr Wilczek * head FULL when the I/O is complete. Then the buffer will be emptied
204b4d36f68SPiotr Wilczek * (again possibly by USB I/O, during which it is marked BUSY) and
205b4d36f68SPiotr Wilczek * finally marked EMPTY again (possibly by a completion routine).
206b4d36f68SPiotr Wilczek *
207b4d36f68SPiotr Wilczek * A module parameter tells the driver to avoid stalling the bulk
208b4d36f68SPiotr Wilczek * endpoints wherever the transport specification allows. This is
209b4d36f68SPiotr Wilczek * necessary for some UDCs like the SuperH, which cannot reliably clear a
210b4d36f68SPiotr Wilczek * halt on a bulk endpoint. However, under certain circumstances the
211b4d36f68SPiotr Wilczek * Bulk-only specification requires a stall. In such cases the driver
212b4d36f68SPiotr Wilczek * will halt the endpoint and set a flag indicating that it should clear
213b4d36f68SPiotr Wilczek * the halt in software during the next device reset. Hopefully this
214b4d36f68SPiotr Wilczek * will permit everything to work correctly. Furthermore, although the
215b4d36f68SPiotr Wilczek * specification allows the bulk-out endpoint to halt when the host sends
216b4d36f68SPiotr Wilczek * too much data, implementing this would cause an unavoidable race.
217b4d36f68SPiotr Wilczek * The driver will always use the "no-stall" approach for OUT transfers.
218b4d36f68SPiotr Wilczek *
219b4d36f68SPiotr Wilczek * One subtle point concerns sending status-stage responses for ep0
220b4d36f68SPiotr Wilczek * requests. Some of these requests, such as device reset, can involve
221b4d36f68SPiotr Wilczek * interrupting an ongoing file I/O operation, which might take an
222b4d36f68SPiotr Wilczek * arbitrarily long time. During that delay the host might give up on
223b4d36f68SPiotr Wilczek * the original ep0 request and issue a new one. When that happens the
224b4d36f68SPiotr Wilczek * driver should not notify the host about completion of the original
225b4d36f68SPiotr Wilczek * request, as the host will no longer be waiting for it. So the driver
226b4d36f68SPiotr Wilczek * assigns to each ep0 request a unique tag, and it keeps track of the
227b4d36f68SPiotr Wilczek * tag value of the request associated with a long-running exception
228b4d36f68SPiotr Wilczek * (device-reset, interface-change, or configuration-change). When the
229b4d36f68SPiotr Wilczek * exception handler is finished, the status-stage response is submitted
230b4d36f68SPiotr Wilczek * only if the current ep0 request tag is equal to the exception request
231b4d36f68SPiotr Wilczek * tag. Thus only the most recently received ep0 request will get a
232b4d36f68SPiotr Wilczek * status-stage response.
233b4d36f68SPiotr Wilczek *
234b4d36f68SPiotr Wilczek * Warning: This driver source file is too long. It ought to be split up
235b4d36f68SPiotr Wilczek * into a header file plus about 3 separate .c files, to handle the details
236b4d36f68SPiotr Wilczek * of the Gadget, USB Mass Storage, and SCSI protocols.
237b4d36f68SPiotr Wilczek */
238b4d36f68SPiotr Wilczek
239b4d36f68SPiotr Wilczek /* #define VERBOSE_DEBUG */
240b4d36f68SPiotr Wilczek /* #define DUMP_MSGS */
241b4d36f68SPiotr Wilczek
242b4d36f68SPiotr Wilczek #include <config.h>
243f8c987f8SAlexey Brodkin #include <hexdump.h>
244b4d36f68SPiotr Wilczek #include <malloc.h>
245b4d36f68SPiotr Wilczek #include <common.h>
24624b852a7SSimon Glass #include <console.h>
24775504e95SMateusz Zalega #include <g_dnl.h>
248b4d36f68SPiotr Wilczek
249b4d36f68SPiotr Wilczek #include <linux/err.h>
250b4d36f68SPiotr Wilczek #include <linux/usb/ch9.h>
251b4d36f68SPiotr Wilczek #include <linux/usb/gadget.h>
252b4d36f68SPiotr Wilczek #include <usb_mass_storage.h>
253b4d36f68SPiotr Wilczek
254b4d36f68SPiotr Wilczek #include <asm/unaligned.h>
2555ac73f68SBryan O'Donoghue #include <linux/bitops.h>
256b4d36f68SPiotr Wilczek #include <linux/usb/gadget.h>
257b4d36f68SPiotr Wilczek #include <linux/usb/gadget.h>
258b4d36f68SPiotr Wilczek #include <linux/usb/composite.h>
259*916fa097SLukasz Majewski #include <linux/bitmap.h>
260c4d0e856SMateusz Zalega #include <g_dnl.h>
261b4d36f68SPiotr Wilczek
262b4d36f68SPiotr Wilczek /*------------------------------------------------------------------------*/
263b4d36f68SPiotr Wilczek
264b4d36f68SPiotr Wilczek #define FSG_DRIVER_DESC "Mass Storage Function"
265b4d36f68SPiotr Wilczek #define FSG_DRIVER_VERSION "2012/06/5"
266b4d36f68SPiotr Wilczek
267b4d36f68SPiotr Wilczek static const char fsg_string_interface[] = "Mass Storage";
268b4d36f68SPiotr Wilczek
269b4d36f68SPiotr Wilczek #define FSG_NO_INTR_EP 1
270b4d36f68SPiotr Wilczek #define FSG_NO_DEVICE_STRINGS 1
271b4d36f68SPiotr Wilczek #define FSG_NO_OTG 1
272b4d36f68SPiotr Wilczek #define FSG_NO_INTR_EP 1
273b4d36f68SPiotr Wilczek
274b4d36f68SPiotr Wilczek #include "storage_common.c"
275b4d36f68SPiotr Wilczek
276b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
277b4d36f68SPiotr Wilczek
278b4d36f68SPiotr Wilczek #define GFP_ATOMIC ((gfp_t) 0)
279b4d36f68SPiotr Wilczek #define PAGE_CACHE_SHIFT 12
280b4d36f68SPiotr Wilczek #define PAGE_CACHE_SIZE (1 << PAGE_CACHE_SHIFT)
281b4d36f68SPiotr Wilczek #define kthread_create(...) __builtin_return_address(0)
282b4d36f68SPiotr Wilczek #define wait_for_completion(...) do {} while (0)
283b4d36f68SPiotr Wilczek
284b4d36f68SPiotr Wilczek struct kref {int x; };
285b4d36f68SPiotr Wilczek struct completion {int x; };
286b4d36f68SPiotr Wilczek
287b4d36f68SPiotr Wilczek struct fsg_dev;
288b4d36f68SPiotr Wilczek struct fsg_common;
289b4d36f68SPiotr Wilczek
290b4d36f68SPiotr Wilczek /* Data shared by all the FSG instances. */
291b4d36f68SPiotr Wilczek struct fsg_common {
292b4d36f68SPiotr Wilczek struct usb_gadget *gadget;
293b4d36f68SPiotr Wilczek struct fsg_dev *fsg, *new_fsg;
294b4d36f68SPiotr Wilczek
295b4d36f68SPiotr Wilczek struct usb_ep *ep0; /* Copy of gadget->ep0 */
296b4d36f68SPiotr Wilczek struct usb_request *ep0req; /* Copy of cdev->req */
297b4d36f68SPiotr Wilczek unsigned int ep0_req_tag;
298b4d36f68SPiotr Wilczek
299b4d36f68SPiotr Wilczek struct fsg_buffhd *next_buffhd_to_fill;
300b4d36f68SPiotr Wilczek struct fsg_buffhd *next_buffhd_to_drain;
301b4d36f68SPiotr Wilczek struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
302b4d36f68SPiotr Wilczek
303b4d36f68SPiotr Wilczek int cmnd_size;
304b4d36f68SPiotr Wilczek u8 cmnd[MAX_COMMAND_SIZE];
305b4d36f68SPiotr Wilczek
306b4d36f68SPiotr Wilczek unsigned int nluns;
307b4d36f68SPiotr Wilczek unsigned int lun;
308b4d36f68SPiotr Wilczek struct fsg_lun luns[FSG_MAX_LUNS];
309b4d36f68SPiotr Wilczek
310b4d36f68SPiotr Wilczek unsigned int bulk_out_maxpacket;
311b4d36f68SPiotr Wilczek enum fsg_state state; /* For exception handling */
312b4d36f68SPiotr Wilczek unsigned int exception_req_tag;
313b4d36f68SPiotr Wilczek
314b4d36f68SPiotr Wilczek enum data_direction data_dir;
315b4d36f68SPiotr Wilczek u32 data_size;
316b4d36f68SPiotr Wilczek u32 data_size_from_cmnd;
317b4d36f68SPiotr Wilczek u32 tag;
318b4d36f68SPiotr Wilczek u32 residue;
319b4d36f68SPiotr Wilczek u32 usb_amount_left;
320b4d36f68SPiotr Wilczek
321b4d36f68SPiotr Wilczek unsigned int can_stall:1;
322b4d36f68SPiotr Wilczek unsigned int free_storage_on_release:1;
323b4d36f68SPiotr Wilczek unsigned int phase_error:1;
324b4d36f68SPiotr Wilczek unsigned int short_packet_received:1;
325b4d36f68SPiotr Wilczek unsigned int bad_lun_okay:1;
326b4d36f68SPiotr Wilczek unsigned int running:1;
327b4d36f68SPiotr Wilczek
328b4d36f68SPiotr Wilczek int thread_wakeup_needed;
329b4d36f68SPiotr Wilczek struct completion thread_notifier;
330b4d36f68SPiotr Wilczek struct task_struct *thread_task;
331b4d36f68SPiotr Wilczek
332b4d36f68SPiotr Wilczek /* Callback functions. */
333b4d36f68SPiotr Wilczek const struct fsg_operations *ops;
334b4d36f68SPiotr Wilczek /* Gadget's private data. */
335b4d36f68SPiotr Wilczek void *private_data;
336b4d36f68SPiotr Wilczek
337b4d36f68SPiotr Wilczek const char *vendor_name; /* 8 characters or less */
338b4d36f68SPiotr Wilczek const char *product_name; /* 16 characters or less */
339b4d36f68SPiotr Wilczek u16 release;
340b4d36f68SPiotr Wilczek
341b4d36f68SPiotr Wilczek /* Vendor (8 chars), product (16 chars), release (4
342b4d36f68SPiotr Wilczek * hexadecimal digits) and NUL byte */
343b4d36f68SPiotr Wilczek char inquiry_string[8 + 16 + 4 + 1];
344b4d36f68SPiotr Wilczek
345b4d36f68SPiotr Wilczek struct kref ref;
346b4d36f68SPiotr Wilczek };
347b4d36f68SPiotr Wilczek
348b4d36f68SPiotr Wilczek struct fsg_config {
349b4d36f68SPiotr Wilczek unsigned nluns;
350b4d36f68SPiotr Wilczek struct fsg_lun_config {
351b4d36f68SPiotr Wilczek const char *filename;
352b4d36f68SPiotr Wilczek char ro;
353b4d36f68SPiotr Wilczek char removable;
354b4d36f68SPiotr Wilczek char cdrom;
355b4d36f68SPiotr Wilczek char nofua;
356b4d36f68SPiotr Wilczek } luns[FSG_MAX_LUNS];
357b4d36f68SPiotr Wilczek
358b4d36f68SPiotr Wilczek /* Callback functions. */
359b4d36f68SPiotr Wilczek const struct fsg_operations *ops;
360b4d36f68SPiotr Wilczek /* Gadget's private data. */
361b4d36f68SPiotr Wilczek void *private_data;
362b4d36f68SPiotr Wilczek
363b4d36f68SPiotr Wilczek const char *vendor_name; /* 8 characters or less */
364b4d36f68SPiotr Wilczek const char *product_name; /* 16 characters or less */
365b4d36f68SPiotr Wilczek
366b4d36f68SPiotr Wilczek char can_stall;
367b4d36f68SPiotr Wilczek };
368b4d36f68SPiotr Wilczek
369b4d36f68SPiotr Wilczek struct fsg_dev {
370b4d36f68SPiotr Wilczek struct usb_function function;
371b4d36f68SPiotr Wilczek struct usb_gadget *gadget; /* Copy of cdev->gadget */
372b4d36f68SPiotr Wilczek struct fsg_common *common;
373b4d36f68SPiotr Wilczek
374b4d36f68SPiotr Wilczek u16 interface_number;
375b4d36f68SPiotr Wilczek
376b4d36f68SPiotr Wilczek unsigned int bulk_in_enabled:1;
377b4d36f68SPiotr Wilczek unsigned int bulk_out_enabled:1;
378b4d36f68SPiotr Wilczek
379b4d36f68SPiotr Wilczek unsigned long atomic_bitflags;
380b4d36f68SPiotr Wilczek #define IGNORE_BULK_OUT 0
381b4d36f68SPiotr Wilczek
382b4d36f68SPiotr Wilczek struct usb_ep *bulk_in;
383b4d36f68SPiotr Wilczek struct usb_ep *bulk_out;
384b4d36f68SPiotr Wilczek };
385b4d36f68SPiotr Wilczek
386b4d36f68SPiotr Wilczek
__fsg_is_set(struct fsg_common * common,const char * func,unsigned line)387b4d36f68SPiotr Wilczek static inline int __fsg_is_set(struct fsg_common *common,
388b4d36f68SPiotr Wilczek const char *func, unsigned line)
389b4d36f68SPiotr Wilczek {
390b4d36f68SPiotr Wilczek if (common->fsg)
391b4d36f68SPiotr Wilczek return 1;
392b4d36f68SPiotr Wilczek ERROR(common, "common->fsg is NULL in %s at %u\n", func, line);
393b4d36f68SPiotr Wilczek WARN_ON(1);
394b4d36f68SPiotr Wilczek return 0;
395b4d36f68SPiotr Wilczek }
396b4d36f68SPiotr Wilczek
397b4d36f68SPiotr Wilczek #define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))
398b4d36f68SPiotr Wilczek
399b4d36f68SPiotr Wilczek
fsg_from_func(struct usb_function * f)400b4d36f68SPiotr Wilczek static inline struct fsg_dev *fsg_from_func(struct usb_function *f)
401b4d36f68SPiotr Wilczek {
402b4d36f68SPiotr Wilczek return container_of(f, struct fsg_dev, function);
403b4d36f68SPiotr Wilczek }
404b4d36f68SPiotr Wilczek
405b4d36f68SPiotr Wilczek
406b4d36f68SPiotr Wilczek typedef void (*fsg_routine_t)(struct fsg_dev *);
407b4d36f68SPiotr Wilczek
exception_in_progress(struct fsg_common * common)408b4d36f68SPiotr Wilczek static int exception_in_progress(struct fsg_common *common)
409b4d36f68SPiotr Wilczek {
410b4d36f68SPiotr Wilczek return common->state > FSG_STATE_IDLE;
411b4d36f68SPiotr Wilczek }
412b4d36f68SPiotr Wilczek
413b4d36f68SPiotr Wilczek /* 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)414b4d36f68SPiotr Wilczek static void set_bulk_out_req_length(struct fsg_common *common,
415b4d36f68SPiotr Wilczek struct fsg_buffhd *bh, unsigned int length)
416b4d36f68SPiotr Wilczek {
417b4d36f68SPiotr Wilczek unsigned int rem;
418b4d36f68SPiotr Wilczek
419b4d36f68SPiotr Wilczek bh->bulk_out_intended_length = length;
420b4d36f68SPiotr Wilczek rem = length % common->bulk_out_maxpacket;
421b4d36f68SPiotr Wilczek if (rem > 0)
422b4d36f68SPiotr Wilczek length += common->bulk_out_maxpacket - rem;
423b4d36f68SPiotr Wilczek bh->outreq->length = length;
424b4d36f68SPiotr Wilczek }
425b4d36f68SPiotr Wilczek
426b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
427b4d36f68SPiotr Wilczek
42802585eb3SStephen Warren static struct ums *ums;
42902585eb3SStephen Warren static int ums_count;
43002585eb3SStephen Warren static struct fsg_common *the_fsg_common;
431b4d36f68SPiotr Wilczek
fsg_set_halt(struct fsg_dev * fsg,struct usb_ep * ep)432b4d36f68SPiotr Wilczek static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
433b4d36f68SPiotr Wilczek {
434b4d36f68SPiotr Wilczek const char *name;
435b4d36f68SPiotr Wilczek
436b4d36f68SPiotr Wilczek if (ep == fsg->bulk_in)
437b4d36f68SPiotr Wilczek name = "bulk-in";
438b4d36f68SPiotr Wilczek else if (ep == fsg->bulk_out)
439b4d36f68SPiotr Wilczek name = "bulk-out";
440b4d36f68SPiotr Wilczek else
441b4d36f68SPiotr Wilczek name = ep->name;
442b4d36f68SPiotr Wilczek DBG(fsg, "%s set halt\n", name);
443b4d36f68SPiotr Wilczek return usb_ep_set_halt(ep);
444b4d36f68SPiotr Wilczek }
445b4d36f68SPiotr Wilczek
446b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
447b4d36f68SPiotr Wilczek
448b4d36f68SPiotr Wilczek /* These routines may be called in process context or in_irq */
449b4d36f68SPiotr Wilczek
450b4d36f68SPiotr Wilczek /* Caller must hold fsg->lock */
wakeup_thread(struct fsg_common * common)451b4d36f68SPiotr Wilczek static void wakeup_thread(struct fsg_common *common)
452b4d36f68SPiotr Wilczek {
453b4d36f68SPiotr Wilczek common->thread_wakeup_needed = 1;
454b4d36f68SPiotr Wilczek }
455b4d36f68SPiotr Wilczek
raise_exception(struct fsg_common * common,enum fsg_state new_state)456b4d36f68SPiotr Wilczek static void raise_exception(struct fsg_common *common, enum fsg_state new_state)
457b4d36f68SPiotr Wilczek {
458b4d36f68SPiotr Wilczek /* Do nothing if a higher-priority exception is already in progress.
459b4d36f68SPiotr Wilczek * If a lower-or-equal priority exception is in progress, preempt it
460b4d36f68SPiotr Wilczek * and notify the main thread by sending it a signal. */
461b4d36f68SPiotr Wilczek if (common->state <= new_state) {
462b4d36f68SPiotr Wilczek common->exception_req_tag = common->ep0_req_tag;
463b4d36f68SPiotr Wilczek common->state = new_state;
464b4d36f68SPiotr Wilczek common->thread_wakeup_needed = 1;
465b4d36f68SPiotr Wilczek }
466b4d36f68SPiotr Wilczek }
467b4d36f68SPiotr Wilczek
468b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
469b4d36f68SPiotr Wilczek
ep0_queue(struct fsg_common * common)470b4d36f68SPiotr Wilczek static int ep0_queue(struct fsg_common *common)
471b4d36f68SPiotr Wilczek {
472b4d36f68SPiotr Wilczek int rc;
473b4d36f68SPiotr Wilczek
474b4d36f68SPiotr Wilczek rc = usb_ep_queue(common->ep0, common->ep0req, GFP_ATOMIC);
475b4d36f68SPiotr Wilczek common->ep0->driver_data = common;
476b4d36f68SPiotr Wilczek if (rc != 0 && rc != -ESHUTDOWN) {
477b4d36f68SPiotr Wilczek /* We can't do much more than wait for a reset */
478b4d36f68SPiotr Wilczek WARNING(common, "error in submission: %s --> %d\n",
479b4d36f68SPiotr Wilczek common->ep0->name, rc);
480b4d36f68SPiotr Wilczek }
481b4d36f68SPiotr Wilczek return rc;
482b4d36f68SPiotr Wilczek }
483b4d36f68SPiotr Wilczek
484b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
485b4d36f68SPiotr Wilczek
486b4d36f68SPiotr Wilczek /* Bulk and interrupt endpoint completion handlers.
487b4d36f68SPiotr Wilczek * These always run in_irq. */
488b4d36f68SPiotr Wilczek
bulk_in_complete(struct usb_ep * ep,struct usb_request * req)489b4d36f68SPiotr Wilczek static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
490b4d36f68SPiotr Wilczek {
491b4d36f68SPiotr Wilczek struct fsg_common *common = ep->driver_data;
492b4d36f68SPiotr Wilczek struct fsg_buffhd *bh = req->context;
493b4d36f68SPiotr Wilczek
494b4d36f68SPiotr Wilczek if (req->status || req->actual != req->length)
495b4d36f68SPiotr Wilczek DBG(common, "%s --> %d, %u/%u\n", __func__,
496b4d36f68SPiotr Wilczek req->status, req->actual, req->length);
497b4d36f68SPiotr Wilczek if (req->status == -ECONNRESET) /* Request was cancelled */
498b4d36f68SPiotr Wilczek usb_ep_fifo_flush(ep);
499b4d36f68SPiotr Wilczek
500b4d36f68SPiotr Wilczek /* Hold the lock while we update the request and buffer states */
501b4d36f68SPiotr Wilczek bh->inreq_busy = 0;
502b4d36f68SPiotr Wilczek bh->state = BUF_STATE_EMPTY;
503b4d36f68SPiotr Wilczek wakeup_thread(common);
504b4d36f68SPiotr Wilczek }
505b4d36f68SPiotr Wilczek
bulk_out_complete(struct usb_ep * ep,struct usb_request * req)506b4d36f68SPiotr Wilczek static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
507b4d36f68SPiotr Wilczek {
508b4d36f68SPiotr Wilczek struct fsg_common *common = ep->driver_data;
509b4d36f68SPiotr Wilczek struct fsg_buffhd *bh = req->context;
510b4d36f68SPiotr Wilczek
511b4d36f68SPiotr Wilczek dump_msg(common, "bulk-out", req->buf, req->actual);
512b4d36f68SPiotr Wilczek if (req->status || req->actual != bh->bulk_out_intended_length)
513b4d36f68SPiotr Wilczek DBG(common, "%s --> %d, %u/%u\n", __func__,
514b4d36f68SPiotr Wilczek req->status, req->actual,
515b4d36f68SPiotr Wilczek bh->bulk_out_intended_length);
516b4d36f68SPiotr Wilczek if (req->status == -ECONNRESET) /* Request was cancelled */
517b4d36f68SPiotr Wilczek usb_ep_fifo_flush(ep);
518b4d36f68SPiotr Wilczek
519b4d36f68SPiotr Wilczek /* Hold the lock while we update the request and buffer states */
520b4d36f68SPiotr Wilczek bh->outreq_busy = 0;
521b4d36f68SPiotr Wilczek bh->state = BUF_STATE_FULL;
522b4d36f68SPiotr Wilczek wakeup_thread(common);
523b4d36f68SPiotr Wilczek }
524b4d36f68SPiotr Wilczek
525b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
526b4d36f68SPiotr Wilczek
527b4d36f68SPiotr Wilczek /* Ep0 class-specific handlers. These always run in_irq. */
528b4d36f68SPiotr Wilczek
fsg_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)529b4d36f68SPiotr Wilczek static int fsg_setup(struct usb_function *f,
530b4d36f68SPiotr Wilczek const struct usb_ctrlrequest *ctrl)
531b4d36f68SPiotr Wilczek {
532b4d36f68SPiotr Wilczek struct fsg_dev *fsg = fsg_from_func(f);
533b4d36f68SPiotr Wilczek struct usb_request *req = fsg->common->ep0req;
5347f3cf406SPiotr Wilczek u16 w_index = get_unaligned_le16(&ctrl->wIndex);
5357f3cf406SPiotr Wilczek u16 w_value = get_unaligned_le16(&ctrl->wValue);
5367f3cf406SPiotr Wilczek u16 w_length = get_unaligned_le16(&ctrl->wLength);
537b4d36f68SPiotr Wilczek
538b4d36f68SPiotr Wilczek if (!fsg_is_set(fsg->common))
539b4d36f68SPiotr Wilczek return -EOPNOTSUPP;
540b4d36f68SPiotr Wilczek
541b4d36f68SPiotr Wilczek switch (ctrl->bRequest) {
542b4d36f68SPiotr Wilczek
543b4d36f68SPiotr Wilczek case USB_BULK_RESET_REQUEST:
544b4d36f68SPiotr Wilczek if (ctrl->bRequestType !=
545b4d36f68SPiotr Wilczek (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
546b4d36f68SPiotr Wilczek break;
547b4d36f68SPiotr Wilczek if (w_index != fsg->interface_number || w_value != 0)
548b4d36f68SPiotr Wilczek return -EDOM;
549b4d36f68SPiotr Wilczek
550b4d36f68SPiotr Wilczek /* Raise an exception to stop the current operation
551b4d36f68SPiotr Wilczek * and reinitialize our state. */
552b4d36f68SPiotr Wilczek DBG(fsg, "bulk reset request\n");
553b4d36f68SPiotr Wilczek raise_exception(fsg->common, FSG_STATE_RESET);
554b4d36f68SPiotr Wilczek return DELAYED_STATUS;
555b4d36f68SPiotr Wilczek
556b4d36f68SPiotr Wilczek case USB_BULK_GET_MAX_LUN_REQUEST:
557b4d36f68SPiotr Wilczek if (ctrl->bRequestType !=
558b4d36f68SPiotr Wilczek (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE))
559b4d36f68SPiotr Wilczek break;
560b4d36f68SPiotr Wilczek if (w_index != fsg->interface_number || w_value != 0)
561b4d36f68SPiotr Wilczek return -EDOM;
562b4d36f68SPiotr Wilczek VDBG(fsg, "get max LUN\n");
563b4d36f68SPiotr Wilczek *(u8 *) req->buf = fsg->common->nluns - 1;
564b4d36f68SPiotr Wilczek
565b4d36f68SPiotr Wilczek /* Respond with data/status */
566b4d36f68SPiotr Wilczek req->length = min((u16)1, w_length);
567b4d36f68SPiotr Wilczek return ep0_queue(fsg->common);
568b4d36f68SPiotr Wilczek }
569b4d36f68SPiotr Wilczek
570b4d36f68SPiotr Wilczek VDBG(fsg,
571b4d36f68SPiotr Wilczek "unknown class-specific control req "
572b4d36f68SPiotr Wilczek "%02x.%02x v%04x i%04x l%u\n",
573b4d36f68SPiotr Wilczek ctrl->bRequestType, ctrl->bRequest,
5747f3cf406SPiotr Wilczek get_unaligned_le16(&ctrl->wValue), w_index, w_length);
575b4d36f68SPiotr Wilczek return -EOPNOTSUPP;
576b4d36f68SPiotr Wilczek }
577b4d36f68SPiotr Wilczek
578b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
579b4d36f68SPiotr Wilczek
580b4d36f68SPiotr Wilczek /* All the following routines run in process context */
581b4d36f68SPiotr Wilczek
582b4d36f68SPiotr Wilczek /* Use this for bulk or interrupt transfers, not ep0 */
start_transfer(struct fsg_dev * fsg,struct usb_ep * ep,struct usb_request * req,int * pbusy,enum fsg_buffer_state * state)583b4d36f68SPiotr Wilczek static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
584b4d36f68SPiotr Wilczek struct usb_request *req, int *pbusy,
585b4d36f68SPiotr Wilczek enum fsg_buffer_state *state)
586b4d36f68SPiotr Wilczek {
587b4d36f68SPiotr Wilczek int rc;
588b4d36f68SPiotr Wilczek
589b4d36f68SPiotr Wilczek if (ep == fsg->bulk_in)
590b4d36f68SPiotr Wilczek dump_msg(fsg, "bulk-in", req->buf, req->length);
591b4d36f68SPiotr Wilczek
592b4d36f68SPiotr Wilczek *pbusy = 1;
593b4d36f68SPiotr Wilczek *state = BUF_STATE_BUSY;
594b4d36f68SPiotr Wilczek rc = usb_ep_queue(ep, req, GFP_KERNEL);
595b4d36f68SPiotr Wilczek if (rc != 0) {
596b4d36f68SPiotr Wilczek *pbusy = 0;
597b4d36f68SPiotr Wilczek *state = BUF_STATE_EMPTY;
598b4d36f68SPiotr Wilczek
599b4d36f68SPiotr Wilczek /* We can't do much more than wait for a reset */
600b4d36f68SPiotr Wilczek
601b4d36f68SPiotr Wilczek /* Note: currently the net2280 driver fails zero-length
602b4d36f68SPiotr Wilczek * submissions if DMA is enabled. */
603b4d36f68SPiotr Wilczek if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
604b4d36f68SPiotr Wilczek req->length == 0))
605b4d36f68SPiotr Wilczek WARNING(fsg, "error in submission: %s --> %d\n",
606b4d36f68SPiotr Wilczek ep->name, rc);
607b4d36f68SPiotr Wilczek }
608b4d36f68SPiotr Wilczek }
609b4d36f68SPiotr Wilczek
610b4d36f68SPiotr Wilczek #define START_TRANSFER_OR(common, ep_name, req, pbusy, state) \
611b4d36f68SPiotr Wilczek if (fsg_is_set(common)) \
612b4d36f68SPiotr Wilczek start_transfer((common)->fsg, (common)->fsg->ep_name, \
613b4d36f68SPiotr Wilczek req, pbusy, state); \
614b4d36f68SPiotr Wilczek else
615b4d36f68SPiotr Wilczek
616b4d36f68SPiotr Wilczek #define START_TRANSFER(common, ep_name, req, pbusy, state) \
617b4d36f68SPiotr Wilczek START_TRANSFER_OR(common, ep_name, req, pbusy, state) (void)0
618b4d36f68SPiotr Wilczek
busy_indicator(void)619b4d36f68SPiotr Wilczek static void busy_indicator(void)
620b4d36f68SPiotr Wilczek {
621b4d36f68SPiotr Wilczek static int state;
622b4d36f68SPiotr Wilczek
623b4d36f68SPiotr Wilczek switch (state) {
624b4d36f68SPiotr Wilczek case 0:
625b4d36f68SPiotr Wilczek puts("\r|"); break;
626b4d36f68SPiotr Wilczek case 1:
627b4d36f68SPiotr Wilczek puts("\r/"); break;
628b4d36f68SPiotr Wilczek case 2:
629b4d36f68SPiotr Wilczek puts("\r-"); break;
630b4d36f68SPiotr Wilczek case 3:
631b4d36f68SPiotr Wilczek puts("\r\\"); break;
632b4d36f68SPiotr Wilczek case 4:
633b4d36f68SPiotr Wilczek puts("\r|"); break;
634b4d36f68SPiotr Wilczek case 5:
635b4d36f68SPiotr Wilczek puts("\r/"); break;
636b4d36f68SPiotr Wilczek case 6:
637b4d36f68SPiotr Wilczek puts("\r-"); break;
638b4d36f68SPiotr Wilczek case 7:
639b4d36f68SPiotr Wilczek puts("\r\\"); break;
640b4d36f68SPiotr Wilczek default:
641b4d36f68SPiotr Wilczek state = 0;
642b4d36f68SPiotr Wilczek }
643b4d36f68SPiotr Wilczek if (state++ == 8)
644b4d36f68SPiotr Wilczek state = 0;
645b4d36f68SPiotr Wilczek }
646b4d36f68SPiotr Wilczek
sleep_thread(struct fsg_common * common)647b4d36f68SPiotr Wilczek static int sleep_thread(struct fsg_common *common)
648b4d36f68SPiotr Wilczek {
649b4d36f68SPiotr Wilczek int rc = 0;
650b4d36f68SPiotr Wilczek int i = 0, k = 0;
651b4d36f68SPiotr Wilczek
652b4d36f68SPiotr Wilczek /* Wait until a signal arrives or we are woken up */
653b4d36f68SPiotr Wilczek for (;;) {
654b4d36f68SPiotr Wilczek if (common->thread_wakeup_needed)
655b4d36f68SPiotr Wilczek break;
656b4d36f68SPiotr Wilczek
657075020abSInha Song if (++i == 20000) {
658b4d36f68SPiotr Wilczek busy_indicator();
659b4d36f68SPiotr Wilczek i = 0;
660b4d36f68SPiotr Wilczek k++;
661b4d36f68SPiotr Wilczek }
662b4d36f68SPiotr Wilczek
663351e9b20SPrzemyslaw Marczak if (k == 10) {
664351e9b20SPrzemyslaw Marczak /* Handle CTRL+C */
665351e9b20SPrzemyslaw Marczak if (ctrlc())
666351e9b20SPrzemyslaw Marczak return -EPIPE;
66775504e95SMateusz Zalega
668351e9b20SPrzemyslaw Marczak /* Check cable connection */
66975504e95SMateusz Zalega if (!g_dnl_board_usb_cable_connected())
670351e9b20SPrzemyslaw Marczak return -EIO;
67175504e95SMateusz Zalega
672351e9b20SPrzemyslaw Marczak k = 0;
673351e9b20SPrzemyslaw Marczak }
674351e9b20SPrzemyslaw Marczak
6752d48aa69SKishon Vijay Abraham I usb_gadget_handle_interrupts(0);
676b4d36f68SPiotr Wilczek }
677b4d36f68SPiotr Wilczek common->thread_wakeup_needed = 0;
678b4d36f68SPiotr Wilczek return rc;
679b4d36f68SPiotr Wilczek }
680b4d36f68SPiotr Wilczek
681b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
682b4d36f68SPiotr Wilczek
do_read(struct fsg_common * common)683b4d36f68SPiotr Wilczek static int do_read(struct fsg_common *common)
684b4d36f68SPiotr Wilczek {
685b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
686b4d36f68SPiotr Wilczek u32 lba;
687b4d36f68SPiotr Wilczek struct fsg_buffhd *bh;
688b4d36f68SPiotr Wilczek int rc;
689b4d36f68SPiotr Wilczek u32 amount_left;
690b4d36f68SPiotr Wilczek loff_t file_offset;
691b4d36f68SPiotr Wilczek unsigned int amount;
692b4d36f68SPiotr Wilczek unsigned int partial_page;
693b4d36f68SPiotr Wilczek ssize_t nread;
694b4d36f68SPiotr Wilczek
695b4d36f68SPiotr Wilczek /* Get the starting Logical Block Address and check that it's
696b4d36f68SPiotr Wilczek * not too big */
697b4d36f68SPiotr Wilczek if (common->cmnd[0] == SC_READ_6)
698b4d36f68SPiotr Wilczek lba = get_unaligned_be24(&common->cmnd[1]);
699b4d36f68SPiotr Wilczek else {
700b4d36f68SPiotr Wilczek lba = get_unaligned_be32(&common->cmnd[2]);
701b4d36f68SPiotr Wilczek
702b4d36f68SPiotr Wilczek /* We allow DPO (Disable Page Out = don't save data in the
703b4d36f68SPiotr Wilczek * cache) and FUA (Force Unit Access = don't read from the
704b4d36f68SPiotr Wilczek * cache), but we don't implement them. */
705b4d36f68SPiotr Wilczek if ((common->cmnd[1] & ~0x18) != 0) {
706b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
707b4d36f68SPiotr Wilczek return -EINVAL;
708b4d36f68SPiotr Wilczek }
709b4d36f68SPiotr Wilczek }
710b4d36f68SPiotr Wilczek if (lba >= curlun->num_sectors) {
711b4d36f68SPiotr Wilczek curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
712b4d36f68SPiotr Wilczek return -EINVAL;
713b4d36f68SPiotr Wilczek }
714b4d36f68SPiotr Wilczek file_offset = ((loff_t) lba) << 9;
715b4d36f68SPiotr Wilczek
716b4d36f68SPiotr Wilczek /* Carry out the file reads */
717b4d36f68SPiotr Wilczek amount_left = common->data_size_from_cmnd;
718b4d36f68SPiotr Wilczek if (unlikely(amount_left == 0))
719b4d36f68SPiotr Wilczek return -EIO; /* No default reply */
720b4d36f68SPiotr Wilczek
721b4d36f68SPiotr Wilczek for (;;) {
722b4d36f68SPiotr Wilczek
723b4d36f68SPiotr Wilczek /* Figure out how much we need to read:
724b4d36f68SPiotr Wilczek * Try to read the remaining amount.
725b4d36f68SPiotr Wilczek * But don't read more than the buffer size.
726b4d36f68SPiotr Wilczek * And don't try to read past the end of the file.
727b4d36f68SPiotr Wilczek * Finally, if we're not at a page boundary, don't read past
728b4d36f68SPiotr Wilczek * the next page.
729b4d36f68SPiotr Wilczek * If this means reading 0 then we were asked to read past
730b4d36f68SPiotr Wilczek * the end of file. */
731b4d36f68SPiotr Wilczek amount = min(amount_left, FSG_BUFLEN);
732b4d36f68SPiotr Wilczek partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
733b4d36f68SPiotr Wilczek if (partial_page > 0)
734b4d36f68SPiotr Wilczek amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
735b4d36f68SPiotr Wilczek partial_page);
736b4d36f68SPiotr Wilczek
737b4d36f68SPiotr Wilczek /* Wait for the next buffer to become available */
738b4d36f68SPiotr Wilczek bh = common->next_buffhd_to_fill;
739b4d36f68SPiotr Wilczek while (bh->state != BUF_STATE_EMPTY) {
740b4d36f68SPiotr Wilczek rc = sleep_thread(common);
741b4d36f68SPiotr Wilczek if (rc)
742b4d36f68SPiotr Wilczek return rc;
743b4d36f68SPiotr Wilczek }
744b4d36f68SPiotr Wilczek
745b4d36f68SPiotr Wilczek /* If we were asked to read past the end of file,
746b4d36f68SPiotr Wilczek * end with an empty buffer. */
747b4d36f68SPiotr Wilczek if (amount == 0) {
748b4d36f68SPiotr Wilczek curlun->sense_data =
749b4d36f68SPiotr Wilczek SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
750b4d36f68SPiotr Wilczek curlun->info_valid = 1;
751b4d36f68SPiotr Wilczek bh->inreq->length = 0;
752b4d36f68SPiotr Wilczek bh->state = BUF_STATE_FULL;
753b4d36f68SPiotr Wilczek break;
754b4d36f68SPiotr Wilczek }
755b4d36f68SPiotr Wilczek
756b4d36f68SPiotr Wilczek /* Perform the read */
75702585eb3SStephen Warren rc = ums[common->lun].read_sector(&ums[common->lun],
758b4d36f68SPiotr Wilczek file_offset / SECTOR_SIZE,
759b4d36f68SPiotr Wilczek amount / SECTOR_SIZE,
760b4d36f68SPiotr Wilczek (char __user *)bh->buf);
76193c813b3SPrzemyslaw Marczak if (!rc)
762b4d36f68SPiotr Wilczek return -EIO;
76393c813b3SPrzemyslaw Marczak
76493c813b3SPrzemyslaw Marczak nread = rc * SECTOR_SIZE;
765b4d36f68SPiotr Wilczek
766b4d36f68SPiotr Wilczek VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
767b4d36f68SPiotr Wilczek (unsigned long long) file_offset,
768b4d36f68SPiotr Wilczek (int) nread);
769b4d36f68SPiotr Wilczek
770b4d36f68SPiotr Wilczek if (nread < 0) {
771b4d36f68SPiotr Wilczek LDBG(curlun, "error in file read: %d\n",
772b4d36f68SPiotr Wilczek (int) nread);
773b4d36f68SPiotr Wilczek nread = 0;
774b4d36f68SPiotr Wilczek } else if (nread < amount) {
775b4d36f68SPiotr Wilczek LDBG(curlun, "partial file read: %d/%u\n",
776b4d36f68SPiotr Wilczek (int) nread, amount);
777b4d36f68SPiotr Wilczek nread -= (nread & 511); /* Round down to a block */
778b4d36f68SPiotr Wilczek }
779b4d36f68SPiotr Wilczek file_offset += nread;
780b4d36f68SPiotr Wilczek amount_left -= nread;
781b4d36f68SPiotr Wilczek common->residue -= nread;
782b4d36f68SPiotr Wilczek bh->inreq->length = nread;
783b4d36f68SPiotr Wilczek bh->state = BUF_STATE_FULL;
784b4d36f68SPiotr Wilczek
785b4d36f68SPiotr Wilczek /* If an error occurred, report it and its position */
786b4d36f68SPiotr Wilczek if (nread < amount) {
787b4d36f68SPiotr Wilczek curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
788b4d36f68SPiotr Wilczek curlun->info_valid = 1;
789b4d36f68SPiotr Wilczek break;
790b4d36f68SPiotr Wilczek }
791b4d36f68SPiotr Wilczek
792b4d36f68SPiotr Wilczek if (amount_left == 0)
793b4d36f68SPiotr Wilczek break; /* No more left to read */
794b4d36f68SPiotr Wilczek
795b4d36f68SPiotr Wilczek /* Send this buffer and go read some more */
796b4d36f68SPiotr Wilczek bh->inreq->zero = 0;
797b4d36f68SPiotr Wilczek START_TRANSFER_OR(common, bulk_in, bh->inreq,
798b4d36f68SPiotr Wilczek &bh->inreq_busy, &bh->state)
799b4d36f68SPiotr Wilczek /* Don't know what to do if
800b4d36f68SPiotr Wilczek * common->fsg is NULL */
801b4d36f68SPiotr Wilczek return -EIO;
802b4d36f68SPiotr Wilczek common->next_buffhd_to_fill = bh->next;
803b4d36f68SPiotr Wilczek }
804b4d36f68SPiotr Wilczek
805b4d36f68SPiotr Wilczek return -EIO; /* No default reply */
806b4d36f68SPiotr Wilczek }
807b4d36f68SPiotr Wilczek
808b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
809b4d36f68SPiotr Wilczek
do_write(struct fsg_common * common)810b4d36f68SPiotr Wilczek static int do_write(struct fsg_common *common)
811b4d36f68SPiotr Wilczek {
812b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
813b4d36f68SPiotr Wilczek u32 lba;
814b4d36f68SPiotr Wilczek struct fsg_buffhd *bh;
815b4d36f68SPiotr Wilczek int get_some_more;
816b4d36f68SPiotr Wilczek u32 amount_left_to_req, amount_left_to_write;
817b4d36f68SPiotr Wilczek loff_t usb_offset, file_offset;
818b4d36f68SPiotr Wilczek unsigned int amount;
819b4d36f68SPiotr Wilczek unsigned int partial_page;
820b4d36f68SPiotr Wilczek ssize_t nwritten;
821b4d36f68SPiotr Wilczek int rc;
822b4d36f68SPiotr Wilczek
823b4d36f68SPiotr Wilczek if (curlun->ro) {
824b4d36f68SPiotr Wilczek curlun->sense_data = SS_WRITE_PROTECTED;
825b4d36f68SPiotr Wilczek return -EINVAL;
826b4d36f68SPiotr Wilczek }
827b4d36f68SPiotr Wilczek
828b4d36f68SPiotr Wilczek /* Get the starting Logical Block Address and check that it's
829b4d36f68SPiotr Wilczek * not too big */
830b4d36f68SPiotr Wilczek if (common->cmnd[0] == SC_WRITE_6)
831b4d36f68SPiotr Wilczek lba = get_unaligned_be24(&common->cmnd[1]);
832b4d36f68SPiotr Wilczek else {
833b4d36f68SPiotr Wilczek lba = get_unaligned_be32(&common->cmnd[2]);
834b4d36f68SPiotr Wilczek
835b4d36f68SPiotr Wilczek /* We allow DPO (Disable Page Out = don't save data in the
836b4d36f68SPiotr Wilczek * cache) and FUA (Force Unit Access = write directly to the
837b4d36f68SPiotr Wilczek * medium). We don't implement DPO; we implement FUA by
838b4d36f68SPiotr Wilczek * performing synchronous output. */
839b4d36f68SPiotr Wilczek if (common->cmnd[1] & ~0x18) {
840b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
841b4d36f68SPiotr Wilczek return -EINVAL;
842b4d36f68SPiotr Wilczek }
843b4d36f68SPiotr Wilczek }
844b4d36f68SPiotr Wilczek if (lba >= curlun->num_sectors) {
845b4d36f68SPiotr Wilczek curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
846b4d36f68SPiotr Wilczek return -EINVAL;
847b4d36f68SPiotr Wilczek }
848b4d36f68SPiotr Wilczek
849b4d36f68SPiotr Wilczek /* Carry out the file writes */
850b4d36f68SPiotr Wilczek get_some_more = 1;
851b4d36f68SPiotr Wilczek file_offset = usb_offset = ((loff_t) lba) << 9;
852b4d36f68SPiotr Wilczek amount_left_to_req = common->data_size_from_cmnd;
853b4d36f68SPiotr Wilczek amount_left_to_write = common->data_size_from_cmnd;
854b4d36f68SPiotr Wilczek
855b4d36f68SPiotr Wilczek while (amount_left_to_write > 0) {
856b4d36f68SPiotr Wilczek
857b4d36f68SPiotr Wilczek /* Queue a request for more data from the host */
858b4d36f68SPiotr Wilczek bh = common->next_buffhd_to_fill;
859b4d36f68SPiotr Wilczek if (bh->state == BUF_STATE_EMPTY && get_some_more) {
860b4d36f68SPiotr Wilczek
861b4d36f68SPiotr Wilczek /* Figure out how much we want to get:
862b4d36f68SPiotr Wilczek * Try to get the remaining amount.
863b4d36f68SPiotr Wilczek * But don't get more than the buffer size.
864b4d36f68SPiotr Wilczek * And don't try to go past the end of the file.
865b4d36f68SPiotr Wilczek * If we're not at a page boundary,
866b4d36f68SPiotr Wilczek * don't go past the next page.
867b4d36f68SPiotr Wilczek * If this means getting 0, then we were asked
868b4d36f68SPiotr Wilczek * to write past the end of file.
869b4d36f68SPiotr Wilczek * Finally, round down to a block boundary. */
870b4d36f68SPiotr Wilczek amount = min(amount_left_to_req, FSG_BUFLEN);
871b4d36f68SPiotr Wilczek partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
872b4d36f68SPiotr Wilczek if (partial_page > 0)
873b4d36f68SPiotr Wilczek amount = min(amount,
874b4d36f68SPiotr Wilczek (unsigned int) PAGE_CACHE_SIZE - partial_page);
875b4d36f68SPiotr Wilczek
876b4d36f68SPiotr Wilczek if (amount == 0) {
877b4d36f68SPiotr Wilczek get_some_more = 0;
878b4d36f68SPiotr Wilczek curlun->sense_data =
879b4d36f68SPiotr Wilczek SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
880b4d36f68SPiotr Wilczek curlun->info_valid = 1;
881b4d36f68SPiotr Wilczek continue;
882b4d36f68SPiotr Wilczek }
883b4d36f68SPiotr Wilczek amount -= (amount & 511);
884b4d36f68SPiotr Wilczek if (amount == 0) {
885b4d36f68SPiotr Wilczek
886b4d36f68SPiotr Wilczek /* Why were we were asked to transfer a
887b4d36f68SPiotr Wilczek * partial block? */
888b4d36f68SPiotr Wilczek get_some_more = 0;
889b4d36f68SPiotr Wilczek continue;
890b4d36f68SPiotr Wilczek }
891b4d36f68SPiotr Wilczek
892b4d36f68SPiotr Wilczek /* Get the next buffer */
893b4d36f68SPiotr Wilczek usb_offset += amount;
894b4d36f68SPiotr Wilczek common->usb_amount_left -= amount;
895b4d36f68SPiotr Wilczek amount_left_to_req -= amount;
896b4d36f68SPiotr Wilczek if (amount_left_to_req == 0)
897b4d36f68SPiotr Wilczek get_some_more = 0;
898b4d36f68SPiotr Wilczek
899b4d36f68SPiotr Wilczek /* amount is always divisible by 512, hence by
900b4d36f68SPiotr Wilczek * the bulk-out maxpacket size */
901b4d36f68SPiotr Wilczek bh->outreq->length = amount;
902b4d36f68SPiotr Wilczek bh->bulk_out_intended_length = amount;
903b4d36f68SPiotr Wilczek bh->outreq->short_not_ok = 1;
904b4d36f68SPiotr Wilczek START_TRANSFER_OR(common, bulk_out, bh->outreq,
905b4d36f68SPiotr Wilczek &bh->outreq_busy, &bh->state)
906b4d36f68SPiotr Wilczek /* Don't know what to do if
907b4d36f68SPiotr Wilczek * common->fsg is NULL */
908b4d36f68SPiotr Wilczek return -EIO;
909b4d36f68SPiotr Wilczek common->next_buffhd_to_fill = bh->next;
910b4d36f68SPiotr Wilczek continue;
911b4d36f68SPiotr Wilczek }
912b4d36f68SPiotr Wilczek
913b4d36f68SPiotr Wilczek /* Write the received data to the backing file */
914b4d36f68SPiotr Wilczek bh = common->next_buffhd_to_drain;
915b4d36f68SPiotr Wilczek if (bh->state == BUF_STATE_EMPTY && !get_some_more)
916b4d36f68SPiotr Wilczek break; /* We stopped early */
917b4d36f68SPiotr Wilczek if (bh->state == BUF_STATE_FULL) {
918b4d36f68SPiotr Wilczek common->next_buffhd_to_drain = bh->next;
919b4d36f68SPiotr Wilczek bh->state = BUF_STATE_EMPTY;
920b4d36f68SPiotr Wilczek
921b4d36f68SPiotr Wilczek /* Did something go wrong with the transfer? */
922b4d36f68SPiotr Wilczek if (bh->outreq->status != 0) {
923b4d36f68SPiotr Wilczek curlun->sense_data = SS_COMMUNICATION_FAILURE;
924b4d36f68SPiotr Wilczek curlun->info_valid = 1;
925b4d36f68SPiotr Wilczek break;
926b4d36f68SPiotr Wilczek }
927b4d36f68SPiotr Wilczek
928b4d36f68SPiotr Wilczek amount = bh->outreq->actual;
929b4d36f68SPiotr Wilczek
930b4d36f68SPiotr Wilczek /* Perform the write */
93102585eb3SStephen Warren rc = ums[common->lun].write_sector(&ums[common->lun],
932b4d36f68SPiotr Wilczek file_offset / SECTOR_SIZE,
933b4d36f68SPiotr Wilczek amount / SECTOR_SIZE,
934b4d36f68SPiotr Wilczek (char __user *)bh->buf);
93593c813b3SPrzemyslaw Marczak if (!rc)
936b4d36f68SPiotr Wilczek return -EIO;
93793c813b3SPrzemyslaw Marczak nwritten = rc * SECTOR_SIZE;
938b4d36f68SPiotr Wilczek
939b4d36f68SPiotr Wilczek VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
940b4d36f68SPiotr Wilczek (unsigned long long) file_offset,
941b4d36f68SPiotr Wilczek (int) nwritten);
942b4d36f68SPiotr Wilczek
943b4d36f68SPiotr Wilczek if (nwritten < 0) {
944b4d36f68SPiotr Wilczek LDBG(curlun, "error in file write: %d\n",
945b4d36f68SPiotr Wilczek (int) nwritten);
946b4d36f68SPiotr Wilczek nwritten = 0;
947b4d36f68SPiotr Wilczek } else if (nwritten < amount) {
948b4d36f68SPiotr Wilczek LDBG(curlun, "partial file write: %d/%u\n",
949b4d36f68SPiotr Wilczek (int) nwritten, amount);
950b4d36f68SPiotr Wilczek nwritten -= (nwritten & 511);
951b4d36f68SPiotr Wilczek /* Round down to a block */
952b4d36f68SPiotr Wilczek }
953b4d36f68SPiotr Wilczek file_offset += nwritten;
954b4d36f68SPiotr Wilczek amount_left_to_write -= nwritten;
955b4d36f68SPiotr Wilczek common->residue -= nwritten;
956b4d36f68SPiotr Wilczek
957b4d36f68SPiotr Wilczek /* If an error occurred, report it and its position */
958b4d36f68SPiotr Wilczek if (nwritten < amount) {
959ec5e78cfSThierry Reding printf("nwritten:%zd amount:%u\n", nwritten,
96093c813b3SPrzemyslaw Marczak amount);
961b4d36f68SPiotr Wilczek curlun->sense_data = SS_WRITE_ERROR;
962b4d36f68SPiotr Wilczek curlun->info_valid = 1;
963b4d36f68SPiotr Wilczek break;
964b4d36f68SPiotr Wilczek }
965b4d36f68SPiotr Wilczek
966b4d36f68SPiotr Wilczek /* Did the host decide to stop early? */
967b4d36f68SPiotr Wilczek if (bh->outreq->actual != bh->outreq->length) {
968b4d36f68SPiotr Wilczek common->short_packet_received = 1;
969b4d36f68SPiotr Wilczek break;
970b4d36f68SPiotr Wilczek }
971b4d36f68SPiotr Wilczek continue;
972b4d36f68SPiotr Wilczek }
973b4d36f68SPiotr Wilczek
974b4d36f68SPiotr Wilczek /* Wait for something to happen */
975b4d36f68SPiotr Wilczek rc = sleep_thread(common);
976b4d36f68SPiotr Wilczek if (rc)
977b4d36f68SPiotr Wilczek return rc;
978b4d36f68SPiotr Wilczek }
979b4d36f68SPiotr Wilczek
980b4d36f68SPiotr Wilczek return -EIO; /* No default reply */
981b4d36f68SPiotr Wilczek }
982b4d36f68SPiotr Wilczek
983b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
984b4d36f68SPiotr Wilczek
do_synchronize_cache(struct fsg_common * common)985b4d36f68SPiotr Wilczek static int do_synchronize_cache(struct fsg_common *common)
986b4d36f68SPiotr Wilczek {
987b4d36f68SPiotr Wilczek return 0;
988b4d36f68SPiotr Wilczek }
989b4d36f68SPiotr Wilczek
990b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
991b4d36f68SPiotr Wilczek
do_verify(struct fsg_common * common)992b4d36f68SPiotr Wilczek static int do_verify(struct fsg_common *common)
993b4d36f68SPiotr Wilczek {
994b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
995b4d36f68SPiotr Wilczek u32 lba;
996b4d36f68SPiotr Wilczek u32 verification_length;
997b4d36f68SPiotr Wilczek struct fsg_buffhd *bh = common->next_buffhd_to_fill;
998b4d36f68SPiotr Wilczek loff_t file_offset;
999b4d36f68SPiotr Wilczek u32 amount_left;
1000b4d36f68SPiotr Wilczek unsigned int amount;
1001b4d36f68SPiotr Wilczek ssize_t nread;
1002b4d36f68SPiotr Wilczek int rc;
1003b4d36f68SPiotr Wilczek
1004b4d36f68SPiotr Wilczek /* Get the starting Logical Block Address and check that it's
1005b4d36f68SPiotr Wilczek * not too big */
1006b4d36f68SPiotr Wilczek lba = get_unaligned_be32(&common->cmnd[2]);
1007b4d36f68SPiotr Wilczek if (lba >= curlun->num_sectors) {
1008b4d36f68SPiotr Wilczek curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1009b4d36f68SPiotr Wilczek return -EINVAL;
1010b4d36f68SPiotr Wilczek }
1011b4d36f68SPiotr Wilczek
1012b4d36f68SPiotr Wilczek /* We allow DPO (Disable Page Out = don't save data in the
1013b4d36f68SPiotr Wilczek * cache) but we don't implement it. */
1014b4d36f68SPiotr Wilczek if (common->cmnd[1] & ~0x10) {
1015b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1016b4d36f68SPiotr Wilczek return -EINVAL;
1017b4d36f68SPiotr Wilczek }
1018b4d36f68SPiotr Wilczek
1019b4d36f68SPiotr Wilczek verification_length = get_unaligned_be16(&common->cmnd[7]);
1020b4d36f68SPiotr Wilczek if (unlikely(verification_length == 0))
1021b4d36f68SPiotr Wilczek return -EIO; /* No default reply */
1022b4d36f68SPiotr Wilczek
1023b4d36f68SPiotr Wilczek /* Prepare to carry out the file verify */
1024b4d36f68SPiotr Wilczek amount_left = verification_length << 9;
1025b4d36f68SPiotr Wilczek file_offset = ((loff_t) lba) << 9;
1026b4d36f68SPiotr Wilczek
1027b4d36f68SPiotr Wilczek /* Write out all the dirty buffers before invalidating them */
1028b4d36f68SPiotr Wilczek
1029b4d36f68SPiotr Wilczek /* Just try to read the requested blocks */
1030b4d36f68SPiotr Wilczek while (amount_left > 0) {
1031b4d36f68SPiotr Wilczek
1032b4d36f68SPiotr Wilczek /* Figure out how much we need to read:
1033b4d36f68SPiotr Wilczek * Try to read the remaining amount, but not more than
1034b4d36f68SPiotr Wilczek * the buffer size.
1035b4d36f68SPiotr Wilczek * And don't try to read past the end of the file.
1036b4d36f68SPiotr Wilczek * If this means reading 0 then we were asked to read
1037b4d36f68SPiotr Wilczek * past the end of file. */
1038b4d36f68SPiotr Wilczek amount = min(amount_left, FSG_BUFLEN);
1039b4d36f68SPiotr Wilczek if (amount == 0) {
1040b4d36f68SPiotr Wilczek curlun->sense_data =
1041b4d36f68SPiotr Wilczek SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1042b4d36f68SPiotr Wilczek curlun->info_valid = 1;
1043b4d36f68SPiotr Wilczek break;
1044b4d36f68SPiotr Wilczek }
1045b4d36f68SPiotr Wilczek
1046b4d36f68SPiotr Wilczek /* Perform the read */
104702585eb3SStephen Warren rc = ums[common->lun].read_sector(&ums[common->lun],
1048b4d36f68SPiotr Wilczek file_offset / SECTOR_SIZE,
1049b4d36f68SPiotr Wilczek amount / SECTOR_SIZE,
1050b4d36f68SPiotr Wilczek (char __user *)bh->buf);
105193c813b3SPrzemyslaw Marczak if (!rc)
1052b4d36f68SPiotr Wilczek return -EIO;
105393c813b3SPrzemyslaw Marczak nread = rc * SECTOR_SIZE;
1054b4d36f68SPiotr Wilczek
1055b4d36f68SPiotr Wilczek VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
1056b4d36f68SPiotr Wilczek (unsigned long long) file_offset,
1057b4d36f68SPiotr Wilczek (int) nread);
1058b4d36f68SPiotr Wilczek if (nread < 0) {
1059b4d36f68SPiotr Wilczek LDBG(curlun, "error in file verify: %d\n",
1060b4d36f68SPiotr Wilczek (int) nread);
1061b4d36f68SPiotr Wilczek nread = 0;
1062b4d36f68SPiotr Wilczek } else if (nread < amount) {
1063b4d36f68SPiotr Wilczek LDBG(curlun, "partial file verify: %d/%u\n",
1064b4d36f68SPiotr Wilczek (int) nread, amount);
1065b4d36f68SPiotr Wilczek nread -= (nread & 511); /* Round down to a sector */
1066b4d36f68SPiotr Wilczek }
1067b4d36f68SPiotr Wilczek if (nread == 0) {
1068b4d36f68SPiotr Wilczek curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
1069b4d36f68SPiotr Wilczek curlun->info_valid = 1;
1070b4d36f68SPiotr Wilczek break;
1071b4d36f68SPiotr Wilczek }
1072b4d36f68SPiotr Wilczek file_offset += nread;
1073b4d36f68SPiotr Wilczek amount_left -= nread;
1074b4d36f68SPiotr Wilczek }
1075b4d36f68SPiotr Wilczek return 0;
1076b4d36f68SPiotr Wilczek }
1077b4d36f68SPiotr Wilczek
1078b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
1079b4d36f68SPiotr Wilczek
do_inquiry(struct fsg_common * common,struct fsg_buffhd * bh)1080b4d36f68SPiotr Wilczek static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1081b4d36f68SPiotr Wilczek {
1082b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1083b4d36f68SPiotr Wilczek static const char vendor_id[] = "Linux ";
1084b4d36f68SPiotr Wilczek u8 *buf = (u8 *) bh->buf;
1085b4d36f68SPiotr Wilczek
1086b4d36f68SPiotr Wilczek if (!curlun) { /* Unsupported LUNs are okay */
1087b4d36f68SPiotr Wilczek common->bad_lun_okay = 1;
1088b4d36f68SPiotr Wilczek memset(buf, 0, 36);
1089b4d36f68SPiotr Wilczek buf[0] = 0x7f; /* Unsupported, no device-type */
1090b4d36f68SPiotr Wilczek buf[4] = 31; /* Additional length */
1091b4d36f68SPiotr Wilczek return 36;
1092b4d36f68SPiotr Wilczek }
1093b4d36f68SPiotr Wilczek
1094b4d36f68SPiotr Wilczek memset(buf, 0, 8);
1095b4d36f68SPiotr Wilczek buf[0] = TYPE_DISK;
1096f9935c87SEric Nelson buf[1] = curlun->removable ? 0x80 : 0;
1097b4d36f68SPiotr Wilczek buf[2] = 2; /* ANSI SCSI level 2 */
1098b4d36f68SPiotr Wilczek buf[3] = 2; /* SCSI-2 INQUIRY data format */
1099b4d36f68SPiotr Wilczek buf[4] = 31; /* Additional length */
1100b4d36f68SPiotr Wilczek /* No special options */
1101b4d36f68SPiotr Wilczek sprintf((char *) (buf + 8), "%-8s%-16s%04x", (char*) vendor_id ,
110202585eb3SStephen Warren ums[common->lun].name, (u16) 0xffff);
1103b4d36f68SPiotr Wilczek
1104b4d36f68SPiotr Wilczek return 36;
1105b4d36f68SPiotr Wilczek }
1106b4d36f68SPiotr Wilczek
1107b4d36f68SPiotr Wilczek
do_request_sense(struct fsg_common * common,struct fsg_buffhd * bh)1108b4d36f68SPiotr Wilczek static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1109b4d36f68SPiotr Wilczek {
1110b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1111b4d36f68SPiotr Wilczek u8 *buf = (u8 *) bh->buf;
1112b4d36f68SPiotr Wilczek u32 sd, sdinfo;
1113b4d36f68SPiotr Wilczek int valid;
1114b4d36f68SPiotr Wilczek
1115b4d36f68SPiotr Wilczek /*
1116b4d36f68SPiotr Wilczek * From the SCSI-2 spec., section 7.9 (Unit attention condition):
1117b4d36f68SPiotr Wilczek *
1118b4d36f68SPiotr Wilczek * If a REQUEST SENSE command is received from an initiator
1119b4d36f68SPiotr Wilczek * with a pending unit attention condition (before the target
1120b4d36f68SPiotr Wilczek * generates the contingent allegiance condition), then the
1121b4d36f68SPiotr Wilczek * target shall either:
1122b4d36f68SPiotr Wilczek * a) report any pending sense data and preserve the unit
1123b4d36f68SPiotr Wilczek * attention condition on the logical unit, or,
1124b4d36f68SPiotr Wilczek * b) report the unit attention condition, may discard any
1125b4d36f68SPiotr Wilczek * pending sense data, and clear the unit attention
1126b4d36f68SPiotr Wilczek * condition on the logical unit for that initiator.
1127b4d36f68SPiotr Wilczek *
1128b4d36f68SPiotr Wilczek * FSG normally uses option a); enable this code to use option b).
1129b4d36f68SPiotr Wilczek */
1130b4d36f68SPiotr Wilczek #if 0
1131b4d36f68SPiotr Wilczek if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
1132b4d36f68SPiotr Wilczek curlun->sense_data = curlun->unit_attention_data;
1133b4d36f68SPiotr Wilczek curlun->unit_attention_data = SS_NO_SENSE;
1134b4d36f68SPiotr Wilczek }
1135b4d36f68SPiotr Wilczek #endif
1136b4d36f68SPiotr Wilczek
1137b4d36f68SPiotr Wilczek if (!curlun) { /* Unsupported LUNs are okay */
1138b4d36f68SPiotr Wilczek common->bad_lun_okay = 1;
1139b4d36f68SPiotr Wilczek sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1140b4d36f68SPiotr Wilczek sdinfo = 0;
1141b4d36f68SPiotr Wilczek valid = 0;
1142b4d36f68SPiotr Wilczek } else {
1143b4d36f68SPiotr Wilczek sd = curlun->sense_data;
1144b4d36f68SPiotr Wilczek valid = curlun->info_valid << 7;
1145b4d36f68SPiotr Wilczek curlun->sense_data = SS_NO_SENSE;
1146b4d36f68SPiotr Wilczek curlun->info_valid = 0;
1147b4d36f68SPiotr Wilczek }
1148b4d36f68SPiotr Wilczek
1149b4d36f68SPiotr Wilczek memset(buf, 0, 18);
1150b4d36f68SPiotr Wilczek buf[0] = valid | 0x70; /* Valid, current error */
1151b4d36f68SPiotr Wilczek buf[2] = SK(sd);
1152b4d36f68SPiotr Wilczek put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
1153b4d36f68SPiotr Wilczek buf[7] = 18 - 8; /* Additional sense length */
1154b4d36f68SPiotr Wilczek buf[12] = ASC(sd);
1155b4d36f68SPiotr Wilczek buf[13] = ASCQ(sd);
1156b4d36f68SPiotr Wilczek return 18;
1157b4d36f68SPiotr Wilczek }
1158b4d36f68SPiotr Wilczek
do_read_capacity(struct fsg_common * common,struct fsg_buffhd * bh)1159b4d36f68SPiotr Wilczek static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
1160b4d36f68SPiotr Wilczek {
1161b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1162b4d36f68SPiotr Wilczek u32 lba = get_unaligned_be32(&common->cmnd[2]);
1163b4d36f68SPiotr Wilczek int pmi = common->cmnd[8];
1164b4d36f68SPiotr Wilczek u8 *buf = (u8 *) bh->buf;
1165b4d36f68SPiotr Wilczek
1166b4d36f68SPiotr Wilczek /* Check the PMI and LBA fields */
1167b4d36f68SPiotr Wilczek if (pmi > 1 || (pmi == 0 && lba != 0)) {
1168b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1169b4d36f68SPiotr Wilczek return -EINVAL;
1170b4d36f68SPiotr Wilczek }
1171b4d36f68SPiotr Wilczek
1172b4d36f68SPiotr Wilczek put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
1173b4d36f68SPiotr Wilczek /* Max logical block */
1174b4d36f68SPiotr Wilczek put_unaligned_be32(512, &buf[4]); /* Block length */
1175b4d36f68SPiotr Wilczek return 8;
1176b4d36f68SPiotr Wilczek }
1177b4d36f68SPiotr Wilczek
do_read_header(struct fsg_common * common,struct fsg_buffhd * bh)1178b4d36f68SPiotr Wilczek static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
1179b4d36f68SPiotr Wilczek {
1180b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1181b4d36f68SPiotr Wilczek int msf = common->cmnd[1] & 0x02;
1182b4d36f68SPiotr Wilczek u32 lba = get_unaligned_be32(&common->cmnd[2]);
1183b4d36f68SPiotr Wilczek u8 *buf = (u8 *) bh->buf;
1184b4d36f68SPiotr Wilczek
1185b4d36f68SPiotr Wilczek if (common->cmnd[1] & ~0x02) { /* Mask away MSF */
1186b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1187b4d36f68SPiotr Wilczek return -EINVAL;
1188b4d36f68SPiotr Wilczek }
1189b4d36f68SPiotr Wilczek if (lba >= curlun->num_sectors) {
1190b4d36f68SPiotr Wilczek curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1191b4d36f68SPiotr Wilczek return -EINVAL;
1192b4d36f68SPiotr Wilczek }
1193b4d36f68SPiotr Wilczek
1194b4d36f68SPiotr Wilczek memset(buf, 0, 8);
1195b4d36f68SPiotr Wilczek buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
1196b4d36f68SPiotr Wilczek store_cdrom_address(&buf[4], msf, lba);
1197b4d36f68SPiotr Wilczek return 8;
1198b4d36f68SPiotr Wilczek }
1199b4d36f68SPiotr Wilczek
1200b4d36f68SPiotr Wilczek
do_read_toc(struct fsg_common * common,struct fsg_buffhd * bh)1201b4d36f68SPiotr Wilczek static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)
1202b4d36f68SPiotr Wilczek {
1203b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1204b4d36f68SPiotr Wilczek int msf = common->cmnd[1] & 0x02;
1205b4d36f68SPiotr Wilczek int start_track = common->cmnd[6];
1206b4d36f68SPiotr Wilczek u8 *buf = (u8 *) bh->buf;
1207b4d36f68SPiotr Wilczek
1208b4d36f68SPiotr Wilczek if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1209b4d36f68SPiotr Wilczek start_track > 1) {
1210b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1211b4d36f68SPiotr Wilczek return -EINVAL;
1212b4d36f68SPiotr Wilczek }
1213b4d36f68SPiotr Wilczek
1214b4d36f68SPiotr Wilczek memset(buf, 0, 20);
1215b4d36f68SPiotr Wilczek buf[1] = (20-2); /* TOC data length */
1216b4d36f68SPiotr Wilczek buf[2] = 1; /* First track number */
1217b4d36f68SPiotr Wilczek buf[3] = 1; /* Last track number */
1218b4d36f68SPiotr Wilczek buf[5] = 0x16; /* Data track, copying allowed */
1219b4d36f68SPiotr Wilczek buf[6] = 0x01; /* Only track is number 1 */
1220b4d36f68SPiotr Wilczek store_cdrom_address(&buf[8], msf, 0);
1221b4d36f68SPiotr Wilczek
1222b4d36f68SPiotr Wilczek buf[13] = 0x16; /* Lead-out track is data */
1223b4d36f68SPiotr Wilczek buf[14] = 0xAA; /* Lead-out track number */
1224b4d36f68SPiotr Wilczek store_cdrom_address(&buf[16], msf, curlun->num_sectors);
1225b4d36f68SPiotr Wilczek
1226b4d36f68SPiotr Wilczek return 20;
1227b4d36f68SPiotr Wilczek }
1228b4d36f68SPiotr Wilczek
do_mode_sense(struct fsg_common * common,struct fsg_buffhd * bh)1229b4d36f68SPiotr Wilczek static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1230b4d36f68SPiotr Wilczek {
1231b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1232b4d36f68SPiotr Wilczek int mscmnd = common->cmnd[0];
1233b4d36f68SPiotr Wilczek u8 *buf = (u8 *) bh->buf;
1234b4d36f68SPiotr Wilczek u8 *buf0 = buf;
1235b4d36f68SPiotr Wilczek int pc, page_code;
1236b4d36f68SPiotr Wilczek int changeable_values, all_pages;
1237b4d36f68SPiotr Wilczek int valid_page = 0;
1238b4d36f68SPiotr Wilczek int len, limit;
1239b4d36f68SPiotr Wilczek
1240b4d36f68SPiotr Wilczek if ((common->cmnd[1] & ~0x08) != 0) { /* Mask away DBD */
1241b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1242b4d36f68SPiotr Wilczek return -EINVAL;
1243b4d36f68SPiotr Wilczek }
1244b4d36f68SPiotr Wilczek pc = common->cmnd[2] >> 6;
1245b4d36f68SPiotr Wilczek page_code = common->cmnd[2] & 0x3f;
1246b4d36f68SPiotr Wilczek if (pc == 3) {
1247b4d36f68SPiotr Wilczek curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1248b4d36f68SPiotr Wilczek return -EINVAL;
1249b4d36f68SPiotr Wilczek }
1250b4d36f68SPiotr Wilczek changeable_values = (pc == 1);
1251b4d36f68SPiotr Wilczek all_pages = (page_code == 0x3f);
1252b4d36f68SPiotr Wilczek
1253b4d36f68SPiotr Wilczek /* Write the mode parameter header. Fixed values are: default
1254b4d36f68SPiotr Wilczek * medium type, no cache control (DPOFUA), and no block descriptors.
1255b4d36f68SPiotr Wilczek * The only variable value is the WriteProtect bit. We will fill in
1256b4d36f68SPiotr Wilczek * the mode data length later. */
1257b4d36f68SPiotr Wilczek memset(buf, 0, 8);
1258b4d36f68SPiotr Wilczek if (mscmnd == SC_MODE_SENSE_6) {
1259b4d36f68SPiotr Wilczek buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1260b4d36f68SPiotr Wilczek buf += 4;
1261b4d36f68SPiotr Wilczek limit = 255;
1262b4d36f68SPiotr Wilczek } else { /* SC_MODE_SENSE_10 */
1263b4d36f68SPiotr Wilczek buf[3] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */
1264b4d36f68SPiotr Wilczek buf += 8;
1265b4d36f68SPiotr Wilczek limit = 65535; /* Should really be FSG_BUFLEN */
1266b4d36f68SPiotr Wilczek }
1267b4d36f68SPiotr Wilczek
1268b4d36f68SPiotr Wilczek /* No block descriptors */
1269b4d36f68SPiotr Wilczek
1270b4d36f68SPiotr Wilczek /* The mode pages, in numerical order. The only page we support
1271b4d36f68SPiotr Wilczek * is the Caching page. */
1272b4d36f68SPiotr Wilczek if (page_code == 0x08 || all_pages) {
1273b4d36f68SPiotr Wilczek valid_page = 1;
1274b4d36f68SPiotr Wilczek buf[0] = 0x08; /* Page code */
1275b4d36f68SPiotr Wilczek buf[1] = 10; /* Page length */
1276b4d36f68SPiotr Wilczek memset(buf+2, 0, 10); /* None of the fields are changeable */
1277b4d36f68SPiotr Wilczek
1278b4d36f68SPiotr Wilczek if (!changeable_values) {
1279b4d36f68SPiotr Wilczek buf[2] = 0x04; /* Write cache enable, */
1280b4d36f68SPiotr Wilczek /* Read cache not disabled */
1281b4d36f68SPiotr Wilczek /* No cache retention priorities */
1282b4d36f68SPiotr Wilczek put_unaligned_be16(0xffff, &buf[4]);
1283b4d36f68SPiotr Wilczek /* Don't disable prefetch */
1284b4d36f68SPiotr Wilczek /* Minimum prefetch = 0 */
1285b4d36f68SPiotr Wilczek put_unaligned_be16(0xffff, &buf[8]);
1286b4d36f68SPiotr Wilczek /* Maximum prefetch */
1287b4d36f68SPiotr Wilczek put_unaligned_be16(0xffff, &buf[10]);
1288b4d36f68SPiotr Wilczek /* Maximum prefetch ceiling */
1289b4d36f68SPiotr Wilczek }
1290b4d36f68SPiotr Wilczek buf += 12;
1291b4d36f68SPiotr Wilczek }
1292b4d36f68SPiotr Wilczek
1293b4d36f68SPiotr Wilczek /* Check that a valid page was requested and the mode data length
1294b4d36f68SPiotr Wilczek * isn't too long. */
1295b4d36f68SPiotr Wilczek len = buf - buf0;
1296b4d36f68SPiotr Wilczek if (!valid_page || len > limit) {
1297b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1298b4d36f68SPiotr Wilczek return -EINVAL;
1299b4d36f68SPiotr Wilczek }
1300b4d36f68SPiotr Wilczek
1301b4d36f68SPiotr Wilczek /* Store the mode data length */
1302b4d36f68SPiotr Wilczek if (mscmnd == SC_MODE_SENSE_6)
1303b4d36f68SPiotr Wilczek buf0[0] = len - 1;
1304b4d36f68SPiotr Wilczek else
1305b4d36f68SPiotr Wilczek put_unaligned_be16(len - 2, buf0);
1306b4d36f68SPiotr Wilczek return len;
1307b4d36f68SPiotr Wilczek }
1308b4d36f68SPiotr Wilczek
1309b4d36f68SPiotr Wilczek
do_start_stop(struct fsg_common * common)1310b4d36f68SPiotr Wilczek static int do_start_stop(struct fsg_common *common)
1311b4d36f68SPiotr Wilczek {
1312b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1313b4d36f68SPiotr Wilczek
1314b4d36f68SPiotr Wilczek if (!curlun) {
1315b4d36f68SPiotr Wilczek return -EINVAL;
1316b4d36f68SPiotr Wilczek } else if (!curlun->removable) {
1317b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_COMMAND;
1318b4d36f68SPiotr Wilczek return -EINVAL;
1319b4d36f68SPiotr Wilczek }
1320b4d36f68SPiotr Wilczek
1321b4d36f68SPiotr Wilczek return 0;
1322b4d36f68SPiotr Wilczek }
1323b4d36f68SPiotr Wilczek
do_prevent_allow(struct fsg_common * common)1324b4d36f68SPiotr Wilczek static int do_prevent_allow(struct fsg_common *common)
1325b4d36f68SPiotr Wilczek {
1326b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1327b4d36f68SPiotr Wilczek int prevent;
1328b4d36f68SPiotr Wilczek
1329b4d36f68SPiotr Wilczek if (!curlun->removable) {
1330b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_COMMAND;
1331b4d36f68SPiotr Wilczek return -EINVAL;
1332b4d36f68SPiotr Wilczek }
1333b4d36f68SPiotr Wilczek
1334b4d36f68SPiotr Wilczek prevent = common->cmnd[4] & 0x01;
1335b4d36f68SPiotr Wilczek if ((common->cmnd[4] & ~0x01) != 0) { /* Mask away Prevent */
1336b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1337b4d36f68SPiotr Wilczek return -EINVAL;
1338b4d36f68SPiotr Wilczek }
1339b4d36f68SPiotr Wilczek
1340b4d36f68SPiotr Wilczek if (curlun->prevent_medium_removal && !prevent)
1341b4d36f68SPiotr Wilczek fsg_lun_fsync_sub(curlun);
1342b4d36f68SPiotr Wilczek curlun->prevent_medium_removal = prevent;
1343b4d36f68SPiotr Wilczek return 0;
1344b4d36f68SPiotr Wilczek }
1345b4d36f68SPiotr Wilczek
1346b4d36f68SPiotr Wilczek
do_read_format_capacities(struct fsg_common * common,struct fsg_buffhd * bh)1347b4d36f68SPiotr Wilczek static int do_read_format_capacities(struct fsg_common *common,
1348b4d36f68SPiotr Wilczek struct fsg_buffhd *bh)
1349b4d36f68SPiotr Wilczek {
1350b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1351b4d36f68SPiotr Wilczek u8 *buf = (u8 *) bh->buf;
1352b4d36f68SPiotr Wilczek
1353b4d36f68SPiotr Wilczek buf[0] = buf[1] = buf[2] = 0;
1354b4d36f68SPiotr Wilczek buf[3] = 8; /* Only the Current/Maximum Capacity Descriptor */
1355b4d36f68SPiotr Wilczek buf += 4;
1356b4d36f68SPiotr Wilczek
1357b4d36f68SPiotr Wilczek put_unaligned_be32(curlun->num_sectors, &buf[0]);
1358b4d36f68SPiotr Wilczek /* Number of blocks */
1359b4d36f68SPiotr Wilczek put_unaligned_be32(512, &buf[4]); /* Block length */
1360b4d36f68SPiotr Wilczek buf[4] = 0x02; /* Current capacity */
1361b4d36f68SPiotr Wilczek return 12;
1362b4d36f68SPiotr Wilczek }
1363b4d36f68SPiotr Wilczek
1364b4d36f68SPiotr Wilczek
do_mode_select(struct fsg_common * common,struct fsg_buffhd * bh)1365b4d36f68SPiotr Wilczek static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)
1366b4d36f68SPiotr Wilczek {
1367b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1368b4d36f68SPiotr Wilczek
1369b4d36f68SPiotr Wilczek /* We don't support MODE SELECT */
1370b4d36f68SPiotr Wilczek if (curlun)
1371b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_COMMAND;
1372b4d36f68SPiotr Wilczek return -EINVAL;
1373b4d36f68SPiotr Wilczek }
1374b4d36f68SPiotr Wilczek
1375b4d36f68SPiotr Wilczek
1376b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
1377b4d36f68SPiotr Wilczek
halt_bulk_in_endpoint(struct fsg_dev * fsg)1378b4d36f68SPiotr Wilczek static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
1379b4d36f68SPiotr Wilczek {
1380b4d36f68SPiotr Wilczek int rc;
1381b4d36f68SPiotr Wilczek
1382b4d36f68SPiotr Wilczek rc = fsg_set_halt(fsg, fsg->bulk_in);
1383b4d36f68SPiotr Wilczek if (rc == -EAGAIN)
1384b4d36f68SPiotr Wilczek VDBG(fsg, "delayed bulk-in endpoint halt\n");
1385b4d36f68SPiotr Wilczek while (rc != 0) {
1386b4d36f68SPiotr Wilczek if (rc != -EAGAIN) {
1387b4d36f68SPiotr Wilczek WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
1388b4d36f68SPiotr Wilczek rc = 0;
1389b4d36f68SPiotr Wilczek break;
1390b4d36f68SPiotr Wilczek }
1391b4d36f68SPiotr Wilczek
1392b4d36f68SPiotr Wilczek rc = usb_ep_set_halt(fsg->bulk_in);
1393b4d36f68SPiotr Wilczek }
1394b4d36f68SPiotr Wilczek return rc;
1395b4d36f68SPiotr Wilczek }
1396b4d36f68SPiotr Wilczek
wedge_bulk_in_endpoint(struct fsg_dev * fsg)1397b4d36f68SPiotr Wilczek static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1398b4d36f68SPiotr Wilczek {
1399b4d36f68SPiotr Wilczek int rc;
1400b4d36f68SPiotr Wilczek
1401b4d36f68SPiotr Wilczek DBG(fsg, "bulk-in set wedge\n");
1402b4d36f68SPiotr Wilczek rc = 0; /* usb_ep_set_wedge(fsg->bulk_in); */
1403b4d36f68SPiotr Wilczek if (rc == -EAGAIN)
1404b4d36f68SPiotr Wilczek VDBG(fsg, "delayed bulk-in endpoint wedge\n");
1405b4d36f68SPiotr Wilczek while (rc != 0) {
1406b4d36f68SPiotr Wilczek if (rc != -EAGAIN) {
1407b4d36f68SPiotr Wilczek WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
1408b4d36f68SPiotr Wilczek rc = 0;
1409b4d36f68SPiotr Wilczek break;
1410b4d36f68SPiotr Wilczek }
1411b4d36f68SPiotr Wilczek }
1412b4d36f68SPiotr Wilczek return rc;
1413b4d36f68SPiotr Wilczek }
1414b4d36f68SPiotr Wilczek
pad_with_zeros(struct fsg_dev * fsg)1415b4d36f68SPiotr Wilczek static int pad_with_zeros(struct fsg_dev *fsg)
1416b4d36f68SPiotr Wilczek {
1417b4d36f68SPiotr Wilczek struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
1418b4d36f68SPiotr Wilczek u32 nkeep = bh->inreq->length;
1419b4d36f68SPiotr Wilczek u32 nsend;
1420b4d36f68SPiotr Wilczek int rc;
1421b4d36f68SPiotr Wilczek
1422b4d36f68SPiotr Wilczek bh->state = BUF_STATE_EMPTY; /* For the first iteration */
1423b4d36f68SPiotr Wilczek fsg->common->usb_amount_left = nkeep + fsg->common->residue;
1424b4d36f68SPiotr Wilczek while (fsg->common->usb_amount_left > 0) {
1425b4d36f68SPiotr Wilczek
1426b4d36f68SPiotr Wilczek /* Wait for the next buffer to be free */
1427b4d36f68SPiotr Wilczek while (bh->state != BUF_STATE_EMPTY) {
1428b4d36f68SPiotr Wilczek rc = sleep_thread(fsg->common);
1429b4d36f68SPiotr Wilczek if (rc)
1430b4d36f68SPiotr Wilczek return rc;
1431b4d36f68SPiotr Wilczek }
1432b4d36f68SPiotr Wilczek
1433b4d36f68SPiotr Wilczek nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN);
1434b4d36f68SPiotr Wilczek memset(bh->buf + nkeep, 0, nsend - nkeep);
1435b4d36f68SPiotr Wilczek bh->inreq->length = nsend;
1436b4d36f68SPiotr Wilczek bh->inreq->zero = 0;
1437b4d36f68SPiotr Wilczek start_transfer(fsg, fsg->bulk_in, bh->inreq,
1438b4d36f68SPiotr Wilczek &bh->inreq_busy, &bh->state);
1439b4d36f68SPiotr Wilczek bh = fsg->common->next_buffhd_to_fill = bh->next;
1440b4d36f68SPiotr Wilczek fsg->common->usb_amount_left -= nsend;
1441b4d36f68SPiotr Wilczek nkeep = 0;
1442b4d36f68SPiotr Wilczek }
1443b4d36f68SPiotr Wilczek return 0;
1444b4d36f68SPiotr Wilczek }
1445b4d36f68SPiotr Wilczek
throw_away_data(struct fsg_common * common)1446b4d36f68SPiotr Wilczek static int throw_away_data(struct fsg_common *common)
1447b4d36f68SPiotr Wilczek {
1448b4d36f68SPiotr Wilczek struct fsg_buffhd *bh;
1449b4d36f68SPiotr Wilczek u32 amount;
1450b4d36f68SPiotr Wilczek int rc;
1451b4d36f68SPiotr Wilczek
1452b4d36f68SPiotr Wilczek for (bh = common->next_buffhd_to_drain;
1453b4d36f68SPiotr Wilczek bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0;
1454b4d36f68SPiotr Wilczek bh = common->next_buffhd_to_drain) {
1455b4d36f68SPiotr Wilczek
1456b4d36f68SPiotr Wilczek /* Throw away the data in a filled buffer */
1457b4d36f68SPiotr Wilczek if (bh->state == BUF_STATE_FULL) {
1458b4d36f68SPiotr Wilczek bh->state = BUF_STATE_EMPTY;
1459b4d36f68SPiotr Wilczek common->next_buffhd_to_drain = bh->next;
1460b4d36f68SPiotr Wilczek
1461b4d36f68SPiotr Wilczek /* A short packet or an error ends everything */
1462b4d36f68SPiotr Wilczek if (bh->outreq->actual != bh->outreq->length ||
1463b4d36f68SPiotr Wilczek bh->outreq->status != 0) {
1464b4d36f68SPiotr Wilczek raise_exception(common,
1465b4d36f68SPiotr Wilczek FSG_STATE_ABORT_BULK_OUT);
1466b4d36f68SPiotr Wilczek return -EINTR;
1467b4d36f68SPiotr Wilczek }
1468b4d36f68SPiotr Wilczek continue;
1469b4d36f68SPiotr Wilczek }
1470b4d36f68SPiotr Wilczek
1471b4d36f68SPiotr Wilczek /* Try to submit another request if we need one */
1472b4d36f68SPiotr Wilczek bh = common->next_buffhd_to_fill;
1473b4d36f68SPiotr Wilczek if (bh->state == BUF_STATE_EMPTY
1474b4d36f68SPiotr Wilczek && common->usb_amount_left > 0) {
1475b4d36f68SPiotr Wilczek amount = min(common->usb_amount_left, FSG_BUFLEN);
1476b4d36f68SPiotr Wilczek
1477b4d36f68SPiotr Wilczek /* amount is always divisible by 512, hence by
1478b4d36f68SPiotr Wilczek * the bulk-out maxpacket size */
1479b4d36f68SPiotr Wilczek bh->outreq->length = amount;
1480b4d36f68SPiotr Wilczek bh->bulk_out_intended_length = amount;
1481b4d36f68SPiotr Wilczek bh->outreq->short_not_ok = 1;
1482b4d36f68SPiotr Wilczek START_TRANSFER_OR(common, bulk_out, bh->outreq,
1483b4d36f68SPiotr Wilczek &bh->outreq_busy, &bh->state)
1484b4d36f68SPiotr Wilczek /* Don't know what to do if
1485b4d36f68SPiotr Wilczek * common->fsg is NULL */
1486b4d36f68SPiotr Wilczek return -EIO;
1487b4d36f68SPiotr Wilczek common->next_buffhd_to_fill = bh->next;
1488b4d36f68SPiotr Wilczek common->usb_amount_left -= amount;
1489b4d36f68SPiotr Wilczek continue;
1490b4d36f68SPiotr Wilczek }
1491b4d36f68SPiotr Wilczek
1492b4d36f68SPiotr Wilczek /* Otherwise wait for something to happen */
1493b4d36f68SPiotr Wilczek rc = sleep_thread(common);
1494b4d36f68SPiotr Wilczek if (rc)
1495b4d36f68SPiotr Wilczek return rc;
1496b4d36f68SPiotr Wilczek }
1497b4d36f68SPiotr Wilczek return 0;
1498b4d36f68SPiotr Wilczek }
1499b4d36f68SPiotr Wilczek
1500b4d36f68SPiotr Wilczek
finish_reply(struct fsg_common * common)1501b4d36f68SPiotr Wilczek static int finish_reply(struct fsg_common *common)
1502b4d36f68SPiotr Wilczek {
1503b4d36f68SPiotr Wilczek struct fsg_buffhd *bh = common->next_buffhd_to_fill;
1504b4d36f68SPiotr Wilczek int rc = 0;
1505b4d36f68SPiotr Wilczek
1506b4d36f68SPiotr Wilczek switch (common->data_dir) {
1507b4d36f68SPiotr Wilczek case DATA_DIR_NONE:
1508b4d36f68SPiotr Wilczek break; /* Nothing to send */
1509b4d36f68SPiotr Wilczek
1510b4d36f68SPiotr Wilczek /* If we don't know whether the host wants to read or write,
1511b4d36f68SPiotr Wilczek * this must be CB or CBI with an unknown command. We mustn't
1512b4d36f68SPiotr Wilczek * try to send or receive any data. So stall both bulk pipes
1513b4d36f68SPiotr Wilczek * if we can and wait for a reset. */
1514b4d36f68SPiotr Wilczek case DATA_DIR_UNKNOWN:
1515b4d36f68SPiotr Wilczek if (!common->can_stall) {
1516b4d36f68SPiotr Wilczek /* Nothing */
1517b4d36f68SPiotr Wilczek } else if (fsg_is_set(common)) {
1518b4d36f68SPiotr Wilczek fsg_set_halt(common->fsg, common->fsg->bulk_out);
1519b4d36f68SPiotr Wilczek rc = halt_bulk_in_endpoint(common->fsg);
1520b4d36f68SPiotr Wilczek } else {
1521b4d36f68SPiotr Wilczek /* Don't know what to do if common->fsg is NULL */
1522b4d36f68SPiotr Wilczek rc = -EIO;
1523b4d36f68SPiotr Wilczek }
1524b4d36f68SPiotr Wilczek break;
1525b4d36f68SPiotr Wilczek
1526b4d36f68SPiotr Wilczek /* All but the last buffer of data must have already been sent */
1527b4d36f68SPiotr Wilczek case DATA_DIR_TO_HOST:
1528b4d36f68SPiotr Wilczek if (common->data_size == 0) {
1529b4d36f68SPiotr Wilczek /* Nothing to send */
1530b4d36f68SPiotr Wilczek
1531b4d36f68SPiotr Wilczek /* If there's no residue, simply send the last buffer */
1532b4d36f68SPiotr Wilczek } else if (common->residue == 0) {
1533b4d36f68SPiotr Wilczek bh->inreq->zero = 0;
1534b4d36f68SPiotr Wilczek START_TRANSFER_OR(common, bulk_in, bh->inreq,
1535b4d36f68SPiotr Wilczek &bh->inreq_busy, &bh->state)
1536b4d36f68SPiotr Wilczek return -EIO;
1537b4d36f68SPiotr Wilczek common->next_buffhd_to_fill = bh->next;
1538b4d36f68SPiotr Wilczek
1539b4d36f68SPiotr Wilczek /* For Bulk-only, if we're allowed to stall then send the
1540b4d36f68SPiotr Wilczek * short packet and halt the bulk-in endpoint. If we can't
1541b4d36f68SPiotr Wilczek * stall, pad out the remaining data with 0's. */
1542b4d36f68SPiotr Wilczek } else if (common->can_stall) {
1543b4d36f68SPiotr Wilczek bh->inreq->zero = 1;
1544b4d36f68SPiotr Wilczek START_TRANSFER_OR(common, bulk_in, bh->inreq,
1545b4d36f68SPiotr Wilczek &bh->inreq_busy, &bh->state)
1546b4d36f68SPiotr Wilczek /* Don't know what to do if
1547b4d36f68SPiotr Wilczek * common->fsg is NULL */
1548b4d36f68SPiotr Wilczek rc = -EIO;
1549b4d36f68SPiotr Wilczek common->next_buffhd_to_fill = bh->next;
1550b4d36f68SPiotr Wilczek if (common->fsg)
1551b4d36f68SPiotr Wilczek rc = halt_bulk_in_endpoint(common->fsg);
1552b4d36f68SPiotr Wilczek } else if (fsg_is_set(common)) {
1553b4d36f68SPiotr Wilczek rc = pad_with_zeros(common->fsg);
1554b4d36f68SPiotr Wilczek } else {
1555b4d36f68SPiotr Wilczek /* Don't know what to do if common->fsg is NULL */
1556b4d36f68SPiotr Wilczek rc = -EIO;
1557b4d36f68SPiotr Wilczek }
1558b4d36f68SPiotr Wilczek break;
1559b4d36f68SPiotr Wilczek
1560b4d36f68SPiotr Wilczek /* We have processed all we want from the data the host has sent.
1561b4d36f68SPiotr Wilczek * There may still be outstanding bulk-out requests. */
1562b4d36f68SPiotr Wilczek case DATA_DIR_FROM_HOST:
1563b4d36f68SPiotr Wilczek if (common->residue == 0) {
1564b4d36f68SPiotr Wilczek /* Nothing to receive */
1565b4d36f68SPiotr Wilczek
1566b4d36f68SPiotr Wilczek /* Did the host stop sending unexpectedly early? */
1567b4d36f68SPiotr Wilczek } else if (common->short_packet_received) {
1568b4d36f68SPiotr Wilczek raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1569b4d36f68SPiotr Wilczek rc = -EINTR;
1570b4d36f68SPiotr Wilczek
1571b4d36f68SPiotr Wilczek /* We haven't processed all the incoming data. Even though
1572b4d36f68SPiotr Wilczek * we may be allowed to stall, doing so would cause a race.
1573b4d36f68SPiotr Wilczek * The controller may already have ACK'ed all the remaining
1574b4d36f68SPiotr Wilczek * bulk-out packets, in which case the host wouldn't see a
1575b4d36f68SPiotr Wilczek * STALL. Not realizing the endpoint was halted, it wouldn't
1576b4d36f68SPiotr Wilczek * clear the halt -- leading to problems later on. */
1577b4d36f68SPiotr Wilczek #if 0
1578b4d36f68SPiotr Wilczek } else if (common->can_stall) {
1579b4d36f68SPiotr Wilczek if (fsg_is_set(common))
1580b4d36f68SPiotr Wilczek fsg_set_halt(common->fsg,
1581b4d36f68SPiotr Wilczek common->fsg->bulk_out);
1582b4d36f68SPiotr Wilczek raise_exception(common, FSG_STATE_ABORT_BULK_OUT);
1583b4d36f68SPiotr Wilczek rc = -EINTR;
1584b4d36f68SPiotr Wilczek #endif
1585b4d36f68SPiotr Wilczek
1586b4d36f68SPiotr Wilczek /* We can't stall. Read in the excess data and throw it
1587b4d36f68SPiotr Wilczek * all away. */
1588b4d36f68SPiotr Wilczek } else {
1589b4d36f68SPiotr Wilczek rc = throw_away_data(common);
1590b4d36f68SPiotr Wilczek }
1591b4d36f68SPiotr Wilczek break;
1592b4d36f68SPiotr Wilczek }
1593b4d36f68SPiotr Wilczek return rc;
1594b4d36f68SPiotr Wilczek }
1595b4d36f68SPiotr Wilczek
1596b4d36f68SPiotr Wilczek
send_status(struct fsg_common * common)1597b4d36f68SPiotr Wilczek static int send_status(struct fsg_common *common)
1598b4d36f68SPiotr Wilczek {
1599b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1600b4d36f68SPiotr Wilczek struct fsg_buffhd *bh;
1601b4d36f68SPiotr Wilczek struct bulk_cs_wrap *csw;
1602b4d36f68SPiotr Wilczek int rc;
1603b4d36f68SPiotr Wilczek u8 status = USB_STATUS_PASS;
1604b4d36f68SPiotr Wilczek u32 sd, sdinfo = 0;
1605b4d36f68SPiotr Wilczek
1606b4d36f68SPiotr Wilczek /* Wait for the next buffer to become available */
1607b4d36f68SPiotr Wilczek bh = common->next_buffhd_to_fill;
1608b4d36f68SPiotr Wilczek while (bh->state != BUF_STATE_EMPTY) {
1609b4d36f68SPiotr Wilczek rc = sleep_thread(common);
1610b4d36f68SPiotr Wilczek if (rc)
1611b4d36f68SPiotr Wilczek return rc;
1612b4d36f68SPiotr Wilczek }
1613b4d36f68SPiotr Wilczek
1614b4d36f68SPiotr Wilczek if (curlun)
1615b4d36f68SPiotr Wilczek sd = curlun->sense_data;
1616b4d36f68SPiotr Wilczek else if (common->bad_lun_okay)
1617b4d36f68SPiotr Wilczek sd = SS_NO_SENSE;
1618b4d36f68SPiotr Wilczek else
1619b4d36f68SPiotr Wilczek sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
1620b4d36f68SPiotr Wilczek
1621b4d36f68SPiotr Wilczek if (common->phase_error) {
1622b4d36f68SPiotr Wilczek DBG(common, "sending phase-error status\n");
1623b4d36f68SPiotr Wilczek status = USB_STATUS_PHASE_ERROR;
1624b4d36f68SPiotr Wilczek sd = SS_INVALID_COMMAND;
1625b4d36f68SPiotr Wilczek } else if (sd != SS_NO_SENSE) {
1626b4d36f68SPiotr Wilczek DBG(common, "sending command-failure status\n");
1627b4d36f68SPiotr Wilczek status = USB_STATUS_FAIL;
1628b4d36f68SPiotr Wilczek VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
1629b4d36f68SPiotr Wilczek " info x%x\n",
1630b4d36f68SPiotr Wilczek SK(sd), ASC(sd), ASCQ(sd), sdinfo);
1631b4d36f68SPiotr Wilczek }
1632b4d36f68SPiotr Wilczek
1633b4d36f68SPiotr Wilczek /* Store and send the Bulk-only CSW */
1634b4d36f68SPiotr Wilczek csw = (void *)bh->buf;
1635b4d36f68SPiotr Wilczek
1636b4d36f68SPiotr Wilczek csw->Signature = cpu_to_le32(USB_BULK_CS_SIG);
1637b4d36f68SPiotr Wilczek csw->Tag = common->tag;
1638b4d36f68SPiotr Wilczek csw->Residue = cpu_to_le32(common->residue);
1639b4d36f68SPiotr Wilczek csw->Status = status;
1640b4d36f68SPiotr Wilczek
1641b4d36f68SPiotr Wilczek bh->inreq->length = USB_BULK_CS_WRAP_LEN;
1642b4d36f68SPiotr Wilczek bh->inreq->zero = 0;
1643b4d36f68SPiotr Wilczek START_TRANSFER_OR(common, bulk_in, bh->inreq,
1644b4d36f68SPiotr Wilczek &bh->inreq_busy, &bh->state)
1645b4d36f68SPiotr Wilczek /* Don't know what to do if common->fsg is NULL */
1646b4d36f68SPiotr Wilczek return -EIO;
1647b4d36f68SPiotr Wilczek
1648b4d36f68SPiotr Wilczek common->next_buffhd_to_fill = bh->next;
1649b4d36f68SPiotr Wilczek return 0;
1650b4d36f68SPiotr Wilczek }
1651b4d36f68SPiotr Wilczek
1652b4d36f68SPiotr Wilczek
1653b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
1654b4d36f68SPiotr Wilczek
1655b4d36f68SPiotr Wilczek /* Check whether the command is properly formed and whether its data size
1656b4d36f68SPiotr Wilczek * and direction agree with the values we already have. */
check_command(struct fsg_common * common,int cmnd_size,enum data_direction data_dir,unsigned int mask,int needs_medium,const char * name)1657b4d36f68SPiotr Wilczek static int check_command(struct fsg_common *common, int cmnd_size,
1658b4d36f68SPiotr Wilczek enum data_direction data_dir, unsigned int mask,
1659b4d36f68SPiotr Wilczek int needs_medium, const char *name)
1660b4d36f68SPiotr Wilczek {
1661b4d36f68SPiotr Wilczek int i;
1662b4d36f68SPiotr Wilczek int lun = common->cmnd[1] >> 5;
1663b4d36f68SPiotr Wilczek static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1664b4d36f68SPiotr Wilczek char hdlen[20];
1665b4d36f68SPiotr Wilczek struct fsg_lun *curlun;
1666b4d36f68SPiotr Wilczek
1667b4d36f68SPiotr Wilczek hdlen[0] = 0;
1668b4d36f68SPiotr Wilczek if (common->data_dir != DATA_DIR_UNKNOWN)
1669b4d36f68SPiotr Wilczek sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],
1670b4d36f68SPiotr Wilczek common->data_size);
1671b4d36f68SPiotr Wilczek VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
1672b4d36f68SPiotr Wilczek name, cmnd_size, dirletter[(int) data_dir],
1673b4d36f68SPiotr Wilczek common->data_size_from_cmnd, common->cmnd_size, hdlen);
1674b4d36f68SPiotr Wilczek
1675b4d36f68SPiotr Wilczek /* We can't reply at all until we know the correct data direction
1676b4d36f68SPiotr Wilczek * and size. */
1677b4d36f68SPiotr Wilczek if (common->data_size_from_cmnd == 0)
1678b4d36f68SPiotr Wilczek data_dir = DATA_DIR_NONE;
1679b4d36f68SPiotr Wilczek if (common->data_size < common->data_size_from_cmnd) {
1680b4d36f68SPiotr Wilczek /* Host data size < Device data size is a phase error.
1681b4d36f68SPiotr Wilczek * Carry out the command, but only transfer as much as
1682b4d36f68SPiotr Wilczek * we are allowed. */
1683b4d36f68SPiotr Wilczek common->data_size_from_cmnd = common->data_size;
1684b4d36f68SPiotr Wilczek common->phase_error = 1;
1685b4d36f68SPiotr Wilczek }
1686b4d36f68SPiotr Wilczek common->residue = common->data_size;
1687b4d36f68SPiotr Wilczek common->usb_amount_left = common->data_size;
1688b4d36f68SPiotr Wilczek
1689b4d36f68SPiotr Wilczek /* Conflicting data directions is a phase error */
1690b4d36f68SPiotr Wilczek if (common->data_dir != data_dir
1691b4d36f68SPiotr Wilczek && common->data_size_from_cmnd > 0) {
1692b4d36f68SPiotr Wilczek common->phase_error = 1;
1693b4d36f68SPiotr Wilczek return -EINVAL;
1694b4d36f68SPiotr Wilczek }
1695b4d36f68SPiotr Wilczek
1696b4d36f68SPiotr Wilczek /* Verify the length of the command itself */
1697b4d36f68SPiotr Wilczek if (cmnd_size != common->cmnd_size) {
1698b4d36f68SPiotr Wilczek
1699b4d36f68SPiotr Wilczek /* Special case workaround: There are plenty of buggy SCSI
1700b4d36f68SPiotr Wilczek * implementations. Many have issues with cbw->Length
1701b4d36f68SPiotr Wilczek * field passing a wrong command size. For those cases we
1702b4d36f68SPiotr Wilczek * always try to work around the problem by using the length
1703b4d36f68SPiotr Wilczek * sent by the host side provided it is at least as large
1704b4d36f68SPiotr Wilczek * as the correct command length.
1705b4d36f68SPiotr Wilczek * Examples of such cases would be MS-Windows, which issues
1706b4d36f68SPiotr Wilczek * REQUEST SENSE with cbw->Length == 12 where it should
1707b4d36f68SPiotr Wilczek * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
1708b4d36f68SPiotr Wilczek * REQUEST SENSE with cbw->Length == 10 where it should
1709b4d36f68SPiotr Wilczek * be 6 as well.
1710b4d36f68SPiotr Wilczek */
1711b4d36f68SPiotr Wilczek if (cmnd_size <= common->cmnd_size) {
1712b4d36f68SPiotr Wilczek DBG(common, "%s is buggy! Expected length %d "
1713b4d36f68SPiotr Wilczek "but we got %d\n", name,
1714b4d36f68SPiotr Wilczek cmnd_size, common->cmnd_size);
1715b4d36f68SPiotr Wilczek cmnd_size = common->cmnd_size;
1716b4d36f68SPiotr Wilczek } else {
1717b4d36f68SPiotr Wilczek common->phase_error = 1;
1718b4d36f68SPiotr Wilczek return -EINVAL;
1719b4d36f68SPiotr Wilczek }
1720b4d36f68SPiotr Wilczek }
1721b4d36f68SPiotr Wilczek
1722b4d36f68SPiotr Wilczek /* Check that the LUN values are consistent */
1723b4d36f68SPiotr Wilczek if (common->lun != lun)
1724b4d36f68SPiotr Wilczek DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n",
1725b4d36f68SPiotr Wilczek common->lun, lun);
1726b4d36f68SPiotr Wilczek
1727b4d36f68SPiotr Wilczek /* Check the LUN */
172848cdfa2fSHeinrich Schuchardt if (common->lun < common->nluns) {
1729b4d36f68SPiotr Wilczek curlun = &common->luns[common->lun];
1730b4d36f68SPiotr Wilczek if (common->cmnd[0] != SC_REQUEST_SENSE) {
1731b4d36f68SPiotr Wilczek curlun->sense_data = SS_NO_SENSE;
1732b4d36f68SPiotr Wilczek curlun->info_valid = 0;
1733b4d36f68SPiotr Wilczek }
1734b4d36f68SPiotr Wilczek } else {
1735b4d36f68SPiotr Wilczek curlun = NULL;
1736b4d36f68SPiotr Wilczek common->bad_lun_okay = 0;
1737b4d36f68SPiotr Wilczek
1738b4d36f68SPiotr Wilczek /* INQUIRY and REQUEST SENSE commands are explicitly allowed
1739b4d36f68SPiotr Wilczek * to use unsupported LUNs; all others may not. */
1740b4d36f68SPiotr Wilczek if (common->cmnd[0] != SC_INQUIRY &&
1741b4d36f68SPiotr Wilczek common->cmnd[0] != SC_REQUEST_SENSE) {
1742b4d36f68SPiotr Wilczek DBG(common, "unsupported LUN %d\n", common->lun);
1743b4d36f68SPiotr Wilczek return -EINVAL;
1744b4d36f68SPiotr Wilczek }
1745b4d36f68SPiotr Wilczek }
1746b4d36f68SPiotr Wilczek #if 0
1747b4d36f68SPiotr Wilczek /* If a unit attention condition exists, only INQUIRY and
1748b4d36f68SPiotr Wilczek * REQUEST SENSE commands are allowed; anything else must fail. */
1749b4d36f68SPiotr Wilczek if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
1750b4d36f68SPiotr Wilczek common->cmnd[0] != SC_INQUIRY &&
1751b4d36f68SPiotr Wilczek common->cmnd[0] != SC_REQUEST_SENSE) {
1752b4d36f68SPiotr Wilczek curlun->sense_data = curlun->unit_attention_data;
1753b4d36f68SPiotr Wilczek curlun->unit_attention_data = SS_NO_SENSE;
1754b4d36f68SPiotr Wilczek return -EINVAL;
1755b4d36f68SPiotr Wilczek }
1756b4d36f68SPiotr Wilczek #endif
1757b4d36f68SPiotr Wilczek /* Check that only command bytes listed in the mask are non-zero */
1758b4d36f68SPiotr Wilczek common->cmnd[1] &= 0x1f; /* Mask away the LUN */
1759b4d36f68SPiotr Wilczek for (i = 1; i < cmnd_size; ++i) {
1760b4d36f68SPiotr Wilczek if (common->cmnd[i] && !(mask & (1 << i))) {
1761b4d36f68SPiotr Wilczek if (curlun)
1762b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1763b4d36f68SPiotr Wilczek return -EINVAL;
1764b4d36f68SPiotr Wilczek }
1765b4d36f68SPiotr Wilczek }
1766b4d36f68SPiotr Wilczek
1767b4d36f68SPiotr Wilczek return 0;
1768b4d36f68SPiotr Wilczek }
1769b4d36f68SPiotr Wilczek
1770b4d36f68SPiotr Wilczek
do_scsi_command(struct fsg_common * common)1771b4d36f68SPiotr Wilczek static int do_scsi_command(struct fsg_common *common)
1772b4d36f68SPiotr Wilczek {
1773b4d36f68SPiotr Wilczek struct fsg_buffhd *bh;
1774b4d36f68SPiotr Wilczek int rc;
1775b4d36f68SPiotr Wilczek int reply = -EINVAL;
1776b4d36f68SPiotr Wilczek int i;
1777b4d36f68SPiotr Wilczek static char unknown[16];
1778b4d36f68SPiotr Wilczek struct fsg_lun *curlun = &common->luns[common->lun];
1779b4d36f68SPiotr Wilczek
1780b4d36f68SPiotr Wilczek dump_cdb(common);
1781b4d36f68SPiotr Wilczek
1782b4d36f68SPiotr Wilczek /* Wait for the next buffer to become available for data or status */
1783b4d36f68SPiotr Wilczek bh = common->next_buffhd_to_fill;
1784b4d36f68SPiotr Wilczek common->next_buffhd_to_drain = bh;
1785b4d36f68SPiotr Wilczek while (bh->state != BUF_STATE_EMPTY) {
1786b4d36f68SPiotr Wilczek rc = sleep_thread(common);
1787b4d36f68SPiotr Wilczek if (rc)
1788b4d36f68SPiotr Wilczek return rc;
1789b4d36f68SPiotr Wilczek }
1790b4d36f68SPiotr Wilczek common->phase_error = 0;
1791b4d36f68SPiotr Wilczek common->short_packet_received = 0;
1792b4d36f68SPiotr Wilczek
1793b4d36f68SPiotr Wilczek down_read(&common->filesem); /* We're using the backing file */
1794b4d36f68SPiotr Wilczek switch (common->cmnd[0]) {
1795b4d36f68SPiotr Wilczek
1796b4d36f68SPiotr Wilczek case SC_INQUIRY:
1797b4d36f68SPiotr Wilczek common->data_size_from_cmnd = common->cmnd[4];
1798b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_TO_HOST,
1799b4d36f68SPiotr Wilczek (1<<4), 0,
1800b4d36f68SPiotr Wilczek "INQUIRY");
1801b4d36f68SPiotr Wilczek if (reply == 0)
1802b4d36f68SPiotr Wilczek reply = do_inquiry(common, bh);
1803b4d36f68SPiotr Wilczek break;
1804b4d36f68SPiotr Wilczek
1805b4d36f68SPiotr Wilczek case SC_MODE_SELECT_6:
1806b4d36f68SPiotr Wilczek common->data_size_from_cmnd = common->cmnd[4];
1807b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_FROM_HOST,
1808b4d36f68SPiotr Wilczek (1<<1) | (1<<4), 0,
1809b4d36f68SPiotr Wilczek "MODE SELECT(6)");
1810b4d36f68SPiotr Wilczek if (reply == 0)
1811b4d36f68SPiotr Wilczek reply = do_mode_select(common, bh);
1812b4d36f68SPiotr Wilczek break;
1813b4d36f68SPiotr Wilczek
1814b4d36f68SPiotr Wilczek case SC_MODE_SELECT_10:
1815b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1816b4d36f68SPiotr Wilczek get_unaligned_be16(&common->cmnd[7]);
1817b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_FROM_HOST,
1818b4d36f68SPiotr Wilczek (1<<1) | (3<<7), 0,
1819b4d36f68SPiotr Wilczek "MODE SELECT(10)");
1820b4d36f68SPiotr Wilczek if (reply == 0)
1821b4d36f68SPiotr Wilczek reply = do_mode_select(common, bh);
1822b4d36f68SPiotr Wilczek break;
1823b4d36f68SPiotr Wilczek
1824b4d36f68SPiotr Wilczek case SC_MODE_SENSE_6:
1825b4d36f68SPiotr Wilczek common->data_size_from_cmnd = common->cmnd[4];
1826b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_TO_HOST,
1827b4d36f68SPiotr Wilczek (1<<1) | (1<<2) | (1<<4), 0,
1828b4d36f68SPiotr Wilczek "MODE SENSE(6)");
1829b4d36f68SPiotr Wilczek if (reply == 0)
1830b4d36f68SPiotr Wilczek reply = do_mode_sense(common, bh);
1831b4d36f68SPiotr Wilczek break;
1832b4d36f68SPiotr Wilczek
1833b4d36f68SPiotr Wilczek case SC_MODE_SENSE_10:
1834b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1835b4d36f68SPiotr Wilczek get_unaligned_be16(&common->cmnd[7]);
1836b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_TO_HOST,
1837b4d36f68SPiotr Wilczek (1<<1) | (1<<2) | (3<<7), 0,
1838b4d36f68SPiotr Wilczek "MODE SENSE(10)");
1839b4d36f68SPiotr Wilczek if (reply == 0)
1840b4d36f68SPiotr Wilczek reply = do_mode_sense(common, bh);
1841b4d36f68SPiotr Wilczek break;
1842b4d36f68SPiotr Wilczek
1843b4d36f68SPiotr Wilczek case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
1844b4d36f68SPiotr Wilczek common->data_size_from_cmnd = 0;
1845b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_NONE,
1846b4d36f68SPiotr Wilczek (1<<4), 0,
1847b4d36f68SPiotr Wilczek "PREVENT-ALLOW MEDIUM REMOVAL");
1848b4d36f68SPiotr Wilczek if (reply == 0)
1849b4d36f68SPiotr Wilczek reply = do_prevent_allow(common);
1850b4d36f68SPiotr Wilczek break;
1851b4d36f68SPiotr Wilczek
1852b4d36f68SPiotr Wilczek case SC_READ_6:
1853b4d36f68SPiotr Wilczek i = common->cmnd[4];
1854b4d36f68SPiotr Wilczek common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1855b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_TO_HOST,
1856b4d36f68SPiotr Wilczek (7<<1) | (1<<4), 1,
1857b4d36f68SPiotr Wilczek "READ(6)");
1858b4d36f68SPiotr Wilczek if (reply == 0)
1859b4d36f68SPiotr Wilczek reply = do_read(common);
1860b4d36f68SPiotr Wilczek break;
1861b4d36f68SPiotr Wilczek
1862b4d36f68SPiotr Wilczek case SC_READ_10:
1863b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1864b4d36f68SPiotr Wilczek get_unaligned_be16(&common->cmnd[7]) << 9;
1865b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_TO_HOST,
1866b4d36f68SPiotr Wilczek (1<<1) | (0xf<<2) | (3<<7), 1,
1867b4d36f68SPiotr Wilczek "READ(10)");
1868b4d36f68SPiotr Wilczek if (reply == 0)
1869b4d36f68SPiotr Wilczek reply = do_read(common);
1870b4d36f68SPiotr Wilczek break;
1871b4d36f68SPiotr Wilczek
1872b4d36f68SPiotr Wilczek case SC_READ_12:
1873b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1874b4d36f68SPiotr Wilczek get_unaligned_be32(&common->cmnd[6]) << 9;
1875b4d36f68SPiotr Wilczek reply = check_command(common, 12, DATA_DIR_TO_HOST,
1876b4d36f68SPiotr Wilczek (1<<1) | (0xf<<2) | (0xf<<6), 1,
1877b4d36f68SPiotr Wilczek "READ(12)");
1878b4d36f68SPiotr Wilczek if (reply == 0)
1879b4d36f68SPiotr Wilczek reply = do_read(common);
1880b4d36f68SPiotr Wilczek break;
1881b4d36f68SPiotr Wilczek
1882b4d36f68SPiotr Wilczek case SC_READ_CAPACITY:
1883b4d36f68SPiotr Wilczek common->data_size_from_cmnd = 8;
1884b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_TO_HOST,
1885b4d36f68SPiotr Wilczek (0xf<<2) | (1<<8), 1,
1886b4d36f68SPiotr Wilczek "READ CAPACITY");
1887b4d36f68SPiotr Wilczek if (reply == 0)
1888b4d36f68SPiotr Wilczek reply = do_read_capacity(common, bh);
1889b4d36f68SPiotr Wilczek break;
1890b4d36f68SPiotr Wilczek
1891b4d36f68SPiotr Wilczek case SC_READ_HEADER:
1892b4d36f68SPiotr Wilczek if (!common->luns[common->lun].cdrom)
1893b4d36f68SPiotr Wilczek goto unknown_cmnd;
1894b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1895b4d36f68SPiotr Wilczek get_unaligned_be16(&common->cmnd[7]);
1896b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_TO_HOST,
1897b4d36f68SPiotr Wilczek (3<<7) | (0x1f<<1), 1,
1898b4d36f68SPiotr Wilczek "READ HEADER");
1899b4d36f68SPiotr Wilczek if (reply == 0)
1900b4d36f68SPiotr Wilczek reply = do_read_header(common, bh);
1901b4d36f68SPiotr Wilczek break;
1902b4d36f68SPiotr Wilczek
1903b4d36f68SPiotr Wilczek case SC_READ_TOC:
1904b4d36f68SPiotr Wilczek if (!common->luns[common->lun].cdrom)
1905b4d36f68SPiotr Wilczek goto unknown_cmnd;
1906b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1907b4d36f68SPiotr Wilczek get_unaligned_be16(&common->cmnd[7]);
1908b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_TO_HOST,
1909b4d36f68SPiotr Wilczek (7<<6) | (1<<1), 1,
1910b4d36f68SPiotr Wilczek "READ TOC");
1911b4d36f68SPiotr Wilczek if (reply == 0)
1912b4d36f68SPiotr Wilczek reply = do_read_toc(common, bh);
1913b4d36f68SPiotr Wilczek break;
1914b4d36f68SPiotr Wilczek
1915b4d36f68SPiotr Wilczek case SC_READ_FORMAT_CAPACITIES:
1916b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1917b4d36f68SPiotr Wilczek get_unaligned_be16(&common->cmnd[7]);
1918b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_TO_HOST,
1919b4d36f68SPiotr Wilczek (3<<7), 1,
1920b4d36f68SPiotr Wilczek "READ FORMAT CAPACITIES");
1921b4d36f68SPiotr Wilczek if (reply == 0)
1922b4d36f68SPiotr Wilczek reply = do_read_format_capacities(common, bh);
1923b4d36f68SPiotr Wilczek break;
1924b4d36f68SPiotr Wilczek
1925b4d36f68SPiotr Wilczek case SC_REQUEST_SENSE:
1926b4d36f68SPiotr Wilczek common->data_size_from_cmnd = common->cmnd[4];
1927b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_TO_HOST,
1928b4d36f68SPiotr Wilczek (1<<4), 0,
1929b4d36f68SPiotr Wilczek "REQUEST SENSE");
1930b4d36f68SPiotr Wilczek if (reply == 0)
1931b4d36f68SPiotr Wilczek reply = do_request_sense(common, bh);
1932b4d36f68SPiotr Wilczek break;
1933b4d36f68SPiotr Wilczek
1934b4d36f68SPiotr Wilczek case SC_START_STOP_UNIT:
1935b4d36f68SPiotr Wilczek common->data_size_from_cmnd = 0;
1936b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_NONE,
1937b4d36f68SPiotr Wilczek (1<<1) | (1<<4), 0,
1938b4d36f68SPiotr Wilczek "START-STOP UNIT");
1939b4d36f68SPiotr Wilczek if (reply == 0)
1940b4d36f68SPiotr Wilczek reply = do_start_stop(common);
1941b4d36f68SPiotr Wilczek break;
1942b4d36f68SPiotr Wilczek
1943b4d36f68SPiotr Wilczek case SC_SYNCHRONIZE_CACHE:
1944b4d36f68SPiotr Wilczek common->data_size_from_cmnd = 0;
1945b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_NONE,
1946b4d36f68SPiotr Wilczek (0xf<<2) | (3<<7), 1,
1947b4d36f68SPiotr Wilczek "SYNCHRONIZE CACHE");
1948b4d36f68SPiotr Wilczek if (reply == 0)
1949b4d36f68SPiotr Wilczek reply = do_synchronize_cache(common);
1950b4d36f68SPiotr Wilczek break;
1951b4d36f68SPiotr Wilczek
1952b4d36f68SPiotr Wilczek case SC_TEST_UNIT_READY:
1953b4d36f68SPiotr Wilczek common->data_size_from_cmnd = 0;
1954b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_NONE,
1955b4d36f68SPiotr Wilczek 0, 1,
1956b4d36f68SPiotr Wilczek "TEST UNIT READY");
1957b4d36f68SPiotr Wilczek break;
1958b4d36f68SPiotr Wilczek
1959b4d36f68SPiotr Wilczek /* Although optional, this command is used by MS-Windows. We
1960b4d36f68SPiotr Wilczek * support a minimal version: BytChk must be 0. */
1961b4d36f68SPiotr Wilczek case SC_VERIFY:
1962b4d36f68SPiotr Wilczek common->data_size_from_cmnd = 0;
1963b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_NONE,
1964b4d36f68SPiotr Wilczek (1<<1) | (0xf<<2) | (3<<7), 1,
1965b4d36f68SPiotr Wilczek "VERIFY");
1966b4d36f68SPiotr Wilczek if (reply == 0)
1967b4d36f68SPiotr Wilczek reply = do_verify(common);
1968b4d36f68SPiotr Wilczek break;
1969b4d36f68SPiotr Wilczek
1970b4d36f68SPiotr Wilczek case SC_WRITE_6:
1971b4d36f68SPiotr Wilczek i = common->cmnd[4];
1972b4d36f68SPiotr Wilczek common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
1973b4d36f68SPiotr Wilczek reply = check_command(common, 6, DATA_DIR_FROM_HOST,
1974b4d36f68SPiotr Wilczek (7<<1) | (1<<4), 1,
1975b4d36f68SPiotr Wilczek "WRITE(6)");
1976b4d36f68SPiotr Wilczek if (reply == 0)
1977b4d36f68SPiotr Wilczek reply = do_write(common);
1978b4d36f68SPiotr Wilczek break;
1979b4d36f68SPiotr Wilczek
1980b4d36f68SPiotr Wilczek case SC_WRITE_10:
1981b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1982b4d36f68SPiotr Wilczek get_unaligned_be16(&common->cmnd[7]) << 9;
1983b4d36f68SPiotr Wilczek reply = check_command(common, 10, DATA_DIR_FROM_HOST,
1984b4d36f68SPiotr Wilczek (1<<1) | (0xf<<2) | (3<<7), 1,
1985b4d36f68SPiotr Wilczek "WRITE(10)");
1986b4d36f68SPiotr Wilczek if (reply == 0)
1987b4d36f68SPiotr Wilczek reply = do_write(common);
1988b4d36f68SPiotr Wilczek break;
1989b4d36f68SPiotr Wilczek
1990b4d36f68SPiotr Wilczek case SC_WRITE_12:
1991b4d36f68SPiotr Wilczek common->data_size_from_cmnd =
1992b4d36f68SPiotr Wilczek get_unaligned_be32(&common->cmnd[6]) << 9;
1993b4d36f68SPiotr Wilczek reply = check_command(common, 12, DATA_DIR_FROM_HOST,
1994b4d36f68SPiotr Wilczek (1<<1) | (0xf<<2) | (0xf<<6), 1,
1995b4d36f68SPiotr Wilczek "WRITE(12)");
1996b4d36f68SPiotr Wilczek if (reply == 0)
1997b4d36f68SPiotr Wilczek reply = do_write(common);
1998b4d36f68SPiotr Wilczek break;
1999b4d36f68SPiotr Wilczek
2000b4d36f68SPiotr Wilczek /* Some mandatory commands that we recognize but don't implement.
2001b4d36f68SPiotr Wilczek * They don't mean much in this setting. It's left as an exercise
2002b4d36f68SPiotr Wilczek * for anyone interested to implement RESERVE and RELEASE in terms
2003b4d36f68SPiotr Wilczek * of Posix locks. */
2004b4d36f68SPiotr Wilczek case SC_FORMAT_UNIT:
2005b4d36f68SPiotr Wilczek case SC_RELEASE:
2006b4d36f68SPiotr Wilczek case SC_RESERVE:
2007b4d36f68SPiotr Wilczek case SC_SEND_DIAGNOSTIC:
2008b4d36f68SPiotr Wilczek /* Fall through */
2009b4d36f68SPiotr Wilczek
2010b4d36f68SPiotr Wilczek default:
2011b4d36f68SPiotr Wilczek unknown_cmnd:
2012b4d36f68SPiotr Wilczek common->data_size_from_cmnd = 0;
2013b4d36f68SPiotr Wilczek sprintf(unknown, "Unknown x%02x", common->cmnd[0]);
2014b4d36f68SPiotr Wilczek reply = check_command(common, common->cmnd_size,
2015b4d36f68SPiotr Wilczek DATA_DIR_UNKNOWN, 0xff, 0, unknown);
2016b4d36f68SPiotr Wilczek if (reply == 0) {
2017b4d36f68SPiotr Wilczek curlun->sense_data = SS_INVALID_COMMAND;
2018b4d36f68SPiotr Wilczek reply = -EINVAL;
2019b4d36f68SPiotr Wilczek }
2020b4d36f68SPiotr Wilczek break;
2021b4d36f68SPiotr Wilczek }
2022b4d36f68SPiotr Wilczek up_read(&common->filesem);
2023b4d36f68SPiotr Wilczek
2024b4d36f68SPiotr Wilczek if (reply == -EINTR)
2025b4d36f68SPiotr Wilczek return -EINTR;
2026b4d36f68SPiotr Wilczek
2027b4d36f68SPiotr Wilczek /* Set up the single reply buffer for finish_reply() */
2028b4d36f68SPiotr Wilczek if (reply == -EINVAL)
2029b4d36f68SPiotr Wilczek reply = 0; /* Error reply length */
2030b4d36f68SPiotr Wilczek if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {
2031b4d36f68SPiotr Wilczek reply = min((u32) reply, common->data_size_from_cmnd);
2032b4d36f68SPiotr Wilczek bh->inreq->length = reply;
2033b4d36f68SPiotr Wilczek bh->state = BUF_STATE_FULL;
2034b4d36f68SPiotr Wilczek common->residue -= reply;
2035b4d36f68SPiotr Wilczek } /* Otherwise it's already set */
2036b4d36f68SPiotr Wilczek
2037b4d36f68SPiotr Wilczek return 0;
2038b4d36f68SPiotr Wilczek }
2039b4d36f68SPiotr Wilczek
2040b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
2041b4d36f68SPiotr Wilczek
received_cbw(struct fsg_dev * fsg,struct fsg_buffhd * bh)2042b4d36f68SPiotr Wilczek static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2043b4d36f68SPiotr Wilczek {
2044b4d36f68SPiotr Wilczek struct usb_request *req = bh->outreq;
2045b4d36f68SPiotr Wilczek struct fsg_bulk_cb_wrap *cbw = req->buf;
2046b4d36f68SPiotr Wilczek struct fsg_common *common = fsg->common;
2047b4d36f68SPiotr Wilczek
2048b4d36f68SPiotr Wilczek /* Was this a real packet? Should it be ignored? */
2049b4d36f68SPiotr Wilczek if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
2050b4d36f68SPiotr Wilczek return -EINVAL;
2051b4d36f68SPiotr Wilczek
2052b4d36f68SPiotr Wilczek /* Is the CBW valid? */
2053b4d36f68SPiotr Wilczek if (req->actual != USB_BULK_CB_WRAP_LEN ||
2054b4d36f68SPiotr Wilczek cbw->Signature != cpu_to_le32(
2055b4d36f68SPiotr Wilczek USB_BULK_CB_SIG)) {
2056b4d36f68SPiotr Wilczek DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
2057b4d36f68SPiotr Wilczek req->actual,
2058b4d36f68SPiotr Wilczek le32_to_cpu(cbw->Signature));
2059b4d36f68SPiotr Wilczek
2060b4d36f68SPiotr Wilczek /* The Bulk-only spec says we MUST stall the IN endpoint
2061b4d36f68SPiotr Wilczek * (6.6.1), so it's unavoidable. It also says we must
2062b4d36f68SPiotr Wilczek * retain this state until the next reset, but there's
2063b4d36f68SPiotr Wilczek * no way to tell the controller driver it should ignore
2064b4d36f68SPiotr Wilczek * Clear-Feature(HALT) requests.
2065b4d36f68SPiotr Wilczek *
2066b4d36f68SPiotr Wilczek * We aren't required to halt the OUT endpoint; instead
2067b4d36f68SPiotr Wilczek * we can simply accept and discard any data received
2068b4d36f68SPiotr Wilczek * until the next reset. */
2069b4d36f68SPiotr Wilczek wedge_bulk_in_endpoint(fsg);
20705ac73f68SBryan O'Donoghue generic_set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2071b4d36f68SPiotr Wilczek return -EINVAL;
2072b4d36f68SPiotr Wilczek }
2073b4d36f68SPiotr Wilczek
2074b4d36f68SPiotr Wilczek /* Is the CBW meaningful? */
2075b4d36f68SPiotr Wilczek if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
2076b4d36f68SPiotr Wilczek cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
2077b4d36f68SPiotr Wilczek DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
2078b4d36f68SPiotr Wilczek "cmdlen %u\n",
2079b4d36f68SPiotr Wilczek cbw->Lun, cbw->Flags, cbw->Length);
2080b4d36f68SPiotr Wilczek
2081b4d36f68SPiotr Wilczek /* We can do anything we want here, so let's stall the
2082b4d36f68SPiotr Wilczek * bulk pipes if we are allowed to. */
2083b4d36f68SPiotr Wilczek if (common->can_stall) {
2084b4d36f68SPiotr Wilczek fsg_set_halt(fsg, fsg->bulk_out);
2085b4d36f68SPiotr Wilczek halt_bulk_in_endpoint(fsg);
2086b4d36f68SPiotr Wilczek }
2087b4d36f68SPiotr Wilczek return -EINVAL;
2088b4d36f68SPiotr Wilczek }
2089b4d36f68SPiotr Wilczek
2090b4d36f68SPiotr Wilczek /* Save the command for later */
2091b4d36f68SPiotr Wilczek common->cmnd_size = cbw->Length;
2092b4d36f68SPiotr Wilczek memcpy(common->cmnd, cbw->CDB, common->cmnd_size);
2093b4d36f68SPiotr Wilczek if (cbw->Flags & USB_BULK_IN_FLAG)
2094b4d36f68SPiotr Wilczek common->data_dir = DATA_DIR_TO_HOST;
2095b4d36f68SPiotr Wilczek else
2096b4d36f68SPiotr Wilczek common->data_dir = DATA_DIR_FROM_HOST;
2097b4d36f68SPiotr Wilczek common->data_size = le32_to_cpu(cbw->DataTransferLength);
2098b4d36f68SPiotr Wilczek if (common->data_size == 0)
2099b4d36f68SPiotr Wilczek common->data_dir = DATA_DIR_NONE;
2100b4d36f68SPiotr Wilczek common->lun = cbw->Lun;
2101b4d36f68SPiotr Wilczek common->tag = cbw->Tag;
2102b4d36f68SPiotr Wilczek return 0;
2103b4d36f68SPiotr Wilczek }
2104b4d36f68SPiotr Wilczek
2105b4d36f68SPiotr Wilczek
get_next_command(struct fsg_common * common)2106b4d36f68SPiotr Wilczek static int get_next_command(struct fsg_common *common)
2107b4d36f68SPiotr Wilczek {
2108b4d36f68SPiotr Wilczek struct fsg_buffhd *bh;
2109b4d36f68SPiotr Wilczek int rc = 0;
2110b4d36f68SPiotr Wilczek
2111b4d36f68SPiotr Wilczek /* Wait for the next buffer to become available */
2112b4d36f68SPiotr Wilczek bh = common->next_buffhd_to_fill;
2113b4d36f68SPiotr Wilczek while (bh->state != BUF_STATE_EMPTY) {
2114b4d36f68SPiotr Wilczek rc = sleep_thread(common);
2115b4d36f68SPiotr Wilczek if (rc)
2116b4d36f68SPiotr Wilczek return rc;
2117b4d36f68SPiotr Wilczek }
2118b4d36f68SPiotr Wilczek
2119b4d36f68SPiotr Wilczek /* Queue a request to read a Bulk-only CBW */
2120b4d36f68SPiotr Wilczek set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN);
2121b4d36f68SPiotr Wilczek bh->outreq->short_not_ok = 1;
2122b4d36f68SPiotr Wilczek START_TRANSFER_OR(common, bulk_out, bh->outreq,
2123b4d36f68SPiotr Wilczek &bh->outreq_busy, &bh->state)
2124b4d36f68SPiotr Wilczek /* Don't know what to do if common->fsg is NULL */
2125b4d36f68SPiotr Wilczek return -EIO;
2126b4d36f68SPiotr Wilczek
2127b4d36f68SPiotr Wilczek /* We will drain the buffer in software, which means we
2128b4d36f68SPiotr Wilczek * can reuse it for the next filling. No need to advance
2129b4d36f68SPiotr Wilczek * next_buffhd_to_fill. */
2130b4d36f68SPiotr Wilczek
2131b4d36f68SPiotr Wilczek /* Wait for the CBW to arrive */
2132b4d36f68SPiotr Wilczek while (bh->state != BUF_STATE_FULL) {
2133b4d36f68SPiotr Wilczek rc = sleep_thread(common);
2134b4d36f68SPiotr Wilczek if (rc)
2135b4d36f68SPiotr Wilczek return rc;
2136b4d36f68SPiotr Wilczek }
2137b4d36f68SPiotr Wilczek
2138b4d36f68SPiotr Wilczek rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO;
2139b4d36f68SPiotr Wilczek bh->state = BUF_STATE_EMPTY;
2140b4d36f68SPiotr Wilczek
2141b4d36f68SPiotr Wilczek return rc;
2142b4d36f68SPiotr Wilczek }
2143b4d36f68SPiotr Wilczek
2144b4d36f68SPiotr Wilczek
2145b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
2146b4d36f68SPiotr Wilczek
enable_endpoint(struct fsg_common * common,struct usb_ep * ep,const struct usb_endpoint_descriptor * d)2147b4d36f68SPiotr Wilczek static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep,
2148b4d36f68SPiotr Wilczek const struct usb_endpoint_descriptor *d)
2149b4d36f68SPiotr Wilczek {
2150b4d36f68SPiotr Wilczek int rc;
2151b4d36f68SPiotr Wilczek
2152b4d36f68SPiotr Wilczek ep->driver_data = common;
2153b4d36f68SPiotr Wilczek rc = usb_ep_enable(ep, d);
2154b4d36f68SPiotr Wilczek if (rc)
2155b4d36f68SPiotr Wilczek ERROR(common, "can't enable %s, result %d\n", ep->name, rc);
2156b4d36f68SPiotr Wilczek return rc;
2157b4d36f68SPiotr Wilczek }
2158b4d36f68SPiotr Wilczek
alloc_request(struct fsg_common * common,struct usb_ep * ep,struct usb_request ** preq)2159b4d36f68SPiotr Wilczek static int alloc_request(struct fsg_common *common, struct usb_ep *ep,
2160b4d36f68SPiotr Wilczek struct usb_request **preq)
2161b4d36f68SPiotr Wilczek {
2162b4d36f68SPiotr Wilczek *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
2163b4d36f68SPiotr Wilczek if (*preq)
2164b4d36f68SPiotr Wilczek return 0;
2165b4d36f68SPiotr Wilczek ERROR(common, "can't allocate request for %s\n", ep->name);
2166b4d36f68SPiotr Wilczek return -ENOMEM;
2167b4d36f68SPiotr Wilczek }
2168b4d36f68SPiotr Wilczek
2169b4d36f68SPiotr Wilczek /* Reset interface setting and re-init endpoint state (toggle etc). */
do_set_interface(struct fsg_common * common,struct fsg_dev * new_fsg)2170b4d36f68SPiotr Wilczek static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg)
2171b4d36f68SPiotr Wilczek {
2172b4d36f68SPiotr Wilczek const struct usb_endpoint_descriptor *d;
2173b4d36f68SPiotr Wilczek struct fsg_dev *fsg;
2174b4d36f68SPiotr Wilczek int i, rc = 0;
2175b4d36f68SPiotr Wilczek
2176b4d36f68SPiotr Wilczek if (common->running)
2177b4d36f68SPiotr Wilczek DBG(common, "reset interface\n");
2178b4d36f68SPiotr Wilczek
2179b4d36f68SPiotr Wilczek reset:
2180b4d36f68SPiotr Wilczek /* Deallocate the requests */
2181b4d36f68SPiotr Wilczek if (common->fsg) {
2182b4d36f68SPiotr Wilczek fsg = common->fsg;
2183b4d36f68SPiotr Wilczek
2184b4d36f68SPiotr Wilczek for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2185b4d36f68SPiotr Wilczek struct fsg_buffhd *bh = &common->buffhds[i];
2186b4d36f68SPiotr Wilczek
2187b4d36f68SPiotr Wilczek if (bh->inreq) {
2188b4d36f68SPiotr Wilczek usb_ep_free_request(fsg->bulk_in, bh->inreq);
2189b4d36f68SPiotr Wilczek bh->inreq = NULL;
2190b4d36f68SPiotr Wilczek }
2191b4d36f68SPiotr Wilczek if (bh->outreq) {
2192b4d36f68SPiotr Wilczek usb_ep_free_request(fsg->bulk_out, bh->outreq);
2193b4d36f68SPiotr Wilczek bh->outreq = NULL;
2194b4d36f68SPiotr Wilczek }
2195b4d36f68SPiotr Wilczek }
2196b4d36f68SPiotr Wilczek
2197b4d36f68SPiotr Wilczek /* Disable the endpoints */
2198b4d36f68SPiotr Wilczek if (fsg->bulk_in_enabled) {
2199b4d36f68SPiotr Wilczek usb_ep_disable(fsg->bulk_in);
2200b4d36f68SPiotr Wilczek fsg->bulk_in_enabled = 0;
2201b4d36f68SPiotr Wilczek }
2202b4d36f68SPiotr Wilczek if (fsg->bulk_out_enabled) {
2203b4d36f68SPiotr Wilczek usb_ep_disable(fsg->bulk_out);
2204b4d36f68SPiotr Wilczek fsg->bulk_out_enabled = 0;
2205b4d36f68SPiotr Wilczek }
2206b4d36f68SPiotr Wilczek
2207b4d36f68SPiotr Wilczek common->fsg = NULL;
2208b4d36f68SPiotr Wilczek /* wake_up(&common->fsg_wait); */
2209b4d36f68SPiotr Wilczek }
2210b4d36f68SPiotr Wilczek
2211b4d36f68SPiotr Wilczek common->running = 0;
2212b4d36f68SPiotr Wilczek if (!new_fsg || rc)
2213b4d36f68SPiotr Wilczek return rc;
2214b4d36f68SPiotr Wilczek
2215b4d36f68SPiotr Wilczek common->fsg = new_fsg;
2216b4d36f68SPiotr Wilczek fsg = common->fsg;
2217b4d36f68SPiotr Wilczek
2218b4d36f68SPiotr Wilczek /* Enable the endpoints */
2219b4d36f68SPiotr Wilczek d = fsg_ep_desc(common->gadget,
2220b4d36f68SPiotr Wilczek &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc);
2221b4d36f68SPiotr Wilczek rc = enable_endpoint(common, fsg->bulk_in, d);
2222b4d36f68SPiotr Wilczek if (rc)
2223b4d36f68SPiotr Wilczek goto reset;
2224b4d36f68SPiotr Wilczek fsg->bulk_in_enabled = 1;
2225b4d36f68SPiotr Wilczek
2226b4d36f68SPiotr Wilczek d = fsg_ep_desc(common->gadget,
2227b4d36f68SPiotr Wilczek &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc);
2228b4d36f68SPiotr Wilczek rc = enable_endpoint(common, fsg->bulk_out, d);
2229b4d36f68SPiotr Wilczek if (rc)
2230b4d36f68SPiotr Wilczek goto reset;
2231b4d36f68SPiotr Wilczek fsg->bulk_out_enabled = 1;
2232f6664ba4SVivek Gautam common->bulk_out_maxpacket =
2233f6664ba4SVivek Gautam le16_to_cpu(get_unaligned(&d->wMaxPacketSize));
22345ac73f68SBryan O'Donoghue generic_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
2235b4d36f68SPiotr Wilczek
2236b4d36f68SPiotr Wilczek /* Allocate the requests */
2237b4d36f68SPiotr Wilczek for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2238b4d36f68SPiotr Wilczek struct fsg_buffhd *bh = &common->buffhds[i];
2239b4d36f68SPiotr Wilczek
2240b4d36f68SPiotr Wilczek rc = alloc_request(common, fsg->bulk_in, &bh->inreq);
2241b4d36f68SPiotr Wilczek if (rc)
2242b4d36f68SPiotr Wilczek goto reset;
2243b4d36f68SPiotr Wilczek rc = alloc_request(common, fsg->bulk_out, &bh->outreq);
2244b4d36f68SPiotr Wilczek if (rc)
2245b4d36f68SPiotr Wilczek goto reset;
2246b4d36f68SPiotr Wilczek bh->inreq->buf = bh->outreq->buf = bh->buf;
2247b4d36f68SPiotr Wilczek bh->inreq->context = bh->outreq->context = bh;
2248b4d36f68SPiotr Wilczek bh->inreq->complete = bulk_in_complete;
2249b4d36f68SPiotr Wilczek bh->outreq->complete = bulk_out_complete;
2250b4d36f68SPiotr Wilczek }
2251b4d36f68SPiotr Wilczek
2252b4d36f68SPiotr Wilczek common->running = 1;
2253b4d36f68SPiotr Wilczek
2254b4d36f68SPiotr Wilczek return rc;
2255b4d36f68SPiotr Wilczek }
2256b4d36f68SPiotr Wilczek
2257b4d36f68SPiotr Wilczek
2258b4d36f68SPiotr Wilczek /****************************** ALT CONFIGS ******************************/
2259b4d36f68SPiotr Wilczek
2260b4d36f68SPiotr Wilczek
fsg_set_alt(struct usb_function * f,unsigned intf,unsigned alt)2261b4d36f68SPiotr Wilczek static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
2262b4d36f68SPiotr Wilczek {
2263b4d36f68SPiotr Wilczek struct fsg_dev *fsg = fsg_from_func(f);
2264b4d36f68SPiotr Wilczek fsg->common->new_fsg = fsg;
2265b4d36f68SPiotr Wilczek raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2266b4d36f68SPiotr Wilczek return 0;
2267b4d36f68SPiotr Wilczek }
2268b4d36f68SPiotr Wilczek
fsg_disable(struct usb_function * f)2269b4d36f68SPiotr Wilczek static void fsg_disable(struct usb_function *f)
2270b4d36f68SPiotr Wilczek {
2271b4d36f68SPiotr Wilczek struct fsg_dev *fsg = fsg_from_func(f);
2272b4d36f68SPiotr Wilczek fsg->common->new_fsg = NULL;
2273b4d36f68SPiotr Wilczek raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2274b4d36f68SPiotr Wilczek }
2275b4d36f68SPiotr Wilczek
2276b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
2277b4d36f68SPiotr Wilczek
handle_exception(struct fsg_common * common)2278b4d36f68SPiotr Wilczek static void handle_exception(struct fsg_common *common)
2279b4d36f68SPiotr Wilczek {
2280b4d36f68SPiotr Wilczek int i;
2281b4d36f68SPiotr Wilczek struct fsg_buffhd *bh;
2282b4d36f68SPiotr Wilczek enum fsg_state old_state;
2283b4d36f68SPiotr Wilczek struct fsg_lun *curlun;
2284b4d36f68SPiotr Wilczek unsigned int exception_req_tag;
2285b4d36f68SPiotr Wilczek
2286b4d36f68SPiotr Wilczek /* Cancel all the pending transfers */
2287b4d36f68SPiotr Wilczek if (common->fsg) {
2288b4d36f68SPiotr Wilczek for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2289b4d36f68SPiotr Wilczek bh = &common->buffhds[i];
2290b4d36f68SPiotr Wilczek if (bh->inreq_busy)
2291b4d36f68SPiotr Wilczek usb_ep_dequeue(common->fsg->bulk_in, bh->inreq);
2292b4d36f68SPiotr Wilczek if (bh->outreq_busy)
2293b4d36f68SPiotr Wilczek usb_ep_dequeue(common->fsg->bulk_out,
2294b4d36f68SPiotr Wilczek bh->outreq);
2295b4d36f68SPiotr Wilczek }
2296b4d36f68SPiotr Wilczek
2297b4d36f68SPiotr Wilczek /* Wait until everything is idle */
2298b4d36f68SPiotr Wilczek for (;;) {
2299b4d36f68SPiotr Wilczek int num_active = 0;
2300b4d36f68SPiotr Wilczek for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2301b4d36f68SPiotr Wilczek bh = &common->buffhds[i];
2302b4d36f68SPiotr Wilczek num_active += bh->inreq_busy + bh->outreq_busy;
2303b4d36f68SPiotr Wilczek }
2304b4d36f68SPiotr Wilczek if (num_active == 0)
2305b4d36f68SPiotr Wilczek break;
2306b4d36f68SPiotr Wilczek if (sleep_thread(common))
2307b4d36f68SPiotr Wilczek return;
2308b4d36f68SPiotr Wilczek }
2309b4d36f68SPiotr Wilczek
2310b4d36f68SPiotr Wilczek /* Clear out the controller's fifos */
2311b4d36f68SPiotr Wilczek if (common->fsg->bulk_in_enabled)
2312b4d36f68SPiotr Wilczek usb_ep_fifo_flush(common->fsg->bulk_in);
2313b4d36f68SPiotr Wilczek if (common->fsg->bulk_out_enabled)
2314b4d36f68SPiotr Wilczek usb_ep_fifo_flush(common->fsg->bulk_out);
2315b4d36f68SPiotr Wilczek }
2316b4d36f68SPiotr Wilczek
2317b4d36f68SPiotr Wilczek /* Reset the I/O buffer states and pointers, the SCSI
2318b4d36f68SPiotr Wilczek * state, and the exception. Then invoke the handler. */
2319b4d36f68SPiotr Wilczek
2320b4d36f68SPiotr Wilczek for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2321b4d36f68SPiotr Wilczek bh = &common->buffhds[i];
2322b4d36f68SPiotr Wilczek bh->state = BUF_STATE_EMPTY;
2323b4d36f68SPiotr Wilczek }
2324b4d36f68SPiotr Wilczek common->next_buffhd_to_fill = &common->buffhds[0];
2325b4d36f68SPiotr Wilczek common->next_buffhd_to_drain = &common->buffhds[0];
2326b4d36f68SPiotr Wilczek exception_req_tag = common->exception_req_tag;
2327b4d36f68SPiotr Wilczek old_state = common->state;
2328b4d36f68SPiotr Wilczek
2329b4d36f68SPiotr Wilczek if (old_state == FSG_STATE_ABORT_BULK_OUT)
2330b4d36f68SPiotr Wilczek common->state = FSG_STATE_STATUS_PHASE;
2331b4d36f68SPiotr Wilczek else {
2332b4d36f68SPiotr Wilczek for (i = 0; i < common->nluns; ++i) {
2333b4d36f68SPiotr Wilczek curlun = &common->luns[i];
2334b4d36f68SPiotr Wilczek curlun->sense_data = SS_NO_SENSE;
2335b4d36f68SPiotr Wilczek curlun->info_valid = 0;
2336b4d36f68SPiotr Wilczek }
2337b4d36f68SPiotr Wilczek common->state = FSG_STATE_IDLE;
2338b4d36f68SPiotr Wilczek }
2339b4d36f68SPiotr Wilczek
2340b4d36f68SPiotr Wilczek /* Carry out any extra actions required for the exception */
2341b4d36f68SPiotr Wilczek switch (old_state) {
2342b4d36f68SPiotr Wilczek case FSG_STATE_ABORT_BULK_OUT:
2343b4d36f68SPiotr Wilczek send_status(common);
2344b4d36f68SPiotr Wilczek
2345b4d36f68SPiotr Wilczek if (common->state == FSG_STATE_STATUS_PHASE)
2346b4d36f68SPiotr Wilczek common->state = FSG_STATE_IDLE;
2347b4d36f68SPiotr Wilczek break;
2348b4d36f68SPiotr Wilczek
2349b4d36f68SPiotr Wilczek case FSG_STATE_RESET:
2350b4d36f68SPiotr Wilczek /* In case we were forced against our will to halt a
2351b4d36f68SPiotr Wilczek * bulk endpoint, clear the halt now. (The SuperH UDC
2352b4d36f68SPiotr Wilczek * requires this.) */
2353b4d36f68SPiotr Wilczek if (!fsg_is_set(common))
2354b4d36f68SPiotr Wilczek break;
2355b4d36f68SPiotr Wilczek if (test_and_clear_bit(IGNORE_BULK_OUT,
2356b4d36f68SPiotr Wilczek &common->fsg->atomic_bitflags))
2357b4d36f68SPiotr Wilczek usb_ep_clear_halt(common->fsg->bulk_in);
2358b4d36f68SPiotr Wilczek
2359b4d36f68SPiotr Wilczek if (common->ep0_req_tag == exception_req_tag)
2360b4d36f68SPiotr Wilczek ep0_queue(common); /* Complete the status stage */
2361b4d36f68SPiotr Wilczek
2362b4d36f68SPiotr Wilczek break;
2363b4d36f68SPiotr Wilczek
2364b4d36f68SPiotr Wilczek case FSG_STATE_CONFIG_CHANGE:
2365b4d36f68SPiotr Wilczek do_set_interface(common, common->new_fsg);
2366b4d36f68SPiotr Wilczek break;
2367b4d36f68SPiotr Wilczek
2368b4d36f68SPiotr Wilczek case FSG_STATE_EXIT:
2369b4d36f68SPiotr Wilczek case FSG_STATE_TERMINATED:
2370b4d36f68SPiotr Wilczek do_set_interface(common, NULL); /* Free resources */
2371b4d36f68SPiotr Wilczek common->state = FSG_STATE_TERMINATED; /* Stop the thread */
2372b4d36f68SPiotr Wilczek break;
2373b4d36f68SPiotr Wilczek
2374b4d36f68SPiotr Wilczek case FSG_STATE_INTERFACE_CHANGE:
2375b4d36f68SPiotr Wilczek case FSG_STATE_DISCONNECT:
2376b4d36f68SPiotr Wilczek case FSG_STATE_COMMAND_PHASE:
2377b4d36f68SPiotr Wilczek case FSG_STATE_DATA_PHASE:
2378b4d36f68SPiotr Wilczek case FSG_STATE_STATUS_PHASE:
2379b4d36f68SPiotr Wilczek case FSG_STATE_IDLE:
2380b4d36f68SPiotr Wilczek break;
2381b4d36f68SPiotr Wilczek }
2382b4d36f68SPiotr Wilczek }
2383b4d36f68SPiotr Wilczek
2384b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
2385b4d36f68SPiotr Wilczek
fsg_main_thread(void * common_)2386b4d36f68SPiotr Wilczek int fsg_main_thread(void *common_)
2387b4d36f68SPiotr Wilczek {
2388351e9b20SPrzemyslaw Marczak int ret;
2389b4d36f68SPiotr Wilczek struct fsg_common *common = the_fsg_common;
2390b4d36f68SPiotr Wilczek /* The main loop */
2391b4d36f68SPiotr Wilczek do {
2392b4d36f68SPiotr Wilczek if (exception_in_progress(common)) {
2393b4d36f68SPiotr Wilczek handle_exception(common);
2394b4d36f68SPiotr Wilczek continue;
2395b4d36f68SPiotr Wilczek }
2396b4d36f68SPiotr Wilczek
2397b4d36f68SPiotr Wilczek if (!common->running) {
2398351e9b20SPrzemyslaw Marczak ret = sleep_thread(common);
2399351e9b20SPrzemyslaw Marczak if (ret)
2400351e9b20SPrzemyslaw Marczak return ret;
2401351e9b20SPrzemyslaw Marczak
2402b4d36f68SPiotr Wilczek continue;
2403b4d36f68SPiotr Wilczek }
2404b4d36f68SPiotr Wilczek
2405351e9b20SPrzemyslaw Marczak ret = get_next_command(common);
2406351e9b20SPrzemyslaw Marczak if (ret)
2407351e9b20SPrzemyslaw Marczak return ret;
2408b4d36f68SPiotr Wilczek
2409b4d36f68SPiotr Wilczek if (!exception_in_progress(common))
2410b4d36f68SPiotr Wilczek common->state = FSG_STATE_DATA_PHASE;
2411b4d36f68SPiotr Wilczek
2412b4d36f68SPiotr Wilczek if (do_scsi_command(common) || finish_reply(common))
2413b4d36f68SPiotr Wilczek continue;
2414b4d36f68SPiotr Wilczek
2415b4d36f68SPiotr Wilczek if (!exception_in_progress(common))
2416b4d36f68SPiotr Wilczek common->state = FSG_STATE_STATUS_PHASE;
2417b4d36f68SPiotr Wilczek
2418b4d36f68SPiotr Wilczek if (send_status(common))
2419b4d36f68SPiotr Wilczek continue;
2420b4d36f68SPiotr Wilczek
2421b4d36f68SPiotr Wilczek if (!exception_in_progress(common))
2422b4d36f68SPiotr Wilczek common->state = FSG_STATE_IDLE;
2423b4d36f68SPiotr Wilczek } while (0);
2424b4d36f68SPiotr Wilczek
2425b4d36f68SPiotr Wilczek common->thread_task = NULL;
2426b4d36f68SPiotr Wilczek
2427b4d36f68SPiotr Wilczek return 0;
2428b4d36f68SPiotr Wilczek }
2429b4d36f68SPiotr Wilczek
2430b4d36f68SPiotr Wilczek static void fsg_common_release(struct kref *ref);
2431b4d36f68SPiotr Wilczek
fsg_common_init(struct fsg_common * common,struct usb_composite_dev * cdev)2432b4d36f68SPiotr Wilczek static struct fsg_common *fsg_common_init(struct fsg_common *common,
2433b4d36f68SPiotr Wilczek struct usb_composite_dev *cdev)
2434b4d36f68SPiotr Wilczek {
2435b4d36f68SPiotr Wilczek struct usb_gadget *gadget = cdev->gadget;
2436b4d36f68SPiotr Wilczek struct fsg_buffhd *bh;
2437b4d36f68SPiotr Wilczek struct fsg_lun *curlun;
2438b4d36f68SPiotr Wilczek int nluns, i, rc;
2439b4d36f68SPiotr Wilczek
2440b4d36f68SPiotr Wilczek /* Find out how many LUNs there should be */
244102585eb3SStephen Warren nluns = ums_count;
2442b4d36f68SPiotr Wilczek if (nluns < 1 || nluns > FSG_MAX_LUNS) {
2443b4d36f68SPiotr Wilczek printf("invalid number of LUNs: %u\n", nluns);
2444b4d36f68SPiotr Wilczek return ERR_PTR(-EINVAL);
2445b4d36f68SPiotr Wilczek }
2446b4d36f68SPiotr Wilczek
2447b4d36f68SPiotr Wilczek /* Allocate? */
2448b4d36f68SPiotr Wilczek if (!common) {
2449933611f7SJeroen Hofstee common = calloc(sizeof(*common), 1);
2450b4d36f68SPiotr Wilczek if (!common)
2451b4d36f68SPiotr Wilczek return ERR_PTR(-ENOMEM);
2452b4d36f68SPiotr Wilczek common->free_storage_on_release = 1;
2453b4d36f68SPiotr Wilczek } else {
2454933611f7SJeroen Hofstee memset(common, 0, sizeof(*common));
2455b4d36f68SPiotr Wilczek common->free_storage_on_release = 0;
2456b4d36f68SPiotr Wilczek }
2457b4d36f68SPiotr Wilczek
2458b4d36f68SPiotr Wilczek common->ops = NULL;
2459b4d36f68SPiotr Wilczek common->private_data = NULL;
2460b4d36f68SPiotr Wilczek
2461b4d36f68SPiotr Wilczek common->gadget = gadget;
2462b4d36f68SPiotr Wilczek common->ep0 = gadget->ep0;
2463b4d36f68SPiotr Wilczek common->ep0req = cdev->req;
2464b4d36f68SPiotr Wilczek
2465b4d36f68SPiotr Wilczek /* Maybe allocate device-global string IDs, and patch descriptors */
2466b4d36f68SPiotr Wilczek if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2467b4d36f68SPiotr Wilczek rc = usb_string_id(cdev);
2468b4d36f68SPiotr Wilczek if (unlikely(rc < 0))
2469b4d36f68SPiotr Wilczek goto error_release;
2470b4d36f68SPiotr Wilczek fsg_strings[FSG_STRING_INTERFACE].id = rc;
2471b4d36f68SPiotr Wilczek fsg_intf_desc.iInterface = rc;
2472b4d36f68SPiotr Wilczek }
2473b4d36f68SPiotr Wilczek
2474b4d36f68SPiotr Wilczek /* Create the LUNs, open their backing files, and register the
2475b4d36f68SPiotr Wilczek * LUN devices in sysfs. */
2476b4d36f68SPiotr Wilczek curlun = calloc(nluns, sizeof *curlun);
2477b4d36f68SPiotr Wilczek if (!curlun) {
2478b4d36f68SPiotr Wilczek rc = -ENOMEM;
2479b4d36f68SPiotr Wilczek goto error_release;
2480b4d36f68SPiotr Wilczek }
2481b4d36f68SPiotr Wilczek common->nluns = nluns;
2482b4d36f68SPiotr Wilczek
2483b4d36f68SPiotr Wilczek for (i = 0; i < nluns; i++) {
2484b4d36f68SPiotr Wilczek common->luns[i].removable = 1;
2485b4d36f68SPiotr Wilczek
248602585eb3SStephen Warren rc = fsg_lun_open(&common->luns[i], ums[i].num_sectors, "");
2487b4d36f68SPiotr Wilczek if (rc)
2488b4d36f68SPiotr Wilczek goto error_luns;
2489b4d36f68SPiotr Wilczek }
2490b4d36f68SPiotr Wilczek common->lun = 0;
2491b4d36f68SPiotr Wilczek
2492b4d36f68SPiotr Wilczek /* Data buffers cyclic list */
2493b4d36f68SPiotr Wilczek bh = common->buffhds;
2494b4d36f68SPiotr Wilczek
2495b4d36f68SPiotr Wilczek i = FSG_NUM_BUFFERS;
2496b4d36f68SPiotr Wilczek goto buffhds_first_it;
2497b4d36f68SPiotr Wilczek do {
2498b4d36f68SPiotr Wilczek bh->next = bh + 1;
2499b4d36f68SPiotr Wilczek ++bh;
2500b4d36f68SPiotr Wilczek buffhds_first_it:
2501b4d36f68SPiotr Wilczek bh->inreq_busy = 0;
2502b4d36f68SPiotr Wilczek bh->outreq_busy = 0;
250316b7a29fSLukasz Majewski bh->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, FSG_BUFLEN);
2504b4d36f68SPiotr Wilczek if (unlikely(!bh->buf)) {
2505b4d36f68SPiotr Wilczek rc = -ENOMEM;
2506b4d36f68SPiotr Wilczek goto error_release;
2507b4d36f68SPiotr Wilczek }
2508b4d36f68SPiotr Wilczek } while (--i);
2509b4d36f68SPiotr Wilczek bh->next = common->buffhds;
2510b4d36f68SPiotr Wilczek
2511b4d36f68SPiotr Wilczek snprintf(common->inquiry_string, sizeof common->inquiry_string,
2512b4d36f68SPiotr Wilczek "%-8s%-16s%04x",
2513b4d36f68SPiotr Wilczek "Linux ",
2514b4d36f68SPiotr Wilczek "File-Store Gadget",
2515b4d36f68SPiotr Wilczek 0xffff);
2516b4d36f68SPiotr Wilczek
2517b4d36f68SPiotr Wilczek /* Some peripheral controllers are known not to be able to
2518b4d36f68SPiotr Wilczek * halt bulk endpoints correctly. If one of them is present,
2519b4d36f68SPiotr Wilczek * disable stalls.
2520b4d36f68SPiotr Wilczek */
2521b4d36f68SPiotr Wilczek
2522b4d36f68SPiotr Wilczek /* Tell the thread to start working */
2523b4d36f68SPiotr Wilczek common->thread_task =
2524b4d36f68SPiotr Wilczek kthread_create(fsg_main_thread, common,
2525b4d36f68SPiotr Wilczek OR(cfg->thread_name, "file-storage"));
2526b4d36f68SPiotr Wilczek if (IS_ERR(common->thread_task)) {
2527b4d36f68SPiotr Wilczek rc = PTR_ERR(common->thread_task);
2528b4d36f68SPiotr Wilczek goto error_release;
2529b4d36f68SPiotr Wilczek }
2530b4d36f68SPiotr Wilczek
2531b4d36f68SPiotr Wilczek #undef OR
2532b4d36f68SPiotr Wilczek /* Information */
2533b4d36f68SPiotr Wilczek INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");
2534b4d36f68SPiotr Wilczek INFO(common, "Number of LUNs=%d\n", common->nluns);
2535b4d36f68SPiotr Wilczek
2536b4d36f68SPiotr Wilczek return common;
2537b4d36f68SPiotr Wilczek
2538b4d36f68SPiotr Wilczek error_luns:
2539b4d36f68SPiotr Wilczek common->nluns = i + 1;
2540b4d36f68SPiotr Wilczek error_release:
2541b4d36f68SPiotr Wilczek common->state = FSG_STATE_TERMINATED; /* The thread is dead */
2542b4d36f68SPiotr Wilczek /* Call fsg_common_release() directly, ref might be not
2543b4d36f68SPiotr Wilczek * initialised */
2544b4d36f68SPiotr Wilczek fsg_common_release(&common->ref);
2545b4d36f68SPiotr Wilczek return ERR_PTR(rc);
2546b4d36f68SPiotr Wilczek }
2547b4d36f68SPiotr Wilczek
fsg_common_release(struct kref * ref)2548b4d36f68SPiotr Wilczek static void fsg_common_release(struct kref *ref)
2549b4d36f68SPiotr Wilczek {
2550b4d36f68SPiotr Wilczek struct fsg_common *common = container_of(ref, struct fsg_common, ref);
2551b4d36f68SPiotr Wilczek
2552b4d36f68SPiotr Wilczek /* If the thread isn't already dead, tell it to exit now */
2553b4d36f68SPiotr Wilczek if (common->state != FSG_STATE_TERMINATED) {
2554b4d36f68SPiotr Wilczek raise_exception(common, FSG_STATE_EXIT);
2555b4d36f68SPiotr Wilczek wait_for_completion(&common->thread_notifier);
2556b4d36f68SPiotr Wilczek }
2557b4d36f68SPiotr Wilczek
2558b4d36f68SPiotr Wilczek if (likely(common->luns)) {
2559b4d36f68SPiotr Wilczek struct fsg_lun *lun = common->luns;
2560b4d36f68SPiotr Wilczek unsigned i = common->nluns;
2561b4d36f68SPiotr Wilczek
2562b4d36f68SPiotr Wilczek /* In error recovery common->nluns may be zero. */
2563b4d36f68SPiotr Wilczek for (; i; --i, ++lun)
2564b4d36f68SPiotr Wilczek fsg_lun_close(lun);
2565b4d36f68SPiotr Wilczek
2566b4d36f68SPiotr Wilczek kfree(common->luns);
2567b4d36f68SPiotr Wilczek }
2568b4d36f68SPiotr Wilczek
2569b4d36f68SPiotr Wilczek {
2570b4d36f68SPiotr Wilczek struct fsg_buffhd *bh = common->buffhds;
2571b4d36f68SPiotr Wilczek unsigned i = FSG_NUM_BUFFERS;
2572b4d36f68SPiotr Wilczek do {
2573b4d36f68SPiotr Wilczek kfree(bh->buf);
2574b4d36f68SPiotr Wilczek } while (++bh, --i);
2575b4d36f68SPiotr Wilczek }
2576b4d36f68SPiotr Wilczek
2577b4d36f68SPiotr Wilczek if (common->free_storage_on_release)
2578b4d36f68SPiotr Wilczek kfree(common);
2579b4d36f68SPiotr Wilczek }
2580b4d36f68SPiotr Wilczek
2581b4d36f68SPiotr Wilczek
2582b4d36f68SPiotr Wilczek /*-------------------------------------------------------------------------*/
2583b4d36f68SPiotr Wilczek
2584b4d36f68SPiotr Wilczek /**
2585b4d36f68SPiotr Wilczek * usb_copy_descriptors - copy a vector of USB descriptors
2586b4d36f68SPiotr Wilczek * @src: null-terminated vector to copy
2587b4d36f68SPiotr Wilczek * Context: initialization code, which may sleep
2588b4d36f68SPiotr Wilczek *
2589b4d36f68SPiotr Wilczek * This makes a copy of a vector of USB descriptors. Its primary use
2590b4d36f68SPiotr Wilczek * is to support usb_function objects which can have multiple copies,
2591b4d36f68SPiotr Wilczek * each needing different descriptors. Functions may have static
2592b4d36f68SPiotr Wilczek * tables of descriptors, which are used as templates and customized
2593b4d36f68SPiotr Wilczek * with identifiers (for interfaces, strings, endpoints, and more)
2594b4d36f68SPiotr Wilczek * as needed by a given function instance.
2595b4d36f68SPiotr Wilczek */
2596b4d36f68SPiotr Wilczek struct usb_descriptor_header **
usb_copy_descriptors(struct usb_descriptor_header ** src)2597b4d36f68SPiotr Wilczek usb_copy_descriptors(struct usb_descriptor_header **src)
2598b4d36f68SPiotr Wilczek {
2599b4d36f68SPiotr Wilczek struct usb_descriptor_header **tmp;
2600b4d36f68SPiotr Wilczek unsigned bytes;
2601b4d36f68SPiotr Wilczek unsigned n_desc;
2602b4d36f68SPiotr Wilczek void *mem;
2603b4d36f68SPiotr Wilczek struct usb_descriptor_header **ret;
2604b4d36f68SPiotr Wilczek
2605b4d36f68SPiotr Wilczek /* count descriptors and their sizes; then add vector size */
2606b4d36f68SPiotr Wilczek for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
2607b4d36f68SPiotr Wilczek bytes += (*tmp)->bLength;
2608b4d36f68SPiotr Wilczek bytes += (n_desc + 1) * sizeof(*tmp);
2609b4d36f68SPiotr Wilczek
261016b7a29fSLukasz Majewski mem = memalign(CONFIG_SYS_CACHELINE_SIZE, bytes);
2611b4d36f68SPiotr Wilczek if (!mem)
2612b4d36f68SPiotr Wilczek return NULL;
2613b4d36f68SPiotr Wilczek
2614b4d36f68SPiotr Wilczek /* fill in pointers starting at "tmp",
2615b4d36f68SPiotr Wilczek * to descriptors copied starting at "mem";
2616b4d36f68SPiotr Wilczek * and return "ret"
2617b4d36f68SPiotr Wilczek */
2618b4d36f68SPiotr Wilczek tmp = mem;
2619b4d36f68SPiotr Wilczek ret = mem;
2620b4d36f68SPiotr Wilczek mem += (n_desc + 1) * sizeof(*tmp);
2621b4d36f68SPiotr Wilczek while (*src) {
2622b4d36f68SPiotr Wilczek memcpy(mem, *src, (*src)->bLength);
2623b4d36f68SPiotr Wilczek *tmp = mem;
2624b4d36f68SPiotr Wilczek tmp++;
2625b4d36f68SPiotr Wilczek mem += (*src)->bLength;
2626b4d36f68SPiotr Wilczek src++;
2627b4d36f68SPiotr Wilczek }
2628b4d36f68SPiotr Wilczek *tmp = NULL;
2629b4d36f68SPiotr Wilczek
2630b4d36f68SPiotr Wilczek return ret;
2631b4d36f68SPiotr Wilczek }
2632b4d36f68SPiotr Wilczek
fsg_unbind(struct usb_configuration * c,struct usb_function * f)2633b4d36f68SPiotr Wilczek static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2634b4d36f68SPiotr Wilczek {
2635b4d36f68SPiotr Wilczek struct fsg_dev *fsg = fsg_from_func(f);
2636b4d36f68SPiotr Wilczek
2637b4d36f68SPiotr Wilczek DBG(fsg, "unbind\n");
2638b4d36f68SPiotr Wilczek if (fsg->common->fsg == fsg) {
2639b4d36f68SPiotr Wilczek fsg->common->new_fsg = NULL;
2640b4d36f68SPiotr Wilczek raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE);
2641b4d36f68SPiotr Wilczek }
2642b4d36f68SPiotr Wilczek
2643b4d36f68SPiotr Wilczek free(fsg->function.descriptors);
2644b4d36f68SPiotr Wilczek free(fsg->function.hs_descriptors);
2645b4d36f68SPiotr Wilczek kfree(fsg);
2646b4d36f68SPiotr Wilczek }
2647b4d36f68SPiotr Wilczek
fsg_bind(struct usb_configuration * c,struct usb_function * f)2648b4d36f68SPiotr Wilczek static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2649b4d36f68SPiotr Wilczek {
2650b4d36f68SPiotr Wilczek struct fsg_dev *fsg = fsg_from_func(f);
2651b4d36f68SPiotr Wilczek struct usb_gadget *gadget = c->cdev->gadget;
2652b4d36f68SPiotr Wilczek int i;
2653b4d36f68SPiotr Wilczek struct usb_ep *ep;
2654b4d36f68SPiotr Wilczek fsg->gadget = gadget;
2655b4d36f68SPiotr Wilczek
2656b4d36f68SPiotr Wilczek /* New interface */
2657b4d36f68SPiotr Wilczek i = usb_interface_id(c, f);
2658b4d36f68SPiotr Wilczek if (i < 0)
2659b4d36f68SPiotr Wilczek return i;
2660b4d36f68SPiotr Wilczek fsg_intf_desc.bInterfaceNumber = i;
2661b4d36f68SPiotr Wilczek fsg->interface_number = i;
2662b4d36f68SPiotr Wilczek
2663b4d36f68SPiotr Wilczek /* Find all the endpoints we will use */
2664b4d36f68SPiotr Wilczek ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
2665b4d36f68SPiotr Wilczek if (!ep)
2666b4d36f68SPiotr Wilczek goto autoconf_fail;
2667b4d36f68SPiotr Wilczek ep->driver_data = fsg->common; /* claim the endpoint */
2668b4d36f68SPiotr Wilczek fsg->bulk_in = ep;
2669b4d36f68SPiotr Wilczek
2670b4d36f68SPiotr Wilczek ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
2671b4d36f68SPiotr Wilczek if (!ep)
2672b4d36f68SPiotr Wilczek goto autoconf_fail;
2673b4d36f68SPiotr Wilczek ep->driver_data = fsg->common; /* claim the endpoint */
2674b4d36f68SPiotr Wilczek fsg->bulk_out = ep;
2675b4d36f68SPiotr Wilczek
2676b4d36f68SPiotr Wilczek /* Copy descriptors */
2677b4d36f68SPiotr Wilczek f->descriptors = usb_copy_descriptors(fsg_fs_function);
2678b4d36f68SPiotr Wilczek if (unlikely(!f->descriptors))
2679b4d36f68SPiotr Wilczek return -ENOMEM;
2680b4d36f68SPiotr Wilczek
2681b4d36f68SPiotr Wilczek if (gadget_is_dualspeed(gadget)) {
2682b4d36f68SPiotr Wilczek /* Assume endpoint addresses are the same for both speeds */
2683b4d36f68SPiotr Wilczek fsg_hs_bulk_in_desc.bEndpointAddress =
2684b4d36f68SPiotr Wilczek fsg_fs_bulk_in_desc.bEndpointAddress;
2685b4d36f68SPiotr Wilczek fsg_hs_bulk_out_desc.bEndpointAddress =
2686b4d36f68SPiotr Wilczek fsg_fs_bulk_out_desc.bEndpointAddress;
2687b4d36f68SPiotr Wilczek f->hs_descriptors = usb_copy_descriptors(fsg_hs_function);
2688b4d36f68SPiotr Wilczek if (unlikely(!f->hs_descriptors)) {
2689b4d36f68SPiotr Wilczek free(f->descriptors);
2690b4d36f68SPiotr Wilczek return -ENOMEM;
2691b4d36f68SPiotr Wilczek }
2692b4d36f68SPiotr Wilczek }
2693b4d36f68SPiotr Wilczek return 0;
2694b4d36f68SPiotr Wilczek
2695b4d36f68SPiotr Wilczek autoconf_fail:
2696b4d36f68SPiotr Wilczek ERROR(fsg, "unable to autoconfigure all endpoints\n");
2697b4d36f68SPiotr Wilczek return -ENOTSUPP;
2698b4d36f68SPiotr Wilczek }
2699b4d36f68SPiotr Wilczek
2700b4d36f68SPiotr Wilczek
2701b4d36f68SPiotr Wilczek /****************************** ADD FUNCTION ******************************/
2702b4d36f68SPiotr Wilczek
2703b4d36f68SPiotr Wilczek static struct usb_gadget_strings *fsg_strings_array[] = {
2704b4d36f68SPiotr Wilczek &fsg_stringtab,
2705b4d36f68SPiotr Wilczek NULL,
2706b4d36f68SPiotr Wilczek };
2707b4d36f68SPiotr Wilczek
fsg_bind_config(struct usb_composite_dev * cdev,struct usb_configuration * c,struct fsg_common * common)2708b4d36f68SPiotr Wilczek static int fsg_bind_config(struct usb_composite_dev *cdev,
2709b4d36f68SPiotr Wilczek struct usb_configuration *c,
2710b4d36f68SPiotr Wilczek struct fsg_common *common)
2711b4d36f68SPiotr Wilczek {
2712b4d36f68SPiotr Wilczek struct fsg_dev *fsg;
2713b4d36f68SPiotr Wilczek int rc;
2714b4d36f68SPiotr Wilczek
2715b4d36f68SPiotr Wilczek fsg = calloc(1, sizeof *fsg);
2716b4d36f68SPiotr Wilczek if (!fsg)
2717b4d36f68SPiotr Wilczek return -ENOMEM;
2718b4d36f68SPiotr Wilczek fsg->function.name = FSG_DRIVER_DESC;
2719b4d36f68SPiotr Wilczek fsg->function.strings = fsg_strings_array;
2720b4d36f68SPiotr Wilczek fsg->function.bind = fsg_bind;
2721b4d36f68SPiotr Wilczek fsg->function.unbind = fsg_unbind;
2722b4d36f68SPiotr Wilczek fsg->function.setup = fsg_setup;
2723b4d36f68SPiotr Wilczek fsg->function.set_alt = fsg_set_alt;
2724b4d36f68SPiotr Wilczek fsg->function.disable = fsg_disable;
2725b4d36f68SPiotr Wilczek
2726b4d36f68SPiotr Wilczek fsg->common = common;
2727b4d36f68SPiotr Wilczek common->fsg = fsg;
2728b4d36f68SPiotr Wilczek /* Our caller holds a reference to common structure so we
2729b4d36f68SPiotr Wilczek * don't have to be worry about it being freed until we return
2730b4d36f68SPiotr Wilczek * from this function. So instead of incrementing counter now
2731b4d36f68SPiotr Wilczek * and decrement in error recovery we increment it only when
2732b4d36f68SPiotr Wilczek * call to usb_add_function() was successful. */
2733b4d36f68SPiotr Wilczek
2734b4d36f68SPiotr Wilczek rc = usb_add_function(c, &fsg->function);
2735b4d36f68SPiotr Wilczek
2736b4d36f68SPiotr Wilczek if (rc)
2737b4d36f68SPiotr Wilczek kfree(fsg);
2738b4d36f68SPiotr Wilczek
2739b4d36f68SPiotr Wilczek return rc;
2740b4d36f68SPiotr Wilczek }
2741b4d36f68SPiotr Wilczek
fsg_add(struct usb_configuration * c)2742b4d36f68SPiotr Wilczek int fsg_add(struct usb_configuration *c)
2743b4d36f68SPiotr Wilczek {
2744b4d36f68SPiotr Wilczek struct fsg_common *fsg_common;
2745b4d36f68SPiotr Wilczek
2746b4d36f68SPiotr Wilczek fsg_common = fsg_common_init(NULL, c->cdev);
2747b4d36f68SPiotr Wilczek
2748b4d36f68SPiotr Wilczek fsg_common->vendor_name = 0;
2749b4d36f68SPiotr Wilczek fsg_common->product_name = 0;
2750b4d36f68SPiotr Wilczek fsg_common->release = 0xffff;
2751b4d36f68SPiotr Wilczek
2752b4d36f68SPiotr Wilczek fsg_common->ops = NULL;
2753b4d36f68SPiotr Wilczek fsg_common->private_data = NULL;
2754b4d36f68SPiotr Wilczek
2755b4d36f68SPiotr Wilczek the_fsg_common = fsg_common;
2756b4d36f68SPiotr Wilczek
2757b4d36f68SPiotr Wilczek return fsg_bind_config(c->cdev, c, fsg_common);
2758b4d36f68SPiotr Wilczek }
2759b4d36f68SPiotr Wilczek
fsg_init(struct ums * ums_devs,int count)276002585eb3SStephen Warren int fsg_init(struct ums *ums_devs, int count)
2761b4d36f68SPiotr Wilczek {
276202585eb3SStephen Warren ums = ums_devs;
276302585eb3SStephen Warren ums_count = count;
2764b4d36f68SPiotr Wilczek
2765b4d36f68SPiotr Wilczek return 0;
2766b4d36f68SPiotr Wilczek }
2767c4d0e856SMateusz Zalega
2768c4d0e856SMateusz Zalega DECLARE_GADGET_BIND_CALLBACK(usb_dnl_ums, fsg_add);
2769