aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/file_storage.c
diff options
context:
space:
mode:
authorMichal Nazarewicz <m.nazarewicz@samsung.com>2009-10-28 11:57:14 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2009-12-11 14:55:18 -0500
commitb6058d0fefc0b5ff777dfbff990a0a50a4ac144b (patch)
tree8ed791c196d814c56a5a0bd16b07b60628aa0254 /drivers/usb/gadget/file_storage.c
parent7e8d5cd93fac4d3720d8f780b350c9421e8997d4 (diff)
USB: g_file_storage: parts of file_storage.c moved to separate file
Moved parts of the file_storage.c file which do not reference fsg_dev structure to newly created storage_common.c file. dump_msg() and dump_cdb() have been changed to macros to remove fsg_dev reference. Signed-off-by: Michal Nazarewicz <m.nazarewicz@samsung.com> Cc: David Brownell <dbrownell@users.sourceforge.net> Cc: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/gadget/file_storage.c')
-rw-r--r--drivers/usb/gadget/file_storage.c572
1 files changed, 5 insertions, 567 deletions
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index 1e6aa504d58a..332599c11987 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -298,46 +298,6 @@ MODULE_LICENSE("Dual BSD/GPL");
298 298
299/*-------------------------------------------------------------------------*/ 299/*-------------------------------------------------------------------------*/
300 300
301#define LDBG(lun,fmt,args...) \
302 dev_dbg(&(lun)->dev , fmt , ## args)
303#define MDBG(fmt,args...) \
304 pr_debug(DRIVER_NAME ": " fmt , ## args)
305
306#ifndef DEBUG
307#undef VERBOSE_DEBUG
308#undef DUMP_MSGS
309#endif /* !DEBUG */
310
311#ifdef VERBOSE_DEBUG
312#define VLDBG LDBG
313#else
314#define VLDBG(lun,fmt,args...) \
315 do { } while (0)
316#endif /* VERBOSE_DEBUG */
317
318#define LERROR(lun,fmt,args...) \
319 dev_err(&(lun)->dev , fmt , ## args)
320#define LWARN(lun,fmt,args...) \
321 dev_warn(&(lun)->dev , fmt , ## args)
322#define LINFO(lun,fmt,args...) \
323 dev_info(&(lun)->dev , fmt , ## args)
324
325#define MINFO(fmt,args...) \
326 pr_info(DRIVER_NAME ": " fmt , ## args)
327
328#define DBG(d, fmt, args...) \
329 dev_dbg(&(d)->gadget->dev , fmt , ## args)
330#define VDBG(d, fmt, args...) \
331 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
332#define ERROR(d, fmt, args...) \
333 dev_err(&(d)->gadget->dev , fmt , ## args)
334#define WARNING(d, fmt, args...) \
335 dev_warn(&(d)->gadget->dev , fmt , ## args)
336#define INFO(d, fmt, args...) \
337 dev_info(&(d)->gadget->dev , fmt , ## args)
338
339
340/*-------------------------------------------------------------------------*/
341 301
342/* Encapsulate the module parameter settings */ 302/* Encapsulate the module parameter settings */
343 303
@@ -425,125 +385,6 @@ MODULE_PARM_DESC(buflen, "I/O buffer size");
425#endif /* CONFIG_USB_FILE_STORAGE_TEST */ 385#endif /* CONFIG_USB_FILE_STORAGE_TEST */
426 386
427 387
428/*-------------------------------------------------------------------------*/
429
430/* SCSI device types */
431#define TYPE_DISK 0x00
432#define TYPE_CDROM 0x05
433
434/* USB protocol value = the transport method */
435#define USB_PR_CBI 0x00 // Control/Bulk/Interrupt
436#define USB_PR_CB 0x01 // Control/Bulk w/o interrupt
437#define USB_PR_BULK 0x50 // Bulk-only
438
439/* USB subclass value = the protocol encapsulation */
440#define USB_SC_RBC 0x01 // Reduced Block Commands (flash)
441#define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM)
442#define USB_SC_QIC 0x03 // QIC-157 (tape)
443#define USB_SC_UFI 0x04 // UFI (floppy)
444#define USB_SC_8070 0x05 // SFF-8070i (removable)
445#define USB_SC_SCSI 0x06 // Transparent SCSI
446
447/* Bulk-only data structures */
448
449/* Command Block Wrapper */
450struct bulk_cb_wrap {
451 __le32 Signature; // Contains 'USBC'
452 u32 Tag; // Unique per command id
453 __le32 DataTransferLength; // Size of the data
454 u8 Flags; // Direction in bit 7
455 u8 Lun; // LUN (normally 0)
456 u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE
457 u8 CDB[16]; // Command Data Block
458};
459
460#define USB_BULK_CB_WRAP_LEN 31
461#define USB_BULK_CB_SIG 0x43425355 // Spells out USBC
462#define USB_BULK_IN_FLAG 0x80
463
464/* Command Status Wrapper */
465struct bulk_cs_wrap {
466 __le32 Signature; // Should = 'USBS'
467 u32 Tag; // Same as original command
468 __le32 Residue; // Amount not transferred
469 u8 Status; // See below
470};
471
472#define USB_BULK_CS_WRAP_LEN 13
473#define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS'
474#define USB_STATUS_PASS 0
475#define USB_STATUS_FAIL 1
476#define USB_STATUS_PHASE_ERROR 2
477
478/* Bulk-only class specific requests */
479#define USB_BULK_RESET_REQUEST 0xff
480#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
481
482
483/* CBI Interrupt data structure */
484struct interrupt_data {
485 u8 bType;
486 u8 bValue;
487};
488
489#define CBI_INTERRUPT_DATA_LEN 2
490
491/* CBI Accept Device-Specific Command request */
492#define USB_CBI_ADSC_REQUEST 0x00
493
494
495#define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
496
497/* SCSI commands that we recognize */
498#define SC_FORMAT_UNIT 0x04
499#define SC_INQUIRY 0x12
500#define SC_MODE_SELECT_6 0x15
501#define SC_MODE_SELECT_10 0x55
502#define SC_MODE_SENSE_6 0x1a
503#define SC_MODE_SENSE_10 0x5a
504#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
505#define SC_READ_6 0x08
506#define SC_READ_10 0x28
507#define SC_READ_12 0xa8
508#define SC_READ_CAPACITY 0x25
509#define SC_READ_FORMAT_CAPACITIES 0x23
510#define SC_READ_HEADER 0x44
511#define SC_READ_TOC 0x43
512#define SC_RELEASE 0x17
513#define SC_REQUEST_SENSE 0x03
514#define SC_RESERVE 0x16
515#define SC_SEND_DIAGNOSTIC 0x1d
516#define SC_START_STOP_UNIT 0x1b
517#define SC_SYNCHRONIZE_CACHE 0x35
518#define SC_TEST_UNIT_READY 0x00
519#define SC_VERIFY 0x2f
520#define SC_WRITE_6 0x0a
521#define SC_WRITE_10 0x2a
522#define SC_WRITE_12 0xaa
523
524/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
525#define SS_NO_SENSE 0
526#define SS_COMMUNICATION_FAILURE 0x040800
527#define SS_INVALID_COMMAND 0x052000
528#define SS_INVALID_FIELD_IN_CDB 0x052400
529#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
530#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
531#define SS_MEDIUM_NOT_PRESENT 0x023a00
532#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
533#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
534#define SS_RESET_OCCURRED 0x062900
535#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
536#define SS_UNRECOVERED_READ_ERROR 0x031100
537#define SS_WRITE_ERROR 0x030c02
538#define SS_WRITE_PROTECTED 0x072700
539
540#define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
541#define ASC(x) ((u8) ((x) >> 8))
542#define ASCQ(x) ((u8) (x))
543
544
545/*-------------------------------------------------------------------------*/
546
547/* 388/*
548 * These definitions will permit the compiler to avoid generating code for 389 * These definitions will permit the compiler to avoid generating code for
549 * parts of the driver that aren't used in the non-TEST version. Even gcc 390 * parts of the driver that aren't used in the non-TEST version. Even gcc
@@ -566,81 +407,12 @@ struct interrupt_data {
566#endif /* CONFIG_USB_FILE_STORAGE_TEST */ 407#endif /* CONFIG_USB_FILE_STORAGE_TEST */
567 408
568 409
569struct lun { 410/*-------------------------------------------------------------------------*/
570 struct file *filp;
571 loff_t file_length;
572 loff_t num_sectors;
573
574 unsigned int ro : 1;
575 unsigned int prevent_medium_removal : 1;
576 unsigned int registered : 1;
577 unsigned int info_valid : 1;
578
579 u32 sense_data;
580 u32 sense_data_info;
581 u32 unit_attention_data;
582
583 struct device dev;
584};
585
586#define backing_file_is_open(curlun) ((curlun)->filp != NULL)
587
588static struct lun *dev_to_lun(struct device *dev)
589{
590 return container_of(dev, struct lun, dev);
591}
592
593
594/* Big enough to hold our biggest descriptor */
595#define EP0_BUFSIZE 256
596#define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
597
598/* Number of buffers we will use. 2 is enough for double-buffering */
599#define NUM_BUFFERS 2
600
601enum fsg_buffer_state {
602 BUF_STATE_EMPTY = 0,
603 BUF_STATE_FULL,
604 BUF_STATE_BUSY
605};
606
607struct fsg_buffhd {
608 void *buf;
609 enum fsg_buffer_state state;
610 struct fsg_buffhd *next;
611
612 /* The NetChip 2280 is faster, and handles some protocol faults
613 * better, if we don't submit any short bulk-out read requests.
614 * So we will record the intended request length here. */
615 unsigned int bulk_out_intended_length;
616 411
617 struct usb_request *inreq; 412#include "storage_common.c"
618 int inreq_busy;
619 struct usb_request *outreq;
620 int outreq_busy;
621};
622 413
623enum fsg_state { 414/*-------------------------------------------------------------------------*/
624 FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere
625 FSG_STATE_DATA_PHASE,
626 FSG_STATE_STATUS_PHASE,
627
628 FSG_STATE_IDLE = 0,
629 FSG_STATE_ABORT_BULK_OUT,
630 FSG_STATE_RESET,
631 FSG_STATE_INTERFACE_CHANGE,
632 FSG_STATE_CONFIG_CHANGE,
633 FSG_STATE_DISCONNECT,
634 FSG_STATE_EXIT,
635 FSG_STATE_TERMINATED
636};
637 415
638enum data_direction {
639 DATA_DIR_UNKNOWN = 0,
640 DATA_DIR_FROM_HOST,
641 DATA_DIR_TO_HOST,
642 DATA_DIR_NONE
643};
644 416
645struct fsg_dev { 417struct fsg_dev {
646 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */ 418 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
@@ -739,49 +511,9 @@ static void set_bulk_out_req_length(struct fsg_dev *fsg,
739static struct fsg_dev *the_fsg; 511static struct fsg_dev *the_fsg;
740static struct usb_gadget_driver fsg_driver; 512static struct usb_gadget_driver fsg_driver;
741 513
742static void close_backing_file(struct lun *curlun);
743
744 514
745/*-------------------------------------------------------------------------*/ 515/*-------------------------------------------------------------------------*/
746 516
747#ifdef DUMP_MSGS
748
749static void dump_msg(struct fsg_dev *fsg, const char *label,
750 const u8 *buf, unsigned int length)
751{
752 if (length < 512) {
753 DBG(fsg, "%s, length %u:\n", label, length);
754 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
755 16, 1, buf, length, 0);
756 }
757}
758
759static void dump_cdb(struct fsg_dev *fsg)
760{}
761
762#else
763
764static void dump_msg(struct fsg_dev *fsg, const char *label,
765 const u8 *buf, unsigned int length)
766{}
767
768#ifdef VERBOSE_DEBUG
769
770static void dump_cdb(struct fsg_dev *fsg)
771{
772 print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE,
773 16, 1, fsg->cmnd, fsg->cmnd_size, 0);
774}
775
776#else
777
778static void dump_cdb(struct fsg_dev *fsg)
779{}
780
781#endif /* VERBOSE_DEBUG */
782#endif /* DUMP_MSGS */
783
784
785static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep) 517static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
786{ 518{
787 const char *name; 519 const char *name;
@@ -799,26 +531,11 @@ static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
799 531
800/*-------------------------------------------------------------------------*/ 532/*-------------------------------------------------------------------------*/
801 533
802/* Routines for unaligned data access */
803
804static u32 get_unaligned_be24(u8 *buf)
805{
806 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
807}
808
809
810/*-------------------------------------------------------------------------*/
811
812/* 534/*
813 * DESCRIPTORS ... most are static, but strings and (full) configuration 535 * DESCRIPTORS ... most are static, but strings and (full) configuration
814 * descriptors are built on demand. Also the (static) config and interface 536 * descriptors are built on demand. Also the (static) config and interface
815 * descriptors are adjusted during fsg_bind(). 537 * descriptors are adjusted during fsg_bind().
816 */ 538 */
817#define STRING_MANUFACTURER 1
818#define STRING_PRODUCT 2
819#define STRING_SERIAL 3
820#define STRING_CONFIG 4
821#define STRING_INTERFACE 5
822 539
823/* There is only one configuration. */ 540/* There is only one configuration. */
824#define CONFIG_VALUE 1 541#define CONFIG_VALUE 1
@@ -855,81 +572,7 @@ config_desc = {
855 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2, 572 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
856}; 573};
857 574
858static struct usb_otg_descriptor
859otg_desc = {
860 .bLength = sizeof(otg_desc),
861 .bDescriptorType = USB_DT_OTG,
862
863 .bmAttributes = USB_OTG_SRP,
864};
865
866/* There is only one interface. */
867
868static struct usb_interface_descriptor
869intf_desc = {
870 .bLength = sizeof intf_desc,
871 .bDescriptorType = USB_DT_INTERFACE,
872
873 .bNumEndpoints = 2, // Adjusted during fsg_bind()
874 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
875 .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind()
876 .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind()
877 .iInterface = STRING_INTERFACE,
878};
879
880/* Three full-speed endpoint descriptors: bulk-in, bulk-out,
881 * and interrupt-in. */
882
883static struct usb_endpoint_descriptor
884fs_bulk_in_desc = {
885 .bLength = USB_DT_ENDPOINT_SIZE,
886 .bDescriptorType = USB_DT_ENDPOINT,
887
888 .bEndpointAddress = USB_DIR_IN,
889 .bmAttributes = USB_ENDPOINT_XFER_BULK,
890 /* wMaxPacketSize set by autoconfiguration */
891};
892 575
893static struct usb_endpoint_descriptor
894fs_bulk_out_desc = {
895 .bLength = USB_DT_ENDPOINT_SIZE,
896 .bDescriptorType = USB_DT_ENDPOINT,
897
898 .bEndpointAddress = USB_DIR_OUT,
899 .bmAttributes = USB_ENDPOINT_XFER_BULK,
900 /* wMaxPacketSize set by autoconfiguration */
901};
902
903static struct usb_endpoint_descriptor
904fs_intr_in_desc = {
905 .bLength = USB_DT_ENDPOINT_SIZE,
906 .bDescriptorType = USB_DT_ENDPOINT,
907
908 .bEndpointAddress = USB_DIR_IN,
909 .bmAttributes = USB_ENDPOINT_XFER_INT,
910 .wMaxPacketSize = cpu_to_le16(2),
911 .bInterval = 32, // frames -> 32 ms
912};
913
914static const struct usb_descriptor_header *fs_function[] = {
915 (struct usb_descriptor_header *) &otg_desc,
916 (struct usb_descriptor_header *) &intf_desc,
917 (struct usb_descriptor_header *) &fs_bulk_in_desc,
918 (struct usb_descriptor_header *) &fs_bulk_out_desc,
919 (struct usb_descriptor_header *) &fs_intr_in_desc,
920 NULL,
921};
922#define FS_FUNCTION_PRE_EP_ENTRIES 2
923
924
925/*
926 * USB 2.0 devices need to expose both high speed and full speed
927 * descriptors, unless they only run at full speed.
928 *
929 * That means alternate endpoint descriptors (bigger packets)
930 * and a "device qualifier" ... plus more construction options
931 * for the config descriptor.
932 */
933static struct usb_qualifier_descriptor 576static struct usb_qualifier_descriptor
934dev_qualifier = { 577dev_qualifier = {
935 .bLength = sizeof dev_qualifier, 578 .bLength = sizeof dev_qualifier,
@@ -941,78 +584,6 @@ dev_qualifier = {
941 .bNumConfigurations = 1, 584 .bNumConfigurations = 1,
942}; 585};
943 586
944static struct usb_endpoint_descriptor
945hs_bulk_in_desc = {
946 .bLength = USB_DT_ENDPOINT_SIZE,
947 .bDescriptorType = USB_DT_ENDPOINT,
948
949 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
950 .bmAttributes = USB_ENDPOINT_XFER_BULK,
951 .wMaxPacketSize = cpu_to_le16(512),
952};
953
954static struct usb_endpoint_descriptor
955hs_bulk_out_desc = {
956 .bLength = USB_DT_ENDPOINT_SIZE,
957 .bDescriptorType = USB_DT_ENDPOINT,
958
959 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
960 .bmAttributes = USB_ENDPOINT_XFER_BULK,
961 .wMaxPacketSize = cpu_to_le16(512),
962 .bInterval = 1, // NAK every 1 uframe
963};
964
965static struct usb_endpoint_descriptor
966hs_intr_in_desc = {
967 .bLength = USB_DT_ENDPOINT_SIZE,
968 .bDescriptorType = USB_DT_ENDPOINT,
969
970 /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
971 .bmAttributes = USB_ENDPOINT_XFER_INT,
972 .wMaxPacketSize = cpu_to_le16(2),
973 .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms
974};
975
976static const struct usb_descriptor_header *hs_function[] = {
977 (struct usb_descriptor_header *) &otg_desc,
978 (struct usb_descriptor_header *) &intf_desc,
979 (struct usb_descriptor_header *) &hs_bulk_in_desc,
980 (struct usb_descriptor_header *) &hs_bulk_out_desc,
981 (struct usb_descriptor_header *) &hs_intr_in_desc,
982 NULL,
983};
984#define HS_FUNCTION_PRE_EP_ENTRIES 2
985
986/* Maxpacket and other transfer characteristics vary by speed. */
987static struct usb_endpoint_descriptor *
988ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
989 struct usb_endpoint_descriptor *hs)
990{
991 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
992 return hs;
993 return fs;
994}
995
996
997/* The CBI specification limits the serial string to 12 uppercase hexadecimal
998 * characters. */
999static char manufacturer[64];
1000static char serial[13];
1001
1002/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
1003static struct usb_string strings[] = {
1004 {STRING_MANUFACTURER, manufacturer},
1005 {STRING_PRODUCT, longname},
1006 {STRING_SERIAL, serial},
1007 {STRING_CONFIG, "Self-powered"},
1008 {STRING_INTERFACE, "Mass Storage"},
1009 {}
1010};
1011
1012static struct usb_gadget_strings stringtab = {
1013 .language = 0x0409, // en-us
1014 .strings = strings,
1015};
1016 587
1017 588
1018/* 589/*
@@ -1864,25 +1435,6 @@ static int do_write(struct fsg_dev *fsg)
1864 1435
1865/*-------------------------------------------------------------------------*/ 1436/*-------------------------------------------------------------------------*/
1866 1437
1867/* Sync the file data, don't bother with the metadata.
1868 * This code was copied from fs/buffer.c:sys_fdatasync(). */
1869static int fsync_sub(struct lun *curlun)
1870{
1871 struct file *filp = curlun->filp;
1872
1873 if (curlun->ro || !filp)
1874 return 0;
1875 return vfs_fsync(filp, filp->f_path.dentry, 1);
1876}
1877
1878static void fsync_all(struct fsg_dev *fsg)
1879{
1880 int i;
1881
1882 for (i = 0; i < fsg->nluns; ++i)
1883 fsync_sub(&fsg->luns[i]);
1884}
1885
1886static int do_synchronize_cache(struct fsg_dev *fsg) 1438static int do_synchronize_cache(struct fsg_dev *fsg)
1887{ 1439{
1888 struct lun *curlun = fsg->curlun; 1440 struct lun *curlun = fsg->curlun;
@@ -2113,24 +1665,6 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2113} 1665}
2114 1666
2115 1667
2116static void store_cdrom_address(u8 *dest, int msf, u32 addr)
2117{
2118 if (msf) {
2119 /* Convert to Minutes-Seconds-Frames */
2120 addr >>= 2; /* Convert to 2048-byte frames */
2121 addr += 2*75; /* Lead-in occupies 2 seconds */
2122 dest[3] = addr % 75; /* Frames */
2123 addr /= 75;
2124 dest[2] = addr % 60; /* Seconds */
2125 addr /= 60;
2126 dest[1] = addr; /* Minutes */
2127 dest[0] = 0; /* Reserved */
2128 } else {
2129 /* Absolute sector */
2130 put_unaligned_be32(addr, dest);
2131 }
2132}
2133
2134static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1668static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2135{ 1669{
2136 struct lun *curlun = fsg->curlun; 1670 struct lun *curlun = fsg->curlun;
@@ -3506,7 +3040,8 @@ static void handle_exception(struct fsg_dev *fsg)
3506 break; 3040 break;
3507 3041
3508 case FSG_STATE_DISCONNECT: 3042 case FSG_STATE_DISCONNECT:
3509 fsync_all(fsg); 3043 for (i = 0; i < fsg->nluns; ++i)
3044 fsync_sub(fsg->luns + i);
3510 do_set_config(fsg, 0); // Unconfigured state 3045 do_set_config(fsg, 0); // Unconfigured state
3511 break; 3046 break;
3512 3047
@@ -3595,103 +3130,6 @@ static int fsg_main_thread(void *fsg_)
3595 3130
3596/*-------------------------------------------------------------------------*/ 3131/*-------------------------------------------------------------------------*/
3597 3132
3598/* If the next two routines are called while the gadget is registered,
3599 * the caller must own fsg->filesem for writing. */
3600
3601static int open_backing_file(struct lun *curlun, const char *filename)
3602{
3603 int ro;
3604 struct file *filp = NULL;
3605 int rc = -EINVAL;
3606 struct inode *inode = NULL;
3607 loff_t size;
3608 loff_t num_sectors;
3609 loff_t min_sectors;
3610
3611 /* R/W if we can, R/O if we must */
3612 ro = curlun->ro;
3613 if (!ro) {
3614 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
3615 if (-EROFS == PTR_ERR(filp))
3616 ro = 1;
3617 }
3618 if (ro)
3619 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
3620 if (IS_ERR(filp)) {
3621 LINFO(curlun, "unable to open backing file: %s\n", filename);
3622 return PTR_ERR(filp);
3623 }
3624
3625 if (!(filp->f_mode & FMODE_WRITE))
3626 ro = 1;
3627
3628 if (filp->f_path.dentry)
3629 inode = filp->f_path.dentry->d_inode;
3630 if (inode && S_ISBLK(inode->i_mode)) {
3631 if (bdev_read_only(inode->i_bdev))
3632 ro = 1;
3633 } else if (!inode || !S_ISREG(inode->i_mode)) {
3634 LINFO(curlun, "invalid file type: %s\n", filename);
3635 goto out;
3636 }
3637
3638 /* If we can't read the file, it's no good.
3639 * If we can't write the file, use it read-only. */
3640 if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
3641 LINFO(curlun, "file not readable: %s\n", filename);
3642 goto out;
3643 }
3644 if (!(filp->f_op->write || filp->f_op->aio_write))
3645 ro = 1;
3646
3647 size = i_size_read(inode->i_mapping->host);
3648 if (size < 0) {
3649 LINFO(curlun, "unable to find file size: %s\n", filename);
3650 rc = (int) size;
3651 goto out;
3652 }
3653 num_sectors = size >> 9; // File size in 512-byte blocks
3654 min_sectors = 1;
3655 if (mod_data.cdrom) {
3656 num_sectors &= ~3; // Reduce to a multiple of 2048
3657 min_sectors = 300*4; // Smallest track is 300 frames
3658 if (num_sectors >= 256*60*75*4) {
3659 num_sectors = (256*60*75 - 1) * 4;
3660 LINFO(curlun, "file too big: %s\n", filename);
3661 LINFO(curlun, "using only first %d blocks\n",
3662 (int) num_sectors);
3663 }
3664 }
3665 if (num_sectors < min_sectors) {
3666 LINFO(curlun, "file too small: %s\n", filename);
3667 rc = -ETOOSMALL;
3668 goto out;
3669 }
3670
3671 get_file(filp);
3672 curlun->ro = ro;
3673 curlun->filp = filp;
3674 curlun->file_length = size;
3675 curlun->num_sectors = num_sectors;
3676 LDBG(curlun, "open backing file: %s\n", filename);
3677 rc = 0;
3678
3679out:
3680 filp_close(filp, current->files);
3681 return rc;
3682}
3683
3684
3685static void close_backing_file(struct lun *curlun)
3686{
3687 if (curlun->filp) {
3688 LDBG(curlun, "close backing file\n");
3689 fput(curlun->filp);
3690 curlun->filp = NULL;
3691 }
3692}
3693
3694
3695static ssize_t show_ro(struct device *dev, struct device_attribute *attr, char *buf) 3133static ssize_t show_ro(struct device *dev, struct device_attribute *attr, char *buf)
3696{ 3134{
3697 struct lun *curlun = dev_to_lun(dev); 3135 struct lun *curlun = dev_to_lun(dev);