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