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