1 /* Driver for USB Mass Storage compliant devices
3 * Current development and maintenance by:
4 * (c) 1999-2003 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
6 * Developed with the assistance of:
7 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
8 * (c) 2003 Alan Stern (stern@rowland.harvard.edu)
11 * (c) 1999 Michael Gee (michael@linuxspecific.com)
13 * usb_device_id support by Adam J. Richter (adam@yggdrasil.com):
14 * (c) 2000 Yggdrasil Computing, Inc.
16 * This driver is based on the 'USB Mass Storage Class' document. This
17 * describes in detail the protocol used to communicate with such
18 * devices. Clearly, the designers had SCSI and ATAPI commands in
19 * mind when they created this document. The commands are all very
20 * similar to commands in the SCSI-II and ATAPI specifications.
22 * It is important to note that in a number of cases this class
23 * exhibits class-specific exemptions from the USB specification.
24 * Notably the usage of NAK, STALL and ACK differs from the norm, in
25 * that they are used to communicate wait, failed and OK on commands.
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License as published by the
35 * Free Software Foundation; either version 2, or (at your option) any
38 * This program is distributed in the hope that it will be useful, but
39 * WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * General Public License for more details.
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 675 Mass Ave, Cambridge, MA 02139, USA.
48 #include <linux/sched.h>
49 #include <linux/errno.h>
50 #include <linux/freezer.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/kthread.h>
55 #include <linux/mutex.h>
56 #include <linux/utsname.h>
58 #include <scsi/scsi.h>
59 #include <scsi/scsi_cmnd.h>
60 #include <scsi/scsi_device.h>
64 #include "transport.h"
67 #include "initializers.h"
69 #ifdef CONFIG_USB_STORAGE_USBAT
70 #include "shuttle_usbat.h"
72 #ifdef CONFIG_USB_STORAGE_SDDR09
75 #ifdef CONFIG_USB_STORAGE_SDDR55
78 #ifdef CONFIG_USB_STORAGE_FREECOM
81 #ifdef CONFIG_USB_STORAGE_ISD200
84 #ifdef CONFIG_USB_STORAGE_DATAFAB
87 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
90 #ifdef CONFIG_USB_STORAGE_ONETOUCH
93 #ifdef CONFIG_USB_STORAGE_ALAUDA
96 #ifdef CONFIG_USB_STORAGE_KARMA
99 #ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
100 #include "cypress_atacb.h"
102 #include "sierra_ms.h"
104 /* Some informational data */
105 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
106 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
107 MODULE_LICENSE("GPL");
109 static unsigned int delay_use = 5;
110 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
111 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
114 module_param(quirks, charp, S_IRUGO);
115 MODULE_PARM_DESC(quirks, "supplemental list of device IDs and their quirks");
117 struct quirks_entry {
121 static struct quirks_entry *quirks_list, *quirks_end;
125 * The entries in this table correspond, line for line,
126 * with the entries of us_unusual_dev_list[].
128 #ifndef CONFIG_USB_LIBUSUAL
130 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
131 vendorName, productName,useProtocol, useTransport, \
132 initFunction, flags) \
133 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax), \
134 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
136 #define USUAL_DEV(useProto, useTrans, useType) \
137 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, useProto, useTrans), \
138 .driver_info = (USB_US_TYPE_STOR<<24) }
140 static struct usb_device_id storage_usb_ids [] = {
142 # include "unusual_devs.h"
145 /* Terminating entry */
149 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
150 #endif /* CONFIG_USB_LIBUSUAL */
152 /* This is the list of devices we recognize, along with their flag data */
154 /* The vendor name should be kept at eight characters or less, and
155 * the product name should be kept at 16 characters or less. If a device
156 * has the US_FL_FIX_INQUIRY flag, then the vendor and product names
157 * normally generated by a device thorugh the INQUIRY response will be
158 * taken from this list, and this is the reason for the above size
159 * restriction. However, if the flag is not present, then you
160 * are free to use as many characters as you like.
163 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
164 vendor_name, product_name, use_protocol, use_transport, \
165 init_function, Flags) \
167 .vendorName = vendor_name, \
168 .productName = product_name, \
169 .useProtocol = use_protocol, \
170 .useTransport = use_transport, \
171 .initFunction = init_function, \
174 #define USUAL_DEV(use_protocol, use_transport, use_type) \
176 .useProtocol = use_protocol, \
177 .useTransport = use_transport, \
180 static struct us_unusual_dev us_unusual_dev_list[] = {
181 # include "unusual_devs.h"
185 /* Terminating entry */
190 #ifdef CONFIG_PM /* Minimal support for suspend and resume */
192 static int storage_suspend(struct usb_interface *iface, pm_message_t message)
194 struct us_data *us = usb_get_intfdata(iface);
196 /* Wait until no command is running */
197 mutex_lock(&us->dev_mutex);
199 US_DEBUGP("%s\n", __func__);
200 if (us->suspend_resume_hook)
201 (us->suspend_resume_hook)(us, US_SUSPEND);
203 /* When runtime PM is working, we'll set a flag to indicate
204 * whether we should autoresume when a SCSI request arrives. */
206 mutex_unlock(&us->dev_mutex);
210 static int storage_resume(struct usb_interface *iface)
212 struct us_data *us = usb_get_intfdata(iface);
214 mutex_lock(&us->dev_mutex);
216 US_DEBUGP("%s\n", __func__);
217 if (us->suspend_resume_hook)
218 (us->suspend_resume_hook)(us, US_RESUME);
220 mutex_unlock(&us->dev_mutex);
224 static int storage_reset_resume(struct usb_interface *iface)
226 struct us_data *us = usb_get_intfdata(iface);
228 US_DEBUGP("%s\n", __func__);
230 /* Report the reset to the SCSI core */
231 usb_stor_report_bus_reset(us);
233 /* FIXME: Notify the subdrivers that they need to reinitialize
238 #endif /* CONFIG_PM */
241 * The next two routines get called just before and just after
242 * a USB port reset, whether from this driver or a different one.
245 static int storage_pre_reset(struct usb_interface *iface)
247 struct us_data *us = usb_get_intfdata(iface);
249 US_DEBUGP("%s\n", __func__);
251 /* Make sure no command runs during the reset */
252 mutex_lock(&us->dev_mutex);
256 static int storage_post_reset(struct usb_interface *iface)
258 struct us_data *us = usb_get_intfdata(iface);
260 US_DEBUGP("%s\n", __func__);
262 /* Report the reset to the SCSI core */
263 usb_stor_report_bus_reset(us);
265 /* FIXME: Notify the subdrivers that they need to reinitialize
268 mutex_unlock(&us->dev_mutex);
273 * fill_inquiry_response takes an unsigned char array (which must
274 * be at least 36 characters) and populates the vendor name,
275 * product name, and revision fields. Then the array is copied
276 * into the SCSI command's response buffer (oddly enough
277 * called request_buffer). data_len contains the length of the
278 * data array, which again must be at least 36.
281 void fill_inquiry_response(struct us_data *us, unsigned char *data,
282 unsigned int data_len)
284 if (data_len<36) // You lose.
287 if(data[0]&0x20) { /* USB device currently not connected. Return
288 peripheral qualifier 001b ("...however, the
289 physical device is not currently connected
290 to this logical unit") and leave vendor and
291 product identification empty. ("If the target
292 does store some of the INQUIRY data on the
293 device, it may return zeros or ASCII spaces
294 (20h) in those fields until the data is
295 available from the device."). */
298 u16 bcdDevice = le16_to_cpu(us->pusb_dev->descriptor.bcdDevice);
299 memcpy(data+8, us->unusual_dev->vendorName,
300 strlen(us->unusual_dev->vendorName) > 8 ? 8 :
301 strlen(us->unusual_dev->vendorName));
302 memcpy(data+16, us->unusual_dev->productName,
303 strlen(us->unusual_dev->productName) > 16 ? 16 :
304 strlen(us->unusual_dev->productName));
305 data[32] = 0x30 + ((bcdDevice>>12) & 0x0F);
306 data[33] = 0x30 + ((bcdDevice>>8) & 0x0F);
307 data[34] = 0x30 + ((bcdDevice>>4) & 0x0F);
308 data[35] = 0x30 + ((bcdDevice) & 0x0F);
311 usb_stor_set_xfer_buf(data, data_len, us->srb);
314 static int usb_stor_control_thread(void * __us)
316 struct us_data *us = (struct us_data *)__us;
317 struct Scsi_Host *host = us_to_host(us);
320 US_DEBUGP("*** thread sleeping.\n");
321 if (wait_for_completion_interruptible(&us->cmnd_ready))
324 US_DEBUGP("*** thread awakened.\n");
326 /* lock the device pointers */
327 mutex_lock(&(us->dev_mutex));
329 /* lock access to the state */
332 /* When we are called with no command pending, we're done */
333 if (us->srb == NULL) {
335 mutex_unlock(&us->dev_mutex);
336 US_DEBUGP("-- exiting\n");
340 /* has the command timed out *already* ? */
341 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
342 us->srb->result = DID_ABORT << 16;
348 /* reject the command if the direction indicator
351 if (us->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
352 US_DEBUGP("UNKNOWN data direction\n");
353 us->srb->result = DID_ERROR << 16;
356 /* reject if target != 0 or if LUN is higher than
357 * the maximum known LUN
359 else if (us->srb->device->id &&
360 !(us->fflags & US_FL_SCM_MULT_TARG)) {
361 US_DEBUGP("Bad target number (%d:%d)\n",
362 us->srb->device->id, us->srb->device->lun);
363 us->srb->result = DID_BAD_TARGET << 16;
366 else if (us->srb->device->lun > us->max_lun) {
367 US_DEBUGP("Bad LUN (%d:%d)\n",
368 us->srb->device->id, us->srb->device->lun);
369 us->srb->result = DID_BAD_TARGET << 16;
372 /* Handle those devices which need us to fake
373 * their inquiry data */
374 else if ((us->srb->cmnd[0] == INQUIRY) &&
375 (us->fflags & US_FL_FIX_INQUIRY)) {
376 unsigned char data_ptr[36] = {
377 0x00, 0x80, 0x02, 0x02,
378 0x1F, 0x00, 0x00, 0x00};
380 US_DEBUGP("Faking INQUIRY command\n");
381 fill_inquiry_response(us, data_ptr, 36);
382 us->srb->result = SAM_STAT_GOOD;
385 /* we've got a command, let's do it! */
387 US_DEBUG(usb_stor_show_command(us->srb));
388 us->proto_handler(us->srb, us);
391 /* lock access to the state */
394 /* indicate that the command is done */
395 if (us->srb->result != DID_ABORT << 16) {
396 US_DEBUGP("scsi cmd done, result=0x%x\n",
398 us->srb->scsi_done(us->srb);
401 US_DEBUGP("scsi command aborted\n");
404 /* If an abort request was received we need to signal that
405 * the abort has finished. The proper test for this is
406 * the TIMED_OUT flag, not srb->result == DID_ABORT, because
407 * the timeout might have occurred after the command had
408 * already completed with a different result code. */
409 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
410 complete(&(us->notify));
412 /* Allow USB transfers to resume */
413 clear_bit(US_FLIDX_ABORTING, &us->dflags);
414 clear_bit(US_FLIDX_TIMED_OUT, &us->dflags);
417 /* finished working on this command */
421 /* unlock the device pointers */
422 mutex_unlock(&us->dev_mutex);
425 /* Wait until we are told to stop */
427 set_current_state(TASK_INTERRUPTIBLE);
428 if (kthread_should_stop())
432 __set_current_state(TASK_RUNNING);
436 /***********************************************************************
437 * Device probing and disconnecting
438 ***********************************************************************/
440 /* Associate our private data with the USB device */
441 static int associate_dev(struct us_data *us, struct usb_interface *intf)
443 US_DEBUGP("-- %s\n", __func__);
445 /* Fill in the device-related fields */
446 us->pusb_dev = interface_to_usbdev(intf);
447 us->pusb_intf = intf;
448 us->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
449 US_DEBUGP("Vendor: 0x%04x, Product: 0x%04x, Revision: 0x%04x\n",
450 le16_to_cpu(us->pusb_dev->descriptor.idVendor),
451 le16_to_cpu(us->pusb_dev->descriptor.idProduct),
452 le16_to_cpu(us->pusb_dev->descriptor.bcdDevice));
453 US_DEBUGP("Interface Subclass: 0x%02x, Protocol: 0x%02x\n",
454 intf->cur_altsetting->desc.bInterfaceSubClass,
455 intf->cur_altsetting->desc.bInterfaceProtocol);
457 /* Store our private data in the interface */
458 usb_set_intfdata(intf, us);
460 /* Allocate the device-related DMA-mapped buffers */
461 us->cr = usb_buffer_alloc(us->pusb_dev, sizeof(*us->cr),
462 GFP_KERNEL, &us->cr_dma);
464 US_DEBUGP("usb_ctrlrequest allocation failed\n");
468 us->iobuf = usb_buffer_alloc(us->pusb_dev, US_IOBUF_SIZE,
469 GFP_KERNEL, &us->iobuf_dma);
471 US_DEBUGP("I/O buffer allocation failed\n");
477 /* Adjust device flags based on the "quirks=" module parameter */
478 static void adjust_quirks(struct us_data *us)
481 struct quirks_entry *q;
482 unsigned int mask = (US_FL_FIX_CAPACITY | US_FL_IGNORE_DEVICE |
483 US_FL_NOT_LOCKABLE | US_FL_MAX_SECTORS_64 |
484 US_FL_IGNORE_RESIDUE | US_FL_SINGLE_LUN |
487 vid = le16_to_cpu(us->pusb_dev->descriptor.idVendor);
488 pid = le16_to_cpu(us->pusb_dev->descriptor.idProduct);
490 for (q = quirks_list; q != quirks_end; ++q) {
491 if (q->vid == vid && q->pid == pid) {
492 us->fflags = (us->fflags & ~mask) | q->fflags;
493 dev_info(&us->pusb_intf->dev, "Quirks match for "
494 "vid %04x pid %04x: %x\n",
495 vid, pid, q->fflags);
501 /* Find an unusual_dev descriptor (always succeeds in the current code) */
502 static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
504 const int id_index = id - storage_usb_ids;
505 return &us_unusual_dev_list[id_index];
508 /* Get the unusual_devs entries and the string descriptors */
509 static int get_device_info(struct us_data *us, const struct usb_device_id *id)
511 struct usb_device *dev = us->pusb_dev;
512 struct usb_interface_descriptor *idesc =
513 &us->pusb_intf->cur_altsetting->desc;
514 struct us_unusual_dev *unusual_dev = find_unusual(id);
516 /* Store the entries */
517 us->unusual_dev = unusual_dev;
518 us->subclass = (unusual_dev->useProtocol == US_SC_DEVICE) ?
519 idesc->bInterfaceSubClass :
520 unusual_dev->useProtocol;
521 us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
522 idesc->bInterfaceProtocol :
523 unusual_dev->useTransport;
524 us->fflags = USB_US_ORIG_FLAGS(id->driver_info);
527 if (us->fflags & US_FL_IGNORE_DEVICE) {
528 printk(KERN_INFO USB_STORAGE "device ignored\n");
533 * This flag is only needed when we're in high-speed, so let's
534 * disable it if we're in full-speed
536 if (dev->speed != USB_SPEED_HIGH)
537 us->fflags &= ~US_FL_GO_SLOW;
539 /* Log a message if a non-generic unusual_dev entry contains an
540 * unnecessary subclass or protocol override. This may stimulate
541 * reports from users that will help us remove unneeded entries
542 * from the unusual_devs.h table.
544 if (id->idVendor || id->idProduct) {
545 static const char *msgs[3] = {
546 "an unneeded SubClass entry",
547 "an unneeded Protocol entry",
548 "unneeded SubClass and Protocol entries"};
549 struct usb_device_descriptor *ddesc = &dev->descriptor;
552 if (unusual_dev->useProtocol != US_SC_DEVICE &&
553 us->subclass == idesc->bInterfaceSubClass)
555 if (unusual_dev->useTransport != US_PR_DEVICE &&
556 us->protocol == idesc->bInterfaceProtocol)
558 if (msg >= 0 && !(us->fflags & US_FL_NEED_OVERRIDE))
559 printk(KERN_NOTICE USB_STORAGE "This device "
560 "(%04x,%04x,%04x S %02x P %02x)"
561 " has %s in unusual_devs.h (kernel"
563 " Please send a copy of this message to "
564 "<linux-usb@vger.kernel.org> and "
565 "<usb-storage@lists.one-eyed-alien.net>\n",
566 le16_to_cpu(ddesc->idVendor),
567 le16_to_cpu(ddesc->idProduct),
568 le16_to_cpu(ddesc->bcdDevice),
569 idesc->bInterfaceSubClass,
570 idesc->bInterfaceProtocol,
578 /* Get the transport settings */
579 static int get_transport(struct us_data *us)
581 switch (us->protocol) {
583 us->transport_name = "Control/Bulk";
584 us->transport = usb_stor_CB_transport;
585 us->transport_reset = usb_stor_CB_reset;
590 us->transport_name = "Control/Bulk/Interrupt";
591 us->transport = usb_stor_CB_transport;
592 us->transport_reset = usb_stor_CB_reset;
597 us->transport_name = "Bulk";
598 us->transport = usb_stor_Bulk_transport;
599 us->transport_reset = usb_stor_Bulk_reset;
602 #ifdef CONFIG_USB_STORAGE_USBAT
604 us->transport_name = "Shuttle USBAT";
605 us->transport = usbat_transport;
606 us->transport_reset = usb_stor_CB_reset;
611 #ifdef CONFIG_USB_STORAGE_SDDR09
612 case US_PR_EUSB_SDDR09:
613 us->transport_name = "EUSB/SDDR09";
614 us->transport = sddr09_transport;
615 us->transport_reset = usb_stor_CB_reset;
620 #ifdef CONFIG_USB_STORAGE_SDDR55
622 us->transport_name = "SDDR55";
623 us->transport = sddr55_transport;
624 us->transport_reset = sddr55_reset;
629 #ifdef CONFIG_USB_STORAGE_DPCM
631 us->transport_name = "Control/Bulk-EUSB/SDDR09";
632 us->transport = dpcm_transport;
633 us->transport_reset = usb_stor_CB_reset;
638 #ifdef CONFIG_USB_STORAGE_FREECOM
640 us->transport_name = "Freecom";
641 us->transport = freecom_transport;
642 us->transport_reset = usb_stor_freecom_reset;
647 #ifdef CONFIG_USB_STORAGE_DATAFAB
649 us->transport_name = "Datafab Bulk-Only";
650 us->transport = datafab_transport;
651 us->transport_reset = usb_stor_Bulk_reset;
656 #ifdef CONFIG_USB_STORAGE_JUMPSHOT
658 us->transport_name = "Lexar Jumpshot Control/Bulk";
659 us->transport = jumpshot_transport;
660 us->transport_reset = usb_stor_Bulk_reset;
665 #ifdef CONFIG_USB_STORAGE_ALAUDA
667 us->transport_name = "Alauda Control/Bulk";
668 us->transport = alauda_transport;
669 us->transport_reset = usb_stor_Bulk_reset;
674 #ifdef CONFIG_USB_STORAGE_KARMA
676 us->transport_name = "Rio Karma/Bulk";
677 us->transport = rio_karma_transport;
678 us->transport_reset = usb_stor_Bulk_reset;
685 US_DEBUGP("Transport: %s\n", us->transport_name);
687 /* fix for single-lun devices */
688 if (us->fflags & US_FL_SINGLE_LUN)
693 /* Get the protocol settings */
694 static int get_protocol(struct us_data *us)
696 switch (us->subclass) {
698 us->protocol_name = "Reduced Block Commands (RBC)";
699 us->proto_handler = usb_stor_transparent_scsi_command;
703 us->protocol_name = "8020i";
704 us->proto_handler = usb_stor_pad12_command;
709 us->protocol_name = "QIC-157";
710 us->proto_handler = usb_stor_pad12_command;
715 us->protocol_name = "8070i";
716 us->proto_handler = usb_stor_pad12_command;
721 us->protocol_name = "Transparent SCSI";
722 us->proto_handler = usb_stor_transparent_scsi_command;
726 us->protocol_name = "Uniform Floppy Interface (UFI)";
727 us->proto_handler = usb_stor_ufi_command;
730 #ifdef CONFIG_USB_STORAGE_ISD200
732 us->protocol_name = "ISD200 ATA/ATAPI";
733 us->proto_handler = isd200_ata_command;
737 #ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
738 case US_SC_CYP_ATACB:
739 us->protocol_name = "Transparent SCSI with Cypress ATACB";
740 us->proto_handler = cypress_atacb_passthrough;
747 US_DEBUGP("Protocol: %s\n", us->protocol_name);
751 /* Get the pipe settings */
752 static int get_pipes(struct us_data *us)
754 struct usb_host_interface *altsetting =
755 us->pusb_intf->cur_altsetting;
757 struct usb_endpoint_descriptor *ep;
758 struct usb_endpoint_descriptor *ep_in = NULL;
759 struct usb_endpoint_descriptor *ep_out = NULL;
760 struct usb_endpoint_descriptor *ep_int = NULL;
763 * Find the first endpoint of each type we need.
764 * We are expecting a minimum of 2 endpoints - in and out (bulk).
765 * An optional interrupt-in is OK (necessary for CBI protocol).
766 * We will ignore any others.
768 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
769 ep = &altsetting->endpoint[i].desc;
771 if (usb_endpoint_xfer_bulk(ep)) {
772 if (usb_endpoint_dir_in(ep)) {
781 else if (usb_endpoint_is_int_in(ep)) {
787 if (!ep_in || !ep_out || (us->protocol == US_PR_CBI && !ep_int)) {
788 US_DEBUGP("Endpoint sanity check failed! Rejecting dev.\n");
792 /* Calculate and store the pipe values */
793 us->send_ctrl_pipe = usb_sndctrlpipe(us->pusb_dev, 0);
794 us->recv_ctrl_pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
795 us->send_bulk_pipe = usb_sndbulkpipe(us->pusb_dev,
796 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
797 us->recv_bulk_pipe = usb_rcvbulkpipe(us->pusb_dev,
798 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
800 us->recv_intr_pipe = usb_rcvintpipe(us->pusb_dev,
801 ep_int->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
802 us->ep_bInterval = ep_int->bInterval;
807 /* Initialize all the dynamic resources we need */
808 static int usb_stor_acquire_resources(struct us_data *us)
811 struct task_struct *th;
813 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
814 if (!us->current_urb) {
815 US_DEBUGP("URB allocation failed\n");
819 /* Just before we start our control thread, initialize
820 * the device if it needs initialization */
821 if (us->unusual_dev->initFunction) {
822 p = us->unusual_dev->initFunction(us);
827 /* Start up our control thread */
828 th = kthread_run(usb_stor_control_thread, us, "usb-storage");
830 printk(KERN_WARNING USB_STORAGE
831 "Unable to start control thread\n");
839 /* Release all our dynamic resources */
840 static void usb_stor_release_resources(struct us_data *us)
842 US_DEBUGP("-- %s\n", __func__);
844 /* Tell the control thread to exit. The SCSI host must
845 * already have been removed and the DISCONNECTING flag set
846 * so that we won't accept any more commands.
848 US_DEBUGP("-- sending exit command to thread\n");
849 complete(&us->cmnd_ready);
851 kthread_stop(us->ctl_thread);
853 /* Call the destructor routine, if it exists */
854 if (us->extra_destructor) {
855 US_DEBUGP("-- calling extra_destructor()\n");
856 us->extra_destructor(us->extra);
859 /* Free the extra data and the URB */
861 usb_free_urb(us->current_urb);
864 /* Dissociate from the USB device */
865 static void dissociate_dev(struct us_data *us)
867 US_DEBUGP("-- %s\n", __func__);
869 /* Free the device-related DMA-mapped buffers */
871 usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
874 usb_buffer_free(us->pusb_dev, US_IOBUF_SIZE, us->iobuf,
877 /* Remove our private data from the interface */
878 usb_set_intfdata(us->pusb_intf, NULL);
881 /* First stage of disconnect processing: stop SCSI scanning,
882 * remove the host, and stop accepting new commands
884 static void quiesce_and_remove_host(struct us_data *us)
886 struct Scsi_Host *host = us_to_host(us);
888 /* If the device is really gone, cut short reset delays */
889 if (us->pusb_dev->state == USB_STATE_NOTATTACHED)
890 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
892 /* Prevent SCSI-scanning (if it hasn't started yet)
893 * and wait for the SCSI-scanning thread to stop.
895 set_bit(US_FLIDX_DONT_SCAN, &us->dflags);
896 wake_up(&us->delay_wait);
897 wait_for_completion(&us->scanning_done);
899 /* Removing the host will perform an orderly shutdown: caches
900 * synchronized, disks spun down, etc.
902 scsi_remove_host(host);
904 /* Prevent any new commands from being accepted and cut short
908 set_bit(US_FLIDX_DISCONNECTING, &us->dflags);
910 wake_up(&us->delay_wait);
913 /* Second stage of disconnect processing: deallocate all resources */
914 static void release_everything(struct us_data *us)
916 usb_stor_release_resources(us);
919 /* Drop our reference to the host; the SCSI core will free it
920 * (and "us" along with it) when the refcount becomes 0. */
921 scsi_host_put(us_to_host(us));
924 /* Thread to carry out delayed SCSI-device scanning */
925 static int usb_stor_scan_thread(void * __us)
927 struct us_data *us = (struct us_data *)__us;
930 "usb-storage: device found at %d\n", us->pusb_dev->devnum);
933 /* Wait for the timeout to expire or for a disconnect */
935 printk(KERN_DEBUG "usb-storage: waiting for device "
936 "to settle before scanning\n");
937 wait_event_freezable_timeout(us->delay_wait,
938 test_bit(US_FLIDX_DONT_SCAN, &us->dflags),
942 /* If the device is still connected, perform the scanning */
943 if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) {
945 /* For bulk-only devices, determine the max LUN value */
946 if (us->protocol == US_PR_BULK &&
947 !(us->fflags & US_FL_SINGLE_LUN)) {
948 mutex_lock(&us->dev_mutex);
949 us->max_lun = usb_stor_Bulk_max_lun(us);
950 mutex_unlock(&us->dev_mutex);
952 scsi_scan_host(us_to_host(us));
953 printk(KERN_DEBUG "usb-storage: device scan complete\n");
955 /* Should we unbind if no devices were detected? */
958 complete_and_exit(&us->scanning_done, 0);
962 /* Probe to see if we can drive a newly-connected USB device */
963 static int storage_probe(struct usb_interface *intf,
964 const struct usb_device_id *id)
966 struct Scsi_Host *host;
969 struct task_struct *th;
971 if (usb_usual_check_type(id, USB_US_TYPE_STOR))
974 US_DEBUGP("USB Mass Storage device detected\n");
977 * Ask the SCSI layer to allocate a host structure, with extra
978 * space at the end for our private us_data structure.
980 host = scsi_host_alloc(&usb_stor_host_template, sizeof(*us));
982 printk(KERN_WARNING USB_STORAGE
983 "Unable to allocate the scsi host\n");
988 * Allow 16-byte CDBs and thus > 2TB
990 host->max_cmd_len = 16;
991 us = host_to_us(host);
992 memset(us, 0, sizeof(struct us_data));
993 mutex_init(&(us->dev_mutex));
994 init_completion(&us->cmnd_ready);
995 init_completion(&(us->notify));
996 init_waitqueue_head(&us->delay_wait);
997 init_completion(&us->scanning_done);
999 /* Associate the us_data structure with the USB device */
1000 result = associate_dev(us, intf);
1005 * Get the unusual_devs entries and the descriptors
1007 * id_index is calculated in the declaration to be the index number
1008 * of the match from the usb_device_id table, so we can find the
1009 * corresponding entry in the private table.
1011 result = get_device_info(us, id);
1015 /* Get the transport, protocol, and pipe settings */
1016 result = get_transport(us);
1019 result = get_protocol(us);
1022 result = get_pipes(us);
1026 /* Acquire all the other resources and add the host */
1027 result = usb_stor_acquire_resources(us);
1030 result = scsi_add_host(host, &intf->dev);
1032 printk(KERN_WARNING USB_STORAGE
1033 "Unable to add the scsi host\n");
1037 /* Start up the thread for delayed SCSI-device scanning */
1038 th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
1040 printk(KERN_WARNING USB_STORAGE
1041 "Unable to start the device-scanning thread\n");
1042 complete(&us->scanning_done);
1043 quiesce_and_remove_host(us);
1044 result = PTR_ERR(th);
1048 wake_up_process(th);
1052 /* We come here if there are any problems */
1054 US_DEBUGP("storage_probe() failed\n");
1055 release_everything(us);
1059 /* Handle a disconnect event from the USB core */
1060 static void storage_disconnect(struct usb_interface *intf)
1062 struct us_data *us = usb_get_intfdata(intf);
1064 US_DEBUGP("storage_disconnect() called\n");
1065 quiesce_and_remove_host(us);
1066 release_everything(us);
1069 /***********************************************************************
1070 * Initialization and registration
1071 ***********************************************************************/
1073 static struct usb_driver usb_storage_driver = {
1074 .name = "usb-storage",
1075 .probe = storage_probe,
1076 .disconnect = storage_disconnect,
1078 .suspend = storage_suspend,
1079 .resume = storage_resume,
1080 .reset_resume = storage_reset_resume,
1082 .pre_reset = storage_pre_reset,
1083 .post_reset = storage_post_reset,
1084 .id_table = storage_usb_ids,
1088 /* Works only for digits and letters, but small and fast */
1089 #define TOLOWER(x) ((x) | 0x20)
1091 static void __init parse_quirks(void)
1099 /* Count the ':' characters to get 2 * the number of entries */
1101 for (p = quirks; *p; ++p) {
1107 return; /* Don't allocate 0 bytes */
1109 quirks_list = kmalloc(n * sizeof(*quirks_list), GFP_KERNEL);
1114 quirks_end = quirks_list;
1115 for (i = 0; i < n && *p; ++i) {
1118 /* Each entry consists of VID:PID:flags */
1119 quirks_end->vid = simple_strtoul(p, &p, 16);
1122 quirks_end->pid = simple_strtoul(p+1, &p, 16);
1126 while (*++p && *p != ',') {
1127 switch (TOLOWER(*p)) {
1129 f |= US_FL_FIX_CAPACITY;
1132 f |= US_FL_IGNORE_DEVICE;
1135 f |= US_FL_NOT_LOCKABLE;
1138 f |= US_FL_MAX_SECTORS_64;
1141 f |= US_FL_IGNORE_RESIDUE;
1144 f |= US_FL_SINGLE_LUN;
1147 f |= US_FL_NO_WP_DETECT;
1149 /* Ignore unrecognized flag characters */
1152 quirks_end->fflags = f;
1156 /* Entries are separated by commas */
1161 } /* for (i = 0; ...) */
1164 static int __init usb_stor_init(void)
1168 printk(KERN_INFO "Initializing USB Mass Storage driver...\n");
1171 /* register the driver, return usb_register return code if error */
1172 retval = usb_register(&usb_storage_driver);
1174 printk(KERN_INFO "USB Mass Storage support registered.\n");
1175 usb_usual_set_present(USB_US_TYPE_STOR);
1180 static void __exit usb_stor_exit(void)
1182 US_DEBUGP("usb_stor_exit() called\n");
1184 /* Deregister the driver
1185 * This will cause disconnect() to be called for each
1188 US_DEBUGP("-- calling usb_deregister()\n");
1189 usb_deregister(&usb_storage_driver) ;
1191 usb_usual_clear_present(USB_US_TYPE_STOR);
1194 module_init(usb_stor_init);
1195 module_exit(usb_stor_exit);