usb: gadget: storage: make FSG_NUM_BUFFERS variable size
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / storage_common.c
blob9fd379998608d1f72c3b33c9a4f240cb9475e951
1 /*
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 (m.nazarewicz@samsung.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.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This file requires the following identifiers used in USB strings to
26 * be defined (each of type pointer to char):
27 * - fsg_string_manufacturer -- name of the manufacturer
28 * - fsg_string_product -- name of the product
29 * - fsg_string_config -- name of the configuration
30 * - fsg_string_interface -- name of the interface
31 * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
32 * macro is defined prior to including this file.
36 * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
37 * fsg_hs_intr_in_desc objects as well as
38 * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
39 * macros are not defined.
41 * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
42 * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
43 * defined (as well as corresponding entries in string tables are
44 * missing) and FSG_STRING_INTERFACE has value of zero.
46 * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
50 * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
51 * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
52 * characters rather then a pointer to void.
56 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
57 * sets the number of pipeline buffers (length of the fsg_buffhd array).
58 * The valid range of num_buffers is: num >= 2 && num <= 4.
62 #include <linux/usb/storage.h>
63 #include <scsi/scsi.h>
64 #include <asm/unaligned.h>
68 * Thanks to NetChip Technologies for donating this product ID.
70 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
71 * Instead: allocate your own, using normal USB-IF procedures.
73 #define FSG_VENDOR_ID 0x0525 /* NetChip */
74 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
77 /*-------------------------------------------------------------------------*/
80 #ifndef DEBUG
81 #undef VERBOSE_DEBUG
82 #undef DUMP_MSGS
83 #endif /* !DEBUG */
85 #ifdef VERBOSE_DEBUG
86 #define VLDBG LDBG
87 #else
88 #define VLDBG(lun, fmt, args...) do { } while (0)
89 #endif /* VERBOSE_DEBUG */
91 #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
92 #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
93 #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
94 #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
97 * Keep those macros in sync with those in
98 * include/linux/usb/composite.h or else GCC will complain. If they
99 * are identical (the same names of arguments, white spaces in the
100 * same places) GCC will allow redefinition otherwise (even if some
101 * white space is removed or added) warning will be issued.
103 * Those macros are needed here because File Storage Gadget does not
104 * include the composite.h header. For composite gadgets those macros
105 * are redundant since composite.h is included any way.
107 * One could check whether those macros are already defined (which
108 * would indicate composite.h had been included) or not (which would
109 * indicate we were in FSG) but this is not done because a warning is
110 * desired if definitions here differ from the ones in composite.h.
112 * We want the definitions to match and be the same in File Storage
113 * Gadget as well as Mass Storage Function (and so composite gadgets
114 * using MSF). If someone changes them in composite.h it will produce
115 * a warning in this file when building MSF.
117 #define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
118 #define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
119 #define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
120 #define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
121 #define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
125 #ifdef DUMP_MSGS
127 # define dump_msg(fsg, /* const char * */ label, \
128 /* const u8 * */ buf, /* unsigned */ length) do { \
129 if (length < 512) { \
130 DBG(fsg, "%s, length %u:\n", label, length); \
131 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
132 16, 1, buf, length, 0); \
134 } while (0)
136 # define dump_cdb(fsg) do { } while (0)
138 #else
140 # define dump_msg(fsg, /* const char * */ label, \
141 /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
143 # ifdef VERBOSE_DEBUG
145 # define dump_cdb(fsg) \
146 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
147 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
149 # else
151 # define dump_cdb(fsg) do { } while (0)
153 # endif /* VERBOSE_DEBUG */
155 #endif /* DUMP_MSGS */
161 /*-------------------------------------------------------------------------*/
163 /* Bulk-only data structures */
165 /* Command Block Wrapper */
166 struct fsg_bulk_cb_wrap {
167 __le32 Signature; /* Contains 'USBC' */
168 u32 Tag; /* Unique per command id */
169 __le32 DataTransferLength; /* Size of the data */
170 u8 Flags; /* Direction in bit 7 */
171 u8 Lun; /* LUN (normally 0) */
172 u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
173 u8 CDB[16]; /* Command Data Block */
176 #define USB_BULK_CB_WRAP_LEN 31
177 #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
178 #define USB_BULK_IN_FLAG 0x80
180 /* Command Status Wrapper */
181 struct bulk_cs_wrap {
182 __le32 Signature; /* Should = 'USBS' */
183 u32 Tag; /* Same as original command */
184 __le32 Residue; /* Amount not transferred */
185 u8 Status; /* See below */
188 #define USB_BULK_CS_WRAP_LEN 13
189 #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
190 #define USB_STATUS_PASS 0
191 #define USB_STATUS_FAIL 1
192 #define USB_STATUS_PHASE_ERROR 2
194 /* Bulk-only class specific requests */
195 #define USB_BULK_RESET_REQUEST 0xff
196 #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
199 /* CBI Interrupt data structure */
200 struct interrupt_data {
201 u8 bType;
202 u8 bValue;
205 #define CBI_INTERRUPT_DATA_LEN 2
207 /* CBI Accept Device-Specific Command request */
208 #define USB_CBI_ADSC_REQUEST 0x00
211 /* Length of a SCSI Command Data Block */
212 #define MAX_COMMAND_SIZE 16
214 /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
215 #define SS_NO_SENSE 0
216 #define SS_COMMUNICATION_FAILURE 0x040800
217 #define SS_INVALID_COMMAND 0x052000
218 #define SS_INVALID_FIELD_IN_CDB 0x052400
219 #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
220 #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
221 #define SS_MEDIUM_NOT_PRESENT 0x023a00
222 #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
223 #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
224 #define SS_RESET_OCCURRED 0x062900
225 #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
226 #define SS_UNRECOVERED_READ_ERROR 0x031100
227 #define SS_WRITE_ERROR 0x030c02
228 #define SS_WRITE_PROTECTED 0x072700
230 #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
231 #define ASC(x) ((u8) ((x) >> 8))
232 #define ASCQ(x) ((u8) (x))
235 /*-------------------------------------------------------------------------*/
238 struct fsg_lun {
239 struct file *filp;
240 loff_t file_length;
241 loff_t num_sectors;
243 unsigned int initially_ro:1;
244 unsigned int ro:1;
245 unsigned int removable:1;
246 unsigned int cdrom:1;
247 unsigned int prevent_medium_removal:1;
248 unsigned int registered:1;
249 unsigned int info_valid:1;
250 unsigned int nofua:1;
252 u32 sense_data;
253 u32 sense_data_info;
254 u32 unit_attention_data;
256 unsigned int blkbits; /* Bits of logical block size of bound block device */
257 unsigned int blksize; /* logical block size of bound block device */
258 struct device dev;
261 #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
263 static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
265 return container_of(dev, struct fsg_lun, dev);
269 /* Big enough to hold our biggest descriptor */
270 #define EP0_BUFSIZE 256
271 #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
273 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
275 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
276 module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);
277 MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers");
279 #else
282 * Number of buffers we will use.
283 * 2 is usually enough for good buffering pipeline
285 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
287 #endif /* CONFIG_USB_DEBUG */
289 /* check if fsg_num_buffers is within a valid range */
290 static inline int fsg_num_buffers_validate(void)
292 if (fsg_num_buffers >= 2 && fsg_num_buffers <= 4)
293 return 0;
294 pr_err("fsg_num_buffers %u is out of range (%d to %d)\n",
295 fsg_num_buffers, 2 ,4);
296 return -EINVAL;
299 /* Default size of buffer length. */
300 #define FSG_BUFLEN ((u32)16384)
302 /* Maximal number of LUNs supported in mass storage function */
303 #define FSG_MAX_LUNS 8
305 enum fsg_buffer_state {
306 BUF_STATE_EMPTY = 0,
307 BUF_STATE_FULL,
308 BUF_STATE_BUSY
311 struct fsg_buffhd {
312 #ifdef FSG_BUFFHD_STATIC_BUFFER
313 char buf[FSG_BUFLEN];
314 #else
315 void *buf;
316 #endif
317 enum fsg_buffer_state state;
318 struct fsg_buffhd *next;
321 * The NetChip 2280 is faster, and handles some protocol faults
322 * better, if we don't submit any short bulk-out read requests.
323 * So we will record the intended request length here.
325 unsigned int bulk_out_intended_length;
327 struct usb_request *inreq;
328 int inreq_busy;
329 struct usb_request *outreq;
330 int outreq_busy;
333 enum fsg_state {
334 /* This one isn't used anywhere */
335 FSG_STATE_COMMAND_PHASE = -10,
336 FSG_STATE_DATA_PHASE,
337 FSG_STATE_STATUS_PHASE,
339 FSG_STATE_IDLE = 0,
340 FSG_STATE_ABORT_BULK_OUT,
341 FSG_STATE_RESET,
342 FSG_STATE_INTERFACE_CHANGE,
343 FSG_STATE_CONFIG_CHANGE,
344 FSG_STATE_DISCONNECT,
345 FSG_STATE_EXIT,
346 FSG_STATE_TERMINATED
349 enum data_direction {
350 DATA_DIR_UNKNOWN = 0,
351 DATA_DIR_FROM_HOST,
352 DATA_DIR_TO_HOST,
353 DATA_DIR_NONE
357 /*-------------------------------------------------------------------------*/
360 static inline u32 get_unaligned_be24(u8 *buf)
362 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
366 /*-------------------------------------------------------------------------*/
369 enum {
370 #ifndef FSG_NO_DEVICE_STRINGS
371 FSG_STRING_MANUFACTURER = 1,
372 FSG_STRING_PRODUCT,
373 FSG_STRING_SERIAL,
374 FSG_STRING_CONFIG,
375 #endif
376 FSG_STRING_INTERFACE
380 #ifndef FSG_NO_OTG
381 static struct usb_otg_descriptor
382 fsg_otg_desc = {
383 .bLength = sizeof fsg_otg_desc,
384 .bDescriptorType = USB_DT_OTG,
386 .bmAttributes = USB_OTG_SRP,
388 #endif
390 /* There is only one interface. */
392 static struct usb_interface_descriptor
393 fsg_intf_desc = {
394 .bLength = sizeof fsg_intf_desc,
395 .bDescriptorType = USB_DT_INTERFACE,
397 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
398 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
399 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
400 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
401 .iInterface = FSG_STRING_INTERFACE,
405 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
406 * interrupt-in.
409 static struct usb_endpoint_descriptor
410 fsg_fs_bulk_in_desc = {
411 .bLength = USB_DT_ENDPOINT_SIZE,
412 .bDescriptorType = USB_DT_ENDPOINT,
414 .bEndpointAddress = USB_DIR_IN,
415 .bmAttributes = USB_ENDPOINT_XFER_BULK,
416 /* wMaxPacketSize set by autoconfiguration */
419 static struct usb_endpoint_descriptor
420 fsg_fs_bulk_out_desc = {
421 .bLength = USB_DT_ENDPOINT_SIZE,
422 .bDescriptorType = USB_DT_ENDPOINT,
424 .bEndpointAddress = USB_DIR_OUT,
425 .bmAttributes = USB_ENDPOINT_XFER_BULK,
426 /* wMaxPacketSize set by autoconfiguration */
429 #ifndef FSG_NO_INTR_EP
431 static struct usb_endpoint_descriptor
432 fsg_fs_intr_in_desc = {
433 .bLength = USB_DT_ENDPOINT_SIZE,
434 .bDescriptorType = USB_DT_ENDPOINT,
436 .bEndpointAddress = USB_DIR_IN,
437 .bmAttributes = USB_ENDPOINT_XFER_INT,
438 .wMaxPacketSize = cpu_to_le16(2),
439 .bInterval = 32, /* frames -> 32 ms */
442 #ifndef FSG_NO_OTG
443 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
444 #else
445 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
446 #endif
448 #endif
450 static struct usb_descriptor_header *fsg_fs_function[] = {
451 #ifndef FSG_NO_OTG
452 (struct usb_descriptor_header *) &fsg_otg_desc,
453 #endif
454 (struct usb_descriptor_header *) &fsg_intf_desc,
455 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
456 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
457 #ifndef FSG_NO_INTR_EP
458 (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
459 #endif
460 NULL,
465 * USB 2.0 devices need to expose both high speed and full speed
466 * descriptors, unless they only run at full speed.
468 * That means alternate endpoint descriptors (bigger packets)
469 * and a "device qualifier" ... plus more construction options
470 * for the configuration descriptor.
472 static struct usb_endpoint_descriptor
473 fsg_hs_bulk_in_desc = {
474 .bLength = USB_DT_ENDPOINT_SIZE,
475 .bDescriptorType = USB_DT_ENDPOINT,
477 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
478 .bmAttributes = USB_ENDPOINT_XFER_BULK,
479 .wMaxPacketSize = cpu_to_le16(512),
482 static struct usb_endpoint_descriptor
483 fsg_hs_bulk_out_desc = {
484 .bLength = USB_DT_ENDPOINT_SIZE,
485 .bDescriptorType = USB_DT_ENDPOINT,
487 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
488 .bmAttributes = USB_ENDPOINT_XFER_BULK,
489 .wMaxPacketSize = cpu_to_le16(512),
490 .bInterval = 1, /* NAK every 1 uframe */
493 #ifndef FSG_NO_INTR_EP
495 static struct usb_endpoint_descriptor
496 fsg_hs_intr_in_desc = {
497 .bLength = USB_DT_ENDPOINT_SIZE,
498 .bDescriptorType = USB_DT_ENDPOINT,
500 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
501 .bmAttributes = USB_ENDPOINT_XFER_INT,
502 .wMaxPacketSize = cpu_to_le16(2),
503 .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
506 #ifndef FSG_NO_OTG
507 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
508 #else
509 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
510 #endif
512 #endif
514 static struct usb_descriptor_header *fsg_hs_function[] = {
515 #ifndef FSG_NO_OTG
516 (struct usb_descriptor_header *) &fsg_otg_desc,
517 #endif
518 (struct usb_descriptor_header *) &fsg_intf_desc,
519 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
520 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
521 #ifndef FSG_NO_INTR_EP
522 (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
523 #endif
524 NULL,
527 /* Maxpacket and other transfer characteristics vary by speed. */
528 static __maybe_unused struct usb_endpoint_descriptor *
529 fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
530 struct usb_endpoint_descriptor *hs)
532 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
533 return hs;
534 return fs;
538 /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
539 static struct usb_string fsg_strings[] = {
540 #ifndef FSG_NO_DEVICE_STRINGS
541 {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
542 {FSG_STRING_PRODUCT, fsg_string_product},
543 {FSG_STRING_SERIAL, ""},
544 {FSG_STRING_CONFIG, fsg_string_config},
545 #endif
546 {FSG_STRING_INTERFACE, fsg_string_interface},
550 static struct usb_gadget_strings fsg_stringtab = {
551 .language = 0x0409, /* en-us */
552 .strings = fsg_strings,
556 /*-------------------------------------------------------------------------*/
559 * If the next two routines are called while the gadget is registered,
560 * the caller must own fsg->filesem for writing.
563 static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
565 int ro;
566 struct file *filp = NULL;
567 int rc = -EINVAL;
568 struct inode *inode = NULL;
569 loff_t size;
570 loff_t num_sectors;
571 loff_t min_sectors;
573 /* R/W if we can, R/O if we must */
574 ro = curlun->initially_ro;
575 if (!ro) {
576 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
577 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
578 ro = 1;
580 if (ro)
581 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
582 if (IS_ERR(filp)) {
583 LINFO(curlun, "unable to open backing file: %s\n", filename);
584 return PTR_ERR(filp);
587 if (!(filp->f_mode & FMODE_WRITE))
588 ro = 1;
590 if (filp->f_path.dentry)
591 inode = filp->f_path.dentry->d_inode;
592 if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
593 LINFO(curlun, "invalid file type: %s\n", filename);
594 goto out;
598 * If we can't read the file, it's no good.
599 * If we can't write the file, use it read-only.
601 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
602 LINFO(curlun, "file not readable: %s\n", filename);
603 goto out;
605 if (!(filp->f_op->write || filp->f_op->aio_write))
606 ro = 1;
608 size = i_size_read(inode->i_mapping->host);
609 if (size < 0) {
610 LINFO(curlun, "unable to find file size: %s\n", filename);
611 rc = (int) size;
612 goto out;
615 if (curlun->cdrom) {
616 curlun->blksize = 2048;
617 curlun->blkbits = 11;
618 } else if (inode->i_bdev) {
619 curlun->blksize = bdev_logical_block_size(inode->i_bdev);
620 curlun->blkbits = blksize_bits(curlun->blksize);
621 } else {
622 curlun->blksize = 512;
623 curlun->blkbits = 9;
626 num_sectors = size >> curlun->blkbits; /* File size in logic-block-size blocks */
627 min_sectors = 1;
628 if (curlun->cdrom) {
629 min_sectors = 300; /* Smallest track is 300 frames */
630 if (num_sectors >= 256*60*75) {
631 num_sectors = 256*60*75 - 1;
632 LINFO(curlun, "file too big: %s\n", filename);
633 LINFO(curlun, "using only first %d blocks\n",
634 (int) num_sectors);
637 if (num_sectors < min_sectors) {
638 LINFO(curlun, "file too small: %s\n", filename);
639 rc = -ETOOSMALL;
640 goto out;
643 get_file(filp);
644 curlun->ro = ro;
645 curlun->filp = filp;
646 curlun->file_length = size;
647 curlun->num_sectors = num_sectors;
648 LDBG(curlun, "open backing file: %s\n", filename);
649 rc = 0;
651 out:
652 filp_close(filp, current->files);
653 return rc;
657 static void fsg_lun_close(struct fsg_lun *curlun)
659 if (curlun->filp) {
660 LDBG(curlun, "close backing file\n");
661 fput(curlun->filp);
662 curlun->filp = NULL;
667 /*-------------------------------------------------------------------------*/
670 * Sync the file data, don't bother with the metadata.
671 * This code was copied from fs/buffer.c:sys_fdatasync().
673 static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
675 struct file *filp = curlun->filp;
677 if (curlun->ro || !filp)
678 return 0;
679 return vfs_fsync(filp, 1);
682 static void store_cdrom_address(u8 *dest, int msf, u32 addr)
684 if (msf) {
685 /* Convert to Minutes-Seconds-Frames */
686 addr >>= 2; /* Convert to 2048-byte frames */
687 addr += 2*75; /* Lead-in occupies 2 seconds */
688 dest[3] = addr % 75; /* Frames */
689 addr /= 75;
690 dest[2] = addr % 60; /* Seconds */
691 addr /= 60;
692 dest[1] = addr; /* Minutes */
693 dest[0] = 0; /* Reserved */
694 } else {
695 /* Absolute sector */
696 put_unaligned_be32(addr, dest);
701 /*-------------------------------------------------------------------------*/
704 static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
705 char *buf)
707 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
709 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
710 ? curlun->ro
711 : curlun->initially_ro);
714 static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
715 char *buf)
717 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
719 return sprintf(buf, "%u\n", curlun->nofua);
722 static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
723 char *buf)
725 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
726 struct rw_semaphore *filesem = dev_get_drvdata(dev);
727 char *p;
728 ssize_t rc;
730 down_read(filesem);
731 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
732 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
733 if (IS_ERR(p))
734 rc = PTR_ERR(p);
735 else {
736 rc = strlen(p);
737 memmove(buf, p, rc);
738 buf[rc] = '\n'; /* Add a newline */
739 buf[++rc] = 0;
741 } else { /* No file, return 0 bytes */
742 *buf = 0;
743 rc = 0;
745 up_read(filesem);
746 return rc;
750 static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
751 const char *buf, size_t count)
753 ssize_t rc;
754 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
755 struct rw_semaphore *filesem = dev_get_drvdata(dev);
756 unsigned ro;
758 rc = kstrtouint(buf, 2, &ro);
759 if (rc)
760 return rc;
763 * Allow the write-enable status to change only while the
764 * backing file is closed.
766 down_read(filesem);
767 if (fsg_lun_is_open(curlun)) {
768 LDBG(curlun, "read-only status change prevented\n");
769 rc = -EBUSY;
770 } else {
771 curlun->ro = ro;
772 curlun->initially_ro = ro;
773 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
774 rc = count;
776 up_read(filesem);
777 return rc;
780 static ssize_t fsg_store_nofua(struct device *dev,
781 struct device_attribute *attr,
782 const char *buf, size_t count)
784 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
785 unsigned nofua;
786 int ret;
788 ret = kstrtouint(buf, 2, &nofua);
789 if (ret)
790 return ret;
792 /* Sync data when switching from async mode to sync */
793 if (!nofua && curlun->nofua)
794 fsg_lun_fsync_sub(curlun);
796 curlun->nofua = nofua;
798 return count;
801 static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
802 const char *buf, size_t count)
804 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
805 struct rw_semaphore *filesem = dev_get_drvdata(dev);
806 int rc = 0;
808 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
809 LDBG(curlun, "eject attempt prevented\n");
810 return -EBUSY; /* "Door is locked" */
813 /* Remove a trailing newline */
814 if (count > 0 && buf[count-1] == '\n')
815 ((char *) buf)[count-1] = 0; /* Ugh! */
817 /* Eject current medium */
818 down_write(filesem);
819 if (fsg_lun_is_open(curlun)) {
820 fsg_lun_close(curlun);
821 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
824 /* Load new medium */
825 if (count > 0 && buf[0]) {
826 rc = fsg_lun_open(curlun, buf);
827 if (rc == 0)
828 curlun->unit_attention_data =
829 SS_NOT_READY_TO_READY_TRANSITION;
831 up_write(filesem);
832 return (rc < 0 ? rc : count);