Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Driver core for serial ports |
| 3 | * |
| 4 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 5 | * |
| 6 | * Copyright 1999 ARM Limited |
| 7 | * Copyright (C) 2000-2001 Deep Blue Solutions Ltd. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/tty.h> |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 25 | #include <linux/tty_flip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/console.h> |
Grant Likely | a208ffd | 2014-03-27 18:29:46 -0700 | [diff] [blame] | 29 | #include <linux/of.h> |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 30 | #include <linux/proc_fs.h> |
| 31 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/device.h> |
| 33 | #include <linux/serial.h> /* for serial_state and serial_icounter_struct */ |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 34 | #include <linux/serial_core.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/delay.h> |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #include <asm/irq.h> |
| 39 | #include <asm/uaccess.h> |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
| 42 | * This is used to lock changes in serial line configuration. |
| 43 | */ |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 44 | static DEFINE_MUTEX(port_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Ingo Molnar | 13e8359 | 2006-07-03 00:25:03 -0700 | [diff] [blame] | 46 | /* |
| 47 | * lockdep: port->lock is initialized in two places, but we |
| 48 | * want only one lock-class: |
| 49 | */ |
| 50 | static struct lock_class_key port_lock_key; |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) |
| 53 | |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 54 | static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 55 | struct ktermios *old_termios); |
Jiri Slaby | 1f33a51 | 2011-07-14 14:35:10 +0200 | [diff] [blame] | 56 | static void uart_wait_until_sent(struct tty_struct *tty, int timeout); |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 57 | static void uart_change_pm(struct uart_state *state, |
| 58 | enum uart_pm_state pm_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Jiri Slaby | b922e19 | 2011-11-09 21:33:51 +0100 | [diff] [blame] | 60 | static void uart_port_shutdown(struct tty_port *port); |
| 61 | |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 62 | static int uart_dcd_enabled(struct uart_port *uport) |
| 63 | { |
Peter Hurley | d4260b5 | 2014-10-16 14:19:47 -0400 | [diff] [blame] | 64 | return !!(uport->status & UPSTAT_DCD_ENABLE); |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* |
| 68 | * This routine is used by the interrupt handler to schedule processing in |
| 69 | * the software interrupt portion of the driver. |
| 70 | */ |
| 71 | void uart_write_wakeup(struct uart_port *port) |
| 72 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 73 | struct uart_state *state = port->state; |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 74 | /* |
| 75 | * This means you called this function _after_ the port was |
| 76 | * closed. No cookie for you. |
| 77 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 78 | BUG_ON(!state); |
Jiri Slaby | 6a3e492b | 2011-07-14 14:35:12 +0200 | [diff] [blame] | 79 | tty_wakeup(state->port.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void uart_stop(struct tty_struct *tty) |
| 83 | { |
| 84 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 85 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | unsigned long flags; |
| 87 | |
| 88 | spin_lock_irqsave(&port->lock, flags); |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 89 | port->ops->stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | spin_unlock_irqrestore(&port->lock, flags); |
| 91 | } |
| 92 | |
| 93 | static void __uart_start(struct tty_struct *tty) |
| 94 | { |
| 95 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 96 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 98 | if (!uart_tx_stopped(port)) |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 99 | port->ops->start_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void uart_start(struct tty_struct *tty) |
| 103 | { |
| 104 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 105 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | unsigned long flags; |
| 107 | |
| 108 | spin_lock_irqsave(&port->lock, flags); |
| 109 | __uart_start(tty); |
| 110 | spin_unlock_irqrestore(&port->lock, flags); |
| 111 | } |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | static inline void |
| 114 | uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear) |
| 115 | { |
| 116 | unsigned long flags; |
| 117 | unsigned int old; |
| 118 | |
| 119 | spin_lock_irqsave(&port->lock, flags); |
| 120 | old = port->mctrl; |
| 121 | port->mctrl = (old & ~clear) | set; |
| 122 | if (old != port->mctrl) |
| 123 | port->ops->set_mctrl(port, port->mctrl); |
| 124 | spin_unlock_irqrestore(&port->lock, flags); |
| 125 | } |
| 126 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 127 | #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0) |
| 128 | #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Startup the port. This will be called once per open. All calls |
Alan Cox | df4f4dd | 2008-07-16 21:53:50 +0100 | [diff] [blame] | 132 | * will be serialised by the per-port mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | */ |
Jiri Slaby | c0d92be | 2011-11-09 21:34:14 +0100 | [diff] [blame] | 134 | static int uart_port_startup(struct tty_struct *tty, struct uart_state *state, |
| 135 | int init_hw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 137 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | unsigned long page; |
| 139 | int retval = 0; |
| 140 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 141 | if (uport->type == PORT_UNKNOWN) |
Jiri Slaby | c0d92be | 2011-11-09 21:34:14 +0100 | [diff] [blame] | 142 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
Thomas Pfaff | 7deb39e | 2014-04-23 12:33:22 +0200 | [diff] [blame] | 145 | * Make sure the device is in D0 state. |
| 146 | */ |
| 147 | uart_change_pm(state, UART_PM_STATE_ON); |
| 148 | |
| 149 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | * Initialise and allocate the transmit and temporary |
| 151 | * buffer. |
| 152 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 153 | if (!state->xmit.buf) { |
Alan Cox | df4f4dd | 2008-07-16 21:53:50 +0100 | [diff] [blame] | 154 | /* This is protected by the per port mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | page = get_zeroed_page(GFP_KERNEL); |
| 156 | if (!page) |
| 157 | return -ENOMEM; |
| 158 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 159 | state->xmit.buf = (unsigned char *) page; |
| 160 | uart_circ_clear(&state->xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 163 | retval = uport->ops->startup(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (retval == 0) { |
Jiri Slaby | c7d7abf | 2011-03-30 00:10:55 +0200 | [diff] [blame] | 165 | if (uart_console(uport) && uport->cons->cflag) { |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 166 | tty->termios.c_cflag = uport->cons->cflag; |
Jiri Slaby | c7d7abf | 2011-03-30 00:10:55 +0200 | [diff] [blame] | 167 | uport->cons->cflag = 0; |
| 168 | } |
| 169 | /* |
| 170 | * Initialise the hardware port settings. |
| 171 | */ |
| 172 | uart_change_speed(tty, state, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Jiri Slaby | c7d7abf | 2011-03-30 00:10:55 +0200 | [diff] [blame] | 174 | if (init_hw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* |
| 176 | * Setup the RTS and DTR signals once the |
| 177 | * port is open and ready to respond. |
| 178 | */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 179 | if (tty->termios.c_cflag & CBAUD) |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 180 | uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
Peter Hurley | a6eec92 | 2014-09-02 17:39:10 -0400 | [diff] [blame] | 182 | |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 183 | spin_lock_irq(&uport->lock); |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 184 | if (uart_cts_enabled(uport) && |
| 185 | !(uport->ops->get_mctrl(uport) & TIOCM_CTS)) |
| 186 | uport->hw_stopped = 1; |
| 187 | else |
| 188 | uport->hw_stopped = 0; |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 189 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jiri Slaby | 0055197 | 2011-08-17 13:48:15 +0200 | [diff] [blame] | 192 | /* |
| 193 | * This is to allow setserial on this port. People may want to set |
| 194 | * port/irq/type and then reconfigure the port properly if it failed |
| 195 | * now. |
| 196 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (retval && capable(CAP_SYS_ADMIN)) |
Jiri Slaby | c0d92be | 2011-11-09 21:34:14 +0100 | [diff] [blame] | 198 | return 1; |
| 199 | |
| 200 | return retval; |
| 201 | } |
| 202 | |
| 203 | static int uart_startup(struct tty_struct *tty, struct uart_state *state, |
| 204 | int init_hw) |
| 205 | { |
| 206 | struct tty_port *port = &state->port; |
| 207 | int retval; |
| 208 | |
| 209 | if (port->flags & ASYNC_INITIALIZED) |
| 210 | return 0; |
| 211 | |
| 212 | /* |
| 213 | * Set the TTY IO error marker - we will only clear this |
| 214 | * once we have successfully opened the port. |
| 215 | */ |
| 216 | set_bit(TTY_IO_ERROR, &tty->flags); |
| 217 | |
| 218 | retval = uart_port_startup(tty, state, init_hw); |
| 219 | if (!retval) { |
| 220 | set_bit(ASYNCB_INITIALIZED, &port->flags); |
| 221 | clear_bit(TTY_IO_ERROR, &tty->flags); |
| 222 | } else if (retval > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | retval = 0; |
| 224 | |
| 225 | return retval; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * This routine will shutdown a serial port; interrupts are disabled, and |
| 230 | * DTR is dropped if the hangup on close termio flag is on. Calls to |
| 231 | * uart_shutdown are serialised by the per-port semaphore. |
| 232 | */ |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 233 | static void uart_shutdown(struct tty_struct *tty, struct uart_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 235 | struct uart_port *uport = state->uart_port; |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 236 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 238 | /* |
| 239 | * Set the TTY IO error marker |
| 240 | */ |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 241 | if (tty) |
| 242 | set_bit(TTY_IO_ERROR, &tty->flags); |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 243 | |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 244 | if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) { |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 245 | /* |
| 246 | * Turn off DTR and RTS early. |
| 247 | */ |
Peter Hurley | ae84db9 | 2014-07-09 09:21:14 -0400 | [diff] [blame] | 248 | if (uart_console(uport) && tty) |
| 249 | uport->cons->cflag = tty->termios.c_cflag; |
| 250 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 251 | if (!tty || (tty->termios.c_cflag & HUPCL)) |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 252 | uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS); |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 253 | |
Jiri Slaby | b922e19 | 2011-11-09 21:33:51 +0100 | [diff] [blame] | 254 | uart_port_shutdown(port); |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | /* |
Doug Anderson | d208a3b | 2011-10-19 11:52:01 -0700 | [diff] [blame] | 258 | * It's possible for shutdown to be called after suspend if we get |
| 259 | * a DCD drop (hangup) at just the right time. Clear suspended bit so |
| 260 | * we don't try to resume a port that has been shutdown. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | */ |
Doug Anderson | d208a3b | 2011-10-19 11:52:01 -0700 | [diff] [blame] | 262 | clear_bit(ASYNCB_SUSPENDED, &port->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | /* |
| 265 | * Free the transmit buffer page. |
| 266 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 267 | if (state->xmit.buf) { |
| 268 | free_page((unsigned long)state->xmit.buf); |
| 269 | state->xmit.buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * uart_update_timeout - update per-port FIFO timeout. |
| 275 | * @port: uart_port structure describing the port |
| 276 | * @cflag: termios cflag value |
| 277 | * @baud: speed of the port |
| 278 | * |
| 279 | * Set the port FIFO timeout value. The @cflag value should |
| 280 | * reflect the actual hardware settings. |
| 281 | */ |
| 282 | void |
| 283 | uart_update_timeout(struct uart_port *port, unsigned int cflag, |
| 284 | unsigned int baud) |
| 285 | { |
| 286 | unsigned int bits; |
| 287 | |
| 288 | /* byte size and parity */ |
| 289 | switch (cflag & CSIZE) { |
| 290 | case CS5: |
| 291 | bits = 7; |
| 292 | break; |
| 293 | case CS6: |
| 294 | bits = 8; |
| 295 | break; |
| 296 | case CS7: |
| 297 | bits = 9; |
| 298 | break; |
| 299 | default: |
| 300 | bits = 10; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 301 | break; /* CS8 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | if (cflag & CSTOPB) |
| 305 | bits++; |
| 306 | if (cflag & PARENB) |
| 307 | bits++; |
| 308 | |
| 309 | /* |
| 310 | * The total number of bits to be transmitted in the fifo. |
| 311 | */ |
| 312 | bits = bits * port->fifosize; |
| 313 | |
| 314 | /* |
| 315 | * Figure the timeout to send the above number of bits. |
| 316 | * Add .02 seconds of slop |
| 317 | */ |
| 318 | port->timeout = (HZ * bits) / baud + HZ/50; |
| 319 | } |
| 320 | |
| 321 | EXPORT_SYMBOL(uart_update_timeout); |
| 322 | |
| 323 | /** |
| 324 | * uart_get_baud_rate - return baud rate for a particular port |
| 325 | * @port: uart_port structure describing the port in question. |
| 326 | * @termios: desired termios settings. |
| 327 | * @old: old termios (or NULL) |
| 328 | * @min: minimum acceptable baud rate |
| 329 | * @max: maximum acceptable baud rate |
| 330 | * |
| 331 | * Decode the termios structure into a numeric baud rate, |
| 332 | * taking account of the magic 38400 baud rate (with spd_* |
| 333 | * flags), and mapping the %B0 rate to 9600 baud. |
| 334 | * |
| 335 | * If the new baud rate is invalid, try the old termios setting. |
| 336 | * If it's still invalid, we try 9600 baud. |
| 337 | * |
| 338 | * Update the @termios structure to reflect the baud rate |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 339 | * we're actually going to be using. Don't do this for the case |
| 340 | * where B0 is requested ("hang up"). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | */ |
| 342 | unsigned int |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 343 | uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, |
| 344 | struct ktermios *old, unsigned int min, unsigned int max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | unsigned int try, baud, altbaud = 38400; |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 347 | int hung_up = 0; |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 348 | upf_t flags = port->flags & UPF_SPD_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | if (flags == UPF_SPD_HI) |
| 351 | altbaud = 57600; |
André Goddard Rosa | 82cb7ba | 2009-10-25 11:18:26 -0200 | [diff] [blame] | 352 | else if (flags == UPF_SPD_VHI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | altbaud = 115200; |
André Goddard Rosa | 82cb7ba | 2009-10-25 11:18:26 -0200 | [diff] [blame] | 354 | else if (flags == UPF_SPD_SHI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | altbaud = 230400; |
André Goddard Rosa | 82cb7ba | 2009-10-25 11:18:26 -0200 | [diff] [blame] | 356 | else if (flags == UPF_SPD_WARP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | altbaud = 460800; |
| 358 | |
| 359 | for (try = 0; try < 2; try++) { |
| 360 | baud = tty_termios_baud_rate(termios); |
| 361 | |
| 362 | /* |
| 363 | * The spd_hi, spd_vhi, spd_shi, spd_warp kludge... |
| 364 | * Die! Die! Die! |
| 365 | */ |
| 366 | if (baud == 38400) |
| 367 | baud = altbaud; |
| 368 | |
| 369 | /* |
| 370 | * Special case: B0 rate. |
| 371 | */ |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 372 | if (baud == 0) { |
| 373 | hung_up = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | baud = 9600; |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
| 377 | if (baud >= min && baud <= max) |
| 378 | return baud; |
| 379 | |
| 380 | /* |
| 381 | * Oops, the quotient was zero. Try again with |
| 382 | * the old baud rate if possible. |
| 383 | */ |
| 384 | termios->c_cflag &= ~CBAUD; |
| 385 | if (old) { |
Alan Cox | 6d4d67b | 2008-02-04 22:27:53 -0800 | [diff] [blame] | 386 | baud = tty_termios_baud_rate(old); |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 387 | if (!hung_up) |
| 388 | tty_termios_encode_baud_rate(termios, |
| 389 | baud, baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | old = NULL; |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | /* |
Alan Cox | 16ae2a8 | 2010-01-04 16:26:21 +0000 | [diff] [blame] | 395 | * As a last resort, if the range cannot be met then clip to |
| 396 | * the nearest chip supported rate. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | */ |
Alan Cox | 16ae2a8 | 2010-01-04 16:26:21 +0000 | [diff] [blame] | 398 | if (!hung_up) { |
| 399 | if (baud <= min) |
| 400 | tty_termios_encode_baud_rate(termios, |
| 401 | min + 1, min + 1); |
| 402 | else |
| 403 | tty_termios_encode_baud_rate(termios, |
| 404 | max - 1, max - 1); |
| 405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
Alan Cox | 16ae2a8 | 2010-01-04 16:26:21 +0000 | [diff] [blame] | 407 | /* Should never happen */ |
| 408 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | EXPORT_SYMBOL(uart_get_baud_rate); |
| 413 | |
| 414 | /** |
| 415 | * uart_get_divisor - return uart clock divisor |
| 416 | * @port: uart_port structure describing the port. |
| 417 | * @baud: desired baud rate |
| 418 | * |
| 419 | * Calculate the uart clock divisor for the port. |
| 420 | */ |
| 421 | unsigned int |
| 422 | uart_get_divisor(struct uart_port *port, unsigned int baud) |
| 423 | { |
| 424 | unsigned int quot; |
| 425 | |
| 426 | /* |
| 427 | * Old custom speed handling. |
| 428 | */ |
| 429 | if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) |
| 430 | quot = port->custom_divisor; |
| 431 | else |
Uwe Kleine-König | 97d2463 | 2011-12-20 11:47:44 +0100 | [diff] [blame] | 432 | quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | return quot; |
| 435 | } |
| 436 | |
| 437 | EXPORT_SYMBOL(uart_get_divisor); |
| 438 | |
Peter Hurley | 7c8ab96 | 2014-10-16 16:54:20 -0400 | [diff] [blame^] | 439 | /* Caller holds port mutex */ |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 440 | static void uart_change_speed(struct tty_struct *tty, struct uart_state *state, |
| 441 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 443 | struct uart_port *uport = state->uart_port; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 444 | struct ktermios *termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | /* |
| 447 | * If we have no tty, termios, or the port does not exist, |
| 448 | * then we can't set the parameters for this port. |
| 449 | */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 450 | if (!tty || uport->type == PORT_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | return; |
| 452 | |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 453 | termios = &tty->termios; |
Peter Hurley | c18b55f | 2014-06-16 09:17:09 -0400 | [diff] [blame] | 454 | uport->ops->set_termios(uport, termios, old_termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | /* |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 457 | * Set modem status enables based on termios cflag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | */ |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 459 | spin_lock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | if (termios->c_cflag & CRTSCTS) |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 461 | uport->status |= UPSTAT_CTS_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | else |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 463 | uport->status &= ~UPSTAT_CTS_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | if (termios->c_cflag & CLOCAL) |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 466 | uport->status &= ~UPSTAT_DCD_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | else |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 468 | uport->status |= UPSTAT_DCD_ENABLE; |
| 469 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 472 | static inline int __uart_put_char(struct uart_port *port, |
| 473 | struct circ_buf *circ, unsigned char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
| 475 | unsigned long flags; |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 476 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | if (!circ->buf) |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 479 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | spin_lock_irqsave(&port->lock, flags); |
| 482 | if (uart_circ_chars_free(circ) != 0) { |
| 483 | circ->buf[circ->head] = c; |
| 484 | circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1); |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 485 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 488 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 491 | static int uart_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | struct uart_state *state = tty->driver_data; |
| 494 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 495 | return __uart_put_char(state->uart_port, &state->xmit, ch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static void uart_flush_chars(struct tty_struct *tty) |
| 499 | { |
| 500 | uart_start(tty); |
| 501 | } |
| 502 | |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 503 | static int uart_write(struct tty_struct *tty, |
| 504 | const unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
| 506 | struct uart_state *state = tty->driver_data; |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 507 | struct uart_port *port; |
| 508 | struct circ_buf *circ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | unsigned long flags; |
| 510 | int c, ret = 0; |
| 511 | |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 512 | /* |
| 513 | * This means you called this function _after_ the port was |
| 514 | * closed. No cookie for you. |
| 515 | */ |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 516 | if (!state) { |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 517 | WARN_ON(1); |
| 518 | return -EL3HLT; |
| 519 | } |
| 520 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 521 | port = state->uart_port; |
| 522 | circ = &state->xmit; |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (!circ->buf) |
| 525 | return 0; |
| 526 | |
| 527 | spin_lock_irqsave(&port->lock, flags); |
| 528 | while (1) { |
| 529 | c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE); |
| 530 | if (count < c) |
| 531 | c = count; |
| 532 | if (c <= 0) |
| 533 | break; |
| 534 | memcpy(circ->buf + circ->head, buf, c); |
| 535 | circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1); |
| 536 | buf += c; |
| 537 | count -= c; |
| 538 | ret += c; |
| 539 | } |
| 540 | spin_unlock_irqrestore(&port->lock, flags); |
| 541 | |
| 542 | uart_start(tty); |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | static int uart_write_room(struct tty_struct *tty) |
| 547 | { |
| 548 | struct uart_state *state = tty->driver_data; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 549 | unsigned long flags; |
| 550 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 552 | spin_lock_irqsave(&state->uart_port->lock, flags); |
| 553 | ret = uart_circ_chars_free(&state->xmit); |
| 554 | spin_unlock_irqrestore(&state->uart_port->lock, flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 555 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static int uart_chars_in_buffer(struct tty_struct *tty) |
| 559 | { |
| 560 | struct uart_state *state = tty->driver_data; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 561 | unsigned long flags; |
| 562 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 564 | spin_lock_irqsave(&state->uart_port->lock, flags); |
| 565 | ret = uart_circ_chars_pending(&state->xmit); |
| 566 | spin_unlock_irqrestore(&state->uart_port->lock, flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 567 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static void uart_flush_buffer(struct tty_struct *tty) |
| 571 | { |
| 572 | struct uart_state *state = tty->driver_data; |
Tetsuo Handa | 55d7b68 | 2008-05-06 20:42:27 -0700 | [diff] [blame] | 573 | struct uart_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | unsigned long flags; |
| 575 | |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 576 | /* |
| 577 | * This means you called this function _after_ the port was |
| 578 | * closed. No cookie for you. |
| 579 | */ |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 580 | if (!state) { |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 581 | WARN_ON(1); |
| 582 | return; |
| 583 | } |
| 584 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 585 | port = state->uart_port; |
Jiri Slaby | eb3a1e1 | 2007-05-06 14:48:52 -0700 | [diff] [blame] | 586 | pr_debug("uart_flush_buffer(%d) called\n", tty->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 589 | uart_circ_clear(&state->xmit); |
Haavard Skinnemoen | 6bb0e3a | 2008-07-16 21:52:36 +0100 | [diff] [blame] | 590 | if (port->ops->flush_buffer) |
| 591 | port->ops->flush_buffer(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | spin_unlock_irqrestore(&port->lock, flags); |
| 593 | tty_wakeup(tty); |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * This function is used to send a high-priority XON/XOFF character to |
| 598 | * the device |
| 599 | */ |
| 600 | static void uart_send_xchar(struct tty_struct *tty, char ch) |
| 601 | { |
| 602 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 603 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | unsigned long flags; |
| 605 | |
| 606 | if (port->ops->send_xchar) |
| 607 | port->ops->send_xchar(port, ch); |
| 608 | else { |
Peter Hurley | c235ccc | 2014-09-02 17:39:13 -0400 | [diff] [blame] | 609 | spin_lock_irqsave(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | port->x_char = ch; |
Peter Hurley | c235ccc | 2014-09-02 17:39:13 -0400 | [diff] [blame] | 611 | if (ch) |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 612 | port->ops->start_tx(port); |
Peter Hurley | c235ccc | 2014-09-02 17:39:13 -0400 | [diff] [blame] | 613 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
| 617 | static void uart_throttle(struct tty_struct *tty) |
| 618 | { |
| 619 | struct uart_state *state = tty->driver_data; |
Russell King | 9aba8d5b | 2012-04-17 17:23:14 +0100 | [diff] [blame] | 620 | struct uart_port *port = state->uart_port; |
Peter Hurley | 91f189de | 2014-10-16 14:19:48 -0400 | [diff] [blame] | 621 | upf_t mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | if (I_IXOFF(tty)) |
Russell King | 9aba8d5b | 2012-04-17 17:23:14 +0100 | [diff] [blame] | 624 | mask |= UPF_SOFT_FLOW; |
| 625 | if (tty->termios.c_cflag & CRTSCTS) |
| 626 | mask |= UPF_HARD_FLOW; |
| 627 | |
| 628 | if (port->flags & mask) { |
| 629 | port->ops->throttle(port); |
| 630 | mask &= ~port->flags; |
| 631 | } |
| 632 | |
| 633 | if (mask & UPF_SOFT_FLOW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | uart_send_xchar(tty, STOP_CHAR(tty)); |
| 635 | |
Russell King | 9aba8d5b | 2012-04-17 17:23:14 +0100 | [diff] [blame] | 636 | if (mask & UPF_HARD_FLOW) |
| 637 | uart_clear_mctrl(port, TIOCM_RTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static void uart_unthrottle(struct tty_struct *tty) |
| 641 | { |
| 642 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 643 | struct uart_port *port = state->uart_port; |
Peter Hurley | 91f189de | 2014-10-16 14:19:48 -0400 | [diff] [blame] | 644 | upf_t mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Russell King | 9aba8d5b | 2012-04-17 17:23:14 +0100 | [diff] [blame] | 646 | if (I_IXOFF(tty)) |
| 647 | mask |= UPF_SOFT_FLOW; |
| 648 | if (tty->termios.c_cflag & CRTSCTS) |
| 649 | mask |= UPF_HARD_FLOW; |
| 650 | |
| 651 | if (port->flags & mask) { |
| 652 | port->ops->unthrottle(port); |
| 653 | mask &= ~port->flags; |
| 654 | } |
| 655 | |
Peter Hurley | fba594a | 2014-09-02 17:39:14 -0400 | [diff] [blame] | 656 | if (mask & UPF_SOFT_FLOW) |
| 657 | uart_send_xchar(tty, START_CHAR(tty)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Russell King | 9aba8d5b | 2012-04-17 17:23:14 +0100 | [diff] [blame] | 659 | if (mask & UPF_HARD_FLOW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | uart_set_mctrl(port, TIOCM_RTS); |
| 661 | } |
| 662 | |
Alan Cox | 9f10969 | 2012-10-29 15:20:25 +0000 | [diff] [blame] | 663 | static void do_uart_get_info(struct tty_port *port, |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 664 | struct serial_struct *retinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
Alan Cox | 9f10969 | 2012-10-29 15:20:25 +0000 | [diff] [blame] | 666 | struct uart_state *state = container_of(port, struct uart_state, port); |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 667 | struct uart_port *uport = state->uart_port; |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 668 | |
Fengguang Wu | 37cd0c9 | 2012-09-06 10:27:51 +0800 | [diff] [blame] | 669 | memset(retinfo, 0, sizeof(*retinfo)); |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 670 | |
| 671 | retinfo->type = uport->type; |
| 672 | retinfo->line = uport->line; |
| 673 | retinfo->port = uport->iobase; |
| 674 | if (HIGH_BITS_OFFSET) |
| 675 | retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET; |
| 676 | retinfo->irq = uport->irq; |
| 677 | retinfo->flags = uport->flags; |
| 678 | retinfo->xmit_fifo_size = uport->fifosize; |
| 679 | retinfo->baud_base = uport->uartclk / 16; |
| 680 | retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10; |
| 681 | retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ? |
| 682 | ASYNC_CLOSING_WAIT_NONE : |
| 683 | jiffies_to_msecs(port->closing_wait) / 10; |
| 684 | retinfo->custom_divisor = uport->custom_divisor; |
| 685 | retinfo->hub6 = uport->hub6; |
| 686 | retinfo->io_type = uport->iotype; |
| 687 | retinfo->iomem_reg_shift = uport->regshift; |
| 688 | retinfo->iomem_base = (void *)(unsigned long)uport->mapbase; |
| 689 | } |
| 690 | |
Alan Cox | 9f10969 | 2012-10-29 15:20:25 +0000 | [diff] [blame] | 691 | static void uart_get_info(struct tty_port *port, |
| 692 | struct serial_struct *retinfo) |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 693 | { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 694 | /* Ensure the state we copy is consistent and no hardware changes |
| 695 | occur as we go */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 696 | mutex_lock(&port->mutex); |
Alan Cox | 9f10969 | 2012-10-29 15:20:25 +0000 | [diff] [blame] | 697 | do_uart_get_info(port, retinfo); |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 698 | mutex_unlock(&port->mutex); |
Alan Cox | 9f10969 | 2012-10-29 15:20:25 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static int uart_get_info_user(struct tty_port *port, |
| 702 | struct serial_struct __user *retinfo) |
| 703 | { |
| 704 | struct serial_struct tmp; |
| 705 | uart_get_info(port, &tmp); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 708 | return -EFAULT; |
| 709 | return 0; |
| 710 | } |
| 711 | |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 712 | static int uart_set_info(struct tty_struct *tty, struct tty_port *port, |
| 713 | struct uart_state *state, |
| 714 | struct serial_struct *new_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 716 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | unsigned long new_port; |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 718 | unsigned int change_irq, change_port, closing_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | unsigned int old_custom_divisor, close_delay; |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 720 | upf_t old_flags, new_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | int retval = 0; |
| 722 | |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 723 | new_port = new_info->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | if (HIGH_BITS_OFFSET) |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 725 | new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 727 | new_info->irq = irq_canonicalize(new_info->irq); |
| 728 | close_delay = msecs_to_jiffies(new_info->close_delay * 10); |
| 729 | closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ? |
Jiri Slaby | 4cb0fbf | 2011-11-09 21:33:45 +0100 | [diff] [blame] | 730 | ASYNC_CLOSING_WAIT_NONE : |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 731 | msecs_to_jiffies(new_info->closing_wait * 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 734 | change_irq = !(uport->flags & UPF_FIXED_PORT) |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 735 | && new_info->irq != uport->irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Since changing the 'type' of the port changes its resource |
| 739 | * allocations, we should treat type changes the same as |
| 740 | * IO port changes. |
| 741 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 742 | change_port = !(uport->flags & UPF_FIXED_PORT) |
| 743 | && (new_port != uport->iobase || |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 744 | (unsigned long)new_info->iomem_base != uport->mapbase || |
| 745 | new_info->hub6 != uport->hub6 || |
| 746 | new_info->io_type != uport->iotype || |
| 747 | new_info->iomem_reg_shift != uport->regshift || |
| 748 | new_info->type != uport->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 750 | old_flags = uport->flags; |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 751 | new_flags = new_info->flags; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 752 | old_custom_divisor = uport->custom_divisor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | if (!capable(CAP_SYS_ADMIN)) { |
| 755 | retval = -EPERM; |
| 756 | if (change_irq || change_port || |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 757 | (new_info->baud_base != uport->uartclk / 16) || |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 758 | (close_delay != port->close_delay) || |
| 759 | (closing_wait != port->closing_wait) || |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 760 | (new_info->xmit_fifo_size && |
| 761 | new_info->xmit_fifo_size != uport->fifosize) || |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 762 | (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | goto exit; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 764 | uport->flags = ((uport->flags & ~UPF_USR_MASK) | |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 765 | (new_flags & UPF_USR_MASK)); |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 766 | uport->custom_divisor = new_info->custom_divisor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | goto check_and_exit; |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Ask the low level driver to verify the settings. |
| 772 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 773 | if (uport->ops->verify_port) |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 774 | retval = uport->ops->verify_port(uport, new_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 776 | if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) || |
| 777 | (new_info->baud_base < 9600)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | retval = -EINVAL; |
| 779 | |
| 780 | if (retval) |
| 781 | goto exit; |
| 782 | |
| 783 | if (change_port || change_irq) { |
| 784 | retval = -EBUSY; |
| 785 | |
| 786 | /* |
| 787 | * Make sure that we are the sole user of this port. |
| 788 | */ |
Alan Cox | b58d13a | 2009-09-19 13:13:32 -0700 | [diff] [blame] | 789 | if (tty_port_users(port) > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | goto exit; |
| 791 | |
| 792 | /* |
| 793 | * We need to shutdown the serial port at the old |
| 794 | * port/type/irq combination. |
| 795 | */ |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 796 | uart_shutdown(tty, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | if (change_port) { |
| 800 | unsigned long old_iobase, old_mapbase; |
| 801 | unsigned int old_type, old_iotype, old_hub6, old_shift; |
| 802 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 803 | old_iobase = uport->iobase; |
| 804 | old_mapbase = uport->mapbase; |
| 805 | old_type = uport->type; |
| 806 | old_hub6 = uport->hub6; |
| 807 | old_iotype = uport->iotype; |
| 808 | old_shift = uport->regshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
| 810 | /* |
| 811 | * Free and release old regions |
| 812 | */ |
| 813 | if (old_type != PORT_UNKNOWN) |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 814 | uport->ops->release_port(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 816 | uport->iobase = new_port; |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 817 | uport->type = new_info->type; |
| 818 | uport->hub6 = new_info->hub6; |
| 819 | uport->iotype = new_info->io_type; |
| 820 | uport->regshift = new_info->iomem_reg_shift; |
| 821 | uport->mapbase = (unsigned long)new_info->iomem_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | /* |
| 824 | * Claim and map the new regions |
| 825 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 826 | if (uport->type != PORT_UNKNOWN) { |
| 827 | retval = uport->ops->request_port(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } else { |
| 829 | /* Always success - Jean II */ |
| 830 | retval = 0; |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * If we fail to request resources for the |
| 835 | * new port, try to restore the old settings. |
| 836 | */ |
Thomas Pfaff | 7deb39e | 2014-04-23 12:33:22 +0200 | [diff] [blame] | 837 | if (retval) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 838 | uport->iobase = old_iobase; |
| 839 | uport->type = old_type; |
| 840 | uport->hub6 = old_hub6; |
| 841 | uport->iotype = old_iotype; |
| 842 | uport->regshift = old_shift; |
| 843 | uport->mapbase = old_mapbase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
Thomas Pfaff | 7deb39e | 2014-04-23 12:33:22 +0200 | [diff] [blame] | 845 | if (old_type != PORT_UNKNOWN) { |
| 846 | retval = uport->ops->request_port(uport); |
| 847 | /* |
| 848 | * If we failed to restore the old settings, |
| 849 | * we fail like this. |
| 850 | */ |
| 851 | if (retval) |
| 852 | uport->type = PORT_UNKNOWN; |
| 853 | |
| 854 | /* |
| 855 | * We failed anyway. |
| 856 | */ |
| 857 | retval = -EBUSY; |
| 858 | } |
| 859 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 860 | /* Added to return the correct error -Ram Gupta */ |
| 861 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
David Gibson | abb4a23 | 2007-05-06 14:48:49 -0700 | [diff] [blame] | 865 | if (change_irq) |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 866 | uport->irq = new_info->irq; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 867 | if (!(uport->flags & UPF_FIXED_PORT)) |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 868 | uport->uartclk = new_info->baud_base * 16; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 869 | uport->flags = (uport->flags & ~UPF_CHANGE_MASK) | |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 870 | (new_flags & UPF_CHANGE_MASK); |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 871 | uport->custom_divisor = new_info->custom_divisor; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 872 | port->close_delay = close_delay; |
| 873 | port->closing_wait = closing_wait; |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 874 | if (new_info->xmit_fifo_size) |
| 875 | uport->fifosize = new_info->xmit_fifo_size; |
Jiri Slaby | d6c53c0 | 2013-01-03 15:53:05 +0100 | [diff] [blame] | 876 | port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
| 878 | check_and_exit: |
| 879 | retval = 0; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 880 | if (uport->type == PORT_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | goto exit; |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 882 | if (port->flags & ASYNC_INITIALIZED) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 883 | if (((old_flags ^ uport->flags) & UPF_SPD_MASK) || |
| 884 | old_custom_divisor != uport->custom_divisor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | /* |
| 886 | * If they're setting up a custom divisor or speed, |
| 887 | * instead of clearing it, then bitch about it. No |
| 888 | * need to rate-limit; it's CAP_SYS_ADMIN only. |
| 889 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 890 | if (uport->flags & UPF_SPD_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | char buf[64]; |
Sudip Mukherjee | 2f2dafe | 2014-09-01 20:49:43 +0530 | [diff] [blame] | 892 | |
| 893 | dev_notice(uport->dev, |
| 894 | "%s sets custom speed on %s. This is deprecated.\n", |
| 895 | current->comm, |
| 896 | tty_name(port->tty, buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 898 | uart_change_speed(tty, state, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
| 900 | } else |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 901 | retval = uart_startup(tty, state, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | exit: |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 903 | return retval; |
| 904 | } |
| 905 | |
| 906 | static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state, |
| 907 | struct serial_struct __user *newinfo) |
| 908 | { |
| 909 | struct serial_struct new_serial; |
| 910 | struct tty_port *port = &state->port; |
| 911 | int retval; |
| 912 | |
| 913 | if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) |
| 914 | return -EFAULT; |
| 915 | |
| 916 | /* |
| 917 | * This semaphore protects port->count. It is also |
| 918 | * very useful to prevent opens. Also, take the |
| 919 | * port configuration semaphore to make sure that a |
| 920 | * module insertion/removal doesn't change anything |
| 921 | * under us. |
| 922 | */ |
| 923 | mutex_lock(&port->mutex); |
| 924 | retval = uart_set_info(tty, port, state, &new_serial); |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 925 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return retval; |
| 927 | } |
| 928 | |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 929 | /** |
| 930 | * uart_get_lsr_info - get line status register info |
| 931 | * @tty: tty associated with the UART |
| 932 | * @state: UART being queried |
| 933 | * @value: returned modem value |
| 934 | * |
| 935 | * Note: uart_ioctl protects us against hangups. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | */ |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 937 | static int uart_get_lsr_info(struct tty_struct *tty, |
| 938 | struct uart_state *state, unsigned int __user *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 940 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | unsigned int result; |
| 942 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 943 | result = uport->ops->tx_empty(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
| 945 | /* |
| 946 | * If we're about to load something into the transmit |
| 947 | * register, we'll pretend the transmitter isn't empty to |
| 948 | * avoid a race condition (depending on when the transmit |
| 949 | * interrupt happens). |
| 950 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 951 | if (uport->x_char || |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 952 | ((uart_circ_chars_pending(&state->xmit) > 0) && |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 953 | !uart_tx_stopped(uport))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | result &= ~TIOCSER_TEMT; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | return put_user(result, value); |
| 957 | } |
| 958 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 959 | static int uart_tiocmget(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | { |
| 961 | struct uart_state *state = tty->driver_data; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 962 | struct tty_port *port = &state->port; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 963 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | int result = -EIO; |
| 965 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 966 | mutex_lock(&port->mutex); |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 967 | if (!(tty->flags & (1 << TTY_IO_ERROR))) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 968 | result = uport->mctrl; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 969 | spin_lock_irq(&uport->lock); |
| 970 | result |= uport->ops->get_mctrl(uport); |
| 971 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 973 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | |
| 975 | return result; |
| 976 | } |
| 977 | |
| 978 | static int |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 979 | uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | { |
| 981 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 982 | struct uart_port *uport = state->uart_port; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 983 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | int ret = -EIO; |
| 985 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 986 | mutex_lock(&port->mutex); |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 987 | if (!(tty->flags & (1 << TTY_IO_ERROR))) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 988 | uart_update_mctrl(uport, set, clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | ret = 0; |
| 990 | } |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 991 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | return ret; |
| 993 | } |
| 994 | |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 995 | static int uart_break_ctl(struct tty_struct *tty, int break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | { |
| 997 | struct uart_state *state = tty->driver_data; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 998 | struct tty_port *port = &state->port; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 999 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1001 | mutex_lock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1003 | if (uport->type != PORT_UNKNOWN) |
| 1004 | uport->ops->break_ctl(uport, break_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1006 | mutex_unlock(&port->mutex); |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 1007 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1010 | static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1012 | struct uart_port *uport = state->uart_port; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1013 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | int flags, ret; |
| 1015 | |
| 1016 | if (!capable(CAP_SYS_ADMIN)) |
| 1017 | return -EPERM; |
| 1018 | |
| 1019 | /* |
| 1020 | * Take the per-port semaphore. This prevents count from |
| 1021 | * changing, and hence any extra opens of the port while |
| 1022 | * we're auto-configuring. |
| 1023 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1024 | if (mutex_lock_interruptible(&port->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | return -ERESTARTSYS; |
| 1026 | |
| 1027 | ret = -EBUSY; |
Alan Cox | b58d13a | 2009-09-19 13:13:32 -0700 | [diff] [blame] | 1028 | if (tty_port_users(port) == 1) { |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1029 | uart_shutdown(tty, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
| 1031 | /* |
| 1032 | * If we already have a port type configured, |
| 1033 | * we must release its resources. |
| 1034 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1035 | if (uport->type != PORT_UNKNOWN) |
| 1036 | uport->ops->release_port(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
| 1038 | flags = UART_CONFIG_TYPE; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1039 | if (uport->flags & UPF_AUTO_IRQ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | flags |= UART_CONFIG_IRQ; |
| 1041 | |
| 1042 | /* |
| 1043 | * This will claim the ports resources if |
| 1044 | * a port is found. |
| 1045 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1046 | uport->ops->config_port(uport, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1048 | ret = uart_startup(tty, state, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1050 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | return ret; |
| 1052 | } |
| 1053 | |
Alexander Shiyan | 1fdc310 | 2014-06-03 18:54:43 +0400 | [diff] [blame] | 1054 | static void uart_enable_ms(struct uart_port *uport) |
| 1055 | { |
| 1056 | /* |
| 1057 | * Force modem status interrupts on |
| 1058 | */ |
| 1059 | if (uport->ops->enable_ms) |
| 1060 | uport->ops->enable_ms(uport); |
| 1061 | } |
| 1062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | /* |
| 1064 | * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change |
| 1065 | * - mask passed in arg for lines of interest |
| 1066 | * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) |
| 1067 | * Caller should use TIOCGICOUNT to see which one it was |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1068 | * |
| 1069 | * FIXME: This wants extracting into a common all driver implementation |
| 1070 | * of TIOCMWAIT using tty_port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | */ |
| 1072 | static int |
| 1073 | uart_wait_modem_status(struct uart_state *state, unsigned long arg) |
| 1074 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1075 | struct uart_port *uport = state->uart_port; |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1076 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | DECLARE_WAITQUEUE(wait, current); |
| 1078 | struct uart_icount cprev, cnow; |
| 1079 | int ret; |
| 1080 | |
| 1081 | /* |
| 1082 | * note the counters on entry |
| 1083 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1084 | spin_lock_irq(&uport->lock); |
| 1085 | memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); |
Alexander Shiyan | 1fdc310 | 2014-06-03 18:54:43 +0400 | [diff] [blame] | 1086 | uart_enable_ms(uport); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1087 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1089 | add_wait_queue(&port->delta_msr_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | for (;;) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1091 | spin_lock_irq(&uport->lock); |
| 1092 | memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); |
| 1093 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
| 1095 | set_current_state(TASK_INTERRUPTIBLE); |
| 1096 | |
| 1097 | if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || |
| 1098 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || |
| 1099 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || |
| 1100 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1101 | ret = 0; |
| 1102 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | schedule(); |
| 1106 | |
| 1107 | /* see if a signal did it */ |
| 1108 | if (signal_pending(current)) { |
| 1109 | ret = -ERESTARTSYS; |
| 1110 | break; |
| 1111 | } |
| 1112 | |
| 1113 | cprev = cnow; |
| 1114 | } |
| 1115 | |
| 1116 | current->state = TASK_RUNNING; |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1117 | remove_wait_queue(&port->delta_msr_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) |
| 1124 | * Return: write counters to the user passed counter struct |
| 1125 | * NB: both 1->0 and 0->1 transitions are counted except for |
| 1126 | * RI where only 0->1 is counted. |
| 1127 | */ |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 1128 | static int uart_get_icount(struct tty_struct *tty, |
| 1129 | struct serial_icounter_struct *icount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 1131 | struct uart_state *state = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | struct uart_icount cnow; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1133 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1135 | spin_lock_irq(&uport->lock); |
| 1136 | memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); |
| 1137 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 1139 | icount->cts = cnow.cts; |
| 1140 | icount->dsr = cnow.dsr; |
| 1141 | icount->rng = cnow.rng; |
| 1142 | icount->dcd = cnow.dcd; |
| 1143 | icount->rx = cnow.rx; |
| 1144 | icount->tx = cnow.tx; |
| 1145 | icount->frame = cnow.frame; |
| 1146 | icount->overrun = cnow.overrun; |
| 1147 | icount->parity = cnow.parity; |
| 1148 | icount->brk = cnow.brk; |
| 1149 | icount->buf_overrun = cnow.buf_overrun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 1151 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /* |
Alan Cox | e523844 | 2008-04-30 00:53:28 -0700 | [diff] [blame] | 1155 | * Called via sys_ioctl. We can use spin_lock_irq() here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | */ |
| 1157 | static int |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 1158 | uart_ioctl(struct tty_struct *tty, unsigned int cmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | unsigned long arg) |
| 1160 | { |
| 1161 | struct uart_state *state = tty->driver_data; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1162 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | void __user *uarg = (void __user *)arg; |
| 1164 | int ret = -ENOIOCTLCMD; |
| 1165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | /* |
| 1168 | * These ioctls don't rely on the hardware to be present. |
| 1169 | */ |
| 1170 | switch (cmd) { |
| 1171 | case TIOCGSERIAL: |
Alan Cox | 9f10969 | 2012-10-29 15:20:25 +0000 | [diff] [blame] | 1172 | ret = uart_get_info_user(port, uarg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | break; |
| 1174 | |
| 1175 | case TIOCSSERIAL: |
Peter Hurley | 7c8ab96 | 2014-10-16 16:54:20 -0400 | [diff] [blame^] | 1176 | down_write(&tty->termios_rwsem); |
Alan Cox | 7ba2e76 | 2012-09-04 16:34:45 +0100 | [diff] [blame] | 1177 | ret = uart_set_info_user(tty, state, uarg); |
Peter Hurley | 7c8ab96 | 2014-10-16 16:54:20 -0400 | [diff] [blame^] | 1178 | up_write(&tty->termios_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | break; |
| 1180 | |
| 1181 | case TIOCSERCONFIG: |
Peter Hurley | 7c8ab96 | 2014-10-16 16:54:20 -0400 | [diff] [blame^] | 1182 | down_write(&tty->termios_rwsem); |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1183 | ret = uart_do_autoconfig(tty, state); |
Peter Hurley | 7c8ab96 | 2014-10-16 16:54:20 -0400 | [diff] [blame^] | 1184 | up_write(&tty->termios_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | break; |
| 1186 | |
| 1187 | case TIOCSERGWILD: /* obsolete */ |
| 1188 | case TIOCSERSWILD: /* obsolete */ |
| 1189 | ret = 0; |
| 1190 | break; |
| 1191 | } |
| 1192 | |
| 1193 | if (ret != -ENOIOCTLCMD) |
| 1194 | goto out; |
| 1195 | |
| 1196 | if (tty->flags & (1 << TTY_IO_ERROR)) { |
| 1197 | ret = -EIO; |
| 1198 | goto out; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
| 1202 | * The following should only be used when hardware is present. |
| 1203 | */ |
| 1204 | switch (cmd) { |
| 1205 | case TIOCMIWAIT: |
| 1206 | ret = uart_wait_modem_status(state, arg); |
| 1207 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | if (ret != -ENOIOCTLCMD) |
| 1211 | goto out; |
| 1212 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1213 | mutex_lock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 1215 | if (tty->flags & (1 << TTY_IO_ERROR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | ret = -EIO; |
| 1217 | goto out_up; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * All these rely on hardware being present and need to be |
| 1222 | * protected against the tty being hung up. |
| 1223 | */ |
| 1224 | switch (cmd) { |
| 1225 | case TIOCSERGETLSR: /* Get line status register */ |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1226 | ret = uart_get_lsr_info(tty, state, uarg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | break; |
| 1228 | |
| 1229 | default: { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1230 | struct uart_port *uport = state->uart_port; |
| 1231 | if (uport->ops->ioctl) |
| 1232 | ret = uport->ops->ioctl(uport, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | break; |
| 1234 | } |
| 1235 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1236 | out_up: |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1237 | mutex_unlock(&port->mutex); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1238 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | return ret; |
| 1240 | } |
| 1241 | |
Linus Torvalds | edeb280 | 2008-06-04 10:35:03 -0700 | [diff] [blame] | 1242 | static void uart_set_ldisc(struct tty_struct *tty) |
Alan Cox | 64e9159 | 2008-06-03 15:18:54 +0100 | [diff] [blame] | 1243 | { |
| 1244 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1245 | struct uart_port *uport = state->uart_port; |
Alan Cox | 64e9159 | 2008-06-03 15:18:54 +0100 | [diff] [blame] | 1246 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1247 | if (uport->ops->set_ldisc) |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1248 | uport->ops->set_ldisc(uport, tty->termios.c_line); |
Alan Cox | 64e9159 | 2008-06-03 15:18:54 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1251 | static void uart_set_termios(struct tty_struct *tty, |
| 1252 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | { |
| 1254 | struct uart_state *state = tty->driver_data; |
Russell King | dec94e7 | 2012-09-24 11:13:15 +0100 | [diff] [blame] | 1255 | struct uart_port *uport = state->uart_port; |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1256 | unsigned int cflag = tty->termios.c_cflag; |
Russell King | 2cbacaf | 2012-04-17 16:34:13 +0100 | [diff] [blame] | 1257 | unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK; |
| 1258 | bool sw_changed = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
Russell King | 2cbacaf | 2012-04-17 16:34:13 +0100 | [diff] [blame] | 1260 | /* |
| 1261 | * Drivers doing software flow control also need to know |
| 1262 | * about changes to these input settings. |
| 1263 | */ |
| 1264 | if (uport->flags & UPF_SOFT_FLOW) { |
| 1265 | iflag_mask |= IXANY|IXON|IXOFF; |
| 1266 | sw_changed = |
| 1267 | tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] || |
| 1268 | tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP]; |
| 1269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
| 1271 | /* |
| 1272 | * These are the bits that are used to setup various |
David Woodhouse | 20620d6 | 2007-08-22 14:01:11 -0700 | [diff] [blame] | 1273 | * flags in the low level driver. We can ignore the Bfoo |
| 1274 | * bits in c_cflag; c_[io]speed will always be set |
| 1275 | * appropriately by set_termios() in tty_ioctl.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | if ((cflag ^ old_termios->c_cflag) == 0 && |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 1278 | tty->termios.c_ospeed == old_termios->c_ospeed && |
| 1279 | tty->termios.c_ispeed == old_termios->c_ispeed && |
Russell King | 2cbacaf | 2012-04-17 16:34:13 +0100 | [diff] [blame] | 1280 | ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 && |
| 1281 | !sw_changed) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | return; |
Alan Cox | e523844 | 2008-04-30 00:53:28 -0700 | [diff] [blame] | 1283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Peter Hurley | 7c8ab96 | 2014-10-16 16:54:20 -0400 | [diff] [blame^] | 1285 | mutex_lock(&state->port.mutex); |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1286 | uart_change_speed(tty, state, old_termios); |
Peter Hurley | 7c8ab96 | 2014-10-16 16:54:20 -0400 | [diff] [blame^] | 1287 | mutex_unlock(&state->port.mutex); |
Peter Hurley | c18b55f | 2014-06-16 09:17:09 -0400 | [diff] [blame] | 1288 | /* reload cflag from termios; port driver may have overriden flags */ |
| 1289 | cflag = tty->termios.c_cflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | |
| 1291 | /* Handle transition to B0 status */ |
| 1292 | if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) |
Russell King | dec94e7 | 2012-09-24 11:13:15 +0100 | [diff] [blame] | 1293 | uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | /* Handle transition away from B0 status */ |
André Goddard Rosa | 82cb7ba | 2009-10-25 11:18:26 -0200 | [diff] [blame] | 1295 | else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | unsigned int mask = TIOCM_DTR; |
Peter Hurley | 99abf3b | 2014-09-02 17:39:11 -0400 | [diff] [blame] | 1297 | if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | mask |= TIOCM_RTS; |
Russell King | dec94e7 | 2012-09-24 11:13:15 +0100 | [diff] [blame] | 1299 | uart_set_mctrl(uport, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Russell King | dba0583 | 2012-04-17 16:41:10 +0100 | [diff] [blame] | 1302 | /* |
| 1303 | * If the port is doing h/w assisted flow control, do nothing. |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 1304 | * We assume that port->hw_stopped has never been set. |
Russell King | dba0583 | 2012-04-17 16:41:10 +0100 | [diff] [blame] | 1305 | */ |
| 1306 | if (uport->flags & UPF_HARD_FLOW) |
| 1307 | return; |
| 1308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | /* Handle turning off CRTSCTS */ |
| 1310 | if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) { |
Peter Hurley | 938f7e1 | 2014-09-10 15:06:29 -0400 | [diff] [blame] | 1311 | spin_lock_irq(&uport->lock); |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 1312 | uport->hw_stopped = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | __uart_start(tty); |
Peter Hurley | 938f7e1 | 2014-09-10 15:06:29 -0400 | [diff] [blame] | 1314 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | } |
Russell King | 0dd7a1a | 2005-06-29 18:40:53 +0100 | [diff] [blame] | 1316 | /* Handle turning on CRTSCTS */ |
André Goddard Rosa | 82cb7ba | 2009-10-25 11:18:26 -0200 | [diff] [blame] | 1317 | else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) { |
Peter Hurley | 938f7e1 | 2014-09-10 15:06:29 -0400 | [diff] [blame] | 1318 | spin_lock_irq(&uport->lock); |
Russell King | dec94e7 | 2012-09-24 11:13:15 +0100 | [diff] [blame] | 1319 | if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS)) { |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 1320 | uport->hw_stopped = 1; |
Russell King | dec94e7 | 2012-09-24 11:13:15 +0100 | [diff] [blame] | 1321 | uport->ops->stop_tx(uport); |
Russell King | 0dd7a1a | 2005-06-29 18:40:53 +0100 | [diff] [blame] | 1322 | } |
Peter Hurley | 938f7e1 | 2014-09-10 15:06:29 -0400 | [diff] [blame] | 1323 | spin_unlock_irq(&uport->lock); |
Russell King | 0dd7a1a | 2005-06-29 18:40:53 +0100 | [diff] [blame] | 1324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /* |
Kevin Cernekee | ef4f527 | 2012-12-26 20:43:41 -0800 | [diff] [blame] | 1328 | * Calls to uart_close() are serialised via the tty_lock in |
| 1329 | * drivers/tty/tty_io.c:tty_release() |
| 1330 | * drivers/tty/tty_io.c:do_tty_hangup() |
| 1331 | * This runs from a workqueue and can sleep for a _short_ time only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | */ |
| 1333 | static void uart_close(struct tty_struct *tty, struct file *filp) |
| 1334 | { |
| 1335 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1336 | struct tty_port *port; |
| 1337 | struct uart_port *uport; |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1338 | unsigned long flags; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1339 | |
Linus Torvalds | eea7e17 | 2009-10-12 19:13:54 +0200 | [diff] [blame] | 1340 | if (!state) |
| 1341 | return; |
| 1342 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1343 | uport = state->uart_port; |
| 1344 | port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
Geert Uytterhoeven | 4ed94cd | 2014-03-17 14:10:59 +0100 | [diff] [blame] | 1346 | pr_debug("uart_close(%d) called\n", uport ? uport->line : -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
Geert Uytterhoeven | 4ed94cd | 2014-03-17 14:10:59 +0100 | [diff] [blame] | 1348 | if (!port->count || tty_port_close_start(port, tty, filp) == 0) |
Nobuhiro Iwamatsu | 5595621 | 2011-08-29 15:43:36 +0900 | [diff] [blame] | 1349 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | /* |
| 1352 | * At this point, we stop accepting input. To do this, we |
| 1353 | * disable the receive line status interrupts. |
| 1354 | */ |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1355 | if (port->flags & ASYNC_INITIALIZED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | unsigned long flags; |
Linus Torvalds | eea7e17 | 2009-10-12 19:13:54 +0200 | [diff] [blame] | 1357 | spin_lock_irqsave(&uport->lock, flags); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1358 | uport->ops->stop_rx(uport); |
Linus Torvalds | eea7e17 | 2009-10-12 19:13:54 +0200 | [diff] [blame] | 1359 | spin_unlock_irqrestore(&uport->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | /* |
| 1361 | * Before we drop DTR, make sure the UART transmitter |
| 1362 | * has completely drained; this is especially |
| 1363 | * important if there is a transmit FIFO! |
| 1364 | */ |
Jiri Slaby | 1f33a51 | 2011-07-14 14:35:10 +0200 | [diff] [blame] | 1365 | uart_wait_until_sent(tty, uport->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Jiri Slaby | bafb0bd | 2011-08-25 15:12:05 +0200 | [diff] [blame] | 1368 | mutex_lock(&port->mutex); |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1369 | uart_shutdown(tty, state); |
Alan Cox | 7b01478 | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 1370 | tty_port_tty_set(port, NULL); |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1371 | tty->closing = 0; |
Peter Hurley | ddc7b75 | 2014-06-16 09:17:03 -0400 | [diff] [blame] | 1372 | spin_lock_irqsave(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1374 | if (port->blocked_open) { |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1375 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1376 | if (port->close_delay) |
Jiri Slaby | 4cb0fbf | 2011-11-09 21:33:45 +0100 | [diff] [blame] | 1377 | msleep_interruptible( |
| 1378 | jiffies_to_msecs(port->close_delay)); |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1379 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1380 | } else if (!uart_console(uport)) { |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1381 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 1382 | uart_change_pm(state, UART_PM_STATE_OFF); |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1383 | spin_lock_irqsave(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /* |
| 1387 | * Wake up anyone trying to open this port. |
| 1388 | */ |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1389 | clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags); |
Jiri Slaby | 426929f | 2011-08-25 15:12:04 +0200 | [diff] [blame] | 1390 | clear_bit(ASYNCB_CLOSING, &port->flags); |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1391 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1392 | wake_up_interruptible(&port->open_wait); |
Jiri Slaby | 426929f | 2011-08-25 15:12:04 +0200 | [diff] [blame] | 1393 | wake_up_interruptible(&port->close_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1395 | mutex_unlock(&port->mutex); |
Peter Hurley | 2e75891 | 2014-10-16 16:54:19 -0400 | [diff] [blame] | 1396 | |
| 1397 | tty_ldisc_flush(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Jiri Slaby | 1f33a51 | 2011-07-14 14:35:10 +0200 | [diff] [blame] | 1400 | static void uart_wait_until_sent(struct tty_struct *tty, int timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | { |
Jiri Slaby | 1f33a51 | 2011-07-14 14:35:10 +0200 | [diff] [blame] | 1402 | struct uart_state *state = tty->driver_data; |
| 1403 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | unsigned long char_time, expire; |
| 1405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | if (port->type == PORT_UNKNOWN || port->fifosize == 0) |
| 1407 | return; |
| 1408 | |
| 1409 | /* |
| 1410 | * Set the check interval to be 1/5 of the estimated time to |
| 1411 | * send a single character, and make it at least 1. The check |
| 1412 | * interval should also be less than the timeout. |
| 1413 | * |
| 1414 | * Note: we have to use pretty tight timings here to satisfy |
| 1415 | * the NIST-PCTS. |
| 1416 | */ |
| 1417 | char_time = (port->timeout - HZ/50) / port->fifosize; |
| 1418 | char_time = char_time / 5; |
| 1419 | if (char_time == 0) |
| 1420 | char_time = 1; |
| 1421 | if (timeout && timeout < char_time) |
| 1422 | char_time = timeout; |
| 1423 | |
| 1424 | /* |
| 1425 | * If the transmitter hasn't cleared in twice the approximate |
| 1426 | * amount of time to send the entire FIFO, it probably won't |
| 1427 | * ever clear. This assumes the UART isn't doing flow |
| 1428 | * control, which is currently the case. Hence, if it ever |
| 1429 | * takes longer than port->timeout, this is probably due to a |
| 1430 | * UART bug of some kind. So, we clamp the timeout parameter at |
| 1431 | * 2*port->timeout. |
| 1432 | */ |
| 1433 | if (timeout == 0 || timeout > 2 * port->timeout) |
| 1434 | timeout = 2 * port->timeout; |
| 1435 | |
| 1436 | expire = jiffies + timeout; |
| 1437 | |
Jiri Slaby | eb3a1e1 | 2007-05-06 14:48:52 -0700 | [diff] [blame] | 1438 | pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n", |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1439 | port->line, jiffies, expire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | |
| 1441 | /* |
| 1442 | * Check whether the transmitter is empty every 'char_time'. |
| 1443 | * 'timeout' / 'expire' give us the maximum amount of time |
| 1444 | * we wait. |
| 1445 | */ |
| 1446 | while (!port->ops->tx_empty(port)) { |
| 1447 | msleep_interruptible(jiffies_to_msecs(char_time)); |
| 1448 | if (signal_pending(current)) |
| 1449 | break; |
| 1450 | if (time_after(jiffies, expire)) |
| 1451 | break; |
| 1452 | } |
Arnd Bergmann | 2036521 | 2010-06-01 22:53:07 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | /* |
Kevin Cernekee | ef4f527 | 2012-12-26 20:43:41 -0800 | [diff] [blame] | 1456 | * Calls to uart_hangup() are serialised by the tty_lock in |
| 1457 | * drivers/tty/tty_io.c:do_tty_hangup() |
| 1458 | * This runs from a workqueue and can sleep for a _short_ time only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | */ |
| 1460 | static void uart_hangup(struct tty_struct *tty) |
| 1461 | { |
| 1462 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1463 | struct tty_port *port = &state->port; |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1464 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1466 | pr_debug("uart_hangup(%d)\n", state->uart_port->line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1468 | mutex_lock(&port->mutex); |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1469 | if (port->flags & ASYNC_NORMAL_ACTIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | uart_flush_buffer(tty); |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1471 | uart_shutdown(tty, state); |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1472 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1473 | port->count = 0; |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1474 | clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags); |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1475 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | 7b01478 | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 1476 | tty_port_tty_set(port, NULL); |
Geert Uytterhoeven | bf903c0 | 2014-03-27 11:40:39 +0100 | [diff] [blame] | 1477 | if (!uart_console(state->uart_port)) |
| 1478 | uart_change_pm(state, UART_PM_STATE_OFF); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1479 | wake_up_interruptible(&port->open_wait); |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1480 | wake_up_interruptible(&port->delta_msr_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | } |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1482 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Jiri Slaby | 0b1db83 | 2011-11-09 21:33:50 +0100 | [diff] [blame] | 1485 | static int uart_port_activate(struct tty_port *port, struct tty_struct *tty) |
| 1486 | { |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
| 1490 | static void uart_port_shutdown(struct tty_port *port) |
| 1491 | { |
Jiri Slaby | b922e19 | 2011-11-09 21:33:51 +0100 | [diff] [blame] | 1492 | struct uart_state *state = container_of(port, struct uart_state, port); |
| 1493 | struct uart_port *uport = state->uart_port; |
| 1494 | |
| 1495 | /* |
| 1496 | * clear delta_msr_wait queue to avoid mem leaks: we may free |
| 1497 | * the irq here so the queue might never be woken up. Note |
| 1498 | * that we won't end up waiting on delta_msr_wait again since |
| 1499 | * any outstanding file descriptors should be pointing at |
| 1500 | * hung_up_tty_fops now. |
| 1501 | */ |
| 1502 | wake_up_interruptible(&port->delta_msr_wait); |
| 1503 | |
| 1504 | /* |
| 1505 | * Free the IRQ and disable the port. |
| 1506 | */ |
| 1507 | uport->ops->shutdown(uport); |
| 1508 | |
| 1509 | /* |
| 1510 | * Ensure that the IRQ handler isn't running on another CPU. |
| 1511 | */ |
| 1512 | synchronize_irq(uport->irq); |
Jiri Slaby | 0b1db83 | 2011-11-09 21:33:50 +0100 | [diff] [blame] | 1513 | } |
| 1514 | |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 1515 | static int uart_carrier_raised(struct tty_port *port) |
| 1516 | { |
| 1517 | struct uart_state *state = container_of(port, struct uart_state, port); |
| 1518 | struct uart_port *uport = state->uart_port; |
| 1519 | int mctrl; |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 1520 | spin_lock_irq(&uport->lock); |
Alexander Shiyan | 1fdc310 | 2014-06-03 18:54:43 +0400 | [diff] [blame] | 1521 | uart_enable_ms(uport); |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 1522 | mctrl = uport->ops->get_mctrl(uport); |
| 1523 | spin_unlock_irq(&uport->lock); |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 1524 | if (mctrl & TIOCM_CAR) |
| 1525 | return 1; |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | static void uart_dtr_rts(struct tty_port *port, int onoff) |
| 1530 | { |
| 1531 | struct uart_state *state = container_of(port, struct uart_state, port); |
| 1532 | struct uart_port *uport = state->uart_port; |
Alan Cox | 24fcc7c | 2010-06-01 22:52:59 +0200 | [diff] [blame] | 1533 | |
Jiri Slaby | 6f5c24a | 2011-03-30 00:10:57 +0200 | [diff] [blame] | 1534 | if (onoff) |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 1535 | uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS); |
| 1536 | else |
| 1537 | uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS); |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 1538 | } |
| 1539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | /* |
Kevin Cernekee | ef4f527 | 2012-12-26 20:43:41 -0800 | [diff] [blame] | 1541 | * Calls to uart_open are serialised by the tty_lock in |
| 1542 | * drivers/tty/tty_io.c:tty_open() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | * Note that if this fails, then uart_close() _will_ be called. |
| 1544 | * |
| 1545 | * In time, we want to scrap the "opening nonpresent ports" |
| 1546 | * behaviour and implement an alternative way for setserial |
| 1547 | * to set base addresses/ports/types. This will allow us to |
| 1548 | * get rid of a certain amount of extra tests. |
| 1549 | */ |
| 1550 | static int uart_open(struct tty_struct *tty, struct file *filp) |
| 1551 | { |
| 1552 | struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | int retval, line = tty->index; |
Jiri Slaby | 1c7b13c | 2011-11-09 21:33:49 +0100 | [diff] [blame] | 1554 | struct uart_state *state = drv->state + line; |
| 1555 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | |
Jiri Slaby | eb3a1e1 | 2007-05-06 14:48:52 -0700 | [diff] [blame] | 1557 | pr_debug("uart_open(%d) called\n", line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
| 1559 | /* |
Jiri Slaby | 1c7b13c | 2011-11-09 21:33:49 +0100 | [diff] [blame] | 1560 | * We take the semaphore here to guarantee that we won't be re-entered |
| 1561 | * while allocating the state structure, or while we request any IRQs |
| 1562 | * that the driver may need. This also has the nice side-effect that |
| 1563 | * it delays the action of uart_hangup, so we can guarantee that |
| 1564 | * state->port.tty will always contain something reasonable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | */ |
Jiri Slaby | 1c7b13c | 2011-11-09 21:33:49 +0100 | [diff] [blame] | 1566 | if (mutex_lock_interruptible(&port->mutex)) { |
| 1567 | retval = -ERESTARTSYS; |
| 1568 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | } |
Jiri Slaby | 1c7b13c | 2011-11-09 21:33:49 +0100 | [diff] [blame] | 1570 | |
| 1571 | port->count++; |
| 1572 | if (!state->uart_port || state->uart_port->flags & UPF_DEAD) { |
| 1573 | retval = -ENXIO; |
| 1574 | goto err_dec_count; |
| 1575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | |
| 1577 | /* |
| 1578 | * Once we set tty->driver_data here, we are guaranteed that |
| 1579 | * uart_close() will decrement the driver module use count. |
| 1580 | * Any failures from here onwards should not touch the count. |
| 1581 | */ |
| 1582 | tty->driver_data = state; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1583 | state->uart_port->state = state; |
Jiri Slaby | d6c53c0 | 2013-01-03 15:53:05 +0100 | [diff] [blame] | 1584 | state->port.low_latency = |
| 1585 | (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0; |
Alan Cox | 7b01478 | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 1586 | tty_port_tty_set(port, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
| 1588 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | * Start up the serial port. |
| 1590 | */ |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 1591 | retval = uart_startup(tty, state, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
| 1593 | /* |
| 1594 | * If we succeeded, wait until the port is ready. |
| 1595 | */ |
Alan Cox | 61cd8a2 | 2010-06-01 22:52:57 +0200 | [diff] [blame] | 1596 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | if (retval == 0) |
Alan Cox | 74c2107 | 2010-06-01 22:53:00 +0200 | [diff] [blame] | 1598 | retval = tty_port_block_til_ready(port, tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
Jiri Slaby | 1c7b13c | 2011-11-09 21:33:49 +0100 | [diff] [blame] | 1600 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | return retval; |
Jiri Slaby | 1c7b13c | 2011-11-09 21:33:49 +0100 | [diff] [blame] | 1602 | err_dec_count: |
| 1603 | port->count--; |
| 1604 | mutex_unlock(&port->mutex); |
| 1605 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | static const char *uart_type(struct uart_port *port) |
| 1609 | { |
| 1610 | const char *str = NULL; |
| 1611 | |
| 1612 | if (port->ops->type) |
| 1613 | str = port->ops->type(port); |
| 1614 | |
| 1615 | if (!str) |
| 1616 | str = "unknown"; |
| 1617 | |
| 1618 | return str; |
| 1619 | } |
| 1620 | |
| 1621 | #ifdef CONFIG_PROC_FS |
| 1622 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1623 | static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | { |
| 1625 | struct uart_state *state = drv->state + i; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1626 | struct tty_port *port = &state->port; |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 1627 | enum uart_pm_state pm_state; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1628 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | char stat_buf[32]; |
| 1630 | unsigned int status; |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1631 | int mmio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1633 | if (!uport) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1634 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1636 | mmio = uport->iotype >= UPIO_MEM; |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1637 | seq_printf(m, "%d: uart:%s %s%08llX irq:%d", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1638 | uport->line, uart_type(uport), |
Sergei Shtylyov | 6c6a233 | 2006-09-04 00:04:20 +0400 | [diff] [blame] | 1639 | mmio ? "mmio:0x" : "port:", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1640 | mmio ? (unsigned long long)uport->mapbase |
| 1641 | : (unsigned long long)uport->iobase, |
| 1642 | uport->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1644 | if (uport->type == PORT_UNKNOWN) { |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1645 | seq_putc(m, '\n'); |
| 1646 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1649 | if (capable(CAP_SYS_ADMIN)) { |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1650 | mutex_lock(&port->mutex); |
George G. Davis | 3689a0e | 2007-02-14 00:33:06 -0800 | [diff] [blame] | 1651 | pm_state = state->pm_state; |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 1652 | if (pm_state != UART_PM_STATE_ON) |
| 1653 | uart_change_pm(state, UART_PM_STATE_ON); |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1654 | spin_lock_irq(&uport->lock); |
| 1655 | status = uport->ops->get_mctrl(uport); |
| 1656 | spin_unlock_irq(&uport->lock); |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 1657 | if (pm_state != UART_PM_STATE_ON) |
George G. Davis | 3689a0e | 2007-02-14 00:33:06 -0800 | [diff] [blame] | 1658 | uart_change_pm(state, pm_state); |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1659 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1661 | seq_printf(m, " tx:%d rx:%d", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1662 | uport->icount.tx, uport->icount.rx); |
| 1663 | if (uport->icount.frame) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1664 | seq_printf(m, " fe:%d", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1665 | uport->icount.frame); |
| 1666 | if (uport->icount.parity) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1667 | seq_printf(m, " pe:%d", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1668 | uport->icount.parity); |
| 1669 | if (uport->icount.brk) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1670 | seq_printf(m, " brk:%d", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1671 | uport->icount.brk); |
| 1672 | if (uport->icount.overrun) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1673 | seq_printf(m, " oe:%d", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1674 | uport->icount.overrun); |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1675 | |
| 1676 | #define INFOBIT(bit, str) \ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1677 | if (uport->mctrl & (bit)) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | strncat(stat_buf, (str), sizeof(stat_buf) - \ |
| 1679 | strlen(stat_buf) - 2) |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1680 | #define STATBIT(bit, str) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | if (status & (bit)) \ |
| 1682 | strncat(stat_buf, (str), sizeof(stat_buf) - \ |
| 1683 | strlen(stat_buf) - 2) |
| 1684 | |
| 1685 | stat_buf[0] = '\0'; |
| 1686 | stat_buf[1] = '\0'; |
| 1687 | INFOBIT(TIOCM_RTS, "|RTS"); |
| 1688 | STATBIT(TIOCM_CTS, "|CTS"); |
| 1689 | INFOBIT(TIOCM_DTR, "|DTR"); |
| 1690 | STATBIT(TIOCM_DSR, "|DSR"); |
| 1691 | STATBIT(TIOCM_CAR, "|CD"); |
| 1692 | STATBIT(TIOCM_RNG, "|RI"); |
| 1693 | if (stat_buf[0]) |
| 1694 | stat_buf[0] = ' '; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1695 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1696 | seq_puts(m, stat_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | } |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1698 | seq_putc(m, '\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | #undef STATBIT |
| 1700 | #undef INFOBIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1703 | static int uart_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | { |
Alexey Dobriyan | 833bb30 | 2009-04-02 01:30:04 +0400 | [diff] [blame] | 1705 | struct tty_driver *ttydrv = m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | struct uart_driver *drv = ttydrv->driver_state; |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1707 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1709 | seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | "", "", ""); |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1711 | for (i = 0; i < drv->nr; i++) |
| 1712 | uart_line_info(m, drv, i); |
| 1713 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | } |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1715 | |
| 1716 | static int uart_proc_open(struct inode *inode, struct file *file) |
| 1717 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 1718 | return single_open(file, uart_proc_show, PDE_DATA(inode)); |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | static const struct file_operations uart_proc_fops = { |
| 1722 | .owner = THIS_MODULE, |
| 1723 | .open = uart_proc_open, |
| 1724 | .read = seq_read, |
| 1725 | .llseek = seq_lseek, |
| 1726 | .release = single_release, |
| 1727 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | #endif |
| 1729 | |
Andrew Morton | 4a1b550 | 2008-03-07 15:51:16 -0800 | [diff] [blame] | 1730 | #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | /* |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 1732 | * uart_console_write - write a console message to a serial port |
| 1733 | * @port: the port to write the message |
| 1734 | * @s: array of characters |
| 1735 | * @count: number of characters in string to write |
| 1736 | * @write: function to write character to port |
| 1737 | */ |
| 1738 | void uart_console_write(struct uart_port *port, const char *s, |
| 1739 | unsigned int count, |
| 1740 | void (*putchar)(struct uart_port *, int)) |
| 1741 | { |
| 1742 | unsigned int i; |
| 1743 | |
| 1744 | for (i = 0; i < count; i++, s++) { |
| 1745 | if (*s == '\n') |
| 1746 | putchar(port, '\r'); |
| 1747 | putchar(port, *s); |
| 1748 | } |
| 1749 | } |
| 1750 | EXPORT_SYMBOL_GPL(uart_console_write); |
| 1751 | |
| 1752 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | * Check whether an invalid uart number has been specified, and |
| 1754 | * if so, search for the first available port that does have |
| 1755 | * console support. |
| 1756 | */ |
| 1757 | struct uart_port * __init |
| 1758 | uart_get_console(struct uart_port *ports, int nr, struct console *co) |
| 1759 | { |
| 1760 | int idx = co->index; |
| 1761 | |
| 1762 | if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 && |
| 1763 | ports[idx].membase == NULL)) |
| 1764 | for (idx = 0; idx < nr; idx++) |
| 1765 | if (ports[idx].iobase != 0 || |
| 1766 | ports[idx].membase != NULL) |
| 1767 | break; |
| 1768 | |
| 1769 | co->index = idx; |
| 1770 | |
| 1771 | return ports + idx; |
| 1772 | } |
| 1773 | |
| 1774 | /** |
Geert Uytterhoeven | 02088ca | 2014-03-11 11:23:35 +0100 | [diff] [blame] | 1775 | * uart_parse_options - Parse serial port baud/parity/bits/flow control. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | * @options: pointer to option string |
| 1777 | * @baud: pointer to an 'int' variable for the baud rate. |
| 1778 | * @parity: pointer to an 'int' variable for the parity. |
| 1779 | * @bits: pointer to an 'int' variable for the number of data bits. |
| 1780 | * @flow: pointer to an 'int' variable for the flow control character. |
| 1781 | * |
| 1782 | * uart_parse_options decodes a string containing the serial console |
| 1783 | * options. The format of the string is <baud><parity><bits><flow>, |
| 1784 | * eg: 115200n8r |
| 1785 | */ |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1786 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow) |
| 1788 | { |
| 1789 | char *s = options; |
| 1790 | |
| 1791 | *baud = simple_strtoul(s, NULL, 10); |
| 1792 | while (*s >= '0' && *s <= '9') |
| 1793 | s++; |
| 1794 | if (*s) |
| 1795 | *parity = *s++; |
| 1796 | if (*s) |
| 1797 | *bits = *s++ - '0'; |
| 1798 | if (*s) |
| 1799 | *flow = *s; |
| 1800 | } |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1801 | EXPORT_SYMBOL_GPL(uart_parse_options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | |
| 1803 | struct baud_rates { |
| 1804 | unsigned int rate; |
| 1805 | unsigned int cflag; |
| 1806 | }; |
| 1807 | |
Arjan van de Ven | cb3592b | 2005-11-28 21:04:11 +0000 | [diff] [blame] | 1808 | static const struct baud_rates baud_rates[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | { 921600, B921600 }, |
| 1810 | { 460800, B460800 }, |
| 1811 | { 230400, B230400 }, |
| 1812 | { 115200, B115200 }, |
| 1813 | { 57600, B57600 }, |
| 1814 | { 38400, B38400 }, |
| 1815 | { 19200, B19200 }, |
| 1816 | { 9600, B9600 }, |
| 1817 | { 4800, B4800 }, |
| 1818 | { 2400, B2400 }, |
| 1819 | { 1200, B1200 }, |
| 1820 | { 0, B38400 } |
| 1821 | }; |
| 1822 | |
| 1823 | /** |
| 1824 | * uart_set_options - setup the serial console parameters |
| 1825 | * @port: pointer to the serial ports uart_port structure |
| 1826 | * @co: console pointer |
| 1827 | * @baud: baud rate |
| 1828 | * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) |
| 1829 | * @bits: number of data bits |
| 1830 | * @flow: flow control character - 'r' (rts) |
| 1831 | */ |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1832 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | uart_set_options(struct uart_port *port, struct console *co, |
| 1834 | int baud, int parity, int bits, int flow) |
| 1835 | { |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1836 | struct ktermios termios; |
Alan Cox | 149b36e | 2007-10-18 01:24:16 -0700 | [diff] [blame] | 1837 | static struct ktermios dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | int i; |
| 1839 | |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 1840 | /* |
| 1841 | * Ensure that the serial console lock is initialised |
| 1842 | * early. |
Randy Witt | 42b6a1b | 2013-10-17 16:56:47 -0400 | [diff] [blame] | 1843 | * If this port is a console, then the spinlock is already |
| 1844 | * initialised. |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 1845 | */ |
Randy Witt | 42b6a1b | 2013-10-17 16:56:47 -0400 | [diff] [blame] | 1846 | if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) { |
| 1847 | spin_lock_init(&port->lock); |
| 1848 | lockdep_set_class(&port->lock, &port_lock_key); |
| 1849 | } |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 1850 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1851 | memset(&termios, 0, sizeof(struct ktermios)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | |
| 1853 | termios.c_cflag = CREAD | HUPCL | CLOCAL; |
| 1854 | |
| 1855 | /* |
| 1856 | * Construct a cflag setting. |
| 1857 | */ |
| 1858 | for (i = 0; baud_rates[i].rate; i++) |
| 1859 | if (baud_rates[i].rate <= baud) |
| 1860 | break; |
| 1861 | |
| 1862 | termios.c_cflag |= baud_rates[i].cflag; |
| 1863 | |
| 1864 | if (bits == 7) |
| 1865 | termios.c_cflag |= CS7; |
| 1866 | else |
| 1867 | termios.c_cflag |= CS8; |
| 1868 | |
| 1869 | switch (parity) { |
| 1870 | case 'o': case 'O': |
| 1871 | termios.c_cflag |= PARODD; |
| 1872 | /*fall through*/ |
| 1873 | case 'e': case 'E': |
| 1874 | termios.c_cflag |= PARENB; |
| 1875 | break; |
| 1876 | } |
| 1877 | |
| 1878 | if (flow == 'r') |
| 1879 | termios.c_cflag |= CRTSCTS; |
| 1880 | |
Yinghai Lu | 7949268 | 2007-07-15 23:37:25 -0700 | [diff] [blame] | 1881 | /* |
| 1882 | * some uarts on other side don't support no flow control. |
| 1883 | * So we set * DTR in host uart to make them happy |
| 1884 | */ |
| 1885 | port->mctrl |= TIOCM_DTR; |
| 1886 | |
Alan Cox | 149b36e | 2007-10-18 01:24:16 -0700 | [diff] [blame] | 1887 | port->ops->set_termios(port, &termios, &dummy); |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1888 | /* |
| 1889 | * Allow the setting of the UART parameters with a NULL console |
| 1890 | * too: |
| 1891 | */ |
| 1892 | if (co) |
| 1893 | co->cflag = termios.c_cflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | |
| 1895 | return 0; |
| 1896 | } |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1897 | EXPORT_SYMBOL_GPL(uart_set_options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | #endif /* CONFIG_SERIAL_CORE_CONSOLE */ |
| 1899 | |
Jiri Slaby | cf75525 | 2011-11-09 21:33:47 +0100 | [diff] [blame] | 1900 | /** |
| 1901 | * uart_change_pm - set power state of the port |
| 1902 | * |
| 1903 | * @state: port descriptor |
| 1904 | * @pm_state: new state |
| 1905 | * |
| 1906 | * Locking: port->mutex has to be held |
| 1907 | */ |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 1908 | static void uart_change_pm(struct uart_state *state, |
| 1909 | enum uart_pm_state pm_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1911 | struct uart_port *port = state->uart_port; |
Andrew Victor | 1281e36 | 2006-05-16 11:28:49 +0100 | [diff] [blame] | 1912 | |
| 1913 | if (state->pm_state != pm_state) { |
| 1914 | if (port->ops->pm) |
| 1915 | port->ops->pm(port, pm_state, state->pm_state); |
| 1916 | state->pm_state = pm_state; |
| 1917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1920 | struct uart_match { |
| 1921 | struct uart_port *port; |
| 1922 | struct uart_driver *driver; |
| 1923 | }; |
| 1924 | |
| 1925 | static int serial_match_port(struct device *dev, void *data) |
| 1926 | { |
| 1927 | struct uart_match *match = data; |
Guennadi Liakhovetski | 7ca796f | 2008-07-04 09:59:28 -0700 | [diff] [blame] | 1928 | struct tty_driver *tty_drv = match->driver->tty_driver; |
| 1929 | dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + |
| 1930 | match->port->line; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1931 | |
| 1932 | return dev->devt == devt; /* Actually, only one tty per port */ |
| 1933 | } |
| 1934 | |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1935 | int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | { |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1937 | struct uart_state *state = drv->state + uport->line; |
| 1938 | struct tty_port *port = &state->port; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1939 | struct device *tty_dev; |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1940 | struct uart_match match = {uport, drv}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1942 | mutex_lock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1944 | tty_dev = device_find_child(uport->dev, &match, serial_match_port); |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1945 | if (device_may_wakeup(tty_dev)) { |
Govindraj.R | 3f960db | 2010-12-16 18:12:47 +0530 | [diff] [blame] | 1946 | if (!enable_irq_wake(uport->irq)) |
| 1947 | uport->irq_wake = 1; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1948 | put_device(tty_dev); |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1949 | mutex_unlock(&port->mutex); |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1950 | return 0; |
| 1951 | } |
Federico Vaga | 5a65dcc | 2013-04-15 16:01:07 +0200 | [diff] [blame] | 1952 | put_device(tty_dev); |
| 1953 | |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 1954 | if (console_suspend_enabled || !uart_console(uport)) |
| 1955 | uport->suspended = 1; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1956 | |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1957 | if (port->flags & ASYNC_INITIALIZED) { |
| 1958 | const struct uart_ops *ops = uport->ops; |
Russell King | c8c6bfa | 2008-02-04 22:27:52 -0800 | [diff] [blame] | 1959 | int tries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 1961 | if (console_suspend_enabled || !uart_console(uport)) { |
| 1962 | set_bit(ASYNCB_SUSPENDED, &port->flags); |
| 1963 | clear_bit(ASYNCB_INITIALIZED, &port->flags); |
Russell King | a6b93a9 | 2006-10-01 17:17:40 +0100 | [diff] [blame] | 1964 | |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 1965 | spin_lock_irq(&uport->lock); |
| 1966 | ops->stop_tx(uport); |
| 1967 | ops->set_mctrl(uport, 0); |
| 1968 | ops->stop_rx(uport); |
| 1969 | spin_unlock_irq(&uport->lock); |
| 1970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | |
| 1972 | /* |
| 1973 | * Wait for the transmitter to empty. |
| 1974 | */ |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1975 | for (tries = 3; !ops->tx_empty(uport) && tries; tries--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | msleep(10); |
Russell King | c8c6bfa | 2008-02-04 22:27:52 -0800 | [diff] [blame] | 1977 | if (!tries) |
Sudip Mukherjee | 2f2dafe | 2014-09-01 20:49:43 +0530 | [diff] [blame] | 1978 | dev_err(uport->dev, "%s%d: Unable to drain transmitter\n", |
| 1979 | drv->dev_name, |
| 1980 | drv->tty_driver->name_base + uport->line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 1982 | if (console_suspend_enabled || !uart_console(uport)) |
| 1983 | ops->shutdown(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* |
| 1987 | * Disable the console device before suspending. |
| 1988 | */ |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 1989 | if (console_suspend_enabled && uart_console(uport)) |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 1990 | console_stop(uport->cons); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 1992 | if (console_suspend_enabled || !uart_console(uport)) |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 1993 | uart_change_pm(state, UART_PM_STATE_OFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1995 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2000 | int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | { |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2002 | struct uart_state *state = drv->state + uport->line; |
| 2003 | struct tty_port *port = &state->port; |
Arjan van de Ven | 03a74dc | 2008-05-23 13:04:49 -0700 | [diff] [blame] | 2004 | struct device *tty_dev; |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2005 | struct uart_match match = {uport, drv}; |
Deepak Saxena | ba15ab0 | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 2006 | struct ktermios termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2008 | mutex_lock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2010 | tty_dev = device_find_child(uport->dev, &match, serial_match_port); |
| 2011 | if (!uport->suspended && device_may_wakeup(tty_dev)) { |
Govindraj.R | 3f960db | 2010-12-16 18:12:47 +0530 | [diff] [blame] | 2012 | if (uport->irq_wake) { |
| 2013 | disable_irq_wake(uport->irq); |
| 2014 | uport->irq_wake = 0; |
| 2015 | } |
Federico Vaga | 5a65dcc | 2013-04-15 16:01:07 +0200 | [diff] [blame] | 2016 | put_device(tty_dev); |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2017 | mutex_unlock(&port->mutex); |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2018 | return 0; |
| 2019 | } |
Federico Vaga | 5a65dcc | 2013-04-15 16:01:07 +0200 | [diff] [blame] | 2020 | put_device(tty_dev); |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2021 | uport->suspended = 0; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | /* |
| 2024 | * Re-enable the console device after suspending. |
| 2025 | */ |
Yin Kangkai | 5933a16 | 2011-01-30 11:15:30 +0800 | [diff] [blame] | 2026 | if (uart_console(uport)) { |
Jason Wang | 891b9dd | 2010-08-21 15:14:42 +0800 | [diff] [blame] | 2027 | /* |
| 2028 | * First try to use the console cflag setting. |
| 2029 | */ |
| 2030 | memset(&termios, 0, sizeof(struct ktermios)); |
| 2031 | termios.c_cflag = uport->cons->cflag; |
| 2032 | |
| 2033 | /* |
| 2034 | * If that's unset, use the tty termios setting. |
| 2035 | */ |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 2036 | if (port->tty && termios.c_cflag == 0) |
| 2037 | termios = port->tty->termios; |
Jason Wang | 891b9dd | 2010-08-21 15:14:42 +0800 | [diff] [blame] | 2038 | |
Ning Jiang | 94abc56 | 2011-09-05 16:28:18 +0800 | [diff] [blame] | 2039 | if (console_suspend_enabled) |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 2040 | uart_change_pm(state, UART_PM_STATE_ON); |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2041 | uport->ops->set_termios(uport, &termios, NULL); |
Yin Kangkai | 5933a16 | 2011-01-30 11:15:30 +0800 | [diff] [blame] | 2042 | if (console_suspend_enabled) |
| 2043 | console_start(uport->cons); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2046 | if (port->flags & ASYNC_SUSPENDED) { |
| 2047 | const struct uart_ops *ops = uport->ops; |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 2048 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 2050 | uart_change_pm(state, UART_PM_STATE_ON); |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2051 | spin_lock_irq(&uport->lock); |
| 2052 | ops->set_mctrl(uport, 0); |
| 2053 | spin_unlock_irq(&uport->lock); |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 2054 | if (console_suspend_enabled || !uart_console(uport)) { |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 2055 | /* Protected by port mutex for now */ |
| 2056 | struct tty_struct *tty = port->tty; |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 2057 | ret = ops->startup(uport); |
| 2058 | if (ret == 0) { |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 2059 | if (tty) |
| 2060 | uart_change_speed(tty, state, NULL); |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 2061 | spin_lock_irq(&uport->lock); |
| 2062 | ops->set_mctrl(uport, uport->mctrl); |
| 2063 | ops->start_tx(uport); |
| 2064 | spin_unlock_irq(&uport->lock); |
| 2065 | set_bit(ASYNCB_INITIALIZED, &port->flags); |
| 2066 | } else { |
| 2067 | /* |
| 2068 | * Failed to resume - maybe hardware went away? |
| 2069 | * Clear the "initialized" flag so we won't try |
| 2070 | * to call the low level drivers shutdown method. |
| 2071 | */ |
Alan Cox | 1922513 | 2010-06-01 22:52:51 +0200 | [diff] [blame] | 2072 | uart_shutdown(tty, state); |
Stanislav Brabec | 4547be7 | 2009-12-02 16:20:56 +0100 | [diff] [blame] | 2073 | } |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 2074 | } |
Russell King | a6b93a9 | 2006-10-01 17:17:40 +0100 | [diff] [blame] | 2075 | |
Alan Cox | ccce6de | 2009-09-19 13:13:30 -0700 | [diff] [blame] | 2076 | clear_bit(ASYNCB_SUSPENDED, &port->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | } |
| 2078 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2079 | mutex_unlock(&port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
| 2084 | static inline void |
| 2085 | uart_report_port(struct uart_driver *drv, struct uart_port *port) |
| 2086 | { |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2087 | char address[64]; |
| 2088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | switch (port->iotype) { |
| 2090 | case UPIO_PORT: |
Andrew Morton | 9bde10a | 2008-10-13 10:35:42 +0100 | [diff] [blame] | 2091 | snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | break; |
| 2093 | case UPIO_HUB6: |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2094 | snprintf(address, sizeof(address), |
Andrew Morton | 9bde10a | 2008-10-13 10:35:42 +0100 | [diff] [blame] | 2095 | "I/O 0x%lx offset 0x%x", port->iobase, port->hub6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | break; |
| 2097 | case UPIO_MEM: |
| 2098 | case UPIO_MEM32: |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 2099 | case UPIO_AU: |
Zang Roy-r61911 | 3be91ec | 2006-06-30 02:29:58 -0700 | [diff] [blame] | 2100 | case UPIO_TSI: |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2101 | snprintf(address, sizeof(address), |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 2102 | "MMIO 0x%llx", (unsigned long long)port->mapbase); |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2103 | break; |
| 2104 | default: |
| 2105 | strlcpy(address, "*unknown*", sizeof(address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | break; |
| 2107 | } |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2108 | |
Sudip Mukherjee | 2f2dafe | 2014-09-01 20:49:43 +0530 | [diff] [blame] | 2109 | dev_info(port->dev, "%s%d at %s (irq = %d, base_baud = %d) is a %s\n", |
David S. Miller | 8440838 | 2008-10-13 10:45:26 +0100 | [diff] [blame] | 2110 | drv->dev_name, |
| 2111 | drv->tty_driver->name_base + port->line, |
Kees Cook | 7d12b97 | 2013-07-12 13:07:39 -0700 | [diff] [blame] | 2112 | address, port->irq, port->uartclk / 16, uart_type(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | static void |
| 2116 | uart_configure_port(struct uart_driver *drv, struct uart_state *state, |
| 2117 | struct uart_port *port) |
| 2118 | { |
| 2119 | unsigned int flags; |
| 2120 | |
| 2121 | /* |
| 2122 | * If there isn't a port here, don't do anything further. |
| 2123 | */ |
| 2124 | if (!port->iobase && !port->mapbase && !port->membase) |
| 2125 | return; |
| 2126 | |
| 2127 | /* |
| 2128 | * Now do the auto configuration stuff. Note that config_port |
| 2129 | * is expected to claim the resources and map the port for us. |
| 2130 | */ |
David Daney | 8e23fcc | 2009-01-02 13:49:54 +0000 | [diff] [blame] | 2131 | flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | if (port->flags & UPF_AUTO_IRQ) |
| 2133 | flags |= UART_CONFIG_IRQ; |
| 2134 | if (port->flags & UPF_BOOT_AUTOCONF) { |
David Daney | 8e23fcc | 2009-01-02 13:49:54 +0000 | [diff] [blame] | 2135 | if (!(port->flags & UPF_FIXED_TYPE)) { |
| 2136 | port->type = PORT_UNKNOWN; |
| 2137 | flags |= UART_CONFIG_TYPE; |
| 2138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | port->ops->config_port(port, flags); |
| 2140 | } |
| 2141 | |
| 2142 | if (port->type != PORT_UNKNOWN) { |
| 2143 | unsigned long flags; |
| 2144 | |
| 2145 | uart_report_port(drv, port); |
| 2146 | |
George G. Davis | 3689a0e | 2007-02-14 00:33:06 -0800 | [diff] [blame] | 2147 | /* Power up port for set_mctrl() */ |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 2148 | uart_change_pm(state, UART_PM_STATE_ON); |
George G. Davis | 3689a0e | 2007-02-14 00:33:06 -0800 | [diff] [blame] | 2149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | /* |
| 2151 | * Ensure that the modem control lines are de-activated. |
Yinghai Lu | c3e4642 | 2008-02-04 22:27:46 -0800 | [diff] [blame] | 2152 | * keep the DTR setting that is set in uart_set_options() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | * We probably don't need a spinlock around this, but |
| 2154 | */ |
| 2155 | spin_lock_irqsave(&port->lock, flags); |
Yinghai Lu | c3e4642 | 2008-02-04 22:27:46 -0800 | [diff] [blame] | 2156 | port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | spin_unlock_irqrestore(&port->lock, flags); |
| 2158 | |
| 2159 | /* |
Russell King | 97d9722 | 2007-09-01 21:25:09 +0100 | [diff] [blame] | 2160 | * If this driver supports console, and it hasn't been |
| 2161 | * successfully registered yet, try to re-register it. |
| 2162 | * It may be that the port was not available. |
| 2163 | */ |
| 2164 | if (port->cons && !(port->cons->flags & CON_ENABLED)) |
| 2165 | register_console(port->cons); |
| 2166 | |
| 2167 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | * Power down all ports by default, except the |
| 2169 | * console if we have one. |
| 2170 | */ |
| 2171 | if (!uart_console(port)) |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 2172 | uart_change_pm(state, UART_PM_STATE_OFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2176 | #ifdef CONFIG_CONSOLE_POLL |
| 2177 | |
| 2178 | static int uart_poll_init(struct tty_driver *driver, int line, char *options) |
| 2179 | { |
| 2180 | struct uart_driver *drv = driver->driver_state; |
| 2181 | struct uart_state *state = drv->state + line; |
| 2182 | struct uart_port *port; |
| 2183 | int baud = 9600; |
| 2184 | int bits = 8; |
| 2185 | int parity = 'n'; |
| 2186 | int flow = 'n'; |
Anton Vorontsov | c7f3e70 | 2012-09-24 14:27:53 -0700 | [diff] [blame] | 2187 | int ret; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2188 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2189 | if (!state || !state->uart_port) |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2190 | return -1; |
| 2191 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2192 | port = state->uart_port; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2193 | if (!(port->ops->poll_get_char && port->ops->poll_put_char)) |
| 2194 | return -1; |
| 2195 | |
Anton Vorontsov | c7f3e70 | 2012-09-24 14:27:53 -0700 | [diff] [blame] | 2196 | if (port->ops->poll_init) { |
| 2197 | struct tty_port *tport = &state->port; |
| 2198 | |
| 2199 | ret = 0; |
| 2200 | mutex_lock(&tport->mutex); |
| 2201 | /* |
| 2202 | * We don't set ASYNCB_INITIALIZED as we only initialized the |
| 2203 | * hw, e.g. state->xmit is still uninitialized. |
| 2204 | */ |
| 2205 | if (!test_bit(ASYNCB_INITIALIZED, &tport->flags)) |
| 2206 | ret = port->ops->poll_init(port); |
| 2207 | mutex_unlock(&tport->mutex); |
| 2208 | if (ret) |
| 2209 | return ret; |
| 2210 | } |
| 2211 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2212 | if (options) { |
| 2213 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 2214 | return uart_set_options(port, NULL, baud, parity, bits, flow); |
| 2215 | } |
| 2216 | |
| 2217 | return 0; |
| 2218 | } |
| 2219 | |
| 2220 | static int uart_poll_get_char(struct tty_driver *driver, int line) |
| 2221 | { |
| 2222 | struct uart_driver *drv = driver->driver_state; |
| 2223 | struct uart_state *state = drv->state + line; |
| 2224 | struct uart_port *port; |
| 2225 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2226 | if (!state || !state->uart_port) |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2227 | return -1; |
| 2228 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2229 | port = state->uart_port; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2230 | return port->ops->poll_get_char(port); |
| 2231 | } |
| 2232 | |
| 2233 | static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) |
| 2234 | { |
| 2235 | struct uart_driver *drv = driver->driver_state; |
| 2236 | struct uart_state *state = drv->state + line; |
| 2237 | struct uart_port *port; |
| 2238 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2239 | if (!state || !state->uart_port) |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2240 | return; |
| 2241 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2242 | port = state->uart_port; |
Doug Anderson | c7d44a0 | 2014-04-21 10:06:43 -0700 | [diff] [blame] | 2243 | |
| 2244 | if (ch == '\n') |
| 2245 | port->ops->poll_put_char(port, '\r'); |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2246 | port->ops->poll_put_char(port, ch); |
| 2247 | } |
| 2248 | #endif |
| 2249 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 2250 | static const struct tty_operations uart_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | .open = uart_open, |
| 2252 | .close = uart_close, |
| 2253 | .write = uart_write, |
| 2254 | .put_char = uart_put_char, |
| 2255 | .flush_chars = uart_flush_chars, |
| 2256 | .write_room = uart_write_room, |
| 2257 | .chars_in_buffer= uart_chars_in_buffer, |
| 2258 | .flush_buffer = uart_flush_buffer, |
| 2259 | .ioctl = uart_ioctl, |
| 2260 | .throttle = uart_throttle, |
| 2261 | .unthrottle = uart_unthrottle, |
| 2262 | .send_xchar = uart_send_xchar, |
| 2263 | .set_termios = uart_set_termios, |
Alan Cox | 64e9159 | 2008-06-03 15:18:54 +0100 | [diff] [blame] | 2264 | .set_ldisc = uart_set_ldisc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | .stop = uart_stop, |
| 2266 | .start = uart_start, |
| 2267 | .hangup = uart_hangup, |
| 2268 | .break_ctl = uart_break_ctl, |
| 2269 | .wait_until_sent= uart_wait_until_sent, |
| 2270 | #ifdef CONFIG_PROC_FS |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 2271 | .proc_fops = &uart_proc_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | #endif |
| 2273 | .tiocmget = uart_tiocmget, |
| 2274 | .tiocmset = uart_tiocmset, |
Alan Cox | d281da7 | 2010-09-16 18:21:24 +0100 | [diff] [blame] | 2275 | .get_icount = uart_get_icount, |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2276 | #ifdef CONFIG_CONSOLE_POLL |
| 2277 | .poll_init = uart_poll_init, |
| 2278 | .poll_get_char = uart_poll_get_char, |
| 2279 | .poll_put_char = uart_poll_put_char, |
| 2280 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | }; |
| 2282 | |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 2283 | static const struct tty_port_operations uart_port_ops = { |
Jiri Slaby | 0b1db83 | 2011-11-09 21:33:50 +0100 | [diff] [blame] | 2284 | .activate = uart_port_activate, |
| 2285 | .shutdown = uart_port_shutdown, |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 2286 | .carrier_raised = uart_carrier_raised, |
| 2287 | .dtr_rts = uart_dtr_rts, |
| 2288 | }; |
| 2289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | /** |
| 2291 | * uart_register_driver - register a driver with the uart core layer |
| 2292 | * @drv: low level driver structure |
| 2293 | * |
| 2294 | * Register a uart driver with the core driver. We in turn register |
| 2295 | * with the tty layer, and initialise the core driver per-port state. |
| 2296 | * |
| 2297 | * We have a proc file in /proc/tty/driver which is named after the |
| 2298 | * normal driver. |
| 2299 | * |
| 2300 | * drv->port should be NULL, and the per-port structures should be |
| 2301 | * registered using uart_add_one_port after this call has succeeded. |
| 2302 | */ |
| 2303 | int uart_register_driver(struct uart_driver *drv) |
| 2304 | { |
André Goddard Rosa | 9e845ab | 2009-10-25 11:16:32 -0200 | [diff] [blame] | 2305 | struct tty_driver *normal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | int i, retval; |
| 2307 | |
| 2308 | BUG_ON(drv->state); |
| 2309 | |
| 2310 | /* |
| 2311 | * Maybe we should be using a slab cache for this, especially if |
| 2312 | * we have a large number of ports to handle. |
| 2313 | */ |
Burman Yan | 8f31bb3 | 2007-02-14 00:33:07 -0800 | [diff] [blame] | 2314 | drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | if (!drv->state) |
| 2316 | goto out; |
| 2317 | |
André Goddard Rosa | 9e845ab | 2009-10-25 11:16:32 -0200 | [diff] [blame] | 2318 | normal = alloc_tty_driver(drv->nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | if (!normal) |
André Goddard Rosa | 9e845ab | 2009-10-25 11:16:32 -0200 | [diff] [blame] | 2320 | goto out_kfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | |
| 2322 | drv->tty_driver = normal; |
| 2323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | normal->driver_name = drv->driver_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | normal->name = drv->dev_name; |
| 2326 | normal->major = drv->major; |
| 2327 | normal->minor_start = drv->minor; |
| 2328 | normal->type = TTY_DRIVER_TYPE_SERIAL; |
| 2329 | normal->subtype = SERIAL_TYPE_NORMAL; |
| 2330 | normal->init_termios = tty_std_termios; |
| 2331 | normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 2332 | normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600; |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2333 | normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | normal->driver_state = drv; |
| 2335 | tty_set_operations(normal, &uart_ops); |
| 2336 | |
| 2337 | /* |
| 2338 | * Initialise the UART state(s). |
| 2339 | */ |
| 2340 | for (i = 0; i < drv->nr; i++) { |
| 2341 | struct uart_state *state = drv->state + i; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2342 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2344 | tty_port_init(port); |
Alan Cox | de0c8cb | 2010-06-01 22:52:58 +0200 | [diff] [blame] | 2345 | port->ops = &uart_port_ops; |
Jiri Slaby | 4cb0fbf | 2011-11-09 21:33:45 +0100 | [diff] [blame] | 2346 | port->close_delay = HZ / 2; /* .5 seconds */ |
| 2347 | port->closing_wait = 30 * HZ;/* 30 seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | retval = tty_register_driver(normal); |
André Goddard Rosa | 9e845ab | 2009-10-25 11:16:32 -0200 | [diff] [blame] | 2351 | if (retval >= 0) |
| 2352 | return retval; |
| 2353 | |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 2354 | for (i = 0; i < drv->nr; i++) |
| 2355 | tty_port_destroy(&drv->state[i].port); |
André Goddard Rosa | 9e845ab | 2009-10-25 11:16:32 -0200 | [diff] [blame] | 2356 | put_tty_driver(normal); |
| 2357 | out_kfree: |
| 2358 | kfree(drv->state); |
| 2359 | out: |
| 2360 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | /** |
| 2364 | * uart_unregister_driver - remove a driver from the uart core layer |
| 2365 | * @drv: low level driver structure |
| 2366 | * |
| 2367 | * Remove all references to a driver from the core driver. The low |
| 2368 | * level driver must have removed all its ports via the |
| 2369 | * uart_remove_one_port() if it registered them with uart_add_one_port(). |
| 2370 | * (ie, drv->port == NULL) |
| 2371 | */ |
| 2372 | void uart_unregister_driver(struct uart_driver *drv) |
| 2373 | { |
| 2374 | struct tty_driver *p = drv->tty_driver; |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 2375 | unsigned int i; |
| 2376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | tty_unregister_driver(p); |
| 2378 | put_tty_driver(p); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 2379 | for (i = 0; i < drv->nr; i++) |
| 2380 | tty_port_destroy(&drv->state[i].port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | kfree(drv->state); |
Alan Cox | 1e66cded | 2012-05-14 14:51:22 +0100 | [diff] [blame] | 2382 | drv->state = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | drv->tty_driver = NULL; |
| 2384 | } |
| 2385 | |
| 2386 | struct tty_driver *uart_console_device(struct console *co, int *index) |
| 2387 | { |
| 2388 | struct uart_driver *p = co->data; |
| 2389 | *index = co->index; |
| 2390 | return p->tty_driver; |
| 2391 | } |
| 2392 | |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 2393 | static ssize_t uart_get_attr_uartclk(struct device *dev, |
| 2394 | struct device_attribute *attr, char *buf) |
| 2395 | { |
Alan Cox | bebe73e | 2012-10-29 15:19:57 +0000 | [diff] [blame] | 2396 | struct serial_struct tmp; |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 2397 | struct tty_port *port = dev_get_drvdata(dev); |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 2398 | |
Alan Cox | 9f10969 | 2012-10-29 15:20:25 +0000 | [diff] [blame] | 2399 | uart_get_info(port, &tmp); |
Alan Cox | bebe73e | 2012-10-29 15:19:57 +0000 | [diff] [blame] | 2400 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16); |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
Alan Cox | 373bac4 | 2012-10-29 15:20:40 +0000 | [diff] [blame] | 2403 | static ssize_t uart_get_attr_type(struct device *dev, |
| 2404 | struct device_attribute *attr, char *buf) |
| 2405 | { |
| 2406 | struct serial_struct tmp; |
| 2407 | struct tty_port *port = dev_get_drvdata(dev); |
| 2408 | |
| 2409 | uart_get_info(port, &tmp); |
| 2410 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type); |
| 2411 | } |
| 2412 | static ssize_t uart_get_attr_line(struct device *dev, |
| 2413 | struct device_attribute *attr, char *buf) |
| 2414 | { |
| 2415 | struct serial_struct tmp; |
| 2416 | struct tty_port *port = dev_get_drvdata(dev); |
| 2417 | |
| 2418 | uart_get_info(port, &tmp); |
| 2419 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line); |
| 2420 | } |
| 2421 | |
| 2422 | static ssize_t uart_get_attr_port(struct device *dev, |
| 2423 | struct device_attribute *attr, char *buf) |
| 2424 | { |
| 2425 | struct serial_struct tmp; |
| 2426 | struct tty_port *port = dev_get_drvdata(dev); |
Andrew Morton | fd985e1 | 2012-11-26 15:47:15 -0800 | [diff] [blame] | 2427 | unsigned long ioaddr; |
Alan Cox | 373bac4 | 2012-10-29 15:20:40 +0000 | [diff] [blame] | 2428 | |
| 2429 | uart_get_info(port, &tmp); |
Andrew Morton | fd985e1 | 2012-11-26 15:47:15 -0800 | [diff] [blame] | 2430 | ioaddr = tmp.port; |
| 2431 | if (HIGH_BITS_OFFSET) |
| 2432 | ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET; |
| 2433 | return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr); |
Alan Cox | 373bac4 | 2012-10-29 15:20:40 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | static ssize_t uart_get_attr_irq(struct device *dev, |
| 2437 | struct device_attribute *attr, char *buf) |
| 2438 | { |
| 2439 | struct serial_struct tmp; |
| 2440 | struct tty_port *port = dev_get_drvdata(dev); |
| 2441 | |
| 2442 | uart_get_info(port, &tmp); |
| 2443 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq); |
| 2444 | } |
| 2445 | |
| 2446 | static ssize_t uart_get_attr_flags(struct device *dev, |
| 2447 | struct device_attribute *attr, char *buf) |
| 2448 | { |
| 2449 | struct serial_struct tmp; |
| 2450 | struct tty_port *port = dev_get_drvdata(dev); |
| 2451 | |
| 2452 | uart_get_info(port, &tmp); |
| 2453 | return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags); |
| 2454 | } |
| 2455 | |
| 2456 | static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev, |
| 2457 | struct device_attribute *attr, char *buf) |
| 2458 | { |
| 2459 | struct serial_struct tmp; |
| 2460 | struct tty_port *port = dev_get_drvdata(dev); |
| 2461 | |
| 2462 | uart_get_info(port, &tmp); |
| 2463 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size); |
| 2464 | } |
| 2465 | |
| 2466 | |
| 2467 | static ssize_t uart_get_attr_close_delay(struct device *dev, |
| 2468 | struct device_attribute *attr, char *buf) |
| 2469 | { |
| 2470 | struct serial_struct tmp; |
| 2471 | struct tty_port *port = dev_get_drvdata(dev); |
| 2472 | |
| 2473 | uart_get_info(port, &tmp); |
| 2474 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay); |
| 2475 | } |
| 2476 | |
| 2477 | |
| 2478 | static ssize_t uart_get_attr_closing_wait(struct device *dev, |
| 2479 | struct device_attribute *attr, char *buf) |
| 2480 | { |
| 2481 | struct serial_struct tmp; |
| 2482 | struct tty_port *port = dev_get_drvdata(dev); |
| 2483 | |
| 2484 | uart_get_info(port, &tmp); |
| 2485 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait); |
| 2486 | } |
| 2487 | |
| 2488 | static ssize_t uart_get_attr_custom_divisor(struct device *dev, |
| 2489 | struct device_attribute *attr, char *buf) |
| 2490 | { |
| 2491 | struct serial_struct tmp; |
| 2492 | struct tty_port *port = dev_get_drvdata(dev); |
| 2493 | |
| 2494 | uart_get_info(port, &tmp); |
| 2495 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor); |
| 2496 | } |
| 2497 | |
| 2498 | static ssize_t uart_get_attr_io_type(struct device *dev, |
| 2499 | struct device_attribute *attr, char *buf) |
| 2500 | { |
| 2501 | struct serial_struct tmp; |
| 2502 | struct tty_port *port = dev_get_drvdata(dev); |
| 2503 | |
| 2504 | uart_get_info(port, &tmp); |
| 2505 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type); |
| 2506 | } |
| 2507 | |
| 2508 | static ssize_t uart_get_attr_iomem_base(struct device *dev, |
| 2509 | struct device_attribute *attr, char *buf) |
| 2510 | { |
| 2511 | struct serial_struct tmp; |
| 2512 | struct tty_port *port = dev_get_drvdata(dev); |
| 2513 | |
| 2514 | uart_get_info(port, &tmp); |
| 2515 | return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base); |
| 2516 | } |
| 2517 | |
| 2518 | static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev, |
| 2519 | struct device_attribute *attr, char *buf) |
| 2520 | { |
| 2521 | struct serial_struct tmp; |
| 2522 | struct tty_port *port = dev_get_drvdata(dev); |
| 2523 | |
| 2524 | uart_get_info(port, &tmp); |
| 2525 | return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift); |
| 2526 | } |
| 2527 | |
| 2528 | static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL); |
| 2529 | static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL); |
| 2530 | static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL); |
| 2531 | static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL); |
| 2532 | static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL); |
| 2533 | static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL); |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 2534 | static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL); |
Alan Cox | 373bac4 | 2012-10-29 15:20:40 +0000 | [diff] [blame] | 2535 | static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL); |
| 2536 | static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL); |
| 2537 | static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL); |
| 2538 | static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL); |
| 2539 | static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL); |
| 2540 | static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL); |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 2541 | |
| 2542 | static struct attribute *tty_dev_attrs[] = { |
Alan Cox | 373bac4 | 2012-10-29 15:20:40 +0000 | [diff] [blame] | 2543 | &dev_attr_type.attr, |
| 2544 | &dev_attr_line.attr, |
| 2545 | &dev_attr_port.attr, |
| 2546 | &dev_attr_irq.attr, |
| 2547 | &dev_attr_flags.attr, |
| 2548 | &dev_attr_xmit_fifo_size.attr, |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 2549 | &dev_attr_uartclk.attr, |
Alan Cox | 373bac4 | 2012-10-29 15:20:40 +0000 | [diff] [blame] | 2550 | &dev_attr_close_delay.attr, |
| 2551 | &dev_attr_closing_wait.attr, |
| 2552 | &dev_attr_custom_divisor.attr, |
| 2553 | &dev_attr_io_type.attr, |
| 2554 | &dev_attr_iomem_base.attr, |
| 2555 | &dev_attr_iomem_reg_shift.attr, |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 2556 | NULL, |
| 2557 | }; |
| 2558 | |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 2559 | static const struct attribute_group tty_dev_attr_group = { |
Tomas Hlavacek | 6915c0e | 2012-09-06 03:17:18 +0200 | [diff] [blame] | 2560 | .attrs = tty_dev_attrs, |
| 2561 | }; |
| 2562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | /** |
| 2564 | * uart_add_one_port - attach a driver-defined port structure |
| 2565 | * @drv: pointer to the uart low level driver structure for this port |
Randy Dunlap | 1b9894f | 2009-09-21 11:12:03 -0700 | [diff] [blame] | 2566 | * @uport: uart port structure to use for this port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | * |
| 2568 | * This allows the driver to register its own uart_port structure |
| 2569 | * with the core driver. The main purpose is to allow the low |
| 2570 | * level uart drivers to expand uart_port, rather than having yet |
| 2571 | * more levels of structures. |
| 2572 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2573 | int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | { |
| 2575 | struct uart_state *state; |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2576 | struct tty_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | int ret = 0; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2578 | struct device *tty_dev; |
Greg Kroah-Hartman | 266dcff | 2014-07-16 01:19:34 +0000 | [diff] [blame] | 2579 | int num_groups; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | |
| 2581 | BUG_ON(in_interrupt()); |
| 2582 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2583 | if (uport->line >= drv->nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | return -EINVAL; |
| 2585 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2586 | state = drv->state + uport->line; |
| 2587 | port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2589 | mutex_lock(&port_mutex); |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2590 | mutex_lock(&port->mutex); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2591 | if (state->uart_port) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | ret = -EINVAL; |
| 2593 | goto out; |
| 2594 | } |
| 2595 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2596 | state->uart_port = uport; |
Linus Walleij | 6f538fe | 2012-12-07 11:36:08 +0100 | [diff] [blame] | 2597 | state->pm_state = UART_PM_STATE_UNDEFINED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2599 | uport->cons = drv->cons; |
| 2600 | uport->state = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 2602 | /* |
| 2603 | * If this port is a console, then the spinlock is already |
| 2604 | * initialised. |
| 2605 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2606 | if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) { |
| 2607 | spin_lock_init(&uport->lock); |
| 2608 | lockdep_set_class(&uport->lock, &port_lock_key); |
Ingo Molnar | 13e8359 | 2006-07-03 00:25:03 -0700 | [diff] [blame] | 2609 | } |
Grant Likely | a208ffd | 2014-03-27 18:29:46 -0700 | [diff] [blame] | 2610 | if (uport->cons && uport->dev) |
| 2611 | of_console_check(uport->dev->of_node, uport->cons->name, uport->line); |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 2612 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2613 | uart_configure_port(drv, state, uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | |
Greg Kroah-Hartman | 266dcff | 2014-07-16 01:19:34 +0000 | [diff] [blame] | 2615 | num_groups = 2; |
| 2616 | if (uport->attr_group) |
| 2617 | num_groups++; |
| 2618 | |
Yoshihiro YUNOMAE | c2b703b | 2014-07-23 06:06:22 +0000 | [diff] [blame] | 2619 | uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups), |
Greg Kroah-Hartman | 266dcff | 2014-07-16 01:19:34 +0000 | [diff] [blame] | 2620 | GFP_KERNEL); |
| 2621 | if (!uport->tty_groups) { |
| 2622 | ret = -ENOMEM; |
| 2623 | goto out; |
| 2624 | } |
| 2625 | uport->tty_groups[0] = &tty_dev_attr_group; |
| 2626 | if (uport->attr_group) |
| 2627 | uport->tty_groups[1] = uport->attr_group; |
| 2628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | /* |
| 2630 | * Register the port whether it's detected or not. This allows |
Geert Uytterhoeven | 015355b | 2014-03-11 11:23:36 +0100 | [diff] [blame] | 2631 | * setserial to be used to alter this port's parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | */ |
Tomas Hlavacek | b1b7991 | 2012-09-06 23:17:47 +0200 | [diff] [blame] | 2633 | tty_dev = tty_port_register_device_attr(port, drv->tty_driver, |
Greg Kroah-Hartman | 266dcff | 2014-07-16 01:19:34 +0000 | [diff] [blame] | 2634 | uport->line, uport->dev, port, uport->tty_groups); |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2635 | if (likely(!IS_ERR(tty_dev))) { |
Simon Glass | 7735983 | 2012-01-19 11:28:56 -0800 | [diff] [blame] | 2636 | device_set_wakeup_capable(tty_dev, 1); |
| 2637 | } else { |
Sudip Mukherjee | 2f2dafe | 2014-09-01 20:49:43 +0530 | [diff] [blame] | 2638 | dev_err(uport->dev, "Cannot register tty device on line %d\n", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2639 | uport->line); |
Simon Glass | 7735983 | 2012-01-19 11:28:56 -0800 | [diff] [blame] | 2640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | |
| 2642 | /* |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2643 | * Ensure UPF_DEAD is not set. |
| 2644 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2645 | uport->flags &= ~UPF_DEAD; |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | out: |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2648 | mutex_unlock(&port->mutex); |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2649 | mutex_unlock(&port_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | |
| 2651 | return ret; |
| 2652 | } |
| 2653 | |
| 2654 | /** |
| 2655 | * uart_remove_one_port - detach a driver defined port structure |
| 2656 | * @drv: pointer to the uart low level driver structure for this port |
Randy Dunlap | 1b9894f | 2009-09-21 11:12:03 -0700 | [diff] [blame] | 2657 | * @uport: uart port structure for this port |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | * |
| 2659 | * This unhooks (and hangs up) the specified port structure from the |
| 2660 | * core driver. No further calls will be made to the low-level code |
| 2661 | * for this port. |
| 2662 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2663 | int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | { |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2665 | struct uart_state *state = drv->state + uport->line; |
| 2666 | struct tty_port *port = &state->port; |
Geert Uytterhoeven | 4c6d5b4 | 2014-03-17 14:10:58 +0100 | [diff] [blame] | 2667 | struct tty_struct *tty; |
Chen Gang | b342dd5 | 2012-12-27 15:51:31 +0800 | [diff] [blame] | 2668 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | |
| 2670 | BUG_ON(in_interrupt()); |
| 2671 | |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2672 | if (state->uart_port != uport) |
Sudip Mukherjee | 2f2dafe | 2014-09-01 20:49:43 +0530 | [diff] [blame] | 2673 | dev_alert(uport->dev, "Removing wrong port: %p != %p\n", |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2674 | state->uart_port, uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2676 | mutex_lock(&port_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | |
| 2678 | /* |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2679 | * Mark the port "dead" - this prevents any opens from |
| 2680 | * succeeding while we shut down the port. |
| 2681 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2682 | mutex_lock(&port->mutex); |
Chen Gang | b342dd5 | 2012-12-27 15:51:31 +0800 | [diff] [blame] | 2683 | if (!state->uart_port) { |
| 2684 | mutex_unlock(&port->mutex); |
| 2685 | ret = -EINVAL; |
| 2686 | goto out; |
| 2687 | } |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2688 | uport->flags |= UPF_DEAD; |
| 2689 | mutex_unlock(&port->mutex); |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2690 | |
| 2691 | /* |
Greg Kroah-Hartman | aa4148c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2692 | * Remove the devices from the tty layer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2694 | tty_unregister_device(drv->tty_driver, uport->line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | |
Geert Uytterhoeven | 4c6d5b4 | 2014-03-17 14:10:58 +0100 | [diff] [blame] | 2696 | tty = tty_port_tty_get(port); |
| 2697 | if (tty) { |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2698 | tty_vhangup(port->tty); |
Geert Uytterhoeven | 4c6d5b4 | 2014-03-17 14:10:58 +0100 | [diff] [blame] | 2699 | tty_kref_put(tty); |
| 2700 | } |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2701 | |
| 2702 | /* |
Geert Uytterhoeven | 5f5c9ae | 2014-02-28 14:21:32 +0100 | [diff] [blame] | 2703 | * If the port is used as a console, unregister it |
| 2704 | */ |
| 2705 | if (uart_console(uport)) |
| 2706 | unregister_console(uport->cons); |
| 2707 | |
| 2708 | /* |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2709 | * Free the port IO and memory resources, if any. |
| 2710 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2711 | if (uport->type != PORT_UNKNOWN) |
| 2712 | uport->ops->release_port(uport); |
Greg Kroah-Hartman | 266dcff | 2014-07-16 01:19:34 +0000 | [diff] [blame] | 2713 | kfree(uport->tty_groups); |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2714 | |
| 2715 | /* |
| 2716 | * Indicate that there isn't a port here anymore. |
| 2717 | */ |
Alan Cox | a2bceae | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 2718 | uport->type = PORT_UNKNOWN; |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2719 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2720 | state->uart_port = NULL; |
Chen Gang | b342dd5 | 2012-12-27 15:51:31 +0800 | [diff] [blame] | 2721 | out: |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2722 | mutex_unlock(&port_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | |
Chen Gang | b342dd5 | 2012-12-27 15:51:31 +0800 | [diff] [blame] | 2724 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | /* |
| 2728 | * Are the two ports equivalent? |
| 2729 | */ |
| 2730 | int uart_match_port(struct uart_port *port1, struct uart_port *port2) |
| 2731 | { |
| 2732 | if (port1->iotype != port2->iotype) |
| 2733 | return 0; |
| 2734 | |
| 2735 | switch (port1->iotype) { |
| 2736 | case UPIO_PORT: |
| 2737 | return (port1->iobase == port2->iobase); |
| 2738 | case UPIO_HUB6: |
| 2739 | return (port1->iobase == port2->iobase) && |
| 2740 | (port1->hub6 == port2->hub6); |
| 2741 | case UPIO_MEM: |
Sergei Shtylyov | d21b55d | 2006-08-28 19:49:03 +0400 | [diff] [blame] | 2742 | case UPIO_MEM32: |
| 2743 | case UPIO_AU: |
| 2744 | case UPIO_TSI: |
Benjamin Herrenschmidt | 1624f00 | 2006-01-04 18:09:44 +0000 | [diff] [blame] | 2745 | return (port1->mapbase == port2->mapbase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | } |
| 2747 | return 0; |
| 2748 | } |
| 2749 | EXPORT_SYMBOL(uart_match_port); |
| 2750 | |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2751 | /** |
| 2752 | * uart_handle_dcd_change - handle a change of carrier detect state |
| 2753 | * @uport: uart_port structure for the open port |
| 2754 | * @status: new carrier detect status, nonzero if active |
Peter Hurley | 4d90bb1 | 2014-09-10 15:06:23 -0400 | [diff] [blame] | 2755 | * |
| 2756 | * Caller must hold uport->lock |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2757 | */ |
| 2758 | void uart_handle_dcd_change(struct uart_port *uport, unsigned int status) |
| 2759 | { |
George Spelvin | 4238157 | 2013-02-10 04:44:30 -0500 | [diff] [blame] | 2760 | struct tty_port *port = &uport->state->port; |
Alan Cox | 43eca0a | 2012-09-19 15:35:46 +0100 | [diff] [blame] | 2761 | struct tty_struct *tty = port->tty; |
Peter Hurley | c993257 | 2014-09-02 17:39:21 -0400 | [diff] [blame] | 2762 | struct tty_ldisc *ld; |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2763 | |
Peter Hurley | 4d90bb1 | 2014-09-10 15:06:23 -0400 | [diff] [blame] | 2764 | lockdep_assert_held_once(&uport->lock); |
| 2765 | |
Peter Hurley | c993257 | 2014-09-02 17:39:21 -0400 | [diff] [blame] | 2766 | if (tty) { |
| 2767 | ld = tty_ldisc_ref(tty); |
| 2768 | if (ld) { |
| 2769 | if (ld->ops->dcd_change) |
| 2770 | ld->ops->dcd_change(tty, status); |
| 2771 | tty_ldisc_deref(ld); |
| 2772 | } |
George Spelvin | 4238157 | 2013-02-10 04:44:30 -0500 | [diff] [blame] | 2773 | } |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2774 | |
| 2775 | uport->icount.dcd++; |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2776 | |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 2777 | if (uart_dcd_enabled(uport)) { |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2778 | if (status) |
| 2779 | wake_up_interruptible(&port->open_wait); |
Alan Cox | 43eca0a | 2012-09-19 15:35:46 +0100 | [diff] [blame] | 2780 | else if (tty) |
| 2781 | tty_hangup(tty); |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2782 | } |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2783 | } |
| 2784 | EXPORT_SYMBOL_GPL(uart_handle_dcd_change); |
| 2785 | |
| 2786 | /** |
| 2787 | * uart_handle_cts_change - handle a change of clear-to-send state |
| 2788 | * @uport: uart_port structure for the open port |
| 2789 | * @status: new clear to send status, nonzero if active |
Peter Hurley | 4d90bb1 | 2014-09-10 15:06:23 -0400 | [diff] [blame] | 2790 | * |
| 2791 | * Caller must hold uport->lock |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2792 | */ |
| 2793 | void uart_handle_cts_change(struct uart_port *uport, unsigned int status) |
| 2794 | { |
Peter Hurley | 4d90bb1 | 2014-09-10 15:06:23 -0400 | [diff] [blame] | 2795 | lockdep_assert_held_once(&uport->lock); |
| 2796 | |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2797 | uport->icount.cts++; |
| 2798 | |
Peter Hurley | 299245a | 2014-09-10 15:06:24 -0400 | [diff] [blame] | 2799 | if (uart_cts_enabled(uport)) { |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 2800 | if (uport->hw_stopped) { |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2801 | if (status) { |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 2802 | uport->hw_stopped = 0; |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2803 | uport->ops->start_tx(uport); |
| 2804 | uart_write_wakeup(uport); |
| 2805 | } |
| 2806 | } else { |
| 2807 | if (!status) { |
Peter Hurley | d01f4d1 | 2014-09-10 15:06:26 -0400 | [diff] [blame] | 2808 | uport->hw_stopped = 1; |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2809 | uport->ops->stop_tx(uport); |
| 2810 | } |
| 2811 | } |
| 2812 | } |
| 2813 | } |
| 2814 | EXPORT_SYMBOL_GPL(uart_handle_cts_change); |
| 2815 | |
Jiri Slaby | cf75525 | 2011-11-09 21:33:47 +0100 | [diff] [blame] | 2816 | /** |
| 2817 | * uart_insert_char - push a char to the uart layer |
| 2818 | * |
| 2819 | * User is responsible to call tty_flip_buffer_push when they are done with |
| 2820 | * insertion. |
| 2821 | * |
| 2822 | * @port: corresponding port |
| 2823 | * @status: state of the serial port RX buffer (LSR for 8250) |
| 2824 | * @overrun: mask of overrun bits in @status |
| 2825 | * @ch: character to push |
| 2826 | * @flag: flag for the character (see TTY_NORMAL and friends) |
| 2827 | */ |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2828 | void uart_insert_char(struct uart_port *port, unsigned int status, |
| 2829 | unsigned int overrun, unsigned int ch, unsigned int flag) |
| 2830 | { |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 2831 | struct tty_port *tport = &port->state->port; |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2832 | |
| 2833 | if ((status & port->ignore_status_mask & ~overrun) == 0) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 2834 | if (tty_insert_flip_char(tport, ch, flag) == 0) |
Corbin | dabfb35 | 2012-05-23 09:37:31 -0500 | [diff] [blame] | 2835 | ++port->icount.buf_overrun; |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2836 | |
| 2837 | /* |
| 2838 | * Overrun is special. Since it's reported immediately, |
| 2839 | * it doesn't affect the current character. |
| 2840 | */ |
| 2841 | if (status & ~port->ignore_status_mask & overrun) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 2842 | if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0) |
Corbin | dabfb35 | 2012-05-23 09:37:31 -0500 | [diff] [blame] | 2843 | ++port->icount.buf_overrun; |
Jiri Slaby | 027d7da | 2011-11-09 21:33:43 +0100 | [diff] [blame] | 2844 | } |
| 2845 | EXPORT_SYMBOL_GPL(uart_insert_char); |
| 2846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | EXPORT_SYMBOL(uart_write_wakeup); |
| 2848 | EXPORT_SYMBOL(uart_register_driver); |
| 2849 | EXPORT_SYMBOL(uart_unregister_driver); |
| 2850 | EXPORT_SYMBOL(uart_suspend_port); |
| 2851 | EXPORT_SYMBOL(uart_resume_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | EXPORT_SYMBOL(uart_add_one_port); |
| 2853 | EXPORT_SYMBOL(uart_remove_one_port); |
| 2854 | |
| 2855 | MODULE_DESCRIPTION("Serial driver core"); |
| 2856 | MODULE_LICENSE("GPL"); |