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