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