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