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