USB: serial: fix race between probe and open
[linux-flexiantxendom0.git] / drivers / usb / serial / usb-serial.c
index 8249fd8..38d7ebd 100644 (file)
@@ -21,7 +21,6 @@
 #include <linux/errno.h>
 #include <linux/init.h>
 #include <linux/slab.h>
-#include <linux/smp_lock.h>
 #include <linux/tty.h>
 #include <linux/tty_driver.h>
 #include <linux/tty_flip.h>
@@ -52,6 +51,7 @@ static struct usb_driver usb_serial_driver = {
        .suspend =      usb_serial_suspend,
        .resume =       usb_serial_resume,
        .no_dynamic_id =        1,
+       .supports_autosuspend = 1,
 };
 
 /* There is no MODULE_DEVICE_TABLE for usbserial.c.  Instead
@@ -406,7 +406,7 @@ static void serial_unthrottle(struct tty_struct *tty)
                port->serial->type->unthrottle(tty);
 }
 
-static int serial_ioctl(struct tty_struct *tty, struct file *file,
+static int serial_ioctl(struct tty_struct *tty,
                                        unsigned int cmd, unsigned long arg)
 {
        struct usb_serial_port *port = tty->driver_data;
@@ -417,7 +417,7 @@ static int serial_ioctl(struct tty_struct *tty, struct file *file,
        /* pass on to the driver specific version of this function
           if it is available */
        if (port->serial->type->ioctl) {
-               retval = port->serial->type->ioctl(tty, file, cmd, arg);
+               retval = port->serial->type->ioctl(tty, cmd, arg);
        } else
                retval = -ENOIOCTLCMD;
        return retval;
@@ -496,18 +496,18 @@ static const struct file_operations serial_proc_fops = {
        .release        = single_release,
 };
 
-static int serial_tiocmget(struct tty_struct *tty, struct file *file)
+static int serial_tiocmget(struct tty_struct *tty)
 {
        struct usb_serial_port *port = tty->driver_data;
 
        dbg("%s - port %d", __func__, port->number);
 
        if (port->serial->type->tiocmget)
-               return port->serial->type->tiocmget(tty, file);
+               return port->serial->type->tiocmget(tty);
        return -EINVAL;
 }
 
-static int serial_tiocmset(struct tty_struct *tty, struct file *file,
+static int serial_tiocmset(struct tty_struct *tty,
                            unsigned int set, unsigned int clear)
 {
        struct usb_serial_port *port = tty->driver_data;
@@ -515,7 +515,19 @@ static int serial_tiocmset(struct tty_struct *tty, struct file *file,
        dbg("%s - port %d", __func__, port->number);
 
        if (port->serial->type->tiocmset)
-               return port->serial->type->tiocmset(tty, file, set, clear);
+               return port->serial->type->tiocmset(tty, set, clear);
+       return -EINVAL;
+}
+
+static int serial_get_icount(struct tty_struct *tty,
+                               struct serial_icounter_struct *icount)
+{
+       struct usb_serial_port *port = tty->driver_data;
+
+       dbg("%s - port %d", __func__, port->number);
+
+       if (port->serial->type->get_icount)
+               return port->serial->type->get_icount(tty, icount);
        return -EINVAL;
 }
 
@@ -548,8 +560,12 @@ static void usb_serial_port_work(struct work_struct *work)
 
 static void kill_traffic(struct usb_serial_port *port)
 {
+       int i;
+
        usb_kill_urb(port->read_urb);
        usb_kill_urb(port->write_urb);
+       for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
+               usb_kill_urb(port->write_urbs[i]);
        /*
         * This is tricky.
         * Some drivers submit the read_urb in the
@@ -568,6 +584,7 @@ static void kill_traffic(struct usb_serial_port *port)
 static void port_release(struct device *dev)
 {
        struct usb_serial_port *port = to_usb_serial_port(dev);
+       int i;
 
        dbg ("%s - %s", __func__, dev_name(dev));
 
@@ -582,6 +599,10 @@ static void port_release(struct device *dev)
        usb_free_urb(port->write_urb);
        usb_free_urb(port->interrupt_in_urb);
        usb_free_urb(port->interrupt_out_urb);
+       for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
+               usb_free_urb(port->write_urbs[i]);
+               kfree(port->bulk_out_buffers[i]);
+       }
        kfifo_free(&port->write_fifo);
        kfree(port->bulk_in_buffer);
        kfree(port->bulk_out_buffer);
@@ -644,6 +665,7 @@ exit:
        return id;
 }
 
+/* Caller must hold table_lock */
 static struct usb_serial_driver *search_serial_device(
                                        struct usb_interface *iface)
 {
@@ -709,17 +731,24 @@ int usb_serial_probe(struct usb_interface *interface,
        int num_ports = 0;
        int max_endpoints;
 
-       lock_kernel(); /* guard against unloading a serial driver module */
+       mutex_lock(&table_lock);
        type = search_serial_device(interface);
        if (!type) {
-               unlock_kernel();
+               mutex_unlock(&table_lock);
                dbg("none matched");
                return -ENODEV;
        }
 
+       if (!try_module_get(type->driver.owner)) {
+               mutex_unlock(&table_lock);
+               dev_err(&interface->dev, "module get failed, exiting\n");
+               return -EIO;
+       }
+       mutex_unlock(&table_lock);
+
        serial = create_serial(dev, interface, type);
        if (!serial) {
-               unlock_kernel();
+               module_put(type->driver.owner);
                dev_err(&interface->dev, "%s - out of memory\n", __func__);
                return -ENOMEM;
        }
@@ -728,22 +757,13 @@ int usb_serial_probe(struct usb_interface *interface,
        if (type->probe) {
                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 = 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);
+                       module_put(type->driver.owner);
                        return retval;
                }
        }
@@ -813,9 +833,9 @@ 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);
+                       module_put(type->driver.owner);
                        return -ENODEV;
                }
        }
@@ -826,27 +846,18 @@ 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);
+                       module_put(type->driver.owner);
                        return -EIO;
                }
        }
 #endif
        if (!num_ports) {
                /* 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;
-                       }
+               if (type->calc_num_ports)
                        num_ports = type->calc_num_ports(serial);
-                       module_put(type->driver.owner);
-               }
                if (!num_ports)
                        num_ports = type->num_ports;
        }
@@ -869,7 +880,6 @@ 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",
                                                __func__, max_endpoints);
@@ -901,9 +911,8 @@ int usb_serial_probe(struct usb_interface *interface,
                        dev_err(&interface->dev, "No free urbs available\n");
                        goto probe_error;
                }
-               buffer_size = serial->type->bulk_in_size;
-               if (!buffer_size)
-                       buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+               buffer_size = max_t(int, serial->type->bulk_in_size,
+                               usb_endpoint_maxp(endpoint));
                port->bulk_in_size = buffer_size;
                port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
                port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
@@ -920,6 +929,8 @@ int usb_serial_probe(struct usb_interface *interface,
        }
 
        for (i = 0; i < num_bulk_out; ++i) {
+               int j;
+
                endpoint = bulk_out_endpoint[i];
                port = serial->port[i];
                port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
@@ -931,7 +942,7 @@ int usb_serial_probe(struct usb_interface *interface,
                        goto probe_error;
                buffer_size = serial->type->bulk_out_size;
                if (!buffer_size)
-                       buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+                       buffer_size = usb_endpoint_maxp(endpoint);
                port->bulk_out_size = buffer_size;
                port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
                port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
@@ -945,6 +956,28 @@ int usb_serial_probe(struct usb_interface *interface,
                                        endpoint->bEndpointAddress),
                                port->bulk_out_buffer, buffer_size,
                                serial->type->write_bulk_callback, port);
+               for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
+                       set_bit(j, &port->write_urbs_free);
+                       port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
+                       if (!port->write_urbs[j]) {
+                               dev_err(&interface->dev,
+                                               "No free urbs available\n");
+                               goto probe_error;
+                       }
+                       port->bulk_out_buffers[j] = kmalloc(buffer_size,
+                                                               GFP_KERNEL);
+                       if (!port->bulk_out_buffers[j]) {
+                               dev_err(&interface->dev,
+                                       "Couldn't allocate bulk_out_buffer\n");
+                               goto probe_error;
+                       }
+                       usb_fill_bulk_urb(port->write_urbs[j], dev,
+                                       usb_sndbulkpipe(dev,
+                                               endpoint->bEndpointAddress),
+                                       port->bulk_out_buffers[j], buffer_size,
+                                       serial->type->write_bulk_callback,
+                                       port);
+               }
        }
 
        if (serial->type->read_int_callback) {
@@ -957,7 +990,7 @@ int usb_serial_probe(struct usb_interface *interface,
                                                "No free urbs available\n");
                                goto probe_error;
                        }
-                       buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+                       buffer_size = usb_endpoint_maxp(endpoint);
                        port->interrupt_in_endpointAddress =
                                                endpoint->bEndpointAddress;
                        port->interrupt_in_buffer = kmalloc(buffer_size,
@@ -988,7 +1021,7 @@ int usb_serial_probe(struct usb_interface *interface,
                                                "No free urbs available\n");
                                goto probe_error;
                        }
-                       buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+                       buffer_size = usb_endpoint_maxp(endpoint);
                        port->interrupt_out_size = buffer_size;
                        port->interrupt_out_endpointAddress =
                                                endpoint->bEndpointAddress;
@@ -1012,13 +1045,7 @@ int usb_serial_probe(struct usb_interface *interface,
 
        /* if this device type has an attach function, call it */
        if (type->attach) {
-               if (!try_module_get(type->driver.owner)) {
-                       dev_err(&interface->dev,
-                                       "module get failed, exiting\n");
-                       goto probe_error;
-               }
                retval = type->attach(serial);
-               module_put(type->driver.owner);
                if (retval < 0)
                        goto probe_error;
                serial->attached = 1;
@@ -1032,6 +1059,12 @@ int usb_serial_probe(struct usb_interface *interface,
                serial->attached = 1;
        }
 
+       /* Avoid race with tty_open and serial_install by setting the
+        * disconnected flag and not clearing it until all ports have been
+        * registered.
+        */
+       serial->disconnected = 1;
+
        if (get_free_serial(serial, num_ports, &minor) == NULL) {
                dev_err(&interface->dev, "No more free serial devices\n");
                goto probe_error;
@@ -1044,6 +1077,8 @@ int usb_serial_probe(struct usb_interface *interface,
                dev_set_name(&port->dev, "ttyUSB%d", port->number);
                dbg ("%s - registering %s", __func__, dev_name(&port->dev));
                port->dev_state = PORT_REGISTERING;
+               device_enable_async_suspend(&port->dev);
+
                retval = device_add(&port->dev);
                if (retval) {
                        dev_err(&port->dev, "Error registering port device, "
@@ -1054,15 +1089,19 @@ int usb_serial_probe(struct usb_interface *interface,
                }
        }
 
+       serial->disconnected = 0;
+
        usb_serial_console_init(debug, minor);
 
 exit:
        /* success */
        usb_set_intfdata(interface, serial);
+       module_put(type->driver.owner);
        return 0;
 
 probe_error:
        usb_serial_put(serial);
+       module_put(type->driver.owner);
        return -EIO;
 }
 EXPORT_SYMBOL_GPL(usb_serial_probe);
@@ -1175,6 +1214,7 @@ static const struct tty_operations serial_ops = {
        .chars_in_buffer =      serial_chars_in_buffer,
        .tiocmget =             serial_tiocmget,
        .tiocmset =             serial_tiocmset,
+       .get_icount =           serial_get_icount,
        .cleanup =              serial_cleanup,
        .install =              serial_install,
        .proc_fops =            &serial_proc_fops,
@@ -1314,8 +1354,15 @@ int usb_serial_register(struct usb_serial_driver *driver)
 
        if (!driver->description)
                driver->description = driver->driver.name;
+       if (!driver->usb_driver) {
+               WARN(1, "Serial driver %s has no usb_driver\n",
+                               driver->description);
+               return -EINVAL;
+       }
+       driver->usb_driver->supports_autosuspend = 1;
 
        /* Add this device to our list of devices */
+       mutex_lock(&table_lock);
        list_add(&driver->driver_list, &usb_serial_driver_list);
 
        retval = usb_serial_bus_register(driver);
@@ -1327,6 +1374,7 @@ int usb_serial_register(struct usb_serial_driver *driver)
                printk(KERN_INFO "USB Serial support registered for %s\n",
                                                driver->description);
 
+       mutex_unlock(&table_lock);
        return retval;
 }
 EXPORT_SYMBOL_GPL(usb_serial_register);
@@ -1337,8 +1385,10 @@ void usb_serial_deregister(struct usb_serial_driver *device)
        /* must be called with BKL held */
        printk(KERN_INFO "USB Serial deregistering driver %s\n",
               device->description);
+       mutex_lock(&table_lock);
        list_del(&device->driver_list);
        usb_serial_bus_deregister(device);
+       mutex_unlock(&table_lock);
 }
 EXPORT_SYMBOL_GPL(usb_serial_deregister);