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