Commit cc56cd01 authored by Alan Stern's avatar Alan Stern Committed by Live-CD User

usb-serial: acquire references when a new tty is installed

This patch (as1287) makes serial_install() be reponsible for acquiring
references to the usb_serial structure and the driver module when a
tty is first used.  This is more sensible than having serial_open() do
it, because a tty can be opened many times whereas it is installed
only once, when it is created.  (Not to mention that these actions are
reversed when the tty is released, not when it is closed.)  Finally,
it is at install time that the TTY core takes its own reference to the
usb_serial module, so it is only fitting that we should act the same
way in regard to the lower-level serial driver.
Signed-off-by: default avatarAlan Stern <stern@rowland.harvard.edu>
Cc: stable <stable@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 8bc2c1b2
...@@ -187,100 +187,92 @@ void usb_serial_put(struct usb_serial *serial) ...@@ -187,100 +187,92 @@ void usb_serial_put(struct usb_serial *serial)
* Create the termios objects for this tty. We use the default * Create the termios objects for this tty. We use the default
* USB serial settings but permit them to be overridden by * USB serial settings but permit them to be overridden by
* serial->type->init_termios. * serial->type->init_termios.
*
* This is the first place a new tty gets used. Hence this is where we
* acquire references to the usb_serial structure and the driver module,
* where we store a pointer to the port, and where we do an autoresume.
* All these actions are reversed in serial_do_free().
*/ */
static int serial_install(struct tty_driver *driver, struct tty_struct *tty) static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
{ {
int idx = tty->index; int idx = tty->index;
struct usb_serial *serial; struct usb_serial *serial;
int retval; struct usb_serial_port *port;
int retval = -ENODEV;
serial = usb_serial_get_by_index(idx);
if (!serial)
return retval;
port = serial->port[idx - serial->minor];
if (!port)
goto error_no_port;
if (!try_module_get(serial->type->driver.owner))
goto error_module_get;
retval = usb_autopm_get_interface(serial->interface);
if (retval)
goto error_get_interface;
/* If the termios setup has yet to be done */ /* If the termios setup has yet to be done */
if (tty->driver->termios[idx] == NULL) { if (tty->driver->termios[idx] == NULL) {
/* perform the standard setup */ /* perform the standard setup */
retval = tty_init_termios(tty); retval = tty_init_termios(tty);
if (retval) if (retval)
return retval; goto error_init_termios;
/* allow the driver to update it */ /* allow the driver to update it */
serial = usb_serial_get_by_index(tty->index);
if (serial) {
if (serial->type->init_termios) if (serial->type->init_termios)
serial->type->init_termios(tty); serial->type->init_termios(tty);
usb_serial_put(serial);
mutex_unlock(&serial->disc_mutex);
}
} }
mutex_unlock(&serial->disc_mutex);
tty->driver_data = port;
/* Final install (we use the default method) */ /* Final install (we use the default method) */
tty_driver_kref_get(driver); tty_driver_kref_get(driver);
tty->count++; tty->count++;
driver->ttys[idx] = tty; driver->ttys[idx] = tty;
return 0; return retval;
error_init_termios:
usb_autopm_put_interface(serial->interface);
error_get_interface:
module_put(serial->type->driver.owner);
error_module_get:
error_no_port:
usb_serial_put(serial);
mutex_unlock(&serial->disc_mutex);
return retval;
} }
static int serial_open (struct tty_struct *tty, struct file *filp) static int serial_open (struct tty_struct *tty, struct file *filp)
{ {
struct usb_serial *serial; struct usb_serial *serial;
struct usb_serial_port *port; struct usb_serial_port *port;
unsigned int portNumber;
int retval = 0; int retval = 0;
int first = 0; int first = 0;
dbg("%s", __func__); dbg("%s", __func__);
/* get the serial object associated with this tty pointer */ port = tty->driver_data;
serial = usb_serial_get_by_index(tty->index); serial = port->serial;
if (!serial) {
tty->driver_data = NULL;
return -ENODEV;
}
portNumber = tty->index - serial->minor;
port = serial->port[portNumber];
if (!port || serial->disconnected)
retval = -ENODEV;
/*
* Note: Our locking order requirement does not allow port->mutex
* to be acquired while serial->disc_mutex is held.
*/
mutex_unlock(&serial->disc_mutex);
if (retval)
goto bailout_serial_put;
if (mutex_lock_interruptible(&port->mutex)) { if (mutex_lock_interruptible(&port->mutex))
retval = -ERESTARTSYS; return -ERESTARTSYS;
goto bailout_serial_put;
}
++port->port.count; ++port->port.count;
/* set up our port structure making the tty driver
* remember our port object, and us it */
tty->driver_data = port;
tty_port_tty_set(&port->port, tty); tty_port_tty_set(&port->port, tty);
/* If the console is attached, the device is already open */ /* If the console is attached, the device is already open */
if (port->port.count == 1 && !port->console) { if (port->port.count == 1 && !port->console) {
first = 1; first = 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_mutex_unlock;
}
mutex_lock(&serial->disc_mutex); mutex_lock(&serial->disc_mutex);
if (serial->disconnected)
retval = -ENODEV;
else
retval = usb_autopm_get_interface(serial->interface);
if (retval)
goto bailout_module_put;
/* only call the device specific open if this /* only call the device specific open if this
* is the first time the port is opened */ * is the first time the port is opened */
retval = serial->type->open(tty, port); retval = serial->type->open(tty, port);
if (retval) if (retval)
goto bailout_interface_put; goto bailout_module_put;
mutex_unlock(&serial->disc_mutex); mutex_unlock(&serial->disc_mutex);
set_bit(ASYNCB_INITIALIZED, &port->port.flags); set_bit(ASYNCB_INITIALIZED, &port->port.flags);
} }
...@@ -297,18 +289,11 @@ static int serial_open (struct tty_struct *tty, struct file *filp) ...@@ -297,18 +289,11 @@ static int serial_open (struct tty_struct *tty, struct file *filp)
goto bailout_mutex_unlock; goto bailout_mutex_unlock;
/* Undo the initial port actions */ /* Undo the initial port actions */
mutex_lock(&serial->disc_mutex); mutex_lock(&serial->disc_mutex);
bailout_interface_put:
usb_autopm_put_interface(serial->interface);
bailout_module_put: bailout_module_put:
mutex_unlock(&serial->disc_mutex); mutex_unlock(&serial->disc_mutex);
module_put(serial->type->driver.owner);
bailout_mutex_unlock: bailout_mutex_unlock:
port->port.count = 0; port->port.count = 0;
tty->driver_data = NULL;
tty_port_tty_set(&port->port, NULL);
mutex_unlock(&port->mutex); mutex_unlock(&port->mutex);
bailout_serial_put:
usb_serial_put(serial);
return retval; return retval;
} }
...@@ -355,9 +340,6 @@ static void serial_close(struct tty_struct *tty, struct file *filp) ...@@ -355,9 +340,6 @@ static void serial_close(struct tty_struct *tty, struct file *filp)
{ {
struct usb_serial_port *port = tty->driver_data; struct usb_serial_port *port = tty->driver_data;
if (!port)
return;
dbg("%s - port %d", __func__, port->number); dbg("%s - port %d", __func__, port->number);
if (tty_port_close_start(&port->port, tty, filp) == 0) if (tty_port_close_start(&port->port, tty, filp) == 0)
...@@ -365,7 +347,6 @@ static void serial_close(struct tty_struct *tty, struct file *filp) ...@@ -365,7 +347,6 @@ static void serial_close(struct tty_struct *tty, struct file *filp)
serial_do_down(port); serial_do_down(port);
tty_port_close_end(&port->port, tty); tty_port_close_end(&port->port, tty);
tty_port_tty_set(&port->port, NULL); tty_port_tty_set(&port->port, NULL);
} }
/** /**
...@@ -386,9 +367,11 @@ static void serial_do_free(struct tty_struct *tty) ...@@ -386,9 +367,11 @@ static void serial_do_free(struct tty_struct *tty)
/* The console is magical. Do not hang up the console hardware /* The console is magical. Do not hang up the console hardware
* or there will be tears. * or there will be tears.
*/ */
if (port == NULL || port->console) if (port->console)
return; return;
tty->driver_data = NULL;
serial = port->serial; serial = port->serial;
owner = serial->type->driver.owner; owner = serial->type->driver.owner;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment