USB: belkin_sa: clean up open
[safe/jmp/linux-2.6] / drivers / usb / serial / usb-serial.c
index bd3fa7f..941c2d4 100644 (file)
@@ -247,96 +247,66 @@ static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
        return retval;
 }
 
-static int serial_open(struct tty_struct *tty, struct file *filp)
+static int serial_activate(struct tty_port *tport, struct tty_struct *tty)
 {
-       struct usb_serial_port *port = tty->driver_data;
+       struct usb_serial_port *port =
+               container_of(tport, struct usb_serial_port, port);
        struct usb_serial *serial = port->serial;
        int retval;
 
-       dbg("%s - port %d", __func__, port->number);
-
-       spin_lock_irq(&port->port.lock);
-       if (!tty_hung_up_p(filp))
-               ++port->port.count;
-       spin_unlock_irq(&port->port.lock);
-       tty_port_tty_set(&port->port, tty);
+       mutex_lock(&serial->disc_mutex);
+       if (serial->disconnected)
+               retval = -ENODEV;
+       else
+               retval = port->serial->type->open(tty, port);
+       mutex_unlock(&serial->disc_mutex);
+       return retval;
+}
 
-       /* Do the device-specific open only if the hardware isn't
-        * already initialized.
-        */
-       if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) {
-               if (mutex_lock_interruptible(&port->mutex))
-                       return -ERESTARTSYS;
-               mutex_lock(&serial->disc_mutex);
-               if (serial->disconnected)
-                       retval = -ENODEV;
-               else
-                       retval = port->serial->type->open(tty, port);
-               mutex_unlock(&serial->disc_mutex);
-               mutex_unlock(&port->mutex);
-               if (retval)
-                       return retval;
-               set_bit(ASYNCB_INITIALIZED, &port->port.flags);
-       }
+static int serial_open(struct tty_struct *tty, struct file *filp)
+{
+       struct usb_serial_port *port = tty->driver_data;
 
-       /* Now do the correct tty layer semantics */
-       retval = tty_port_block_til_ready(&port->port, tty, filp);
-       return retval;
+       dbg("%s - port %d", __func__, port->number);
+       return tty_port_open(&port->port, tty, filp);
 }
 
 /**
  * serial_down - shut down hardware
- * @port: port to shut down
+ * @tport: tty port to shut down
  *
  * Shut down a USB serial port unless it is the console.  We never
- * shut down the console hardware as it will always be in use.
+ * shut down the console hardware as it will always be in use. Serialized
+ * against activate by the tport mutex and kept to matching open/close pairs
+ * of calls by the ASYNCB_INITIALIZED flag.
  */
-static void serial_down(struct usb_serial_port *port)
+static void serial_down(struct tty_port *tport)
 {
+       struct usb_serial_port *port =
+               container_of(tport, struct usb_serial_port, port);
        struct usb_serial_driver *drv = port->serial->type;
-
        /*
         * The console is magical.  Do not hang up the console hardware
         * or there will be tears.
         */
-       if (port->console)
-               return;
-
-       /* Don't call the close method if the hardware hasn't been
-        * initialized.
-        */
-       if (!test_and_clear_bit(ASYNCB_INITIALIZED, &port->port.flags))
+       if (port->port.console)
                return;
-
-       mutex_lock(&port->mutex);
        if (drv->close)
                drv->close(port);
-       mutex_unlock(&port->mutex);
 }
 
 static void serial_hangup(struct tty_struct *tty)
 {
        struct usb_serial_port *port = tty->driver_data;
-
        dbg("%s - port %d", __func__, port->number);
-
-       serial_down(port);
        tty_port_hangup(&port->port);
 }
 
 static void serial_close(struct tty_struct *tty, struct file *filp)
 {
        struct usb_serial_port *port = tty->driver_data;
-
        dbg("%s - port %d", __func__, port->number);
-
-       if (tty_hung_up_p(filp))
-               return;
-       if (tty_port_close_start(&port->port, tty, filp) == 0)
-               return;
-       serial_down(port);
-       tty_port_close_end(&port->port, tty);
-       tty_port_tty_set(&port->port, NULL);
+       tty_port_close(&port->port, tty, filp);
 }
 
 /**
@@ -358,7 +328,7 @@ static void serial_cleanup(struct tty_struct *tty)
        /* The console is magical.  Do not hang up the console hardware
         * or there will be tears.
         */
-       if (port->console)
+       if (port->port.console)
                return;
 
        dbg("%s - port %d", __func__, port->number);
@@ -388,10 +358,6 @@ static int serial_write(struct tty_struct *tty, const unsigned char *buf,
 
        dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
 
-       /* count is managed under the mutex lock for the tty so cannot
-          drop to zero until after the last close completes */
-       WARN_ON(!port->port.count);
-
        /* pass on to the driver specific version of this function */
        retval = port->serial->type->write(tty, port, buf, count);
 
@@ -403,7 +369,6 @@ static int serial_write_room(struct tty_struct *tty)
 {
        struct usb_serial_port *port = tty->driver_data;
        dbg("%s - port %d", __func__, port->number);
-       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function */
        return port->serial->type->write_room(tty);
 }
@@ -411,7 +376,7 @@ static int serial_write_room(struct tty_struct *tty)
 static int serial_chars_in_buffer(struct tty_struct *tty)
 {
        struct usb_serial_port *port = tty->driver_data;
-       dbg("%s = port %d", __func__, port->number);
+       dbg("%s - port %d", __func__, port->number);
 
        /* if the device was unplugged then any remaining characters
           fell out of the connector ;) */
@@ -426,7 +391,6 @@ static void serial_throttle(struct tty_struct *tty)
        struct usb_serial_port *port = tty->driver_data;
        dbg("%s - port %d", __func__, port->number);
 
-       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function */
        if (port->serial->type->throttle)
                port->serial->type->throttle(tty);
@@ -437,7 +401,6 @@ static void serial_unthrottle(struct tty_struct *tty)
        struct usb_serial_port *port = tty->driver_data;
        dbg("%s - port %d", __func__, port->number);
 
-       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function */
        if (port->serial->type->unthrottle)
                port->serial->type->unthrottle(tty);
@@ -451,8 +414,6 @@ static int serial_ioctl(struct tty_struct *tty, struct file *file,
 
        dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
 
-       WARN_ON(!port->port.count);
-
        /* pass on to the driver specific version of this function
           if it is available */
        if (port->serial->type->ioctl) {
@@ -467,7 +428,6 @@ static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
        struct usb_serial_port *port = tty->driver_data;
        dbg("%s - port %d", __func__, port->number);
 
-       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function
           if it is available */
        if (port->serial->type->set_termios)
@@ -482,7 +442,6 @@ static int serial_break(struct tty_struct *tty, int break_state)
 
        dbg("%s - port %d", __func__, port->number);
 
-       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function
           if it is available */
        if (port->serial->type->break_ctl)
@@ -543,7 +502,6 @@ static int serial_tiocmget(struct tty_struct *tty, struct file *file)
 
        dbg("%s - port %d", __func__, port->number);
 
-       WARN_ON(!port->port.count);
        if (port->serial->type->tiocmget)
                return port->serial->type->tiocmget(tty, file);
        return -EINVAL;
@@ -556,7 +514,6 @@ static int serial_tiocmset(struct tty_struct *tty, struct file *file,
 
        dbg("%s - port %d", __func__, port->number);
 
-       WARN_ON(!port->port.count);
        if (port->serial->type->tiocmset)
                return port->serial->type->tiocmset(tty, file, set, clear);
        return -EINVAL;
@@ -591,8 +548,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
@@ -611,6 +572,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));
 
@@ -625,8 +587,11 @@ 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);
-       if (!IS_ERR(port->write_fifo) && port->write_fifo)
-               kfifo_free(port->write_fifo);
+       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);
        kfree(port->interrupt_in_buffer);
@@ -725,6 +690,8 @@ static void serial_dtr_rts(struct tty_port *port, int on)
 static const struct tty_port_operations serial_port_ops = {
        .carrier_raised = serial_carrier_raised,
        .dtr_rts = serial_dtr_rts,
+       .activate = serial_activate,
+       .shutdown = serial_down,
 };
 
 int usb_serial_probe(struct usb_interface *interface,
@@ -923,7 +890,8 @@ int usb_serial_probe(struct usb_interface *interface,
                port->port.ops = &serial_port_ops;
                port->serial = serial;
                spin_lock_init(&port->lock);
-               mutex_init(&port->mutex);
+               /* Keep this for private driver use for the moment but
+                  should probably go away */
                INIT_WORK(&port->work, usb_serial_port_work);
                serial->port[i] = port;
                port->dev.parent = &interface->dev;
@@ -942,7 +910,9 @@ int usb_serial_probe(struct usb_interface *interface,
                        dev_err(&interface->dev, "No free urbs available\n");
                        goto probe_error;
                }
-               buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+               buffer_size = serial->type->bulk_in_size;
+               if (!buffer_size)
+                       buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
                port->bulk_in_size = buffer_size;
                port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
                port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
@@ -959,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);
@@ -966,11 +938,11 @@ int usb_serial_probe(struct usb_interface *interface,
                        dev_err(&interface->dev, "No free urbs available\n");
                        goto probe_error;
                }
-               port->write_fifo = kfifo_alloc(PAGE_SIZE, GFP_KERNEL,
-                       &port->lock);
-               if (IS_ERR(port->write_fifo))
+               if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
                        goto probe_error;
-               buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+               buffer_size = serial->type->bulk_out_size;
+               if (!buffer_size)
+                       buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
                port->bulk_out_size = buffer_size;
                port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
                port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
@@ -984,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) {
@@ -1337,6 +1331,8 @@ static void fixup_generic(struct usb_serial_driver *device)
        set_to_generic_if_null(device, write_bulk_callback);
        set_to_generic_if_null(device, disconnect);
        set_to_generic_if_null(device, release);
+       set_to_generic_if_null(device, process_read_urb);
+       set_to_generic_if_null(device, prepare_write_buffer);
 }
 
 int usb_serial_register(struct usb_serial_driver *driver)