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