Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/core.c |
| 3 | * |
| 4 | * Driver core for serial ports |
| 5 | * |
| 6 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 7 | * |
| 8 | * Copyright 1999 ARM Limited |
| 9 | * Copyright (C) 2000-2001 Deep Blue Solutions Ltd. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/tty.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/console.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/serial_core.h> |
| 33 | #include <linux/smp_lock.h> |
| 34 | #include <linux/device.h> |
| 35 | #include <linux/serial.h> /* for serial_state and serial_icounter_struct */ |
| 36 | #include <linux/delay.h> |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include <asm/irq.h> |
| 40 | #include <asm/uaccess.h> |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* |
| 43 | * This is used to lock changes in serial line configuration. |
| 44 | */ |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 45 | static DEFINE_MUTEX(port_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Ingo Molnar | 13e8359 | 2006-07-03 00:25:03 -0700 | [diff] [blame] | 47 | /* |
| 48 | * lockdep: port->lock is initialized in two places, but we |
| 49 | * want only one lock-class: |
| 50 | */ |
| 51 | static struct lock_class_key port_lock_key; |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) |
| 54 | |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 55 | #define uart_users(state) ((state)->port.count + (state)->port.blocked_open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #ifdef CONFIG_SERIAL_CORE_CONSOLE |
| 58 | #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line) |
| 59 | #else |
| 60 | #define uart_console(port) (0) |
| 61 | #endif |
| 62 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 63 | static void uart_change_speed(struct uart_state *state, |
| 64 | struct ktermios *old_termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static void uart_wait_until_sent(struct tty_struct *tty, int timeout); |
| 66 | static void uart_change_pm(struct uart_state *state, int pm_state); |
| 67 | |
| 68 | /* |
| 69 | * This routine is used by the interrupt handler to schedule processing in |
| 70 | * the software interrupt portion of the driver. |
| 71 | */ |
| 72 | void uart_write_wakeup(struct uart_port *port) |
| 73 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 74 | struct uart_state *state = port->state; |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 75 | /* |
| 76 | * This means you called this function _after_ the port was |
| 77 | * closed. No cookie for you. |
| 78 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 79 | BUG_ON(!state); |
| 80 | tasklet_schedule(&state->tlet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void uart_stop(struct tty_struct *tty) |
| 84 | { |
| 85 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 86 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | unsigned long flags; |
| 88 | |
| 89 | spin_lock_irqsave(&port->lock, flags); |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 90 | port->ops->stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | spin_unlock_irqrestore(&port->lock, flags); |
| 92 | } |
| 93 | |
| 94 | static void __uart_start(struct tty_struct *tty) |
| 95 | { |
| 96 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 97 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 99 | if (!uart_circ_empty(&state->xmit) && state->xmit.buf && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | !tty->stopped && !tty->hw_stopped) |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 101 | port->ops->start_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void uart_start(struct tty_struct *tty) |
| 105 | { |
| 106 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 107 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | unsigned long flags; |
| 109 | |
| 110 | spin_lock_irqsave(&port->lock, flags); |
| 111 | __uart_start(tty); |
| 112 | spin_unlock_irqrestore(&port->lock, flags); |
| 113 | } |
| 114 | |
| 115 | static void uart_tasklet_action(unsigned long data) |
| 116 | { |
| 117 | struct uart_state *state = (struct uart_state *)data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 118 | tty_wakeup(state->port.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static inline void |
| 122 | uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear) |
| 123 | { |
| 124 | unsigned long flags; |
| 125 | unsigned int old; |
| 126 | |
| 127 | spin_lock_irqsave(&port->lock, flags); |
| 128 | old = port->mctrl; |
| 129 | port->mctrl = (old & ~clear) | set; |
| 130 | if (old != port->mctrl) |
| 131 | port->ops->set_mctrl(port, port->mctrl); |
| 132 | spin_unlock_irqrestore(&port->lock, flags); |
| 133 | } |
| 134 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 135 | #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0) |
| 136 | #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Startup the port. This will be called once per open. All calls |
Alan Cox | df4f4dd | 2008-07-16 21:53:50 +0100 | [diff] [blame] | 140 | * will be serialised by the per-port mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | */ |
| 142 | static int uart_startup(struct uart_state *state, int init_hw) |
| 143 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 144 | struct uart_port *uport = state->uart_port; |
| 145 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | unsigned long page; |
| 147 | int retval = 0; |
| 148 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 149 | if (state->flags & UIF_INITIALIZED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return 0; |
| 151 | |
| 152 | /* |
| 153 | * Set the TTY IO error marker - we will only clear this |
| 154 | * once we have successfully opened the port. Also set |
| 155 | * up the tty->alt_speed kludge |
| 156 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 157 | set_bit(TTY_IO_ERROR, &port->tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 159 | if (uport->type == PORT_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return 0; |
| 161 | |
| 162 | /* |
| 163 | * Initialise and allocate the transmit and temporary |
| 164 | * buffer. |
| 165 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 166 | if (!state->xmit.buf) { |
Alan Cox | df4f4dd | 2008-07-16 21:53:50 +0100 | [diff] [blame] | 167 | /* This is protected by the per port mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | page = get_zeroed_page(GFP_KERNEL); |
| 169 | if (!page) |
| 170 | return -ENOMEM; |
| 171 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 172 | state->xmit.buf = (unsigned char *) page; |
| 173 | uart_circ_clear(&state->xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 176 | retval = uport->ops->startup(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (retval == 0) { |
| 178 | if (init_hw) { |
| 179 | /* |
| 180 | * Initialise the hardware port settings. |
| 181 | */ |
| 182 | uart_change_speed(state, NULL); |
| 183 | |
| 184 | /* |
| 185 | * Setup the RTS and DTR signals once the |
| 186 | * port is open and ready to respond. |
| 187 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 188 | if (port->tty->termios->c_cflag & CBAUD) |
| 189 | uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 192 | if (state->flags & UIF_CTS_FLOW) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 193 | spin_lock_irq(&uport->lock); |
| 194 | if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS)) |
| 195 | port->tty->hw_stopped = 1; |
| 196 | spin_unlock_irq(&uport->lock); |
Russell King | 0dd7a1a | 2005-06-29 18:40:53 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 199 | state->flags |= UIF_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 201 | clear_bit(TTY_IO_ERROR, &port->tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (retval && capable(CAP_SYS_ADMIN)) |
| 205 | retval = 0; |
| 206 | |
| 207 | return retval; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * This routine will shutdown a serial port; interrupts are disabled, and |
| 212 | * DTR is dropped if the hangup on close termio flag is on. Calls to |
| 213 | * uart_shutdown are serialised by the per-port semaphore. |
| 214 | */ |
| 215 | static void uart_shutdown(struct uart_state *state) |
| 216 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 217 | struct uart_port *port = state->uart_port; |
| 218 | struct tty_struct *tty = state->port.tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 220 | /* |
| 221 | * Set the TTY IO error marker |
| 222 | */ |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 223 | if (tty) |
| 224 | set_bit(TTY_IO_ERROR, &tty->flags); |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 225 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 226 | if (state->flags & UIF_INITIALIZED) { |
| 227 | state->flags &= ~UIF_INITIALIZED; |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * Turn off DTR and RTS early. |
| 231 | */ |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 232 | if (!tty || (tty->termios->c_cflag & HUPCL)) |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 233 | uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS); |
| 234 | |
| 235 | /* |
| 236 | * clear delta_msr_wait queue to avoid mem leaks: we may free |
| 237 | * the irq here so the queue might never be woken up. Note |
| 238 | * that we won't end up waiting on delta_msr_wait again since |
| 239 | * any outstanding file descriptors should be pointing at |
| 240 | * hung_up_tty_fops now. |
| 241 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 242 | wake_up_interruptible(&state->delta_msr_wait); |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * Free the IRQ and disable the port. |
| 246 | */ |
| 247 | port->ops->shutdown(port); |
| 248 | |
| 249 | /* |
| 250 | * Ensure that the IRQ handler isn't running on another CPU. |
| 251 | */ |
| 252 | synchronize_irq(port->irq); |
| 253 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | /* |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 256 | * kill off our tasklet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 258 | tasklet_kill(&state->tlet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * Free the transmit buffer page. |
| 262 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 263 | if (state->xmit.buf) { |
| 264 | free_page((unsigned long)state->xmit.buf); |
| 265 | state->xmit.buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * uart_update_timeout - update per-port FIFO timeout. |
| 271 | * @port: uart_port structure describing the port |
| 272 | * @cflag: termios cflag value |
| 273 | * @baud: speed of the port |
| 274 | * |
| 275 | * Set the port FIFO timeout value. The @cflag value should |
| 276 | * reflect the actual hardware settings. |
| 277 | */ |
| 278 | void |
| 279 | uart_update_timeout(struct uart_port *port, unsigned int cflag, |
| 280 | unsigned int baud) |
| 281 | { |
| 282 | unsigned int bits; |
| 283 | |
| 284 | /* byte size and parity */ |
| 285 | switch (cflag & CSIZE) { |
| 286 | case CS5: |
| 287 | bits = 7; |
| 288 | break; |
| 289 | case CS6: |
| 290 | bits = 8; |
| 291 | break; |
| 292 | case CS7: |
| 293 | bits = 9; |
| 294 | break; |
| 295 | default: |
| 296 | bits = 10; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 297 | break; /* CS8 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (cflag & CSTOPB) |
| 301 | bits++; |
| 302 | if (cflag & PARENB) |
| 303 | bits++; |
| 304 | |
| 305 | /* |
| 306 | * The total number of bits to be transmitted in the fifo. |
| 307 | */ |
| 308 | bits = bits * port->fifosize; |
| 309 | |
| 310 | /* |
| 311 | * Figure the timeout to send the above number of bits. |
| 312 | * Add .02 seconds of slop |
| 313 | */ |
| 314 | port->timeout = (HZ * bits) / baud + HZ/50; |
| 315 | } |
| 316 | |
| 317 | EXPORT_SYMBOL(uart_update_timeout); |
| 318 | |
| 319 | /** |
| 320 | * uart_get_baud_rate - return baud rate for a particular port |
| 321 | * @port: uart_port structure describing the port in question. |
| 322 | * @termios: desired termios settings. |
| 323 | * @old: old termios (or NULL) |
| 324 | * @min: minimum acceptable baud rate |
| 325 | * @max: maximum acceptable baud rate |
| 326 | * |
| 327 | * Decode the termios structure into a numeric baud rate, |
| 328 | * taking account of the magic 38400 baud rate (with spd_* |
| 329 | * flags), and mapping the %B0 rate to 9600 baud. |
| 330 | * |
| 331 | * If the new baud rate is invalid, try the old termios setting. |
| 332 | * If it's still invalid, we try 9600 baud. |
| 333 | * |
| 334 | * Update the @termios structure to reflect the baud rate |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 335 | * we're actually going to be using. Don't do this for the case |
| 336 | * where B0 is requested ("hang up"). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | */ |
| 338 | unsigned int |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 339 | uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, |
| 340 | struct ktermios *old, unsigned int min, unsigned int max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | unsigned int try, baud, altbaud = 38400; |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 343 | int hung_up = 0; |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 344 | upf_t flags = port->flags & UPF_SPD_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | if (flags == UPF_SPD_HI) |
| 347 | altbaud = 57600; |
| 348 | if (flags == UPF_SPD_VHI) |
| 349 | altbaud = 115200; |
| 350 | if (flags == UPF_SPD_SHI) |
| 351 | altbaud = 230400; |
| 352 | if (flags == UPF_SPD_WARP) |
| 353 | altbaud = 460800; |
| 354 | |
| 355 | for (try = 0; try < 2; try++) { |
| 356 | baud = tty_termios_baud_rate(termios); |
| 357 | |
| 358 | /* |
| 359 | * The spd_hi, spd_vhi, spd_shi, spd_warp kludge... |
| 360 | * Die! Die! Die! |
| 361 | */ |
| 362 | if (baud == 38400) |
| 363 | baud = altbaud; |
| 364 | |
| 365 | /* |
| 366 | * Special case: B0 rate. |
| 367 | */ |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 368 | if (baud == 0) { |
| 369 | hung_up = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | baud = 9600; |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | if (baud >= min && baud <= max) |
| 374 | return baud; |
| 375 | |
| 376 | /* |
| 377 | * Oops, the quotient was zero. Try again with |
| 378 | * the old baud rate if possible. |
| 379 | */ |
| 380 | termios->c_cflag &= ~CBAUD; |
| 381 | if (old) { |
Alan Cox | 6d4d67b | 2008-02-04 22:27:53 -0800 | [diff] [blame] | 382 | baud = tty_termios_baud_rate(old); |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 383 | if (!hung_up) |
| 384 | tty_termios_encode_baud_rate(termios, |
| 385 | baud, baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | old = NULL; |
| 387 | continue; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * As a last resort, if the quotient is zero, |
| 392 | * default to 9600 bps |
| 393 | */ |
Alan Cox | eb424fd | 2008-04-28 02:14:07 -0700 | [diff] [blame] | 394 | if (!hung_up) |
| 395 | tty_termios_encode_baud_rate(termios, 9600, 9600); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | EXPORT_SYMBOL(uart_get_baud_rate); |
| 402 | |
| 403 | /** |
| 404 | * uart_get_divisor - return uart clock divisor |
| 405 | * @port: uart_port structure describing the port. |
| 406 | * @baud: desired baud rate |
| 407 | * |
| 408 | * Calculate the uart clock divisor for the port. |
| 409 | */ |
| 410 | unsigned int |
| 411 | uart_get_divisor(struct uart_port *port, unsigned int baud) |
| 412 | { |
| 413 | unsigned int quot; |
| 414 | |
| 415 | /* |
| 416 | * Old custom speed handling. |
| 417 | */ |
| 418 | if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) |
| 419 | quot = port->custom_divisor; |
| 420 | else |
| 421 | quot = (port->uartclk + (8 * baud)) / (16 * baud); |
| 422 | |
| 423 | return quot; |
| 424 | } |
| 425 | |
| 426 | EXPORT_SYMBOL(uart_get_divisor); |
| 427 | |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 428 | /* FIXME: Consistent locking policy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 430 | uart_change_speed(struct uart_state *state, struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 432 | struct tty_struct *tty = state->port.tty; |
| 433 | struct uart_port *port = state->uart_port; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 434 | struct ktermios *termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * If we have no tty, termios, or the port does not exist, |
| 438 | * then we can't set the parameters for this port. |
| 439 | */ |
| 440 | if (!tty || !tty->termios || port->type == PORT_UNKNOWN) |
| 441 | return; |
| 442 | |
| 443 | termios = tty->termios; |
| 444 | |
| 445 | /* |
| 446 | * Set flags based on termios cflag |
| 447 | */ |
| 448 | if (termios->c_cflag & CRTSCTS) |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 449 | state->flags |= UIF_CTS_FLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | else |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 451 | state->flags &= ~UIF_CTS_FLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | if (termios->c_cflag & CLOCAL) |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 454 | state->flags &= ~UIF_CHECK_CD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | else |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 456 | state->flags |= UIF_CHECK_CD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | port->ops->set_termios(port, termios, old_termios); |
| 459 | } |
| 460 | |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 461 | static inline int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | __uart_put_char(struct uart_port *port, struct circ_buf *circ, unsigned char c) |
| 463 | { |
| 464 | unsigned long flags; |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 465 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | if (!circ->buf) |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 468 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | spin_lock_irqsave(&port->lock, flags); |
| 471 | if (uart_circ_chars_free(circ) != 0) { |
| 472 | circ->buf[circ->head] = c; |
| 473 | circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1); |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 474 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | spin_unlock_irqrestore(&port->lock, flags); |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 477 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Alan Cox | 23d22ce | 2008-04-30 00:54:11 -0700 | [diff] [blame] | 480 | static int uart_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
| 482 | struct uart_state *state = tty->driver_data; |
| 483 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 484 | return __uart_put_char(state->uart_port, &state->xmit, ch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static void uart_flush_chars(struct tty_struct *tty) |
| 488 | { |
| 489 | uart_start(tty); |
| 490 | } |
| 491 | |
| 492 | static int |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 493 | uart_write(struct tty_struct *tty, const unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
| 495 | struct uart_state *state = tty->driver_data; |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 496 | struct uart_port *port; |
| 497 | struct circ_buf *circ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | unsigned long flags; |
| 499 | int c, ret = 0; |
| 500 | |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 501 | /* |
| 502 | * This means you called this function _after_ the port was |
| 503 | * closed. No cookie for you. |
| 504 | */ |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 505 | if (!state) { |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 506 | WARN_ON(1); |
| 507 | return -EL3HLT; |
| 508 | } |
| 509 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 510 | port = state->uart_port; |
| 511 | circ = &state->xmit; |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (!circ->buf) |
| 514 | return 0; |
| 515 | |
| 516 | spin_lock_irqsave(&port->lock, flags); |
| 517 | while (1) { |
| 518 | c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE); |
| 519 | if (count < c) |
| 520 | c = count; |
| 521 | if (c <= 0) |
| 522 | break; |
| 523 | memcpy(circ->buf + circ->head, buf, c); |
| 524 | circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1); |
| 525 | buf += c; |
| 526 | count -= c; |
| 527 | ret += c; |
| 528 | } |
| 529 | spin_unlock_irqrestore(&port->lock, flags); |
| 530 | |
| 531 | uart_start(tty); |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | static int uart_write_room(struct tty_struct *tty) |
| 536 | { |
| 537 | struct uart_state *state = tty->driver_data; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 538 | unsigned long flags; |
| 539 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 541 | spin_lock_irqsave(&state->uart_port->lock, flags); |
| 542 | ret = uart_circ_chars_free(&state->xmit); |
| 543 | spin_unlock_irqrestore(&state->uart_port->lock, flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 544 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static int uart_chars_in_buffer(struct tty_struct *tty) |
| 548 | { |
| 549 | struct uart_state *state = tty->driver_data; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 550 | unsigned long flags; |
| 551 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 553 | spin_lock_irqsave(&state->uart_port->lock, flags); |
| 554 | ret = uart_circ_chars_pending(&state->xmit); |
| 555 | spin_unlock_irqrestore(&state->uart_port->lock, flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 556 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static void uart_flush_buffer(struct tty_struct *tty) |
| 560 | { |
| 561 | struct uart_state *state = tty->driver_data; |
Tetsuo Handa | 55d7b68 | 2008-05-06 20:42:27 -0700 | [diff] [blame] | 562 | struct uart_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | unsigned long flags; |
| 564 | |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 565 | /* |
| 566 | * This means you called this function _after_ the port was |
| 567 | * closed. No cookie for you. |
| 568 | */ |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 569 | if (!state) { |
Pavel Machek | d5f735e | 2006-03-07 21:55:20 -0800 | [diff] [blame] | 570 | WARN_ON(1); |
| 571 | return; |
| 572 | } |
| 573 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 574 | port = state->uart_port; |
Jiri Slaby | eb3a1e1 | 2007-05-06 14:48:52 -0700 | [diff] [blame] | 575 | pr_debug("uart_flush_buffer(%d) called\n", tty->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 578 | uart_circ_clear(&state->xmit); |
Haavard Skinnemoen | 6bb0e3a | 2008-07-16 21:52:36 +0100 | [diff] [blame] | 579 | if (port->ops->flush_buffer) |
| 580 | port->ops->flush_buffer(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | spin_unlock_irqrestore(&port->lock, flags); |
| 582 | tty_wakeup(tty); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * This function is used to send a high-priority XON/XOFF character to |
| 587 | * the device |
| 588 | */ |
| 589 | static void uart_send_xchar(struct tty_struct *tty, char ch) |
| 590 | { |
| 591 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 592 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | unsigned long flags; |
| 594 | |
| 595 | if (port->ops->send_xchar) |
| 596 | port->ops->send_xchar(port, ch); |
| 597 | else { |
| 598 | port->x_char = ch; |
| 599 | if (ch) { |
| 600 | spin_lock_irqsave(&port->lock, flags); |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 601 | port->ops->start_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | spin_unlock_irqrestore(&port->lock, flags); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static void uart_throttle(struct tty_struct *tty) |
| 608 | { |
| 609 | struct uart_state *state = tty->driver_data; |
| 610 | |
| 611 | if (I_IXOFF(tty)) |
| 612 | uart_send_xchar(tty, STOP_CHAR(tty)); |
| 613 | |
| 614 | if (tty->termios->c_cflag & CRTSCTS) |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 615 | uart_clear_mctrl(state->uart_port, TIOCM_RTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static void uart_unthrottle(struct tty_struct *tty) |
| 619 | { |
| 620 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 621 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | if (I_IXOFF(tty)) { |
| 624 | if (port->x_char) |
| 625 | port->x_char = 0; |
| 626 | else |
| 627 | uart_send_xchar(tty, START_CHAR(tty)); |
| 628 | } |
| 629 | |
| 630 | if (tty->termios->c_cflag & CRTSCTS) |
| 631 | uart_set_mctrl(port, TIOCM_RTS); |
| 632 | } |
| 633 | |
| 634 | static int uart_get_info(struct uart_state *state, |
| 635 | struct serial_struct __user *retinfo) |
| 636 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 637 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | struct serial_struct tmp; |
| 639 | |
| 640 | memset(&tmp, 0, sizeof(tmp)); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 641 | |
| 642 | /* Ensure the state we copy is consistent and no hardware changes |
| 643 | occur as we go */ |
| 644 | mutex_lock(&state->mutex); |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | tmp.type = port->type; |
| 647 | tmp.line = port->line; |
| 648 | tmp.port = port->iobase; |
| 649 | if (HIGH_BITS_OFFSET) |
| 650 | tmp.port_high = (long) port->iobase >> HIGH_BITS_OFFSET; |
| 651 | tmp.irq = port->irq; |
| 652 | tmp.flags = port->flags; |
| 653 | tmp.xmit_fifo_size = port->fifosize; |
| 654 | tmp.baud_base = port->uartclk / 16; |
Alan Cox | 5e99df5 | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 655 | tmp.close_delay = state->port.close_delay / 10; |
| 656 | tmp.closing_wait = state->port.closing_wait == USF_CLOSING_WAIT_NONE ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | ASYNC_CLOSING_WAIT_NONE : |
Alan Cox | 5e99df5 | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 658 | state->port.closing_wait / 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | tmp.custom_divisor = port->custom_divisor; |
| 660 | tmp.hub6 = port->hub6; |
| 661 | tmp.io_type = port->iotype; |
| 662 | tmp.iomem_reg_shift = port->regshift; |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 663 | tmp.iomem_base = (void *)(unsigned long)port->mapbase; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 665 | mutex_unlock(&state->mutex); |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 668 | return -EFAULT; |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static int uart_set_info(struct uart_state *state, |
| 673 | struct serial_struct __user *newinfo) |
| 674 | { |
| 675 | struct serial_struct new_serial; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 676 | struct uart_port *uport = state->uart_port; |
| 677 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | unsigned long new_port; |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 679 | unsigned int change_irq, change_port, closing_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | unsigned int old_custom_divisor, close_delay; |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 681 | upf_t old_flags, new_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | int retval = 0; |
| 683 | |
| 684 | if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) |
| 685 | return -EFAULT; |
| 686 | |
| 687 | new_port = new_serial.port; |
| 688 | if (HIGH_BITS_OFFSET) |
| 689 | new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET; |
| 690 | |
| 691 | new_serial.irq = irq_canonicalize(new_serial.irq); |
| 692 | close_delay = new_serial.close_delay * 10; |
| 693 | closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? |
| 694 | USF_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; |
| 695 | |
| 696 | /* |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 697 | * This semaphore protects port->count. It is also |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | * very useful to prevent opens. Also, take the |
| 699 | * port configuration semaphore to make sure that a |
| 700 | * module insertion/removal doesn't change anything |
| 701 | * under us. |
| 702 | */ |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 703 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 705 | change_irq = !(uport->flags & UPF_FIXED_PORT) |
| 706 | && new_serial.irq != uport->irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
| 708 | /* |
| 709 | * Since changing the 'type' of the port changes its resource |
| 710 | * allocations, we should treat type changes the same as |
| 711 | * IO port changes. |
| 712 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 713 | change_port = !(uport->flags & UPF_FIXED_PORT) |
| 714 | && (new_port != uport->iobase || |
| 715 | (unsigned long)new_serial.iomem_base != uport->mapbase || |
| 716 | new_serial.hub6 != uport->hub6 || |
| 717 | new_serial.io_type != uport->iotype || |
| 718 | new_serial.iomem_reg_shift != uport->regshift || |
| 719 | new_serial.type != uport->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 721 | old_flags = uport->flags; |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 722 | new_flags = new_serial.flags; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 723 | old_custom_divisor = uport->custom_divisor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | if (!capable(CAP_SYS_ADMIN)) { |
| 726 | retval = -EPERM; |
| 727 | if (change_irq || change_port || |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 728 | (new_serial.baud_base != uport->uartclk / 16) || |
| 729 | (close_delay != port->close_delay) || |
| 730 | (closing_wait != port->closing_wait) || |
Russell King | 947deee | 2006-07-02 20:45:51 +0100 | [diff] [blame] | 731 | (new_serial.xmit_fifo_size && |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 732 | new_serial.xmit_fifo_size != uport->fifosize) || |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 733 | (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | goto exit; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 735 | uport->flags = ((uport->flags & ~UPF_USR_MASK) | |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 736 | (new_flags & UPF_USR_MASK)); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 737 | uport->custom_divisor = new_serial.custom_divisor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | goto check_and_exit; |
| 739 | } |
| 740 | |
| 741 | /* |
| 742 | * Ask the low level driver to verify the settings. |
| 743 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 744 | if (uport->ops->verify_port) |
| 745 | retval = uport->ops->verify_port(uport, &new_serial); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Yinghai Lu | a62c413 | 2008-08-19 20:49:55 -0700 | [diff] [blame] | 747 | if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | (new_serial.baud_base < 9600)) |
| 749 | retval = -EINVAL; |
| 750 | |
| 751 | if (retval) |
| 752 | goto exit; |
| 753 | |
| 754 | if (change_port || change_irq) { |
| 755 | retval = -EBUSY; |
| 756 | |
| 757 | /* |
| 758 | * Make sure that we are the sole user of this port. |
| 759 | */ |
| 760 | if (uart_users(state) > 1) |
| 761 | goto exit; |
| 762 | |
| 763 | /* |
| 764 | * We need to shutdown the serial port at the old |
| 765 | * port/type/irq combination. |
| 766 | */ |
| 767 | uart_shutdown(state); |
| 768 | } |
| 769 | |
| 770 | if (change_port) { |
| 771 | unsigned long old_iobase, old_mapbase; |
| 772 | unsigned int old_type, old_iotype, old_hub6, old_shift; |
| 773 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 774 | old_iobase = uport->iobase; |
| 775 | old_mapbase = uport->mapbase; |
| 776 | old_type = uport->type; |
| 777 | old_hub6 = uport->hub6; |
| 778 | old_iotype = uport->iotype; |
| 779 | old_shift = uport->regshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | /* |
| 782 | * Free and release old regions |
| 783 | */ |
| 784 | if (old_type != PORT_UNKNOWN) |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 785 | uport->ops->release_port(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 787 | uport->iobase = new_port; |
| 788 | uport->type = new_serial.type; |
| 789 | uport->hub6 = new_serial.hub6; |
| 790 | uport->iotype = new_serial.io_type; |
| 791 | uport->regshift = new_serial.iomem_reg_shift; |
| 792 | uport->mapbase = (unsigned long)new_serial.iomem_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | /* |
| 795 | * Claim and map the new regions |
| 796 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 797 | if (uport->type != PORT_UNKNOWN) { |
| 798 | retval = uport->ops->request_port(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } else { |
| 800 | /* Always success - Jean II */ |
| 801 | retval = 0; |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * If we fail to request resources for the |
| 806 | * new port, try to restore the old settings. |
| 807 | */ |
| 808 | if (retval && old_type != PORT_UNKNOWN) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 809 | uport->iobase = old_iobase; |
| 810 | uport->type = old_type; |
| 811 | uport->hub6 = old_hub6; |
| 812 | uport->iotype = old_iotype; |
| 813 | uport->regshift = old_shift; |
| 814 | uport->mapbase = old_mapbase; |
| 815 | retval = uport->ops->request_port(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | /* |
| 817 | * If we failed to restore the old settings, |
| 818 | * we fail like this. |
| 819 | */ |
| 820 | if (retval) |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 821 | uport->type = PORT_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | /* |
| 824 | * We failed anyway. |
| 825 | */ |
| 826 | retval = -EBUSY; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 827 | /* Added to return the correct error -Ram Gupta */ |
| 828 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
David Gibson | abb4a23 | 2007-05-06 14:48:49 -0700 | [diff] [blame] | 832 | if (change_irq) |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 833 | uport->irq = new_serial.irq; |
| 834 | if (!(uport->flags & UPF_FIXED_PORT)) |
| 835 | uport->uartclk = new_serial.baud_base * 16; |
| 836 | uport->flags = (uport->flags & ~UPF_CHANGE_MASK) | |
Russell King | 0077d45 | 2006-01-21 23:03:28 +0000 | [diff] [blame] | 837 | (new_flags & UPF_CHANGE_MASK); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 838 | uport->custom_divisor = new_serial.custom_divisor; |
| 839 | port->close_delay = close_delay; |
| 840 | port->closing_wait = closing_wait; |
Russell King | 947deee | 2006-07-02 20:45:51 +0100 | [diff] [blame] | 841 | if (new_serial.xmit_fifo_size) |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 842 | uport->fifosize = new_serial.xmit_fifo_size; |
| 843 | if (port->tty) |
| 844 | port->tty->low_latency = |
| 845 | (uport->flags & UPF_LOW_LATENCY) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
| 847 | check_and_exit: |
| 848 | retval = 0; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 849 | if (uport->type == PORT_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | goto exit; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 851 | if (state->flags & UIF_INITIALIZED) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 852 | if (((old_flags ^ uport->flags) & UPF_SPD_MASK) || |
| 853 | old_custom_divisor != uport->custom_divisor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | /* |
| 855 | * If they're setting up a custom divisor or speed, |
| 856 | * instead of clearing it, then bitch about it. No |
| 857 | * need to rate-limit; it's CAP_SYS_ADMIN only. |
| 858 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 859 | if (uport->flags & UPF_SPD_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | char buf[64]; |
| 861 | printk(KERN_NOTICE |
| 862 | "%s sets custom speed on %s. This " |
| 863 | "is deprecated.\n", current->comm, |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 864 | tty_name(port->tty, buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | } |
| 866 | uart_change_speed(state, NULL); |
| 867 | } |
| 868 | } else |
| 869 | retval = uart_startup(state, 1); |
| 870 | exit: |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 871 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | return retval; |
| 873 | } |
| 874 | |
| 875 | |
| 876 | /* |
| 877 | * uart_get_lsr_info - get line status register info. |
| 878 | * Note: uart_ioctl protects us against hangups. |
| 879 | */ |
| 880 | static int uart_get_lsr_info(struct uart_state *state, |
| 881 | unsigned int __user *value) |
| 882 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 883 | struct uart_port *uport = state->uart_port; |
| 884 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | unsigned int result; |
| 886 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 887 | result = uport->ops->tx_empty(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | /* |
| 890 | * If we're about to load something into the transmit |
| 891 | * register, we'll pretend the transmitter isn't empty to |
| 892 | * avoid a race condition (depending on when the transmit |
| 893 | * interrupt happens). |
| 894 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 895 | if (uport->x_char || |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 896 | ((uart_circ_chars_pending(&state->xmit) > 0) && |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 897 | !port->tty->stopped && !port->tty->hw_stopped)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | result &= ~TIOCSER_TEMT; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | return put_user(result, value); |
| 901 | } |
| 902 | |
| 903 | static int uart_tiocmget(struct tty_struct *tty, struct file *file) |
| 904 | { |
| 905 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 906 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | int result = -EIO; |
| 908 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 909 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | if ((!file || !tty_hung_up_p(file)) && |
| 911 | !(tty->flags & (1 << TTY_IO_ERROR))) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 912 | result = uport->mctrl; |
Russell King | c5f4644 | 2005-06-29 09:42:38 +0100 | [diff] [blame] | 913 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 914 | spin_lock_irq(&uport->lock); |
| 915 | result |= uport->ops->get_mctrl(uport); |
| 916 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 918 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
| 920 | return result; |
| 921 | } |
| 922 | |
| 923 | static int |
| 924 | uart_tiocmset(struct tty_struct *tty, struct file *file, |
| 925 | unsigned int set, unsigned int clear) |
| 926 | { |
| 927 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 928 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | int ret = -EIO; |
| 930 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 931 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | if ((!file || !tty_hung_up_p(file)) && |
| 933 | !(tty->flags & (1 << TTY_IO_ERROR))) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 934 | uart_update_mctrl(uport, set, clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | ret = 0; |
| 936 | } |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 937 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | return ret; |
| 939 | } |
| 940 | |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 941 | static int uart_break_ctl(struct tty_struct *tty, int break_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | { |
| 943 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 944 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 946 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 948 | if (uport->type != PORT_UNKNOWN) |
| 949 | uport->ops->break_ctl(uport, break_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 951 | mutex_unlock(&state->mutex); |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 952 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | static int uart_do_autoconfig(struct uart_state *state) |
| 956 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 957 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | int flags, ret; |
| 959 | |
| 960 | if (!capable(CAP_SYS_ADMIN)) |
| 961 | return -EPERM; |
| 962 | |
| 963 | /* |
| 964 | * Take the per-port semaphore. This prevents count from |
| 965 | * changing, and hence any extra opens of the port while |
| 966 | * we're auto-configuring. |
| 967 | */ |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 968 | if (mutex_lock_interruptible(&state->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | return -ERESTARTSYS; |
| 970 | |
| 971 | ret = -EBUSY; |
| 972 | if (uart_users(state) == 1) { |
| 973 | uart_shutdown(state); |
| 974 | |
| 975 | /* |
| 976 | * If we already have a port type configured, |
| 977 | * we must release its resources. |
| 978 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 979 | if (uport->type != PORT_UNKNOWN) |
| 980 | uport->ops->release_port(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
| 982 | flags = UART_CONFIG_TYPE; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 983 | if (uport->flags & UPF_AUTO_IRQ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | flags |= UART_CONFIG_IRQ; |
| 985 | |
| 986 | /* |
| 987 | * This will claim the ports resources if |
| 988 | * a port is found. |
| 989 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 990 | uport->ops->config_port(uport, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | ret = uart_startup(state, 1); |
| 993 | } |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 994 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change |
| 1000 | * - mask passed in arg for lines of interest |
| 1001 | * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) |
| 1002 | * Caller should use TIOCGICOUNT to see which one it was |
| 1003 | */ |
| 1004 | static int |
| 1005 | uart_wait_modem_status(struct uart_state *state, unsigned long arg) |
| 1006 | { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1007 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | DECLARE_WAITQUEUE(wait, current); |
| 1009 | struct uart_icount cprev, cnow; |
| 1010 | int ret; |
| 1011 | |
| 1012 | /* |
| 1013 | * note the counters on entry |
| 1014 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1015 | spin_lock_irq(&uport->lock); |
| 1016 | memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | /* |
| 1019 | * Force modem status interrupts on |
| 1020 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1021 | uport->ops->enable_ms(uport); |
| 1022 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1024 | add_wait_queue(&state->delta_msr_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | for (;;) { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1026 | spin_lock_irq(&uport->lock); |
| 1027 | memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); |
| 1028 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
| 1030 | set_current_state(TASK_INTERRUPTIBLE); |
| 1031 | |
| 1032 | if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || |
| 1033 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || |
| 1034 | ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || |
| 1035 | ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1036 | ret = 0; |
| 1037 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | schedule(); |
| 1041 | |
| 1042 | /* see if a signal did it */ |
| 1043 | if (signal_pending(current)) { |
| 1044 | ret = -ERESTARTSYS; |
| 1045 | break; |
| 1046 | } |
| 1047 | |
| 1048 | cprev = cnow; |
| 1049 | } |
| 1050 | |
| 1051 | current->state = TASK_RUNNING; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1052 | remove_wait_queue(&state->delta_msr_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) |
| 1059 | * Return: write counters to the user passed counter struct |
| 1060 | * NB: both 1->0 and 0->1 transitions are counted except for |
| 1061 | * RI where only 0->1 is counted. |
| 1062 | */ |
| 1063 | static int uart_get_count(struct uart_state *state, |
| 1064 | struct serial_icounter_struct __user *icnt) |
| 1065 | { |
| 1066 | struct serial_icounter_struct icount; |
| 1067 | struct uart_icount cnow; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1068 | struct uart_port *uport = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1070 | spin_lock_irq(&uport->lock); |
| 1071 | memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); |
| 1072 | spin_unlock_irq(&uport->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
| 1074 | icount.cts = cnow.cts; |
| 1075 | icount.dsr = cnow.dsr; |
| 1076 | icount.rng = cnow.rng; |
| 1077 | icount.dcd = cnow.dcd; |
| 1078 | icount.rx = cnow.rx; |
| 1079 | icount.tx = cnow.tx; |
| 1080 | icount.frame = cnow.frame; |
| 1081 | icount.overrun = cnow.overrun; |
| 1082 | icount.parity = cnow.parity; |
| 1083 | icount.brk = cnow.brk; |
| 1084 | icount.buf_overrun = cnow.buf_overrun; |
| 1085 | |
| 1086 | return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
Alan Cox | e523844 | 2008-04-30 00:53:28 -0700 | [diff] [blame] | 1090 | * Called via sys_ioctl. We can use spin_lock_irq() here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | */ |
| 1092 | static int |
| 1093 | uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, |
| 1094 | unsigned long arg) |
| 1095 | { |
| 1096 | struct uart_state *state = tty->driver_data; |
| 1097 | void __user *uarg = (void __user *)arg; |
| 1098 | int ret = -ENOIOCTLCMD; |
| 1099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
| 1101 | /* |
| 1102 | * These ioctls don't rely on the hardware to be present. |
| 1103 | */ |
| 1104 | switch (cmd) { |
| 1105 | case TIOCGSERIAL: |
| 1106 | ret = uart_get_info(state, uarg); |
| 1107 | break; |
| 1108 | |
| 1109 | case TIOCSSERIAL: |
| 1110 | ret = uart_set_info(state, uarg); |
| 1111 | break; |
| 1112 | |
| 1113 | case TIOCSERCONFIG: |
| 1114 | ret = uart_do_autoconfig(state); |
| 1115 | break; |
| 1116 | |
| 1117 | case TIOCSERGWILD: /* obsolete */ |
| 1118 | case TIOCSERSWILD: /* obsolete */ |
| 1119 | ret = 0; |
| 1120 | break; |
| 1121 | } |
| 1122 | |
| 1123 | if (ret != -ENOIOCTLCMD) |
| 1124 | goto out; |
| 1125 | |
| 1126 | if (tty->flags & (1 << TTY_IO_ERROR)) { |
| 1127 | ret = -EIO; |
| 1128 | goto out; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * The following should only be used when hardware is present. |
| 1133 | */ |
| 1134 | switch (cmd) { |
| 1135 | case TIOCMIWAIT: |
| 1136 | ret = uart_wait_modem_status(state, arg); |
| 1137 | break; |
| 1138 | |
| 1139 | case TIOCGICOUNT: |
| 1140 | ret = uart_get_count(state, uarg); |
| 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | if (ret != -ENOIOCTLCMD) |
| 1145 | goto out; |
| 1146 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1147 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | |
| 1149 | if (tty_hung_up_p(filp)) { |
| 1150 | ret = -EIO; |
| 1151 | goto out_up; |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | * All these rely on hardware being present and need to be |
| 1156 | * protected against the tty being hung up. |
| 1157 | */ |
| 1158 | switch (cmd) { |
| 1159 | case TIOCSERGETLSR: /* Get line status register */ |
| 1160 | ret = uart_get_lsr_info(state, uarg); |
| 1161 | break; |
| 1162 | |
| 1163 | default: { |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1164 | struct uart_port *uport = state->uart_port; |
| 1165 | if (uport->ops->ioctl) |
| 1166 | ret = uport->ops->ioctl(uport, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | break; |
| 1168 | } |
| 1169 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1170 | out_up: |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1171 | mutex_unlock(&state->mutex); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1172 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | return ret; |
| 1174 | } |
| 1175 | |
Linus Torvalds | edeb280 | 2008-06-04 10:35:03 -0700 | [diff] [blame] | 1176 | static void uart_set_ldisc(struct tty_struct *tty) |
Alan Cox | 64e9159 | 2008-06-03 15:18:54 +0100 | [diff] [blame] | 1177 | { |
| 1178 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1179 | struct uart_port *uport = state->uart_port; |
Alan Cox | 64e9159 | 2008-06-03 15:18:54 +0100 | [diff] [blame] | 1180 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1181 | if (uport->ops->set_ldisc) |
| 1182 | uport->ops->set_ldisc(uport); |
Alan Cox | 64e9159 | 2008-06-03 15:18:54 +0100 | [diff] [blame] | 1183 | } |
| 1184 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1185 | static void uart_set_termios(struct tty_struct *tty, |
| 1186 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | { |
| 1188 | struct uart_state *state = tty->driver_data; |
| 1189 | unsigned long flags; |
| 1190 | unsigned int cflag = tty->termios->c_cflag; |
| 1191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
| 1193 | /* |
| 1194 | * These are the bits that are used to setup various |
David Woodhouse | 20620d6 | 2007-08-22 14:01:11 -0700 | [diff] [blame] | 1195 | * flags in the low level driver. We can ignore the Bfoo |
| 1196 | * bits in c_cflag; c_[io]speed will always be set |
| 1197 | * appropriately by set_termios() in tty_ioctl.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | */ |
| 1199 | #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | if ((cflag ^ old_termios->c_cflag) == 0 && |
David Woodhouse | 20620d6 | 2007-08-22 14:01:11 -0700 | [diff] [blame] | 1201 | tty->termios->c_ospeed == old_termios->c_ospeed && |
| 1202 | tty->termios->c_ispeed == old_termios->c_ispeed && |
Alan Cox | e523844 | 2008-04-30 00:53:28 -0700 | [diff] [blame] | 1203 | RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | return; |
Alan Cox | e523844 | 2008-04-30 00:53:28 -0700 | [diff] [blame] | 1205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
| 1207 | uart_change_speed(state, old_termios); |
| 1208 | |
| 1209 | /* Handle transition to B0 status */ |
| 1210 | if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1211 | uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
| 1213 | /* Handle transition away from B0 status */ |
| 1214 | if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { |
| 1215 | unsigned int mask = TIOCM_DTR; |
| 1216 | if (!(cflag & CRTSCTS) || |
| 1217 | !test_bit(TTY_THROTTLED, &tty->flags)) |
| 1218 | mask |= TIOCM_RTS; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1219 | uart_set_mctrl(state->uart_port, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /* Handle turning off CRTSCTS */ |
| 1223 | if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1224 | spin_lock_irqsave(&state->uart_port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | tty->hw_stopped = 0; |
| 1226 | __uart_start(tty); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1227 | spin_unlock_irqrestore(&state->uart_port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Russell King | 0dd7a1a | 2005-06-29 18:40:53 +0100 | [diff] [blame] | 1230 | /* Handle turning on CRTSCTS */ |
| 1231 | if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1232 | spin_lock_irqsave(&state->uart_port->lock, flags); |
| 1233 | if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) { |
Russell King | 0dd7a1a | 2005-06-29 18:40:53 +0100 | [diff] [blame] | 1234 | tty->hw_stopped = 1; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1235 | state->uart_port->ops->stop_tx(state->uart_port); |
Russell King | 0dd7a1a | 2005-06-29 18:40:53 +0100 | [diff] [blame] | 1236 | } |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1237 | spin_unlock_irqrestore(&state->uart_port->lock, flags); |
Russell King | 0dd7a1a | 2005-06-29 18:40:53 +0100 | [diff] [blame] | 1238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | #if 0 |
| 1240 | /* |
| 1241 | * No need to wake up processes in open wait, since they |
| 1242 | * sample the CLOCAL flag once, and don't recheck it. |
| 1243 | * XXX It's not clear whether the current behavior is correct |
| 1244 | * or not. Hence, this may change..... |
| 1245 | */ |
| 1246 | if (!(old_termios->c_cflag & CLOCAL) && |
| 1247 | (tty->termios->c_cflag & CLOCAL)) |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1248 | wake_up_interruptible(&state->uart_port.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | #endif |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * In 2.4.5, calls to this will be serialized via the BKL in |
| 1254 | * linux/drivers/char/tty_io.c:tty_release() |
| 1255 | * linux/drivers/char/tty_io.c:do_tty_handup() |
| 1256 | */ |
| 1257 | static void uart_close(struct tty_struct *tty, struct file *filp) |
| 1258 | { |
| 1259 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1260 | struct tty_port *port; |
| 1261 | struct uart_port *uport; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | BUG_ON(!kernel_locked()); |
| 1264 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1265 | uport = state->uart_port; |
| 1266 | port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1268 | pr_debug("uart_close(%d) called\n", uport->line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1270 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
| 1272 | if (tty_hung_up_p(filp)) |
| 1273 | goto done; |
| 1274 | |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1275 | if ((tty->count == 1) && (port->count != 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | /* |
| 1277 | * Uh, oh. tty->count is 1, which means that the tty |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1278 | * structure will be freed. port->count should always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | * be one in these conditions. If it's greater than |
| 1280 | * one, we've got real problems, since it means the |
| 1281 | * serial port won't be shutdown. |
| 1282 | */ |
| 1283 | printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, " |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1284 | "port->count is %d\n", port->count); |
| 1285 | port->count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | } |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1287 | if (--port->count < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n", |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1289 | tty->name, port->count); |
| 1290 | port->count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1292 | if (port->count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | goto done; |
| 1294 | |
| 1295 | /* |
| 1296 | * Now we wait for the transmit buffer to clear; and we notify |
| 1297 | * the line discipline to only process XON/XOFF characters by |
| 1298 | * setting tty->closing. |
| 1299 | */ |
| 1300 | tty->closing = 1; |
| 1301 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1302 | if (port->closing_wait != USF_CLOSING_WAIT_NONE) |
| 1303 | tty_wait_until_sent(tty, msecs_to_jiffies(port->closing_wait)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
| 1305 | /* |
| 1306 | * At this point, we stop accepting input. To do this, we |
| 1307 | * disable the receive line status interrupts. |
| 1308 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1309 | if (state->flags & UIF_INITIALIZED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | unsigned long flags; |
| 1311 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1312 | uport->ops->stop_rx(uport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | spin_unlock_irqrestore(&port->lock, flags); |
| 1314 | /* |
| 1315 | * Before we drop DTR, make sure the UART transmitter |
| 1316 | * has completely drained; this is especially |
| 1317 | * important if there is a transmit FIFO! |
| 1318 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1319 | uart_wait_until_sent(tty, uport->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | uart_shutdown(state); |
| 1323 | uart_flush_buffer(tty); |
| 1324 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1325 | tty_ldisc_flush(tty); |
| 1326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | tty->closing = 0; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1328 | port->tty = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1330 | if (port->blocked_open) { |
| 1331 | if (port->close_delay) |
| 1332 | msleep_interruptible(port->close_delay); |
| 1333 | } else if (!uart_console(uport)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | uart_change_pm(state, 3); |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Wake up anyone trying to open this port. |
| 1339 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1340 | state->flags &= ~UIF_NORMAL_ACTIVE; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1341 | wake_up_interruptible(&port->open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1343 | done: |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1344 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | static void uart_wait_until_sent(struct tty_struct *tty, int timeout) |
| 1348 | { |
| 1349 | struct uart_state *state = tty->driver_data; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1350 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | unsigned long char_time, expire; |
| 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | if (port->type == PORT_UNKNOWN || port->fifosize == 0) |
| 1354 | return; |
| 1355 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1356 | lock_kernel(); |
| 1357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | /* |
| 1359 | * Set the check interval to be 1/5 of the estimated time to |
| 1360 | * send a single character, and make it at least 1. The check |
| 1361 | * interval should also be less than the timeout. |
| 1362 | * |
| 1363 | * Note: we have to use pretty tight timings here to satisfy |
| 1364 | * the NIST-PCTS. |
| 1365 | */ |
| 1366 | char_time = (port->timeout - HZ/50) / port->fifosize; |
| 1367 | char_time = char_time / 5; |
| 1368 | if (char_time == 0) |
| 1369 | char_time = 1; |
| 1370 | if (timeout && timeout < char_time) |
| 1371 | char_time = timeout; |
| 1372 | |
| 1373 | /* |
| 1374 | * If the transmitter hasn't cleared in twice the approximate |
| 1375 | * amount of time to send the entire FIFO, it probably won't |
| 1376 | * ever clear. This assumes the UART isn't doing flow |
| 1377 | * control, which is currently the case. Hence, if it ever |
| 1378 | * takes longer than port->timeout, this is probably due to a |
| 1379 | * UART bug of some kind. So, we clamp the timeout parameter at |
| 1380 | * 2*port->timeout. |
| 1381 | */ |
| 1382 | if (timeout == 0 || timeout > 2 * port->timeout) |
| 1383 | timeout = 2 * port->timeout; |
| 1384 | |
| 1385 | expire = jiffies + timeout; |
| 1386 | |
Jiri Slaby | eb3a1e1 | 2007-05-06 14:48:52 -0700 | [diff] [blame] | 1387 | pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n", |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1388 | port->line, jiffies, expire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
| 1390 | /* |
| 1391 | * Check whether the transmitter is empty every 'char_time'. |
| 1392 | * 'timeout' / 'expire' give us the maximum amount of time |
| 1393 | * we wait. |
| 1394 | */ |
| 1395 | while (!port->ops->tx_empty(port)) { |
| 1396 | msleep_interruptible(jiffies_to_msecs(char_time)); |
| 1397 | if (signal_pending(current)) |
| 1398 | break; |
| 1399 | if (time_after(jiffies, expire)) |
| 1400 | break; |
| 1401 | } |
| 1402 | set_current_state(TASK_RUNNING); /* might not be needed */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1403 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * This is called with the BKL held in |
| 1408 | * linux/drivers/char/tty_io.c:do_tty_hangup() |
| 1409 | * We're called from the eventd thread, so we can sleep for |
| 1410 | * a _short_ time only. |
| 1411 | */ |
| 1412 | static void uart_hangup(struct tty_struct *tty) |
| 1413 | { |
| 1414 | struct uart_state *state = tty->driver_data; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1415 | struct tty_port *port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | BUG_ON(!kernel_locked()); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1418 | pr_debug("uart_hangup(%d)\n", state->uart_port->line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1420 | mutex_lock(&state->mutex); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1421 | if (state->flags & UIF_NORMAL_ACTIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | uart_flush_buffer(tty); |
| 1423 | uart_shutdown(state); |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1424 | port->count = 0; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1425 | state->flags &= ~UIF_NORMAL_ACTIVE; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1426 | port->tty = NULL; |
| 1427 | wake_up_interruptible(&port->open_wait); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1428 | wake_up_interruptible(&state->delta_msr_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | } |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1430 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | * Copy across the serial console cflag setting into the termios settings |
| 1435 | * for the initial open of the port. This allows continuity between the |
| 1436 | * kernel settings, and the settings init adopts when it opens the port |
| 1437 | * for the first time. |
| 1438 | */ |
| 1439 | static void uart_update_termios(struct uart_state *state) |
| 1440 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1441 | struct tty_struct *tty = state->port.tty; |
| 1442 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
| 1444 | if (uart_console(port) && port->cons->cflag) { |
| 1445 | tty->termios->c_cflag = port->cons->cflag; |
| 1446 | port->cons->cflag = 0; |
| 1447 | } |
| 1448 | |
| 1449 | /* |
| 1450 | * If the device failed to grab its irq resources, |
| 1451 | * or some other error occurred, don't try to talk |
| 1452 | * to the port hardware. |
| 1453 | */ |
| 1454 | if (!(tty->flags & (1 << TTY_IO_ERROR))) { |
| 1455 | /* |
| 1456 | * Make termios settings take effect. |
| 1457 | */ |
| 1458 | uart_change_speed(state, NULL); |
| 1459 | |
| 1460 | /* |
| 1461 | * And finally enable the RTS and DTR signals. |
| 1462 | */ |
| 1463 | if (tty->termios->c_cflag & CBAUD) |
| 1464 | uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Block the open until the port is ready. We must be called with |
| 1470 | * the per-port semaphore held. |
| 1471 | */ |
| 1472 | static int |
| 1473 | uart_block_til_ready(struct file *filp, struct uart_state *state) |
| 1474 | { |
| 1475 | DECLARE_WAITQUEUE(wait, current); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1476 | struct uart_port *uport = state->uart_port; |
| 1477 | struct tty_port *port = &state->port; |
Russell King | c5f4644 | 2005-06-29 09:42:38 +0100 | [diff] [blame] | 1478 | unsigned int mctrl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1480 | port->blocked_open++; |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1481 | port->count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1483 | add_wait_queue(&port->open_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | while (1) { |
| 1485 | set_current_state(TASK_INTERRUPTIBLE); |
| 1486 | |
| 1487 | /* |
| 1488 | * If we have been hung up, tell userspace/restart open. |
| 1489 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1490 | if (tty_hung_up_p(filp) || port->tty == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | break; |
| 1492 | |
| 1493 | /* |
| 1494 | * If the port has been closed, tell userspace/restart open. |
| 1495 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1496 | if (!(state->flags & UIF_INITIALIZED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | break; |
| 1498 | |
| 1499 | /* |
| 1500 | * If non-blocking mode is set, or CLOCAL mode is set, |
| 1501 | * we don't want to wait for the modem status lines to |
| 1502 | * indicate that the port is ready. |
| 1503 | * |
| 1504 | * Also, if the port is not enabled/configured, we want |
| 1505 | * to allow the open to succeed here. Note that we will |
| 1506 | * have set TTY_IO_ERROR for a non-existant port. |
| 1507 | */ |
| 1508 | if ((filp->f_flags & O_NONBLOCK) || |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1509 | (port->tty->termios->c_cflag & CLOCAL) || |
| 1510 | (port->tty->flags & (1 << TTY_IO_ERROR))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | |
| 1513 | /* |
| 1514 | * Set DTR to allow modem to know we're waiting. Do |
| 1515 | * not set RTS here - we want to make sure we catch |
| 1516 | * the data from the modem. |
| 1517 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1518 | if (port->tty->termios->c_cflag & CBAUD) |
| 1519 | uart_set_mctrl(uport, TIOCM_DTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
| 1521 | /* |
| 1522 | * and wait for the carrier to indicate that the |
| 1523 | * modem is ready for us. |
| 1524 | */ |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1525 | spin_lock_irq(&uport->lock); |
| 1526 | uport->ops->enable_ms(uport); |
| 1527 | mctrl = uport->ops->get_mctrl(uport); |
| 1528 | spin_unlock_irq(&uport->lock); |
Russell King | c5f4644 | 2005-06-29 09:42:38 +0100 | [diff] [blame] | 1529 | if (mctrl & TIOCM_CAR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | break; |
| 1531 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1532 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | schedule(); |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1534 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
| 1536 | if (signal_pending(current)) |
| 1537 | break; |
| 1538 | } |
| 1539 | set_current_state(TASK_RUNNING); |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1540 | remove_wait_queue(&port->open_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1542 | port->count++; |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1543 | port->blocked_open--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
| 1545 | if (signal_pending(current)) |
| 1546 | return -ERESTARTSYS; |
| 1547 | |
Alan Cox | 46d57a4 | 2009-09-19 13:13:29 -0700 | [diff] [blame] | 1548 | if (!port->tty || tty_hung_up_p(filp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | return -EAGAIN; |
| 1550 | |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | static struct uart_state *uart_get(struct uart_driver *drv, int line) |
| 1555 | { |
| 1556 | struct uart_state *state; |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 1557 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | state = drv->state + line; |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1560 | if (mutex_lock_interruptible(&state->mutex)) { |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 1561 | ret = -ERESTARTSYS; |
| 1562 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1565 | state->port.count++; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1566 | if (!state->uart_port || state->uart_port->flags & UPF_DEAD) { |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 1567 | ret = -ENXIO; |
| 1568 | goto err_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | return state; |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 1571 | |
| 1572 | err_unlock: |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1573 | state->port.count--; |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 1574 | mutex_unlock(&state->mutex); |
| 1575 | err: |
| 1576 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
Denis Cheng | 922f9cf | 2008-02-08 04:22:00 -0800 | [diff] [blame] | 1580 | * calls to uart_open are serialised by the BKL in |
| 1581 | * fs/char_dev.c:chrdev_open() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | * Note that if this fails, then uart_close() _will_ be called. |
| 1583 | * |
| 1584 | * In time, we want to scrap the "opening nonpresent ports" |
| 1585 | * behaviour and implement an alternative way for setserial |
| 1586 | * to set base addresses/ports/types. This will allow us to |
| 1587 | * get rid of a certain amount of extra tests. |
| 1588 | */ |
| 1589 | static int uart_open(struct tty_struct *tty, struct file *filp) |
| 1590 | { |
| 1591 | struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state; |
| 1592 | struct uart_state *state; |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1593 | struct tty_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | int retval, line = tty->index; |
| 1595 | |
| 1596 | BUG_ON(!kernel_locked()); |
Jiri Slaby | eb3a1e1 | 2007-05-06 14:48:52 -0700 | [diff] [blame] | 1597 | pr_debug("uart_open(%d) called\n", line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | |
| 1599 | /* |
| 1600 | * tty->driver->num won't change, so we won't fail here with |
| 1601 | * tty->driver_data set to something non-NULL (and therefore |
| 1602 | * we won't get caught by uart_close()). |
| 1603 | */ |
| 1604 | retval = -ENODEV; |
| 1605 | if (line >= tty->driver->num) |
| 1606 | goto fail; |
| 1607 | |
| 1608 | /* |
| 1609 | * We take the semaphore inside uart_get to guarantee that we won't |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1610 | * be re-entered while allocating the state structure, or while we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | * request any IRQs that the driver may need. This also has the nice |
| 1612 | * side-effect that it delays the action of uart_hangup, so we can |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1613 | * guarantee that state->port.tty will always contain something |
| 1614 | * reasonable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | */ |
| 1616 | state = uart_get(drv, line); |
| 1617 | if (IS_ERR(state)) { |
| 1618 | retval = PTR_ERR(state); |
| 1619 | goto fail; |
| 1620 | } |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1621 | port = &state->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
| 1623 | /* |
| 1624 | * Once we set tty->driver_data here, we are guaranteed that |
| 1625 | * uart_close() will decrement the driver module use count. |
| 1626 | * Any failures from here onwards should not touch the count. |
| 1627 | */ |
| 1628 | tty->driver_data = state; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1629 | state->uart_port->state = state; |
| 1630 | tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | tty->alt_speed = 0; |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1632 | port->tty = tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | |
| 1634 | /* |
| 1635 | * If the port is in the middle of closing, bail out now. |
| 1636 | */ |
| 1637 | if (tty_hung_up_p(filp)) { |
| 1638 | retval = -EAGAIN; |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1639 | port->count--; |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1640 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | goto fail; |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * Make sure the device is in D0 state. |
| 1646 | */ |
Alan Cox | 91312cd | 2009-09-19 13:13:29 -0700 | [diff] [blame^] | 1647 | if (port->count == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | uart_change_pm(state, 0); |
| 1649 | |
| 1650 | /* |
| 1651 | * Start up the serial port. |
| 1652 | */ |
| 1653 | retval = uart_startup(state, 0); |
| 1654 | |
| 1655 | /* |
| 1656 | * If we succeeded, wait until the port is ready. |
| 1657 | */ |
| 1658 | if (retval == 0) |
| 1659 | retval = uart_block_til_ready(filp, state); |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1660 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
| 1662 | /* |
| 1663 | * If this is the first open to succeed, adjust things to suit. |
| 1664 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1665 | if (retval == 0 && !(state->flags & UIF_NORMAL_ACTIVE)) { |
| 1666 | state->flags |= UIF_NORMAL_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | |
| 1668 | uart_update_termios(state); |
| 1669 | } |
| 1670 | |
| 1671 | fail: |
| 1672 | return retval; |
| 1673 | } |
| 1674 | |
| 1675 | static const char *uart_type(struct uart_port *port) |
| 1676 | { |
| 1677 | const char *str = NULL; |
| 1678 | |
| 1679 | if (port->ops->type) |
| 1680 | str = port->ops->type(port); |
| 1681 | |
| 1682 | if (!str) |
| 1683 | str = "unknown"; |
| 1684 | |
| 1685 | return str; |
| 1686 | } |
| 1687 | |
| 1688 | #ifdef CONFIG_PROC_FS |
| 1689 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1690 | 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] | 1691 | { |
| 1692 | struct uart_state *state = drv->state + i; |
George G. Davis | 3689a0e | 2007-02-14 00:33:06 -0800 | [diff] [blame] | 1693 | int pm_state; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1694 | struct uart_port *port = state->uart_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | char stat_buf[32]; |
| 1696 | unsigned int status; |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1697 | int mmio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | |
| 1699 | if (!port) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1700 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
Sergei Shtylyov | 6c6a233 | 2006-09-04 00:04:20 +0400 | [diff] [blame] | 1702 | mmio = port->iotype >= UPIO_MEM; |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1703 | seq_printf(m, "%d: uart:%s %s%08llX irq:%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | port->line, uart_type(port), |
Sergei Shtylyov | 6c6a233 | 2006-09-04 00:04:20 +0400 | [diff] [blame] | 1705 | mmio ? "mmio:0x" : "port:", |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 1706 | mmio ? (unsigned long long)port->mapbase |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1707 | : (unsigned long long) port->iobase, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | port->irq); |
| 1709 | |
| 1710 | if (port->type == PORT_UNKNOWN) { |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1711 | seq_putc(m, '\n'); |
| 1712 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1715 | if (capable(CAP_SYS_ADMIN)) { |
George G. Davis | 3689a0e | 2007-02-14 00:33:06 -0800 | [diff] [blame] | 1716 | mutex_lock(&state->mutex); |
| 1717 | pm_state = state->pm_state; |
| 1718 | if (pm_state) |
| 1719 | uart_change_pm(state, 0); |
Russell King | c5f4644 | 2005-06-29 09:42:38 +0100 | [diff] [blame] | 1720 | spin_lock_irq(&port->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | status = port->ops->get_mctrl(port); |
Russell King | c5f4644 | 2005-06-29 09:42:38 +0100 | [diff] [blame] | 1722 | spin_unlock_irq(&port->lock); |
George G. Davis | 3689a0e | 2007-02-14 00:33:06 -0800 | [diff] [blame] | 1723 | if (pm_state) |
| 1724 | uart_change_pm(state, pm_state); |
| 1725 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1727 | seq_printf(m, " tx:%d rx:%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | port->icount.tx, port->icount.rx); |
| 1729 | if (port->icount.frame) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1730 | seq_printf(m, " fe:%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | port->icount.frame); |
| 1732 | if (port->icount.parity) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1733 | seq_printf(m, " pe:%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | port->icount.parity); |
| 1735 | if (port->icount.brk) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1736 | seq_printf(m, " brk:%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | port->icount.brk); |
| 1738 | if (port->icount.overrun) |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1739 | seq_printf(m, " oe:%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | port->icount.overrun); |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1741 | |
| 1742 | #define INFOBIT(bit, str) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | if (port->mctrl & (bit)) \ |
| 1744 | strncat(stat_buf, (str), sizeof(stat_buf) - \ |
| 1745 | strlen(stat_buf) - 2) |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1746 | #define STATBIT(bit, str) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | if (status & (bit)) \ |
| 1748 | strncat(stat_buf, (str), sizeof(stat_buf) - \ |
| 1749 | strlen(stat_buf) - 2) |
| 1750 | |
| 1751 | stat_buf[0] = '\0'; |
| 1752 | stat_buf[1] = '\0'; |
| 1753 | INFOBIT(TIOCM_RTS, "|RTS"); |
| 1754 | STATBIT(TIOCM_CTS, "|CTS"); |
| 1755 | INFOBIT(TIOCM_DTR, "|DTR"); |
| 1756 | STATBIT(TIOCM_DSR, "|DSR"); |
| 1757 | STATBIT(TIOCM_CAR, "|CD"); |
| 1758 | STATBIT(TIOCM_RNG, "|RI"); |
| 1759 | if (stat_buf[0]) |
| 1760 | stat_buf[0] = ' '; |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 1761 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1762 | seq_puts(m, stat_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1764 | seq_putc(m, '\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | #undef STATBIT |
| 1766 | #undef INFOBIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1769 | static int uart_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | { |
Alexey Dobriyan | 833bb30 | 2009-04-02 01:30:04 +0400 | [diff] [blame] | 1771 | struct tty_driver *ttydrv = m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | struct uart_driver *drv = ttydrv->driver_state; |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1773 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1775 | seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | "", "", ""); |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1777 | for (i = 0; i < drv->nr; i++) |
| 1778 | uart_line_info(m, drv, i); |
| 1779 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | } |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 1781 | |
| 1782 | static int uart_proc_open(struct inode *inode, struct file *file) |
| 1783 | { |
| 1784 | return single_open(file, uart_proc_show, PDE(inode)->data); |
| 1785 | } |
| 1786 | |
| 1787 | static const struct file_operations uart_proc_fops = { |
| 1788 | .owner = THIS_MODULE, |
| 1789 | .open = uart_proc_open, |
| 1790 | .read = seq_read, |
| 1791 | .llseek = seq_lseek, |
| 1792 | .release = single_release, |
| 1793 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | #endif |
| 1795 | |
Andrew Morton | 4a1b550 | 2008-03-07 15:51:16 -0800 | [diff] [blame] | 1796 | #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | /* |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 1798 | * uart_console_write - write a console message to a serial port |
| 1799 | * @port: the port to write the message |
| 1800 | * @s: array of characters |
| 1801 | * @count: number of characters in string to write |
| 1802 | * @write: function to write character to port |
| 1803 | */ |
| 1804 | void uart_console_write(struct uart_port *port, const char *s, |
| 1805 | unsigned int count, |
| 1806 | void (*putchar)(struct uart_port *, int)) |
| 1807 | { |
| 1808 | unsigned int i; |
| 1809 | |
| 1810 | for (i = 0; i < count; i++, s++) { |
| 1811 | if (*s == '\n') |
| 1812 | putchar(port, '\r'); |
| 1813 | putchar(port, *s); |
| 1814 | } |
| 1815 | } |
| 1816 | EXPORT_SYMBOL_GPL(uart_console_write); |
| 1817 | |
| 1818 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | * Check whether an invalid uart number has been specified, and |
| 1820 | * if so, search for the first available port that does have |
| 1821 | * console support. |
| 1822 | */ |
| 1823 | struct uart_port * __init |
| 1824 | uart_get_console(struct uart_port *ports, int nr, struct console *co) |
| 1825 | { |
| 1826 | int idx = co->index; |
| 1827 | |
| 1828 | if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 && |
| 1829 | ports[idx].membase == NULL)) |
| 1830 | for (idx = 0; idx < nr; idx++) |
| 1831 | if (ports[idx].iobase != 0 || |
| 1832 | ports[idx].membase != NULL) |
| 1833 | break; |
| 1834 | |
| 1835 | co->index = idx; |
| 1836 | |
| 1837 | return ports + idx; |
| 1838 | } |
| 1839 | |
| 1840 | /** |
| 1841 | * uart_parse_options - Parse serial port baud/parity/bits/flow contro. |
| 1842 | * @options: pointer to option string |
| 1843 | * @baud: pointer to an 'int' variable for the baud rate. |
| 1844 | * @parity: pointer to an 'int' variable for the parity. |
| 1845 | * @bits: pointer to an 'int' variable for the number of data bits. |
| 1846 | * @flow: pointer to an 'int' variable for the flow control character. |
| 1847 | * |
| 1848 | * uart_parse_options decodes a string containing the serial console |
| 1849 | * options. The format of the string is <baud><parity><bits><flow>, |
| 1850 | * eg: 115200n8r |
| 1851 | */ |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1852 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow) |
| 1854 | { |
| 1855 | char *s = options; |
| 1856 | |
| 1857 | *baud = simple_strtoul(s, NULL, 10); |
| 1858 | while (*s >= '0' && *s <= '9') |
| 1859 | s++; |
| 1860 | if (*s) |
| 1861 | *parity = *s++; |
| 1862 | if (*s) |
| 1863 | *bits = *s++ - '0'; |
| 1864 | if (*s) |
| 1865 | *flow = *s; |
| 1866 | } |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1867 | EXPORT_SYMBOL_GPL(uart_parse_options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
| 1869 | struct baud_rates { |
| 1870 | unsigned int rate; |
| 1871 | unsigned int cflag; |
| 1872 | }; |
| 1873 | |
Arjan van de Ven | cb3592b | 2005-11-28 21:04:11 +0000 | [diff] [blame] | 1874 | static const struct baud_rates baud_rates[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | { 921600, B921600 }, |
| 1876 | { 460800, B460800 }, |
| 1877 | { 230400, B230400 }, |
| 1878 | { 115200, B115200 }, |
| 1879 | { 57600, B57600 }, |
| 1880 | { 38400, B38400 }, |
| 1881 | { 19200, B19200 }, |
| 1882 | { 9600, B9600 }, |
| 1883 | { 4800, B4800 }, |
| 1884 | { 2400, B2400 }, |
| 1885 | { 1200, B1200 }, |
| 1886 | { 0, B38400 } |
| 1887 | }; |
| 1888 | |
| 1889 | /** |
| 1890 | * uart_set_options - setup the serial console parameters |
| 1891 | * @port: pointer to the serial ports uart_port structure |
| 1892 | * @co: console pointer |
| 1893 | * @baud: baud rate |
| 1894 | * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) |
| 1895 | * @bits: number of data bits |
| 1896 | * @flow: flow control character - 'r' (rts) |
| 1897 | */ |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1898 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | uart_set_options(struct uart_port *port, struct console *co, |
| 1900 | int baud, int parity, int bits, int flow) |
| 1901 | { |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1902 | struct ktermios termios; |
Alan Cox | 149b36e | 2007-10-18 01:24:16 -0700 | [diff] [blame] | 1903 | static struct ktermios dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | int i; |
| 1905 | |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 1906 | /* |
| 1907 | * Ensure that the serial console lock is initialised |
| 1908 | * early. |
| 1909 | */ |
| 1910 | spin_lock_init(&port->lock); |
Ingo Molnar | 13e8359 | 2006-07-03 00:25:03 -0700 | [diff] [blame] | 1911 | lockdep_set_class(&port->lock, &port_lock_key); |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 1912 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1913 | memset(&termios, 0, sizeof(struct ktermios)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | |
| 1915 | termios.c_cflag = CREAD | HUPCL | CLOCAL; |
| 1916 | |
| 1917 | /* |
| 1918 | * Construct a cflag setting. |
| 1919 | */ |
| 1920 | for (i = 0; baud_rates[i].rate; i++) |
| 1921 | if (baud_rates[i].rate <= baud) |
| 1922 | break; |
| 1923 | |
| 1924 | termios.c_cflag |= baud_rates[i].cflag; |
| 1925 | |
| 1926 | if (bits == 7) |
| 1927 | termios.c_cflag |= CS7; |
| 1928 | else |
| 1929 | termios.c_cflag |= CS8; |
| 1930 | |
| 1931 | switch (parity) { |
| 1932 | case 'o': case 'O': |
| 1933 | termios.c_cflag |= PARODD; |
| 1934 | /*fall through*/ |
| 1935 | case 'e': case 'E': |
| 1936 | termios.c_cflag |= PARENB; |
| 1937 | break; |
| 1938 | } |
| 1939 | |
| 1940 | if (flow == 'r') |
| 1941 | termios.c_cflag |= CRTSCTS; |
| 1942 | |
Yinghai Lu | 7949268 | 2007-07-15 23:37:25 -0700 | [diff] [blame] | 1943 | /* |
| 1944 | * some uarts on other side don't support no flow control. |
| 1945 | * So we set * DTR in host uart to make them happy |
| 1946 | */ |
| 1947 | port->mctrl |= TIOCM_DTR; |
| 1948 | |
Alan Cox | 149b36e | 2007-10-18 01:24:16 -0700 | [diff] [blame] | 1949 | port->ops->set_termios(port, &termios, &dummy); |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1950 | /* |
| 1951 | * Allow the setting of the UART parameters with a NULL console |
| 1952 | * too: |
| 1953 | */ |
| 1954 | if (co) |
| 1955 | co->cflag = termios.c_cflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
| 1957 | return 0; |
| 1958 | } |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1959 | EXPORT_SYMBOL_GPL(uart_set_options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | #endif /* CONFIG_SERIAL_CORE_CONSOLE */ |
| 1961 | |
| 1962 | static void uart_change_pm(struct uart_state *state, int pm_state) |
| 1963 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1964 | struct uart_port *port = state->uart_port; |
Andrew Victor | 1281e36 | 2006-05-16 11:28:49 +0100 | [diff] [blame] | 1965 | |
| 1966 | if (state->pm_state != pm_state) { |
| 1967 | if (port->ops->pm) |
| 1968 | port->ops->pm(port, pm_state, state->pm_state); |
| 1969 | state->pm_state = pm_state; |
| 1970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1973 | struct uart_match { |
| 1974 | struct uart_port *port; |
| 1975 | struct uart_driver *driver; |
| 1976 | }; |
| 1977 | |
| 1978 | static int serial_match_port(struct device *dev, void *data) |
| 1979 | { |
| 1980 | struct uart_match *match = data; |
Guennadi Liakhovetski | 7ca796f | 2008-07-04 09:59:28 -0700 | [diff] [blame] | 1981 | struct tty_driver *tty_drv = match->driver->tty_driver; |
| 1982 | dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + |
| 1983 | match->port->line; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1984 | |
| 1985 | return dev->devt == devt; /* Actually, only one tty per port */ |
| 1986 | } |
| 1987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | int uart_suspend_port(struct uart_driver *drv, struct uart_port *port) |
| 1989 | { |
| 1990 | struct uart_state *state = drv->state + port->line; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 1991 | struct device *tty_dev; |
| 1992 | struct uart_match match = {port, drv}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 1994 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 1996 | if (!console_suspend_enabled && uart_console(port)) { |
| 1997 | /* we're going to avoid suspending serial console */ |
Rafael J. Wysocki | e1da95a | 2006-09-25 23:32:57 -0700 | [diff] [blame] | 1998 | mutex_unlock(&state->mutex); |
| 1999 | return 0; |
| 2000 | } |
Rafael J. Wysocki | e1da95a | 2006-09-25 23:32:57 -0700 | [diff] [blame] | 2001 | |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2002 | tty_dev = device_find_child(port->dev, &match, serial_match_port); |
| 2003 | if (device_may_wakeup(tty_dev)) { |
| 2004 | enable_irq_wake(port->irq); |
| 2005 | put_device(tty_dev); |
| 2006 | mutex_unlock(&state->mutex); |
| 2007 | return 0; |
| 2008 | } |
| 2009 | port->suspended = 1; |
| 2010 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2011 | if (state->flags & UIF_INITIALIZED) { |
Russell King | ba899db | 2006-01-21 22:45:50 +0000 | [diff] [blame] | 2012 | const struct uart_ops *ops = port->ops; |
Russell King | c8c6bfa | 2008-02-04 22:27:52 -0800 | [diff] [blame] | 2013 | int tries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2015 | state->flags = (state->flags & ~UIF_INITIALIZED) |
Russell King | a6b93a9 | 2006-10-01 17:17:40 +0100 | [diff] [blame] | 2016 | | UIF_SUSPENDED; |
| 2017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | spin_lock_irq(&port->lock); |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 2019 | ops->stop_tx(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | ops->set_mctrl(port, 0); |
| 2021 | ops->stop_rx(port); |
| 2022 | spin_unlock_irq(&port->lock); |
| 2023 | |
| 2024 | /* |
| 2025 | * Wait for the transmitter to empty. |
| 2026 | */ |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 2027 | for (tries = 3; !ops->tx_empty(port) && tries; tries--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | msleep(10); |
Russell King | c8c6bfa | 2008-02-04 22:27:52 -0800 | [diff] [blame] | 2029 | if (!tries) |
Alan Cox | a46c999 | 2008-02-08 04:18:53 -0800 | [diff] [blame] | 2030 | printk(KERN_ERR "%s%s%s%d: Unable to drain " |
| 2031 | "transmitter\n", |
Kay Sievers | 4bfe090 | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 2032 | port->dev ? dev_name(port->dev) : "", |
Russell King | c8c6bfa | 2008-02-04 22:27:52 -0800 | [diff] [blame] | 2033 | port->dev ? ": " : "", |
David S. Miller | 8440838 | 2008-10-13 10:45:26 +0100 | [diff] [blame] | 2034 | drv->dev_name, |
| 2035 | drv->tty_driver->name_base + port->line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
| 2037 | ops->shutdown(port); |
| 2038 | } |
| 2039 | |
| 2040 | /* |
| 2041 | * Disable the console device before suspending. |
| 2042 | */ |
| 2043 | if (uart_console(port)) |
| 2044 | console_stop(port->cons); |
| 2045 | |
| 2046 | uart_change_pm(state, 3); |
| 2047 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 2048 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | |
| 2050 | return 0; |
| 2051 | } |
| 2052 | |
| 2053 | int uart_resume_port(struct uart_driver *drv, struct uart_port *port) |
| 2054 | { |
| 2055 | struct uart_state *state = drv->state + port->line; |
Arjan van de Ven | 03a74dc | 2008-05-23 13:04:49 -0700 | [diff] [blame] | 2056 | struct device *tty_dev; |
| 2057 | struct uart_match match = {port, drv}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 2059 | mutex_lock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2061 | if (!console_suspend_enabled && uart_console(port)) { |
| 2062 | /* no need to resume serial console, it wasn't suspended */ |
Rafael J. Wysocki | e1da95a | 2006-09-25 23:32:57 -0700 | [diff] [blame] | 2063 | mutex_unlock(&state->mutex); |
| 2064 | return 0; |
| 2065 | } |
Rafael J. Wysocki | e1da95a | 2006-09-25 23:32:57 -0700 | [diff] [blame] | 2066 | |
Arjan van de Ven | 03a74dc | 2008-05-23 13:04:49 -0700 | [diff] [blame] | 2067 | tty_dev = device_find_child(port->dev, &match, serial_match_port); |
| 2068 | if (!port->suspended && device_may_wakeup(tty_dev)) { |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2069 | disable_irq_wake(port->irq); |
| 2070 | mutex_unlock(&state->mutex); |
| 2071 | return 0; |
| 2072 | } |
| 2073 | port->suspended = 0; |
| 2074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | /* |
| 2076 | * Re-enable the console device after suspending. |
| 2077 | */ |
| 2078 | if (uart_console(port)) { |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 2079 | struct ktermios termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | |
| 2081 | /* |
| 2082 | * First try to use the console cflag setting. |
| 2083 | */ |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 2084 | memset(&termios, 0, sizeof(struct ktermios)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | termios.c_cflag = port->cons->cflag; |
| 2086 | |
| 2087 | /* |
| 2088 | * If that's unset, use the tty termios setting. |
| 2089 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2090 | if (state->port.tty && termios.c_cflag == 0) |
| 2091 | termios = *state->port.tty->termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | |
Russell King | 9d778a6 | 2008-02-04 22:27:51 -0800 | [diff] [blame] | 2093 | uart_change_pm(state, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | port->ops->set_termios(port, &termios, NULL); |
| 2095 | console_start(port->cons); |
| 2096 | } |
| 2097 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2098 | if (state->flags & UIF_SUSPENDED) { |
Russell King | ba899db | 2006-01-21 22:45:50 +0000 | [diff] [blame] | 2099 | const struct uart_ops *ops = port->ops; |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 2100 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
Russell King | 9d778a6 | 2008-02-04 22:27:51 -0800 | [diff] [blame] | 2102 | uart_change_pm(state, 0); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2103 | spin_lock_irq(&port->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | ops->set_mctrl(port, 0); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2105 | spin_unlock_irq(&port->lock); |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 2106 | ret = ops->startup(port); |
| 2107 | if (ret == 0) { |
| 2108 | uart_change_speed(state, NULL); |
| 2109 | spin_lock_irq(&port->lock); |
| 2110 | ops->set_mctrl(port, port->mctrl); |
| 2111 | ops->start_tx(port); |
| 2112 | spin_unlock_irq(&port->lock); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2113 | state->flags |= UIF_INITIALIZED; |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 2114 | } else { |
| 2115 | /* |
| 2116 | * Failed to resume - maybe hardware went away? |
| 2117 | * Clear the "initialized" flag so we won't try |
| 2118 | * to call the low level drivers shutdown method. |
| 2119 | */ |
Russell King | ee31b33 | 2005-11-13 15:28:51 +0000 | [diff] [blame] | 2120 | uart_shutdown(state); |
| 2121 | } |
Russell King | a6b93a9 | 2006-10-01 17:17:40 +0100 | [diff] [blame] | 2122 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2123 | state->flags &= ~UIF_SUSPENDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 2126 | mutex_unlock(&state->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
| 2128 | return 0; |
| 2129 | } |
| 2130 | |
| 2131 | static inline void |
| 2132 | uart_report_port(struct uart_driver *drv, struct uart_port *port) |
| 2133 | { |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2134 | char address[64]; |
| 2135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | switch (port->iotype) { |
| 2137 | case UPIO_PORT: |
Andrew Morton | 9bde10a | 2008-10-13 10:35:42 +0100 | [diff] [blame] | 2138 | snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | break; |
| 2140 | case UPIO_HUB6: |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2141 | snprintf(address, sizeof(address), |
Andrew Morton | 9bde10a | 2008-10-13 10:35:42 +0100 | [diff] [blame] | 2142 | "I/O 0x%lx offset 0x%x", port->iobase, port->hub6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | break; |
| 2144 | case UPIO_MEM: |
| 2145 | case UPIO_MEM32: |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 2146 | case UPIO_AU: |
Zang Roy-r61911 | 3be91ec | 2006-06-30 02:29:58 -0700 | [diff] [blame] | 2147 | case UPIO_TSI: |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 2148 | case UPIO_DWAPB: |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2149 | snprintf(address, sizeof(address), |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 2150 | "MMIO 0x%llx", (unsigned long long)port->mapbase); |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2151 | break; |
| 2152 | default: |
| 2153 | strlcpy(address, "*unknown*", sizeof(address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | break; |
| 2155 | } |
Russell King | 30b7a3b | 2005-09-03 15:30:21 +0100 | [diff] [blame] | 2156 | |
Russell King | 0cf669d | 2005-10-31 11:42:22 +0000 | [diff] [blame] | 2157 | printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n", |
Kay Sievers | 4bfe090 | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 2158 | port->dev ? dev_name(port->dev) : "", |
Russell King | 0cf669d | 2005-10-31 11:42:22 +0000 | [diff] [blame] | 2159 | port->dev ? ": " : "", |
David S. Miller | 8440838 | 2008-10-13 10:45:26 +0100 | [diff] [blame] | 2160 | drv->dev_name, |
| 2161 | drv->tty_driver->name_base + port->line, |
| 2162 | address, port->irq, uart_type(port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | static void |
| 2166 | uart_configure_port(struct uart_driver *drv, struct uart_state *state, |
| 2167 | struct uart_port *port) |
| 2168 | { |
| 2169 | unsigned int flags; |
| 2170 | |
| 2171 | /* |
| 2172 | * If there isn't a port here, don't do anything further. |
| 2173 | */ |
| 2174 | if (!port->iobase && !port->mapbase && !port->membase) |
| 2175 | return; |
| 2176 | |
| 2177 | /* |
| 2178 | * Now do the auto configuration stuff. Note that config_port |
| 2179 | * is expected to claim the resources and map the port for us. |
| 2180 | */ |
David Daney | 8e23fcc | 2009-01-02 13:49:54 +0000 | [diff] [blame] | 2181 | flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | if (port->flags & UPF_AUTO_IRQ) |
| 2183 | flags |= UART_CONFIG_IRQ; |
| 2184 | if (port->flags & UPF_BOOT_AUTOCONF) { |
David Daney | 8e23fcc | 2009-01-02 13:49:54 +0000 | [diff] [blame] | 2185 | if (!(port->flags & UPF_FIXED_TYPE)) { |
| 2186 | port->type = PORT_UNKNOWN; |
| 2187 | flags |= UART_CONFIG_TYPE; |
| 2188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | port->ops->config_port(port, flags); |
| 2190 | } |
| 2191 | |
| 2192 | if (port->type != PORT_UNKNOWN) { |
| 2193 | unsigned long flags; |
| 2194 | |
| 2195 | uart_report_port(drv, port); |
| 2196 | |
George G. Davis | 3689a0e | 2007-02-14 00:33:06 -0800 | [diff] [blame] | 2197 | /* Power up port for set_mctrl() */ |
| 2198 | uart_change_pm(state, 0); |
| 2199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | /* |
| 2201 | * Ensure that the modem control lines are de-activated. |
Yinghai Lu | c3e4642 | 2008-02-04 22:27:46 -0800 | [diff] [blame] | 2202 | * keep the DTR setting that is set in uart_set_options() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | * We probably don't need a spinlock around this, but |
| 2204 | */ |
| 2205 | spin_lock_irqsave(&port->lock, flags); |
Yinghai Lu | c3e4642 | 2008-02-04 22:27:46 -0800 | [diff] [blame] | 2206 | port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | spin_unlock_irqrestore(&port->lock, flags); |
| 2208 | |
| 2209 | /* |
Russell King | 97d9722 | 2007-09-01 21:25:09 +0100 | [diff] [blame] | 2210 | * If this driver supports console, and it hasn't been |
| 2211 | * successfully registered yet, try to re-register it. |
| 2212 | * It may be that the port was not available. |
| 2213 | */ |
| 2214 | if (port->cons && !(port->cons->flags & CON_ENABLED)) |
| 2215 | register_console(port->cons); |
| 2216 | |
| 2217 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | * Power down all ports by default, except the |
| 2219 | * console if we have one. |
| 2220 | */ |
| 2221 | if (!uart_console(port)) |
| 2222 | uart_change_pm(state, 3); |
| 2223 | } |
| 2224 | } |
| 2225 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2226 | #ifdef CONFIG_CONSOLE_POLL |
| 2227 | |
| 2228 | static int uart_poll_init(struct tty_driver *driver, int line, char *options) |
| 2229 | { |
| 2230 | struct uart_driver *drv = driver->driver_state; |
| 2231 | struct uart_state *state = drv->state + line; |
| 2232 | struct uart_port *port; |
| 2233 | int baud = 9600; |
| 2234 | int bits = 8; |
| 2235 | int parity = 'n'; |
| 2236 | int flow = 'n'; |
| 2237 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2238 | if (!state || !state->uart_port) |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2239 | return -1; |
| 2240 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2241 | port = state->uart_port; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2242 | if (!(port->ops->poll_get_char && port->ops->poll_put_char)) |
| 2243 | return -1; |
| 2244 | |
| 2245 | if (options) { |
| 2246 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 2247 | return uart_set_options(port, NULL, baud, parity, bits, flow); |
| 2248 | } |
| 2249 | |
| 2250 | return 0; |
| 2251 | } |
| 2252 | |
| 2253 | static int uart_poll_get_char(struct tty_driver *driver, int line) |
| 2254 | { |
| 2255 | struct uart_driver *drv = driver->driver_state; |
| 2256 | struct uart_state *state = drv->state + line; |
| 2257 | struct uart_port *port; |
| 2258 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2259 | if (!state || !state->uart_port) |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2260 | return -1; |
| 2261 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2262 | port = state->uart_port; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2263 | return port->ops->poll_get_char(port); |
| 2264 | } |
| 2265 | |
| 2266 | static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) |
| 2267 | { |
| 2268 | struct uart_driver *drv = driver->driver_state; |
| 2269 | struct uart_state *state = drv->state + line; |
| 2270 | struct uart_port *port; |
| 2271 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2272 | if (!state || !state->uart_port) |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2273 | return; |
| 2274 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2275 | port = state->uart_port; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2276 | port->ops->poll_put_char(port, ch); |
| 2277 | } |
| 2278 | #endif |
| 2279 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 2280 | static const struct tty_operations uart_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | .open = uart_open, |
| 2282 | .close = uart_close, |
| 2283 | .write = uart_write, |
| 2284 | .put_char = uart_put_char, |
| 2285 | .flush_chars = uart_flush_chars, |
| 2286 | .write_room = uart_write_room, |
| 2287 | .chars_in_buffer= uart_chars_in_buffer, |
| 2288 | .flush_buffer = uart_flush_buffer, |
| 2289 | .ioctl = uart_ioctl, |
| 2290 | .throttle = uart_throttle, |
| 2291 | .unthrottle = uart_unthrottle, |
| 2292 | .send_xchar = uart_send_xchar, |
| 2293 | .set_termios = uart_set_termios, |
Alan Cox | 64e9159 | 2008-06-03 15:18:54 +0100 | [diff] [blame] | 2294 | .set_ldisc = uart_set_ldisc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | .stop = uart_stop, |
| 2296 | .start = uart_start, |
| 2297 | .hangup = uart_hangup, |
| 2298 | .break_ctl = uart_break_ctl, |
| 2299 | .wait_until_sent= uart_wait_until_sent, |
| 2300 | #ifdef CONFIG_PROC_FS |
Alexey Dobriyan | d196a94 | 2009-03-31 15:19:21 -0700 | [diff] [blame] | 2301 | .proc_fops = &uart_proc_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | #endif |
| 2303 | .tiocmget = uart_tiocmget, |
| 2304 | .tiocmset = uart_tiocmset, |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2305 | #ifdef CONFIG_CONSOLE_POLL |
| 2306 | .poll_init = uart_poll_init, |
| 2307 | .poll_get_char = uart_poll_get_char, |
| 2308 | .poll_put_char = uart_poll_put_char, |
| 2309 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | }; |
| 2311 | |
| 2312 | /** |
| 2313 | * uart_register_driver - register a driver with the uart core layer |
| 2314 | * @drv: low level driver structure |
| 2315 | * |
| 2316 | * Register a uart driver with the core driver. We in turn register |
| 2317 | * with the tty layer, and initialise the core driver per-port state. |
| 2318 | * |
| 2319 | * We have a proc file in /proc/tty/driver which is named after the |
| 2320 | * normal driver. |
| 2321 | * |
| 2322 | * drv->port should be NULL, and the per-port structures should be |
| 2323 | * registered using uart_add_one_port after this call has succeeded. |
| 2324 | */ |
| 2325 | int uart_register_driver(struct uart_driver *drv) |
| 2326 | { |
| 2327 | struct tty_driver *normal = NULL; |
| 2328 | int i, retval; |
| 2329 | |
| 2330 | BUG_ON(drv->state); |
| 2331 | |
| 2332 | /* |
| 2333 | * Maybe we should be using a slab cache for this, especially if |
| 2334 | * we have a large number of ports to handle. |
| 2335 | */ |
Burman Yan | 8f31bb3 | 2007-02-14 00:33:07 -0800 | [diff] [blame] | 2336 | drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | retval = -ENOMEM; |
| 2338 | if (!drv->state) |
| 2339 | goto out; |
| 2340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | normal = alloc_tty_driver(drv->nr); |
| 2342 | if (!normal) |
| 2343 | goto out; |
| 2344 | |
| 2345 | drv->tty_driver = normal; |
| 2346 | |
| 2347 | normal->owner = drv->owner; |
| 2348 | normal->driver_name = drv->driver_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | normal->name = drv->dev_name; |
| 2350 | normal->major = drv->major; |
| 2351 | normal->minor_start = drv->minor; |
| 2352 | normal->type = TTY_DRIVER_TYPE_SERIAL; |
| 2353 | normal->subtype = SERIAL_TYPE_NORMAL; |
| 2354 | normal->init_termios = tty_std_termios; |
| 2355 | normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 2356 | normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600; |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2357 | normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | normal->driver_state = drv; |
| 2359 | tty_set_operations(normal, &uart_ops); |
| 2360 | |
| 2361 | /* |
| 2362 | * Initialise the UART state(s). |
| 2363 | */ |
| 2364 | for (i = 0; i < drv->nr; i++) { |
| 2365 | struct uart_state *state = drv->state + i; |
| 2366 | |
Ingo Molnar | e2862f6 | 2006-01-13 21:37:07 +0000 | [diff] [blame] | 2367 | mutex_init(&state->mutex); |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 2368 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2369 | tty_port_init(&state->port); |
Alan Cox | 5e99df5 | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2370 | state->port.close_delay = 500; /* .5 seconds */ |
| 2371 | state->port.closing_wait = 30000; /* 30 seconds */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2372 | init_waitqueue_head(&state->delta_msr_wait); |
| 2373 | tasklet_init(&state->tlet, uart_tasklet_action, |
Alan Cox | f751928 | 2009-01-02 13:49:21 +0000 | [diff] [blame] | 2374 | (unsigned long)state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | retval = tty_register_driver(normal); |
| 2378 | out: |
| 2379 | if (retval < 0) { |
| 2380 | put_tty_driver(normal); |
| 2381 | kfree(drv->state); |
| 2382 | } |
| 2383 | return retval; |
| 2384 | } |
| 2385 | |
| 2386 | /** |
| 2387 | * uart_unregister_driver - remove a driver from the uart core layer |
| 2388 | * @drv: low level driver structure |
| 2389 | * |
| 2390 | * Remove all references to a driver from the core driver. The low |
| 2391 | * level driver must have removed all its ports via the |
| 2392 | * uart_remove_one_port() if it registered them with uart_add_one_port(). |
| 2393 | * (ie, drv->port == NULL) |
| 2394 | */ |
| 2395 | void uart_unregister_driver(struct uart_driver *drv) |
| 2396 | { |
| 2397 | struct tty_driver *p = drv->tty_driver; |
| 2398 | tty_unregister_driver(p); |
| 2399 | put_tty_driver(p); |
| 2400 | kfree(drv->state); |
| 2401 | drv->tty_driver = NULL; |
| 2402 | } |
| 2403 | |
| 2404 | struct tty_driver *uart_console_device(struct console *co, int *index) |
| 2405 | { |
| 2406 | struct uart_driver *p = co->data; |
| 2407 | *index = co->index; |
| 2408 | return p->tty_driver; |
| 2409 | } |
| 2410 | |
| 2411 | /** |
| 2412 | * uart_add_one_port - attach a driver-defined port structure |
| 2413 | * @drv: pointer to the uart low level driver structure for this port |
| 2414 | * @port: uart port structure to use for this port. |
| 2415 | * |
| 2416 | * This allows the driver to register its own uart_port structure |
| 2417 | * with the core driver. The main purpose is to allow the low |
| 2418 | * level uart drivers to expand uart_port, rather than having yet |
| 2419 | * more levels of structures. |
| 2420 | */ |
| 2421 | int uart_add_one_port(struct uart_driver *drv, struct uart_port *port) |
| 2422 | { |
| 2423 | struct uart_state *state; |
| 2424 | int ret = 0; |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2425 | struct device *tty_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | |
| 2427 | BUG_ON(in_interrupt()); |
| 2428 | |
| 2429 | if (port->line >= drv->nr) |
| 2430 | return -EINVAL; |
| 2431 | |
| 2432 | state = drv->state + port->line; |
| 2433 | |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2434 | mutex_lock(&port_mutex); |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2435 | mutex_lock(&state->mutex); |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2436 | if (state->uart_port) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | ret = -EINVAL; |
| 2438 | goto out; |
| 2439 | } |
| 2440 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2441 | state->uart_port = port; |
Russell King | 97d9722 | 2007-09-01 21:25:09 +0100 | [diff] [blame] | 2442 | state->pm_state = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | port->cons = drv->cons; |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2445 | port->state = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 2447 | /* |
| 2448 | * If this port is a console, then the spinlock is already |
| 2449 | * initialised. |
| 2450 | */ |
Ingo Molnar | 13e8359 | 2006-07-03 00:25:03 -0700 | [diff] [blame] | 2451 | if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) { |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 2452 | spin_lock_init(&port->lock); |
Ingo Molnar | 13e8359 | 2006-07-03 00:25:03 -0700 | [diff] [blame] | 2453 | lockdep_set_class(&port->lock, &port_lock_key); |
| 2454 | } |
Russell King | 976ecd1 | 2005-07-03 21:05:45 +0100 | [diff] [blame] | 2455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | uart_configure_port(drv, state, port); |
| 2457 | |
| 2458 | /* |
| 2459 | * Register the port whether it's detected or not. This allows |
| 2460 | * setserial to be used to alter this ports parameters. |
| 2461 | */ |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2462 | tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev); |
| 2463 | if (likely(!IS_ERR(tty_dev))) { |
Alan Stern | 74081f8 | 2008-03-19 22:35:13 +0100 | [diff] [blame] | 2464 | device_init_wakeup(tty_dev, 1); |
Guennadi Liakhovetski | b3b708f | 2007-10-16 01:24:02 -0700 | [diff] [blame] | 2465 | device_set_wakeup_enable(tty_dev, 0); |
| 2466 | } else |
| 2467 | printk(KERN_ERR "Cannot register tty device on line %d\n", |
| 2468 | port->line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | |
| 2470 | /* |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2471 | * Ensure UPF_DEAD is not set. |
| 2472 | */ |
| 2473 | port->flags &= ~UPF_DEAD; |
| 2474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | out: |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2476 | mutex_unlock(&state->mutex); |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2477 | mutex_unlock(&port_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | |
| 2479 | return ret; |
| 2480 | } |
| 2481 | |
| 2482 | /** |
| 2483 | * uart_remove_one_port - detach a driver defined port structure |
| 2484 | * @drv: pointer to the uart low level driver structure for this port |
| 2485 | * @port: uart port structure for this port |
| 2486 | * |
| 2487 | * This unhooks (and hangs up) the specified port structure from the |
| 2488 | * core driver. No further calls will be made to the low-level code |
| 2489 | * for this port. |
| 2490 | */ |
| 2491 | int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port) |
| 2492 | { |
| 2493 | struct uart_state *state = drv->state + port->line; |
| 2494 | |
| 2495 | BUG_ON(in_interrupt()); |
| 2496 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2497 | if (state->uart_port != port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | printk(KERN_ALERT "Removing wrong port: %p != %p\n", |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2499 | state->uart_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2501 | mutex_lock(&port_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | |
| 2503 | /* |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2504 | * Mark the port "dead" - this prevents any opens from |
| 2505 | * succeeding while we shut down the port. |
| 2506 | */ |
| 2507 | mutex_lock(&state->mutex); |
| 2508 | port->flags |= UPF_DEAD; |
| 2509 | mutex_unlock(&state->mutex); |
| 2510 | |
| 2511 | /* |
Greg Kroah-Hartman | aa4148c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2512 | * Remove the devices from the tty layer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | */ |
| 2514 | tty_unregister_device(drv->tty_driver, port->line); |
| 2515 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2516 | if (state->port.tty) |
| 2517 | tty_vhangup(state->port.tty); |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2518 | |
| 2519 | /* |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2520 | * Free the port IO and memory resources, if any. |
| 2521 | */ |
| 2522 | if (port->type != PORT_UNKNOWN) |
| 2523 | port->ops->release_port(port); |
| 2524 | |
| 2525 | /* |
| 2526 | * Indicate that there isn't a port here anymore. |
| 2527 | */ |
| 2528 | port->type = PORT_UNKNOWN; |
| 2529 | |
| 2530 | /* |
| 2531 | * Kill the tasklet, and free resources. |
| 2532 | */ |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2533 | tasklet_kill(&state->tlet); |
Russell King | 68ac64c | 2006-04-30 11:13:50 +0100 | [diff] [blame] | 2534 | |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 2535 | state->uart_port = NULL; |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2536 | mutex_unlock(&port_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
| 2538 | return 0; |
| 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | * Are the two ports equivalent? |
| 2543 | */ |
| 2544 | int uart_match_port(struct uart_port *port1, struct uart_port *port2) |
| 2545 | { |
| 2546 | if (port1->iotype != port2->iotype) |
| 2547 | return 0; |
| 2548 | |
| 2549 | switch (port1->iotype) { |
| 2550 | case UPIO_PORT: |
| 2551 | return (port1->iobase == port2->iobase); |
| 2552 | case UPIO_HUB6: |
| 2553 | return (port1->iobase == port2->iobase) && |
| 2554 | (port1->hub6 == port2->hub6); |
| 2555 | case UPIO_MEM: |
Sergei Shtylyov | d21b55d | 2006-08-28 19:49:03 +0400 | [diff] [blame] | 2556 | case UPIO_MEM32: |
| 2557 | case UPIO_AU: |
| 2558 | case UPIO_TSI: |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 2559 | case UPIO_DWAPB: |
Benjamin Herrenschmidt | 1624f00 | 2006-01-04 18:09:44 +0000 | [diff] [blame] | 2560 | return (port1->mapbase == port2->mapbase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | } |
| 2562 | return 0; |
| 2563 | } |
| 2564 | EXPORT_SYMBOL(uart_match_port); |
| 2565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | EXPORT_SYMBOL(uart_write_wakeup); |
| 2567 | EXPORT_SYMBOL(uart_register_driver); |
| 2568 | EXPORT_SYMBOL(uart_unregister_driver); |
| 2569 | EXPORT_SYMBOL(uart_suspend_port); |
| 2570 | EXPORT_SYMBOL(uart_resume_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | EXPORT_SYMBOL(uart_add_one_port); |
| 2572 | EXPORT_SYMBOL(uart_remove_one_port); |
| 2573 | |
| 2574 | MODULE_DESCRIPTION("Serial driver core"); |
| 2575 | MODULE_LICENSE("GPL"); |