usb: gadget: add quirk_ep_out_aligned_size field to struct usb_gadget
[deliverable/linux.git] / drivers / usb / gadget / storage_common.c
CommitLineData
b6058d0f
MN
1/*
2 * storage_common.c -- Common definitions for mass storage functionality
3 *
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyeight (C) 2009 Samsung Electronics
54b8360f 6 * Author: Michal Nazarewicz (mina86@mina86.com)
d6181702
MN
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
d6181702
MN
12 */
13
d6181702
MN
14/*
15 * This file requires the following identifiers used in USB strings to
16 * be defined (each of type pointer to char):
d6181702 17 * - fsg_string_interface -- name of the interface
93bcf12e
MN
18 */
19
6532c7fd
PF
20/*
21 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
22 * sets the number of pipeline buffers (length of the fsg_buffhd array).
23 * The valid range of num_buffers is: num >= 2 && num <= 4.
24 */
25
6fdc5dd2
AP
26#include <linux/module.h>
27#include <linux/blkdev.h>
28#include <linux/file.h>
29#include <linux/fs.h>
30#include <linux/usb/composite.h>
b6058d0f 31
6fdc5dd2 32#include "storage_common.h"
b6058d0f 33
b6058d0f
MN
34/* There is only one interface. */
35
6fdc5dd2 36struct usb_interface_descriptor fsg_intf_desc = {
d6181702 37 .bLength = sizeof fsg_intf_desc,
b6058d0f
MN
38 .bDescriptorType = USB_DT_INTERFACE,
39
d26a6aa0 40 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
b6058d0f 41 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
d26a6aa0
MN
42 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
43 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
d6181702 44 .iInterface = FSG_STRING_INTERFACE,
b6058d0f 45};
6fdc5dd2 46EXPORT_SYMBOL(fsg_intf_desc);
b6058d0f 47
d0893264
MN
48/*
49 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
50 * interrupt-in.
51 */
b6058d0f 52
6fdc5dd2 53struct usb_endpoint_descriptor fsg_fs_bulk_in_desc = {
b6058d0f
MN
54 .bLength = USB_DT_ENDPOINT_SIZE,
55 .bDescriptorType = USB_DT_ENDPOINT,
56
57 .bEndpointAddress = USB_DIR_IN,
58 .bmAttributes = USB_ENDPOINT_XFER_BULK,
59 /* wMaxPacketSize set by autoconfiguration */
60};
6fdc5dd2 61EXPORT_SYMBOL(fsg_fs_bulk_in_desc);
b6058d0f 62
6fdc5dd2 63struct usb_endpoint_descriptor fsg_fs_bulk_out_desc = {
b6058d0f
MN
64 .bLength = USB_DT_ENDPOINT_SIZE,
65 .bDescriptorType = USB_DT_ENDPOINT,
66
67 .bEndpointAddress = USB_DIR_OUT,
68 .bmAttributes = USB_ENDPOINT_XFER_BULK,
69 /* wMaxPacketSize set by autoconfiguration */
70};
6fdc5dd2 71EXPORT_SYMBOL(fsg_fs_bulk_out_desc);
b6058d0f 72
6fdc5dd2 73struct usb_descriptor_header *fsg_fs_function[] = {
d6181702
MN
74 (struct usb_descriptor_header *) &fsg_intf_desc,
75 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
76 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
b6058d0f
MN
77 NULL,
78};
6fdc5dd2 79EXPORT_SYMBOL(fsg_fs_function);
b6058d0f
MN
80
81
82/*
83 * USB 2.0 devices need to expose both high speed and full speed
84 * descriptors, unless they only run at full speed.
85 *
86 * That means alternate endpoint descriptors (bigger packets)
87 * and a "device qualifier" ... plus more construction options
d0893264 88 * for the configuration descriptor.
b6058d0f 89 */
6fdc5dd2 90struct usb_endpoint_descriptor fsg_hs_bulk_in_desc = {
b6058d0f
MN
91 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
93
94 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
95 .bmAttributes = USB_ENDPOINT_XFER_BULK,
96 .wMaxPacketSize = cpu_to_le16(512),
97};
6fdc5dd2 98EXPORT_SYMBOL(fsg_hs_bulk_in_desc);
b6058d0f 99
6fdc5dd2 100struct usb_endpoint_descriptor fsg_hs_bulk_out_desc = {
b6058d0f
MN
101 .bLength = USB_DT_ENDPOINT_SIZE,
102 .bDescriptorType = USB_DT_ENDPOINT,
103
104 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
105 .bmAttributes = USB_ENDPOINT_XFER_BULK,
106 .wMaxPacketSize = cpu_to_le16(512),
d26a6aa0 107 .bInterval = 1, /* NAK every 1 uframe */
b6058d0f 108};
6fdc5dd2 109EXPORT_SYMBOL(fsg_hs_bulk_out_desc);
b6058d0f 110
93bcf12e 111
6fdc5dd2 112struct usb_descriptor_header *fsg_hs_function[] = {
d6181702
MN
113 (struct usb_descriptor_header *) &fsg_intf_desc,
114 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
115 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
b6058d0f
MN
116 NULL,
117};
6fdc5dd2 118EXPORT_SYMBOL(fsg_hs_function);
b6058d0f 119
6fdc5dd2 120struct usb_endpoint_descriptor fsg_ss_bulk_in_desc = {
4bb99b7c
FB
121 .bLength = USB_DT_ENDPOINT_SIZE,
122 .bDescriptorType = USB_DT_ENDPOINT,
123
124 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
125 .bmAttributes = USB_ENDPOINT_XFER_BULK,
126 .wMaxPacketSize = cpu_to_le16(1024),
127};
6fdc5dd2 128EXPORT_SYMBOL(fsg_ss_bulk_in_desc);
4bb99b7c 129
6fdc5dd2 130struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
4bb99b7c
FB
131 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
132 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
133
134 /*.bMaxBurst = DYNAMIC, */
135};
6fdc5dd2 136EXPORT_SYMBOL(fsg_ss_bulk_in_comp_desc);
4bb99b7c 137
6fdc5dd2 138struct usb_endpoint_descriptor fsg_ss_bulk_out_desc = {
4bb99b7c
FB
139 .bLength = USB_DT_ENDPOINT_SIZE,
140 .bDescriptorType = USB_DT_ENDPOINT,
141
142 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
143 .bmAttributes = USB_ENDPOINT_XFER_BULK,
144 .wMaxPacketSize = cpu_to_le16(1024),
145};
6fdc5dd2 146EXPORT_SYMBOL(fsg_ss_bulk_out_desc);
4bb99b7c 147
6fdc5dd2 148struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
4bb99b7c
FB
149 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
150 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
151
152 /*.bMaxBurst = DYNAMIC, */
153};
6fdc5dd2 154EXPORT_SYMBOL(fsg_ss_bulk_out_comp_desc);
4bb99b7c 155
6fdc5dd2 156struct usb_descriptor_header *fsg_ss_function[] = {
4bb99b7c
FB
157 (struct usb_descriptor_header *) &fsg_intf_desc,
158 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
159 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
160 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
161 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
4bb99b7c
FB
162 NULL,
163};
6fdc5dd2 164EXPORT_SYMBOL(fsg_ss_function);
b6058d0f
MN
165
166
167 /*-------------------------------------------------------------------------*/
168
d0893264
MN
169/*
170 * If the next two routines are called while the gadget is registered,
171 * the caller must own fsg->filesem for writing.
172 */
b6058d0f 173
6fdc5dd2 174void fsg_lun_close(struct fsg_lun *curlun)
d6e16a89
MN
175{
176 if (curlun->filp) {
177 LDBG(curlun, "close backing file\n");
178 fput(curlun->filp);
179 curlun->filp = NULL;
180 }
181}
6fdc5dd2 182EXPORT_SYMBOL(fsg_lun_close);
d6e16a89 183
6fdc5dd2 184int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
b6058d0f
MN
185{
186 int ro;
187 struct file *filp = NULL;
188 int rc = -EINVAL;
189 struct inode *inode = NULL;
190 loff_t size;
191 loff_t num_sectors;
192 loff_t min_sectors;
d6e16a89
MN
193 unsigned int blkbits;
194 unsigned int blksize;
b6058d0f
MN
195
196 /* R/W if we can, R/O if we must */
e909ef5d 197 ro = curlun->initially_ro;
b6058d0f
MN
198 if (!ro) {
199 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
75f1dc0d 200 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
b6058d0f
MN
201 ro = 1;
202 }
203 if (ro)
204 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
205 if (IS_ERR(filp)) {
206 LINFO(curlun, "unable to open backing file: %s\n", filename);
207 return PTR_ERR(filp);
208 }
209
210 if (!(filp->f_mode & FMODE_WRITE))
211 ro = 1;
212
496ad9aa 213 inode = file_inode(filp);
20818a0c 214 if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
b6058d0f
MN
215 LINFO(curlun, "invalid file type: %s\n", filename);
216 goto out;
217 }
218
d0893264
MN
219 /*
220 * If we can't read the file, it's no good.
221 * If we can't write the file, use it read-only.
222 */
20818a0c 223 if (!(filp->f_op->read || filp->f_op->aio_read)) {
b6058d0f
MN
224 LINFO(curlun, "file not readable: %s\n", filename);
225 goto out;
226 }
227 if (!(filp->f_op->write || filp->f_op->aio_write))
228 ro = 1;
229
230 size = i_size_read(inode->i_mapping->host);
231 if (size < 0) {
232 LINFO(curlun, "unable to find file size: %s\n", filename);
233 rc = (int) size;
234 goto out;
235 }
3f565a36
PL
236
237 if (curlun->cdrom) {
d6e16a89
MN
238 blksize = 2048;
239 blkbits = 11;
3f565a36 240 } else if (inode->i_bdev) {
d6e16a89
MN
241 blksize = bdev_logical_block_size(inode->i_bdev);
242 blkbits = blksize_bits(blksize);
3f565a36 243 } else {
d6e16a89
MN
244 blksize = 512;
245 blkbits = 9;
3f565a36
PL
246 }
247
d6e16a89 248 num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
b6058d0f 249 min_sectors = 1;
e909ef5d 250 if (curlun->cdrom) {
3f565a36
PL
251 min_sectors = 300; /* Smallest track is 300 frames */
252 if (num_sectors >= 256*60*75) {
253 num_sectors = 256*60*75 - 1;
b6058d0f
MN
254 LINFO(curlun, "file too big: %s\n", filename);
255 LINFO(curlun, "using only first %d blocks\n",
256 (int) num_sectors);
257 }
258 }
259 if (num_sectors < min_sectors) {
260 LINFO(curlun, "file too small: %s\n", filename);
261 rc = -ETOOSMALL;
262 goto out;
263 }
264
d6e16a89
MN
265 if (fsg_lun_is_open(curlun))
266 fsg_lun_close(curlun);
267
d6e16a89
MN
268 curlun->blksize = blksize;
269 curlun->blkbits = blkbits;
b6058d0f
MN
270 curlun->ro = ro;
271 curlun->filp = filp;
272 curlun->file_length = size;
273 curlun->num_sectors = num_sectors;
274 LDBG(curlun, "open backing file: %s\n", filename);
20818a0c 275 return 0;
b6058d0f
MN
276
277out:
20818a0c 278 fput(filp);
b6058d0f
MN
279 return rc;
280}
6fdc5dd2 281EXPORT_SYMBOL(fsg_lun_open);
b6058d0f
MN
282
283
b6058d0f
MN
284/*-------------------------------------------------------------------------*/
285
d0893264
MN
286/*
287 * Sync the file data, don't bother with the metadata.
288 * This code was copied from fs/buffer.c:sys_fdatasync().
289 */
6fdc5dd2 290int fsg_lun_fsync_sub(struct fsg_lun *curlun)
b6058d0f
MN
291{
292 struct file *filp = curlun->filp;
293
294 if (curlun->ro || !filp)
295 return 0;
8018ab05 296 return vfs_fsync(filp, 1);
b6058d0f 297}
6fdc5dd2 298EXPORT_SYMBOL(fsg_lun_fsync_sub);
b6058d0f 299
6fdc5dd2 300void store_cdrom_address(u8 *dest, int msf, u32 addr)
b6058d0f
MN
301{
302 if (msf) {
303 /* Convert to Minutes-Seconds-Frames */
304 addr >>= 2; /* Convert to 2048-byte frames */
305 addr += 2*75; /* Lead-in occupies 2 seconds */
306 dest[3] = addr % 75; /* Frames */
307 addr /= 75;
308 dest[2] = addr % 60; /* Seconds */
309 addr /= 60;
310 dest[1] = addr; /* Minutes */
311 dest[0] = 0; /* Reserved */
312 } else {
313 /* Absolute sector */
314 put_unaligned_be32(addr, dest);
315 }
316}
6fdc5dd2 317EXPORT_SYMBOL(store_cdrom_address);
93f93740
MN
318
319/*-------------------------------------------------------------------------*/
320
321
77850ae2 322ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf)
93f93740 323{
93f93740
MN
324 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
325 ? curlun->ro
326 : curlun->initially_ro);
327}
6fdc5dd2 328EXPORT_SYMBOL(fsg_show_ro);
93f93740 329
77850ae2 330ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf)
a93917d3 331{
a93917d3
AS
332 return sprintf(buf, "%u\n", curlun->nofua);
333}
6fdc5dd2 334EXPORT_SYMBOL(fsg_show_nofua);
a93917d3 335
77850ae2 336ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 337 char *buf)
93f93740 338{
93f93740
MN
339 char *p;
340 ssize_t rc;
341
342 down_read(filesem);
d26a6aa0 343 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
93f93740
MN
344 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
345 if (IS_ERR(p))
346 rc = PTR_ERR(p);
347 else {
348 rc = strlen(p);
349 memmove(buf, p, rc);
d26a6aa0 350 buf[rc] = '\n'; /* Add a newline */
93f93740
MN
351 buf[++rc] = 0;
352 }
d26a6aa0 353 } else { /* No file, return 0 bytes */
93f93740
MN
354 *buf = 0;
355 rc = 0;
356 }
357 up_read(filesem);
358 return rc;
359}
6fdc5dd2 360EXPORT_SYMBOL(fsg_show_file);
93f93740 361
864328ef
AP
362ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf)
363{
364 return sprintf(buf, "%u\n", curlun->cdrom);
365}
366EXPORT_SYMBOL(fsg_show_cdrom);
367
368ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf)
369{
370 return sprintf(buf, "%u\n", curlun->removable);
371}
372EXPORT_SYMBOL(fsg_show_removable);
93f93740 373
b8798636
AP
374/*
375 * The caller must hold fsg->filesem for reading when calling this function.
376 */
377static ssize_t _fsg_store_ro(struct fsg_lun *curlun, bool ro)
378{
379 if (fsg_lun_is_open(curlun)) {
380 LDBG(curlun, "read-only status change prevented\n");
381 return -EBUSY;
382 }
383
384 curlun->ro = ro;
385 curlun->initially_ro = ro;
386 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
387
388 return 0;
389}
390
77850ae2 391ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 392 const char *buf, size_t count)
93f93740 393{
fd4477b0 394 ssize_t rc;
81a1d5ea 395 bool ro;
93f93740 396
81a1d5ea 397 rc = strtobool(buf, &ro);
db8fa285
MN
398 if (rc)
399 return rc;
93f93740 400
d0893264
MN
401 /*
402 * Allow the write-enable status to change only while the
403 * backing file is closed.
404 */
93f93740 405 down_read(filesem);
b8798636
AP
406 rc = _fsg_store_ro(curlun, ro);
407 if (!rc)
fd4477b0 408 rc = count;
93f93740 409 up_read(filesem);
b8798636 410
93f93740
MN
411 return rc;
412}
6fdc5dd2 413EXPORT_SYMBOL(fsg_store_ro);
93f93740 414
77850ae2 415ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count)
a93917d3 416{
81a1d5ea 417 bool nofua;
db8fa285 418 int ret;
a93917d3 419
81a1d5ea 420 ret = strtobool(buf, &nofua);
db8fa285
MN
421 if (ret)
422 return ret;
a93917d3
AS
423
424 /* Sync data when switching from async mode to sync */
425 if (!nofua && curlun->nofua)
426 fsg_lun_fsync_sub(curlun);
427
428 curlun->nofua = nofua;
429
430 return count;
431}
6fdc5dd2 432EXPORT_SYMBOL(fsg_store_nofua);
a93917d3 433
77850ae2 434ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 435 const char *buf, size_t count)
93f93740 436{
93f93740
MN
437 int rc = 0;
438
439 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
440 LDBG(curlun, "eject attempt prevented\n");
d26a6aa0 441 return -EBUSY; /* "Door is locked" */
93f93740
MN
442 }
443
444 /* Remove a trailing newline */
445 if (count > 0 && buf[count-1] == '\n')
d26a6aa0 446 ((char *) buf)[count-1] = 0; /* Ugh! */
93f93740 447
93f93740 448 /* Load new medium */
d6e16a89 449 down_write(filesem);
93f93740 450 if (count > 0 && buf[0]) {
d6e16a89 451 /* fsg_lun_open() will close existing file if any. */
93f93740
MN
452 rc = fsg_lun_open(curlun, buf);
453 if (rc == 0)
454 curlun->unit_attention_data =
455 SS_NOT_READY_TO_READY_TRANSITION;
d6e16a89
MN
456 } else if (fsg_lun_is_open(curlun)) {
457 fsg_lun_close(curlun);
458 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
93f93740
MN
459 }
460 up_write(filesem);
461 return (rc < 0 ? rc : count);
462}
6fdc5dd2
AP
463EXPORT_SYMBOL(fsg_store_file);
464
b8798636
AP
465ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
466 const char *buf, size_t count)
864328ef 467{
81a1d5ea 468 bool cdrom;
864328ef
AP
469 int ret;
470
81a1d5ea 471 ret = strtobool(buf, &cdrom);
864328ef
AP
472 if (ret)
473 return ret;
474
b8798636
AP
475 down_read(filesem);
476 ret = cdrom ? _fsg_store_ro(curlun, true) : 0;
864328ef 477
b8798636
AP
478 if (!ret) {
479 curlun->cdrom = cdrom;
480 ret = count;
481 }
482 up_read(filesem);
483
484 return ret;
864328ef
AP
485}
486EXPORT_SYMBOL(fsg_store_cdrom);
487
488ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
489 size_t count)
490{
81a1d5ea 491 bool removable;
864328ef
AP
492 int ret;
493
81a1d5ea 494 ret = strtobool(buf, &removable);
864328ef
AP
495 if (ret)
496 return ret;
497
498 curlun->removable = removable;
499
500 return count;
501}
502EXPORT_SYMBOL(fsg_store_removable);
503
6fdc5dd2 504MODULE_LICENSE("GPL");
This page took 0.311122 seconds and 5 git commands to generate.