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/ctype.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/kernel.h> 20 #include <linux/of.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/proc_fs.h> 23 #include <linux/seq_file.h> 24 #include <linux/device.h> 25 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */ 26 #include <linux/serial_core.h> 27 #include <linux/sysrq.h> 28 #include <linux/delay.h> 29 #include <linux/mutex.h> 30 #include <linux/math64.h> 31 #include <linux/security.h> 32 33 #include <linux/irq.h> 34 #include <linux/uaccess.h> 35 36 #include "serial_base.h" 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 u8 *buf; 251 252 /* 253 * Initialise and allocate the transmit and temporary 254 * buffer. 255 */ 256 buf = kzalloc(PAGE_SIZE, GFP_KERNEL); 257 if (!buf) 258 return -ENOMEM; 259 260 uport = uart_port_ref_lock(state, &flags); 261 if (!state->port.xmit_buf) { 262 state->port.xmit_buf = buf; 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 buffer under the port lock, see 270 * uart_free_xmit_buf(). 271 */ 272 kfree(buf); 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 u8 *xmit_buf; 284 285 /* 286 * Do not free() the transmit buffer 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 kfree(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 /* 900 * TTY_IO_ERROR is also set on a port that could not be started, and 901 * this is how such a port is configured. Only refuse a hung up port. 902 */ 903 if (!uport || !tty_port_active(port)) 904 return -EIO; 905 906 new_port = new_info->port; 907 if (HIGH_BITS_OFFSET) 908 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET; 909 910 new_info->irq = irq_canonicalize(new_info->irq); 911 close_delay = msecs_to_jiffies(new_info->close_delay * 10); 912 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ? 913 ASYNC_CLOSING_WAIT_NONE : 914 msecs_to_jiffies(new_info->closing_wait * 10); 915 916 917 change_irq = !(uport->flags & UPF_FIXED_PORT) 918 && new_info->irq != uport->irq; 919 920 /* 921 * Since changing the 'type' of the port changes its resource 922 * allocations, we should treat type changes the same as 923 * IO port changes. 924 */ 925 change_port = !(uport->flags & UPF_FIXED_PORT) 926 && (new_port != uport->iobase || 927 (unsigned long)new_info->iomem_base != uport->mapbase || 928 new_info->hub6 != uport->hub6 || 929 new_info->io_type != uport->iotype || 930 new_info->iomem_reg_shift != uport->regshift || 931 new_info->type != uport->type); 932 933 old_flags = uport->flags; 934 new_flags = (__force upf_t)new_info->flags; 935 old_custom_divisor = uport->custom_divisor; 936 937 if (!(uport->flags & UPF_FIXED_PORT)) { 938 unsigned int uartclk = new_info->baud_base * 16; 939 /* check needs to be done here before other settings made */ 940 if (uartclk == 0) 941 return -EINVAL; 942 } 943 if (!capable(CAP_SYS_ADMIN)) { 944 if (change_irq || change_port || 945 (new_info->baud_base != uport->uartclk / 16) || 946 (close_delay != port->close_delay) || 947 (closing_wait != port->closing_wait) || 948 (new_info->xmit_fifo_size && 949 new_info->xmit_fifo_size != uport->fifosize) || 950 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0)) 951 return -EPERM; 952 uport->flags = ((uport->flags & ~UPF_USR_MASK) | 953 (new_flags & UPF_USR_MASK)); 954 uport->custom_divisor = new_info->custom_divisor; 955 goto check_and_exit; 956 } 957 958 if (change_irq || change_port) { 959 retval = security_locked_down(LOCKDOWN_TIOCSSERIAL); 960 if (retval) 961 return retval; 962 } 963 964 /* Ask the low level driver to verify the settings. */ 965 if (uport->ops->verify_port) { 966 retval = uport->ops->verify_port(uport, new_info); 967 if (retval) 968 return retval; 969 } 970 971 if ((new_info->irq >= irq_get_nr_irqs()) || (new_info->irq < 0) || 972 (new_info->baud_base < 9600)) 973 return -EINVAL; 974 975 if (change_port || change_irq) { 976 /* Make sure that we are the sole user of this port. */ 977 if (tty_port_users(port) > 1) 978 return -EBUSY; 979 980 /* 981 * We need to shutdown the serial port at the old 982 * port/type/irq combination. 983 */ 984 uart_shutdown(tty, state); 985 } 986 987 if (change_port) { 988 retval = uart_change_port(uport, new_info, new_port); 989 if (retval) 990 return retval; 991 } 992 993 if (change_irq) 994 uport->irq = new_info->irq; 995 if (!(uport->flags & UPF_FIXED_PORT)) 996 uport->uartclk = new_info->baud_base * 16; 997 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) | 998 (new_flags & UPF_CHANGE_MASK); 999 uport->custom_divisor = new_info->custom_divisor; 1000 port->close_delay = close_delay; 1001 port->closing_wait = closing_wait; 1002 if (new_info->xmit_fifo_size) 1003 uport->fifosize = new_info->xmit_fifo_size; 1004 1005 check_and_exit: 1006 if (uport->type == PORT_UNKNOWN) 1007 return 0; 1008 1009 if (tty_port_initialized(port)) { 1010 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) || 1011 old_custom_divisor != uport->custom_divisor) { 1012 /* 1013 * If they're setting up a custom divisor or speed, 1014 * instead of clearing it, then bitch about it. 1015 */ 1016 if (uport->flags & UPF_SPD_MASK) { 1017 dev_notice_ratelimited(uport->dev, 1018 "%s sets custom speed on %s. This is deprecated.\n", 1019 current->comm, 1020 tty_name(port->tty)); 1021 } 1022 uart_change_line_settings(tty, state, NULL); 1023 } 1024 1025 return 0; 1026 } 1027 1028 retval = uart_startup(tty, state, true); 1029 if (retval < 0) 1030 return retval; 1031 if (retval == 0) 1032 tty_port_set_initialized(port, true); 1033 1034 return 0; 1035 } 1036 1037 static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss) 1038 { 1039 struct uart_state *state = tty->driver_data; 1040 struct tty_port *port = &state->port; 1041 1042 guard(rwsem_write)(&tty->termios_rwsem); 1043 /* 1044 * This semaphore protects port->count. It is also 1045 * very useful to prevent opens. Also, take the 1046 * port configuration semaphore to make sure that a 1047 * module insertion/removal doesn't change anything 1048 * under us. 1049 */ 1050 guard(mutex)(&port->mutex); 1051 return uart_set_info(tty, port, state, ss); 1052 } 1053 1054 /** 1055 * uart_get_lsr_info - get line status register info 1056 * @tty: tty associated with the UART 1057 * @state: UART being queried 1058 * @value: returned modem value 1059 */ 1060 static int uart_get_lsr_info(struct tty_struct *tty, 1061 struct uart_state *state, unsigned int __user *value) 1062 { 1063 struct uart_port *uport = uart_port_check(state); 1064 unsigned int result; 1065 1066 result = uport->ops->tx_empty(uport); 1067 1068 /* 1069 * If we're about to load something into the transmit 1070 * register, we'll pretend the transmitter isn't empty to 1071 * avoid a race condition (depending on when the transmit 1072 * interrupt happens). 1073 */ 1074 if (uport->x_char || 1075 (!kfifo_is_empty(&state->port.xmit_fifo) && 1076 !uart_tx_stopped(uport))) 1077 result &= ~TIOCSER_TEMT; 1078 1079 return put_user(result, value); 1080 } 1081 1082 static int uart_tiocmget(struct tty_struct *tty) 1083 { 1084 struct uart_state *state = tty->driver_data; 1085 struct tty_port *port = &state->port; 1086 struct uart_port *uport; 1087 1088 guard(mutex)(&port->mutex); 1089 1090 uport = uart_port_check(state); 1091 if (!uport || tty_io_error(tty)) 1092 return -EIO; 1093 1094 guard(uart_port_lock_irq)(uport); 1095 1096 return uport->mctrl | uport->ops->get_mctrl(uport); 1097 } 1098 1099 static int 1100 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) 1101 { 1102 struct uart_state *state = tty->driver_data; 1103 struct tty_port *port = &state->port; 1104 struct uart_port *uport; 1105 1106 guard(mutex)(&port->mutex); 1107 1108 uport = uart_port_check(state); 1109 if (!uport || tty_io_error(tty)) 1110 return -EIO; 1111 1112 uart_update_mctrl(uport, set, clear); 1113 1114 return 0; 1115 } 1116 1117 static int uart_break_ctl(struct tty_struct *tty, int break_state) 1118 { 1119 struct uart_state *state = tty->driver_data; 1120 struct tty_port *port = &state->port; 1121 struct uart_port *uport; 1122 1123 guard(mutex)(&port->mutex); 1124 1125 uport = uart_port_check(state); 1126 if (!uport || tty_io_error(tty)) 1127 return -EIO; 1128 1129 if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl) 1130 uport->ops->break_ctl(uport, break_state); 1131 1132 return 0; 1133 } 1134 1135 static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state) 1136 { 1137 struct tty_port *port = &state->port; 1138 struct uart_port *uport; 1139 int flags, ret; 1140 1141 if (!capable(CAP_SYS_ADMIN)) 1142 return -EPERM; 1143 1144 /* 1145 * Take the per-port semaphore. This prevents count from 1146 * changing, and hence any extra opens of the port while 1147 * we're auto-configuring. 1148 */ 1149 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->mutex) { 1150 uport = uart_port_check(state); 1151 /* As in uart_set_info(), only refuse a hung up port. */ 1152 if (!uport || !tty_port_active(port)) 1153 return -EIO; 1154 1155 if (tty_port_users(port) != 1) 1156 return -EBUSY; 1157 1158 uart_shutdown(tty, state); 1159 1160 /* 1161 * If we already have a port type configured, 1162 * we must release its resources. 1163 */ 1164 if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 1165 uport->ops->release_port(uport); 1166 1167 flags = UART_CONFIG_TYPE; 1168 if (uport->flags & UPF_AUTO_IRQ) 1169 flags |= UART_CONFIG_IRQ; 1170 1171 /* 1172 * This will claim the ports resources if 1173 * a port is found. 1174 */ 1175 uport->ops->config_port(uport, flags); 1176 1177 ret = uart_startup(tty, state, true); 1178 if (ret < 0) 1179 return ret; 1180 if (ret > 0) 1181 return 0; 1182 1183 tty_port_set_initialized(port, true); 1184 } 1185 1186 return 0; 1187 } 1188 1189 static void uart_enable_ms(struct uart_port *uport) 1190 { 1191 /* 1192 * Force modem status interrupts on 1193 */ 1194 if (uport->ops->enable_ms) 1195 uport->ops->enable_ms(uport); 1196 } 1197 1198 /* 1199 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 1200 * - mask passed in arg for lines of interest 1201 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 1202 * Caller should use TIOCGICOUNT to see which one it was 1203 * 1204 * FIXME: This wants extracting into a common all driver implementation 1205 * of TIOCMWAIT using tty_port. 1206 */ 1207 static int uart_wait_modem_status(struct tty_struct *tty, struct uart_state *state, unsigned long arg) 1208 { 1209 struct uart_port *uport; 1210 struct tty_port *port = &state->port; 1211 DECLARE_WAITQUEUE(wait, current); 1212 struct uart_icount cprev, cnow; 1213 int ret; 1214 1215 /* 1216 * note the counters on entry 1217 */ 1218 uport = uart_port_ref(state); 1219 if (!uport) 1220 return -EIO; 1221 1222 mutex_lock(&port->mutex); 1223 if (tty_io_error(tty)) { 1224 mutex_unlock(&port->mutex); 1225 ret = -EIO; 1226 goto out_deref; 1227 } 1228 1229 uart_port_lock_irq(uport); 1230 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount)); 1231 uart_enable_ms(uport); 1232 uart_port_unlock_irq(uport); 1233 1234 mutex_unlock(&port->mutex); 1235 1236 add_wait_queue(&port->delta_msr_wait, &wait); 1237 for (;;) { 1238 set_current_state(TASK_INTERRUPTIBLE); 1239 1240 uart_port_lock_irq(uport); 1241 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1242 uart_port_unlock_irq(uport); 1243 1244 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1245 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1246 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 1247 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 1248 ret = 0; 1249 break; 1250 } 1251 1252 if (tty_io_error(tty)) { 1253 ret = -EIO; 1254 break; 1255 } 1256 1257 schedule(); 1258 1259 /* see if a signal did it */ 1260 if (signal_pending(current)) { 1261 ret = -ERESTARTSYS; 1262 break; 1263 } 1264 1265 cprev = cnow; 1266 } 1267 __set_current_state(TASK_RUNNING); 1268 remove_wait_queue(&port->delta_msr_wait, &wait); 1269 out_deref: 1270 uart_port_deref(uport); 1271 1272 return ret; 1273 } 1274 1275 /* 1276 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) 1277 * Return: write counters to the user passed counter struct 1278 * NB: both 1->0 and 0->1 transitions are counted except for 1279 * RI where only 0->1 is counted. 1280 */ 1281 static int uart_get_icount(struct tty_struct *tty, 1282 struct serial_icounter_struct *icount) 1283 { 1284 struct uart_state *state = tty->driver_data; 1285 struct uart_icount cnow; 1286 struct uart_port *uport; 1287 unsigned long flags; 1288 1289 uport = uart_port_ref_lock(state, &flags); 1290 if (!uport) 1291 return -EIO; 1292 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount)); 1293 uart_port_unlock_deref(uport, flags); 1294 1295 icount->cts = cnow.cts; 1296 icount->dsr = cnow.dsr; 1297 icount->rng = cnow.rng; 1298 icount->dcd = cnow.dcd; 1299 icount->rx = cnow.rx; 1300 icount->tx = cnow.tx; 1301 icount->frame = cnow.frame; 1302 icount->overrun = cnow.overrun; 1303 icount->parity = cnow.parity; 1304 icount->brk = cnow.brk; 1305 icount->buf_overrun = cnow.buf_overrun; 1306 1307 return 0; 1308 } 1309 1310 #define SER_RS485_LEGACY_FLAGS (SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \ 1311 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \ 1312 SER_RS485_TERMINATE_BUS) 1313 1314 static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485) 1315 { 1316 u32 flags = rs485->flags; 1317 1318 /* Don't return -EINVAL for unsupported legacy flags */ 1319 flags &= ~SER_RS485_LEGACY_FLAGS; 1320 1321 /* 1322 * For any bit outside of the legacy ones that is not supported by 1323 * the driver, return -EINVAL. 1324 */ 1325 if (flags & ~port->rs485_supported.flags) 1326 return -EINVAL; 1327 1328 /* Asking for address w/o addressing mode? */ 1329 if (!(rs485->flags & SER_RS485_ADDRB) && 1330 (rs485->flags & (SER_RS485_ADDR_RECV|SER_RS485_ADDR_DEST))) 1331 return -EINVAL; 1332 1333 /* Address given but not enabled? */ 1334 if (!(rs485->flags & SER_RS485_ADDR_RECV) && rs485->addr_recv) 1335 return -EINVAL; 1336 if (!(rs485->flags & SER_RS485_ADDR_DEST) && rs485->addr_dest) 1337 return -EINVAL; 1338 1339 return 0; 1340 } 1341 1342 static void uart_sanitize_serial_rs485_delays(struct uart_port *port, 1343 struct serial_rs485 *rs485) 1344 { 1345 if (!port->rs485_supported.delay_rts_before_send) { 1346 if (rs485->delay_rts_before_send) { 1347 dev_warn_ratelimited(port->dev, 1348 "%s (%u): RTS delay before sending not supported\n", 1349 port->name, port->line); 1350 } 1351 rs485->delay_rts_before_send = 0; 1352 } else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) { 1353 rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY; 1354 dev_warn_ratelimited(port->dev, 1355 "%s (%u): RTS delay before sending clamped to %u ms\n", 1356 port->name, port->line, rs485->delay_rts_before_send); 1357 } 1358 1359 if (!port->rs485_supported.delay_rts_after_send) { 1360 if (rs485->delay_rts_after_send) { 1361 dev_warn_ratelimited(port->dev, 1362 "%s (%u): RTS delay after sending not supported\n", 1363 port->name, port->line); 1364 } 1365 rs485->delay_rts_after_send = 0; 1366 } else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) { 1367 rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY; 1368 dev_warn_ratelimited(port->dev, 1369 "%s (%u): RTS delay after sending clamped to %u ms\n", 1370 port->name, port->line, rs485->delay_rts_after_send); 1371 } 1372 } 1373 1374 static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485) 1375 { 1376 u32 supported_flags = port->rs485_supported.flags; 1377 1378 if (!(rs485->flags & SER_RS485_ENABLED)) { 1379 memset(rs485, 0, sizeof(*rs485)); 1380 return; 1381 } 1382 1383 /* Clear other RS485 flags but SER_RS485_TERMINATE_BUS and return if enabling RS422 */ 1384 if (rs485->flags & SER_RS485_MODE_RS422) { 1385 rs485->flags &= (SER_RS485_ENABLED | SER_RS485_MODE_RS422 | SER_RS485_TERMINATE_BUS); 1386 return; 1387 } 1388 1389 rs485->flags &= supported_flags; 1390 1391 /* Pick sane settings if the user hasn't */ 1392 if (!(rs485->flags & SER_RS485_RTS_ON_SEND) == 1393 !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) { 1394 if (supported_flags & SER_RS485_RTS_ON_SEND) { 1395 rs485->flags |= SER_RS485_RTS_ON_SEND; 1396 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND; 1397 1398 dev_warn_ratelimited(port->dev, 1399 "%s (%u): invalid RTS setting, using RTS_ON_SEND instead\n", 1400 port->name, port->line); 1401 } else { 1402 rs485->flags |= SER_RS485_RTS_AFTER_SEND; 1403 rs485->flags &= ~SER_RS485_RTS_ON_SEND; 1404 1405 dev_warn_ratelimited(port->dev, 1406 "%s (%u): invalid RTS setting, using RTS_AFTER_SEND instead\n", 1407 port->name, port->line); 1408 } 1409 } 1410 1411 uart_sanitize_serial_rs485_delays(port, rs485); 1412 1413 /* Return clean padding area to userspace */ 1414 memset(rs485->padding0, 0, sizeof(rs485->padding0)); 1415 memset(rs485->padding1, 0, sizeof(rs485->padding1)); 1416 } 1417 1418 static void uart_set_rs485_termination(struct uart_port *port, 1419 const struct serial_rs485 *rs485) 1420 { 1421 if (!(rs485->flags & SER_RS485_ENABLED)) 1422 return; 1423 1424 gpiod_set_value_cansleep(port->rs485_term_gpio, 1425 !!(rs485->flags & SER_RS485_TERMINATE_BUS)); 1426 } 1427 1428 static void uart_set_rs485_rx_during_tx(struct uart_port *port, 1429 const struct serial_rs485 *rs485) 1430 { 1431 if (!(rs485->flags & SER_RS485_ENABLED)) 1432 return; 1433 1434 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 1435 !!(rs485->flags & SER_RS485_RX_DURING_TX)); 1436 } 1437 1438 static int uart_rs485_config(struct uart_port *port) 1439 { 1440 struct serial_rs485 *rs485 = &port->rs485; 1441 int ret; 1442 1443 if (!(rs485->flags & SER_RS485_ENABLED)) 1444 return 0; 1445 1446 uart_sanitize_serial_rs485(port, rs485); 1447 uart_set_rs485_termination(port, rs485); 1448 uart_set_rs485_rx_during_tx(port, rs485); 1449 1450 scoped_guard(uart_port_lock_irqsave, port) 1451 ret = port->rs485_config(port, NULL, rs485); 1452 if (ret) { 1453 memset(rs485, 0, sizeof(*rs485)); 1454 /* unset GPIOs */ 1455 gpiod_set_value_cansleep(port->rs485_term_gpio, 0); 1456 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0); 1457 } 1458 1459 return ret; 1460 } 1461 1462 static int uart_get_rs485_config(struct uart_port *port, 1463 struct serial_rs485 __user *rs485) 1464 { 1465 struct serial_rs485 aux; 1466 1467 scoped_guard(uart_port_lock_irqsave, port) 1468 aux = port->rs485; 1469 1470 if (copy_to_user(rs485, &aux, sizeof(aux))) 1471 return -EFAULT; 1472 1473 return 0; 1474 } 1475 1476 static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port, 1477 struct serial_rs485 __user *rs485_user) 1478 { 1479 struct serial_rs485 rs485; 1480 int ret; 1481 1482 if (!(port->rs485_supported.flags & SER_RS485_ENABLED)) 1483 return -ENOTTY; 1484 1485 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user))) 1486 return -EFAULT; 1487 1488 ret = uart_check_rs485_flags(port, &rs485); 1489 if (ret) 1490 return ret; 1491 uart_sanitize_serial_rs485(port, &rs485); 1492 uart_set_rs485_termination(port, &rs485); 1493 uart_set_rs485_rx_during_tx(port, &rs485); 1494 1495 scoped_guard(uart_port_lock_irqsave, port) { 1496 ret = port->rs485_config(port, &tty->termios, &rs485); 1497 if (!ret) { 1498 port->rs485 = rs485; 1499 1500 /* Reset RTS and other mctrl lines when disabling RS485 */ 1501 if (!(rs485.flags & SER_RS485_ENABLED)) 1502 port->ops->set_mctrl(port, port->mctrl); 1503 } 1504 } 1505 if (ret) { 1506 /* restore old GPIO settings */ 1507 gpiod_set_value_cansleep(port->rs485_term_gpio, 1508 !!(port->rs485.flags & SER_RS485_TERMINATE_BUS)); 1509 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 1510 !!(port->rs485.flags & SER_RS485_RX_DURING_TX)); 1511 return ret; 1512 } 1513 1514 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485))) 1515 return -EFAULT; 1516 1517 return 0; 1518 } 1519 1520 static int uart_get_iso7816_config(struct uart_port *port, 1521 struct serial_iso7816 __user *iso7816) 1522 { 1523 struct serial_iso7816 aux; 1524 1525 if (!port->iso7816_config) 1526 return -ENOTTY; 1527 1528 scoped_guard(uart_port_lock_irqsave, port) 1529 aux = port->iso7816; 1530 1531 if (copy_to_user(iso7816, &aux, sizeof(aux))) 1532 return -EFAULT; 1533 1534 return 0; 1535 } 1536 1537 static int uart_set_iso7816_config(struct uart_port *port, 1538 struct serial_iso7816 __user *iso7816_user) 1539 { 1540 struct serial_iso7816 iso7816; 1541 int i; 1542 1543 if (!port->iso7816_config) 1544 return -ENOTTY; 1545 1546 if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user))) 1547 return -EFAULT; 1548 1549 /* 1550 * There are 5 words reserved for future use. Check that userspace 1551 * doesn't put stuff in there to prevent breakages in the future. 1552 */ 1553 for (i = 0; i < ARRAY_SIZE(iso7816.reserved); i++) 1554 if (iso7816.reserved[i]) 1555 return -EINVAL; 1556 1557 scoped_guard(uart_port_lock_irqsave, port) { 1558 int ret = port->iso7816_config(port, &iso7816); 1559 if (ret) 1560 return ret; 1561 } 1562 1563 if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816))) 1564 return -EFAULT; 1565 1566 return 0; 1567 } 1568 1569 /* 1570 * Called via sys_ioctl. We can use spin_lock_irq() here. 1571 */ 1572 static int 1573 uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) 1574 { 1575 struct uart_state *state = tty->driver_data; 1576 struct tty_port *port = &state->port; 1577 struct uart_port *uport; 1578 void __user *uarg = (void __user *)arg; 1579 int ret = -ENOIOCTLCMD; 1580 1581 /* This ioctl doesn't rely on the hardware to be present. */ 1582 if (cmd == TIOCSERCONFIG) { 1583 guard(rwsem_write)(&tty->termios_rwsem); 1584 return uart_do_autoconfig(tty, state); 1585 } 1586 1587 if (tty_io_error(tty)) 1588 return -EIO; 1589 1590 /* This should only be used when the hardware is present. */ 1591 if (cmd == TIOCMIWAIT) 1592 return uart_wait_modem_status(tty, state, arg); 1593 1594 /* rs485_config requires more locking than others */ 1595 if (cmd == TIOCSRS485) 1596 down_write(&tty->termios_rwsem); 1597 1598 scoped_guard(mutex, &port->mutex) { 1599 uport = uart_port_check(state); 1600 1601 if (!uport || tty_io_error(tty)) { 1602 ret = -EIO; 1603 break; 1604 } 1605 1606 /* 1607 * All these rely on hardware being present and need to be 1608 * protected against the tty being hung up. 1609 */ 1610 1611 switch (cmd) { 1612 case TIOCSERGETLSR: /* Get line status register */ 1613 ret = uart_get_lsr_info(tty, state, uarg); 1614 break; 1615 1616 case TIOCGRS485: 1617 ret = uart_get_rs485_config(uport, uarg); 1618 break; 1619 1620 case TIOCSRS485: 1621 ret = uart_set_rs485_config(tty, uport, uarg); 1622 break; 1623 1624 case TIOCSISO7816: 1625 ret = uart_set_iso7816_config(state->uart_port, uarg); 1626 break; 1627 1628 case TIOCGISO7816: 1629 ret = uart_get_iso7816_config(state->uart_port, uarg); 1630 break; 1631 default: 1632 if (uport->ops->ioctl) 1633 ret = uport->ops->ioctl(uport, cmd, arg); 1634 break; 1635 } 1636 } 1637 1638 if (cmd == TIOCSRS485) 1639 up_write(&tty->termios_rwsem); 1640 1641 return ret; 1642 } 1643 1644 static void uart_set_ldisc(struct tty_struct *tty) 1645 { 1646 struct uart_state *state = tty->driver_data; 1647 struct uart_port *uport; 1648 1649 guard(mutex)(&state->port.mutex); 1650 uport = uart_port_check(state); 1651 if (!uport || tty_io_error(tty)) 1652 return; 1653 1654 if (uport->ops->set_ldisc) 1655 uport->ops->set_ldisc(uport, &tty->termios); 1656 } 1657 1658 static void uart_set_termios(struct tty_struct *tty, 1659 const struct ktermios *old_termios) 1660 { 1661 struct uart_state *state = tty->driver_data; 1662 struct uart_port *uport; 1663 unsigned int cflag = tty->termios.c_cflag; 1664 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK; 1665 bool sw_changed = false; 1666 1667 guard(mutex)(&state->port.mutex); 1668 1669 uport = uart_port_check(state); 1670 if (!uport || tty_io_error(tty)) 1671 return; 1672 1673 /* 1674 * Drivers doing software flow control also need to know 1675 * about changes to these input settings. 1676 */ 1677 if (uport->flags & UPF_SOFT_FLOW) { 1678 iflag_mask |= IXANY|IXON|IXOFF; 1679 sw_changed = 1680 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] || 1681 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP]; 1682 } 1683 1684 /* 1685 * These are the bits that are used to setup various 1686 * flags in the low level driver. We can ignore the Bfoo 1687 * bits in c_cflag; c_[io]speed will always be set 1688 * appropriately by set_termios() in tty_ioctl.c 1689 */ 1690 if ((cflag ^ old_termios->c_cflag) == 0 && 1691 tty->termios.c_ospeed == old_termios->c_ospeed && 1692 tty->termios.c_ispeed == old_termios->c_ispeed && 1693 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 && 1694 !sw_changed) 1695 return; 1696 1697 uart_change_line_settings(tty, state, old_termios); 1698 /* reload cflag from termios; port driver may have overridden flags */ 1699 cflag = tty->termios.c_cflag; 1700 1701 /* Handle transition to B0 status */ 1702 if (((old_termios->c_cflag & CBAUD) != B0) && ((cflag & CBAUD) == B0)) 1703 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR); 1704 /* Handle transition away from B0 status */ 1705 else if (((old_termios->c_cflag & CBAUD) == B0) && ((cflag & CBAUD) != B0)) { 1706 unsigned int mask = TIOCM_DTR; 1707 1708 if (!(cflag & CRTSCTS) || !tty_throttled(tty)) 1709 mask |= TIOCM_RTS; 1710 uart_set_mctrl(uport, mask); 1711 } 1712 } 1713 1714 /* 1715 * Calls to uart_close() are serialised via the tty_lock in 1716 * drivers/tty/tty_io.c:tty_release() 1717 * drivers/tty/tty_io.c:do_tty_hangup() 1718 */ 1719 static void uart_close(struct tty_struct *tty, struct file *filp) 1720 { 1721 struct uart_state *state = tty->driver_data; 1722 1723 if (!state) { 1724 struct uart_driver *drv = tty->driver->driver_state; 1725 struct tty_port *port; 1726 1727 state = drv->state + tty->index; 1728 port = &state->port; 1729 guard(spinlock_irq)(&port->lock); 1730 --port->count; 1731 return; 1732 } 1733 1734 pr_debug("uart_close(%d) called\n", tty->index); 1735 1736 tty_port_close(tty->port, tty, filp); 1737 } 1738 1739 static void uart_tty_port_shutdown(struct tty_port *port) 1740 { 1741 struct uart_state *state = container_of(port, struct uart_state, port); 1742 struct uart_port *uport = uart_port_check(state); 1743 1744 /* 1745 * At this point, we stop accepting input. To do this, we 1746 * disable the receive line status interrupts. 1747 */ 1748 if (WARN(!uport, "detached port still initialized!\n")) 1749 return; 1750 1751 scoped_guard(uart_port_lock_irq, uport) 1752 uport->ops->stop_rx(uport); 1753 1754 serial_base_port_shutdown(uport); 1755 uart_port_shutdown(port); 1756 1757 /* 1758 * It's possible for shutdown to be called after suspend if we get 1759 * a DCD drop (hangup) at just the right time. Clear suspended bit so 1760 * we don't try to resume a port that has been shutdown. 1761 */ 1762 tty_port_set_suspended(port, false); 1763 1764 uart_free_xmit_buf(port); 1765 1766 uart_change_pm(state, UART_PM_STATE_OFF); 1767 } 1768 1769 static void uart_wait_until_sent(struct tty_struct *tty, int timeout) 1770 { 1771 struct uart_state *state = tty->driver_data; 1772 struct uart_port *port; 1773 unsigned long char_time, expire, fifo_timeout; 1774 1775 port = uart_port_ref(state); 1776 if (!port) 1777 return; 1778 1779 if (port->type == PORT_UNKNOWN || port->fifosize == 0) { 1780 uart_port_deref(port); 1781 return; 1782 } 1783 1784 /* 1785 * Set the check interval to be 1/5 of the estimated time to 1786 * send a single character, and make it at least 1. The check 1787 * interval should also be less than the timeout. 1788 * 1789 * Note: we have to use pretty tight timings here to satisfy 1790 * the NIST-PCTS. 1791 */ 1792 char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL); 1793 1794 if (timeout && timeout < char_time) 1795 char_time = timeout; 1796 1797 if (!uart_cts_enabled(port)) { 1798 /* 1799 * If the transmitter hasn't cleared in twice the approximate 1800 * amount of time to send the entire FIFO, it probably won't 1801 * ever clear. This assumes the UART isn't doing flow 1802 * control, which is currently the case. Hence, if it ever 1803 * takes longer than FIFO timeout, this is probably due to a 1804 * UART bug of some kind. So, we clamp the timeout parameter at 1805 * 2 * FIFO timeout. 1806 */ 1807 fifo_timeout = uart_fifo_timeout(port); 1808 if (timeout == 0 || timeout > 2 * fifo_timeout) 1809 timeout = 2 * fifo_timeout; 1810 } 1811 1812 expire = jiffies + timeout; 1813 1814 pr_debug("uart_wait_until_sent(%u), jiffies=%lu, expire=%lu...\n", 1815 port->line, jiffies, expire); 1816 1817 /* 1818 * Check whether the transmitter is empty every 'char_time'. 1819 * 'timeout' / 'expire' give us the maximum amount of time 1820 * we wait. 1821 */ 1822 for (;;) { 1823 mutex_lock(&state->port.mutex); 1824 if (tty_io_error(tty) || port->ops->tx_empty(port)) { 1825 mutex_unlock(&state->port.mutex); 1826 break; 1827 } 1828 mutex_unlock(&state->port.mutex); 1829 1830 msleep_interruptible(jiffies_to_msecs(char_time)); 1831 if (signal_pending(current)) 1832 break; 1833 if (timeout && time_after(jiffies, expire)) 1834 break; 1835 } 1836 uart_port_deref(port); 1837 } 1838 1839 /* 1840 * Calls to uart_hangup() are serialised by the tty_lock in 1841 * drivers/tty/tty_io.c:do_tty_hangup() 1842 * This runs from a workqueue and can sleep for a _short_ time only. 1843 */ 1844 static void uart_hangup(struct tty_struct *tty) 1845 { 1846 struct uart_state *state = tty->driver_data; 1847 struct tty_port *port = &state->port; 1848 struct uart_port *uport; 1849 1850 pr_debug("uart_hangup(%d)\n", tty->index); 1851 1852 guard(mutex)(&port->mutex); 1853 uport = uart_port_check(state); 1854 WARN(!uport, "hangup of detached port!\n"); 1855 1856 if (tty_port_active(port)) { 1857 uart_flush_buffer(tty); 1858 uart_shutdown(tty, state); 1859 scoped_guard(spinlock_irqsave, &port->lock) 1860 port->count = 0; 1861 tty_port_set_active(port, false); 1862 tty_port_tty_set(port, NULL); 1863 if (uport && !uart_console(uport)) 1864 uart_change_pm(state, UART_PM_STATE_OFF); 1865 wake_up_interruptible(&port->open_wait); 1866 wake_up_interruptible(&port->delta_msr_wait); 1867 } 1868 } 1869 1870 /* uport == NULL if uart_port has already been removed */ 1871 static void uart_port_shutdown(struct tty_port *port) 1872 { 1873 struct uart_state *state = container_of(port, struct uart_state, port); 1874 struct uart_port *uport = uart_port_check(state); 1875 1876 /* 1877 * clear delta_msr_wait queue to avoid mem leaks: we may free 1878 * the irq here so the queue might never be woken up. Note 1879 * that we won't end up waiting on delta_msr_wait again since 1880 * any outstanding file descriptors should be pointing at 1881 * hung_up_tty_fops now. 1882 */ 1883 wake_up_interruptible(&port->delta_msr_wait); 1884 1885 if (uport) { 1886 /* Free the IRQ and disable the port. */ 1887 uport->ops->shutdown(uport); 1888 1889 /* Ensure that the IRQ handler isn't running on another CPU. */ 1890 synchronize_irq(uport->irq); 1891 } 1892 } 1893 1894 static bool uart_carrier_raised(struct tty_port *port) 1895 { 1896 struct uart_state *state = container_of(port, struct uart_state, port); 1897 struct uart_port *uport; 1898 unsigned long flags; 1899 int mctrl; 1900 1901 uport = uart_port_ref_lock(state, &flags); 1902 /* 1903 * Should never observe uport == NULL since checks for hangup should 1904 * abort the tty_port_block_til_ready() loop before checking for carrier 1905 * raised -- but report carrier raised if it does anyway so open will 1906 * continue and not sleep 1907 */ 1908 if (WARN_ON(!uport)) 1909 return true; 1910 uart_enable_ms(uport); 1911 mctrl = uport->ops->get_mctrl(uport); 1912 uart_port_unlock_deref(uport, flags); 1913 1914 return mctrl & TIOCM_CAR; 1915 } 1916 1917 static void uart_dtr_rts(struct tty_port *port, bool active) 1918 { 1919 struct uart_state *state = container_of(port, struct uart_state, port); 1920 struct uart_port *uport; 1921 1922 uport = uart_port_ref(state); 1923 if (!uport) 1924 return; 1925 uart_port_dtr_rts(uport, active); 1926 uart_port_deref(uport); 1927 } 1928 1929 static int uart_install(struct tty_driver *driver, struct tty_struct *tty) 1930 { 1931 struct uart_driver *drv = driver->driver_state; 1932 struct uart_state *state = drv->state + tty->index; 1933 1934 tty->driver_data = state; 1935 1936 return tty_standard_install(driver, tty); 1937 } 1938 1939 /* 1940 * Calls to uart_open are serialised by the tty_lock in 1941 * drivers/tty/tty_io.c:tty_open() 1942 * Note that if this fails, then uart_close() _will_ be called. 1943 * 1944 * In time, we want to scrap the "opening nonpresent ports" 1945 * behaviour and implement an alternative way for setserial 1946 * to set base addresses/ports/types. This will allow us to 1947 * get rid of a certain amount of extra tests. 1948 */ 1949 static int uart_open(struct tty_struct *tty, struct file *filp) 1950 { 1951 struct uart_state *state = tty->driver_data; 1952 int retval; 1953 1954 retval = tty_port_open(&state->port, tty, filp); 1955 if (retval > 0) 1956 retval = 0; 1957 1958 return retval; 1959 } 1960 1961 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty) 1962 { 1963 struct uart_state *state = container_of(port, struct uart_state, port); 1964 struct uart_port *uport; 1965 int ret; 1966 1967 uport = uart_port_check(state); 1968 if (!uport || uport->flags & UPF_DEAD) 1969 return -ENXIO; 1970 1971 /* 1972 * Start up the serial port. 1973 */ 1974 ret = uart_startup(tty, state, false); 1975 if (ret > 0) 1976 tty_port_set_active(port, true); 1977 1978 return ret; 1979 } 1980 1981 static const char *uart_type(struct uart_port *port) 1982 { 1983 const char *str = NULL; 1984 1985 if (port->ops->type) 1986 str = port->ops->type(port); 1987 1988 if (!str) 1989 str = "unknown"; 1990 1991 return str; 1992 } 1993 1994 bool uart_iotype_mmio(enum uart_iotype iotype) 1995 { 1996 switch (iotype) { 1997 case UPIO_MEM: 1998 case UPIO_MEM32: 1999 case UPIO_AU: 2000 case UPIO_TSI: 2001 case UPIO_MEM32BE: 2002 case UPIO_MEM16: 2003 return true; 2004 default: 2005 return false; 2006 } 2007 } 2008 EXPORT_SYMBOL_GPL(uart_iotype_mmio); 2009 2010 bool uart_iotype_io(enum uart_iotype iotype) 2011 { 2012 switch (iotype) { 2013 case UPIO_PORT: 2014 case UPIO_HUB6: 2015 return true; 2016 default: 2017 return false; 2018 } 2019 } 2020 EXPORT_SYMBOL_GPL(uart_iotype_io); 2021 2022 #ifdef CONFIG_PROC_FS 2023 2024 static void uart_line_info(struct seq_file *m, struct uart_state *state) 2025 { 2026 struct tty_port *port = &state->port; 2027 enum uart_pm_state pm_state; 2028 struct uart_port *uport; 2029 char ioinfos[64]; 2030 char stat_buf[32]; 2031 unsigned int status; 2032 2033 guard(mutex)(&port->mutex); 2034 2035 uport = uart_port_check(state); 2036 if (!uport) 2037 return; 2038 2039 seq_printf(m, "%u: uart:%s", uport->line, uart_type(uport)); 2040 uart_get_ioinfos(uport, ioinfos, sizeof(ioinfos)); 2041 seq_printf(m, "%s", ioinfos); 2042 seq_printf(m, " irq:%u", uport->irq); 2043 2044 if (uport->type == PORT_UNKNOWN) { 2045 seq_putc(m, '\n'); 2046 return; 2047 } 2048 2049 if (capable(CAP_SYS_ADMIN)) { 2050 pm_state = state->pm_state; 2051 if (pm_state != UART_PM_STATE_ON) 2052 uart_change_pm(state, UART_PM_STATE_ON); 2053 scoped_guard(uart_port_lock_irq, uport) 2054 status = uport->ops->get_mctrl(uport); 2055 if (pm_state != UART_PM_STATE_ON) 2056 uart_change_pm(state, pm_state); 2057 2058 seq_printf(m, " tx:%u rx:%u", 2059 uport->icount.tx, uport->icount.rx); 2060 if (uport->icount.frame) 2061 seq_printf(m, " fe:%u", uport->icount.frame); 2062 if (uport->icount.parity) 2063 seq_printf(m, " pe:%u", uport->icount.parity); 2064 if (uport->icount.brk) 2065 seq_printf(m, " brk:%u", uport->icount.brk); 2066 if (uport->icount.overrun) 2067 seq_printf(m, " oe:%u", uport->icount.overrun); 2068 if (uport->icount.buf_overrun) 2069 seq_printf(m, " bo:%u", uport->icount.buf_overrun); 2070 2071 #define INFOBIT(bit, str) \ 2072 if (uport->mctrl & (bit)) \ 2073 strncat(stat_buf, (str), sizeof(stat_buf) - \ 2074 strlen(stat_buf) - 2) 2075 #define STATBIT(bit, str) \ 2076 if (status & (bit)) \ 2077 strncat(stat_buf, (str), sizeof(stat_buf) - \ 2078 strlen(stat_buf) - 2) 2079 2080 stat_buf[0] = '\0'; 2081 stat_buf[1] = '\0'; 2082 INFOBIT(TIOCM_RTS, "|RTS"); 2083 STATBIT(TIOCM_CTS, "|CTS"); 2084 INFOBIT(TIOCM_DTR, "|DTR"); 2085 STATBIT(TIOCM_DSR, "|DSR"); 2086 STATBIT(TIOCM_CAR, "|CD"); 2087 STATBIT(TIOCM_RNG, "|RI"); 2088 if (stat_buf[0]) 2089 stat_buf[0] = ' '; 2090 2091 seq_puts(m, stat_buf); 2092 } 2093 seq_putc(m, '\n'); 2094 #undef STATBIT 2095 #undef INFOBIT 2096 } 2097 2098 static int uart_proc_show(struct seq_file *m, void *v) 2099 { 2100 struct tty_driver *ttydrv = m->private; 2101 struct uart_driver *drv = ttydrv->driver_state; 2102 int i; 2103 2104 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", ""); 2105 for (i = 0; i < drv->nr; i++) 2106 uart_line_info(m, drv->state + i); 2107 return 0; 2108 } 2109 #endif 2110 2111 static void uart_port_spin_lock_init(struct uart_port *port) 2112 { 2113 spin_lock_init(&port->lock); 2114 lockdep_set_class(&port->lock, &port_lock_key); 2115 } 2116 2117 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) 2118 /** 2119 * uart_console_write - write a console message to a serial port 2120 * @port: the port to write the message 2121 * @s: array of characters 2122 * @count: number of characters in string to write 2123 * @putchar: function to write character to port 2124 */ 2125 void uart_console_write(struct uart_port *port, const char *s, 2126 unsigned int count, 2127 void (*putchar)(struct uart_port *, unsigned char)) 2128 { 2129 unsigned int i; 2130 2131 for (i = 0; i < count; i++, s++) { 2132 if (*s == '\n') 2133 putchar(port, '\r'); 2134 putchar(port, *s); 2135 } 2136 } 2137 EXPORT_SYMBOL_GPL(uart_console_write); 2138 2139 /** 2140 * uart_parse_earlycon - Parse earlycon options 2141 * @p: ptr to 2nd field (ie., just beyond '<name>,'); %NULL if 2142 * no console options were supplied 2143 * @iotype: ptr for decoded iotype (out) 2144 * @addr: ptr for decoded mapbase/iobase (out) 2145 * @options: ptr for <options> field; %NULL if not present (out) 2146 * 2147 * Decodes earlycon kernel command line parameters of the form: 2148 * * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 2149 * * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options> 2150 * 2151 * The optional form: 2152 * * earlycon=<name>,0x<addr>,<options> 2153 * * console=<name>,0x<addr>,<options> 2154 * 2155 * is also accepted; the returned @iotype will be %UPIO_MEM. 2156 * 2157 * Returns: 0 on success or -%EINVAL on failure 2158 */ 2159 int uart_parse_earlycon(char *p, enum uart_iotype *iotype, 2160 resource_size_t *addr, char **options) 2161 { 2162 if (!p) 2163 return -EINVAL; 2164 2165 if (strncmp(p, "mmio,", 5) == 0) { 2166 *iotype = UPIO_MEM; 2167 p += 5; 2168 } else if (strncmp(p, "mmio16,", 7) == 0) { 2169 *iotype = UPIO_MEM16; 2170 p += 7; 2171 } else if (strncmp(p, "mmio32,", 7) == 0) { 2172 *iotype = UPIO_MEM32; 2173 p += 7; 2174 } else if (strncmp(p, "mmio32be,", 9) == 0) { 2175 *iotype = UPIO_MEM32BE; 2176 p += 9; 2177 } else if (strncmp(p, "mmio32native,", 13) == 0) { 2178 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 2179 UPIO_MEM32BE : UPIO_MEM32; 2180 p += 13; 2181 } else if (strncmp(p, "io,", 3) == 0) { 2182 *iotype = UPIO_PORT; 2183 p += 3; 2184 } else if (strncmp(p, "0x", 2) == 0) { 2185 *iotype = UPIO_MEM; 2186 } else { 2187 return -EINVAL; 2188 } 2189 2190 /* 2191 * Before you replace it with kstrtoull(), think about options separator 2192 * (',') it will not tolerate 2193 */ 2194 *addr = simple_strtoull(p, NULL, 0); 2195 p = strchr(p, ','); 2196 if (p) 2197 p++; 2198 2199 *options = p; 2200 return 0; 2201 } 2202 EXPORT_SYMBOL_GPL(uart_parse_earlycon); 2203 2204 /** 2205 * uart_parse_options - Parse serial port baud/parity/bits/flow control. 2206 * @options: pointer to option string 2207 * @baud: pointer to an 'int' variable for the baud rate. 2208 * @parity: pointer to an 'int' variable for the parity. 2209 * @bits: pointer to an 'int' variable for the number of data bits. 2210 * @flow: pointer to an 'int' variable for the flow control character. 2211 * 2212 * uart_parse_options() decodes a string containing the serial console 2213 * options. The format of the string is <baud><parity><bits><flow>, 2214 * eg: 115200n8r 2215 */ 2216 void 2217 uart_parse_options(const char *options, int *baud, int *parity, 2218 int *bits, int *flow) 2219 { 2220 const char *s = options; 2221 2222 *baud = simple_strtoul(s, NULL, 10); 2223 while (isdigit(*s)) 2224 s++; 2225 if (*s) 2226 *parity = *s++; 2227 if (*s) 2228 *bits = *s++ - '0'; 2229 if (*s) 2230 *flow = *s; 2231 } 2232 EXPORT_SYMBOL_GPL(uart_parse_options); 2233 2234 /** 2235 * uart_set_options - setup the serial console parameters 2236 * @port: pointer to the serial ports uart_port structure 2237 * @co: console pointer 2238 * @baud: baud rate 2239 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) 2240 * @bits: number of data bits 2241 * @flow: flow control character - 'r' (rts) 2242 * 2243 * Locking: Caller must hold console_list_lock in order to serialize 2244 * early initialization of the serial-console lock. 2245 */ 2246 int 2247 uart_set_options(struct uart_port *port, struct console *co, 2248 int baud, int parity, int bits, int flow) 2249 { 2250 struct ktermios termios; 2251 static struct ktermios dummy; 2252 2253 /* 2254 * Ensure that the serial-console lock is initialised early. 2255 * 2256 * Note that the console-registered check is needed because 2257 * kgdboc can call uart_set_options() for an already registered 2258 * console via tty_find_polling_driver() and uart_poll_init(). 2259 */ 2260 if (!uart_console_registered_locked(port) && !port->console_reinit) 2261 uart_port_spin_lock_init(port); 2262 2263 memset(&termios, 0, sizeof(struct ktermios)); 2264 2265 termios.c_cflag |= CREAD | HUPCL | CLOCAL; 2266 tty_termios_encode_baud_rate(&termios, baud, baud); 2267 2268 if (bits == 7) 2269 termios.c_cflag |= CS7; 2270 else 2271 termios.c_cflag |= CS8; 2272 2273 switch (parity) { 2274 case 'o': case 'O': 2275 termios.c_cflag |= PARODD; 2276 fallthrough; 2277 case 'e': case 'E': 2278 termios.c_cflag |= PARENB; 2279 break; 2280 } 2281 2282 if (flow == 'r') 2283 termios.c_cflag |= CRTSCTS; 2284 2285 /* 2286 * some uarts on other side don't support no flow control. 2287 * So we set * DTR in host uart to make them happy 2288 */ 2289 port->mctrl |= TIOCM_DTR; 2290 2291 port->ops->set_termios(port, &termios, &dummy); 2292 2293 /* 2294 * If console hardware flow control was specified and is supported, 2295 * the related policy UPSTAT_CTS_ENABLE must be set to allow console 2296 * drivers to identify if CTS should be used for polling. 2297 */ 2298 if (flow == 'r' && (termios.c_cflag & CRTSCTS)) { 2299 /* Synchronize @status RMW update against the console. */ 2300 guard(uart_port_lock_irqsave)(port); 2301 port->status |= UPSTAT_CTS_ENABLE; 2302 } 2303 2304 /* 2305 * Allow the setting of the UART parameters with a NULL console 2306 * too: 2307 */ 2308 if (co) { 2309 co->cflag = termios.c_cflag; 2310 co->ispeed = termios.c_ispeed; 2311 co->ospeed = termios.c_ospeed; 2312 } 2313 2314 return 0; 2315 } 2316 EXPORT_SYMBOL_GPL(uart_set_options); 2317 #endif /* CONFIG_SERIAL_CORE_CONSOLE */ 2318 2319 /** 2320 * uart_change_pm - set power state of the port 2321 * 2322 * @state: port descriptor 2323 * @pm_state: new state 2324 * 2325 * Locking: port->mutex has to be held 2326 */ 2327 static void uart_change_pm(struct uart_state *state, 2328 enum uart_pm_state pm_state) 2329 { 2330 struct uart_port *port = uart_port_check(state); 2331 2332 if (state->pm_state != pm_state) { 2333 if (port && port->ops->pm) 2334 port->ops->pm(port, pm_state, state->pm_state); 2335 state->pm_state = pm_state; 2336 } 2337 } 2338 2339 struct uart_match { 2340 struct uart_port *port; 2341 struct uart_driver *driver; 2342 }; 2343 2344 static int serial_match_port(struct device *dev, const void *data) 2345 { 2346 const struct uart_match *match = data; 2347 struct tty_driver *tty_drv = match->driver->tty_driver; 2348 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + 2349 match->port->line; 2350 2351 return dev->devt == devt; /* Actually, only one tty per port */ 2352 } 2353 2354 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) 2355 { 2356 struct uart_state *state = drv->state + uport->line; 2357 struct tty_port *port = &state->port; 2358 struct device *tty_dev; 2359 struct uart_match match = {uport, drv}; 2360 2361 guard(mutex)(&port->mutex); 2362 2363 tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port); 2364 if (tty_dev && device_may_wakeup(tty_dev)) { 2365 enable_irq_wake(uport->irq); 2366 put_device(tty_dev); 2367 return 0; 2368 } 2369 put_device(tty_dev); 2370 2371 /* 2372 * Nothing to do if the console is not suspending 2373 * except stop_rx to prevent any asynchronous data 2374 * over RX line. However ensure that we will be 2375 * able to Re-start_rx later. 2376 */ 2377 if (!console_suspend_enabled && uart_console(uport)) { 2378 if (uport->ops->start_rx) { 2379 guard(uart_port_lock_irq)(uport); 2380 uport->ops->stop_rx(uport); 2381 } 2382 device_set_awake_path(uport->dev); 2383 return 0; 2384 } 2385 2386 uport->suspended = 1; 2387 2388 if (tty_port_initialized(port)) { 2389 const struct uart_ops *ops = uport->ops; 2390 int tries; 2391 unsigned int mctrl; 2392 2393 tty_port_set_suspended(port, true); 2394 tty_port_set_initialized(port, false); 2395 2396 scoped_guard(uart_port_lock_irq, uport) { 2397 ops->stop_tx(uport); 2398 if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2399 ops->set_mctrl(uport, 0); 2400 /* save mctrl so it can be restored on resume */ 2401 mctrl = uport->mctrl; 2402 uport->mctrl = 0; 2403 ops->stop_rx(uport); 2404 } 2405 2406 /* 2407 * Wait for the transmitter to empty. 2408 */ 2409 for (tries = 3; !ops->tx_empty(uport) && tries; tries--) 2410 msleep(10); 2411 if (!tries) 2412 dev_err(uport->dev, "%s: Unable to drain transmitter\n", 2413 uport->name); 2414 2415 ops->shutdown(uport); 2416 uport->mctrl = mctrl; 2417 } 2418 2419 /* 2420 * Suspend the console device before suspending the port. 2421 */ 2422 if (uart_console(uport)) 2423 console_suspend(uport->cons); 2424 2425 uart_change_pm(state, UART_PM_STATE_OFF); 2426 2427 return 0; 2428 } 2429 EXPORT_SYMBOL(uart_suspend_port); 2430 2431 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) 2432 { 2433 struct uart_state *state = drv->state + uport->line; 2434 struct tty_port *port = &state->port; 2435 struct device *tty_dev; 2436 struct uart_match match = {uport, drv}; 2437 struct ktermios termios; 2438 2439 guard(mutex)(&port->mutex); 2440 2441 tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port); 2442 if (!uport->suspended && device_may_wakeup(tty_dev)) { 2443 if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq)))) 2444 disable_irq_wake(uport->irq); 2445 put_device(tty_dev); 2446 return 0; 2447 } 2448 put_device(tty_dev); 2449 uport->suspended = 0; 2450 2451 /* 2452 * Re-enable the console device after suspending. 2453 */ 2454 if (uart_console(uport)) { 2455 /* 2456 * First try to use the console cflag setting. 2457 */ 2458 memset(&termios, 0, sizeof(struct ktermios)); 2459 termios.c_cflag = uport->cons->cflag; 2460 termios.c_ispeed = uport->cons->ispeed; 2461 termios.c_ospeed = uport->cons->ospeed; 2462 2463 /* 2464 * If that's unset, use the tty termios setting. 2465 */ 2466 if (port->tty && termios.c_cflag == 0) 2467 termios = port->tty->termios; 2468 2469 if (console_suspend_enabled) 2470 uart_change_pm(state, UART_PM_STATE_ON); 2471 uport->ops->set_termios(uport, &termios, NULL); 2472 if (!console_suspend_enabled && uport->ops->start_rx) { 2473 guard(uart_port_lock_irq)(uport); 2474 uport->ops->start_rx(uport); 2475 } 2476 if (console_suspend_enabled) 2477 console_resume(uport->cons); 2478 } 2479 2480 if (tty_port_suspended(port)) { 2481 const struct uart_ops *ops = uport->ops; 2482 int ret; 2483 2484 uart_change_pm(state, UART_PM_STATE_ON); 2485 scoped_guard(uart_port_lock_irq, uport) 2486 if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2487 ops->set_mctrl(uport, 0); 2488 if (console_suspend_enabled || !uart_console(uport)) { 2489 /* Protected by port mutex for now */ 2490 struct tty_struct *tty = port->tty; 2491 2492 ret = ops->startup(uport); 2493 if (ret == 0) { 2494 if (tty) 2495 uart_change_line_settings(tty, state, NULL); 2496 uart_rs485_config(uport); 2497 scoped_guard(uart_port_lock_irq, uport) { 2498 if (!(uport->rs485.flags & SER_RS485_ENABLED)) 2499 ops->set_mctrl(uport, uport->mctrl); 2500 ops->start_tx(uport); 2501 } 2502 tty_port_set_initialized(port, true); 2503 } else { 2504 /* 2505 * Failed to resume - maybe hardware went away? 2506 * Clear the "initialized" flag so we won't try 2507 * to call the low level drivers shutdown method. 2508 */ 2509 uart_shutdown(tty, state); 2510 } 2511 } 2512 2513 tty_port_set_suspended(port, false); 2514 } 2515 2516 return 0; 2517 } 2518 EXPORT_SYMBOL(uart_resume_port); 2519 2520 static const char *uart_get_mmio_width(struct uart_port *port) 2521 { 2522 switch (port->iotype) { 2523 case UPIO_MEM16: 2524 return "16"; 2525 case UPIO_MEM32: 2526 case UPIO_MEM32BE: 2527 return "32"; 2528 case UPIO_AU: 2529 case UPIO_MEM: 2530 default: 2531 return ""; 2532 } 2533 } 2534 2535 void uart_get_ioinfos(struct uart_port *port, char *buf, size_t size) 2536 { 2537 buf[0] = '\0'; 2538 2539 if (uart_iotype_mmio(port->iotype)) { 2540 scnprintf(buf, size, " MMIO%s:%pa", uart_get_mmio_width(port), &port->mapbase); 2541 } else if (uart_iotype_io(port->iotype)) { 2542 if (port->iotype == UPIO_PORT) 2543 scnprintf(buf, size, " I/O:0x%lx", port->iobase); 2544 else if (port->iotype == UPIO_HUB6) 2545 scnprintf(buf, size, " I/O:0x%lx, offset 0x%x", port->iobase, port->hub6); 2546 } 2547 } 2548 EXPORT_SYMBOL(uart_get_ioinfos); 2549 2550 static inline void 2551 uart_report_port(struct uart_driver *drv, struct uart_port *port) 2552 { 2553 char ioinfos[64]; 2554 2555 uart_get_ioinfos(port, ioinfos, sizeof(ioinfos)); 2556 2557 pr_info("%s%s%s%s (irq = %u, base_baud = %u) is a %s\n", 2558 port->dev ? dev_name(port->dev) : "", 2559 port->dev ? ": " : "", 2560 port->name, ioinfos, port->irq, port->uartclk / 16, uart_type(port)); 2561 2562 /* The magic multiplier feature is a bit obscure, so report it too. */ 2563 if (port->flags & UPF_MAGIC_MULTIPLIER) 2564 pr_info("%s%s%s extra baud rates supported: %u, %u", 2565 port->dev ? dev_name(port->dev) : "", 2566 port->dev ? ": " : "", 2567 port->name, 2568 port->uartclk / 8, port->uartclk / 4); 2569 } 2570 2571 static void 2572 uart_configure_port(struct uart_driver *drv, struct uart_state *state, 2573 struct uart_port *port) 2574 { 2575 unsigned int flags; 2576 2577 /* If there isn't a port here, don't do anything further. */ 2578 if (uart_iotype_mmio(port->iotype) || uart_iotype_io(port->iotype)) 2579 if (!port->iobase && !port->mapbase && !port->membase) 2580 return; 2581 2582 /* 2583 * Now do the auto configuration stuff. Note that config_port 2584 * is expected to claim the resources and map the port for us. 2585 */ 2586 flags = 0; 2587 if (port->flags & UPF_AUTO_IRQ) 2588 flags |= UART_CONFIG_IRQ; 2589 if (port->flags & UPF_BOOT_AUTOCONF) { 2590 if (!(port->flags & UPF_FIXED_TYPE)) { 2591 port->type = PORT_UNKNOWN; 2592 flags |= UART_CONFIG_TYPE; 2593 } 2594 /* Synchronize with possible boot console. */ 2595 if (uart_console(port)) 2596 console_lock(); 2597 port->ops->config_port(port, flags); 2598 if (uart_console(port)) 2599 console_unlock(); 2600 } 2601 2602 if (port->type != PORT_UNKNOWN) { 2603 uart_report_port(drv, port); 2604 2605 /* Synchronize with possible boot console. */ 2606 if (uart_console(port)) 2607 console_lock(); 2608 2609 /* Power up port for set_mctrl() */ 2610 uart_change_pm(state, UART_PM_STATE_ON); 2611 2612 /* 2613 * Ensure that the modem control lines are de-activated. 2614 * keep the DTR setting that is set in uart_set_options() 2615 * We probably don't need a spinlock around this, but 2616 */ 2617 scoped_guard(uart_port_lock_irqsave, port) { 2618 unsigned int mask = TIOCM_DTR; 2619 2620 /* Console hardware flow control polls CTS. */ 2621 if (uart_console_hwflow_active(port)) 2622 mask |= TIOCM_RTS; 2623 2624 port->mctrl &= mask; 2625 2626 if (!(port->rs485.flags & SER_RS485_ENABLED)) 2627 port->ops->set_mctrl(port, port->mctrl); 2628 } 2629 2630 uart_rs485_config(port); 2631 2632 if (uart_console(port)) 2633 console_unlock(); 2634 2635 /* 2636 * If this driver supports console, and it hasn't been 2637 * successfully registered yet, try to re-register it. 2638 * It may be that the port was not available. 2639 */ 2640 if (port->cons && !console_is_registered(port->cons)) 2641 register_console(port->cons); 2642 2643 /* 2644 * Power down all ports by default, except the 2645 * console if we have one. 2646 */ 2647 if (!uart_console(port)) 2648 uart_change_pm(state, UART_PM_STATE_OFF); 2649 } 2650 } 2651 2652 #ifdef CONFIG_CONSOLE_POLL 2653 2654 static int uart_poll_init(struct tty_driver *driver, int line, char *options) 2655 { 2656 struct uart_driver *drv = driver->driver_state; 2657 struct uart_state *state = drv->state + line; 2658 enum uart_pm_state pm_state; 2659 struct tty_port *tport; 2660 struct uart_port *port; 2661 int baud = 9600; 2662 int bits = 8; 2663 int parity = 'n'; 2664 int flow = 'n'; 2665 int ret = 0; 2666 2667 tport = &state->port; 2668 2669 guard(mutex)(&tport->mutex); 2670 2671 port = uart_port_check(state); 2672 if (!port || port->type == PORT_UNKNOWN || 2673 !(port->ops->poll_get_char && port->ops->poll_put_char)) 2674 return -1; 2675 2676 pm_state = state->pm_state; 2677 uart_change_pm(state, UART_PM_STATE_ON); 2678 2679 if (port->ops->poll_init) { 2680 /* 2681 * We don't set initialized as we only initialized the hw, 2682 * e.g. state->xmit is still uninitialized. 2683 */ 2684 if (!tty_port_initialized(tport)) 2685 ret = port->ops->poll_init(port); 2686 } 2687 2688 if (!ret && options) { 2689 uart_parse_options(options, &baud, &parity, &bits, &flow); 2690 console_list_lock(); 2691 ret = uart_set_options(port, NULL, baud, parity, bits, flow); 2692 console_list_unlock(); 2693 } 2694 2695 if (ret) 2696 uart_change_pm(state, pm_state); 2697 2698 return ret; 2699 } 2700 2701 static int uart_poll_get_char(struct tty_driver *driver, int line) 2702 { 2703 struct uart_driver *drv = driver->driver_state; 2704 struct uart_state *state = drv->state + line; 2705 struct uart_port *port; 2706 int ret = -1; 2707 2708 port = uart_port_ref(state); 2709 if (port) { 2710 ret = port->ops->poll_get_char(port); 2711 uart_port_deref(port); 2712 } 2713 2714 return ret; 2715 } 2716 2717 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) 2718 { 2719 struct uart_driver *drv = driver->driver_state; 2720 struct uart_state *state = drv->state + line; 2721 struct uart_port *port; 2722 2723 port = uart_port_ref(state); 2724 if (!port) 2725 return; 2726 2727 if (ch == '\n') 2728 port->ops->poll_put_char(port, '\r'); 2729 port->ops->poll_put_char(port, ch); 2730 uart_port_deref(port); 2731 } 2732 #endif 2733 2734 static const struct tty_operations uart_ops = { 2735 .install = uart_install, 2736 .open = uart_open, 2737 .close = uart_close, 2738 .write = uart_write, 2739 .put_char = uart_put_char, 2740 .flush_chars = uart_flush_chars, 2741 .write_room = uart_write_room, 2742 .chars_in_buffer= uart_chars_in_buffer, 2743 .flush_buffer = uart_flush_buffer, 2744 .ioctl = uart_ioctl, 2745 .throttle = uart_throttle, 2746 .unthrottle = uart_unthrottle, 2747 .send_xchar = uart_send_xchar, 2748 .set_termios = uart_set_termios, 2749 .set_ldisc = uart_set_ldisc, 2750 .stop = uart_stop, 2751 .start = uart_start, 2752 .hangup = uart_hangup, 2753 .break_ctl = uart_break_ctl, 2754 .wait_until_sent= uart_wait_until_sent, 2755 #ifdef CONFIG_PROC_FS 2756 .proc_show = uart_proc_show, 2757 #endif 2758 .tiocmget = uart_tiocmget, 2759 .tiocmset = uart_tiocmset, 2760 .set_serial = uart_set_info_user, 2761 .get_serial = uart_get_info_user, 2762 .get_icount = uart_get_icount, 2763 #ifdef CONFIG_CONSOLE_POLL 2764 .poll_init = uart_poll_init, 2765 .poll_get_char = uart_poll_get_char, 2766 .poll_put_char = uart_poll_put_char, 2767 #endif 2768 }; 2769 2770 static const struct tty_port_operations uart_port_ops = { 2771 .carrier_raised = uart_carrier_raised, 2772 .dtr_rts = uart_dtr_rts, 2773 .activate = uart_port_activate, 2774 .shutdown = uart_tty_port_shutdown, 2775 }; 2776 2777 /** 2778 * uart_register_driver - register a driver with the uart core layer 2779 * @drv: low level driver structure 2780 * 2781 * Register a uart driver with the core driver. We in turn register with the 2782 * tty layer, and initialise the core driver per-port state. 2783 * 2784 * We have a proc file in /proc/tty/driver which is named after the normal 2785 * driver. 2786 * 2787 * @drv->port should be %NULL, and the per-port structures should be registered 2788 * using uart_add_one_port() after this call has succeeded. 2789 * 2790 * Locking: none, Interrupts: enabled 2791 */ 2792 int uart_register_driver(struct uart_driver *drv) 2793 { 2794 struct tty_driver *normal; 2795 int i, retval = -ENOMEM; 2796 2797 BUG_ON(drv->state); 2798 2799 /* 2800 * Maybe we should be using a slab cache for this, especially if 2801 * we have a large number of ports to handle. 2802 */ 2803 drv->state = kzalloc_objs(struct uart_state, drv->nr); 2804 if (!drv->state) 2805 goto out; 2806 2807 normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW | 2808 TTY_DRIVER_DYNAMIC_DEV); 2809 if (IS_ERR(normal)) { 2810 retval = PTR_ERR(normal); 2811 goto out_kfree; 2812 } 2813 2814 drv->tty_driver = normal; 2815 2816 normal->driver_name = drv->driver_name; 2817 normal->name = drv->dev_name; 2818 normal->major = drv->major; 2819 normal->minor_start = drv->minor; 2820 normal->type = TTY_DRIVER_TYPE_SERIAL; 2821 normal->subtype = SERIAL_TYPE_NORMAL; 2822 normal->init_termios = tty_std_termios; 2823 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 2824 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600; 2825 normal->driver_state = drv; 2826 tty_set_operations(normal, &uart_ops); 2827 2828 /* 2829 * Initialise the UART state(s). 2830 */ 2831 for (i = 0; i < drv->nr; i++) { 2832 struct uart_state *state = drv->state + i; 2833 struct tty_port *port = &state->port; 2834 2835 tty_port_init(port); 2836 port->ops = &uart_port_ops; 2837 } 2838 2839 retval = tty_register_driver(normal); 2840 if (retval >= 0) 2841 return retval; 2842 2843 for (i = 0; i < drv->nr; i++) 2844 tty_port_destroy(&drv->state[i].port); 2845 tty_driver_kref_put(normal); 2846 drv->tty_driver = NULL; 2847 out_kfree: 2848 kfree(drv->state); 2849 drv->state = NULL; 2850 out: 2851 return retval; 2852 } 2853 EXPORT_SYMBOL(uart_register_driver); 2854 2855 /** 2856 * uart_unregister_driver - remove a driver from the uart core layer 2857 * @drv: low level driver structure 2858 * 2859 * Remove all references to a driver from the core driver. The low level 2860 * driver must have removed all its ports via the uart_remove_one_port() if it 2861 * registered them with uart_add_one_port(). (I.e. @drv->port is %NULL.) 2862 * 2863 * Locking: none, Interrupts: enabled 2864 */ 2865 void uart_unregister_driver(struct uart_driver *drv) 2866 { 2867 struct tty_driver *p = drv->tty_driver; 2868 unsigned int i; 2869 2870 tty_unregister_driver(p); 2871 tty_driver_kref_put(p); 2872 for (i = 0; i < drv->nr; i++) 2873 tty_port_destroy(&drv->state[i].port); 2874 kfree(drv->state); 2875 drv->state = NULL; 2876 drv->tty_driver = NULL; 2877 } 2878 EXPORT_SYMBOL(uart_unregister_driver); 2879 2880 struct tty_driver *uart_console_device(struct console *co, int *index) 2881 { 2882 struct uart_driver *p = co->data; 2883 *index = co->index; 2884 return p->tty_driver; 2885 } 2886 EXPORT_SYMBOL_GPL(uart_console_device); 2887 2888 static ssize_t uartclk_show(struct device *dev, 2889 struct device_attribute *attr, char *buf) 2890 { 2891 struct serial_struct tmp; 2892 struct tty_port *port = dev_get_drvdata(dev); 2893 2894 uart_get_info(port, &tmp); 2895 return sprintf(buf, "%d\n", tmp.baud_base * 16); 2896 } 2897 2898 static ssize_t type_show(struct device *dev, 2899 struct device_attribute *attr, char *buf) 2900 { 2901 struct serial_struct tmp; 2902 struct tty_port *port = dev_get_drvdata(dev); 2903 2904 uart_get_info(port, &tmp); 2905 return sprintf(buf, "%d\n", tmp.type); 2906 } 2907 2908 static ssize_t line_show(struct device *dev, 2909 struct device_attribute *attr, char *buf) 2910 { 2911 struct serial_struct tmp; 2912 struct tty_port *port = dev_get_drvdata(dev); 2913 2914 uart_get_info(port, &tmp); 2915 return sprintf(buf, "%d\n", tmp.line); 2916 } 2917 2918 static ssize_t port_show(struct device *dev, 2919 struct device_attribute *attr, char *buf) 2920 { 2921 struct serial_struct tmp; 2922 struct tty_port *port = dev_get_drvdata(dev); 2923 unsigned long ioaddr; 2924 2925 uart_get_info(port, &tmp); 2926 ioaddr = tmp.port; 2927 if (HIGH_BITS_OFFSET) 2928 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET; 2929 return sprintf(buf, "0x%lX\n", ioaddr); 2930 } 2931 2932 static ssize_t irq_show(struct device *dev, 2933 struct device_attribute *attr, char *buf) 2934 { 2935 struct serial_struct tmp; 2936 struct tty_port *port = dev_get_drvdata(dev); 2937 2938 uart_get_info(port, &tmp); 2939 return sprintf(buf, "%d\n", tmp.irq); 2940 } 2941 2942 static ssize_t flags_show(struct device *dev, 2943 struct device_attribute *attr, char *buf) 2944 { 2945 struct serial_struct tmp; 2946 struct tty_port *port = dev_get_drvdata(dev); 2947 2948 uart_get_info(port, &tmp); 2949 return sprintf(buf, "0x%X\n", tmp.flags); 2950 } 2951 2952 static ssize_t xmit_fifo_size_show(struct device *dev, 2953 struct device_attribute *attr, char *buf) 2954 { 2955 struct serial_struct tmp; 2956 struct tty_port *port = dev_get_drvdata(dev); 2957 2958 uart_get_info(port, &tmp); 2959 return sprintf(buf, "%d\n", tmp.xmit_fifo_size); 2960 } 2961 2962 static ssize_t close_delay_show(struct device *dev, 2963 struct device_attribute *attr, char *buf) 2964 { 2965 struct serial_struct tmp; 2966 struct tty_port *port = dev_get_drvdata(dev); 2967 2968 uart_get_info(port, &tmp); 2969 return sprintf(buf, "%u\n", tmp.close_delay); 2970 } 2971 2972 static ssize_t closing_wait_show(struct device *dev, 2973 struct device_attribute *attr, char *buf) 2974 { 2975 struct serial_struct tmp; 2976 struct tty_port *port = dev_get_drvdata(dev); 2977 2978 uart_get_info(port, &tmp); 2979 return sprintf(buf, "%u\n", tmp.closing_wait); 2980 } 2981 2982 static ssize_t custom_divisor_show(struct device *dev, 2983 struct device_attribute *attr, char *buf) 2984 { 2985 struct serial_struct tmp; 2986 struct tty_port *port = dev_get_drvdata(dev); 2987 2988 uart_get_info(port, &tmp); 2989 return sprintf(buf, "%d\n", tmp.custom_divisor); 2990 } 2991 2992 static ssize_t io_type_show(struct device *dev, 2993 struct device_attribute *attr, char *buf) 2994 { 2995 struct serial_struct tmp; 2996 struct tty_port *port = dev_get_drvdata(dev); 2997 2998 uart_get_info(port, &tmp); 2999 return sprintf(buf, "%u\n", tmp.io_type); 3000 } 3001 3002 static ssize_t iomem_base_show(struct device *dev, 3003 struct device_attribute *attr, char *buf) 3004 { 3005 struct serial_struct tmp; 3006 struct tty_port *port = dev_get_drvdata(dev); 3007 3008 uart_get_info(port, &tmp); 3009 return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base); 3010 } 3011 3012 static ssize_t iomem_reg_shift_show(struct device *dev, 3013 struct device_attribute *attr, char *buf) 3014 { 3015 struct serial_struct tmp; 3016 struct tty_port *port = dev_get_drvdata(dev); 3017 3018 uart_get_info(port, &tmp); 3019 return sprintf(buf, "%u\n", tmp.iomem_reg_shift); 3020 } 3021 3022 static ssize_t console_show(struct device *dev, 3023 struct device_attribute *attr, char *buf) 3024 { 3025 struct tty_port *port = dev_get_drvdata(dev); 3026 struct uart_state *state = container_of(port, struct uart_state, port); 3027 struct uart_port *uport; 3028 bool console = false; 3029 3030 scoped_guard(mutex, &port->mutex) { 3031 uport = uart_port_check(state); 3032 if (uport) 3033 console = uart_console_registered(uport); 3034 } 3035 3036 return sprintf(buf, "%c\n", console ? 'Y' : 'N'); 3037 } 3038 3039 static ssize_t console_store(struct device *dev, 3040 struct device_attribute *attr, const char *buf, size_t count) 3041 { 3042 struct tty_port *port = dev_get_drvdata(dev); 3043 struct uart_state *state = container_of(port, struct uart_state, port); 3044 struct uart_port *uport; 3045 bool oldconsole, newconsole; 3046 int ret; 3047 3048 ret = kstrtobool(buf, &newconsole); 3049 if (ret) 3050 return ret; 3051 3052 guard(mutex)(&port->mutex); 3053 uport = uart_port_check(state); 3054 if (!uport) 3055 return -ENXIO; 3056 3057 oldconsole = uart_console_registered(uport); 3058 if (oldconsole && !newconsole) { 3059 ret = unregister_console(uport->cons); 3060 if (ret < 0) 3061 return ret; 3062 } else if (!oldconsole && newconsole) { 3063 if (!uart_console(uport)) 3064 return -ENOENT; 3065 3066 uport->console_reinit = 1; 3067 register_console(uport->cons); 3068 } 3069 3070 return count; 3071 } 3072 3073 static DEVICE_ATTR_RO(uartclk); 3074 static DEVICE_ATTR_RO(type); 3075 static DEVICE_ATTR_RO(line); 3076 static DEVICE_ATTR_RO(port); 3077 static DEVICE_ATTR_RO(irq); 3078 static DEVICE_ATTR_RO(flags); 3079 static DEVICE_ATTR_RO(xmit_fifo_size); 3080 static DEVICE_ATTR_RO(close_delay); 3081 static DEVICE_ATTR_RO(closing_wait); 3082 static DEVICE_ATTR_RO(custom_divisor); 3083 static DEVICE_ATTR_RO(io_type); 3084 static DEVICE_ATTR_RO(iomem_base); 3085 static DEVICE_ATTR_RO(iomem_reg_shift); 3086 static DEVICE_ATTR_RW(console); 3087 3088 static struct attribute *tty_dev_attrs[] = { 3089 &dev_attr_uartclk.attr, 3090 &dev_attr_type.attr, 3091 &dev_attr_line.attr, 3092 &dev_attr_port.attr, 3093 &dev_attr_irq.attr, 3094 &dev_attr_flags.attr, 3095 &dev_attr_xmit_fifo_size.attr, 3096 &dev_attr_close_delay.attr, 3097 &dev_attr_closing_wait.attr, 3098 &dev_attr_custom_divisor.attr, 3099 &dev_attr_io_type.attr, 3100 &dev_attr_iomem_base.attr, 3101 &dev_attr_iomem_reg_shift.attr, 3102 &dev_attr_console.attr, 3103 NULL 3104 }; 3105 3106 static const struct attribute_group tty_dev_attr_group = { 3107 .attrs = tty_dev_attrs, 3108 }; 3109 3110 /** 3111 * serial_core_add_one_port - attach a driver-defined port structure 3112 * @drv: pointer to the uart low level driver structure for this port 3113 * @uport: uart port structure to use for this port. 3114 * 3115 * Context: task context, might sleep 3116 * 3117 * This allows the driver @drv to register its own uart_port structure with the 3118 * core driver. The main purpose is to allow the low level uart drivers to 3119 * expand uart_port, rather than having yet more levels of structures. 3120 * Caller must hold port_mutex. 3121 */ 3122 static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *uport) 3123 { 3124 struct uart_state *state; 3125 struct tty_port *port; 3126 struct device *tty_dev; 3127 3128 if (uport->line >= drv->nr) 3129 return -EINVAL; 3130 3131 state = drv->state + uport->line; 3132 port = &state->port; 3133 3134 guard(mutex)(&port->mutex); 3135 if (state->uart_port) 3136 return -EINVAL; 3137 3138 uport->name = kasprintf(GFP_KERNEL, "%s%u", drv->dev_name, 3139 drv->tty_driver->name_base + uport->line); 3140 if (!uport->name) 3141 return -ENOMEM; 3142 3143 /* 3144 * uart_configure_port() may set uport->attr_group and register the 3145 * console. Allocate room for both groups and a NULL terminator first. 3146 */ 3147 uport->tty_groups = kzalloc_objs(*uport->tty_groups, 3); 3148 if (!uport->tty_groups) { 3149 kfree(uport->name); 3150 return -ENOMEM; 3151 } 3152 uport->tty_groups[0] = &tty_dev_attr_group; 3153 3154 /* Link the port to the driver state table and vice versa */ 3155 atomic_set(&state->refcount, 1); 3156 init_waitqueue_head(&state->remove_wait); 3157 state->uart_port = uport; 3158 uport->state = state; 3159 3160 /* 3161 * If this port is in use as a console then the spinlock is already 3162 * initialised. 3163 */ 3164 if (!uart_console_registered(uport)) 3165 uart_port_spin_lock_init(uport); 3166 3167 state->pm_state = UART_PM_STATE_UNDEFINED; 3168 uart_port_set_cons(uport, drv->cons); 3169 uport->minor = drv->tty_driver->minor_start + uport->line; 3170 3171 if (uport->cons && uport->dev) 3172 of_console_check(uport->dev->of_node, uport->cons->name, uport->line); 3173 3174 /* 3175 * TTY port has to be linked with the driver before register_console() 3176 * in uart_configure_port(), because user-space could open the console 3177 * immediately after. 3178 */ 3179 tty_port_link_device(port, drv->tty_driver, uport->line); 3180 uart_configure_port(drv, state, uport); 3181 3182 port->console = uart_console(uport); 3183 3184 if (uport->attr_group) 3185 uport->tty_groups[1] = uport->attr_group; 3186 3187 /* Ensure serdev drivers can call serdev_device_open() right away */ 3188 uport->flags &= ~UPF_DEAD; 3189 3190 /* 3191 * Register the port whether it's detected or not. This allows 3192 * setserial to be used to alter this port's parameters. 3193 */ 3194 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver, 3195 uport->line, uport->dev, &uport->port_dev->dev, port, 3196 uport->tty_groups); 3197 if (!IS_ERR(tty_dev)) { 3198 device_set_wakeup_capable(tty_dev, 1); 3199 } else { 3200 uport->flags |= UPF_DEAD; 3201 dev_err(uport->dev, "Cannot register tty device on line %u\n", 3202 uport->line); 3203 } 3204 3205 return 0; 3206 } 3207 3208 /** 3209 * serial_core_remove_one_port - detach a driver defined port structure 3210 * @drv: pointer to the uart low level driver structure for this port 3211 * @uport: uart port structure for this port 3212 * 3213 * Context: task context, might sleep 3214 * 3215 * This unhooks (and hangs up) the specified port structure from the core 3216 * driver. No further calls will be made to the low-level code for this port. 3217 * Caller must hold port_mutex. 3218 */ 3219 static void serial_core_remove_one_port(struct uart_driver *drv, 3220 struct uart_port *uport) 3221 { 3222 struct uart_state *state = drv->state + uport->line; 3223 struct tty_port *port = &state->port; 3224 struct uart_port *uart_port; 3225 3226 scoped_guard(mutex, &port->mutex) { 3227 uart_port = uart_port_check(state); 3228 if (uart_port != uport) 3229 dev_alert(uport->dev, "Removing wrong port: %p != %p\n", uart_port, uport); 3230 3231 if (!uart_port) 3232 return; 3233 } 3234 3235 /* 3236 * Remove the devices from the tty layer 3237 */ 3238 tty_port_unregister_device(port, drv->tty_driver, uport->line); 3239 3240 tty_port_tty_vhangup(port); 3241 3242 /* 3243 * If the port is used as a console, unregister it 3244 */ 3245 if (uart_console(uport)) 3246 unregister_console(uport->cons); 3247 3248 scoped_guard(mutex, &port->mutex) { 3249 port->console = false; 3250 3251 if (tty_port_initialized(port)) 3252 uart_shutdown(NULL, state); 3253 } 3254 3255 /* 3256 * Free the port IO and memory resources, if any. 3257 */ 3258 if (uport->type != PORT_UNKNOWN && uport->ops->release_port) 3259 uport->ops->release_port(uport); 3260 kfree(uport->tty_groups); 3261 kfree(uport->name); 3262 3263 /* 3264 * Indicate that there isn't a port here anymore. 3265 */ 3266 uport->type = PORT_UNKNOWN; 3267 uport->port_dev = NULL; 3268 3269 guard(mutex)(&port->mutex); 3270 WARN_ON(atomic_dec_return(&state->refcount) < 0); 3271 wait_event(state->remove_wait, !atomic_read(&state->refcount)); 3272 state->uart_port = NULL; 3273 } 3274 3275 static struct serial_ctrl_device * 3276 serial_core_get_ctrl_dev(struct serial_port_device *port_dev) 3277 { 3278 struct device *dev = &port_dev->dev; 3279 3280 return to_serial_base_ctrl_device(dev->parent); 3281 } 3282 3283 /* 3284 * Find a registered serial core controller device if one exists. Returns 3285 * the first device matching the ctrl_id. Caller must hold port_mutex. 3286 */ 3287 static struct serial_ctrl_device *serial_core_ctrl_find(struct uart_driver *drv, 3288 struct device *phys_dev, 3289 int ctrl_id) 3290 { 3291 struct uart_state *state; 3292 int i; 3293 3294 lockdep_assert_held(&port_mutex); 3295 3296 for (i = 0; i < drv->nr; i++) { 3297 state = drv->state + i; 3298 if (!state->uart_port || !state->uart_port->port_dev) 3299 continue; 3300 3301 if (state->uart_port->dev == phys_dev && 3302 state->uart_port->ctrl_id == ctrl_id) 3303 return serial_core_get_ctrl_dev(state->uart_port->port_dev); 3304 } 3305 3306 return NULL; 3307 } 3308 3309 static struct serial_ctrl_device *serial_core_ctrl_device_add(struct uart_port *port) 3310 { 3311 return serial_base_ctrl_add(port, port->dev); 3312 } 3313 3314 static int serial_core_port_device_add(struct serial_ctrl_device *ctrl_dev, 3315 struct uart_port *port) 3316 { 3317 struct serial_port_device *port_dev; 3318 3319 port_dev = serial_base_port_add(port, ctrl_dev); 3320 if (IS_ERR(port_dev)) 3321 return PTR_ERR(port_dev); 3322 3323 port->port_dev = port_dev; 3324 3325 return 0; 3326 } 3327 3328 /* 3329 * Initialize a serial core port device, and a controller device if needed. 3330 */ 3331 int serial_core_register_port(struct uart_driver *drv, struct uart_port *port) 3332 { 3333 struct serial_ctrl_device *ctrl_dev, *new_ctrl_dev = NULL; 3334 int ret; 3335 3336 guard(mutex)(&port_mutex); 3337 3338 /* 3339 * Prevent serial_port_runtime_resume() from trying to use the port 3340 * until serial_core_add_one_port() has completed 3341 */ 3342 port->flags |= UPF_DEAD; 3343 3344 /* Inititalize a serial core controller device if needed */ 3345 ctrl_dev = serial_core_ctrl_find(drv, port->dev, port->ctrl_id); 3346 if (!ctrl_dev) { 3347 new_ctrl_dev = serial_core_ctrl_device_add(port); 3348 if (IS_ERR(new_ctrl_dev)) 3349 return PTR_ERR(new_ctrl_dev); 3350 ctrl_dev = new_ctrl_dev; 3351 } 3352 3353 /* 3354 * Initialize a serial core port device. Tag the port dead to prevent 3355 * serial_port_runtime_resume() trying to do anything until port has 3356 * been registered. It gets cleared by serial_core_add_one_port(). 3357 */ 3358 ret = serial_core_port_device_add(ctrl_dev, port); 3359 if (ret) 3360 goto err_unregister_ctrl_dev; 3361 3362 ret = serial_base_match_and_update_preferred_console(drv, port); 3363 if (ret) 3364 goto err_unregister_port_dev; 3365 3366 ret = serial_core_add_one_port(drv, port); 3367 if (ret) 3368 goto err_unregister_port_dev; 3369 3370 return 0; 3371 3372 err_unregister_port_dev: 3373 serial_base_port_device_remove(port->port_dev); 3374 port->port_dev = NULL; 3375 3376 err_unregister_ctrl_dev: 3377 serial_base_ctrl_device_remove(new_ctrl_dev); 3378 3379 return ret; 3380 } 3381 3382 /* 3383 * Removes a serial core port device, and the related serial core controller 3384 * device if the last instance. 3385 */ 3386 void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port) 3387 { 3388 struct device *phys_dev = port->dev; 3389 struct serial_port_device *port_dev; 3390 struct serial_ctrl_device *ctrl_dev; 3391 int ctrl_id = port->ctrl_id; 3392 3393 guard(mutex)(&port_mutex); 3394 3395 /* 3396 * A NULL port device means there is no registered port device to 3397 * remove: serial_core_remove_one_port() clears port_dev on 3398 * teardown, and it is never set if registration failed before 3399 * serial_core_port_device_add(). 3400 */ 3401 port_dev = port->port_dev; 3402 if (!port_dev) 3403 return; 3404 3405 ctrl_dev = serial_core_get_ctrl_dev(port_dev); 3406 3407 port->flags |= UPF_DEAD; 3408 3409 serial_core_remove_one_port(drv, port); 3410 3411 /* Note that struct uart_port *port is no longer valid at this point */ 3412 serial_base_port_device_remove(port_dev); 3413 3414 /* Drop the serial core controller device if no ports are using it */ 3415 if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id)) 3416 serial_base_ctrl_device_remove(ctrl_dev); 3417 } 3418 3419 /** 3420 * uart_handle_dcd_change - handle a change of carrier detect state 3421 * @uport: uart_port structure for the open port 3422 * @active: new carrier detect status 3423 * 3424 * Caller must hold uport->lock. 3425 */ 3426 void uart_handle_dcd_change(struct uart_port *uport, bool active) 3427 { 3428 struct tty_port *port = &uport->state->port; 3429 struct tty_struct *tty = port->tty; 3430 struct tty_ldisc *ld; 3431 3432 lockdep_assert_held_once(&uport->lock); 3433 3434 if (tty) { 3435 ld = tty_ldisc_ref(tty); 3436 if (ld) { 3437 if (ld->ops->dcd_change) 3438 ld->ops->dcd_change(tty, active); 3439 tty_ldisc_deref(ld); 3440 } 3441 } 3442 3443 uport->icount.dcd++; 3444 3445 if (uart_dcd_enabled(uport)) { 3446 if (active) 3447 wake_up_interruptible(&port->open_wait); 3448 else if (tty) 3449 tty_hangup(tty); 3450 } 3451 } 3452 EXPORT_SYMBOL_GPL(uart_handle_dcd_change); 3453 3454 /** 3455 * uart_handle_cts_change - handle a change of clear-to-send state 3456 * @uport: uart_port structure for the open port 3457 * @active: new clear-to-send status 3458 * 3459 * Caller must hold uport->lock. 3460 */ 3461 void uart_handle_cts_change(struct uart_port *uport, bool active) 3462 { 3463 lockdep_assert_held_once(&uport->lock); 3464 3465 uport->icount.cts++; 3466 3467 if (uart_softcts_mode(uport)) { 3468 if (uport->hw_stopped) { 3469 if (active) { 3470 uport->hw_stopped = false; 3471 uport->ops->start_tx(uport); 3472 uart_write_wakeup(uport); 3473 } 3474 } else { 3475 if (!active) { 3476 uport->hw_stopped = true; 3477 uport->ops->stop_tx(uport); 3478 } 3479 } 3480 3481 } 3482 } 3483 EXPORT_SYMBOL_GPL(uart_handle_cts_change); 3484 3485 /** 3486 * uart_insert_char - push a char to the uart layer 3487 * 3488 * User is responsible to call tty_flip_buffer_push when they are done with 3489 * insertion. 3490 * 3491 * @port: corresponding port 3492 * @status: state of the serial port RX buffer (LSR for 8250) 3493 * @overrun: mask of overrun bits in @status 3494 * @ch: character to push 3495 * @flag: flag for the character (see TTY_NORMAL and friends) 3496 */ 3497 void uart_insert_char(struct uart_port *port, unsigned int status, 3498 unsigned int overrun, u8 ch, u8 flag) 3499 { 3500 struct tty_port *tport = &port->state->port; 3501 3502 if ((status & port->ignore_status_mask & ~overrun) == 0) 3503 if (tty_insert_flip_char(tport, ch, flag) == 0) 3504 ++port->icount.buf_overrun; 3505 3506 /* 3507 * Overrun is special. Since it's reported immediately, 3508 * it doesn't affect the current character. 3509 */ 3510 if (status & ~port->ignore_status_mask & overrun) 3511 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0) 3512 ++port->icount.buf_overrun; 3513 } 3514 EXPORT_SYMBOL_GPL(uart_insert_char); 3515 3516 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL 3517 static const u8 sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE; 3518 3519 static void uart_sysrq_on(struct work_struct *w) 3520 { 3521 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq); 3522 3523 sysrq_toggle_support(1); 3524 pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n", 3525 sysrq_toggle_seq_len, sysrq_toggle_seq); 3526 } 3527 static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on); 3528 3529 /** 3530 * uart_try_toggle_sysrq - Enables SysRq from serial line 3531 * @port: uart_port structure where char(s) after BREAK met 3532 * @ch: new character in the sequence after received BREAK 3533 * 3534 * Enables magic SysRq when the required sequence is met on port 3535 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE). 3536 * 3537 * Returns: %false if @ch is out of enabling sequence and should be 3538 * handled some other way, %true if @ch was consumed. 3539 */ 3540 bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch) 3541 { 3542 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq); 3543 3544 if (!sysrq_toggle_seq_len) 3545 return false; 3546 3547 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX); 3548 if (sysrq_toggle_seq[port->sysrq_seq] != ch) { 3549 port->sysrq_seq = 0; 3550 return false; 3551 } 3552 3553 if (++port->sysrq_seq < sysrq_toggle_seq_len) { 3554 port->sysrq = jiffies + SYSRQ_TIMEOUT; 3555 return true; 3556 } 3557 3558 schedule_work(&sysrq_enable_work); 3559 3560 port->sysrq = 0; 3561 return true; 3562 } 3563 EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq); 3564 #endif 3565 3566 /** 3567 * uart_get_rs485_mode() - retrieve rs485 properties for given uart 3568 * @port: uart device's target port 3569 * 3570 * This function implements the device tree binding described in 3571 * Documentation/devicetree/bindings/serial/rs485.yaml. 3572 */ 3573 int uart_get_rs485_mode(struct uart_port *port) 3574 { 3575 struct serial_rs485 *rs485conf = &port->rs485; 3576 struct device *dev = port->dev; 3577 enum gpiod_flags dflags; 3578 struct gpio_desc *desc; 3579 u32 rs485_delay[2]; 3580 int ret; 3581 3582 if (!(port->rs485_supported.flags & SER_RS485_ENABLED)) 3583 return 0; 3584 3585 /* 3586 * Retrieve properties only if a firmware node exists. If no firmware 3587 * node exists, then don't touch rs485 config and keep initial rs485 3588 * properties set by driver. 3589 */ 3590 if (!dev_fwnode(dev)) 3591 return 0; 3592 3593 ret = device_property_read_u32_array(dev, "rs485-rts-delay", 3594 rs485_delay, 2); 3595 if (!ret) { 3596 rs485conf->delay_rts_before_send = rs485_delay[0]; 3597 rs485conf->delay_rts_after_send = rs485_delay[1]; 3598 } else { 3599 rs485conf->delay_rts_before_send = 0; 3600 rs485conf->delay_rts_after_send = 0; 3601 } 3602 3603 uart_sanitize_serial_rs485_delays(port, rs485conf); 3604 3605 /* 3606 * Clear full-duplex and enabled flags, set RTS polarity to active high 3607 * to get to a defined state with the following properties: 3608 */ 3609 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED | 3610 SER_RS485_TERMINATE_BUS | 3611 SER_RS485_RTS_AFTER_SEND); 3612 rs485conf->flags |= SER_RS485_RTS_ON_SEND; 3613 3614 if (device_property_read_bool(dev, "rs485-rx-during-tx")) 3615 rs485conf->flags |= SER_RS485_RX_DURING_TX; 3616 3617 if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time")) 3618 rs485conf->flags |= SER_RS485_ENABLED; 3619 3620 if (device_property_read_bool(dev, "rs485-rts-active-low")) { 3621 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND; 3622 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 3623 } 3624 3625 /* 3626 * Disabling termination by default is the safe choice: Else if many 3627 * bus participants enable it, no communication is possible at all. 3628 * Works fine for short cables and users may enable for longer cables. 3629 */ 3630 desc = devm_gpiod_get_optional(dev, "rs485-term", GPIOD_OUT_LOW); 3631 if (IS_ERR(desc)) 3632 return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-term-gpios\n"); 3633 port->rs485_term_gpio = desc; 3634 if (port->rs485_term_gpio) 3635 port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS; 3636 3637 dflags = (rs485conf->flags & SER_RS485_RX_DURING_TX) ? 3638 GPIOD_OUT_HIGH : GPIOD_OUT_LOW; 3639 desc = devm_gpiod_get_optional(dev, "rs485-rx-during-tx", dflags); 3640 if (IS_ERR(desc)) 3641 return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-rx-during-tx-gpios\n"); 3642 port->rs485_rx_during_tx_gpio = desc; 3643 if (port->rs485_rx_during_tx_gpio) 3644 port->rs485_supported.flags |= SER_RS485_RX_DURING_TX; 3645 3646 return 0; 3647 } 3648 EXPORT_SYMBOL_GPL(uart_get_rs485_mode); 3649 3650 /* Compile-time assertions for serial_rs485 layout */ 3651 static_assert(offsetof(struct serial_rs485, padding) == 3652 (offsetof(struct serial_rs485, delay_rts_after_send) + sizeof(__u32))); 3653 static_assert(offsetof(struct serial_rs485, padding1) == 3654 offsetof(struct serial_rs485, padding[1])); 3655 static_assert((offsetof(struct serial_rs485, padding[4]) + sizeof(__u32)) == 3656 sizeof(struct serial_rs485)); 3657 3658 MODULE_DESCRIPTION("Serial driver core"); 3659 MODULE_LICENSE("GPL"); 3660