This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / usb / serial / usb-serial.c
index e6c344b..03e8a7e 100644 (file)
@@ -388,7 +388,7 @@ static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_po
 
                good_spot = 1;
                for (j = 1; j <= num_ports-1; ++j)
-                       if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
+                       if ((serial_table[i+j]) || (i+j >= SERIAL_TTY_MINORS)) {
                                good_spot = 0;
                                i += j;
                                break;
@@ -405,7 +405,7 @@ static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_po
        return NULL;
 }
 
-static void return_serial(struct usb_serial *serial)
+static void return_serial (struct usb_serial *serial)
 {
        int i;
 
@@ -417,6 +417,8 @@ static void return_serial(struct usb_serial *serial)
        for (i = 0; i < serial->num_ports; ++i) {
                serial_table[serial->minor + i] = NULL;
        }
+
+       return;
 }
 
 static void destroy_serial(struct kref *kref)
@@ -452,18 +454,21 @@ 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);
+                       if (port->read_urb) {
+                               usb_unlink_urb(port->read_urb);
+                               usb_free_urb(port->read_urb);
+                       }
+                       if (port->write_urb) {
+                               usb_unlink_urb(port->write_urb);
+                               usb_free_urb(port->write_urb);
+                       }
+                       if (port->interrupt_in_urb) {
+                               usb_unlink_urb(port->interrupt_in_urb);
+                               usb_free_urb(port->interrupt_in_urb);
+                       }
                        kfree(port->bulk_in_buffer);
                        kfree(port->bulk_out_buffer);
                        kfree(port->interrupt_in_buffer);
-                       kfree(port->interrupt_out_buffer);
                }
        }
 
@@ -481,51 +486,46 @@ static int serial_open (struct tty_struct *tty, struct file * filp)
        struct usb_serial *serial;
        struct usb_serial_port *port;
        unsigned int portNumber;
-       int retval;
+       int retval = 0;
        
        dbg("%s", __FUNCTION__);
 
+       /* initialize the pointer incase something fails */
+       tty->driver_data = NULL;
+
        /* get the serial object associated with this tty pointer */
        serial = usb_serial_get_by_index(tty->index);
        if (!serial) {
-               tty->driver_data = NULL;
-               return -ENODEV;
+               retval = -ENODEV;
+               goto bailout;
        }
 
+       /* set up our port structure making the tty driver remember our port object, and us it */
        portNumber = tty->index - serial->minor;
        port = serial->port[portNumber];
+       tty->driver_data = port;
+
+       port->tty = tty;
         
-       ++port->open_count;
+       /* lock this module before we call it,
+          this may, which means we must bail out, safe because we are called with BKL held */
+       if (!try_module_get(serial->type->owner)) {
+               retval = -ENODEV;
+               goto bailout;
+       }
 
+       ++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;
-
-               /* 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->owner)) {
-                       retval = -ENODEV;
-                       goto bailout_kref_put;
-               }
-
                /* only call the device specific open if this 
                 * is the first time the port is opened */
                retval = serial->type->open(port, filp);
-               if (retval)
-                       goto bailout_module_put;
+               if (retval) {
+                       port->open_count = 0;
+                       module_put(serial->type->owner);
+                       kref_put(&serial->kref, destroy_serial);
+               }
        }
-
-       return 0;
-
-bailout_module_put:
-       module_put(serial->type->owner);
-bailout_kref_put:
-       kref_put(&serial->kref, destroy_serial);
-       port->open_count = 0;
+bailout:
        return retval;
 }
 
@@ -538,28 +538,25 @@ static void serial_close(struct tty_struct *tty, struct file * filp)
 
        dbg("%s - port %d", __FUNCTION__, port->number);
 
-       if (port->open_count == 0)
-               return;
-
        --port->open_count;
-       if (port->open_count == 0) {
+       if (port->open_count <= 0) {
                /* only call the device specific close if this 
                 * port is being closed by the last owner */
                port->serial->type->close(port, filp);
+               port->open_count = 0;
 
                if (port->tty) {
                        if (port->tty->driver_data)
                                port->tty->driver_data = NULL;
                        port->tty = NULL;
                }
-
-               module_put(port->serial->type->owner);
        }
 
+       module_put(port->serial->type->owner);
        kref_put(&port->serial->kref, destroy_serial);
 }
 
-static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
+static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
 {
        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
        int retval = -EINVAL;
@@ -572,7 +569,7 @@ static int serial_write (struct tty_struct * tty, const unsigned char *buf, int
        }
 
        /* pass on to the driver specific version of this function */
-       retval = port->serial->type->write(port, buf, count);
+       retval = port->serial->type->write(port, from_user, buf, count);
 
 exit:
        return retval;
@@ -624,12 +621,15 @@ static void serial_throttle (struct tty_struct * tty)
 
        if (!port->open_count) {
                dbg ("%s - port not open", __FUNCTION__);
-               return;
+               goto exit;
        }
 
        /* pass on to the driver specific version of this function */
        if (port->serial->type->throttle)
                port->serial->type->throttle(port);
+
+exit:
+       ;
 }
 
 static void serial_unthrottle (struct tty_struct * tty)
@@ -640,12 +640,15 @@ static void serial_unthrottle (struct tty_struct * tty)
 
        if (!port->open_count) {
                dbg("%s - port not open", __FUNCTION__);
-               return;
+               goto exit;
        }
 
        /* pass on to the driver specific version of this function */
        if (port->serial->type->unthrottle)
                port->serial->type->unthrottle(port);
+
+exit:
+       ;
 }
 
 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
@@ -678,12 +681,15 @@ static void serial_set_termios (struct tty_struct *tty, struct termios * old)
 
        if (!port->open_count) {
                dbg("%s - port not open", __FUNCTION__);
-               return;
+               goto exit;
        }
 
        /* pass on to the driver specific version of this function if it is available */
        if (port->serial->type->set_termios)
                port->serial->type->set_termios(port, old);
+
+exit:
+       ;
 }
 
 static void serial_break (struct tty_struct *tty, int break_state)
@@ -694,12 +700,15 @@ static void serial_break (struct tty_struct *tty, int break_state)
 
        if (!port->open_count) {
                dbg("%s - port not open", __FUNCTION__);
-               return;
+               goto exit;
        }
 
        /* pass on to the driver specific version of this function if it is available */
        if (port->serial->type->break_ctl)
                port->serial->type->break_ctl(port, break_state);
+
+exit:
+       ;
 }
 
 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
@@ -804,18 +813,21 @@ static void port_release(struct device *dev)
        struct usb_serial_port *port = to_usb_serial_port(dev);
 
        dbg ("%s - %s", __FUNCTION__, dev->bus_id);
-       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);
+       if (port->read_urb) {
+               usb_unlink_urb(port->read_urb);
+               usb_free_urb(port->read_urb);
+       }
+       if (port->write_urb) {
+               usb_unlink_urb(port->write_urb);
+               usb_free_urb(port->write_urb);
+       }
+       if (port->interrupt_in_urb) {
+               usb_unlink_urb(port->interrupt_in_urb);
+               usb_free_urb(port->interrupt_in_urb);
+       }
        kfree(port->bulk_in_buffer);
        kfree(port->bulk_out_buffer);
        kfree(port->interrupt_in_buffer);
-       kfree(port->interrupt_out_buffer);
        kfree(port);
 }
 
@@ -841,25 +853,6 @@ static struct usb_serial * create_serial (struct usb_device *dev,
        return serial;
 }
 
-static struct usb_serial_device_type *search_serial_device(struct usb_interface *iface)
-{
-       struct list_head *p;
-       const struct usb_device_id *id;
-       struct usb_serial_device_type *t;
-
-       /* List trough know devices and see if the usb id matches */
-       list_for_each(p, &usb_serial_driver_list) {
-               t = list_entry(p, struct usb_serial_device_type, driver_list);
-               id = usb_match_id(iface, t->id_table);
-               if (id != NULL) {
-                       dbg("descriptor matches");
-                       return t;
-               }
-       }
-
-       return NULL;
-}
-
 int usb_serial_probe(struct usb_interface *interface,
                               const struct usb_device_id *id)
 {
@@ -869,23 +862,36 @@ int usb_serial_probe(struct usb_interface *interface,
        struct usb_host_interface *iface_desc;
        struct usb_endpoint_descriptor *endpoint;
        struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
-       struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
        struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
        struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
        struct usb_serial_device_type *type = NULL;
+       struct list_head *tmp;
        int retval;
+       int found;
        int minor;
        int buffer_size;
        int i;
        int num_interrupt_in = 0;
-       int num_interrupt_out = 0;
        int num_bulk_in = 0;
        int num_bulk_out = 0;
        int num_ports = 0;
        int max_endpoints;
-
-       type = search_serial_device(interface);
-       if (!type) {
+       const struct usb_device_id *id_pattern = NULL;
+
+       /* loop through our list of known serial converters, and see if this
+          device matches. */
+       found = 0;
+       list_for_each (tmp, &usb_serial_driver_list) {
+               type = list_entry(tmp, struct usb_serial_device_type, driver_list);
+               id_pattern = usb_match_id(interface, type->id_table);
+               if (id_pattern != NULL) {
+                       dbg("descriptor matches");
+                       found = 1;
+                       break;
+               }
+       }
+       if (!found) {
+               /* no match */
                dbg("none matched");
                return -ENODEV;
        }
@@ -893,21 +899,17 @@ int usb_serial_probe(struct usb_interface *interface,
        serial = create_serial (dev, interface, type);
        if (!serial) {
                dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
-               return -ENOMEM;
+               return -ENODEV;
        }
 
        /* if this device type has a probe function, call it */
        if (type->probe) {
-               const struct usb_device_id *id;
-
                if (!try_module_get(type->owner)) {
                        dev_err(&interface->dev, "module get failed, exiting\n");
                        kfree (serial);
                        return -EIO;
                }
-
-               id = usb_match_id(interface, type->id_table);
-               retval = type->probe(serial, id);
+               retval = type->probe (serial, id_pattern);
                module_put(type->owner);
 
                if (retval) {
@@ -926,7 +928,7 @@ int usb_serial_probe(struct usb_interface *interface,
                if ((endpoint->bEndpointAddress & 0x80) &&
                    ((endpoint->bmAttributes & 3) == 0x02)) {
                        /* we found a bulk in endpoint */
-                       dbg("found bulk in on endpoint %d", i);
+                       dbg("found bulk in");
                        bulk_in_endpoint[num_bulk_in] = endpoint;
                        ++num_bulk_in;
                }
@@ -934,7 +936,7 @@ int usb_serial_probe(struct usb_interface *interface,
                if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
                    ((endpoint->bmAttributes & 3) == 0x02)) {
                        /* we found a bulk out endpoint */
-                       dbg("found bulk out on endpoint %d", i);
+                       dbg("found bulk out");
                        bulk_out_endpoint[num_bulk_out] = endpoint;
                        ++num_bulk_out;
                }
@@ -942,18 +944,10 @@ int usb_serial_probe(struct usb_interface *interface,
                if ((endpoint->bEndpointAddress & 0x80) &&
                    ((endpoint->bmAttributes & 3) == 0x03)) {
                        /* we found a interrupt in endpoint */
-                       dbg("found interrupt in on endpoint %d", i);
+                       dbg("found interrupt in");
                        interrupt_in_endpoint[num_interrupt_in] = endpoint;
                        ++num_interrupt_in;
                }
-
-               if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
-                   ((endpoint->bmAttributes & 3) == 0x03)) {
-                       /* we found an interrupt out endpoint */
-                       dbg("found interrupt out on endpoint %d", i);
-                       interrupt_out_endpoint[num_interrupt_out] = endpoint;
-                       ++num_interrupt_out;
-               }
        }
 
 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
@@ -1030,13 +1024,11 @@ int usb_serial_probe(struct usb_interface *interface,
        serial->num_bulk_in = num_bulk_in;
        serial->num_bulk_out = num_bulk_out;
        serial->num_interrupt_in = num_interrupt_in;
-       serial->num_interrupt_out = num_interrupt_out;
 
        /* create our ports, we need as many as the max endpoints */
        /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
        max_endpoints = max(num_bulk_in, num_bulk_out);
        max_endpoints = max(max_endpoints, num_interrupt_in);
-       max_endpoints = max(max_endpoints, num_interrupt_out);
        max_endpoints = max(max_endpoints, (int)serial->num_ports);
        serial->num_port_pointers = max_endpoints;
        dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
@@ -1061,7 +1053,6 @@ int usb_serial_probe(struct usb_interface *interface,
                        goto probe_error;
                }
                buffer_size = endpoint->wMaxPacketSize;
-               port->bulk_in_size = buffer_size;
                port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
                port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
                if (!port->bulk_in_buffer) {
@@ -1100,61 +1091,29 @@ int usb_serial_probe(struct usb_interface *interface,
                                   port);
        }
 
-       if (serial->type->read_int_callback) {
-               for (i = 0; i < num_interrupt_in; ++i) {
-                       endpoint = interrupt_in_endpoint[i];
-                       port = serial->port[i];
-                       port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
-                       if (!port->interrupt_in_urb) {
-                               dev_err(&interface->dev, "No free urbs available\n");
-                               goto probe_error;
-                       }
-                       buffer_size = endpoint->wMaxPacketSize;
-                       port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
-                       port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
-                       if (!port->interrupt_in_buffer) {
-                               dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
-                               goto probe_error;
-                       }
-                       usb_fill_int_urb (port->interrupt_in_urb, dev, 
-                                         usb_rcvintpipe (dev,
-                                                         endpoint->bEndpointAddress),
-                                         port->interrupt_in_buffer, buffer_size, 
-                                         serial->type->read_int_callback, port, 
-                                         endpoint->bInterval);
+       for (i = 0; i < num_interrupt_in; ++i) {
+               endpoint = interrupt_in_endpoint[i];
+               port = serial->port[i];
+               port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
+               if (!port->interrupt_in_urb) {
+                       dev_err(&interface->dev, "No free urbs available\n");
+                       goto probe_error;
                }
-       } else if (num_interrupt_in) {
-               dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
-       }
-       
-       if (serial->type->write_int_callback) {
-               for (i = 0; i < num_interrupt_out; ++i) {
-                       endpoint = interrupt_out_endpoint[i];
-                       port = serial->port[i];
-                       port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
-                       if (!port->interrupt_out_urb) {
-                               dev_err(&interface->dev, "No free urbs available\n");
-                               goto probe_error;
-                       }
-                       buffer_size = endpoint->wMaxPacketSize;
-                       port->interrupt_out_size = buffer_size;
-                       port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
-                       port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
-                       if (!port->interrupt_out_buffer) {
-                               dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
-                               goto probe_error;
-                       }
-                       usb_fill_int_urb (port->interrupt_out_urb, dev,
-                                         usb_sndintpipe (dev,
-                                                         endpoint->bEndpointAddress),
-                                         port->interrupt_out_buffer, buffer_size,
-                                         serial->type->write_int_callback, port,
-                                         endpoint->bInterval);
+               buffer_size = endpoint->wMaxPacketSize;
+               port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
+               port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
+               if (!port->interrupt_in_buffer) {
+                       dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
+                       goto probe_error;
                }
-       } else if (num_interrupt_out) {
-               dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
+               usb_fill_int_urb (port->interrupt_in_urb, dev, 
+                                 usb_rcvintpipe (dev,
+                                                 endpoint->bEndpointAddress),
+                                 port->interrupt_in_buffer, buffer_size, 
+                                 serial->type->read_int_callback, port, 
+                                 endpoint->bInterval);
        }
-       
+
        /* if this device type has an attach function, call it */
        if (type->attach) {
                if (!try_module_get(type->owner)) {
@@ -1217,14 +1176,6 @@ probe_error:
                        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);
-               kfree(port->interrupt_out_buffer);
-       }
 
        /* return the minor range that this device had */
        return_serial (serial);
@@ -1273,7 +1224,7 @@ struct tty_driver *usb_serial_tty_driver;
 static int __init usb_serial_init(void)
 {
        int i;
-       int result;
+       int result = 0;
 
        usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
        if (!usb_serial_tty_driver)
@@ -1284,17 +1235,13 @@ static int __init usb_serial_init(void)
                serial_table[i] = NULL;
        }
 
-       result = bus_register(&usb_serial_bus_type);
-       if (result) {
-               err("%s - registering bus driver failed", __FUNCTION__);
-               goto exit_bus;
-       }
+       bus_register(&usb_serial_bus_type);
 
        /* register the generic driver, if we should */
        result = usb_serial_generic_register(debug);
        if (result < 0) {
                err("%s - registering generic driver failed", __FUNCTION__);
-               goto exit_generic;
+               goto exit;
        }
 
        usb_serial_tty_driver->owner = THIS_MODULE;
@@ -1312,7 +1259,7 @@ static int __init usb_serial_init(void)
        result = tty_register_driver(usb_serial_tty_driver);
        if (result) {
                err("%s - tty_register_driver failed", __FUNCTION__);
-               goto exit_reg_driver;
+               goto exit_generic;
        }
 
        /* register the USB driver */
@@ -1329,13 +1276,10 @@ static int __init usb_serial_init(void)
 exit_tty:
        tty_unregister_driver(usb_serial_tty_driver);
 
-exit_reg_driver:
-       usb_serial_generic_deregister();
-
 exit_generic:
-       bus_unregister(&usb_serial_bus_type);
+       usb_serial_generic_deregister();
 
-exit_bus:
+exit:
        err ("%s - returning with error %d", __FUNCTION__, result);
        put_tty_driver(usb_serial_tty_driver);
        return result;
@@ -1388,13 +1332,17 @@ int usb_serial_register(struct usb_serial_device_type *new_device)
        /* Add this device to our list of devices */
        list_add(&new_device->driver_list, &usb_serial_driver_list);
 
-       retval = usb_serial_bus_register(new_device);
-       if (retval) {
-               err("problem %d when registering driver %s", retval, new_device->name);
-               list_del(&new_device->driver_list);
-       }
-       else
-               info("USB Serial support registered for %s", new_device->name);
+       retval =  usb_serial_bus_register (new_device);
+
+       if (retval)
+               goto error;
+
+       info("USB Serial support registered for %s", new_device->name);
+
+       return retval;
+error:
+       err("problem %d when registering driver %s", retval, new_device->name);
+       list_del(&new_device->driver_list);
 
        return retval;
 }
@@ -1421,7 +1369,6 @@ EXPORT_SYMBOL(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);