Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * storage_common.c -- Common definitions for mass storage functionality |
| 3 | * |
| 4 | * Copyright (C) 2003-2008 Alan Stern |
| 5 | * Copyeight (C) 2009 Samsung Electronics |
| 6 | * Author: Michal Nazarewicz (m.nazarewicz@samsung.com) |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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 |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 24 | /* |
| 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 |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 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. |
| 33 | */ |
| 34 | |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 35 | /* |
| 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. |
| 40 | * |
| 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. |
| 45 | * |
| 46 | * When FSG_NO_OTG is defined fsg_otg_desc won't be defined. |
| 47 | */ |
| 48 | |
Michal Nazarewicz | 606206c2 | 2009-10-28 16:57:21 +0100 | [diff] [blame] | 49 | /* |
| 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. |
| 53 | */ |
| 54 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 55 | |
Michal Nazarewicz | 3323b71 | 2010-10-07 13:05:24 +0200 | [diff] [blame] | 56 | #include <linux/usb/storage.h> |
Michal Nazarewicz | 0a6a717 | 2010-10-07 14:46:15 +0200 | [diff] [blame] | 57 | #include <scsi/scsi.h> |
| 58 | #include <asm/unaligned.h> |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 59 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 60 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 61 | /* |
| 62 | * Thanks to NetChip Technologies for donating this product ID. |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 63 | * |
| 64 | * DO NOT REUSE THESE IDs with any other driver!! Ever!! |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 65 | * Instead: allocate your own, using normal USB-IF procedures. |
| 66 | */ |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 67 | #define FSG_VENDOR_ID 0x0525 /* NetChip */ |
| 68 | #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 69 | |
| 70 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 71 | /*-------------------------------------------------------------------------*/ |
| 72 | |
| 73 | |
| 74 | #ifndef DEBUG |
| 75 | #undef VERBOSE_DEBUG |
| 76 | #undef DUMP_MSGS |
| 77 | #endif /* !DEBUG */ |
| 78 | |
| 79 | #ifdef VERBOSE_DEBUG |
| 80 | #define VLDBG LDBG |
| 81 | #else |
| 82 | #define VLDBG(lun, fmt, args...) do { } while (0) |
| 83 | #endif /* VERBOSE_DEBUG */ |
| 84 | |
| 85 | #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args) |
| 86 | #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args) |
| 87 | #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args) |
| 88 | #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args) |
| 89 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 90 | /* |
| 91 | * Keep those macros in sync with those in |
| 92 | * include/linux/usb/composite.h or else GCC will complain. If they |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 93 | * are identical (the same names of arguments, white spaces in the |
| 94 | * same places) GCC will allow redefinition otherwise (even if some |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 95 | * white space is removed or added) warning will be issued. |
| 96 | * |
| 97 | * Those macros are needed here because File Storage Gadget does not |
| 98 | * include the composite.h header. For composite gadgets those macros |
| 99 | * are redundant since composite.h is included any way. |
| 100 | * |
| 101 | * One could check whether those macros are already defined (which |
| 102 | * would indicate composite.h had been included) or not (which would |
| 103 | * indicate we were in FSG) but this is not done because a warning is |
| 104 | * desired if definitions here differ from the ones in composite.h. |
| 105 | * |
| 106 | * We want the definitions to match and be the same in File Storage |
| 107 | * Gadget as well as Mass Storage Function (and so composite gadgets |
| 108 | * using MSF). If someone changes them in composite.h it will produce |
| 109 | * a warning in this file when building MSF. |
| 110 | */ |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 111 | #define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args) |
| 112 | #define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args) |
| 113 | #define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args) |
| 114 | #define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args) |
| 115 | #define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 116 | |
| 117 | |
| 118 | |
| 119 | #ifdef DUMP_MSGS |
| 120 | |
| 121 | # define dump_msg(fsg, /* const char * */ label, \ |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 122 | /* const u8 * */ buf, /* unsigned */ length) do { \ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 123 | if (length < 512) { \ |
| 124 | DBG(fsg, "%s, length %u:\n", label, length); \ |
| 125 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \ |
| 126 | 16, 1, buf, length, 0); \ |
| 127 | } \ |
| 128 | } while (0) |
| 129 | |
| 130 | # define dump_cdb(fsg) do { } while (0) |
| 131 | |
| 132 | #else |
| 133 | |
| 134 | # define dump_msg(fsg, /* const char * */ label, \ |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 135 | /* const u8 * */ buf, /* unsigned */ length) do { } while (0) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 136 | |
| 137 | # ifdef VERBOSE_DEBUG |
| 138 | |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 139 | # define dump_cdb(fsg) \ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 140 | print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \ |
| 141 | 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \ |
| 142 | |
| 143 | # else |
| 144 | |
| 145 | # define dump_cdb(fsg) do { } while (0) |
| 146 | |
| 147 | # endif /* VERBOSE_DEBUG */ |
| 148 | |
| 149 | #endif /* DUMP_MSGS */ |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | /*-------------------------------------------------------------------------*/ |
| 156 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 157 | /* Bulk-only data structures */ |
| 158 | |
| 159 | /* Command Block Wrapper */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 160 | struct fsg_bulk_cb_wrap { |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 161 | __le32 Signature; /* Contains 'USBC' */ |
| 162 | u32 Tag; /* Unique per command id */ |
| 163 | __le32 DataTransferLength; /* Size of the data */ |
| 164 | u8 Flags; /* Direction in bit 7 */ |
| 165 | u8 Lun; /* LUN (normally 0) */ |
| 166 | u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */ |
| 167 | u8 CDB[16]; /* Command Data Block */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | #define USB_BULK_CB_WRAP_LEN 31 |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 171 | #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 172 | #define USB_BULK_IN_FLAG 0x80 |
| 173 | |
| 174 | /* Command Status Wrapper */ |
| 175 | struct bulk_cs_wrap { |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 176 | __le32 Signature; /* Should = 'USBS' */ |
| 177 | u32 Tag; /* Same as original command */ |
| 178 | __le32 Residue; /* Amount not transferred */ |
| 179 | u8 Status; /* See below */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | #define USB_BULK_CS_WRAP_LEN 13 |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 183 | #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 184 | #define USB_STATUS_PASS 0 |
| 185 | #define USB_STATUS_FAIL 1 |
| 186 | #define USB_STATUS_PHASE_ERROR 2 |
| 187 | |
| 188 | /* Bulk-only class specific requests */ |
| 189 | #define USB_BULK_RESET_REQUEST 0xff |
| 190 | #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe |
| 191 | |
| 192 | |
| 193 | /* CBI Interrupt data structure */ |
| 194 | struct interrupt_data { |
| 195 | u8 bType; |
| 196 | u8 bValue; |
| 197 | }; |
| 198 | |
| 199 | #define CBI_INTERRUPT_DATA_LEN 2 |
| 200 | |
| 201 | /* CBI Accept Device-Specific Command request */ |
| 202 | #define USB_CBI_ADSC_REQUEST 0x00 |
| 203 | |
| 204 | |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 205 | /* Length of a SCSI Command Data Block */ |
| 206 | #define MAX_COMMAND_SIZE 16 |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 207 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 208 | /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */ |
| 209 | #define SS_NO_SENSE 0 |
| 210 | #define SS_COMMUNICATION_FAILURE 0x040800 |
| 211 | #define SS_INVALID_COMMAND 0x052000 |
| 212 | #define SS_INVALID_FIELD_IN_CDB 0x052400 |
| 213 | #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100 |
| 214 | #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500 |
| 215 | #define SS_MEDIUM_NOT_PRESENT 0x023a00 |
| 216 | #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302 |
| 217 | #define SS_NOT_READY_TO_READY_TRANSITION 0x062800 |
| 218 | #define SS_RESET_OCCURRED 0x062900 |
| 219 | #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900 |
| 220 | #define SS_UNRECOVERED_READ_ERROR 0x031100 |
| 221 | #define SS_WRITE_ERROR 0x030c02 |
| 222 | #define SS_WRITE_PROTECTED 0x072700 |
| 223 | |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 224 | #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 225 | #define ASC(x) ((u8) ((x) >> 8)) |
| 226 | #define ASCQ(x) ((u8) (x)) |
| 227 | |
| 228 | |
| 229 | /*-------------------------------------------------------------------------*/ |
| 230 | |
| 231 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 232 | struct fsg_lun { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 233 | struct file *filp; |
| 234 | loff_t file_length; |
| 235 | loff_t num_sectors; |
| 236 | |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 237 | unsigned int initially_ro:1; |
| 238 | unsigned int ro:1; |
| 239 | unsigned int removable:1; |
| 240 | unsigned int cdrom:1; |
| 241 | unsigned int prevent_medium_removal:1; |
| 242 | unsigned int registered:1; |
| 243 | unsigned int info_valid:1; |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 244 | unsigned int nofua:1; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 245 | |
| 246 | u32 sense_data; |
| 247 | u32 sense_data_info; |
| 248 | u32 unit_attention_data; |
| 249 | |
| 250 | struct device dev; |
| 251 | }; |
| 252 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 253 | #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 254 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 255 | static struct fsg_lun *fsg_lun_from_dev(struct device *dev) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 256 | { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 257 | return container_of(dev, struct fsg_lun, dev); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |
| 261 | /* Big enough to hold our biggest descriptor */ |
| 262 | #define EP0_BUFSIZE 256 |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 263 | #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 264 | |
| 265 | /* Number of buffers we will use. 2 is enough for double-buffering */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 266 | #define FSG_NUM_BUFFERS 2 |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 267 | |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 268 | /* Default size of buffer length. */ |
| 269 | #define FSG_BUFLEN ((u32)16384) |
| 270 | |
| 271 | /* Maximal number of LUNs supported in mass storage function */ |
| 272 | #define FSG_MAX_LUNS 8 |
| 273 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 274 | enum fsg_buffer_state { |
| 275 | BUF_STATE_EMPTY = 0, |
| 276 | BUF_STATE_FULL, |
| 277 | BUF_STATE_BUSY |
| 278 | }; |
| 279 | |
| 280 | struct fsg_buffhd { |
Michal Nazarewicz | 606206c2 | 2009-10-28 16:57:21 +0100 | [diff] [blame] | 281 | #ifdef FSG_BUFFHD_STATIC_BUFFER |
| 282 | char buf[FSG_BUFLEN]; |
| 283 | #else |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 284 | void *buf; |
Michal Nazarewicz | 606206c2 | 2009-10-28 16:57:21 +0100 | [diff] [blame] | 285 | #endif |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 286 | enum fsg_buffer_state state; |
| 287 | struct fsg_buffhd *next; |
| 288 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 289 | /* |
| 290 | * The NetChip 2280 is faster, and handles some protocol faults |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 291 | * better, if we don't submit any short bulk-out read requests. |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 292 | * So we will record the intended request length here. |
| 293 | */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 294 | unsigned int bulk_out_intended_length; |
| 295 | |
| 296 | struct usb_request *inreq; |
| 297 | int inreq_busy; |
| 298 | struct usb_request *outreq; |
| 299 | int outreq_busy; |
| 300 | }; |
| 301 | |
| 302 | enum fsg_state { |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 303 | /* This one isn't used anywhere */ |
| 304 | FSG_STATE_COMMAND_PHASE = -10, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 305 | FSG_STATE_DATA_PHASE, |
| 306 | FSG_STATE_STATUS_PHASE, |
| 307 | |
| 308 | FSG_STATE_IDLE = 0, |
| 309 | FSG_STATE_ABORT_BULK_OUT, |
| 310 | FSG_STATE_RESET, |
| 311 | FSG_STATE_INTERFACE_CHANGE, |
| 312 | FSG_STATE_CONFIG_CHANGE, |
| 313 | FSG_STATE_DISCONNECT, |
| 314 | FSG_STATE_EXIT, |
| 315 | FSG_STATE_TERMINATED |
| 316 | }; |
| 317 | |
| 318 | enum data_direction { |
| 319 | DATA_DIR_UNKNOWN = 0, |
| 320 | DATA_DIR_FROM_HOST, |
| 321 | DATA_DIR_TO_HOST, |
| 322 | DATA_DIR_NONE |
| 323 | }; |
| 324 | |
| 325 | |
| 326 | /*-------------------------------------------------------------------------*/ |
| 327 | |
| 328 | |
| 329 | static inline u32 get_unaligned_be24(u8 *buf) |
| 330 | { |
| 331 | return 0xffffff & (u32) get_unaligned_be32(buf - 1); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /*-------------------------------------------------------------------------*/ |
| 336 | |
| 337 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 338 | enum { |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 339 | #ifndef FSG_NO_DEVICE_STRINGS |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 340 | FSG_STRING_MANUFACTURER = 1, |
| 341 | FSG_STRING_PRODUCT, |
| 342 | FSG_STRING_SERIAL, |
| 343 | FSG_STRING_CONFIG, |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 344 | #endif |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 345 | FSG_STRING_INTERFACE |
| 346 | }; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 347 | |
| 348 | |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 349 | #ifndef FSG_NO_OTG |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 350 | static struct usb_otg_descriptor |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 351 | fsg_otg_desc = { |
| 352 | .bLength = sizeof fsg_otg_desc, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 353 | .bDescriptorType = USB_DT_OTG, |
| 354 | |
| 355 | .bmAttributes = USB_OTG_SRP, |
| 356 | }; |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 357 | #endif |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 358 | |
| 359 | /* There is only one interface. */ |
| 360 | |
| 361 | static struct usb_interface_descriptor |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 362 | fsg_intf_desc = { |
| 363 | .bLength = sizeof fsg_intf_desc, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 364 | .bDescriptorType = USB_DT_INTERFACE, |
| 365 | |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 366 | .bNumEndpoints = 2, /* Adjusted during fsg_bind() */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 367 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 368 | .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */ |
| 369 | .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 370 | .iInterface = FSG_STRING_INTERFACE, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 371 | }; |
| 372 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 373 | /* |
| 374 | * Three full-speed endpoint descriptors: bulk-in, bulk-out, and |
| 375 | * interrupt-in. |
| 376 | */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 377 | |
| 378 | static struct usb_endpoint_descriptor |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 379 | fsg_fs_bulk_in_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 380 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 381 | .bDescriptorType = USB_DT_ENDPOINT, |
| 382 | |
| 383 | .bEndpointAddress = USB_DIR_IN, |
| 384 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 385 | /* wMaxPacketSize set by autoconfiguration */ |
| 386 | }; |
| 387 | |
| 388 | static struct usb_endpoint_descriptor |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 389 | fsg_fs_bulk_out_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 390 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 391 | .bDescriptorType = USB_DT_ENDPOINT, |
| 392 | |
| 393 | .bEndpointAddress = USB_DIR_OUT, |
| 394 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 395 | /* wMaxPacketSize set by autoconfiguration */ |
| 396 | }; |
| 397 | |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 398 | #ifndef FSG_NO_INTR_EP |
| 399 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 400 | static struct usb_endpoint_descriptor |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 401 | fsg_fs_intr_in_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 402 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 403 | .bDescriptorType = USB_DT_ENDPOINT, |
| 404 | |
| 405 | .bEndpointAddress = USB_DIR_IN, |
| 406 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 407 | .wMaxPacketSize = cpu_to_le16(2), |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 408 | .bInterval = 32, /* frames -> 32 ms */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 409 | }; |
| 410 | |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 411 | #ifndef FSG_NO_OTG |
| 412 | # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2 |
| 413 | #else |
| 414 | # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1 |
| 415 | #endif |
| 416 | |
| 417 | #endif |
| 418 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 419 | static struct usb_descriptor_header *fsg_fs_function[] = { |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 420 | #ifndef FSG_NO_OTG |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 421 | (struct usb_descriptor_header *) &fsg_otg_desc, |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 422 | #endif |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 423 | (struct usb_descriptor_header *) &fsg_intf_desc, |
| 424 | (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc, |
| 425 | (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc, |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 426 | #ifndef FSG_NO_INTR_EP |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 427 | (struct usb_descriptor_header *) &fsg_fs_intr_in_desc, |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 428 | #endif |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 429 | NULL, |
| 430 | }; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 431 | |
| 432 | |
| 433 | /* |
| 434 | * USB 2.0 devices need to expose both high speed and full speed |
| 435 | * descriptors, unless they only run at full speed. |
| 436 | * |
| 437 | * That means alternate endpoint descriptors (bigger packets) |
| 438 | * and a "device qualifier" ... plus more construction options |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 439 | * for the configuration descriptor. |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 440 | */ |
| 441 | static struct usb_endpoint_descriptor |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 442 | fsg_hs_bulk_in_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 443 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 444 | .bDescriptorType = USB_DT_ENDPOINT, |
| 445 | |
| 446 | /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */ |
| 447 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 448 | .wMaxPacketSize = cpu_to_le16(512), |
| 449 | }; |
| 450 | |
| 451 | static struct usb_endpoint_descriptor |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 452 | fsg_hs_bulk_out_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 453 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 454 | .bDescriptorType = USB_DT_ENDPOINT, |
| 455 | |
| 456 | /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ |
| 457 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 458 | .wMaxPacketSize = cpu_to_le16(512), |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 459 | .bInterval = 1, /* NAK every 1 uframe */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 460 | }; |
| 461 | |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 462 | #ifndef FSG_NO_INTR_EP |
| 463 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 464 | static struct usb_endpoint_descriptor |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 465 | fsg_hs_intr_in_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 466 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 467 | .bDescriptorType = USB_DT_ENDPOINT, |
| 468 | |
| 469 | /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */ |
| 470 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 471 | .wMaxPacketSize = cpu_to_le16(2), |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 472 | .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 473 | }; |
| 474 | |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 475 | #ifndef FSG_NO_OTG |
| 476 | # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2 |
| 477 | #else |
| 478 | # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1 |
| 479 | #endif |
| 480 | |
| 481 | #endif |
| 482 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 483 | static struct usb_descriptor_header *fsg_hs_function[] = { |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 484 | #ifndef FSG_NO_OTG |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 485 | (struct usb_descriptor_header *) &fsg_otg_desc, |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 486 | #endif |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 487 | (struct usb_descriptor_header *) &fsg_intf_desc, |
| 488 | (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc, |
| 489 | (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc, |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 490 | #ifndef FSG_NO_INTR_EP |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 491 | (struct usb_descriptor_header *) &fsg_hs_intr_in_desc, |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 492 | #endif |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 493 | NULL, |
| 494 | }; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 495 | |
| 496 | /* Maxpacket and other transfer characteristics vary by speed. */ |
| 497 | static struct usb_endpoint_descriptor * |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 498 | fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 499 | struct usb_endpoint_descriptor *hs) |
| 500 | { |
| 501 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 502 | return hs; |
| 503 | return fs; |
| 504 | } |
| 505 | |
| 506 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 507 | /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 508 | static struct usb_string fsg_strings[] = { |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 509 | #ifndef FSG_NO_DEVICE_STRINGS |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 510 | {FSG_STRING_MANUFACTURER, fsg_string_manufacturer}, |
| 511 | {FSG_STRING_PRODUCT, fsg_string_product}, |
Alan Stern | d808742 | 2010-09-03 11:15:41 -0400 | [diff] [blame] | 512 | {FSG_STRING_SERIAL, ""}, |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 513 | {FSG_STRING_CONFIG, fsg_string_config}, |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 514 | #endif |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 515 | {FSG_STRING_INTERFACE, fsg_string_interface}, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 516 | {} |
| 517 | }; |
| 518 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 519 | static struct usb_gadget_strings fsg_stringtab = { |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 520 | .language = 0x0409, /* en-us */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 521 | .strings = fsg_strings, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 522 | }; |
| 523 | |
| 524 | |
| 525 | /*-------------------------------------------------------------------------*/ |
| 526 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 527 | /* |
| 528 | * If the next two routines are called while the gadget is registered, |
| 529 | * the caller must own fsg->filesem for writing. |
| 530 | */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 531 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 532 | static int fsg_lun_open(struct fsg_lun *curlun, const char *filename) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 533 | { |
| 534 | int ro; |
| 535 | struct file *filp = NULL; |
| 536 | int rc = -EINVAL; |
| 537 | struct inode *inode = NULL; |
| 538 | loff_t size; |
| 539 | loff_t num_sectors; |
| 540 | loff_t min_sectors; |
| 541 | |
| 542 | /* R/W if we can, R/O if we must */ |
Michal Nazarewicz | e909ef5 | 2009-10-28 16:57:16 +0100 | [diff] [blame] | 543 | ro = curlun->initially_ro; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 544 | if (!ro) { |
| 545 | filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0); |
Tejun Heo | 75f1dc0 | 2010-11-13 11:55:17 +0100 | [diff] [blame^] | 546 | if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 547 | ro = 1; |
| 548 | } |
| 549 | if (ro) |
| 550 | filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0); |
| 551 | if (IS_ERR(filp)) { |
| 552 | LINFO(curlun, "unable to open backing file: %s\n", filename); |
| 553 | return PTR_ERR(filp); |
| 554 | } |
| 555 | |
| 556 | if (!(filp->f_mode & FMODE_WRITE)) |
| 557 | ro = 1; |
| 558 | |
| 559 | if (filp->f_path.dentry) |
| 560 | inode = filp->f_path.dentry->d_inode; |
Tejun Heo | 75f1dc0 | 2010-11-13 11:55:17 +0100 | [diff] [blame^] | 561 | if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 562 | LINFO(curlun, "invalid file type: %s\n", filename); |
| 563 | goto out; |
| 564 | } |
| 565 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 566 | /* |
| 567 | * If we can't read the file, it's no good. |
| 568 | * If we can't write the file, use it read-only. |
| 569 | */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 570 | if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) { |
| 571 | LINFO(curlun, "file not readable: %s\n", filename); |
| 572 | goto out; |
| 573 | } |
| 574 | if (!(filp->f_op->write || filp->f_op->aio_write)) |
| 575 | ro = 1; |
| 576 | |
| 577 | size = i_size_read(inode->i_mapping->host); |
| 578 | if (size < 0) { |
| 579 | LINFO(curlun, "unable to find file size: %s\n", filename); |
| 580 | rc = (int) size; |
| 581 | goto out; |
| 582 | } |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 583 | num_sectors = size >> 9; /* File size in 512-byte blocks */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 584 | min_sectors = 1; |
Michal Nazarewicz | e909ef5 | 2009-10-28 16:57:16 +0100 | [diff] [blame] | 585 | if (curlun->cdrom) { |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 586 | num_sectors &= ~3; /* Reduce to a multiple of 2048 */ |
| 587 | min_sectors = 300*4; /* Smallest track is 300 frames */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 588 | if (num_sectors >= 256*60*75*4) { |
| 589 | num_sectors = (256*60*75 - 1) * 4; |
| 590 | LINFO(curlun, "file too big: %s\n", filename); |
| 591 | LINFO(curlun, "using only first %d blocks\n", |
| 592 | (int) num_sectors); |
| 593 | } |
| 594 | } |
| 595 | if (num_sectors < min_sectors) { |
| 596 | LINFO(curlun, "file too small: %s\n", filename); |
| 597 | rc = -ETOOSMALL; |
| 598 | goto out; |
| 599 | } |
| 600 | |
| 601 | get_file(filp); |
| 602 | curlun->ro = ro; |
| 603 | curlun->filp = filp; |
| 604 | curlun->file_length = size; |
| 605 | curlun->num_sectors = num_sectors; |
| 606 | LDBG(curlun, "open backing file: %s\n", filename); |
| 607 | rc = 0; |
| 608 | |
| 609 | out: |
| 610 | filp_close(filp, current->files); |
| 611 | return rc; |
| 612 | } |
| 613 | |
| 614 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 615 | static void fsg_lun_close(struct fsg_lun *curlun) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 616 | { |
| 617 | if (curlun->filp) { |
| 618 | LDBG(curlun, "close backing file\n"); |
| 619 | fput(curlun->filp); |
| 620 | curlun->filp = NULL; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | |
| 625 | /*-------------------------------------------------------------------------*/ |
| 626 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 627 | /* |
| 628 | * Sync the file data, don't bother with the metadata. |
| 629 | * This code was copied from fs/buffer.c:sys_fdatasync(). |
| 630 | */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 631 | static int fsg_lun_fsync_sub(struct fsg_lun *curlun) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 632 | { |
| 633 | struct file *filp = curlun->filp; |
| 634 | |
| 635 | if (curlun->ro || !filp) |
| 636 | return 0; |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 637 | return vfs_fsync(filp, 1); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static void store_cdrom_address(u8 *dest, int msf, u32 addr) |
| 641 | { |
| 642 | if (msf) { |
| 643 | /* Convert to Minutes-Seconds-Frames */ |
| 644 | addr >>= 2; /* Convert to 2048-byte frames */ |
| 645 | addr += 2*75; /* Lead-in occupies 2 seconds */ |
| 646 | dest[3] = addr % 75; /* Frames */ |
| 647 | addr /= 75; |
| 648 | dest[2] = addr % 60; /* Seconds */ |
| 649 | addr /= 60; |
| 650 | dest[1] = addr; /* Minutes */ |
| 651 | dest[0] = 0; /* Reserved */ |
| 652 | } else { |
| 653 | /* Absolute sector */ |
| 654 | put_unaligned_be32(addr, dest); |
| 655 | } |
| 656 | } |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 657 | |
| 658 | |
| 659 | /*-------------------------------------------------------------------------*/ |
| 660 | |
| 661 | |
| 662 | static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr, |
| 663 | char *buf) |
| 664 | { |
| 665 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); |
| 666 | |
| 667 | return sprintf(buf, "%d\n", fsg_lun_is_open(curlun) |
| 668 | ? curlun->ro |
| 669 | : curlun->initially_ro); |
| 670 | } |
| 671 | |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 672 | static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr, |
| 673 | char *buf) |
| 674 | { |
| 675 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); |
| 676 | |
| 677 | return sprintf(buf, "%u\n", curlun->nofua); |
| 678 | } |
| 679 | |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 680 | static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr, |
| 681 | char *buf) |
| 682 | { |
| 683 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); |
| 684 | struct rw_semaphore *filesem = dev_get_drvdata(dev); |
| 685 | char *p; |
| 686 | ssize_t rc; |
| 687 | |
| 688 | down_read(filesem); |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 689 | if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 690 | p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1); |
| 691 | if (IS_ERR(p)) |
| 692 | rc = PTR_ERR(p); |
| 693 | else { |
| 694 | rc = strlen(p); |
| 695 | memmove(buf, p, rc); |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 696 | buf[rc] = '\n'; /* Add a newline */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 697 | buf[++rc] = 0; |
| 698 | } |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 699 | } else { /* No file, return 0 bytes */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 700 | *buf = 0; |
| 701 | rc = 0; |
| 702 | } |
| 703 | up_read(filesem); |
| 704 | return rc; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr, |
| 709 | const char *buf, size_t count) |
| 710 | { |
| 711 | ssize_t rc = count; |
| 712 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); |
| 713 | struct rw_semaphore *filesem = dev_get_drvdata(dev); |
Andy Shevchenko | 8156d15 | 2010-07-22 11:58:47 +0300 | [diff] [blame] | 714 | unsigned long ro; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 715 | |
Andy Shevchenko | 8156d15 | 2010-07-22 11:58:47 +0300 | [diff] [blame] | 716 | if (strict_strtoul(buf, 2, &ro)) |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 717 | return -EINVAL; |
| 718 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 719 | /* |
| 720 | * Allow the write-enable status to change only while the |
| 721 | * backing file is closed. |
| 722 | */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 723 | down_read(filesem); |
| 724 | if (fsg_lun_is_open(curlun)) { |
| 725 | LDBG(curlun, "read-only status change prevented\n"); |
| 726 | rc = -EBUSY; |
| 727 | } else { |
Andy Shevchenko | 8156d15 | 2010-07-22 11:58:47 +0300 | [diff] [blame] | 728 | curlun->ro = ro; |
| 729 | curlun->initially_ro = ro; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 730 | LDBG(curlun, "read-only status set to %d\n", curlun->ro); |
| 731 | } |
| 732 | up_read(filesem); |
| 733 | return rc; |
| 734 | } |
| 735 | |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 736 | static ssize_t fsg_store_nofua(struct device *dev, |
| 737 | struct device_attribute *attr, |
| 738 | const char *buf, size_t count) |
| 739 | { |
| 740 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); |
| 741 | unsigned long nofua; |
| 742 | |
| 743 | if (strict_strtoul(buf, 2, &nofua)) |
| 744 | return -EINVAL; |
| 745 | |
| 746 | /* Sync data when switching from async mode to sync */ |
| 747 | if (!nofua && curlun->nofua) |
| 748 | fsg_lun_fsync_sub(curlun); |
| 749 | |
| 750 | curlun->nofua = nofua; |
| 751 | |
| 752 | return count; |
| 753 | } |
| 754 | |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 755 | static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr, |
| 756 | const char *buf, size_t count) |
| 757 | { |
| 758 | struct fsg_lun *curlun = fsg_lun_from_dev(dev); |
| 759 | struct rw_semaphore *filesem = dev_get_drvdata(dev); |
| 760 | int rc = 0; |
| 761 | |
| 762 | if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) { |
| 763 | LDBG(curlun, "eject attempt prevented\n"); |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 764 | return -EBUSY; /* "Door is locked" */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* Remove a trailing newline */ |
| 768 | if (count > 0 && buf[count-1] == '\n') |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 769 | ((char *) buf)[count-1] = 0; /* Ugh! */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 770 | |
| 771 | /* Eject current medium */ |
| 772 | down_write(filesem); |
| 773 | if (fsg_lun_is_open(curlun)) { |
| 774 | fsg_lun_close(curlun); |
| 775 | curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT; |
| 776 | } |
| 777 | |
| 778 | /* Load new medium */ |
| 779 | if (count > 0 && buf[0]) { |
| 780 | rc = fsg_lun_open(curlun, buf); |
| 781 | if (rc == 0) |
| 782 | curlun->unit_attention_data = |
| 783 | SS_NOT_READY_TO_READY_TRANSITION; |
| 784 | } |
| 785 | up_write(filesem); |
| 786 | return (rc < 0 ? rc : count); |
| 787 | } |