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/kstrtox.h>
27 #include <linux/usb/composite.h>
29 #include "storage_common.h"
31 /* There is only one interface. */
33 struct usb_interface_descriptor fsg_intf_desc = {
34 .bLength = sizeof fsg_intf_desc,
35 .bDescriptorType = USB_DT_INTERFACE,
37 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
38 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
39 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
40 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
41 .iInterface = FSG_STRING_INTERFACE,
43 EXPORT_SYMBOL_GPL(fsg_intf_desc);
46 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
50 struct usb_endpoint_descriptor fsg_fs_bulk_in_desc = {
51 .bLength = USB_DT_ENDPOINT_SIZE,
52 .bDescriptorType = USB_DT_ENDPOINT,
54 .bEndpointAddress = USB_DIR_IN,
55 .bmAttributes = USB_ENDPOINT_XFER_BULK,
56 /* wMaxPacketSize set by autoconfiguration */
58 EXPORT_SYMBOL_GPL(fsg_fs_bulk_in_desc);
60 struct usb_endpoint_descriptor fsg_fs_bulk_out_desc = {
61 .bLength = USB_DT_ENDPOINT_SIZE,
62 .bDescriptorType = USB_DT_ENDPOINT,
64 .bEndpointAddress = USB_DIR_OUT,
65 .bmAttributes = USB_ENDPOINT_XFER_BULK,
66 /* wMaxPacketSize set by autoconfiguration */
68 EXPORT_SYMBOL_GPL(fsg_fs_bulk_out_desc);
70 struct usb_descriptor_header *fsg_fs_function[] = {
71 (struct usb_descriptor_header *) &fsg_intf_desc,
72 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
73 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
76 EXPORT_SYMBOL_GPL(fsg_fs_function);
80 * USB 2.0 devices need to expose both high speed and full speed
81 * descriptors, unless they only run at full speed.
83 * That means alternate endpoint descriptors (bigger packets).
85 struct usb_endpoint_descriptor fsg_hs_bulk_in_desc = {
86 .bLength = USB_DT_ENDPOINT_SIZE,
87 .bDescriptorType = USB_DT_ENDPOINT,
89 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
90 .bmAttributes = USB_ENDPOINT_XFER_BULK,
91 .wMaxPacketSize = cpu_to_le16(512),
93 EXPORT_SYMBOL_GPL(fsg_hs_bulk_in_desc);
95 struct usb_endpoint_descriptor fsg_hs_bulk_out_desc = {
96 .bLength = USB_DT_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_ENDPOINT,
99 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
100 .bmAttributes = USB_ENDPOINT_XFER_BULK,
101 .wMaxPacketSize = cpu_to_le16(512),
102 .bInterval = 1, /* NAK every 1 uframe */
104 EXPORT_SYMBOL_GPL(fsg_hs_bulk_out_desc);
107 struct usb_descriptor_header *fsg_hs_function[] = {
108 (struct usb_descriptor_header *) &fsg_intf_desc,
109 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
110 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
113 EXPORT_SYMBOL_GPL(fsg_hs_function);
115 struct usb_endpoint_descriptor fsg_ss_bulk_in_desc = {
116 .bLength = USB_DT_ENDPOINT_SIZE,
117 .bDescriptorType = USB_DT_ENDPOINT,
119 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
120 .bmAttributes = USB_ENDPOINT_XFER_BULK,
121 .wMaxPacketSize = cpu_to_le16(1024),
123 EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_desc);
125 struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
126 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
127 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
129 /*.bMaxBurst = DYNAMIC, */
131 EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_comp_desc);
133 struct usb_endpoint_descriptor fsg_ss_bulk_out_desc = {
134 .bLength = USB_DT_ENDPOINT_SIZE,
135 .bDescriptorType = USB_DT_ENDPOINT,
137 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
138 .bmAttributes = USB_ENDPOINT_XFER_BULK,
139 .wMaxPacketSize = cpu_to_le16(1024),
141 EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_desc);
143 struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
144 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
145 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
147 /*.bMaxBurst = DYNAMIC, */
149 EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_comp_desc);
151 struct usb_descriptor_header *fsg_ss_function[] = {
152 (struct usb_descriptor_header *) &fsg_intf_desc,
153 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
154 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
155 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
156 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
159 EXPORT_SYMBOL_GPL(fsg_ss_function);
162 /*-------------------------------------------------------------------------*/
165 * If the next two routines are called while the gadget is registered,
166 * the caller must own fsg->filesem for writing.
169 void fsg_lun_close(struct fsg_lun *curlun)
172 LDBG(curlun, "close backing file\n");
177 EXPORT_SYMBOL_GPL(fsg_lun_close);
179 int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
182 struct file *filp = NULL;
184 struct inode *inode = NULL;
188 unsigned int blkbits;
189 unsigned int blksize;
191 /* R/W if we can, R/O if we must */
192 ro = curlun->initially_ro;
194 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
195 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
199 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
201 LINFO(curlun, "unable to open backing file: %s\n", filename);
202 return PTR_ERR(filp);
205 if (!(filp->f_mode & FMODE_WRITE))
208 inode = filp->f_mapping->host;
209 if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
210 LINFO(curlun, "invalid file type: %s\n", filename);
215 * If we can't read the file, it's no good.
216 * If we can't write the file, use it read-only.
218 if (!(filp->f_mode & FMODE_CAN_READ)) {
219 LINFO(curlun, "file not readable: %s\n", filename);
222 if (!(filp->f_mode & FMODE_CAN_WRITE))
225 size = i_size_read(inode);
227 LINFO(curlun, "unable to find file size: %s\n", filename);
235 } else if (S_ISBLK(inode->i_mode)) {
236 blksize = bdev_logical_block_size(I_BDEV(inode));
237 blkbits = blksize_bits(blksize);
243 num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
246 min_sectors = 300; /* Smallest track is 300 frames */
247 if (num_sectors >= 256*60*75) {
248 num_sectors = 256*60*75 - 1;
249 LINFO(curlun, "file too big: %s\n", filename);
250 LINFO(curlun, "using only first %d blocks\n",
254 if (num_sectors < min_sectors) {
255 LINFO(curlun, "file too small: %s\n", filename);
260 if (fsg_lun_is_open(curlun))
261 fsg_lun_close(curlun);
263 curlun->blksize = blksize;
264 curlun->blkbits = blkbits;
267 curlun->file_length = size;
268 curlun->num_sectors = num_sectors;
269 LDBG(curlun, "open backing file: %s\n", filename);
276 EXPORT_SYMBOL_GPL(fsg_lun_open);
279 /*-------------------------------------------------------------------------*/
282 * Sync the file data, don't bother with the metadata.
283 * This code was copied from fs/buffer.c:sys_fdatasync().
285 int fsg_lun_fsync_sub(struct fsg_lun *curlun)
287 struct file *filp = curlun->filp;
289 if (curlun->ro || !filp)
291 return vfs_fsync(filp, 1);
293 EXPORT_SYMBOL_GPL(fsg_lun_fsync_sub);
295 void store_cdrom_address(u8 *dest, int msf, u32 addr)
299 * Convert to Minutes-Seconds-Frames.
300 * Sector size is already set to 2048 bytes.
302 addr += 2*75; /* Lead-in occupies 2 seconds */
303 dest[3] = addr % 75; /* Frames */
305 dest[2] = addr % 60; /* Seconds */
307 dest[1] = addr; /* Minutes */
308 dest[0] = 0; /* Reserved */
310 /* Absolute sector */
311 put_unaligned_be32(addr, dest);
314 EXPORT_SYMBOL_GPL(store_cdrom_address);
316 /*-------------------------------------------------------------------------*/
319 ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf)
321 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
323 : curlun->initially_ro);
325 EXPORT_SYMBOL_GPL(fsg_show_ro);
327 ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf)
329 return sprintf(buf, "%u\n", curlun->nofua);
331 EXPORT_SYMBOL_GPL(fsg_show_nofua);
333 ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
340 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
341 p = file_path(curlun->filp, buf, PAGE_SIZE - 1);
347 buf[rc] = '\n'; /* Add a newline */
350 } else { /* No file, return 0 bytes */
357 EXPORT_SYMBOL_GPL(fsg_show_file);
359 ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf)
361 return sprintf(buf, "%u\n", curlun->cdrom);
363 EXPORT_SYMBOL_GPL(fsg_show_cdrom);
365 ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf)
367 return sprintf(buf, "%u\n", curlun->removable);
369 EXPORT_SYMBOL_GPL(fsg_show_removable);
371 ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf)
373 return sprintf(buf, "%s\n", curlun->inquiry_string);
375 EXPORT_SYMBOL_GPL(fsg_show_inquiry_string);
378 * The caller must hold fsg->filesem for reading when calling this function.
380 static ssize_t _fsg_store_ro(struct fsg_lun *curlun, bool ro)
382 if (fsg_lun_is_open(curlun)) {
383 LDBG(curlun, "read-only status change prevented\n");
388 curlun->initially_ro = ro;
389 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
394 ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
395 const char *buf, size_t count)
400 rc = kstrtobool(buf, &ro);
405 * Allow the write-enable status to change only while the
406 * backing file is closed.
409 rc = _fsg_store_ro(curlun, ro);
416 EXPORT_SYMBOL_GPL(fsg_store_ro);
418 ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count)
423 ret = kstrtobool(buf, &nofua);
427 /* Sync data when switching from async mode to sync */
428 if (!nofua && curlun->nofua)
429 fsg_lun_fsync_sub(curlun);
431 curlun->nofua = nofua;
435 EXPORT_SYMBOL_GPL(fsg_store_nofua);
437 ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
438 const char *buf, size_t count)
442 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
443 LDBG(curlun, "eject attempt prevented\n");
444 return -EBUSY; /* "Door is locked" */
447 /* Remove a trailing newline */
448 if (count > 0 && buf[count-1] == '\n')
449 ((char *) buf)[count-1] = 0; /* Ugh! */
451 /* Load new medium */
453 if (count > 0 && buf[0]) {
454 /* fsg_lun_open() will close existing file if any. */
455 rc = fsg_lun_open(curlun, buf);
457 curlun->unit_attention_data =
458 SS_NOT_READY_TO_READY_TRANSITION;
459 } else if (fsg_lun_is_open(curlun)) {
460 fsg_lun_close(curlun);
461 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
464 return (rc < 0 ? rc : count);
466 EXPORT_SYMBOL_GPL(fsg_store_file);
468 ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
469 const char *buf, size_t count)
474 ret = kstrtobool(buf, &cdrom);
479 ret = cdrom ? _fsg_store_ro(curlun, true) : 0;
482 curlun->cdrom = cdrom;
489 EXPORT_SYMBOL_GPL(fsg_store_cdrom);
491 ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
497 ret = kstrtobool(buf, &removable);
501 curlun->removable = removable;
505 EXPORT_SYMBOL_GPL(fsg_store_removable);
507 ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
510 const size_t len = min(count, sizeof(curlun->inquiry_string));
512 if (len == 0 || buf[0] == '\n') {
513 curlun->inquiry_string[0] = 0;
515 snprintf(curlun->inquiry_string,
516 sizeof(curlun->inquiry_string), "%-28s", buf);
517 if (curlun->inquiry_string[len-1] == '\n')
518 curlun->inquiry_string[len-1] = ' ';
523 EXPORT_SYMBOL_GPL(fsg_store_inquiry_string);
525 ssize_t fsg_store_forced_eject(struct fsg_lun *curlun, struct rw_semaphore *filesem,
526 const char *buf, size_t count)
531 * Forcibly detach the backing file from the LUN
532 * regardless of whether the host has allowed it.
534 curlun->prevent_medium_removal = 0;
535 ret = fsg_store_file(curlun, filesem, "", 0);
536 return ret < 0 ? ret : count;
538 EXPORT_SYMBOL_GPL(fsg_store_forced_eject);
540 MODULE_LICENSE("GPL");