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