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