usb_serial: API all change
[pandora-kernel.git] / drivers / usb / serial / usb-serial.c
index 2138ba8..ffaed8a 100644 (file)
@@ -81,7 +81,7 @@ static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_po
        unsigned int i, j;
        int good_spot;
 
-       dbg("%s %d", __FUNCTION__, num_ports);
+       dbg("%s %d", __func__, num_ports);
 
        *minor = 0;
        mutex_lock(&table_lock);
@@ -101,7 +101,7 @@ static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_po
 
                *minor = i;
                j = 0;
-               dbg("%s - minor base = %d", __FUNCTION__, *minor);
+               dbg("%s - minor base = %d", __func__, *minor);
                for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
                        serial_table[i] = serial;
                        serial->port[j++]->number = i;
@@ -117,7 +117,7 @@ static void return_serial(struct usb_serial *serial)
 {
        int i;
 
-       dbg("%s", __FUNCTION__);
+       dbg("%s", __func__);
 
        if (serial == NULL)
                return;
@@ -135,7 +135,7 @@ static void destroy_serial(struct kref *kref)
 
        serial = to_usb_serial(kref);
 
-       dbg("%s - %s", __FUNCTION__, serial->type->description);
+       dbg("%s - %s", __func__, serial->type->description);
 
        serial->type->shutdown(serial);
 
@@ -143,7 +143,7 @@ static void destroy_serial(struct kref *kref)
        return_serial(serial);
 
        for (i = 0; i < serial->num_ports; ++i)
-               serial->port[i]->open_count = 0;
+               serial->port[i]->port.count = 0;
 
        /* the ports are cleaned up and released in port_release() */
        for (i = 0; i < serial->num_ports; ++i)
@@ -187,7 +187,7 @@ static int serial_open (struct tty_struct *tty, struct file * filp)
        unsigned int portNumber;
        int retval;
        
-       dbg("%s", __FUNCTION__);
+       dbg("%s", __func__);
 
        /* get the serial object associated with this tty pointer */
        serial = usb_serial_get_by_index(tty->index);
@@ -208,14 +208,14 @@ static int serial_open (struct tty_struct *tty, struct file * filp)
                goto bailout_kref_put;
        }
         
-       ++port->open_count;
+       ++port->port.count;
 
        /* set up our port structure making the tty driver
         * remember our port object, and us it */
        tty->driver_data = port;
-       port->tty = tty;
+       port->port.tty = tty;
 
-       if (port->open_count == 1) {
+       if (port->port.count == 1) {
 
                /* lock this module before we call it
                 * this may fail, which means we must bail out,
@@ -230,7 +230,7 @@ static int serial_open (struct tty_struct *tty, struct file * filp)
                        goto bailout_module_put;
                /* only call the device specific open if this 
                 * is the first time the port is opened */
-               retval = serial->type->open(port, filp);
+               retval = serial->type->open(tty, port, filp);
                if (retval)
                        goto bailout_interface_put;
        }
@@ -243,9 +243,9 @@ bailout_interface_put:
 bailout_module_put:
        module_put(serial->type->driver.owner);
 bailout_mutex_unlock:
-       port->open_count = 0;
+       port->port.count = 0;
        tty->driver_data = NULL;
-       port->tty = NULL;
+       port->port.tty = NULL;
        mutex_unlock(&port->mutex);
 bailout_kref_put:
        usb_serial_put(serial);
@@ -259,31 +259,34 @@ static void serial_close(struct tty_struct *tty, struct file * filp)
        if (!port)
                return;
 
-       dbg("%s - port %d", __FUNCTION__, port->number);
+       dbg("%s - port %d", __func__, port->number);
 
        mutex_lock(&port->mutex);
 
-       if (port->open_count == 0) {
+       if (port->port.count == 0) {
                mutex_unlock(&port->mutex);
                return;
        }
 
-       --port->open_count;
-       if (port->open_count == 0)
+       --port->port.count;
+       if (port->port.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->serial->type->close(tty, port, filp);
 
-       if (port->open_count == (port->console? 1 : 0)) {
-               if (port->tty) {
-                       if (port->tty->driver_data)
-                               port->tty->driver_data = NULL;
-                       port->tty = NULL;
+       if (port->port.count == (port->console? 1 : 0)) {
+               if (port->port.tty) {
+                       if (port->port.tty->driver_data)
+                               port->port.tty->driver_data = NULL;
+                       port->port.tty = NULL;
                }
        }
 
-       if (port->open_count == 0) {
-               usb_autopm_put_interface(port->serial->interface);
+       if (port->port.count == 0) {
+               mutex_lock(&port->serial->disc_mutex);
+               if (!port->serial->disconnected)
+                       usb_autopm_put_interface(port->serial->interface);
+               mutex_unlock(&port->serial->disc_mutex);
                module_put(port->serial->type->driver.owner);
        }
 
@@ -296,19 +299,17 @@ static int serial_write (struct tty_struct * tty, const unsigned char *buf, int
        struct usb_serial_port *port = tty->driver_data;
        int retval = -ENODEV;
 
-       if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
+       if (port->serial->dev->state == USB_STATE_NOTATTACHED)
                goto exit;
 
-       dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
+       dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
 
-       if (!port->open_count) {
-               retval = -EINVAL;
-               dbg("%s - port not opened", __FUNCTION__);
-               goto exit;
-       }
+       /* 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(port, buf, count);
+       retval = port->serial->type->write(tty, port, buf, count);
 
 exit:
        return retval;
@@ -317,83 +318,42 @@ exit:
 static int serial_write_room (struct tty_struct *tty) 
 {
        struct usb_serial_port *port = tty->driver_data;
-       int retval = -ENODEV;
-
-       if (!port)
-               goto exit;
-
-       dbg("%s - port %d", __FUNCTION__, port->number);
-
-       if (!port->open_count) {
-               dbg("%s - port not open", __FUNCTION__);
-               goto exit;
-       }
-
+       dbg("%s - port %d", __func__, port->number);
+       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function */
-       retval = port->serial->type->write_room(port);
-
-exit:
-       return retval;
+       return port->serial->type->write_room(tty);
 }
 
 static int serial_chars_in_buffer (struct tty_struct *tty) 
 {
        struct usb_serial_port *port = tty->driver_data;
-       int retval = -ENODEV;
-
-       if (!port)
-               goto exit;
-
-       dbg("%s = port %d", __FUNCTION__, port->number);
-
-       if (!port->open_count) {
-               dbg("%s - port not open", __FUNCTION__);
-               goto exit;
-       }
+       dbg("%s = port %d", __func__, port->number);
 
+       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function */
-       retval = port->serial->type->chars_in_buffer(port);
-
-exit:
-       return retval;
+       return port->serial->type->chars_in_buffer(tty);
 }
 
 static void serial_throttle (struct tty_struct * tty)
 {
        struct usb_serial_port *port = tty->driver_data;
+       dbg("%s - port %d", __func__, port->number);
 
-       if (!port)
-               return;
-
-       dbg("%s - port %d", __FUNCTION__, port->number);
-
-       if (!port->open_count) {
-               dbg ("%s - port not open", __FUNCTION__);
-               return;
-       }
-
+       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function */
        if (port->serial->type->throttle)
-               port->serial->type->throttle(port);
+               port->serial->type->throttle(tty);
 }
 
 static void serial_unthrottle (struct tty_struct * tty)
 {
        struct usb_serial_port *port = tty->driver_data;
+       dbg("%s - port %d", __func__, port->number);
 
-       if (!port)
-               return;
-
-       dbg("%s - port %d", __FUNCTION__, port->number);
-
-       if (!port->open_count) {
-               dbg("%s - port not open", __FUNCTION__);
-               return;
-       }
-
+       WARN_ON(!port->port.count);
        /* pass on to the driver specific version of this function */
        if (port->serial->type->unthrottle)
-               port->serial->type->unthrottle(port);
+               port->serial->type->unthrottle(tty);
 }
 
 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
@@ -401,43 +361,30 @@ 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);
+       dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
 
-       if (!port->open_count) {
-               dbg ("%s - port not open", __FUNCTION__);
-               goto exit;
-       }
+       WARN_ON(!port->port.count);
 
        /* pass on to the driver specific version of this function if it is available */
-       if (port->serial->type->ioctl)
-               retval = port->serial->type->ioctl(port, file, cmd, arg);
+       if (port->serial->type->ioctl) {
+               lock_kernel();
+               retval = port->serial->type->ioctl(tty, file, cmd, arg);
+               unlock_kernel();
+       }
        else
                retval = -ENOIOCTLCMD;
-
-exit:
        return retval;
 }
 
 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);
 
-       if (!port)
-               return;
-
-       dbg("%s - port %d", __FUNCTION__, port->number);
-
-       if (!port->open_count) {
-               dbg("%s - port not open", __FUNCTION__);
-               return;
-       }
-
+       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)
-               port->serial->type->set_termios(port, old);
+               port->serial->type->set_termios(tty, port, old);
        else
                tty_termios_copy_hw(tty->termios, old);
 }
@@ -446,19 +393,15 @@ 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) {
-               dbg("%s - port not open", __FUNCTION__);
-               return;
-       }
+       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)
-               port->serial->type->break_ctl(port, break_state);
+       if (port->serial->type->break_ctl) {
+               lock_kernel();
+               port->serial->type->break_ctl(tty, break_state);
+               unlock_kernel();
+       }
 }
 
 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
@@ -469,7 +412,7 @@ static int serial_read_proc (char *page, char **start, off_t off, int count, int
        off_t begin = 0;
        char tmp[40];
 
-       dbg("%s", __FUNCTION__);
+       dbg("%s", __func__);
        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);
@@ -512,19 +455,11 @@ 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__);
-               return -ENODEV;
-       }
+       dbg("%s - port %d", __func__, port->number);
 
+       WARN_ON(!port->port.count);
        if (port->serial->type->tiocmget)
-               return port->serial->type->tiocmget(port, file);
-
+               return port->serial->type->tiocmget(tty, file);
        return -EINVAL;
 }
 
@@ -533,19 +468,11 @@ 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__);
-               return -ENODEV;
-       }
+       dbg("%s - port %d", __func__, port->number);
 
+       WARN_ON(!port->port.count);
        if (port->serial->type->tiocmset)
-               return port->serial->type->tiocmset(port, file, set, clear);
-
+               return port->serial->type->tiocmset(tty, file, set, clear);
        return -EINVAL;
 }
 
@@ -565,12 +492,12 @@ static void usb_serial_port_work(struct work_struct *work)
                container_of(work, struct usb_serial_port, work);
        struct tty_struct *tty;
 
-       dbg("%s - port %d", __FUNCTION__, port->number);
+       dbg("%s - port %d", __func__, port->number);
        
        if (!port)
                return;
 
-       tty = port->tty;
+       tty = port->port.tty;
        if (!tty)
                return;
 
@@ -581,7 +508,7 @@ static void port_release(struct device *dev)
 {
        struct usb_serial_port *port = to_usb_serial_port(dev);
 
-       dbg ("%s - %s", __FUNCTION__, dev->bus_id);
+       dbg ("%s - %s", __func__, dev_name(dev));
        port_free(port);
 }
 
@@ -627,7 +554,7 @@ static struct usb_serial * create_serial (struct usb_device *dev,
 
        serial = kzalloc(sizeof(*serial), GFP_KERNEL);
        if (!serial) {
-               dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
+               dev_err(&dev->dev, "%s - out of memory\n", __func__);
                return NULL;
        }
        serial->dev = usb_get_dev(dev);
@@ -701,7 +628,7 @@ int usb_serial_probe(struct usb_interface *interface,
        struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
        struct usb_serial_driver *type = NULL;
        int retval;
-       int minor;
+       unsigned int minor;
        int buffer_size;
        int i;
        int num_interrupt_in = 0;
@@ -722,7 +649,7 @@ int usb_serial_probe(struct usb_interface *interface,
        serial = create_serial (dev, interface, type);
        if (!serial) {
                unlock_kernel();
-               dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
+               dev_err(&interface->dev, "%s - out of memory\n", __func__);
                return -ENOMEM;
        }
 
@@ -854,22 +781,6 @@ int usb_serial_probe(struct usb_interface *interface,
        serial->num_interrupt_in = num_interrupt_in;
        serial->num_interrupt_out = num_interrupt_out;
 
-#if 0
-       /* check that the device meets the driver's requirements */
-       if ((type->num_interrupt_in != NUM_DONT_CARE &&
-                               type->num_interrupt_in != num_interrupt_in)
-                       || (type->num_interrupt_out != NUM_DONT_CARE &&
-                               type->num_interrupt_out != num_interrupt_out)
-                       || (type->num_bulk_in != NUM_DONT_CARE &&
-                               type->num_bulk_in != num_bulk_in)
-                       || (type->num_bulk_out != NUM_DONT_CARE &&
-                               type->num_bulk_out != num_bulk_out)) {
-               dbg("wrong number of endpoints");
-               kfree(serial);
-               return -EIO;
-       }
-#endif
-
        /* found all that we need */
        dev_info(&interface->dev, "%s converter detected\n",
                        type->description);
@@ -883,7 +794,7 @@ int usb_serial_probe(struct usb_interface *interface,
        serial->num_port_pointers = max_endpoints;
        unlock_kernel();
 
-       dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
+       dbg("%s - setting up %d port structures for this device", __func__, max_endpoints);
        for (i = 0; i < max_endpoints; ++i) {
                port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
                if (!port)
@@ -1030,8 +941,8 @@ int usb_serial_probe(struct usb_interface *interface,
                port->dev.bus = &usb_serial_bus_type;
                port->dev.release = &port_release;
 
-               snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
-               dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
+               dev_set_name(&port->dev, "ttyUSB%d", port->number);
+               dbg ("%s - registering %s", __func__, dev_name(&port->dev));
                retval = device_register(&port->dev);
                if (retval)
                        dev_err(&port->dev, "Error registering port device, "
@@ -1090,7 +1001,7 @@ void usb_serial_disconnect(struct usb_interface *interface)
        struct usb_serial_port *port;
 
        usb_serial_console_disconnect(serial);
-       dbg ("%s", __FUNCTION__);
+       dbg ("%s", __func__);
 
        mutex_lock(&serial->disc_mutex);
        usb_set_intfdata (interface, NULL);
@@ -1099,8 +1010,8 @@ void usb_serial_disconnect(struct usb_interface *interface)
        for (i = 0; i < serial->num_ports; ++i) {
                port = serial->port[i];
                if (port) {
-                       if (port->tty)
-                               tty_hangup(port->tty);
+                       if (port->port.tty)
+                               tty_hangup(port->port.tty);
                        kill_traffic(port);
                }
        }
@@ -1174,7 +1085,7 @@ static int __init usb_serial_init(void)
 
        result = bus_register(&usb_serial_bus_type);
        if (result) {
-               err("%s - registering bus driver failed", __FUNCTION__);
+               err("%s - registering bus driver failed", __func__);
                goto exit_bus;
        }
 
@@ -1188,24 +1099,26 @@ static int __init usb_serial_init(void)
        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;
+       usb_serial_tty_driver->init_termios.c_ispeed = 9600;
+       usb_serial_tty_driver->init_termios.c_ospeed = 9600;
        tty_set_operations(usb_serial_tty_driver, &serial_ops);
        result = tty_register_driver(usb_serial_tty_driver);
        if (result) {
-               err("%s - tty_register_driver failed", __FUNCTION__);
+               err("%s - tty_register_driver failed", __func__);
                goto exit_reg_driver;
        }
 
        /* register the USB driver */
        result = usb_register(&usb_serial_driver);
        if (result < 0) {
-               err("%s - usb_register failed", __FUNCTION__);
+               err("%s - usb_register failed", __func__);
                goto exit_tty;
        }
 
        /* register the generic driver, if we should */
        result = usb_serial_generic_register(debug);
        if (result < 0) {
-               err("%s - registering generic driver failed", __FUNCTION__);
+               err("%s - registering generic driver failed", __func__);
                goto exit_generic;
        }
 
@@ -1223,7 +1136,7 @@ exit_reg_driver:
        bus_unregister(&usb_serial_bus_type);
 
 exit_bus:
-       err ("%s - returning with error %d", __FUNCTION__, result);
+       err ("%s - returning with error %d", __func__, result);
        put_tty_driver(usb_serial_tty_driver);
        return result;
 }