| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Driver for USB Mass Storage compliant devices | 
|  | 2 | * SCSI layer glue code | 
|  | 3 | * | 
|  | 4 | * $Id: scsiglue.c,v 1.26 2002/04/22 03:39:43 mdharm Exp $ | 
|  | 5 | * | 
|  | 6 | * Current development and maintenance by: | 
|  | 7 | *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) | 
|  | 8 | * | 
|  | 9 | * Developed with the assistance of: | 
|  | 10 | *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) | 
|  | 11 | *   (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov) | 
|  | 12 | * | 
|  | 13 | * Initial work by: | 
|  | 14 | *   (c) 1999 Michael Gee (michael@linuxspecific.com) | 
|  | 15 | * | 
|  | 16 | * This driver is based on the 'USB Mass Storage Class' document. This | 
|  | 17 | * describes in detail the protocol used to communicate with such | 
|  | 18 | * devices.  Clearly, the designers had SCSI and ATAPI commands in | 
|  | 19 | * mind when they created this document.  The commands are all very | 
|  | 20 | * similar to commands in the SCSI-II and ATAPI specifications. | 
|  | 21 | * | 
|  | 22 | * It is important to note that in a number of cases this class | 
|  | 23 | * exhibits class-specific exemptions from the USB specification. | 
|  | 24 | * Notably the usage of NAK, STALL and ACK differs from the norm, in | 
|  | 25 | * that they are used to communicate wait, failed and OK on commands. | 
|  | 26 | * | 
|  | 27 | * Also, for certain devices, the interrupt endpoint is used to convey | 
|  | 28 | * status of a command. | 
|  | 29 | * | 
|  | 30 | * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more | 
|  | 31 | * information about this driver. | 
|  | 32 | * | 
|  | 33 | * This program is free software; you can redistribute it and/or modify it | 
|  | 34 | * under the terms of the GNU General Public License as published by the | 
|  | 35 | * Free Software Foundation; either version 2, or (at your option) any | 
|  | 36 | * later version. | 
|  | 37 | * | 
|  | 38 | * This program is distributed in the hope that it will be useful, but | 
|  | 39 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 41 | * General Public License for more details. | 
|  | 42 | * | 
|  | 43 | * You should have received a copy of the GNU General Public License along | 
|  | 44 | * with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 45 | * 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 46 | */ | 
|  | 47 |  | 
|  | 48 | #include <linux/slab.h> | 
|  | 49 | #include <linux/module.h> | 
| Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 50 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | #include <scsi/scsi.h> | 
|  | 53 | #include <scsi/scsi_cmnd.h> | 
|  | 54 | #include <scsi/scsi_devinfo.h> | 
|  | 55 | #include <scsi/scsi_device.h> | 
|  | 56 | #include <scsi/scsi_eh.h> | 
|  | 57 |  | 
|  | 58 | #include "usb.h" | 
|  | 59 | #include "scsiglue.h" | 
|  | 60 | #include "debug.h" | 
|  | 61 | #include "transport.h" | 
|  | 62 | #include "protocol.h" | 
|  | 63 |  | 
|  | 64 | /*********************************************************************** | 
|  | 65 | * Host functions | 
|  | 66 | ***********************************************************************/ | 
|  | 67 |  | 
|  | 68 | static const char* host_info(struct Scsi_Host *host) | 
|  | 69 | { | 
|  | 70 | return "SCSI emulation for USB Mass Storage devices"; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | static int slave_alloc (struct scsi_device *sdev) | 
|  | 74 | { | 
| Alan Stern | 3a3416b | 2006-08-21 12:00:53 -0400 | [diff] [blame] | 75 | struct us_data *us = host_to_us(sdev->host); | 
|  | 76 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* | 
|  | 78 | * Set the INQUIRY transfer length to 36.  We don't use any of | 
|  | 79 | * the extra data and many devices choke if asked for more or | 
|  | 80 | * less than 36 bytes. | 
|  | 81 | */ | 
|  | 82 | sdev->inquiry_len = 36; | 
| Alan Stern | 3a3416b | 2006-08-21 12:00:53 -0400 | [diff] [blame] | 83 |  | 
|  | 84 | /* | 
|  | 85 | * The UFI spec treates the Peripheral Qualifier bits in an | 
|  | 86 | * INQUIRY result as reserved and requires devices to set them | 
|  | 87 | * to 0.  However the SCSI spec requires these bits to be set | 
|  | 88 | * to 3 to indicate when a LUN is not present. | 
|  | 89 | * | 
|  | 90 | * Let the scanning code know if this target merely sets | 
|  | 91 | * Peripheral Device Type to 0x1f to indicate no LUN. | 
|  | 92 | */ | 
|  | 93 | if (us->subclass == US_SC_UFI) | 
|  | 94 | sdev->sdev_target->pdt_1f_for_no_lun = 1; | 
|  | 95 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return 0; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | static int slave_configure(struct scsi_device *sdev) | 
|  | 100 | { | 
|  | 101 | struct us_data *us = host_to_us(sdev->host); | 
|  | 102 |  | 
|  | 103 | /* Scatter-gather buffers (all but the last) must have a length | 
|  | 104 | * divisible by the bulk maxpacket size.  Otherwise a data packet | 
|  | 105 | * would end up being short, causing a premature end to the data | 
|  | 106 | * transfer.  Since high-speed bulk pipes have a maxpacket size | 
|  | 107 | * of 512, we'll use that as the scsi device queue's DMA alignment | 
|  | 108 | * mask.  Guaranteeing proper alignment of the first buffer will | 
|  | 109 | * have the desired effect because, except at the beginning and | 
|  | 110 | * the end, scatter-gather buffers follow page boundaries. */ | 
|  | 111 | blk_queue_dma_alignment(sdev->request_queue, (512 - 1)); | 
|  | 112 |  | 
| Phil Dibowitz | 883d989 | 2006-06-24 17:27:10 -0700 | [diff] [blame] | 113 | /* Many devices have trouble transfering more than 32KB at a time, | 
|  | 114 | * while others have trouble with more than 64K. At this time we | 
|  | 115 | * are limiting both to 32K (64 sectores). | 
|  | 116 | */ | 
|  | 117 | if ((us->flags & US_FL_MAX_SECTORS_64) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | sdev->request_queue->max_sectors > 64) | 
|  | 119 | blk_queue_max_sectors(sdev->request_queue, 64); | 
|  | 120 |  | 
|  | 121 | /* We can't put these settings in slave_alloc() because that gets | 
|  | 122 | * called before the device type is known.  Consequently these | 
|  | 123 | * settings can't be overridden via the scsi devinfo mechanism. */ | 
|  | 124 | if (sdev->type == TYPE_DISK) { | 
|  | 125 |  | 
|  | 126 | /* Disk-type devices use MODE SENSE(6) if the protocol | 
|  | 127 | * (SubClass) is Transparent SCSI, otherwise they use | 
|  | 128 | * MODE SENSE(10). */ | 
|  | 129 | if (us->subclass != US_SC_SCSI) | 
|  | 130 | sdev->use_10_for_ms = 1; | 
|  | 131 |  | 
|  | 132 | /* Many disks only accept MODE SENSE transfer lengths of | 
|  | 133 | * 192 bytes (that's what Windows uses). */ | 
|  | 134 | sdev->use_192_bytes_for_3f = 1; | 
|  | 135 |  | 
|  | 136 | /* Some devices don't like MODE SENSE with page=0x3f, | 
|  | 137 | * which is the command used for checking if a device | 
|  | 138 | * is write-protected.  Now that we tell the sd driver | 
|  | 139 | * to do a 192-byte transfer with this command the | 
|  | 140 | * majority of devices work fine, but a few still can't | 
|  | 141 | * handle it.  The sd driver will simply assume those | 
|  | 142 | * devices are write-enabled. */ | 
|  | 143 | if (us->flags & US_FL_NO_WP_DETECT) | 
|  | 144 | sdev->skip_ms_page_3f = 1; | 
|  | 145 |  | 
|  | 146 | /* A number of devices have problems with MODE SENSE for | 
|  | 147 | * page x08, so we will skip it. */ | 
|  | 148 | sdev->skip_ms_page_8 = 1; | 
|  | 149 |  | 
|  | 150 | /* Some disks return the total number of blocks in response | 
|  | 151 | * to READ CAPACITY rather than the highest block number. | 
|  | 152 | * If this device makes that mistake, tell the sd driver. */ | 
|  | 153 | if (us->flags & US_FL_FIX_CAPACITY) | 
|  | 154 | sdev->fix_capacity = 1; | 
| Matthew Dharm | 86dbde9 | 2005-06-06 17:22:42 -0700 | [diff] [blame] | 155 |  | 
| Oliver Neukum | 61bf54b | 2007-02-08 09:04:48 +0100 | [diff] [blame] | 156 | /* A few disks have two indistinguishable version, one of | 
|  | 157 | * which reports the correct capacity and the other does not. | 
|  | 158 | * The sd driver has to guess which is the case. */ | 
|  | 159 | if (us->flags & US_FL_CAPACITY_HEURISTICS) | 
|  | 160 | sdev->guess_capacity = 1; | 
|  | 161 |  | 
| Matthew Dharm | a4e6283 | 2005-07-28 14:50:29 -0700 | [diff] [blame] | 162 | /* Some devices report a SCSI revision level above 2 but are | 
|  | 163 | * unable to handle the REPORT LUNS command (for which | 
|  | 164 | * support is mandatory at level 3).  Since we already have | 
|  | 165 | * a Get-Max-LUN request, we won't lose much by setting the | 
|  | 166 | * revision level down to 2.  The only devices that would be | 
|  | 167 | * affected are those with sparse LUNs. */ | 
| Alan Stern | f3f4906 | 2007-01-08 16:18:05 -0500 | [diff] [blame] | 168 | if (sdev->scsi_level > SCSI_2) | 
|  | 169 | sdev->sdev_target->scsi_level = | 
|  | 170 | sdev->scsi_level = SCSI_2; | 
| Matthew Dharm | a4e6283 | 2005-07-28 14:50:29 -0700 | [diff] [blame] | 171 |  | 
| Matthew Dharm | 86dbde9 | 2005-06-06 17:22:42 -0700 | [diff] [blame] | 172 | /* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable | 
|  | 173 | * Hardware Error) when any low-level error occurs, | 
|  | 174 | * recoverable or not.  Setting this flag tells the SCSI | 
|  | 175 | * midlayer to retry such commands, which frequently will | 
|  | 176 | * succeed and fix the error.  The worst this can lead to | 
|  | 177 | * is an occasional series of retries that will all fail. */ | 
|  | 178 | sdev->retry_hwerror = 1; | 
|  | 179 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } else { | 
|  | 181 |  | 
|  | 182 | /* Non-disk-type devices don't need to blacklist any pages | 
|  | 183 | * or to force 192-byte transfer lengths for MODE SENSE. | 
|  | 184 | * But they do need to use MODE SENSE(10). */ | 
|  | 185 | sdev->use_10_for_ms = 1; | 
|  | 186 | } | 
|  | 187 |  | 
| Alan Stern | f3f4906 | 2007-01-08 16:18:05 -0500 | [diff] [blame] | 188 | /* The CB and CBI transports have no way to pass LUN values | 
|  | 189 | * other than the bits in the second byte of a CDB.  But those | 
|  | 190 | * bits don't get set to the LUN value if the device reports | 
|  | 191 | * scsi_level == 0 (UNKNOWN).  Hence such devices must necessarily | 
|  | 192 | * be single-LUN. | 
|  | 193 | */ | 
|  | 194 | if ((us->protocol == US_PR_CB || us->protocol == US_PR_CBI) && | 
|  | 195 | sdev->scsi_level == SCSI_UNKNOWN) | 
|  | 196 | us->max_lun = 0; | 
|  | 197 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | /* Some devices choke when they receive a PREVENT-ALLOW MEDIUM | 
|  | 199 | * REMOVAL command, so suppress those commands. */ | 
|  | 200 | if (us->flags & US_FL_NOT_LOCKABLE) | 
|  | 201 | sdev->lockable = 0; | 
|  | 202 |  | 
|  | 203 | /* this is to satisfy the compiler, tho I don't think the | 
|  | 204 | * return code is ever checked anywhere. */ | 
|  | 205 | return 0; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | /* queue a command */ | 
|  | 209 | /* This is always called with scsi_lock(host) held */ | 
|  | 210 | static int queuecommand(struct scsi_cmnd *srb, | 
|  | 211 | void (*done)(struct scsi_cmnd *)) | 
|  | 212 | { | 
|  | 213 | struct us_data *us = host_to_us(srb->device->host); | 
|  | 214 |  | 
|  | 215 | US_DEBUGP("%s called\n", __FUNCTION__); | 
|  | 216 |  | 
|  | 217 | /* check for state-transition errors */ | 
|  | 218 | if (us->srb != NULL) { | 
|  | 219 | printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n", | 
|  | 220 | __FUNCTION__, us->srb); | 
|  | 221 | return SCSI_MLQUEUE_HOST_BUSY; | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | /* fail the command if we are disconnecting */ | 
|  | 225 | if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { | 
|  | 226 | US_DEBUGP("Fail command during disconnect\n"); | 
|  | 227 | srb->result = DID_NO_CONNECT << 16; | 
|  | 228 | done(srb); | 
|  | 229 | return 0; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | /* enqueue the command and wake up the control thread */ | 
|  | 233 | srb->scsi_done = done; | 
|  | 234 | us->srb = srb; | 
|  | 235 | up(&(us->sema)); | 
|  | 236 |  | 
|  | 237 | return 0; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | /*********************************************************************** | 
|  | 241 | * Error handling functions | 
|  | 242 | ***********************************************************************/ | 
|  | 243 |  | 
|  | 244 | /* Command timeout and abort */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | static int command_abort(struct scsi_cmnd *srb) | 
|  | 246 | { | 
|  | 247 | struct us_data *us = host_to_us(srb->device->host); | 
|  | 248 |  | 
|  | 249 | US_DEBUGP("%s called\n", __FUNCTION__); | 
|  | 250 |  | 
| Matthew Dharm | 226173e | 2005-08-25 20:03:50 -0700 | [diff] [blame] | 251 | /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING | 
|  | 252 | * bits are protected by the host lock. */ | 
|  | 253 | scsi_lock(us_to_host(us)); | 
|  | 254 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | /* Is this command still active? */ | 
|  | 256 | if (us->srb != srb) { | 
| Matthew Dharm | 226173e | 2005-08-25 20:03:50 -0700 | [diff] [blame] | 257 | scsi_unlock(us_to_host(us)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | US_DEBUGP ("-- nothing to abort\n"); | 
|  | 259 | return FAILED; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | /* Set the TIMED_OUT bit.  Also set the ABORTING bit, but only if | 
|  | 263 | * a device reset isn't already in progress (to avoid interfering | 
| Matthew Dharm | 226173e | 2005-08-25 20:03:50 -0700 | [diff] [blame] | 264 | * with the reset).  Note that we must retain the host lock while | 
|  | 265 | * calling usb_stor_stop_transport(); otherwise it might interfere | 
|  | 266 | * with an auto-reset that begins as soon as we release the lock. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | set_bit(US_FLIDX_TIMED_OUT, &us->flags); | 
|  | 268 | if (!test_bit(US_FLIDX_RESETTING, &us->flags)) { | 
|  | 269 | set_bit(US_FLIDX_ABORTING, &us->flags); | 
|  | 270 | usb_stor_stop_transport(us); | 
|  | 271 | } | 
| Matthew Dharm | 226173e | 2005-08-25 20:03:50 -0700 | [diff] [blame] | 272 | scsi_unlock(us_to_host(us)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 |  | 
|  | 274 | /* Wait for the aborted command to finish */ | 
|  | 275 | wait_for_completion(&us->notify); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return SUCCESS; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | /* This invokes the transport reset mechanism to reset the state of the | 
|  | 280 | * device */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | static int device_reset(struct scsi_cmnd *srb) | 
|  | 282 | { | 
|  | 283 | struct us_data *us = host_to_us(srb->device->host); | 
|  | 284 | int result; | 
|  | 285 |  | 
|  | 286 | US_DEBUGP("%s called\n", __FUNCTION__); | 
|  | 287 |  | 
| Alan Stern | 8dfe4b1 | 2007-07-06 14:24:27 -0400 | [diff] [blame] | 288 | result = usb_autopm_get_interface(us->pusb_intf); | 
|  | 289 | if (result == 0) { | 
|  | 290 |  | 
|  | 291 | /* lock the device pointers and do the reset */ | 
|  | 292 | mutex_lock(&(us->dev_mutex)); | 
|  | 293 | result = us->transport_reset(us); | 
|  | 294 | mutex_unlock(&us->dev_mutex); | 
|  | 295 | usb_autopm_put_interface(us->pusb_intf); | 
|  | 296 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 |  | 
| Matthew Dharm | 4d07ef7 | 2005-06-06 17:21:41 -0700 | [diff] [blame] | 298 | return result < 0 ? FAILED : SUCCESS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
| Matthew Dharm | 4d07ef7 | 2005-06-06 17:21:41 -0700 | [diff] [blame] | 301 | /* Simulate a SCSI bus reset by resetting the device's USB port. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | static int bus_reset(struct scsi_cmnd *srb) | 
|  | 303 | { | 
|  | 304 | struct us_data *us = host_to_us(srb->device->host); | 
| Matthew Dharm | 4d07ef7 | 2005-06-06 17:21:41 -0700 | [diff] [blame] | 305 | int result; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 |  | 
|  | 307 | US_DEBUGP("%s called\n", __FUNCTION__); | 
| Matthew Dharm | 4d07ef7 | 2005-06-06 17:21:41 -0700 | [diff] [blame] | 308 | result = usb_stor_port_reset(us); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return result < 0 ? FAILED : SUCCESS; | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | /* Report a driver-initiated device reset to the SCSI layer. | 
|  | 313 | * Calling this for a SCSI-initiated reset is unnecessary but harmless. | 
|  | 314 | * The caller must own the SCSI host lock. */ | 
|  | 315 | void usb_stor_report_device_reset(struct us_data *us) | 
|  | 316 | { | 
|  | 317 | int i; | 
|  | 318 | struct Scsi_Host *host = us_to_host(us); | 
|  | 319 |  | 
|  | 320 | scsi_report_device_reset(host, 0, 0); | 
|  | 321 | if (us->flags & US_FL_SCM_MULT_TARG) { | 
|  | 322 | for (i = 1; i < host->max_id; ++i) | 
|  | 323 | scsi_report_device_reset(host, 0, i); | 
|  | 324 | } | 
|  | 325 | } | 
|  | 326 |  | 
| Matthew Dharm | 4d07ef7 | 2005-06-06 17:21:41 -0700 | [diff] [blame] | 327 | /* Report a driver-initiated bus reset to the SCSI layer. | 
|  | 328 | * Calling this for a SCSI-initiated reset is unnecessary but harmless. | 
| Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 329 | * The caller must not own the SCSI host lock. */ | 
| Matthew Dharm | 4d07ef7 | 2005-06-06 17:21:41 -0700 | [diff] [blame] | 330 | void usb_stor_report_bus_reset(struct us_data *us) | 
|  | 331 | { | 
| Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 332 | struct Scsi_Host *host = us_to_host(us); | 
|  | 333 |  | 
|  | 334 | scsi_lock(host); | 
|  | 335 | scsi_report_bus_reset(host, 0); | 
|  | 336 | scsi_unlock(host); | 
| Matthew Dharm | 4d07ef7 | 2005-06-06 17:21:41 -0700 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | /*********************************************************************** | 
|  | 340 | * /proc/scsi/ functions | 
|  | 341 | ***********************************************************************/ | 
|  | 342 |  | 
|  | 343 | /* we use this macro to help us write into the buffer */ | 
|  | 344 | #undef SPRINTF | 
|  | 345 | #define SPRINTF(args...) \ | 
|  | 346 | do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0) | 
|  | 347 |  | 
|  | 348 | static int proc_info (struct Scsi_Host *host, char *buffer, | 
|  | 349 | char **start, off_t offset, int length, int inout) | 
|  | 350 | { | 
|  | 351 | struct us_data *us = host_to_us(host); | 
|  | 352 | char *pos = buffer; | 
|  | 353 | const char *string; | 
|  | 354 |  | 
|  | 355 | /* if someone is sending us data, just throw it away */ | 
|  | 356 | if (inout) | 
|  | 357 | return length; | 
|  | 358 |  | 
|  | 359 | /* print the controller name */ | 
|  | 360 | SPRINTF("   Host scsi%d: usb-storage\n", host->host_no); | 
|  | 361 |  | 
|  | 362 | /* print product, vendor, and serial number strings */ | 
|  | 363 | if (us->pusb_dev->manufacturer) | 
|  | 364 | string = us->pusb_dev->manufacturer; | 
|  | 365 | else if (us->unusual_dev->vendorName) | 
|  | 366 | string = us->unusual_dev->vendorName; | 
|  | 367 | else | 
|  | 368 | string = "Unknown"; | 
|  | 369 | SPRINTF("       Vendor: %s\n", string); | 
|  | 370 | if (us->pusb_dev->product) | 
|  | 371 | string = us->pusb_dev->product; | 
|  | 372 | else if (us->unusual_dev->productName) | 
|  | 373 | string = us->unusual_dev->productName; | 
|  | 374 | else | 
|  | 375 | string = "Unknown"; | 
|  | 376 | SPRINTF("      Product: %s\n", string); | 
|  | 377 | if (us->pusb_dev->serial) | 
|  | 378 | string = us->pusb_dev->serial; | 
|  | 379 | else | 
|  | 380 | string = "None"; | 
|  | 381 | SPRINTF("Serial Number: %s\n", string); | 
|  | 382 |  | 
|  | 383 | /* show the protocol and transport */ | 
|  | 384 | SPRINTF("     Protocol: %s\n", us->protocol_name); | 
|  | 385 | SPRINTF("    Transport: %s\n", us->transport_name); | 
|  | 386 |  | 
|  | 387 | /* show the device flags */ | 
|  | 388 | if (pos < buffer + length) { | 
|  | 389 | pos += sprintf(pos, "       Quirks:"); | 
|  | 390 |  | 
|  | 391 | #define US_FLAG(name, value) \ | 
|  | 392 | if (us->flags & value) pos += sprintf(pos, " " #name); | 
|  | 393 | US_DO_ALL_FLAGS | 
|  | 394 | #undef US_FLAG | 
|  | 395 |  | 
|  | 396 | *(pos++) = '\n'; | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | /* | 
|  | 400 | * Calculate start of next buffer, and return value. | 
|  | 401 | */ | 
|  | 402 | *start = buffer + offset; | 
|  | 403 |  | 
|  | 404 | if ((pos - buffer) < offset) | 
|  | 405 | return (0); | 
|  | 406 | else if ((pos - buffer - offset) < length) | 
|  | 407 | return (pos - buffer - offset); | 
|  | 408 | else | 
|  | 409 | return (length); | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 | /*********************************************************************** | 
|  | 413 | * Sysfs interface | 
|  | 414 | ***********************************************************************/ | 
|  | 415 |  | 
|  | 416 | /* Output routine for the sysfs max_sectors file */ | 
| Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 417 | static ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { | 
|  | 419 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 420 |  | 
|  | 421 | return sprintf(buf, "%u\n", sdev->request_queue->max_sectors); | 
|  | 422 | } | 
|  | 423 |  | 
|  | 424 | /* Input routine for the sysfs max_sectors file */ | 
| Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 425 | static ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | size_t count) | 
|  | 427 | { | 
|  | 428 | struct scsi_device *sdev = to_scsi_device(dev); | 
|  | 429 | unsigned short ms; | 
|  | 430 |  | 
|  | 431 | if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) { | 
|  | 432 | blk_queue_max_sectors(sdev->request_queue, ms); | 
|  | 433 | return strlen(buf); | 
|  | 434 | } | 
|  | 435 | return -EINVAL; | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors, | 
|  | 439 | store_max_sectors); | 
|  | 440 |  | 
|  | 441 | static struct device_attribute *sysfs_device_attr_list[] = { | 
|  | 442 | &dev_attr_max_sectors, | 
|  | 443 | NULL, | 
|  | 444 | }; | 
|  | 445 |  | 
|  | 446 | /* | 
|  | 447 | * this defines our host template, with which we'll allocate hosts | 
|  | 448 | */ | 
|  | 449 |  | 
|  | 450 | struct scsi_host_template usb_stor_host_template = { | 
|  | 451 | /* basic userland interface stuff */ | 
|  | 452 | .name =				"usb-storage", | 
|  | 453 | .proc_name =			"usb-storage", | 
|  | 454 | .proc_info =			proc_info, | 
|  | 455 | .info =				host_info, | 
|  | 456 |  | 
|  | 457 | /* command interface -- queued only */ | 
|  | 458 | .queuecommand =			queuecommand, | 
|  | 459 |  | 
|  | 460 | /* error and abort handlers */ | 
|  | 461 | .eh_abort_handler =		command_abort, | 
|  | 462 | .eh_device_reset_handler =	device_reset, | 
|  | 463 | .eh_bus_reset_handler =		bus_reset, | 
|  | 464 |  | 
|  | 465 | /* queue commands only, only one command per LUN */ | 
|  | 466 | .can_queue =			1, | 
|  | 467 | .cmd_per_lun =			1, | 
|  | 468 |  | 
|  | 469 | /* unknown initiator id */ | 
|  | 470 | .this_id =			-1, | 
|  | 471 |  | 
|  | 472 | .slave_alloc =			slave_alloc, | 
|  | 473 | .slave_configure =		slave_configure, | 
|  | 474 |  | 
|  | 475 | /* lots of sg segments can be handled */ | 
|  | 476 | .sg_tablesize =			SG_ALL, | 
|  | 477 |  | 
|  | 478 | /* limit the total size of a transfer to 120 KB */ | 
|  | 479 | .max_sectors =                  240, | 
|  | 480 |  | 
|  | 481 | /* merge commands... this seems to help performance, but | 
|  | 482 | * periodically someone should test to see which setting is more | 
|  | 483 | * optimal. | 
|  | 484 | */ | 
|  | 485 | .use_clustering =		1, | 
|  | 486 |  | 
|  | 487 | /* emulated HBA */ | 
|  | 488 | .emulated =			1, | 
|  | 489 |  | 
|  | 490 | /* we do our own delay after a device or bus reset */ | 
|  | 491 | .skip_settle_delay =		1, | 
|  | 492 |  | 
|  | 493 | /* sysfs device attributes */ | 
|  | 494 | .sdev_attrs =			sysfs_device_attr_list, | 
|  | 495 |  | 
|  | 496 | /* module management */ | 
|  | 497 | .module =			THIS_MODULE | 
|  | 498 | }; | 
|  | 499 |  | 
|  | 500 | /* To Report "Illegal Request: Invalid Field in CDB */ | 
|  | 501 | unsigned char usb_stor_sense_invalidCDB[18] = { | 
|  | 502 | [0]	= 0x70,			    /* current error */ | 
|  | 503 | [2]	= ILLEGAL_REQUEST,	    /* Illegal Request = 0x05 */ | 
|  | 504 | [7]	= 0x0a,			    /* additional length */ | 
|  | 505 | [12]	= 0x24			    /* Invalid Field in CDB */ | 
|  | 506 | }; | 
|  | 507 |  |