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