48 #include <scsi/scsi.h>
49 #include <asm/unaligned.h>
58 #define FSG_VENDOR_ID 0x0525
59 #define FSG_PRODUCT_ID 0xa4a5
73 #define VLDBG(lun, fmt, args...) do { } while (0)
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)
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, label, \
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, label, \
126 buf, 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)
150 #define CBI_INTERRUPT_DATA_LEN 2
153 #define USB_CBI_ADSC_REQUEST 0x00
157 #define MAX_COMMAND_SIZE 16
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))
176 #define ASC(x) ((u8) ((x) >> 8))
177 #define ASCQ(x) ((u8) (x))
191 unsigned int cdrom:1;
195 unsigned int nofua:1;
206 #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
215 #define EP0_BUFSIZE 256
216 #define DELAYED_STATUS (EP0_BUFSIZE + 999)
218 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
220 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
230 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
235 static inline int fsg_num_buffers_validate(
void)
239 pr_err(
"fsg_num_buffers %u is out of range (%d to %d)\n",
245 #define FSG_BUFLEN ((u32)16384)
248 #define FSG_MAX_LUNS 8
301 static inline u32 get_unaligned_be24(
u8 *
buf)
311 #ifndef FSG_NO_DEVICE_STRINGS
324 .bLength =
sizeof fsg_otg_desc,
335 .bLength =
sizeof fsg_intf_desc,
351 fsg_fs_bulk_in_desc = {
361 fsg_fs_bulk_out_desc = {
370 #ifndef FSG_NO_INTR_EP
373 fsg_fs_intr_in_desc = {
384 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
386 # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
398 #ifndef FSG_NO_INTR_EP
414 fsg_hs_bulk_in_desc = {
424 fsg_hs_bulk_out_desc = {
434 #ifndef FSG_NO_INTR_EP
437 fsg_hs_intr_in_desc = {
448 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
450 # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
462 #ifndef FSG_NO_INTR_EP
469 fsg_ss_bulk_in_desc = {
479 .
bLength =
sizeof(fsg_ss_bulk_in_comp_desc),
486 fsg_ss_bulk_out_desc = {
496 .
bLength =
sizeof(fsg_ss_bulk_in_comp_desc),
502 #ifndef FSG_NO_INTR_EP
505 fsg_ss_intr_in_desc = {
516 .
bLength =
sizeof(fsg_ss_bulk_in_comp_desc),
523 # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 2
525 # define FSG_SS_FUNCTION_PRE_EP_ENTRIES 1
574 #ifndef FSG_NO_INTR_EP
597 #ifndef FSG_NO_DEVICE_STRINGS
609 .strings = fsg_strings,
620 static void fsg_lun_close(
struct fsg_lun *curlun)
623 LDBG(curlun,
"close backing file\n");
639 unsigned int blkbits;
640 unsigned int blksize;
646 if (PTR_ERR(filp) == -
EROFS || PTR_ERR(filp) == -
EACCES)
652 LINFO(curlun,
"unable to open backing file: %s\n", filename);
653 return PTR_ERR(filp);
659 inode = filp->
f_path.dentry->d_inode;
661 LINFO(curlun,
"invalid file type: %s\n", filename);
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;
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);
712 fsg_lun_close(curlun);
720 LDBG(curlun,
"open backing file: %s\n", filename);
735 static int fsg_lun_fsync_sub(
struct fsg_lun *curlun)
739 if (curlun->
ro || !filp)
769 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
779 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
787 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
813 const char *buf,
size_t count)
816 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
830 LDBG(curlun,
"read-only status change prevented\n");
835 LDBG(curlun,
"read-only status set to %d\n", curlun->
ro);
844 const char *buf,
size_t count)
846 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
855 if (!nofua && curlun->
nofua)
856 fsg_lun_fsync_sub(curlun);
864 const char *buf,
size_t count)
866 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
871 LDBG(curlun,
"eject attempt prevented\n");
876 if (count > 0 && buf[count-1] ==
'\n')
877 ((
char *) buf)[count-1] = 0;
881 if (count > 0 && buf[0]) {
883 rc = fsg_lun_open(curlun, buf);
888 fsg_lun_close(curlun);
892 return (rc < 0 ? rc : count);