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