USB: unusual_devs entry for Lacie DVD+-RW
[safe/jmp/linux-2.6] / drivers / usb / storage / usb.c
index 5164900..8d7bdcb 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/module.h>
 #include <linux/init.h>
 #include <linux/slab.h>
+#include <linux/kthread.h>
+#include <linux/mutex.h>
+#include <linux/utsrelease.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
@@ -93,6 +95,9 @@
 #ifdef CONFIG_USB_STORAGE_ONETOUCH
 #include "onetouch.h"
 #endif
+#ifdef CONFIG_USB_STORAGE_ALAUDA
+#include "alauda.h"
+#endif
 
 /* Some informational data */
 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
@@ -111,54 +116,33 @@ static atomic_t total_threads = ATOMIC_INIT(0);
 static DECLARE_COMPLETION(threads_gone);
 
 
-static int storage_probe(struct usb_interface *iface,
-                        const struct usb_device_id *id);
-
-static void storage_disconnect(struct usb_interface *iface);
-
-/* The entries in this table, except for final ones here
- * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
- * line for line with the entries of us_unsuaul_dev_list[].
+/*
+ * The entries in this table correspond, line for line,
+ * with the entries of us_unusual_dev_list[].
  */
+#ifndef CONFIG_USB_LIBUSUAL
 
 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
                    vendorName, productName,useProtocol, useTransport, \
                    initFunction, flags) \
-{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
+{ 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) }
 
 static struct usb_device_id storage_usb_ids [] = {
 
 #      include "unusual_devs.h"
 #undef UNUSUAL_DEV
-       /* Control/Bulk transport for all SubClass values */
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CB) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CB) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CB) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CB) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CB) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CB) },
-
-       /* Control/Bulk/Interrupt transport for all SubClass values */
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_CBI) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_CBI) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_CBI) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_CBI) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_CBI) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_CBI) },
-
-       /* Bulk-only transport for all SubClass values */
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC, US_PR_BULK) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020, US_PR_BULK) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_QIC, US_PR_BULK) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_UFI, US_PR_BULK) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8070, US_PR_BULK) },
-       { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
-
+#undef USUAL_DEV
        /* Terminating entry */
        { }
 };
 
 MODULE_DEVICE_TABLE (usb, storage_usb_ids);
+#endif /* CONFIG_USB_LIBUSUAL */
 
 /* This is the list of devices we recognize, along with their flag data */
 
@@ -171,7 +155,6 @@ MODULE_DEVICE_TABLE (usb, storage_usb_ids);
  * are free to use as many characters as you like.
  */
 
-#undef UNUSUAL_DEV
 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
                    vendor_name, product_name, use_protocol, use_transport, \
                    init_function, Flags) \
@@ -181,65 +164,92 @@ MODULE_DEVICE_TABLE (usb, storage_usb_ids);
        .useProtocol = use_protocol,    \
        .useTransport = use_transport,  \
        .initFunction = init_function,  \
-       .flags = Flags, \
+}
+
+#define USUAL_DEV(use_protocol, use_transport, use_type) \
+{ \
+       .useProtocol = use_protocol,    \
+       .useTransport = use_transport,  \
 }
 
 static struct us_unusual_dev us_unusual_dev_list[] = {
 #      include "unusual_devs.h" 
 #      undef UNUSUAL_DEV
-       /* Control/Bulk transport for all SubClass values */
-       { .useProtocol = US_SC_RBC,
-         .useTransport = US_PR_CB},
-       { .useProtocol = US_SC_8020,
-         .useTransport = US_PR_CB},
-       { .useProtocol = US_SC_QIC,
-         .useTransport = US_PR_CB},
-       { .useProtocol = US_SC_UFI,
-         .useTransport = US_PR_CB},
-       { .useProtocol = US_SC_8070,
-         .useTransport = US_PR_CB},
-       { .useProtocol = US_SC_SCSI,
-         .useTransport = US_PR_CB},
-
-       /* Control/Bulk/Interrupt transport for all SubClass values */
-       { .useProtocol = US_SC_RBC,
-         .useTransport = US_PR_CBI},
-       { .useProtocol = US_SC_8020,
-         .useTransport = US_PR_CBI},
-       { .useProtocol = US_SC_QIC,
-         .useTransport = US_PR_CBI},
-       { .useProtocol = US_SC_UFI,
-         .useTransport = US_PR_CBI},
-       { .useProtocol = US_SC_8070,
-         .useTransport = US_PR_CBI},
-       { .useProtocol = US_SC_SCSI,
-         .useTransport = US_PR_CBI},
-
-       /* Bulk-only transport for all SubClass values */
-       { .useProtocol = US_SC_RBC,
-         .useTransport = US_PR_BULK},
-       { .useProtocol = US_SC_8020,
-         .useTransport = US_PR_BULK},
-       { .useProtocol = US_SC_QIC,
-         .useTransport = US_PR_BULK},
-       { .useProtocol = US_SC_UFI,
-         .useTransport = US_PR_BULK},
-       { .useProtocol = US_SC_8070,
-         .useTransport = US_PR_BULK},
-       { .useProtocol = US_SC_SCSI,
-         .useTransport = US_PR_BULK},
+#      undef USUAL_DEV
 
        /* Terminating entry */
        { NULL }
 };
 
-static struct usb_driver usb_storage_driver = {
-       .owner =        THIS_MODULE,
-       .name =         "usb-storage",
-       .probe =        storage_probe,
-       .disconnect =   storage_disconnect,
-       .id_table =     storage_usb_ids,
-};
+
+#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 */
+       mutex_lock(&us->dev_mutex);
+
+       US_DEBUGP("%s\n", __FUNCTION__);
+       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. */
+
+       mutex_unlock(&us->dev_mutex);
+       return 0;
+}
+
+static int storage_resume(struct usb_interface *iface)
+{
+       struct us_data *us = usb_get_intfdata(iface);
+
+       mutex_lock(&us->dev_mutex);
+
+       US_DEBUGP("%s\n", __FUNCTION__);
+       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;
+}
+
+#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 void storage_pre_reset(struct usb_interface *iface)
+{
+       struct us_data *us = usb_get_intfdata(iface);
+
+       US_DEBUGP("%s\n", __FUNCTION__);
+
+       /* Make sure no command runs during the reset */
+       mutex_lock(&us->dev_mutex);
+}
+
+static void storage_post_reset(struct usb_interface *iface)
+{
+       struct us_data *us = usb_get_intfdata(iface);
+
+       US_DEBUGP("%s\n", __FUNCTION__);
+
+       /* Report the reset to the SCSI core */
+       scsi_lock(us_to_host(us));
+       usb_stor_report_bus_reset(us);
+       scsi_unlock(us_to_host(us));
+
+       /* FIXME: Notify the subdrivers that they need to reinitialize
+        * the device */
+       mutex_unlock(&us->dev_mutex);
+}
 
 /*
  * fill_inquiry_response takes an unsigned char array (which must
@@ -288,22 +298,7 @@ static int usb_stor_control_thread(void * __us)
        struct us_data *us = (struct us_data *)__us;
        struct Scsi_Host *host = us_to_host(us);
 
-       lock_kernel();
-
-       /*
-        * This thread doesn't need any user-level access,
-        * so get rid of all our resources.
-        */
-       daemonize("usb-storage");
        current->flags |= PF_NOFREEZE;
-       unlock_kernel();
-
-       /* acquire a reference to the host, so it won't be deallocated
-        * until we're ready to exit */
-       scsi_host_get(host);
-
-       /* signal that we've started the thread */
-       complete(&(us->notify));
 
        for(;;) {
                US_DEBUGP("*** thread sleeping.\n");
@@ -313,12 +308,12 @@ static int usb_stor_control_thread(void * __us)
                US_DEBUGP("*** thread awakened.\n");
 
                /* lock the device pointers */
-               down(&(us->dev_semaphore));
+               mutex_lock(&(us->dev_mutex));
 
                /* if the device has disconnected, we are free to exit */
                if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
                        US_DEBUGP("-- exiting\n");
-                       up(&(us->dev_semaphore));
+                       mutex_unlock(&us->dev_mutex);
                        break;
                }
 
@@ -379,8 +374,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);
@@ -407,7 +406,7 @@ SkipForAbort:
                scsi_unlock(host);
 
                /* unlock the device pointers */
-               up(&(us->dev_semaphore));
+               mutex_unlock(&us->dev_mutex);
        } /* for (;;) */
 
        scsi_host_put(host);
@@ -467,17 +466,29 @@ static int associate_dev(struct us_data *us, struct usb_interface *intf)
                US_DEBUGP("I/O buffer allocation failed\n");
                return -ENOMEM;
        }
+
+       us->sensebuf = kmalloc(US_SENSE_SIZE, GFP_KERNEL);
+       if (!us->sensebuf) {
+               US_DEBUGP("Sense buffer allocation failed\n");
+               return -ENOMEM;
+       }
        return 0;
 }
 
+/* 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];
+}
+
 /* Get the unusual_devs entries and the string descriptors */
-static void get_device_info(struct us_data *us, int id_index)
+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 =
                &us->pusb_intf->cur_altsetting->desc;
-       struct us_unusual_dev *unusual_dev = &us_unusual_dev_list[id_index];
-       struct usb_device_id *id = &storage_usb_ids[id_index];
+       struct us_unusual_dev *unusual_dev = find_unusual(id);
 
        /* Store the entries */
        us->unusual_dev = unusual_dev;
@@ -487,7 +498,12 @@ static void get_device_info(struct us_data *us, int id_index)
        us->protocol = (unusual_dev->useTransport == US_PR_DEVICE) ?
                        idesc->bInterfaceProtocol :
                        unusual_dev->useTransport;
-       us->flags = unusual_dev->flags;
+       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
@@ -502,7 +518,7 @@ static void get_device_info(struct us_data *us, int id_index)
         * from the unusual_devs.h table.
         */
        if (id->idVendor || id->idProduct) {
-               static char *msgs[3] = {
+               static const char *msgs[3] = {
                        "an unneeded SubClass entry",
                        "an unneeded Protocol entry",
                        "unneeded SubClass and Protocol entries"};
@@ -515,10 +531,11 @@ static void get_device_info(struct us_data *us, int id_index)
                if (unusual_dev->useTransport != US_PR_DEVICE &&
                        us->protocol == idesc->bInterfaceProtocol)
                        msg += 2;
-               if (msg >= 0 && !(unusual_dev->flags & US_FL_NEED_OVERRIDE))
+               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",
                                le16_to_cpu(ddesc->idVendor),
@@ -526,8 +543,11 @@ static void get_device_info(struct us_data *us, int id_index)
                                le16_to_cpu(ddesc->bcdDevice),
                                idesc->bInterfaceSubClass,
                                idesc->bInterfaceProtocol,
-                               msgs[msg]);
+                               msgs[msg],
+                               UTS_RELEASE);
        }
+
+       return 0;
 }
 
 /* Get the transport settings */
@@ -617,6 +637,15 @@ 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
+
        default:
                return -EIO;
        }
@@ -740,6 +769,7 @@ static int get_pipes(struct us_data *us)
 static int usb_stor_acquire_resources(struct us_data *us)
 {
        int p;
+       struct task_struct *th;
 
        us->current_urb = usb_alloc_urb(0, GFP_KERNEL);
        if (!us->current_urb) {
@@ -747,38 +777,28 @@ static int usb_stor_acquire_resources(struct us_data *us)
                return -ENOMEM;
        }
 
-       /* Lock the device while we carry out the next two operations */
-       down(&us->dev_semaphore);
-
-       /* For bulk-only devices, determine the max LUN value */
-       if (us->protocol == US_PR_BULK) {
-               p = usb_stor_Bulk_max_lun(us);
-               if (p < 0) {
-                       up(&us->dev_semaphore);
-                       return p;
-               }
-               us->max_lun = p;
-       }
-
        /* Just before we start our control thread, initialize
         * the device if it needs initialization */
-       if (us->unusual_dev->initFunction)
-               us->unusual_dev->initFunction(us);
-
-       up(&us->dev_semaphore);
+       if (us->unusual_dev->initFunction) {
+               p = us->unusual_dev->initFunction(us);
+               if (p)
+                       return p;
+       }
 
        /* Start up our control thread */
-       p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
-       if (p < 0) {
+       th = kthread_create(usb_stor_control_thread, us, "usb-storage");
+       if (IS_ERR(th)) {
                printk(KERN_WARNING USB_STORAGE 
                       "Unable to start control thread\n");
-               return p;
+               return PTR_ERR(th);
        }
-       us->pid = p;
-       atomic_inc(&total_threads);
 
-       /* Wait for the thread to start */
-       wait_for_completion(&(us->notify));
+       /* 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);
 
        return 0;
 }
@@ -812,6 +832,8 @@ static void dissociate_dev(struct us_data *us)
 {
        US_DEBUGP("-- %s\n", __FUNCTION__);
 
+       kfree(us->sensebuf);
+
        /* Free the device-related DMA-mapped buffers */
        if (us->cr)
                usb_buffer_free(us->pusb_dev, sizeof(*us->cr), us->cr,
@@ -828,32 +850,34 @@ 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 */
-       down(&us->dev_semaphore);
-       up(&us->dev_semaphore);
-
        /* 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));
+               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);
 }
 
 /* Second stage of disconnect processing: deallocate all resources */
@@ -872,21 +896,6 @@ static int usb_stor_scan_thread(void * __us)
 {
        struct us_data *us = (struct us_data *)__us;
 
-       /*
-        * This thread doesn't need any user-level access,
-        * so get rid of all our resources.
-        */
-       lock_kernel();
-       daemonize("usb-stor-scan");
-       unlock_kernel();
-
-       /* Acquire a reference to the host, so it won't be deallocated
-        * until we're ready to exit */
-       scsi_host_get(us_to_host(us));
-
-       /* Signal that we've started the thread */
-       complete(&(us->notify));
-
        printk(KERN_DEBUG
                "usb-storage: device found at %d\n", us->pusb_dev->devnum);
 
@@ -904,6 +913,14 @@ retry:
 
        /* If the device is still connected, perform the scanning */
        if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
+
+               /* For bulk-only devices, determine the max LUN value */
+               if (us->protocol == US_PR_BULK &&
+                               !(us->flags & US_FL_SINGLE_LUN)) {
+                       mutex_lock(&us->dev_mutex);
+                       us->max_lun = usb_stor_Bulk_max_lun(us);
+                       mutex_unlock(&us->dev_mutex);
+               }
                scsi_scan_host(us_to_host(us));
                printk(KERN_DEBUG "usb-storage: device scan complete\n");
 
@@ -921,8 +938,11 @@ static int storage_probe(struct usb_interface *intf,
 {
        struct Scsi_Host *host;
        struct us_data *us;
-       const int id_index = id - storage_usb_ids; 
        int result;
+       struct task_struct *th;
+
+       if (usb_usual_check_type(id, USB_US_TYPE_STOR))
+               return -ENXIO;
 
        US_DEBUGP("USB Mass Storage device detected\n");
 
@@ -939,7 +959,7 @@ static int storage_probe(struct usb_interface *intf,
 
        us = host_to_us(host);
        memset(us, 0, sizeof(struct us_data));
-       init_MUTEX(&(us->dev_semaphore));
+       mutex_init(&(us->dev_mutex));
        init_MUTEX_LOCKED(&(us->sema));
        init_completion(&(us->notify));
        init_waitqueue_head(&us->delay_wait);
@@ -956,29 +976,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_index);
-
-#ifdef CONFIG_USB_STORAGE_SDDR09
-       if (us->protocol == US_PR_EUSB_SDDR09 ||
-                       us->protocol == US_PR_DPCM_USB) {
-               /* set the configuration -- STALL is an acceptable response here */
-               if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
-                       US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev
-                               ->actconfig->desc.bConfigurationValue);
-                       goto BadDevice;
-               }
-               result = usb_reset_configuration(us->pusb_dev);
-
-               US_DEBUGP("Result of usb_reset_configuration is %d\n", result);
-               if (result == -EPIPE) {
-                       US_DEBUGP("-- stall on control interface\n");
-               } else if (result != 0) {
-                       /* it's not a stall, but another error -- time to bail */
-                       US_DEBUGP("-- Unknown error.  Rejecting device\n");
-                       goto BadDevice;
-               }
-       }
-#endif
+       result = get_device_info(us, id);
+       if (result)
+               goto BadDevice;
 
        /* Get the transport, protocol, and pipe settings */
        result = get_transport(us);
@@ -1003,17 +1003,21 @@ static int storage_probe(struct usb_interface *intf,
        }
 
        /* Start up the thread for delayed SCSI-device scanning */
-       result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM);
-       if (result < 0) {
+       th = kthread_create(usb_stor_scan_thread, us, "usb-stor-scan");
+       if (IS_ERR(th)) {
                printk(KERN_WARNING USB_STORAGE 
                       "Unable to start the device-scanning thread\n");
                quiesce_and_remove_host(us);
+               result = PTR_ERR(th);
                goto BadDevice;
        }
-       atomic_inc(&total_threads);
 
-       /* Wait for the thread to start */
-       wait_for_completion(&(us->notify));
+       /* 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;
 
@@ -1038,6 +1042,19 @@ static void storage_disconnect(struct usb_interface *intf)
  * Initialization and registration
  ***********************************************************************/
 
+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,
+#endif
+       .pre_reset =    storage_pre_reset,
+       .post_reset =   storage_post_reset,
+       .id_table =     storage_usb_ids,
+};
+
 static int __init usb_stor_init(void)
 {
        int retval;
@@ -1045,9 +1062,10 @@ static int __init usb_stor_init(void)
 
        /* register the driver, return usb_register return code if error */
        retval = usb_register(&usb_storage_driver);
-       if (retval == 0)
+       if (retval == 0) {
                printk(KERN_INFO "USB Mass Storage support registered.\n");
-
+               usb_usual_set_present(USB_US_TYPE_STOR);
+       }
        return retval;
 }
 
@@ -1071,6 +1089,8 @@ static void __exit usb_stor_exit(void)
                wait_for_completion(&threads_gone);
                atomic_dec(&total_threads);
        }
+
+       usb_usual_clear_present(USB_US_TYPE_STOR);
 }
 
 module_init(usb_stor_init);