1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * linux/drivers/char/serial_core.h 4 * 5 * Copyright (C) 2000 Deep Blue Solutions Ltd. 6 */ 7 #ifndef LINUX_SERIAL_CORE_H 8 #define LINUX_SERIAL_CORE_H 9 10 #include <linux/bitops.h> 11 #include <linux/compiler.h> 12 #include <linux/console.h> 13 #include <linux/interrupt.h> 14 #include <linux/circ_buf.h> 15 #include <linux/spinlock.h> 16 #include <linux/sched.h> 17 #include <linux/tty.h> 18 #include <linux/mutex.h> 19 #include <linux/sysrq.h> 20 #include <uapi/linux/serial_core.h> 21 22 #ifdef CONFIG_SERIAL_CORE_CONSOLE 23 #define uart_console(port) \ 24 ((port)->cons && (port)->cons->index == (port)->line) 25 #else 26 #define uart_console(port) ({ (void)port; 0; }) 27 #endif 28 29 struct uart_port; 30 struct serial_struct; 31 struct device; 32 struct gpio_desc; 33 34 /** 35 * struct uart_ops -- interface between serial_core and the driver 36 * 37 * This structure describes all the operations that can be done on the 38 * physical hardware. 39 * 40 * @tx_empty: ``unsigned int ()(struct uart_port *port)`` 41 * 42 * This function tests whether the transmitter fifo and shifter for the 43 * @port is empty. If it is empty, this function should return 44 * %TIOCSER_TEMT, otherwise return 0. If the port does not support this 45 * operation, then it should return %TIOCSER_TEMT. 46 * 47 * Locking: none. 48 * Interrupts: caller dependent. 49 * This call must not sleep 50 * 51 * @set_mctrl: ``void ()(struct uart_port *port, unsigned int mctrl)`` 52 * 53 * This function sets the modem control lines for @port to the state 54 * described by @mctrl. The relevant bits of @mctrl are: 55 * 56 * - %TIOCM_RTS RTS signal. 57 * - %TIOCM_DTR DTR signal. 58 * - %TIOCM_OUT1 OUT1 signal. 59 * - %TIOCM_OUT2 OUT2 signal. 60 * - %TIOCM_LOOP Set the port into loopback mode. 61 * 62 * If the appropriate bit is set, the signal should be driven 63 * active. If the bit is clear, the signal should be driven 64 * inactive. 65 * 66 * Locking: @port->lock taken. 67 * Interrupts: locally disabled. 68 * This call must not sleep 69 * 70 * @get_mctrl: ``unsigned int ()(struct uart_port *port)`` 71 * 72 * Returns the current state of modem control inputs of @port. The state 73 * of the outputs should not be returned, since the core keeps track of 74 * their state. The state information should include: 75 * 76 * - %TIOCM_CAR state of DCD signal 77 * - %TIOCM_CTS state of CTS signal 78 * - %TIOCM_DSR state of DSR signal 79 * - %TIOCM_RI state of RI signal 80 * 81 * The bit is set if the signal is currently driven active. If 82 * the port does not support CTS, DCD or DSR, the driver should 83 * indicate that the signal is permanently active. If RI is 84 * not available, the signal should not be indicated as active. 85 * 86 * Locking: @port->lock taken. 87 * Interrupts: locally disabled. 88 * This call must not sleep 89 * 90 * @stop_tx: ``void ()(struct uart_port *port)`` 91 * 92 * Stop transmitting characters. This might be due to the CTS line 93 * becoming inactive or the tty layer indicating we want to stop 94 * transmission due to an %XOFF character. 95 * 96 * The driver should stop transmitting characters as soon as possible. 97 * 98 * Locking: @port->lock taken. 99 * Interrupts: locally disabled. 100 * This call must not sleep 101 * 102 * @start_tx: ``void ()(struct uart_port *port)`` 103 * 104 * Start transmitting characters. 105 * 106 * Locking: @port->lock taken. 107 * Interrupts: locally disabled. 108 * This call must not sleep 109 * 110 * @throttle: ``void ()(struct uart_port *port)`` 111 * 112 * Notify the serial driver that input buffers for the line discipline are 113 * close to full, and it should somehow signal that no more characters 114 * should be sent to the serial port. 115 * This will be called only if hardware assisted flow control is enabled. 116 * 117 * Locking: serialized with @unthrottle() and termios modification by the 118 * tty layer. 119 * 120 * @unthrottle: ``void ()(struct uart_port *port)`` 121 * 122 * Notify the serial driver that characters can now be sent to the serial 123 * port without fear of overrunning the input buffers of the line 124 * disciplines. 125 * 126 * This will be called only if hardware assisted flow control is enabled. 127 * 128 * Locking: serialized with @throttle() and termios modification by the 129 * tty layer. 130 * 131 * @send_xchar: ``void ()(struct uart_port *port, char ch)`` 132 * 133 * Transmit a high priority character, even if the port is stopped. This 134 * is used to implement XON/XOFF flow control and tcflow(). If the serial 135 * driver does not implement this function, the tty core will append the 136 * character to the circular buffer and then call start_tx() / stop_tx() 137 * to flush the data out. 138 * 139 * Do not transmit if @ch == '\0' (%__DISABLED_CHAR). 140 * 141 * Locking: none. 142 * Interrupts: caller dependent. 143 * 144 * @stop_rx: ``void ()(struct uart_port *port)`` 145 * 146 * Stop receiving characters; the @port is in the process of being closed. 147 * 148 * Locking: @port->lock taken. 149 * Interrupts: locally disabled. 150 * This call must not sleep 151 * 152 * @enable_ms: ``void ()(struct uart_port *port)`` 153 * 154 * Enable the modem status interrupts. 155 * 156 * This method may be called multiple times. Modem status interrupts 157 * should be disabled when the @shutdown() method is called. 158 * 159 * Locking: @port->lock taken. 160 * Interrupts: locally disabled. 161 * This call must not sleep 162 * 163 * @break_ctl: ``void ()(struct uart_port *port, int ctl)`` 164 * 165 * Control the transmission of a break signal. If @ctl is nonzero, the 166 * break signal should be transmitted. The signal should be terminated 167 * when another call is made with a zero @ctl. 168 * 169 * Locking: caller holds tty_port->mutex 170 * 171 * @startup: ``int ()(struct uart_port *port)`` 172 * 173 * Grab any interrupt resources and initialise any low level driver state. 174 * Enable the port for reception. It should not activate RTS nor DTR; 175 * this will be done via a separate call to @set_mctrl(). 176 * 177 * This method will only be called when the port is initially opened. 178 * 179 * Locking: port_sem taken. 180 * Interrupts: globally disabled. 181 * 182 * @shutdown: ``void ()(struct uart_port *port)`` 183 * 184 * Disable the @port, disable any break condition that may be in effect, 185 * and free any interrupt resources. It should not disable RTS nor DTR; 186 * this will have already been done via a separate call to @set_mctrl(). 187 * 188 * Drivers must not access @port->state once this call has completed. 189 * 190 * This method will only be called when there are no more users of this 191 * @port. 192 * 193 * Locking: port_sem taken. 194 * Interrupts: caller dependent. 195 * 196 * @flush_buffer: ``void ()(struct uart_port *port)`` 197 * 198 * Flush any write buffers, reset any DMA state and stop any ongoing DMA 199 * transfers. 200 * 201 * This will be called whenever the @port->state->xmit circular buffer is 202 * cleared. 203 * 204 * Locking: @port->lock taken. 205 * Interrupts: locally disabled. 206 * This call must not sleep 207 * 208 * @set_termios: ``void ()(struct uart_port *port, struct ktermios *new, 209 * struct ktermios *old)`` 210 * 211 * Change the @port parameters, including word length, parity, stop bits. 212 * Update @port->read_status_mask and @port->ignore_status_mask to 213 * indicate the types of events we are interested in receiving. Relevant 214 * ktermios::c_cflag bits are: 215 * 216 * - %CSIZE - word size 217 * - %CSTOPB - 2 stop bits 218 * - %PARENB - parity enable 219 * - %PARODD - odd parity (when %PARENB is in force) 220 * - %ADDRB - address bit (changed through uart_port::rs485_config()). 221 * - %CREAD - enable reception of characters (if not set, still receive 222 * characters from the port, but throw them away). 223 * - %CRTSCTS - if set, enable CTS status change reporting. 224 * - %CLOCAL - if not set, enable modem status change reporting. 225 * 226 * Relevant ktermios::c_iflag bits are: 227 * 228 * - %INPCK - enable frame and parity error events to be passed to the TTY 229 * layer. 230 * - %BRKINT / %PARMRK - both of these enable break events to be passed to 231 * the TTY layer. 232 * - %IGNPAR - ignore parity and framing errors. 233 * - %IGNBRK - ignore break errors. If %IGNPAR is also set, ignore overrun 234 * errors as well. 235 * 236 * The interaction of the ktermios::c_iflag bits is as follows (parity 237 * error given as an example): 238 * 239 * ============ ======= ======= ========================================= 240 * Parity error INPCK IGNPAR 241 * ============ ======= ======= ========================================= 242 * n/a 0 n/a character received, marked as %TTY_NORMAL 243 * None 1 n/a character received, marked as %TTY_NORMAL 244 * Yes 1 0 character received, marked as %TTY_PARITY 245 * Yes 1 1 character discarded 246 * ============ ======= ======= ========================================= 247 * 248 * Other flags may be used (eg, xon/xoff characters) if your hardware 249 * supports hardware "soft" flow control. 250 * 251 * Locking: caller holds tty_port->mutex 252 * Interrupts: caller dependent. 253 * This call must not sleep 254 * 255 * @set_ldisc: ``void ()(struct uart_port *port, struct ktermios *termios)`` 256 * 257 * Notifier for discipline change. See 258 * Documentation/driver-api/tty/tty_ldisc.rst. 259 * 260 * Locking: caller holds tty_port->mutex 261 * 262 * @pm: ``void ()(struct uart_port *port, unsigned int state, 263 * unsigned int oldstate)`` 264 * 265 * Perform any power management related activities on the specified @port. 266 * @state indicates the new state (defined by enum uart_pm_state), 267 * @oldstate indicates the previous state. 268 * 269 * This function should not be used to grab any resources. 270 * 271 * This will be called when the @port is initially opened and finally 272 * closed, except when the @port is also the system console. This will 273 * occur even if %CONFIG_PM is not set. 274 * 275 * Locking: none. 276 * Interrupts: caller dependent. 277 * 278 * @type: ``const char *()(struct uart_port *port)`` 279 * 280 * Return a pointer to a string constant describing the specified @port, 281 * or return %NULL, in which case the string 'unknown' is substituted. 282 * 283 * Locking: none. 284 * Interrupts: caller dependent. 285 * 286 * @release_port: ``void ()(struct uart_port *port)`` 287 * 288 * Release any memory and IO region resources currently in use by the 289 * @port. 290 * 291 * Locking: none. 292 * Interrupts: caller dependent. 293 * 294 * @request_port: ``int ()(struct uart_port *port)`` 295 * 296 * Request any memory and IO region resources required by the port. If any 297 * fail, no resources should be registered when this function returns, and 298 * it should return -%EBUSY on failure. 299 * 300 * Locking: none. 301 * Interrupts: caller dependent. 302 * 303 * @config_port: ``void ()(struct uart_port *port, int type)`` 304 * 305 * Perform any autoconfiguration steps required for the @port. @type 306 * contains a bit mask of the required configuration. %UART_CONFIG_TYPE 307 * indicates that the port requires detection and identification. 308 * @port->type should be set to the type found, or %PORT_UNKNOWN if no 309 * port was detected. 310 * 311 * %UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal, 312 * which should be probed using standard kernel autoprobing techniques. 313 * This is not necessary on platforms where ports have interrupts 314 * internally hard wired (eg, system on a chip implementations). 315 * 316 * Locking: none. 317 * Interrupts: caller dependent. 318 * 319 * @verify_port: ``int ()(struct uart_port *port, 320 * struct serial_struct *serinfo)`` 321 * 322 * Verify the new serial port information contained within @serinfo is 323 * suitable for this port type. 324 * 325 * Locking: none. 326 * Interrupts: caller dependent. 327 * 328 * @ioctl: ``int ()(struct uart_port *port, unsigned int cmd, 329 * unsigned long arg)`` 330 * 331 * Perform any port specific IOCTLs. IOCTL commands must be defined using 332 * the standard numbering system found in <asm/ioctl.h>. 333 * 334 * Locking: none. 335 * Interrupts: caller dependent. 336 * 337 * @poll_init: ``int ()(struct uart_port *port)`` 338 * 339 * Called by kgdb to perform the minimal hardware initialization needed to 340 * support @poll_put_char() and @poll_get_char(). Unlike @startup(), this 341 * should not request interrupts. 342 * 343 * Locking: %tty_mutex and tty_port->mutex taken. 344 * Interrupts: n/a. 345 * 346 * @poll_put_char: ``void ()(struct uart_port *port, unsigned char ch)`` 347 * 348 * Called by kgdb to write a single character @ch directly to the serial 349 * @port. It can and should block until there is space in the TX FIFO. 350 * 351 * Locking: none. 352 * Interrupts: caller dependent. 353 * This call must not sleep 354 * 355 * @poll_get_char: ``int ()(struct uart_port *port)`` 356 * 357 * Called by kgdb to read a single character directly from the serial 358 * port. If data is available, it should be returned; otherwise the 359 * function should return %NO_POLL_CHAR immediately. 360 * 361 * Locking: none. 362 * Interrupts: caller dependent. 363 * This call must not sleep 364 */ 365 struct uart_ops { 366 unsigned int (*tx_empty)(struct uart_port *); 367 void (*set_mctrl)(struct uart_port *, unsigned int mctrl); 368 unsigned int (*get_mctrl)(struct uart_port *); 369 void (*stop_tx)(struct uart_port *); 370 void (*start_tx)(struct uart_port *); 371 void (*throttle)(struct uart_port *); 372 void (*unthrottle)(struct uart_port *); 373 void (*send_xchar)(struct uart_port *, char ch); 374 void (*stop_rx)(struct uart_port *); 375 void (*start_rx)(struct uart_port *); 376 void (*enable_ms)(struct uart_port *); 377 void (*break_ctl)(struct uart_port *, int ctl); 378 int (*startup)(struct uart_port *); 379 void (*shutdown)(struct uart_port *); 380 void (*flush_buffer)(struct uart_port *); 381 void (*set_termios)(struct uart_port *, struct ktermios *new, 382 struct ktermios *old); 383 void (*set_ldisc)(struct uart_port *, struct ktermios *); 384 void (*pm)(struct uart_port *, unsigned int state, 385 unsigned int oldstate); 386 const char *(*type)(struct uart_port *); 387 void (*release_port)(struct uart_port *); 388 int (*request_port)(struct uart_port *); 389 void (*config_port)(struct uart_port *, int); 390 int (*verify_port)(struct uart_port *, struct serial_struct *); 391 int (*ioctl)(struct uart_port *, unsigned int, unsigned long); 392 #ifdef CONFIG_CONSOLE_POLL 393 int (*poll_init)(struct uart_port *); 394 void (*poll_put_char)(struct uart_port *, unsigned char); 395 int (*poll_get_char)(struct uart_port *); 396 #endif 397 }; 398 399 #define NO_POLL_CHAR 0x00ff0000 400 #define UART_CONFIG_TYPE (1 << 0) 401 #define UART_CONFIG_IRQ (1 << 1) 402 403 struct uart_icount { 404 __u32 cts; 405 __u32 dsr; 406 __u32 rng; 407 __u32 dcd; 408 __u32 rx; 409 __u32 tx; 410 __u32 frame; 411 __u32 overrun; 412 __u32 parity; 413 __u32 brk; 414 __u32 buf_overrun; 415 }; 416 417 typedef unsigned int __bitwise upf_t; 418 typedef unsigned int __bitwise upstat_t; 419 420 struct uart_port { 421 spinlock_t lock; /* port lock */ 422 unsigned long iobase; /* in/out[bwl] */ 423 unsigned char __iomem *membase; /* read/write[bwl] */ 424 unsigned int (*serial_in)(struct uart_port *, int); 425 void (*serial_out)(struct uart_port *, int, int); 426 void (*set_termios)(struct uart_port *, 427 struct ktermios *new, 428 struct ktermios *old); 429 void (*set_ldisc)(struct uart_port *, 430 struct ktermios *); 431 unsigned int (*get_mctrl)(struct uart_port *); 432 void (*set_mctrl)(struct uart_port *, unsigned int); 433 unsigned int (*get_divisor)(struct uart_port *, 434 unsigned int baud, 435 unsigned int *frac); 436 void (*set_divisor)(struct uart_port *, 437 unsigned int baud, 438 unsigned int quot, 439 unsigned int quot_frac); 440 int (*startup)(struct uart_port *port); 441 void (*shutdown)(struct uart_port *port); 442 void (*throttle)(struct uart_port *port); 443 void (*unthrottle)(struct uart_port *port); 444 int (*handle_irq)(struct uart_port *); 445 void (*pm)(struct uart_port *, unsigned int state, 446 unsigned int old); 447 void (*handle_break)(struct uart_port *); 448 int (*rs485_config)(struct uart_port *, 449 struct ktermios *termios, 450 struct serial_rs485 *rs485); 451 int (*iso7816_config)(struct uart_port *, 452 struct serial_iso7816 *iso7816); 453 unsigned int irq; /* irq number */ 454 unsigned long irqflags; /* irq flags */ 455 unsigned int uartclk; /* base uart clock */ 456 unsigned int fifosize; /* tx fifo size */ 457 unsigned char x_char; /* xon/xoff char */ 458 unsigned char regshift; /* reg offset shift */ 459 unsigned char iotype; /* io access style */ 460 unsigned char quirks; /* internal quirks */ 461 462 #define UPIO_PORT (SERIAL_IO_PORT) /* 8b I/O port access */ 463 #define UPIO_HUB6 (SERIAL_IO_HUB6) /* Hub6 ISA card */ 464 #define UPIO_MEM (SERIAL_IO_MEM) /* driver-specific */ 465 #define UPIO_MEM32 (SERIAL_IO_MEM32) /* 32b little endian */ 466 #define UPIO_AU (SERIAL_IO_AU) /* Au1x00 and RT288x type IO */ 467 #define UPIO_TSI (SERIAL_IO_TSI) /* Tsi108/109 type IO */ 468 #define UPIO_MEM32BE (SERIAL_IO_MEM32BE) /* 32b big endian */ 469 #define UPIO_MEM16 (SERIAL_IO_MEM16) /* 16b little endian */ 470 471 /* quirks must be updated while holding port mutex */ 472 #define UPQ_NO_TXEN_TEST BIT(0) 473 474 unsigned int read_status_mask; /* driver specific */ 475 unsigned int ignore_status_mask; /* driver specific */ 476 struct uart_state *state; /* pointer to parent state */ 477 struct uart_icount icount; /* statistics */ 478 479 struct console *cons; /* struct console, if any */ 480 /* flags must be updated while holding port mutex */ 481 upf_t flags; 482 483 /* 484 * These flags must be equivalent to the flags defined in 485 * include/uapi/linux/tty_flags.h which are the userspace definitions 486 * assigned from the serial_struct flags in uart_set_info() 487 * [for bit definitions in the UPF_CHANGE_MASK] 488 * 489 * Bits [0..ASYNCB_LAST_USER] are userspace defined/visible/changeable 490 * The remaining bits are serial-core specific and not modifiable by 491 * userspace. 492 */ 493 #define UPF_FOURPORT ((__force upf_t) ASYNC_FOURPORT /* 1 */ ) 494 #define UPF_SAK ((__force upf_t) ASYNC_SAK /* 2 */ ) 495 #define UPF_SPD_HI ((__force upf_t) ASYNC_SPD_HI /* 4 */ ) 496 #define UPF_SPD_VHI ((__force upf_t) ASYNC_SPD_VHI /* 5 */ ) 497 #define UPF_SPD_CUST ((__force upf_t) ASYNC_SPD_CUST /* 0x0030 */ ) 498 #define UPF_SPD_WARP ((__force upf_t) ASYNC_SPD_WARP /* 0x1010 */ ) 499 #define UPF_SPD_MASK ((__force upf_t) ASYNC_SPD_MASK /* 0x1030 */ ) 500 #define UPF_SKIP_TEST ((__force upf_t) ASYNC_SKIP_TEST /* 6 */ ) 501 #define UPF_AUTO_IRQ ((__force upf_t) ASYNC_AUTO_IRQ /* 7 */ ) 502 #define UPF_HARDPPS_CD ((__force upf_t) ASYNC_HARDPPS_CD /* 11 */ ) 503 #define UPF_SPD_SHI ((__force upf_t) ASYNC_SPD_SHI /* 12 */ ) 504 #define UPF_LOW_LATENCY ((__force upf_t) ASYNC_LOW_LATENCY /* 13 */ ) 505 #define UPF_BUGGY_UART ((__force upf_t) ASYNC_BUGGY_UART /* 14 */ ) 506 #define UPF_MAGIC_MULTIPLIER ((__force upf_t) ASYNC_MAGIC_MULTIPLIER /* 16 */ ) 507 508 #define UPF_NO_THRE_TEST ((__force upf_t) (1 << 19)) 509 /* Port has hardware-assisted h/w flow control */ 510 #define UPF_AUTO_CTS ((__force upf_t) (1 << 20)) 511 #define UPF_AUTO_RTS ((__force upf_t) (1 << 21)) 512 #define UPF_HARD_FLOW ((__force upf_t) (UPF_AUTO_CTS | UPF_AUTO_RTS)) 513 /* Port has hardware-assisted s/w flow control */ 514 #define UPF_SOFT_FLOW ((__force upf_t) (1 << 22)) 515 #define UPF_CONS_FLOW ((__force upf_t) (1 << 23)) 516 #define UPF_SHARE_IRQ ((__force upf_t) (1 << 24)) 517 #define UPF_EXAR_EFR ((__force upf_t) (1 << 25)) 518 #define UPF_BUG_THRE ((__force upf_t) (1 << 26)) 519 /* The exact UART type is known and should not be probed. */ 520 #define UPF_FIXED_TYPE ((__force upf_t) (1 << 27)) 521 #define UPF_BOOT_AUTOCONF ((__force upf_t) (1 << 28)) 522 #define UPF_FIXED_PORT ((__force upf_t) (1 << 29)) 523 #define UPF_DEAD ((__force upf_t) (1 << 30)) 524 #define UPF_IOREMAP ((__force upf_t) (1 << 31)) 525 526 #define __UPF_CHANGE_MASK 0x17fff 527 #define UPF_CHANGE_MASK ((__force upf_t) __UPF_CHANGE_MASK) 528 #define UPF_USR_MASK ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY)) 529 530 #if __UPF_CHANGE_MASK > ASYNC_FLAGS 531 #error Change mask not equivalent to userspace-visible bit defines 532 #endif 533 534 /* 535 * Must hold termios_rwsem, port mutex and port lock to change; 536 * can hold any one lock to read. 537 */ 538 upstat_t status; 539 540 #define UPSTAT_CTS_ENABLE ((__force upstat_t) (1 << 0)) 541 #define UPSTAT_DCD_ENABLE ((__force upstat_t) (1 << 1)) 542 #define UPSTAT_AUTORTS ((__force upstat_t) (1 << 2)) 543 #define UPSTAT_AUTOCTS ((__force upstat_t) (1 << 3)) 544 #define UPSTAT_AUTOXOFF ((__force upstat_t) (1 << 4)) 545 #define UPSTAT_SYNC_FIFO ((__force upstat_t) (1 << 5)) 546 547 int hw_stopped; /* sw-assisted CTS flow state */ 548 unsigned int mctrl; /* current modem ctrl settings */ 549 unsigned int frame_time; /* frame timing in ns */ 550 unsigned int type; /* port type */ 551 const struct uart_ops *ops; 552 unsigned int custom_divisor; 553 unsigned int line; /* port index */ 554 unsigned int minor; 555 resource_size_t mapbase; /* for ioremap */ 556 resource_size_t mapsize; 557 struct device *dev; /* parent device */ 558 559 unsigned long sysrq; /* sysrq timeout */ 560 unsigned int sysrq_ch; /* char for sysrq */ 561 unsigned char has_sysrq; 562 unsigned char sysrq_seq; /* index in sysrq_toggle_seq */ 563 564 unsigned char hub6; /* this should be in the 8250 driver */ 565 unsigned char suspended; 566 unsigned char console_reinit; 567 const char *name; /* port name */ 568 struct attribute_group *attr_group; /* port specific attributes */ 569 const struct attribute_group **tty_groups; /* all attributes (serial core use only) */ 570 struct serial_rs485 rs485; 571 struct serial_rs485 rs485_supported; /* Supported mask for serial_rs485 */ 572 struct gpio_desc *rs485_term_gpio; /* enable RS485 bus termination */ 573 struct serial_iso7816 iso7816; 574 void *private_data; /* generic platform data pointer */ 575 }; 576 577 static inline int serial_port_in(struct uart_port *up, int offset) 578 { 579 return up->serial_in(up, offset); 580 } 581 582 static inline void serial_port_out(struct uart_port *up, int offset, int value) 583 { 584 up->serial_out(up, offset, value); 585 } 586 587 /** 588 * enum uart_pm_state - power states for UARTs 589 * @UART_PM_STATE_ON: UART is powered, up and operational 590 * @UART_PM_STATE_OFF: UART is powered off 591 * @UART_PM_STATE_UNDEFINED: sentinel 592 */ 593 enum uart_pm_state { 594 UART_PM_STATE_ON = 0, 595 UART_PM_STATE_OFF = 3, /* number taken from ACPI */ 596 UART_PM_STATE_UNDEFINED, 597 }; 598 599 /* 600 * This is the state information which is persistent across opens. 601 */ 602 struct uart_state { 603 struct tty_port port; 604 605 enum uart_pm_state pm_state; 606 struct circ_buf xmit; 607 608 atomic_t refcount; 609 wait_queue_head_t remove_wait; 610 struct uart_port *uart_port; 611 }; 612 613 #define UART_XMIT_SIZE PAGE_SIZE 614 615 616 /* number of characters left in xmit buffer before we ask for more */ 617 #define WAKEUP_CHARS 256 618 619 struct module; 620 struct tty_driver; 621 622 struct uart_driver { 623 struct module *owner; 624 const char *driver_name; 625 const char *dev_name; 626 int major; 627 int minor; 628 int nr; 629 struct console *cons; 630 631 /* 632 * these are private; the low level driver should not 633 * touch these; they should be initialised to NULL 634 */ 635 struct uart_state *state; 636 struct tty_driver *tty_driver; 637 }; 638 639 void uart_write_wakeup(struct uart_port *port); 640 641 /* 642 * Baud rate helpers. 643 */ 644 void uart_update_timeout(struct uart_port *port, unsigned int cflag, 645 unsigned int baud); 646 unsigned int uart_get_baud_rate(struct uart_port *port, struct ktermios *termios, 647 struct ktermios *old, unsigned int min, 648 unsigned int max); 649 unsigned int uart_get_divisor(struct uart_port *port, unsigned int baud); 650 651 /* 652 * Calculates FIFO drain time. 653 */ 654 static inline unsigned long uart_fifo_timeout(struct uart_port *port) 655 { 656 u64 fifo_timeout = (u64)READ_ONCE(port->frame_time) * port->fifosize; 657 658 /* Add .02 seconds of slop */ 659 fifo_timeout += 20 * NSEC_PER_MSEC; 660 661 return max(nsecs_to_jiffies(fifo_timeout), 1UL); 662 } 663 664 /* Base timer interval for polling */ 665 static inline int uart_poll_timeout(struct uart_port *port) 666 { 667 int timeout = uart_fifo_timeout(port); 668 669 return timeout > 6 ? (timeout / 2 - 2) : 1; 670 } 671 672 /* 673 * Console helpers. 674 */ 675 struct earlycon_device { 676 struct console *con; 677 struct uart_port port; 678 char options[16]; /* e.g., 115200n8 */ 679 unsigned int baud; 680 }; 681 682 struct earlycon_id { 683 char name[15]; 684 char name_term; /* In case compiler didn't '\0' term name */ 685 char compatible[128]; 686 int (*setup)(struct earlycon_device *, const char *options); 687 }; 688 689 extern const struct earlycon_id __earlycon_table[]; 690 extern const struct earlycon_id __earlycon_table_end[]; 691 692 #if defined(CONFIG_SERIAL_EARLYCON) && !defined(MODULE) 693 #define EARLYCON_USED_OR_UNUSED __used 694 #else 695 #define EARLYCON_USED_OR_UNUSED __maybe_unused 696 #endif 697 698 #define OF_EARLYCON_DECLARE(_name, compat, fn) \ 699 static const struct earlycon_id __UNIQUE_ID(__earlycon_##_name) \ 700 EARLYCON_USED_OR_UNUSED __section("__earlycon_table") \ 701 __aligned(__alignof__(struct earlycon_id)) \ 702 = { .name = __stringify(_name), \ 703 .compatible = compat, \ 704 .setup = fn } 705 706 #define EARLYCON_DECLARE(_name, fn) OF_EARLYCON_DECLARE(_name, "", fn) 707 708 extern int of_setup_earlycon(const struct earlycon_id *match, 709 unsigned long node, 710 const char *options); 711 712 #ifdef CONFIG_SERIAL_EARLYCON 713 extern bool earlycon_acpi_spcr_enable __initdata; 714 int setup_earlycon(char *buf); 715 #else 716 static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED; 717 static inline int setup_earlycon(char *buf) { return 0; } 718 #endif 719 720 static inline bool uart_console_enabled(struct uart_port *port) 721 { 722 return uart_console(port) && (port->cons->flags & CON_ENABLED); 723 } 724 725 struct uart_port *uart_get_console(struct uart_port *ports, int nr, 726 struct console *c); 727 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr, 728 char **options); 729 void uart_parse_options(const char *options, int *baud, int *parity, int *bits, 730 int *flow); 731 int uart_set_options(struct uart_port *port, struct console *co, int baud, 732 int parity, int bits, int flow); 733 struct tty_driver *uart_console_device(struct console *co, int *index); 734 void uart_console_write(struct uart_port *port, const char *s, 735 unsigned int count, 736 void (*putchar)(struct uart_port *, unsigned char)); 737 738 /* 739 * Port/driver registration/removal 740 */ 741 int uart_register_driver(struct uart_driver *uart); 742 void uart_unregister_driver(struct uart_driver *uart); 743 int uart_add_one_port(struct uart_driver *reg, struct uart_port *port); 744 int uart_remove_one_port(struct uart_driver *reg, struct uart_port *port); 745 bool uart_match_port(const struct uart_port *port1, 746 const struct uart_port *port2); 747 748 /* 749 * Power Management 750 */ 751 int uart_suspend_port(struct uart_driver *reg, struct uart_port *port); 752 int uart_resume_port(struct uart_driver *reg, struct uart_port *port); 753 754 #define uart_circ_empty(circ) ((circ)->head == (circ)->tail) 755 #define uart_circ_clear(circ) ((circ)->head = (circ)->tail = 0) 756 757 #define uart_circ_chars_pending(circ) \ 758 (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE)) 759 760 #define uart_circ_chars_free(circ) \ 761 (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE)) 762 763 static inline int uart_tx_stopped(struct uart_port *port) 764 { 765 struct tty_struct *tty = port->state->port.tty; 766 if ((tty && tty->flow.stopped) || port->hw_stopped) 767 return 1; 768 return 0; 769 } 770 771 static inline bool uart_cts_enabled(struct uart_port *uport) 772 { 773 return !!(uport->status & UPSTAT_CTS_ENABLE); 774 } 775 776 static inline bool uart_softcts_mode(struct uart_port *uport) 777 { 778 upstat_t mask = UPSTAT_CTS_ENABLE | UPSTAT_AUTOCTS; 779 780 return ((uport->status & mask) == UPSTAT_CTS_ENABLE); 781 } 782 783 /* 784 * The following are helper functions for the low level drivers. 785 */ 786 787 extern void uart_handle_dcd_change(struct uart_port *uport, 788 unsigned int status); 789 extern void uart_handle_cts_change(struct uart_port *uport, 790 unsigned int status); 791 792 extern void uart_insert_char(struct uart_port *port, unsigned int status, 793 unsigned int overrun, unsigned int ch, unsigned int flag); 794 795 void uart_xchar_out(struct uart_port *uport, int offset); 796 797 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL 798 #define SYSRQ_TIMEOUT (HZ * 5) 799 800 bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch); 801 802 static inline int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch) 803 { 804 if (!port->sysrq) 805 return 0; 806 807 if (ch && time_before(jiffies, port->sysrq)) { 808 if (sysrq_mask()) { 809 handle_sysrq(ch); 810 port->sysrq = 0; 811 return 1; 812 } 813 if (uart_try_toggle_sysrq(port, ch)) 814 return 1; 815 } 816 port->sysrq = 0; 817 818 return 0; 819 } 820 821 static inline int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) 822 { 823 if (!port->sysrq) 824 return 0; 825 826 if (ch && time_before(jiffies, port->sysrq)) { 827 if (sysrq_mask()) { 828 port->sysrq_ch = ch; 829 port->sysrq = 0; 830 return 1; 831 } 832 if (uart_try_toggle_sysrq(port, ch)) 833 return 1; 834 } 835 port->sysrq = 0; 836 837 return 0; 838 } 839 840 static inline void uart_unlock_and_check_sysrq(struct uart_port *port) 841 { 842 int sysrq_ch; 843 844 if (!port->has_sysrq) { 845 spin_unlock(&port->lock); 846 return; 847 } 848 849 sysrq_ch = port->sysrq_ch; 850 port->sysrq_ch = 0; 851 852 spin_unlock(&port->lock); 853 854 if (sysrq_ch) 855 handle_sysrq(sysrq_ch); 856 } 857 858 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port, 859 unsigned long flags) 860 { 861 int sysrq_ch; 862 863 if (!port->has_sysrq) { 864 spin_unlock_irqrestore(&port->lock, flags); 865 return; 866 } 867 868 sysrq_ch = port->sysrq_ch; 869 port->sysrq_ch = 0; 870 871 spin_unlock_irqrestore(&port->lock, flags); 872 873 if (sysrq_ch) 874 handle_sysrq(sysrq_ch); 875 } 876 #else /* CONFIG_MAGIC_SYSRQ_SERIAL */ 877 static inline int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch) 878 { 879 return 0; 880 } 881 static inline int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) 882 { 883 return 0; 884 } 885 static inline void uart_unlock_and_check_sysrq(struct uart_port *port) 886 { 887 spin_unlock(&port->lock); 888 } 889 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port, 890 unsigned long flags) 891 { 892 spin_unlock_irqrestore(&port->lock, flags); 893 } 894 #endif /* CONFIG_MAGIC_SYSRQ_SERIAL */ 895 896 /* 897 * We do the SysRQ and SAK checking like this... 898 */ 899 static inline int uart_handle_break(struct uart_port *port) 900 { 901 struct uart_state *state = port->state; 902 903 if (port->handle_break) 904 port->handle_break(port); 905 906 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL 907 if (port->has_sysrq && uart_console(port)) { 908 if (!port->sysrq) { 909 port->sysrq = jiffies + SYSRQ_TIMEOUT; 910 return 1; 911 } 912 port->sysrq = 0; 913 } 914 #endif 915 if (port->flags & UPF_SAK) 916 do_SAK(state->port.tty); 917 return 0; 918 } 919 920 /* 921 * UART_ENABLE_MS - determine if port should enable modem status irqs 922 */ 923 #define UART_ENABLE_MS(port,cflag) ((port)->flags & UPF_HARDPPS_CD || \ 924 (cflag) & CRTSCTS || \ 925 !((cflag) & CLOCAL)) 926 927 int uart_get_rs485_mode(struct uart_port *port); 928 int uart_rs485_config(struct uart_port *port); 929 #endif /* LINUX_SERIAL_CORE_H */ 930