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