]>
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 | ||
8ea864cf | 1572 | static int throw_away_data(struct fsg_common *common) |
d5e2b67a MN |
1573 | { |
1574 | struct fsg_buffhd *bh; | |
1575 | u32 amount; | |
1576 | int rc; | |
1577 | ||
8ea864cf MN |
1578 | for (bh = common->next_buffhd_to_drain; |
1579 | bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; | |
1580 | bh = common->next_buffhd_to_drain) { | |
d5e2b67a MN |
1581 | |
1582 | /* Throw away the data in a filled buffer */ | |
1583 | if (bh->state == BUF_STATE_FULL) { | |
1584 | smp_rmb(); | |
1585 | bh->state = BUF_STATE_EMPTY; | |
8ea864cf | 1586 | common->next_buffhd_to_drain = bh->next; |
d5e2b67a MN |
1587 | |
1588 | /* A short packet or an error ends everything */ | |
1589 | if (bh->outreq->actual != bh->outreq->length || | |
b73af61e | 1590 | bh->outreq->status != 0) { |
8ea864cf MN |
1591 | raise_exception(common, |
1592 | FSG_STATE_ABORT_BULK_OUT); | |
d5e2b67a MN |
1593 | return -EINTR; |
1594 | } | |
1595 | continue; | |
1596 | } | |
1597 | ||
1598 | /* Try to submit another request if we need one */ | |
8ea864cf MN |
1599 | bh = common->next_buffhd_to_fill; |
1600 | if (bh->state == BUF_STATE_EMPTY | |
1601 | && common->usb_amount_left > 0) { | |
1602 | amount = min(common->usb_amount_left, FSG_BUFLEN); | |
d5e2b67a | 1603 | |
b73af61e MN |
1604 | /* |
1605 | * amount is always divisible by 512, hence by | |
1606 | * the bulk-out maxpacket size. | |
1607 | */ | |
d26a6aa0 | 1608 | bh->outreq->length = amount; |
d5e2b67a | 1609 | bh->outreq->short_not_ok = 1; |
fe52f792 | 1610 | if (!start_out_transfer(common, bh)) |
b73af61e | 1611 | /* Dunno what to do if common->fsg is NULL */ |
8ea864cf MN |
1612 | return -EIO; |
1613 | common->next_buffhd_to_fill = bh->next; | |
1614 | common->usb_amount_left -= amount; | |
d5e2b67a MN |
1615 | continue; |
1616 | } | |
1617 | ||
1618 | /* Otherwise wait for something to happen */ | |
8ea864cf | 1619 | rc = sleep_thread(common); |
d5e2b67a MN |
1620 | if (rc) |
1621 | return rc; | |
1622 | } | |
1623 | return 0; | |
1624 | } | |
1625 | ||
8ea864cf | 1626 | static int finish_reply(struct fsg_common *common) |
d5e2b67a | 1627 | { |
8ea864cf | 1628 | struct fsg_buffhd *bh = common->next_buffhd_to_fill; |
d5e2b67a MN |
1629 | int rc = 0; |
1630 | ||
8ea864cf | 1631 | switch (common->data_dir) { |
d5e2b67a | 1632 | case DATA_DIR_NONE: |
d26a6aa0 | 1633 | break; /* Nothing to send */ |
d5e2b67a | 1634 | |
b73af61e MN |
1635 | /* |
1636 | * If we don't know whether the host wants to read or write, | |
d5e2b67a MN |
1637 | * this must be CB or CBI with an unknown command. We mustn't |
1638 | * try to send or receive any data. So stall both bulk pipes | |
b73af61e MN |
1639 | * if we can and wait for a reset. |
1640 | */ | |
d5e2b67a | 1641 | case DATA_DIR_UNKNOWN: |
8ea864cf MN |
1642 | if (!common->can_stall) { |
1643 | /* Nothing */ | |
1644 | } else if (fsg_is_set(common)) { | |
1645 | fsg_set_halt(common->fsg, common->fsg->bulk_out); | |
1646 | rc = halt_bulk_in_endpoint(common->fsg); | |
1647 | } else { | |
1648 | /* Don't know what to do if common->fsg is NULL */ | |
1649 | rc = -EIO; | |
d5e2b67a MN |
1650 | } |
1651 | break; | |
1652 | ||
1653 | /* All but the last buffer of data must have already been sent */ | |
1654 | case DATA_DIR_TO_HOST: | |
8ea864cf | 1655 | if (common->data_size == 0) { |
93bcf12e | 1656 | /* Nothing to send */ |
d5e2b67a | 1657 | |
ee81b3e0 AS |
1658 | /* Don't know what to do if common->fsg is NULL */ |
1659 | } else if (!fsg_is_set(common)) { | |
1660 | rc = -EIO; | |
1661 | ||
d5e2b67a | 1662 | /* If there's no residue, simply send the last buffer */ |
8ea864cf | 1663 | } else if (common->residue == 0) { |
d5e2b67a | 1664 | bh->inreq->zero = 0; |
fe52f792 | 1665 | if (!start_in_transfer(common, bh)) |
8ea864cf MN |
1666 | return -EIO; |
1667 | common->next_buffhd_to_fill = bh->next; | |
d5e2b67a | 1668 | |
b73af61e | 1669 | /* |
ee81b3e0 AS |
1670 | * For Bulk-only, mark the end of the data with a short |
1671 | * packet. If we are allowed to stall, halt the bulk-in | |
1672 | * endpoint. (Note: This violates the Bulk-Only Transport | |
1673 | * specification, which requires us to pad the data if we | |
1674 | * don't halt the endpoint. Presumably nobody will mind.) | |
b73af61e | 1675 | */ |
ee81b3e0 | 1676 | } else { |
93bcf12e | 1677 | bh->inreq->zero = 1; |
fe52f792 | 1678 | if (!start_in_transfer(common, bh)) |
8ea864cf MN |
1679 | rc = -EIO; |
1680 | common->next_buffhd_to_fill = bh->next; | |
ee81b3e0 | 1681 | if (common->can_stall) |
8ea864cf | 1682 | rc = halt_bulk_in_endpoint(common->fsg); |
d5e2b67a MN |
1683 | } |
1684 | break; | |
1685 | ||
b73af61e MN |
1686 | /* |
1687 | * We have processed all we want from the data the host has sent. | |
1688 | * There may still be outstanding bulk-out requests. | |
1689 | */ | |
d5e2b67a | 1690 | case DATA_DIR_FROM_HOST: |
8ea864cf | 1691 | if (common->residue == 0) { |
d26a6aa0 | 1692 | /* Nothing to receive */ |
d5e2b67a MN |
1693 | |
1694 | /* Did the host stop sending unexpectedly early? */ | |
8ea864cf MN |
1695 | } else if (common->short_packet_received) { |
1696 | raise_exception(common, FSG_STATE_ABORT_BULK_OUT); | |
d5e2b67a | 1697 | rc = -EINTR; |
d5e2b67a | 1698 | |
b73af61e MN |
1699 | /* |
1700 | * We haven't processed all the incoming data. Even though | |
d5e2b67a MN |
1701 | * we may be allowed to stall, doing so would cause a race. |
1702 | * The controller may already have ACK'ed all the remaining | |
1703 | * bulk-out packets, in which case the host wouldn't see a | |
1704 | * STALL. Not realizing the endpoint was halted, it wouldn't | |
b73af61e MN |
1705 | * clear the halt -- leading to problems later on. |
1706 | */ | |
d5e2b67a | 1707 | #if 0 |
8ea864cf MN |
1708 | } else if (common->can_stall) { |
1709 | if (fsg_is_set(common)) | |
1710 | fsg_set_halt(common->fsg, | |
1711 | common->fsg->bulk_out); | |
1712 | raise_exception(common, FSG_STATE_ABORT_BULK_OUT); | |
d5e2b67a | 1713 | rc = -EINTR; |
d5e2b67a MN |
1714 | #endif |
1715 | ||
b73af61e MN |
1716 | /* |
1717 | * We can't stall. Read in the excess data and throw it | |
1718 | * all away. | |
1719 | */ | |
d26a6aa0 | 1720 | } else { |
8ea864cf | 1721 | rc = throw_away_data(common); |
d26a6aa0 | 1722 | } |
d5e2b67a MN |
1723 | break; |
1724 | } | |
1725 | return rc; | |
1726 | } | |
1727 | ||
8ea864cf | 1728 | static int send_status(struct fsg_common *common) |
d5e2b67a | 1729 | { |
8ea864cf | 1730 | struct fsg_lun *curlun = common->curlun; |
d5e2b67a | 1731 | struct fsg_buffhd *bh; |
93bcf12e | 1732 | struct bulk_cs_wrap *csw; |
d5e2b67a MN |
1733 | int rc; |
1734 | u8 status = USB_STATUS_PASS; | |
1735 | u32 sd, sdinfo = 0; | |
1736 | ||
1737 | /* Wait for the next buffer to become available */ | |
8ea864cf | 1738 | bh = common->next_buffhd_to_fill; |
d5e2b67a | 1739 | while (bh->state != BUF_STATE_EMPTY) { |
8ea864cf | 1740 | rc = sleep_thread(common); |
d5e2b67a MN |
1741 | if (rc) |
1742 | return rc; | |
1743 | } | |
1744 | ||
1745 | if (curlun) { | |
1746 | sd = curlun->sense_data; | |
1747 | sdinfo = curlun->sense_data_info; | |
8ea864cf | 1748 | } else if (common->bad_lun_okay) |
d5e2b67a MN |
1749 | sd = SS_NO_SENSE; |
1750 | else | |
1751 | sd = SS_LOGICAL_UNIT_NOT_SUPPORTED; | |
1752 | ||
8ea864cf MN |
1753 | if (common->phase_error) { |
1754 | DBG(common, "sending phase-error status\n"); | |
d5e2b67a MN |
1755 | status = USB_STATUS_PHASE_ERROR; |
1756 | sd = SS_INVALID_COMMAND; | |
1757 | } else if (sd != SS_NO_SENSE) { | |
8ea864cf | 1758 | DBG(common, "sending command-failure status\n"); |
d5e2b67a | 1759 | status = USB_STATUS_FAIL; |
8ea864cf | 1760 | VDBG(common, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;" |
d5e2b67a MN |
1761 | " info x%x\n", |
1762 | SK(sd), ASC(sd), ASCQ(sd), sdinfo); | |
1763 | } | |
1764 | ||
93bcf12e | 1765 | /* Store and send the Bulk-only CSW */ |
d26a6aa0 | 1766 | csw = (void *)bh->buf; |
d5e2b67a | 1767 | |
93bcf12e | 1768 | csw->Signature = cpu_to_le32(USB_BULK_CS_SIG); |
8ea864cf MN |
1769 | csw->Tag = common->tag; |
1770 | csw->Residue = cpu_to_le32(common->residue); | |
93bcf12e | 1771 | csw->Status = status; |
d5e2b67a | 1772 | |
93bcf12e MN |
1773 | bh->inreq->length = USB_BULK_CS_WRAP_LEN; |
1774 | bh->inreq->zero = 0; | |
fe52f792 | 1775 | if (!start_in_transfer(common, bh)) |
8ea864cf MN |
1776 | /* Don't know what to do if common->fsg is NULL */ |
1777 | return -EIO; | |
d5e2b67a | 1778 | |
8ea864cf | 1779 | common->next_buffhd_to_fill = bh->next; |
d5e2b67a MN |
1780 | return 0; |
1781 | } | |
1782 | ||
1783 | ||
1784 | /*-------------------------------------------------------------------------*/ | |
1785 | ||
b73af61e MN |
1786 | /* |
1787 | * Check whether the command is properly formed and whether its data size | |
1788 | * and direction agree with the values we already have. | |
1789 | */ | |
8ea864cf | 1790 | static int check_command(struct fsg_common *common, int cmnd_size, |
b73af61e MN |
1791 | enum data_direction data_dir, unsigned int mask, |
1792 | int needs_medium, const char *name) | |
d5e2b67a MN |
1793 | { |
1794 | int i; | |
8ea864cf | 1795 | int lun = common->cmnd[1] >> 5; |
d5e2b67a MN |
1796 | static const char dirletter[4] = {'u', 'o', 'i', 'n'}; |
1797 | char hdlen[20]; | |
1798 | struct fsg_lun *curlun; | |
1799 | ||
d5e2b67a | 1800 | hdlen[0] = 0; |
8ea864cf MN |
1801 | if (common->data_dir != DATA_DIR_UNKNOWN) |
1802 | sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir], | |
b73af61e | 1803 | common->data_size); |
8ea864cf | 1804 | VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", |
d26a6aa0 | 1805 | name, cmnd_size, dirletter[(int) data_dir], |
8ea864cf | 1806 | common->data_size_from_cmnd, common->cmnd_size, hdlen); |
d5e2b67a | 1807 | |
b73af61e MN |
1808 | /* |
1809 | * We can't reply at all until we know the correct data direction | |
1810 | * and size. | |
1811 | */ | |
8ea864cf | 1812 | if (common->data_size_from_cmnd == 0) |
d5e2b67a | 1813 | data_dir = DATA_DIR_NONE; |
8ea864cf | 1814 | if (common->data_size < common->data_size_from_cmnd) { |
b73af61e MN |
1815 | /* |
1816 | * Host data size < Device data size is a phase error. | |
8ea864cf | 1817 | * Carry out the command, but only transfer as much as |
b73af61e MN |
1818 | * we are allowed. |
1819 | */ | |
8ea864cf MN |
1820 | common->data_size_from_cmnd = common->data_size; |
1821 | common->phase_error = 1; | |
d5e2b67a | 1822 | } |
8ea864cf MN |
1823 | common->residue = common->data_size; |
1824 | common->usb_amount_left = common->data_size; | |
d5e2b67a MN |
1825 | |
1826 | /* Conflicting data directions is a phase error */ | |
b73af61e | 1827 | if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) { |
8ea864cf | 1828 | common->phase_error = 1; |
d5e2b67a MN |
1829 | return -EINVAL; |
1830 | } | |
1831 | ||
1832 | /* Verify the length of the command itself */ | |
8ea864cf | 1833 | if (cmnd_size != common->cmnd_size) { |
d5e2b67a | 1834 | |
b73af61e MN |
1835 | /* |
1836 | * Special case workaround: There are plenty of buggy SCSI | |
d5e2b67a MN |
1837 | * implementations. Many have issues with cbw->Length |
1838 | * field passing a wrong command size. For those cases we | |
1839 | * always try to work around the problem by using the length | |
1840 | * sent by the host side provided it is at least as large | |
1841 | * as the correct command length. | |
1842 | * Examples of such cases would be MS-Windows, which issues | |
1843 | * REQUEST SENSE with cbw->Length == 12 where it should | |
1844 | * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and | |
1845 | * REQUEST SENSE with cbw->Length == 10 where it should | |
1846 | * be 6 as well. | |
1847 | */ | |
8ea864cf MN |
1848 | if (cmnd_size <= common->cmnd_size) { |
1849 | DBG(common, "%s is buggy! Expected length %d " | |
a41ae418 | 1850 | "but we got %d\n", name, |
8ea864cf MN |
1851 | cmnd_size, common->cmnd_size); |
1852 | cmnd_size = common->cmnd_size; | |
d5e2b67a | 1853 | } else { |
8ea864cf | 1854 | common->phase_error = 1; |
d5e2b67a MN |
1855 | return -EINVAL; |
1856 | } | |
1857 | } | |
1858 | ||
1859 | /* Check that the LUN values are consistent */ | |
8ea864cf MN |
1860 | if (common->lun != lun) |
1861 | DBG(common, "using LUN %d from CBW, not LUN %d from CDB\n", | |
1862 | common->lun, lun); | |
d5e2b67a MN |
1863 | |
1864 | /* Check the LUN */ | |
8ea864cf MN |
1865 | if (common->lun >= 0 && common->lun < common->nluns) { |
1866 | curlun = &common->luns[common->lun]; | |
1867 | common->curlun = curlun; | |
0a6a717c | 1868 | if (common->cmnd[0] != REQUEST_SENSE) { |
d5e2b67a MN |
1869 | curlun->sense_data = SS_NO_SENSE; |
1870 | curlun->sense_data_info = 0; | |
1871 | curlun->info_valid = 0; | |
1872 | } | |
1873 | } else { | |
8ea864cf MN |
1874 | common->curlun = NULL; |
1875 | curlun = NULL; | |
1876 | common->bad_lun_okay = 0; | |
d5e2b67a | 1877 | |
b73af61e MN |
1878 | /* |
1879 | * INQUIRY and REQUEST SENSE commands are explicitly allowed | |
1880 | * to use unsupported LUNs; all others may not. | |
1881 | */ | |
0a6a717c MN |
1882 | if (common->cmnd[0] != INQUIRY && |
1883 | common->cmnd[0] != REQUEST_SENSE) { | |
8ea864cf | 1884 | DBG(common, "unsupported LUN %d\n", common->lun); |
d5e2b67a MN |
1885 | return -EINVAL; |
1886 | } | |
1887 | } | |
1888 | ||
b73af61e MN |
1889 | /* |
1890 | * If a unit attention condition exists, only INQUIRY and | |
1891 | * REQUEST SENSE commands are allowed; anything else must fail. | |
1892 | */ | |
d5e2b67a | 1893 | if (curlun && curlun->unit_attention_data != SS_NO_SENSE && |
b73af61e MN |
1894 | common->cmnd[0] != INQUIRY && |
1895 | common->cmnd[0] != REQUEST_SENSE) { | |
d5e2b67a MN |
1896 | curlun->sense_data = curlun->unit_attention_data; |
1897 | curlun->unit_attention_data = SS_NO_SENSE; | |
1898 | return -EINVAL; | |
1899 | } | |
1900 | ||
1901 | /* Check that only command bytes listed in the mask are non-zero */ | |
8ea864cf | 1902 | common->cmnd[1] &= 0x1f; /* Mask away the LUN */ |
d5e2b67a | 1903 | for (i = 1; i < cmnd_size; ++i) { |
8ea864cf | 1904 | if (common->cmnd[i] && !(mask & (1 << i))) { |
d5e2b67a MN |
1905 | if (curlun) |
1906 | curlun->sense_data = SS_INVALID_FIELD_IN_CDB; | |
1907 | return -EINVAL; | |
1908 | } | |
1909 | } | |
1910 | ||
1911 | /* If the medium isn't mounted and the command needs to access | |
1912 | * it, return an error. */ | |
1913 | if (curlun && !fsg_lun_is_open(curlun) && needs_medium) { | |
1914 | curlun->sense_data = SS_MEDIUM_NOT_PRESENT; | |
1915 | return -EINVAL; | |
1916 | } | |
1917 | ||
1918 | return 0; | |
1919 | } | |
1920 | ||
8ea864cf | 1921 | static int do_scsi_command(struct fsg_common *common) |
d5e2b67a MN |
1922 | { |
1923 | struct fsg_buffhd *bh; | |
1924 | int rc; | |
1925 | int reply = -EINVAL; | |
1926 | int i; | |
1927 | static char unknown[16]; | |
1928 | ||
8ea864cf | 1929 | dump_cdb(common); |
d5e2b67a MN |
1930 | |
1931 | /* Wait for the next buffer to become available for data or status */ | |
8ea864cf MN |
1932 | bh = common->next_buffhd_to_fill; |
1933 | common->next_buffhd_to_drain = bh; | |
d5e2b67a | 1934 | while (bh->state != BUF_STATE_EMPTY) { |
8ea864cf | 1935 | rc = sleep_thread(common); |
d5e2b67a MN |
1936 | if (rc) |
1937 | return rc; | |
1938 | } | |
8ea864cf MN |
1939 | common->phase_error = 0; |
1940 | common->short_packet_received = 0; | |
d5e2b67a | 1941 | |
8ea864cf MN |
1942 | down_read(&common->filesem); /* We're using the backing file */ |
1943 | switch (common->cmnd[0]) { | |
d5e2b67a | 1944 | |
0a6a717c | 1945 | case INQUIRY: |
8ea864cf MN |
1946 | common->data_size_from_cmnd = common->cmnd[4]; |
1947 | reply = check_command(common, 6, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
1948 | (1<<4), 0, |
1949 | "INQUIRY"); | |
1950 | if (reply == 0) | |
8ea864cf | 1951 | reply = do_inquiry(common, bh); |
d5e2b67a MN |
1952 | break; |
1953 | ||
0a6a717c | 1954 | case MODE_SELECT: |
8ea864cf MN |
1955 | common->data_size_from_cmnd = common->cmnd[4]; |
1956 | reply = check_command(common, 6, DATA_DIR_FROM_HOST, | |
d26a6aa0 MN |
1957 | (1<<1) | (1<<4), 0, |
1958 | "MODE SELECT(6)"); | |
1959 | if (reply == 0) | |
8ea864cf | 1960 | reply = do_mode_select(common, bh); |
d5e2b67a MN |
1961 | break; |
1962 | ||
0a6a717c | 1963 | case MODE_SELECT_10: |
8ea864cf MN |
1964 | common->data_size_from_cmnd = |
1965 | get_unaligned_be16(&common->cmnd[7]); | |
1966 | reply = check_command(common, 10, DATA_DIR_FROM_HOST, | |
d26a6aa0 MN |
1967 | (1<<1) | (3<<7), 0, |
1968 | "MODE SELECT(10)"); | |
1969 | if (reply == 0) | |
8ea864cf | 1970 | reply = do_mode_select(common, bh); |
d5e2b67a MN |
1971 | break; |
1972 | ||
0a6a717c | 1973 | case MODE_SENSE: |
8ea864cf MN |
1974 | common->data_size_from_cmnd = common->cmnd[4]; |
1975 | reply = check_command(common, 6, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
1976 | (1<<1) | (1<<2) | (1<<4), 0, |
1977 | "MODE SENSE(6)"); | |
1978 | if (reply == 0) | |
8ea864cf | 1979 | reply = do_mode_sense(common, bh); |
d5e2b67a MN |
1980 | break; |
1981 | ||
0a6a717c | 1982 | case MODE_SENSE_10: |
8ea864cf MN |
1983 | common->data_size_from_cmnd = |
1984 | get_unaligned_be16(&common->cmnd[7]); | |
1985 | reply = check_command(common, 10, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
1986 | (1<<1) | (1<<2) | (3<<7), 0, |
1987 | "MODE SENSE(10)"); | |
1988 | if (reply == 0) | |
8ea864cf | 1989 | reply = do_mode_sense(common, bh); |
d5e2b67a MN |
1990 | break; |
1991 | ||
0a6a717c | 1992 | case ALLOW_MEDIUM_REMOVAL: |
8ea864cf MN |
1993 | common->data_size_from_cmnd = 0; |
1994 | reply = check_command(common, 6, DATA_DIR_NONE, | |
d26a6aa0 MN |
1995 | (1<<4), 0, |
1996 | "PREVENT-ALLOW MEDIUM REMOVAL"); | |
1997 | if (reply == 0) | |
8ea864cf | 1998 | reply = do_prevent_allow(common); |
d5e2b67a MN |
1999 | break; |
2000 | ||
0a6a717c | 2001 | case READ_6: |
8ea864cf MN |
2002 | i = common->cmnd[4]; |
2003 | common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; | |
2004 | reply = check_command(common, 6, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
2005 | (7<<1) | (1<<4), 1, |
2006 | "READ(6)"); | |
2007 | if (reply == 0) | |
8ea864cf | 2008 | reply = do_read(common); |
d5e2b67a MN |
2009 | break; |
2010 | ||
0a6a717c | 2011 | case READ_10: |
8ea864cf MN |
2012 | common->data_size_from_cmnd = |
2013 | get_unaligned_be16(&common->cmnd[7]) << 9; | |
2014 | reply = check_command(common, 10, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
2015 | (1<<1) | (0xf<<2) | (3<<7), 1, |
2016 | "READ(10)"); | |
2017 | if (reply == 0) | |
8ea864cf | 2018 | reply = do_read(common); |
d5e2b67a MN |
2019 | break; |
2020 | ||
0a6a717c | 2021 | case READ_12: |
8ea864cf MN |
2022 | common->data_size_from_cmnd = |
2023 | get_unaligned_be32(&common->cmnd[6]) << 9; | |
2024 | reply = check_command(common, 12, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
2025 | (1<<1) | (0xf<<2) | (0xf<<6), 1, |
2026 | "READ(12)"); | |
2027 | if (reply == 0) | |
8ea864cf | 2028 | reply = do_read(common); |
d5e2b67a MN |
2029 | break; |
2030 | ||
0a6a717c | 2031 | case READ_CAPACITY: |
8ea864cf MN |
2032 | common->data_size_from_cmnd = 8; |
2033 | reply = check_command(common, 10, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
2034 | (0xf<<2) | (1<<8), 1, |
2035 | "READ CAPACITY"); | |
2036 | if (reply == 0) | |
8ea864cf | 2037 | reply = do_read_capacity(common, bh); |
d5e2b67a MN |
2038 | break; |
2039 | ||
0a6a717c | 2040 | case READ_HEADER: |
8ea864cf | 2041 | if (!common->curlun || !common->curlun->cdrom) |
d5e2b67a | 2042 | goto unknown_cmnd; |
8ea864cf MN |
2043 | common->data_size_from_cmnd = |
2044 | get_unaligned_be16(&common->cmnd[7]); | |
2045 | reply = check_command(common, 10, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
2046 | (3<<7) | (0x1f<<1), 1, |
2047 | "READ HEADER"); | |
2048 | if (reply == 0) | |
8ea864cf | 2049 | reply = do_read_header(common, bh); |
d5e2b67a MN |
2050 | break; |
2051 | ||
0a6a717c | 2052 | case READ_TOC: |
8ea864cf | 2053 | if (!common->curlun || !common->curlun->cdrom) |
d5e2b67a | 2054 | goto unknown_cmnd; |
8ea864cf MN |
2055 | common->data_size_from_cmnd = |
2056 | get_unaligned_be16(&common->cmnd[7]); | |
2057 | reply = check_command(common, 10, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
2058 | (7<<6) | (1<<1), 1, |
2059 | "READ TOC"); | |
2060 | if (reply == 0) | |
8ea864cf | 2061 | reply = do_read_toc(common, bh); |
d5e2b67a MN |
2062 | break; |
2063 | ||
0a6a717c | 2064 | case READ_FORMAT_CAPACITIES: |
8ea864cf MN |
2065 | common->data_size_from_cmnd = |
2066 | get_unaligned_be16(&common->cmnd[7]); | |
2067 | reply = check_command(common, 10, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
2068 | (3<<7), 1, |
2069 | "READ FORMAT CAPACITIES"); | |
2070 | if (reply == 0) | |
8ea864cf | 2071 | reply = do_read_format_capacities(common, bh); |
d5e2b67a MN |
2072 | break; |
2073 | ||
0a6a717c | 2074 | case REQUEST_SENSE: |
8ea864cf MN |
2075 | common->data_size_from_cmnd = common->cmnd[4]; |
2076 | reply = check_command(common, 6, DATA_DIR_TO_HOST, | |
d26a6aa0 MN |
2077 | (1<<4), 0, |
2078 | "REQUEST SENSE"); | |
2079 | if (reply == 0) | |
8ea864cf | 2080 | reply = do_request_sense(common, bh); |
d5e2b67a MN |
2081 | break; |
2082 | ||
0a6a717c | 2083 | case START_STOP: |
8ea864cf MN |
2084 | common->data_size_from_cmnd = 0; |
2085 | reply = check_command(common, 6, DATA_DIR_NONE, | |
d26a6aa0 MN |
2086 | (1<<1) | (1<<4), 0, |
2087 | "START-STOP UNIT"); | |
2088 | if (reply == 0) | |
8ea864cf | 2089 | reply = do_start_stop(common); |
d5e2b67a MN |
2090 | break; |
2091 | ||
0a6a717c | 2092 | case SYNCHRONIZE_CACHE: |
8ea864cf MN |
2093 | common->data_size_from_cmnd = 0; |
2094 | reply = check_command(common, 10, DATA_DIR_NONE, | |
d26a6aa0 MN |
2095 | (0xf<<2) | (3<<7), 1, |
2096 | "SYNCHRONIZE CACHE"); | |
2097 | if (reply == 0) | |
8ea864cf | 2098 | reply = do_synchronize_cache(common); |
d5e2b67a MN |
2099 | break; |
2100 | ||
0a6a717c | 2101 | case TEST_UNIT_READY: |
8ea864cf MN |
2102 | common->data_size_from_cmnd = 0; |
2103 | reply = check_command(common, 6, DATA_DIR_NONE, | |
d5e2b67a MN |
2104 | 0, 1, |
2105 | "TEST UNIT READY"); | |
2106 | break; | |
2107 | ||
b73af61e MN |
2108 | /* |
2109 | * Although optional, this command is used by MS-Windows. We | |
2110 | * support a minimal version: BytChk must be 0. | |
2111 | */ | |
0a6a717c | 2112 | case VERIFY: |
8ea864cf MN |
2113 | common->data_size_from_cmnd = 0; |
2114 | reply = check_command(common, 10, DATA_DIR_NONE, | |
d26a6aa0 MN |
2115 | (1<<1) | (0xf<<2) | (3<<7), 1, |
2116 | "VERIFY"); | |
2117 | if (reply == 0) | |
8ea864cf | 2118 | reply = do_verify(common); |
d5e2b67a MN |
2119 | break; |
2120 | ||
0a6a717c | 2121 | case WRITE_6: |
8ea864cf MN |
2122 | i = common->cmnd[4]; |
2123 | common->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; | |
2124 | reply = check_command(common, 6, DATA_DIR_FROM_HOST, | |
d26a6aa0 MN |
2125 | (7<<1) | (1<<4), 1, |
2126 | "WRITE(6)"); | |
2127 | if (reply == 0) | |
8ea864cf | 2128 | reply = do_write(common); |
d5e2b67a MN |
2129 | break; |
2130 | ||
0a6a717c | 2131 | case WRITE_10: |
8ea864cf MN |
2132 | common->data_size_from_cmnd = |
2133 | get_unaligned_be16(&common->cmnd[7]) << 9; | |
2134 | reply = check_command(common, 10, DATA_DIR_FROM_HOST, | |
d26a6aa0 MN |
2135 | (1<<1) | (0xf<<2) | (3<<7), 1, |
2136 | "WRITE(10)"); | |
2137 | if (reply == 0) | |
8ea864cf | 2138 | reply = do_write(common); |
d5e2b67a MN |
2139 | break; |
2140 | ||
0a6a717c | 2141 | case WRITE_12: |
8ea864cf MN |
2142 | common->data_size_from_cmnd = |
2143 | get_unaligned_be32(&common->cmnd[6]) << 9; | |
2144 | reply = check_command(common, 12, DATA_DIR_FROM_HOST, | |
d26a6aa0 MN |
2145 | (1<<1) | (0xf<<2) | (0xf<<6), 1, |
2146 | "WRITE(12)"); | |
2147 | if (reply == 0) | |
8ea864cf | 2148 | reply = do_write(common); |
d5e2b67a MN |
2149 | break; |
2150 | ||
b73af61e MN |
2151 | /* |
2152 | * Some mandatory commands that we recognize but don't implement. | |
d5e2b67a MN |
2153 | * They don't mean much in this setting. It's left as an exercise |
2154 | * for anyone interested to implement RESERVE and RELEASE in terms | |
b73af61e MN |
2155 | * of Posix locks. |
2156 | */ | |
0a6a717c MN |
2157 | case FORMAT_UNIT: |
2158 | case RELEASE: | |
2159 | case RESERVE: | |
2160 | case SEND_DIAGNOSTIC: | |
d26a6aa0 | 2161 | /* Fall through */ |
d5e2b67a MN |
2162 | |
2163 | default: | |
d26a6aa0 | 2164 | unknown_cmnd: |
8ea864cf MN |
2165 | common->data_size_from_cmnd = 0; |
2166 | sprintf(unknown, "Unknown x%02x", common->cmnd[0]); | |
2167 | reply = check_command(common, common->cmnd_size, | |
d26a6aa0 MN |
2168 | DATA_DIR_UNKNOWN, 0xff, 0, unknown); |
2169 | if (reply == 0) { | |
8ea864cf | 2170 | common->curlun->sense_data = SS_INVALID_COMMAND; |
d5e2b67a MN |
2171 | reply = -EINVAL; |
2172 | } | |
2173 | break; | |
2174 | } | |
8ea864cf | 2175 | up_read(&common->filesem); |
d5e2b67a MN |
2176 | |
2177 | if (reply == -EINTR || signal_pending(current)) | |
2178 | return -EINTR; | |
2179 | ||
2180 | /* Set up the single reply buffer for finish_reply() */ | |
2181 | if (reply == -EINVAL) | |
d26a6aa0 | 2182 | reply = 0; /* Error reply length */ |
8ea864cf | 2183 | if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) { |
b73af61e | 2184 | reply = min((u32)reply, common->data_size_from_cmnd); |
d5e2b67a MN |
2185 | bh->inreq->length = reply; |
2186 | bh->state = BUF_STATE_FULL; | |
8ea864cf | 2187 | common->residue -= reply; |
d26a6aa0 | 2188 | } /* Otherwise it's already set */ |
d5e2b67a MN |
2189 | |
2190 | return 0; | |
2191 | } | |
2192 | ||
2193 | ||
2194 | /*-------------------------------------------------------------------------*/ | |
2195 | ||
2196 | static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh) | |
2197 | { | |
8ea864cf | 2198 | struct usb_request *req = bh->outreq; |
d5e2b67a | 2199 | struct fsg_bulk_cb_wrap *cbw = req->buf; |
8ea864cf | 2200 | struct fsg_common *common = fsg->common; |
d5e2b67a MN |
2201 | |
2202 | /* Was this a real packet? Should it be ignored? */ | |
2203 | if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags)) | |
2204 | return -EINVAL; | |
2205 | ||
2206 | /* Is the CBW valid? */ | |
2207 | if (req->actual != USB_BULK_CB_WRAP_LEN || | |
2208 | cbw->Signature != cpu_to_le32( | |
2209 | USB_BULK_CB_SIG)) { | |
2210 | DBG(fsg, "invalid CBW: len %u sig 0x%x\n", | |
2211 | req->actual, | |
2212 | le32_to_cpu(cbw->Signature)); | |
2213 | ||
b73af61e MN |
2214 | /* |
2215 | * The Bulk-only spec says we MUST stall the IN endpoint | |
d5e2b67a MN |
2216 | * (6.6.1), so it's unavoidable. It also says we must |
2217 | * retain this state until the next reset, but there's | |
2218 | * no way to tell the controller driver it should ignore | |
2219 | * Clear-Feature(HALT) requests. | |
2220 | * | |
2221 | * We aren't required to halt the OUT endpoint; instead | |
2222 | * we can simply accept and discard any data received | |
b73af61e MN |
2223 | * until the next reset. |
2224 | */ | |
d5e2b67a MN |
2225 | wedge_bulk_in_endpoint(fsg); |
2226 | set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); | |
2227 | return -EINVAL; | |
2228 | } | |
2229 | ||
2230 | /* Is the CBW meaningful? */ | |
2231 | if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG || | |
2232 | cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) { | |
2233 | DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, " | |
2234 | "cmdlen %u\n", | |
2235 | cbw->Lun, cbw->Flags, cbw->Length); | |
2236 | ||
b73af61e MN |
2237 | /* |
2238 | * We can do anything we want here, so let's stall the | |
2239 | * bulk pipes if we are allowed to. | |
2240 | */ | |
8ea864cf | 2241 | if (common->can_stall) { |
d5e2b67a MN |
2242 | fsg_set_halt(fsg, fsg->bulk_out); |
2243 | halt_bulk_in_endpoint(fsg); | |
2244 | } | |
2245 | return -EINVAL; | |
2246 | } | |
2247 | ||
2248 | /* Save the command for later */ | |
8ea864cf MN |
2249 | common->cmnd_size = cbw->Length; |
2250 | memcpy(common->cmnd, cbw->CDB, common->cmnd_size); | |
d5e2b67a | 2251 | if (cbw->Flags & USB_BULK_IN_FLAG) |
8ea864cf | 2252 | common->data_dir = DATA_DIR_TO_HOST; |
d5e2b67a | 2253 | else |
8ea864cf MN |
2254 | common->data_dir = DATA_DIR_FROM_HOST; |
2255 | common->data_size = le32_to_cpu(cbw->DataTransferLength); | |
2256 | if (common->data_size == 0) | |
2257 | common->data_dir = DATA_DIR_NONE; | |
2258 | common->lun = cbw->Lun; | |
2259 | common->tag = cbw->Tag; | |
d5e2b67a MN |
2260 | return 0; |
2261 | } | |
2262 | ||
8ea864cf | 2263 | static int get_next_command(struct fsg_common *common) |
d5e2b67a MN |
2264 | { |
2265 | struct fsg_buffhd *bh; | |
2266 | int rc = 0; | |
2267 | ||
93bcf12e | 2268 | /* Wait for the next buffer to become available */ |
8ea864cf | 2269 | bh = common->next_buffhd_to_fill; |
93bcf12e | 2270 | while (bh->state != BUF_STATE_EMPTY) { |
8ea864cf | 2271 | rc = sleep_thread(common); |
93bcf12e MN |
2272 | if (rc) |
2273 | return rc; | |
2274 | } | |
d5e2b67a | 2275 | |
93bcf12e | 2276 | /* Queue a request to read a Bulk-only CBW */ |
806e8f8f MYK |
2277 | bh->outreq->length = USB_BULK_CB_WRAP_LEN; |
2278 | bh->outreq->short_not_ok = 0; | |
fe52f792 | 2279 | if (!start_out_transfer(common, bh)) |
8ea864cf MN |
2280 | /* Don't know what to do if common->fsg is NULL */ |
2281 | return -EIO; | |
d5e2b67a | 2282 | |
b73af61e MN |
2283 | /* |
2284 | * We will drain the buffer in software, which means we | |
93bcf12e | 2285 | * can reuse it for the next filling. No need to advance |
b73af61e MN |
2286 | * next_buffhd_to_fill. |
2287 | */ | |
d5e2b67a | 2288 | |
93bcf12e MN |
2289 | /* Wait for the CBW to arrive */ |
2290 | while (bh->state != BUF_STATE_FULL) { | |
8ea864cf | 2291 | rc = sleep_thread(common); |
93bcf12e MN |
2292 | if (rc) |
2293 | return rc; | |
d5e2b67a | 2294 | } |
93bcf12e | 2295 | smp_rmb(); |
8ea864cf | 2296 | rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; |
93bcf12e MN |
2297 | bh->state = BUF_STATE_EMPTY; |
2298 | ||
d5e2b67a MN |
2299 | return rc; |
2300 | } | |
2301 | ||
2302 | ||
2303 | /*-------------------------------------------------------------------------*/ | |
2304 | ||
8ea864cf | 2305 | static int enable_endpoint(struct fsg_common *common, struct usb_ep *ep, |
d5e2b67a MN |
2306 | const struct usb_endpoint_descriptor *d) |
2307 | { | |
2308 | int rc; | |
2309 | ||
8ea864cf | 2310 | ep->driver_data = common; |
d5e2b67a MN |
2311 | rc = usb_ep_enable(ep, d); |
2312 | if (rc) | |
8ea864cf | 2313 | ERROR(common, "can't enable %s, result %d\n", ep->name, rc); |
d5e2b67a MN |
2314 | return rc; |
2315 | } | |
2316 | ||
8ea864cf | 2317 | static int alloc_request(struct fsg_common *common, struct usb_ep *ep, |
d5e2b67a MN |
2318 | struct usb_request **preq) |
2319 | { | |
2320 | *preq = usb_ep_alloc_request(ep, GFP_ATOMIC); | |
2321 | if (*preq) | |
2322 | return 0; | |
8ea864cf | 2323 | ERROR(common, "can't allocate request for %s\n", ep->name); |
d5e2b67a MN |
2324 | return -ENOMEM; |
2325 | } | |
2326 | ||
b894f60a MN |
2327 | /* Reset interface setting and re-init endpoint state (toggle etc). */ |
2328 | static int do_set_interface(struct fsg_common *common, struct fsg_dev *new_fsg) | |
d5e2b67a | 2329 | { |
b894f60a MN |
2330 | const struct usb_endpoint_descriptor *d; |
2331 | struct fsg_dev *fsg; | |
2332 | int i, rc = 0; | |
d5e2b67a | 2333 | |
8ea864cf MN |
2334 | if (common->running) |
2335 | DBG(common, "reset interface\n"); | |
d5e2b67a MN |
2336 | |
2337 | reset: | |
2338 | /* Deallocate the requests */ | |
b894f60a MN |
2339 | if (common->fsg) { |
2340 | fsg = common->fsg; | |
8ea864cf MN |
2341 | |
2342 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | |
2343 | struct fsg_buffhd *bh = &common->buffhds[i]; | |
d5e2b67a | 2344 | |
8ea864cf MN |
2345 | if (bh->inreq) { |
2346 | usb_ep_free_request(fsg->bulk_in, bh->inreq); | |
2347 | bh->inreq = NULL; | |
2348 | } | |
2349 | if (bh->outreq) { | |
2350 | usb_ep_free_request(fsg->bulk_out, bh->outreq); | |
2351 | bh->outreq = NULL; | |
2352 | } | |
d5e2b67a | 2353 | } |
8ea864cf MN |
2354 | |
2355 | /* Disable the endpoints */ | |
2356 | if (fsg->bulk_in_enabled) { | |
2357 | usb_ep_disable(fsg->bulk_in); | |
2358 | fsg->bulk_in_enabled = 0; | |
2359 | } | |
2360 | if (fsg->bulk_out_enabled) { | |
2361 | usb_ep_disable(fsg->bulk_out); | |
2362 | fsg->bulk_out_enabled = 0; | |
d5e2b67a | 2363 | } |
d5e2b67a | 2364 | |
b894f60a MN |
2365 | common->fsg = NULL; |
2366 | wake_up(&common->fsg_wait); | |
d5e2b67a | 2367 | } |
d5e2b67a | 2368 | |
8ea864cf | 2369 | common->running = 0; |
b894f60a | 2370 | if (!new_fsg || rc) |
d5e2b67a MN |
2371 | return rc; |
2372 | ||
b894f60a MN |
2373 | common->fsg = new_fsg; |
2374 | fsg = common->fsg; | |
d5e2b67a | 2375 | |
b894f60a MN |
2376 | /* Enable the endpoints */ |
2377 | d = fsg_ep_desc(common->gadget, | |
2378 | &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc); | |
2379 | rc = enable_endpoint(common, fsg->bulk_in, d); | |
2380 | if (rc) | |
2381 | goto reset; | |
2382 | fsg->bulk_in_enabled = 1; | |
8ea864cf | 2383 | |
b894f60a MN |
2384 | d = fsg_ep_desc(common->gadget, |
2385 | &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc); | |
2386 | rc = enable_endpoint(common, fsg->bulk_out, d); | |
2387 | if (rc) | |
2388 | goto reset; | |
2389 | fsg->bulk_out_enabled = 1; | |
2390 | common->bulk_out_maxpacket = le16_to_cpu(d->wMaxPacketSize); | |
2391 | clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags); | |
2392 | ||
2393 | /* Allocate the requests */ | |
2394 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | |
2395 | struct fsg_buffhd *bh = &common->buffhds[i]; | |
2396 | ||
2397 | rc = alloc_request(common, fsg->bulk_in, &bh->inreq); | |
8ea864cf | 2398 | if (rc) |
d5e2b67a | 2399 | goto reset; |
b894f60a | 2400 | rc = alloc_request(common, fsg->bulk_out, &bh->outreq); |
8ea864cf | 2401 | if (rc) |
d5e2b67a | 2402 | goto reset; |
b894f60a MN |
2403 | bh->inreq->buf = bh->outreq->buf = bh->buf; |
2404 | bh->inreq->context = bh->outreq->context = bh; | |
2405 | bh->inreq->complete = bulk_in_complete; | |
2406 | bh->outreq->complete = bulk_out_complete; | |
8ea864cf | 2407 | } |
d5e2b67a | 2408 | |
b894f60a MN |
2409 | common->running = 1; |
2410 | for (i = 0; i < common->nluns; ++i) | |
2411 | common->luns[i].unit_attention_data = SS_RESET_OCCURRED; | |
d5e2b67a MN |
2412 | return rc; |
2413 | } | |
2414 | ||
2415 | ||
d23b0f08 MN |
2416 | /****************************** ALT CONFIGS ******************************/ |
2417 | ||
d23b0f08 MN |
2418 | static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
2419 | { | |
2420 | struct fsg_dev *fsg = fsg_from_func(f); | |
b894f60a | 2421 | fsg->common->new_fsg = fsg; |
8ea864cf | 2422 | raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); |
d23b0f08 MN |
2423 | return 0; |
2424 | } | |
2425 | ||
2426 | static void fsg_disable(struct usb_function *f) | |
2427 | { | |
2428 | struct fsg_dev *fsg = fsg_from_func(f); | |
b894f60a | 2429 | fsg->common->new_fsg = NULL; |
8ea864cf | 2430 | raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); |
d23b0f08 MN |
2431 | } |
2432 | ||
2433 | ||
d5e2b67a MN |
2434 | /*-------------------------------------------------------------------------*/ |
2435 | ||
8ea864cf | 2436 | static void handle_exception(struct fsg_common *common) |
d5e2b67a MN |
2437 | { |
2438 | siginfo_t info; | |
d5e2b67a | 2439 | int i; |
d5e2b67a MN |
2440 | struct fsg_buffhd *bh; |
2441 | enum fsg_state old_state; | |
d5e2b67a MN |
2442 | struct fsg_lun *curlun; |
2443 | unsigned int exception_req_tag; | |
d5e2b67a | 2444 | |
b73af61e MN |
2445 | /* |
2446 | * Clear the existing signals. Anything but SIGUSR1 is converted | |
2447 | * into a high-priority EXIT exception. | |
2448 | */ | |
d5e2b67a | 2449 | for (;;) { |
b894f60a MN |
2450 | int sig = |
2451 | dequeue_signal_lock(current, ¤t->blocked, &info); | |
d5e2b67a MN |
2452 | if (!sig) |
2453 | break; | |
2454 | if (sig != SIGUSR1) { | |
8ea864cf MN |
2455 | if (common->state < FSG_STATE_EXIT) |
2456 | DBG(common, "Main thread exiting on signal\n"); | |
2457 | raise_exception(common, FSG_STATE_EXIT); | |
d5e2b67a MN |
2458 | } |
2459 | } | |
2460 | ||
2461 | /* Cancel all the pending transfers */ | |
b894f60a | 2462 | if (likely(common->fsg)) { |
d5e2b67a | 2463 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { |
8ea864cf MN |
2464 | bh = &common->buffhds[i]; |
2465 | if (bh->inreq_busy) | |
2466 | usb_ep_dequeue(common->fsg->bulk_in, bh->inreq); | |
2467 | if (bh->outreq_busy) | |
2468 | usb_ep_dequeue(common->fsg->bulk_out, | |
2469 | bh->outreq); | |
d5e2b67a | 2470 | } |
d5e2b67a | 2471 | |
8ea864cf MN |
2472 | /* Wait until everything is idle */ |
2473 | for (;;) { | |
2474 | int num_active = 0; | |
2475 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | |
2476 | bh = &common->buffhds[i]; | |
2477 | num_active += bh->inreq_busy + bh->outreq_busy; | |
2478 | } | |
2479 | if (num_active == 0) | |
2480 | break; | |
2481 | if (sleep_thread(common)) | |
2482 | return; | |
2483 | } | |
2484 | ||
2485 | /* Clear out the controller's fifos */ | |
2486 | if (common->fsg->bulk_in_enabled) | |
2487 | usb_ep_fifo_flush(common->fsg->bulk_in); | |
2488 | if (common->fsg->bulk_out_enabled) | |
2489 | usb_ep_fifo_flush(common->fsg->bulk_out); | |
2490 | } | |
d5e2b67a | 2491 | |
b73af61e MN |
2492 | /* |
2493 | * Reset the I/O buffer states and pointers, the SCSI | |
2494 | * state, and the exception. Then invoke the handler. | |
2495 | */ | |
8ea864cf | 2496 | spin_lock_irq(&common->lock); |
d5e2b67a MN |
2497 | |
2498 | for (i = 0; i < FSG_NUM_BUFFERS; ++i) { | |
8ea864cf | 2499 | bh = &common->buffhds[i]; |
d5e2b67a MN |
2500 | bh->state = BUF_STATE_EMPTY; |
2501 | } | |
8ea864cf MN |
2502 | common->next_buffhd_to_fill = &common->buffhds[0]; |
2503 | common->next_buffhd_to_drain = &common->buffhds[0]; | |
2504 | exception_req_tag = common->exception_req_tag; | |
8ea864cf | 2505 | old_state = common->state; |
d5e2b67a MN |
2506 | |
2507 | if (old_state == FSG_STATE_ABORT_BULK_OUT) | |
8ea864cf | 2508 | common->state = FSG_STATE_STATUS_PHASE; |
d5e2b67a | 2509 | else { |
8ea864cf MN |
2510 | for (i = 0; i < common->nluns; ++i) { |
2511 | curlun = &common->luns[i]; | |
d5e2b67a | 2512 | curlun->prevent_medium_removal = 0; |
d26a6aa0 MN |
2513 | curlun->sense_data = SS_NO_SENSE; |
2514 | curlun->unit_attention_data = SS_NO_SENSE; | |
d5e2b67a MN |
2515 | curlun->sense_data_info = 0; |
2516 | curlun->info_valid = 0; | |
2517 | } | |
8ea864cf | 2518 | common->state = FSG_STATE_IDLE; |
d5e2b67a | 2519 | } |
8ea864cf | 2520 | spin_unlock_irq(&common->lock); |
d5e2b67a MN |
2521 | |
2522 | /* Carry out any extra actions required for the exception */ | |
2523 | switch (old_state) { | |
d5e2b67a | 2524 | case FSG_STATE_ABORT_BULK_OUT: |
8ea864cf MN |
2525 | send_status(common); |
2526 | spin_lock_irq(&common->lock); | |
2527 | if (common->state == FSG_STATE_STATUS_PHASE) | |
2528 | common->state = FSG_STATE_IDLE; | |
2529 | spin_unlock_irq(&common->lock); | |
d5e2b67a MN |
2530 | break; |
2531 | ||
2532 | case FSG_STATE_RESET: | |
b73af61e MN |
2533 | /* |
2534 | * In case we were forced against our will to halt a | |
d5e2b67a | 2535 | * bulk endpoint, clear the halt now. (The SuperH UDC |
b73af61e MN |
2536 | * requires this.) |
2537 | */ | |
8ea864cf MN |
2538 | if (!fsg_is_set(common)) |
2539 | break; | |
2540 | if (test_and_clear_bit(IGNORE_BULK_OUT, | |
2541 | &common->fsg->atomic_bitflags)) | |
2542 | usb_ep_clear_halt(common->fsg->bulk_in); | |
d5e2b67a | 2543 | |
8ea864cf MN |
2544 | if (common->ep0_req_tag == exception_req_tag) |
2545 | ep0_queue(common); /* Complete the status stage */ | |
d5e2b67a | 2546 | |
b73af61e MN |
2547 | /* |
2548 | * Technically this should go here, but it would only be | |
d5e2b67a | 2549 | * a waste of time. Ditto for the INTERFACE_CHANGE and |
b73af61e MN |
2550 | * CONFIG_CHANGE cases. |
2551 | */ | |
8ea864cf MN |
2552 | /* for (i = 0; i < common->nluns; ++i) */ |
2553 | /* common->luns[i].unit_attention_data = */ | |
d26a6aa0 | 2554 | /* SS_RESET_OCCURRED; */ |
d5e2b67a MN |
2555 | break; |
2556 | ||
d5e2b67a | 2557 | case FSG_STATE_CONFIG_CHANGE: |
b894f60a | 2558 | do_set_interface(common, common->new_fsg); |
d5e2b67a MN |
2559 | break; |
2560 | ||
d5e2b67a MN |
2561 | case FSG_STATE_EXIT: |
2562 | case FSG_STATE_TERMINATED: | |
b894f60a | 2563 | do_set_interface(common, NULL); /* Free resources */ |
8ea864cf MN |
2564 | spin_lock_irq(&common->lock); |
2565 | common->state = FSG_STATE_TERMINATED; /* Stop the thread */ | |
2566 | spin_unlock_irq(&common->lock); | |
d5e2b67a | 2567 | break; |
d23b0f08 MN |
2568 | |
2569 | case FSG_STATE_INTERFACE_CHANGE: | |
2570 | case FSG_STATE_DISCONNECT: | |
2571 | case FSG_STATE_COMMAND_PHASE: | |
2572 | case FSG_STATE_DATA_PHASE: | |
2573 | case FSG_STATE_STATUS_PHASE: | |
2574 | case FSG_STATE_IDLE: | |
2575 | break; | |
d5e2b67a MN |
2576 | } |
2577 | } | |
2578 | ||
2579 | ||
2580 | /*-------------------------------------------------------------------------*/ | |
2581 | ||
8ea864cf | 2582 | static int fsg_main_thread(void *common_) |
d5e2b67a | 2583 | { |
8ea864cf | 2584 | struct fsg_common *common = common_; |
d5e2b67a | 2585 | |
b73af61e MN |
2586 | /* |
2587 | * Allow the thread to be killed by a signal, but set the signal mask | |
2588 | * to block everything but INT, TERM, KILL, and USR1. | |
2589 | */ | |
d5e2b67a MN |
2590 | allow_signal(SIGINT); |
2591 | allow_signal(SIGTERM); | |
2592 | allow_signal(SIGKILL); | |
2593 | allow_signal(SIGUSR1); | |
2594 | ||
2595 | /* Allow the thread to be frozen */ | |
2596 | set_freezable(); | |
2597 | ||
b73af61e MN |
2598 | /* |
2599 | * Arrange for userspace references to be interpreted as kernel | |
d5e2b67a | 2600 | * pointers. That way we can pass a kernel pointer to a routine |
b73af61e MN |
2601 | * that expects a __user pointer and it will work okay. |
2602 | */ | |
d5e2b67a MN |
2603 | set_fs(get_ds()); |
2604 | ||
2605 | /* The main loop */ | |
8ea864cf MN |
2606 | while (common->state != FSG_STATE_TERMINATED) { |
2607 | if (exception_in_progress(common) || signal_pending(current)) { | |
2608 | handle_exception(common); | |
d5e2b67a MN |
2609 | continue; |
2610 | } | |
2611 | ||
8ea864cf MN |
2612 | if (!common->running) { |
2613 | sleep_thread(common); | |
d5e2b67a MN |
2614 | continue; |
2615 | } | |
2616 | ||
8ea864cf | 2617 | if (get_next_command(common)) |
d5e2b67a MN |
2618 | continue; |
2619 | ||
8ea864cf MN |
2620 | spin_lock_irq(&common->lock); |
2621 | if (!exception_in_progress(common)) | |
2622 | common->state = FSG_STATE_DATA_PHASE; | |
2623 | spin_unlock_irq(&common->lock); | |
d5e2b67a | 2624 | |
8ea864cf | 2625 | if (do_scsi_command(common) || finish_reply(common)) |
d5e2b67a MN |
2626 | continue; |
2627 | ||
8ea864cf MN |
2628 | spin_lock_irq(&common->lock); |
2629 | if (!exception_in_progress(common)) | |
2630 | common->state = FSG_STATE_STATUS_PHASE; | |
2631 | spin_unlock_irq(&common->lock); | |
d5e2b67a | 2632 | |
8ea864cf | 2633 | if (send_status(common)) |
d5e2b67a MN |
2634 | continue; |
2635 | ||
8ea864cf MN |
2636 | spin_lock_irq(&common->lock); |
2637 | if (!exception_in_progress(common)) | |
2638 | common->state = FSG_STATE_IDLE; | |
2639 | spin_unlock_irq(&common->lock); | |
d23b0f08 | 2640 | } |
d5e2b67a | 2641 | |
8ea864cf MN |
2642 | spin_lock_irq(&common->lock); |
2643 | common->thread_task = NULL; | |
2644 | spin_unlock_irq(&common->lock); | |
d5e2b67a | 2645 | |
8876f5e7 MN |
2646 | if (!common->ops || !common->ops->thread_exits |
2647 | || common->ops->thread_exits(common) < 0) { | |
7f1ee826 MN |
2648 | struct fsg_lun *curlun = common->luns; |
2649 | unsigned i = common->nluns; | |
2650 | ||
2651 | down_write(&common->filesem); | |
2652 | for (; i--; ++curlun) { | |
2653 | if (!fsg_lun_is_open(curlun)) | |
2654 | continue; | |
2655 | ||
2656 | fsg_lun_close(curlun); | |
2657 | curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT; | |
2658 | } | |
2659 | up_write(&common->filesem); | |
2660 | } | |
d5e2b67a | 2661 | |
00cb636e | 2662 | /* Let fsg_unbind() know the thread has exited */ |
8ea864cf | 2663 | complete_and_exit(&common->thread_notifier, 0); |
d5e2b67a MN |
2664 | } |
2665 | ||
2666 | ||
9c610213 | 2667 | /*************************** DEVICE ATTRIBUTES ***************************/ |
d5e2b67a | 2668 | |
d23b0f08 MN |
2669 | /* Write permission is checked per LUN in store_*() functions. */ |
2670 | static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro); | |
9cfe745e | 2671 | static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, fsg_store_nofua); |
d23b0f08 | 2672 | static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file); |
d5e2b67a MN |
2673 | |
2674 | ||
9c610213 MN |
2675 | /****************************** FSG COMMON ******************************/ |
2676 | ||
2677 | static void fsg_common_release(struct kref *ref); | |
d5e2b67a | 2678 | |
9c610213 | 2679 | static void fsg_lun_release(struct device *dev) |
d5e2b67a | 2680 | { |
9c610213 | 2681 | /* Nothing needs to be done */ |
d5e2b67a MN |
2682 | } |
2683 | ||
9c610213 | 2684 | static inline void fsg_common_get(struct fsg_common *common) |
d5e2b67a | 2685 | { |
9c610213 | 2686 | kref_get(&common->ref); |
d5e2b67a MN |
2687 | } |
2688 | ||
9c610213 MN |
2689 | static inline void fsg_common_put(struct fsg_common *common) |
2690 | { | |
2691 | kref_put(&common->ref, fsg_common_release); | |
2692 | } | |
2693 | ||
9c610213 | 2694 | static struct fsg_common *fsg_common_init(struct fsg_common *common, |
481e4929 MN |
2695 | struct usb_composite_dev *cdev, |
2696 | struct fsg_config *cfg) | |
9c610213 | 2697 | { |
d23b0f08 | 2698 | struct usb_gadget *gadget = cdev->gadget; |
9c610213 MN |
2699 | struct fsg_buffhd *bh; |
2700 | struct fsg_lun *curlun; | |
481e4929 | 2701 | struct fsg_lun_config *lcfg; |
9c610213 | 2702 | int nluns, i, rc; |
d23b0f08 | 2703 | char *pathbuf; |
9c610213 MN |
2704 | |
2705 | /* Find out how many LUNs there should be */ | |
481e4929 | 2706 | nluns = cfg->nluns; |
9c610213 MN |
2707 | if (nluns < 1 || nluns > FSG_MAX_LUNS) { |
2708 | dev_err(&gadget->dev, "invalid number of LUNs: %u\n", nluns); | |
2709 | return ERR_PTR(-EINVAL); | |
2710 | } | |
2711 | ||
2712 | /* Allocate? */ | |
2713 | if (!common) { | |
2714 | common = kzalloc(sizeof *common, GFP_KERNEL); | |
2715 | if (!common) | |
2716 | return ERR_PTR(-ENOMEM); | |
2717 | common->free_storage_on_release = 1; | |
2718 | } else { | |
a283c03a | 2719 | memset(common, 0, sizeof *common); |
9c610213 MN |
2720 | common->free_storage_on_release = 0; |
2721 | } | |
8ea864cf | 2722 | |
8876f5e7 | 2723 | common->ops = cfg->ops; |
c85efcb9 MN |
2724 | common->private_data = cfg->private_data; |
2725 | ||
9c610213 | 2726 | common->gadget = gadget; |
8ea864cf MN |
2727 | common->ep0 = gadget->ep0; |
2728 | common->ep0req = cdev->req; | |
2729 | ||
2730 | /* Maybe allocate device-global string IDs, and patch descriptors */ | |
2731 | if (fsg_strings[FSG_STRING_INTERFACE].id == 0) { | |
2732 | rc = usb_string_id(cdev); | |
b9e00088 MN |
2733 | if (unlikely(rc < 0)) |
2734 | goto error_release; | |
8ea864cf MN |
2735 | fsg_strings[FSG_STRING_INTERFACE].id = rc; |
2736 | fsg_intf_desc.iInterface = rc; | |
2737 | } | |
9c610213 | 2738 | |
b73af61e MN |
2739 | /* |
2740 | * Create the LUNs, open their backing files, and register the | |
2741 | * LUN devices in sysfs. | |
2742 | */ | |
9c610213 | 2743 | curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL); |
b9e00088 MN |
2744 | if (unlikely(!curlun)) { |
2745 | rc = -ENOMEM; | |
2746 | goto error_release; | |
9c610213 MN |
2747 | } |
2748 | common->luns = curlun; | |
2749 | ||
2750 | init_rwsem(&common->filesem); | |
2751 | ||
481e4929 MN |
2752 | for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) { |
2753 | curlun->cdrom = !!lcfg->cdrom; | |
2754 | curlun->ro = lcfg->cdrom || lcfg->ro; | |
2755 | curlun->removable = lcfg->removable; | |
9c610213 MN |
2756 | curlun->dev.release = fsg_lun_release; |
2757 | curlun->dev.parent = &gadget->dev; | |
d23b0f08 | 2758 | /* curlun->dev.driver = &fsg_driver.driver; XXX */ |
9c610213 | 2759 | dev_set_drvdata(&curlun->dev, &common->filesem); |
e8b6f8c5 MN |
2760 | dev_set_name(&curlun->dev, |
2761 | cfg->lun_name_format | |
2762 | ? cfg->lun_name_format | |
2763 | : "lun%d", | |
2764 | i); | |
9c610213 MN |
2765 | |
2766 | rc = device_register(&curlun->dev); | |
2767 | if (rc) { | |
2768 | INFO(common, "failed to register LUN%d: %d\n", i, rc); | |
2769 | common->nluns = i; | |
17a93611 | 2770 | put_device(&curlun->dev); |
9c610213 MN |
2771 | goto error_release; |
2772 | } | |
2773 | ||
2774 | rc = device_create_file(&curlun->dev, &dev_attr_ro); | |
2775 | if (rc) | |
2776 | goto error_luns; | |
2777 | rc = device_create_file(&curlun->dev, &dev_attr_file); | |
9cfe745e MN |
2778 | if (rc) |
2779 | goto error_luns; | |
2780 | rc = device_create_file(&curlun->dev, &dev_attr_nofua); | |
9c610213 MN |
2781 | if (rc) |
2782 | goto error_luns; | |
2783 | ||
481e4929 MN |
2784 | if (lcfg->filename) { |
2785 | rc = fsg_lun_open(curlun, lcfg->filename); | |
9c610213 MN |
2786 | if (rc) |
2787 | goto error_luns; | |
481e4929 | 2788 | } else if (!curlun->removable) { |
9c610213 MN |
2789 | ERROR(common, "no file given for LUN%d\n", i); |
2790 | rc = -EINVAL; | |
2791 | goto error_luns; | |
2792 | } | |
2793 | } | |
2794 | common->nluns = nluns; | |
2795 | ||
9c610213 | 2796 | /* Data buffers cyclic list */ |
9c610213 | 2797 | bh = common->buffhds; |
aae86e8a MN |
2798 | i = FSG_NUM_BUFFERS; |
2799 | goto buffhds_first_it; | |
9c610213 MN |
2800 | do { |
2801 | bh->next = bh + 1; | |
aae86e8a MN |
2802 | ++bh; |
2803 | buffhds_first_it: | |
2804 | bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL); | |
2805 | if (unlikely(!bh->buf)) { | |
2806 | rc = -ENOMEM; | |
2807 | goto error_release; | |
2808 | } | |
2809 | } while (--i); | |
9c610213 MN |
2810 | bh->next = common->buffhds; |
2811 | ||
481e4929 MN |
2812 | /* Prepare inquiryString */ |
2813 | if (cfg->release != 0xffff) { | |
2814 | i = cfg->release; | |
2815 | } else { | |
90f79768 | 2816 | i = usb_gadget_controller_number(gadget); |
481e4929 MN |
2817 | if (i >= 0) { |
2818 | i = 0x0300 + i; | |
2819 | } else { | |
9c610213 MN |
2820 | WARNING(common, "controller '%s' not recognized\n", |
2821 | gadget->name); | |
481e4929 | 2822 | i = 0x0399; |
9c610213 MN |
2823 | } |
2824 | } | |
481e4929 | 2825 | snprintf(common->inquiry_string, sizeof common->inquiry_string, |
1ccd7923 | 2826 | "%-8s%-16s%04x", cfg->vendor_name ?: "Linux", |
481e4929 | 2827 | /* Assume product name dependent on the first LUN */ |
1ccd7923 | 2828 | cfg->product_name ?: (common->luns->cdrom |
481e4929 | 2829 | ? "File-Stor Gadget" |
1ccd7923 | 2830 | : "File-CD Gadget"), |
481e4929 | 2831 | i); |
9c610213 | 2832 | |
b73af61e MN |
2833 | /* |
2834 | * Some peripheral controllers are known not to be able to | |
9c610213 MN |
2835 | * halt bulk endpoints correctly. If one of them is present, |
2836 | * disable stalls. | |
2837 | */ | |
481e4929 | 2838 | common->can_stall = cfg->can_stall && |
90f79768 | 2839 | !(gadget_is_at91(common->gadget)); |
9c610213 | 2840 | |
8ea864cf | 2841 | spin_lock_init(&common->lock); |
9c610213 | 2842 | kref_init(&common->ref); |
8ea864cf | 2843 | |
8ea864cf MN |
2844 | /* Tell the thread to start working */ |
2845 | common->thread_task = | |
2846 | kthread_create(fsg_main_thread, common, | |
1ccd7923 | 2847 | cfg->thread_name ?: "file-storage"); |
8ea864cf MN |
2848 | if (IS_ERR(common->thread_task)) { |
2849 | rc = PTR_ERR(common->thread_task); | |
2850 | goto error_release; | |
2851 | } | |
2852 | init_completion(&common->thread_notifier); | |
b894f60a | 2853 | init_waitqueue_head(&common->fsg_wait); |
e8b6f8c5 | 2854 | |
d23b0f08 MN |
2855 | /* Information */ |
2856 | INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n"); | |
2857 | INFO(common, "Number of LUNs=%d\n", common->nluns); | |
2858 | ||
2859 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); | |
2860 | for (i = 0, nluns = common->nluns, curlun = common->luns; | |
2861 | i < nluns; | |
2862 | ++curlun, ++i) { | |
2863 | char *p = "(no medium)"; | |
2864 | if (fsg_lun_is_open(curlun)) { | |
2865 | p = "(error)"; | |
2866 | if (pathbuf) { | |
2867 | p = d_path(&curlun->filp->f_path, | |
2868 | pathbuf, PATH_MAX); | |
2869 | if (IS_ERR(p)) | |
2870 | p = "(error)"; | |
2871 | } | |
2872 | } | |
2873 | LINFO(curlun, "LUN: %s%s%sfile: %s\n", | |
2874 | curlun->removable ? "removable " : "", | |
2875 | curlun->ro ? "read only " : "", | |
2876 | curlun->cdrom ? "CD-ROM " : "", | |
2877 | p); | |
2878 | } | |
2879 | kfree(pathbuf); | |
2880 | ||
8ea864cf MN |
2881 | DBG(common, "I/O thread pid: %d\n", task_pid_nr(common->thread_task)); |
2882 | ||
2883 | wake_up_process(common->thread_task); | |
2884 | ||
9c610213 MN |
2885 | return common; |
2886 | ||
9c610213 MN |
2887 | error_luns: |
2888 | common->nluns = i + 1; | |
2889 | error_release: | |
8ea864cf | 2890 | common->state = FSG_STATE_TERMINATED; /* The thread is dead */ |
b73af61e | 2891 | /* Call fsg_common_release() directly, ref might be not initialised. */ |
9c610213 MN |
2892 | fsg_common_release(&common->ref); |
2893 | return ERR_PTR(rc); | |
2894 | } | |
2895 | ||
9c610213 MN |
2896 | static void fsg_common_release(struct kref *ref) |
2897 | { | |
b9e00088 | 2898 | struct fsg_common *common = container_of(ref, struct fsg_common, ref); |
9c610213 | 2899 | |
8ea864cf MN |
2900 | /* If the thread isn't already dead, tell it to exit now */ |
2901 | if (common->state != FSG_STATE_TERMINATED) { | |
2902 | raise_exception(common, FSG_STATE_EXIT); | |
2903 | wait_for_completion(&common->thread_notifier); | |
8ea864cf MN |
2904 | } |
2905 | ||
b9e00088 MN |
2906 | if (likely(common->luns)) { |
2907 | struct fsg_lun *lun = common->luns; | |
2908 | unsigned i = common->nluns; | |
2909 | ||
2910 | /* In error recovery common->nluns may be zero. */ | |
2911 | for (; i; --i, ++lun) { | |
9cfe745e | 2912 | device_remove_file(&lun->dev, &dev_attr_nofua); |
b9e00088 MN |
2913 | device_remove_file(&lun->dev, &dev_attr_ro); |
2914 | device_remove_file(&lun->dev, &dev_attr_file); | |
2915 | fsg_lun_close(lun); | |
2916 | device_unregister(&lun->dev); | |
2917 | } | |
9c610213 | 2918 | |
b9e00088 | 2919 | kfree(common->luns); |
9c610213 MN |
2920 | } |
2921 | ||
b9e00088 MN |
2922 | { |
2923 | struct fsg_buffhd *bh = common->buffhds; | |
2924 | unsigned i = FSG_NUM_BUFFERS; | |
2925 | do { | |
2926 | kfree(bh->buf); | |
2927 | } while (++bh, --i); | |
2928 | } | |
aae86e8a | 2929 | |
9c610213 MN |
2930 | if (common->free_storage_on_release) |
2931 | kfree(common); | |
2932 | } | |
2933 | ||
2934 | ||
2935 | /*-------------------------------------------------------------------------*/ | |
2936 | ||
d23b0f08 | 2937 | static void fsg_unbind(struct usb_configuration *c, struct usb_function *f) |
d5e2b67a | 2938 | { |
d23b0f08 | 2939 | struct fsg_dev *fsg = fsg_from_func(f); |
b894f60a | 2940 | struct fsg_common *common = fsg->common; |
d5e2b67a MN |
2941 | |
2942 | DBG(fsg, "unbind\n"); | |
b894f60a MN |
2943 | if (fsg->common->fsg == fsg) { |
2944 | fsg->common->new_fsg = NULL; | |
2945 | raise_exception(fsg->common, FSG_STATE_CONFIG_CHANGE); | |
2946 | /* FIXME: make interruptible or killable somehow? */ | |
2947 | wait_event(common->fsg_wait, common->fsg != fsg); | |
2948 | } | |
2949 | ||
2950 | fsg_common_put(common); | |
0fb2c2a1 MN |
2951 | usb_free_descriptors(fsg->function.descriptors); |
2952 | usb_free_descriptors(fsg->function.hs_descriptors); | |
9c610213 | 2953 | kfree(fsg); |
d5e2b67a MN |
2954 | } |
2955 | ||
28824b18 | 2956 | static int fsg_bind(struct usb_configuration *c, struct usb_function *f) |
d5e2b67a | 2957 | { |
d23b0f08 MN |
2958 | struct fsg_dev *fsg = fsg_from_func(f); |
2959 | struct usb_gadget *gadget = c->cdev->gadget; | |
d5e2b67a | 2960 | int i; |
d5e2b67a | 2961 | struct usb_ep *ep; |
d5e2b67a MN |
2962 | |
2963 | fsg->gadget = gadget; | |
d5e2b67a | 2964 | |
d23b0f08 MN |
2965 | /* New interface */ |
2966 | i = usb_interface_id(c, f); | |
2967 | if (i < 0) | |
2968 | return i; | |
2969 | fsg_intf_desc.bInterfaceNumber = i; | |
2970 | fsg->interface_number = i; | |
d5e2b67a | 2971 | |
d5e2b67a | 2972 | /* Find all the endpoints we will use */ |
d5e2b67a MN |
2973 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc); |
2974 | if (!ep) | |
2975 | goto autoconf_fail; | |
8ea864cf | 2976 | ep->driver_data = fsg->common; /* claim the endpoint */ |
d5e2b67a MN |
2977 | fsg->bulk_in = ep; |
2978 | ||
2979 | ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc); | |
2980 | if (!ep) | |
2981 | goto autoconf_fail; | |
8ea864cf | 2982 | ep->driver_data = fsg->common; /* claim the endpoint */ |
d5e2b67a MN |
2983 | fsg->bulk_out = ep; |
2984 | ||
e5fd39d9 MN |
2985 | /* Copy descriptors */ |
2986 | f->descriptors = usb_copy_descriptors(fsg_fs_function); | |
2987 | if (unlikely(!f->descriptors)) | |
2988 | return -ENOMEM; | |
2989 | ||
d5e2b67a | 2990 | if (gadget_is_dualspeed(gadget)) { |
d5e2b67a MN |
2991 | /* Assume endpoint addresses are the same for both speeds */ |
2992 | fsg_hs_bulk_in_desc.bEndpointAddress = | |
2993 | fsg_fs_bulk_in_desc.bEndpointAddress; | |
2994 | fsg_hs_bulk_out_desc.bEndpointAddress = | |
2995 | fsg_fs_bulk_out_desc.bEndpointAddress; | |
0fb2c2a1 | 2996 | f->hs_descriptors = usb_copy_descriptors(fsg_hs_function); |
e5fd39d9 MN |
2997 | if (unlikely(!f->hs_descriptors)) { |
2998 | usb_free_descriptors(f->descriptors); | |
0fb2c2a1 | 2999 | return -ENOMEM; |
e5fd39d9 | 3000 | } |
d5e2b67a MN |
3001 | } |
3002 | ||
d5e2b67a MN |
3003 | return 0; |
3004 | ||
3005 | autoconf_fail: | |
3006 | ERROR(fsg, "unable to autoconfigure all endpoints\n"); | |
e5fd39d9 | 3007 | return -ENOTSUPP; |
d5e2b67a MN |
3008 | } |
3009 | ||
3010 | ||
d23b0f08 | 3011 | /****************************** ADD FUNCTION ******************************/ |
d5e2b67a | 3012 | |
d23b0f08 MN |
3013 | static struct usb_gadget_strings *fsg_strings_array[] = { |
3014 | &fsg_stringtab, | |
3015 | NULL, | |
d5e2b67a MN |
3016 | }; |
3017 | ||
1dc90985 MN |
3018 | static int fsg_bind_config(struct usb_composite_dev *cdev, |
3019 | struct usb_configuration *c, | |
3020 | struct fsg_common *common) | |
d5e2b67a | 3021 | { |
d23b0f08 MN |
3022 | struct fsg_dev *fsg; |
3023 | int rc; | |
3024 | ||
3025 | fsg = kzalloc(sizeof *fsg, GFP_KERNEL); | |
3026 | if (unlikely(!fsg)) | |
3027 | return -ENOMEM; | |
d5e2b67a | 3028 | |
d23b0f08 MN |
3029 | fsg->function.name = FSG_DRIVER_DESC; |
3030 | fsg->function.strings = fsg_strings_array; | |
d23b0f08 MN |
3031 | fsg->function.bind = fsg_bind; |
3032 | fsg->function.unbind = fsg_unbind; | |
3033 | fsg->function.setup = fsg_setup; | |
3034 | fsg->function.set_alt = fsg_set_alt; | |
3035 | fsg->function.disable = fsg_disable; | |
3036 | ||
3037 | fsg->common = common; | |
b73af61e MN |
3038 | /* |
3039 | * Our caller holds a reference to common structure so we | |
d23b0f08 MN |
3040 | * don't have to be worry about it being freed until we return |
3041 | * from this function. So instead of incrementing counter now | |
3042 | * and decrement in error recovery we increment it only when | |
b73af61e MN |
3043 | * call to usb_add_function() was successful. |
3044 | */ | |
d23b0f08 MN |
3045 | |
3046 | rc = usb_add_function(c, &fsg->function); | |
0fb2c2a1 | 3047 | if (unlikely(rc)) |
e5fd39d9 MN |
3048 | kfree(fsg); |
3049 | else | |
3050 | fsg_common_get(fsg->common); | |
d23b0f08 | 3051 | return rc; |
d5e2b67a | 3052 | } |
481e4929 | 3053 | |
1dc90985 | 3054 | static inline int __deprecated __maybe_unused |
b73af61e | 3055 | fsg_add(struct usb_composite_dev *cdev, struct usb_configuration *c, |
1dc90985 MN |
3056 | struct fsg_common *common) |
3057 | { | |
3058 | return fsg_bind_config(cdev, c, common); | |
3059 | } | |
481e4929 MN |
3060 | |
3061 | ||
3062 | /************************* Module parameters *************************/ | |
3063 | ||
481e4929 MN |
3064 | struct fsg_module_parameters { |
3065 | char *file[FSG_MAX_LUNS]; | |
3066 | int ro[FSG_MAX_LUNS]; | |
3067 | int removable[FSG_MAX_LUNS]; | |
3068 | int cdrom[FSG_MAX_LUNS]; | |
9cfe745e | 3069 | int nofua[FSG_MAX_LUNS]; |
481e4929 MN |
3070 | |
3071 | unsigned int file_count, ro_count, removable_count, cdrom_count; | |
9cfe745e | 3072 | unsigned int nofua_count; |
481e4929 MN |
3073 | unsigned int luns; /* nluns */ |
3074 | int stall; /* can_stall */ | |
3075 | }; | |
3076 | ||
481e4929 MN |
3077 | #define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \ |
3078 | module_param_array_named(prefix ## name, params.name, type, \ | |
3079 | &prefix ## params.name ## _count, \ | |
3080 | S_IRUGO); \ | |
3081 | MODULE_PARM_DESC(prefix ## name, desc) | |
3082 | ||
3083 | #define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \ | |
3084 | module_param_named(prefix ## name, params.name, type, \ | |
3085 | S_IRUGO); \ | |
3086 | MODULE_PARM_DESC(prefix ## name, desc) | |
3087 | ||
3088 | #define FSG_MODULE_PARAMETERS(prefix, params) \ | |
3089 | _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \ | |
3090 | "names of backing files or devices"); \ | |
3091 | _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \ | |
3092 | "true to force read-only"); \ | |
3093 | _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \ | |
3094 | "true to simulate removable media"); \ | |
3095 | _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \ | |
3096 | "true to simulate CD-ROM instead of disk"); \ | |
9cfe745e MN |
3097 | _FSG_MODULE_PARAM_ARRAY(prefix, params, nofua, bool, \ |
3098 | "true to ignore SCSI WRITE(10,12) FUA bit"); \ | |
481e4929 MN |
3099 | _FSG_MODULE_PARAM(prefix, params, luns, uint, \ |
3100 | "number of LUNs"); \ | |
3101 | _FSG_MODULE_PARAM(prefix, params, stall, bool, \ | |
3102 | "false to prevent bulk stalls") | |
3103 | ||
481e4929 MN |
3104 | static void |
3105 | fsg_config_from_params(struct fsg_config *cfg, | |
3106 | const struct fsg_module_parameters *params) | |
3107 | { | |
3108 | struct fsg_lun_config *lun; | |
d26a6aa0 | 3109 | unsigned i; |
481e4929 MN |
3110 | |
3111 | /* Configure LUNs */ | |
d26a6aa0 MN |
3112 | cfg->nluns = |
3113 | min(params->luns ?: (params->file_count ?: 1u), | |
3114 | (unsigned)FSG_MAX_LUNS); | |
3115 | for (i = 0, lun = cfg->luns; i < cfg->nluns; ++i, ++lun) { | |
481e4929 MN |
3116 | lun->ro = !!params->ro[i]; |
3117 | lun->cdrom = !!params->cdrom[i]; | |
d26a6aa0 | 3118 | lun->removable = /* Removable by default */ |
481e4929 MN |
3119 | params->removable_count <= i || params->removable[i]; |
3120 | lun->filename = | |
3121 | params->file_count > i && params->file[i][0] | |
3122 | ? params->file[i] | |
3123 | : 0; | |
3124 | } | |
3125 | ||
d26a6aa0 | 3126 | /* Let MSF use defaults */ |
e8b6f8c5 MN |
3127 | cfg->lun_name_format = 0; |
3128 | cfg->thread_name = 0; | |
481e4929 MN |
3129 | cfg->vendor_name = 0; |
3130 | cfg->product_name = 0; | |
3131 | cfg->release = 0xffff; | |
3132 | ||
8876f5e7 MN |
3133 | cfg->ops = NULL; |
3134 | cfg->private_data = NULL; | |
c85efcb9 | 3135 | |
481e4929 MN |
3136 | /* Finalise */ |
3137 | cfg->can_stall = params->stall; | |
3138 | } | |
3139 | ||
3140 | static inline struct fsg_common * | |
3141 | fsg_common_from_params(struct fsg_common *common, | |
3142 | struct usb_composite_dev *cdev, | |
3143 | const struct fsg_module_parameters *params) | |
3144 | __attribute__((unused)); | |
3145 | static inline struct fsg_common * | |
3146 | fsg_common_from_params(struct fsg_common *common, | |
3147 | struct usb_composite_dev *cdev, | |
3148 | const struct fsg_module_parameters *params) | |
3149 | { | |
3150 | struct fsg_config cfg; | |
3151 | fsg_config_from_params(&cfg, params); | |
3152 | return fsg_common_init(common, cdev, &cfg); | |
3153 | } | |
3154 |