2 * storage_common.c -- Common definitions for mass storage functionality
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyeight (C) 2009 Samsung Electronics
6 * Author: Michal Nazarewicz (mina86@mina86.com)
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.
15 * This file requires the following identifiers used in USB strings to
16 * be defined (each of type pointer to char):
17 * - fsg_string_interface -- name of the interface
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.
26 #include <linux/module.h>
27 #include <linux/blkdev.h>
28 #include <linux/file.h>
30 #include <linux/usb/composite.h>
32 #include "storage_common.h"
34 /* There is only one interface. */
36 struct usb_interface_descriptor fsg_intf_desc
= {
37 .bLength
= sizeof fsg_intf_desc
,
38 .bDescriptorType
= USB_DT_INTERFACE
,
40 .bNumEndpoints
= 2, /* Adjusted during fsg_bind() */
41 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
42 .bInterfaceSubClass
= USB_SC_SCSI
, /* Adjusted during fsg_bind() */
43 .bInterfaceProtocol
= USB_PR_BULK
, /* Adjusted during fsg_bind() */
44 .iInterface
= FSG_STRING_INTERFACE
,
46 EXPORT_SYMBOL_GPL(fsg_intf_desc
);
49 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
53 struct usb_endpoint_descriptor fsg_fs_bulk_in_desc
= {
54 .bLength
= USB_DT_ENDPOINT_SIZE
,
55 .bDescriptorType
= USB_DT_ENDPOINT
,
57 .bEndpointAddress
= USB_DIR_IN
,
58 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
59 /* wMaxPacketSize set by autoconfiguration */
61 EXPORT_SYMBOL_GPL(fsg_fs_bulk_in_desc
);
63 struct usb_endpoint_descriptor fsg_fs_bulk_out_desc
= {
64 .bLength
= USB_DT_ENDPOINT_SIZE
,
65 .bDescriptorType
= USB_DT_ENDPOINT
,
67 .bEndpointAddress
= USB_DIR_OUT
,
68 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
69 /* wMaxPacketSize set by autoconfiguration */
71 EXPORT_SYMBOL_GPL(fsg_fs_bulk_out_desc
);
73 struct usb_descriptor_header
*fsg_fs_function
[] = {
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
,
79 EXPORT_SYMBOL_GPL(fsg_fs_function
);
83 * USB 2.0 devices need to expose both high speed and full speed
84 * descriptors, unless they only run at full speed.
86 * That means alternate endpoint descriptors (bigger packets)
87 * and a "device qualifier" ... plus more construction options
88 * for the configuration descriptor.
90 struct usb_endpoint_descriptor fsg_hs_bulk_in_desc
= {
91 .bLength
= USB_DT_ENDPOINT_SIZE
,
92 .bDescriptorType
= USB_DT_ENDPOINT
,
94 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
95 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
96 .wMaxPacketSize
= cpu_to_le16(512),
98 EXPORT_SYMBOL_GPL(fsg_hs_bulk_in_desc
);
100 struct usb_endpoint_descriptor fsg_hs_bulk_out_desc
= {
101 .bLength
= USB_DT_ENDPOINT_SIZE
,
102 .bDescriptorType
= USB_DT_ENDPOINT
,
104 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
105 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
106 .wMaxPacketSize
= cpu_to_le16(512),
107 .bInterval
= 1, /* NAK every 1 uframe */
109 EXPORT_SYMBOL_GPL(fsg_hs_bulk_out_desc
);
112 struct usb_descriptor_header
*fsg_hs_function
[] = {
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
,
118 EXPORT_SYMBOL_GPL(fsg_hs_function
);
120 struct usb_endpoint_descriptor fsg_ss_bulk_in_desc
= {
121 .bLength
= USB_DT_ENDPOINT_SIZE
,
122 .bDescriptorType
= USB_DT_ENDPOINT
,
124 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
125 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
126 .wMaxPacketSize
= cpu_to_le16(1024),
128 EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_desc
);
130 struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc
= {
131 .bLength
= sizeof(fsg_ss_bulk_in_comp_desc
),
132 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
134 /*.bMaxBurst = DYNAMIC, */
136 EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_comp_desc
);
138 struct usb_endpoint_descriptor fsg_ss_bulk_out_desc
= {
139 .bLength
= USB_DT_ENDPOINT_SIZE
,
140 .bDescriptorType
= USB_DT_ENDPOINT
,
142 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
143 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
144 .wMaxPacketSize
= cpu_to_le16(1024),
146 EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_desc
);
148 struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc
= {
149 .bLength
= sizeof(fsg_ss_bulk_in_comp_desc
),
150 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
152 /*.bMaxBurst = DYNAMIC, */
154 EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_comp_desc
);
156 struct usb_descriptor_header
*fsg_ss_function
[] = {
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
,
164 EXPORT_SYMBOL_GPL(fsg_ss_function
);
167 /*-------------------------------------------------------------------------*/
170 * If the next two routines are called while the gadget is registered,
171 * the caller must own fsg->filesem for writing.
174 void fsg_lun_close(struct fsg_lun
*curlun
)
177 LDBG(curlun
, "close backing file\n");
182 EXPORT_SYMBOL_GPL(fsg_lun_close
);
184 int fsg_lun_open(struct fsg_lun
*curlun
, const char *filename
)
187 struct file
*filp
= NULL
;
189 struct inode
*inode
= NULL
;
193 unsigned int blkbits
;
194 unsigned int blksize
;
196 /* R/W if we can, R/O if we must */
197 ro
= curlun
->initially_ro
;
199 filp
= filp_open(filename
, O_RDWR
| O_LARGEFILE
, 0);
200 if (PTR_ERR(filp
) == -EROFS
|| PTR_ERR(filp
) == -EACCES
)
204 filp
= filp_open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
206 LINFO(curlun
, "unable to open backing file: %s\n", filename
);
207 return PTR_ERR(filp
);
210 if (!(filp
->f_mode
& FMODE_WRITE
))
213 inode
= file_inode(filp
);
214 if ((!S_ISREG(inode
->i_mode
) && !S_ISBLK(inode
->i_mode
))) {
215 LINFO(curlun
, "invalid file type: %s\n", filename
);
220 * If we can't read the file, it's no good.
221 * If we can't write the file, use it read-only.
223 if (!(filp
->f_mode
& FMODE_CAN_READ
)) {
224 LINFO(curlun
, "file not readable: %s\n", filename
);
227 if (!(filp
->f_mode
& FMODE_CAN_WRITE
))
230 size
= i_size_read(inode
->i_mapping
->host
);
232 LINFO(curlun
, "unable to find file size: %s\n", filename
);
240 } else if (inode
->i_bdev
) {
241 blksize
= bdev_logical_block_size(inode
->i_bdev
);
242 blkbits
= blksize_bits(blksize
);
248 num_sectors
= size
>> blkbits
; /* File size in logic-block-size blocks */
251 min_sectors
= 300; /* Smallest track is 300 frames */
252 if (num_sectors
>= 256*60*75) {
253 num_sectors
= 256*60*75 - 1;
254 LINFO(curlun
, "file too big: %s\n", filename
);
255 LINFO(curlun
, "using only first %d blocks\n",
259 if (num_sectors
< min_sectors
) {
260 LINFO(curlun
, "file too small: %s\n", filename
);
265 if (fsg_lun_is_open(curlun
))
266 fsg_lun_close(curlun
);
268 curlun
->blksize
= blksize
;
269 curlun
->blkbits
= blkbits
;
272 curlun
->file_length
= size
;
273 curlun
->num_sectors
= num_sectors
;
274 LDBG(curlun
, "open backing file: %s\n", filename
);
281 EXPORT_SYMBOL_GPL(fsg_lun_open
);
284 /*-------------------------------------------------------------------------*/
287 * Sync the file data, don't bother with the metadata.
288 * This code was copied from fs/buffer.c:sys_fdatasync().
290 int fsg_lun_fsync_sub(struct fsg_lun
*curlun
)
292 struct file
*filp
= curlun
->filp
;
294 if (curlun
->ro
|| !filp
)
296 return vfs_fsync(filp
, 1);
298 EXPORT_SYMBOL_GPL(fsg_lun_fsync_sub
);
300 void store_cdrom_address(u8
*dest
, int msf
, u32 addr
)
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 */
308 dest
[2] = addr
% 60; /* Seconds */
310 dest
[1] = addr
; /* Minutes */
311 dest
[0] = 0; /* Reserved */
313 /* Absolute sector */
314 put_unaligned_be32(addr
, dest
);
317 EXPORT_SYMBOL_GPL(store_cdrom_address
);
319 /*-------------------------------------------------------------------------*/
322 ssize_t
fsg_show_ro(struct fsg_lun
*curlun
, char *buf
)
324 return sprintf(buf
, "%d\n", fsg_lun_is_open(curlun
)
326 : curlun
->initially_ro
);
328 EXPORT_SYMBOL_GPL(fsg_show_ro
);
330 ssize_t
fsg_show_nofua(struct fsg_lun
*curlun
, char *buf
)
332 return sprintf(buf
, "%u\n", curlun
->nofua
);
334 EXPORT_SYMBOL_GPL(fsg_show_nofua
);
336 ssize_t
fsg_show_file(struct fsg_lun
*curlun
, struct rw_semaphore
*filesem
,
343 if (fsg_lun_is_open(curlun
)) { /* Get the complete pathname */
344 p
= file_path(curlun
->filp
, buf
, PAGE_SIZE
- 1);
350 buf
[rc
] = '\n'; /* Add a newline */
353 } else { /* No file, return 0 bytes */
360 EXPORT_SYMBOL_GPL(fsg_show_file
);
362 ssize_t
fsg_show_cdrom(struct fsg_lun
*curlun
, char *buf
)
364 return sprintf(buf
, "%u\n", curlun
->cdrom
);
366 EXPORT_SYMBOL_GPL(fsg_show_cdrom
);
368 ssize_t
fsg_show_removable(struct fsg_lun
*curlun
, char *buf
)
370 return sprintf(buf
, "%u\n", curlun
->removable
);
372 EXPORT_SYMBOL_GPL(fsg_show_removable
);
375 * The caller must hold fsg->filesem for reading when calling this function.
377 static ssize_t
_fsg_store_ro(struct fsg_lun
*curlun
, bool ro
)
379 if (fsg_lun_is_open(curlun
)) {
380 LDBG(curlun
, "read-only status change prevented\n");
385 curlun
->initially_ro
= ro
;
386 LDBG(curlun
, "read-only status set to %d\n", curlun
->ro
);
391 ssize_t
fsg_store_ro(struct fsg_lun
*curlun
, struct rw_semaphore
*filesem
,
392 const char *buf
, size_t count
)
397 rc
= strtobool(buf
, &ro
);
402 * Allow the write-enable status to change only while the
403 * backing file is closed.
406 rc
= _fsg_store_ro(curlun
, ro
);
413 EXPORT_SYMBOL_GPL(fsg_store_ro
);
415 ssize_t
fsg_store_nofua(struct fsg_lun
*curlun
, const char *buf
, size_t count
)
420 ret
= strtobool(buf
, &nofua
);
424 /* Sync data when switching from async mode to sync */
425 if (!nofua
&& curlun
->nofua
)
426 fsg_lun_fsync_sub(curlun
);
428 curlun
->nofua
= nofua
;
432 EXPORT_SYMBOL_GPL(fsg_store_nofua
);
434 ssize_t
fsg_store_file(struct fsg_lun
*curlun
, struct rw_semaphore
*filesem
,
435 const char *buf
, size_t count
)
439 if (curlun
->prevent_medium_removal
&& fsg_lun_is_open(curlun
)) {
440 LDBG(curlun
, "eject attempt prevented\n");
441 return -EBUSY
; /* "Door is locked" */
444 /* Remove a trailing newline */
445 if (count
> 0 && buf
[count
-1] == '\n')
446 ((char *) buf
)[count
-1] = 0; /* Ugh! */
448 /* Load new medium */
450 if (count
> 0 && buf
[0]) {
451 /* fsg_lun_open() will close existing file if any. */
452 rc
= fsg_lun_open(curlun
, buf
);
454 curlun
->unit_attention_data
=
455 SS_NOT_READY_TO_READY_TRANSITION
;
456 } else if (fsg_lun_is_open(curlun
)) {
457 fsg_lun_close(curlun
);
458 curlun
->unit_attention_data
= SS_MEDIUM_NOT_PRESENT
;
461 return (rc
< 0 ? rc
: count
);
463 EXPORT_SYMBOL_GPL(fsg_store_file
);
465 ssize_t
fsg_store_cdrom(struct fsg_lun
*curlun
, struct rw_semaphore
*filesem
,
466 const char *buf
, size_t count
)
471 ret
= strtobool(buf
, &cdrom
);
476 ret
= cdrom
? _fsg_store_ro(curlun
, true) : 0;
479 curlun
->cdrom
= cdrom
;
486 EXPORT_SYMBOL_GPL(fsg_store_cdrom
);
488 ssize_t
fsg_store_removable(struct fsg_lun
*curlun
, const char *buf
,
494 ret
= strtobool(buf
, &removable
);
498 curlun
->removable
= removable
;
502 EXPORT_SYMBOL_GPL(fsg_store_removable
);
504 MODULE_LICENSE("GPL");