| 1 |
/* Driver for USB Mass Storage compliant devices |
| 2 |
* SCSI layer glue code |
| 3 |
* |
| 4 |
* |
| 5 |
* |
| 6 |
* Current development and maintenance by: |
| 7 |
* (c) 1999-2002 Matthew Dharm ([email protected]) |
| 8 |
* |
| 9 |
* Developed with the assistance of: |
| 10 |
* (c) 2000 David L. Brown, Jr. ([email protected]) |
| 11 |
* (c) 2000 Stephen J. Gowdy ([email protected]) |
| 12 |
* |
| 13 |
* Initial work by: |
| 14 |
* (c) 1999 Michael Gee ([email protected]) |
| 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 |
#include "scsiglue.h" |
| 48 |
#include "usb.h" |
| 49 |
#include "debug.h" |
| 50 |
#include "transport.h" |
| 51 |
|
| 52 |
#include <linux/slab.h> |
| 53 |
#include <linux/module.h> |
| 54 |
|
| 55 |
|
| 56 |
/*********************************************************************** |
| 57 |
* Host functions |
| 58 |
***********************************************************************/ |
| 59 |
|
| 60 |
static const char* usb_storage_info(struct Scsi_Host *host) |
| 61 |
{ |
| 62 |
return "SCSI emulation for USB Mass Storage devices"; |
| 63 |
} |
| 64 |
|
| 65 |
#if 0 |
| 66 |
/* detect a virtual adapter (always works) |
| 67 |
* Synchronization: 2.4: with the io_request_lock |
| 68 |
* 2.5: no locks. |
| 69 |
* fortunately we don't care. |
| 70 |
* */ |
| 71 |
static int usb_storage_detect(struct SHT *sht) |
| 72 |
{ |
| 73 |
struct us_data *us; |
| 74 |
char local_name[32]; |
| 75 |
|
| 76 |
/* This is not nice at all, but how else are we to get the |
| 77 |
* data here? */ |
| 78 |
us = (struct us_data *)sht->proc_dir; |
| 79 |
|
| 80 |
/* set up the name of our subdirectory under /proc/scsi/ */ |
| 81 |
sprintf(local_name, "usb-storage-%d", us->host_number); |
| 82 |
sht->proc_name = kmalloc (strlen(local_name) + 1, GFP_ATOMIC); |
| 83 |
if (!sht->proc_name) |
| 84 |
return 0; |
| 85 |
strcpy(sht->proc_name, local_name); |
| 86 |
|
| 87 |
/* we start with no /proc directory entry */ |
| 88 |
sht->proc_dir = NULL; |
| 89 |
|
| 90 |
/* register the host */ |
| 91 |
us->host = scsi_register(sht, sizeof(us)); |
| 92 |
if (us->host) { |
| 93 |
struct usb_interface *iface; |
| 94 |
us->host->hostdata[0] = (unsigned long)us; |
| 95 |
us->host_no = us->host->host_no; |
| 96 |
iface = usb_ifnum_to_if(us->pusb_dev, us->ifnum); |
| 97 |
if (iface) |
| 98 |
scsi_set_device(us->host, &iface->dev); |
| 99 |
return 1; |
| 100 |
} |
| 101 |
|
| 102 |
/* odd... didn't register properly. Abort and free pointers */ |
| 103 |
kfree(sht->proc_name); |
| 104 |
sht->proc_name = NULL; |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
/* Release all resources used by the virtual host |
| 109 |
* |
| 110 |
* NOTE: There is no contention here, because we're already deregistered |
| 111 |
* the driver and we're doing each virtual host in turn, not in parallel |
| 112 |
* Synchronization: BKL, no spinlock. |
| 113 |
*/ |
| 114 |
static int usb_storage_release(struct Scsi_Host *psh) |
| 115 |
{ |
| 116 |
struct us_data *us = (struct us_data *)psh->hostdata[0]; |
| 117 |
|
| 118 |
US_DEBUGP("release() called for host %s\n", us->htmplt.name); |
| 119 |
|
| 120 |
/* Kill the control threads |
| 121 |
* |
| 122 |
* Enqueue the command, wake up the thread, and wait for |
| 123 |
* notification that it has exited. |
| 124 |
*/ |
| 125 |
US_DEBUGP("-- sending exit command to thread\n"); |
| 126 |
BUG_ON(atomic_read(&us->sm_state) != US_STATE_IDLE); |
| 127 |
us->srb = NULL; |
| 128 |
up(&(us->sema)); |
| 129 |
wait_for_completion(&(us->notify)); |
| 130 |
|
| 131 |
/* remove the pointer to the data structure we were using */ |
| 132 |
(struct us_data*)psh->hostdata[0] = NULL; |
| 133 |
|
| 134 |
/* we always have a successful release */ |
| 135 |
return 0; |
| 136 |
} |
| 137 |
#endif |
| 138 |
|
| 139 |
/* queue a command */ |
| 140 |
/* This is always called with scsi_lock(srb->host) held */ |
| 141 |
static int usb_storage_queuecommand( Scsi_Cmnd *srb , void (*done)(Scsi_Cmnd *)) |
| 142 |
{ |
| 143 |
struct us_data *us = (struct us_data *)srb->device->host->hostdata[0]; |
| 144 |
int state = atomic_read(&us->sm_state); |
| 145 |
|
| 146 |
US_DEBUGP("queuecommand() called\n"); |
| 147 |
srb->host_scribble = (unsigned char *)us; |
| 148 |
|
| 149 |
/* enqueue the command */ |
| 150 |
if (state != US_STATE_IDLE || us->srb != NULL) { |
| 151 |
printk(KERN_ERR USB_STORAGE "Error in %s: " |
| 152 |
"state = %d, us->srb = %p\n", |
| 153 |
__FUNCTION__, state, us->srb); |
| 154 |
return SCSI_MLQUEUE_HOST_BUSY; |
| 155 |
} |
| 156 |
|
| 157 |
srb->scsi_done = done; |
| 158 |
us->srb = srb; |
| 159 |
|
| 160 |
/* wake up the process task */ |
| 161 |
up(&(us->sema)); |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
/*********************************************************************** |
| 167 |
* Error handling functions |
| 168 |
***********************************************************************/ |
| 169 |
|
| 170 |
/* Command abort */ |
| 171 |
/* This is always called with scsi_lock(srb->host) held */ |
| 172 |
static int usb_storage_command_abort( Scsi_Cmnd *srb ) |
| 173 |
{ |
| 174 |
struct us_data *us = (struct us_data *)srb->device->host->hostdata[0]; |
| 175 |
|
| 176 |
US_DEBUGP("command_abort() called\n"); |
| 177 |
|
| 178 |
/* Is this command still active? */ |
| 179 |
if (us->srb != srb) { |
| 180 |
US_DEBUGP ("-- nothing to abort\n"); |
| 181 |
return FAILED; |
| 182 |
} |
| 183 |
|
| 184 |
return usb_stor_abort_transport(us); |
| 185 |
} |
| 186 |
|
| 187 |
/* This invokes the transport reset mechanism to reset the state of the |
| 188 |
* device */ |
| 189 |
/* This is always called with scsi_lock(srb->host) held */ |
| 190 |
static int usb_storage_device_reset( Scsi_Cmnd *srb ) |
| 191 |
{ |
| 192 |
struct us_data *us = (struct us_data *)srb->device->host->hostdata[0]; |
| 193 |
int state = atomic_read(&us->sm_state); |
| 194 |
int result; |
| 195 |
|
| 196 |
US_DEBUGP("device_reset() called\n" ); |
| 197 |
if (state != US_STATE_IDLE) { |
| 198 |
printk(KERN_ERR USB_STORAGE "Error in %s: " |
| 199 |
"invalid state %d\n", __FUNCTION__, state); |
| 200 |
return FAILED; |
| 201 |
} |
| 202 |
|
| 203 |
/* set the state and release the lock */ |
| 204 |
atomic_set(&us->sm_state, US_STATE_RESETTING); |
| 205 |
scsi_unlock(srb->device->host); |
| 206 |
|
| 207 |
/* lock the device pointers */ |
| 208 |
down(&(us->dev_semaphore)); |
| 209 |
|
| 210 |
/* do the reset */ |
| 211 |
result = us->transport_reset(us); |
| 212 |
|
| 213 |
/* unlock */ |
| 214 |
up(&(us->dev_semaphore)); |
| 215 |
|
| 216 |
/* lock access to the state and clear it */ |
| 217 |
scsi_lock(srb->device->host); |
| 218 |
atomic_set(&us->sm_state, US_STATE_IDLE); |
| 219 |
return result; |
| 220 |
} |
| 221 |
|
| 222 |
/* This resets the device port */ |
| 223 |
/* It refuses to work if there's more than one interface in |
| 224 |
this device, so that other users are not affected. */ |
| 225 |
/* This is always called with scsi_lock(srb->host) held */ |
| 226 |
|
| 227 |
static int usb_storage_bus_reset( Scsi_Cmnd *srb ) |
| 228 |
{ |
| 229 |
struct us_data *us; |
| 230 |
int result; |
| 231 |
|
| 232 |
/* we use the usb_reset_device() function to handle this for us */ |
| 233 |
US_DEBUGP("bus_reset() called\n"); |
| 234 |
scsi_unlock(srb->device->host); |
| 235 |
us = (struct us_data *)srb->device->host->hostdata[0]; |
| 236 |
|
| 237 |
/* The USB subsystem doesn't handle synchronisation between |
| 238 |
a device's several drivers. Therefore we reset only devices |
| 239 |
with one interface which we of course own. |
| 240 |
*/ |
| 241 |
|
| 242 |
//FIXME: needs locking against config changes |
| 243 |
|
| 244 |
if ( us->pusb_dev->actconfig->desc.bNumInterfaces == 1) { |
| 245 |
/* attempt to reset the port */ |
| 246 |
result = usb_reset_device(us->pusb_dev); |
| 247 |
US_DEBUGP("usb_reset_device returns %d\n", result); |
| 248 |
} else { |
| 249 |
result = -EBUSY; |
| 250 |
US_DEBUGP("cannot reset a multiinterface device. failing to reset.\n"); |
| 251 |
} |
| 252 |
|
| 253 |
US_DEBUGP("bus_reset() complete\n"); |
| 254 |
scsi_lock(srb->device->host); |
| 255 |
return result < 0 ? FAILED : SUCCESS; |
| 256 |
} |
| 257 |
|
| 258 |
/*********************************************************************** |
| 259 |
* /proc/scsi/ functions |
| 260 |
***********************************************************************/ |
| 261 |
|
| 262 |
/* we use this macro to help us write into the buffer */ |
| 263 |
#undef SPRINTF |
| 264 |
#define SPRINTF(args...) \ |
| 265 |
do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0) |
| 266 |
|
| 267 |
static int usb_storage_proc_info (struct Scsi_Host *hostptr, char *buffer, char **start, off_t offset, |
| 268 |
int length, int inout) |
| 269 |
{ |
| 270 |
struct us_data *us; |
| 271 |
char *pos = buffer; |
| 272 |
unsigned long f; |
| 273 |
|
| 274 |
/* if someone is sending us data, just throw it away */ |
| 275 |
if (inout) |
| 276 |
return length; |
| 277 |
|
| 278 |
us = (struct us_data*)hostptr->hostdata[0]; |
| 279 |
|
| 280 |
/* print the controller name */ |
| 281 |
SPRINTF(" Host scsi%d: usb-storage\n", hostptr->host_no); |
| 282 |
|
| 283 |
/* print product, vendor, and serial number strings */ |
| 284 |
SPRINTF(" Vendor: %s\n", us->vendor); |
| 285 |
SPRINTF(" Product: %s\n", us->product); |
| 286 |
SPRINTF("Serial Number: %s\n", us->serial); |
| 287 |
|
| 288 |
/* show the protocol and transport */ |
| 289 |
SPRINTF(" Protocol: %s\n", us->protocol_name); |
| 290 |
SPRINTF(" Transport: %s\n", us->transport_name); |
| 291 |
|
| 292 |
/* show the device flags */ |
| 293 |
if (pos < buffer + length) { |
| 294 |
pos += sprintf(pos, " Quirks:"); |
| 295 |
f = us->flags; |
| 296 |
|
| 297 |
#define DO_FLAG(a) if (f & US_FL_##a) pos += sprintf(pos, " " #a) |
| 298 |
DO_FLAG(SINGLE_LUN); |
| 299 |
DO_FLAG(MODE_XLATE); |
| 300 |
DO_FLAG(START_STOP); |
| 301 |
DO_FLAG(IGNORE_SER); |
| 302 |
DO_FLAG(SCM_MULT_TARG); |
| 303 |
DO_FLAG(FIX_INQUIRY); |
| 304 |
DO_FLAG(FIX_CAPACITY); |
| 305 |
#undef DO_FLAG |
| 306 |
|
| 307 |
*(pos++) = '\n'; |
| 308 |
} |
| 309 |
|
| 310 |
/* |
| 311 |
* Calculate start of next buffer, and return value. |
| 312 |
*/ |
| 313 |
*start = buffer + offset; |
| 314 |
|
| 315 |
if ((pos - buffer) < offset) |
| 316 |
return (0); |
| 317 |
else if ((pos - buffer - offset) < length) |
| 318 |
return (pos - buffer - offset); |
| 319 |
else |
| 320 |
return (length); |
| 321 |
} |
| 322 |
|
| 323 |
/* |
| 324 |
* this defines our host template, with which we'll allocate hosts |
| 325 |
*/ |
| 326 |
|
| 327 |
struct SHT usb_stor_host_template = { |
| 328 |
/* basic userland interface stuff */ |
| 329 |
.name = "usb-storage", |
| 330 |
.proc_name = "usb-storage", |
| 331 |
.proc_info = usb_storage_proc_info, |
| 332 |
.proc_dir = NULL, |
| 333 |
.info = usb_storage_info, |
| 334 |
.ioctl = NULL, |
| 335 |
|
| 336 |
/* old-style detect and release */ |
| 337 |
.detect = NULL, |
| 338 |
.release = NULL, |
| 339 |
|
| 340 |
/* command interface -- queued only */ |
| 341 |
.command = NULL, |
| 342 |
.queuecommand = usb_storage_queuecommand, |
| 343 |
|
| 344 |
/* error and abort handlers */ |
| 345 |
.eh_abort_handler = usb_storage_command_abort, |
| 346 |
.eh_device_reset_handler = usb_storage_device_reset, |
| 347 |
.eh_bus_reset_handler = usb_storage_bus_reset, |
| 348 |
.eh_host_reset_handler = NULL, |
| 349 |
.eh_strategy_handler = NULL, |
| 350 |
|
| 351 |
/* queue commands only, only one command per LUN */ |
| 352 |
.can_queue = 1, |
| 353 |
.cmd_per_lun = 1, |
| 354 |
|
| 355 |
/* unknown initiator id */ |
| 356 |
.this_id = -1, |
| 357 |
|
| 358 |
/* no limit on commands */ |
| 359 |
.max_sectors = 0, |
| 360 |
|
| 361 |
/* pre- and post- device scan functions */ |
| 362 |
.slave_alloc = NULL, |
| 363 |
.slave_configure = NULL, |
| 364 |
.slave_destroy = NULL, |
| 365 |
|
| 366 |
/* lots of sg segments can be handled */ |
| 367 |
.sg_tablesize = SG_ALL, |
| 368 |
|
| 369 |
/* use 32-bit address space for DMA */ |
| 370 |
.unchecked_isa_dma = FALSE, |
| 371 |
.highmem_io = FALSE, |
| 372 |
|
| 373 |
/* merge commands... this seems to help performance, but |
| 374 |
* periodically someone should test to see which setting is more |
| 375 |
* optimal. |
| 376 |
*/ |
| 377 |
.use_clustering = TRUE, |
| 378 |
|
| 379 |
/* emulated HBA */ |
| 380 |
.emulated = TRUE, |
| 381 |
|
| 382 |
/* sorry, no BIOS to help us */ |
| 383 |
.bios_param = NULL, |
| 384 |
|
| 385 |
/* module management */ |
| 386 |
.module = THIS_MODULE |
| 387 |
}; |
| 388 |
|
| 389 |
/* For a device that is "Not Ready" */ |
| 390 |
unsigned char usb_stor_sense_notready[18] = { |
| 391 |
[0] = 0x70, /* current error */ |
| 392 |
[2] = 0x02, /* not ready */ |
| 393 |
[7] = 0x0a, /* additional length */ |
| 394 |
[12] = 0x04, /* not ready */ |
| 395 |
[13] = 0x03 /* manual intervention */ |
| 396 |
}; |
| 397 |
|
| 398 |
/* To Report "Illegal Request: Invalid Field in CDB */ |
| 399 |
unsigned char usb_stor_sense_invalidCDB[18] = { |
| 400 |
[0] = 0x70, /* current error */ |
| 401 |
[2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */ |
| 402 |
[7] = 0x0a, /* additional length */ |
| 403 |
[12] = 0x24 /* Invalid Field in CDB */ |
| 404 |
}; |
| 405 |
|
| 406 |
#define USB_STOR_SCSI_SENSE_HDRSZ 4 |
| 407 |
#define USB_STOR_SCSI_SENSE_10_HDRSZ 8 |
| 408 |
|
| 409 |
struct usb_stor_scsi_sense_hdr |
| 410 |
{ |
| 411 |
__u8* dataLength; |
| 412 |
__u8* mediumType; |
| 413 |
__u8* devSpecParms; |
| 414 |
__u8* blkDescLength; |
| 415 |
}; |
| 416 |
|
| 417 |
typedef struct usb_stor_scsi_sense_hdr Usb_Stor_Scsi_Sense_Hdr; |
| 418 |
|
| 419 |
union usb_stor_scsi_sense_hdr_u |
| 420 |
{ |
| 421 |
Usb_Stor_Scsi_Sense_Hdr hdr; |
| 422 |
__u8* array[USB_STOR_SCSI_SENSE_HDRSZ]; |
| 423 |
}; |
| 424 |
|
| 425 |
typedef union usb_stor_scsi_sense_hdr_u Usb_Stor_Scsi_Sense_Hdr_u; |
| 426 |
|
| 427 |
struct usb_stor_scsi_sense_hdr_10 |
| 428 |
{ |
| 429 |
__u8* dataLengthMSB; |
| 430 |
__u8* dataLengthLSB; |
| 431 |
__u8* mediumType; |
| 432 |
__u8* devSpecParms; |
| 433 |
__u8* reserved1; |
| 434 |
__u8* reserved2; |
| 435 |
__u8* blkDescLengthMSB; |
| 436 |
__u8* blkDescLengthLSB; |
| 437 |
}; |
| 438 |
|
| 439 |
typedef struct usb_stor_scsi_sense_hdr_10 Usb_Stor_Scsi_Sense_Hdr_10; |
| 440 |
|
| 441 |
union usb_stor_scsi_sense_hdr_10_u |
| 442 |
{ |
| 443 |
Usb_Stor_Scsi_Sense_Hdr_10 hdr; |
| 444 |
__u8* array[USB_STOR_SCSI_SENSE_10_HDRSZ]; |
| 445 |
}; |
| 446 |
|
| 447 |
typedef union usb_stor_scsi_sense_hdr_10_u Usb_Stor_Scsi_Sense_Hdr_10_u; |
| 448 |
|
| 449 |
void usb_stor_scsiSenseParseBuffer( Scsi_Cmnd* , Usb_Stor_Scsi_Sense_Hdr_u*, |
| 450 |
Usb_Stor_Scsi_Sense_Hdr_10_u*, int* ); |
| 451 |
|
| 452 |
int usb_stor_scsiSense10to6( Scsi_Cmnd* the10 ) |
| 453 |
{ |
| 454 |
__u8 *buffer=0; |
| 455 |
int outputBufferSize = 0; |
| 456 |
int length=0; |
| 457 |
struct scatterlist *sg = 0; |
| 458 |
int i=0, j=0, element=0; |
| 459 |
Usb_Stor_Scsi_Sense_Hdr_u the6Locations; |
| 460 |
Usb_Stor_Scsi_Sense_Hdr_10_u the10Locations; |
| 461 |
int sb=0,si=0,db=0,di=0; |
| 462 |
int sgLength=0; |
| 463 |
|
| 464 |
US_DEBUGP("-- converting 10 byte sense data to 6 byte\n"); |
| 465 |
the10->cmnd[0] = the10->cmnd[0] & 0xBF; |
| 466 |
|
| 467 |
/* Determine buffer locations */ |
| 468 |
usb_stor_scsiSenseParseBuffer( the10, &the6Locations, &the10Locations, |
| 469 |
&length ); |
| 470 |
|
| 471 |
/* Work out minimum buffer to output */ |
| 472 |
outputBufferSize = *the10Locations.hdr.dataLengthLSB; |
| 473 |
outputBufferSize += USB_STOR_SCSI_SENSE_HDRSZ; |
| 474 |
|
| 475 |
/* Check to see if we need to truncate the output */ |
| 476 |
if ( outputBufferSize > length ) |
| 477 |
{ |
| 478 |
printk( KERN_WARNING USB_STORAGE |
| 479 |
"Had to truncate MODE_SENSE_10 buffer into MODE_SENSE.\n" ); |
| 480 |
printk( KERN_WARNING USB_STORAGE |
| 481 |
"outputBufferSize is %d and length is %d.\n", |
| 482 |
outputBufferSize, length ); |
| 483 |
} |
| 484 |
outputBufferSize = length; |
| 485 |
|
| 486 |
/* Data length */ |
| 487 |
if ( *the10Locations.hdr.dataLengthMSB != 0 ) /* MSB must be zero */ |
| 488 |
{ |
| 489 |
printk( KERN_WARNING USB_STORAGE |
| 490 |
"Command will be truncated to fit in SENSE6 buffer.\n" ); |
| 491 |
*the6Locations.hdr.dataLength = 0xff; |
| 492 |
} |
| 493 |
else |
| 494 |
{ |
| 495 |
*the6Locations.hdr.dataLength = *the10Locations.hdr.dataLengthLSB; |
| 496 |
} |
| 497 |
|
| 498 |
/* Medium type and DevSpecific parms */ |
| 499 |
*the6Locations.hdr.mediumType = *the10Locations.hdr.mediumType; |
| 500 |
*the6Locations.hdr.devSpecParms = *the10Locations.hdr.devSpecParms; |
| 501 |
|
| 502 |
/* Block descriptor length */ |
| 503 |
if ( *the10Locations.hdr.blkDescLengthMSB != 0 ) /* MSB must be zero */ |
| 504 |
{ |
| 505 |
printk( KERN_WARNING USB_STORAGE |
| 506 |
"Command will be truncated to fit in SENSE6 buffer.\n" ); |
| 507 |
*the6Locations.hdr.blkDescLength = 0xff; |
| 508 |
} |
| 509 |
else |
| 510 |
{ |
| 511 |
*the6Locations.hdr.blkDescLength = *the10Locations.hdr.blkDescLengthLSB; |
| 512 |
} |
| 513 |
|
| 514 |
if ( the10->use_sg == 0 ) |
| 515 |
{ |
| 516 |
buffer = the10->request_buffer; |
| 517 |
/* Copy the rest of the data */ |
| 518 |
memmove( &(buffer[USB_STOR_SCSI_SENSE_HDRSZ]), |
| 519 |
&(buffer[USB_STOR_SCSI_SENSE_10_HDRSZ]), |
| 520 |
outputBufferSize - USB_STOR_SCSI_SENSE_HDRSZ ); |
| 521 |
/* initialise last bytes left in buffer due to smaller header */ |
| 522 |
memset( &(buffer[outputBufferSize |
| 523 |
-(USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ)]), |
| 524 |
0, |
| 525 |
USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ ); |
| 526 |
} |
| 527 |
else |
| 528 |
{ |
| 529 |
sg = (struct scatterlist *) the10->request_buffer; |
| 530 |
/* scan through this scatterlist and figure out starting positions */ |
| 531 |
for ( i=0; i < the10->use_sg; i++) |
| 532 |
{ |
| 533 |
sgLength = sg[i].length; |
| 534 |
for ( j=0; j<sgLength; j++ ) |
| 535 |
{ |
| 536 |
/* get to end of header */ |
| 537 |
if ( element == USB_STOR_SCSI_SENSE_HDRSZ ) |
| 538 |
{ |
| 539 |
db=i; |
| 540 |
di=j; |
| 541 |
} |
| 542 |
if ( element == USB_STOR_SCSI_SENSE_10_HDRSZ ) |
| 543 |
{ |
| 544 |
sb=i; |
| 545 |
si=j; |
| 546 |
/* we've found both sets now, exit loops */ |
| 547 |
j=sgLength; |
| 548 |
i=the10->use_sg; |
| 549 |
} |
| 550 |
element++; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
/* Now we know where to start the copy from */ |
| 555 |
element = USB_STOR_SCSI_SENSE_HDRSZ; |
| 556 |
while ( element < outputBufferSize |
| 557 |
-(USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ) ) |
| 558 |
{ |
| 559 |
/* check limits */ |
| 560 |
if ( sb >= the10->use_sg || |
| 561 |
si >= sg[sb].length || |
| 562 |
db >= the10->use_sg || |
| 563 |
di >= sg[db].length ) |
| 564 |
{ |
| 565 |
printk( KERN_ERR USB_STORAGE |
| 566 |
"Buffer overrun averted, this shouldn't happen!\n" ); |
| 567 |
break; |
| 568 |
} |
| 569 |
|
| 570 |
/* copy one byte */ |
| 571 |
{ |
| 572 |
char *src = sg_address(sg[sb]) + si; |
| 573 |
char *dst = sg_address(sg[db]) + di; |
| 574 |
|
| 575 |
*dst = *src; |
| 576 |
} |
| 577 |
|
| 578 |
/* get next destination */ |
| 579 |
if ( sg[db].length-1 == di ) |
| 580 |
{ |
| 581 |
db++; |
| 582 |
di=0; |
| 583 |
} |
| 584 |
else |
| 585 |
{ |
| 586 |
di++; |
| 587 |
} |
| 588 |
|
| 589 |
/* get next source */ |
| 590 |
if ( sg[sb].length-1 == si ) |
| 591 |
{ |
| 592 |
sb++; |
| 593 |
si=0; |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
si++; |
| 598 |
} |
| 599 |
|
| 600 |
element++; |
| 601 |
} |
| 602 |
/* zero the remaining bytes */ |
| 603 |
while ( element < outputBufferSize ) |
| 604 |
{ |
| 605 |
/* check limits */ |
| 606 |
if ( db >= the10->use_sg || |
| 607 |
di >= sg[db].length ) |
| 608 |
{ |
| 609 |
printk( KERN_ERR USB_STORAGE |
| 610 |
"Buffer overrun averted, this shouldn't happen!\n" ); |
| 611 |
break; |
| 612 |
} |
| 613 |
|
| 614 |
*(char*)(sg_address(sg[db])) = 0; |
| 615 |
|
| 616 |
/* get next destination */ |
| 617 |
if ( sg[db].length-1 == di ) |
| 618 |
{ |
| 619 |
db++; |
| 620 |
di=0; |
| 621 |
} |
| 622 |
else |
| 623 |
{ |
| 624 |
di++; |
| 625 |
} |
| 626 |
element++; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
/* All done any everything was fine */ |
| 631 |
return 0; |
| 632 |
} |
| 633 |
|
| 634 |
int usb_stor_scsiSense6to10( Scsi_Cmnd* the6 ) |
| 635 |
{ |
| 636 |
/* will be used to store part of buffer */ |
| 637 |
__u8 tempBuffer[USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ], |
| 638 |
*buffer=0; |
| 639 |
int outputBufferSize = 0; |
| 640 |
int length=0; |
| 641 |
struct scatterlist *sg = 0; |
| 642 |
int i=0, j=0, element=0; |
| 643 |
Usb_Stor_Scsi_Sense_Hdr_u the6Locations; |
| 644 |
Usb_Stor_Scsi_Sense_Hdr_10_u the10Locations; |
| 645 |
int sb=0,si=0,db=0,di=0; |
| 646 |
int lsb=0,lsi=0,ldb=0,ldi=0; |
| 647 |
|
| 648 |
US_DEBUGP("-- converting 6 byte sense data to 10 byte\n"); |
| 649 |
the6->cmnd[0] = the6->cmnd[0] | 0x40; |
| 650 |
|
| 651 |
/* Determine buffer locations */ |
| 652 |
usb_stor_scsiSenseParseBuffer( the6, &the6Locations, &the10Locations, |
| 653 |
&length ); |
| 654 |
|
| 655 |
/* Work out minimum buffer to output */ |
| 656 |
outputBufferSize = *the6Locations.hdr.dataLength; |
| 657 |
outputBufferSize += USB_STOR_SCSI_SENSE_10_HDRSZ; |
| 658 |
|
| 659 |
/* Check to see if we need to truncate the output */ |
| 660 |
if ( outputBufferSize > length ) |
| 661 |
{ |
| 662 |
printk( KERN_WARNING USB_STORAGE |
| 663 |
"Had to truncate MODE_SENSE into MODE_SENSE_10 buffer.\n" ); |
| 664 |
printk( KERN_WARNING USB_STORAGE |
| 665 |
"outputBufferSize is %d and length is %d.\n", |
| 666 |
outputBufferSize, length ); |
| 667 |
} |
| 668 |
outputBufferSize = length; |
| 669 |
|
| 670 |
/* Block descriptor length - save these before overwriting */ |
| 671 |
tempBuffer[2] = *the10Locations.hdr.blkDescLengthMSB; |
| 672 |
tempBuffer[3] = *the10Locations.hdr.blkDescLengthLSB; |
| 673 |
*the10Locations.hdr.blkDescLengthLSB = *the6Locations.hdr.blkDescLength; |
| 674 |
*the10Locations.hdr.blkDescLengthMSB = 0; |
| 675 |
|
| 676 |
/* reserved - save these before overwriting */ |
| 677 |
tempBuffer[0] = *the10Locations.hdr.reserved1; |
| 678 |
tempBuffer[1] = *the10Locations.hdr.reserved2; |
| 679 |
*the10Locations.hdr.reserved1 = *the10Locations.hdr.reserved2 = 0; |
| 680 |
|
| 681 |
/* Medium type and DevSpecific parms */ |
| 682 |
*the10Locations.hdr.devSpecParms = *the6Locations.hdr.devSpecParms; |
| 683 |
*the10Locations.hdr.mediumType = *the6Locations.hdr.mediumType; |
| 684 |
|
| 685 |
/* Data length */ |
| 686 |
*the10Locations.hdr.dataLengthLSB = *the6Locations.hdr.dataLength; |
| 687 |
*the10Locations.hdr.dataLengthMSB = 0; |
| 688 |
|
| 689 |
if ( !the6->use_sg ) |
| 690 |
{ |
| 691 |
buffer = the6->request_buffer; |
| 692 |
/* Copy the rest of the data */ |
| 693 |
memmove( &(buffer[USB_STOR_SCSI_SENSE_10_HDRSZ]), |
| 694 |
&(buffer[USB_STOR_SCSI_SENSE_HDRSZ]), |
| 695 |
outputBufferSize-USB_STOR_SCSI_SENSE_10_HDRSZ ); |
| 696 |
/* Put the first four bytes (after header) in place */ |
| 697 |
memcpy( &(buffer[USB_STOR_SCSI_SENSE_10_HDRSZ]), |
| 698 |
tempBuffer, |
| 699 |
USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ ); |
| 700 |
} |
| 701 |
else |
| 702 |
{ |
| 703 |
sg = (struct scatterlist *) the6->request_buffer; |
| 704 |
/* scan through this scatterlist and figure out ending positions */ |
| 705 |
for ( i=0; i < the6->use_sg; i++) |
| 706 |
{ |
| 707 |
for ( j=0; j<sg[i].length; j++ ) |
| 708 |
{ |
| 709 |
/* get to end of header */ |
| 710 |
if ( element == USB_STOR_SCSI_SENSE_HDRSZ ) |
| 711 |
{ |
| 712 |
ldb=i; |
| 713 |
ldi=j; |
| 714 |
} |
| 715 |
if ( element == USB_STOR_SCSI_SENSE_10_HDRSZ ) |
| 716 |
{ |
| 717 |
lsb=i; |
| 718 |
lsi=j; |
| 719 |
/* we've found both sets now, exit loops */ |
| 720 |
j=sg[i].length; |
| 721 |
i=the6->use_sg; |
| 722 |
break; |
| 723 |
} |
| 724 |
element++; |
| 725 |
} |
| 726 |
} |
| 727 |
/* scan through this scatterlist and figure out starting positions */ |
| 728 |
element = length-1; |
| 729 |
/* destination is the last element */ |
| 730 |
db=the6->use_sg-1; |
| 731 |
di=sg[db].length-1; |
| 732 |
for ( i=the6->use_sg-1; i >= 0; i--) |
| 733 |
{ |
| 734 |
for ( j=sg[i].length-1; j>=0; j-- ) |
| 735 |
{ |
| 736 |
/* get to end of header and find source for copy */ |
| 737 |
if ( element == length - 1 |
| 738 |
- (USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ) ) |
| 739 |
{ |
| 740 |
sb=i; |
| 741 |
si=j; |
| 742 |
/* we've found both sets now, exit loops */ |
| 743 |
j=-1; |
| 744 |
i=-1; |
| 745 |
} |
| 746 |
element--; |
| 747 |
} |
| 748 |
} |
| 749 |
/* Now we know where to start the copy from */ |
| 750 |
element = length-1 |
| 751 |
- (USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ); |
| 752 |
while ( element >= USB_STOR_SCSI_SENSE_10_HDRSZ ) |
| 753 |
{ |
| 754 |
/* check limits */ |
| 755 |
if ( ( sb <= lsb && si < lsi ) || |
| 756 |
( db <= ldb && di < ldi ) ) |
| 757 |
{ |
| 758 |
printk( KERN_ERR USB_STORAGE |
| 759 |
"Buffer overrun averted, this shouldn't happen!\n" ); |
| 760 |
break; |
| 761 |
} |
| 762 |
|
| 763 |
/* copy one byte */ |
| 764 |
{ |
| 765 |
char *src = sg_address(sg[sb]) + si; |
| 766 |
char *dst = sg_address(sg[db]) + di; |
| 767 |
|
| 768 |
*dst = *src; |
| 769 |
} |
| 770 |
|
| 771 |
/* get next destination */ |
| 772 |
if ( di == 0 ) |
| 773 |
{ |
| 774 |
db--; |
| 775 |
di=sg[db].length-1; |
| 776 |
} |
| 777 |
else |
| 778 |
{ |
| 779 |
di--; |
| 780 |
} |
| 781 |
|
| 782 |
/* get next source */ |
| 783 |
if ( si == 0 ) |
| 784 |
{ |
| 785 |
sb--; |
| 786 |
si=sg[sb].length-1; |
| 787 |
} |
| 788 |
else |
| 789 |
{ |
| 790 |
si--; |
| 791 |
} |
| 792 |
|
| 793 |
element--; |
| 794 |
} |
| 795 |
/* copy the remaining four bytes */ |
| 796 |
while ( element >= USB_STOR_SCSI_SENSE_HDRSZ ) |
| 797 |
{ |
| 798 |
/* check limits */ |
| 799 |
if ( db <= ldb && di < ldi ) |
| 800 |
{ |
| 801 |
printk( KERN_ERR USB_STORAGE |
| 802 |
"Buffer overrun averted, this shouldn't happen!\n" ); |
| 803 |
break; |
| 804 |
} |
| 805 |
|
| 806 |
{ |
| 807 |
char *dst = sg_address(sg[db]) + di; |
| 808 |
|
| 809 |
*dst = tempBuffer[element-USB_STOR_SCSI_SENSE_HDRSZ]; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
/* get next destination */ |
| 814 |
if ( di == 0 ) |
| 815 |
{ |
| 816 |
db--; |
| 817 |
di=sg[db].length-1; |
| 818 |
} |
| 819 |
else |
| 820 |
{ |
| 821 |
di--; |
| 822 |
} |
| 823 |
element--; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
/* All done and everything was fine */ |
| 828 |
return 0; |
| 829 |
} |
| 830 |
|
| 831 |
void usb_stor_scsiSenseParseBuffer( Scsi_Cmnd* srb, Usb_Stor_Scsi_Sense_Hdr_u* the6, |
| 832 |
Usb_Stor_Scsi_Sense_Hdr_10_u* the10, |
| 833 |
int* length_p ) |
| 834 |
|
| 835 |
{ |
| 836 |
int i = 0, j=0, element=0; |
| 837 |
struct scatterlist *sg = 0; |
| 838 |
int length = 0; |
| 839 |
__u8* buffer=0; |
| 840 |
|
| 841 |
/* are we scatter-gathering? */ |
| 842 |
if ( srb->use_sg != 0 ) |
| 843 |
{ |
| 844 |
/* loop over all the scatter gather structures and |
| 845 |
* get pointer to the data members in the headers |
| 846 |
* (also work out the length while we're here) |
| 847 |
*/ |
| 848 |
sg = (struct scatterlist *) srb->request_buffer; |
| 849 |
for (i = 0; i < srb->use_sg; i++) |
| 850 |
{ |
| 851 |
length += sg[i].length; |
| 852 |
/* We only do the inner loop for the headers */ |
| 853 |
if ( element < USB_STOR_SCSI_SENSE_10_HDRSZ ) |
| 854 |
{ |
| 855 |
/* scan through this scatterlist */ |
| 856 |
for ( j=0; j<sg[i].length; j++ ) |
| 857 |
{ |
| 858 |
if ( element < USB_STOR_SCSI_SENSE_HDRSZ ) |
| 859 |
{ |
| 860 |
/* fill in the pointers for both header types */ |
| 861 |
the6->array[element] = sg_address(sg[i]) + j; |
| 862 |
the10->array[element] = sg_address(sg[i]) + j; |
| 863 |
|
| 864 |
} |
| 865 |
else if ( element < USB_STOR_SCSI_SENSE_10_HDRSZ ) |
| 866 |
{ |
| 867 |
/* only the longer headers still cares now */ |
| 868 |
the10->array[element] = sg_address(sg[i]) + j; |
| 869 |
|
| 870 |
} |
| 871 |
/* increase element counter */ |
| 872 |
element++; |
| 873 |
} |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
else |
| 878 |
{ |
| 879 |
length = srb->request_bufflen; |
| 880 |
buffer = srb->request_buffer; |
| 881 |
if ( length < USB_STOR_SCSI_SENSE_10_HDRSZ ) |
| 882 |
printk( KERN_ERR USB_STORAGE |
| 883 |
"Buffer length smaller than header!!" ); |
| 884 |
for( i=0; i<USB_STOR_SCSI_SENSE_10_HDRSZ; i++ ) |
| 885 |
{ |
| 886 |
if ( i < USB_STOR_SCSI_SENSE_HDRSZ ) |
| 887 |
{ |
| 888 |
the6->array[i] = &(buffer[i]); |
| 889 |
the10->array[i] = &(buffer[i]); |
| 890 |
} |
| 891 |
else |
| 892 |
{ |
| 893 |
the10->array[i] = &(buffer[i]); |
| 894 |
} |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
/* Set value of length passed in */ |
| 899 |
*length_p = length; |
| 900 |
} |
| 901 |
|