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