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.
16 * This file requires the following identifiers used in USB strings to
17 * be defined (each of type pointer to char):
18 * - fsg_string_manufacturer -- name of the manufacturer
19 * - fsg_string_product -- name of the product
20 * - fsg_string_config -- name of the configuration
21 * - fsg_string_interface -- name of the interface
22 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
23 * macro is defined prior to including this file.
27 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
28 * fsg_hs_intr_in_desc objects as well as
29 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
30 * macros are not defined.
32 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
33 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
34 * defined (as well as corresponding entries in string tables are
35 * missing) and FSG_STRING_INTERFACE has value of zero.
37 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
41 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
42 * sets the number of pipeline buffers (length of the fsg_buffhd array).
43 * The valid range of num_buffers is: num >= 2 && num <= 4.
47 #include <linux/usb/storage.h>
48 #include <scsi/scsi.h>
49 #include <asm/unaligned.h>
53 * Thanks to NetChip Technologies for donating this product ID.
55 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
56 * Instead: allocate your own, using normal USB-IF procedures.
58 #define FSG_VENDOR_ID 0x0525 /* NetChip */
59 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
62 /*-------------------------------------------------------------------------*/
73 #define VLDBG(lun, fmt, args...) do { } while (0)
74 #endif /* VERBOSE_DEBUG */
76 #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
77 #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
78 #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
79 #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
84 # define dump_msg(fsg, /* const char * */ label, \
85 /* const u8 * */ buf, /* unsigned */ length) do { \
87 DBG(fsg, "%s, length %u:\n", label, length); \
88 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
89 16, 1, buf, length, 0); \
93 # define dump_cdb(fsg) do { } while (0)
97 # define dump_msg(fsg, /* const char * */ label, \
98 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
100 # ifdef VERBOSE_DEBUG
102 # define dump_cdb(fsg) \
103 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
104 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
108 # define dump_cdb(fsg) do { } while (0)
110 # endif /* VERBOSE_DEBUG */
112 #endif /* DUMP_MSGS */
114 /*-------------------------------------------------------------------------*/
116 /* CBI Interrupt data structure */
117 struct interrupt_data {
122 #define CBI_INTERRUPT_DATA_LEN 2
124 /* CBI Accept Device-Specific Command request */
125 #define USB_CBI_ADSC_REQUEST 0x00
128 /* Length of a SCSI Command Data Block */
129 #define MAX_COMMAND_SIZE 16
131 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
132 #define SS_NO_SENSE 0
133 #define SS_COMMUNICATION_FAILURE 0x040800
134 #define SS_INVALID_COMMAND 0x052000
135 #define SS_INVALID_FIELD_IN_CDB 0x052400
136 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
137 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
138 #define SS_MEDIUM_NOT_PRESENT 0x023a00
139 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
140 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
141 #define SS_RESET_OCCURRED 0x062900
142 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
143 #define SS_UNRECOVERED_READ_ERROR 0x031100
144 #define SS_WRITE_ERROR 0x030c02
145 #define SS_WRITE_PROTECTED 0x072700
147 #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
148 #define ASC(x) ((u8) ((x) >> 8))
149 #define ASCQ(x) ((u8) (x))
152 /*-------------------------------------------------------------------------*/
160 unsigned int initially_ro:1;
162 unsigned int removable:1;
163 unsigned int cdrom:1;
164 unsigned int prevent_medium_removal:1;
165 unsigned int registered:1;
166 unsigned int info_valid:1;
167 unsigned int nofua:1;
171 u32 unit_attention_data;
173 unsigned int blkbits; /* Bits of logical block size of bound block device */
174 unsigned int blksize; /* logical block size of bound block device */
178 #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
180 static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
182 return container_of(dev, struct fsg_lun, dev);
186 /* Big enough to hold our biggest descriptor */
187 #define EP0_BUFSIZE 256
188 #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
190 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
192 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
193 module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
194 MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
199 * Number of buffers we will use.
200 * 2 is usually enough for good buffering pipeline
202 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
204 #endif /* CONFIG_USB_DEBUG */
206 /* check if fsg_num_buffers is within a valid range */
207 static inline int fsg_num_buffers_validate(void)
209 if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
211 pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
212 fsg_num_buffers, 2 ,4);
216 /* Default size of buffer length. */
217 #define FSG_BUFLEN ((u32)16384)
219 /* Maximal number of LUNs supported in mass storage function */
220 #define FSG_MAX_LUNS 8
222 enum fsg_buffer_state {
230 enum fsg_buffer_state state;
231 struct fsg_buffhd *next;
234 * The NetChip 2280 is faster, and handles some protocol faults
235 * better, if we don't submit any short bulk-out read requests.
236 * So we will record the intended request length here.
238 unsigned int bulk_out_intended_length;
240 struct usb_request *inreq;
242 struct usb_request *outreq;
247 /* This one isn't used anywhere */
248 FSG_STATE_COMMAND_PHASE = -10,
249 FSG_STATE_DATA_PHASE,
250 FSG_STATE_STATUS_PHASE,
253 FSG_STATE_ABORT_BULK_OUT,
255 FSG_STATE_INTERFACE_CHANGE,
256 FSG_STATE_CONFIG_CHANGE,
257 FSG_STATE_DISCONNECT,
262 enum data_direction {
263 DATA_DIR_UNKNOWN = 0,
270 /*-------------------------------------------------------------------------*/
273 static inline u32 get_unaligned_be24(u8 *buf)
275 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
279 /*-------------------------------------------------------------------------*/
283 #ifndef FSG_NO_DEVICE_STRINGS
284 FSG_STRING_MANUFACTURER = 1,
294 static struct usb_otg_descriptor
296 .bLength = sizeof fsg_otg_desc,
297 .bDescriptorType = USB_DT_OTG,
299 .bmAttributes = USB_OTG_SRP,
303 /* There is only one interface. */
305 static struct usb_interface_descriptor
307 .bLength = sizeof fsg_intf_desc,
308 .bDescriptorType = USB_DT_INTERFACE,
310 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
311 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
312 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
313 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
314 .iInterface = FSG_STRING_INTERFACE,
318 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
322 static struct usb_endpoint_descriptor
323 fsg_fs_bulk_in_desc = {
324 .bLength = USB_DT_ENDPOINT_SIZE,
325 .bDescriptorType = USB_DT_ENDPOINT,
327 .bEndpointAddress = USB_DIR_IN,
328 .bmAttributes = USB_ENDPOINT_XFER_BULK,
329 /* wMaxPacketSize set by autoconfiguration */
332 static struct usb_endpoint_descriptor
333 fsg_fs_bulk_out_desc = {
334 .bLength = USB_DT_ENDPOINT_SIZE,
335 .bDescriptorType = USB_DT_ENDPOINT,
337 .bEndpointAddress = USB_DIR_OUT,
338 .bmAttributes = USB_ENDPOINT_XFER_BULK,
339 /* wMaxPacketSize set by autoconfiguration */
342 #ifndef FSG_NO_INTR_EP
344 static struct usb_endpoint_descriptor
345 fsg_fs_intr_in_desc = {
346 .bLength = USB_DT_ENDPOINT_SIZE,
347 .bDescriptorType = USB_DT_ENDPOINT,
349 .bEndpointAddress = USB_DIR_IN,
350 .bmAttributes = USB_ENDPOINT_XFER_INT,
351 .wMaxPacketSize = cpu_to_le16(2),
352 .bInterval = 32, /* frames -> 32 ms */
356 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
358 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
363 static struct usb_descriptor_header *fsg_fs_function[] = {
365 (struct usb_descriptor_header *) &fsg_otg_desc,
367 (struct usb_descriptor_header *) &fsg_intf_desc,
368 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
369 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
370 #ifndef FSG_NO_INTR_EP
371 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
378 * USB 2.0 devices need to expose both high speed and full speed
379 * descriptors, unless they only run at full speed.
381 * That means alternate endpoint descriptors (bigger packets)
382 * and a "device qualifier" ... plus more construction options
383 * for the configuration descriptor.
385 static struct usb_endpoint_descriptor
386 fsg_hs_bulk_in_desc = {
387 .bLength = USB_DT_ENDPOINT_SIZE,
388 .bDescriptorType = USB_DT_ENDPOINT,
390 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
391 .bmAttributes = USB_ENDPOINT_XFER_BULK,
392 .wMaxPacketSize = cpu_to_le16(512),
395 static struct usb_endpoint_descriptor
396 fsg_hs_bulk_out_desc = {
397 .bLength = USB_DT_ENDPOINT_SIZE,
398 .bDescriptorType = USB_DT_ENDPOINT,
400 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
401 .bmAttributes = USB_ENDPOINT_XFER_BULK,
402 .wMaxPacketSize = cpu_to_le16(512),
403 .bInterval = 1, /* NAK every 1 uframe */
406 #ifndef FSG_NO_INTR_EP
408 static struct usb_endpoint_descriptor
409 fsg_hs_intr_in_desc = {
410 .bLength = USB_DT_ENDPOINT_SIZE,
411 .bDescriptorType = USB_DT_ENDPOINT,
413 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
414 .bmAttributes = USB_ENDPOINT_XFER_INT,
415 .wMaxPacketSize = cpu_to_le16(2),
416 .bInterval = USB_MS_TO_HS_INTERVAL(32), /* 32 ms */
420 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
422 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
427 static struct usb_descriptor_header *fsg_hs_function[] = {
429 (struct usb_descriptor_header *) &fsg_otg_desc,
431 (struct usb_descriptor_header *) &fsg_intf_desc,
432 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
433 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
434 #ifndef FSG_NO_INTR_EP
435 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
440 static struct usb_endpoint_descriptor
441 fsg_ss_bulk_in_desc = {
442 .bLength = USB_DT_ENDPOINT_SIZE,
443 .bDescriptorType = USB_DT_ENDPOINT,
445 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
446 .bmAttributes = USB_ENDPOINT_XFER_BULK,
447 .wMaxPacketSize = cpu_to_le16(1024),
450 static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
451 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
452 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
454 /*.bMaxBurst = DYNAMIC, */
457 static struct usb_endpoint_descriptor
458 fsg_ss_bulk_out_desc = {
459 .bLength = USB_DT_ENDPOINT_SIZE,
460 .bDescriptorType = USB_DT_ENDPOINT,
462 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
463 .bmAttributes = USB_ENDPOINT_XFER_BULK,
464 .wMaxPacketSize = cpu_to_le16(1024),
467 static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
468 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
469 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
471 /*.bMaxBurst = DYNAMIC, */
474 #ifndef FSG_NO_INTR_EP
476 static struct usb_endpoint_descriptor
477 fsg_ss_intr_in_desc = {
478 .bLength = USB_DT_ENDPOINT_SIZE,
479 .bDescriptorType = USB_DT_ENDPOINT,
481 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
482 .bmAttributes = USB_ENDPOINT_XFER_INT,
483 .wMaxPacketSize = cpu_to_le16(2),
484 .bInterval = USB_MS_TO_HS_INTERVAL(32), /* 32 ms */
487 static struct usb_ss_ep_comp_descriptor fsg_ss_intr_in_comp_desc = {
488 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
489 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
491 .wBytesPerInterval = cpu_to_le16(2),
495 # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 2
497 # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 1
502 static __maybe_unused struct usb_ext_cap_descriptor fsg_ext_cap_desc = {
503 .bLength = USB_DT_USB_EXT_CAP_SIZE,
504 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
505 .bDevCapabilityType = USB_CAP_TYPE_EXT,
507 .bmAttributes = cpu_to_le32(USB_LPM_SUPPORT),
510 static __maybe_unused struct usb_ss_cap_descriptor fsg_ss_cap_desc = {
511 .bLength = USB_DT_USB_SS_CAP_SIZE,
512 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
513 .bDevCapabilityType = USB_SS_CAP_TYPE,
515 /* .bmAttributes = LTM is not supported yet */
517 .wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION
518 | USB_FULL_SPEED_OPERATION
519 | USB_HIGH_SPEED_OPERATION
520 | USB_5GBPS_OPERATION),
521 .bFunctionalitySupport = USB_LOW_SPEED_OPERATION,
522 .bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT,
523 .bU2DevExitLat = cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT),
526 static __maybe_unused struct usb_bos_descriptor fsg_bos_desc = {
527 .bLength = USB_DT_BOS_SIZE,
528 .bDescriptorType = USB_DT_BOS,
530 .wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE
531 + USB_DT_USB_EXT_CAP_SIZE
532 + USB_DT_USB_SS_CAP_SIZE),
537 static struct usb_descriptor_header *fsg_ss_function[] = {
539 (struct usb_descriptor_header *) &fsg_otg_desc,
541 (struct usb_descriptor_header *) &fsg_intf_desc,
542 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
543 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
544 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
545 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
546 #ifndef FSG_NO_INTR_EP
547 (struct usb_descriptor_header *) &fsg_ss_intr_in_desc,
548 (struct usb_descriptor_header *) &fsg_ss_intr_in_comp_desc,
553 /* Maxpacket and other transfer characteristics vary by speed. */
554 static __maybe_unused struct usb_endpoint_descriptor *
555 fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
556 struct usb_endpoint_descriptor *hs,
557 struct usb_endpoint_descriptor *ss)
559 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
561 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
567 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
568 static struct usb_string fsg_strings[] = {
569 #ifndef FSG_NO_DEVICE_STRINGS
570 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
571 {FSG_STRING_PRODUCT, fsg_string_product},
572 {FSG_STRING_SERIAL, ""},
573 {FSG_STRING_CONFIG, fsg_string_config},
575 {FSG_STRING_INTERFACE, fsg_string_interface},
579 static struct usb_gadget_strings fsg_stringtab = {
580 .language = 0x0409, /* en-us */
581 .strings = fsg_strings,
585 /*-------------------------------------------------------------------------*/
588 * If the next two routines are called while the gadget is registered,
589 * the caller must own fsg->filesem for writing.
592 static void fsg_lun_close(struct fsg_lun *curlun)
595 LDBG(curlun, "close backing file\n");
602 static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
605 struct file *filp = NULL;
607 struct inode *inode = NULL;
611 unsigned int blkbits;
612 unsigned int blksize;
614 /* R/W if we can, R/O if we must */
615 ro = curlun->initially_ro;
617 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
618 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
622 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
624 LINFO(curlun, "unable to open backing file: %s\n", filename);
625 return PTR_ERR(filp);
628 if (!(filp->f_mode & FMODE_WRITE))
631 inode = filp->f_path.dentry->d_inode;
632 if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
633 LINFO(curlun, "invalid file type: %s\n", filename);
638 * If we can't read the file, it's no good.
639 * If we can't write the file, use it read-only.
641 if (!(filp->f_op->read || filp->f_op->aio_read)) {
642 LINFO(curlun, "file not readable: %s\n", filename);
645 if (!(filp->f_op->write || filp->f_op->aio_write))
648 size = i_size_read(inode->i_mapping->host);
650 LINFO(curlun, "unable to find file size: %s\n", filename);
658 } else if (inode->i_bdev) {
659 blksize = bdev_logical_block_size(inode->i_bdev);
660 blkbits = blksize_bits(blksize);
666 num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
669 min_sectors = 300; /* Smallest track is 300 frames */
670 if (num_sectors >= 256*60*75) {
671 num_sectors = 256*60*75 - 1;
672 LINFO(curlun, "file too big: %s\n", filename);
673 LINFO(curlun, "using only first %d blocks\n",
677 if (num_sectors < min_sectors) {
678 LINFO(curlun, "file too small: %s\n", filename);
683 if (fsg_lun_is_open(curlun))
684 fsg_lun_close(curlun);
686 curlun->blksize = blksize;
687 curlun->blkbits = blkbits;
690 curlun->file_length = size;
691 curlun->num_sectors = num_sectors;
692 LDBG(curlun, "open backing file: %s\n", filename);
701 /*-------------------------------------------------------------------------*/
704 * Sync the file data, don't bother with the metadata.
705 * This code was copied from fs/buffer.c:sys_fdatasync().
707 static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
709 struct file *filp = curlun->filp;
711 if (curlun->ro || !filp)
713 return vfs_fsync(filp, 1);
716 static void store_cdrom_address(u8 *dest, int msf, u32 addr)
719 /* Convert to Minutes-Seconds-Frames */
720 addr >>= 2; /* Convert to 2048-byte frames */
721 addr += 2*75; /* Lead-in occupies 2 seconds */
722 dest[3] = addr % 75; /* Frames */
724 dest[2] = addr % 60; /* Seconds */
726 dest[1] = addr; /* Minutes */
727 dest[0] = 0; /* Reserved */
729 /* Absolute sector */
730 put_unaligned_be32(addr, dest);
735 /*-------------------------------------------------------------------------*/
738 static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
741 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
743 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
745 : curlun->initially_ro);
748 static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
751 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
753 return sprintf(buf, "%u\n", curlun->nofua);
756 static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
759 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
760 struct rw_semaphore *filesem = dev_get_drvdata(dev);
765 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
766 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
772 buf[rc] = '\n'; /* Add a newline */
775 } else { /* No file, return 0 bytes */
784 static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
785 const char *buf, size_t count)
788 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
789 struct rw_semaphore *filesem = dev_get_drvdata(dev);
792 rc = kstrtouint(buf, 2, &ro);
797 * Allow the write-enable status to change only while the
798 * backing file is closed.
801 if (fsg_lun_is_open(curlun)) {
802 LDBG(curlun, "read-only status change prevented\n");
806 curlun->initially_ro = ro;
807 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
814 static ssize_t fsg_store_nofua(struct device *dev,
815 struct device_attribute *attr,
816 const char *buf, size_t count)
818 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
822 ret = kstrtouint(buf, 2, &nofua);
826 /* Sync data when switching from async mode to sync */
827 if (!nofua && curlun->nofua)
828 fsg_lun_fsync_sub(curlun);
830 curlun->nofua = nofua;
835 static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
836 const char *buf, size_t count)
838 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
839 struct rw_semaphore *filesem = dev_get_drvdata(dev);
842 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
843 LDBG(curlun, "eject attempt prevented\n");
844 return -EBUSY; /* "Door is locked" */
847 /* Remove a trailing newline */
848 if (count > 0 && buf[count-1] == '\n')
849 ((char *) buf)[count-1] = 0; /* Ugh! */
851 /* Load new medium */
853 if (count > 0 && buf[0]) {
854 /* fsg_lun_open() will close existing file if any. */
855 rc = fsg_lun_open(curlun, buf);
857 curlun->unit_attention_data =
858 SS_NOT_READY_TO_READY_TRANSITION;
859 } else if (fsg_lun_is_open(curlun)) {
860 fsg_lun_close(curlun);
861 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
864 return (rc < 0 ? rc : count);