serial_core: fix uart PORT_UNKNOWN handling

While porting a RS485 driver from 2.6.29 to 3.14, i noticed that the serial tty
driver could break it by using uart ports that it does not own :

1. uart_change_pm ist called during uart_open and calls the uart pm function
without checking for PORT_UNKNOWN.
The fix is to move uart_change_pm from uart_open to uart_port_startup.
2. The return code from the uart request_port call in uart_set_info is not
handled properly, leading to the situation that the serial driver also
thinks it owns the uart ports.
This can triggered by doing following actions :

setserial /dev/ttyS0 uart none # release the uart ports
modprobe lirc-serial # or any other device that uses the uart
setserial /dev/ttyS0 uart 16550 # gives no error and the uart tty driver
# can use the ports as well

Signed-off-by: Thomas Pfaff <tpfaff@pcs.com>
Reviewed-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Thomas Pfaff and committed by
Greg Kroah-Hartman
7deb39ed f94b0572

+21 -18
+21 -18
drivers/tty/serial/serial_core.c
··· 137 137 return 1; 138 138 139 139 /* 140 + * Make sure the device is in D0 state. 141 + */ 142 + uart_change_pm(state, UART_PM_STATE_ON); 143 + 144 + /* 140 145 * Initialise and allocate the transmit and temporary 141 146 * buffer. 142 147 */ ··· 830 825 * If we fail to request resources for the 831 826 * new port, try to restore the old settings. 832 827 */ 833 - if (retval && old_type != PORT_UNKNOWN) { 828 + if (retval) { 834 829 uport->iobase = old_iobase; 835 830 uport->type = old_type; 836 831 uport->hub6 = old_hub6; 837 832 uport->iotype = old_iotype; 838 833 uport->regshift = old_shift; 839 834 uport->mapbase = old_mapbase; 840 - retval = uport->ops->request_port(uport); 841 - /* 842 - * If we failed to restore the old settings, 843 - * we fail like this. 844 - */ 845 - if (retval) 846 - uport->type = PORT_UNKNOWN; 847 835 848 - /* 849 - * We failed anyway. 850 - */ 851 - retval = -EBUSY; 836 + if (old_type != PORT_UNKNOWN) { 837 + retval = uport->ops->request_port(uport); 838 + /* 839 + * If we failed to restore the old settings, 840 + * we fail like this. 841 + */ 842 + if (retval) 843 + uport->type = PORT_UNKNOWN; 844 + 845 + /* 846 + * We failed anyway. 847 + */ 848 + retval = -EBUSY; 849 + } 850 + 852 851 /* Added to return the correct error -Ram Gupta */ 853 852 goto exit; 854 853 } ··· 1578 1569 retval = -EAGAIN; 1579 1570 goto err_dec_count; 1580 1571 } 1581 - 1582 - /* 1583 - * Make sure the device is in D0 state. 1584 - */ 1585 - if (port->count == 1) 1586 - uart_change_pm(state, UART_PM_STATE_ON); 1587 1572 1588 1573 /* 1589 1574 * Start up the serial port.