dm crypt: add merge
[safe/jmp/linux-2.6] / drivers / usb / storage / usb.c
index dd10863..e268aac 100644 (file)
  * 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#include <linux/config.h>
 #include <linux/sched.h>
 #include <linux/errno.h>
-#include <linux/suspend.h>
+#include <linux/freezer.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/kthread.h>
 #include <linux/mutex.h>
+#include <linux/utsname.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
 #ifdef CONFIG_USB_STORAGE_ALAUDA
 #include "alauda.h"
 #endif
+#ifdef CONFIG_USB_STORAGE_KARMA
+#include "karma.h"
+#endif
+#ifdef CONFIG_USB_STORAGE_CYPRESS_ATACB
+#include "cypress_atacb.h"
+#endif
 
 /* Some informational data */
 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
@@ -109,13 +115,6 @@ module_param(delay_use, uint, S_IRUGO | S_IWUSR);
 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
 
 
-/* These are used to make sure the module doesn't unload before all the
- * threads have exited.
- */
-static atomic_t total_threads = ATOMIC_INIT(0);
-static DECLARE_COMPLETION(threads_gone);
-
-
 /*
  * The entries in this table correspond, line for line,
  * with the entries of us_unusual_dev_list[].
@@ -191,10 +190,9 @@ static int storage_suspend(struct usb_interface *iface, pm_message_t message)
        /* Wait until no command is running */
        mutex_lock(&us->dev_mutex);
 
-       US_DEBUGP("%s\n", __FUNCTION__);
+       US_DEBUGP("%s\n", __func__);
        if (us->suspend_resume_hook)
                (us->suspend_resume_hook)(us, US_SUSPEND);
-       iface->dev.power.power_state.event = message.event;
 
        /* When runtime PM is working, we'll set a flag to indicate
         * whether we should autoresume when a SCSI request arrives. */
@@ -209,18 +207,63 @@ static int storage_resume(struct usb_interface *iface)
 
        mutex_lock(&us->dev_mutex);
 
-       US_DEBUGP("%s\n", __FUNCTION__);
+       US_DEBUGP("%s\n", __func__);
        if (us->suspend_resume_hook)
                (us->suspend_resume_hook)(us, US_RESUME);
-       iface->dev.power.power_state.event = PM_EVENT_ON;
 
        mutex_unlock(&us->dev_mutex);
        return 0;
 }
 
+static int storage_reset_resume(struct usb_interface *iface)
+{
+       struct us_data *us = usb_get_intfdata(iface);
+
+       US_DEBUGP("%s\n", __func__);
+
+       /* 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;
+}
+
 #endif /* CONFIG_PM */
 
 /*
+ * The next two routines get called just before and just after
+ * a USB port reset, whether from this driver or a different one.
+ */
+
+static int storage_pre_reset(struct usb_interface *iface)
+{
+       struct us_data *us = usb_get_intfdata(iface);
+
+       US_DEBUGP("%s\n", __func__);
+
+       /* Make sure no command runs during the reset */
+       mutex_lock(&us->dev_mutex);
+       return 0;
+}
+
+static int storage_post_reset(struct usb_interface *iface)
+{
+       struct us_data *us = usb_get_intfdata(iface);
+
+       US_DEBUGP("%s\n", __func__);
+
+       /* Report the reset to the SCSI core */
+       usb_stor_report_bus_reset(us);
+
+       /* FIXME: Notify the subdrivers that they need to reinitialize
+        * the device */
+
+       mutex_unlock(&us->dev_mutex);
+       return 0;
+}
+
+/*
  * 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
@@ -267,8 +310,6 @@ static int usb_stor_control_thread(void * __us)
        struct us_data *us = (struct us_data *)__us;
        struct Scsi_Host *host = us_to_host(us);
 
-       current->flags |= PF_NOFREEZE;
-
        for(;;) {
                US_DEBUGP("*** thread sleeping.\n");
                if(down_interruptible(&us->sema))
@@ -343,8 +384,12 @@ static int usb_stor_control_thread(void * __us)
                /* lock access to the state */
                scsi_lock(host);
 
+               /* did the command already complete because of a disconnect? */
+               if (!us->srb)
+                       ;               /* nothing to do */
+
                /* indicate that the command is done */
-               if (us->srb->result != DID_ABORT << 16) {
+               else if (us->srb->result != DID_ABORT << 16) {
                        US_DEBUGP("scsi cmd done, result=0x%x\n", 
                                   us->srb->result);
                        us->srb->scsi_done(us->srb);
@@ -374,23 +419,15 @@ SkipForAbort:
                mutex_unlock(&us->dev_mutex);
        } /* for (;;) */
 
-       scsi_host_put(host);
-
-       /* notify the exit routine that we're actually exiting now 
-        *
-        * complete()/wait_for_completion() is similar to up()/down(),
-        * except that complete() is safe in the case where the structure
-        * is getting deleted in a parallel mode of execution (i.e. just
-        * after the down() -- that's necessary for the thread-shutdown
-        * case.
-        *
-        * complete_and_exit() goes even further than this -- it is safe in
-        * the case that the thread of the caller is going away (not just
-        * the structure) -- this is necessary for the module-remove case.
-        * This is important in preemption kernels, which transfer the flow
-        * of execution immediately upon a complete().
-        */
-       complete_and_exit(&threads_gone, 0);
+       /* 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;
 }      
 
 /***********************************************************************
@@ -400,7 +437,7 @@ SkipForAbort:
 /* Associate our private data with the USB device */
 static int associate_dev(struct us_data *us, struct usb_interface *intf)
 {
-       US_DEBUGP("-- %s\n", __FUNCTION__);
+       US_DEBUGP("-- %s\n", __func__);
 
        /* Fill in the device-related fields */
        us->pusb_dev = interface_to_usbdev(intf);
@@ -448,7 +485,7 @@ static struct us_unusual_dev *find_unusual(const struct usb_device_id *id)
 }
 
 /* Get the unusual_devs entries and the string descriptors */
-static void get_device_info(struct us_data *us, const struct usb_device_id *id)
+static int get_device_info(struct us_data *us, const struct usb_device_id *id)
 {
        struct usb_device *dev = us->pusb_dev;
        struct usb_interface_descriptor *idesc =
@@ -465,6 +502,11 @@ static void get_device_info(struct us_data *us, const struct usb_device_id *id)
                        unusual_dev->useTransport;
        us->flags = USB_US_ORIG_FLAGS(id->driver_info);
 
+       if (us->flags & US_FL_IGNORE_DEVICE) {
+               printk(KERN_INFO USB_STORAGE "device ignored\n");
+               return -ENODEV;
+       }
+
        /*
         * This flag is only needed when we're in high-speed, so let's
         * disable it if we're in full-speed
@@ -494,16 +536,21 @@ static void get_device_info(struct us_data *us, const struct usb_device_id *id)
                if (msg >= 0 && !(us->flags & US_FL_NEED_OVERRIDE))
                        printk(KERN_NOTICE USB_STORAGE "This device "
                                "(%04x,%04x,%04x S %02x P %02x)"
-                               " has %s in unusual_devs.h\n"
+                               " has %s in unusual_devs.h (kernel"
+                               " %s)\n"
                                "   Please send a copy of this message to "
-                               "<linux-usb-devel@lists.sourceforge.net>\n",
+                               "<linux-usb@vger.kernel.org> and "
+                               "<usb-storage@lists.one-eyed-alien.net>\n",
                                le16_to_cpu(ddesc->idVendor),
                                le16_to_cpu(ddesc->idProduct),
                                le16_to_cpu(ddesc->bcdDevice),
                                idesc->bInterfaceSubClass,
                                idesc->bInterfaceProtocol,
-                               msgs[msg]);
+                               msgs[msg],
+                               utsname()->release);
        }
+
+       return 0;
 }
 
 /* Get the transport settings */
@@ -593,6 +640,23 @@ static int get_transport(struct us_data *us)
                break;
 #endif
 
+#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
+
+#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
+
        default:
                return -EIO;
        }
@@ -648,12 +712,10 @@ static int get_protocol(struct us_data *us)
                break;
 #endif
 
-#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;
+#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
 
@@ -676,28 +738,27 @@ static int get_pipes(struct us_data *us)
        struct usb_endpoint_descriptor *ep_int = NULL;
 
        /*
-        * Find the endpoints we need.
+        * Find the first endpoint of each type we need.
         * We are expecting a minimum of 2 endpoints - in and out (bulk).
-        * An optional interrupt is OK (necessary for CBI protocol).
+        * An optional interrupt-in is OK (necessary for CBI protocol).
         * We will ignore any others.
         */
        for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
                ep = &altsetting->endpoint[i].desc;
 
-               /* Is it a BULK endpoint? */
-               if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
-                               == USB_ENDPOINT_XFER_BULK) {
-                       /* BULK in or out? */
-                       if (ep->bEndpointAddress & USB_DIR_IN)
-                               ep_in = ep;
-                       else
-                               ep_out = ep;
+               if (usb_endpoint_xfer_bulk(ep)) {
+                       if (usb_endpoint_dir_in(ep)) {
+                               if (!ep_in)
+                                       ep_in = ep;
+                       } else {
+                               if (!ep_out)
+                                       ep_out = ep;
+                       }
                }
 
-               /* Is it an interrupt endpoint? */
-               else if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
-                               == USB_ENDPOINT_XFER_INT) {
-                       ep_int = ep;
+               else if (usb_endpoint_is_int_in(ep)) {
+                       if (!ep_int)
+                               ep_int = ep;
                }
        }
 
@@ -742,19 +803,13 @@ static int usb_stor_acquire_resources(struct us_data *us)
        }
 
        /* Start up our control thread */
-       th = kthread_create(usb_stor_control_thread, us, "usb-storage");
+       th = kthread_run(usb_stor_control_thread, us, "usb-storage");
        if (IS_ERR(th)) {
                printk(KERN_WARNING USB_STORAGE 
                       "Unable to start control thread\n");
                return PTR_ERR(th);
        }
-
-       /* Take a reference to the host for the control thread and
-        * count it among all the threads we have launched.  Then
-        * start it up. */
-       scsi_host_get(us_to_host(us));
-       atomic_inc(&total_threads);
-       wake_up_process(th);
+       us->ctl_thread = th;
 
        return 0;
 }
@@ -762,7 +817,7 @@ static int usb_stor_acquire_resources(struct us_data *us)
 /* Release all our dynamic resources */
 static void usb_stor_release_resources(struct us_data *us)
 {
-       US_DEBUGP("-- %s\n", __FUNCTION__);
+       US_DEBUGP("-- %s\n", __func__);
 
        /* Tell the control thread to exit.  The SCSI host must
         * already have been removed so it won't try to queue
@@ -771,6 +826,8 @@ static void usb_stor_release_resources(struct us_data *us)
        US_DEBUGP("-- sending exit command to thread\n");
        set_bit(US_FLIDX_DISCONNECTING, &us->flags);
        up(&us->sema);
+       if (us->ctl_thread)
+               kthread_stop(us->ctl_thread);
 
        /* Call the destructor routine, if it exists */
        if (us->extra_destructor) {
@@ -786,7 +843,7 @@ static void usb_stor_release_resources(struct us_data *us)
 /* Dissociate from the USB device */
 static void dissociate_dev(struct us_data *us)
 {
-       US_DEBUGP("-- %s\n", __FUNCTION__);
+       US_DEBUGP("-- %s\n", __func__);
 
        kfree(us->sensebuf);
 
@@ -806,32 +863,35 @@ static void dissociate_dev(struct us_data *us)
  * the host */
 static void quiesce_and_remove_host(struct us_data *us)
 {
+       struct Scsi_Host *host = us_to_host(us);
+
        /* Prevent new USB transfers, stop the current command, and
         * interrupt a SCSI-scan or device-reset delay */
+       scsi_lock(host);
        set_bit(US_FLIDX_DISCONNECTING, &us->flags);
+       scsi_unlock(host);
        usb_stor_stop_transport(us);
        wake_up(&us->delay_wait);
 
-       /* It doesn't matter if the SCSI-scanning thread is still running.
-        * The thread will exit when it sees the DISCONNECTING flag. */
-
-       /* Wait for the current command to finish, then remove the host */
-       mutex_lock(&us->dev_mutex);
-       mutex_unlock(&us->dev_mutex);
-
        /* queuecommand won't accept any new commands and the control
         * thread won't execute a previously-queued command.  If there
         * is such a command pending, complete it with an error. */
+       mutex_lock(&us->dev_mutex);
        if (us->srb) {
                us->srb->result = DID_NO_CONNECT << 16;
-               scsi_lock(us_to_host(us));
+               scsi_lock(host);
                us->srb->scsi_done(us->srb);
                us->srb = NULL;
-               scsi_unlock(us_to_host(us));
+               complete(&us->notify);          /* in case of an abort */
+               scsi_unlock(host);
        }
+       mutex_unlock(&us->dev_mutex);
 
        /* Now we own no commands so it's safe to remove the SCSI host */
-       scsi_remove_host(us_to_host(us));
+       scsi_remove_host(host);
+
+       /* Wait for the SCSI-scanning thread to stop */
+       wait_for_completion(&us->scanning_done);
 }
 
 /* Second stage of disconnect processing: deallocate all resources */
@@ -853,16 +913,14 @@ static int usb_stor_scan_thread(void * __us)
        printk(KERN_DEBUG
                "usb-storage: device found at %d\n", us->pusb_dev->devnum);
 
+       set_freezable();
        /* 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");
-retry:
-               wait_event_interruptible_timeout(us->delay_wait,
+               wait_event_freezable_timeout(us->delay_wait,
                                test_bit(US_FLIDX_DISCONNECTING, &us->flags),
                                delay_use * HZ);
-               if (try_to_freeze())
-                       goto retry;
        }
 
        /* If the device is still connected, perform the scanning */
@@ -881,8 +939,7 @@ retry:
                /* Should we unbind if no devices were detected? */
        }
 
-       scsi_host_put(us_to_host(us));
-       complete_and_exit(&threads_gone, 0);
+       complete_and_exit(&us->scanning_done, 0);
 }
 
 
@@ -911,12 +968,17 @@ static int storage_probe(struct usb_interface *intf,
                return -ENOMEM;
        }
 
+       /*
+        * Allow 16-byte CDBs and thus > 2TB
+        */
+       host->max_cmd_len = 16;
        us = host_to_us(host);
        memset(us, 0, sizeof(struct us_data));
        mutex_init(&(us->dev_mutex));
        init_MUTEX_LOCKED(&(us->sema));
        init_completion(&(us->notify));
        init_waitqueue_head(&us->delay_wait);
+       init_completion(&us->scanning_done);
 
        /* Associate the us_data structure with the USB device */
        result = associate_dev(us, intf);
@@ -930,7 +992,9 @@ static int storage_probe(struct usb_interface *intf,
         * of the match from the usb_device_id table, so we can find the
         * corresponding entry in the private table.
         */
-       get_device_info(us, id);
+       result = get_device_info(us, id);
+       if (result)
+               goto BadDevice;
 
        /* Get the transport, protocol, and pipe settings */
        result = get_transport(us);
@@ -964,11 +1028,6 @@ static int storage_probe(struct usb_interface *intf,
                goto BadDevice;
        }
 
-       /* Take a reference to the host for the scanning thread and
-        * count it among all the threads we have launched.  Then
-        * start it up. */
-       scsi_host_get(us_to_host(us));
-       atomic_inc(&total_threads);
        wake_up_process(th);
 
        return 0;
@@ -1001,7 +1060,10 @@ static struct usb_driver usb_storage_driver = {
 #ifdef CONFIG_PM
        .suspend =      storage_suspend,
        .resume =       storage_resume,
+       .reset_resume = storage_reset_resume,
 #endif
+       .pre_reset =    storage_pre_reset,
+       .post_reset =   storage_post_reset,
        .id_table =     storage_usb_ids,
 };
 
@@ -1030,16 +1092,6 @@ static void __exit usb_stor_exit(void)
        US_DEBUGP("-- calling usb_deregister()\n");
        usb_deregister(&usb_storage_driver) ;
 
-       /* Don't return until all of our control and scanning threads
-        * have exited.  Since each thread signals threads_gone as its
-        * last act, we have to call wait_for_completion the right number
-        * of times.
-        */
-       while (atomic_read(&total_threads) > 0) {
-               wait_for_completion(&threads_gone);
-               atomic_dec(&total_threads);
-       }
-
        usb_usual_clear_present(USB_US_TYPE_STOR);
 }