header cleaning: don't include smp_lock.h when not used
[safe/jmp/linux-2.6] / drivers / usb / serial / usb-serial.c
index 13f6592..87f3788 100644 (file)
@@ -16,7 +16,6 @@
  *
  */
 
-#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/spinlock.h>
+#include <linux/mutex.h>
 #include <linux/list.h>
-#include <linux/smp_lock.h>
 #include <asm/uaccess.h>
 #include <linux/usb.h>
-#include "usb-serial.h"
+#include <linux/usb/serial.h>
 #include "pl2303.h"
 
 /*
  * Version Information
  */
-#define DRIVER_VERSION "v2.0"
 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
 #define DRIVER_DESC "USB Serial Driver core"
 
+static void port_free(struct usb_serial_port *port);
+
 /* Driver structure we register with the USB core */
 static struct usb_driver usb_serial_driver = {
-       .owner =        THIS_MODULE,
        .name =         "usbserial",
        .probe =        usb_serial_probe,
        .disconnect =   usb_serial_disconnect,
+       .no_dynamic_id =        1,
 };
 
 /* There is no MODULE_DEVICE_TABLE for usbserial.c.  Instead
@@ -58,14 +58,19 @@ static struct usb_driver usb_serial_driver = {
 
 static int debug;
 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];     /* initially all NULL */
+static spinlock_t table_lock;
 static LIST_HEAD(usb_serial_driver_list);
 
 struct usb_serial *usb_serial_get_by_index(unsigned index)
 {
-       struct usb_serial *serial = serial_table[index];
+       struct usb_serial *serial;
+
+       spin_lock(&table_lock);
+       serial = serial_table[index];
 
        if (serial)
                kref_get(&serial->kref);
+       spin_unlock(&table_lock);
        return serial;
 }
 
@@ -77,6 +82,7 @@ static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_po
        dbg("%s %d", __FUNCTION__, num_ports);
 
        *minor = 0;
+       spin_lock(&table_lock);
        for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
                if (serial_table[i])
                        continue;
@@ -92,11 +98,16 @@ static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_po
                        continue;
 
                *minor = i;
+               j = 0;
                dbg("%s - minor base = %d", __FUNCTION__, *minor);
-               for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
+               for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
                        serial_table[i] = serial;
+                       serial->port[j++]->number = i;
+               }
+               spin_unlock(&table_lock);
                return serial;
        }
+       spin_unlock(&table_lock);
        return NULL;
 }
 
@@ -109,9 +120,11 @@ static void return_serial(struct usb_serial *serial)
        if (serial == NULL)
                return;
 
+       spin_lock(&table_lock);
        for (i = 0; i < serial->num_ports; ++i) {
                serial_table[serial->minor + i] = NULL;
        }
+       spin_unlock(&table_lock);
 }
 
 static void destroy_serial(struct kref *kref)
@@ -147,18 +160,7 @@ static void destroy_serial(struct kref *kref)
                        port = serial->port[i];
                        if (!port)
                                continue;
-                       usb_kill_urb(port->read_urb);
-                       usb_free_urb(port->read_urb);
-                       usb_kill_urb(port->write_urb);
-                       usb_free_urb(port->write_urb);
-                       usb_kill_urb(port->interrupt_in_urb);
-                       usb_free_urb(port->interrupt_in_urb);
-                       usb_kill_urb(port->interrupt_out_urb);
-                       usb_free_urb(port->interrupt_out_urb);
-                       kfree(port->bulk_in_buffer);
-                       kfree(port->bulk_out_buffer);
-                       kfree(port->interrupt_in_buffer);
-                       kfree(port->interrupt_out_buffer);
+                       port_free(port);
                }
        }
 
@@ -168,6 +170,11 @@ static void destroy_serial(struct kref *kref)
        kfree (serial);
 }
 
+void usb_serial_put(struct usb_serial *serial)
+{
+       kref_put(&serial->kref, destroy_serial);
+}
+
 /*****************************************************************************
  * Driver tty interface functions
  *****************************************************************************/
@@ -189,22 +196,31 @@ static int serial_open (struct tty_struct *tty, struct file * filp)
 
        portNumber = tty->index - serial->minor;
        port = serial->port[portNumber];
+       if (!port) {
+               retval = -ENODEV;
+               goto bailout_kref_put;
+       }
+
+       if (mutex_lock_interruptible(&port->mutex)) {
+               retval = -ERESTARTSYS;
+               goto bailout_kref_put;
+       }
         
        ++port->open_count;
 
-       if (port->open_count == 1) {
+       /* set up our port structure making the tty driver
+        * remember our port object, and us it */
+       tty->driver_data = port;
+       port->tty = tty;
 
-               /* set up our port structure making the tty driver
-                * remember our port object, and us it */
-               tty->driver_data = port;
-               port->tty = tty;
+       if (port->open_count == 1) {
 
                /* lock this module before we call it
                 * this may fail, which means we must bail out,
                 * safe because we are called with BKL held */
                if (!try_module_get(serial->type->driver.owner)) {
                        retval = -ENODEV;
-                       goto bailout_kref_put;
+                       goto bailout_mutex_unlock;
                }
 
                /* only call the device specific open if this 
@@ -214,13 +230,18 @@ static int serial_open (struct tty_struct *tty, struct file * filp)
                        goto bailout_module_put;
        }
 
+       mutex_unlock(&port->mutex);
        return 0;
 
 bailout_module_put:
        module_put(serial->type->driver.owner);
-bailout_kref_put:
-       kref_put(&serial->kref, destroy_serial);
+bailout_mutex_unlock:
        port->open_count = 0;
+       tty->driver_data = NULL;
+       port->tty = NULL;
+       mutex_unlock(&port->mutex);
+bailout_kref_put:
+       usb_serial_put(serial);
        return retval;
 }
 
@@ -233,8 +254,12 @@ static void serial_close(struct tty_struct *tty, struct file * filp)
 
        dbg("%s - port %d", __FUNCTION__, port->number);
 
-       if (port->open_count == 0)
+       mutex_lock(&port->mutex);
+
+       if (port->open_count == 0) {
+               mutex_unlock(&port->mutex);
                return;
+       }
 
        --port->open_count;
        if (port->open_count == 0) {
@@ -251,17 +276,22 @@ static void serial_close(struct tty_struct *tty, struct file * filp)
                module_put(port->serial->type->driver.owner);
        }
 
-       kref_put(&port->serial->kref, destroy_serial);
+       mutex_unlock(&port->mutex);
+       usb_serial_put(port->serial);
 }
 
 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
 {
        struct usb_serial_port *port = tty->driver_data;
-       int retval = -EINVAL;
+       int retval = -ENODEV;
+
+       if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
+               goto exit;
 
        dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
 
        if (!port->open_count) {
+               retval = -EINVAL;
                dbg("%s - port not opened", __FUNCTION__);
                goto exit;
        }
@@ -276,7 +306,10 @@ exit:
 static int serial_write_room (struct tty_struct *tty) 
 {
        struct usb_serial_port *port = tty->driver_data;
-       int retval = -EINVAL;
+       int retval = -ENODEV;
+
+       if (!port)
+               goto exit;
 
        dbg("%s - port %d", __FUNCTION__, port->number);
 
@@ -295,7 +328,10 @@ exit:
 static int serial_chars_in_buffer (struct tty_struct *tty) 
 {
        struct usb_serial_port *port = tty->driver_data;
-       int retval = -EINVAL;
+       int retval = -ENODEV;
+
+       if (!port)
+               goto exit;
 
        dbg("%s = port %d", __FUNCTION__, port->number);
 
@@ -315,6 +351,9 @@ static void serial_throttle (struct tty_struct * tty)
 {
        struct usb_serial_port *port = tty->driver_data;
 
+       if (!port)
+               return;
+
        dbg("%s - port %d", __FUNCTION__, port->number);
 
        if (!port->open_count) {
@@ -331,6 +370,9 @@ static void serial_unthrottle (struct tty_struct * tty)
 {
        struct usb_serial_port *port = tty->driver_data;
 
+       if (!port)
+               return;
+
        dbg("%s - port %d", __FUNCTION__, port->number);
 
        if (!port->open_count) {
@@ -348,6 +390,9 @@ static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned in
        struct usb_serial_port *port = tty->driver_data;
        int retval = -ENODEV;
 
+       if (!port)
+               goto exit;
+
        dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
 
        if (!port->open_count) {
@@ -365,10 +410,13 @@ exit:
        return retval;
 }
 
-static void serial_set_termios (struct tty_struct *tty, struct termios * old)
+static void serial_set_termios (struct tty_struct *tty, struct ktermios * old)
 {
        struct usb_serial_port *port = tty->driver_data;
 
+       if (!port)
+               return;
+
        dbg("%s - port %d", __FUNCTION__, port->number);
 
        if (!port->open_count) {
@@ -385,6 +433,9 @@ static void serial_break (struct tty_struct *tty, int break_state)
 {
        struct usb_serial_port *port = tty->driver_data;
 
+       if (!port)
+               return;
+
        dbg("%s - port %d", __FUNCTION__, port->number);
 
        if (!port->open_count) {
@@ -406,7 +457,7 @@ static int serial_read_proc (char *page, char **start, off_t off, int count, int
        char tmp[40];
 
        dbg("%s", __FUNCTION__);
-       length += sprintf (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);
+       length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
        for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
                serial = usb_serial_get_by_index(i);
                if (serial == NULL)
@@ -426,13 +477,15 @@ static int serial_read_proc (char *page, char **start, off_t off, int count, int
                length += sprintf (page+length, " path:%s", tmp);
                        
                length += sprintf (page+length, "\n");
-               if ((length + begin) > (off + count))
+               if ((length + begin) > (off + count)) {
+                       usb_serial_put(serial);
                        goto done;
+               }
                if ((length + begin) < off) {
                        begin += length;
                        length = 0;
                }
-               kref_put(&serial->kref, destroy_serial);
+               usb_serial_put(serial);
        }
        *eof = 1;
 done:
@@ -446,17 +499,19 @@ static int serial_tiocmget (struct tty_struct *tty, struct file *file)
 {
        struct usb_serial_port *port = tty->driver_data;
 
+       if (!port)
+               return -ENODEV;
+
        dbg("%s - port %d", __FUNCTION__, port->number);
 
        if (!port->open_count) {
                dbg("%s - port not open", __FUNCTION__);
-               goto exit;
+               return -ENODEV;
        }
 
        if (port->serial->type->tiocmget)
                return port->serial->type->tiocmget(port, file);
 
-exit:
        return -EINVAL;
 }
 
@@ -465,23 +520,36 @@ static int serial_tiocmset (struct tty_struct *tty, struct file *file,
 {
        struct usb_serial_port *port = tty->driver_data;
 
+       if (!port)
+               return -ENODEV;
+
        dbg("%s - port %d", __FUNCTION__, port->number);
 
        if (!port->open_count) {
                dbg("%s - port not open", __FUNCTION__);
-               goto exit;
+               return -ENODEV;
        }
 
        if (port->serial->type->tiocmset)
                return port->serial->type->tiocmset(port, file, set, clear);
 
-exit:
        return -EINVAL;
 }
 
-void usb_serial_port_softint(void *private)
+/*
+ * We would be calling tty_wakeup here, but unfortunately some line
+ * disciplines have an annoying habit of calling tty->write from
+ * the write wakeup callback (e.g. n_hdlc.c).
+ */
+void usb_serial_port_softint(struct usb_serial_port *port)
+{
+       schedule_work(&port->work);
+}
+
+static void usb_serial_port_work(struct work_struct *work)
 {
-       struct usb_serial_port *port = private;
+       struct usb_serial_port *port =
+               container_of(work, struct usb_serial_port, work);
        struct tty_struct *tty;
 
        dbg("%s - port %d", __FUNCTION__, port->number);
@@ -501,18 +569,29 @@ static void port_release(struct device *dev)
        struct usb_serial_port *port = to_usb_serial_port(dev);
 
        dbg ("%s - %s", __FUNCTION__, dev->bus_id);
+       port_free(port);
+}
+
+static void kill_traffic(struct usb_serial_port *port)
+{
        usb_kill_urb(port->read_urb);
-       usb_free_urb(port->read_urb);
        usb_kill_urb(port->write_urb);
-       usb_free_urb(port->write_urb);
        usb_kill_urb(port->interrupt_in_urb);
-       usb_free_urb(port->interrupt_in_urb);
        usb_kill_urb(port->interrupt_out_urb);
+}
+
+static void port_free(struct usb_serial_port *port)
+{
+       kill_traffic(port);
+       usb_free_urb(port->read_urb);
+       usb_free_urb(port->write_urb);
+       usb_free_urb(port->interrupt_in_urb);
        usb_free_urb(port->interrupt_out_urb);
        kfree(port->bulk_in_buffer);
        kfree(port->bulk_out_buffer);
        kfree(port->interrupt_in_buffer);
        kfree(port->interrupt_out_buffer);
+       flush_scheduled_work();         /* port->work */
        kfree(port);
 }
 
@@ -522,12 +601,11 @@ static struct usb_serial * create_serial (struct usb_device *dev,
 {
        struct usb_serial *serial;
 
-       serial = kmalloc (sizeof (*serial), GFP_KERNEL);
+       serial = kzalloc(sizeof(*serial), GFP_KERNEL);
        if (!serial) {
                dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
                return NULL;
        }
-       memset (serial, 0, sizeof(*serial));
        serial->dev = usb_get_dev(dev);
        serial->type = driver;
        serial->interface = interface;
@@ -536,20 +614,51 @@ static struct usb_serial * create_serial (struct usb_device *dev,
        return serial;
 }
 
+static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
+                                                   struct usb_serial_driver *drv)
+{
+       struct usb_dynid *dynid;
+
+       spin_lock(&drv->dynids.lock);
+       list_for_each_entry(dynid, &drv->dynids.list, node) {
+               if (usb_match_one_id(intf, &dynid->id)) {
+                       spin_unlock(&drv->dynids.lock);
+                       return &dynid->id;
+               }
+       }
+       spin_unlock(&drv->dynids.lock);
+       return NULL;
+}
+
+static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
+                                               struct usb_interface *intf)
+{
+       const struct usb_device_id *id;
+
+       id = usb_match_id(intf, drv->id_table);
+       if (id) {
+               dbg("static descriptor matches");
+               goto exit;
+       }
+       id = match_dynamic_id(intf, drv);
+       if (id)
+               dbg("dynamic descriptor matches");
+exit:
+       return id;
+}
+
 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
 {
        struct list_head *p;
        const struct usb_device_id *id;
        struct usb_serial_driver *t;
 
-       /* List trough know devices and see if the usb id matches */
+       /* Check if the usb id matches a known device */
        list_for_each(p, &usb_serial_driver_list) {
                t = list_entry(p, struct usb_serial_driver, driver_list);
-               id = usb_match_id(iface, t->id_table);
-               if (id != NULL) {
-                       dbg("descriptor matches");
+               id = get_iface_id(t, iface);
+               if (id)
                        return t;
-               }
        }
 
        return NULL;
@@ -579,14 +688,17 @@ int usb_serial_probe(struct usb_interface *interface,
        int num_ports = 0;
        int max_endpoints;
 
+       lock_kernel(); /* guard against unloading a serial driver module */
        type = search_serial_device(interface);
        if (!type) {
+               unlock_kernel();
                dbg("none matched");
                return -ENODEV;
        }
 
        serial = create_serial (dev, interface, type);
        if (!serial) {
+               unlock_kernel();
                dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
                return -ENOMEM;
        }
@@ -596,16 +708,18 @@ int usb_serial_probe(struct usb_interface *interface,
                const struct usb_device_id *id;
 
                if (!try_module_get(type->driver.owner)) {
+                       unlock_kernel();
                        dev_err(&interface->dev, "module get failed, exiting\n");
                        kfree (serial);
                        return -EIO;
                }
 
-               id = usb_match_id(interface, type->id_table);
+               id = get_iface_id(type, interface);
                retval = type->probe(serial, id);
                module_put(type->driver.owner);
 
                if (retval) {
+                       unlock_kernel();
                        dbg ("sub driver rejected device");
                        kfree (serial);
                        return retval;
@@ -617,33 +731,29 @@ int usb_serial_probe(struct usb_interface *interface,
        iface_desc = interface->cur_altsetting;
        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
                endpoint = &iface_desc->endpoint[i].desc;
-               
-               if ((endpoint->bEndpointAddress & 0x80) &&
-                   ((endpoint->bmAttributes & 3) == 0x02)) {
+
+               if (usb_endpoint_is_bulk_in(endpoint)) {
                        /* we found a bulk in endpoint */
                        dbg("found bulk in on endpoint %d", i);
                        bulk_in_endpoint[num_bulk_in] = endpoint;
                        ++num_bulk_in;
                }
 
-               if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
-                   ((endpoint->bmAttributes & 3) == 0x02)) {
+               if (usb_endpoint_is_bulk_out(endpoint)) {
                        /* we found a bulk out endpoint */
                        dbg("found bulk out on endpoint %d", i);
                        bulk_out_endpoint[num_bulk_out] = endpoint;
                        ++num_bulk_out;
                }
-               
-               if ((endpoint->bEndpointAddress & 0x80) &&
-                   ((endpoint->bmAttributes & 3) == 0x03)) {
+
+               if (usb_endpoint_is_int_in(endpoint)) {
                        /* we found a interrupt in endpoint */
                        dbg("found interrupt in on endpoint %d", i);
                        interrupt_in_endpoint[num_interrupt_in] = endpoint;
                        ++num_interrupt_in;
                }
 
-               if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
-                   ((endpoint->bmAttributes & 3) == 0x03)) {
+               if (usb_endpoint_is_int_out(endpoint)) {
                        /* we found an interrupt out endpoint */
                        dbg("found interrupt out on endpoint %d", i);
                        interrupt_out_endpoint[num_interrupt_out] = endpoint;
@@ -657,14 +767,15 @@ int usb_serial_probe(struct usb_interface *interface,
        if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
             (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
            ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
-            (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
+            (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
+           ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
+            (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID))) {
                if (interface != dev->actconfig->interface[0]) {
                        /* check out the endpoints of the other interface*/
                        iface_desc = dev->actconfig->interface[0]->cur_altsetting;
                        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
                                endpoint = &iface_desc->endpoint[i].desc;
-                               if ((endpoint->bEndpointAddress & 0x80) &&
-                                   ((endpoint->bmAttributes & 3) == 0x03)) {
+                               if (usb_endpoint_is_int_in(endpoint)) {
                                        /* we found a interrupt in endpoint */
                                        dbg("found interrupt in for Prolific device on separate interface");
                                        interrupt_in_endpoint[num_interrupt_in] = endpoint;
@@ -678,6 +789,7 @@ int usb_serial_probe(struct usb_interface *interface,
                 * properly during a later invocation of usb_serial_probe
                 */
                if (num_bulk_in == 0 || num_bulk_out == 0) {
+                       unlock_kernel();
                        dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
                        kfree (serial);
                        return -ENODEV;
@@ -693,6 +805,7 @@ int usb_serial_probe(struct usb_interface *interface,
        if (type == &usb_serial_generic_device) {
                num_ports = num_bulk_out;
                if (num_ports == 0) {
+                       unlock_kernel();
                        dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
                        kfree (serial);
                        return -EIO;
@@ -703,6 +816,7 @@ int usb_serial_probe(struct usb_interface *interface,
                /* if this device type has a calc_num_ports function, call it */
                if (type->calc_num_ports) {
                        if (!try_module_get(type->driver.owner)) {
+                               unlock_kernel();
                                dev_err(&interface->dev, "module get failed, exiting\n");
                                kfree (serial);
                                return -EIO;
@@ -714,13 +828,6 @@ int usb_serial_probe(struct usb_interface *interface,
                        num_ports = type->num_ports;
        }
 
-       if (get_free_serial (serial, num_ports, &minor) == NULL) {
-               dev_err(&interface->dev, "No more free serial devices\n");
-               kfree (serial);
-               return -ENOMEM;
-       }
-
-       serial->minor = minor;
        serial->num_ports = num_ports;
        serial->num_bulk_in = num_bulk_in;
        serial->num_bulk_out = num_bulk_out;
@@ -734,16 +841,17 @@ int usb_serial_probe(struct usb_interface *interface,
        max_endpoints = max(max_endpoints, num_interrupt_out);
        max_endpoints = max(max_endpoints, (int)serial->num_ports);
        serial->num_port_pointers = max_endpoints;
+       unlock_kernel();
+
        dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
        for (i = 0; i < max_endpoints; ++i) {
-               port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
+               port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
                if (!port)
                        goto probe_error;
-               memset(port, 0x00, sizeof(struct usb_serial_port));
-               port->number = i + serial->minor;
                port->serial = serial;
                spin_lock_init(&port->lock);
-               INIT_WORK(&port->work, usb_serial_port_softint, port);
+               mutex_init(&port->mutex);
+               INIT_WORK(&port->work, usb_serial_port_work);
                serial->port[i] = port;
        }
 
@@ -868,6 +976,12 @@ int usb_serial_probe(struct usb_interface *interface,
                }
        }
 
+       if (get_free_serial (serial, num_ports, &minor) == NULL) {
+               dev_err(&interface->dev, "No more free serial devices\n");
+               goto probe_error;
+       }
+       serial->minor = minor;
+
        /* register all of the individual ports with the driver core */
        for (i = 0; i < num_ports; ++i) {
                port = serial->port[i];
@@ -878,7 +992,10 @@ int usb_serial_probe(struct usb_interface *interface,
 
                snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
                dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
-               device_register (&port->dev);
+               retval = device_register(&port->dev);
+               if (retval)
+                       dev_err(&port->dev, "Error registering port device, "
+                               "continuing\n");
        }
 
        usb_serial_console_init (debug, minor);
@@ -893,38 +1010,31 @@ probe_error:
                port = serial->port[i];
                if (!port)
                        continue;
-               if (port->read_urb)
-                       usb_free_urb (port->read_urb);
+               usb_free_urb(port->read_urb);
                kfree(port->bulk_in_buffer);
        }
        for (i = 0; i < num_bulk_out; ++i) {
                port = serial->port[i];
                if (!port)
                        continue;
-               if (port->write_urb)
-                       usb_free_urb (port->write_urb);
+               usb_free_urb(port->write_urb);
                kfree(port->bulk_out_buffer);
        }
        for (i = 0; i < num_interrupt_in; ++i) {
                port = serial->port[i];
                if (!port)
                        continue;
-               if (port->interrupt_in_urb)
-                       usb_free_urb (port->interrupt_in_urb);
+               usb_free_urb(port->interrupt_in_urb);
                kfree(port->interrupt_in_buffer);
        }
        for (i = 0; i < num_interrupt_out; ++i) {
                port = serial->port[i];
                if (!port)
                        continue;
-               if (port->interrupt_out_urb)
-                       usb_free_urb (port->interrupt_out_urb);
+               usb_free_urb(port->interrupt_out_urb);
                kfree(port->interrupt_out_buffer);
        }
 
-       /* return the minor range that this device had */
-       return_serial (serial);
-
        /* free up any memory that we allocated */
        for (i = 0; i < serial->num_port_pointers; ++i)
                kfree(serial->port[i]);
@@ -939,23 +1049,27 @@ void usb_serial_disconnect(struct usb_interface *interface)
        struct device *dev = &interface->dev;
        struct usb_serial_port *port;
 
+       usb_serial_console_disconnect(serial);
        dbg ("%s", __FUNCTION__);
 
        usb_set_intfdata (interface, NULL);
        if (serial) {
                for (i = 0; i < serial->num_ports; ++i) {
                        port = serial->port[i];
-                       if (port && port->tty)
-                               tty_hangup(port->tty);
+                       if (port) {
+                               if (port->tty)
+                                       tty_hangup(port->tty);
+                               kill_traffic(port);
+                       }
                }
                /* let the last holder of this object 
                 * cause it to be cleaned up */
-               kref_put(&serial->kref, destroy_serial);
+               usb_serial_put(serial);
        }
        dev_info(dev, "device disconnected\n");
 }
 
-static struct tty_operations serial_ops = {
+static const struct tty_operations serial_ops = {
        .open =                 serial_open,
        .close =                serial_close,
        .write =                serial_write,
@@ -983,6 +1097,7 @@ static int __init usb_serial_init(void)
                return -ENOMEM;
 
        /* Initialize our global data */
+       spin_lock_init(&table_lock);
        for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
                serial_table[i] = NULL;
        }
@@ -995,13 +1110,12 @@ static int __init usb_serial_init(void)
 
        usb_serial_tty_driver->owner = THIS_MODULE;
        usb_serial_tty_driver->driver_name = "usbserial";
-       usb_serial_tty_driver->devfs_name = "usb/tts/";
        usb_serial_tty_driver->name =   "ttyUSB";
        usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
        usb_serial_tty_driver->minor_start = 0;
        usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
        usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
-       usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
+       usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
        usb_serial_tty_driver->init_termios = tty_std_termios;
        usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
        tty_set_operations(usb_serial_tty_driver, &serial_ops);
@@ -1025,7 +1139,7 @@ static int __init usb_serial_init(void)
                goto exit_generic;
        }
 
-       info(DRIVER_DESC " " DRIVER_VERSION);
+       info(DRIVER_DESC);
 
        return result;
 
@@ -1082,7 +1196,7 @@ static void fixup_generic(struct usb_serial_driver *device)
        set_to_generic_if_null(device, shutdown);
 }
 
-int usb_serial_register(struct usb_serial_driver *driver)
+int usb_serial_register(struct usb_serial_driver *driver) /* must be called with BKL held */
 {
        int retval;
 
@@ -1106,7 +1220,7 @@ int usb_serial_register(struct usb_serial_driver *driver)
 }
 
 
-void usb_serial_deregister(struct usb_serial_driver *device)
+void usb_serial_deregister(struct usb_serial_driver *device) /* must be called with BKL held */
 {
        info("USB Serial deregistering driver %s", device->description);
        list_del(&device->driver_list);
@@ -1127,7 +1241,6 @@ EXPORT_SYMBOL_GPL(usb_serial_port_softint);
 /* Module information */
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
-MODULE_VERSION( DRIVER_VERSION );
 MODULE_LICENSE("GPL");
 
 module_param(debug, bool, S_IRUGO | S_IWUSR);