linux/include/linux/serial_core.h

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 *  linux/drivers/char/serial_core.h
 *
 *  Copyright (C) 2000 Deep Blue Solutions Ltd.
 */
#ifndef LINUX_SERIAL_CORE_H
#define LINUX_SERIAL_CORE_H

#include <linux/bitops.h>
#include <linux/compiler.h>
#include <linux/console.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/mutex.h>
#include <linux/sysrq.h>
#include <uapi/linux/serial_core.h>

#ifdef CONFIG_SERIAL_CORE_CONSOLE
#define uart_console(port)
#else
#define uart_console
#endif

struct uart_port;
struct serial_struct;
struct serial_port_device;
struct device;
struct gpio_desc;

/**
 * struct uart_ops -- interface between serial_core and the driver
 *
 * This structure describes all the operations that can be done on the
 * physical hardware.
 *
 * @tx_empty: ``unsigned int ()(struct uart_port *port)``
 *
 *	This function tests whether the transmitter fifo and shifter for the
 *	@port is empty. If it is empty, this function should return
 *	%TIOCSER_TEMT, otherwise return 0. If the port does not support this
 *	operation, then it should return %TIOCSER_TEMT.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *	This call must not sleep
 *
 * @set_mctrl: ``void ()(struct uart_port *port, unsigned int mctrl)``
 *
 *	This function sets the modem control lines for @port to the state
 *	described by @mctrl. The relevant bits of @mctrl are:
 *
 *		- %TIOCM_RTS	RTS signal.
 *		- %TIOCM_DTR	DTR signal.
 *		- %TIOCM_OUT1	OUT1 signal.
 *		- %TIOCM_OUT2	OUT2 signal.
 *		- %TIOCM_LOOP	Set the port into loopback mode.
 *
 *	If the appropriate bit is set, the signal should be driven
 *	active.  If the bit is clear, the signal should be driven
 *	inactive.
 *
 *	Locking: @port->lock taken.
 *	Interrupts: locally disabled.
 *	This call must not sleep
 *
 * @get_mctrl: ``unsigned int ()(struct uart_port *port)``
 *
 *	Returns the current state of modem control inputs of @port. The state
 *	of the outputs should not be returned, since the core keeps track of
 *	their state. The state information should include:
 *
 *		- %TIOCM_CAR	state of DCD signal
 *		- %TIOCM_CTS	state of CTS signal
 *		- %TIOCM_DSR	state of DSR signal
 *		- %TIOCM_RI	state of RI signal
 *
 *	The bit is set if the signal is currently driven active.  If
 *	the port does not support CTS, DCD or DSR, the driver should
 *	indicate that the signal is permanently active. If RI is
 *	not available, the signal should not be indicated as active.
 *
 *	Locking: @port->lock taken.
 *	Interrupts: locally disabled.
 *	This call must not sleep
 *
 * @stop_tx: ``void ()(struct uart_port *port)``
 *
 *	Stop transmitting characters. This might be due to the CTS line
 *	becoming inactive or the tty layer indicating we want to stop
 *	transmission due to an %XOFF character.
 *
 *	The driver should stop transmitting characters as soon as possible.
 *
 *	Locking: @port->lock taken.
 *	Interrupts: locally disabled.
 *	This call must not sleep
 *
 * @start_tx: ``void ()(struct uart_port *port)``
 *
 *	Start transmitting characters.
 *
 *	Locking: @port->lock taken.
 *	Interrupts: locally disabled.
 *	This call must not sleep
 *
 * @throttle: ``void ()(struct uart_port *port)``
 *
 *	Notify the serial driver that input buffers for the line discipline are
 *	close to full, and it should somehow signal that no more characters
 *	should be sent to the serial port.
 *	This will be called only if hardware assisted flow control is enabled.
 *
 *	Locking: serialized with @unthrottle() and termios modification by the
 *	tty layer.
 *
 * @unthrottle: ``void ()(struct uart_port *port)``
 *
 *	Notify the serial driver that characters can now be sent to the serial
 *	port without fear of overrunning the input buffers of the line
 *	disciplines.
 *
 *	This will be called only if hardware assisted flow control is enabled.
 *
 *	Locking: serialized with @throttle() and termios modification by the
 *	tty layer.
 *
 * @send_xchar: ``void ()(struct uart_port *port, char ch)``
 *
 *	Transmit a high priority character, even if the port is stopped. This
 *	is used to implement XON/XOFF flow control and tcflow(). If the serial
 *	driver does not implement this function, the tty core will append the
 *	character to the circular buffer and then call start_tx() / stop_tx()
 *	to flush the data out.
 *
 *	Do not transmit if @ch == '\0' (%__DISABLED_CHAR).
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *
 * @start_rx: ``void ()(struct uart_port *port)``
 *
 *	Start receiving characters.
 *
 *	Locking: @port->lock taken.
 *	Interrupts: locally disabled.
 *	This call must not sleep
 *
 * @stop_rx: ``void ()(struct uart_port *port)``
 *
 *	Stop receiving characters; the @port is in the process of being closed.
 *
 *	Locking: @port->lock taken.
 *	Interrupts: locally disabled.
 *	This call must not sleep
 *
 * @enable_ms: ``void ()(struct uart_port *port)``
 *
 *	Enable the modem status interrupts.
 *
 *	This method may be called multiple times. Modem status interrupts
 *	should be disabled when the @shutdown() method is called.
 *
 *	Locking: @port->lock taken.
 *	Interrupts: locally disabled.
 *	This call must not sleep
 *
 * @break_ctl: ``void ()(struct uart_port *port, int ctl)``
 *
 *	Control the transmission of a break signal. If @ctl is nonzero, the
 *	break signal should be transmitted. The signal should be terminated
 *	when another call is made with a zero @ctl.
 *
 *	Locking: caller holds tty_port->mutex
 *
 * @startup: ``int ()(struct uart_port *port)``
 *
 *	Grab any interrupt resources and initialise any low level driver state.
 *	Enable the port for reception. It should not activate RTS nor DTR;
 *	this will be done via a separate call to @set_mctrl().
 *
 *	This method will only be called when the port is initially opened.
 *
 *	Locking: port_sem taken.
 *	Interrupts: globally disabled.
 *
 * @shutdown: ``void ()(struct uart_port *port)``
 *
 *	Disable the @port, disable any break condition that may be in effect,
 *	and free any interrupt resources. It should not disable RTS nor DTR;
 *	this will have already been done via a separate call to @set_mctrl().
 *
 *	Drivers must not access @port->state once this call has completed.
 *
 *	This method will only be called when there are no more users of this
 *	@port.
 *
 *	Locking: port_sem taken.
 *	Interrupts: caller dependent.
 *
 * @flush_buffer: ``void ()(struct uart_port *port)``
 *
 *	Flush any write buffers, reset any DMA state and stop any ongoing DMA
 *	transfers.
 *
 *	This will be called whenever the @port->state->xmit circular buffer is
 *	cleared.
 *
 *	Locking: @port->lock taken.
 *	Interrupts: locally disabled.
 *	This call must not sleep
 *
 * @set_termios: ``void ()(struct uart_port *port, struct ktermios *new,
 *			struct ktermios *old)``
 *
 *	Change the @port parameters, including word length, parity, stop bits.
 *	Update @port->read_status_mask and @port->ignore_status_mask to
 *	indicate the types of events we are interested in receiving. Relevant
 *	ktermios::c_cflag bits are:
 *
 *	- %CSIZE - word size
 *	- %CSTOPB - 2 stop bits
 *	- %PARENB - parity enable
 *	- %PARODD - odd parity (when %PARENB is in force)
 *	- %ADDRB - address bit (changed through uart_port::rs485_config()).
 *	- %CREAD - enable reception of characters (if not set, still receive
 *	  characters from the port, but throw them away).
 *	- %CRTSCTS - if set, enable CTS status change reporting.
 *	- %CLOCAL - if not set, enable modem status change reporting.
 *
 *	Relevant ktermios::c_iflag bits are:
 *
 *	- %INPCK - enable frame and parity error events to be passed to the TTY
 *	  layer.
 *	- %BRKINT / %PARMRK - both of these enable break events to be passed to
 *	  the TTY layer.
 *	- %IGNPAR - ignore parity and framing errors.
 *	- %IGNBRK - ignore break errors. If %IGNPAR is also set, ignore overrun
 *	  errors as well.
 *
 *	The interaction of the ktermios::c_iflag bits is as follows (parity
 *	error given as an example):
 *
 *	============ ======= ======= =========================================
 *	Parity error INPCK   IGNPAR
 *	============ ======= ======= =========================================
 *	n/a	     0	     n/a     character received, marked as %TTY_NORMAL
 *	None	     1	     n/a     character received, marked as %TTY_NORMAL
 *	Yes	     1	     0	     character received, marked as %TTY_PARITY
 *	Yes	     1	     1	     character discarded
 *	============ ======= ======= =========================================
 *
 *	Other flags may be used (eg, xon/xoff characters) if your hardware
 *	supports hardware "soft" flow control.
 *
 *	Locking: caller holds tty_port->mutex
 *	Interrupts: caller dependent.
 *	This call must not sleep
 *
 * @set_ldisc: ``void ()(struct uart_port *port, struct ktermios *termios)``
 *
 *	Notifier for discipline change. See
 *	Documentation/driver-api/tty/tty_ldisc.rst.
 *
 *	Locking: caller holds tty_port->mutex
 *
 * @pm: ``void ()(struct uart_port *port, unsigned int state,
 *		 unsigned int oldstate)``
 *
 *	Perform any power management related activities on the specified @port.
 *	@state indicates the new state (defined by enum uart_pm_state),
 *	@oldstate indicates the previous state.
 *
 *	This function should not be used to grab any resources.
 *
 *	This will be called when the @port is initially opened and finally
 *	closed, except when the @port is also the system console. This will
 *	occur even if %CONFIG_PM is not set.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *
 * @type: ``const char *()(struct uart_port *port)``
 *
 *	Return a pointer to a string constant describing the specified @port,
 *	or return %NULL, in which case the string 'unknown' is substituted.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *
 * @release_port: ``void ()(struct uart_port *port)``
 *
 *	Release any memory and IO region resources currently in use by the
 *	@port.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *
 * @request_port: ``int ()(struct uart_port *port)``
 *
 *	Request any memory and IO region resources required by the port. If any
 *	fail, no resources should be registered when this function returns, and
 *	it should return -%EBUSY on failure.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *
 * @config_port: ``void ()(struct uart_port *port, int type)``
 *
 *	Perform any autoconfiguration steps required for the @port. @type
 *	contains a bit mask of the required configuration. %UART_CONFIG_TYPE
 *	indicates that the port requires detection and identification.
 *	@port->type should be set to the type found, or %PORT_UNKNOWN if no
 *	port was detected.
 *
 *	%UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
 *	which should be probed using standard kernel autoprobing techniques.
 *	This is not necessary on platforms where ports have interrupts
 *	internally hard wired (eg, system on a chip implementations).
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *
 * @verify_port: ``int ()(struct uart_port *port,
 *			struct serial_struct *serinfo)``
 *
 *	Verify the new serial port information contained within @serinfo is
 *	suitable for this port type.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *
 * @ioctl: ``int ()(struct uart_port *port, unsigned int cmd,
 *		unsigned long arg)``
 *
 *	Perform any port specific IOCTLs. IOCTL commands must be defined using
 *	the standard numbering system found in <asm/ioctl.h>.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *
 * @poll_init: ``int ()(struct uart_port *port)``
 *
 *	Called by kgdb to perform the minimal hardware initialization needed to
 *	support @poll_put_char() and @poll_get_char(). Unlike @startup(), this
 *	should not request interrupts.
 *
 *	Locking: %tty_mutex and tty_port->mutex taken.
 *	Interrupts: n/a.
 *
 * @poll_put_char: ``void ()(struct uart_port *port, unsigned char ch)``
 *
 *	Called by kgdb to write a single character @ch directly to the serial
 *	@port. It can and should block until there is space in the TX FIFO.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *	This call must not sleep
 *
 * @poll_get_char: ``int ()(struct uart_port *port)``
 *
 *	Called by kgdb to read a single character directly from the serial
 *	port. If data is available, it should be returned; otherwise the
 *	function should return %NO_POLL_CHAR immediately.
 *
 *	Locking: none.
 *	Interrupts: caller dependent.
 *	This call must not sleep
 */
struct uart_ops {};

#define NO_POLL_CHAR
#define UART_CONFIG_TYPE
#define UART_CONFIG_IRQ

struct uart_icount {};

upf_t;
upstat_t;

struct uart_port {};

/**
 * uart_port_lock - Lock the UART port
 * @up:		Pointer to UART port structure
 */
static inline void uart_port_lock(struct uart_port *up)
{}

/**
 * uart_port_lock_irq - Lock the UART port and disable interrupts
 * @up:		Pointer to UART port structure
 */
static inline void uart_port_lock_irq(struct uart_port *up)
{}

/**
 * uart_port_lock_irqsave - Lock the UART port, save and disable interrupts
 * @up:		Pointer to UART port structure
 * @flags:	Pointer to interrupt flags storage
 */
static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
{}

/**
 * uart_port_trylock - Try to lock the UART port
 * @up:		Pointer to UART port structure
 *
 * Returns: True if lock was acquired, false otherwise
 */
static inline bool uart_port_trylock(struct uart_port *up)
{}

/**
 * uart_port_trylock_irqsave - Try to lock the UART port, save and disable interrupts
 * @up:		Pointer to UART port structure
 * @flags:	Pointer to interrupt flags storage
 *
 * Returns: True if lock was acquired, false otherwise
 */
static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigned long *flags)
{}

/**
 * uart_port_unlock - Unlock the UART port
 * @up:		Pointer to UART port structure
 */
static inline void uart_port_unlock(struct uart_port *up)
{}

/**
 * uart_port_unlock_irq - Unlock the UART port and re-enable interrupts
 * @up:		Pointer to UART port structure
 */
static inline void uart_port_unlock_irq(struct uart_port *up)
{}

/**
 * uart_port_unlock_irqrestore - Unlock the UART port, restore interrupts
 * @up:		Pointer to UART port structure
 * @flags:	The saved interrupt flags for restore
 */
static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
{}

static inline int serial_port_in(struct uart_port *up, int offset)
{}

static inline void serial_port_out(struct uart_port *up, int offset, int value)
{}

/**
 * enum uart_pm_state - power states for UARTs
 * @UART_PM_STATE_ON: UART is powered, up and operational
 * @UART_PM_STATE_OFF: UART is powered off
 * @UART_PM_STATE_UNDEFINED: sentinel
 */
enum uart_pm_state {};

/*
 * This is the state information which is persistent across opens.
 */
struct uart_state {};

#define UART_XMIT_SIZE


/* number of characters left in xmit buffer before we ask for more */
#define WAKEUP_CHARS

/**
 * uart_xmit_advance - Advance xmit buffer and account Tx'ed chars
 * @up: uart_port structure describing the port
 * @chars: number of characters sent
 *
 * This function advances the tail of circular xmit buffer by the number of
 * @chars transmitted and handles accounting of transmitted bytes (into
 * @up's icount.tx).
 */
static inline void uart_xmit_advance(struct uart_port *up, unsigned int chars)
{}

static inline unsigned int uart_fifo_out(struct uart_port *up,
		unsigned char *buf, unsigned int chars)
{}

static inline unsigned int uart_fifo_get(struct uart_port *up,
		unsigned char *ch)
{}

struct module;
struct tty_driver;

struct uart_driver {};

void uart_write_wakeup(struct uart_port *port);

/**
 * enum UART_TX_FLAGS -- flags for uart_port_tx_flags()
 *
 * @UART_TX_NOSTOP: don't call port->ops->stop_tx() on empty buffer
 */
enum UART_TX_FLAGS {};

#define __uart_port_tx(uport, ch, flags, tx_ready, put_char, tx_done,	      \
		       for_test, for_post)

/**
 * uart_port_tx_limited -- transmit helper for uart_port with count limiting
 * @port: uart port
 * @ch: variable to store a character to be written to the HW
 * @count: a limit of characters to send
 * @tx_ready: can HW accept more data function
 * @put_char: function to write a character
 * @tx_done: function to call after the loop is done
 *
 * This helper transmits characters from the xmit buffer to the hardware using
 * @put_char(). It does so until @count characters are sent and while @tx_ready
 * evaluates to true.
 *
 * Returns: the number of characters in the xmit buffer when done.
 *
 * The expression in macro parameters shall be designed as follows:
 *  * **tx_ready:** should evaluate to true if the HW can accept more data to
 *    be sent. This parameter can be %true, which means the HW is always ready.
 *  * **put_char:** shall write @ch to the device of @port.
 *  * **tx_done:** when the write loop is done, this can perform arbitrary
 *    action before potential invocation of ops->stop_tx() happens. If the
 *    driver does not need to do anything, use e.g. ({}).
 *
 * For all of them, @port->lock is held, interrupts are locally disabled and
 * the expressions must not sleep.
 */
#define uart_port_tx_limited(port, ch, count, tx_ready, put_char, tx_done)

/**
 * uart_port_tx_limited_flags -- transmit helper for uart_port with count limiting with flags
 * @port: uart port
 * @ch: variable to store a character to be written to the HW
 * @flags: %UART_TX_NOSTOP or similar
 * @count: a limit of characters to send
 * @tx_ready: can HW accept more data function
 * @put_char: function to write a character
 * @tx_done: function to call after the loop is done
 *
 * See uart_port_tx_limited() for more details.
 */
#define uart_port_tx_limited_flags(port, ch, flags, count, tx_ready, put_char, tx_done)

/**
 * uart_port_tx -- transmit helper for uart_port
 * @port: uart port
 * @ch: variable to store a character to be written to the HW
 * @tx_ready: can HW accept more data function
 * @put_char: function to write a character
 *
 * See uart_port_tx_limited() for more details.
 */
#define uart_port_tx(port, ch, tx_ready, put_char)


/**
 * uart_port_tx_flags -- transmit helper for uart_port with flags
 * @port: uart port
 * @ch: variable to store a character to be written to the HW
 * @flags: %UART_TX_NOSTOP or similar
 * @tx_ready: can HW accept more data function
 * @put_char: function to write a character
 *
 * See uart_port_tx_limited() for more details.
 */
#define uart_port_tx_flags(port, ch, flags, tx_ready, put_char)
/*
 * Baud rate helpers.
 */
void uart_update_timeout(struct uart_port *port, unsigned int cflag,
			 unsigned int baud);
unsigned int uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
				const struct ktermios *old, unsigned int min,
				unsigned int max);
unsigned int uart_get_divisor(struct uart_port *port, unsigned int baud);

/*
 * Calculates FIFO drain time.
 */
static inline unsigned long uart_fifo_timeout(struct uart_port *port)
{}

/* Base timer interval for polling */
static inline unsigned long uart_poll_timeout(struct uart_port *port)
{}

/*
 * Console helpers.
 */
struct earlycon_device {};

struct earlycon_id {};

extern const struct earlycon_id __earlycon_table[];
extern const struct earlycon_id __earlycon_table_end[];

#if defined(CONFIG_SERIAL_EARLYCON) && !defined(MODULE)
#define EARLYCON_USED_OR_UNUSED
#else
#define EARLYCON_USED_OR_UNUSED
#endif

#define OF_EARLYCON_DECLARE(_name, compat, fn)

#define EARLYCON_DECLARE(_name, fn)

int of_setup_earlycon(const struct earlycon_id *match, unsigned long node,
		      const char *options);

#ifdef CONFIG_SERIAL_EARLYCON
extern bool earlycon_acpi_spcr_enable __initdata;
int setup_earlycon(char *buf);
#else
static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
static inline int setup_earlycon(char *buf) { return 0; }
#endif

/* Variant of uart_console_registered() when the console_list_lock is held. */
static inline bool uart_console_registered_locked(struct uart_port *port)
{}

static inline bool uart_console_registered(struct uart_port *port)
{}

struct uart_port *uart_get_console(struct uart_port *ports, int nr,
				   struct console *c);
int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
			char **options);
void uart_parse_options(const char *options, int *baud, int *parity, int *bits,
			int *flow);
int uart_set_options(struct uart_port *port, struct console *co, int baud,
		     int parity, int bits, int flow);
struct tty_driver *uart_console_device(struct console *co, int *index);
void uart_console_write(struct uart_port *port, const char *s,
			unsigned int count,
			void (*putchar)(struct uart_port *, unsigned char));

/*
 * Port/driver registration/removal
 */
int uart_register_driver(struct uart_driver *uart);
void uart_unregister_driver(struct uart_driver *uart);
int uart_add_one_port(struct uart_driver *reg, struct uart_port *port);
void uart_remove_one_port(struct uart_driver *reg, struct uart_port *port);
int uart_read_port_properties(struct uart_port *port);
int uart_read_and_validate_port_properties(struct uart_port *port);
bool uart_match_port(const struct uart_port *port1,
		const struct uart_port *port2);

/*
 * Power Management
 */
int uart_suspend_port(struct uart_driver *reg, struct uart_port *port);
int uart_resume_port(struct uart_driver *reg, struct uart_port *port);

static inline int uart_tx_stopped(struct uart_port *port)
{}

static inline bool uart_cts_enabled(struct uart_port *uport)
{}

static inline bool uart_softcts_mode(struct uart_port *uport)
{}

/*
 * The following are helper functions for the low level drivers.
 */

void uart_handle_dcd_change(struct uart_port *uport, bool active);
void uart_handle_cts_change(struct uart_port *uport, bool active);

void uart_insert_char(struct uart_port *port, unsigned int status,
		      unsigned int overrun, u8 ch, u8 flag);

void uart_xchar_out(struct uart_port *uport, int offset);

#ifdef CONFIG_MAGIC_SYSRQ_SERIAL
#define SYSRQ_TIMEOUT

bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch);

static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
{}

static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
{}

static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
{}

static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
		unsigned long flags)
{}
#else	/* CONFIG_MAGIC_SYSRQ_SERIAL */
static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
{
	return 0;
}
static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
{
	return 0;
}
static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
{
	uart_port_unlock(port);
}
static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
		unsigned long flags)
{
	uart_port_unlock_irqrestore(port, flags);
}
#endif	/* CONFIG_MAGIC_SYSRQ_SERIAL */

/*
 * We do the SysRQ and SAK checking like this...
 */
static inline int uart_handle_break(struct uart_port *port)
{}

/*
 *	UART_ENABLE_MS - determine if port should enable modem status irqs
 */
#define UART_ENABLE_MS(port,cflag)

int uart_get_rs485_mode(struct uart_port *port);
#endif /* LINUX_SERIAL_CORE_H */