Skip to content

Commit 320348c

Browse files
AlanSternLive-CD User
authored andcommitted
usb-serial: straighten out serial_open
This patch (as1291) removes a bunch of code from serial_open(), things that were rendered unnecessary by earlier patches. A missing spinlock is added to protect port->port.count, which needs to be incremented even if the open fails but not if the tty has gotten a hangup. The test for whether the hardware has been initialized, based on the use count, is replaced by a more transparent test of the ASYNCB_INITIALIZED bit in the port flags. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Cc: stable <stable@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent ff8324d commit 320348c

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

drivers/usb/serial/usb-serial.c

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -245,55 +245,40 @@ static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
245245
return retval;
246246
}
247247

248-
static int serial_open (struct tty_struct *tty, struct file *filp)
248+
static int serial_open(struct tty_struct *tty, struct file *filp)
249249
{
250-
struct usb_serial *serial;
251-
struct usb_serial_port *port;
252-
int retval = 0;
253-
int first = 0;
254-
255-
port = tty->driver_data;
256-
serial = port->serial;
250+
struct usb_serial_port *port = tty->driver_data;
251+
struct usb_serial *serial = port->serial;
252+
int retval;
257253

258254
dbg("%s - port %d", __func__, port->number);
259255

260-
if (mutex_lock_interruptible(&port->mutex))
261-
return -ERESTARTSYS;
262-
263-
++port->port.count;
256+
spin_lock_irq(&port->port.lock);
257+
if (!tty_hung_up_p(filp))
258+
++port->port.count;
259+
spin_unlock_irq(&port->port.lock);
264260
tty_port_tty_set(&port->port, tty);
265261

266-
/* If the console is attached, the device is already open */
267-
if (port->port.count == 1 && !port->console) {
268-
first = 1;
262+
/* Do the device-specific open only if the hardware isn't
263+
* already initialized.
264+
*/
265+
if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) {
266+
if (mutex_lock_interruptible(&port->mutex))
267+
return -ERESTARTSYS;
269268
mutex_lock(&serial->disc_mutex);
270-
271-
/* only call the device specific open if this
272-
* is the first time the port is opened */
273-
retval = serial->type->open(tty, port);
274-
if (retval)
275-
goto bailout_module_put;
269+
if (serial->disconnected)
270+
retval = -ENODEV;
271+
else
272+
retval = port->serial->type->open(tty, port);
276273
mutex_unlock(&serial->disc_mutex);
274+
mutex_unlock(&port->mutex);
275+
if (retval)
276+
return retval;
277277
set_bit(ASYNCB_INITIALIZED, &port->port.flags);
278278
}
279-
mutex_unlock(&port->mutex);
279+
280280
/* Now do the correct tty layer semantics */
281281
retval = tty_port_block_til_ready(&port->port, tty, filp);
282-
if (retval == 0) {
283-
if (!first)
284-
usb_serial_put(serial);
285-
return 0;
286-
}
287-
mutex_lock(&port->mutex);
288-
if (first == 0)
289-
goto bailout_mutex_unlock;
290-
/* Undo the initial port actions */
291-
mutex_lock(&serial->disc_mutex);
292-
bailout_module_put:
293-
mutex_unlock(&serial->disc_mutex);
294-
bailout_mutex_unlock:
295-
port->port.count = 0;
296-
mutex_unlock(&port->mutex);
297282
return retval;
298283
}
299284

0 commit comments

Comments
 (0)