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