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