1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * linux/drivers/char/serial_core.h
5 * Copyright (C) 2000 Deep Blue Solutions Ltd.
7 #ifndef LINUX_SERIAL_CORE_H
8 #define LINUX_SERIAL_CORE_H
10 #include <linux/bitops.h>
11 #include <linux/compiler.h>
12 #include <linux/console.h>
13 #include <linux/interrupt.h>
14 #include <linux/lockdep.h>
15 #include <linux/printk.h>
16 #include <linux/spinlock.h>
17 #include <linux/sched.h>
18 #include <linux/tty.h>
19 #include <linux/mutex.h>
20 #include <linux/sysrq.h>
21 #include <uapi/linux/serial_core.h>
23 #ifdef CONFIG_SERIAL_CORE_CONSOLE
24 #define uart_console(port) \
25 ((port)->cons && (port)->cons->index == (port)->line)
27 #define uart_console(port) ({ (void)port; 0; })
32 struct serial_port_device;
37 * struct uart_ops -- interface between serial_core and the driver
39 * This structure describes all the operations that can be done on the
42 * @tx_empty: ``unsigned int ()(struct uart_port *port)``
44 * This function tests whether the transmitter fifo and shifter for the
45 * @port is empty. If it is empty, this function should return
46 * %TIOCSER_TEMT, otherwise return 0. If the port does not support this
47 * operation, then it should return %TIOCSER_TEMT.
50 * Interrupts: caller dependent.
51 * This call must not sleep
53 * @set_mctrl: ``void ()(struct uart_port *port, unsigned int mctrl)``
55 * This function sets the modem control lines for @port to the state
56 * described by @mctrl. The relevant bits of @mctrl are:
58 * - %TIOCM_RTS RTS signal.
59 * - %TIOCM_DTR DTR signal.
60 * - %TIOCM_OUT1 OUT1 signal.
61 * - %TIOCM_OUT2 OUT2 signal.
62 * - %TIOCM_LOOP Set the port into loopback mode.
64 * If the appropriate bit is set, the signal should be driven
65 * active. If the bit is clear, the signal should be driven
68 * Locking: @port->lock taken.
69 * Interrupts: locally disabled.
70 * This call must not sleep
72 * @get_mctrl: ``unsigned int ()(struct uart_port *port)``
74 * Returns the current state of modem control inputs of @port. The state
75 * of the outputs should not be returned, since the core keeps track of
76 * their state. The state information should include:
78 * - %TIOCM_CAR state of DCD signal
79 * - %TIOCM_CTS state of CTS signal
80 * - %TIOCM_DSR state of DSR signal
81 * - %TIOCM_RI state of RI signal
83 * The bit is set if the signal is currently driven active. If
84 * the port does not support CTS, DCD or DSR, the driver should
85 * indicate that the signal is permanently active. If RI is
86 * not available, the signal should not be indicated as active.
88 * Locking: @port->lock taken.
89 * Interrupts: locally disabled.
90 * This call must not sleep
92 * @stop_tx: ``void ()(struct uart_port *port)``
94 * Stop transmitting characters. This might be due to the CTS line
95 * becoming inactive or the tty layer indicating we want to stop
96 * transmission due to an %XOFF character.
98 * The driver should stop transmitting characters as soon as possible.
100 * Locking: @port->lock taken.
101 * Interrupts: locally disabled.
102 * This call must not sleep
104 * @start_tx: ``void ()(struct uart_port *port)``
106 * Start transmitting characters.
108 * Locking: @port->lock taken.
109 * Interrupts: locally disabled.
110 * This call must not sleep
112 * @throttle: ``void ()(struct uart_port *port)``
114 * Notify the serial driver that input buffers for the line discipline are
115 * close to full, and it should somehow signal that no more characters
116 * should be sent to the serial port.
117 * This will be called only if hardware assisted flow control is enabled.
119 * Locking: serialized with @unthrottle() and termios modification by the
122 * @unthrottle: ``void ()(struct uart_port *port)``
124 * Notify the serial driver that characters can now be sent to the serial
125 * port without fear of overrunning the input buffers of the line
128 * This will be called only if hardware assisted flow control is enabled.
130 * Locking: serialized with @throttle() and termios modification by the
133 * @send_xchar: ``void ()(struct uart_port *port, char ch)``
135 * Transmit a high priority character, even if the port is stopped. This
136 * is used to implement XON/XOFF flow control and tcflow(). If the serial
137 * driver does not implement this function, the tty core will append the
138 * character to the circular buffer and then call start_tx() / stop_tx()
139 * to flush the data out.
141 * Do not transmit if @ch == '\0' (%__DISABLED_CHAR).
144 * Interrupts: caller dependent.
146 * @start_rx: ``void ()(struct uart_port *port)``
148 * Start receiving characters.
150 * Locking: @port->lock taken.
151 * Interrupts: locally disabled.
152 * This call must not sleep
154 * @stop_rx: ``void ()(struct uart_port *port)``
156 * Stop receiving characters; the @port is in the process of being closed.
158 * Locking: @port->lock taken.
159 * Interrupts: locally disabled.
160 * This call must not sleep
162 * @enable_ms: ``void ()(struct uart_port *port)``
164 * Enable the modem status interrupts.
166 * This method may be called multiple times. Modem status interrupts
167 * should be disabled when the @shutdown() method is called.
169 * Locking: @port->lock taken.
170 * Interrupts: locally disabled.
171 * This call must not sleep
173 * @break_ctl: ``void ()(struct uart_port *port, int ctl)``
175 * Control the transmission of a break signal. If @ctl is nonzero, the
176 * break signal should be transmitted. The signal should be terminated
177 * when another call is made with a zero @ctl.
179 * Locking: caller holds tty_port->mutex
181 * @startup: ``int ()(struct uart_port *port)``
183 * Grab any interrupt resources and initialise any low level driver state.
184 * Enable the port for reception. It should not activate RTS nor DTR;
185 * this will be done via a separate call to @set_mctrl().
187 * This method will only be called when the port is initially opened.
189 * Locking: port_sem taken.
190 * Interrupts: globally disabled.
192 * @shutdown: ``void ()(struct uart_port *port)``
194 * Disable the @port, disable any break condition that may be in effect,
195 * and free any interrupt resources. It should not disable RTS nor DTR;
196 * this will have already been done via a separate call to @set_mctrl().
198 * Drivers must not access @port->state once this call has completed.
200 * This method will only be called when there are no more users of this
203 * Locking: port_sem taken.
204 * Interrupts: caller dependent.
206 * @flush_buffer: ``void ()(struct uart_port *port)``
208 * Flush any write buffers, reset any DMA state and stop any ongoing DMA
211 * This will be called whenever the @port->state->xmit circular buffer is
214 * Locking: @port->lock taken.
215 * Interrupts: locally disabled.
216 * This call must not sleep
218 * @set_termios: ``void ()(struct uart_port *port, struct ktermios *new,
219 * struct ktermios *old)``
221 * Change the @port parameters, including word length, parity, stop bits.
222 * Update @port->read_status_mask and @port->ignore_status_mask to
223 * indicate the types of events we are interested in receiving. Relevant
224 * ktermios::c_cflag bits are:
226 * - %CSIZE - word size
227 * - %CSTOPB - 2 stop bits
228 * - %PARENB - parity enable
229 * - %PARODD - odd parity (when %PARENB is in force)
230 * - %ADDRB - address bit (changed through uart_port::rs485_config()).
231 * - %CREAD - enable reception of characters (if not set, still receive
232 * characters from the port, but throw them away).
233 * - %CRTSCTS - if set, enable CTS status change reporting.
234 * - %CLOCAL - if not set, enable modem status change reporting.
236 * Relevant ktermios::c_iflag bits are:
238 * - %INPCK - enable frame and parity error events to be passed to the TTY
240 * - %BRKINT / %PARMRK - both of these enable break events to be passed to
242 * - %IGNPAR - ignore parity and framing errors.
243 * - %IGNBRK - ignore break errors. If %IGNPAR is also set, ignore overrun
246 * The interaction of the ktermios::c_iflag bits is as follows (parity
247 * error given as an example):
249 * ============ ======= ======= =========================================
250 * Parity error INPCK IGNPAR
251 * ============ ======= ======= =========================================
252 * n/a 0 n/a character received, marked as %TTY_NORMAL
253 * None 1 n/a character received, marked as %TTY_NORMAL
254 * Yes 1 0 character received, marked as %TTY_PARITY
255 * Yes 1 1 character discarded
256 * ============ ======= ======= =========================================
258 * Other flags may be used (eg, xon/xoff characters) if your hardware
259 * supports hardware "soft" flow control.
261 * Locking: caller holds tty_port->mutex
262 * Interrupts: caller dependent.
263 * This call must not sleep
265 * @set_ldisc: ``void ()(struct uart_port *port, struct ktermios *termios)``
267 * Notifier for discipline change. See
268 * Documentation/driver-api/tty/tty_ldisc.rst.
270 * Locking: caller holds tty_port->mutex
272 * @pm: ``void ()(struct uart_port *port, unsigned int state,
273 * unsigned int oldstate)``
275 * Perform any power management related activities on the specified @port.
276 * @state indicates the new state (defined by enum uart_pm_state),
277 * @oldstate indicates the previous state.
279 * This function should not be used to grab any resources.
281 * This will be called when the @port is initially opened and finally
282 * closed, except when the @port is also the system console. This will
283 * occur even if %CONFIG_PM is not set.
286 * Interrupts: caller dependent.
288 * @type: ``const char *()(struct uart_port *port)``
290 * Return a pointer to a string constant describing the specified @port,
291 * or return %NULL, in which case the string 'unknown' is substituted.
294 * Interrupts: caller dependent.
296 * @release_port: ``void ()(struct uart_port *port)``
298 * Release any memory and IO region resources currently in use by the
302 * Interrupts: caller dependent.
304 * @request_port: ``int ()(struct uart_port *port)``
306 * Request any memory and IO region resources required by the port. If any
307 * fail, no resources should be registered when this function returns, and
308 * it should return -%EBUSY on failure.
311 * Interrupts: caller dependent.
313 * @config_port: ``void ()(struct uart_port *port, int type)``
315 * Perform any autoconfiguration steps required for the @port. @type
316 * contains a bit mask of the required configuration. %UART_CONFIG_TYPE
317 * indicates that the port requires detection and identification.
318 * @port->type should be set to the type found, or %PORT_UNKNOWN if no
321 * %UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
322 * which should be probed using standard kernel autoprobing techniques.
323 * This is not necessary on platforms where ports have interrupts
324 * internally hard wired (eg, system on a chip implementations).
327 * Interrupts: caller dependent.
329 * @verify_port: ``int ()(struct uart_port *port,
330 * struct serial_struct *serinfo)``
332 * Verify the new serial port information contained within @serinfo is
333 * suitable for this port type.
336 * Interrupts: caller dependent.
338 * @ioctl: ``int ()(struct uart_port *port, unsigned int cmd,
339 * unsigned long arg)``
341 * Perform any port specific IOCTLs. IOCTL commands must be defined using
342 * the standard numbering system found in <asm/ioctl.h>.
345 * Interrupts: caller dependent.
347 * @poll_init: ``int ()(struct uart_port *port)``
349 * Called by kgdb to perform the minimal hardware initialization needed to
350 * support @poll_put_char() and @poll_get_char(). Unlike @startup(), this
351 * should not request interrupts.
353 * Locking: %tty_mutex and tty_port->mutex taken.
356 * @poll_put_char: ``void ()(struct uart_port *port, unsigned char ch)``
358 * Called by kgdb to write a single character @ch directly to the serial
359 * @port. It can and should block until there is space in the TX FIFO.
362 * Interrupts: caller dependent.
363 * This call must not sleep
365 * @poll_get_char: ``int ()(struct uart_port *port)``
367 * Called by kgdb to read a single character directly from the serial
368 * port. If data is available, it should be returned; otherwise the
369 * function should return %NO_POLL_CHAR immediately.
372 * Interrupts: caller dependent.
373 * This call must not sleep
376 unsigned int (*tx_empty)(struct uart_port *);
377 void (*set_mctrl)(struct uart_port *, unsigned int mctrl);
378 unsigned int (*get_mctrl)(struct uart_port *);
379 void (*stop_tx)(struct uart_port *);
380 void (*start_tx)(struct uart_port *);
381 void (*throttle)(struct uart_port *);
382 void (*unthrottle)(struct uart_port *);
383 void (*send_xchar)(struct uart_port *, char ch);
384 void (*stop_rx)(struct uart_port *);
385 void (*start_rx)(struct uart_port *);
386 void (*enable_ms)(struct uart_port *);
387 void (*break_ctl)(struct uart_port *, int ctl);
388 int (*startup)(struct uart_port *);
389 void (*shutdown)(struct uart_port *);
390 void (*flush_buffer)(struct uart_port *);
391 void (*set_termios)(struct uart_port *, struct ktermios *new,
392 const struct ktermios *old);
393 void (*set_ldisc)(struct uart_port *, struct ktermios *);
394 void (*pm)(struct uart_port *, unsigned int state,
395 unsigned int oldstate);
396 const char *(*type)(struct uart_port *);
397 void (*release_port)(struct uart_port *);
398 int (*request_port)(struct uart_port *);
399 void (*config_port)(struct uart_port *, int);
400 int (*verify_port)(struct uart_port *, struct serial_struct *);
401 int (*ioctl)(struct uart_port *, unsigned int, unsigned long);
402 #ifdef CONFIG_CONSOLE_POLL
403 int (*poll_init)(struct uart_port *);
404 void (*poll_put_char)(struct uart_port *, unsigned char);
405 int (*poll_get_char)(struct uart_port *);
409 #define NO_POLL_CHAR 0x00ff0000
410 #define UART_CONFIG_TYPE (1 << 0)
411 #define UART_CONFIG_IRQ (1 << 1)
427 typedef u64 __bitwise upf_t;
428 typedef unsigned int __bitwise upstat_t;
431 spinlock_t lock; /* port lock */
432 unsigned long iobase; /* in/out[bwl] */
433 unsigned char __iomem *membase; /* read/write[bwl] */
434 unsigned int (*serial_in)(struct uart_port *, int);
435 void (*serial_out)(struct uart_port *, int, int);
436 void (*set_termios)(struct uart_port *,
437 struct ktermios *new,
438 const struct ktermios *old);
439 void (*set_ldisc)(struct uart_port *,
441 unsigned int (*get_mctrl)(struct uart_port *);
442 void (*set_mctrl)(struct uart_port *, unsigned int);
443 unsigned int (*get_divisor)(struct uart_port *,
446 void (*set_divisor)(struct uart_port *,
449 unsigned int quot_frac);
450 int (*startup)(struct uart_port *port);
451 void (*shutdown)(struct uart_port *port);
452 void (*throttle)(struct uart_port *port);
453 void (*unthrottle)(struct uart_port *port);
454 int (*handle_irq)(struct uart_port *);
455 void (*pm)(struct uart_port *, unsigned int state,
457 void (*handle_break)(struct uart_port *);
458 int (*rs485_config)(struct uart_port *,
459 struct ktermios *termios,
460 struct serial_rs485 *rs485);
461 int (*iso7816_config)(struct uart_port *,
462 struct serial_iso7816 *iso7816);
463 unsigned int ctrl_id; /* optional serial core controller id */
464 unsigned int port_id; /* optional serial core port id */
465 unsigned int irq; /* irq number */
466 unsigned long irqflags; /* irq flags */
467 unsigned int uartclk; /* base uart clock */
468 unsigned int fifosize; /* tx fifo size */
469 unsigned char x_char; /* xon/xoff char */
470 unsigned char regshift; /* reg offset shift */
472 unsigned char iotype; /* io access style */
474 #define UPIO_UNKNOWN ((unsigned char)~0U) /* UCHAR_MAX */
475 #define UPIO_PORT (SERIAL_IO_PORT) /* 8b I/O port access */
476 #define UPIO_HUB6 (SERIAL_IO_HUB6) /* Hub6 ISA card */
477 #define UPIO_MEM (SERIAL_IO_MEM) /* driver-specific */
478 #define UPIO_MEM32 (SERIAL_IO_MEM32) /* 32b little endian */
479 #define UPIO_AU (SERIAL_IO_AU) /* Au1x00 and RT288x type IO */
480 #define UPIO_TSI (SERIAL_IO_TSI) /* Tsi108/109 type IO */
481 #define UPIO_MEM32BE (SERIAL_IO_MEM32BE) /* 32b big endian */
482 #define UPIO_MEM16 (SERIAL_IO_MEM16) /* 16b little endian */
484 unsigned char quirks; /* internal quirks */
486 /* internal quirks must be updated while holding port mutex */
487 #define UPQ_NO_TXEN_TEST BIT(0)
489 unsigned int read_status_mask; /* driver specific */
490 unsigned int ignore_status_mask; /* driver specific */
491 struct uart_state *state; /* pointer to parent state */
492 struct uart_icount icount; /* statistics */
494 struct console *cons; /* struct console, if any */
495 /* flags must be updated while holding port mutex */
499 * These flags must be equivalent to the flags defined in
500 * include/uapi/linux/tty_flags.h which are the userspace definitions
501 * assigned from the serial_struct flags in uart_set_info()
502 * [for bit definitions in the UPF_CHANGE_MASK]
504 * Bits [0..ASYNCB_LAST_USER] are userspace defined/visible/changeable
505 * The remaining bits are serial-core specific and not modifiable by
508 #define UPF_FOURPORT ((__force upf_t) ASYNC_FOURPORT /* 1 */ )
509 #define UPF_SAK ((__force upf_t) ASYNC_SAK /* 2 */ )
510 #define UPF_SPD_HI ((__force upf_t) ASYNC_SPD_HI /* 4 */ )
511 #define UPF_SPD_VHI ((__force upf_t) ASYNC_SPD_VHI /* 5 */ )
512 #define UPF_SPD_CUST ((__force upf_t) ASYNC_SPD_CUST /* 0x0030 */ )
513 #define UPF_SPD_WARP ((__force upf_t) ASYNC_SPD_WARP /* 0x1010 */ )
514 #define UPF_SPD_MASK ((__force upf_t) ASYNC_SPD_MASK /* 0x1030 */ )
515 #define UPF_SKIP_TEST ((__force upf_t) ASYNC_SKIP_TEST /* 6 */ )
516 #define UPF_AUTO_IRQ ((__force upf_t) ASYNC_AUTO_IRQ /* 7 */ )
517 #define UPF_HARDPPS_CD ((__force upf_t) ASYNC_HARDPPS_CD /* 11 */ )
518 #define UPF_SPD_SHI ((__force upf_t) ASYNC_SPD_SHI /* 12 */ )
519 #define UPF_LOW_LATENCY ((__force upf_t) ASYNC_LOW_LATENCY /* 13 */ )
520 #define UPF_BUGGY_UART ((__force upf_t) ASYNC_BUGGY_UART /* 14 */ )
521 #define UPF_MAGIC_MULTIPLIER ((__force upf_t) ASYNC_MAGIC_MULTIPLIER /* 16 */ )
523 #define UPF_NO_THRE_TEST ((__force upf_t) BIT_ULL(19))
524 /* Port has hardware-assisted h/w flow control */
525 #define UPF_AUTO_CTS ((__force upf_t) BIT_ULL(20))
526 #define UPF_AUTO_RTS ((__force upf_t) BIT_ULL(21))
527 #define UPF_HARD_FLOW ((__force upf_t) (UPF_AUTO_CTS | UPF_AUTO_RTS))
528 /* Port has hardware-assisted s/w flow control */
529 #define UPF_SOFT_FLOW ((__force upf_t) BIT_ULL(22))
530 #define UPF_CONS_FLOW ((__force upf_t) BIT_ULL(23))
531 #define UPF_SHARE_IRQ ((__force upf_t) BIT_ULL(24))
532 #define UPF_EXAR_EFR ((__force upf_t) BIT_ULL(25))
533 #define UPF_BUG_THRE ((__force upf_t) BIT_ULL(26))
534 /* The exact UART type is known and should not be probed. */
535 #define UPF_FIXED_TYPE ((__force upf_t) BIT_ULL(27))
536 #define UPF_BOOT_AUTOCONF ((__force upf_t) BIT_ULL(28))
537 #define UPF_FIXED_PORT ((__force upf_t) BIT_ULL(29))
538 #define UPF_DEAD ((__force upf_t) BIT_ULL(30))
539 #define UPF_IOREMAP ((__force upf_t) BIT_ULL(31))
540 #define UPF_FULL_PROBE ((__force upf_t) BIT_ULL(32))
542 #define __UPF_CHANGE_MASK 0x17fff
543 #define UPF_CHANGE_MASK ((__force upf_t) __UPF_CHANGE_MASK)
544 #define UPF_USR_MASK ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
546 #if __UPF_CHANGE_MASK > ASYNC_FLAGS
547 #error Change mask not equivalent to userspace-visible bit defines
551 * Must hold termios_rwsem, port mutex and port lock to change;
552 * can hold any one lock to read.
556 #define UPSTAT_CTS_ENABLE ((__force upstat_t) (1 << 0))
557 #define UPSTAT_DCD_ENABLE ((__force upstat_t) (1 << 1))
558 #define UPSTAT_AUTORTS ((__force upstat_t) (1 << 2))
559 #define UPSTAT_AUTOCTS ((__force upstat_t) (1 << 3))
560 #define UPSTAT_AUTOXOFF ((__force upstat_t) (1 << 4))
561 #define UPSTAT_SYNC_FIFO ((__force upstat_t) (1 << 5))
563 bool hw_stopped; /* sw-assisted CTS flow state */
564 unsigned int mctrl; /* current modem ctrl settings */
565 unsigned int frame_time; /* frame timing in ns */
566 unsigned int type; /* port type */
567 const struct uart_ops *ops;
568 unsigned int custom_divisor;
569 unsigned int line; /* port index */
571 resource_size_t mapbase; /* for ioremap */
572 resource_size_t mapsize;
573 struct device *dev; /* serial port physical parent device */
574 struct serial_port_device *port_dev; /* serial core port device */
576 unsigned long sysrq; /* sysrq timeout */
577 u8 sysrq_ch; /* char for sysrq */
578 unsigned char has_sysrq;
579 unsigned char sysrq_seq; /* index in sysrq_toggle_seq */
581 unsigned char hub6; /* this should be in the 8250 driver */
582 unsigned char suspended;
583 unsigned char console_reinit;
584 const char *name; /* port name */
585 struct attribute_group *attr_group; /* port specific attributes */
586 const struct attribute_group **tty_groups; /* all attributes (serial core use only) */
587 struct serial_rs485 rs485;
588 struct serial_rs485 rs485_supported; /* Supported mask for serial_rs485 */
589 struct gpio_desc *rs485_term_gpio; /* enable RS485 bus termination */
590 struct gpio_desc *rs485_rx_during_tx_gpio; /* Output GPIO that sets the state of RS485 RX during TX */
591 struct serial_iso7816 iso7816;
592 void *private_data; /* generic platform data pointer */
596 * Only for console->device_lock()/_unlock() callbacks and internal
597 * port lock wrapper synchronization.
599 static inline void __uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
601 spin_lock_irqsave(&up->lock, *flags);
605 * Only for console->device_lock()/_unlock() callbacks and internal
606 * port lock wrapper synchronization.
608 static inline void __uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
610 spin_unlock_irqrestore(&up->lock, flags);
614 * uart_port_set_cons - Safely set the @cons field for a uart
615 * @up: The uart port to set
616 * @con: The new console to set to
618 * This function must be used to set @up->cons. It uses the port lock to
619 * synchronize with the port lock wrappers in order to ensure that the console
620 * cannot change or disappear while another context is holding the port lock.
622 static inline void uart_port_set_cons(struct uart_port *up, struct console *con)
626 __uart_port_lock_irqsave(up, &flags);
628 __uart_port_unlock_irqrestore(up, flags);
631 /* Only for internal port lock wrapper usage. */
632 static inline bool __uart_port_using_nbcon(struct uart_port *up)
634 lockdep_assert_held_once(&up->lock);
636 if (likely(!uart_console(up)))
640 * @up->cons is only modified under the port lock. Therefore it is
641 * certain that it cannot disappear here.
643 * @up->cons->node is added/removed from the console list under the
644 * port lock. Therefore it is certain that the registration status
645 * cannot change here, thus @up->cons->flags can be read directly.
647 if (hlist_unhashed_lockless(&up->cons->node) ||
648 !(up->cons->flags & CON_NBCON) ||
649 !up->cons->write_atomic) {
656 /* Only for internal port lock wrapper usage. */
657 static inline bool __uart_port_nbcon_try_acquire(struct uart_port *up)
659 if (!__uart_port_using_nbcon(up))
662 return nbcon_device_try_acquire(up->cons);
665 /* Only for internal port lock wrapper usage. */
666 static inline void __uart_port_nbcon_acquire(struct uart_port *up)
668 if (!__uart_port_using_nbcon(up))
671 while (!nbcon_device_try_acquire(up->cons))
675 /* Only for internal port lock wrapper usage. */
676 static inline void __uart_port_nbcon_release(struct uart_port *up)
678 if (!__uart_port_using_nbcon(up))
681 nbcon_device_release(up->cons);
685 * uart_port_lock - Lock the UART port
686 * @up: Pointer to UART port structure
688 static inline void uart_port_lock(struct uart_port *up)
690 spin_lock(&up->lock);
691 __uart_port_nbcon_acquire(up);
695 * uart_port_lock_irq - Lock the UART port and disable interrupts
696 * @up: Pointer to UART port structure
698 static inline void uart_port_lock_irq(struct uart_port *up)
700 spin_lock_irq(&up->lock);
701 __uart_port_nbcon_acquire(up);
705 * uart_port_lock_irqsave - Lock the UART port, save and disable interrupts
706 * @up: Pointer to UART port structure
707 * @flags: Pointer to interrupt flags storage
709 static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
711 spin_lock_irqsave(&up->lock, *flags);
712 __uart_port_nbcon_acquire(up);
716 * uart_port_trylock - Try to lock the UART port
717 * @up: Pointer to UART port structure
719 * Returns: True if lock was acquired, false otherwise
721 static inline bool uart_port_trylock(struct uart_port *up)
723 if (!spin_trylock(&up->lock))
726 if (!__uart_port_nbcon_try_acquire(up)) {
727 spin_unlock(&up->lock);
735 * uart_port_trylock_irqsave - Try to lock the UART port, save and disable interrupts
736 * @up: Pointer to UART port structure
737 * @flags: Pointer to interrupt flags storage
739 * Returns: True if lock was acquired, false otherwise
741 static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigned long *flags)
743 if (!spin_trylock_irqsave(&up->lock, *flags))
746 if (!__uart_port_nbcon_try_acquire(up)) {
747 spin_unlock_irqrestore(&up->lock, *flags);
755 * uart_port_unlock - Unlock the UART port
756 * @up: Pointer to UART port structure
758 static inline void uart_port_unlock(struct uart_port *up)
760 __uart_port_nbcon_release(up);
761 spin_unlock(&up->lock);
765 * uart_port_unlock_irq - Unlock the UART port and re-enable interrupts
766 * @up: Pointer to UART port structure
768 static inline void uart_port_unlock_irq(struct uart_port *up)
770 __uart_port_nbcon_release(up);
771 spin_unlock_irq(&up->lock);
775 * uart_port_unlock_irqrestore - Unlock the UART port, restore interrupts
776 * @up: Pointer to UART port structure
777 * @flags: The saved interrupt flags for restore
779 static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
781 __uart_port_nbcon_release(up);
782 spin_unlock_irqrestore(&up->lock, flags);
785 static inline int serial_port_in(struct uart_port *up, int offset)
787 return up->serial_in(up, offset);
790 static inline void serial_port_out(struct uart_port *up, int offset, int value)
792 up->serial_out(up, offset, value);
796 * enum uart_pm_state - power states for UARTs
797 * @UART_PM_STATE_ON: UART is powered, up and operational
798 * @UART_PM_STATE_OFF: UART is powered off
799 * @UART_PM_STATE_UNDEFINED: sentinel
802 UART_PM_STATE_ON = 0,
803 UART_PM_STATE_OFF = 3, /* number taken from ACPI */
804 UART_PM_STATE_UNDEFINED,
808 * This is the state information which is persistent across opens.
811 struct tty_port port;
813 enum uart_pm_state pm_state;
816 wait_queue_head_t remove_wait;
817 struct uart_port *uart_port;
820 #define UART_XMIT_SIZE PAGE_SIZE
823 /* number of characters left in xmit buffer before we ask for more */
824 #define WAKEUP_CHARS 256
827 * uart_xmit_advance - Advance xmit buffer and account Tx'ed chars
828 * @up: uart_port structure describing the port
829 * @chars: number of characters sent
831 * This function advances the tail of circular xmit buffer by the number of
832 * @chars transmitted and handles accounting of transmitted bytes (into
835 static inline void uart_xmit_advance(struct uart_port *up, unsigned int chars)
837 struct tty_port *tport = &up->state->port;
839 kfifo_skip_count(&tport->xmit_fifo, chars);
840 up->icount.tx += chars;
843 static inline unsigned int uart_fifo_out(struct uart_port *up,
844 unsigned char *buf, unsigned int chars)
846 struct tty_port *tport = &up->state->port;
848 chars = kfifo_out(&tport->xmit_fifo, buf, chars);
849 up->icount.tx += chars;
854 static inline unsigned int uart_fifo_get(struct uart_port *up,
857 struct tty_port *tport = &up->state->port;
860 chars = kfifo_get(&tport->xmit_fifo, ch);
861 up->icount.tx += chars;
870 struct module *owner;
871 const char *driver_name;
872 const char *dev_name;
876 struct console *cons;
879 * these are private; the low level driver should not
880 * touch these; they should be initialised to NULL
882 struct uart_state *state;
883 struct tty_driver *tty_driver;
886 void uart_write_wakeup(struct uart_port *port);
889 * enum UART_TX_FLAGS -- flags for uart_port_tx_flags()
891 * @UART_TX_NOSTOP: don't call port->ops->stop_tx() on empty buffer
894 UART_TX_NOSTOP = BIT(0),
897 #define __uart_port_tx(uport, ch, flags, tx_ready, put_char, tx_done, \
898 for_test, for_post) \
900 struct uart_port *__port = (uport); \
901 struct tty_port *__tport = &__port->state->port; \
902 unsigned int pending; \
904 for (; (for_test) && (tx_ready); (for_post), __port->icount.tx++) { \
905 if (__port->x_char) { \
906 (ch) = __port->x_char; \
908 __port->x_char = 0; \
912 if (uart_tx_stopped(__port)) \
915 if (!kfifo_get(&__tport->xmit_fifo, &(ch))) \
923 pending = kfifo_len(&__tport->xmit_fifo); \
924 if (pending < WAKEUP_CHARS) { \
925 uart_write_wakeup(__port); \
927 if (!((flags) & UART_TX_NOSTOP) && pending == 0) \
928 __port->ops->stop_tx(__port); \
935 * uart_port_tx_limited -- transmit helper for uart_port with count limiting
937 * @ch: variable to store a character to be written to the HW
938 * @count: a limit of characters to send
939 * @tx_ready: can HW accept more data function
940 * @put_char: function to write a character
941 * @tx_done: function to call after the loop is done
943 * This helper transmits characters from the xmit buffer to the hardware using
944 * @put_char(). It does so until @count characters are sent and while @tx_ready
947 * Returns: the number of characters in the xmit buffer when done.
949 * The expression in macro parameters shall be designed as follows:
950 * * **tx_ready:** should evaluate to true if the HW can accept more data to
951 * be sent. This parameter can be %true, which means the HW is always ready.
952 * * **put_char:** shall write @ch to the device of @port.
953 * * **tx_done:** when the write loop is done, this can perform arbitrary
954 * action before potential invocation of ops->stop_tx() happens. If the
955 * driver does not need to do anything, use e.g. ({}).
957 * For all of them, @port->lock is held, interrupts are locally disabled and
958 * the expressions must not sleep.
960 #define uart_port_tx_limited(port, ch, count, tx_ready, put_char, tx_done) ({ \
961 unsigned int __count = (count); \
962 __uart_port_tx(port, ch, 0, tx_ready, put_char, tx_done, __count, \
967 * uart_port_tx_limited_flags -- transmit helper for uart_port with count limiting with flags
969 * @ch: variable to store a character to be written to the HW
970 * @flags: %UART_TX_NOSTOP or similar
971 * @count: a limit of characters to send
972 * @tx_ready: can HW accept more data function
973 * @put_char: function to write a character
974 * @tx_done: function to call after the loop is done
976 * See uart_port_tx_limited() for more details.
978 #define uart_port_tx_limited_flags(port, ch, flags, count, tx_ready, put_char, tx_done) ({ \
979 unsigned int __count = (count); \
980 __uart_port_tx(port, ch, flags, tx_ready, put_char, tx_done, __count, \
985 * uart_port_tx -- transmit helper for uart_port
987 * @ch: variable to store a character to be written to the HW
988 * @tx_ready: can HW accept more data function
989 * @put_char: function to write a character
991 * See uart_port_tx_limited() for more details.
993 #define uart_port_tx(port, ch, tx_ready, put_char) \
994 __uart_port_tx(port, ch, 0, tx_ready, put_char, ({}), true, ({}))
998 * uart_port_tx_flags -- transmit helper for uart_port with flags
1000 * @ch: variable to store a character to be written to the HW
1001 * @flags: %UART_TX_NOSTOP or similar
1002 * @tx_ready: can HW accept more data function
1003 * @put_char: function to write a character
1005 * See uart_port_tx_limited() for more details.
1007 #define uart_port_tx_flags(port, ch, flags, tx_ready, put_char) \
1008 __uart_port_tx(port, ch, flags, tx_ready, put_char, ({}), true, ({}))
1010 * Baud rate helpers.
1012 void uart_update_timeout(struct uart_port *port, unsigned int cflag,
1014 unsigned int uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
1015 const struct ktermios *old, unsigned int min,
1017 unsigned int uart_get_divisor(struct uart_port *port, unsigned int baud);
1020 * Calculates FIFO drain time.
1022 static inline unsigned long uart_fifo_timeout(struct uart_port *port)
1024 u64 fifo_timeout = (u64)READ_ONCE(port->frame_time) * port->fifosize;
1026 /* Add .02 seconds of slop */
1027 fifo_timeout += 20 * NSEC_PER_MSEC;
1029 return max(nsecs_to_jiffies(fifo_timeout), 1UL);
1032 /* Base timer interval for polling */
1033 static inline unsigned long uart_poll_timeout(struct uart_port *port)
1035 unsigned long timeout = uart_fifo_timeout(port);
1037 return timeout > 6 ? (timeout / 2 - 2) : 1;
1043 struct earlycon_device {
1044 struct console *con;
1045 struct uart_port port;
1046 char options[32]; /* e.g., 115200n8 */
1050 struct earlycon_id {
1052 char name_term; /* In case compiler didn't '\0' term name */
1053 char compatible[128];
1054 int (*setup)(struct earlycon_device *, const char *options);
1057 extern const struct earlycon_id __earlycon_table[];
1058 extern const struct earlycon_id __earlycon_table_end[];
1060 #if defined(CONFIG_SERIAL_EARLYCON) && !defined(MODULE)
1061 #define EARLYCON_USED_OR_UNUSED __used
1063 #define EARLYCON_USED_OR_UNUSED __maybe_unused
1066 #define OF_EARLYCON_DECLARE(_name, compat, fn) \
1067 static const struct earlycon_id __UNIQUE_ID(__earlycon_##_name) \
1068 EARLYCON_USED_OR_UNUSED __section("__earlycon_table") \
1069 __aligned(__alignof__(struct earlycon_id)) \
1070 = { .name = __stringify(_name), \
1071 .compatible = compat, \
1074 #define EARLYCON_DECLARE(_name, fn) OF_EARLYCON_DECLARE(_name, "", fn)
1076 int of_setup_earlycon(const struct earlycon_id *match, unsigned long node,
1077 const char *options);
1079 #ifdef CONFIG_SERIAL_EARLYCON
1080 extern bool earlycon_acpi_spcr_enable __initdata;
1081 int setup_earlycon(char *buf);
1083 static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
1084 static inline int setup_earlycon(char *buf) { return 0; }
1087 /* Variant of uart_console_registered() when the console_list_lock is held. */
1088 static inline bool uart_console_registered_locked(struct uart_port *port)
1090 return uart_console(port) && console_is_registered_locked(port->cons);
1093 static inline bool uart_console_registered(struct uart_port *port)
1095 return uart_console(port) && console_is_registered(port->cons);
1098 struct uart_port *uart_get_console(struct uart_port *ports, int nr,
1100 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
1102 void uart_parse_options(const char *options, int *baud, int *parity, int *bits,
1104 int uart_set_options(struct uart_port *port, struct console *co, int baud,
1105 int parity, int bits, int flow);
1106 struct tty_driver *uart_console_device(struct console *co, int *index);
1107 void uart_console_write(struct uart_port *port, const char *s,
1109 void (*putchar)(struct uart_port *, unsigned char));
1112 * Port/driver registration/removal
1114 int uart_register_driver(struct uart_driver *uart);
1115 void uart_unregister_driver(struct uart_driver *uart);
1116 int uart_add_one_port(struct uart_driver *reg, struct uart_port *port);
1117 void uart_remove_one_port(struct uart_driver *reg, struct uart_port *port);
1118 int uart_read_port_properties(struct uart_port *port);
1119 int uart_read_and_validate_port_properties(struct uart_port *port);
1120 bool uart_match_port(const struct uart_port *port1,
1121 const struct uart_port *port2);
1126 int uart_suspend_port(struct uart_driver *reg, struct uart_port *port);
1127 int uart_resume_port(struct uart_driver *reg, struct uart_port *port);
1129 static inline int uart_tx_stopped(struct uart_port *port)
1131 struct tty_struct *tty = port->state->port.tty;
1132 if ((tty && tty->flow.stopped) || port->hw_stopped)
1137 static inline bool uart_cts_enabled(struct uart_port *uport)
1139 return !!(uport->status & UPSTAT_CTS_ENABLE);
1142 static inline bool uart_softcts_mode(struct uart_port *uport)
1144 upstat_t mask = UPSTAT_CTS_ENABLE | UPSTAT_AUTOCTS;
1146 return ((uport->status & mask) == UPSTAT_CTS_ENABLE);
1150 * The following are helper functions for the low level drivers.
1153 void uart_handle_dcd_change(struct uart_port *uport, bool active);
1154 void uart_handle_cts_change(struct uart_port *uport, bool active);
1156 void uart_insert_char(struct uart_port *port, unsigned int status,
1157 unsigned int overrun, u8 ch, u8 flag);
1159 void uart_xchar_out(struct uart_port *uport, int offset);
1161 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
1162 #define SYSRQ_TIMEOUT (HZ * 5)
1164 bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch);
1166 static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
1171 if (ch && time_before(jiffies, port->sysrq)) {
1177 if (uart_try_toggle_sysrq(port, ch))
1185 static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
1190 if (ch && time_before(jiffies, port->sysrq)) {
1192 port->sysrq_ch = ch;
1196 if (uart_try_toggle_sysrq(port, ch))
1204 static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
1208 if (!port->has_sysrq) {
1209 uart_port_unlock(port);
1213 sysrq_ch = port->sysrq_ch;
1216 uart_port_unlock(port);
1219 handle_sysrq(sysrq_ch);
1222 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
1223 unsigned long flags)
1227 if (!port->has_sysrq) {
1228 uart_port_unlock_irqrestore(port, flags);
1232 sysrq_ch = port->sysrq_ch;
1235 uart_port_unlock_irqrestore(port, flags);
1238 handle_sysrq(sysrq_ch);
1240 #else /* CONFIG_MAGIC_SYSRQ_SERIAL */
1241 static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
1245 static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
1249 static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
1251 uart_port_unlock(port);
1253 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
1254 unsigned long flags)
1256 uart_port_unlock_irqrestore(port, flags);
1258 #endif /* CONFIG_MAGIC_SYSRQ_SERIAL */
1261 * We do the SysRQ and SAK checking like this...
1263 static inline int uart_handle_break(struct uart_port *port)
1265 struct uart_state *state = port->state;
1267 if (port->handle_break)
1268 port->handle_break(port);
1270 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
1271 if (port->has_sysrq && uart_console(port)) {
1273 port->sysrq = jiffies + SYSRQ_TIMEOUT;
1279 if (port->flags & UPF_SAK)
1280 do_SAK(state->port.tty);
1285 * UART_ENABLE_MS - determine if port should enable modem status irqs
1287 #define UART_ENABLE_MS(port,cflag) ((port)->flags & UPF_HARDPPS_CD || \
1288 (cflag) & CRTSCTS || \
1289 !((cflag) & CLOCAL))
1291 int uart_get_rs485_mode(struct uart_port *port);
1292 #endif /* LINUX_SERIAL_CORE_H */