scsiglue.c revision 96983d2d861bf94b7f70bc47ac3c5b289f519a2d
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Driver for USB Mass Storage compliant devices
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * SCSI layer glue code
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Current development and maintenance by:
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Developed with the assistance of:
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Initial work by:
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   (c) 1999 Michael Gee (michael@linuxspecific.com)
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This driver is based on the 'USB Mass Storage Class' document. This
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * describes in detail the protocol used to communicate with such
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * devices.  Clearly, the designers had SCSI and ATAPI commands in
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * mind when they created this document.  The commands are all very
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * similar to commands in the SCSI-II and ATAPI specifications.
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * It is important to note that in a number of cases this class
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * exhibits class-specific exemptions from the USB specification.
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Notably the usage of NAK, STALL and ACK differs from the norm, in
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that they are used to communicate wait, failed and OK on commands.
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Also, for certain devices, the interrupt endpoint is used to convey
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * status of a command.
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * information about this driver.
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is free software; you can redistribute it and/or modify it
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * under the terms of the GNU General Public License as published by the
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Free Software Foundation; either version 2, or (at your option) any
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * later version.
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is distributed in the hope that it will be useful, but
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WITHOUT ANY WARRANTY; without even the implied warranty of
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * General Public License for more details.
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * You should have received a copy of the GNU General Public License along
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * with this program; if not, write to the Free Software Foundation, Inc.,
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 675 Mass Ave, Cambridge, MA 02139, USA.
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
484186ecf8ad16dd05759a09594de6a87e48759ba6Arjan van de Ven#include <linux/mutex.h>
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi.h>
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_cmnd.h>
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_devinfo.h>
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h>
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_eh.h>
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "usb.h"
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsiglue.h"
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "debug.h"
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "transport.h"
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "protocol.h"
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host functions
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char* host_info(struct Scsi_Host *host)
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return "SCSI emulation for USB Mass Storage devices";
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_alloc (struct scsi_device *sdev)
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
733a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	struct us_data *us = host_to_us(sdev->host);
743a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set the INQUIRY transfer length to 36.  We don't use any of
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the extra data and many devices choke if asked for more or
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * less than 36 bytes.
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sdev->inquiry_len = 36;
813a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
82f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	/* USB has unusual DMA-alignment requirements: Although the
83f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * starting address of each scatter-gather element doesn't matter,
84f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * the length of each element except the last must be divisible
85f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * by the Bulk maxpacket value.  There's currently no way to
86f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * express this by block-layer constraints, so we'll cop out
87f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * and simply require addresses to be aligned at 512-byte
88f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * boundaries.  This is okay since most block I/O involves
89f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * hardware sectors that are multiples of 512 bytes in length,
90f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * and since host controllers up through USB 2.0 have maxpacket
91f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * values no larger than 512.
92f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 *
93f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
94f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * values can be as large as 2048.  To make that work properly
95f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * will require changes to the block layer.
96148d9fe4c91a6356dae1b05b76b8133586c26be4Alan Stern	 */
97f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
98465ff3185e0cb76d46137335a4d21d0d9d3ac8a2James Bottomley
993a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	/*
1003a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * The UFI spec treates the Peripheral Qualifier bits in an
1013a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * INQUIRY result as reserved and requires devices to set them
1023a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * to 0.  However the SCSI spec requires these bits to be set
1033a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * to 3 to indicate when a LUN is not present.
1043a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 *
1053a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * Let the scanning code know if this target merely sets
1063a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * Peripheral Device Type to 0x1f to indicate no LUN.
1073a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 */
1083a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	if (us->subclass == US_SC_UFI)
1093a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern		sdev->sdev_target->pdt_1f_for_no_lun = 1;
1103a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_configure(struct scsi_device *sdev)
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(sdev->host);
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
118883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	/* Many devices have trouble transfering more than 32KB at a time,
119883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 * while others have trouble with more than 64K. At this time we
120883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 * are limiting both to 32K (64 sectores).
121883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 */
1227e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
12333abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey		unsigned int max_sectors = 64;
12433abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey
1257e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_MAX_SECTORS_MIN)
12633abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey			max_sectors = PAGE_CACHE_SIZE >> 9;
12733abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey		if (sdev->request_queue->max_sectors > max_sectors)
12833abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey			blk_queue_max_sectors(sdev->request_queue,
12933abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey					      max_sectors);
13033abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey	}
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13296983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	/* Some USB host controllers can't do DMA; they have to use PIO.
13396983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * They indicate this by setting their dma_mask to NULL.  For
13496983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * such controllers we need to make sure the block layer sets
13596983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * up bounce buffers in addressable memory.
13696983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 */
13796983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	if (!us->pusb_dev->bus->controller->dma_mask)
13896983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern		blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
13996983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* We can't put these settings in slave_alloc() because that gets
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * called before the device type is known.  Consequently these
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * settings can't be overridden via the scsi devinfo mechanism. */
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sdev->type == TYPE_DISK) {
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Disk-type devices use MODE SENSE(6) if the protocol
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * (SubClass) is Transparent SCSI, otherwise they use
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * MODE SENSE(10). */
148d277064e7e16d02e0078a6bc1820764ae00dea87Matthieu CASTET		if (us->subclass != US_SC_SCSI && us->subclass != US_SC_CYP_ATACB)
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->use_10_for_ms = 1;
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Many disks only accept MODE SENSE transfer lengths of
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * 192 bytes (that's what Windows uses). */
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->use_192_bytes_for_3f = 1;
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Some devices don't like MODE SENSE with page=0x3f,
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * which is the command used for checking if a device
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * is write-protected.  Now that we tell the sd driver
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to do a 192-byte transfer with this command the
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * majority of devices work fine, but a few still can't
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * handle it.  The sd driver will simply assume those
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * devices are write-enabled. */
1627e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_NO_WP_DETECT)
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->skip_ms_page_3f = 1;
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* A number of devices have problems with MODE SENSE for
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * page x08, so we will skip it. */
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->skip_ms_page_8 = 1;
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Some disks return the total number of blocks in response
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to READ CAPACITY rather than the highest block number.
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * If this device makes that mistake, tell the sd driver. */
1727e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_FIX_CAPACITY)
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->fix_capacity = 1;
17486dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm
17561bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		/* A few disks have two indistinguishable version, one of
17661bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		 * which reports the correct capacity and the other does not.
17761bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		 * The sd driver has to guess which is the case. */
1787e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_CAPACITY_HEURISTICS)
17961bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum			sdev->guess_capacity = 1;
18061bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum
1811537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros		/* assume SPC3 or latter devices support sense size > 18 */
1821537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros		if (sdev->scsi_level > SCSI_SPC_2)
1831537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros			us->fflags |= US_FL_SANE_SENSE;
1841537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros
185a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		/* Some devices report a SCSI revision level above 2 but are
186a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * unable to handle the REPORT LUNS command (for which
187a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * support is mandatory at level 3).  Since we already have
188a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * a Get-Max-LUN request, we won't lose much by setting the
189a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * revision level down to 2.  The only devices that would be
190a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * affected are those with sparse LUNs. */
191f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern		if (sdev->scsi_level > SCSI_2)
192f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern			sdev->sdev_target->scsi_level =
193f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern					sdev->scsi_level = SCSI_2;
194a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm
19586dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		/* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
19686dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * Hardware Error) when any low-level error occurs,
19786dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * recoverable or not.  Setting this flag tells the SCSI
19886dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * midlayer to retry such commands, which frequently will
19986dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * succeed and fix the error.  The worst this can lead to
20086dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * is an occasional series of retries that will all fail. */
20186dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		sdev->retry_hwerror = 1;
20286dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm
203f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		/* USB disks should allow restart.  Some drives spin down
204f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		 * automatically, requiring a START-STOP UNIT command. */
205f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		sdev->allow_restart = 1;
206f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab
20723c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		/* Some USB cardreaders have trouble reading an sdcard's last
20823c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		 * sector in a larger then 1 sector read, since the performance
20923c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		 * impact is negible we set this flag for all USB disks */
21023c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		sdev->last_sector_bug = 1;
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Non-disk-type devices don't need to blacklist any pages
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * or to force 192-byte transfer lengths for MODE SENSE.
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * But they do need to use MODE SENSE(10). */
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->use_10_for_ms = 1;
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
219f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	/* The CB and CBI transports have no way to pass LUN values
220f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * other than the bits in the second byte of a CDB.  But those
221f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * bits don't get set to the LUN value if the device reports
222f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * scsi_level == 0 (UNKNOWN).  Hence such devices must necessarily
223f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * be single-LUN.
224f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 */
225f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	if ((us->protocol == US_PR_CB || us->protocol == US_PR_CBI) &&
226f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern			sdev->scsi_level == SCSI_UNKNOWN)
227f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern		us->max_lun = 0;
228f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * REMOVAL command, so suppress those commands. */
2317e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & US_FL_NOT_LOCKABLE)
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->lockable = 0;
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* this is to satisfy the compiler, tho I don't think the
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * return code is ever checked anywhere. */
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* queue a command */
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This is always called with scsi_lock(host) held */
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int queuecommand(struct scsi_cmnd *srb,
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			void (*done)(struct scsi_cmnd *))
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
246441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* check for state-transition errors */
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->srb != NULL) {
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
251441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, us->srb);
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return SCSI_MLQUEUE_HOST_BUSY;
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* fail the command if we are disconnecting */
2567e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		US_DEBUGP("Fail command during disconnect\n");
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		srb->result = DID_NO_CONNECT << 16;
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		done(srb);
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* enqueue the command and wake up the control thread */
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	srb->scsi_done = done;
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	us->srb = srb;
2667119e3c37fbf7c27adb5929f344c826ecb8c7859Alan Stern	complete(&us->cmnd_ready);
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Error handling functions
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Command timeout and abort */
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int command_abort(struct scsi_cmnd *srb)
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
280441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
282226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	/* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
283226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * bits are protected by the host lock. */
284226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	scsi_lock(us_to_host(us));
285226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Is this command still active? */
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->srb != srb) {
288226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm		scsi_unlock(us_to_host(us));
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		US_DEBUGP ("-- nothing to abort\n");
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return FAILED;
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Set the TIMED_OUT bit.  Also set the ABORTING bit, but only if
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * a device reset isn't already in progress (to avoid interfering
295226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * with the reset).  Note that we must retain the host lock while
296226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * calling usb_stor_stop_transport(); otherwise it might interfere
297226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * with an auto-reset that begins as soon as we release the lock. */
2987e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
2997e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
3007e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		set_bit(US_FLIDX_ABORTING, &us->dflags);
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_stor_stop_transport(us);
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
303226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	scsi_unlock(us_to_host(us));
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Wait for the aborted command to finish */
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_for_completion(&us->notify);
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return SUCCESS;
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This invokes the transport reset mechanism to reset the state of the
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device */
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int device_reset(struct scsi_cmnd *srb)
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result;
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
317441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
319d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	/* lock the device pointers and do the reset */
320d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	mutex_lock(&(us->dev_mutex));
321d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	result = us->transport_reset(us);
322d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	mutex_unlock(&us->dev_mutex);
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3244d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	return result < 0 ? FAILED : SUCCESS;
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3274d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Simulate a SCSI bus reset by resetting the device's USB port. */
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int bus_reset(struct scsi_cmnd *srb)
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3314d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	int result;
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
333441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3344d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	result = usb_stor_port_reset(us);
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return result < 0 ? FAILED : SUCCESS;
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Report a driver-initiated device reset to the SCSI layer.
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Calling this for a SCSI-initiated reset is unnecessary but harmless.
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The caller must own the SCSI host lock. */
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid usb_stor_report_device_reset(struct us_data *us)
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *host = us_to_host(us);
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	scsi_report_device_reset(host, 0, 0);
3477e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & US_FL_SCM_MULT_TARG) {
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (i = 1; i < host->max_id; ++i)
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			scsi_report_device_reset(host, 0, i);
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3534d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Report a driver-initiated bus reset to the SCSI layer.
3544d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm * Calling this for a SCSI-initiated reset is unnecessary but harmless.
355f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern * The caller must not own the SCSI host lock. */
3564d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharmvoid usb_stor_report_bus_reset(struct us_data *us)
3574d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm{
358f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	struct Scsi_Host *host = us_to_host(us);
359f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern
360f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_lock(host);
361f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_report_bus_reset(host, 0);
362f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_unlock(host);
3634d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm}
3644d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * /proc/scsi/ functions
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* we use this macro to help us write into the buffer */
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef SPRINTF
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SPRINTF(args...) \
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int proc_info (struct Scsi_Host *host, char *buffer,
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		char **start, off_t offset, int length, int inout)
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(host);
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *pos = buffer;
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *string;
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if someone is sending us data, just throw it away */
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (inout)
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return length;
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* print the controller name */
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("   Host scsi%d: usb-storage\n", host->host_no);
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* print product, vendor, and serial number strings */
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->manufacturer)
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->manufacturer;
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (us->unusual_dev->vendorName)
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->unusual_dev->vendorName;
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "Unknown";
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("       Vendor: %s\n", string);
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->product)
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->product;
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (us->unusual_dev->productName)
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->unusual_dev->productName;
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "Unknown";
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("      Product: %s\n", string);
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->serial)
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->serial;
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "None";
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("Serial Number: %s\n", string);
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* show the protocol and transport */
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("     Protocol: %s\n", us->protocol_name);
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("    Transport: %s\n", us->transport_name);
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* show the device flags */
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (pos < buffer + length) {
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		pos += sprintf(pos, "       Quirks:");
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define US_FLAG(name, value) \
4187e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & value) pos += sprintf(pos, " " #name);
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsUS_DO_ALL_FLAGS
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef US_FLAG
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*(pos++) = '\n';
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Calculate start of next buffer, and return value.
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*start = buffer + offset;
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((pos - buffer) < offset)
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (0);
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if ((pos - buffer - offset) < length)
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (pos - buffer - offset);
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (length);
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Sysfs interface
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Output routine for the sysfs max_sectors file */
443060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_device *sdev = to_scsi_device(dev);
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", sdev->request_queue->max_sectors);
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Input routine for the sysfs max_sectors file */
451060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		size_t count)
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_device *sdev = to_scsi_device(dev);
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned short ms;
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		blk_queue_max_sectors(sdev->request_queue, ms);
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return strlen(buf);
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -EINVAL;
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		store_max_sectors);
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct device_attribute *sysfs_device_attr_list[] = {
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		&dev_attr_max_sectors,
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		NULL,
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		};
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * this defines our host template, with which we'll allocate hosts
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_host_template usb_stor_host_template = {
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* basic userland interface stuff */
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name =				"usb-storage",
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.proc_name =			"usb-storage",
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.proc_info =			proc_info,
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.info =				host_info,
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* command interface -- queued only */
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.queuecommand =			queuecommand,
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* error and abort handlers */
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_abort_handler =		command_abort,
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_device_reset_handler =	device_reset,
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_bus_reset_handler =		bus_reset,
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* queue commands only, only one command per LUN */
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.can_queue =			1,
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.cmd_per_lun =			1,
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* unknown initiator id */
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.this_id =			-1,
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.slave_alloc =			slave_alloc,
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.slave_configure =		slave_configure,
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* lots of sg segments can be handled */
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.sg_tablesize =			SG_ALL,
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* limit the total size of a transfer to 120 KB */
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.max_sectors =                  240,
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* merge commands... this seems to help performance, but
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * periodically someone should test to see which setting is more
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * optimal.
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.use_clustering =		1,
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* emulated HBA */
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.emulated =			1,
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* we do our own delay after a device or bus reset */
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.skip_settle_delay =		1,
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* sysfs device attributes */
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.sdev_attrs =			sysfs_device_attr_list,
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* module management */
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.module =			THIS_MODULE
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* To Report "Illegal Request: Invalid Field in CDB */
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsunsigned char usb_stor_sense_invalidCDB[18] = {
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[0]	= 0x70,			    /* current error */
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[2]	= ILLEGAL_REQUEST,	    /* Illegal Request = 0x05 */
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[7]	= 0x0a,			    /* additional length */
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[12]	= 0x24			    /* Invalid Field in CDB */
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
534