usb.c 29.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/* Driver for USB Mass Storage compliant devices
 *
 * Current development and maintenance by:
 *   (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
 *
 * Developed with the assistance of:
 *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
 *   (c) 2003 Alan Stern (stern@rowland.harvard.edu)
 *
 * Initial work by:
 *   (c) 1999 Michael Gee (michael@linuxspecific.com)
 *
 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
 *   (c) 2000 Yggdrasil Computing, Inc.
 *
 * This driver is based on the 'USB Mass Storage Class' document. This
 * describes in detail the protocol used to communicate with such
 * devices.  Clearly, the designers had SCSI and ATAPI commands in
 * mind when they created this document.  The commands are all very
 * similar to commands in the SCSI-II and ATAPI specifications.
 *
 * It is important to note that in a number of cases this class
 * exhibits class-specific exemptions from the USB specification.
 * Notably the usage of NAK, STALL and ACK differs from the norm, in
 * that they are used to communicate wait, failed and OK on commands.
 *
 * Also, for certain devices, the interrupt endpoint is used to convey
 * status of a command.
 *
 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
 * information about this driver.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/sched.h>
#include <linux/errno.h>
50
#include <linux/freezer.h>
L
Linus Torvalds 已提交
51 52 53
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
54
#include <linux/kthread.h>
55
#include <linux/mutex.h>
56
#include <linux/utsname.h>
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>

#include "usb.h"
#include "scsiglue.h"
#include "transport.h"
#include "protocol.h"
#include "debug.h"
#include "initializers.h"

#ifdef CONFIG_USB_STORAGE_USBAT
#include "shuttle_usbat.h"
#endif
#ifdef CONFIG_USB_STORAGE_SDDR09
#include "sddr09.h"
#endif
#ifdef CONFIG_USB_STORAGE_SDDR55
#include "sddr55.h"
#endif
#ifdef CONFIG_USB_STORAGE_DPCM
#include "dpcm.h"
#endif
#ifdef CONFIG_USB_STORAGE_FREECOM
#include "freecom.h"
#endif
#ifdef CONFIG_USB_STORAGE_ISD200
#include "isd200.h"
#endif
#ifdef CONFIG_USB_STORAGE_DATAFAB
#include "datafab.h"
#endif
#ifdef CONFIG_USB_STORAGE_JUMPSHOT
#include "jumpshot.h"
#endif
93 94 95
#ifdef CONFIG_USB_STORAGE_ONETOUCH
#include "onetouch.h"
#endif
96 97 98
#ifdef CONFIG_USB_STORAGE_ALAUDA
#include "alauda.h"
#endif
99 100 101
#ifdef CONFIG_USB_STORAGE_KARMA
#include "karma.h"
#endif
102 103 104
#ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
#include "cypress_atacb.h"
#endif
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114 115

/* Some informational data */
MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
MODULE_LICENSE("GPL");

static unsigned int delay_use = 5;
module_param(delay_use, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");


116 117 118
/*
 * The entries in this table correspond, line for line,
 * with the entries of us_unusual_dev_list[].
L
Linus Torvalds 已提交
119
 */
120
#ifndef CONFIG_USB_LIBUSUAL
L
Linus Torvalds 已提交
121 122 123 124

#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
		    vendorName, productName,useProtocol, useTransport, \
		    initFunction, flags) \
125 126 127 128 129 130
{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
  .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }

#define USUAL_DEV(useProto, useTrans, useType) \
{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
  .driver_info = (USB_US_TYPE_STOR<<24) }
L
Linus Torvalds 已提交
131 132 133 134 135

static struct usb_device_id storage_usb_ids [] = {

#	include "unusual_devs.h"
#undef UNUSUAL_DEV
136
#undef USUAL_DEV
L
Linus Torvalds 已提交
137 138 139 140 141
	/* Terminating entry */
	{ }
};

MODULE_DEVICE_TABLE (usb, storage_usb_ids);
142
#endif /* CONFIG_USB_LIBUSUAL */
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

/* This is the list of devices we recognize, along with their flag data */

/* The vendor name should be kept at eight characters or less, and
 * the product name should be kept at 16 characters or less. If a device
 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
 * normally generated by a device thorugh the INQUIRY response will be
 * taken from this list, and this is the reason for the above size
 * restriction. However, if the flag is not present, then you
 * are free to use as many characters as you like.
 */

#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
		    vendor_name, product_name, use_protocol, use_transport, \
		    init_function, Flags) \
{ \
	.vendorName = vendor_name,	\
	.productName = product_name,	\
	.useProtocol = use_protocol,	\
	.useTransport = use_transport,	\
	.initFunction = init_function,	\
164 165 166 167 168 169
}

#define USUAL_DEV(use_protocol, use_transport, use_type) \
{ \
	.useProtocol = use_protocol,	\
	.useTransport = use_transport,	\
L
Linus Torvalds 已提交
170 171 172 173 174
}

static struct us_unusual_dev us_unusual_dev_list[] = {
#	include "unusual_devs.h" 
#	undef UNUSUAL_DEV
175
#	undef USUAL_DEV
L
Linus Torvalds 已提交
176 177 178 179 180

	/* Terminating entry */
	{ NULL }
};

181 182 183 184 185 186 187 188

#ifdef CONFIG_PM	/* Minimal support for suspend and resume */

static int storage_suspend(struct usb_interface *iface, pm_message_t message)
{
	struct us_data *us = usb_get_intfdata(iface);

	/* Wait until no command is running */
189
	mutex_lock(&us->dev_mutex);
190

191
	US_DEBUGP("%s\n", __func__);
192 193
	if (us->suspend_resume_hook)
		(us->suspend_resume_hook)(us, US_SUSPEND);
194

195 196 197
	/* When runtime PM is working, we'll set a flag to indicate
	 * whether we should autoresume when a SCSI request arrives. */

198
	mutex_unlock(&us->dev_mutex);
199 200 201 202 203 204 205
	return 0;
}

static int storage_resume(struct usb_interface *iface)
{
	struct us_data *us = usb_get_intfdata(iface);

206
	mutex_lock(&us->dev_mutex);
A
Alan Stern 已提交
207

208
	US_DEBUGP("%s\n", __func__);
209 210
	if (us->suspend_resume_hook)
		(us->suspend_resume_hook)(us, US_RESUME);
211

212
	mutex_unlock(&us->dev_mutex);
213 214 215
	return 0;
}

A
Alan Stern 已提交
216 217 218 219
static int storage_reset_resume(struct usb_interface *iface)
{
	struct us_data *us = usb_get_intfdata(iface);

220
	US_DEBUGP("%s\n", __func__);
A
Alan Stern 已提交
221 222 223 224 225 226 227 228 229

	/* Report the reset to the SCSI core */
	usb_stor_report_bus_reset(us);

	/* FIXME: Notify the subdrivers that they need to reinitialize
	 * the device */
	return 0;
}

230
#endif /* CONFIG_PM */
L
Linus Torvalds 已提交
231

232 233 234 235 236
/*
 * The next two routines get called just before and just after
 * a USB port reset, whether from this driver or a different one.
 */

A
Alan Stern 已提交
237
static int storage_pre_reset(struct usb_interface *iface)
238 239 240
{
	struct us_data *us = usb_get_intfdata(iface);

241
	US_DEBUGP("%s\n", __func__);
242 243 244

	/* Make sure no command runs during the reset */
	mutex_lock(&us->dev_mutex);
A
Alan Stern 已提交
245
	return 0;
246 247
}

A
Alan Stern 已提交
248
static int storage_post_reset(struct usb_interface *iface)
249 250 251
{
	struct us_data *us = usb_get_intfdata(iface);

252
	US_DEBUGP("%s\n", __func__);
253 254 255 256 257 258

	/* Report the reset to the SCSI core */
	usb_stor_report_bus_reset(us);

	/* FIXME: Notify the subdrivers that they need to reinitialize
	 * the device */
A
Alan Stern 已提交
259

A
Alan Stern 已提交
260 261
	mutex_unlock(&us->dev_mutex);
	return 0;
262 263
}

L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
/*
 * fill_inquiry_response takes an unsigned char array (which must
 * be at least 36 characters) and populates the vendor name,
 * product name, and revision fields. Then the array is copied
 * into the SCSI command's response buffer (oddly enough
 * called request_buffer). data_len contains the length of the
 * data array, which again must be at least 36.
 */

void fill_inquiry_response(struct us_data *us, unsigned char *data,
		unsigned int data_len)
{
	if (data_len<36) // You lose.
		return;

	if(data[0]&0x20) { /* USB device currently not connected. Return
			      peripheral qualifier 001b ("...however, the
			      physical device is not currently connected
			      to this logical unit") and leave vendor and
			      product identification empty. ("If the target
			      does store some of the INQUIRY data on the
			      device, it may return zeros or ASCII spaces 
			      (20h) in those fields until the data is
			      available from the device."). */
		memset(data+8,0,28);
	} else {
		u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
		memcpy(data+8, us->unusual_dev->vendorName, 
			strlen(us->unusual_dev->vendorName) > 8 ? 8 :
			strlen(us->unusual_dev->vendorName));
		memcpy(data+16, us->unusual_dev->productName, 
			strlen(us->unusual_dev->productName) > 16 ? 16 :
			strlen(us->unusual_dev->productName));
		data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
		data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
		data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
		data[35] = 0x30 + ((bcdDevice) & 0x0F);
	}

	usb_stor_set_xfer_buf(data, data_len, us->srb);
}

static int usb_stor_control_thread(void * __us)
{
	struct us_data *us = (struct us_data *)__us;
	struct Scsi_Host *host = us_to_host(us);

	for(;;) {
		US_DEBUGP("*** thread sleeping.\n");
313
		if (wait_for_completion_interruptible(&us->cmnd_ready))
L
Linus Torvalds 已提交
314
			break;
315

L
Linus Torvalds 已提交
316 317 318
		US_DEBUGP("*** thread awakened.\n");

		/* lock the device pointers */
319
		mutex_lock(&(us->dev_mutex));
L
Linus Torvalds 已提交
320

321 322 323 324 325 326
		/* lock access to the state */
		scsi_lock(host);

		/* When we are called with no command pending, we're done */
		if (us->srb == NULL) {
			scsi_unlock(host);
327
			mutex_unlock(&us->dev_mutex);
328
			US_DEBUGP("-- exiting\n");
L
Linus Torvalds 已提交
329 330 331 332
			break;
		}

		/* has the command timed out *already* ? */
333
		if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
			us->srb->result = DID_ABORT << 16;
			goto SkipForAbort;
		}

		scsi_unlock(host);

		/* reject the command if the direction indicator 
		 * is UNKNOWN
		 */
		if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
			US_DEBUGP("UNKNOWN data direction\n");
			us->srb->result = DID_ERROR << 16;
		}

		/* reject if target != 0 or if LUN is higher than
		 * the maximum known LUN
		 */
		else if (us->srb->device->id && 
352
				!(us->fflags & US_FL_SCM_MULT_TARG)) {
L
Linus Torvalds 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366
			US_DEBUGP("Bad target number (%d:%d)\n",
				  us->srb->device->id, us->srb->device->lun);
			us->srb->result = DID_BAD_TARGET << 16;
		}

		else if (us->srb->device->lun > us->max_lun) {
			US_DEBUGP("Bad LUN (%d:%d)\n",
				  us->srb->device->id, us->srb->device->lun);
			us->srb->result = DID_BAD_TARGET << 16;
		}

		/* Handle those devices which need us to fake 
		 * their inquiry data */
		else if ((us->srb->cmnd[0] == INQUIRY) &&
367
			    (us->fflags & US_FL_FIX_INQUIRY)) {
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
			unsigned char data_ptr[36] = {
			    0x00, 0x80, 0x02, 0x02,
			    0x1F, 0x00, 0x00, 0x00};

			US_DEBUGP("Faking INQUIRY command\n");
			fill_inquiry_response(us, data_ptr, 36);
			us->srb->result = SAM_STAT_GOOD;
		}

		/* we've got a command, let's do it! */
		else {
			US_DEBUG(usb_stor_show_command(us->srb));
			us->proto_handler(us->srb, us);
		}

		/* lock access to the state */
		scsi_lock(host);

		/* indicate that the command is done */
387
		if (us->srb->result != DID_ABORT << 16) {
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398
			US_DEBUGP("scsi cmd done, result=0x%x\n", 
				   us->srb->result);
			us->srb->scsi_done(us->srb);
		} else {
SkipForAbort:
			US_DEBUGP("scsi command aborted\n");
		}

		/* If an abort request was received we need to signal that
		 * the abort has finished.  The proper test for this is
		 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
399 400
		 * the timeout might have occurred after the command had
		 * already completed with a different result code. */
401
		if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
L
Linus Torvalds 已提交
402 403
			complete(&(us->notify));

404
			/* Allow USB transfers to resume */
405 406
			clear_bit(US_FLIDX_ABORTING, &us->dflags);
			clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
407 408
		}

L
Linus Torvalds 已提交
409 410 411 412 413
		/* finished working on this command */
		us->srb = NULL;
		scsi_unlock(host);

		/* unlock the device pointers */
414
		mutex_unlock(&us->dev_mutex);
L
Linus Torvalds 已提交
415 416
	} /* for (;;) */

417 418 419 420 421 422 423 424 425
	/* Wait until we are told to stop */
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (kthread_should_stop())
			break;
		schedule();
	}
	__set_current_state(TASK_RUNNING);
	return 0;
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434
}	

/***********************************************************************
 * Device probing and disconnecting
 ***********************************************************************/

/* Associate our private data with the USB device */
static int associate_dev(struct us_data *us, struct usb_interface *intf)
{
435
	US_DEBUGP("-- %s\n", __func__);
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

	/* Fill in the device-related fields */
	us->pusb_dev = interface_to_usbdev(intf);
	us->pusb_intf = intf;
	us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
	US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
			le16_to_cpu(us->pusb_dev->descriptor.idVendor),
			le16_to_cpu(us->pusb_dev->descriptor.idProduct),
			le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
	US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
			intf->cur_altsetting->desc.bInterfaceSubClass,
			intf->cur_altsetting->desc.bInterfaceProtocol);

	/* Store our private data in the interface */
	usb_set_intfdata(intf, us);

	/* Allocate the device-related DMA-mapped buffers */
	us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
			GFP_KERNEL, &us->cr_dma);
	if (!us->cr) {
		US_DEBUGP("usb_ctrlrequest allocation failed\n");
		return -ENOMEM;
	}

	us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
			GFP_KERNEL, &us->iobuf_dma);
	if (!us->iobuf) {
		US_DEBUGP("I/O buffer allocation failed\n");
		return -ENOMEM;
	}
466 467 468 469 470 471

	us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
	if (!us->sensebuf) {
		US_DEBUGP("Sense buffer allocation failed\n");
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
472 473 474
	return 0;
}

475 476 477 478 479 480 481
/* Find an unusual_dev descriptor (always succeeds in the current code) */
static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
{
	const int id_index = id - storage_usb_ids;
	return &us_unusual_dev_list[id_index];
}

L
Linus Torvalds 已提交
482
/* Get the unusual_devs entries and the string descriptors */
483
static int get_device_info(struct us_data *us, const struct usb_device_id *id)
L
Linus Torvalds 已提交
484 485 486 487
{
	struct usb_device *dev = us->pusb_dev;
	struct usb_interface_descriptor *idesc =
		&us->pusb_intf->cur_altsetting->desc;
488
	struct us_unusual_dev *unusual_dev = find_unusual(id);
L
Linus Torvalds 已提交
489 490 491 492 493 494 495 496 497

	/* Store the entries */
	us->unusual_dev = unusual_dev;
	us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
			idesc->bInterfaceSubClass :
			unusual_dev->useProtocol;
	us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
			idesc->bInterfaceProtocol :
			unusual_dev->useTransport;
498
	us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
L
Linus Torvalds 已提交
499

500
	if (us->fflags & US_FL_IGNORE_DEVICE) {
501 502 503 504
		printk(KERN_INFO USB_STORAGE "device ignored\n");
		return -ENODEV;
	}

L
Linus Torvalds 已提交
505 506 507 508 509
	/*
	 * This flag is only needed when we're in high-speed, so let's
	 * disable it if we're in full-speed
	 */
	if (dev->speed != USB_SPEED_HIGH)
510
		us->fflags &= ~US_FL_GO_SLOW;
L
Linus Torvalds 已提交
511 512 513 514 515 516 517

	/* Log a message if a non-generic unusual_dev entry contains an
	 * unnecessary subclass or protocol override.  This may stimulate
	 * reports from users that will help us remove unneeded entries
	 * from the unusual_devs.h table.
	 */
	if (id->idVendor || id->idProduct) {
518
		static const char *msgs[3] = {
L
Linus Torvalds 已提交
519 520 521 522 523 524 525 526 527 528 529 530
			"an unneeded SubClass entry",
			"an unneeded Protocol entry",
			"unneeded SubClass and Protocol entries"};
		struct usb_device_descriptor *ddesc = &dev->descriptor;
		int msg = -1;

		if (unusual_dev->useProtocol != US_SC_DEVICE &&
			us->subclass == idesc->bInterfaceSubClass)
			msg += 1;
		if (unusual_dev->useTransport != US_PR_DEVICE &&
			us->protocol == idesc->bInterfaceProtocol)
			msg += 2;
531
		if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
L
Linus Torvalds 已提交
532 533
			printk(KERN_NOTICE USB_STORAGE "This device "
				"(%04x,%04x,%04x S %02x P %02x)"
534 535
				" has %s in unusual_devs.h (kernel"
				" %s)\n"
L
Linus Torvalds 已提交
536
				"   Please send a copy of this message to "
537 538
				"<linux-usb@vger.kernel.org> and "
				"<usb-storage@lists.one-eyed-alien.net>\n",
L
Linus Torvalds 已提交
539 540 541 542 543
				le16_to_cpu(ddesc->idVendor),
				le16_to_cpu(ddesc->idProduct),
				le16_to_cpu(ddesc->bcdDevice),
				idesc->bInterfaceSubClass,
				idesc->bInterfaceProtocol,
544
				msgs[msg],
545
				utsname()->release);
L
Linus Torvalds 已提交
546
	}
547 548

	return 0;
L
Linus Torvalds 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
}

/* Get the transport settings */
static int get_transport(struct us_data *us)
{
	switch (us->protocol) {
	case US_PR_CB:
		us->transport_name = "Control/Bulk";
		us->transport = usb_stor_CB_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 7;
		break;

	case US_PR_CBI:
		us->transport_name = "Control/Bulk/Interrupt";
		us->transport = usb_stor_CBI_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 7;
		break;

	case US_PR_BULK:
		us->transport_name = "Bulk";
		us->transport = usb_stor_Bulk_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		break;

#ifdef CONFIG_USB_STORAGE_USBAT
576 577
	case US_PR_USBAT:
		us->transport_name = "Shuttle USBAT";
L
Linus Torvalds 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
		us->transport = usbat_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 1;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_SDDR09
	case US_PR_EUSB_SDDR09:
		us->transport_name = "EUSB/SDDR09";
		us->transport = sddr09_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 0;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_SDDR55
	case US_PR_SDDR55:
		us->transport_name = "SDDR55";
		us->transport = sddr55_transport;
		us->transport_reset = sddr55_reset;
		us->max_lun = 0;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_DPCM
	case US_PR_DPCM_USB:
		us->transport_name = "Control/Bulk-EUSB/SDDR09";
		us->transport = dpcm_transport;
		us->transport_reset = usb_stor_CB_reset;
		us->max_lun = 1;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_FREECOM
	case US_PR_FREECOM:
		us->transport_name = "Freecom";
		us->transport = freecom_transport;
		us->transport_reset = usb_stor_freecom_reset;
		us->max_lun = 0;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_DATAFAB
	case US_PR_DATAFAB:
		us->transport_name  = "Datafab Bulk-Only";
		us->transport = datafab_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		us->max_lun = 1;
		break;
#endif

#ifdef CONFIG_USB_STORAGE_JUMPSHOT
	case US_PR_JUMPSHOT:
		us->transport_name  = "Lexar Jumpshot Control/Bulk";
		us->transport = jumpshot_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		us->max_lun = 1;
		break;
#endif

638 639 640 641 642 643 644 645 646
#ifdef CONFIG_USB_STORAGE_ALAUDA
	case US_PR_ALAUDA:
		us->transport_name  = "Alauda Control/Bulk";
		us->transport = alauda_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		us->max_lun = 1;
		break;
#endif

647 648 649 650 651 652 653 654
#ifdef CONFIG_USB_STORAGE_KARMA
	case US_PR_KARMA:
		us->transport_name = "Rio Karma/Bulk";
		us->transport = rio_karma_transport;
		us->transport_reset = usb_stor_Bulk_reset;
		break;
#endif

L
Linus Torvalds 已提交
655 656 657 658 659 660
	default:
		return -EIO;
	}
	US_DEBUGP("Transport: %s\n", us->transport_name);

	/* fix for single-lun devices */
661
	if (us->fflags & US_FL_SINGLE_LUN)
L
Linus Torvalds 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
		us->max_lun = 0;
	return 0;
}

/* Get the protocol settings */
static int get_protocol(struct us_data *us)
{
	switch (us->subclass) {
	case US_SC_RBC:
		us->protocol_name = "Reduced Block Commands (RBC)";
		us->proto_handler = usb_stor_transparent_scsi_command;
		break;

	case US_SC_8020:
		us->protocol_name = "8020i";
		us->proto_handler = usb_stor_ATAPI_command;
		us->max_lun = 0;
		break;

	case US_SC_QIC:
		us->protocol_name = "QIC-157";
		us->proto_handler = usb_stor_qic157_command;
		us->max_lun = 0;
		break;

	case US_SC_8070:
		us->protocol_name = "8070i";
		us->proto_handler = usb_stor_ATAPI_command;
		us->max_lun = 0;
		break;

	case US_SC_SCSI:
		us->protocol_name = "Transparent SCSI";
		us->proto_handler = usb_stor_transparent_scsi_command;
		break;

	case US_SC_UFI:
		us->protocol_name = "Uniform Floppy Interface (UFI)";
		us->proto_handler = usb_stor_ufi_command;
		break;

#ifdef CONFIG_USB_STORAGE_ISD200
	case US_SC_ISD200:
		us->protocol_name = "ISD200 ATA/ATAPI";
		us->proto_handler = isd200_ata_command;
		break;
#endif

710 711 712 713 714 715 716
#ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
	case US_SC_CYP_ATACB:
		us->protocol_name = "Transparent SCSI with Cypress ATACB";
		us->proto_handler = cypress_atacb_passthrough;
		break;
#endif

L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	default:
		return -EIO;
	}
	US_DEBUGP("Protocol: %s\n", us->protocol_name);
	return 0;
}

/* Get the pipe settings */
static int get_pipes(struct us_data *us)
{
	struct usb_host_interface *altsetting =
		us->pusb_intf->cur_altsetting;
	int i;
	struct usb_endpoint_descriptor *ep;
	struct usb_endpoint_descriptor *ep_in = NULL;
	struct usb_endpoint_descriptor *ep_out = NULL;
	struct usb_endpoint_descriptor *ep_int = NULL;

	/*
736
	 * Find the first endpoint of each type we need.
L
Linus Torvalds 已提交
737
	 * We are expecting a minimum of 2 endpoints - in and out (bulk).
738
	 * An optional interrupt-in is OK (necessary for CBI protocol).
L
Linus Torvalds 已提交
739 740 741 742 743
	 * We will ignore any others.
	 */
	for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
		ep = &altsetting->endpoint[i].desc;

744
		if (usb_endpoint_xfer_bulk(ep)) {
745 746 747 748 749 750 751
			if (usb_endpoint_dir_in(ep)) {
				if (!ep_in)
					ep_in = ep;
			} else {
				if (!ep_out)
					ep_out = ep;
			}
L
Linus Torvalds 已提交
752 753
		}

754 755 756
		else if (usb_endpoint_is_int_in(ep)) {
			if (!ep_int)
				ep_int = ep;
L
Linus Torvalds 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
		}
	}

	if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
		US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
		return -EIO;
	}

	/* Calculate and store the pipe values */
	us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
	us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
	us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
		ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
	us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev, 
		ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
	if (ep_int) {
		us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
			ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
		us->ep_bInterval = ep_int->bInterval;
	}
	return 0;
}

/* Initialize all the dynamic resources we need */
static int usb_stor_acquire_resources(struct us_data *us)
{
	int p;
784
	struct task_struct *th;
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792 793

	us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!us->current_urb) {
		US_DEBUGP("URB allocation failed\n");
		return -ENOMEM;
	}

	/* Just before we start our control thread, initialize
	 * the device if it needs initialization */
794 795 796 797 798
	if (us->unusual_dev->initFunction) {
		p = us->unusual_dev->initFunction(us);
		if (p)
			return p;
	}
L
Linus Torvalds 已提交
799 800

	/* Start up our control thread */
801
	th = kthread_run(usb_stor_control_thread, us, "usb-storage");
802
	if (IS_ERR(th)) {
L
Linus Torvalds 已提交
803 804
		printk(KERN_WARNING USB_STORAGE 
		       "Unable to start control thread\n");
805
		return PTR_ERR(th);
L
Linus Torvalds 已提交
806
	}
807
	us->ctl_thread = th;
L
Linus Torvalds 已提交
808 809 810 811 812 813 814

	return 0;
}

/* Release all our dynamic resources */
static void usb_stor_release_resources(struct us_data *us)
{
815
	US_DEBUGP("-- %s\n", __func__);
L
Linus Torvalds 已提交
816 817

	/* Tell the control thread to exit.  The SCSI host must
818 819
	 * already have been removed and the DISCONNECTING flag set
	 * so that we won't accept any more commands.
L
Linus Torvalds 已提交
820 821
	 */
	US_DEBUGP("-- sending exit command to thread\n");
822
	complete(&us->cmnd_ready);
823 824
	if (us->ctl_thread)
		kthread_stop(us->ctl_thread);
L
Linus Torvalds 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

	/* Call the destructor routine, if it exists */
	if (us->extra_destructor) {
		US_DEBUGP("-- calling extra_destructor()\n");
		us->extra_destructor(us->extra);
	}

	/* Free the extra data and the URB */
	kfree(us->extra);
	usb_free_urb(us->current_urb);
}

/* Dissociate from the USB device */
static void dissociate_dev(struct us_data *us)
{
840
	US_DEBUGP("-- %s\n", __func__);
L
Linus Torvalds 已提交
841

842 843
	kfree(us->sensebuf);

L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852 853 854 855
	/* Free the device-related DMA-mapped buffers */
	if (us->cr)
		usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
				us->cr_dma);
	if (us->iobuf)
		usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
				us->iobuf_dma);

	/* Remove our private data from the interface */
	usb_set_intfdata(us->pusb_intf, NULL);
}

856 857 858
/* First stage of disconnect processing: stop SCSI scanning,
 * remove the host, and stop accepting new commands
 */
859 860
static void quiesce_and_remove_host(struct us_data *us)
{
861 862
	struct Scsi_Host *host = us_to_host(us);

863 864 865
	/* If the device is really gone, cut short reset delays */
	if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
		set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
866

867 868 869 870 871 872
	/* Prevent SCSI-scanning (if it hasn't started yet)
	 * and wait for the SCSI-scanning thread to stop.
	 */
	set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
	wake_up(&us->delay_wait);
	wait_for_completion(&us->scanning_done);
873

874 875 876
	/* Removing the host will perform an orderly shutdown: caches
	 * synchronized, disks spun down, etc.
	 */
877
	scsi_remove_host(host);
878

879 880 881 882 883 884 885
	/* Prevent any new commands from being accepted and cut short
	 * reset delays.
	 */
	scsi_lock(host);
	set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
	scsi_unlock(host);
	wake_up(&us->delay_wait);
886 887 888 889 890 891 892 893 894 895 896 897 898
}

/* Second stage of disconnect processing: deallocate all resources */
static void release_everything(struct us_data *us)
{
	usb_stor_release_resources(us);
	dissociate_dev(us);

	/* Drop our reference to the host; the SCSI core will free it
	 * (and "us" along with it) when the refcount becomes 0. */
	scsi_host_put(us_to_host(us));
}

L
Linus Torvalds 已提交
899 900 901 902 903 904 905 906
/* Thread to carry out delayed SCSI-device scanning */
static int usb_stor_scan_thread(void * __us)
{
	struct us_data *us = (struct us_data *)__us;

	printk(KERN_DEBUG
		"usb-storage: device found at %d\n", us->pusb_dev->devnum);

907
	set_freezable();
L
Linus Torvalds 已提交
908 909 910 911
	/* Wait for the timeout to expire or for a disconnect */
	if (delay_use > 0) {
		printk(KERN_DEBUG "usb-storage: waiting for device "
				"to settle before scanning\n");
912
		wait_event_freezable_timeout(us->delay_wait,
913
				test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
L
Linus Torvalds 已提交
914 915 916 917
				delay_use * HZ);
	}

	/* If the device is still connected, perform the scanning */
918
	if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
919 920 921

		/* For bulk-only devices, determine the max LUN value */
		if (us->protocol == US_PR_BULK &&
922
				!(us->fflags & US_FL_SINGLE_LUN)) {
923
			mutex_lock(&us->dev_mutex);
924
			us->max_lun = usb_stor_Bulk_max_lun(us);
925
			mutex_unlock(&us->dev_mutex);
926
		}
L
Linus Torvalds 已提交
927 928 929 930 931 932
		scsi_scan_host(us_to_host(us));
		printk(KERN_DEBUG "usb-storage: device scan complete\n");

		/* Should we unbind if no devices were detected? */
	}

933
	complete_and_exit(&us->scanning_done, 0);
L
Linus Torvalds 已提交
934 935 936 937 938 939 940 941 942 943
}


/* Probe to see if we can drive a newly-connected USB device */
static int storage_probe(struct usb_interface *intf,
			 const struct usb_device_id *id)
{
	struct Scsi_Host *host;
	struct us_data *us;
	int result;
944
	struct task_struct *th;
L
Linus Torvalds 已提交
945

946 947 948
	if (usb_usual_check_type(id, USB_US_TYPE_STOR))
		return -ENXIO;

L
Linus Torvalds 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961
	US_DEBUGP("USB Mass Storage device detected\n");

	/*
	 * Ask the SCSI layer to allocate a host structure, with extra
	 * space at the end for our private us_data structure.
	 */
	host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
	if (!host) {
		printk(KERN_WARNING USB_STORAGE
			"Unable to allocate the scsi host\n");
		return -ENOMEM;
	}

962 963 964 965
	/*
	 * Allow 16-byte CDBs and thus > 2TB
	 */
	host->max_cmd_len = 16;
L
Linus Torvalds 已提交
966 967
	us = host_to_us(host);
	memset(us, 0, sizeof(struct us_data));
968
	mutex_init(&(us->dev_mutex));
969
	init_completion(&us->cmnd_ready);
L
Linus Torvalds 已提交
970 971
	init_completion(&(us->notify));
	init_waitqueue_head(&us->delay_wait);
972
	init_completion(&us->scanning_done);
L
Linus Torvalds 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985

	/* Associate the us_data structure with the USB device */
	result = associate_dev(us, intf);
	if (result)
		goto BadDevice;

	/*
	 * Get the unusual_devs entries and the descriptors
	 *
	 * id_index is calculated in the declaration to be the index number
	 * of the match from the usb_device_id table, so we can find the
	 * corresponding entry in the private table.
	 */
986 987 988
	result = get_device_info(us, id);
	if (result)
		goto BadDevice;
L
Linus Torvalds 已提交
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

	/* Get the transport, protocol, and pipe settings */
	result = get_transport(us);
	if (result)
		goto BadDevice;
	result = get_protocol(us);
	if (result)
		goto BadDevice;
	result = get_pipes(us);
	if (result)
		goto BadDevice;

	/* Acquire all the other resources and add the host */
	result = usb_stor_acquire_resources(us);
	if (result)
		goto BadDevice;
	result = scsi_add_host(host, &intf->dev);
	if (result) {
		printk(KERN_WARNING USB_STORAGE
			"Unable to add the scsi host\n");
		goto BadDevice;
	}

	/* Start up the thread for delayed SCSI-device scanning */
1013 1014
	th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
	if (IS_ERR(th)) {
L
Linus Torvalds 已提交
1015 1016
		printk(KERN_WARNING USB_STORAGE 
		       "Unable to start the device-scanning thread\n");
1017
		complete(&us->scanning_done);
1018
		quiesce_and_remove_host(us);
1019
		result = PTR_ERR(th);
L
Linus Torvalds 已提交
1020 1021 1022
		goto BadDevice;
	}

1023
	wake_up_process(th);
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029

	return 0;

	/* We come here if there are any problems */
BadDevice:
	US_DEBUGP("storage_probe() failed\n");
1030
	release_everything(us);
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039
	return result;
}

/* Handle a disconnect event from the USB core */
static void storage_disconnect(struct usb_interface *intf)
{
	struct us_data *us = usb_get_intfdata(intf);

	US_DEBUGP("storage_disconnect() called\n");
1040 1041
	quiesce_and_remove_host(us);
	release_everything(us);
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047
}

/***********************************************************************
 * Initialization and registration
 ***********************************************************************/

1048 1049 1050 1051 1052 1053 1054
static struct usb_driver usb_storage_driver = {
	.name =		"usb-storage",
	.probe =	storage_probe,
	.disconnect =	storage_disconnect,
#ifdef CONFIG_PM
	.suspend =	storage_suspend,
	.resume =	storage_resume,
A
Alan Stern 已提交
1055
	.reset_resume =	storage_reset_resume,
1056
#endif
1057 1058
	.pre_reset =	storage_pre_reset,
	.post_reset =	storage_post_reset,
1059
	.id_table =	storage_usb_ids,
1060
	.soft_unbind =	1,
1061 1062
};

L
Linus Torvalds 已提交
1063 1064 1065 1066 1067 1068 1069
static int __init usb_stor_init(void)
{
	int retval;
	printk(KERN_INFO "Initializing USB Mass Storage driver...\n");

	/* register the driver, return usb_register return code if error */
	retval = usb_register(&usb_storage_driver);
1070
	if (retval == 0) {
L
Linus Torvalds 已提交
1071
		printk(KERN_INFO "USB Mass Storage support registered.\n");
1072 1073
		usb_usual_set_present(USB_US_TYPE_STOR);
	}
L
Linus Torvalds 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
	return retval;
}

static void __exit usb_stor_exit(void)
{
	US_DEBUGP("usb_stor_exit() called\n");

	/* Deregister the driver
	 * This will cause disconnect() to be called for each
	 * attached unit
	 */
	US_DEBUGP("-- calling usb_deregister()\n");
	usb_deregister(&usb_storage_driver) ;

1088
	usb_usual_clear_present(USB_US_TYPE_STOR);
L
Linus Torvalds 已提交
1089 1090 1091 1092
}

module_init(usb_stor_init);
module_exit(usb_stor_exit);