usb-storage: implement "soft" unbinding
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / storage / usb.c
blob6bfd99dd57aaadb4e23eba00cf670466b849ca2a
1 /* Driver for USB Mass Storage compliant devices
3 * $Id: usb.c,v 1.75 2002/04/22 03:39:43 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2003 Alan Stern (stern@rowland.harvard.edu)
12 * Initial work by:
13 * (c) 1999 Michael Gee (michael@linuxspecific.com)
15 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
16 * (c) 2000 Yggdrasil Computing, Inc.
18 * This driver is based on the 'USB Mass Storage Class' document. This
19 * describes in detail the protocol used to communicate with such
20 * devices. Clearly, the designers had SCSI and ATAPI commands in
21 * mind when they created this document. The commands are all very
22 * similar to commands in the SCSI-II and ATAPI specifications.
24 * It is important to note that in a number of cases this class
25 * exhibits class-specific exemptions from the USB specification.
26 * Notably the usage of NAK, STALL and ACK differs from the norm, in
27 * that they are used to communicate wait, failed and OK on commands.
29 * Also, for certain devices, the interrupt endpoint is used to convey
30 * status of a command.
32 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
33 * information about this driver.
35 * This program is free software; you can redistribute it and/or modify it
36 * under the terms of the GNU General Public License as published by the
37 * Free Software Foundation; either version 2, or (at your option) any
38 * later version.
40 * This program is distributed in the hope that it will be useful, but
41 * WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * General Public License for more details.
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 675 Mass Ave, Cambridge, MA 02139, USA.
50 #include <linux/sched.h>
51 #include <linux/errno.h>
52 #include <linux/freezer.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/kthread.h>
57 #include <linux/mutex.h>
58 #include <linux/utsname.h>
60 #include <scsi/scsi.h>
61 #include <scsi/scsi_cmnd.h>
62 #include <scsi/scsi_device.h>
64 #include "usb.h"
65 #include "scsiglue.h"
66 #include "transport.h"
67 #include "protocol.h"
68 #include "debug.h"
69 #include "initializers.h"
71 #ifdef CONFIG_USB_STORAGE_USBAT
72 #include "shuttle_usbat.h"
73 #endif
74 #ifdef CONFIG_USB_STORAGE_SDDR09
75 #include "sddr09.h"
76 #endif
77 #ifdef CONFIG_USB_STORAGE_SDDR55
78 #include "sddr55.h"
79 #endif
80 #ifdef CONFIG_USB_STORAGE_DPCM
81 #include "dpcm.h"
82 #endif
83 #ifdef CONFIG_USB_STORAGE_FREECOM
84 #include "freecom.h"
85 #endif
86 #ifdef CONFIG_USB_STORAGE_ISD200
87 #include "isd200.h"
88 #endif
89 #ifdef CONFIG_USB_STORAGE_DATAFAB
90 #include "datafab.h"
91 #endif
92 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
93 #include "jumpshot.h"
94 #endif
95 #ifdef CONFIG_USB_STORAGE_ONETOUCH
96 #include "onetouch.h"
97 #endif
98 #ifdef CONFIG_USB_STORAGE_ALAUDA
99 #include "alauda.h"
100 #endif
101 #ifdef CONFIG_USB_STORAGE_KARMA
102 #include "karma.h"
103 #endif
104 #ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
105 #include "cypress_atacb.h"
106 #endif
108 /* Some informational data */
109 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
110 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
111 MODULE_LICENSE("GPL");
113 static unsigned int delay_use = 5;
114 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
115 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
119 * The entries in this table correspond, line for line,
120 * with the entries of us_unusual_dev_list[].
122 #ifndef CONFIG_USB_LIBUSUAL
124 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
125 vendorName, productName,useProtocol, useTransport, \
126 initFunction, flags) \
127 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
128 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
130 #define USUAL_DEV(useProto, useTrans, useType) \
131 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
132 .driver_info = (USB_US_TYPE_STOR<<24) }
134 static struct usb_device_id storage_usb_ids [] = {
136 # include "unusual_devs.h"
137 #undef UNUSUAL_DEV
138 #undef USUAL_DEV
139 /* Terminating entry */
143 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
144 #endif /* CONFIG_USB_LIBUSUAL */
146 /* This is the list of devices we recognize, along with their flag data */
148 /* The vendor name should be kept at eight characters or less, and
149 * the product name should be kept at 16 characters or less. If a device
150 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
151 * normally generated by a device thorugh the INQUIRY response will be
152 * taken from this list, and this is the reason for the above size
153 * restriction. However, if the flag is not present, then you
154 * are free to use as many characters as you like.
157 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
158 vendor_name, product_name, use_protocol, use_transport, \
159 init_function, Flags) \
161 .vendorName = vendor_name, \
162 .productName = product_name, \
163 .useProtocol = use_protocol, \
164 .useTransport = use_transport, \
165 .initFunction = init_function, \
168 #define USUAL_DEV(use_protocol, use_transport, use_type) \
170 .useProtocol = use_protocol, \
171 .useTransport = use_transport, \
174 static struct us_unusual_dev us_unusual_dev_list[] = {
175 # include "unusual_devs.h"
176 # undef UNUSUAL_DEV
177 # undef USUAL_DEV
179 /* Terminating entry */
180 { NULL }
184 #ifdef CONFIG_PM /* Minimal support for suspend and resume */
186 static int storage_suspend(struct usb_interface *iface, pm_message_t message)
188 struct us_data *us = usb_get_intfdata(iface);
190 /* Wait until no command is running */
191 mutex_lock(&us->dev_mutex);
193 US_DEBUGP("%s\n", __func__);
194 if (us->suspend_resume_hook)
195 (us->suspend_resume_hook)(us, US_SUSPEND);
197 /* When runtime PM is working, we'll set a flag to indicate
198 * whether we should autoresume when a SCSI request arrives. */
200 mutex_unlock(&us->dev_mutex);
201 return 0;
204 static int storage_resume(struct usb_interface *iface)
206 struct us_data *us = usb_get_intfdata(iface);
208 mutex_lock(&us->dev_mutex);
210 US_DEBUGP("%s\n", __func__);
211 if (us->suspend_resume_hook)
212 (us->suspend_resume_hook)(us, US_RESUME);
214 mutex_unlock(&us->dev_mutex);
215 return 0;
218 static int storage_reset_resume(struct usb_interface *iface)
220 struct us_data *us = usb_get_intfdata(iface);
222 US_DEBUGP("%s\n", __func__);
224 /* Report the reset to the SCSI core */
225 usb_stor_report_bus_reset(us);
227 /* FIXME: Notify the subdrivers that they need to reinitialize
228 * the device */
229 return 0;
232 #endif /* CONFIG_PM */
235 * The next two routines get called just before and just after
236 * a USB port reset, whether from this driver or a different one.
239 static int storage_pre_reset(struct usb_interface *iface)
241 struct us_data *us = usb_get_intfdata(iface);
243 US_DEBUGP("%s\n", __func__);
245 /* Make sure no command runs during the reset */
246 mutex_lock(&us->dev_mutex);
247 return 0;
250 static int storage_post_reset(struct usb_interface *iface)
252 struct us_data *us = usb_get_intfdata(iface);
254 US_DEBUGP("%s\n", __func__);
256 /* Report the reset to the SCSI core */
257 usb_stor_report_bus_reset(us);
259 /* FIXME: Notify the subdrivers that they need to reinitialize
260 * the device */
262 mutex_unlock(&us->dev_mutex);
263 return 0;
267 * fill_inquiry_response takes an unsigned char array (which must
268 * be at least 36 characters) and populates the vendor name,
269 * product name, and revision fields. Then the array is copied
270 * into the SCSI command's response buffer (oddly enough
271 * called request_buffer). data_len contains the length of the
272 * data array, which again must be at least 36.
275 void fill_inquiry_response(struct us_data *us, unsigned char *data,
276 unsigned int data_len)
278 if (data_len<36) // You lose.
279 return;
281 if(data[0]&0x20) { /* USB device currently not connected. Return
282 peripheral qualifier 001b ("...however, the
283 physical device is not currently connected
284 to this logical unit") and leave vendor and
285 product identification empty. ("If the target
286 does store some of the INQUIRY data on the
287 device, it may return zeros or ASCII spaces
288 (20h) in those fields until the data is
289 available from the device."). */
290 memset(data+8,0,28);
291 } else {
292 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
293 memcpy(data+8, us->unusual_dev->vendorName,
294 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
295 strlen(us->unusual_dev->vendorName));
296 memcpy(data+16, us->unusual_dev->productName,
297 strlen(us->unusual_dev->productName) > 16 ? 16 :
298 strlen(us->unusual_dev->productName));
299 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
300 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
301 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
302 data[35] = 0x30 + ((bcdDevice) & 0x0F);
305 usb_stor_set_xfer_buf(data, data_len, us->srb);
308 static int usb_stor_control_thread(void * __us)
310 struct us_data *us = (struct us_data *)__us;
311 struct Scsi_Host *host = us_to_host(us);
313 for(;;) {
314 US_DEBUGP("*** thread sleeping.\n");
315 if (wait_for_completion_interruptible(&us->cmnd_ready))
316 break;
318 US_DEBUGP("*** thread awakened.\n");
320 /* lock the device pointers */
321 mutex_lock(&(us->dev_mutex));
323 /* lock access to the state */
324 scsi_lock(host);
326 /* When we are called with no command pending, we're done */
327 if (us->srb == NULL) {
328 scsi_unlock(host);
329 mutex_unlock(&us->dev_mutex);
330 US_DEBUGP("-- exiting\n");
331 break;
334 /* has the command timed out *already* ? */
335 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
336 us->srb->result = DID_ABORT << 16;
337 goto SkipForAbort;
340 scsi_unlock(host);
342 /* reject the command if the direction indicator
343 * is UNKNOWN
345 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
346 US_DEBUGP("UNKNOWN data direction\n");
347 us->srb->result = DID_ERROR << 16;
350 /* reject if target != 0 or if LUN is higher than
351 * the maximum known LUN
353 else if (us->srb->device->id &&
354 !(us->fflags & US_FL_SCM_MULT_TARG)) {
355 US_DEBUGP("Bad target number (%d:%d)\n",
356 us->srb->device->id, us->srb->device->lun);
357 us->srb->result = DID_BAD_TARGET << 16;
360 else if (us->srb->device->lun > us->max_lun) {
361 US_DEBUGP("Bad LUN (%d:%d)\n",
362 us->srb->device->id, us->srb->device->lun);
363 us->srb->result = DID_BAD_TARGET << 16;
366 /* Handle those devices which need us to fake
367 * their inquiry data */
368 else if ((us->srb->cmnd[0] == INQUIRY) &&
369 (us->fflags & US_FL_FIX_INQUIRY)) {
370 unsigned char data_ptr[36] = {
371 0x00, 0x80, 0x02, 0x02,
372 0x1F, 0x00, 0x00, 0x00};
374 US_DEBUGP("Faking INQUIRY command\n");
375 fill_inquiry_response(us, data_ptr, 36);
376 us->srb->result = SAM_STAT_GOOD;
379 /* we've got a command, let's do it! */
380 else {
381 US_DEBUG(usb_stor_show_command(us->srb));
382 us->proto_handler(us->srb, us);
385 /* lock access to the state */
386 scsi_lock(host);
388 /* indicate that the command is done */
389 if (us->srb->result != DID_ABORT << 16) {
390 US_DEBUGP("scsi cmd done, result=0x%x\n",
391 us->srb->result);
392 us->srb->scsi_done(us->srb);
393 } else {
394 SkipForAbort:
395 US_DEBUGP("scsi command aborted\n");
398 /* If an abort request was received we need to signal that
399 * the abort has finished. The proper test for this is
400 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
401 * the timeout might have occurred after the command had
402 * already completed with a different result code. */
403 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
404 complete(&(us->notify));
406 /* Allow USB transfers to resume */
407 clear_bit(US_FLIDX_ABORTING, &us->dflags);
408 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
411 /* finished working on this command */
412 us->srb = NULL;
413 scsi_unlock(host);
415 /* unlock the device pointers */
416 mutex_unlock(&us->dev_mutex);
417 } /* for (;;) */
419 /* Wait until we are told to stop */
420 for (;;) {
421 set_current_state(TASK_INTERRUPTIBLE);
422 if (kthread_should_stop())
423 break;
424 schedule();
426 __set_current_state(TASK_RUNNING);
427 return 0;
430 /***********************************************************************
431 * Device probing and disconnecting
432 ***********************************************************************/
434 /* Associate our private data with the USB device */
435 static int associate_dev(struct us_data *us, struct usb_interface *intf)
437 US_DEBUGP("-- %s\n", __func__);
439 /* Fill in the device-related fields */
440 us->pusb_dev = interface_to_usbdev(intf);
441 us->pusb_intf = intf;
442 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
443 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
444 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
445 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
446 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
447 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
448 intf->cur_altsetting->desc.bInterfaceSubClass,
449 intf->cur_altsetting->desc.bInterfaceProtocol);
451 /* Store our private data in the interface */
452 usb_set_intfdata(intf, us);
454 /* Allocate the device-related DMA-mapped buffers */
455 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
456 GFP_KERNEL, &us->cr_dma);
457 if (!us->cr) {
458 US_DEBUGP("usb_ctrlrequest allocation failed\n");
459 return -ENOMEM;
462 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
463 GFP_KERNEL, &us->iobuf_dma);
464 if (!us->iobuf) {
465 US_DEBUGP("I/O buffer allocation failed\n");
466 return -ENOMEM;
469 us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
470 if (!us->sensebuf) {
471 US_DEBUGP("Sense buffer allocation failed\n");
472 return -ENOMEM;
474 return 0;
477 /* Find an unusual_dev descriptor (always succeeds in the current code) */
478 static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
480 const int id_index = id - storage_usb_ids;
481 return &us_unusual_dev_list[id_index];
484 /* Get the unusual_devs entries and the string descriptors */
485 static int get_device_info(struct us_data *us, const struct usb_device_id *id)
487 struct usb_device *dev = us->pusb_dev;
488 struct usb_interface_descriptor *idesc =
489 &us->pusb_intf->cur_altsetting->desc;
490 struct us_unusual_dev *unusual_dev = find_unusual(id);
492 /* Store the entries */
493 us->unusual_dev = unusual_dev;
494 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
495 idesc->bInterfaceSubClass :
496 unusual_dev->useProtocol;
497 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
498 idesc->bInterfaceProtocol :
499 unusual_dev->useTransport;
500 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
502 if (us->fflags & US_FL_IGNORE_DEVICE) {
503 printk(KERN_INFO USB_STORAGE "device ignored\n");
504 return -ENODEV;
508 * This flag is only needed when we're in high-speed, so let's
509 * disable it if we're in full-speed
511 if (dev->speed != USB_SPEED_HIGH)
512 us->fflags &= ~US_FL_GO_SLOW;
514 /* Log a message if a non-generic unusual_dev entry contains an
515 * unnecessary subclass or protocol override. This may stimulate
516 * reports from users that will help us remove unneeded entries
517 * from the unusual_devs.h table.
519 if (id->idVendor || id->idProduct) {
520 static const char *msgs[3] = {
521 "an unneeded SubClass entry",
522 "an unneeded Protocol entry",
523 "unneeded SubClass and Protocol entries"};
524 struct usb_device_descriptor *ddesc = &dev->descriptor;
525 int msg = -1;
527 if (unusual_dev->useProtocol != US_SC_DEVICE &&
528 us->subclass == idesc->bInterfaceSubClass)
529 msg += 1;
530 if (unusual_dev->useTransport != US_PR_DEVICE &&
531 us->protocol == idesc->bInterfaceProtocol)
532 msg += 2;
533 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
534 printk(KERN_NOTICE USB_STORAGE "This device "
535 "(%04x,%04x,%04x S %02x P %02x)"
536 " has %s in unusual_devs.h (kernel"
537 " %s)\n"
538 " Please send a copy of this message to "
539 "<linux-usb@vger.kernel.org> and "
540 "<usb-storage@lists.one-eyed-alien.net>\n",
541 le16_to_cpu(ddesc->idVendor),
542 le16_to_cpu(ddesc->idProduct),
543 le16_to_cpu(ddesc->bcdDevice),
544 idesc->bInterfaceSubClass,
545 idesc->bInterfaceProtocol,
546 msgs[msg],
547 utsname()->release);
550 return 0;
553 /* Get the transport settings */
554 static int get_transport(struct us_data *us)
556 switch (us->protocol) {
557 case US_PR_CB:
558 us->transport_name = "Control/Bulk";
559 us->transport = usb_stor_CB_transport;
560 us->transport_reset = usb_stor_CB_reset;
561 us->max_lun = 7;
562 break;
564 case US_PR_CBI:
565 us->transport_name = "Control/Bulk/Interrupt";
566 us->transport = usb_stor_CBI_transport;
567 us->transport_reset = usb_stor_CB_reset;
568 us->max_lun = 7;
569 break;
571 case US_PR_BULK:
572 us->transport_name = "Bulk";
573 us->transport = usb_stor_Bulk_transport;
574 us->transport_reset = usb_stor_Bulk_reset;
575 break;
577 #ifdef CONFIG_USB_STORAGE_USBAT
578 case US_PR_USBAT:
579 us->transport_name = "Shuttle USBAT";
580 us->transport = usbat_transport;
581 us->transport_reset = usb_stor_CB_reset;
582 us->max_lun = 1;
583 break;
584 #endif
586 #ifdef CONFIG_USB_STORAGE_SDDR09
587 case US_PR_EUSB_SDDR09:
588 us->transport_name = "EUSB/SDDR09";
589 us->transport = sddr09_transport;
590 us->transport_reset = usb_stor_CB_reset;
591 us->max_lun = 0;
592 break;
593 #endif
595 #ifdef CONFIG_USB_STORAGE_SDDR55
596 case US_PR_SDDR55:
597 us->transport_name = "SDDR55";
598 us->transport = sddr55_transport;
599 us->transport_reset = sddr55_reset;
600 us->max_lun = 0;
601 break;
602 #endif
604 #ifdef CONFIG_USB_STORAGE_DPCM
605 case US_PR_DPCM_USB:
606 us->transport_name = "Control/Bulk-EUSB/SDDR09";
607 us->transport = dpcm_transport;
608 us->transport_reset = usb_stor_CB_reset;
609 us->max_lun = 1;
610 break;
611 #endif
613 #ifdef CONFIG_USB_STORAGE_FREECOM
614 case US_PR_FREECOM:
615 us->transport_name = "Freecom";
616 us->transport = freecom_transport;
617 us->transport_reset = usb_stor_freecom_reset;
618 us->max_lun = 0;
619 break;
620 #endif
622 #ifdef CONFIG_USB_STORAGE_DATAFAB
623 case US_PR_DATAFAB:
624 us->transport_name = "Datafab Bulk-Only";
625 us->transport = datafab_transport;
626 us->transport_reset = usb_stor_Bulk_reset;
627 us->max_lun = 1;
628 break;
629 #endif
631 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
632 case US_PR_JUMPSHOT:
633 us->transport_name = "Lexar Jumpshot Control/Bulk";
634 us->transport = jumpshot_transport;
635 us->transport_reset = usb_stor_Bulk_reset;
636 us->max_lun = 1;
637 break;
638 #endif
640 #ifdef CONFIG_USB_STORAGE_ALAUDA
641 case US_PR_ALAUDA:
642 us->transport_name = "Alauda Control/Bulk";
643 us->transport = alauda_transport;
644 us->transport_reset = usb_stor_Bulk_reset;
645 us->max_lun = 1;
646 break;
647 #endif
649 #ifdef CONFIG_USB_STORAGE_KARMA
650 case US_PR_KARMA:
651 us->transport_name = "Rio Karma/Bulk";
652 us->transport = rio_karma_transport;
653 us->transport_reset = usb_stor_Bulk_reset;
654 break;
655 #endif
657 default:
658 return -EIO;
660 US_DEBUGP("Transport: %s\n", us->transport_name);
662 /* fix for single-lun devices */
663 if (us->fflags & US_FL_SINGLE_LUN)
664 us->max_lun = 0;
665 return 0;
668 /* Get the protocol settings */
669 static int get_protocol(struct us_data *us)
671 switch (us->subclass) {
672 case US_SC_RBC:
673 us->protocol_name = "Reduced Block Commands (RBC)";
674 us->proto_handler = usb_stor_transparent_scsi_command;
675 break;
677 case US_SC_8020:
678 us->protocol_name = "8020i";
679 us->proto_handler = usb_stor_ATAPI_command;
680 us->max_lun = 0;
681 break;
683 case US_SC_QIC:
684 us->protocol_name = "QIC-157";
685 us->proto_handler = usb_stor_qic157_command;
686 us->max_lun = 0;
687 break;
689 case US_SC_8070:
690 us->protocol_name = "8070i";
691 us->proto_handler = usb_stor_ATAPI_command;
692 us->max_lun = 0;
693 break;
695 case US_SC_SCSI:
696 us->protocol_name = "Transparent SCSI";
697 us->proto_handler = usb_stor_transparent_scsi_command;
698 break;
700 case US_SC_UFI:
701 us->protocol_name = "Uniform Floppy Interface (UFI)";
702 us->proto_handler = usb_stor_ufi_command;
703 break;
705 #ifdef CONFIG_USB_STORAGE_ISD200
706 case US_SC_ISD200:
707 us->protocol_name = "ISD200 ATA/ATAPI";
708 us->proto_handler = isd200_ata_command;
709 break;
710 #endif
712 #ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
713 case US_SC_CYP_ATACB:
714 us->protocol_name = "Transparent SCSI with Cypress ATACB";
715 us->proto_handler = cypress_atacb_passthrough;
716 break;
717 #endif
719 default:
720 return -EIO;
722 US_DEBUGP("Protocol: %s\n", us->protocol_name);
723 return 0;
726 /* Get the pipe settings */
727 static int get_pipes(struct us_data *us)
729 struct usb_host_interface *altsetting =
730 us->pusb_intf->cur_altsetting;
731 int i;
732 struct usb_endpoint_descriptor *ep;
733 struct usb_endpoint_descriptor *ep_in = NULL;
734 struct usb_endpoint_descriptor *ep_out = NULL;
735 struct usb_endpoint_descriptor *ep_int = NULL;
738 * Find the first endpoint of each type we need.
739 * We are expecting a minimum of 2 endpoints - in and out (bulk).
740 * An optional interrupt-in is OK (necessary for CBI protocol).
741 * We will ignore any others.
743 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
744 ep = &altsetting->endpoint[i].desc;
746 if (usb_endpoint_xfer_bulk(ep)) {
747 if (usb_endpoint_dir_in(ep)) {
748 if (!ep_in)
749 ep_in = ep;
750 } else {
751 if (!ep_out)
752 ep_out = ep;
756 else if (usb_endpoint_is_int_in(ep)) {
757 if (!ep_int)
758 ep_int = ep;
762 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
763 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
764 return -EIO;
767 /* Calculate and store the pipe values */
768 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
769 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
770 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
771 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
772 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
773 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
774 if (ep_int) {
775 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
776 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
777 us->ep_bInterval = ep_int->bInterval;
779 return 0;
782 /* Initialize all the dynamic resources we need */
783 static int usb_stor_acquire_resources(struct us_data *us)
785 int p;
786 struct task_struct *th;
788 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
789 if (!us->current_urb) {
790 US_DEBUGP("URB allocation failed\n");
791 return -ENOMEM;
794 /* Just before we start our control thread, initialize
795 * the device if it needs initialization */
796 if (us->unusual_dev->initFunction) {
797 p = us->unusual_dev->initFunction(us);
798 if (p)
799 return p;
802 /* Start up our control thread */
803 th = kthread_run(usb_stor_control_thread, us, "usb-storage");
804 if (IS_ERR(th)) {
805 printk(KERN_WARNING USB_STORAGE
806 "Unable to start control thread\n");
807 return PTR_ERR(th);
809 us->ctl_thread = th;
811 return 0;
814 /* Release all our dynamic resources */
815 static void usb_stor_release_resources(struct us_data *us)
817 US_DEBUGP("-- %s\n", __func__);
819 /* Tell the control thread to exit. The SCSI host must
820 * already have been removed and the DISCONNECTING flag set
821 * so that we won't accept any more commands.
823 US_DEBUGP("-- sending exit command to thread\n");
824 complete(&us->cmnd_ready);
825 if (us->ctl_thread)
826 kthread_stop(us->ctl_thread);
828 /* Call the destructor routine, if it exists */
829 if (us->extra_destructor) {
830 US_DEBUGP("-- calling extra_destructor()\n");
831 us->extra_destructor(us->extra);
834 /* Free the extra data and the URB */
835 kfree(us->extra);
836 usb_free_urb(us->current_urb);
839 /* Dissociate from the USB device */
840 static void dissociate_dev(struct us_data *us)
842 US_DEBUGP("-- %s\n", __func__);
844 kfree(us->sensebuf);
846 /* Free the device-related DMA-mapped buffers */
847 if (us->cr)
848 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
849 us->cr_dma);
850 if (us->iobuf)
851 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
852 us->iobuf_dma);
854 /* Remove our private data from the interface */
855 usb_set_intfdata(us->pusb_intf, NULL);
858 /* First stage of disconnect processing: stop SCSI scanning,
859 * remove the host, and stop accepting new commands
861 static void quiesce_and_remove_host(struct us_data *us)
863 struct Scsi_Host *host = us_to_host(us);
865 /* If the device is really gone, cut short reset delays */
866 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
867 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
869 /* Prevent SCSI-scanning (if it hasn't started yet)
870 * and wait for the SCSI-scanning thread to stop.
872 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
873 wake_up(&us->delay_wait);
874 wait_for_completion(&us->scanning_done);
876 /* Removing the host will perform an orderly shutdown: caches
877 * synchronized, disks spun down, etc.
879 scsi_remove_host(host);
881 /* Prevent any new commands from being accepted and cut short
882 * reset delays.
884 scsi_lock(host);
885 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
886 scsi_unlock(host);
887 wake_up(&us->delay_wait);
890 /* Second stage of disconnect processing: deallocate all resources */
891 static void release_everything(struct us_data *us)
893 usb_stor_release_resources(us);
894 dissociate_dev(us);
896 /* Drop our reference to the host; the SCSI core will free it
897 * (and "us" along with it) when the refcount becomes 0. */
898 scsi_host_put(us_to_host(us));
901 /* Thread to carry out delayed SCSI-device scanning */
902 static int usb_stor_scan_thread(void * __us)
904 struct us_data *us = (struct us_data *)__us;
906 printk(KERN_DEBUG
907 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
909 set_freezable();
910 /* Wait for the timeout to expire or for a disconnect */
911 if (delay_use > 0) {
912 printk(KERN_DEBUG "usb-storage: waiting for device "
913 "to settle before scanning\n");
914 wait_event_freezable_timeout(us->delay_wait,
915 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
916 delay_use * HZ);
919 /* If the device is still connected, perform the scanning */
920 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
922 /* For bulk-only devices, determine the max LUN value */
923 if (us->protocol == US_PR_BULK &&
924 !(us->fflags & US_FL_SINGLE_LUN)) {
925 mutex_lock(&us->dev_mutex);
926 us->max_lun = usb_stor_Bulk_max_lun(us);
927 mutex_unlock(&us->dev_mutex);
929 scsi_scan_host(us_to_host(us));
930 printk(KERN_DEBUG "usb-storage: device scan complete\n");
932 /* Should we unbind if no devices were detected? */
935 complete_and_exit(&us->scanning_done, 0);
939 /* Probe to see if we can drive a newly-connected USB device */
940 static int storage_probe(struct usb_interface *intf,
941 const struct usb_device_id *id)
943 struct Scsi_Host *host;
944 struct us_data *us;
945 int result;
946 struct task_struct *th;
948 if (usb_usual_check_type(id, USB_US_TYPE_STOR))
949 return -ENXIO;
951 US_DEBUGP("USB Mass Storage device detected\n");
954 * Ask the SCSI layer to allocate a host structure, with extra
955 * space at the end for our private us_data structure.
957 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
958 if (!host) {
959 printk(KERN_WARNING USB_STORAGE
960 "Unable to allocate the scsi host\n");
961 return -ENOMEM;
965 * Allow 16-byte CDBs and thus > 2TB
967 host->max_cmd_len = 16;
968 us = host_to_us(host);
969 memset(us, 0, sizeof(struct us_data));
970 mutex_init(&(us->dev_mutex));
971 init_completion(&us->cmnd_ready);
972 init_completion(&(us->notify));
973 init_waitqueue_head(&us->delay_wait);
974 init_completion(&us->scanning_done);
976 /* Associate the us_data structure with the USB device */
977 result = associate_dev(us, intf);
978 if (result)
979 goto BadDevice;
982 * Get the unusual_devs entries and the descriptors
984 * id_index is calculated in the declaration to be the index number
985 * of the match from the usb_device_id table, so we can find the
986 * corresponding entry in the private table.
988 result = get_device_info(us, id);
989 if (result)
990 goto BadDevice;
992 /* Get the transport, protocol, and pipe settings */
993 result = get_transport(us);
994 if (result)
995 goto BadDevice;
996 result = get_protocol(us);
997 if (result)
998 goto BadDevice;
999 result = get_pipes(us);
1000 if (result)
1001 goto BadDevice;
1003 /* Acquire all the other resources and add the host */
1004 result = usb_stor_acquire_resources(us);
1005 if (result)
1006 goto BadDevice;
1007 result = scsi_add_host(host, &intf->dev);
1008 if (result) {
1009 printk(KERN_WARNING USB_STORAGE
1010 "Unable to add the scsi host\n");
1011 goto BadDevice;
1014 /* Start up the thread for delayed SCSI-device scanning */
1015 th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
1016 if (IS_ERR(th)) {
1017 printk(KERN_WARNING USB_STORAGE
1018 "Unable to start the device-scanning thread\n");
1019 complete(&us->scanning_done);
1020 quiesce_and_remove_host(us);
1021 result = PTR_ERR(th);
1022 goto BadDevice;
1025 wake_up_process(th);
1027 return 0;
1029 /* We come here if there are any problems */
1030 BadDevice:
1031 US_DEBUGP("storage_probe() failed\n");
1032 release_everything(us);
1033 return result;
1036 /* Handle a disconnect event from the USB core */
1037 static void storage_disconnect(struct usb_interface *intf)
1039 struct us_data *us = usb_get_intfdata(intf);
1041 US_DEBUGP("storage_disconnect() called\n");
1042 quiesce_and_remove_host(us);
1043 release_everything(us);
1046 /***********************************************************************
1047 * Initialization and registration
1048 ***********************************************************************/
1050 static struct usb_driver usb_storage_driver = {
1051 .name = "usb-storage",
1052 .probe = storage_probe,
1053 .disconnect = storage_disconnect,
1054 #ifdef CONFIG_PM
1055 .suspend = storage_suspend,
1056 .resume = storage_resume,
1057 .reset_resume = storage_reset_resume,
1058 #endif
1059 .pre_reset = storage_pre_reset,
1060 .post_reset = storage_post_reset,
1061 .id_table = storage_usb_ids,
1062 .soft_unbind = 1,
1065 static int __init usb_stor_init(void)
1067 int retval;
1068 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1070 /* register the driver, return usb_register return code if error */
1071 retval = usb_register(&usb_storage_driver);
1072 if (retval == 0) {
1073 printk(KERN_INFO "USB Mass Storage support registered.\n");
1074 usb_usual_set_present(USB_US_TYPE_STOR);
1076 return retval;
1079 static void __exit usb_stor_exit(void)
1081 US_DEBUGP("usb_stor_exit() called\n");
1083 /* Deregister the driver
1084 * This will cause disconnect() to be called for each
1085 * attached unit
1087 US_DEBUGP("-- calling usb_deregister()\n");
1088 usb_deregister(&usb_storage_driver) ;
1090 usb_usual_clear_present(USB_US_TYPE_STOR);
1093 module_init(usb_stor_init);
1094 module_exit(usb_stor_exit);