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