1dee1d999SLukasz Majewski /* 2dee1d999SLukasz Majewski * storage_common.c -- Common definitions for mass storage functionality 3dee1d999SLukasz Majewski * 4dee1d999SLukasz Majewski * Copyright (C) 2003-2008 Alan Stern 5dee1d999SLukasz Majewski * Copyeight (C) 2009 Samsung Electronics 6dee1d999SLukasz Majewski * Author: Michal Nazarewicz (m.nazarewicz@samsung.com) 7dee1d999SLukasz Majewski * 8dee1d999SLukasz Majewski * Ported to u-boot: 9dee1d999SLukasz Majewski * Andrzej Pietrasiewicz <andrzej.p@samsung.com> 10dee1d999SLukasz Majewski * 11dee1d999SLukasz Majewski * Code refactoring & cleanup: 12dee1d999SLukasz Majewski * Łukasz Majewski <l.majewski@samsung.com> 13dee1d999SLukasz Majewski * 14*1a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 15dee1d999SLukasz Majewski */ 16dee1d999SLukasz Majewski 17dee1d999SLukasz Majewski 18dee1d999SLukasz Majewski /* 19dee1d999SLukasz Majewski * This file requires the following identifiers used in USB strings to 20dee1d999SLukasz Majewski * be defined (each of type pointer to char): 21dee1d999SLukasz Majewski * - fsg_string_manufacturer -- name of the manufacturer 22dee1d999SLukasz Majewski * - fsg_string_product -- name of the product 23dee1d999SLukasz Majewski * - fsg_string_serial -- product's serial 24dee1d999SLukasz Majewski * - fsg_string_config -- name of the configuration 25dee1d999SLukasz Majewski * - fsg_string_interface -- name of the interface 26dee1d999SLukasz Majewski * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS 27dee1d999SLukasz Majewski * macro is defined prior to including this file. 28dee1d999SLukasz Majewski */ 29dee1d999SLukasz Majewski 30dee1d999SLukasz Majewski /* 31dee1d999SLukasz Majewski * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and 32dee1d999SLukasz Majewski * fsg_hs_intr_in_desc objects as well as 33dee1d999SLukasz Majewski * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES 34dee1d999SLukasz Majewski * macros are not defined. 35dee1d999SLukasz Majewski * 36dee1d999SLukasz Majewski * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER, 37dee1d999SLukasz Majewski * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not 38dee1d999SLukasz Majewski * defined (as well as corresponding entries in string tables are 39dee1d999SLukasz Majewski * missing) and FSG_STRING_INTERFACE has value of zero. 40dee1d999SLukasz Majewski * 41dee1d999SLukasz Majewski * When FSG_NO_OTG is defined fsg_otg_desc won't be defined. 42dee1d999SLukasz Majewski */ 43dee1d999SLukasz Majewski 44dee1d999SLukasz Majewski /* 45dee1d999SLukasz Majewski * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included 46dee1d999SLukasz Majewski * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN 47dee1d999SLukasz Majewski * characters rather then a pointer to void. 48dee1d999SLukasz Majewski */ 49dee1d999SLukasz Majewski 50dee1d999SLukasz Majewski 51dee1d999SLukasz Majewski /* #include <asm/unaligned.h> */ 52dee1d999SLukasz Majewski 53dee1d999SLukasz Majewski 54dee1d999SLukasz Majewski /* 55dee1d999SLukasz Majewski * Thanks to NetChip Technologies for donating this product ID. 56dee1d999SLukasz Majewski * 57dee1d999SLukasz Majewski * DO NOT REUSE THESE IDs with any other driver!! Ever!! 58dee1d999SLukasz Majewski * Instead: allocate your own, using normal USB-IF procedures. 59dee1d999SLukasz Majewski */ 60dee1d999SLukasz Majewski #define FSG_VENDOR_ID 0x0525 /* NetChip */ 61dee1d999SLukasz Majewski #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ 62dee1d999SLukasz Majewski 63dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/ 64dee1d999SLukasz Majewski 65dee1d999SLukasz Majewski #ifndef DEBUG 66dee1d999SLukasz Majewski #undef VERBOSE_DEBUG 67dee1d999SLukasz Majewski #undef DUMP_MSGS 68dee1d999SLukasz Majewski #endif /* !DEBUG */ 69dee1d999SLukasz Majewski 70dee1d999SLukasz Majewski #ifdef VERBOSE_DEBUG 71dee1d999SLukasz Majewski #define VLDBG LDBG 72dee1d999SLukasz Majewski #else 73dee1d999SLukasz Majewski #define VLDBG(lun, fmt, args...) do { } while (0) 74dee1d999SLukasz Majewski #endif /* VERBOSE_DEBUG */ 75dee1d999SLukasz Majewski 76dee1d999SLukasz Majewski /* 77dee1d999SLukasz Majewski #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args) 78dee1d999SLukasz Majewski #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args) 79dee1d999SLukasz Majewski #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args) 80dee1d999SLukasz Majewski #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args) 81dee1d999SLukasz Majewski */ 82dee1d999SLukasz Majewski 83dee1d999SLukasz Majewski #define LDBG(lun, fmt, args...) do { } while (0) 84dee1d999SLukasz Majewski #define LERROR(lun, fmt, args...) do { } while (0) 85dee1d999SLukasz Majewski #define LWARN(lun, fmt, args...) do { } while (0) 86dee1d999SLukasz Majewski #define LINFO(lun, fmt, args...) do { } while (0) 87dee1d999SLukasz Majewski 88dee1d999SLukasz Majewski /* 89dee1d999SLukasz Majewski * Keep those macros in sync with those in 90dee1d999SLukasz Majewski * include/linux/usb/composite.h or else GCC will complain. If they 91dee1d999SLukasz Majewski * are identical (the same names of arguments, white spaces in the 92dee1d999SLukasz Majewski * same places) GCC will allow redefinition otherwise (even if some 93dee1d999SLukasz Majewski * white space is removed or added) warning will be issued. 94dee1d999SLukasz Majewski * 95dee1d999SLukasz Majewski * Those macros are needed here because File Storage Gadget does not 96dee1d999SLukasz Majewski * include the composite.h header. For composite gadgets those macros 97dee1d999SLukasz Majewski * are redundant since composite.h is included any way. 98dee1d999SLukasz Majewski * 99dee1d999SLukasz Majewski * One could check whether those macros are already defined (which 100dee1d999SLukasz Majewski * would indicate composite.h had been included) or not (which would 101dee1d999SLukasz Majewski * indicate we were in FSG) but this is not done because a warning is 102dee1d999SLukasz Majewski * desired if definitions here differ from the ones in composite.h. 103dee1d999SLukasz Majewski * 104dee1d999SLukasz Majewski * We want the definitions to match and be the same in File Storage 105dee1d999SLukasz Majewski * Gadget as well as Mass Storage Function (and so composite gadgets 106dee1d999SLukasz Majewski * using MSF). If someone changes them in composite.h it will produce 107dee1d999SLukasz Majewski * a warning in this file when building MSF. 108dee1d999SLukasz Majewski */ 109dee1d999SLukasz Majewski 110dee1d999SLukasz Majewski #define DBG(d, fmt, args...) debug(fmt , ## args) 111dee1d999SLukasz Majewski #define VDBG(d, fmt, args...) debug(fmt , ## args) 112dee1d999SLukasz Majewski /* #define ERROR(d, fmt, args...) printf(fmt , ## args) */ 113dee1d999SLukasz Majewski /* #define WARNING(d, fmt, args...) printf(fmt , ## args) */ 114dee1d999SLukasz Majewski /* #define INFO(d, fmt, args...) printf(fmt , ## args) */ 115dee1d999SLukasz Majewski 116dee1d999SLukasz Majewski /* #define DBG(d, fmt, args...) do { } while (0) */ 117dee1d999SLukasz Majewski /* #define VDBG(d, fmt, args...) do { } while (0) */ 118dee1d999SLukasz Majewski #define ERROR(d, fmt, args...) do { } while (0) 119dee1d999SLukasz Majewski #define WARNING(d, fmt, args...) do { } while (0) 120dee1d999SLukasz Majewski #define INFO(d, fmt, args...) do { } while (0) 121dee1d999SLukasz Majewski 122dee1d999SLukasz Majewski #ifdef DUMP_MSGS 123dee1d999SLukasz Majewski 124dee1d999SLukasz Majewski /* dump_msg(fsg, const char * label, const u8 * buf, unsigned length); */ 125dee1d999SLukasz Majewski # define dump_msg(fsg, label, buf, length) do { \ 126dee1d999SLukasz Majewski if (length < 512) { \ 127dee1d999SLukasz Majewski DBG(fsg, "%s, length %u:\n", label, length); \ 128dee1d999SLukasz Majewski print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \ 129dee1d999SLukasz Majewski 16, 1, buf, length, 0); \ 130dee1d999SLukasz Majewski } \ 131dee1d999SLukasz Majewski } while (0) 132dee1d999SLukasz Majewski 133dee1d999SLukasz Majewski # define dump_cdb(fsg) do { } while (0) 134dee1d999SLukasz Majewski 135dee1d999SLukasz Majewski #else 136dee1d999SLukasz Majewski 137dee1d999SLukasz Majewski # define dump_msg(fsg, /* const char * */ label, \ 138dee1d999SLukasz Majewski /* const u8 * */ buf, /* unsigned */ length) do { } while (0) 139dee1d999SLukasz Majewski 140dee1d999SLukasz Majewski # ifdef VERBOSE_DEBUG 141dee1d999SLukasz Majewski 142dee1d999SLukasz Majewski # define dump_cdb(fsg) \ 143dee1d999SLukasz Majewski print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \ 144dee1d999SLukasz Majewski 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \ 145dee1d999SLukasz Majewski 146dee1d999SLukasz Majewski # else 147dee1d999SLukasz Majewski 148dee1d999SLukasz Majewski # define dump_cdb(fsg) do { } while (0) 149dee1d999SLukasz Majewski 150dee1d999SLukasz Majewski # endif /* VERBOSE_DEBUG */ 151dee1d999SLukasz Majewski 152dee1d999SLukasz Majewski #endif /* DUMP_MSGS */ 153dee1d999SLukasz Majewski 154dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/ 155dee1d999SLukasz Majewski 156dee1d999SLukasz Majewski /* SCSI device types */ 157dee1d999SLukasz Majewski #define TYPE_DISK 0x00 158dee1d999SLukasz Majewski #define TYPE_CDROM 0x05 159dee1d999SLukasz Majewski 160dee1d999SLukasz Majewski /* USB protocol value = the transport method */ 161dee1d999SLukasz Majewski #define USB_PR_CBI 0x00 /* Control/Bulk/Interrupt */ 162dee1d999SLukasz Majewski #define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */ 163dee1d999SLukasz Majewski #define USB_PR_BULK 0x50 /* Bulk-only */ 164dee1d999SLukasz Majewski 165dee1d999SLukasz Majewski /* USB subclass value = the protocol encapsulation */ 166dee1d999SLukasz Majewski #define USB_SC_RBC 0x01 /* Reduced Block Commands (flash) */ 167dee1d999SLukasz Majewski #define USB_SC_8020 0x02 /* SFF-8020i, MMC-2, ATAPI (CD-ROM) */ 168dee1d999SLukasz Majewski #define USB_SC_QIC 0x03 /* QIC-157 (tape) */ 169dee1d999SLukasz Majewski #define USB_SC_UFI 0x04 /* UFI (floppy) */ 170dee1d999SLukasz Majewski #define USB_SC_8070 0x05 /* SFF-8070i (removable) */ 171dee1d999SLukasz Majewski #define USB_SC_SCSI 0x06 /* Transparent SCSI */ 172dee1d999SLukasz Majewski 173dee1d999SLukasz Majewski /* Bulk-only data structures */ 174dee1d999SLukasz Majewski 175dee1d999SLukasz Majewski /* Command Block Wrapper */ 176dee1d999SLukasz Majewski struct fsg_bulk_cb_wrap { 177dee1d999SLukasz Majewski __le32 Signature; /* Contains 'USBC' */ 178dee1d999SLukasz Majewski u32 Tag; /* Unique per command id */ 179dee1d999SLukasz Majewski __le32 DataTransferLength; /* Size of the data */ 180dee1d999SLukasz Majewski u8 Flags; /* Direction in bit 7 */ 181dee1d999SLukasz Majewski u8 Lun; /* LUN (normally 0) */ 182dee1d999SLukasz Majewski u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */ 183dee1d999SLukasz Majewski u8 CDB[16]; /* Command Data Block */ 184dee1d999SLukasz Majewski }; 185dee1d999SLukasz Majewski 186dee1d999SLukasz Majewski #define USB_BULK_CB_WRAP_LEN 31 187dee1d999SLukasz Majewski #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */ 188dee1d999SLukasz Majewski #define USB_BULK_IN_FLAG 0x80 189dee1d999SLukasz Majewski 190dee1d999SLukasz Majewski /* Command Status Wrapper */ 191dee1d999SLukasz Majewski struct bulk_cs_wrap { 192dee1d999SLukasz Majewski __le32 Signature; /* Should = 'USBS' */ 193dee1d999SLukasz Majewski u32 Tag; /* Same as original command */ 194dee1d999SLukasz Majewski __le32 Residue; /* Amount not transferred */ 195dee1d999SLukasz Majewski u8 Status; /* See below */ 196dee1d999SLukasz Majewski }; 197dee1d999SLukasz Majewski 198dee1d999SLukasz Majewski #define USB_BULK_CS_WRAP_LEN 13 199dee1d999SLukasz Majewski #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */ 200dee1d999SLukasz Majewski #define USB_STATUS_PASS 0 201dee1d999SLukasz Majewski #define USB_STATUS_FAIL 1 202dee1d999SLukasz Majewski #define USB_STATUS_PHASE_ERROR 2 203dee1d999SLukasz Majewski 204dee1d999SLukasz Majewski /* Bulk-only class specific requests */ 205dee1d999SLukasz Majewski #define USB_BULK_RESET_REQUEST 0xff 206dee1d999SLukasz Majewski #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe 207dee1d999SLukasz Majewski 208dee1d999SLukasz Majewski /* CBI Interrupt data structure */ 209dee1d999SLukasz Majewski struct interrupt_data { 210dee1d999SLukasz Majewski u8 bType; 211dee1d999SLukasz Majewski u8 bValue; 212dee1d999SLukasz Majewski }; 213dee1d999SLukasz Majewski 214dee1d999SLukasz Majewski #define CBI_INTERRUPT_DATA_LEN 2 215dee1d999SLukasz Majewski 216dee1d999SLukasz Majewski /* CBI Accept Device-Specific Command request */ 217dee1d999SLukasz Majewski #define USB_CBI_ADSC_REQUEST 0x00 218dee1d999SLukasz Majewski 219dee1d999SLukasz Majewski /* Length of a SCSI Command Data Block */ 220dee1d999SLukasz Majewski #define MAX_COMMAND_SIZE 16 221dee1d999SLukasz Majewski 222dee1d999SLukasz Majewski /* SCSI commands that we recognize */ 223dee1d999SLukasz Majewski #define SC_FORMAT_UNIT 0x04 224dee1d999SLukasz Majewski #define SC_INQUIRY 0x12 225dee1d999SLukasz Majewski #define SC_MODE_SELECT_6 0x15 226dee1d999SLukasz Majewski #define SC_MODE_SELECT_10 0x55 227dee1d999SLukasz Majewski #define SC_MODE_SENSE_6 0x1a 228dee1d999SLukasz Majewski #define SC_MODE_SENSE_10 0x5a 229dee1d999SLukasz Majewski #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e 230dee1d999SLukasz Majewski #define SC_READ_6 0x08 231dee1d999SLukasz Majewski #define SC_READ_10 0x28 232dee1d999SLukasz Majewski #define SC_READ_12 0xa8 233dee1d999SLukasz Majewski #define SC_READ_CAPACITY 0x25 234dee1d999SLukasz Majewski #define SC_READ_FORMAT_CAPACITIES 0x23 235dee1d999SLukasz Majewski #define SC_READ_HEADER 0x44 236dee1d999SLukasz Majewski #define SC_READ_TOC 0x43 237dee1d999SLukasz Majewski #define SC_RELEASE 0x17 238dee1d999SLukasz Majewski #define SC_REQUEST_SENSE 0x03 239dee1d999SLukasz Majewski #define SC_RESERVE 0x16 240dee1d999SLukasz Majewski #define SC_SEND_DIAGNOSTIC 0x1d 241dee1d999SLukasz Majewski #define SC_START_STOP_UNIT 0x1b 242dee1d999SLukasz Majewski #define SC_SYNCHRONIZE_CACHE 0x35 243dee1d999SLukasz Majewski #define SC_TEST_UNIT_READY 0x00 244dee1d999SLukasz Majewski #define SC_VERIFY 0x2f 245dee1d999SLukasz Majewski #define SC_WRITE_6 0x0a 246dee1d999SLukasz Majewski #define SC_WRITE_10 0x2a 247dee1d999SLukasz Majewski #define SC_WRITE_12 0xaa 248dee1d999SLukasz Majewski 249dee1d999SLukasz Majewski /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */ 250dee1d999SLukasz Majewski #define SS_NO_SENSE 0 251dee1d999SLukasz Majewski #define SS_COMMUNICATION_FAILURE 0x040800 252dee1d999SLukasz Majewski #define SS_INVALID_COMMAND 0x052000 253dee1d999SLukasz Majewski #define SS_INVALID_FIELD_IN_CDB 0x052400 254dee1d999SLukasz Majewski #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100 255dee1d999SLukasz Majewski #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500 256dee1d999SLukasz Majewski #define SS_MEDIUM_NOT_PRESENT 0x023a00 257dee1d999SLukasz Majewski #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302 258dee1d999SLukasz Majewski #define SS_NOT_READY_TO_READY_TRANSITION 0x062800 259dee1d999SLukasz Majewski #define SS_RESET_OCCURRED 0x062900 260dee1d999SLukasz Majewski #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900 261dee1d999SLukasz Majewski #define SS_UNRECOVERED_READ_ERROR 0x031100 262dee1d999SLukasz Majewski #define SS_WRITE_ERROR 0x030c02 263dee1d999SLukasz Majewski #define SS_WRITE_PROTECTED 0x072700 264dee1d999SLukasz Majewski 265dee1d999SLukasz Majewski #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */ 266dee1d999SLukasz Majewski #define ASC(x) ((u8) ((x) >> 8)) 267dee1d999SLukasz Majewski #define ASCQ(x) ((u8) (x)) 268dee1d999SLukasz Majewski 269dee1d999SLukasz Majewski struct device_attribute { int i; }; 270dee1d999SLukasz Majewski struct rw_semaphore { int i; }; 271dee1d999SLukasz Majewski #define down_write(...) do { } while (0) 272dee1d999SLukasz Majewski #define up_write(...) do { } while (0) 273dee1d999SLukasz Majewski #define down_read(...) do { } while (0) 274dee1d999SLukasz Majewski #define up_read(...) do { } while (0) 275dee1d999SLukasz Majewski #define ETOOSMALL 525 276dee1d999SLukasz Majewski 277dee1d999SLukasz Majewski #include <usb_mass_storage.h> 278dee1d999SLukasz Majewski extern struct ums_board_info *ums_info; 279dee1d999SLukasz Majewski 280dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/ 281dee1d999SLukasz Majewski 282dee1d999SLukasz Majewski struct fsg_lun { 283dee1d999SLukasz Majewski loff_t file_length; 284dee1d999SLukasz Majewski loff_t num_sectors; 285dee1d999SLukasz Majewski 286dee1d999SLukasz Majewski unsigned int initially_ro:1; 287dee1d999SLukasz Majewski unsigned int ro:1; 288dee1d999SLukasz Majewski unsigned int removable:1; 289dee1d999SLukasz Majewski unsigned int cdrom:1; 290dee1d999SLukasz Majewski unsigned int prevent_medium_removal:1; 291dee1d999SLukasz Majewski unsigned int registered:1; 292dee1d999SLukasz Majewski unsigned int info_valid:1; 293dee1d999SLukasz Majewski unsigned int nofua:1; 294dee1d999SLukasz Majewski 295dee1d999SLukasz Majewski u32 sense_data; 296dee1d999SLukasz Majewski u32 sense_data_info; 297dee1d999SLukasz Majewski u32 unit_attention_data; 298dee1d999SLukasz Majewski 299dee1d999SLukasz Majewski struct device dev; 300dee1d999SLukasz Majewski }; 301dee1d999SLukasz Majewski 302dee1d999SLukasz Majewski #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL) 303dee1d999SLukasz Majewski #if 0 304dee1d999SLukasz Majewski static struct fsg_lun *fsg_lun_from_dev(struct device *dev) 305dee1d999SLukasz Majewski { 306dee1d999SLukasz Majewski return container_of(dev, struct fsg_lun, dev); 307dee1d999SLukasz Majewski } 308dee1d999SLukasz Majewski #endif 309dee1d999SLukasz Majewski 310dee1d999SLukasz Majewski /* Big enough to hold our biggest descriptor */ 311dee1d999SLukasz Majewski #define EP0_BUFSIZE 256 312dee1d999SLukasz Majewski #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */ 313dee1d999SLukasz Majewski 314dee1d999SLukasz Majewski /* Number of buffers we will use. 2 is enough for double-buffering */ 315dee1d999SLukasz Majewski #define FSG_NUM_BUFFERS 2 316dee1d999SLukasz Majewski 317dee1d999SLukasz Majewski /* Default size of buffer length. */ 318dee1d999SLukasz Majewski #define FSG_BUFLEN ((u32)16384) 319dee1d999SLukasz Majewski 320dee1d999SLukasz Majewski /* Maximal number of LUNs supported in mass storage function */ 321dee1d999SLukasz Majewski #define FSG_MAX_LUNS 8 322dee1d999SLukasz Majewski 323dee1d999SLukasz Majewski enum fsg_buffer_state { 324dee1d999SLukasz Majewski BUF_STATE_EMPTY = 0, 325dee1d999SLukasz Majewski BUF_STATE_FULL, 326dee1d999SLukasz Majewski BUF_STATE_BUSY 327dee1d999SLukasz Majewski }; 328dee1d999SLukasz Majewski 329dee1d999SLukasz Majewski struct fsg_buffhd { 330dee1d999SLukasz Majewski #ifdef FSG_BUFFHD_STATIC_BUFFER 331dee1d999SLukasz Majewski char buf[FSG_BUFLEN]; 332dee1d999SLukasz Majewski #else 333dee1d999SLukasz Majewski void *buf; 334dee1d999SLukasz Majewski #endif 335dee1d999SLukasz Majewski enum fsg_buffer_state state; 336dee1d999SLukasz Majewski struct fsg_buffhd *next; 337dee1d999SLukasz Majewski 338dee1d999SLukasz Majewski /* 339dee1d999SLukasz Majewski * The NetChip 2280 is faster, and handles some protocol faults 340dee1d999SLukasz Majewski * better, if we don't submit any short bulk-out read requests. 341dee1d999SLukasz Majewski * So we will record the intended request length here. 342dee1d999SLukasz Majewski */ 343dee1d999SLukasz Majewski unsigned int bulk_out_intended_length; 344dee1d999SLukasz Majewski 345dee1d999SLukasz Majewski struct usb_request *inreq; 346dee1d999SLukasz Majewski int inreq_busy; 347dee1d999SLukasz Majewski struct usb_request *outreq; 348dee1d999SLukasz Majewski int outreq_busy; 349dee1d999SLukasz Majewski }; 350dee1d999SLukasz Majewski 351dee1d999SLukasz Majewski enum fsg_state { 352dee1d999SLukasz Majewski /* This one isn't used anywhere */ 353dee1d999SLukasz Majewski FSG_STATE_COMMAND_PHASE = -10, 354dee1d999SLukasz Majewski FSG_STATE_DATA_PHASE, 355dee1d999SLukasz Majewski FSG_STATE_STATUS_PHASE, 356dee1d999SLukasz Majewski 357dee1d999SLukasz Majewski FSG_STATE_IDLE = 0, 358dee1d999SLukasz Majewski FSG_STATE_ABORT_BULK_OUT, 359dee1d999SLukasz Majewski FSG_STATE_RESET, 360dee1d999SLukasz Majewski FSG_STATE_INTERFACE_CHANGE, 361dee1d999SLukasz Majewski FSG_STATE_CONFIG_CHANGE, 362dee1d999SLukasz Majewski FSG_STATE_DISCONNECT, 363dee1d999SLukasz Majewski FSG_STATE_EXIT, 364dee1d999SLukasz Majewski FSG_STATE_TERMINATED 365dee1d999SLukasz Majewski }; 366dee1d999SLukasz Majewski 367dee1d999SLukasz Majewski enum data_direction { 368dee1d999SLukasz Majewski DATA_DIR_UNKNOWN = 0, 369dee1d999SLukasz Majewski DATA_DIR_FROM_HOST, 370dee1d999SLukasz Majewski DATA_DIR_TO_HOST, 371dee1d999SLukasz Majewski DATA_DIR_NONE 372dee1d999SLukasz Majewski }; 373dee1d999SLukasz Majewski 374dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/ 375dee1d999SLukasz Majewski 376dee1d999SLukasz Majewski static inline u32 get_unaligned_be24(u8 *buf) 377dee1d999SLukasz Majewski { 378dee1d999SLukasz Majewski return 0xffffff & (u32) get_unaligned_be32(buf - 1); 379dee1d999SLukasz Majewski } 380dee1d999SLukasz Majewski 381dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/ 382dee1d999SLukasz Majewski 383dee1d999SLukasz Majewski enum { 384dee1d999SLukasz Majewski #ifndef FSG_NO_DEVICE_STRINGS 385dee1d999SLukasz Majewski FSG_STRING_MANUFACTURER = 1, 386dee1d999SLukasz Majewski FSG_STRING_PRODUCT, 387dee1d999SLukasz Majewski FSG_STRING_SERIAL, 388dee1d999SLukasz Majewski FSG_STRING_CONFIG, 389dee1d999SLukasz Majewski #endif 390dee1d999SLukasz Majewski FSG_STRING_INTERFACE 391dee1d999SLukasz Majewski }; 392dee1d999SLukasz Majewski 393dee1d999SLukasz Majewski #ifndef FSG_NO_OTG 394dee1d999SLukasz Majewski static struct usb_otg_descriptor 395dee1d999SLukasz Majewski fsg_otg_desc = { 396dee1d999SLukasz Majewski .bLength = sizeof fsg_otg_desc, 397dee1d999SLukasz Majewski .bDescriptorType = USB_DT_OTG, 398dee1d999SLukasz Majewski 399dee1d999SLukasz Majewski .bmAttributes = USB_OTG_SRP, 400dee1d999SLukasz Majewski }; 401dee1d999SLukasz Majewski #endif 402dee1d999SLukasz Majewski 403dee1d999SLukasz Majewski /* There is only one interface. */ 404dee1d999SLukasz Majewski 405dee1d999SLukasz Majewski static struct usb_interface_descriptor 406dee1d999SLukasz Majewski fsg_intf_desc = { 407dee1d999SLukasz Majewski .bLength = sizeof fsg_intf_desc, 408dee1d999SLukasz Majewski .bDescriptorType = USB_DT_INTERFACE, 409dee1d999SLukasz Majewski 410dee1d999SLukasz Majewski .bNumEndpoints = 2, /* Adjusted during fsg_bind() */ 411dee1d999SLukasz Majewski .bInterfaceClass = USB_CLASS_MASS_STORAGE, 412dee1d999SLukasz Majewski .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */ 413dee1d999SLukasz Majewski .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */ 414dee1d999SLukasz Majewski .iInterface = FSG_STRING_INTERFACE, 415dee1d999SLukasz Majewski }; 416dee1d999SLukasz Majewski 417dee1d999SLukasz Majewski /* 418dee1d999SLukasz Majewski * Three full-speed endpoint descriptors: bulk-in, bulk-out, and 419dee1d999SLukasz Majewski * interrupt-in. 420dee1d999SLukasz Majewski */ 421dee1d999SLukasz Majewski 422dee1d999SLukasz Majewski static struct usb_endpoint_descriptor 423dee1d999SLukasz Majewski fsg_fs_bulk_in_desc = { 424dee1d999SLukasz Majewski .bLength = USB_DT_ENDPOINT_SIZE, 425dee1d999SLukasz Majewski .bDescriptorType = USB_DT_ENDPOINT, 426dee1d999SLukasz Majewski 427dee1d999SLukasz Majewski .bEndpointAddress = USB_DIR_IN, 428dee1d999SLukasz Majewski .bmAttributes = USB_ENDPOINT_XFER_BULK, 429dee1d999SLukasz Majewski /* wMaxPacketSize set by autoconfiguration */ 430dee1d999SLukasz Majewski }; 431dee1d999SLukasz Majewski 432dee1d999SLukasz Majewski static struct usb_endpoint_descriptor 433dee1d999SLukasz Majewski fsg_fs_bulk_out_desc = { 434dee1d999SLukasz Majewski .bLength = USB_DT_ENDPOINT_SIZE, 435dee1d999SLukasz Majewski .bDescriptorType = USB_DT_ENDPOINT, 436dee1d999SLukasz Majewski 437dee1d999SLukasz Majewski .bEndpointAddress = USB_DIR_OUT, 438dee1d999SLukasz Majewski .bmAttributes = USB_ENDPOINT_XFER_BULK, 439dee1d999SLukasz Majewski /* wMaxPacketSize set by autoconfiguration */ 440dee1d999SLukasz Majewski }; 441dee1d999SLukasz Majewski 442dee1d999SLukasz Majewski #ifndef FSG_NO_INTR_EP 443dee1d999SLukasz Majewski 444dee1d999SLukasz Majewski static struct usb_endpoint_descriptor 445dee1d999SLukasz Majewski fsg_fs_intr_in_desc = { 446dee1d999SLukasz Majewski .bLength = USB_DT_ENDPOINT_SIZE, 447dee1d999SLukasz Majewski .bDescriptorType = USB_DT_ENDPOINT, 448dee1d999SLukasz Majewski 449dee1d999SLukasz Majewski .bEndpointAddress = USB_DIR_IN, 450dee1d999SLukasz Majewski .bmAttributes = USB_ENDPOINT_XFER_INT, 451dee1d999SLukasz Majewski .wMaxPacketSize = cpu_to_le16(2), 452dee1d999SLukasz Majewski .bInterval = 32, /* frames -> 32 ms */ 453dee1d999SLukasz Majewski }; 454dee1d999SLukasz Majewski 455dee1d999SLukasz Majewski #ifndef FSG_NO_OTG 456dee1d999SLukasz Majewski # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2 457dee1d999SLukasz Majewski #else 458dee1d999SLukasz Majewski # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1 459dee1d999SLukasz Majewski #endif 460dee1d999SLukasz Majewski 461dee1d999SLukasz Majewski #endif 462dee1d999SLukasz Majewski 463dee1d999SLukasz Majewski static struct usb_descriptor_header *fsg_fs_function[] = { 464dee1d999SLukasz Majewski #ifndef FSG_NO_OTG 465dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_otg_desc, 466dee1d999SLukasz Majewski #endif 467dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_intf_desc, 468dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc, 469dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc, 470dee1d999SLukasz Majewski #ifndef FSG_NO_INTR_EP 471dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_fs_intr_in_desc, 472dee1d999SLukasz Majewski #endif 473dee1d999SLukasz Majewski NULL, 474dee1d999SLukasz Majewski }; 475dee1d999SLukasz Majewski 476dee1d999SLukasz Majewski /* 477dee1d999SLukasz Majewski * USB 2.0 devices need to expose both high speed and full speed 478dee1d999SLukasz Majewski * descriptors, unless they only run at full speed. 479dee1d999SLukasz Majewski * 480dee1d999SLukasz Majewski * That means alternate endpoint descriptors (bigger packets) 481dee1d999SLukasz Majewski * and a "device qualifier" ... plus more construction options 482dee1d999SLukasz Majewski * for the configuration descriptor. 483dee1d999SLukasz Majewski */ 484dee1d999SLukasz Majewski static struct usb_endpoint_descriptor 485dee1d999SLukasz Majewski fsg_hs_bulk_in_desc = { 486dee1d999SLukasz Majewski .bLength = USB_DT_ENDPOINT_SIZE, 487dee1d999SLukasz Majewski .bDescriptorType = USB_DT_ENDPOINT, 488dee1d999SLukasz Majewski 489dee1d999SLukasz Majewski /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */ 490dee1d999SLukasz Majewski .bmAttributes = USB_ENDPOINT_XFER_BULK, 491dee1d999SLukasz Majewski .wMaxPacketSize = cpu_to_le16(512), 492dee1d999SLukasz Majewski }; 493dee1d999SLukasz Majewski 494dee1d999SLukasz Majewski static struct usb_endpoint_descriptor 495dee1d999SLukasz Majewski fsg_hs_bulk_out_desc = { 496dee1d999SLukasz Majewski .bLength = USB_DT_ENDPOINT_SIZE, 497dee1d999SLukasz Majewski .bDescriptorType = USB_DT_ENDPOINT, 498dee1d999SLukasz Majewski 499dee1d999SLukasz Majewski /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ 500dee1d999SLukasz Majewski .bmAttributes = USB_ENDPOINT_XFER_BULK, 501dee1d999SLukasz Majewski .wMaxPacketSize = cpu_to_le16(512), 502dee1d999SLukasz Majewski .bInterval = 1, /* NAK every 1 uframe */ 503dee1d999SLukasz Majewski }; 504dee1d999SLukasz Majewski 505dee1d999SLukasz Majewski #ifndef FSG_NO_INTR_EP 506dee1d999SLukasz Majewski 507dee1d999SLukasz Majewski static struct usb_endpoint_descriptor 508dee1d999SLukasz Majewski fsg_hs_intr_in_desc = { 509dee1d999SLukasz Majewski .bLength = USB_DT_ENDPOINT_SIZE, 510dee1d999SLukasz Majewski .bDescriptorType = USB_DT_ENDPOINT, 511dee1d999SLukasz Majewski 512dee1d999SLukasz Majewski /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */ 513dee1d999SLukasz Majewski .bmAttributes = USB_ENDPOINT_XFER_INT, 514dee1d999SLukasz Majewski .wMaxPacketSize = cpu_to_le16(2), 515dee1d999SLukasz Majewski .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */ 516dee1d999SLukasz Majewski }; 517dee1d999SLukasz Majewski 518dee1d999SLukasz Majewski #ifndef FSG_NO_OTG 519dee1d999SLukasz Majewski # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2 520dee1d999SLukasz Majewski #else 521dee1d999SLukasz Majewski # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1 522dee1d999SLukasz Majewski #endif 523dee1d999SLukasz Majewski 524dee1d999SLukasz Majewski #endif 525dee1d999SLukasz Majewski 526dee1d999SLukasz Majewski static struct usb_descriptor_header *fsg_hs_function[] = { 527dee1d999SLukasz Majewski #ifndef FSG_NO_OTG 528dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_otg_desc, 529dee1d999SLukasz Majewski #endif 530dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_intf_desc, 531dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc, 532dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc, 533dee1d999SLukasz Majewski #ifndef FSG_NO_INTR_EP 534dee1d999SLukasz Majewski (struct usb_descriptor_header *) &fsg_hs_intr_in_desc, 535dee1d999SLukasz Majewski #endif 536dee1d999SLukasz Majewski NULL, 537dee1d999SLukasz Majewski }; 538dee1d999SLukasz Majewski 539dee1d999SLukasz Majewski /* Maxpacket and other transfer characteristics vary by speed. */ 540dee1d999SLukasz Majewski static struct usb_endpoint_descriptor * 541dee1d999SLukasz Majewski fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, 542dee1d999SLukasz Majewski struct usb_endpoint_descriptor *hs) 543dee1d999SLukasz Majewski { 544dee1d999SLukasz Majewski if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 545dee1d999SLukasz Majewski return hs; 546dee1d999SLukasz Majewski return fs; 547dee1d999SLukasz Majewski } 548dee1d999SLukasz Majewski 549dee1d999SLukasz Majewski /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */ 550dee1d999SLukasz Majewski static struct usb_string fsg_strings[] = { 551dee1d999SLukasz Majewski #ifndef FSG_NO_DEVICE_STRINGS 552dee1d999SLukasz Majewski {FSG_STRING_MANUFACTURER, fsg_string_manufacturer}, 553dee1d999SLukasz Majewski {FSG_STRING_PRODUCT, fsg_string_product}, 554dee1d999SLukasz Majewski {FSG_STRING_SERIAL, fsg_string_serial}, 555dee1d999SLukasz Majewski {FSG_STRING_CONFIG, fsg_string_config}, 556dee1d999SLukasz Majewski #endif 557dee1d999SLukasz Majewski {FSG_STRING_INTERFACE, fsg_string_interface}, 558dee1d999SLukasz Majewski {} 559dee1d999SLukasz Majewski }; 560dee1d999SLukasz Majewski 561dee1d999SLukasz Majewski static struct usb_gadget_strings fsg_stringtab = { 562dee1d999SLukasz Majewski .language = 0x0409, /* en-us */ 563dee1d999SLukasz Majewski .strings = fsg_strings, 564dee1d999SLukasz Majewski }; 565dee1d999SLukasz Majewski 566dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/ 567dee1d999SLukasz Majewski 568dee1d999SLukasz Majewski /* 569dee1d999SLukasz Majewski * If the next two routines are called while the gadget is registered, 570dee1d999SLukasz Majewski * the caller must own fsg->filesem for writing. 571dee1d999SLukasz Majewski */ 572dee1d999SLukasz Majewski 573dee1d999SLukasz Majewski static int fsg_lun_open(struct fsg_lun *curlun, const char *filename) 574dee1d999SLukasz Majewski { 575dee1d999SLukasz Majewski int ro; 576dee1d999SLukasz Majewski int rc = -EINVAL; 577dee1d999SLukasz Majewski loff_t size; 578dee1d999SLukasz Majewski loff_t num_sectors; 579dee1d999SLukasz Majewski loff_t min_sectors; 580dee1d999SLukasz Majewski 581dee1d999SLukasz Majewski /* R/W if we can, R/O if we must */ 582dee1d999SLukasz Majewski ro = curlun->initially_ro; 583dee1d999SLukasz Majewski 584dee1d999SLukasz Majewski ums_info->get_capacity(&(ums_info->ums_dev), &size); 585dee1d999SLukasz Majewski if (size < 0) { 586dee1d999SLukasz Majewski printf("unable to find file size: %s\n", filename); 587dee1d999SLukasz Majewski rc = (int) size; 588dee1d999SLukasz Majewski goto out; 589dee1d999SLukasz Majewski } 590dee1d999SLukasz Majewski num_sectors = size >> 9; /* File size in 512-byte blocks */ 591dee1d999SLukasz Majewski min_sectors = 1; 592dee1d999SLukasz Majewski if (num_sectors < min_sectors) { 593dee1d999SLukasz Majewski printf("file too small: %s\n", filename); 594dee1d999SLukasz Majewski rc = -ETOOSMALL; 595dee1d999SLukasz Majewski goto out; 596dee1d999SLukasz Majewski } 597dee1d999SLukasz Majewski 598dee1d999SLukasz Majewski curlun->ro = ro; 599dee1d999SLukasz Majewski curlun->file_length = size; 600dee1d999SLukasz Majewski curlun->num_sectors = num_sectors; 601dee1d999SLukasz Majewski debug("open backing file: %s\n", filename); 602dee1d999SLukasz Majewski rc = 0; 603dee1d999SLukasz Majewski 604dee1d999SLukasz Majewski out: 605dee1d999SLukasz Majewski return rc; 606dee1d999SLukasz Majewski } 607dee1d999SLukasz Majewski 608dee1d999SLukasz Majewski static void fsg_lun_close(struct fsg_lun *curlun) 609dee1d999SLukasz Majewski { 610dee1d999SLukasz Majewski } 611dee1d999SLukasz Majewski 612dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/ 613dee1d999SLukasz Majewski 614dee1d999SLukasz Majewski /* 615dee1d999SLukasz Majewski * Sync the file data, don't bother with the metadata. 616dee1d999SLukasz Majewski * This code was copied from fs/buffer.c:sys_fdatasync(). 617dee1d999SLukasz Majewski */ 618dee1d999SLukasz Majewski static int fsg_lun_fsync_sub(struct fsg_lun *curlun) 619dee1d999SLukasz Majewski { 620dee1d999SLukasz Majewski return 0; 621dee1d999SLukasz Majewski } 622dee1d999SLukasz Majewski 623dee1d999SLukasz Majewski static void store_cdrom_address(u8 *dest, int msf, u32 addr) 624dee1d999SLukasz Majewski { 625dee1d999SLukasz Majewski if (msf) { 626dee1d999SLukasz Majewski /* Convert to Minutes-Seconds-Frames */ 627dee1d999SLukasz Majewski addr >>= 2; /* Convert to 2048-byte frames */ 628dee1d999SLukasz Majewski addr += 2*75; /* Lead-in occupies 2 seconds */ 629dee1d999SLukasz Majewski dest[3] = addr % 75; /* Frames */ 630dee1d999SLukasz Majewski addr /= 75; 631dee1d999SLukasz Majewski dest[2] = addr % 60; /* Seconds */ 632dee1d999SLukasz Majewski addr /= 60; 633dee1d999SLukasz Majewski dest[1] = addr; /* Minutes */ 634dee1d999SLukasz Majewski dest[0] = 0; /* Reserved */ 635dee1d999SLukasz Majewski } else { 636dee1d999SLukasz Majewski /* Absolute sector */ 637dee1d999SLukasz Majewski put_unaligned_be32(addr, dest); 638dee1d999SLukasz Majewski } 639dee1d999SLukasz Majewski } 640dee1d999SLukasz Majewski 641dee1d999SLukasz Majewski /*-------------------------------------------------------------------------*/ 642