xref: /linux-6.15/include/linux/serial_core.h (revision 77e73c06)
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/spinlock.h>
15 #include <linux/sched.h>
16 #include <linux/tty.h>
17 #include <linux/mutex.h>
18 #include <linux/sysrq.h>
19 #include <uapi/linux/serial_core.h>
20 
21 #ifdef CONFIG_SERIAL_CORE_CONSOLE
22 #define uart_console(port) \
23 	((port)->cons && (port)->cons->index == (port)->line)
24 #else
25 #define uart_console(port)      ({ (void)port; 0; })
26 #endif
27 
28 struct uart_port;
29 struct serial_struct;
30 struct serial_port_device;
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  * @start_rx: ``void ()(struct uart_port *port)``
145  *
146  *	Start receiving characters.
147  *
148  *	Locking: @port->lock taken.
149  *	Interrupts: locally disabled.
150  *	This call must not sleep
151  *
152  * @stop_rx: ``void ()(struct uart_port *port)``
153  *
154  *	Stop receiving characters; the @port is in the process of being closed.
155  *
156  *	Locking: @port->lock taken.
157  *	Interrupts: locally disabled.
158  *	This call must not sleep
159  *
160  * @enable_ms: ``void ()(struct uart_port *port)``
161  *
162  *	Enable the modem status interrupts.
163  *
164  *	This method may be called multiple times. Modem status interrupts
165  *	should be disabled when the @shutdown() method is called.
166  *
167  *	Locking: @port->lock taken.
168  *	Interrupts: locally disabled.
169  *	This call must not sleep
170  *
171  * @break_ctl: ``void ()(struct uart_port *port, int ctl)``
172  *
173  *	Control the transmission of a break signal. If @ctl is nonzero, the
174  *	break signal should be transmitted. The signal should be terminated
175  *	when another call is made with a zero @ctl.
176  *
177  *	Locking: caller holds tty_port->mutex
178  *
179  * @startup: ``int ()(struct uart_port *port)``
180  *
181  *	Grab any interrupt resources and initialise any low level driver state.
182  *	Enable the port for reception. It should not activate RTS nor DTR;
183  *	this will be done via a separate call to @set_mctrl().
184  *
185  *	This method will only be called when the port is initially opened.
186  *
187  *	Locking: port_sem taken.
188  *	Interrupts: globally disabled.
189  *
190  * @shutdown: ``void ()(struct uart_port *port)``
191  *
192  *	Disable the @port, disable any break condition that may be in effect,
193  *	and free any interrupt resources. It should not disable RTS nor DTR;
194  *	this will have already been done via a separate call to @set_mctrl().
195  *
196  *	Drivers must not access @port->state once this call has completed.
197  *
198  *	This method will only be called when there are no more users of this
199  *	@port.
200  *
201  *	Locking: port_sem taken.
202  *	Interrupts: caller dependent.
203  *
204  * @flush_buffer: ``void ()(struct uart_port *port)``
205  *
206  *	Flush any write buffers, reset any DMA state and stop any ongoing DMA
207  *	transfers.
208  *
209  *	This will be called whenever the @port->state->xmit circular buffer is
210  *	cleared.
211  *
212  *	Locking: @port->lock taken.
213  *	Interrupts: locally disabled.
214  *	This call must not sleep
215  *
216  * @set_termios: ``void ()(struct uart_port *port, struct ktermios *new,
217  *			struct ktermios *old)``
218  *
219  *	Change the @port parameters, including word length, parity, stop bits.
220  *	Update @port->read_status_mask and @port->ignore_status_mask to
221  *	indicate the types of events we are interested in receiving. Relevant
222  *	ktermios::c_cflag bits are:
223  *
224  *	- %CSIZE - word size
225  *	- %CSTOPB - 2 stop bits
226  *	- %PARENB - parity enable
227  *	- %PARODD - odd parity (when %PARENB is in force)
228  *	- %ADDRB - address bit (changed through uart_port::rs485_config()).
229  *	- %CREAD - enable reception of characters (if not set, still receive
230  *	  characters from the port, but throw them away).
231  *	- %CRTSCTS - if set, enable CTS status change reporting.
232  *	- %CLOCAL - if not set, enable modem status change reporting.
233  *
234  *	Relevant ktermios::c_iflag bits are:
235  *
236  *	- %INPCK - enable frame and parity error events to be passed to the TTY
237  *	  layer.
238  *	- %BRKINT / %PARMRK - both of these enable break events to be passed to
239  *	  the TTY layer.
240  *	- %IGNPAR - ignore parity and framing errors.
241  *	- %IGNBRK - ignore break errors. If %IGNPAR is also set, ignore overrun
242  *	  errors as well.
243  *
244  *	The interaction of the ktermios::c_iflag bits is as follows (parity
245  *	error given as an example):
246  *
247  *	============ ======= ======= =========================================
248  *	Parity error INPCK   IGNPAR
249  *	============ ======= ======= =========================================
250  *	n/a	     0	     n/a     character received, marked as %TTY_NORMAL
251  *	None	     1	     n/a     character received, marked as %TTY_NORMAL
252  *	Yes	     1	     0	     character received, marked as %TTY_PARITY
253  *	Yes	     1	     1	     character discarded
254  *	============ ======= ======= =========================================
255  *
256  *	Other flags may be used (eg, xon/xoff characters) if your hardware
257  *	supports hardware "soft" flow control.
258  *
259  *	Locking: caller holds tty_port->mutex
260  *	Interrupts: caller dependent.
261  *	This call must not sleep
262  *
263  * @set_ldisc: ``void ()(struct uart_port *port, struct ktermios *termios)``
264  *
265  *	Notifier for discipline change. See
266  *	Documentation/driver-api/tty/tty_ldisc.rst.
267  *
268  *	Locking: caller holds tty_port->mutex
269  *
270  * @pm: ``void ()(struct uart_port *port, unsigned int state,
271  *		 unsigned int oldstate)``
272  *
273  *	Perform any power management related activities on the specified @port.
274  *	@state indicates the new state (defined by enum uart_pm_state),
275  *	@oldstate indicates the previous state.
276  *
277  *	This function should not be used to grab any resources.
278  *
279  *	This will be called when the @port is initially opened and finally
280  *	closed, except when the @port is also the system console. This will
281  *	occur even if %CONFIG_PM is not set.
282  *
283  *	Locking: none.
284  *	Interrupts: caller dependent.
285  *
286  * @type: ``const char *()(struct uart_port *port)``
287  *
288  *	Return a pointer to a string constant describing the specified @port,
289  *	or return %NULL, in which case the string 'unknown' is substituted.
290  *
291  *	Locking: none.
292  *	Interrupts: caller dependent.
293  *
294  * @release_port: ``void ()(struct uart_port *port)``
295  *
296  *	Release any memory and IO region resources currently in use by the
297  *	@port.
298  *
299  *	Locking: none.
300  *	Interrupts: caller dependent.
301  *
302  * @request_port: ``int ()(struct uart_port *port)``
303  *
304  *	Request any memory and IO region resources required by the port. If any
305  *	fail, no resources should be registered when this function returns, and
306  *	it should return -%EBUSY on failure.
307  *
308  *	Locking: none.
309  *	Interrupts: caller dependent.
310  *
311  * @config_port: ``void ()(struct uart_port *port, int type)``
312  *
313  *	Perform any autoconfiguration steps required for the @port. @type
314  *	contains a bit mask of the required configuration. %UART_CONFIG_TYPE
315  *	indicates that the port requires detection and identification.
316  *	@port->type should be set to the type found, or %PORT_UNKNOWN if no
317  *	port was detected.
318  *
319  *	%UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
320  *	which should be probed using standard kernel autoprobing techniques.
321  *	This is not necessary on platforms where ports have interrupts
322  *	internally hard wired (eg, system on a chip implementations).
323  *
324  *	Locking: none.
325  *	Interrupts: caller dependent.
326  *
327  * @verify_port: ``int ()(struct uart_port *port,
328  *			struct serial_struct *serinfo)``
329  *
330  *	Verify the new serial port information contained within @serinfo is
331  *	suitable for this port type.
332  *
333  *	Locking: none.
334  *	Interrupts: caller dependent.
335  *
336  * @ioctl: ``int ()(struct uart_port *port, unsigned int cmd,
337  *		unsigned long arg)``
338  *
339  *	Perform any port specific IOCTLs. IOCTL commands must be defined using
340  *	the standard numbering system found in <asm/ioctl.h>.
341  *
342  *	Locking: none.
343  *	Interrupts: caller dependent.
344  *
345  * @poll_init: ``int ()(struct uart_port *port)``
346  *
347  *	Called by kgdb to perform the minimal hardware initialization needed to
348  *	support @poll_put_char() and @poll_get_char(). Unlike @startup(), this
349  *	should not request interrupts.
350  *
351  *	Locking: %tty_mutex and tty_port->mutex taken.
352  *	Interrupts: n/a.
353  *
354  * @poll_put_char: ``void ()(struct uart_port *port, unsigned char ch)``
355  *
356  *	Called by kgdb to write a single character @ch directly to the serial
357  *	@port. It can and should block until there is space in the TX FIFO.
358  *
359  *	Locking: none.
360  *	Interrupts: caller dependent.
361  *	This call must not sleep
362  *
363  * @poll_get_char: ``int ()(struct uart_port *port)``
364  *
365  *	Called by kgdb to read a single character directly from the serial
366  *	port. If data is available, it should be returned; otherwise the
367  *	function should return %NO_POLL_CHAR immediately.
368  *
369  *	Locking: none.
370  *	Interrupts: caller dependent.
371  *	This call must not sleep
372  */
373 struct uart_ops {
374 	unsigned int	(*tx_empty)(struct uart_port *);
375 	void		(*set_mctrl)(struct uart_port *, unsigned int mctrl);
376 	unsigned int	(*get_mctrl)(struct uart_port *);
377 	void		(*stop_tx)(struct uart_port *);
378 	void		(*start_tx)(struct uart_port *);
379 	void		(*throttle)(struct uart_port *);
380 	void		(*unthrottle)(struct uart_port *);
381 	void		(*send_xchar)(struct uart_port *, char ch);
382 	void		(*stop_rx)(struct uart_port *);
383 	void		(*start_rx)(struct uart_port *);
384 	void		(*enable_ms)(struct uart_port *);
385 	void		(*break_ctl)(struct uart_port *, int ctl);
386 	int		(*startup)(struct uart_port *);
387 	void		(*shutdown)(struct uart_port *);
388 	void		(*flush_buffer)(struct uart_port *);
389 	void		(*set_termios)(struct uart_port *, struct ktermios *new,
390 				       const struct ktermios *old);
391 	void		(*set_ldisc)(struct uart_port *, struct ktermios *);
392 	void		(*pm)(struct uart_port *, unsigned int state,
393 			      unsigned int oldstate);
394 	const char	*(*type)(struct uart_port *);
395 	void		(*release_port)(struct uart_port *);
396 	int		(*request_port)(struct uart_port *);
397 	void		(*config_port)(struct uart_port *, int);
398 	int		(*verify_port)(struct uart_port *, struct serial_struct *);
399 	int		(*ioctl)(struct uart_port *, unsigned int, unsigned long);
400 #ifdef CONFIG_CONSOLE_POLL
401 	int		(*poll_init)(struct uart_port *);
402 	void		(*poll_put_char)(struct uart_port *, unsigned char);
403 	int		(*poll_get_char)(struct uart_port *);
404 #endif
405 };
406 
407 #define NO_POLL_CHAR		0x00ff0000
408 #define UART_CONFIG_TYPE	(1 << 0)
409 #define UART_CONFIG_IRQ		(1 << 1)
410 
411 struct uart_icount {
412 	__u32	cts;
413 	__u32	dsr;
414 	__u32	rng;
415 	__u32	dcd;
416 	__u32	rx;
417 	__u32	tx;
418 	__u32	frame;
419 	__u32	overrun;
420 	__u32	parity;
421 	__u32	brk;
422 	__u32	buf_overrun;
423 };
424 
425 typedef u64 __bitwise upf_t;
426 typedef unsigned int __bitwise upstat_t;
427 
428 struct uart_port {
429 	spinlock_t		lock;			/* port lock */
430 	unsigned long		iobase;			/* in/out[bwl] */
431 	unsigned char __iomem	*membase;		/* read/write[bwl] */
432 	unsigned int		(*serial_in)(struct uart_port *, int);
433 	void			(*serial_out)(struct uart_port *, int, int);
434 	void			(*set_termios)(struct uart_port *,
435 				               struct ktermios *new,
436 				               const struct ktermios *old);
437 	void			(*set_ldisc)(struct uart_port *,
438 					     struct ktermios *);
439 	unsigned int		(*get_mctrl)(struct uart_port *);
440 	void			(*set_mctrl)(struct uart_port *, unsigned int);
441 	unsigned int		(*get_divisor)(struct uart_port *,
442 					       unsigned int baud,
443 					       unsigned int *frac);
444 	void			(*set_divisor)(struct uart_port *,
445 					       unsigned int baud,
446 					       unsigned int quot,
447 					       unsigned int quot_frac);
448 	int			(*startup)(struct uart_port *port);
449 	void			(*shutdown)(struct uart_port *port);
450 	void			(*throttle)(struct uart_port *port);
451 	void			(*unthrottle)(struct uart_port *port);
452 	int			(*handle_irq)(struct uart_port *);
453 	void			(*pm)(struct uart_port *, unsigned int state,
454 				      unsigned int old);
455 	void			(*handle_break)(struct uart_port *);
456 	int			(*rs485_config)(struct uart_port *,
457 						struct ktermios *termios,
458 						struct serial_rs485 *rs485);
459 	int			(*iso7816_config)(struct uart_port *,
460 						  struct serial_iso7816 *iso7816);
461 	unsigned int		ctrl_id;		/* optional serial core controller id */
462 	unsigned int		port_id;		/* optional serial core port id */
463 	unsigned int		irq;			/* irq number */
464 	unsigned long		irqflags;		/* irq flags  */
465 	unsigned int		uartclk;		/* base uart clock */
466 	unsigned int		fifosize;		/* tx fifo size */
467 	unsigned char		x_char;			/* xon/xoff char */
468 	unsigned char		regshift;		/* reg offset shift */
469 
470 	unsigned char		iotype;			/* io access style */
471 
472 #define UPIO_UNKNOWN		((unsigned char)~0U)	/* UCHAR_MAX */
473 #define UPIO_PORT		(SERIAL_IO_PORT)	/* 8b I/O port access */
474 #define UPIO_HUB6		(SERIAL_IO_HUB6)	/* Hub6 ISA card */
475 #define UPIO_MEM		(SERIAL_IO_MEM)		/* driver-specific */
476 #define UPIO_MEM32		(SERIAL_IO_MEM32)	/* 32b little endian */
477 #define UPIO_AU			(SERIAL_IO_AU)		/* Au1x00 and RT288x type IO */
478 #define UPIO_TSI		(SERIAL_IO_TSI)		/* Tsi108/109 type IO */
479 #define UPIO_MEM32BE		(SERIAL_IO_MEM32BE)	/* 32b big endian */
480 #define UPIO_MEM16		(SERIAL_IO_MEM16)	/* 16b little endian */
481 
482 	unsigned char		quirks;			/* internal quirks */
483 
484 	/* internal quirks must be updated while holding port mutex */
485 #define UPQ_NO_TXEN_TEST	BIT(0)
486 
487 	unsigned int		read_status_mask;	/* driver specific */
488 	unsigned int		ignore_status_mask;	/* driver specific */
489 	struct uart_state	*state;			/* pointer to parent state */
490 	struct uart_icount	icount;			/* statistics */
491 
492 	struct console		*cons;			/* struct console, if any */
493 	/* flags must be updated while holding port mutex */
494 	upf_t			flags;
495 
496 	/*
497 	 * These flags must be equivalent to the flags defined in
498 	 * include/uapi/linux/tty_flags.h which are the userspace definitions
499 	 * assigned from the serial_struct flags in uart_set_info()
500 	 * [for bit definitions in the UPF_CHANGE_MASK]
501 	 *
502 	 * Bits [0..ASYNCB_LAST_USER] are userspace defined/visible/changeable
503 	 * The remaining bits are serial-core specific and not modifiable by
504 	 * userspace.
505 	 */
506 #define UPF_FOURPORT		((__force upf_t) ASYNC_FOURPORT       /* 1  */ )
507 #define UPF_SAK			((__force upf_t) ASYNC_SAK            /* 2  */ )
508 #define UPF_SPD_HI		((__force upf_t) ASYNC_SPD_HI         /* 4  */ )
509 #define UPF_SPD_VHI		((__force upf_t) ASYNC_SPD_VHI        /* 5  */ )
510 #define UPF_SPD_CUST		((__force upf_t) ASYNC_SPD_CUST   /* 0x0030 */ )
511 #define UPF_SPD_WARP		((__force upf_t) ASYNC_SPD_WARP   /* 0x1010 */ )
512 #define UPF_SPD_MASK		((__force upf_t) ASYNC_SPD_MASK   /* 0x1030 */ )
513 #define UPF_SKIP_TEST		((__force upf_t) ASYNC_SKIP_TEST      /* 6  */ )
514 #define UPF_AUTO_IRQ		((__force upf_t) ASYNC_AUTO_IRQ       /* 7  */ )
515 #define UPF_HARDPPS_CD		((__force upf_t) ASYNC_HARDPPS_CD     /* 11 */ )
516 #define UPF_SPD_SHI		((__force upf_t) ASYNC_SPD_SHI        /* 12 */ )
517 #define UPF_LOW_LATENCY		((__force upf_t) ASYNC_LOW_LATENCY    /* 13 */ )
518 #define UPF_BUGGY_UART		((__force upf_t) ASYNC_BUGGY_UART     /* 14 */ )
519 #define UPF_MAGIC_MULTIPLIER	((__force upf_t) ASYNC_MAGIC_MULTIPLIER /* 16 */ )
520 
521 #define UPF_NO_THRE_TEST	((__force upf_t) BIT_ULL(19))
522 /* Port has hardware-assisted h/w flow control */
523 #define UPF_AUTO_CTS		((__force upf_t) BIT_ULL(20))
524 #define UPF_AUTO_RTS		((__force upf_t) BIT_ULL(21))
525 #define UPF_HARD_FLOW		((__force upf_t) (UPF_AUTO_CTS | UPF_AUTO_RTS))
526 /* Port has hardware-assisted s/w flow control */
527 #define UPF_SOFT_FLOW		((__force upf_t) BIT_ULL(22))
528 #define UPF_CONS_FLOW		((__force upf_t) BIT_ULL(23))
529 #define UPF_SHARE_IRQ		((__force upf_t) BIT_ULL(24))
530 #define UPF_EXAR_EFR		((__force upf_t) BIT_ULL(25))
531 #define UPF_BUG_THRE		((__force upf_t) BIT_ULL(26))
532 /* The exact UART type is known and should not be probed.  */
533 #define UPF_FIXED_TYPE		((__force upf_t) BIT_ULL(27))
534 #define UPF_BOOT_AUTOCONF	((__force upf_t) BIT_ULL(28))
535 #define UPF_FIXED_PORT		((__force upf_t) BIT_ULL(29))
536 #define UPF_DEAD		((__force upf_t) BIT_ULL(30))
537 #define UPF_IOREMAP		((__force upf_t) BIT_ULL(31))
538 #define UPF_FULL_PROBE		((__force upf_t) BIT_ULL(32))
539 
540 #define __UPF_CHANGE_MASK	0x17fff
541 #define UPF_CHANGE_MASK		((__force upf_t) __UPF_CHANGE_MASK)
542 #define UPF_USR_MASK		((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
543 
544 #if __UPF_CHANGE_MASK > ASYNC_FLAGS
545 #error Change mask not equivalent to userspace-visible bit defines
546 #endif
547 
548 	/*
549 	 * Must hold termios_rwsem, port mutex and port lock to change;
550 	 * can hold any one lock to read.
551 	 */
552 	upstat_t		status;
553 
554 #define UPSTAT_CTS_ENABLE	((__force upstat_t) (1 << 0))
555 #define UPSTAT_DCD_ENABLE	((__force upstat_t) (1 << 1))
556 #define UPSTAT_AUTORTS		((__force upstat_t) (1 << 2))
557 #define UPSTAT_AUTOCTS		((__force upstat_t) (1 << 3))
558 #define UPSTAT_AUTOXOFF		((__force upstat_t) (1 << 4))
559 #define UPSTAT_SYNC_FIFO	((__force upstat_t) (1 << 5))
560 
561 	bool			hw_stopped;		/* sw-assisted CTS flow state */
562 	unsigned int		mctrl;			/* current modem ctrl settings */
563 	unsigned int		frame_time;		/* frame timing in ns */
564 	unsigned int		type;			/* port type */
565 	const struct uart_ops	*ops;
566 	unsigned int		custom_divisor;
567 	unsigned int		line;			/* port index */
568 	unsigned int		minor;
569 	resource_size_t		mapbase;		/* for ioremap */
570 	resource_size_t		mapsize;
571 	struct device		*dev;			/* serial port physical parent device */
572 	struct serial_port_device *port_dev;		/* serial core port device */
573 
574 	unsigned long		sysrq;			/* sysrq timeout */
575 	u8			sysrq_ch;		/* char for sysrq */
576 	unsigned char		has_sysrq;
577 	unsigned char		sysrq_seq;		/* index in sysrq_toggle_seq */
578 
579 	unsigned char		hub6;			/* this should be in the 8250 driver */
580 	unsigned char		suspended;
581 	unsigned char		console_reinit;
582 	const char		*name;			/* port name */
583 	struct attribute_group	*attr_group;		/* port specific attributes */
584 	const struct attribute_group **tty_groups;	/* all attributes (serial core use only) */
585 	struct serial_rs485     rs485;
586 	struct serial_rs485	rs485_supported;	/* Supported mask for serial_rs485 */
587 	struct gpio_desc	*rs485_term_gpio;	/* enable RS485 bus termination */
588 	struct gpio_desc	*rs485_rx_during_tx_gpio; /* Output GPIO that sets the state of RS485 RX during TX */
589 	struct serial_iso7816   iso7816;
590 	void			*private_data;		/* generic platform data pointer */
591 };
592 
593 /*
594  * Only for console->device_lock()/_unlock() callbacks and internal
595  * port lock wrapper synchronization.
596  */
597 static inline void __uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
598 {
599 	spin_lock_irqsave(&up->lock, *flags);
600 }
601 
602 /*
603  * Only for console->device_lock()/_unlock() callbacks and internal
604  * port lock wrapper synchronization.
605  */
606 static inline void __uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
607 {
608 	spin_unlock_irqrestore(&up->lock, flags);
609 }
610 
611 /**
612  * uart_port_lock - Lock the UART port
613  * @up:		Pointer to UART port structure
614  */
615 static inline void uart_port_lock(struct uart_port *up)
616 {
617 	spin_lock(&up->lock);
618 }
619 
620 /**
621  * uart_port_lock_irq - Lock the UART port and disable interrupts
622  * @up:		Pointer to UART port structure
623  */
624 static inline void uart_port_lock_irq(struct uart_port *up)
625 {
626 	spin_lock_irq(&up->lock);
627 }
628 
629 /**
630  * uart_port_lock_irqsave - Lock the UART port, save and disable interrupts
631  * @up:		Pointer to UART port structure
632  * @flags:	Pointer to interrupt flags storage
633  */
634 static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
635 {
636 	spin_lock_irqsave(&up->lock, *flags);
637 }
638 
639 /**
640  * uart_port_trylock - Try to lock the UART port
641  * @up:		Pointer to UART port structure
642  *
643  * Returns: True if lock was acquired, false otherwise
644  */
645 static inline bool uart_port_trylock(struct uart_port *up)
646 {
647 	return spin_trylock(&up->lock);
648 }
649 
650 /**
651  * uart_port_trylock_irqsave - Try to lock the UART port, save and disable interrupts
652  * @up:		Pointer to UART port structure
653  * @flags:	Pointer to interrupt flags storage
654  *
655  * Returns: True if lock was acquired, false otherwise
656  */
657 static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigned long *flags)
658 {
659 	return spin_trylock_irqsave(&up->lock, *flags);
660 }
661 
662 /**
663  * uart_port_unlock - Unlock the UART port
664  * @up:		Pointer to UART port structure
665  */
666 static inline void uart_port_unlock(struct uart_port *up)
667 {
668 	spin_unlock(&up->lock);
669 }
670 
671 /**
672  * uart_port_unlock_irq - Unlock the UART port and re-enable interrupts
673  * @up:		Pointer to UART port structure
674  */
675 static inline void uart_port_unlock_irq(struct uart_port *up)
676 {
677 	spin_unlock_irq(&up->lock);
678 }
679 
680 /**
681  * uart_port_unlock_irqrestore - Unlock the UART port, restore interrupts
682  * @up:		Pointer to UART port structure
683  * @flags:	The saved interrupt flags for restore
684  */
685 static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
686 {
687 	spin_unlock_irqrestore(&up->lock, flags);
688 }
689 
690 static inline int serial_port_in(struct uart_port *up, int offset)
691 {
692 	return up->serial_in(up, offset);
693 }
694 
695 static inline void serial_port_out(struct uart_port *up, int offset, int value)
696 {
697 	up->serial_out(up, offset, value);
698 }
699 
700 /**
701  * enum uart_pm_state - power states for UARTs
702  * @UART_PM_STATE_ON: UART is powered, up and operational
703  * @UART_PM_STATE_OFF: UART is powered off
704  * @UART_PM_STATE_UNDEFINED: sentinel
705  */
706 enum uart_pm_state {
707 	UART_PM_STATE_ON = 0,
708 	UART_PM_STATE_OFF = 3, /* number taken from ACPI */
709 	UART_PM_STATE_UNDEFINED,
710 };
711 
712 /*
713  * This is the state information which is persistent across opens.
714  */
715 struct uart_state {
716 	struct tty_port		port;
717 
718 	enum uart_pm_state	pm_state;
719 
720 	atomic_t		refcount;
721 	wait_queue_head_t	remove_wait;
722 	struct uart_port	*uart_port;
723 };
724 
725 #define UART_XMIT_SIZE	PAGE_SIZE
726 
727 
728 /* number of characters left in xmit buffer before we ask for more */
729 #define WAKEUP_CHARS		256
730 
731 /**
732  * uart_xmit_advance - Advance xmit buffer and account Tx'ed chars
733  * @up: uart_port structure describing the port
734  * @chars: number of characters sent
735  *
736  * This function advances the tail of circular xmit buffer by the number of
737  * @chars transmitted and handles accounting of transmitted bytes (into
738  * @up's icount.tx).
739  */
740 static inline void uart_xmit_advance(struct uart_port *up, unsigned int chars)
741 {
742 	struct tty_port *tport = &up->state->port;
743 
744 	kfifo_skip_count(&tport->xmit_fifo, chars);
745 	up->icount.tx += chars;
746 }
747 
748 static inline unsigned int uart_fifo_out(struct uart_port *up,
749 		unsigned char *buf, unsigned int chars)
750 {
751 	struct tty_port *tport = &up->state->port;
752 
753 	chars = kfifo_out(&tport->xmit_fifo, buf, chars);
754 	up->icount.tx += chars;
755 
756 	return chars;
757 }
758 
759 static inline unsigned int uart_fifo_get(struct uart_port *up,
760 		unsigned char *ch)
761 {
762 	struct tty_port *tport = &up->state->port;
763 	unsigned int chars;
764 
765 	chars = kfifo_get(&tport->xmit_fifo, ch);
766 	up->icount.tx += chars;
767 
768 	return chars;
769 }
770 
771 struct module;
772 struct tty_driver;
773 
774 struct uart_driver {
775 	struct module		*owner;
776 	const char		*driver_name;
777 	const char		*dev_name;
778 	int			 major;
779 	int			 minor;
780 	int			 nr;
781 	struct console		*cons;
782 
783 	/*
784 	 * these are private; the low level driver should not
785 	 * touch these; they should be initialised to NULL
786 	 */
787 	struct uart_state	*state;
788 	struct tty_driver	*tty_driver;
789 };
790 
791 void uart_write_wakeup(struct uart_port *port);
792 
793 /**
794  * enum UART_TX_FLAGS -- flags for uart_port_tx_flags()
795  *
796  * @UART_TX_NOSTOP: don't call port->ops->stop_tx() on empty buffer
797  */
798 enum UART_TX_FLAGS {
799 	UART_TX_NOSTOP = BIT(0),
800 };
801 
802 #define __uart_port_tx(uport, ch, flags, tx_ready, put_char, tx_done,	      \
803 		       for_test, for_post)				      \
804 ({									      \
805 	struct uart_port *__port = (uport);				      \
806 	struct tty_port *__tport = &__port->state->port;		      \
807 	unsigned int pending;						      \
808 									      \
809 	for (; (for_test) && (tx_ready); (for_post), __port->icount.tx++) {   \
810 		if (__port->x_char) {					      \
811 			(ch) = __port->x_char;				      \
812 			(put_char);					      \
813 			__port->x_char = 0;				      \
814 			continue;					      \
815 		}							      \
816 									      \
817 		if (uart_tx_stopped(__port))				      \
818 			break;						      \
819 									      \
820 		if (!kfifo_get(&__tport->xmit_fifo, &(ch)))		      \
821 			break;						      \
822 									      \
823 		(put_char);						      \
824 	}								      \
825 									      \
826 	(tx_done);							      \
827 									      \
828 	pending = kfifo_len(&__tport->xmit_fifo);			      \
829 	if (pending < WAKEUP_CHARS) {					      \
830 		uart_write_wakeup(__port);				      \
831 									      \
832 		if (!((flags) & UART_TX_NOSTOP) && pending == 0)	      \
833 			__port->ops->stop_tx(__port);			      \
834 	}								      \
835 									      \
836 	pending;							      \
837 })
838 
839 /**
840  * uart_port_tx_limited -- transmit helper for uart_port with count limiting
841  * @port: uart port
842  * @ch: variable to store a character to be written to the HW
843  * @count: a limit of characters to send
844  * @tx_ready: can HW accept more data function
845  * @put_char: function to write a character
846  * @tx_done: function to call after the loop is done
847  *
848  * This helper transmits characters from the xmit buffer to the hardware using
849  * @put_char(). It does so until @count characters are sent and while @tx_ready
850  * evaluates to true.
851  *
852  * Returns: the number of characters in the xmit buffer when done.
853  *
854  * The expression in macro parameters shall be designed as follows:
855  *  * **tx_ready:** should evaluate to true if the HW can accept more data to
856  *    be sent. This parameter can be %true, which means the HW is always ready.
857  *  * **put_char:** shall write @ch to the device of @port.
858  *  * **tx_done:** when the write loop is done, this can perform arbitrary
859  *    action before potential invocation of ops->stop_tx() happens. If the
860  *    driver does not need to do anything, use e.g. ({}).
861  *
862  * For all of them, @port->lock is held, interrupts are locally disabled and
863  * the expressions must not sleep.
864  */
865 #define uart_port_tx_limited(port, ch, count, tx_ready, put_char, tx_done) ({ \
866 	unsigned int __count = (count);					      \
867 	__uart_port_tx(port, ch, 0, tx_ready, put_char, tx_done, __count,     \
868 			__count--);					      \
869 })
870 
871 /**
872  * uart_port_tx_limited_flags -- transmit helper for uart_port with count limiting with flags
873  * @port: uart port
874  * @ch: variable to store a character to be written to the HW
875  * @flags: %UART_TX_NOSTOP or similar
876  * @count: a limit of characters to send
877  * @tx_ready: can HW accept more data function
878  * @put_char: function to write a character
879  * @tx_done: function to call after the loop is done
880  *
881  * See uart_port_tx_limited() for more details.
882  */
883 #define uart_port_tx_limited_flags(port, ch, flags, count, tx_ready, put_char, tx_done) ({ \
884 	unsigned int __count = (count);							   \
885 	__uart_port_tx(port, ch, flags, tx_ready, put_char, tx_done, __count,		   \
886 			__count--);							   \
887 })
888 
889 /**
890  * uart_port_tx -- transmit helper for uart_port
891  * @port: uart port
892  * @ch: variable to store a character to be written to the HW
893  * @tx_ready: can HW accept more data function
894  * @put_char: function to write a character
895  *
896  * See uart_port_tx_limited() for more details.
897  */
898 #define uart_port_tx(port, ch, tx_ready, put_char)			\
899 	__uart_port_tx(port, ch, 0, tx_ready, put_char, ({}), true, ({}))
900 
901 
902 /**
903  * uart_port_tx_flags -- transmit helper for uart_port with flags
904  * @port: uart port
905  * @ch: variable to store a character to be written to the HW
906  * @flags: %UART_TX_NOSTOP or similar
907  * @tx_ready: can HW accept more data function
908  * @put_char: function to write a character
909  *
910  * See uart_port_tx_limited() for more details.
911  */
912 #define uart_port_tx_flags(port, ch, flags, tx_ready, put_char)		\
913 	__uart_port_tx(port, ch, flags, tx_ready, put_char, ({}), true, ({}))
914 /*
915  * Baud rate helpers.
916  */
917 void uart_update_timeout(struct uart_port *port, unsigned int cflag,
918 			 unsigned int baud);
919 unsigned int uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
920 				const struct ktermios *old, unsigned int min,
921 				unsigned int max);
922 unsigned int uart_get_divisor(struct uart_port *port, unsigned int baud);
923 
924 /*
925  * Calculates FIFO drain time.
926  */
927 static inline unsigned long uart_fifo_timeout(struct uart_port *port)
928 {
929 	u64 fifo_timeout = (u64)READ_ONCE(port->frame_time) * port->fifosize;
930 
931 	/* Add .02 seconds of slop */
932 	fifo_timeout += 20 * NSEC_PER_MSEC;
933 
934 	return max(nsecs_to_jiffies(fifo_timeout), 1UL);
935 }
936 
937 /* Base timer interval for polling */
938 static inline unsigned long uart_poll_timeout(struct uart_port *port)
939 {
940 	unsigned long timeout = uart_fifo_timeout(port);
941 
942 	return timeout > 6 ? (timeout / 2 - 2) : 1;
943 }
944 
945 /*
946  * Console helpers.
947  */
948 struct earlycon_device {
949 	struct console *con;
950 	struct uart_port port;
951 	char options[32];		/* e.g., 115200n8 */
952 	unsigned int baud;
953 };
954 
955 struct earlycon_id {
956 	char	name[15];
957 	char	name_term;	/* In case compiler didn't '\0' term name */
958 	char	compatible[128];
959 	int	(*setup)(struct earlycon_device *, const char *options);
960 };
961 
962 extern const struct earlycon_id __earlycon_table[];
963 extern const struct earlycon_id __earlycon_table_end[];
964 
965 #if defined(CONFIG_SERIAL_EARLYCON) && !defined(MODULE)
966 #define EARLYCON_USED_OR_UNUSED	__used
967 #else
968 #define EARLYCON_USED_OR_UNUSED	__maybe_unused
969 #endif
970 
971 #define OF_EARLYCON_DECLARE(_name, compat, fn)				\
972 	static const struct earlycon_id __UNIQUE_ID(__earlycon_##_name) \
973 		EARLYCON_USED_OR_UNUSED  __section("__earlycon_table")  \
974 		__aligned(__alignof__(struct earlycon_id))		\
975 		= { .name = __stringify(_name),				\
976 		    .compatible = compat,				\
977 		    .setup = fn }
978 
979 #define EARLYCON_DECLARE(_name, fn)	OF_EARLYCON_DECLARE(_name, "", fn)
980 
981 int of_setup_earlycon(const struct earlycon_id *match, unsigned long node,
982 		      const char *options);
983 
984 #ifdef CONFIG_SERIAL_EARLYCON
985 extern bool earlycon_acpi_spcr_enable __initdata;
986 int setup_earlycon(char *buf);
987 #else
988 static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
989 static inline int setup_earlycon(char *buf) { return 0; }
990 #endif
991 
992 /* Variant of uart_console_registered() when the console_list_lock is held. */
993 static inline bool uart_console_registered_locked(struct uart_port *port)
994 {
995 	return uart_console(port) && console_is_registered_locked(port->cons);
996 }
997 
998 static inline bool uart_console_registered(struct uart_port *port)
999 {
1000 	return uart_console(port) && console_is_registered(port->cons);
1001 }
1002 
1003 struct uart_port *uart_get_console(struct uart_port *ports, int nr,
1004 				   struct console *c);
1005 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
1006 			char **options);
1007 void uart_parse_options(const char *options, int *baud, int *parity, int *bits,
1008 			int *flow);
1009 int uart_set_options(struct uart_port *port, struct console *co, int baud,
1010 		     int parity, int bits, int flow);
1011 struct tty_driver *uart_console_device(struct console *co, int *index);
1012 void uart_console_write(struct uart_port *port, const char *s,
1013 			unsigned int count,
1014 			void (*putchar)(struct uart_port *, unsigned char));
1015 
1016 /*
1017  * Port/driver registration/removal
1018  */
1019 int uart_register_driver(struct uart_driver *uart);
1020 void uart_unregister_driver(struct uart_driver *uart);
1021 int uart_add_one_port(struct uart_driver *reg, struct uart_port *port);
1022 void uart_remove_one_port(struct uart_driver *reg, struct uart_port *port);
1023 int uart_read_port_properties(struct uart_port *port);
1024 int uart_read_and_validate_port_properties(struct uart_port *port);
1025 bool uart_match_port(const struct uart_port *port1,
1026 		const struct uart_port *port2);
1027 
1028 /*
1029  * Power Management
1030  */
1031 int uart_suspend_port(struct uart_driver *reg, struct uart_port *port);
1032 int uart_resume_port(struct uart_driver *reg, struct uart_port *port);
1033 
1034 static inline int uart_tx_stopped(struct uart_port *port)
1035 {
1036 	struct tty_struct *tty = port->state->port.tty;
1037 	if ((tty && tty->flow.stopped) || port->hw_stopped)
1038 		return 1;
1039 	return 0;
1040 }
1041 
1042 static inline bool uart_cts_enabled(struct uart_port *uport)
1043 {
1044 	return !!(uport->status & UPSTAT_CTS_ENABLE);
1045 }
1046 
1047 static inline bool uart_softcts_mode(struct uart_port *uport)
1048 {
1049 	upstat_t mask = UPSTAT_CTS_ENABLE | UPSTAT_AUTOCTS;
1050 
1051 	return ((uport->status & mask) == UPSTAT_CTS_ENABLE);
1052 }
1053 
1054 /*
1055  * The following are helper functions for the low level drivers.
1056  */
1057 
1058 void uart_handle_dcd_change(struct uart_port *uport, bool active);
1059 void uart_handle_cts_change(struct uart_port *uport, bool active);
1060 
1061 void uart_insert_char(struct uart_port *port, unsigned int status,
1062 		      unsigned int overrun, u8 ch, u8 flag);
1063 
1064 void uart_xchar_out(struct uart_port *uport, int offset);
1065 
1066 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
1067 #define SYSRQ_TIMEOUT	(HZ * 5)
1068 
1069 bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch);
1070 
1071 static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
1072 {
1073 	if (!port->sysrq)
1074 		return 0;
1075 
1076 	if (ch && time_before(jiffies, port->sysrq)) {
1077 		if (sysrq_mask()) {
1078 			handle_sysrq(ch);
1079 			port->sysrq = 0;
1080 			return 1;
1081 		}
1082 		if (uart_try_toggle_sysrq(port, ch))
1083 			return 1;
1084 	}
1085 	port->sysrq = 0;
1086 
1087 	return 0;
1088 }
1089 
1090 static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
1091 {
1092 	if (!port->sysrq)
1093 		return 0;
1094 
1095 	if (ch && time_before(jiffies, port->sysrq)) {
1096 		if (sysrq_mask()) {
1097 			port->sysrq_ch = ch;
1098 			port->sysrq = 0;
1099 			return 1;
1100 		}
1101 		if (uart_try_toggle_sysrq(port, ch))
1102 			return 1;
1103 	}
1104 	port->sysrq = 0;
1105 
1106 	return 0;
1107 }
1108 
1109 static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
1110 {
1111 	u8 sysrq_ch;
1112 
1113 	if (!port->has_sysrq) {
1114 		uart_port_unlock(port);
1115 		return;
1116 	}
1117 
1118 	sysrq_ch = port->sysrq_ch;
1119 	port->sysrq_ch = 0;
1120 
1121 	uart_port_unlock(port);
1122 
1123 	if (sysrq_ch)
1124 		handle_sysrq(sysrq_ch);
1125 }
1126 
1127 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
1128 		unsigned long flags)
1129 {
1130 	u8 sysrq_ch;
1131 
1132 	if (!port->has_sysrq) {
1133 		uart_port_unlock_irqrestore(port, flags);
1134 		return;
1135 	}
1136 
1137 	sysrq_ch = port->sysrq_ch;
1138 	port->sysrq_ch = 0;
1139 
1140 	uart_port_unlock_irqrestore(port, flags);
1141 
1142 	if (sysrq_ch)
1143 		handle_sysrq(sysrq_ch);
1144 }
1145 #else	/* CONFIG_MAGIC_SYSRQ_SERIAL */
1146 static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
1147 {
1148 	return 0;
1149 }
1150 static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
1151 {
1152 	return 0;
1153 }
1154 static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
1155 {
1156 	uart_port_unlock(port);
1157 }
1158 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
1159 		unsigned long flags)
1160 {
1161 	uart_port_unlock_irqrestore(port, flags);
1162 }
1163 #endif	/* CONFIG_MAGIC_SYSRQ_SERIAL */
1164 
1165 /*
1166  * We do the SysRQ and SAK checking like this...
1167  */
1168 static inline int uart_handle_break(struct uart_port *port)
1169 {
1170 	struct uart_state *state = port->state;
1171 
1172 	if (port->handle_break)
1173 		port->handle_break(port);
1174 
1175 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
1176 	if (port->has_sysrq && uart_console(port)) {
1177 		if (!port->sysrq) {
1178 			port->sysrq = jiffies + SYSRQ_TIMEOUT;
1179 			return 1;
1180 		}
1181 		port->sysrq = 0;
1182 	}
1183 #endif
1184 	if (port->flags & UPF_SAK)
1185 		do_SAK(state->port.tty);
1186 	return 0;
1187 }
1188 
1189 /*
1190  *	UART_ENABLE_MS - determine if port should enable modem status irqs
1191  */
1192 #define UART_ENABLE_MS(port,cflag)	((port)->flags & UPF_HARDPPS_CD || \
1193 					 (cflag) & CRTSCTS || \
1194 					 !((cflag) & CLOCAL))
1195 
1196 int uart_get_rs485_mode(struct uart_port *port);
1197 #endif /* LINUX_SERIAL_CORE_H */
1198