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)
82 * Keep those macros in sync with those in
83 * include/linux/usb/composite.h or else GCC will complain. If they
84 * are identical (the same names of arguments, white spaces in the
85 * same places) GCC will allow redefinition otherwise (even if some
86 * white space is removed or added) warning will be issued.
88 * Those macros are needed here because File Storage Gadget does not
89 * include the composite.h header. For composite gadgets those macros
90 * are redundant since composite.h is included any way.
92 * One could check whether those macros are already defined (which
93 * would indicate composite.h had been included) or not (which would
94 * indicate we were in FSG) but this is not done because a warning is
95 * desired if definitions here differ from the ones in composite.h.
97 * We want the definitions to match and be the same in File Storage
98 * Gadget as well as Mass Storage Function (and so composite gadgets
99 * using MSF). If someone changes them in composite.h it will produce
100 * a warning in this file when building MSF.
102 #define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
103 #define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
104 #define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
105 #define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
106 #define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
112 # define dump_msg(fsg, /* const char * */ label, \
113 /* const u8 * */ buf, /* unsigned */ length) do { \
114 if (length < 512) { \
115 DBG(fsg, "%s, length %u:\n", label, length); \
116 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
117 16, 1, buf, length, 0); \
121 # define dump_cdb(fsg) do { } while (0)
125 # define dump_msg(fsg, /* const char * */ label, \
126 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
128 # ifdef VERBOSE_DEBUG
130 # define dump_cdb(fsg) \
131 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
132 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
136 # define dump_cdb(fsg) do { } while (0)
138 # endif /* VERBOSE_DEBUG */
140 #endif /* DUMP_MSGS */
142 /*-------------------------------------------------------------------------*/
144 /* CBI Interrupt data structure */
145 struct interrupt_data {
150 #define CBI_INTERRUPT_DATA_LEN 2
152 /* CBI Accept Device-Specific Command request */
153 #define USB_CBI_ADSC_REQUEST 0x00
156 /* Length of a SCSI Command Data Block */
157 #define MAX_COMMAND_SIZE 16
159 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
160 #define SS_NO_SENSE 0
161 #define SS_COMMUNICATION_FAILURE 0x040800
162 #define SS_INVALID_COMMAND 0x052000
163 #define SS_INVALID_FIELD_IN_CDB 0x052400
164 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
165 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
166 #define SS_MEDIUM_NOT_PRESENT 0x023a00
167 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
168 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
169 #define SS_RESET_OCCURRED 0x062900
170 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
171 #define SS_UNRECOVERED_READ_ERROR 0x031100
172 #define SS_WRITE_ERROR 0x030c02
173 #define SS_WRITE_PROTECTED 0x072700
175 #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
176 #define ASC(x) ((u8) ((x) >> 8))
177 #define ASCQ(x) ((u8) (x))
180 /*-------------------------------------------------------------------------*/
188 unsigned int initially_ro:1;
190 unsigned int removable:1;
191 unsigned int cdrom:1;
192 unsigned int prevent_medium_removal:1;
193 unsigned int registered:1;
194 unsigned int info_valid:1;
195 unsigned int nofua:1;
199 u32 unit_attention_data;
201 unsigned int blkbits; /* Bits of logical block size of bound block device */
202 unsigned int blksize; /* logical block size of bound block device */
206 #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
208 static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
210 return container_of(dev, struct fsg_lun, dev);
214 /* Big enough to hold our biggest descriptor */
215 #define EP0_BUFSIZE 256
216 #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
218 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
220 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
221 module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
222 MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
227 * Number of buffers we will use.
228 * 2 is usually enough for good buffering pipeline
230 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
232 #endif /* CONFIG_USB_DEBUG */
234 /* check if fsg_num_buffers is within a valid range */
235 static inline int fsg_num_buffers_validate(void)
237 if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
239 pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
240 fsg_num_buffers, 2 ,4);
244 /* Default size of buffer length. */
245 #define FSG_BUFLEN ((u32)16384)
247 /* Maximal number of LUNs supported in mass storage function */
248 #define FSG_MAX_LUNS 8
250 enum fsg_buffer_state {
258 enum fsg_buffer_state state;
259 struct fsg_buffhd *next;
262 * The NetChip 2280 is faster, and handles some protocol faults
263 * better, if we don't submit any short bulk-out read requests.
264 * So we will record the intended request length here.
266 unsigned int bulk_out_intended_length;
268 struct usb_request *inreq;
270 struct usb_request *outreq;
275 /* This one isn't used anywhere */
276 FSG_STATE_COMMAND_PHASE = -10,
277 FSG_STATE_DATA_PHASE,
278 FSG_STATE_STATUS_PHASE,
281 FSG_STATE_ABORT_BULK_OUT,
283 FSG_STATE_INTERFACE_CHANGE,
284 FSG_STATE_CONFIG_CHANGE,
285 FSG_STATE_DISCONNECT,
290 enum data_direction {
291 DATA_DIR_UNKNOWN = 0,
298 /*-------------------------------------------------------------------------*/
301 static inline u32 get_unaligned_be24(u8 *buf)
303 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
307 /*-------------------------------------------------------------------------*/
311 #ifndef FSG_NO_DEVICE_STRINGS
312 FSG_STRING_MANUFACTURER = 1,
322 static struct usb_otg_descriptor
324 .bLength = sizeof fsg_otg_desc,
325 .bDescriptorType = USB_DT_OTG,
327 .bmAttributes = USB_OTG_SRP,
331 /* There is only one interface. */
333 static struct usb_interface_descriptor
335 .bLength = sizeof fsg_intf_desc,
336 .bDescriptorType = USB_DT_INTERFACE,
338 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
339 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
340 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
341 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
342 .iInterface = FSG_STRING_INTERFACE,
346 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
350 static struct usb_endpoint_descriptor
351 fsg_fs_bulk_in_desc = {
352 .bLength = USB_DT_ENDPOINT_SIZE,
353 .bDescriptorType = USB_DT_ENDPOINT,
355 .bEndpointAddress = USB_DIR_IN,
356 .bmAttributes = USB_ENDPOINT_XFER_BULK,
357 /* wMaxPacketSize set by autoconfiguration */
360 static struct usb_endpoint_descriptor
361 fsg_fs_bulk_out_desc = {
362 .bLength = USB_DT_ENDPOINT_SIZE,
363 .bDescriptorType = USB_DT_ENDPOINT,
365 .bEndpointAddress = USB_DIR_OUT,
366 .bmAttributes = USB_ENDPOINT_XFER_BULK,
367 /* wMaxPacketSize set by autoconfiguration */
370 #ifndef FSG_NO_INTR_EP
372 static struct usb_endpoint_descriptor
373 fsg_fs_intr_in_desc = {
374 .bLength = USB_DT_ENDPOINT_SIZE,
375 .bDescriptorType = USB_DT_ENDPOINT,
377 .bEndpointAddress = USB_DIR_IN,
378 .bmAttributes = USB_ENDPOINT_XFER_INT,
379 .wMaxPacketSize = cpu_to_le16(2),
380 .bInterval = 32, /* frames -> 32 ms */
384 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
386 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
391 static struct usb_descriptor_header *fsg_fs_function[] = {
393 (struct usb_descriptor_header *) &fsg_otg_desc,
395 (struct usb_descriptor_header *) &fsg_intf_desc,
396 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
397 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
398 #ifndef FSG_NO_INTR_EP
399 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
406 * USB 2.0 devices need to expose both high speed and full speed
407 * descriptors, unless they only run at full speed.
409 * That means alternate endpoint descriptors (bigger packets)
410 * and a "device qualifier" ... plus more construction options
411 * for the configuration descriptor.
413 static struct usb_endpoint_descriptor
414 fsg_hs_bulk_in_desc = {
415 .bLength = USB_DT_ENDPOINT_SIZE,
416 .bDescriptorType = USB_DT_ENDPOINT,
418 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
419 .bmAttributes = USB_ENDPOINT_XFER_BULK,
420 .wMaxPacketSize = cpu_to_le16(512),
423 static struct usb_endpoint_descriptor
424 fsg_hs_bulk_out_desc = {
425 .bLength = USB_DT_ENDPOINT_SIZE,
426 .bDescriptorType = USB_DT_ENDPOINT,
428 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
429 .bmAttributes = USB_ENDPOINT_XFER_BULK,
430 .wMaxPacketSize = cpu_to_le16(512),
431 .bInterval = 1, /* NAK every 1 uframe */
434 #ifndef FSG_NO_INTR_EP
436 static struct usb_endpoint_descriptor
437 fsg_hs_intr_in_desc = {
438 .bLength = USB_DT_ENDPOINT_SIZE,
439 .bDescriptorType = USB_DT_ENDPOINT,
441 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
442 .bmAttributes = USB_ENDPOINT_XFER_INT,
443 .wMaxPacketSize = cpu_to_le16(2),
444 .bInterval = USB_MS_TO_HS_INTERVAL(32), /* 32 ms */
448 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
450 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
455 static struct usb_descriptor_header *fsg_hs_function[] = {
457 (struct usb_descriptor_header *) &fsg_otg_desc,
459 (struct usb_descriptor_header *) &fsg_intf_desc,
460 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
461 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
462 #ifndef FSG_NO_INTR_EP
463 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
468 static struct usb_endpoint_descriptor
469 fsg_ss_bulk_in_desc = {
470 .bLength = USB_DT_ENDPOINT_SIZE,
471 .bDescriptorType = USB_DT_ENDPOINT,
473 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
474 .bmAttributes = USB_ENDPOINT_XFER_BULK,
475 .wMaxPacketSize = cpu_to_le16(1024),
478 static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
479 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
480 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
482 /*.bMaxBurst = DYNAMIC, */
485 static struct usb_endpoint_descriptor
486 fsg_ss_bulk_out_desc = {
487 .bLength = USB_DT_ENDPOINT_SIZE,
488 .bDescriptorType = USB_DT_ENDPOINT,
490 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
491 .bmAttributes = USB_ENDPOINT_XFER_BULK,
492 .wMaxPacketSize = cpu_to_le16(1024),
495 static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
496 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
497 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
499 /*.bMaxBurst = DYNAMIC, */
502 #ifndef FSG_NO_INTR_EP
504 static struct usb_endpoint_descriptor
505 fsg_ss_intr_in_desc = {
506 .bLength = USB_DT_ENDPOINT_SIZE,
507 .bDescriptorType = USB_DT_ENDPOINT,
509 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
510 .bmAttributes = USB_ENDPOINT_XFER_INT,
511 .wMaxPacketSize = cpu_to_le16(2),
512 .bInterval = USB_MS_TO_HS_INTERVAL(32), /* 32 ms */
515 static struct usb_ss_ep_comp_descriptor fsg_ss_intr_in_comp_desc = {
516 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
517 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
519 .wBytesPerInterval = cpu_to_le16(2),
523 # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 2
525 # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 1
530 static __maybe_unused struct usb_ext_cap_descriptor fsg_ext_cap_desc = {
531 .bLength = USB_DT_USB_EXT_CAP_SIZE,
532 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
533 .bDevCapabilityType = USB_CAP_TYPE_EXT,
535 .bmAttributes = cpu_to_le32(USB_LPM_SUPPORT),
538 static __maybe_unused struct usb_ss_cap_descriptor fsg_ss_cap_desc = {
539 .bLength = USB_DT_USB_SS_CAP_SIZE,
540 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
541 .bDevCapabilityType = USB_SS_CAP_TYPE,
543 /* .bmAttributes = LTM is not supported yet */
545 .wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION
546 | USB_FULL_SPEED_OPERATION
547 | USB_HIGH_SPEED_OPERATION
548 | USB_5GBPS_OPERATION),
549 .bFunctionalitySupport = USB_LOW_SPEED_OPERATION,
550 .bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT,
551 .bU2DevExitLat = cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT),
554 static __maybe_unused struct usb_bos_descriptor fsg_bos_desc = {
555 .bLength = USB_DT_BOS_SIZE,
556 .bDescriptorType = USB_DT_BOS,
558 .wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE
559 + USB_DT_USB_EXT_CAP_SIZE
560 + USB_DT_USB_SS_CAP_SIZE),
565 static struct usb_descriptor_header *fsg_ss_function[] = {
567 (struct usb_descriptor_header *) &fsg_otg_desc,
569 (struct usb_descriptor_header *) &fsg_intf_desc,
570 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
571 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
572 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
573 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
574 #ifndef FSG_NO_INTR_EP
575 (struct usb_descriptor_header *) &fsg_ss_intr_in_desc,
576 (struct usb_descriptor_header *) &fsg_ss_intr_in_comp_desc,
581 /* Maxpacket and other transfer characteristics vary by speed. */
582 static __maybe_unused struct usb_endpoint_descriptor *
583 fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
584 struct usb_endpoint_descriptor *hs,
585 struct usb_endpoint_descriptor *ss)
587 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
589 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
595 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
596 static struct usb_string fsg_strings[] = {
597 #ifndef FSG_NO_DEVICE_STRINGS
598 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
599 {FSG_STRING_PRODUCT, fsg_string_product},
600 {FSG_STRING_SERIAL, ""},
601 {FSG_STRING_CONFIG, fsg_string_config},
603 {FSG_STRING_INTERFACE, fsg_string_interface},
607 static struct usb_gadget_strings fsg_stringtab = {
608 .language = 0x0409, /* en-us */
609 .strings = fsg_strings,
613 /*-------------------------------------------------------------------------*/
616 * If the next two routines are called while the gadget is registered,
617 * the caller must own fsg->filesem for writing.
620 static void fsg_lun_close(struct fsg_lun *curlun)
623 LDBG(curlun, "close backing file\n");
630 static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
633 struct file *filp = NULL;
635 struct inode *inode = NULL;
639 unsigned int blkbits;
640 unsigned int blksize;
642 /* R/W if we can, R/O if we must */
643 ro = curlun->initially_ro;
645 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
646 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
650 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
652 LINFO(curlun, "unable to open backing file: %s\n", filename);
653 return PTR_ERR(filp);
656 if (!(filp->f_mode & FMODE_WRITE))
659 inode = filp->f_path.dentry->d_inode;
660 if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
661 LINFO(curlun, "invalid file type: %s\n", filename);
666 * If we can't read the file, it's no good.
667 * If we can't write the file, use it read-only.
669 if (!(filp->f_op->read || filp->f_op->aio_read)) {
670 LINFO(curlun, "file not readable: %s\n", filename);
673 if (!(filp->f_op->write || filp->f_op->aio_write))
676 size = i_size_read(inode->i_mapping->host);
678 LINFO(curlun, "unable to find file size: %s\n", filename);
686 } else if (inode->i_bdev) {
687 blksize = bdev_logical_block_size(inode->i_bdev);
688 blkbits = blksize_bits(blksize);
694 num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
697 min_sectors = 300; /* Smallest track is 300 frames */
698 if (num_sectors >= 256*60*75) {
699 num_sectors = 256*60*75 - 1;
700 LINFO(curlun, "file too big: %s\n", filename);
701 LINFO(curlun, "using only first %d blocks\n",
705 if (num_sectors < min_sectors) {
706 LINFO(curlun, "file too small: %s\n", filename);
711 if (fsg_lun_is_open(curlun))
712 fsg_lun_close(curlun);
714 curlun->blksize = blksize;
715 curlun->blkbits = blkbits;
718 curlun->file_length = size;
719 curlun->num_sectors = num_sectors;
720 LDBG(curlun, "open backing file: %s\n", filename);
729 /*-------------------------------------------------------------------------*/
732 * Sync the file data, don't bother with the metadata.
733 * This code was copied from fs/buffer.c:sys_fdatasync().
735 static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
737 struct file *filp = curlun->filp;
739 if (curlun->ro || !filp)
741 return vfs_fsync(filp, 1);
744 static void store_cdrom_address(u8 *dest, int msf, u32 addr)
747 /* Convert to Minutes-Seconds-Frames */
748 addr >>= 2; /* Convert to 2048-byte frames */
749 addr += 2*75; /* Lead-in occupies 2 seconds */
750 dest[3] = addr % 75; /* Frames */
752 dest[2] = addr % 60; /* Seconds */
754 dest[1] = addr; /* Minutes */
755 dest[0] = 0; /* Reserved */
757 /* Absolute sector */
758 put_unaligned_be32(addr, dest);
763 /*-------------------------------------------------------------------------*/
766 static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
769 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
771 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
773 : curlun->initially_ro);
776 static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
779 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
781 return sprintf(buf, "%u\n", curlun->nofua);
784 static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
787 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
788 struct rw_semaphore *filesem = dev_get_drvdata(dev);
793 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
794 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
800 buf[rc] = '\n'; /* Add a newline */
803 } else { /* No file, return 0 bytes */
812 static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
813 const char *buf, size_t count)
816 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
817 struct rw_semaphore *filesem = dev_get_drvdata(dev);
820 rc = kstrtouint(buf, 2, &ro);
825 * Allow the write-enable status to change only while the
826 * backing file is closed.
829 if (fsg_lun_is_open(curlun)) {
830 LDBG(curlun, "read-only status change prevented\n");
834 curlun->initially_ro = ro;
835 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
842 static ssize_t fsg_store_nofua(struct device *dev,
843 struct device_attribute *attr,
844 const char *buf, size_t count)
846 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
850 ret = kstrtouint(buf, 2, &nofua);
854 /* Sync data when switching from async mode to sync */
855 if (!nofua && curlun->nofua)
856 fsg_lun_fsync_sub(curlun);
858 curlun->nofua = nofua;
863 static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
864 const char *buf, size_t count)
866 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
867 struct rw_semaphore *filesem = dev_get_drvdata(dev);
870 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
871 LDBG(curlun, "eject attempt prevented\n");
872 return -EBUSY; /* "Door is locked" */
875 /* Remove a trailing newline */
876 if (count > 0 && buf[count-1] == '\n')
877 ((char *) buf)[count-1] = 0; /* Ugh! */
879 /* Load new medium */
881 if (count > 0 && buf[0]) {
882 /* fsg_lun_open() will close existing file if any. */
883 rc = fsg_lun_open(curlun, buf);
885 curlun->unit_attention_data =
886 SS_NOT_READY_TO_READY_TRANSITION;
887 } else if (fsg_lun_is_open(curlun)) {
888 fsg_lun_close(curlun);
889 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
892 return (rc < 0 ? rc : count);