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