Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *  linux/drivers/char/serial_core.h
0004  *
0005  *  Copyright (C) 2000 Deep Blue Solutions Ltd.
0006  */
0007 #ifndef LINUX_SERIAL_CORE_H
0008 #define LINUX_SERIAL_CORE_H
0009 
0010 #include <linux/bitops.h>
0011 #include <linux/compiler.h>
0012 #include <linux/console.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/circ_buf.h>
0015 #include <linux/spinlock.h>
0016 #include <linux/sched.h>
0017 #include <linux/tty.h>
0018 #include <linux/mutex.h>
0019 #include <linux/sysrq.h>
0020 #include <uapi/linux/serial_core.h>
0021 
0022 #ifdef CONFIG_SERIAL_CORE_CONSOLE
0023 #define uart_console(port) \
0024     ((port)->cons && (port)->cons->index == (port)->line)
0025 #else
0026 #define uart_console(port)      ({ (void)port; 0; })
0027 #endif
0028 
0029 struct uart_port;
0030 struct serial_struct;
0031 struct device;
0032 struct gpio_desc;
0033 
0034 /**
0035  * struct uart_ops -- interface between serial_core and the driver
0036  *
0037  * This structure describes all the operations that can be done on the
0038  * physical hardware.
0039  *
0040  * @tx_empty: ``unsigned int ()(struct uart_port *port)``
0041  *
0042  *  This function tests whether the transmitter fifo and shifter for the
0043  *  @port is empty. If it is empty, this function should return
0044  *  %TIOCSER_TEMT, otherwise return 0. If the port does not support this
0045  *  operation, then it should return %TIOCSER_TEMT.
0046  *
0047  *  Locking: none.
0048  *  Interrupts: caller dependent.
0049  *  This call must not sleep
0050  *
0051  * @set_mctrl: ``void ()(struct uart_port *port, unsigned int mctrl)``
0052  *
0053  *  This function sets the modem control lines for @port to the state
0054  *  described by @mctrl. The relevant bits of @mctrl are:
0055  *
0056  *      - %TIOCM_RTS    RTS signal.
0057  *      - %TIOCM_DTR    DTR signal.
0058  *      - %TIOCM_OUT1   OUT1 signal.
0059  *      - %TIOCM_OUT2   OUT2 signal.
0060  *      - %TIOCM_LOOP   Set the port into loopback mode.
0061  *
0062  *  If the appropriate bit is set, the signal should be driven
0063  *  active.  If the bit is clear, the signal should be driven
0064  *  inactive.
0065  *
0066  *  Locking: @port->lock taken.
0067  *  Interrupts: locally disabled.
0068  *  This call must not sleep
0069  *
0070  * @get_mctrl: ``unsigned int ()(struct uart_port *port)``
0071  *
0072  *  Returns the current state of modem control inputs of @port. The state
0073  *  of the outputs should not be returned, since the core keeps track of
0074  *  their state. The state information should include:
0075  *
0076  *      - %TIOCM_CAR    state of DCD signal
0077  *      - %TIOCM_CTS    state of CTS signal
0078  *      - %TIOCM_DSR    state of DSR signal
0079  *      - %TIOCM_RI state of RI signal
0080  *
0081  *  The bit is set if the signal is currently driven active.  If
0082  *  the port does not support CTS, DCD or DSR, the driver should
0083  *  indicate that the signal is permanently active. If RI is
0084  *  not available, the signal should not be indicated as active.
0085  *
0086  *  Locking: @port->lock taken.
0087  *  Interrupts: locally disabled.
0088  *  This call must not sleep
0089  *
0090  * @stop_tx: ``void ()(struct uart_port *port)``
0091  *
0092  *  Stop transmitting characters. This might be due to the CTS line
0093  *  becoming inactive or the tty layer indicating we want to stop
0094  *  transmission due to an %XOFF character.
0095  *
0096  *  The driver should stop transmitting characters as soon as possible.
0097  *
0098  *  Locking: @port->lock taken.
0099  *  Interrupts: locally disabled.
0100  *  This call must not sleep
0101  *
0102  * @start_tx: ``void ()(struct uart_port *port)``
0103  *
0104  *  Start transmitting characters.
0105  *
0106  *  Locking: @port->lock taken.
0107  *  Interrupts: locally disabled.
0108  *  This call must not sleep
0109  *
0110  * @throttle: ``void ()(struct uart_port *port)``
0111  *
0112  *  Notify the serial driver that input buffers for the line discipline are
0113  *  close to full, and it should somehow signal that no more characters
0114  *  should be sent to the serial port.
0115  *  This will be called only if hardware assisted flow control is enabled.
0116  *
0117  *  Locking: serialized with @unthrottle() and termios modification by the
0118  *  tty layer.
0119  *
0120  * @unthrottle: ``void ()(struct uart_port *port)``
0121  *
0122  *  Notify the serial driver that characters can now be sent to the serial
0123  *  port without fear of overrunning the input buffers of the line
0124  *  disciplines.
0125  *
0126  *  This will be called only if hardware assisted flow control is enabled.
0127  *
0128  *  Locking: serialized with @throttle() and termios modification by the
0129  *  tty layer.
0130  *
0131  * @send_xchar: ``void ()(struct uart_port *port, char ch)``
0132  *
0133  *  Transmit a high priority character, even if the port is stopped. This
0134  *  is used to implement XON/XOFF flow control and tcflow(). If the serial
0135  *  driver does not implement this function, the tty core will append the
0136  *  character to the circular buffer and then call start_tx() / stop_tx()
0137  *  to flush the data out.
0138  *
0139  *  Do not transmit if @ch == '\0' (%__DISABLED_CHAR).
0140  *
0141  *  Locking: none.
0142  *  Interrupts: caller dependent.
0143  *
0144  * @start_rx: ``void ()(struct uart_port *port)``
0145  *
0146  *  Start receiving characters.
0147  *
0148  *  Locking: @port->lock taken.
0149  *  Interrupts: locally disabled.
0150  *  This call must not sleep
0151  *
0152  * @stop_rx: ``void ()(struct uart_port *port)``
0153  *
0154  *  Stop receiving characters; the @port is in the process of being closed.
0155  *
0156  *  Locking: @port->lock taken.
0157  *  Interrupts: locally disabled.
0158  *  This call must not sleep
0159  *
0160  * @enable_ms: ``void ()(struct uart_port *port)``
0161  *
0162  *  Enable the modem status interrupts.
0163  *
0164  *  This method may be called multiple times. Modem status interrupts
0165  *  should be disabled when the @shutdown() method is called.
0166  *
0167  *  Locking: @port->lock taken.
0168  *  Interrupts: locally disabled.
0169  *  This call must not sleep
0170  *
0171  * @break_ctl: ``void ()(struct uart_port *port, int ctl)``
0172  *
0173  *  Control the transmission of a break signal. If @ctl is nonzero, the
0174  *  break signal should be transmitted. The signal should be terminated
0175  *  when another call is made with a zero @ctl.
0176  *
0177  *  Locking: caller holds tty_port->mutex
0178  *
0179  * @startup: ``int ()(struct uart_port *port)``
0180  *
0181  *  Grab any interrupt resources and initialise any low level driver state.
0182  *  Enable the port for reception. It should not activate RTS nor DTR;
0183  *  this will be done via a separate call to @set_mctrl().
0184  *
0185  *  This method will only be called when the port is initially opened.
0186  *
0187  *  Locking: port_sem taken.
0188  *  Interrupts: globally disabled.
0189  *
0190  * @shutdown: ``void ()(struct uart_port *port)``
0191  *
0192  *  Disable the @port, disable any break condition that may be in effect,
0193  *  and free any interrupt resources. It should not disable RTS nor DTR;
0194  *  this will have already been done via a separate call to @set_mctrl().
0195  *
0196  *  Drivers must not access @port->state once this call has completed.
0197  *
0198  *  This method will only be called when there are no more users of this
0199  *  @port.
0200  *
0201  *  Locking: port_sem taken.
0202  *  Interrupts: caller dependent.
0203  *
0204  * @flush_buffer: ``void ()(struct uart_port *port)``
0205  *
0206  *  Flush any write buffers, reset any DMA state and stop any ongoing DMA
0207  *  transfers.
0208  *
0209  *  This will be called whenever the @port->state->xmit circular buffer is
0210  *  cleared.
0211  *
0212  *  Locking: @port->lock taken.
0213  *  Interrupts: locally disabled.
0214  *  This call must not sleep
0215  *
0216  * @set_termios: ``void ()(struct uart_port *port, struct ktermios *new,
0217  *          struct ktermios *old)``
0218  *
0219  *  Change the @port parameters, including word length, parity, stop bits.
0220  *  Update @port->read_status_mask and @port->ignore_status_mask to
0221  *  indicate the types of events we are interested in receiving. Relevant
0222  *  ktermios::c_cflag bits are:
0223  *
0224  *  - %CSIZE - word size
0225  *  - %CSTOPB - 2 stop bits
0226  *  - %PARENB - parity enable
0227  *  - %PARODD - odd parity (when %PARENB is in force)
0228  *  - %ADDRB - address bit (changed through uart_port::rs485_config()).
0229  *  - %CREAD - enable reception of characters (if not set, still receive
0230  *    characters from the port, but throw them away).
0231  *  - %CRTSCTS - if set, enable CTS status change reporting.
0232  *  - %CLOCAL - if not set, enable modem status change reporting.
0233  *
0234  *  Relevant ktermios::c_iflag bits are:
0235  *
0236  *  - %INPCK - enable frame and parity error events to be passed to the TTY
0237  *    layer.
0238  *  - %BRKINT / %PARMRK - both of these enable break events to be passed to
0239  *    the TTY layer.
0240  *  - %IGNPAR - ignore parity and framing errors.
0241  *  - %IGNBRK - ignore break errors. If %IGNPAR is also set, ignore overrun
0242  *    errors as well.
0243  *
0244  *  The interaction of the ktermios::c_iflag bits is as follows (parity
0245  *  error given as an example):
0246  *
0247  *  ============ ======= ======= =========================================
0248  *  Parity error INPCK   IGNPAR
0249  *  ============ ======= ======= =========================================
0250  *  n/a      0       n/a     character received, marked as %TTY_NORMAL
0251  *  None         1       n/a     character received, marked as %TTY_NORMAL
0252  *  Yes      1       0       character received, marked as %TTY_PARITY
0253  *  Yes      1       1       character discarded
0254  *  ============ ======= ======= =========================================
0255  *
0256  *  Other flags may be used (eg, xon/xoff characters) if your hardware
0257  *  supports hardware "soft" flow control.
0258  *
0259  *  Locking: caller holds tty_port->mutex
0260  *  Interrupts: caller dependent.
0261  *  This call must not sleep
0262  *
0263  * @set_ldisc: ``void ()(struct uart_port *port, struct ktermios *termios)``
0264  *
0265  *  Notifier for discipline change. See
0266  *  Documentation/driver-api/tty/tty_ldisc.rst.
0267  *
0268  *  Locking: caller holds tty_port->mutex
0269  *
0270  * @pm: ``void ()(struct uart_port *port, unsigned int state,
0271  *       unsigned int oldstate)``
0272  *
0273  *  Perform any power management related activities on the specified @port.
0274  *  @state indicates the new state (defined by enum uart_pm_state),
0275  *  @oldstate indicates the previous state.
0276  *
0277  *  This function should not be used to grab any resources.
0278  *
0279  *  This will be called when the @port is initially opened and finally
0280  *  closed, except when the @port is also the system console. This will
0281  *  occur even if %CONFIG_PM is not set.
0282  *
0283  *  Locking: none.
0284  *  Interrupts: caller dependent.
0285  *
0286  * @type: ``const char *()(struct uart_port *port)``
0287  *
0288  *  Return a pointer to a string constant describing the specified @port,
0289  *  or return %NULL, in which case the string 'unknown' is substituted.
0290  *
0291  *  Locking: none.
0292  *  Interrupts: caller dependent.
0293  *
0294  * @release_port: ``void ()(struct uart_port *port)``
0295  *
0296  *  Release any memory and IO region resources currently in use by the
0297  *  @port.
0298  *
0299  *  Locking: none.
0300  *  Interrupts: caller dependent.
0301  *
0302  * @request_port: ``int ()(struct uart_port *port)``
0303  *
0304  *  Request any memory and IO region resources required by the port. If any
0305  *  fail, no resources should be registered when this function returns, and
0306  *  it should return -%EBUSY on failure.
0307  *
0308  *  Locking: none.
0309  *  Interrupts: caller dependent.
0310  *
0311  * @config_port: ``void ()(struct uart_port *port, int type)``
0312  *
0313  *  Perform any autoconfiguration steps required for the @port. @type
0314  *  contains a bit mask of the required configuration. %UART_CONFIG_TYPE
0315  *  indicates that the port requires detection and identification.
0316  *  @port->type should be set to the type found, or %PORT_UNKNOWN if no
0317  *  port was detected.
0318  *
0319  *  %UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
0320  *  which should be probed using standard kernel autoprobing techniques.
0321  *  This is not necessary on platforms where ports have interrupts
0322  *  internally hard wired (eg, system on a chip implementations).
0323  *
0324  *  Locking: none.
0325  *  Interrupts: caller dependent.
0326  *
0327  * @verify_port: ``int ()(struct uart_port *port,
0328  *          struct serial_struct *serinfo)``
0329  *
0330  *  Verify the new serial port information contained within @serinfo is
0331  *  suitable for this port type.
0332  *
0333  *  Locking: none.
0334  *  Interrupts: caller dependent.
0335  *
0336  * @ioctl: ``int ()(struct uart_port *port, unsigned int cmd,
0337  *      unsigned long arg)``
0338  *
0339  *  Perform any port specific IOCTLs. IOCTL commands must be defined using
0340  *  the standard numbering system found in <asm/ioctl.h>.
0341  *
0342  *  Locking: none.
0343  *  Interrupts: caller dependent.
0344  *
0345  * @poll_init: ``int ()(struct uart_port *port)``
0346  *
0347  *  Called by kgdb to perform the minimal hardware initialization needed to
0348  *  support @poll_put_char() and @poll_get_char(). Unlike @startup(), this
0349  *  should not request interrupts.
0350  *
0351  *  Locking: %tty_mutex and tty_port->mutex taken.
0352  *  Interrupts: n/a.
0353  *
0354  * @poll_put_char: ``void ()(struct uart_port *port, unsigned char ch)``
0355  *
0356  *  Called by kgdb to write a single character @ch directly to the serial
0357  *  @port. It can and should block until there is space in the TX FIFO.
0358  *
0359  *  Locking: none.
0360  *  Interrupts: caller dependent.
0361  *  This call must not sleep
0362  *
0363  * @poll_get_char: ``int ()(struct uart_port *port)``
0364  *
0365  *  Called by kgdb to read a single character directly from the serial
0366  *  port. If data is available, it should be returned; otherwise the
0367  *  function should return %NO_POLL_CHAR immediately.
0368  *
0369  *  Locking: none.
0370  *  Interrupts: caller dependent.
0371  *  This call must not sleep
0372  */
0373 struct uart_ops {
0374     unsigned int    (*tx_empty)(struct uart_port *);
0375     void        (*set_mctrl)(struct uart_port *, unsigned int mctrl);
0376     unsigned int    (*get_mctrl)(struct uart_port *);
0377     void        (*stop_tx)(struct uart_port *);
0378     void        (*start_tx)(struct uart_port *);
0379     void        (*throttle)(struct uart_port *);
0380     void        (*unthrottle)(struct uart_port *);
0381     void        (*send_xchar)(struct uart_port *, char ch);
0382     void        (*stop_rx)(struct uart_port *);
0383     void        (*start_rx)(struct uart_port *);
0384     void        (*enable_ms)(struct uart_port *);
0385     void        (*break_ctl)(struct uart_port *, int ctl);
0386     int     (*startup)(struct uart_port *);
0387     void        (*shutdown)(struct uart_port *);
0388     void        (*flush_buffer)(struct uart_port *);
0389     void        (*set_termios)(struct uart_port *, struct ktermios *new,
0390                        struct ktermios *old);
0391     void        (*set_ldisc)(struct uart_port *, struct ktermios *);
0392     void        (*pm)(struct uart_port *, unsigned int state,
0393                   unsigned int oldstate);
0394     const char  *(*type)(struct uart_port *);
0395     void        (*release_port)(struct uart_port *);
0396     int     (*request_port)(struct uart_port *);
0397     void        (*config_port)(struct uart_port *, int);
0398     int     (*verify_port)(struct uart_port *, struct serial_struct *);
0399     int     (*ioctl)(struct uart_port *, unsigned int, unsigned long);
0400 #ifdef CONFIG_CONSOLE_POLL
0401     int     (*poll_init)(struct uart_port *);
0402     void        (*poll_put_char)(struct uart_port *, unsigned char);
0403     int     (*poll_get_char)(struct uart_port *);
0404 #endif
0405 };
0406 
0407 #define NO_POLL_CHAR        0x00ff0000
0408 #define UART_CONFIG_TYPE    (1 << 0)
0409 #define UART_CONFIG_IRQ     (1 << 1)
0410 
0411 struct uart_icount {
0412     __u32   cts;
0413     __u32   dsr;
0414     __u32   rng;
0415     __u32   dcd;
0416     __u32   rx;
0417     __u32   tx;
0418     __u32   frame;
0419     __u32   overrun;
0420     __u32   parity;
0421     __u32   brk;
0422     __u32   buf_overrun;
0423 };
0424 
0425 typedef unsigned int __bitwise upf_t;
0426 typedef unsigned int __bitwise upstat_t;
0427 
0428 struct uart_port {
0429     spinlock_t      lock;           /* port lock */
0430     unsigned long       iobase;         /* in/out[bwl] */
0431     unsigned char __iomem   *membase;       /* read/write[bwl] */
0432     unsigned int        (*serial_in)(struct uart_port *, int);
0433     void            (*serial_out)(struct uart_port *, int, int);
0434     void            (*set_termios)(struct uart_port *,
0435                                struct ktermios *new,
0436                                struct ktermios *old);
0437     void            (*set_ldisc)(struct uart_port *,
0438                          struct ktermios *);
0439     unsigned int        (*get_mctrl)(struct uart_port *);
0440     void            (*set_mctrl)(struct uart_port *, unsigned int);
0441     unsigned int        (*get_divisor)(struct uart_port *,
0442                            unsigned int baud,
0443                            unsigned int *frac);
0444     void            (*set_divisor)(struct uart_port *,
0445                            unsigned int baud,
0446                            unsigned int quot,
0447                            unsigned int quot_frac);
0448     int         (*startup)(struct uart_port *port);
0449     void            (*shutdown)(struct uart_port *port);
0450     void            (*throttle)(struct uart_port *port);
0451     void            (*unthrottle)(struct uart_port *port);
0452     int         (*handle_irq)(struct uart_port *);
0453     void            (*pm)(struct uart_port *, unsigned int state,
0454                       unsigned int old);
0455     void            (*handle_break)(struct uart_port *);
0456     int         (*rs485_config)(struct uart_port *,
0457                         struct ktermios *termios,
0458                         struct serial_rs485 *rs485);
0459     int         (*iso7816_config)(struct uart_port *,
0460                           struct serial_iso7816 *iso7816);
0461     unsigned int        irq;            /* irq number */
0462     unsigned long       irqflags;       /* irq flags  */
0463     unsigned int        uartclk;        /* base uart clock */
0464     unsigned int        fifosize;       /* tx fifo size */
0465     unsigned char       x_char;         /* xon/xoff char */
0466     unsigned char       regshift;       /* reg offset shift */
0467     unsigned char       iotype;         /* io access style */
0468     unsigned char       quirks;         /* internal quirks */
0469 
0470 #define UPIO_PORT       (SERIAL_IO_PORT)    /* 8b I/O port access */
0471 #define UPIO_HUB6       (SERIAL_IO_HUB6)    /* Hub6 ISA card */
0472 #define UPIO_MEM        (SERIAL_IO_MEM)     /* driver-specific */
0473 #define UPIO_MEM32      (SERIAL_IO_MEM32)   /* 32b little endian */
0474 #define UPIO_AU         (SERIAL_IO_AU)      /* Au1x00 and RT288x type IO */
0475 #define UPIO_TSI        (SERIAL_IO_TSI)     /* Tsi108/109 type IO */
0476 #define UPIO_MEM32BE        (SERIAL_IO_MEM32BE) /* 32b big endian */
0477 #define UPIO_MEM16      (SERIAL_IO_MEM16)   /* 16b little endian */
0478 
0479     /* quirks must be updated while holding port mutex */
0480 #define UPQ_NO_TXEN_TEST    BIT(0)
0481 
0482     unsigned int        read_status_mask;   /* driver specific */
0483     unsigned int        ignore_status_mask; /* driver specific */
0484     struct uart_state   *state;         /* pointer to parent state */
0485     struct uart_icount  icount;         /* statistics */
0486 
0487     struct console      *cons;          /* struct console, if any */
0488     /* flags must be updated while holding port mutex */
0489     upf_t           flags;
0490 
0491     /*
0492      * These flags must be equivalent to the flags defined in
0493      * include/uapi/linux/tty_flags.h which are the userspace definitions
0494      * assigned from the serial_struct flags in uart_set_info()
0495      * [for bit definitions in the UPF_CHANGE_MASK]
0496      *
0497      * Bits [0..ASYNCB_LAST_USER] are userspace defined/visible/changeable
0498      * The remaining bits are serial-core specific and not modifiable by
0499      * userspace.
0500      */
0501 #define UPF_FOURPORT        ((__force upf_t) ASYNC_FOURPORT       /* 1  */ )
0502 #define UPF_SAK         ((__force upf_t) ASYNC_SAK            /* 2  */ )
0503 #define UPF_SPD_HI      ((__force upf_t) ASYNC_SPD_HI         /* 4  */ )
0504 #define UPF_SPD_VHI     ((__force upf_t) ASYNC_SPD_VHI        /* 5  */ )
0505 #define UPF_SPD_CUST        ((__force upf_t) ASYNC_SPD_CUST   /* 0x0030 */ )
0506 #define UPF_SPD_WARP        ((__force upf_t) ASYNC_SPD_WARP   /* 0x1010 */ )
0507 #define UPF_SPD_MASK        ((__force upf_t) ASYNC_SPD_MASK   /* 0x1030 */ )
0508 #define UPF_SKIP_TEST       ((__force upf_t) ASYNC_SKIP_TEST      /* 6  */ )
0509 #define UPF_AUTO_IRQ        ((__force upf_t) ASYNC_AUTO_IRQ       /* 7  */ )
0510 #define UPF_HARDPPS_CD      ((__force upf_t) ASYNC_HARDPPS_CD     /* 11 */ )
0511 #define UPF_SPD_SHI     ((__force upf_t) ASYNC_SPD_SHI        /* 12 */ )
0512 #define UPF_LOW_LATENCY     ((__force upf_t) ASYNC_LOW_LATENCY    /* 13 */ )
0513 #define UPF_BUGGY_UART      ((__force upf_t) ASYNC_BUGGY_UART     /* 14 */ )
0514 #define UPF_MAGIC_MULTIPLIER    ((__force upf_t) ASYNC_MAGIC_MULTIPLIER /* 16 */ )
0515 
0516 #define UPF_NO_THRE_TEST    ((__force upf_t) (1 << 19))
0517 /* Port has hardware-assisted h/w flow control */
0518 #define UPF_AUTO_CTS        ((__force upf_t) (1 << 20))
0519 #define UPF_AUTO_RTS        ((__force upf_t) (1 << 21))
0520 #define UPF_HARD_FLOW       ((__force upf_t) (UPF_AUTO_CTS | UPF_AUTO_RTS))
0521 /* Port has hardware-assisted s/w flow control */
0522 #define UPF_SOFT_FLOW       ((__force upf_t) (1 << 22))
0523 #define UPF_CONS_FLOW       ((__force upf_t) (1 << 23))
0524 #define UPF_SHARE_IRQ       ((__force upf_t) (1 << 24))
0525 #define UPF_EXAR_EFR        ((__force upf_t) (1 << 25))
0526 #define UPF_BUG_THRE        ((__force upf_t) (1 << 26))
0527 /* The exact UART type is known and should not be probed.  */
0528 #define UPF_FIXED_TYPE      ((__force upf_t) (1 << 27))
0529 #define UPF_BOOT_AUTOCONF   ((__force upf_t) (1 << 28))
0530 #define UPF_FIXED_PORT      ((__force upf_t) (1 << 29))
0531 #define UPF_DEAD        ((__force upf_t) (1 << 30))
0532 #define UPF_IOREMAP     ((__force upf_t) (1 << 31))
0533 
0534 #define __UPF_CHANGE_MASK   0x17fff
0535 #define UPF_CHANGE_MASK     ((__force upf_t) __UPF_CHANGE_MASK)
0536 #define UPF_USR_MASK        ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
0537 
0538 #if __UPF_CHANGE_MASK > ASYNC_FLAGS
0539 #error Change mask not equivalent to userspace-visible bit defines
0540 #endif
0541 
0542     /*
0543      * Must hold termios_rwsem, port mutex and port lock to change;
0544      * can hold any one lock to read.
0545      */
0546     upstat_t        status;
0547 
0548 #define UPSTAT_CTS_ENABLE   ((__force upstat_t) (1 << 0))
0549 #define UPSTAT_DCD_ENABLE   ((__force upstat_t) (1 << 1))
0550 #define UPSTAT_AUTORTS      ((__force upstat_t) (1 << 2))
0551 #define UPSTAT_AUTOCTS      ((__force upstat_t) (1 << 3))
0552 #define UPSTAT_AUTOXOFF     ((__force upstat_t) (1 << 4))
0553 #define UPSTAT_SYNC_FIFO    ((__force upstat_t) (1 << 5))
0554 
0555     int         hw_stopped;     /* sw-assisted CTS flow state */
0556     unsigned int        mctrl;          /* current modem ctrl settings */
0557     unsigned int        frame_time;     /* frame timing in ns */
0558     unsigned int        type;           /* port type */
0559     const struct uart_ops   *ops;
0560     unsigned int        custom_divisor;
0561     unsigned int        line;           /* port index */
0562     unsigned int        minor;
0563     resource_size_t     mapbase;        /* for ioremap */
0564     resource_size_t     mapsize;
0565     struct device       *dev;           /* parent device */
0566 
0567     unsigned long       sysrq;          /* sysrq timeout */
0568     unsigned int        sysrq_ch;       /* char for sysrq */
0569     unsigned char       has_sysrq;
0570     unsigned char       sysrq_seq;      /* index in sysrq_toggle_seq */
0571 
0572     unsigned char       hub6;           /* this should be in the 8250 driver */
0573     unsigned char       suspended;
0574     unsigned char       console_reinit;
0575     const char      *name;          /* port name */
0576     struct attribute_group  *attr_group;        /* port specific attributes */
0577     const struct attribute_group **tty_groups;  /* all attributes (serial core use only) */
0578     struct serial_rs485     rs485;
0579     struct serial_rs485 rs485_supported;    /* Supported mask for serial_rs485 */
0580     struct gpio_desc    *rs485_term_gpio;   /* enable RS485 bus termination */
0581     struct serial_iso7816   iso7816;
0582     void            *private_data;      /* generic platform data pointer */
0583 };
0584 
0585 static inline int serial_port_in(struct uart_port *up, int offset)
0586 {
0587     return up->serial_in(up, offset);
0588 }
0589 
0590 static inline void serial_port_out(struct uart_port *up, int offset, int value)
0591 {
0592     up->serial_out(up, offset, value);
0593 }
0594 
0595 /**
0596  * enum uart_pm_state - power states for UARTs
0597  * @UART_PM_STATE_ON: UART is powered, up and operational
0598  * @UART_PM_STATE_OFF: UART is powered off
0599  * @UART_PM_STATE_UNDEFINED: sentinel
0600  */
0601 enum uart_pm_state {
0602     UART_PM_STATE_ON = 0,
0603     UART_PM_STATE_OFF = 3, /* number taken from ACPI */
0604     UART_PM_STATE_UNDEFINED,
0605 };
0606 
0607 /*
0608  * This is the state information which is persistent across opens.
0609  */
0610 struct uart_state {
0611     struct tty_port     port;
0612 
0613     enum uart_pm_state  pm_state;
0614     struct circ_buf     xmit;
0615 
0616     atomic_t        refcount;
0617     wait_queue_head_t   remove_wait;
0618     struct uart_port    *uart_port;
0619 };
0620 
0621 #define UART_XMIT_SIZE  PAGE_SIZE
0622 
0623 
0624 /* number of characters left in xmit buffer before we ask for more */
0625 #define WAKEUP_CHARS        256
0626 
0627 /**
0628  * uart_xmit_advance - Advance xmit buffer and account Tx'ed chars
0629  * @up: uart_port structure describing the port
0630  * @chars: number of characters sent
0631  *
0632  * This function advances the tail of circular xmit buffer by the number of
0633  * @chars transmitted and handles accounting of transmitted bytes (into
0634  * @up's icount.tx).
0635  */
0636 static inline void uart_xmit_advance(struct uart_port *up, unsigned int chars)
0637 {
0638     struct circ_buf *xmit = &up->state->xmit;
0639 
0640     xmit->tail = (xmit->tail + chars) & (UART_XMIT_SIZE - 1);
0641     up->icount.tx += chars;
0642 }
0643 
0644 struct module;
0645 struct tty_driver;
0646 
0647 struct uart_driver {
0648     struct module       *owner;
0649     const char      *driver_name;
0650     const char      *dev_name;
0651     int          major;
0652     int          minor;
0653     int          nr;
0654     struct console      *cons;
0655 
0656     /*
0657      * these are private; the low level driver should not
0658      * touch these; they should be initialised to NULL
0659      */
0660     struct uart_state   *state;
0661     struct tty_driver   *tty_driver;
0662 };
0663 
0664 void uart_write_wakeup(struct uart_port *port);
0665 
0666 /*
0667  * Baud rate helpers.
0668  */
0669 void uart_update_timeout(struct uart_port *port, unsigned int cflag,
0670              unsigned int baud);
0671 unsigned int uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
0672                 struct ktermios *old, unsigned int min,
0673                 unsigned int max);
0674 unsigned int uart_get_divisor(struct uart_port *port, unsigned int baud);
0675 
0676 /*
0677  * Calculates FIFO drain time.
0678  */
0679 static inline unsigned long uart_fifo_timeout(struct uart_port *port)
0680 {
0681     u64 fifo_timeout = (u64)READ_ONCE(port->frame_time) * port->fifosize;
0682 
0683     /* Add .02 seconds of slop */
0684     fifo_timeout += 20 * NSEC_PER_MSEC;
0685 
0686     return max(nsecs_to_jiffies(fifo_timeout), 1UL);
0687 }
0688 
0689 /* Base timer interval for polling */
0690 static inline int uart_poll_timeout(struct uart_port *port)
0691 {
0692     int timeout = uart_fifo_timeout(port);
0693 
0694     return timeout > 6 ? (timeout / 2 - 2) : 1;
0695 }
0696 
0697 /*
0698  * Console helpers.
0699  */
0700 struct earlycon_device {
0701     struct console *con;
0702     struct uart_port port;
0703     char options[16];       /* e.g., 115200n8 */
0704     unsigned int baud;
0705 };
0706 
0707 struct earlycon_id {
0708     char    name[15];
0709     char    name_term;  /* In case compiler didn't '\0' term name */
0710     char    compatible[128];
0711     int (*setup)(struct earlycon_device *, const char *options);
0712 };
0713 
0714 extern const struct earlycon_id __earlycon_table[];
0715 extern const struct earlycon_id __earlycon_table_end[];
0716 
0717 #if defined(CONFIG_SERIAL_EARLYCON) && !defined(MODULE)
0718 #define EARLYCON_USED_OR_UNUSED __used
0719 #else
0720 #define EARLYCON_USED_OR_UNUSED __maybe_unused
0721 #endif
0722 
0723 #define OF_EARLYCON_DECLARE(_name, compat, fn)              \
0724     static const struct earlycon_id __UNIQUE_ID(__earlycon_##_name) \
0725         EARLYCON_USED_OR_UNUSED  __section("__earlycon_table")  \
0726         __aligned(__alignof__(struct earlycon_id))      \
0727         = { .name = __stringify(_name),             \
0728             .compatible = compat,               \
0729             .setup = fn }
0730 
0731 #define EARLYCON_DECLARE(_name, fn) OF_EARLYCON_DECLARE(_name, "", fn)
0732 
0733 extern int of_setup_earlycon(const struct earlycon_id *match,
0734                  unsigned long node,
0735                  const char *options);
0736 
0737 #ifdef CONFIG_SERIAL_EARLYCON
0738 extern bool earlycon_acpi_spcr_enable __initdata;
0739 int setup_earlycon(char *buf);
0740 #else
0741 static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
0742 static inline int setup_earlycon(char *buf) { return 0; }
0743 #endif
0744 
0745 static inline bool uart_console_enabled(struct uart_port *port)
0746 {
0747     return uart_console(port) && (port->cons->flags & CON_ENABLED);
0748 }
0749 
0750 struct uart_port *uart_get_console(struct uart_port *ports, int nr,
0751                    struct console *c);
0752 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
0753             char **options);
0754 void uart_parse_options(const char *options, int *baud, int *parity, int *bits,
0755             int *flow);
0756 int uart_set_options(struct uart_port *port, struct console *co, int baud,
0757              int parity, int bits, int flow);
0758 struct tty_driver *uart_console_device(struct console *co, int *index);
0759 void uart_console_write(struct uart_port *port, const char *s,
0760             unsigned int count,
0761             void (*putchar)(struct uart_port *, unsigned char));
0762 
0763 /*
0764  * Port/driver registration/removal
0765  */
0766 int uart_register_driver(struct uart_driver *uart);
0767 void uart_unregister_driver(struct uart_driver *uart);
0768 int uart_add_one_port(struct uart_driver *reg, struct uart_port *port);
0769 int uart_remove_one_port(struct uart_driver *reg, struct uart_port *port);
0770 bool uart_match_port(const struct uart_port *port1,
0771         const struct uart_port *port2);
0772 
0773 /*
0774  * Power Management
0775  */
0776 int uart_suspend_port(struct uart_driver *reg, struct uart_port *port);
0777 int uart_resume_port(struct uart_driver *reg, struct uart_port *port);
0778 
0779 #define uart_circ_empty(circ)       ((circ)->head == (circ)->tail)
0780 #define uart_circ_clear(circ)       ((circ)->head = (circ)->tail = 0)
0781 
0782 #define uart_circ_chars_pending(circ)   \
0783     (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
0784 
0785 #define uart_circ_chars_free(circ)  \
0786     (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
0787 
0788 static inline int uart_tx_stopped(struct uart_port *port)
0789 {
0790     struct tty_struct *tty = port->state->port.tty;
0791     if ((tty && tty->flow.stopped) || port->hw_stopped)
0792         return 1;
0793     return 0;
0794 }
0795 
0796 static inline bool uart_cts_enabled(struct uart_port *uport)
0797 {
0798     return !!(uport->status & UPSTAT_CTS_ENABLE);
0799 }
0800 
0801 static inline bool uart_softcts_mode(struct uart_port *uport)
0802 {
0803     upstat_t mask = UPSTAT_CTS_ENABLE | UPSTAT_AUTOCTS;
0804 
0805     return ((uport->status & mask) == UPSTAT_CTS_ENABLE);
0806 }
0807 
0808 /*
0809  * The following are helper functions for the low level drivers.
0810  */
0811 
0812 extern void uart_handle_dcd_change(struct uart_port *uport,
0813         unsigned int status);
0814 extern void uart_handle_cts_change(struct uart_port *uport,
0815         unsigned int status);
0816 
0817 extern void uart_insert_char(struct uart_port *port, unsigned int status,
0818          unsigned int overrun, unsigned int ch, unsigned int flag);
0819 
0820 void uart_xchar_out(struct uart_port *uport, int offset);
0821 
0822 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
0823 #define SYSRQ_TIMEOUT   (HZ * 5)
0824 
0825 bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch);
0826 
0827 static inline int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
0828 {
0829     if (!port->sysrq)
0830         return 0;
0831 
0832     if (ch && time_before(jiffies, port->sysrq)) {
0833         if (sysrq_mask()) {
0834             handle_sysrq(ch);
0835             port->sysrq = 0;
0836             return 1;
0837         }
0838         if (uart_try_toggle_sysrq(port, ch))
0839             return 1;
0840     }
0841     port->sysrq = 0;
0842 
0843     return 0;
0844 }
0845 
0846 static inline int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch)
0847 {
0848     if (!port->sysrq)
0849         return 0;
0850 
0851     if (ch && time_before(jiffies, port->sysrq)) {
0852         if (sysrq_mask()) {
0853             port->sysrq_ch = ch;
0854             port->sysrq = 0;
0855             return 1;
0856         }
0857         if (uart_try_toggle_sysrq(port, ch))
0858             return 1;
0859     }
0860     port->sysrq = 0;
0861 
0862     return 0;
0863 }
0864 
0865 static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
0866 {
0867     int sysrq_ch;
0868 
0869     if (!port->has_sysrq) {
0870         spin_unlock(&port->lock);
0871         return;
0872     }
0873 
0874     sysrq_ch = port->sysrq_ch;
0875     port->sysrq_ch = 0;
0876 
0877     spin_unlock(&port->lock);
0878 
0879     if (sysrq_ch)
0880         handle_sysrq(sysrq_ch);
0881 }
0882 
0883 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
0884         unsigned long flags)
0885 {
0886     int sysrq_ch;
0887 
0888     if (!port->has_sysrq) {
0889         spin_unlock_irqrestore(&port->lock, flags);
0890         return;
0891     }
0892 
0893     sysrq_ch = port->sysrq_ch;
0894     port->sysrq_ch = 0;
0895 
0896     spin_unlock_irqrestore(&port->lock, flags);
0897 
0898     if (sysrq_ch)
0899         handle_sysrq(sysrq_ch);
0900 }
0901 #else   /* CONFIG_MAGIC_SYSRQ_SERIAL */
0902 static inline int uart_handle_sysrq_char(struct uart_port *port, unsigned int ch)
0903 {
0904     return 0;
0905 }
0906 static inline int uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch)
0907 {
0908     return 0;
0909 }
0910 static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
0911 {
0912     spin_unlock(&port->lock);
0913 }
0914 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
0915         unsigned long flags)
0916 {
0917     spin_unlock_irqrestore(&port->lock, flags);
0918 }
0919 #endif  /* CONFIG_MAGIC_SYSRQ_SERIAL */
0920 
0921 /*
0922  * We do the SysRQ and SAK checking like this...
0923  */
0924 static inline int uart_handle_break(struct uart_port *port)
0925 {
0926     struct uart_state *state = port->state;
0927 
0928     if (port->handle_break)
0929         port->handle_break(port);
0930 
0931 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
0932     if (port->has_sysrq && uart_console(port)) {
0933         if (!port->sysrq) {
0934             port->sysrq = jiffies + SYSRQ_TIMEOUT;
0935             return 1;
0936         }
0937         port->sysrq = 0;
0938     }
0939 #endif
0940     if (port->flags & UPF_SAK)
0941         do_SAK(state->port.tty);
0942     return 0;
0943 }
0944 
0945 /*
0946  *  UART_ENABLE_MS - determine if port should enable modem status irqs
0947  */
0948 #define UART_ENABLE_MS(port,cflag)  ((port)->flags & UPF_HARDPPS_CD || \
0949                      (cflag) & CRTSCTS || \
0950                      !((cflag) & CLOCAL))
0951 
0952 int uart_get_rs485_mode(struct uart_port *port);
0953 int uart_rs485_config(struct uart_port *port);
0954 #endif /* LINUX_SERIAL_CORE_H */