1 /* Driver for USB Mass Storage compliant devices
6 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
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)
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
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.
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.
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
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
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.
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.
50 #include "transport.h"
52 #include <linux/slab.h>
53 #include <linux/module.h>
56 /***********************************************************************
58 ***********************************************************************/
60 static const char* usb_storage_info(struct Scsi_Host *host)
62 return "SCSI emulation for USB Mass Storage devices";
66 /* detect a virtual adapter (always works)
67 * Synchronization: 2.4: with the io_request_lock
69 * fortunately we don't care.
71 static int usb_storage_detect(struct SHT *sht)
76 /* This is not nice at all, but how else are we to get the
78 us = (struct us_data *)sht->proc_dir;
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);
85 strcpy(sht->proc_name, local_name);
87 /* we start with no /proc directory entry */
90 /* register the host */
91 us->host = scsi_register(sht, sizeof(us));
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);
98 scsi_set_device(us->host, &iface->dev);
102 /* odd... didn't register properly. Abort and free pointers */
103 kfree(sht->proc_name);
104 sht->proc_name = NULL;
108 /* Release all resources used by the virtual host
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.
114 static int usb_storage_release(struct Scsi_Host *psh)
116 struct us_data *us = (struct us_data *)psh->hostdata[0];
118 US_DEBUGP("release() called for host %s\n", us->htmplt.name);
120 /* Kill the control threads
122 * Enqueue the command, wake up the thread, and wait for
123 * notification that it has exited.
125 US_DEBUGP("-- sending exit command to thread\n");
126 BUG_ON(atomic_read(&us->sm_state) != US_STATE_IDLE);
129 wait_for_completion(&(us->notify));
131 /* remove the pointer to the data structure we were using */
132 (struct us_data*)psh->hostdata[0] = NULL;
134 /* we always have a successful release */
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 *))
143 struct us_data *us = (struct us_data *)srb->device->host->hostdata[0];
144 int state = atomic_read(&us->sm_state);
146 US_DEBUGP("queuecommand() called\n");
147 srb->host_scribble = (unsigned char *)us;
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;
157 srb->scsi_done = done;
160 /* wake up the process task */
166 /***********************************************************************
167 * Error handling functions
168 ***********************************************************************/
171 /* This is always called with scsi_lock(srb->host) held */
172 static int usb_storage_command_abort( Scsi_Cmnd *srb )
174 struct us_data *us = (struct us_data *)srb->device->host->hostdata[0];
176 US_DEBUGP("command_abort() called\n");
178 /* Is this command still active? */
179 if (us->srb != srb) {
180 US_DEBUGP ("-- nothing to abort\n");
184 return usb_stor_abort_transport(us);
187 /* This invokes the transport reset mechanism to reset the state of the
189 /* This is always called with scsi_lock(srb->host) held */
190 static int usb_storage_device_reset( Scsi_Cmnd *srb )
192 struct us_data *us = (struct us_data *)srb->device->host->hostdata[0];
193 int state = atomic_read(&us->sm_state);
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);
203 /* set the state and release the lock */
204 atomic_set(&us->sm_state, US_STATE_RESETTING);
205 scsi_unlock(srb->device->host);
207 /* lock the device pointers */
208 down(&(us->dev_semaphore));
211 result = us->transport_reset(us);
214 up(&(us->dev_semaphore));
216 /* lock access to the state and clear it */
217 scsi_lock(srb->device->host);
218 atomic_set(&us->sm_state, US_STATE_IDLE);
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 */
227 static int usb_storage_bus_reset( Scsi_Cmnd *srb )
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];
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.
242 //FIXME: needs locking against config changes
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);
250 US_DEBUGP("cannot reset a multiinterface device. failing to reset.\n");
253 US_DEBUGP("bus_reset() complete\n");
254 scsi_lock(srb->device->host);
255 return result < 0 ? FAILED : SUCCESS;
258 /***********************************************************************
259 * /proc/scsi/ functions
260 ***********************************************************************/
262 /* we use this macro to help us write into the buffer */
264 #define SPRINTF(args...) \
265 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
267 static int usb_storage_proc_info (struct Scsi_Host *hostptr, char *buffer, char **start, off_t offset,
268 int length, int inout)
274 /* if someone is sending us data, just throw it away */
278 us = (struct us_data*)hostptr->hostdata[0];
280 /* print the controller name */
281 SPRINTF(" Host scsi%d: usb-storage\n", hostptr->host_no);
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);
288 /* show the protocol and transport */
289 SPRINTF(" Protocol: %s\n", us->protocol_name);
290 SPRINTF(" Transport: %s\n", us->transport_name);
292 /* show the device flags */
293 if (pos < buffer + length) {
294 pos += sprintf(pos, " Quirks:");
297 #define DO_FLAG(a) if (f & US_FL_##a) pos += sprintf(pos, " " #a)
302 DO_FLAG(SCM_MULT_TARG);
303 DO_FLAG(FIX_INQUIRY);
304 DO_FLAG(FIX_CAPACITY);
311 * Calculate start of next buffer, and return value.
313 *start = buffer + offset;
315 if ((pos - buffer) < offset)
317 else if ((pos - buffer - offset) < length)
318 return (pos - buffer - offset);
324 * this defines our host template, with which we'll allocate hosts
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,
333 .info = usb_storage_info,
336 /* old-style detect and release */
340 /* command interface -- queued only */
342 .queuecommand = usb_storage_queuecommand,
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,
351 /* queue commands only, only one command per LUN */
355 /* unknown initiator id */
358 /* no limit on commands */
361 /* pre- and post- device scan functions */
363 .slave_configure = NULL,
364 .slave_destroy = NULL,
366 /* lots of sg segments can be handled */
367 .sg_tablesize = SG_ALL,
369 /* use 32-bit address space for DMA */
370 .unchecked_isa_dma = FALSE,
373 /* merge commands... this seems to help performance, but
374 * periodically someone should test to see which setting is more
377 .use_clustering = TRUE,
382 /* sorry, no BIOS to help us */
385 /* module management */
386 .module = THIS_MODULE
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 */
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 */
406 #define USB_STOR_SCSI_SENSE_HDRSZ 4
407 #define USB_STOR_SCSI_SENSE_10_HDRSZ 8
409 struct usb_stor_scsi_sense_hdr
417 typedef struct usb_stor_scsi_sense_hdr Usb_Stor_Scsi_Sense_Hdr;
419 union usb_stor_scsi_sense_hdr_u
421 Usb_Stor_Scsi_Sense_Hdr hdr;
422 __u8* array[USB_STOR_SCSI_SENSE_HDRSZ];
425 typedef union usb_stor_scsi_sense_hdr_u Usb_Stor_Scsi_Sense_Hdr_u;
427 struct usb_stor_scsi_sense_hdr_10
435 __u8* blkDescLengthMSB;
436 __u8* blkDescLengthLSB;
439 typedef struct usb_stor_scsi_sense_hdr_10 Usb_Stor_Scsi_Sense_Hdr_10;
441 union usb_stor_scsi_sense_hdr_10_u
443 Usb_Stor_Scsi_Sense_Hdr_10 hdr;
444 __u8* array[USB_STOR_SCSI_SENSE_10_HDRSZ];
447 typedef union usb_stor_scsi_sense_hdr_10_u Usb_Stor_Scsi_Sense_Hdr_10_u;
449 void usb_stor_scsiSenseParseBuffer( Scsi_Cmnd* , Usb_Stor_Scsi_Sense_Hdr_u*,
450 Usb_Stor_Scsi_Sense_Hdr_10_u*, int* );
452 int usb_stor_scsiSense10to6( Scsi_Cmnd* the10 )
455 int outputBufferSize = 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;
464 US_DEBUGP("-- converting 10 byte sense data to 6 byte\n");
465 the10->cmnd[0] = the10->cmnd[0] & 0xBF;
467 /* Determine buffer locations */
468 usb_stor_scsiSenseParseBuffer( the10, &the6Locations, &the10Locations,
471 /* Work out minimum buffer to output */
472 outputBufferSize = *the10Locations.hdr.dataLengthLSB;
473 outputBufferSize += USB_STOR_SCSI_SENSE_HDRSZ;
475 /* Check to see if we need to trucate the output */
476 if ( outputBufferSize > length )
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 );
484 outputBufferSize = length;
487 if ( *the10Locations.hdr.dataLengthMSB != 0 ) /* MSB must be zero */
489 printk( KERN_WARNING USB_STORAGE
490 "Command will be truncated to fit in SENSE6 buffer.\n" );
491 *the6Locations.hdr.dataLength = 0xff;
495 *the6Locations.hdr.dataLength = *the10Locations.hdr.dataLengthLSB;
498 /* Medium type and DevSpecific parms */
499 *the6Locations.hdr.mediumType = *the10Locations.hdr.mediumType;
500 *the6Locations.hdr.devSpecParms = *the10Locations.hdr.devSpecParms;
502 /* Block descriptor length */
503 if ( *the10Locations.hdr.blkDescLengthMSB != 0 ) /* MSB must be zero */
505 printk( KERN_WARNING USB_STORAGE
506 "Command will be truncated to fit in SENSE6 buffer.\n" );
507 *the6Locations.hdr.blkDescLength = 0xff;
511 *the6Locations.hdr.blkDescLength = *the10Locations.hdr.blkDescLengthLSB;
514 if ( the10->use_sg == 0 )
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)]),
525 USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ );
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++)
533 sgLength = sg[i].length;
534 for ( j=0; j<sgLength; j++ )
536 /* get to end of header */
537 if ( element == USB_STOR_SCSI_SENSE_HDRSZ )
542 if ( element == USB_STOR_SCSI_SENSE_10_HDRSZ )
546 /* we've found both sets now, exit loops */
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) )
560 if ( sb >= the10->use_sg ||
561 si >= sg[sb].length ||
562 db >= the10->use_sg ||
563 di >= sg[db].length )
565 printk( KERN_ERR USB_STORAGE
566 "Buffer overrun averted, this shouldn't happen!\n" );
572 char *src = sg_address(sg[sb]) + si;
573 char *dst = sg_address(sg[db]) + di;
578 /* get next destination */
579 if ( sg[db].length-1 == di )
589 /* get next source */
590 if ( sg[sb].length-1 == si )
602 /* zero the remaining bytes */
603 while ( element < outputBufferSize )
606 if ( db >= the10->use_sg ||
607 di >= sg[db].length )
609 printk( KERN_ERR USB_STORAGE
610 "Buffer overrun averted, this shouldn't happen!\n" );
614 *(char*)(sg_address(sg[db])) = 0;
616 /* get next destination */
617 if ( sg[db].length-1 == di )
630 /* All done any everything was fine */
634 int usb_stor_scsiSense6to10( Scsi_Cmnd* the6 )
636 /* will be used to store part of buffer */
637 __u8 tempBuffer[USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ],
639 int outputBufferSize = 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;
648 US_DEBUGP("-- converting 6 byte sense data to 10 byte\n");
649 the6->cmnd[0] = the6->cmnd[0] | 0x40;
651 /* Determine buffer locations */
652 usb_stor_scsiSenseParseBuffer( the6, &the6Locations, &the10Locations,
655 /* Work out minimum buffer to output */
656 outputBufferSize = *the6Locations.hdr.dataLength;
657 outputBufferSize += USB_STOR_SCSI_SENSE_10_HDRSZ;
659 /* Check to see if we need to trucate the output */
660 if ( outputBufferSize > length )
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 );
668 outputBufferSize = length;
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;
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;
681 /* Medium type and DevSpecific parms */
682 *the10Locations.hdr.devSpecParms = *the6Locations.hdr.devSpecParms;
683 *the10Locations.hdr.mediumType = *the6Locations.hdr.mediumType;
686 *the10Locations.hdr.dataLengthLSB = *the6Locations.hdr.dataLength;
687 *the10Locations.hdr.dataLengthMSB = 0;
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]),
699 USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ );
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++)
707 for ( j=0; j<sg[i].length; j++ )
709 /* get to end of header */
710 if ( element == USB_STOR_SCSI_SENSE_HDRSZ )
715 if ( element == USB_STOR_SCSI_SENSE_10_HDRSZ )
719 /* we've found both sets now, exit loops */
727 /* scan through this scatterlist and figure out starting positions */
729 /* destination is the last element */
732 for ( i=the6->use_sg-1; i >= 0; i--)
734 for ( j=sg[i].length-1; j>=0; j-- )
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) )
742 /* we've found both sets now, exit loops */
749 /* Now we know where to start the copy from */
751 - (USB_STOR_SCSI_SENSE_10_HDRSZ-USB_STOR_SCSI_SENSE_HDRSZ);
752 while ( element >= USB_STOR_SCSI_SENSE_10_HDRSZ )
755 if ( ( sb <= lsb && si < lsi ) ||
756 ( db <= ldb && di < ldi ) )
758 printk( KERN_ERR USB_STORAGE
759 "Buffer overrun averted, this shouldn't happen!\n" );
765 char *src = sg_address(sg[sb]) + si;
766 char *dst = sg_address(sg[db]) + di;
771 /* get next destination */
782 /* get next source */
795 /* copy the remaining four bytes */
796 while ( element >= USB_STOR_SCSI_SENSE_HDRSZ )
799 if ( db <= ldb && di < ldi )
801 printk( KERN_ERR USB_STORAGE
802 "Buffer overrun averted, this shouldn't happen!\n" );
807 char *dst = sg_address(sg[db]) + di;
809 *dst = tempBuffer[element-USB_STOR_SCSI_SENSE_HDRSZ];
813 /* get next destination */
827 /* All done and everything was fine */
831 void usb_stor_scsiSenseParseBuffer( Scsi_Cmnd* srb, Usb_Stor_Scsi_Sense_Hdr_u* the6,
832 Usb_Stor_Scsi_Sense_Hdr_10_u* the10,
836 int i = 0, j=0, element=0;
837 struct scatterlist *sg = 0;
841 /* are we scatter-gathering? */
842 if ( srb->use_sg != 0 )
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)
848 sg = (struct scatterlist *) srb->request_buffer;
849 for (i = 0; i < srb->use_sg; i++)
851 length += sg[i].length;
852 /* We only do the inner loop for the headers */
853 if ( element < USB_STOR_SCSI_SENSE_10_HDRSZ )
855 /* scan through this scatterlist */
856 for ( j=0; j<sg[i].length; j++ )
858 if ( element < USB_STOR_SCSI_SENSE_HDRSZ )
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;
865 else if ( element < USB_STOR_SCSI_SENSE_10_HDRSZ )
867 /* only the longer headers still cares now */
868 the10->array[element] = sg_address(sg[i]) + j;
871 /* increase element counter */
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++ )
886 if ( i < USB_STOR_SCSI_SENSE_HDRSZ )
888 the6->array[i] = &(buffer[i]);
889 the10->array[i] = &(buffer[i]);
893 the10->array[i] = &(buffer[i]);
898 /* Set value of length passed in */