1 // SPDX-License-Identifier: GPL-2.0+
3 * storage_common.c -- Common definitions for mass storage functionality
5 * Copyright (C) 2003-2008 Alan Stern
6 * Copyeight (C) 2009 Samsung Electronics
7 * Author: Michal Nazarewicz (mina86@mina86.com)
11 * This file requires the following identifiers used in USB strings to
12 * be defined (each of type pointer to char):
13 * - fsg_string_interface -- name of the interface
17 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
18 * sets the number of pipeline buffers (length of the fsg_buffhd array).
19 * The valid range of num_buffers is: num >= 2 && num <= 4.
22 #include <linux/module.h>
23 #include <linux/blkdev.h>
24 #include <linux/file.h>
26 #include <linux/usb/composite.h>
28 #include "storage_common.h"
30 /* There is only one interface. */
32 struct usb_interface_descriptor fsg_intf_desc
= {
33 .bLength
= sizeof fsg_intf_desc
,
34 .bDescriptorType
= USB_DT_INTERFACE
,
36 .bNumEndpoints
= 2, /* Adjusted during fsg_bind() */
37 .bInterfaceClass
= USB_CLASS_MASS_STORAGE
,
38 .bInterfaceSubClass
= USB_SC_SCSI
, /* Adjusted during fsg_bind() */
39 .bInterfaceProtocol
= USB_PR_BULK
, /* Adjusted during fsg_bind() */
40 .iInterface
= FSG_STRING_INTERFACE
,
42 EXPORT_SYMBOL_GPL(fsg_intf_desc
);
45 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
49 struct usb_endpoint_descriptor fsg_fs_bulk_in_desc
= {
50 .bLength
= USB_DT_ENDPOINT_SIZE
,
51 .bDescriptorType
= USB_DT_ENDPOINT
,
53 .bEndpointAddress
= USB_DIR_IN
,
54 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
55 /* wMaxPacketSize set by autoconfiguration */
57 EXPORT_SYMBOL_GPL(fsg_fs_bulk_in_desc
);
59 struct usb_endpoint_descriptor fsg_fs_bulk_out_desc
= {
60 .bLength
= USB_DT_ENDPOINT_SIZE
,
61 .bDescriptorType
= USB_DT_ENDPOINT
,
63 .bEndpointAddress
= USB_DIR_OUT
,
64 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
65 /* wMaxPacketSize set by autoconfiguration */
67 EXPORT_SYMBOL_GPL(fsg_fs_bulk_out_desc
);
69 struct usb_descriptor_header
*fsg_fs_function
[] = {
70 (struct usb_descriptor_header
*) &fsg_intf_desc
,
71 (struct usb_descriptor_header
*) &fsg_fs_bulk_in_desc
,
72 (struct usb_descriptor_header
*) &fsg_fs_bulk_out_desc
,
75 EXPORT_SYMBOL_GPL(fsg_fs_function
);
79 * USB 2.0 devices need to expose both high speed and full speed
80 * descriptors, unless they only run at full speed.
82 * That means alternate endpoint descriptors (bigger packets).
84 struct usb_endpoint_descriptor fsg_hs_bulk_in_desc
= {
85 .bLength
= USB_DT_ENDPOINT_SIZE
,
86 .bDescriptorType
= USB_DT_ENDPOINT
,
88 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
89 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
90 .wMaxPacketSize
= cpu_to_le16(512),
92 EXPORT_SYMBOL_GPL(fsg_hs_bulk_in_desc
);
94 struct usb_endpoint_descriptor fsg_hs_bulk_out_desc
= {
95 .bLength
= USB_DT_ENDPOINT_SIZE
,
96 .bDescriptorType
= USB_DT_ENDPOINT
,
98 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
99 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
100 .wMaxPacketSize
= cpu_to_le16(512),
101 .bInterval
= 1, /* NAK every 1 uframe */
103 EXPORT_SYMBOL_GPL(fsg_hs_bulk_out_desc
);
106 struct usb_descriptor_header
*fsg_hs_function
[] = {
107 (struct usb_descriptor_header
*) &fsg_intf_desc
,
108 (struct usb_descriptor_header
*) &fsg_hs_bulk_in_desc
,
109 (struct usb_descriptor_header
*) &fsg_hs_bulk_out_desc
,
112 EXPORT_SYMBOL_GPL(fsg_hs_function
);
114 struct usb_endpoint_descriptor fsg_ss_bulk_in_desc
= {
115 .bLength
= USB_DT_ENDPOINT_SIZE
,
116 .bDescriptorType
= USB_DT_ENDPOINT
,
118 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
119 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
120 .wMaxPacketSize
= cpu_to_le16(1024),
122 EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_desc
);
124 struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc
= {
125 .bLength
= sizeof(fsg_ss_bulk_in_comp_desc
),
126 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
128 /*.bMaxBurst = DYNAMIC, */
130 EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_comp_desc
);
132 struct usb_endpoint_descriptor fsg_ss_bulk_out_desc
= {
133 .bLength
= USB_DT_ENDPOINT_SIZE
,
134 .bDescriptorType
= USB_DT_ENDPOINT
,
136 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
137 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
138 .wMaxPacketSize
= cpu_to_le16(1024),
140 EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_desc
);
142 struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc
= {
143 .bLength
= sizeof(fsg_ss_bulk_in_comp_desc
),
144 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
146 /*.bMaxBurst = DYNAMIC, */
148 EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_comp_desc
);
150 struct usb_descriptor_header
*fsg_ss_function
[] = {
151 (struct usb_descriptor_header
*) &fsg_intf_desc
,
152 (struct usb_descriptor_header
*) &fsg_ss_bulk_in_desc
,
153 (struct usb_descriptor_header
*) &fsg_ss_bulk_in_comp_desc
,
154 (struct usb_descriptor_header
*) &fsg_ss_bulk_out_desc
,
155 (struct usb_descriptor_header
*) &fsg_ss_bulk_out_comp_desc
,
158 EXPORT_SYMBOL_GPL(fsg_ss_function
);
161 /*-------------------------------------------------------------------------*/
164 * If the next two routines are called while the gadget is registered,
165 * the caller must own fsg->filesem for writing.
168 void fsg_lun_close(struct fsg_lun
*curlun
)
171 LDBG(curlun
, "close backing file\n");
176 EXPORT_SYMBOL_GPL(fsg_lun_close
);
178 int fsg_lun_open(struct fsg_lun
*curlun
, const char *filename
)
181 struct file
*filp
= NULL
;
183 struct inode
*inode
= NULL
;
187 unsigned int blkbits
;
188 unsigned int blksize
;
190 /* R/W if we can, R/O if we must */
191 ro
= curlun
->initially_ro
;
193 filp
= filp_open(filename
, O_RDWR
| O_LARGEFILE
, 0);
194 if (PTR_ERR(filp
) == -EROFS
|| PTR_ERR(filp
) == -EACCES
)
198 filp
= filp_open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
200 LINFO(curlun
, "unable to open backing file: %s\n", filename
);
201 return PTR_ERR(filp
);
204 if (!(filp
->f_mode
& FMODE_WRITE
))
207 inode
= filp
->f_mapping
->host
;
208 if ((!S_ISREG(inode
->i_mode
) && !S_ISBLK(inode
->i_mode
))) {
209 LINFO(curlun
, "invalid file type: %s\n", filename
);
214 * If we can't read the file, it's no good.
215 * If we can't write the file, use it read-only.
217 if (!(filp
->f_mode
& FMODE_CAN_READ
)) {
218 LINFO(curlun
, "file not readable: %s\n", filename
);
221 if (!(filp
->f_mode
& FMODE_CAN_WRITE
))
224 size
= i_size_read(inode
);
226 LINFO(curlun
, "unable to find file size: %s\n", filename
);
234 } else if (S_ISBLK(inode
->i_mode
)) {
235 blksize
= bdev_logical_block_size(I_BDEV(inode
));
236 blkbits
= blksize_bits(blksize
);
242 num_sectors
= size
>> blkbits
; /* File size in logic-block-size blocks */
245 min_sectors
= 300; /* Smallest track is 300 frames */
246 if (num_sectors
>= 256*60*75) {
247 num_sectors
= 256*60*75 - 1;
248 LINFO(curlun
, "file too big: %s\n", filename
);
249 LINFO(curlun
, "using only first %d blocks\n",
253 if (num_sectors
< min_sectors
) {
254 LINFO(curlun
, "file too small: %s\n", filename
);
259 if (fsg_lun_is_open(curlun
))
260 fsg_lun_close(curlun
);
262 curlun
->blksize
= blksize
;
263 curlun
->blkbits
= blkbits
;
266 curlun
->file_length
= size
;
267 curlun
->num_sectors
= num_sectors
;
268 LDBG(curlun
, "open backing file: %s\n", filename
);
275 EXPORT_SYMBOL_GPL(fsg_lun_open
);
278 /*-------------------------------------------------------------------------*/
281 * Sync the file data, don't bother with the metadata.
282 * This code was copied from fs/buffer.c:sys_fdatasync().
284 int fsg_lun_fsync_sub(struct fsg_lun
*curlun
)
286 struct file
*filp
= curlun
->filp
;
288 if (curlun
->ro
|| !filp
)
290 return vfs_fsync(filp
, 1);
292 EXPORT_SYMBOL_GPL(fsg_lun_fsync_sub
);
294 void store_cdrom_address(u8
*dest
, int msf
, u32 addr
)
297 /* Convert to Minutes-Seconds-Frames */
298 addr
>>= 2; /* Convert to 2048-byte frames */
299 addr
+= 2*75; /* Lead-in occupies 2 seconds */
300 dest
[3] = addr
% 75; /* Frames */
302 dest
[2] = addr
% 60; /* Seconds */
304 dest
[1] = addr
; /* Minutes */
305 dest
[0] = 0; /* Reserved */
307 /* Absolute sector */
308 put_unaligned_be32(addr
, dest
);
311 EXPORT_SYMBOL_GPL(store_cdrom_address
);
313 /*-------------------------------------------------------------------------*/
316 ssize_t
fsg_show_ro(struct fsg_lun
*curlun
, char *buf
)
318 return sprintf(buf
, "%d\n", fsg_lun_is_open(curlun
)
320 : curlun
->initially_ro
);
322 EXPORT_SYMBOL_GPL(fsg_show_ro
);
324 ssize_t
fsg_show_nofua(struct fsg_lun
*curlun
, char *buf
)
326 return sprintf(buf
, "%u\n", curlun
->nofua
);
328 EXPORT_SYMBOL_GPL(fsg_show_nofua
);
330 ssize_t
fsg_show_file(struct fsg_lun
*curlun
, struct rw_semaphore
*filesem
,
337 if (fsg_lun_is_open(curlun
)) { /* Get the complete pathname */
338 p
= file_path(curlun
->filp
, buf
, PAGE_SIZE
- 1);
344 buf
[rc
] = '\n'; /* Add a newline */
347 } else { /* No file, return 0 bytes */
354 EXPORT_SYMBOL_GPL(fsg_show_file
);
356 ssize_t
fsg_show_cdrom(struct fsg_lun
*curlun
, char *buf
)
358 return sprintf(buf
, "%u\n", curlun
->cdrom
);
360 EXPORT_SYMBOL_GPL(fsg_show_cdrom
);
362 ssize_t
fsg_show_removable(struct fsg_lun
*curlun
, char *buf
)
364 return sprintf(buf
, "%u\n", curlun
->removable
);
366 EXPORT_SYMBOL_GPL(fsg_show_removable
);
368 ssize_t
fsg_show_inquiry_string(struct fsg_lun
*curlun
, char *buf
)
370 return sprintf(buf
, "%s\n", curlun
->inquiry_string
);
372 EXPORT_SYMBOL_GPL(fsg_show_inquiry_string
);
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 ssize_t
fsg_store_inquiry_string(struct fsg_lun
*curlun
, const char *buf
,
507 const size_t len
= min(count
, sizeof(curlun
->inquiry_string
));
509 if (len
== 0 || buf
[0] == '\n') {
510 curlun
->inquiry_string
[0] = 0;
512 snprintf(curlun
->inquiry_string
,
513 sizeof(curlun
->inquiry_string
), "%-28s", buf
);
514 if (curlun
->inquiry_string
[len
-1] == '\n')
515 curlun
->inquiry_string
[len
-1] = ' ';
520 EXPORT_SYMBOL_GPL(fsg_store_inquiry_string
);
522 MODULE_LICENSE("GPL");