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