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