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