Merge remote-tracking branch 'usb-gadget/next'
[deliverable/linux.git] / drivers / usb / gadget / function / 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};
0700faaf 46EXPORT_SYMBOL_GPL(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};
0700faaf 61EXPORT_SYMBOL_GPL(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};
0700faaf 71EXPORT_SYMBOL_GPL(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};
0700faaf 79EXPORT_SYMBOL_GPL(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 *
cc50dc28 86 * That means alternate endpoint descriptors (bigger packets).
b6058d0f 87 */
6fdc5dd2 88struct usb_endpoint_descriptor fsg_hs_bulk_in_desc = {
b6058d0f
MN
89 .bLength = USB_DT_ENDPOINT_SIZE,
90 .bDescriptorType = USB_DT_ENDPOINT,
91
92 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
93 .bmAttributes = USB_ENDPOINT_XFER_BULK,
94 .wMaxPacketSize = cpu_to_le16(512),
95};
0700faaf 96EXPORT_SYMBOL_GPL(fsg_hs_bulk_in_desc);
b6058d0f 97
6fdc5dd2 98struct usb_endpoint_descriptor fsg_hs_bulk_out_desc = {
b6058d0f
MN
99 .bLength = USB_DT_ENDPOINT_SIZE,
100 .bDescriptorType = USB_DT_ENDPOINT,
101
102 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
103 .bmAttributes = USB_ENDPOINT_XFER_BULK,
104 .wMaxPacketSize = cpu_to_le16(512),
d26a6aa0 105 .bInterval = 1, /* NAK every 1 uframe */
b6058d0f 106};
0700faaf 107EXPORT_SYMBOL_GPL(fsg_hs_bulk_out_desc);
b6058d0f 108
93bcf12e 109
6fdc5dd2 110struct usb_descriptor_header *fsg_hs_function[] = {
d6181702
MN
111 (struct usb_descriptor_header *) &fsg_intf_desc,
112 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
113 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
b6058d0f
MN
114 NULL,
115};
0700faaf 116EXPORT_SYMBOL_GPL(fsg_hs_function);
b6058d0f 117
6fdc5dd2 118struct usb_endpoint_descriptor fsg_ss_bulk_in_desc = {
4bb99b7c
FB
119 .bLength = USB_DT_ENDPOINT_SIZE,
120 .bDescriptorType = USB_DT_ENDPOINT,
121
122 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
123 .bmAttributes = USB_ENDPOINT_XFER_BULK,
124 .wMaxPacketSize = cpu_to_le16(1024),
125};
0700faaf 126EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_desc);
4bb99b7c 127
6fdc5dd2 128struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
4bb99b7c
FB
129 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
130 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
131
132 /*.bMaxBurst = DYNAMIC, */
133};
0700faaf 134EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_comp_desc);
4bb99b7c 135
6fdc5dd2 136struct usb_endpoint_descriptor fsg_ss_bulk_out_desc = {
4bb99b7c
FB
137 .bLength = USB_DT_ENDPOINT_SIZE,
138 .bDescriptorType = USB_DT_ENDPOINT,
139
140 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
141 .bmAttributes = USB_ENDPOINT_XFER_BULK,
142 .wMaxPacketSize = cpu_to_le16(1024),
143};
0700faaf 144EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_desc);
4bb99b7c 145
6fdc5dd2 146struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
4bb99b7c
FB
147 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
148 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
149
150 /*.bMaxBurst = DYNAMIC, */
151};
0700faaf 152EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_comp_desc);
4bb99b7c 153
6fdc5dd2 154struct usb_descriptor_header *fsg_ss_function[] = {
4bb99b7c
FB
155 (struct usb_descriptor_header *) &fsg_intf_desc,
156 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
157 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
158 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
159 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
4bb99b7c
FB
160 NULL,
161};
0700faaf 162EXPORT_SYMBOL_GPL(fsg_ss_function);
b6058d0f
MN
163
164
165 /*-------------------------------------------------------------------------*/
166
d0893264
MN
167/*
168 * If the next two routines are called while the gadget is registered,
169 * the caller must own fsg->filesem for writing.
170 */
b6058d0f 171
6fdc5dd2 172void fsg_lun_close(struct fsg_lun *curlun)
d6e16a89
MN
173{
174 if (curlun->filp) {
175 LDBG(curlun, "close backing file\n");
176 fput(curlun->filp);
177 curlun->filp = NULL;
178 }
179}
0700faaf 180EXPORT_SYMBOL_GPL(fsg_lun_close);
d6e16a89 181
6fdc5dd2 182int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
b6058d0f
MN
183{
184 int ro;
185 struct file *filp = NULL;
186 int rc = -EINVAL;
187 struct inode *inode = NULL;
188 loff_t size;
189 loff_t num_sectors;
190 loff_t min_sectors;
d6e16a89
MN
191 unsigned int blkbits;
192 unsigned int blksize;
b6058d0f
MN
193
194 /* R/W if we can, R/O if we must */
e909ef5d 195 ro = curlun->initially_ro;
b6058d0f
MN
196 if (!ro) {
197 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
75f1dc0d 198 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
b6058d0f
MN
199 ro = 1;
200 }
201 if (ro)
202 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
203 if (IS_ERR(filp)) {
204 LINFO(curlun, "unable to open backing file: %s\n", filename);
205 return PTR_ERR(filp);
206 }
207
208 if (!(filp->f_mode & FMODE_WRITE))
209 ro = 1;
210
496ad9aa 211 inode = file_inode(filp);
20818a0c 212 if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
b6058d0f
MN
213 LINFO(curlun, "invalid file type: %s\n", filename);
214 goto out;
215 }
216
d0893264
MN
217 /*
218 * If we can't read the file, it's no good.
219 * If we can't write the file, use it read-only.
220 */
7f7f25e8 221 if (!(filp->f_mode & FMODE_CAN_READ)) {
b6058d0f
MN
222 LINFO(curlun, "file not readable: %s\n", filename);
223 goto out;
224 }
7f7f25e8 225 if (!(filp->f_mode & FMODE_CAN_WRITE))
b6058d0f
MN
226 ro = 1;
227
228 size = i_size_read(inode->i_mapping->host);
229 if (size < 0) {
230 LINFO(curlun, "unable to find file size: %s\n", filename);
231 rc = (int) size;
232 goto out;
233 }
3f565a36
PL
234
235 if (curlun->cdrom) {
d6e16a89
MN
236 blksize = 2048;
237 blkbits = 11;
3f565a36 238 } else if (inode->i_bdev) {
d6e16a89
MN
239 blksize = bdev_logical_block_size(inode->i_bdev);
240 blkbits = blksize_bits(blksize);
3f565a36 241 } else {
d6e16a89
MN
242 blksize = 512;
243 blkbits = 9;
3f565a36
PL
244 }
245
d6e16a89 246 num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
b6058d0f 247 min_sectors = 1;
e909ef5d 248 if (curlun->cdrom) {
3f565a36
PL
249 min_sectors = 300; /* Smallest track is 300 frames */
250 if (num_sectors >= 256*60*75) {
251 num_sectors = 256*60*75 - 1;
b6058d0f
MN
252 LINFO(curlun, "file too big: %s\n", filename);
253 LINFO(curlun, "using only first %d blocks\n",
254 (int) num_sectors);
255 }
256 }
257 if (num_sectors < min_sectors) {
258 LINFO(curlun, "file too small: %s\n", filename);
259 rc = -ETOOSMALL;
260 goto out;
261 }
262
d6e16a89
MN
263 if (fsg_lun_is_open(curlun))
264 fsg_lun_close(curlun);
265
d6e16a89
MN
266 curlun->blksize = blksize;
267 curlun->blkbits = blkbits;
b6058d0f
MN
268 curlun->ro = ro;
269 curlun->filp = filp;
270 curlun->file_length = size;
271 curlun->num_sectors = num_sectors;
272 LDBG(curlun, "open backing file: %s\n", filename);
20818a0c 273 return 0;
b6058d0f
MN
274
275out:
20818a0c 276 fput(filp);
b6058d0f
MN
277 return rc;
278}
0700faaf 279EXPORT_SYMBOL_GPL(fsg_lun_open);
b6058d0f
MN
280
281
b6058d0f
MN
282/*-------------------------------------------------------------------------*/
283
d0893264
MN
284/*
285 * Sync the file data, don't bother with the metadata.
286 * This code was copied from fs/buffer.c:sys_fdatasync().
287 */
6fdc5dd2 288int fsg_lun_fsync_sub(struct fsg_lun *curlun)
b6058d0f
MN
289{
290 struct file *filp = curlun->filp;
291
292 if (curlun->ro || !filp)
293 return 0;
8018ab05 294 return vfs_fsync(filp, 1);
b6058d0f 295}
0700faaf 296EXPORT_SYMBOL_GPL(fsg_lun_fsync_sub);
b6058d0f 297
6fdc5dd2 298void store_cdrom_address(u8 *dest, int msf, u32 addr)
b6058d0f
MN
299{
300 if (msf) {
301 /* Convert to Minutes-Seconds-Frames */
302 addr >>= 2; /* Convert to 2048-byte frames */
303 addr += 2*75; /* Lead-in occupies 2 seconds */
304 dest[3] = addr % 75; /* Frames */
305 addr /= 75;
306 dest[2] = addr % 60; /* Seconds */
307 addr /= 60;
308 dest[1] = addr; /* Minutes */
309 dest[0] = 0; /* Reserved */
310 } else {
311 /* Absolute sector */
312 put_unaligned_be32(addr, dest);
313 }
314}
0700faaf 315EXPORT_SYMBOL_GPL(store_cdrom_address);
93f93740
MN
316
317/*-------------------------------------------------------------------------*/
318
319
77850ae2 320ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf)
93f93740 321{
93f93740
MN
322 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
323 ? curlun->ro
324 : curlun->initially_ro);
325}
0700faaf 326EXPORT_SYMBOL_GPL(fsg_show_ro);
93f93740 327
77850ae2 328ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf)
a93917d3 329{
a93917d3
AS
330 return sprintf(buf, "%u\n", curlun->nofua);
331}
0700faaf 332EXPORT_SYMBOL_GPL(fsg_show_nofua);
a93917d3 333
77850ae2 334ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 335 char *buf)
93f93740 336{
93f93740
MN
337 char *p;
338 ssize_t rc;
339
340 down_read(filesem);
d26a6aa0 341 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
9bf39ab2 342 p = file_path(curlun->filp, buf, PAGE_SIZE - 1);
93f93740
MN
343 if (IS_ERR(p))
344 rc = PTR_ERR(p);
345 else {
346 rc = strlen(p);
347 memmove(buf, p, rc);
d26a6aa0 348 buf[rc] = '\n'; /* Add a newline */
93f93740
MN
349 buf[++rc] = 0;
350 }
d26a6aa0 351 } else { /* No file, return 0 bytes */
93f93740
MN
352 *buf = 0;
353 rc = 0;
354 }
355 up_read(filesem);
356 return rc;
357}
0700faaf 358EXPORT_SYMBOL_GPL(fsg_show_file);
93f93740 359
864328ef
AP
360ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf)
361{
362 return sprintf(buf, "%u\n", curlun->cdrom);
363}
0700faaf 364EXPORT_SYMBOL_GPL(fsg_show_cdrom);
864328ef
AP
365
366ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf)
367{
368 return sprintf(buf, "%u\n", curlun->removable);
369}
0700faaf 370EXPORT_SYMBOL_GPL(fsg_show_removable);
93f93740 371
6ac47090
PG
372ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf)
373{
374 return sprintf(buf, "%s\n", curlun->inquiry_string);
375}
376EXPORT_SYMBOL_GPL(fsg_show_inquiry_string);
377
b8798636
AP
378/*
379 * The caller must hold fsg->filesem for reading when calling this function.
380 */
381static ssize_t _fsg_store_ro(struct fsg_lun *curlun, bool ro)
382{
383 if (fsg_lun_is_open(curlun)) {
384 LDBG(curlun, "read-only status change prevented\n");
385 return -EBUSY;
386 }
387
388 curlun->ro = ro;
389 curlun->initially_ro = ro;
390 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
391
392 return 0;
393}
394
77850ae2 395ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 396 const char *buf, size_t count)
93f93740 397{
fd4477b0 398 ssize_t rc;
81a1d5ea 399 bool ro;
93f93740 400
81a1d5ea 401 rc = strtobool(buf, &ro);
db8fa285
MN
402 if (rc)
403 return rc;
93f93740 404
d0893264
MN
405 /*
406 * Allow the write-enable status to change only while the
407 * backing file is closed.
408 */
93f93740 409 down_read(filesem);
b8798636
AP
410 rc = _fsg_store_ro(curlun, ro);
411 if (!rc)
fd4477b0 412 rc = count;
93f93740 413 up_read(filesem);
b8798636 414
93f93740
MN
415 return rc;
416}
0700faaf 417EXPORT_SYMBOL_GPL(fsg_store_ro);
93f93740 418
77850ae2 419ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count)
a93917d3 420{
81a1d5ea 421 bool nofua;
db8fa285 422 int ret;
a93917d3 423
81a1d5ea 424 ret = strtobool(buf, &nofua);
db8fa285
MN
425 if (ret)
426 return ret;
a93917d3
AS
427
428 /* Sync data when switching from async mode to sync */
429 if (!nofua && curlun->nofua)
430 fsg_lun_fsync_sub(curlun);
431
432 curlun->nofua = nofua;
433
434 return count;
435}
0700faaf 436EXPORT_SYMBOL_GPL(fsg_store_nofua);
a93917d3 437
77850ae2 438ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
6fdc5dd2 439 const char *buf, size_t count)
93f93740 440{
93f93740
MN
441 int rc = 0;
442
443 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
444 LDBG(curlun, "eject attempt prevented\n");
d26a6aa0 445 return -EBUSY; /* "Door is locked" */
93f93740
MN
446 }
447
448 /* Remove a trailing newline */
449 if (count > 0 && buf[count-1] == '\n')
d26a6aa0 450 ((char *) buf)[count-1] = 0; /* Ugh! */
93f93740 451
93f93740 452 /* Load new medium */
d6e16a89 453 down_write(filesem);
93f93740 454 if (count > 0 && buf[0]) {
d6e16a89 455 /* fsg_lun_open() will close existing file if any. */
93f93740
MN
456 rc = fsg_lun_open(curlun, buf);
457 if (rc == 0)
458 curlun->unit_attention_data =
459 SS_NOT_READY_TO_READY_TRANSITION;
d6e16a89
MN
460 } else if (fsg_lun_is_open(curlun)) {
461 fsg_lun_close(curlun);
462 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
93f93740
MN
463 }
464 up_write(filesem);
465 return (rc < 0 ? rc : count);
466}
0700faaf 467EXPORT_SYMBOL_GPL(fsg_store_file);
6fdc5dd2 468
b8798636
AP
469ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
470 const char *buf, size_t count)
864328ef 471{
81a1d5ea 472 bool cdrom;
864328ef
AP
473 int ret;
474
81a1d5ea 475 ret = strtobool(buf, &cdrom);
864328ef
AP
476 if (ret)
477 return ret;
478
b8798636
AP
479 down_read(filesem);
480 ret = cdrom ? _fsg_store_ro(curlun, true) : 0;
864328ef 481
b8798636
AP
482 if (!ret) {
483 curlun->cdrom = cdrom;
484 ret = count;
485 }
486 up_read(filesem);
487
488 return ret;
864328ef 489}
0700faaf 490EXPORT_SYMBOL_GPL(fsg_store_cdrom);
864328ef
AP
491
492ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
493 size_t count)
494{
81a1d5ea 495 bool removable;
864328ef
AP
496 int ret;
497
81a1d5ea 498 ret = strtobool(buf, &removable);
864328ef
AP
499 if (ret)
500 return ret;
501
502 curlun->removable = removable;
503
504 return count;
505}
0700faaf 506EXPORT_SYMBOL_GPL(fsg_store_removable);
864328ef 507
6ac47090
PG
508ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
509 size_t count)
510{
511 const size_t len = min(count, sizeof(curlun->inquiry_string));
512
513 if (len == 0 || buf[0] == '\n') {
514 curlun->inquiry_string[0] = 0;
515 } else {
516 snprintf(curlun->inquiry_string,
517 sizeof(curlun->inquiry_string), "%-28s", buf);
518 if (curlun->inquiry_string[len-1] == '\n')
519 curlun->inquiry_string[len-1] = ' ';
520 }
521
522 return count;
523}
524EXPORT_SYMBOL_GPL(fsg_store_inquiry_string);
525
6fdc5dd2 526MODULE_LICENSE("GPL");
This page took 0.469265 seconds and 5 git commands to generate.