scsiglue.c revision 506e9469833c66ed6bb9acd902e208f7301b6adb
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
62a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern/* Vendor IDs for companies that seem to include the READ CAPACITY bug
63a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern * in all their devices
64a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern */
65a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_NOKIA		0x0421
66a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_NIKON		0x04b0
67506e9469833c66ed6bb9acd902e208f7301b6adbAlan Stern#define VENDOR_ID_PENTAX	0x0a17
68a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern#define VENDOR_ID_MOTOROLA	0x22b8
69a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Host functions
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char* host_info(struct Scsi_Host *host)
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return "SCSI emulation for USB Mass Storage devices";
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_alloc (struct scsi_device *sdev)
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
813a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	struct us_data *us = host_to_us(sdev->host);
823a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set the INQUIRY transfer length to 36.  We don't use any of
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the extra data and many devices choke if asked for more or
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * less than 36 bytes.
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sdev->inquiry_len = 36;
893a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
90f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	/* USB has unusual DMA-alignment requirements: Although the
91f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * starting address of each scatter-gather element doesn't matter,
92f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * the length of each element except the last must be divisible
93f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * by the Bulk maxpacket value.  There's currently no way to
94f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * express this by block-layer constraints, so we'll cop out
95f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * and simply require addresses to be aligned at 512-byte
96f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * boundaries.  This is okay since most block I/O involves
97f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * hardware sectors that are multiples of 512 bytes in length,
98f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * and since host controllers up through USB 2.0 have maxpacket
99f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * values no larger than 512.
100f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 *
101f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
102f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * values can be as large as 2048.  To make that work properly
103f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	 * will require changes to the block layer.
104148d9fe4c91a6356dae1b05b76b8133586c26be4Alan Stern	 */
105f756cbd458ab71c996a069cb3928fb1e2d7cd9ccAlan Stern	blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
106465ff3185e0cb76d46137335a4d21d0d9d3ac8a2James Bottomley
1073a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	/*
1083a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * The UFI spec treates the Peripheral Qualifier bits in an
1093a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * INQUIRY result as reserved and requires devices to set them
1103a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * to 0.  However the SCSI spec requires these bits to be set
1113a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * to 3 to indicate when a LUN is not present.
1123a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 *
1133a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * Let the scanning code know if this target merely sets
1143a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 * Peripheral Device Type to 0x1f to indicate no LUN.
1153a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	 */
1163a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern	if (us->subclass == US_SC_UFI)
1173a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern		sdev->sdev_target->pdt_1f_for_no_lun = 1;
1183a3416b12f1fbd607bc137a57c924a628aa5485cAlan Stern
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int slave_configure(struct scsi_device *sdev)
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(sdev->host);
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
126883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	/* Many devices have trouble transfering more than 32KB at a time,
127883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 * while others have trouble with more than 64K. At this time we
128883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 * are limiting both to 32K (64 sectores).
129883d989a7edf7a62e38e9150990b56209420e9e5Phil Dibowitz	 */
1307e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
13133abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey		unsigned int max_sectors = 64;
13233abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey
1337e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_MAX_SECTORS_MIN)
13433abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey			max_sectors = PAGE_CACHE_SIZE >> 9;
13533abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey		if (sdev->request_queue->max_sectors > max_sectors)
13633abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey			blk_queue_max_sectors(sdev->request_queue,
13733abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey					      max_sectors);
13833abc04f0420dceed0ebc2d1094019d3bb2b5c29Doug Maxey	}
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14096983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	/* Some USB host controllers can't do DMA; they have to use PIO.
14196983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * They indicate this by setting their dma_mask to NULL.  For
14296983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * such controllers we need to make sure the block layer sets
14396983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 * up bounce buffers in addressable memory.
14496983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	 */
14596983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern	if (!us->pusb_dev->bus->controller->dma_mask)
14696983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern		blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
14796983d2d861bf94b7f70bc47ac3c5b289f519a2dAlan Stern
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* We can't put these settings in slave_alloc() because that gets
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * called before the device type is known.  Consequently these
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * settings can't be overridden via the scsi devinfo mechanism. */
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sdev->type == TYPE_DISK) {
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
153a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		/* Some vendors seem to put the READ CAPACITY bug into
154a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * all their devices -- primarily makers of cell phones
155a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * and digital cameras.  Since these devices always use
156a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * flash media and can be expected to have an even number
157a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * of sectors, we will always enable the CAPACITY_HEURISTICS
158a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		 * flag unless told otherwise. */
159a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
160a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_NOKIA:
161a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_NIKON:
162506e9469833c66ed6bb9acd902e208f7301b6adbAlan Stern		case VENDOR_ID_PENTAX:
163a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		case VENDOR_ID_MOTOROLA:
164a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern			if (!(us->fflags & (US_FL_FIX_CAPACITY |
165a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern					US_FL_CAPACITY_OK)))
166a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern				us->fflags |= US_FL_CAPACITY_HEURISTICS;
167a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern			break;
168a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern		}
169a81a81a25d3ecdab777abca87c5ddf484056103dAlan Stern
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Disk-type devices use MODE SENSE(6) if the protocol
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * (SubClass) is Transparent SCSI, otherwise they use
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * MODE SENSE(10). */
173d277064e7e16d02e0078a6bc1820764ae00dea87Matthieu CASTET		if (us->subclass != US_SC_SCSI && us->subclass != US_SC_CYP_ATACB)
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->use_10_for_ms = 1;
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Many disks only accept MODE SENSE transfer lengths of
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * 192 bytes (that's what Windows uses). */
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->use_192_bytes_for_3f = 1;
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Some devices don't like MODE SENSE with page=0x3f,
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * which is the command used for checking if a device
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * is write-protected.  Now that we tell the sd driver
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to do a 192-byte transfer with this command the
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * majority of devices work fine, but a few still can't
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * handle it.  The sd driver will simply assume those
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * devices are write-enabled. */
1877e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_NO_WP_DETECT)
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->skip_ms_page_3f = 1;
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* A number of devices have problems with MODE SENSE for
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * page x08, so we will skip it. */
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->skip_ms_page_8 = 1;
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Some disks return the total number of blocks in response
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to READ CAPACITY rather than the highest block number.
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * If this device makes that mistake, tell the sd driver. */
1977e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_FIX_CAPACITY)
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sdev->fix_capacity = 1;
19986dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm
20061bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		/* A few disks have two indistinguishable version, one of
20161bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		 * which reports the correct capacity and the other does not.
20261bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum		 * The sd driver has to guess which is the case. */
2037e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		if (us->fflags & US_FL_CAPACITY_HEURISTICS)
20461bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum			sdev->guess_capacity = 1;
20561bf54b71d5abf767ee46284be19965d7253ddbfOliver Neukum
2061537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros		/* assume SPC3 or latter devices support sense size > 18 */
2071537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros		if (sdev->scsi_level > SCSI_SPC_2)
2081537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros			us->fflags |= US_FL_SANE_SENSE;
2091537e0ad944acf3a4c2b311a646d7993b89499f7Ben Efros
210a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		/* Some devices report a SCSI revision level above 2 but are
211a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * unable to handle the REPORT LUNS command (for which
212a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * support is mandatory at level 3).  Since we already have
213a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * a Get-Max-LUN request, we won't lose much by setting the
214a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * revision level down to 2.  The only devices that would be
215a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm		 * affected are those with sparse LUNs. */
216f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern		if (sdev->scsi_level > SCSI_2)
217f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern			sdev->sdev_target->scsi_level =
218f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern					sdev->scsi_level = SCSI_2;
219a4e628328ec60873fec9d506d682155391f589ceMatthew Dharm
22086dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		/* USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
22186dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * Hardware Error) when any low-level error occurs,
22286dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * recoverable or not.  Setting this flag tells the SCSI
22386dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * midlayer to retry such commands, which frequently will
22486dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * succeed and fix the error.  The worst this can lead to
22586dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		 * is an occasional series of retries that will all fail. */
22686dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm		sdev->retry_hwerror = 1;
22786dbde9cbdfe8bc2c2dfe5d33027d3acc55e0470Matthew Dharm
228f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		/* USB disks should allow restart.  Some drives spin down
229f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		 * automatically, requiring a START-STOP UNIT command. */
230f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab		sdev->allow_restart = 1;
231f09e495df27d80ae77005ddb2e93df18ec24d04aMauro Carvalho Chehab
23223c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		/* Some USB cardreaders have trouble reading an sdcard's last
23323c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		 * sector in a larger then 1 sector read, since the performance
23423c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		 * impact is negible we set this flag for all USB disks */
23523c3e290fb9ce38cabc2822b47583fc8702411bfHans de Goede		sdev->last_sector_bug = 1;
23625ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern
23725ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		/* Enable last-sector hacks for single-target devices using
23825ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		 * the Bulk-only transport, unless we already know the
23925ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		 * capacity will be decremented or is correct. */
24025ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern		if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
24125ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern					US_FL_SCM_MULT_TARG)) &&
24225ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern				us->protocol == US_PR_BULK)
24325ff1c316f6a763f1eefe7f8984b2d8c03888432Alan Stern			us->use_last_sector_hacks = 1;
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Non-disk-type devices don't need to blacklist any pages
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * or to force 192-byte transfer lengths for MODE SENSE.
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * But they do need to use MODE SENSE(10). */
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->use_10_for_ms = 1;
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
252f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	/* The CB and CBI transports have no way to pass LUN values
253f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * other than the bits in the second byte of a CDB.  But those
254f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * bits don't get set to the LUN value if the device reports
255f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * scsi_level == 0 (UNKNOWN).  Hence such devices must necessarily
256f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 * be single-LUN.
257f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	 */
258f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern	if ((us->protocol == US_PR_CB || us->protocol == US_PR_CBI) &&
259f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern			sdev->scsi_level == SCSI_UNKNOWN)
260f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern		us->max_lun = 0;
261f3f4906516a084bbd9aa3da7592e6b029fe78f5bAlan Stern
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Some devices choke when they receive a PREVENT-ALLOW MEDIUM
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * REMOVAL command, so suppress those commands. */
2647e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & US_FL_NOT_LOCKABLE)
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sdev->lockable = 0;
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* this is to satisfy the compiler, tho I don't think the
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * return code is ever checked anywhere. */
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* queue a command */
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This is always called with scsi_lock(host) held */
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int queuecommand(struct scsi_cmnd *srb,
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			void (*done)(struct scsi_cmnd *))
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
279441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* check for state-transition errors */
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->srb != NULL) {
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
284441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, us->srb);
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return SCSI_MLQUEUE_HOST_BUSY;
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* fail the command if we are disconnecting */
2897e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		US_DEBUGP("Fail command during disconnect\n");
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		srb->result = DID_NO_CONNECT << 16;
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		done(srb);
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* enqueue the command and wake up the control thread */
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	srb->scsi_done = done;
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	us->srb = srb;
2997119e3c37fbf7c27adb5929f344c826ecb8c7859Alan Stern	complete(&us->cmnd_ready);
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Error handling functions
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Command timeout and abort */
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int command_abort(struct scsi_cmnd *srb)
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
313441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
315226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	/* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
316226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * bits are protected by the host lock. */
317226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	scsi_lock(us_to_host(us));
318226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Is this command still active? */
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->srb != srb) {
321226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm		scsi_unlock(us_to_host(us));
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		US_DEBUGP ("-- nothing to abort\n");
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return FAILED;
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Set the TIMED_OUT bit.  Also set the ABORTING bit, but only if
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * a device reset isn't already in progress (to avoid interfering
328226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * with the reset).  Note that we must retain the host lock while
329226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * calling usb_stor_stop_transport(); otherwise it might interfere
330226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	 * with an auto-reset that begins as soon as we release the lock. */
3317e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
3327e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
3337e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern		set_bit(US_FLIDX_ABORTING, &us->dflags);
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_stor_stop_transport(us);
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
336226173edae1c49c68ebb723771a02302c85e3475Matthew Dharm	scsi_unlock(us_to_host(us));
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Wait for the aborted command to finish */
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_for_completion(&us->notify);
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return SUCCESS;
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This invokes the transport reset mechanism to reset the state of the
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * device */
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int device_reset(struct scsi_cmnd *srb)
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result;
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
350441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
352d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	/* lock the device pointers and do the reset */
353d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	mutex_lock(&(us->dev_mutex));
354d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	result = us->transport_reset(us);
355d526875deb42e0f4c0d31cb50b4e5bfaf19c3138Greg Kroah-Hartman	mutex_unlock(&us->dev_mutex);
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3574d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	return result < 0 ? FAILED : SUCCESS;
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3604d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Simulate a SCSI bus reset by resetting the device's USB port. */
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int bus_reset(struct scsi_cmnd *srb)
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(srb->device->host);
3644d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	int result;
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
366441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	US_DEBUGP("%s called\n", __func__);
3674d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm	result = usb_stor_port_reset(us);
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return result < 0 ? FAILED : SUCCESS;
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Report a driver-initiated device reset to the SCSI layer.
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Calling this for a SCSI-initiated reset is unnecessary but harmless.
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The caller must own the SCSI host lock. */
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid usb_stor_report_device_reset(struct us_data *us)
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct Scsi_Host *host = us_to_host(us);
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	scsi_report_device_reset(host, 0, 0);
3807e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & US_FL_SCM_MULT_TARG) {
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (i = 1; i < host->max_id; ++i)
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			scsi_report_device_reset(host, 0, i);
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3864d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm/* Report a driver-initiated bus reset to the SCSI layer.
3874d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm * Calling this for a SCSI-initiated reset is unnecessary but harmless.
388f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern * The caller must not own the SCSI host lock. */
3894d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharmvoid usb_stor_report_bus_reset(struct us_data *us)
3904d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm{
391f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	struct Scsi_Host *host = us_to_host(us);
392f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern
393f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_lock(host);
394f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_report_bus_reset(host, 0);
395f07600cf9eb3ee92777b2001e564faa413144a99Alan Stern	scsi_unlock(host);
3964d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm}
3974d07ef762fc8d6d35ecc1511a3b953a733a61a5fMatthew Dharm
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * /proc/scsi/ functions
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* we use this macro to help us write into the buffer */
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef SPRINTF
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SPRINTF(args...) \
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int proc_info (struct Scsi_Host *host, char *buffer,
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		char **start, off_t offset, int length, int inout)
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct us_data *us = host_to_us(host);
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *pos = buffer;
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *string;
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if someone is sending us data, just throw it away */
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (inout)
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return length;
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* print the controller name */
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("   Host scsi%d: usb-storage\n", host->host_no);
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* print product, vendor, and serial number strings */
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->manufacturer)
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->manufacturer;
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (us->unusual_dev->vendorName)
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->unusual_dev->vendorName;
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "Unknown";
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("       Vendor: %s\n", string);
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->product)
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->product;
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (us->unusual_dev->productName)
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->unusual_dev->productName;
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "Unknown";
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("      Product: %s\n", string);
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (us->pusb_dev->serial)
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = us->pusb_dev->serial;
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		string = "None";
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("Serial Number: %s\n", string);
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* show the protocol and transport */
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("     Protocol: %s\n", us->protocol_name);
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SPRINTF("    Transport: %s\n", us->transport_name);
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* show the device flags */
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (pos < buffer + length) {
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		pos += sprintf(pos, "       Quirks:");
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define US_FLAG(name, value) \
4517e4d6c387994294ac8198b624ee71e75de60dfd2Alan Stern	if (us->fflags & value) pos += sprintf(pos, " " #name);
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsUS_DO_ALL_FLAGS
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef US_FLAG
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*(pos++) = '\n';
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Calculate start of next buffer, and return value.
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*start = buffer + offset;
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((pos - buffer) < offset)
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (0);
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if ((pos - buffer - offset) < length)
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (pos - buffer - offset);
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return (length);
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/***********************************************************************
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Sysfs interface
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ***********************************************************************/
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Output routine for the sysfs max_sectors file */
476060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t show_max_sectors(struct device *dev, struct device_attribute *attr, char *buf)
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_device *sdev = to_scsi_device(dev);
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", sdev->request_queue->max_sectors);
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Input routine for the sysfs max_sectors file */
484060b8845e6bea938d65ad6f89e83507e5ff4fec4Yani Ioannoustatic ssize_t store_max_sectors(struct device *dev, struct device_attribute *attr, const char *buf,
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		size_t count)
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct scsi_device *sdev = to_scsi_device(dev);
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned short ms;
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		blk_queue_max_sectors(sdev->request_queue, ms);
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return strlen(buf);
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -EINVAL;
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors,
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		store_max_sectors);
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct device_attribute *sysfs_device_attr_list[] = {
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		&dev_attr_max_sectors,
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		NULL,
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		};
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * this defines our host template, with which we'll allocate hosts
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct scsi_host_template usb_stor_host_template = {
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* basic userland interface stuff */
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name =				"usb-storage",
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.proc_name =			"usb-storage",
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.proc_info =			proc_info,
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.info =				host_info,
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* command interface -- queued only */
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.queuecommand =			queuecommand,
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* error and abort handlers */
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_abort_handler =		command_abort,
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_device_reset_handler =	device_reset,
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.eh_bus_reset_handler =		bus_reset,
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* queue commands only, only one command per LUN */
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.can_queue =			1,
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.cmd_per_lun =			1,
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* unknown initiator id */
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.this_id =			-1,
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.slave_alloc =			slave_alloc,
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.slave_configure =		slave_configure,
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* lots of sg segments can be handled */
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.sg_tablesize =			SG_ALL,
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* limit the total size of a transfer to 120 KB */
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.max_sectors =                  240,
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* merge commands... this seems to help performance, but
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * periodically someone should test to see which setting is more
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * optimal.
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.use_clustering =		1,
5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* emulated HBA */
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.emulated =			1,
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* we do our own delay after a device or bus reset */
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.skip_settle_delay =		1,
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* sysfs device attributes */
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.sdev_attrs =			sysfs_device_attr_list,
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* module management */
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.module =			THIS_MODULE
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* To Report "Illegal Request: Invalid Field in CDB */
5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsunsigned char usb_stor_sense_invalidCDB[18] = {
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[0]	= 0x70,			    /* current error */
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[2]	= ILLEGAL_REQUEST,	    /* Illegal Request = 0x05 */
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[7]	= 0x0a,			    /* additional length */
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	[12]	= 0x24			    /* Invalid Field in CDB */
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
567