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