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