usb_serial: Fix remaining ref count/lock bugs

This fixes
- locking bug that was hidden by ecc2e05e739c30870c8e4f252b63a0c4041f2724
- Regression #13821
- Spurious warning when closing and blocking for data write out

With these changes my PL2303 always ends up as ttyUSB0 when it should and
the module refcounts stay correct.

I'll do a more wholesale split & tidy of _open in the next release or two
as we get a standard tty_port_open and port->ops->init port->ops->shutdown
call backs.

Copy sent to Alan Stern and Carlos Mafra just to confirm it fixes all the
reports but it passes local testing with the same hardware as Alan Stern.

Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Alan Cox and committed by Linus Torvalds c56d3000 fc013a58

+13 -4
+13 -4
drivers/usb/serial/usb-serial.c
··· 32 #include <linux/mutex.h> 33 #include <linux/list.h> 34 #include <linux/uaccess.h> 35 #include <linux/usb.h> 36 #include <linux/usb/serial.h> 37 #include "pl2303.h" ··· 185 struct usb_serial_port *port; 186 unsigned int portNumber; 187 int retval = 0; 188 189 dbg("%s", __func__); 190 ··· 225 226 /* If the console is attached, the device is already open */ 227 if (port->port.count == 1 && !port->console) { 228 - 229 /* lock this module before we call it 230 * this may fail, which means we must bail out, 231 * safe because we are called with BKL held */ ··· 248 if (retval) 249 goto bailout_interface_put; 250 mutex_unlock(&serial->disc_mutex); 251 } 252 mutex_unlock(&port->mutex); 253 /* Now do the correct tty layer semantics */ 254 retval = tty_port_block_til_ready(&port->port, tty, filp); 255 - if (retval == 0) 256 return 0; 257 - 258 bailout_interface_put: 259 usb_autopm_put_interface(serial->interface); 260 bailout_module_put: ··· 421 struct usb_serial_port *port = tty->driver_data; 422 dbg("%s = port %d", __func__, port->number); 423 424 - WARN_ON(!port->port.count); 425 /* if the device was unplugged then any remaining characters 426 fell out of the connector ;) */ 427 if (port->serial->disconnected)
··· 32 #include <linux/mutex.h> 33 #include <linux/list.h> 34 #include <linux/uaccess.h> 35 + #include <linux/serial.h> 36 #include <linux/usb.h> 37 #include <linux/usb/serial.h> 38 #include "pl2303.h" ··· 184 struct usb_serial_port *port; 185 unsigned int portNumber; 186 int retval = 0; 187 + int first = 0; 188 189 dbg("%s", __func__); 190 ··· 223 224 /* If the console is attached, the device is already open */ 225 if (port->port.count == 1 && !port->console) { 226 + first = 1; 227 /* lock this module before we call it 228 * this may fail, which means we must bail out, 229 * safe because we are called with BKL held */ ··· 246 if (retval) 247 goto bailout_interface_put; 248 mutex_unlock(&serial->disc_mutex); 249 + set_bit(ASYNCB_INITIALIZED, &port->port.flags); 250 } 251 mutex_unlock(&port->mutex); 252 /* Now do the correct tty layer semantics */ 253 retval = tty_port_block_til_ready(&port->port, tty, filp); 254 + if (retval == 0) { 255 + if (!first) 256 + usb_serial_put(serial); 257 return 0; 258 + } 259 + mutex_lock(&port->mutex); 260 + if (first == 0) 261 + goto bailout_mutex_unlock; 262 + /* Undo the initial port actions */ 263 + mutex_lock(&serial->disc_mutex); 264 bailout_interface_put: 265 usb_autopm_put_interface(serial->interface); 266 bailout_module_put: ··· 411 struct usb_serial_port *port = tty->driver_data; 412 dbg("%s = port %d", __func__, port->number); 413 414 /* if the device was unplugged then any remaining characters 415 fell out of the connector ;) */ 416 if (port->serial->disconnected)