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