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 #include <linux/module.h> 11 #include <linux/tty.h> 12 #include <linux/tty_flip.h> 13 #include <linux/slab.h> 14 #include <linux/sched/signal.h> 15 #include <linux/init.h> 16 #include <linux/console.h> 17 #include <linux/gpio/consumer.h> 18 #include <linux/kernel.h> 19 #include <linux/of.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/proc_fs.h> 22 #include <linux/seq_file.h> 23 #include <linux/device.h> 24 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */ 25 #include <linux/serial_core.h> 26 #include <linux/sysrq.h> 27 #include <linux/delay.h> 28 #include <linux/mutex.h> 29 #include <linux/math64.h> 30 #include <linux/security.h> 31 32 #include <linux/irq.h> 33 #include <linux/uaccess.h> 34 35 #include "serial_base.h" 36 #include "8250/8250.h" /* For hub6_match_port() */ 37 38 /* 39 * This is used to lock changes in serial line configuration. 40 */ 41 static DEFINE_MUTEX(port_mutex); 42 43 /* 44 * lockdep: port->lock is initialized in two places, but we 45 * want only one lock-class: 46 */ 47 static struct lock_class_key port_lock_key; 48 49 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8) 50 51 /* 52 * Max time with active RTS before/after data is sent. 53 */ 54 #define RS485_MAX_RTS_DELAY 100 /* msecs */ 55 56 static void uart_change_pm(struct uart_state *state, 57 enum uart_pm_state pm_state); 58 59 static void uart_port_shutdown(struct tty_port *port); 60 61 static int uart_dcd_enabled(struct uart_port *uport) 62 { 63 return !!(uport->status & UPSTAT_DCD_ENABLE); 64 } 65 66 static inline struct uart_port *uart_port_ref(struct uart_state *state) 67 { 68 if (atomic_add_unless(&state->refcount, 1, 0)) 69 return state->uart_port; 70 return NULL; 71 } 72 73 static inline void uart_port_deref(struct uart_port *uport) 74 { 75 if (atomic_dec_and_test(&uport->state->refcount)) 76 wake_up(&uport->state->remove_wait); 77 } 78 79 static inline struct uart_port *uart_port_ref_lock(struct uart_state *state, unsigned long *flags) 80 { 81 struct uart_port *uport = uart_port_ref(state); 82 83 if (uport) 84 uart_port_lock_irqsave(uport, flags); 85 86 return uport; 87 } 88 89 static inline void uart_port_unlock_deref(struct uart_port *uport, unsigned long flags) 90 { 91 if (uport) { 92 uart_port_unlock_irqrestore(uport, flags); 93 uart_port_deref(uport); 94 } 95 } 96 97 static inline struct uart_port *uart_port_check(struct uart_state *state) 98 { 99 lockdep_assert_held(&state->port.mutex); 100 return state->uart_port; 101 } 102 103 /** 104 * uart_write_wakeup - schedule write processing 105 * @port: port to be processed 106 * 107 * This routine is used by the interrupt handler to schedule processing in the 108 * software interrupt portion of the driver. A driver is expected to call this 109 * function when the number of characters in the transmit buffer have dropped 110 * below a threshold. 111 * 112 * Locking: @port->lock should be held 113 */ 114 void uart_write_wakeup(struct uart_port *port) 115 { 116 struct uart_state *state = port->state; 117 /* 118 * This means you called this function _after_ the port was 119 * closed. No cookie for you. 120 */ 121 BUG_ON(!state); 122 tty_port_tty_wakeup(&state->port); 123 } 124 EXPORT_SYMBOL(uart_write_wakeup); 125 126 static void uart_stop(struct tty_struct *tty) 127 { 128 struct uart_state *state = tty->driver_data; 129 struct uart_port *port; 130 unsigned long flags; 131 132 port = uart_port_ref_lock(state, &flags); 133 if (port) 134 port->ops->stop_tx(port); 135 uart_port_unlock_deref(port, flags); 136 } 137 138 static void __uart_start(struct uart_state *state) 139 { 140 struct uart_port *port = state->uart_port; 141 struct serial_port_device *port_dev; 142 int err; 143 144 if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port)) 145 return; 146 147 port_dev = port->port_dev; 148 149 /* Increment the runtime PM usage count for the active check below */ 150 err = pm_runtime_get(&port_dev->dev); 151 if (err < 0 && err != -EINPROGRESS) { 152 pm_runtime_put_noidle(&port_dev->dev); 153 return; 154 } 155 156 /* 157 * Start TX if enabled, and kick runtime PM. If the device is not 158 * enabled, serial_port_runtime_resume() calls start_tx() again 159 * after enabling the device. 160 */ 161 if (!pm_runtime_enabled(port->dev) || pm_runtime_active(&port_dev->dev)) 162 port->ops->start_tx(port); 163 pm_runtime_mark_last_busy(&port_dev->dev); 164 pm_runtime_put_autosuspend(&port_dev->dev); 165 } 166 167 static void uart_start(struct tty_struct *tty) 168 { 169 struct uart_state *state = tty->driver_data; 170 struct uart_port *port; 171 unsigned long flags; 172 173 port = uart_port_ref_lock(state, &flags); 174 __uart_start(state); 175 uart_port_unlock_deref(port, flags); 176 } 177 178 static void 179 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear) 180 { 181 unsigned int old; 182 183 guard(uart_port_lock_irqsave)(port); 184 old = port->mctrl; 185 port->mctrl = (old & ~clear) | set; 186 if (old != port->mctrl && !(port->rs485.flags & SER_RS485_ENABLED)) 187 port->ops->set_mctrl(port, port->mctrl); 188 } 189 190 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0) 191 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear) 192 193 static void uart_port_dtr_rts(struct uart_port *uport, bool active) 194 { 195 if (active) 196 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS); 197 else 198 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS); 199 } 200 201 /* Caller holds port mutex */ 202 static void uart_change_line_settings(struct tty_struct *tty, struct uart_state *state, 203 const struct ktermios *old_termios) 204 { 205 struct uart_port *uport = uart_port_check(state); 206 struct ktermios *termios; 207 bool old_hw_stopped; 208 209 /* 210 * If we have no tty, termios, or the port does not exist, 211 * then we can't set the parameters for this port. 212 */ 213 if (!tty || uport->type == PORT_UNKNOWN) 214 return; 215 216 termios = &tty->termios; 217 uport->ops->set_termios(uport, termios, old_termios); 218 219 /* 220 * Set modem status enables based on termios cflag 221 */ 222 guard(uart_port_lock_irq)(uport); 223 if (termios->c_cflag & CRTSCTS) 224 uport->status |= UPSTAT_CTS_ENABLE; 225 else 226 uport->status &= ~UPSTAT_CTS_ENABLE; 227 228 if (termios->c_cflag & CLOCAL) 229 uport->status &= ~UPSTAT_DCD_ENABLE; 230 else 231 uport->status |= UPSTAT_DCD_ENABLE; 232 233 /* reset sw-assisted CTS flow control based on (possibly) new mode */ 234 old_hw_stopped = uport->hw_stopped; 235 uport->hw_stopped = uart_softcts_mode(uport) && 236 !(uport->ops->get_mctrl(uport) & TIOCM_CTS); 237 if (uport->hw_stopped != old_hw_stopped) { 238 if (!old_hw_stopped) 239 uport->ops->stop_tx(uport); 240 else 241 __uart_start(state); 242 } 243 } 244 245 static int uart_alloc_xmit_buf(struct tty_port *port) 246 { 247 struct uart_state *state = container_of(port, struct uart_state, port); 248 struct uart_port *uport; 249 unsigned long flags; 250 unsigned long page; 251 252 /* 253 * Initialise and allocate the transmit and temporary 254 * buffer. 255 */ 256 page = get_zeroed_page(GFP_KERNEL); 257 if (!page) 258 return -ENOMEM; 259 260 uport = uart_port_ref_lock(state, &flags); 261 if (!state->port.xmit_buf) { 262 state->port.xmit_buf = (unsigned char *)page; 263 kfifo_init(&state->port.xmit_fifo, state->port.xmit_buf, 264 PAGE_SIZE); 265 uart_port_unlock_deref(uport, flags); 266 } else { 267 uart_port_unlock_deref(uport, flags); 268 /* 269 * Do not free() the page under the port lock, see 270 * uart_free_xmit_buf(). 271 */ 272 free_page(page); 273 } 274 275 return 0; 276 } 277 278 static void uart_free_xmit_buf(struct tty_port *port) 279 { 280 struct uart_state *state = container_of(port, struct uart_state, port); 281 struct uart_port *uport; 282 unsigned long flags; 283 char *xmit_buf; 284 285 /* 286 * Do not free() the transmit buffer page under the port lock since 287 * this can create various circular locking scenarios. For instance, 288 * console driver may need to allocate/free a debug object, which 289 * can end up in printk() recursion. 290 */ 291 uport = uart_port_ref_lock(state, &flags); 292 xmit_buf = port->xmit_buf; 293 port->xmit_buf = NULL; 294 INIT_KFIFO(port->xmit_fifo); 295 uart_port_unlock_deref(uport, flags); 296 297 free_page((unsigned long)xmit_buf); 298 } 299 300 /* 301 * Startup the port. This will be called once per open. All calls 302 * will be serialised by the per-port mutex. 303 */ 304 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state, 305 bool init_hw) 306 { 307 struct uart_port *uport = uart_port_check(state); 308 int retval; 309 310 if (uport->type == PORT_UNKNOWN) 311 return 1; 312 313 /* 314 * Make sure the device is in D0 state. 315 */ 316 uart_change_pm(state, UART_PM_STATE_ON); 317 318 retval = uart_alloc_xmit_buf(&state->port); 319 if (retval) 320 return retval; 321 322 retval = uport->ops->startup(uport); 323 if (retval == 0) { 324 if (uart_console(uport) && uport->cons->cflag) { 325 tty->termios.c_cflag = uport->cons->cflag; 326 tty->termios.c_ispeed = uport->cons->ispeed; 327 tty->termios.c_ospeed = uport->cons->ospeed; 328 uport->cons->cflag = 0; 329 uport->cons->ispeed = 0; 330 uport->cons->ospeed = 0; 331 } 332 /* 333 * Initialise the hardware port settings. 334 */ 335 uart_change_line_settings(tty, state, NULL); 336 337 /* 338 * Setup the RTS and DTR signals once the 339 * port is open and ready to respond. 340 */ 341 if (init_hw && C_BAUD(tty)) 342 uart_port_dtr_rts(uport, true); 343 } 344 345 /* 346 * This is to allow setserial on this port. People may want to set 347 * port/irq/type and then reconfigure the port properly if it failed 348 * now. 349 */ 350 if (retval && capable(CAP_SYS_ADMIN)) 351 return 1; 352 353 return retval; 354 } 355 356 static int uart_startup(struct tty_struct *tty, struct uart_state *state, 357 bool init_hw) 358 { 359 struct tty_port *port = &state->port; 360 struct uart_port *uport; 361 int retval; 362 363 if (tty_port_initialized(port)) 364 goto out_base_port_startup; 365 366 retval = uart_port_startup(tty, state, init_hw); 367 if (retval) { 368 set_bit(TTY_IO_ERROR, &tty->flags); 369 return retval; 370 } 371 372 out_base_port_startup: 373 uport = uart_port_check(state); 374 if (!uport) 375 return -EIO; 376 377 serial_base_port_startup(uport); 378 379 return 0; 380 } 381 382 /* 383 * This routine will shutdown a serial port; interrupts are disabled, and 384 * DTR is dropped if the hangup on close termio flag is on. Calls to 385 * uart_shutdown are serialised by the per-port semaphore. 386 * 387 * uport == NULL if uart_port has already been removed 388 */ 389 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state) 390 { 391 struct uart_port *uport = uart_port_check(state); 392 struct tty_port *port = &state->port; 393 394 /* 395 * Set the TTY IO error marker 396 */ 397 if (tty) 398 set_bit(TTY_IO_ERROR, &tty->flags); 399 400 if (uport) 401 serial_base_port_shutdown(uport); 402 403 if (tty_port_initialized(port)) { 404 tty_port_set_initialized(port, false); 405 406 /* 407 * Turn off DTR and RTS early. 408 */ 409 if (uport) { 410 if (uart_console(uport) && tty) { 411 uport->cons->cflag = tty->termios.c_cflag; 412 uport->cons->ispeed = tty->termios.c_ispeed; 413 uport->cons->ospeed = tty->termios.c_ospeed; 414 } 415 416 if (!tty || C_HUPCL(tty)) 417 uart_port_dtr_rts(uport, false); 418 } 419 420 uart_port_shutdown(port); 421 } 422 423 /* 424 * It's possible for shutdown to be called after suspend if we get 425 * a DCD drop (hangup) at just the right time. Clear suspended bit so 426 * we don't try to resume a port that has been shutdown. 427 */ 428 tty_port_set_suspended(port, false); 429 430 uart_free_xmit_buf(port); 431 } 432 433 /** 434 * uart_update_timeout - update per-port frame timing information 435 * @port: uart_port structure describing the port 436 * @cflag: termios cflag value 437 * @baud: speed of the port 438 * 439 * Set the @port frame timing information from which the FIFO timeout value is 440 * derived. The @cflag value should reflect the actual hardware settings as 441 * number of bits, parity, stop bits and baud rate is taken into account here. 442 * 443 * Locking: caller is expected to take @port->lock 444 */ 445 void 446 uart_update_timeout(struct uart_port *port, unsigned int cflag, 447 unsigned int baud) 448 { 449 u64 temp = tty_get_frame_size(cflag); 450 451 temp *= NSEC_PER_SEC; 452 port->frame_time = (unsigned int)DIV64_U64_ROUND_UP(temp, baud); 453 } 454 EXPORT_SYMBOL(uart_update_timeout); 455 456 /** 457 * uart_get_baud_rate - return baud rate for a particular port 458 * @port: uart_port structure describing the port in question. 459 * @termios: desired termios settings 460 * @old: old termios (or %NULL) 461 * @min: minimum acceptable baud rate 462 * @max: maximum acceptable baud rate 463 * 464 * Decode the termios structure into a numeric baud rate, taking account of the 465 * magic 38400 baud rate (with spd_* flags), and mapping the %B0 rate to 9600 466 * baud or min argument, whichever is greater. 467 * 468 * If the new baud rate is invalid, try the @old termios setting. If it's still 469 * invalid, clip to the nearest chip supported rate. 470 * 471 * The @termios structure is updated to reflect the baud rate we're actually 472 * going to be using. Don't do this for the case where B0 is requested ("hang 473 * up"). 474 * 475 * Locking: caller dependent 476 */ 477 unsigned int 478 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, 479 const struct ktermios *old, unsigned int min, unsigned int max) 480 { 481 unsigned int try; 482 unsigned int baud; 483 unsigned int altbaud; 484 upf_t flags = port->flags & UPF_SPD_MASK; 485 486 switch (flags) { 487 case UPF_SPD_HI: 488 altbaud = 57600; 489 break; 490 case UPF_SPD_VHI: 491 altbaud = 115200; 492 break; 493 case UPF_SPD_SHI: 494 altbaud = 230400; 495 break; 496 case UPF_SPD_WARP: 497 altbaud = 460800; 498 break; 499 default: 500 altbaud = 38400; 501 break; 502 } 503 504 for (try = 0; try < 2; try++) { 505 baud = tty_termios_baud_rate(termios); 506 507 /* 508 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge... 509 * Die! Die! Die! 510 */ 511 if (try == 0 && baud == 38400) 512 baud = altbaud; 513 514 /* 515 * Special case: B0 rate. 516 */ 517 if (baud == 0) 518 return max(min, 9600); 519 520 if (baud >= min && baud <= max) 521 return baud; 522 523 /* 524 * If the range cannot be met then try again with 525 * the old baud rate if possible. 526 */ 527 termios->c_cflag &= ~CBAUD; 528 if (old) { 529 baud = tty_termios_baud_rate(old); 530 tty_termios_encode_baud_rate(termios, baud, baud); 531 old = NULL; 532 continue; 533 } 534 535 /* 536 * As a last resort, if the range cannot be met then clip to 537 * the nearest chip supported rate. 538 */ 539 if (baud <= min) 540 baud = min + 1; 541 else 542 baud = max - 1; 543 544 tty_termios_encode_baud_rate(termios, baud, baud); 545 } 546 547 /* Should never happen */ 548 WARN_ON(1); 549 return 0; 550 } 551 EXPORT_SYMBOL(uart_get_baud_rate); 552 553 /** 554 * uart_get_divisor - return uart clock divisor 555 * @port: uart_port structure describing the port 556 * @baud: desired baud rate 557 * 558 * Calculate the divisor (baud_base / baud) for the specified @baud, 559 * appropriately rounded. 560 * 561 * If 38400 baud and custom divisor is selected, return the custom divisor 562 * instead. 563 * 564 * Locking: caller dependent 565 */ 566 unsigned int 567 uart_get_divisor(struct uart_port *port, unsigned int baud) 568 { 569 unsigned int quot; 570 571 /* 572 * Old custom speed handling. 573 */ 574 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) 575 quot = port->custom_divisor; 576 else 577 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud); 578 579 return quot; 580 } 581 EXPORT_SYMBOL(uart_get_divisor); 582 583 static int uart_put_char(struct tty_struct *tty, u8 c) 584 { 585 struct uart_state *state = tty->driver_data; 586 struct uart_port *port; 587 unsigned long flags; 588 int ret = 0; 589 590 port = uart_port_ref_lock(state, &flags); 591 if (!state->port.xmit_buf) { 592 uart_port_unlock_deref(port, flags); 593 return 0; 594 } 595 596 if (port) 597 ret = kfifo_put(&state->port.xmit_fifo, c); 598 uart_port_unlock_deref(port, flags); 599 return ret; 600 } 601 602 static void uart_flush_chars(struct tty_struct *tty) 603 { 604 uart_start(tty); 605 } 606 607 static ssize_t uart_write(struct tty_struct *tty, const u8 *buf, size_t count) 608 { 609 struct uart_state *state = tty->driver_data; 610 struct uart_port *port; 611 unsigned long flags; 612 int ret = 0; 613 614 /* 615 * This means you called this function _after_ the port was 616 * closed. No cookie for you. 617 */ 618 if (WARN_ON(!state)) 619 return -EL3HLT; 620 621 port = uart_port_ref_lock(state, &flags); 622 if (!state->port.xmit_buf) { 623 uart_port_unlock_deref(port, flags); 624 return 0; 625 } 626 627 if (port) 628 ret = kfifo_in(&state->port.xmit_fifo, buf, count); 629 630 __uart_start(state); 631 uart_port_unlock_deref(port, flags); 632 return ret; 633 } 634 635 static unsigned int uart_write_room(struct tty_struct *tty) 636 { 637 struct uart_state *state = tty->driver_data; 638 struct uart_port *port; 639 unsigned long flags; 640 unsigned int ret; 641 642 port = uart_port_ref_lock(state, &flags); 643 if (!state->port.xmit_buf) 644 ret = 0; 645 else 646 ret = kfifo_avail(&state->port.xmit_fifo); 647 uart_port_unlock_deref(port, flags); 648 return ret; 649 } 650 651 static unsigned int uart_chars_in_buffer(struct tty_struct *tty) 652 { 653 struct uart_state *state = tty->driver_data; 654 struct uart_port *port; 655 unsigned long flags; 656 unsigned int ret; 657 658 port = uart_port_ref_lock(state, &flags); 659 ret = kfifo_len(&state->port.xmit_fifo); 660 uart_port_unlock_deref(port, flags); 661 return ret; 662 } 663 664 static void uart_flush_buffer(struct tty_struct *tty) 665 { 666 struct uart_state *state = tty->driver_data; 667 struct uart_port *port; 668 unsigned long flags; 669 670 /* 671 * This means you called this function _after_ the port was 672 * closed. No cookie for you. 673 */ 674 if (WARN_ON(!state)) 675 return; 676 677 pr_debug("uart_flush_buffer(%d) called\n", tty->index); 678 679 port = uart_port_ref_lock(state, &flags); 680 if (!port) 681 return; 682 kfifo_reset(&state->port.xmit_fifo); 683 if (port->ops->flush_buffer) 684 port->ops->flush_buffer(port); 685 uart_port_unlock_deref(port, flags); 686 tty_port_tty_wakeup(&state->port); 687 } 688 689 /* 690 * This function performs low-level write of high-priority XON/XOFF 691 * character and accounting for it. 692 * 693 * Requires uart_port to implement .serial_out(). 694 */ 695 void uart_xchar_out(struct uart_port *uport, int offset) 696 { 697 serial_port_out(uport, offset, uport->x_char); 698 uport->icount.tx++; 699 uport->x_char = 0; 700 } 701 EXPORT_SYMBOL_GPL(uart_xchar_out); 702 703 /* 704 * This function is used to send a high-priority XON/XOFF character to 705 * the device 706 */ 707 static void uart_send_xchar(struct tty_struct *tty, u8 ch) 708 { 709 struct uart_state *state = tty->driver_data; 710 struct uart_port *port; 711 712 port = uart_port_ref(state); 713 if (!port) 714 return; 715 716 if (port->ops->send_xchar) 717 port->ops->send_xchar(port, ch); 718 else { 719 guard(uart_port_lock_irqsave)(port); 720 port->x_char = ch; 721 if (ch) 722 port->ops->start_tx(port); 723 } 724 uart_port_deref(port); 725 } 726 727 static void uart_throttle(struct tty_struct *tty) 728 { 729 struct uart_state *state = tty->driver_data; 730 upstat_t mask = UPSTAT_SYNC_FIFO; 731 struct uart_port *port; 732 733 port = uart_port_ref(state); 734 if (!port) 735 return; 736 737 if (I_IXOFF(tty)) 738 mask |= UPSTAT_AUTOXOFF; 739 if (C_CRTSCTS(tty)) 740 mask |= UPSTAT_AUTORTS; 741 742 if (port->status & mask) { 743 port->ops->throttle(port); 744 mask &= ~port->status; 745 } 746 747 if (mask & UPSTAT_AUTORTS) 748 uart_clear_mctrl(port, TIOCM_RTS); 749 750 if (mask & UPSTAT_AUTOXOFF) 751 uart_send_xchar(tty, STOP_CHAR(tty)); 752 753 uart_port_deref(port); 754 } 755 756 static void uart_unthrottle(struct tty_struct *tty) 757 { 758 struct uart_state *state = tty->driver_data; 759 upstat_t mask = UPSTAT_SYNC_FIFO; 760 struct uart_port *port; 761 762 port = uart_port_ref(state); 763 if (!port) 764 return; 765 766 if (I_IXOFF(tty)) 767 mask |= UPSTAT_AUTOXOFF; 768 if (C_CRTSCTS(tty)) 769 mask |= UPSTAT_AUTORTS; 770 771 if (port->status & mask) { 772 port->ops->unthrottle(port); 773 mask &= ~port->status; 774 } 775 776 if (mask & UPSTAT_AUTORTS) 777 uart_set_mctrl(port, TIOCM_RTS); 778 779 if (mask & UPSTAT_AUTOXOFF) 780 uart_send_xchar(tty, START_CHAR(tty)); 781 782 uart_port_deref(port); 783 } 784 785 static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo) 786 { 787 struct uart_state *state = container_of(port, struct uart_state, port); 788 struct uart_port *uport; 789 790 /* Initialize structure in case we error out later to prevent any stack info leakage. */ 791 *retinfo = (struct serial_struct){}; 792 793 /* 794 * Ensure the state we copy is consistent and no hardware changes 795 * occur as we go 796 */ 797 guard(mutex)(&port->mutex); 798 uport = uart_port_check(state); 799 if (!uport) 800 return -ENODEV; 801 802 retinfo->type = uport->type; 803 retinfo->line = uport->line; 804 retinfo->port = uport->iobase; 805 if (HIGH_BITS_OFFSET) 806 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET; 807 retinfo->irq = uport->irq; 808 retinfo->flags = (__force int)uport->flags; 809 retinfo->xmit_fifo_size = uport->fifosize; 810 retinfo->baud_base = uport->uartclk / 16; 811 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10; 812 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 813 ASYNC_CLOSING_WAIT_NONE : 814 jiffies_to_msecs(port->closing_wait) / 10; 815 retinfo->custom_divisor = uport->custom_divisor; 816 retinfo->hub6 = uport->hub6; 817 retinfo->io_type = uport->iotype; 818 retinfo->iomem_reg_shift = uport->regshift; 819 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase; 820 821 return 0; 822 } 823 824 static int uart_get_info_user(struct tty_struct *tty, 825 struct serial_struct *ss) 826 { 827 struct uart_state *state = tty->driver_data; 828 struct tty_port *port = &state->port; 829 830 return uart_get_info(port, ss) < 0 ? -EIO : 0; 831 } 832 833 static int uart_change_port(struct uart_port *uport, 834 const struct serial_struct *new_info, 835 unsigned long new_port) 836 { 837 unsigned long old_iobase, old_mapbase; 838 unsigned int old_type, old_iotype, old_hub6, old_shift; 839 int retval; 840 841 old_iobase = uport->iobase; 842 old_mapbase = uport->mapbase; 843 old_type = uport->type; 844 old_hub6 = uport->hub6; 845 old_iotype = uport->iotype; 846 old_shift = uport->regshift; 847 848 if (old_type != PORT_UNKNOWN && uport->ops->release_port) 849 uport->ops->release_port(uport); 850 851 uport->iobase = new_port; 852 uport->type = new_info->type; 853 uport->hub6 = new_info->hub6; 854 uport->iotype = new_info->io_type; 855 uport->regshift = new_info->iomem_reg_shift; 856 uport->mapbase = (unsigned long)new_info->iomem_base; 857 858 if (uport->type == PORT_UNKNOWN || !uport->ops->request_port) 859 return 0; 860 861 retval = uport->ops->request_port(uport); 862 if (retval == 0) 863 return 0; /* succeeded => done */ 864 865 /* 866 * If we fail to request resources for the new port, try to restore the 867 * old settings. 868 */ 869 uport->iobase = old_iobase; 870 uport->type = old_type; 871 uport->hub6 = old_hub6; 872 uport->iotype = old_iotype; 873 uport->regshift = old_shift; 874 uport->mapbase = old_mapbase; 875 876 if (old_type == PORT_UNKNOWN) 877 return retval; 878 879 retval = uport->ops->request_port(uport); 880 /* If we failed to restore the old settings, we fail like this. */ 881 if (retval) 882 uport->type = PORT_UNKNOWN; 883 884 /* We failed anyway. */ 885 return -EBUSY; 886 } 887 888 static int uart_set_info(struct tty_struct *tty, struct tty_port *port, 889 struct uart_state *state, 890 struct serial_struct *new_info) 891 { 892 struct uart_port *uport = uart_port_check(state); 893 unsigned long new_port; 894 unsigned int old_custom_divisor, close_delay, closing_wait; 895 bool change_irq, change_port; 896 upf_t old_flags, new_flags; 897 int retval; 898 899 if (!uport) 900 return -EIO; 901 902 new_port = new_info->port; 903 if (HIGH_BITS_OFFSET) 904 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET; 905 906 new_info->irq = irq_canonicalize(new_info->irq); 907 close_delay = msecs_to_jiffies(new_info->close_delay * 10); 908 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 909 ASYNC_CLOSING_WAIT_NONE : 910 msecs_to_jiffies(new_info->closing_wait * 10); 911 912 913 change_irq = !(uport->flags & UPF_FIXED_PORT) 914 && new_info->irq != uport->irq; 915 916 /* 917 * Since changing the 'type' of the port changes its resource 918 * allocations, we should treat type changes the same as 919 * IO port changes. 920 */ 921 change_port = !(uport->flags & UPF_FIXED_PORT) 922 && (new_port != uport->iobase || 923 (unsigned long)new_info->iomem_base != uport->mapbase || 924 new_info->hub6 != uport->hub6 || 925 new_info->io_type != uport->iotype || 926 new_info->iomem_reg_shift != uport->regshift || 927 new_info->type != uport->type); 928 929 old_flags = uport->flags; 930 new_flags = (__force upf_t)new_info->flags; 931 old_custom_divisor = uport->custom_divisor; 932 933 if (!(uport->flags & UPF_FIXED_PORT)) { 934 unsigned int uartclk = new_info->baud_base * 16; 935 /* check needs to be done here before other settings made */ 936 if (uartclk == 0) 937 return -EINVAL; 938 } 939 if (!capable(CAP_SYS_ADMIN)) { 940 if (change_irq || change_port || 941 (new_info->baud_base != uport->uartclk / 16) || 942 (close_delay != port->close_delay) || 943 (closing_wait != port->closing_wait) || 944 (new_info->xmit_fifo_size && 945 new_info->xmit_fifo_size != uport->fifosize) || 946 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0)) 947 return -EPERM; 948 uport->flags = ((uport->flags & ~UPF_USR_MASK) | 949 (new_flags & UPF_USR_MASK)); 950 uport->custom_divisor = new_info->custom_divisor; 951 goto check_and_exit; 952 } 953 954 if (change_irq || change_port) { 955 retval = security_locked_down(LOCKDOWN_TIOCSSERIAL); 956 if (retval) 957 return retval; 958 } 959 960 /* Ask the low level driver to verify the settings. */ 961 if (uport->ops->verify_port) { 962 retval = uport->ops->verify_port(uport, new_info); 963 if (retval) 964 return retval; 965 } 966 967 if ((new_info->irq >= irq_get_nr_irqs()) || (new_info->irq < 0) || 968 (new_info->baud_base < 9600)) 969 return -EINVAL; 970 971 if (change_port || change_irq) { 972 /* Make sure that we are the sole user of this port. */ 973 if (tty_port_users(port) > 1) 974 return -EBUSY; 975 976 /* 977 * We need to shutdown the serial port at the old 978 * port/type/irq combination. 979 */ 980 uart_shutdown(tty, state); 981 } 982 983 if (change_port) { 984 retval = uart_change_port(uport, new_info, new_port); 985 if (retval) 986 return retval; 987 } 988 989 if (change_irq) 990 uport->irq = new_info->irq; 991 if (!(uport->flags & UPF_FIXED_PORT)) 992 uport->uartclk = new_info->baud_base * 16; 993 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) | 994 (new_flags & UPF_CHANGE_MASK); 995 uport->custom_divisor = new_info->custom_divisor; 996 port->close_delay = close_delay; 997 port->closing_wait = closing_wait; 998 if (new_info->xmit_fifo_size) 999 uport->fifosize = new_info->xmit_fifo_size; 1000 1001 check_and_exit: 1002 if (uport->type == PORT_UNKNOWN) 1003 return 0; 1004 1005 if (tty_port_initialized(port)) { 1006 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) || 1007 old_custom_divisor != uport->custom_divisor) { 1008 /* 1009 * If they're setting up a custom divisor or speed, 1010 * instead of clearing it, then bitch about it. 1011 */ 1012 if (uport->flags & UPF_SPD_MASK) { 1013 dev_notice_ratelimited(uport->dev, 1014 "%s sets custom speed on %s. This is deprecated.\n", 1015 current->comm, 1016 tty_name(port->tty)); 1017 } 1018 uart_change_line_settings(tty, state, NULL); 1019 } 1020 1021 return 0; 1022 } 1023 1024 retval = uart_startup(tty, state, true); 1025 if (retval < 0) 1026 return retval; 1027 if (retval == 0) 1028 tty_port_set_initialized(port, true); 1029 1030 return 0; 1031 } 1032 1033 static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss) 1034 { 1035 struct uart_state *state = tty->driver_data; 1036 struct tty_port *port = &state->port; 1037 1038 guard(rwsem_write)(&tty->termios_rwsem); 1039 /* 1040 * This semaphore protects port->count. It is also 1041 * very useful to prevent opens. Also, take the 1042 * port configuration semaphore to make sure that a 1043 * module insertion/removal doesn't change anything 1044 * under us. 1045 */ 1046 guard(mutex)(&port->mutex); 1047 return uart_set_info(tty, port, state, ss); 1048 } 1049 1050 /** 1051 * uart_get_lsr_info - get line status register info 1052 * @tty: tty associated with the UART 1053 * @state: UART being queried 1054 * @value: returned modem value 1055 */ 1056 static int uart_get_lsr_info(struct tty_struct *tty, 1057 struct uart_state *state, unsigned int __user *value) 1058 { 1059 struct uart_port *uport = uart_port_check(state); 1060 unsigned int result; 1061 1062 result = uport->ops->tx_empty(uport); 1063 1064 /* 1065 * If we're about to load something into the transmit 1066 * register, we'll pretend the transmitter isn't empty to 1067 * avoid a race condition (depending on when the transmit 1068 * interrupt happens). 1069 */ 1070 if (uport->x_char || 1071 (!kfifo_is_empty(&state->port.xmit_fifo) && 1072 !uart_tx_stopped(uport))) 1073 result &= ~TIOCSER_TEMT; 1074 1075 return put_user(result, value); 1076 } 1077 1078 static int uart_tiocmget(struct tty_struct *tty) 1079 { 1080 struct uart_state *state = tty->driver_data; 1081 struct tty_port *port = &state->port; 1082 struct uart_port *uport; 1083 1084 guard(mutex)(&port->mutex); 1085 1086 uport = uart_port_check(state); 1087 if (!uport || tty_io_error(tty)) 1088 return -EIO; 1089 1090 guard(uart_port_lock_irq)(uport); 1091 1092 return uport->mctrl | uport->ops->get_mctrl(uport); 1093 } 1094 1095 static int 1096 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) 1097 { 1098 struct uart_state *state = tty->driver_data; 1099 struct tty_port *port = &state->port; 1100 struct uart_port *uport; 1101 1102 guard(mutex)(&port->mutex); 1103 1104 uport = uart_port_check(state); 1105 if (!uport || tty_io_error(tty)) 1106 return -EIO; 1107 1108 uart_update_mctrl(uport, set, clear); 1109 1110 return 0; 1111 } 1112 1113 static int uart_break_ctl(struct tty_struct *tty, int break_state) 1114 { 1115 struct uart_state *state = tty->driver_data; 1116 struct tty_port *port = &state->port; 1117 struct uart_port *uport; 1118 1119 guard(mutex)(&port->mutex); 1120 1121 uport = uart_port_check(state); 1122 if (!uport) 1123 return -EIO; 1124 1125 if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl) 1126 uport->ops->break_ctl(uport, break_state); 1127 1128 return 0; 1129 } 1130 1131 static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state) 1132 { 1133 struct tty_port *port = &state->port; 1134 struct uart_port *uport; 1135 int flags, ret; 1136 1137 if (!capable(CAP_SYS_ADMIN)) 1138 return -EPERM; 1139 1140 /* 1141 * Take the per-port semaphore. This prevents count from 1142 * changing, and hence any extra opens of the port while 1143 * we're auto-configuring. 1144 */ 1145 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->mutex) { 1146 uport = uart_port_check(state); 1147 if (!uport) 1148 return -EIO; 1149 1150 if (tty_port_users(port) != 1) 1151 return -EBUSY; 1152 1153 uart_shutdown(tty, state); 1154 1155 /* 1156 * If we already have a port type configured, 1157 * we must release its resources. 1158 */ 1159 if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 1160 uport->ops->release_port(uport); 1161 1162 flags = UART_CONFIG_TYPE; 1163 if (uport->flags & UPF_AUTO_IRQ) 1164 flags |= UART_CONFIG_IRQ; 1165 1166 /* 1167 * This will claim the ports resources if 1168 * a port is found. 1169 */ 1170 uport->ops->config_port(uport, flags); 1171 1172 ret = uart_startup(tty, state, true); 1173 if (ret < 0) 1174 return ret; 1175 if (ret > 0) 1176 return 0; 1177 1178 tty_port_set_initialized(port, true); 1179 } 1180 1181 return 0; 1182 } 1183 1184 static void uart_enable_ms(struct uart_port *uport) 1185 { 1186 /* 1187 * Force modem status interrupts on 1188 */ 1189 if (uport->ops->enable_ms) 1190 uport->ops->enable_ms(uport); 1191 } 1192 1193 /* 1194 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 1195 * - mask passed in arg for lines of interest 1196 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 1197 * Caller should use TIOCGICOUNT to see which one it was 1198 * 1199 * FIXME: This wants extracting into a common all driver implementation 1200 * of TIOCMWAIT using tty_port. 1201 */ 1202 static int uart_wait_modem_status(struct uart_state *state, unsigned long arg) 1203 { 1204 struct uart_port *uport; 1205 struct tty_port *port = &state->port; 1206 DECLARE_WAITQUEUE(wait, current); 1207 struct uart_icount cprev, cnow; 1208 int ret; 1209 1210 /* 1211 * note the counters on entry 1212 */ 1213 uport = uart_port_ref(state); 1214 if (!uport) 1215 return -EIO; 1216 scoped_guard(uart_port_lock_irq, uport) { 1217 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); 1218 uart_enable_ms(uport); 1219 } 1220 1221 add_wait_queue(&port->delta_msr_wait, &wait); 1222 for (;;) { 1223 scoped_guard(uart_port_lock_irq, uport) 1224 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1225 1226 set_current_state(TASK_INTERRUPTIBLE); 1227 1228 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1229 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1230 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 1231 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 1232 ret = 0; 1233 break; 1234 } 1235 1236 schedule(); 1237 1238 /* see if a signal did it */ 1239 if (signal_pending(current)) { 1240 ret = -ERESTARTSYS; 1241 break; 1242 } 1243 1244 cprev = cnow; 1245 } 1246 __set_current_state(TASK_RUNNING); 1247 remove_wait_queue(&port->delta_msr_wait, &wait); 1248 uart_port_deref(uport); 1249 1250 return ret; 1251 } 1252 1253 /* 1254 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) 1255 * Return: write counters to the user passed counter struct 1256 * NB: both 1->0 and 0->1 transitions are counted except for 1257 * RI where only 0->1 is counted. 1258 */ 1259 static int uart_get_icount(struct tty_struct *tty, 1260 struct serial_icounter_struct *icount) 1261 { 1262 struct uart_state *state = tty->driver_data; 1263 struct uart_icount cnow; 1264 struct uart_port *uport; 1265 unsigned long flags; 1266 1267 uport = uart_port_ref_lock(state, &flags); 1268 if (!uport) 1269 return -EIO; 1270 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1271 uart_port_unlock_deref(uport, flags); 1272 1273 icount->cts = cnow.cts; 1274 icount->dsr = cnow.dsr; 1275 icount->rng = cnow.rng; 1276 icount->dcd = cnow.dcd; 1277 icount->rx = cnow.rx; 1278 icount->tx = cnow.tx; 1279 icount->frame = cnow.frame; 1280 icount->overrun = cnow.overrun; 1281 icount->parity = cnow.parity; 1282 icount->brk = cnow.brk; 1283 icount->buf_overrun = cnow.buf_overrun; 1284 1285 return 0; 1286 } 1287 1288 #define SER_RS485_LEGACY_FLAGS (SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \ 1289 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \ 1290 SER_RS485_TERMINATE_BUS) 1291 1292 static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485) 1293 { 1294 u32 flags = rs485->flags; 1295 1296 /* Don't return -EINVAL for unsupported legacy flags */ 1297 flags &= ~SER_RS485_LEGACY_FLAGS; 1298 1299 /* 1300 * For any bit outside of the legacy ones that is not supported by 1301 * the driver, return -EINVAL. 1302 */ 1303 if (flags & ~port->rs485_supported.flags) 1304 return -EINVAL; 1305 1306 /* Asking for address w/o addressing mode? */ 1307 if (!(rs485->flags & SER_RS485_ADDRB) && 1308 (rs485->flags & (SER_RS485_ADDR_RECV|SER_RS485_ADDR_DEST))) 1309 return -EINVAL; 1310 1311 /* Address given but not enabled? */ 1312 if (!(rs485->flags & SER_RS485_ADDR_RECV) && rs485->addr_recv) 1313 return -EINVAL; 1314 if (!(rs485->flags & SER_RS485_ADDR_DEST) && rs485->addr_dest) 1315 return -EINVAL; 1316 1317 return 0; 1318 } 1319 1320 static void uart_sanitize_serial_rs485_delays(struct uart_port *port, 1321 struct serial_rs485 *rs485) 1322 { 1323 if (!port->rs485_supported.delay_rts_before_send) { 1324 if (rs485->delay_rts_before_send) { 1325 dev_warn_ratelimited(port->dev, 1326 "%s (%u): RTS delay before sending not supported\n", 1327 port->name, port->line); 1328 } 1329 rs485->delay_rts_before_send = 0; 1330 } else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) { 1331 rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY; 1332 dev_warn_ratelimited(port->dev, 1333 "%s (%u): RTS delay before sending clamped to %u ms\n", 1334 port->name, port->line, rs485->delay_rts_before_send); 1335 } 1336 1337 if (!port->rs485_supported.delay_rts_after_send) { 1338 if (rs485->delay_rts_after_send) { 1339 dev_warn_ratelimited(port->dev, 1340 "%s (%u): RTS delay after sending not supported\n", 1341 port->name, port->line); 1342 } 1343 rs485->delay_rts_after_send = 0; 1344 } else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) { 1345 rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY; 1346 dev_warn_ratelimited(port->dev, 1347 "%s (%u): RTS delay after sending clamped to %u ms\n", 1348 port->name, port->line, rs485->delay_rts_after_send); 1349 } 1350 } 1351 1352 static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485) 1353 { 1354 u32 supported_flags = port->rs485_supported.flags; 1355 1356 if (!(rs485->flags & SER_RS485_ENABLED)) { 1357 memset(rs485, 0, sizeof(*rs485)); 1358 return; 1359 } 1360 1361 /* Clear other RS485 flags but SER_RS485_TERMINATE_BUS and return if enabling RS422 */ 1362 if (rs485->flags & SER_RS485_MODE_RS422) { 1363 rs485->flags &= (SER_RS485_ENABLED | SER_RS485_MODE_RS422 | SER_RS485_TERMINATE_BUS); 1364 return; 1365 } 1366 1367 rs485->flags &= supported_flags; 1368 1369 /* Pick sane settings if the user hasn't */ 1370 if (!(rs485->flags & SER_RS485_RTS_ON_SEND) == 1371 !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) { 1372 if (supported_flags & SER_RS485_RTS_ON_SEND) { 1373 rs485->flags |= SER_RS485_RTS_ON_SEND; 1374 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND; 1375 1376 dev_warn_ratelimited(port->dev, 1377 "%s (%u): invalid RTS setting, using RTS_ON_SEND instead\n", 1378 port->name, port->line); 1379 } else { 1380 rs485->flags |= SER_RS485_RTS_AFTER_SEND; 1381 rs485->flags &= ~SER_RS485_RTS_ON_SEND; 1382 1383 dev_warn_ratelimited(port->dev, 1384 "%s (%u): invalid RTS setting, using RTS_AFTER_SEND instead\n", 1385 port->name, port->line); 1386 } 1387 } 1388 1389 uart_sanitize_serial_rs485_delays(port, rs485); 1390 1391 /* Return clean padding area to userspace */ 1392 memset(rs485->padding0, 0, sizeof(rs485->padding0)); 1393 memset(rs485->padding1, 0, sizeof(rs485->padding1)); 1394 } 1395 1396 static void uart_set_rs485_termination(struct uart_port *port, 1397 const struct serial_rs485 *rs485) 1398 { 1399 if (!(rs485->flags & SER_RS485_ENABLED)) 1400 return; 1401 1402 gpiod_set_value_cansleep(port->rs485_term_gpio, 1403 !!(rs485->flags & SER_RS485_TERMINATE_BUS)); 1404 } 1405 1406 static void uart_set_rs485_rx_during_tx(struct uart_port *port, 1407 const struct serial_rs485 *rs485) 1408 { 1409 if (!(rs485->flags & SER_RS485_ENABLED)) 1410 return; 1411 1412 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 1413 !!(rs485->flags & SER_RS485_RX_DURING_TX)); 1414 } 1415 1416 static int uart_rs485_config(struct uart_port *port) 1417 { 1418 struct serial_rs485 *rs485 = &port->rs485; 1419 int ret; 1420 1421 if (!(rs485->flags & SER_RS485_ENABLED)) 1422 return 0; 1423 1424 uart_sanitize_serial_rs485(port, rs485); 1425 uart_set_rs485_termination(port, rs485); 1426 uart_set_rs485_rx_during_tx(port, rs485); 1427 1428 scoped_guard(uart_port_lock_irqsave, port) 1429 ret = port->rs485_config(port, NULL, rs485); 1430 if (ret) { 1431 memset(rs485, 0, sizeof(*rs485)); 1432 /* unset GPIOs */ 1433 gpiod_set_value_cansleep(port->rs485_term_gpio, 0); 1434 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0); 1435 } 1436 1437 return ret; 1438 } 1439 1440 static int uart_get_rs485_config(struct uart_port *port, 1441 struct serial_rs485 __user *rs485) 1442 { 1443 struct serial_rs485 aux; 1444 1445 scoped_guard(uart_port_lock_irqsave, port) 1446 aux = port->rs485; 1447 1448 if (copy_to_user(rs485, &aux, sizeof(aux))) 1449 return -EFAULT; 1450 1451 return 0; 1452 } 1453 1454 static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port, 1455 struct serial_rs485 __user *rs485_user) 1456 { 1457 struct serial_rs485 rs485; 1458 int ret; 1459 1460 if (!(port->rs485_supported.flags & SER_RS485_ENABLED)) 1461 return -ENOTTY; 1462 1463 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user))) 1464 return -EFAULT; 1465 1466 ret = uart_check_rs485_flags(port, &rs485); 1467 if (ret) 1468 return ret; 1469 uart_sanitize_serial_rs485(port, &rs485); 1470 uart_set_rs485_termination(port, &rs485); 1471 uart_set_rs485_rx_during_tx(port, &rs485); 1472 1473 scoped_guard(uart_port_lock_irqsave, port) { 1474 ret = port->rs485_config(port, &tty->termios, &rs485); 1475 if (!ret) { 1476 port->rs485 = rs485; 1477 1478 /* Reset RTS and other mctrl lines when disabling RS485 */ 1479 if (!(rs485.flags & SER_RS485_ENABLED)) 1480 port->ops->set_mctrl(port, port->mctrl); 1481 } 1482 } 1483 if (ret) { 1484 /* restore old GPIO settings */ 1485 gpiod_set_value_cansleep(port->rs485_term_gpio, 1486 !!(port->rs485.flags & SER_RS485_TERMINATE_BUS)); 1487 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 1488 !!(port->rs485.flags & SER_RS485_RX_DURING_TX)); 1489 return ret; 1490 } 1491 1492 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485))) 1493 return -EFAULT; 1494 1495 return 0; 1496 } 1497 1498 static int uart_get_iso7816_config(struct uart_port *port, 1499 struct serial_iso7816 __user *iso7816) 1500 { 1501 struct serial_iso7816 aux; 1502 1503 if (!port->iso7816_config) 1504 return -ENOTTY; 1505 1506 scoped_guard(uart_port_lock_irqsave, port) 1507 aux = port->iso7816; 1508 1509 if (copy_to_user(iso7816, &aux, sizeof(aux))) 1510 return -EFAULT; 1511 1512 return 0; 1513 } 1514 1515 static int uart_set_iso7816_config(struct uart_port *port, 1516 struct serial_iso7816 __user *iso7816_user) 1517 { 1518 struct serial_iso7816 iso7816; 1519 int i; 1520 1521 if (!port->iso7816_config) 1522 return -ENOTTY; 1523 1524 if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user))) 1525 return -EFAULT; 1526 1527 /* 1528 * There are 5 words reserved for future use. Check that userspace 1529 * doesn't put stuff in there to prevent breakages in the future. 1530 */ 1531 for (i = 0; i < ARRAY_SIZE(iso7816.reserved); i++) 1532 if (iso7816.reserved[i]) 1533 return -EINVAL; 1534 1535 scoped_guard(uart_port_lock_irqsave, port) { 1536 int ret = port->iso7816_config(port, &iso7816); 1537 if (ret) 1538 return ret; 1539 } 1540 1541 if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816))) 1542 return -EFAULT; 1543 1544 return 0; 1545 } 1546 1547 /* 1548 * Called via sys_ioctl. We can use spin_lock_irq() here. 1549 */ 1550 static int 1551 uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) 1552 { 1553 struct uart_state *state = tty->driver_data; 1554 struct tty_port *port = &state->port; 1555 struct uart_port *uport; 1556 void __user *uarg = (void __user *)arg; 1557 int ret = -ENOIOCTLCMD; 1558 1559 /* This ioctl doesn't rely on the hardware to be present. */ 1560 if (cmd == TIOCSERCONFIG) { 1561 guard(rwsem_write)(&tty->termios_rwsem); 1562 return uart_do_autoconfig(tty, state); 1563 } 1564 1565 if (tty_io_error(tty)) 1566 return -EIO; 1567 1568 /* This should only be used when the hardware is present. */ 1569 if (cmd == TIOCMIWAIT) 1570 return uart_wait_modem_status(state, arg); 1571 1572 /* rs485_config requires more locking than others */ 1573 if (cmd == TIOCSRS485) 1574 down_write(&tty->termios_rwsem); 1575 1576 scoped_guard(mutex, &port->mutex) { 1577 uport = uart_port_check(state); 1578 1579 if (!uport || tty_io_error(tty)) { 1580 ret = -EIO; 1581 break; 1582 } 1583 1584 /* 1585 * All these rely on hardware being present and need to be 1586 * protected against the tty being hung up. 1587 */ 1588 1589 switch (cmd) { 1590 case TIOCSERGETLSR: /* Get line status register */ 1591 ret = uart_get_lsr_info(tty, state, uarg); 1592 break; 1593 1594 case TIOCGRS485: 1595 ret = uart_get_rs485_config(uport, uarg); 1596 break; 1597 1598 case TIOCSRS485: 1599 ret = uart_set_rs485_config(tty, uport, uarg); 1600 break; 1601 1602 case TIOCSISO7816: 1603 ret = uart_set_iso7816_config(state->uart_port, uarg); 1604 break; 1605 1606 case TIOCGISO7816: 1607 ret = uart_get_iso7816_config(state->uart_port, uarg); 1608 break; 1609 default: 1610 if (uport->ops->ioctl) 1611 ret = uport->ops->ioctl(uport, cmd, arg); 1612 break; 1613 } 1614 } 1615 1616 if (cmd == TIOCSRS485) 1617 up_write(&tty->termios_rwsem); 1618 1619 return ret; 1620 } 1621 1622 static void uart_set_ldisc(struct tty_struct *tty) 1623 { 1624 struct uart_state *state = tty->driver_data; 1625 struct uart_port *uport; 1626 struct tty_port *port = &state->port; 1627 1628 if (!tty_port_initialized(port)) 1629 return; 1630 1631 guard(mutex)(&state->port.mutex); 1632 uport = uart_port_check(state); 1633 if (uport && uport->ops->set_ldisc) 1634 uport->ops->set_ldisc(uport, &tty->termios); 1635 } 1636 1637 static void uart_set_termios(struct tty_struct *tty, 1638 const struct ktermios *old_termios) 1639 { 1640 struct uart_state *state = tty->driver_data; 1641 struct uart_port *uport; 1642 unsigned int cflag = tty->termios.c_cflag; 1643 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK; 1644 bool sw_changed = false; 1645 1646 guard(mutex)(&state->port.mutex); 1647 1648 uport = uart_port_check(state); 1649 if (!uport) 1650 return; 1651 1652 /* 1653 * Drivers doing software flow control also need to know 1654 * about changes to these input settings. 1655 */ 1656 if (uport->flags & UPF_SOFT_FLOW) { 1657 iflag_mask |= IXANY|IXON|IXOFF; 1658 sw_changed = 1659 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] || 1660 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP]; 1661 } 1662 1663 /* 1664 * These are the bits that are used to setup various 1665 * flags in the low level driver. We can ignore the Bfoo 1666 * bits in c_cflag; c_[io]speed will always be set 1667 * appropriately by set_termios() in tty_ioctl.c 1668 */ 1669 if ((cflag ^ old_termios->c_cflag) == 0 && 1670 tty->termios.c_ospeed == old_termios->c_ospeed && 1671 tty->termios.c_ispeed == old_termios->c_ispeed && 1672 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 && 1673 !sw_changed) 1674 return; 1675 1676 uart_change_line_settings(tty, state, old_termios); 1677 /* reload cflag from termios; port driver may have overridden flags */ 1678 cflag = tty->termios.c_cflag; 1679 1680 /* Handle transition to B0 status */ 1681 if (((old_termios->c_cflag & CBAUD) != B0) && ((cflag & CBAUD) == B0)) 1682 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR); 1683 /* Handle transition away from B0 status */ 1684 else if (((old_termios->c_cflag & CBAUD) == B0) && ((cflag & CBAUD) != B0)) { 1685 unsigned int mask = TIOCM_DTR; 1686 1687 if (!(cflag & CRTSCTS) || !tty_throttled(tty)) 1688 mask |= TIOCM_RTS; 1689 uart_set_mctrl(uport, mask); 1690 } 1691 } 1692 1693 /* 1694 * Calls to uart_close() are serialised via the tty_lock in 1695 * drivers/tty/tty_io.c:tty_release() 1696 * drivers/tty/tty_io.c:do_tty_hangup() 1697 */ 1698 static void uart_close(struct tty_struct *tty, struct file *filp) 1699 { 1700 struct uart_state *state = tty->driver_data; 1701 1702 if (!state) { 1703 struct uart_driver *drv = tty->driver->driver_state; 1704 struct tty_port *port; 1705 1706 state = drv->state + tty->index; 1707 port = &state->port; 1708 guard(spinlock_irq)(&port->lock); 1709 --port->count; 1710 return; 1711 } 1712 1713 pr_debug("uart_close(%d) called\n", tty->index); 1714 1715 tty_port_close(tty->port, tty, filp); 1716 } 1717 1718 static void uart_tty_port_shutdown(struct tty_port *port) 1719 { 1720 struct uart_state *state = container_of(port, struct uart_state, port); 1721 struct uart_port *uport = uart_port_check(state); 1722 1723 /* 1724 * At this point, we stop accepting input. To do this, we 1725 * disable the receive line status interrupts. 1726 */ 1727 if (WARN(!uport, "detached port still initialized!\n")) 1728 return; 1729 1730 scoped_guard(uart_port_lock_irq, uport) 1731 uport->ops->stop_rx(uport); 1732 1733 serial_base_port_shutdown(uport); 1734 uart_port_shutdown(port); 1735 1736 /* 1737 * It's possible for shutdown to be called after suspend if we get 1738 * a DCD drop (hangup) at just the right time. Clear suspended bit so 1739 * we don't try to resume a port that has been shutdown. 1740 */ 1741 tty_port_set_suspended(port, false); 1742 1743 uart_free_xmit_buf(port); 1744 1745 uart_change_pm(state, UART_PM_STATE_OFF); 1746 } 1747 1748 static void uart_wait_until_sent(struct tty_struct *tty, int timeout) 1749 { 1750 struct uart_state *state = tty->driver_data; 1751 struct uart_port *port; 1752 unsigned long char_time, expire, fifo_timeout; 1753 1754 port = uart_port_ref(state); 1755 if (!port) 1756 return; 1757 1758 if (port->type == PORT_UNKNOWN || port->fifosize == 0) { 1759 uart_port_deref(port); 1760 return; 1761 } 1762 1763 /* 1764 * Set the check interval to be 1/5 of the estimated time to 1765 * send a single character, and make it at least 1. The check 1766 * interval should also be less than the timeout. 1767 * 1768 * Note: we have to use pretty tight timings here to satisfy 1769 * the NIST-PCTS. 1770 */ 1771 char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL); 1772 1773 if (timeout && timeout < char_time) 1774 char_time = timeout; 1775 1776 if (!uart_cts_enabled(port)) { 1777 /* 1778 * If the transmitter hasn't cleared in twice the approximate 1779 * amount of time to send the entire FIFO, it probably won't 1780 * ever clear. This assumes the UART isn't doing flow 1781 * control, which is currently the case. Hence, if it ever 1782 * takes longer than FIFO timeout, this is probably due to a 1783 * UART bug of some kind. So, we clamp the timeout parameter at 1784 * 2 * FIFO timeout. 1785 */ 1786 fifo_timeout = uart_fifo_timeout(port); 1787 if (timeout == 0 || timeout > 2 * fifo_timeout) 1788 timeout = 2 * fifo_timeout; 1789 } 1790 1791 expire = jiffies + timeout; 1792 1793 pr_debug("uart_wait_until_sent(%u), jiffies=%lu, expire=%lu...\n", 1794 port->line, jiffies, expire); 1795 1796 /* 1797 * Check whether the transmitter is empty every 'char_time'. 1798 * 'timeout' / 'expire' give us the maximum amount of time 1799 * we wait. 1800 */ 1801 while (!port->ops->tx_empty(port)) { 1802 msleep_interruptible(jiffies_to_msecs(char_time)); 1803 if (signal_pending(current)) 1804 break; 1805 if (timeout && time_after(jiffies, expire)) 1806 break; 1807 } 1808 uart_port_deref(port); 1809 } 1810 1811 /* 1812 * Calls to uart_hangup() are serialised by the tty_lock in 1813 * drivers/tty/tty_io.c:do_tty_hangup() 1814 * This runs from a workqueue and can sleep for a _short_ time only. 1815 */ 1816 static void uart_hangup(struct tty_struct *tty) 1817 { 1818 struct uart_state *state = tty->driver_data; 1819 struct tty_port *port = &state->port; 1820 struct uart_port *uport; 1821 1822 pr_debug("uart_hangup(%d)\n", tty->index); 1823 1824 guard(mutex)(&port->mutex); 1825 uport = uart_port_check(state); 1826 WARN(!uport, "hangup of detached port!\n"); 1827 1828 if (tty_port_active(port)) { 1829 uart_flush_buffer(tty); 1830 uart_shutdown(tty, state); 1831 scoped_guard(spinlock_irqsave, &port->lock) 1832 port->count = 0; 1833 tty_port_set_active(port, false); 1834 tty_port_tty_set(port, NULL); 1835 if (uport && !uart_console(uport)) 1836 uart_change_pm(state, UART_PM_STATE_OFF); 1837 wake_up_interruptible(&port->open_wait); 1838 wake_up_interruptible(&port->delta_msr_wait); 1839 } 1840 } 1841 1842 /* uport == NULL if uart_port has already been removed */ 1843 static void uart_port_shutdown(struct tty_port *port) 1844 { 1845 struct uart_state *state = container_of(port, struct uart_state, port); 1846 struct uart_port *uport = uart_port_check(state); 1847 1848 /* 1849 * clear delta_msr_wait queue to avoid mem leaks: we may free 1850 * the irq here so the queue might never be woken up. Note 1851 * that we won't end up waiting on delta_msr_wait again since 1852 * any outstanding file descriptors should be pointing at 1853 * hung_up_tty_fops now. 1854 */ 1855 wake_up_interruptible(&port->delta_msr_wait); 1856 1857 if (uport) { 1858 /* Free the IRQ and disable the port. */ 1859 uport->ops->shutdown(uport); 1860 1861 /* Ensure that the IRQ handler isn't running on another CPU. */ 1862 synchronize_irq(uport->irq); 1863 } 1864 } 1865 1866 static bool uart_carrier_raised(struct tty_port *port) 1867 { 1868 struct uart_state *state = container_of(port, struct uart_state, port); 1869 struct uart_port *uport; 1870 unsigned long flags; 1871 int mctrl; 1872 1873 uport = uart_port_ref_lock(state, &flags); 1874 /* 1875 * Should never observe uport == NULL since checks for hangup should 1876 * abort the tty_port_block_til_ready() loop before checking for carrier 1877 * raised -- but report carrier raised if it does anyway so open will 1878 * continue and not sleep 1879 */ 1880 if (WARN_ON(!uport)) 1881 return true; 1882 uart_enable_ms(uport); 1883 mctrl = uport->ops->get_mctrl(uport); 1884 uart_port_unlock_deref(uport, flags); 1885 1886 return mctrl & TIOCM_CAR; 1887 } 1888 1889 static void uart_dtr_rts(struct tty_port *port, bool active) 1890 { 1891 struct uart_state *state = container_of(port, struct uart_state, port); 1892 struct uart_port *uport; 1893 1894 uport = uart_port_ref(state); 1895 if (!uport) 1896 return; 1897 uart_port_dtr_rts(uport, active); 1898 uart_port_deref(uport); 1899 } 1900 1901 static int uart_install(struct tty_driver *driver, struct tty_struct *tty) 1902 { 1903 struct uart_driver *drv = driver->driver_state; 1904 struct uart_state *state = drv->state + tty->index; 1905 1906 tty->driver_data = state; 1907 1908 return tty_standard_install(driver, tty); 1909 } 1910 1911 /* 1912 * Calls to uart_open are serialised by the tty_lock in 1913 * drivers/tty/tty_io.c:tty_open() 1914 * Note that if this fails, then uart_close() _will_ be called. 1915 * 1916 * In time, we want to scrap the "opening nonpresent ports" 1917 * behaviour and implement an alternative way for setserial 1918 * to set base addresses/ports/types. This will allow us to 1919 * get rid of a certain amount of extra tests. 1920 */ 1921 static int uart_open(struct tty_struct *tty, struct file *filp) 1922 { 1923 struct uart_state *state = tty->driver_data; 1924 int retval; 1925 1926 retval = tty_port_open(&state->port, tty, filp); 1927 if (retval > 0) 1928 retval = 0; 1929 1930 return retval; 1931 } 1932 1933 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty) 1934 { 1935 struct uart_state *state = container_of(port, struct uart_state, port); 1936 struct uart_port *uport; 1937 int ret; 1938 1939 uport = uart_port_check(state); 1940 if (!uport || uport->flags & UPF_DEAD) 1941 return -ENXIO; 1942 1943 /* 1944 * Start up the serial port. 1945 */ 1946 ret = uart_startup(tty, state, false); 1947 if (ret > 0) 1948 tty_port_set_active(port, true); 1949 1950 return ret; 1951 } 1952 1953 static const char *uart_type(struct uart_port *port) 1954 { 1955 const char *str = NULL; 1956 1957 if (port->ops->type) 1958 str = port->ops->type(port); 1959 1960 if (!str) 1961 str = "unknown"; 1962 1963 return str; 1964 } 1965 1966 bool uart_iotype_mmio(enum uart_iotype iotype) 1967 { 1968 switch (iotype) { 1969 case UPIO_MEM: 1970 case UPIO_MEM32: 1971 case UPIO_AU: 1972 case UPIO_TSI: 1973 case UPIO_MEM32BE: 1974 case UPIO_MEM16: 1975 return true; 1976 default: 1977 return false; 1978 } 1979 } 1980 EXPORT_SYMBOL_GPL(uart_iotype_mmio); 1981 1982 bool uart_iotype_io(enum uart_iotype iotype) 1983 { 1984 switch (iotype) { 1985 case UPIO_PORT: 1986 case UPIO_HUB6: 1987 return true; 1988 default: 1989 return false; 1990 } 1991 } 1992 EXPORT_SYMBOL_GPL(uart_iotype_io); 1993 1994 #ifdef CONFIG_PROC_FS 1995 1996 static void uart_line_info(struct seq_file *m, struct uart_state *state) 1997 { 1998 struct tty_port *port = &state->port; 1999 enum uart_pm_state pm_state; 2000 struct uart_port *uport; 2001 char ioinfos[64]; 2002 char stat_buf[32]; 2003 unsigned int status; 2004 2005 guard(mutex)(&port->mutex); 2006 2007 uport = uart_port_check(state); 2008 if (!uport) 2009 return; 2010 2011 seq_printf(m, "%u: uart:%s", uport->line, uart_type(uport)); 2012 uart_get_ioinfos(uport, ioinfos, sizeof(ioinfos)); 2013 seq_printf(m, "%s", ioinfos); 2014 seq_printf(m, " irq:%u", uport->irq); 2015 2016 if (uport->type == PORT_UNKNOWN) { 2017 seq_putc(m, '\n'); 2018 return; 2019 } 2020 2021 if (capable(CAP_SYS_ADMIN)) { 2022 pm_state = state->pm_state; 2023 if (pm_state != UART_PM_STATE_ON) 2024 uart_change_pm(state, UART_PM_STATE_ON); 2025 scoped_guard(uart_port_lock_irq, uport) 2026 status = uport->ops->get_mctrl(uport); 2027 if (pm_state != UART_PM_STATE_ON) 2028 uart_change_pm(state, pm_state); 2029 2030 seq_printf(m, " tx:%u rx:%u", 2031 uport->icount.tx, uport->icount.rx); 2032 if (uport->icount.frame) 2033 seq_printf(m, " fe:%u", uport->icount.frame); 2034 if (uport->icount.parity) 2035 seq_printf(m, " pe:%u", uport->icount.parity); 2036 if (uport->icount.brk) 2037 seq_printf(m, " brk:%u", uport->icount.brk); 2038 if (uport->icount.overrun) 2039 seq_printf(m, " oe:%u", uport->icount.overrun); 2040 if (uport->icount.buf_overrun) 2041 seq_printf(m, " bo:%u", uport->icount.buf_overrun); 2042 2043 #define INFOBIT(bit, str) \ 2044 if (uport->mctrl & (bit)) \ 2045 strncat(stat_buf, (str), sizeof(stat_buf) - \ 2046 strlen(stat_buf) - 2) 2047 #define STATBIT(bit, str) \ 2048 if (status & (bit)) \ 2049 strncat(stat_buf, (str), sizeof(stat_buf) - \ 2050 strlen(stat_buf) - 2) 2051 2052 stat_buf[0] = '\0'; 2053 stat_buf[1] = '\0'; 2054 INFOBIT(TIOCM_RTS, "|RTS"); 2055 STATBIT(TIOCM_CTS, "|CTS"); 2056 INFOBIT(TIOCM_DTR, "|DTR"); 2057 STATBIT(TIOCM_DSR, "|DSR"); 2058 STATBIT(TIOCM_CAR, "|CD"); 2059 STATBIT(TIOCM_RNG, "|RI"); 2060 if (stat_buf[0]) 2061 stat_buf[0] = ' '; 2062 2063 seq_puts(m, stat_buf); 2064 } 2065 seq_putc(m, '\n'); 2066 #undef STATBIT 2067 #undef INFOBIT 2068 } 2069 2070 static int uart_proc_show(struct seq_file *m, void *v) 2071 { 2072 struct tty_driver *ttydrv = m->private; 2073 struct uart_driver *drv = ttydrv->driver_state; 2074 int i; 2075 2076 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", ""); 2077 for (i = 0; i < drv->nr; i++) 2078 uart_line_info(m, drv->state + i); 2079 return 0; 2080 } 2081 #endif 2082 2083 static void uart_port_spin_lock_init(struct uart_port *port) 2084 { 2085 spin_lock_init(&port->lock); 2086 lockdep_set_class(&port->lock, &port_lock_key); 2087 } 2088 2089 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) 2090 /** 2091 * uart_console_write - write a console message to a serial port 2092 * @port: the port to write the message 2093 * @s: array of characters 2094 * @count: number of characters in string to write 2095 * @putchar: function to write character to port 2096 */ 2097 void uart_console_write(struct uart_port *port, const char *s, 2098 unsigned int count, 2099 void (*putchar)(struct uart_port *, unsigned char)) 2100 { 2101 unsigned int i; 2102 2103 for (i = 0; i < count; i++, s++) { 2104 if (*s == '\n') 2105 putchar(port, '\r'); 2106 putchar(port, *s); 2107 } 2108 } 2109 EXPORT_SYMBOL_GPL(uart_console_write); 2110 2111 /** 2112 * uart_parse_earlycon - Parse earlycon options 2113 * @p: ptr to 2nd field (ie., just beyond '<name>,') 2114 * @iotype: ptr for decoded iotype (out) 2115 * @addr: ptr for decoded mapbase/iobase (out) 2116 * @options: ptr for <options> field; %NULL if not present (out) 2117 * 2118 * Decodes earlycon kernel command line parameters of the form: 2119 * * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 2120 * * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 2121 * 2122 * The optional form: 2123 * * earlycon=<name>,0x<addr>,<options> 2124 * * console=<name>,0x<addr>,<options> 2125 * 2126 * is also accepted; the returned @iotype will be %UPIO_MEM. 2127 * 2128 * Returns: 0 on success or -%EINVAL on failure 2129 */ 2130 int uart_parse_earlycon(char *p, enum uart_iotype *iotype, 2131 resource_size_t *addr, char **options) 2132 { 2133 if (strncmp(p, "mmio,", 5) == 0) { 2134 *iotype = UPIO_MEM; 2135 p += 5; 2136 } else if (strncmp(p, "mmio16,", 7) == 0) { 2137 *iotype = UPIO_MEM16; 2138 p += 7; 2139 } else if (strncmp(p, "mmio32,", 7) == 0) { 2140 *iotype = UPIO_MEM32; 2141 p += 7; 2142 } else if (strncmp(p, "mmio32be,", 9) == 0) { 2143 *iotype = UPIO_MEM32BE; 2144 p += 9; 2145 } else if (strncmp(p, "mmio32native,", 13) == 0) { 2146 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 2147 UPIO_MEM32BE : UPIO_MEM32; 2148 p += 13; 2149 } else if (strncmp(p, "io,", 3) == 0) { 2150 *iotype = UPIO_PORT; 2151 p += 3; 2152 } else if (strncmp(p, "0x", 2) == 0) { 2153 *iotype = UPIO_MEM; 2154 } else { 2155 return -EINVAL; 2156 } 2157 2158 /* 2159 * Before you replace it with kstrtoull(), think about options separator 2160 * (',') it will not tolerate 2161 */ 2162 *addr = simple_strtoull(p, NULL, 0); 2163 p = strchr(p, ','); 2164 if (p) 2165 p++; 2166 2167 *options = p; 2168 return 0; 2169 } 2170 EXPORT_SYMBOL_GPL(uart_parse_earlycon); 2171 2172 /** 2173 * uart_parse_options - Parse serial port baud/parity/bits/flow control. 2174 * @options: pointer to option string 2175 * @baud: pointer to an 'int' variable for the baud rate. 2176 * @parity: pointer to an 'int' variable for the parity. 2177 * @bits: pointer to an 'int' variable for the number of data bits. 2178 * @flow: pointer to an 'int' variable for the flow control character. 2179 * 2180 * uart_parse_options() decodes a string containing the serial console 2181 * options. The format of the string is <baud><parity><bits><flow>, 2182 * eg: 115200n8r 2183 */ 2184 void 2185 uart_parse_options(const char *options, int *baud, int *parity, 2186 int *bits, int *flow) 2187 { 2188 const char *s = options; 2189 2190 *baud = simple_strtoul(s, NULL, 10); 2191 while (*s >= '0' && *s <= '9') 2192 s++; 2193 if (*s) 2194 *parity = *s++; 2195 if (*s) 2196 *bits = *s++ - '0'; 2197 if (*s) 2198 *flow = *s; 2199 } 2200 EXPORT_SYMBOL_GPL(uart_parse_options); 2201 2202 /** 2203 * uart_set_options - setup the serial console parameters 2204 * @port: pointer to the serial ports uart_port structure 2205 * @co: console pointer 2206 * @baud: baud rate 2207 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) 2208 * @bits: number of data bits 2209 * @flow: flow control character - 'r' (rts) 2210 * 2211 * Locking: Caller must hold console_list_lock in order to serialize 2212 * early initialization of the serial-console lock. 2213 */ 2214 int 2215 uart_set_options(struct uart_port *port, struct console *co, 2216 int baud, int parity, int bits, int flow) 2217 { 2218 struct ktermios termios; 2219 static struct ktermios dummy; 2220 2221 /* 2222 * Ensure that the serial-console lock is initialised early. 2223 * 2224 * Note that the console-registered check is needed because 2225 * kgdboc can call uart_set_options() for an already registered 2226 * console via tty_find_polling_driver() and uart_poll_init(). 2227 */ 2228 if (!uart_console_registered_locked(port) && !port->console_reinit) 2229 uart_port_spin_lock_init(port); 2230 2231 memset(&termios, 0, sizeof(struct ktermios)); 2232 2233 termios.c_cflag |= CREAD | HUPCL | CLOCAL; 2234 tty_termios_encode_baud_rate(&termios, baud, baud); 2235 2236 if (bits == 7) 2237 termios.c_cflag |= CS7; 2238 else 2239 termios.c_cflag |= CS8; 2240 2241 switch (parity) { 2242 case 'o': case 'O': 2243 termios.c_cflag |= PARODD; 2244 fallthrough; 2245 case 'e': case 'E': 2246 termios.c_cflag |= PARENB; 2247 break; 2248 } 2249 2250 if (flow == 'r') 2251 termios.c_cflag |= CRTSCTS; 2252 2253 /* 2254 * some uarts on other side don't support no flow control. 2255 * So we set * DTR in host uart to make them happy 2256 */ 2257 port->mctrl |= TIOCM_DTR; 2258 2259 port->ops->set_termios(port, &termios, &dummy); 2260 2261 /* 2262 * If console hardware flow control was specified and is supported, 2263 * the related policy UPSTAT_CTS_ENABLE must be set to allow console 2264 * drivers to identify if CTS should be used for polling. 2265 */ 2266 if (flow == 'r' && (termios.c_cflag & CRTSCTS)) { 2267 /* Synchronize @status RMW update against the console. */ 2268 guard(uart_port_lock_irqsave)(port); 2269 port->status |= UPSTAT_CTS_ENABLE; 2270 } 2271 2272 /* 2273 * Allow the setting of the UART parameters with a NULL console 2274 * too: 2275 */ 2276 if (co) { 2277 co->cflag = termios.c_cflag; 2278 co->ispeed = termios.c_ispeed; 2279 co->ospeed = termios.c_ospeed; 2280 } 2281 2282 return 0; 2283 } 2284 EXPORT_SYMBOL_GPL(uart_set_options); 2285 #endif /* CONFIG_SERIAL_CORE_CONSOLE */ 2286 2287 /** 2288 * uart_change_pm - set power state of the port 2289 * 2290 * @state: port descriptor 2291 * @pm_state: new state 2292 * 2293 * Locking: port->mutex has to be held 2294 */ 2295 static void uart_change_pm(struct uart_state *state, 2296 enum uart_pm_state pm_state) 2297 { 2298 struct uart_port *port = uart_port_check(state); 2299 2300 if (state->pm_state != pm_state) { 2301 if (port && port->ops->pm) 2302 port->ops->pm(port, pm_state, state->pm_state); 2303 state->pm_state = pm_state; 2304 } 2305 } 2306 2307 struct uart_match { 2308 struct uart_port *port; 2309 struct uart_driver *driver; 2310 }; 2311 2312 static int serial_match_port(struct device *dev, const void *data) 2313 { 2314 const struct uart_match *match = data; 2315 struct tty_driver *tty_drv = match->driver->tty_driver; 2316 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + 2317 match->port->line; 2318 2319 return dev->devt == devt; /* Actually, only one tty per port */ 2320 } 2321 2322 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) 2323 { 2324 struct uart_state *state = drv->state + uport->line; 2325 struct tty_port *port = &state->port; 2326 struct device *tty_dev; 2327 struct uart_match match = {uport, drv}; 2328 2329 guard(mutex)(&port->mutex); 2330 2331 tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port); 2332 if (tty_dev && device_may_wakeup(tty_dev)) { 2333 enable_irq_wake(uport->irq); 2334 put_device(tty_dev); 2335 return 0; 2336 } 2337 put_device(tty_dev); 2338 2339 /* 2340 * Nothing to do if the console is not suspending 2341 * except stop_rx to prevent any asynchronous data 2342 * over RX line. However ensure that we will be 2343 * able to Re-start_rx later. 2344 */ 2345 if (!console_suspend_enabled && uart_console(uport)) { 2346 if (uport->ops->start_rx) { 2347 guard(uart_port_lock_irq)(uport); 2348 uport->ops->stop_rx(uport); 2349 } 2350 device_set_awake_path(uport->dev); 2351 return 0; 2352 } 2353 2354 uport->suspended = 1; 2355 2356 if (tty_port_initialized(port)) { 2357 const struct uart_ops *ops = uport->ops; 2358 int tries; 2359 unsigned int mctrl; 2360 2361 tty_port_set_suspended(port, true); 2362 tty_port_set_initialized(port, false); 2363 2364 scoped_guard(uart_port_lock_irq, uport) { 2365 ops->stop_tx(uport); 2366 if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2367 ops->set_mctrl(uport, 0); 2368 /* save mctrl so it can be restored on resume */ 2369 mctrl = uport->mctrl; 2370 uport->mctrl = 0; 2371 ops->stop_rx(uport); 2372 } 2373 2374 /* 2375 * Wait for the transmitter to empty. 2376 */ 2377 for (tries = 3; !ops->tx_empty(uport) && tries; tries--) 2378 msleep(10); 2379 if (!tries) 2380 dev_err(uport->dev, "%s: Unable to drain transmitter\n", 2381 uport->name); 2382 2383 ops->shutdown(uport); 2384 uport->mctrl = mctrl; 2385 } 2386 2387 /* 2388 * Suspend the console device before suspending the port. 2389 */ 2390 if (uart_console(uport)) 2391 console_suspend(uport->cons); 2392 2393 uart_change_pm(state, UART_PM_STATE_OFF); 2394 2395 return 0; 2396 } 2397 EXPORT_SYMBOL(uart_suspend_port); 2398 2399 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) 2400 { 2401 struct uart_state *state = drv->state + uport->line; 2402 struct tty_port *port = &state->port; 2403 struct device *tty_dev; 2404 struct uart_match match = {uport, drv}; 2405 struct ktermios termios; 2406 2407 guard(mutex)(&port->mutex); 2408 2409 tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port); 2410 if (!uport->suspended && device_may_wakeup(tty_dev)) { 2411 if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq)))) 2412 disable_irq_wake(uport->irq); 2413 put_device(tty_dev); 2414 return 0; 2415 } 2416 put_device(tty_dev); 2417 uport->suspended = 0; 2418 2419 /* 2420 * Re-enable the console device after suspending. 2421 */ 2422 if (uart_console(uport)) { 2423 /* 2424 * First try to use the console cflag setting. 2425 */ 2426 memset(&termios, 0, sizeof(struct ktermios)); 2427 termios.c_cflag = uport->cons->cflag; 2428 termios.c_ispeed = uport->cons->ispeed; 2429 termios.c_ospeed = uport->cons->ospeed; 2430 2431 /* 2432 * If that's unset, use the tty termios setting. 2433 */ 2434 if (port->tty && termios.c_cflag == 0) 2435 termios = port->tty->termios; 2436 2437 if (console_suspend_enabled) 2438 uart_change_pm(state, UART_PM_STATE_ON); 2439 uport->ops->set_termios(uport, &termios, NULL); 2440 if (!console_suspend_enabled && uport->ops->start_rx) { 2441 guard(uart_port_lock_irq)(uport); 2442 uport->ops->start_rx(uport); 2443 } 2444 if (console_suspend_enabled) 2445 console_resume(uport->cons); 2446 } 2447 2448 if (tty_port_suspended(port)) { 2449 const struct uart_ops *ops = uport->ops; 2450 int ret; 2451 2452 uart_change_pm(state, UART_PM_STATE_ON); 2453 scoped_guard(uart_port_lock_irq, uport) 2454 if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2455 ops->set_mctrl(uport, 0); 2456 if (console_suspend_enabled || !uart_console(uport)) { 2457 /* Protected by port mutex for now */ 2458 struct tty_struct *tty = port->tty; 2459 2460 ret = ops->startup(uport); 2461 if (ret == 0) { 2462 if (tty) 2463 uart_change_line_settings(tty, state, NULL); 2464 uart_rs485_config(uport); 2465 scoped_guard(uart_port_lock_irq, uport) { 2466 if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2467 ops->set_mctrl(uport, uport->mctrl); 2468 ops->start_tx(uport); 2469 } 2470 tty_port_set_initialized(port, true); 2471 } else { 2472 /* 2473 * Failed to resume - maybe hardware went away? 2474 * Clear the "initialized" flag so we won't try 2475 * to call the low level drivers shutdown method. 2476 */ 2477 uart_shutdown(tty, state); 2478 } 2479 } 2480 2481 tty_port_set_suspended(port, false); 2482 } 2483 2484 return 0; 2485 } 2486 EXPORT_SYMBOL(uart_resume_port); 2487 2488 static const char *uart_get_mmio_width(struct uart_port *port) 2489 { 2490 switch (port->iotype) { 2491 case UPIO_MEM16: 2492 return "16"; 2493 case UPIO_MEM32: 2494 case UPIO_MEM32BE: 2495 return "32"; 2496 case UPIO_AU: 2497 case UPIO_MEM: 2498 default: 2499 return ""; 2500 } 2501 } 2502 2503 void uart_get_ioinfos(struct uart_port *port, char *buf, size_t size) 2504 { 2505 buf[0] = '\0'; 2506 2507 if (uart_iotype_mmio(port->iotype)) { 2508 scnprintf(buf, size, " MMIO%s:%pa", uart_get_mmio_width(port), &port->mapbase); 2509 } else if (uart_iotype_io(port->iotype)) { 2510 if (port->iotype == UPIO_PORT) 2511 scnprintf(buf, size, " I/O:0x%lx", port->iobase); 2512 else if (port->iotype == UPIO_HUB6) 2513 scnprintf(buf, size, " I/O:0x%lx, offset 0x%x", port->iobase, port->hub6); 2514 } 2515 } 2516 EXPORT_SYMBOL(uart_get_ioinfos); 2517 2518 static inline void 2519 uart_report_port(struct uart_driver *drv, struct uart_port *port) 2520 { 2521 char ioinfos[64]; 2522 2523 uart_get_ioinfos(port, ioinfos, sizeof(ioinfos)); 2524 2525 pr_info("%s%s%s%s (irq = %u, base_baud = %u) is a %s\n", 2526 port->dev ? dev_name(port->dev) : "", 2527 port->dev ? ": " : "", 2528 port->name, ioinfos, port->irq, port->uartclk / 16, uart_type(port)); 2529 2530 /* The magic multiplier feature is a bit obscure, so report it too. */ 2531 if (port->flags & UPF_MAGIC_MULTIPLIER) 2532 pr_info("%s%s%s extra baud rates supported: %u, %u", 2533 port->dev ? dev_name(port->dev) : "", 2534 port->dev ? ": " : "", 2535 port->name, 2536 port->uartclk / 8, port->uartclk / 4); 2537 } 2538 2539 static void 2540 uart_configure_port(struct uart_driver *drv, struct uart_state *state, 2541 struct uart_port *port) 2542 { 2543 unsigned int flags; 2544 2545 /* If there isn't a port here, don't do anything further. */ 2546 if (uart_iotype_mmio(port->iotype) || uart_iotype_io(port->iotype)) 2547 if (!port->iobase && !port->mapbase && !port->membase) 2548 return; 2549 2550 /* 2551 * Now do the auto configuration stuff. Note that config_port 2552 * is expected to claim the resources and map the port for us. 2553 */ 2554 flags = 0; 2555 if (port->flags & UPF_AUTO_IRQ) 2556 flags |= UART_CONFIG_IRQ; 2557 if (port->flags & UPF_BOOT_AUTOCONF) { 2558 if (!(port->flags & UPF_FIXED_TYPE)) { 2559 port->type = PORT_UNKNOWN; 2560 flags |= UART_CONFIG_TYPE; 2561 } 2562 /* Synchronize with possible boot console. */ 2563 if (uart_console(port)) 2564 console_lock(); 2565 port->ops->config_port(port, flags); 2566 if (uart_console(port)) 2567 console_unlock(); 2568 } 2569 2570 if (port->type != PORT_UNKNOWN) { 2571 uart_report_port(drv, port); 2572 2573 /* Synchronize with possible boot console. */ 2574 if (uart_console(port)) 2575 console_lock(); 2576 2577 /* Power up port for set_mctrl() */ 2578 uart_change_pm(state, UART_PM_STATE_ON); 2579 2580 /* 2581 * Ensure that the modem control lines are de-activated. 2582 * keep the DTR setting that is set in uart_set_options() 2583 * We probably don't need a spinlock around this, but 2584 */ 2585 scoped_guard(uart_port_lock_irqsave, port) { 2586 unsigned int mask = TIOCM_DTR; 2587 2588 /* Console hardware flow control polls CTS. */ 2589 if (uart_console_hwflow_active(port)) 2590 mask |= TIOCM_RTS; 2591 2592 port->mctrl &= mask; 2593 2594 if (!(port->rs485.flags & SER_RS485_ENABLED)) 2595 port->ops->set_mctrl(port, port->mctrl); 2596 } 2597 2598 uart_rs485_config(port); 2599 2600 if (uart_console(port)) 2601 console_unlock(); 2602 2603 /* 2604 * If this driver supports console, and it hasn't been 2605 * successfully registered yet, try to re-register it. 2606 * It may be that the port was not available. 2607 */ 2608 if (port->cons && !console_is_registered(port->cons)) 2609 register_console(port->cons); 2610 2611 /* 2612 * Power down all ports by default, except the 2613 * console if we have one. 2614 */ 2615 if (!uart_console(port)) 2616 uart_change_pm(state, UART_PM_STATE_OFF); 2617 } 2618 } 2619 2620 #ifdef CONFIG_CONSOLE_POLL 2621 2622 static int uart_poll_init(struct tty_driver *driver, int line, char *options) 2623 { 2624 struct uart_driver *drv = driver->driver_state; 2625 struct uart_state *state = drv->state + line; 2626 enum uart_pm_state pm_state; 2627 struct tty_port *tport; 2628 struct uart_port *port; 2629 int baud = 9600; 2630 int bits = 8; 2631 int parity = 'n'; 2632 int flow = 'n'; 2633 int ret = 0; 2634 2635 tport = &state->port; 2636 2637 guard(mutex)(&tport->mutex); 2638 2639 port = uart_port_check(state); 2640 if (!port || port->type == PORT_UNKNOWN || 2641 !(port->ops->poll_get_char && port->ops->poll_put_char)) 2642 return -1; 2643 2644 pm_state = state->pm_state; 2645 uart_change_pm(state, UART_PM_STATE_ON); 2646 2647 if (port->ops->poll_init) { 2648 /* 2649 * We don't set initialized as we only initialized the hw, 2650 * e.g. state->xmit is still uninitialized. 2651 */ 2652 if (!tty_port_initialized(tport)) 2653 ret = port->ops->poll_init(port); 2654 } 2655 2656 if (!ret && options) { 2657 uart_parse_options(options, &baud, &parity, &bits, &flow); 2658 console_list_lock(); 2659 ret = uart_set_options(port, NULL, baud, parity, bits, flow); 2660 console_list_unlock(); 2661 } 2662 2663 if (ret) 2664 uart_change_pm(state, pm_state); 2665 2666 return ret; 2667 } 2668 2669 static int uart_poll_get_char(struct tty_driver *driver, int line) 2670 { 2671 struct uart_driver *drv = driver->driver_state; 2672 struct uart_state *state = drv->state + line; 2673 struct uart_port *port; 2674 int ret = -1; 2675 2676 port = uart_port_ref(state); 2677 if (port) { 2678 ret = port->ops->poll_get_char(port); 2679 uart_port_deref(port); 2680 } 2681 2682 return ret; 2683 } 2684 2685 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) 2686 { 2687 struct uart_driver *drv = driver->driver_state; 2688 struct uart_state *state = drv->state + line; 2689 struct uart_port *port; 2690 2691 port = uart_port_ref(state); 2692 if (!port) 2693 return; 2694 2695 if (ch == '\n') 2696 port->ops->poll_put_char(port, '\r'); 2697 port->ops->poll_put_char(port, ch); 2698 uart_port_deref(port); 2699 } 2700 #endif 2701 2702 static const struct tty_operations uart_ops = { 2703 .install = uart_install, 2704 .open = uart_open, 2705 .close = uart_close, 2706 .write = uart_write, 2707 .put_char = uart_put_char, 2708 .flush_chars = uart_flush_chars, 2709 .write_room = uart_write_room, 2710 .chars_in_buffer= uart_chars_in_buffer, 2711 .flush_buffer = uart_flush_buffer, 2712 .ioctl = uart_ioctl, 2713 .throttle = uart_throttle, 2714 .unthrottle = uart_unthrottle, 2715 .send_xchar = uart_send_xchar, 2716 .set_termios = uart_set_termios, 2717 .set_ldisc = uart_set_ldisc, 2718 .stop = uart_stop, 2719 .start = uart_start, 2720 .hangup = uart_hangup, 2721 .break_ctl = uart_break_ctl, 2722 .wait_until_sent= uart_wait_until_sent, 2723 #ifdef CONFIG_PROC_FS 2724 .proc_show = uart_proc_show, 2725 #endif 2726 .tiocmget = uart_tiocmget, 2727 .tiocmset = uart_tiocmset, 2728 .set_serial = uart_set_info_user, 2729 .get_serial = uart_get_info_user, 2730 .get_icount = uart_get_icount, 2731 #ifdef CONFIG_CONSOLE_POLL 2732 .poll_init = uart_poll_init, 2733 .poll_get_char = uart_poll_get_char, 2734 .poll_put_char = uart_poll_put_char, 2735 #endif 2736 }; 2737 2738 static const struct tty_port_operations uart_port_ops = { 2739 .carrier_raised = uart_carrier_raised, 2740 .dtr_rts = uart_dtr_rts, 2741 .activate = uart_port_activate, 2742 .shutdown = uart_tty_port_shutdown, 2743 }; 2744 2745 /** 2746 * uart_register_driver - register a driver with the uart core layer 2747 * @drv: low level driver structure 2748 * 2749 * Register a uart driver with the core driver. We in turn register with the 2750 * tty layer, and initialise the core driver per-port state. 2751 * 2752 * We have a proc file in /proc/tty/driver which is named after the normal 2753 * driver. 2754 * 2755 * @drv->port should be %NULL, and the per-port structures should be registered 2756 * using uart_add_one_port() after this call has succeeded. 2757 * 2758 * Locking: none, Interrupts: enabled 2759 */ 2760 int uart_register_driver(struct uart_driver *drv) 2761 { 2762 struct tty_driver *normal; 2763 int i, retval = -ENOMEM; 2764 2765 BUG_ON(drv->state); 2766 2767 /* 2768 * Maybe we should be using a slab cache for this, especially if 2769 * we have a large number of ports to handle. 2770 */ 2771 drv->state = kzalloc_objs(struct uart_state, drv->nr); 2772 if (!drv->state) 2773 goto out; 2774 2775 normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW | 2776 TTY_DRIVER_DYNAMIC_DEV); 2777 if (IS_ERR(normal)) { 2778 retval = PTR_ERR(normal); 2779 goto out_kfree; 2780 } 2781 2782 drv->tty_driver = normal; 2783 2784 normal->driver_name = drv->driver_name; 2785 normal->name = drv->dev_name; 2786 normal->major = drv->major; 2787 normal->minor_start = drv->minor; 2788 normal->type = TTY_DRIVER_TYPE_SERIAL; 2789 normal->subtype = SERIAL_TYPE_NORMAL; 2790 normal->init_termios = tty_std_termios; 2791 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 2792 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600; 2793 normal->driver_state = drv; 2794 tty_set_operations(normal, &uart_ops); 2795 2796 /* 2797 * Initialise the UART state(s). 2798 */ 2799 for (i = 0; i < drv->nr; i++) { 2800 struct uart_state *state = drv->state + i; 2801 struct tty_port *port = &state->port; 2802 2803 tty_port_init(port); 2804 port->ops = &uart_port_ops; 2805 } 2806 2807 retval = tty_register_driver(normal); 2808 if (retval >= 0) 2809 return retval; 2810 2811 for (i = 0; i < drv->nr; i++) 2812 tty_port_destroy(&drv->state[i].port); 2813 tty_driver_kref_put(normal); 2814 drv->tty_driver = NULL; 2815 out_kfree: 2816 kfree(drv->state); 2817 drv->state = NULL; 2818 out: 2819 return retval; 2820 } 2821 EXPORT_SYMBOL(uart_register_driver); 2822 2823 /** 2824 * uart_unregister_driver - remove a driver from the uart core layer 2825 * @drv: low level driver structure 2826 * 2827 * Remove all references to a driver from the core driver. The low level 2828 * driver must have removed all its ports via the uart_remove_one_port() if it 2829 * registered them with uart_add_one_port(). (I.e. @drv->port is %NULL.) 2830 * 2831 * Locking: none, Interrupts: enabled 2832 */ 2833 void uart_unregister_driver(struct uart_driver *drv) 2834 { 2835 struct tty_driver *p = drv->tty_driver; 2836 unsigned int i; 2837 2838 tty_unregister_driver(p); 2839 tty_driver_kref_put(p); 2840 for (i = 0; i < drv->nr; i++) 2841 tty_port_destroy(&drv->state[i].port); 2842 kfree(drv->state); 2843 drv->state = NULL; 2844 drv->tty_driver = NULL; 2845 } 2846 EXPORT_SYMBOL(uart_unregister_driver); 2847 2848 struct tty_driver *uart_console_device(struct console *co, int *index) 2849 { 2850 struct uart_driver *p = co->data; 2851 *index = co->index; 2852 return p->tty_driver; 2853 } 2854 EXPORT_SYMBOL_GPL(uart_console_device); 2855 2856 static ssize_t uartclk_show(struct device *dev, 2857 struct device_attribute *attr, char *buf) 2858 { 2859 struct serial_struct tmp; 2860 struct tty_port *port = dev_get_drvdata(dev); 2861 2862 uart_get_info(port, &tmp); 2863 return sprintf(buf, "%d\n", tmp.baud_base * 16); 2864 } 2865 2866 static ssize_t type_show(struct device *dev, 2867 struct device_attribute *attr, char *buf) 2868 { 2869 struct serial_struct tmp; 2870 struct tty_port *port = dev_get_drvdata(dev); 2871 2872 uart_get_info(port, &tmp); 2873 return sprintf(buf, "%d\n", tmp.type); 2874 } 2875 2876 static ssize_t line_show(struct device *dev, 2877 struct device_attribute *attr, char *buf) 2878 { 2879 struct serial_struct tmp; 2880 struct tty_port *port = dev_get_drvdata(dev); 2881 2882 uart_get_info(port, &tmp); 2883 return sprintf(buf, "%d\n", tmp.line); 2884 } 2885 2886 static ssize_t port_show(struct device *dev, 2887 struct device_attribute *attr, char *buf) 2888 { 2889 struct serial_struct tmp; 2890 struct tty_port *port = dev_get_drvdata(dev); 2891 unsigned long ioaddr; 2892 2893 uart_get_info(port, &tmp); 2894 ioaddr = tmp.port; 2895 if (HIGH_BITS_OFFSET) 2896 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET; 2897 return sprintf(buf, "0x%lX\n", ioaddr); 2898 } 2899 2900 static ssize_t irq_show(struct device *dev, 2901 struct device_attribute *attr, char *buf) 2902 { 2903 struct serial_struct tmp; 2904 struct tty_port *port = dev_get_drvdata(dev); 2905 2906 uart_get_info(port, &tmp); 2907 return sprintf(buf, "%d\n", tmp.irq); 2908 } 2909 2910 static ssize_t flags_show(struct device *dev, 2911 struct device_attribute *attr, char *buf) 2912 { 2913 struct serial_struct tmp; 2914 struct tty_port *port = dev_get_drvdata(dev); 2915 2916 uart_get_info(port, &tmp); 2917 return sprintf(buf, "0x%X\n", tmp.flags); 2918 } 2919 2920 static ssize_t xmit_fifo_size_show(struct device *dev, 2921 struct device_attribute *attr, char *buf) 2922 { 2923 struct serial_struct tmp; 2924 struct tty_port *port = dev_get_drvdata(dev); 2925 2926 uart_get_info(port, &tmp); 2927 return sprintf(buf, "%d\n", tmp.xmit_fifo_size); 2928 } 2929 2930 static ssize_t close_delay_show(struct device *dev, 2931 struct device_attribute *attr, char *buf) 2932 { 2933 struct serial_struct tmp; 2934 struct tty_port *port = dev_get_drvdata(dev); 2935 2936 uart_get_info(port, &tmp); 2937 return sprintf(buf, "%u\n", tmp.close_delay); 2938 } 2939 2940 static ssize_t closing_wait_show(struct device *dev, 2941 struct device_attribute *attr, char *buf) 2942 { 2943 struct serial_struct tmp; 2944 struct tty_port *port = dev_get_drvdata(dev); 2945 2946 uart_get_info(port, &tmp); 2947 return sprintf(buf, "%u\n", tmp.closing_wait); 2948 } 2949 2950 static ssize_t custom_divisor_show(struct device *dev, 2951 struct device_attribute *attr, char *buf) 2952 { 2953 struct serial_struct tmp; 2954 struct tty_port *port = dev_get_drvdata(dev); 2955 2956 uart_get_info(port, &tmp); 2957 return sprintf(buf, "%d\n", tmp.custom_divisor); 2958 } 2959 2960 static ssize_t io_type_show(struct device *dev, 2961 struct device_attribute *attr, char *buf) 2962 { 2963 struct serial_struct tmp; 2964 struct tty_port *port = dev_get_drvdata(dev); 2965 2966 uart_get_info(port, &tmp); 2967 return sprintf(buf, "%u\n", tmp.io_type); 2968 } 2969 2970 static ssize_t iomem_base_show(struct device *dev, 2971 struct device_attribute *attr, char *buf) 2972 { 2973 struct serial_struct tmp; 2974 struct tty_port *port = dev_get_drvdata(dev); 2975 2976 uart_get_info(port, &tmp); 2977 return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base); 2978 } 2979 2980 static ssize_t iomem_reg_shift_show(struct device *dev, 2981 struct device_attribute *attr, char *buf) 2982 { 2983 struct serial_struct tmp; 2984 struct tty_port *port = dev_get_drvdata(dev); 2985 2986 uart_get_info(port, &tmp); 2987 return sprintf(buf, "%u\n", tmp.iomem_reg_shift); 2988 } 2989 2990 static ssize_t console_show(struct device *dev, 2991 struct device_attribute *attr, char *buf) 2992 { 2993 struct tty_port *port = dev_get_drvdata(dev); 2994 struct uart_state *state = container_of(port, struct uart_state, port); 2995 struct uart_port *uport; 2996 bool console = false; 2997 2998 scoped_guard(mutex, &port->mutex) { 2999 uport = uart_port_check(state); 3000 if (uport) 3001 console = uart_console_registered(uport); 3002 } 3003 3004 return sprintf(buf, "%c\n", console ? 'Y' : 'N'); 3005 } 3006 3007 static ssize_t console_store(struct device *dev, 3008 struct device_attribute *attr, const char *buf, size_t count) 3009 { 3010 struct tty_port *port = dev_get_drvdata(dev); 3011 struct uart_state *state = container_of(port, struct uart_state, port); 3012 struct uart_port *uport; 3013 bool oldconsole, newconsole; 3014 int ret; 3015 3016 ret = kstrtobool(buf, &newconsole); 3017 if (ret) 3018 return ret; 3019 3020 guard(mutex)(&port->mutex); 3021 uport = uart_port_check(state); 3022 if (!uport) 3023 return -ENXIO; 3024 3025 oldconsole = uart_console_registered(uport); 3026 if (oldconsole && !newconsole) { 3027 ret = unregister_console(uport->cons); 3028 if (ret < 0) 3029 return ret; 3030 } else if (!oldconsole && newconsole) { 3031 if (!uart_console(uport)) 3032 return -ENOENT; 3033 3034 uport->console_reinit = 1; 3035 register_console(uport->cons); 3036 } 3037 3038 return count; 3039 } 3040 3041 static DEVICE_ATTR_RO(uartclk); 3042 static DEVICE_ATTR_RO(type); 3043 static DEVICE_ATTR_RO(line); 3044 static DEVICE_ATTR_RO(port); 3045 static DEVICE_ATTR_RO(irq); 3046 static DEVICE_ATTR_RO(flags); 3047 static DEVICE_ATTR_RO(xmit_fifo_size); 3048 static DEVICE_ATTR_RO(close_delay); 3049 static DEVICE_ATTR_RO(closing_wait); 3050 static DEVICE_ATTR_RO(custom_divisor); 3051 static DEVICE_ATTR_RO(io_type); 3052 static DEVICE_ATTR_RO(iomem_base); 3053 static DEVICE_ATTR_RO(iomem_reg_shift); 3054 static DEVICE_ATTR_RW(console); 3055 3056 static struct attribute *tty_dev_attrs[] = { 3057 &dev_attr_uartclk.attr, 3058 &dev_attr_type.attr, 3059 &dev_attr_line.attr, 3060 &dev_attr_port.attr, 3061 &dev_attr_irq.attr, 3062 &dev_attr_flags.attr, 3063 &dev_attr_xmit_fifo_size.attr, 3064 &dev_attr_close_delay.attr, 3065 &dev_attr_closing_wait.attr, 3066 &dev_attr_custom_divisor.attr, 3067 &dev_attr_io_type.attr, 3068 &dev_attr_iomem_base.attr, 3069 &dev_attr_iomem_reg_shift.attr, 3070 &dev_attr_console.attr, 3071 NULL 3072 }; 3073 3074 static const struct attribute_group tty_dev_attr_group = { 3075 .attrs = tty_dev_attrs, 3076 }; 3077 3078 /** 3079 * serial_core_add_one_port - attach a driver-defined port structure 3080 * @drv: pointer to the uart low level driver structure for this port 3081 * @uport: uart port structure to use for this port. 3082 * 3083 * Context: task context, might sleep 3084 * 3085 * This allows the driver @drv to register its own uart_port structure with the 3086 * core driver. The main purpose is to allow the low level uart drivers to 3087 * expand uart_port, rather than having yet more levels of structures. 3088 * Caller must hold port_mutex. 3089 */ 3090 static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *uport) 3091 { 3092 struct uart_state *state; 3093 struct tty_port *port; 3094 struct device *tty_dev; 3095 3096 if (uport->line >= drv->nr) 3097 return -EINVAL; 3098 3099 state = drv->state + uport->line; 3100 port = &state->port; 3101 3102 guard(mutex)(&port->mutex); 3103 if (state->uart_port) 3104 return -EINVAL; 3105 3106 uport->name = kasprintf(GFP_KERNEL, "%s%u", drv->dev_name, 3107 drv->tty_driver->name_base + uport->line); 3108 if (!uport->name) 3109 return -ENOMEM; 3110 3111 /* 3112 * uart_configure_port() may set uport->attr_group and register the 3113 * console. Allocate room for both groups and a NULL terminator first. 3114 */ 3115 uport->tty_groups = kzalloc_objs(*uport->tty_groups, 3); 3116 if (!uport->tty_groups) { 3117 kfree(uport->name); 3118 return -ENOMEM; 3119 } 3120 uport->tty_groups[0] = &tty_dev_attr_group; 3121 3122 /* Link the port to the driver state table and vice versa */ 3123 atomic_set(&state->refcount, 1); 3124 init_waitqueue_head(&state->remove_wait); 3125 state->uart_port = uport; 3126 uport->state = state; 3127 3128 /* 3129 * If this port is in use as a console then the spinlock is already 3130 * initialised. 3131 */ 3132 if (!uart_console_registered(uport)) 3133 uart_port_spin_lock_init(uport); 3134 3135 state->pm_state = UART_PM_STATE_UNDEFINED; 3136 uart_port_set_cons(uport, drv->cons); 3137 uport->minor = drv->tty_driver->minor_start + uport->line; 3138 3139 if (uport->cons && uport->dev) 3140 of_console_check(uport->dev->of_node, uport->cons->name, uport->line); 3141 3142 /* 3143 * TTY port has to be linked with the driver before register_console() 3144 * in uart_configure_port(), because user-space could open the console 3145 * immediately after. 3146 */ 3147 tty_port_link_device(port, drv->tty_driver, uport->line); 3148 uart_configure_port(drv, state, uport); 3149 3150 port->console = uart_console(uport); 3151 3152 if (uport->attr_group) 3153 uport->tty_groups[1] = uport->attr_group; 3154 3155 /* Ensure serdev drivers can call serdev_device_open() right away */ 3156 uport->flags &= ~UPF_DEAD; 3157 3158 /* 3159 * Register the port whether it's detected or not. This allows 3160 * setserial to be used to alter this port's parameters. 3161 */ 3162 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver, 3163 uport->line, uport->dev, &uport->port_dev->dev, port, 3164 uport->tty_groups); 3165 if (!IS_ERR(tty_dev)) { 3166 device_set_wakeup_capable(tty_dev, 1); 3167 } else { 3168 uport->flags |= UPF_DEAD; 3169 dev_err(uport->dev, "Cannot register tty device on line %u\n", 3170 uport->line); 3171 } 3172 3173 return 0; 3174 } 3175 3176 /** 3177 * serial_core_remove_one_port - detach a driver defined port structure 3178 * @drv: pointer to the uart low level driver structure for this port 3179 * @uport: uart port structure for this port 3180 * 3181 * Context: task context, might sleep 3182 * 3183 * This unhooks (and hangs up) the specified port structure from the core 3184 * driver. No further calls will be made to the low-level code for this port. 3185 * Caller must hold port_mutex. 3186 */ 3187 static void serial_core_remove_one_port(struct uart_driver *drv, 3188 struct uart_port *uport) 3189 { 3190 struct uart_state *state = drv->state + uport->line; 3191 struct tty_port *port = &state->port; 3192 struct uart_port *uart_port; 3193 3194 scoped_guard(mutex, &port->mutex) { 3195 uart_port = uart_port_check(state); 3196 if (uart_port != uport) 3197 dev_alert(uport->dev, "Removing wrong port: %p != %p\n", uart_port, uport); 3198 3199 if (!uart_port) 3200 return; 3201 } 3202 3203 /* 3204 * Remove the devices from the tty layer 3205 */ 3206 tty_port_unregister_device(port, drv->tty_driver, uport->line); 3207 3208 tty_port_tty_vhangup(port); 3209 3210 /* 3211 * If the port is used as a console, unregister it 3212 */ 3213 if (uart_console(uport)) 3214 unregister_console(uport->cons); 3215 3216 /* 3217 * Free the port IO and memory resources, if any. 3218 */ 3219 if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 3220 uport->ops->release_port(uport); 3221 kfree(uport->tty_groups); 3222 kfree(uport->name); 3223 3224 /* 3225 * Indicate that there isn't a port here anymore. 3226 */ 3227 uport->type = PORT_UNKNOWN; 3228 uport->port_dev = NULL; 3229 3230 guard(mutex)(&port->mutex); 3231 WARN_ON(atomic_dec_return(&state->refcount) < 0); 3232 wait_event(state->remove_wait, !atomic_read(&state->refcount)); 3233 state->uart_port = NULL; 3234 } 3235 3236 /** 3237 * uart_match_port - are the two ports equivalent? 3238 * @port1: first port 3239 * @port2: second port 3240 * 3241 * This utility function can be used to determine whether two uart_port 3242 * structures describe the same port. 3243 */ 3244 bool uart_match_port(const struct uart_port *port1, 3245 const struct uart_port *port2) 3246 { 3247 if (port1->iotype != port2->iotype) 3248 return false; 3249 else if (port1->iotype == UPIO_PORT) 3250 return port1->iobase == port2->iobase; 3251 else if (port1->iotype == UPIO_HUB6) 3252 return hub6_match_port(port1, port2); 3253 else if (uart_iotype_mmio(port1->iotype)) 3254 return port1->mapbase == port2->mapbase; 3255 else if (port1->iotype == UPIO_BUS) 3256 return true; 3257 else 3258 return false; 3259 } 3260 EXPORT_SYMBOL(uart_match_port); 3261 3262 static struct serial_ctrl_device * 3263 serial_core_get_ctrl_dev(struct serial_port_device *port_dev) 3264 { 3265 struct device *dev = &port_dev->dev; 3266 3267 return to_serial_base_ctrl_device(dev->parent); 3268 } 3269 3270 /* 3271 * Find a registered serial core controller device if one exists. Returns 3272 * the first device matching the ctrl_id. Caller must hold port_mutex. 3273 */ 3274 static struct serial_ctrl_device *serial_core_ctrl_find(struct uart_driver *drv, 3275 struct device *phys_dev, 3276 int ctrl_id) 3277 { 3278 struct uart_state *state; 3279 int i; 3280 3281 lockdep_assert_held(&port_mutex); 3282 3283 for (i = 0; i < drv->nr; i++) { 3284 state = drv->state + i; 3285 if (!state->uart_port || !state->uart_port->port_dev) 3286 continue; 3287 3288 if (state->uart_port->dev == phys_dev && 3289 state->uart_port->ctrl_id == ctrl_id) 3290 return serial_core_get_ctrl_dev(state->uart_port->port_dev); 3291 } 3292 3293 return NULL; 3294 } 3295 3296 static struct serial_ctrl_device *serial_core_ctrl_device_add(struct uart_port *port) 3297 { 3298 return serial_base_ctrl_add(port, port->dev); 3299 } 3300 3301 static int serial_core_port_device_add(struct serial_ctrl_device *ctrl_dev, 3302 struct uart_port *port) 3303 { 3304 struct serial_port_device *port_dev; 3305 3306 port_dev = serial_base_port_add(port, ctrl_dev); 3307 if (IS_ERR(port_dev)) 3308 return PTR_ERR(port_dev); 3309 3310 port->port_dev = port_dev; 3311 3312 return 0; 3313 } 3314 3315 /* 3316 * Initialize a serial core port device, and a controller device if needed. 3317 */ 3318 int serial_core_register_port(struct uart_driver *drv, struct uart_port *port) 3319 { 3320 struct serial_ctrl_device *ctrl_dev, *new_ctrl_dev = NULL; 3321 int ret; 3322 3323 guard(mutex)(&port_mutex); 3324 3325 /* 3326 * Prevent serial_port_runtime_resume() from trying to use the port 3327 * until serial_core_add_one_port() has completed 3328 */ 3329 port->flags |= UPF_DEAD; 3330 3331 /* Inititalize a serial core controller device if needed */ 3332 ctrl_dev = serial_core_ctrl_find(drv, port->dev, port->ctrl_id); 3333 if (!ctrl_dev) { 3334 new_ctrl_dev = serial_core_ctrl_device_add(port); 3335 if (IS_ERR(new_ctrl_dev)) 3336 return PTR_ERR(new_ctrl_dev); 3337 ctrl_dev = new_ctrl_dev; 3338 } 3339 3340 /* 3341 * Initialize a serial core port device. Tag the port dead to prevent 3342 * serial_port_runtime_resume() trying to do anything until port has 3343 * been registered. It gets cleared by serial_core_add_one_port(). 3344 */ 3345 ret = serial_core_port_device_add(ctrl_dev, port); 3346 if (ret) 3347 goto err_unregister_ctrl_dev; 3348 3349 ret = serial_base_match_and_update_preferred_console(drv, port); 3350 if (ret) 3351 goto err_unregister_port_dev; 3352 3353 ret = serial_core_add_one_port(drv, port); 3354 if (ret) 3355 goto err_unregister_port_dev; 3356 3357 return 0; 3358 3359 err_unregister_port_dev: 3360 serial_base_port_device_remove(port->port_dev); 3361 3362 err_unregister_ctrl_dev: 3363 serial_base_ctrl_device_remove(new_ctrl_dev); 3364 3365 return ret; 3366 } 3367 3368 /* 3369 * Removes a serial core port device, and the related serial core controller 3370 * device if the last instance. 3371 */ 3372 void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port) 3373 { 3374 struct device *phys_dev = port->dev; 3375 struct serial_port_device *port_dev = port->port_dev; 3376 struct serial_ctrl_device *ctrl_dev = serial_core_get_ctrl_dev(port_dev); 3377 int ctrl_id = port->ctrl_id; 3378 3379 guard(mutex)(&port_mutex); 3380 3381 port->flags |= UPF_DEAD; 3382 3383 serial_core_remove_one_port(drv, port); 3384 3385 /* Note that struct uart_port *port is no longer valid at this point */ 3386 serial_base_port_device_remove(port_dev); 3387 3388 /* Drop the serial core controller device if no ports are using it */ 3389 if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id)) 3390 serial_base_ctrl_device_remove(ctrl_dev); 3391 } 3392 3393 /** 3394 * uart_handle_dcd_change - handle a change of carrier detect state 3395 * @uport: uart_port structure for the open port 3396 * @active: new carrier detect status 3397 * 3398 * Caller must hold uport->lock. 3399 */ 3400 void uart_handle_dcd_change(struct uart_port *uport, bool active) 3401 { 3402 struct tty_port *port = &uport->state->port; 3403 struct tty_struct *tty = port->tty; 3404 struct tty_ldisc *ld; 3405 3406 lockdep_assert_held_once(&uport->lock); 3407 3408 if (tty) { 3409 ld = tty_ldisc_ref(tty); 3410 if (ld) { 3411 if (ld->ops->dcd_change) 3412 ld->ops->dcd_change(tty, active); 3413 tty_ldisc_deref(ld); 3414 } 3415 } 3416 3417 uport->icount.dcd++; 3418 3419 if (uart_dcd_enabled(uport)) { 3420 if (active) 3421 wake_up_interruptible(&port->open_wait); 3422 else if (tty) 3423 tty_hangup(tty); 3424 } 3425 } 3426 EXPORT_SYMBOL_GPL(uart_handle_dcd_change); 3427 3428 /** 3429 * uart_handle_cts_change - handle a change of clear-to-send state 3430 * @uport: uart_port structure for the open port 3431 * @active: new clear-to-send status 3432 * 3433 * Caller must hold uport->lock. 3434 */ 3435 void uart_handle_cts_change(struct uart_port *uport, bool active) 3436 { 3437 lockdep_assert_held_once(&uport->lock); 3438 3439 uport->icount.cts++; 3440 3441 if (uart_softcts_mode(uport)) { 3442 if (uport->hw_stopped) { 3443 if (active) { 3444 uport->hw_stopped = false; 3445 uport->ops->start_tx(uport); 3446 uart_write_wakeup(uport); 3447 } 3448 } else { 3449 if (!active) { 3450 uport->hw_stopped = true; 3451 uport->ops->stop_tx(uport); 3452 } 3453 } 3454 3455 } 3456 } 3457 EXPORT_SYMBOL_GPL(uart_handle_cts_change); 3458 3459 /** 3460 * uart_insert_char - push a char to the uart layer 3461 * 3462 * User is responsible to call tty_flip_buffer_push when they are done with 3463 * insertion. 3464 * 3465 * @port: corresponding port 3466 * @status: state of the serial port RX buffer (LSR for 8250) 3467 * @overrun: mask of overrun bits in @status 3468 * @ch: character to push 3469 * @flag: flag for the character (see TTY_NORMAL and friends) 3470 */ 3471 void uart_insert_char(struct uart_port *port, unsigned int status, 3472 unsigned int overrun, u8 ch, u8 flag) 3473 { 3474 struct tty_port *tport = &port->state->port; 3475 3476 if ((status & port->ignore_status_mask & ~overrun) == 0) 3477 if (tty_insert_flip_char(tport, ch, flag) == 0) 3478 ++port->icount.buf_overrun; 3479 3480 /* 3481 * Overrun is special. Since it's reported immediately, 3482 * it doesn't affect the current character. 3483 */ 3484 if (status & ~port->ignore_status_mask & overrun) 3485 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0) 3486 ++port->icount.buf_overrun; 3487 } 3488 EXPORT_SYMBOL_GPL(uart_insert_char); 3489 3490 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL 3491 static const u8 sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE; 3492 3493 static void uart_sysrq_on(struct work_struct *w) 3494 { 3495 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq); 3496 3497 sysrq_toggle_support(1); 3498 pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n", 3499 sysrq_toggle_seq_len, sysrq_toggle_seq); 3500 } 3501 static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on); 3502 3503 /** 3504 * uart_try_toggle_sysrq - Enables SysRq from serial line 3505 * @port: uart_port structure where char(s) after BREAK met 3506 * @ch: new character in the sequence after received BREAK 3507 * 3508 * Enables magic SysRq when the required sequence is met on port 3509 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE). 3510 * 3511 * Returns: %false if @ch is out of enabling sequence and should be 3512 * handled some other way, %true if @ch was consumed. 3513 */ 3514 bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch) 3515 { 3516 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq); 3517 3518 if (!sysrq_toggle_seq_len) 3519 return false; 3520 3521 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX); 3522 if (sysrq_toggle_seq[port->sysrq_seq] != ch) { 3523 port->sysrq_seq = 0; 3524 return false; 3525 } 3526 3527 if (++port->sysrq_seq < sysrq_toggle_seq_len) { 3528 port->sysrq = jiffies + SYSRQ_TIMEOUT; 3529 return true; 3530 } 3531 3532 schedule_work(&sysrq_enable_work); 3533 3534 port->sysrq = 0; 3535 return true; 3536 } 3537 EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq); 3538 #endif 3539 3540 /** 3541 * uart_get_rs485_mode() - retrieve rs485 properties for given uart 3542 * @port: uart device's target port 3543 * 3544 * This function implements the device tree binding described in 3545 * Documentation/devicetree/bindings/serial/rs485.yaml. 3546 */ 3547 int uart_get_rs485_mode(struct uart_port *port) 3548 { 3549 struct serial_rs485 *rs485conf = &port->rs485; 3550 struct device *dev = port->dev; 3551 enum gpiod_flags dflags; 3552 struct gpio_desc *desc; 3553 u32 rs485_delay[2]; 3554 int ret; 3555 3556 if (!(port->rs485_supported.flags & SER_RS485_ENABLED)) 3557 return 0; 3558 3559 /* 3560 * Retrieve properties only if a firmware node exists. If no firmware 3561 * node exists, then don't touch rs485 config and keep initial rs485 3562 * properties set by driver. 3563 */ 3564 if (!dev_fwnode(dev)) 3565 return 0; 3566 3567 ret = device_property_read_u32_array(dev, "rs485-rts-delay", 3568 rs485_delay, 2); 3569 if (!ret) { 3570 rs485conf->delay_rts_before_send = rs485_delay[0]; 3571 rs485conf->delay_rts_after_send = rs485_delay[1]; 3572 } else { 3573 rs485conf->delay_rts_before_send = 0; 3574 rs485conf->delay_rts_after_send = 0; 3575 } 3576 3577 uart_sanitize_serial_rs485_delays(port, rs485conf); 3578 3579 /* 3580 * Clear full-duplex and enabled flags, set RTS polarity to active high 3581 * to get to a defined state with the following properties: 3582 */ 3583 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED | 3584 SER_RS485_TERMINATE_BUS | 3585 SER_RS485_RTS_AFTER_SEND); 3586 rs485conf->flags |= SER_RS485_RTS_ON_SEND; 3587 3588 if (device_property_read_bool(dev, "rs485-rx-during-tx")) 3589 rs485conf->flags |= SER_RS485_RX_DURING_TX; 3590 3591 if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time")) 3592 rs485conf->flags |= SER_RS485_ENABLED; 3593 3594 if (device_property_read_bool(dev, "rs485-rts-active-low")) { 3595 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND; 3596 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 3597 } 3598 3599 /* 3600 * Disabling termination by default is the safe choice: Else if many 3601 * bus participants enable it, no communication is possible at all. 3602 * Works fine for short cables and users may enable for longer cables. 3603 */ 3604 desc = devm_gpiod_get_optional(dev, "rs485-term", GPIOD_OUT_LOW); 3605 if (IS_ERR(desc)) 3606 return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-term-gpios\n"); 3607 port->rs485_term_gpio = desc; 3608 if (port->rs485_term_gpio) 3609 port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS; 3610 3611 dflags = (rs485conf->flags & SER_RS485_RX_DURING_TX) ? 3612 GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 3613 desc = devm_gpiod_get_optional(dev, "rs485-rx-during-tx", dflags); 3614 if (IS_ERR(desc)) 3615 return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-rx-during-tx-gpios\n"); 3616 port->rs485_rx_during_tx_gpio = desc; 3617 if (port->rs485_rx_during_tx_gpio) 3618 port->rs485_supported.flags |= SER_RS485_RX_DURING_TX; 3619 3620 return 0; 3621 } 3622 EXPORT_SYMBOL_GPL(uart_get_rs485_mode); 3623 3624 /* Compile-time assertions for serial_rs485 layout */ 3625 static_assert(offsetof(struct serial_rs485, padding) == 3626 (offsetof(struct serial_rs485, delay_rts_after_send) + sizeof(__u32))); 3627 static_assert(offsetof(struct serial_rs485, padding1) == 3628 offsetof(struct serial_rs485, padding[1])); 3629 static_assert((offsetof(struct serial_rs485, padding[4]) + sizeof(__u32)) == 3630 sizeof(struct serial_rs485)); 3631 3632 MODULE_DESCRIPTION("Serial driver core"); 3633 MODULE_LICENSE("GPL"); 3634