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