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