1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver core for serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Copyright 1999 ARM Limited
8 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
10 #include <linux/module.h>
11 #include <linux/tty.h>
12 #include <linux/tty_flip.h>
13 #include <linux/slab.h>
14 #include <linux/sched/signal.h>
15 #include <linux/init.h>
16 #include <linux/console.h>
17 #include <linux/gpio/consumer.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <linux/device.h>
22 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
23 #include <linux/serial_core.h>
24 #include <linux/sysrq.h>
25 #include <linux/delay.h>
26 #include <linux/mutex.h>
27 #include <linux/math64.h>
28 #include <linux/security.h>
30 #include <linux/irq.h>
31 #include <linux/uaccess.h>
34 * This is used to lock changes in serial line configuration.
36 static DEFINE_MUTEX(port_mutex);
39 * lockdep: port->lock is initialized in two places, but we
40 * want only one lock-class:
42 static struct lock_class_key port_lock_key;
44 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
47 * Max time with active RTS before/after data is sent.
49 #define RS485_MAX_RTS_DELAY 100 /* msecs */
51 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
52 struct ktermios *old_termios);
53 static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
54 static void uart_change_pm(struct uart_state *state,
55 enum uart_pm_state pm_state);
57 static void uart_port_shutdown(struct tty_port *port);
59 static int uart_dcd_enabled(struct uart_port *uport)
61 return !!(uport->status & UPSTAT_DCD_ENABLE);
64 static inline struct uart_port *uart_port_ref(struct uart_state *state)
66 if (atomic_add_unless(&state->refcount, 1, 0))
67 return state->uart_port;
71 static inline void uart_port_deref(struct uart_port *uport)
73 if (atomic_dec_and_test(&uport->state->refcount))
74 wake_up(&uport->state->remove_wait);
77 #define uart_port_lock(state, flags) \
79 struct uart_port *__uport = uart_port_ref(state); \
81 spin_lock_irqsave(&__uport->lock, flags); \
85 #define uart_port_unlock(uport, flags) \
87 struct uart_port *__uport = uport; \
89 spin_unlock_irqrestore(&__uport->lock, flags); \
90 uart_port_deref(__uport); \
94 static inline struct uart_port *uart_port_check(struct uart_state *state)
96 lockdep_assert_held(&state->port.mutex);
97 return state->uart_port;
101 * This routine is used by the interrupt handler to schedule processing in
102 * the software interrupt portion of the driver.
104 void uart_write_wakeup(struct uart_port *port)
106 struct uart_state *state = port->state;
108 * This means you called this function _after_ the port was
109 * closed. No cookie for you.
112 tty_port_tty_wakeup(&state->port);
114 EXPORT_SYMBOL(uart_write_wakeup);
116 static void uart_stop(struct tty_struct *tty)
118 struct uart_state *state = tty->driver_data;
119 struct uart_port *port;
122 port = uart_port_lock(state, flags);
124 port->ops->stop_tx(port);
125 uart_port_unlock(port, flags);
128 static void __uart_start(struct tty_struct *tty)
130 struct uart_state *state = tty->driver_data;
131 struct uart_port *port = state->uart_port;
133 if (port && !uart_tx_stopped(port))
134 port->ops->start_tx(port);
137 static void uart_start(struct tty_struct *tty)
139 struct uart_state *state = tty->driver_data;
140 struct uart_port *port;
143 port = uart_port_lock(state, flags);
145 uart_port_unlock(port, flags);
149 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
154 if (port->rs485.flags & SER_RS485_ENABLED) {
159 spin_lock_irqsave(&port->lock, flags);
161 port->mctrl = (old & ~clear) | set;
162 if (old != port->mctrl)
163 port->ops->set_mctrl(port, port->mctrl);
164 spin_unlock_irqrestore(&port->lock, flags);
167 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
168 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
170 static void uart_port_dtr_rts(struct uart_port *uport, int raise)
173 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
175 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
179 * Startup the port. This will be called once per open. All calls
180 * will be serialised by the per-port mutex.
182 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
185 struct uart_port *uport = uart_port_check(state);
190 if (uport->type == PORT_UNKNOWN)
194 * Make sure the device is in D0 state.
196 uart_change_pm(state, UART_PM_STATE_ON);
199 * Initialise and allocate the transmit and temporary
202 page = get_zeroed_page(GFP_KERNEL);
206 uart_port_lock(state, flags);
207 if (!state->xmit.buf) {
208 state->xmit.buf = (unsigned char *) page;
209 uart_circ_clear(&state->xmit);
210 uart_port_unlock(uport, flags);
212 uart_port_unlock(uport, flags);
214 * Do not free() the page under the port lock, see
220 retval = uport->ops->startup(uport);
222 if (uart_console(uport) && uport->cons->cflag) {
223 tty->termios.c_cflag = uport->cons->cflag;
224 tty->termios.c_ispeed = uport->cons->ispeed;
225 tty->termios.c_ospeed = uport->cons->ospeed;
226 uport->cons->cflag = 0;
227 uport->cons->ispeed = 0;
228 uport->cons->ospeed = 0;
231 * Initialise the hardware port settings.
233 uart_change_speed(tty, state, NULL);
236 * Setup the RTS and DTR signals once the
237 * port is open and ready to respond.
239 if (init_hw && C_BAUD(tty))
240 uart_port_dtr_rts(uport, 1);
244 * This is to allow setserial on this port. People may want to set
245 * port/irq/type and then reconfigure the port properly if it failed
248 if (retval && capable(CAP_SYS_ADMIN))
254 static int uart_startup(struct tty_struct *tty, struct uart_state *state,
257 struct tty_port *port = &state->port;
260 if (tty_port_initialized(port))
263 retval = uart_port_startup(tty, state, init_hw);
265 set_bit(TTY_IO_ERROR, &tty->flags);
271 * This routine will shutdown a serial port; interrupts are disabled, and
272 * DTR is dropped if the hangup on close termio flag is on. Calls to
273 * uart_shutdown are serialised by the per-port semaphore.
275 * uport == NULL if uart_port has already been removed
277 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
279 struct uart_port *uport = uart_port_check(state);
280 struct tty_port *port = &state->port;
282 char *xmit_buf = NULL;
285 * Set the TTY IO error marker
288 set_bit(TTY_IO_ERROR, &tty->flags);
290 if (tty_port_initialized(port)) {
291 tty_port_set_initialized(port, 0);
294 * Turn off DTR and RTS early.
296 if (uport && uart_console(uport) && tty) {
297 uport->cons->cflag = tty->termios.c_cflag;
298 uport->cons->ispeed = tty->termios.c_ispeed;
299 uport->cons->ospeed = tty->termios.c_ospeed;
302 if (!tty || C_HUPCL(tty))
303 uart_port_dtr_rts(uport, 0);
305 uart_port_shutdown(port);
309 * It's possible for shutdown to be called after suspend if we get
310 * a DCD drop (hangup) at just the right time. Clear suspended bit so
311 * we don't try to resume a port that has been shutdown.
313 tty_port_set_suspended(port, 0);
316 * Do not free() the transmit buffer page under the port lock since
317 * this can create various circular locking scenarios. For instance,
318 * console driver may need to allocate/free a debug object, which
319 * can endup in printk() recursion.
321 uart_port_lock(state, flags);
322 xmit_buf = state->xmit.buf;
323 state->xmit.buf = NULL;
324 uart_port_unlock(uport, flags);
326 free_page((unsigned long)xmit_buf);
330 * uart_update_timeout - update per-port FIFO timeout.
331 * @port: uart_port structure describing the port
332 * @cflag: termios cflag value
333 * @baud: speed of the port
335 * Set the port FIFO timeout value. The @cflag value should
336 * reflect the actual hardware settings.
339 uart_update_timeout(struct uart_port *port, unsigned int cflag,
342 unsigned int size = tty_get_frame_size(cflag);
345 frame_time = (u64)size * NSEC_PER_SEC;
346 size *= port->fifosize;
349 * Figure the timeout to send the above number of bits.
350 * Add .02 seconds of slop
352 port->timeout = (HZ * size) / baud + HZ/50;
353 port->frame_time = DIV64_U64_ROUND_UP(frame_time, baud);
355 EXPORT_SYMBOL(uart_update_timeout);
358 * uart_get_baud_rate - return baud rate for a particular port
359 * @port: uart_port structure describing the port in question.
360 * @termios: desired termios settings.
361 * @old: old termios (or NULL)
362 * @min: minimum acceptable baud rate
363 * @max: maximum acceptable baud rate
365 * Decode the termios structure into a numeric baud rate,
366 * taking account of the magic 38400 baud rate (with spd_*
367 * flags), and mapping the %B0 rate to 9600 baud.
369 * If the new baud rate is invalid, try the old termios setting.
370 * If it's still invalid, we try 9600 baud.
372 * Update the @termios structure to reflect the baud rate
373 * we're actually going to be using. Don't do this for the case
374 * where B0 is requested ("hang up").
377 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
378 struct ktermios *old, unsigned int min, unsigned int max)
382 unsigned int altbaud;
384 upf_t flags = port->flags & UPF_SPD_MASK;
404 for (try = 0; try < 2; try++) {
405 baud = tty_termios_baud_rate(termios);
408 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
411 if (try == 0 && baud == 38400)
415 * Special case: B0 rate.
422 if (baud >= min && baud <= max)
426 * Oops, the quotient was zero. Try again with
427 * the old baud rate if possible.
429 termios->c_cflag &= ~CBAUD;
431 baud = tty_termios_baud_rate(old);
433 tty_termios_encode_baud_rate(termios,
440 * As a last resort, if the range cannot be met then clip to
441 * the nearest chip supported rate.
445 tty_termios_encode_baud_rate(termios,
448 tty_termios_encode_baud_rate(termios,
452 /* Should never happen */
456 EXPORT_SYMBOL(uart_get_baud_rate);
459 * uart_get_divisor - return uart clock divisor
460 * @port: uart_port structure describing the port.
461 * @baud: desired baud rate
463 * Calculate the uart clock divisor for the port.
466 uart_get_divisor(struct uart_port *port, unsigned int baud)
471 * Old custom speed handling.
473 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
474 quot = port->custom_divisor;
476 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
480 EXPORT_SYMBOL(uart_get_divisor);
482 /* Caller holds port mutex */
483 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
484 struct ktermios *old_termios)
486 struct uart_port *uport = uart_port_check(state);
487 struct ktermios *termios;
491 * If we have no tty, termios, or the port does not exist,
492 * then we can't set the parameters for this port.
494 if (!tty || uport->type == PORT_UNKNOWN)
497 termios = &tty->termios;
498 uport->ops->set_termios(uport, termios, old_termios);
501 * Set modem status enables based on termios cflag
503 spin_lock_irq(&uport->lock);
504 if (termios->c_cflag & CRTSCTS)
505 uport->status |= UPSTAT_CTS_ENABLE;
507 uport->status &= ~UPSTAT_CTS_ENABLE;
509 if (termios->c_cflag & CLOCAL)
510 uport->status &= ~UPSTAT_DCD_ENABLE;
512 uport->status |= UPSTAT_DCD_ENABLE;
514 /* reset sw-assisted CTS flow control based on (possibly) new mode */
515 hw_stopped = uport->hw_stopped;
516 uport->hw_stopped = uart_softcts_mode(uport) &&
517 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
518 if (uport->hw_stopped) {
520 uport->ops->stop_tx(uport);
525 spin_unlock_irq(&uport->lock);
528 static int uart_put_char(struct tty_struct *tty, unsigned char c)
530 struct uart_state *state = tty->driver_data;
531 struct uart_port *port;
532 struct circ_buf *circ;
537 port = uart_port_lock(state, flags);
539 uart_port_unlock(port, flags);
543 if (port && uart_circ_chars_free(circ) != 0) {
544 circ->buf[circ->head] = c;
545 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
548 uart_port_unlock(port, flags);
552 static void uart_flush_chars(struct tty_struct *tty)
557 static int uart_write(struct tty_struct *tty,
558 const unsigned char *buf, int count)
560 struct uart_state *state = tty->driver_data;
561 struct uart_port *port;
562 struct circ_buf *circ;
567 * This means you called this function _after_ the port was
568 * closed. No cookie for you.
575 port = uart_port_lock(state, flags);
578 uart_port_unlock(port, flags);
583 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
588 memcpy(circ->buf + circ->head, buf, c);
589 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
596 uart_port_unlock(port, flags);
600 static unsigned int uart_write_room(struct tty_struct *tty)
602 struct uart_state *state = tty->driver_data;
603 struct uart_port *port;
607 port = uart_port_lock(state, flags);
608 ret = uart_circ_chars_free(&state->xmit);
609 uart_port_unlock(port, flags);
613 static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
615 struct uart_state *state = tty->driver_data;
616 struct uart_port *port;
620 port = uart_port_lock(state, flags);
621 ret = uart_circ_chars_pending(&state->xmit);
622 uart_port_unlock(port, flags);
626 static void uart_flush_buffer(struct tty_struct *tty)
628 struct uart_state *state = tty->driver_data;
629 struct uart_port *port;
633 * This means you called this function _after_ the port was
634 * closed. No cookie for you.
641 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
643 port = uart_port_lock(state, flags);
646 uart_circ_clear(&state->xmit);
647 if (port->ops->flush_buffer)
648 port->ops->flush_buffer(port);
649 uart_port_unlock(port, flags);
650 tty_port_tty_wakeup(&state->port);
654 * This function performs low-level write of high-priority XON/XOFF
655 * character and accounting for it.
657 * Requires uart_port to implement .serial_out().
659 void uart_xchar_out(struct uart_port *uport, int offset)
661 serial_port_out(uport, offset, uport->x_char);
665 EXPORT_SYMBOL_GPL(uart_xchar_out);
668 * This function is used to send a high-priority XON/XOFF character to
671 static void uart_send_xchar(struct tty_struct *tty, char ch)
673 struct uart_state *state = tty->driver_data;
674 struct uart_port *port;
677 port = uart_port_ref(state);
681 if (port->ops->send_xchar)
682 port->ops->send_xchar(port, ch);
684 spin_lock_irqsave(&port->lock, flags);
687 port->ops->start_tx(port);
688 spin_unlock_irqrestore(&port->lock, flags);
690 uart_port_deref(port);
693 static void uart_throttle(struct tty_struct *tty)
695 struct uart_state *state = tty->driver_data;
696 upstat_t mask = UPSTAT_SYNC_FIFO;
697 struct uart_port *port;
699 port = uart_port_ref(state);
704 mask |= UPSTAT_AUTOXOFF;
706 mask |= UPSTAT_AUTORTS;
708 if (port->status & mask) {
709 port->ops->throttle(port);
710 mask &= ~port->status;
713 if (mask & UPSTAT_AUTORTS)
714 uart_clear_mctrl(port, TIOCM_RTS);
716 if (mask & UPSTAT_AUTOXOFF)
717 uart_send_xchar(tty, STOP_CHAR(tty));
719 uart_port_deref(port);
722 static void uart_unthrottle(struct tty_struct *tty)
724 struct uart_state *state = tty->driver_data;
725 upstat_t mask = UPSTAT_SYNC_FIFO;
726 struct uart_port *port;
728 port = uart_port_ref(state);
733 mask |= UPSTAT_AUTOXOFF;
735 mask |= UPSTAT_AUTORTS;
737 if (port->status & mask) {
738 port->ops->unthrottle(port);
739 mask &= ~port->status;
742 if (mask & UPSTAT_AUTORTS)
743 uart_set_mctrl(port, TIOCM_RTS);
745 if (mask & UPSTAT_AUTOXOFF)
746 uart_send_xchar(tty, START_CHAR(tty));
748 uart_port_deref(port);
751 static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
753 struct uart_state *state = container_of(port, struct uart_state, port);
754 struct uart_port *uport;
758 * Ensure the state we copy is consistent and no hardware changes
761 mutex_lock(&port->mutex);
762 uport = uart_port_check(state);
766 retinfo->type = uport->type;
767 retinfo->line = uport->line;
768 retinfo->port = uport->iobase;
769 if (HIGH_BITS_OFFSET)
770 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
771 retinfo->irq = uport->irq;
772 retinfo->flags = (__force int)uport->flags;
773 retinfo->xmit_fifo_size = uport->fifosize;
774 retinfo->baud_base = uport->uartclk / 16;
775 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
776 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
777 ASYNC_CLOSING_WAIT_NONE :
778 jiffies_to_msecs(port->closing_wait) / 10;
779 retinfo->custom_divisor = uport->custom_divisor;
780 retinfo->hub6 = uport->hub6;
781 retinfo->io_type = uport->iotype;
782 retinfo->iomem_reg_shift = uport->regshift;
783 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
787 mutex_unlock(&port->mutex);
791 static int uart_get_info_user(struct tty_struct *tty,
792 struct serial_struct *ss)
794 struct uart_state *state = tty->driver_data;
795 struct tty_port *port = &state->port;
797 return uart_get_info(port, ss) < 0 ? -EIO : 0;
800 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
801 struct uart_state *state,
802 struct serial_struct *new_info)
804 struct uart_port *uport = uart_port_check(state);
805 unsigned long new_port;
806 unsigned int change_irq, change_port, closing_wait;
807 unsigned int old_custom_divisor, close_delay;
808 upf_t old_flags, new_flags;
814 new_port = new_info->port;
815 if (HIGH_BITS_OFFSET)
816 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
818 new_info->irq = irq_canonicalize(new_info->irq);
819 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
820 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
821 ASYNC_CLOSING_WAIT_NONE :
822 msecs_to_jiffies(new_info->closing_wait * 10);
825 change_irq = !(uport->flags & UPF_FIXED_PORT)
826 && new_info->irq != uport->irq;
829 * Since changing the 'type' of the port changes its resource
830 * allocations, we should treat type changes the same as
833 change_port = !(uport->flags & UPF_FIXED_PORT)
834 && (new_port != uport->iobase ||
835 (unsigned long)new_info->iomem_base != uport->mapbase ||
836 new_info->hub6 != uport->hub6 ||
837 new_info->io_type != uport->iotype ||
838 new_info->iomem_reg_shift != uport->regshift ||
839 new_info->type != uport->type);
841 old_flags = uport->flags;
842 new_flags = (__force upf_t)new_info->flags;
843 old_custom_divisor = uport->custom_divisor;
845 if (!capable(CAP_SYS_ADMIN)) {
847 if (change_irq || change_port ||
848 (new_info->baud_base != uport->uartclk / 16) ||
849 (close_delay != port->close_delay) ||
850 (closing_wait != port->closing_wait) ||
851 (new_info->xmit_fifo_size &&
852 new_info->xmit_fifo_size != uport->fifosize) ||
853 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
855 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
856 (new_flags & UPF_USR_MASK));
857 uport->custom_divisor = new_info->custom_divisor;
861 if (change_irq || change_port) {
862 retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
868 * Ask the low level driver to verify the settings.
870 if (uport->ops->verify_port)
871 retval = uport->ops->verify_port(uport, new_info);
873 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
874 (new_info->baud_base < 9600))
880 if (change_port || change_irq) {
884 * Make sure that we are the sole user of this port.
886 if (tty_port_users(port) > 1)
890 * We need to shutdown the serial port at the old
891 * port/type/irq combination.
893 uart_shutdown(tty, state);
897 unsigned long old_iobase, old_mapbase;
898 unsigned int old_type, old_iotype, old_hub6, old_shift;
900 old_iobase = uport->iobase;
901 old_mapbase = uport->mapbase;
902 old_type = uport->type;
903 old_hub6 = uport->hub6;
904 old_iotype = uport->iotype;
905 old_shift = uport->regshift;
908 * Free and release old regions
910 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
911 uport->ops->release_port(uport);
913 uport->iobase = new_port;
914 uport->type = new_info->type;
915 uport->hub6 = new_info->hub6;
916 uport->iotype = new_info->io_type;
917 uport->regshift = new_info->iomem_reg_shift;
918 uport->mapbase = (unsigned long)new_info->iomem_base;
921 * Claim and map the new regions
923 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
924 retval = uport->ops->request_port(uport);
926 /* Always success - Jean II */
931 * If we fail to request resources for the
932 * new port, try to restore the old settings.
935 uport->iobase = old_iobase;
936 uport->type = old_type;
937 uport->hub6 = old_hub6;
938 uport->iotype = old_iotype;
939 uport->regshift = old_shift;
940 uport->mapbase = old_mapbase;
942 if (old_type != PORT_UNKNOWN) {
943 retval = uport->ops->request_port(uport);
945 * If we failed to restore the old settings,
949 uport->type = PORT_UNKNOWN;
957 /* Added to return the correct error -Ram Gupta */
963 uport->irq = new_info->irq;
964 if (!(uport->flags & UPF_FIXED_PORT))
965 uport->uartclk = new_info->baud_base * 16;
966 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
967 (new_flags & UPF_CHANGE_MASK);
968 uport->custom_divisor = new_info->custom_divisor;
969 port->close_delay = close_delay;
970 port->closing_wait = closing_wait;
971 if (new_info->xmit_fifo_size)
972 uport->fifosize = new_info->xmit_fifo_size;
976 if (uport->type == PORT_UNKNOWN)
978 if (tty_port_initialized(port)) {
979 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
980 old_custom_divisor != uport->custom_divisor) {
982 * If they're setting up a custom divisor or speed,
983 * instead of clearing it, then bitch about it.
985 if (uport->flags & UPF_SPD_MASK) {
986 dev_notice_ratelimited(uport->dev,
987 "%s sets custom speed on %s. This is deprecated.\n",
989 tty_name(port->tty));
991 uart_change_speed(tty, state, NULL);
994 retval = uart_startup(tty, state, 1);
996 tty_port_set_initialized(port, true);
1004 static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
1006 struct uart_state *state = tty->driver_data;
1007 struct tty_port *port = &state->port;
1010 down_write(&tty->termios_rwsem);
1012 * This semaphore protects port->count. It is also
1013 * very useful to prevent opens. Also, take the
1014 * port configuration semaphore to make sure that a
1015 * module insertion/removal doesn't change anything
1018 mutex_lock(&port->mutex);
1019 retval = uart_set_info(tty, port, state, ss);
1020 mutex_unlock(&port->mutex);
1021 up_write(&tty->termios_rwsem);
1026 * uart_get_lsr_info - get line status register info
1027 * @tty: tty associated with the UART
1028 * @state: UART being queried
1029 * @value: returned modem value
1031 static int uart_get_lsr_info(struct tty_struct *tty,
1032 struct uart_state *state, unsigned int __user *value)
1034 struct uart_port *uport = uart_port_check(state);
1035 unsigned int result;
1037 result = uport->ops->tx_empty(uport);
1040 * If we're about to load something into the transmit
1041 * register, we'll pretend the transmitter isn't empty to
1042 * avoid a race condition (depending on when the transmit
1043 * interrupt happens).
1045 if (uport->x_char ||
1046 ((uart_circ_chars_pending(&state->xmit) > 0) &&
1047 !uart_tx_stopped(uport)))
1048 result &= ~TIOCSER_TEMT;
1050 return put_user(result, value);
1053 static int uart_tiocmget(struct tty_struct *tty)
1055 struct uart_state *state = tty->driver_data;
1056 struct tty_port *port = &state->port;
1057 struct uart_port *uport;
1060 mutex_lock(&port->mutex);
1061 uport = uart_port_check(state);
1065 if (!tty_io_error(tty)) {
1066 result = uport->mctrl;
1067 spin_lock_irq(&uport->lock);
1068 result |= uport->ops->get_mctrl(uport);
1069 spin_unlock_irq(&uport->lock);
1072 mutex_unlock(&port->mutex);
1077 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1079 struct uart_state *state = tty->driver_data;
1080 struct tty_port *port = &state->port;
1081 struct uart_port *uport;
1084 mutex_lock(&port->mutex);
1085 uport = uart_port_check(state);
1089 if (!tty_io_error(tty)) {
1090 uart_update_mctrl(uport, set, clear);
1094 mutex_unlock(&port->mutex);
1098 static int uart_break_ctl(struct tty_struct *tty, int break_state)
1100 struct uart_state *state = tty->driver_data;
1101 struct tty_port *port = &state->port;
1102 struct uart_port *uport;
1105 mutex_lock(&port->mutex);
1106 uport = uart_port_check(state);
1110 if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1111 uport->ops->break_ctl(uport, break_state);
1114 mutex_unlock(&port->mutex);
1118 static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
1120 struct tty_port *port = &state->port;
1121 struct uart_port *uport;
1124 if (!capable(CAP_SYS_ADMIN))
1128 * Take the per-port semaphore. This prevents count from
1129 * changing, and hence any extra opens of the port while
1130 * we're auto-configuring.
1132 if (mutex_lock_interruptible(&port->mutex))
1133 return -ERESTARTSYS;
1135 uport = uart_port_check(state);
1142 if (tty_port_users(port) == 1) {
1143 uart_shutdown(tty, state);
1146 * If we already have a port type configured,
1147 * we must release its resources.
1149 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1150 uport->ops->release_port(uport);
1152 flags = UART_CONFIG_TYPE;
1153 if (uport->flags & UPF_AUTO_IRQ)
1154 flags |= UART_CONFIG_IRQ;
1157 * This will claim the ports resources if
1160 uport->ops->config_port(uport, flags);
1162 ret = uart_startup(tty, state, 1);
1164 tty_port_set_initialized(port, true);
1169 mutex_unlock(&port->mutex);
1173 static void uart_enable_ms(struct uart_port *uport)
1176 * Force modem status interrupts on
1178 if (uport->ops->enable_ms)
1179 uport->ops->enable_ms(uport);
1183 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1184 * - mask passed in arg for lines of interest
1185 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1186 * Caller should use TIOCGICOUNT to see which one it was
1188 * FIXME: This wants extracting into a common all driver implementation
1189 * of TIOCMWAIT using tty_port.
1191 static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1193 struct uart_port *uport;
1194 struct tty_port *port = &state->port;
1195 DECLARE_WAITQUEUE(wait, current);
1196 struct uart_icount cprev, cnow;
1200 * note the counters on entry
1202 uport = uart_port_ref(state);
1205 spin_lock_irq(&uport->lock);
1206 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1207 uart_enable_ms(uport);
1208 spin_unlock_irq(&uport->lock);
1210 add_wait_queue(&port->delta_msr_wait, &wait);
1212 spin_lock_irq(&uport->lock);
1213 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1214 spin_unlock_irq(&uport->lock);
1216 set_current_state(TASK_INTERRUPTIBLE);
1218 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1219 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1220 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1221 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1228 /* see if a signal did it */
1229 if (signal_pending(current)) {
1236 __set_current_state(TASK_RUNNING);
1237 remove_wait_queue(&port->delta_msr_wait, &wait);
1238 uart_port_deref(uport);
1244 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1245 * Return: write counters to the user passed counter struct
1246 * NB: both 1->0 and 0->1 transitions are counted except for
1247 * RI where only 0->1 is counted.
1249 static int uart_get_icount(struct tty_struct *tty,
1250 struct serial_icounter_struct *icount)
1252 struct uart_state *state = tty->driver_data;
1253 struct uart_icount cnow;
1254 struct uart_port *uport;
1256 uport = uart_port_ref(state);
1259 spin_lock_irq(&uport->lock);
1260 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1261 spin_unlock_irq(&uport->lock);
1262 uart_port_deref(uport);
1264 icount->cts = cnow.cts;
1265 icount->dsr = cnow.dsr;
1266 icount->rng = cnow.rng;
1267 icount->dcd = cnow.dcd;
1268 icount->rx = cnow.rx;
1269 icount->tx = cnow.tx;
1270 icount->frame = cnow.frame;
1271 icount->overrun = cnow.overrun;
1272 icount->parity = cnow.parity;
1273 icount->brk = cnow.brk;
1274 icount->buf_overrun = cnow.buf_overrun;
1279 #define SER_RS485_LEGACY_FLAGS (SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
1280 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
1281 SER_RS485_TERMINATE_BUS)
1283 static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
1285 u32 flags = rs485->flags;
1287 /* Don't return -EINVAL for unsupported legacy flags */
1288 flags &= ~SER_RS485_LEGACY_FLAGS;
1291 * For any bit outside of the legacy ones that is not supported by
1292 * the driver, return -EINVAL.
1294 if (flags & ~port->rs485_supported->flags)
1300 static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485)
1302 u32 supported_flags = port->rs485_supported->flags;
1304 if (!(rs485->flags & SER_RS485_ENABLED)) {
1305 memset(rs485, 0, sizeof(*rs485));
1309 /* pick sane settings if the user hasn't */
1310 if ((supported_flags & (SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND)) &&
1311 !(rs485->flags & SER_RS485_RTS_ON_SEND) ==
1312 !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
1313 dev_warn_ratelimited(port->dev,
1314 "%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
1315 port->name, port->line);
1316 rs485->flags |= SER_RS485_RTS_ON_SEND;
1317 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
1318 supported_flags |= SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND;
1321 if (!port->rs485_supported->delay_rts_before_send) {
1322 if (rs485->delay_rts_before_send) {
1323 dev_warn_ratelimited(port->dev,
1324 "%s (%d): RTS delay before sending not supported\n",
1325 port->name, port->line);
1327 rs485->delay_rts_before_send = 0;
1328 } else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) {
1329 rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY;
1330 dev_warn_ratelimited(port->dev,
1331 "%s (%d): RTS delay before sending clamped to %u ms\n",
1332 port->name, port->line, rs485->delay_rts_before_send);
1335 if (!port->rs485_supported->delay_rts_after_send) {
1336 if (rs485->delay_rts_after_send) {
1337 dev_warn_ratelimited(port->dev,
1338 "%s (%d): RTS delay after sending not supported\n",
1339 port->name, port->line);
1341 rs485->delay_rts_after_send = 0;
1342 } else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
1343 rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;
1344 dev_warn_ratelimited(port->dev,
1345 "%s (%d): RTS delay after sending clamped to %u ms\n",
1346 port->name, port->line, rs485->delay_rts_after_send);
1349 rs485->flags &= supported_flags;
1351 /* Return clean padding area to userspace */
1352 memset(rs485->padding, 0, sizeof(rs485->padding));
1355 int uart_rs485_config(struct uart_port *port)
1357 struct serial_rs485 *rs485 = &port->rs485;
1360 uart_sanitize_serial_rs485(port, rs485);
1362 ret = port->rs485_config(port, rs485);
1364 memset(rs485, 0, sizeof(*rs485));
1368 EXPORT_SYMBOL_GPL(uart_rs485_config);
1370 static int uart_get_rs485_config(struct uart_port *port,
1371 struct serial_rs485 __user *rs485)
1373 unsigned long flags;
1374 struct serial_rs485 aux;
1376 spin_lock_irqsave(&port->lock, flags);
1378 spin_unlock_irqrestore(&port->lock, flags);
1380 if (copy_to_user(rs485, &aux, sizeof(aux)))
1386 static int uart_set_rs485_config(struct uart_port *port,
1387 struct serial_rs485 __user *rs485_user)
1389 struct serial_rs485 rs485;
1391 unsigned long flags;
1393 if (!port->rs485_config)
1396 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1399 ret = uart_check_rs485_flags(port, &rs485);
1402 uart_sanitize_serial_rs485(port, &rs485);
1404 spin_lock_irqsave(&port->lock, flags);
1405 ret = port->rs485_config(port, &rs485);
1407 port->rs485 = rs485;
1408 spin_unlock_irqrestore(&port->lock, flags);
1412 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1418 static int uart_get_iso7816_config(struct uart_port *port,
1419 struct serial_iso7816 __user *iso7816)
1421 unsigned long flags;
1422 struct serial_iso7816 aux;
1424 if (!port->iso7816_config)
1427 spin_lock_irqsave(&port->lock, flags);
1428 aux = port->iso7816;
1429 spin_unlock_irqrestore(&port->lock, flags);
1431 if (copy_to_user(iso7816, &aux, sizeof(aux)))
1437 static int uart_set_iso7816_config(struct uart_port *port,
1438 struct serial_iso7816 __user *iso7816_user)
1440 struct serial_iso7816 iso7816;
1442 unsigned long flags;
1444 if (!port->iso7816_config)
1447 if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1451 * There are 5 words reserved for future use. Check that userspace
1452 * doesn't put stuff in there to prevent breakages in the future.
1454 for (i = 0; i < 5; i++)
1455 if (iso7816.reserved[i])
1458 spin_lock_irqsave(&port->lock, flags);
1459 ret = port->iso7816_config(port, &iso7816);
1460 spin_unlock_irqrestore(&port->lock, flags);
1464 if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1471 * Called via sys_ioctl. We can use spin_lock_irq() here.
1474 uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1476 struct uart_state *state = tty->driver_data;
1477 struct tty_port *port = &state->port;
1478 struct uart_port *uport;
1479 void __user *uarg = (void __user *)arg;
1480 int ret = -ENOIOCTLCMD;
1484 * These ioctls don't rely on the hardware to be present.
1488 down_write(&tty->termios_rwsem);
1489 ret = uart_do_autoconfig(tty, state);
1490 up_write(&tty->termios_rwsem);
1494 if (ret != -ENOIOCTLCMD)
1497 if (tty_io_error(tty)) {
1503 * The following should only be used when hardware is present.
1507 ret = uart_wait_modem_status(state, arg);
1511 if (ret != -ENOIOCTLCMD)
1514 mutex_lock(&port->mutex);
1515 uport = uart_port_check(state);
1517 if (!uport || tty_io_error(tty)) {
1523 * All these rely on hardware being present and need to be
1524 * protected against the tty being hung up.
1528 case TIOCSERGETLSR: /* Get line status register */
1529 ret = uart_get_lsr_info(tty, state, uarg);
1533 ret = uart_get_rs485_config(uport, uarg);
1537 ret = uart_set_rs485_config(uport, uarg);
1541 ret = uart_set_iso7816_config(state->uart_port, uarg);
1545 ret = uart_get_iso7816_config(state->uart_port, uarg);
1548 if (uport->ops->ioctl)
1549 ret = uport->ops->ioctl(uport, cmd, arg);
1553 mutex_unlock(&port->mutex);
1558 static void uart_set_ldisc(struct tty_struct *tty)
1560 struct uart_state *state = tty->driver_data;
1561 struct uart_port *uport;
1562 struct tty_port *port = &state->port;
1564 if (!tty_port_initialized(port))
1567 mutex_lock(&state->port.mutex);
1568 uport = uart_port_check(state);
1569 if (uport && uport->ops->set_ldisc)
1570 uport->ops->set_ldisc(uport, &tty->termios);
1571 mutex_unlock(&state->port.mutex);
1574 static void uart_set_termios(struct tty_struct *tty,
1575 struct ktermios *old_termios)
1577 struct uart_state *state = tty->driver_data;
1578 struct uart_port *uport;
1579 unsigned int cflag = tty->termios.c_cflag;
1580 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1581 bool sw_changed = false;
1583 mutex_lock(&state->port.mutex);
1584 uport = uart_port_check(state);
1589 * Drivers doing software flow control also need to know
1590 * about changes to these input settings.
1592 if (uport->flags & UPF_SOFT_FLOW) {
1593 iflag_mask |= IXANY|IXON|IXOFF;
1595 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1596 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1600 * These are the bits that are used to setup various
1601 * flags in the low level driver. We can ignore the Bfoo
1602 * bits in c_cflag; c_[io]speed will always be set
1603 * appropriately by set_termios() in tty_ioctl.c
1605 if ((cflag ^ old_termios->c_cflag) == 0 &&
1606 tty->termios.c_ospeed == old_termios->c_ospeed &&
1607 tty->termios.c_ispeed == old_termios->c_ispeed &&
1608 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1613 uart_change_speed(tty, state, old_termios);
1614 /* reload cflag from termios; port driver may have overridden flags */
1615 cflag = tty->termios.c_cflag;
1617 /* Handle transition to B0 status */
1618 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1619 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1620 /* Handle transition away from B0 status */
1621 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1622 unsigned int mask = TIOCM_DTR;
1624 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1626 uart_set_mctrl(uport, mask);
1629 mutex_unlock(&state->port.mutex);
1633 * Calls to uart_close() are serialised via the tty_lock in
1634 * drivers/tty/tty_io.c:tty_release()
1635 * drivers/tty/tty_io.c:do_tty_hangup()
1637 static void uart_close(struct tty_struct *tty, struct file *filp)
1639 struct uart_state *state = tty->driver_data;
1642 struct uart_driver *drv = tty->driver->driver_state;
1643 struct tty_port *port;
1645 state = drv->state + tty->index;
1646 port = &state->port;
1647 spin_lock_irq(&port->lock);
1649 spin_unlock_irq(&port->lock);
1653 pr_debug("uart_close(%d) called\n", tty->index);
1655 tty_port_close(tty->port, tty, filp);
1658 static void uart_tty_port_shutdown(struct tty_port *port)
1660 struct uart_state *state = container_of(port, struct uart_state, port);
1661 struct uart_port *uport = uart_port_check(state);
1665 * At this point, we stop accepting input. To do this, we
1666 * disable the receive line status interrupts.
1668 if (WARN(!uport, "detached port still initialized!\n"))
1671 spin_lock_irq(&uport->lock);
1672 uport->ops->stop_rx(uport);
1673 spin_unlock_irq(&uport->lock);
1675 uart_port_shutdown(port);
1678 * It's possible for shutdown to be called after suspend if we get
1679 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1680 * we don't try to resume a port that has been shutdown.
1682 tty_port_set_suspended(port, 0);
1685 * Free the transmit buffer.
1687 spin_lock_irq(&uport->lock);
1688 buf = state->xmit.buf;
1689 state->xmit.buf = NULL;
1690 spin_unlock_irq(&uport->lock);
1692 free_page((unsigned long)buf);
1694 uart_change_pm(state, UART_PM_STATE_OFF);
1697 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1699 struct uart_state *state = tty->driver_data;
1700 struct uart_port *port;
1701 unsigned long char_time, expire;
1703 port = uart_port_ref(state);
1707 if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
1708 uart_port_deref(port);
1713 * Set the check interval to be 1/5 of the estimated time to
1714 * send a single character, and make it at least 1. The check
1715 * interval should also be less than the timeout.
1717 * Note: we have to use pretty tight timings here to satisfy
1720 char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL);
1722 if (timeout && timeout < char_time)
1723 char_time = timeout;
1725 if (!uart_cts_enabled(port)) {
1727 * If the transmitter hasn't cleared in twice the approximate
1728 * amount of time to send the entire FIFO, it probably won't
1729 * ever clear. This assumes the UART isn't doing flow
1730 * control, which is currently the case. Hence, if it ever
1731 * takes longer than port->timeout, this is probably due to a
1732 * UART bug of some kind. So, we clamp the timeout parameter at
1735 if (timeout == 0 || timeout > 2 * port->timeout)
1736 timeout = 2 * port->timeout;
1739 expire = jiffies + timeout;
1741 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1742 port->line, jiffies, expire);
1745 * Check whether the transmitter is empty every 'char_time'.
1746 * 'timeout' / 'expire' give us the maximum amount of time
1749 while (!port->ops->tx_empty(port)) {
1750 msleep_interruptible(jiffies_to_msecs(char_time));
1751 if (signal_pending(current))
1753 if (timeout && time_after(jiffies, expire))
1756 uart_port_deref(port);
1760 * Calls to uart_hangup() are serialised by the tty_lock in
1761 * drivers/tty/tty_io.c:do_tty_hangup()
1762 * This runs from a workqueue and can sleep for a _short_ time only.
1764 static void uart_hangup(struct tty_struct *tty)
1766 struct uart_state *state = tty->driver_data;
1767 struct tty_port *port = &state->port;
1768 struct uart_port *uport;
1769 unsigned long flags;
1771 pr_debug("uart_hangup(%d)\n", tty->index);
1773 mutex_lock(&port->mutex);
1774 uport = uart_port_check(state);
1775 WARN(!uport, "hangup of detached port!\n");
1777 if (tty_port_active(port)) {
1778 uart_flush_buffer(tty);
1779 uart_shutdown(tty, state);
1780 spin_lock_irqsave(&port->lock, flags);
1782 spin_unlock_irqrestore(&port->lock, flags);
1783 tty_port_set_active(port, 0);
1784 tty_port_tty_set(port, NULL);
1785 if (uport && !uart_console(uport))
1786 uart_change_pm(state, UART_PM_STATE_OFF);
1787 wake_up_interruptible(&port->open_wait);
1788 wake_up_interruptible(&port->delta_msr_wait);
1790 mutex_unlock(&port->mutex);
1793 /* uport == NULL if uart_port has already been removed */
1794 static void uart_port_shutdown(struct tty_port *port)
1796 struct uart_state *state = container_of(port, struct uart_state, port);
1797 struct uart_port *uport = uart_port_check(state);
1800 * clear delta_msr_wait queue to avoid mem leaks: we may free
1801 * the irq here so the queue might never be woken up. Note
1802 * that we won't end up waiting on delta_msr_wait again since
1803 * any outstanding file descriptors should be pointing at
1804 * hung_up_tty_fops now.
1806 wake_up_interruptible(&port->delta_msr_wait);
1809 /* Free the IRQ and disable the port. */
1810 uport->ops->shutdown(uport);
1812 /* Ensure that the IRQ handler isn't running on another CPU. */
1813 synchronize_irq(uport->irq);
1817 static int uart_carrier_raised(struct tty_port *port)
1819 struct uart_state *state = container_of(port, struct uart_state, port);
1820 struct uart_port *uport;
1823 uport = uart_port_ref(state);
1825 * Should never observe uport == NULL since checks for hangup should
1826 * abort the tty_port_block_til_ready() loop before checking for carrier
1827 * raised -- but report carrier raised if it does anyway so open will
1828 * continue and not sleep
1830 if (WARN_ON(!uport))
1832 spin_lock_irq(&uport->lock);
1833 uart_enable_ms(uport);
1834 mctrl = uport->ops->get_mctrl(uport);
1835 spin_unlock_irq(&uport->lock);
1836 uart_port_deref(uport);
1837 if (mctrl & TIOCM_CAR)
1842 static void uart_dtr_rts(struct tty_port *port, int raise)
1844 struct uart_state *state = container_of(port, struct uart_state, port);
1845 struct uart_port *uport;
1847 uport = uart_port_ref(state);
1850 uart_port_dtr_rts(uport, raise);
1851 uart_port_deref(uport);
1854 static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
1856 struct uart_driver *drv = driver->driver_state;
1857 struct uart_state *state = drv->state + tty->index;
1859 tty->driver_data = state;
1861 return tty_standard_install(driver, tty);
1865 * Calls to uart_open are serialised by the tty_lock in
1866 * drivers/tty/tty_io.c:tty_open()
1867 * Note that if this fails, then uart_close() _will_ be called.
1869 * In time, we want to scrap the "opening nonpresent ports"
1870 * behaviour and implement an alternative way for setserial
1871 * to set base addresses/ports/types. This will allow us to
1872 * get rid of a certain amount of extra tests.
1874 static int uart_open(struct tty_struct *tty, struct file *filp)
1876 struct uart_state *state = tty->driver_data;
1879 retval = tty_port_open(&state->port, tty, filp);
1886 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1888 struct uart_state *state = container_of(port, struct uart_state, port);
1889 struct uart_port *uport;
1892 uport = uart_port_check(state);
1893 if (!uport || uport->flags & UPF_DEAD)
1897 * Start up the serial port.
1899 ret = uart_startup(tty, state, 0);
1901 tty_port_set_active(port, 1);
1906 static const char *uart_type(struct uart_port *port)
1908 const char *str = NULL;
1910 if (port->ops->type)
1911 str = port->ops->type(port);
1919 #ifdef CONFIG_PROC_FS
1921 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1923 struct uart_state *state = drv->state + i;
1924 struct tty_port *port = &state->port;
1925 enum uart_pm_state pm_state;
1926 struct uart_port *uport;
1928 unsigned int status;
1931 mutex_lock(&port->mutex);
1932 uport = uart_port_check(state);
1936 mmio = uport->iotype >= UPIO_MEM;
1937 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1938 uport->line, uart_type(uport),
1939 mmio ? "mmio:0x" : "port:",
1940 mmio ? (unsigned long long)uport->mapbase
1941 : (unsigned long long)uport->iobase,
1944 if (uport->type == PORT_UNKNOWN) {
1949 if (capable(CAP_SYS_ADMIN)) {
1950 pm_state = state->pm_state;
1951 if (pm_state != UART_PM_STATE_ON)
1952 uart_change_pm(state, UART_PM_STATE_ON);
1953 spin_lock_irq(&uport->lock);
1954 status = uport->ops->get_mctrl(uport);
1955 spin_unlock_irq(&uport->lock);
1956 if (pm_state != UART_PM_STATE_ON)
1957 uart_change_pm(state, pm_state);
1959 seq_printf(m, " tx:%d rx:%d",
1960 uport->icount.tx, uport->icount.rx);
1961 if (uport->icount.frame)
1962 seq_printf(m, " fe:%d", uport->icount.frame);
1963 if (uport->icount.parity)
1964 seq_printf(m, " pe:%d", uport->icount.parity);
1965 if (uport->icount.brk)
1966 seq_printf(m, " brk:%d", uport->icount.brk);
1967 if (uport->icount.overrun)
1968 seq_printf(m, " oe:%d", uport->icount.overrun);
1969 if (uport->icount.buf_overrun)
1970 seq_printf(m, " bo:%d", uport->icount.buf_overrun);
1972 #define INFOBIT(bit, str) \
1973 if (uport->mctrl & (bit)) \
1974 strncat(stat_buf, (str), sizeof(stat_buf) - \
1975 strlen(stat_buf) - 2)
1976 #define STATBIT(bit, str) \
1977 if (status & (bit)) \
1978 strncat(stat_buf, (str), sizeof(stat_buf) - \
1979 strlen(stat_buf) - 2)
1983 INFOBIT(TIOCM_RTS, "|RTS");
1984 STATBIT(TIOCM_CTS, "|CTS");
1985 INFOBIT(TIOCM_DTR, "|DTR");
1986 STATBIT(TIOCM_DSR, "|DSR");
1987 STATBIT(TIOCM_CAR, "|CD");
1988 STATBIT(TIOCM_RNG, "|RI");
1992 seq_puts(m, stat_buf);
1998 mutex_unlock(&port->mutex);
2001 static int uart_proc_show(struct seq_file *m, void *v)
2003 struct tty_driver *ttydrv = m->private;
2004 struct uart_driver *drv = ttydrv->driver_state;
2007 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
2008 for (i = 0; i < drv->nr; i++)
2009 uart_line_info(m, drv, i);
2014 static inline bool uart_console_enabled(struct uart_port *port)
2016 return uart_console(port) && (port->cons->flags & CON_ENABLED);
2019 static void uart_port_spin_lock_init(struct uart_port *port)
2021 spin_lock_init(&port->lock);
2022 lockdep_set_class(&port->lock, &port_lock_key);
2025 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
2027 * uart_console_write - write a console message to a serial port
2028 * @port: the port to write the message
2029 * @s: array of characters
2030 * @count: number of characters in string to write
2031 * @putchar: function to write character to port
2033 void uart_console_write(struct uart_port *port, const char *s,
2035 void (*putchar)(struct uart_port *, unsigned char))
2039 for (i = 0; i < count; i++, s++) {
2041 putchar(port, '\r');
2045 EXPORT_SYMBOL_GPL(uart_console_write);
2048 * Check whether an invalid uart number has been specified, and
2049 * if so, search for the first available port that does have
2052 struct uart_port * __init
2053 uart_get_console(struct uart_port *ports, int nr, struct console *co)
2055 int idx = co->index;
2057 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
2058 ports[idx].membase == NULL))
2059 for (idx = 0; idx < nr; idx++)
2060 if (ports[idx].iobase != 0 ||
2061 ports[idx].membase != NULL)
2070 * uart_parse_earlycon - Parse earlycon options
2071 * @p: ptr to 2nd field (ie., just beyond '<name>,')
2072 * @iotype: ptr for decoded iotype (out)
2073 * @addr: ptr for decoded mapbase/iobase (out)
2074 * @options: ptr for <options> field; NULL if not present (out)
2076 * Decodes earlycon kernel command line parameters of the form
2077 * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2078 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2082 * earlycon=<name>,0x<addr>,<options>
2083 * console=<name>,0x<addr>,<options>
2085 * is also accepted; the returned @iotype will be UPIO_MEM.
2087 * Returns 0 on success or -EINVAL on failure
2089 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
2092 if (strncmp(p, "mmio,", 5) == 0) {
2095 } else if (strncmp(p, "mmio16,", 7) == 0) {
2096 *iotype = UPIO_MEM16;
2098 } else if (strncmp(p, "mmio32,", 7) == 0) {
2099 *iotype = UPIO_MEM32;
2101 } else if (strncmp(p, "mmio32be,", 9) == 0) {
2102 *iotype = UPIO_MEM32BE;
2104 } else if (strncmp(p, "mmio32native,", 13) == 0) {
2105 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
2106 UPIO_MEM32BE : UPIO_MEM32;
2108 } else if (strncmp(p, "io,", 3) == 0) {
2109 *iotype = UPIO_PORT;
2111 } else if (strncmp(p, "0x", 2) == 0) {
2118 * Before you replace it with kstrtoull(), think about options separator
2119 * (',') it will not tolerate
2121 *addr = simple_strtoull(p, NULL, 0);
2129 EXPORT_SYMBOL_GPL(uart_parse_earlycon);
2132 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
2133 * @options: pointer to option string
2134 * @baud: pointer to an 'int' variable for the baud rate.
2135 * @parity: pointer to an 'int' variable for the parity.
2136 * @bits: pointer to an 'int' variable for the number of data bits.
2137 * @flow: pointer to an 'int' variable for the flow control character.
2139 * uart_parse_options decodes a string containing the serial console
2140 * options. The format of the string is <baud><parity><bits><flow>,
2144 uart_parse_options(const char *options, int *baud, int *parity,
2145 int *bits, int *flow)
2147 const char *s = options;
2149 *baud = simple_strtoul(s, NULL, 10);
2150 while (*s >= '0' && *s <= '9')
2159 EXPORT_SYMBOL_GPL(uart_parse_options);
2162 * uart_set_options - setup the serial console parameters
2163 * @port: pointer to the serial ports uart_port structure
2164 * @co: console pointer
2166 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2167 * @bits: number of data bits
2168 * @flow: flow control character - 'r' (rts)
2171 uart_set_options(struct uart_port *port, struct console *co,
2172 int baud, int parity, int bits, int flow)
2174 struct ktermios termios;
2175 static struct ktermios dummy;
2178 * Ensure that the serial-console lock is initialised early.
2180 * Note that the console-enabled check is needed because of kgdboc,
2181 * which can end up calling uart_set_options() for an already enabled
2182 * console via tty_find_polling_driver() and uart_poll_init().
2184 if (!uart_console_enabled(port) && !port->console_reinit)
2185 uart_port_spin_lock_init(port);
2187 memset(&termios, 0, sizeof(struct ktermios));
2189 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2190 tty_termios_encode_baud_rate(&termios, baud, baud);
2193 termios.c_cflag |= CS7;
2195 termios.c_cflag |= CS8;
2199 termios.c_cflag |= PARODD;
2202 termios.c_cflag |= PARENB;
2207 termios.c_cflag |= CRTSCTS;
2210 * some uarts on other side don't support no flow control.
2211 * So we set * DTR in host uart to make them happy
2213 port->mctrl |= TIOCM_DTR;
2215 port->ops->set_termios(port, &termios, &dummy);
2217 * Allow the setting of the UART parameters with a NULL console
2221 co->cflag = termios.c_cflag;
2222 co->ispeed = termios.c_ispeed;
2223 co->ospeed = termios.c_ospeed;
2228 EXPORT_SYMBOL_GPL(uart_set_options);
2229 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2232 * uart_change_pm - set power state of the port
2234 * @state: port descriptor
2235 * @pm_state: new state
2237 * Locking: port->mutex has to be held
2239 static void uart_change_pm(struct uart_state *state,
2240 enum uart_pm_state pm_state)
2242 struct uart_port *port = uart_port_check(state);
2244 if (state->pm_state != pm_state) {
2245 if (port && port->ops->pm)
2246 port->ops->pm(port, pm_state, state->pm_state);
2247 state->pm_state = pm_state;
2252 struct uart_port *port;
2253 struct uart_driver *driver;
2256 static int serial_match_port(struct device *dev, void *data)
2258 struct uart_match *match = data;
2259 struct tty_driver *tty_drv = match->driver->tty_driver;
2260 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2263 return dev->devt == devt; /* Actually, only one tty per port */
2266 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2268 struct uart_state *state = drv->state + uport->line;
2269 struct tty_port *port = &state->port;
2270 struct device *tty_dev;
2271 struct uart_match match = {uport, drv};
2273 mutex_lock(&port->mutex);
2275 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2276 if (tty_dev && device_may_wakeup(tty_dev)) {
2277 enable_irq_wake(uport->irq);
2278 put_device(tty_dev);
2279 mutex_unlock(&port->mutex);
2282 put_device(tty_dev);
2285 * Nothing to do if the console is not suspending
2286 * except stop_rx to prevent any asynchronous data
2287 * over RX line. However ensure that we will be
2288 * able to Re-start_rx later.
2290 if (!console_suspend_enabled && uart_console(uport)) {
2291 if (uport->ops->start_rx)
2292 uport->ops->stop_rx(uport);
2296 uport->suspended = 1;
2298 if (tty_port_initialized(port)) {
2299 const struct uart_ops *ops = uport->ops;
2303 tty_port_set_suspended(port, 1);
2304 tty_port_set_initialized(port, 0);
2306 spin_lock_irq(&uport->lock);
2307 ops->stop_tx(uport);
2308 ops->set_mctrl(uport, 0);
2309 /* save mctrl so it can be restored on resume */
2310 mctrl = uport->mctrl;
2312 ops->stop_rx(uport);
2313 spin_unlock_irq(&uport->lock);
2316 * Wait for the transmitter to empty.
2318 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2321 dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2324 ops->shutdown(uport);
2325 uport->mctrl = mctrl;
2329 * Disable the console device before suspending.
2331 if (uart_console(uport))
2332 console_stop(uport->cons);
2334 uart_change_pm(state, UART_PM_STATE_OFF);
2336 mutex_unlock(&port->mutex);
2340 EXPORT_SYMBOL(uart_suspend_port);
2342 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2344 struct uart_state *state = drv->state + uport->line;
2345 struct tty_port *port = &state->port;
2346 struct device *tty_dev;
2347 struct uart_match match = {uport, drv};
2348 struct ktermios termios;
2350 mutex_lock(&port->mutex);
2352 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2353 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2354 if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2355 disable_irq_wake(uport->irq);
2356 put_device(tty_dev);
2357 mutex_unlock(&port->mutex);
2360 put_device(tty_dev);
2361 uport->suspended = 0;
2364 * Re-enable the console device after suspending.
2366 if (uart_console(uport)) {
2368 * First try to use the console cflag setting.
2370 memset(&termios, 0, sizeof(struct ktermios));
2371 termios.c_cflag = uport->cons->cflag;
2372 termios.c_ispeed = uport->cons->ispeed;
2373 termios.c_ospeed = uport->cons->ospeed;
2376 * If that's unset, use the tty termios setting.
2378 if (port->tty && termios.c_cflag == 0)
2379 termios = port->tty->termios;
2381 if (console_suspend_enabled)
2382 uart_change_pm(state, UART_PM_STATE_ON);
2383 uport->ops->set_termios(uport, &termios, NULL);
2384 if (!console_suspend_enabled && uport->ops->start_rx)
2385 uport->ops->start_rx(uport);
2386 if (console_suspend_enabled)
2387 console_start(uport->cons);
2390 if (tty_port_suspended(port)) {
2391 const struct uart_ops *ops = uport->ops;
2394 uart_change_pm(state, UART_PM_STATE_ON);
2395 spin_lock_irq(&uport->lock);
2396 ops->set_mctrl(uport, 0);
2397 spin_unlock_irq(&uport->lock);
2398 if (console_suspend_enabled || !uart_console(uport)) {
2399 /* Protected by port mutex for now */
2400 struct tty_struct *tty = port->tty;
2402 ret = ops->startup(uport);
2405 uart_change_speed(tty, state, NULL);
2406 spin_lock_irq(&uport->lock);
2407 ops->set_mctrl(uport, uport->mctrl);
2408 ops->start_tx(uport);
2409 spin_unlock_irq(&uport->lock);
2410 tty_port_set_initialized(port, 1);
2413 * Failed to resume - maybe hardware went away?
2414 * Clear the "initialized" flag so we won't try
2415 * to call the low level drivers shutdown method.
2417 uart_shutdown(tty, state);
2421 tty_port_set_suspended(port, 0);
2424 mutex_unlock(&port->mutex);
2428 EXPORT_SYMBOL(uart_resume_port);
2431 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2435 switch (port->iotype) {
2437 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2440 snprintf(address, sizeof(address),
2441 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2449 snprintf(address, sizeof(address),
2450 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2453 strlcpy(address, "*unknown*", sizeof(address));
2457 pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
2458 port->dev ? dev_name(port->dev) : "",
2459 port->dev ? ": " : "",
2461 address, port->irq, port->uartclk / 16, uart_type(port));
2463 /* The magic multiplier feature is a bit obscure, so report it too. */
2464 if (port->flags & UPF_MAGIC_MULTIPLIER)
2465 pr_info("%s%s%s extra baud rates supported: %d, %d",
2466 port->dev ? dev_name(port->dev) : "",
2467 port->dev ? ": " : "",
2469 port->uartclk / 8, port->uartclk / 4);
2473 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2474 struct uart_port *port)
2479 * If there isn't a port here, don't do anything further.
2481 if (!port->iobase && !port->mapbase && !port->membase)
2485 * Now do the auto configuration stuff. Note that config_port
2486 * is expected to claim the resources and map the port for us.
2489 if (port->flags & UPF_AUTO_IRQ)
2490 flags |= UART_CONFIG_IRQ;
2491 if (port->flags & UPF_BOOT_AUTOCONF) {
2492 if (!(port->flags & UPF_FIXED_TYPE)) {
2493 port->type = PORT_UNKNOWN;
2494 flags |= UART_CONFIG_TYPE;
2496 port->ops->config_port(port, flags);
2499 if (port->type != PORT_UNKNOWN) {
2500 unsigned long flags;
2502 uart_report_port(drv, port);
2504 /* Power up port for set_mctrl() */
2505 uart_change_pm(state, UART_PM_STATE_ON);
2508 * Ensure that the modem control lines are de-activated.
2509 * keep the DTR setting that is set in uart_set_options()
2510 * We probably don't need a spinlock around this, but
2512 spin_lock_irqsave(&port->lock, flags);
2513 port->mctrl &= TIOCM_DTR;
2514 if (port->rs485.flags & SER_RS485_ENABLED &&
2515 !(port->rs485.flags & SER_RS485_RTS_AFTER_SEND))
2516 port->mctrl |= TIOCM_RTS;
2517 port->ops->set_mctrl(port, port->mctrl);
2518 spin_unlock_irqrestore(&port->lock, flags);
2521 * If this driver supports console, and it hasn't been
2522 * successfully registered yet, try to re-register it.
2523 * It may be that the port was not available.
2525 if (port->cons && !(port->cons->flags & CON_ENABLED))
2526 register_console(port->cons);
2529 * Power down all ports by default, except the
2530 * console if we have one.
2532 if (!uart_console(port))
2533 uart_change_pm(state, UART_PM_STATE_OFF);
2537 #ifdef CONFIG_CONSOLE_POLL
2539 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2541 struct uart_driver *drv = driver->driver_state;
2542 struct uart_state *state = drv->state + line;
2543 struct tty_port *tport;
2544 struct uart_port *port;
2551 tport = &state->port;
2552 mutex_lock(&tport->mutex);
2554 port = uart_port_check(state);
2555 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
2560 if (port->ops->poll_init) {
2562 * We don't set initialized as we only initialized the hw,
2563 * e.g. state->xmit is still uninitialized.
2565 if (!tty_port_initialized(tport))
2566 ret = port->ops->poll_init(port);
2569 if (!ret && options) {
2570 uart_parse_options(options, &baud, &parity, &bits, &flow);
2571 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2574 mutex_unlock(&tport->mutex);
2578 static int uart_poll_get_char(struct tty_driver *driver, int line)
2580 struct uart_driver *drv = driver->driver_state;
2581 struct uart_state *state = drv->state + line;
2582 struct uart_port *port;
2585 port = uart_port_ref(state);
2587 ret = port->ops->poll_get_char(port);
2588 uart_port_deref(port);
2594 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2596 struct uart_driver *drv = driver->driver_state;
2597 struct uart_state *state = drv->state + line;
2598 struct uart_port *port;
2600 port = uart_port_ref(state);
2605 port->ops->poll_put_char(port, '\r');
2606 port->ops->poll_put_char(port, ch);
2607 uart_port_deref(port);
2611 static const struct tty_operations uart_ops = {
2612 .install = uart_install,
2614 .close = uart_close,
2615 .write = uart_write,
2616 .put_char = uart_put_char,
2617 .flush_chars = uart_flush_chars,
2618 .write_room = uart_write_room,
2619 .chars_in_buffer= uart_chars_in_buffer,
2620 .flush_buffer = uart_flush_buffer,
2621 .ioctl = uart_ioctl,
2622 .throttle = uart_throttle,
2623 .unthrottle = uart_unthrottle,
2624 .send_xchar = uart_send_xchar,
2625 .set_termios = uart_set_termios,
2626 .set_ldisc = uart_set_ldisc,
2628 .start = uart_start,
2629 .hangup = uart_hangup,
2630 .break_ctl = uart_break_ctl,
2631 .wait_until_sent= uart_wait_until_sent,
2632 #ifdef CONFIG_PROC_FS
2633 .proc_show = uart_proc_show,
2635 .tiocmget = uart_tiocmget,
2636 .tiocmset = uart_tiocmset,
2637 .set_serial = uart_set_info_user,
2638 .get_serial = uart_get_info_user,
2639 .get_icount = uart_get_icount,
2640 #ifdef CONFIG_CONSOLE_POLL
2641 .poll_init = uart_poll_init,
2642 .poll_get_char = uart_poll_get_char,
2643 .poll_put_char = uart_poll_put_char,
2647 static const struct tty_port_operations uart_port_ops = {
2648 .carrier_raised = uart_carrier_raised,
2649 .dtr_rts = uart_dtr_rts,
2650 .activate = uart_port_activate,
2651 .shutdown = uart_tty_port_shutdown,
2655 * uart_register_driver - register a driver with the uart core layer
2656 * @drv: low level driver structure
2658 * Register a uart driver with the core driver. We in turn register
2659 * with the tty layer, and initialise the core driver per-port state.
2661 * We have a proc file in /proc/tty/driver which is named after the
2664 * drv->port should be NULL, and the per-port structures should be
2665 * registered using uart_add_one_port after this call has succeeded.
2667 int uart_register_driver(struct uart_driver *drv)
2669 struct tty_driver *normal;
2670 int i, retval = -ENOMEM;
2675 * Maybe we should be using a slab cache for this, especially if
2676 * we have a large number of ports to handle.
2678 drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL);
2682 normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
2683 TTY_DRIVER_DYNAMIC_DEV);
2684 if (IS_ERR(normal)) {
2685 retval = PTR_ERR(normal);
2689 drv->tty_driver = normal;
2691 normal->driver_name = drv->driver_name;
2692 normal->name = drv->dev_name;
2693 normal->major = drv->major;
2694 normal->minor_start = drv->minor;
2695 normal->type = TTY_DRIVER_TYPE_SERIAL;
2696 normal->subtype = SERIAL_TYPE_NORMAL;
2697 normal->init_termios = tty_std_termios;
2698 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2699 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2700 normal->driver_state = drv;
2701 tty_set_operations(normal, &uart_ops);
2704 * Initialise the UART state(s).
2706 for (i = 0; i < drv->nr; i++) {
2707 struct uart_state *state = drv->state + i;
2708 struct tty_port *port = &state->port;
2710 tty_port_init(port);
2711 port->ops = &uart_port_ops;
2714 retval = tty_register_driver(normal);
2718 for (i = 0; i < drv->nr; i++)
2719 tty_port_destroy(&drv->state[i].port);
2720 tty_driver_kref_put(normal);
2726 EXPORT_SYMBOL(uart_register_driver);
2729 * uart_unregister_driver - remove a driver from the uart core layer
2730 * @drv: low level driver structure
2732 * Remove all references to a driver from the core driver. The low
2733 * level driver must have removed all its ports via the
2734 * uart_remove_one_port() if it registered them with uart_add_one_port().
2735 * (ie, drv->port == NULL)
2737 void uart_unregister_driver(struct uart_driver *drv)
2739 struct tty_driver *p = drv->tty_driver;
2742 tty_unregister_driver(p);
2743 tty_driver_kref_put(p);
2744 for (i = 0; i < drv->nr; i++)
2745 tty_port_destroy(&drv->state[i].port);
2748 drv->tty_driver = NULL;
2750 EXPORT_SYMBOL(uart_unregister_driver);
2752 struct tty_driver *uart_console_device(struct console *co, int *index)
2754 struct uart_driver *p = co->data;
2756 return p->tty_driver;
2758 EXPORT_SYMBOL_GPL(uart_console_device);
2760 static ssize_t uartclk_show(struct device *dev,
2761 struct device_attribute *attr, char *buf)
2763 struct serial_struct tmp;
2764 struct tty_port *port = dev_get_drvdata(dev);
2766 uart_get_info(port, &tmp);
2767 return sprintf(buf, "%d\n", tmp.baud_base * 16);
2770 static ssize_t type_show(struct device *dev,
2771 struct device_attribute *attr, char *buf)
2773 struct serial_struct tmp;
2774 struct tty_port *port = dev_get_drvdata(dev);
2776 uart_get_info(port, &tmp);
2777 return sprintf(buf, "%d\n", tmp.type);
2780 static ssize_t line_show(struct device *dev,
2781 struct device_attribute *attr, char *buf)
2783 struct serial_struct tmp;
2784 struct tty_port *port = dev_get_drvdata(dev);
2786 uart_get_info(port, &tmp);
2787 return sprintf(buf, "%d\n", tmp.line);
2790 static ssize_t port_show(struct device *dev,
2791 struct device_attribute *attr, char *buf)
2793 struct serial_struct tmp;
2794 struct tty_port *port = dev_get_drvdata(dev);
2795 unsigned long ioaddr;
2797 uart_get_info(port, &tmp);
2799 if (HIGH_BITS_OFFSET)
2800 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2801 return sprintf(buf, "0x%lX\n", ioaddr);
2804 static ssize_t irq_show(struct device *dev,
2805 struct device_attribute *attr, char *buf)
2807 struct serial_struct tmp;
2808 struct tty_port *port = dev_get_drvdata(dev);
2810 uart_get_info(port, &tmp);
2811 return sprintf(buf, "%d\n", tmp.irq);
2814 static ssize_t flags_show(struct device *dev,
2815 struct device_attribute *attr, char *buf)
2817 struct serial_struct tmp;
2818 struct tty_port *port = dev_get_drvdata(dev);
2820 uart_get_info(port, &tmp);
2821 return sprintf(buf, "0x%X\n", tmp.flags);
2824 static ssize_t xmit_fifo_size_show(struct device *dev,
2825 struct device_attribute *attr, char *buf)
2827 struct serial_struct tmp;
2828 struct tty_port *port = dev_get_drvdata(dev);
2830 uart_get_info(port, &tmp);
2831 return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
2834 static ssize_t close_delay_show(struct device *dev,
2835 struct device_attribute *attr, char *buf)
2837 struct serial_struct tmp;
2838 struct tty_port *port = dev_get_drvdata(dev);
2840 uart_get_info(port, &tmp);
2841 return sprintf(buf, "%d\n", tmp.close_delay);
2844 static ssize_t closing_wait_show(struct device *dev,
2845 struct device_attribute *attr, char *buf)
2847 struct serial_struct tmp;
2848 struct tty_port *port = dev_get_drvdata(dev);
2850 uart_get_info(port, &tmp);
2851 return sprintf(buf, "%d\n", tmp.closing_wait);
2854 static ssize_t custom_divisor_show(struct device *dev,
2855 struct device_attribute *attr, char *buf)
2857 struct serial_struct tmp;
2858 struct tty_port *port = dev_get_drvdata(dev);
2860 uart_get_info(port, &tmp);
2861 return sprintf(buf, "%d\n", tmp.custom_divisor);
2864 static ssize_t io_type_show(struct device *dev,
2865 struct device_attribute *attr, char *buf)
2867 struct serial_struct tmp;
2868 struct tty_port *port = dev_get_drvdata(dev);
2870 uart_get_info(port, &tmp);
2871 return sprintf(buf, "%d\n", tmp.io_type);
2874 static ssize_t iomem_base_show(struct device *dev,
2875 struct device_attribute *attr, char *buf)
2877 struct serial_struct tmp;
2878 struct tty_port *port = dev_get_drvdata(dev);
2880 uart_get_info(port, &tmp);
2881 return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
2884 static ssize_t iomem_reg_shift_show(struct device *dev,
2885 struct device_attribute *attr, char *buf)
2887 struct serial_struct tmp;
2888 struct tty_port *port = dev_get_drvdata(dev);
2890 uart_get_info(port, &tmp);
2891 return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
2894 static ssize_t console_show(struct device *dev,
2895 struct device_attribute *attr, char *buf)
2897 struct tty_port *port = dev_get_drvdata(dev);
2898 struct uart_state *state = container_of(port, struct uart_state, port);
2899 struct uart_port *uport;
2900 bool console = false;
2902 mutex_lock(&port->mutex);
2903 uport = uart_port_check(state);
2905 console = uart_console_enabled(uport);
2906 mutex_unlock(&port->mutex);
2908 return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2911 static ssize_t console_store(struct device *dev,
2912 struct device_attribute *attr, const char *buf, size_t count)
2914 struct tty_port *port = dev_get_drvdata(dev);
2915 struct uart_state *state = container_of(port, struct uart_state, port);
2916 struct uart_port *uport;
2917 bool oldconsole, newconsole;
2920 ret = kstrtobool(buf, &newconsole);
2924 mutex_lock(&port->mutex);
2925 uport = uart_port_check(state);
2927 oldconsole = uart_console_enabled(uport);
2928 if (oldconsole && !newconsole) {
2929 ret = unregister_console(uport->cons);
2930 } else if (!oldconsole && newconsole) {
2931 if (uart_console(uport)) {
2932 uport->console_reinit = 1;
2933 register_console(uport->cons);
2941 mutex_unlock(&port->mutex);
2943 return ret < 0 ? ret : count;
2946 static DEVICE_ATTR_RO(uartclk);
2947 static DEVICE_ATTR_RO(type);
2948 static DEVICE_ATTR_RO(line);
2949 static DEVICE_ATTR_RO(port);
2950 static DEVICE_ATTR_RO(irq);
2951 static DEVICE_ATTR_RO(flags);
2952 static DEVICE_ATTR_RO(xmit_fifo_size);
2953 static DEVICE_ATTR_RO(close_delay);
2954 static DEVICE_ATTR_RO(closing_wait);
2955 static DEVICE_ATTR_RO(custom_divisor);
2956 static DEVICE_ATTR_RO(io_type);
2957 static DEVICE_ATTR_RO(iomem_base);
2958 static DEVICE_ATTR_RO(iomem_reg_shift);
2959 static DEVICE_ATTR_RW(console);
2961 static struct attribute *tty_dev_attrs[] = {
2962 &dev_attr_uartclk.attr,
2963 &dev_attr_type.attr,
2964 &dev_attr_line.attr,
2965 &dev_attr_port.attr,
2967 &dev_attr_flags.attr,
2968 &dev_attr_xmit_fifo_size.attr,
2969 &dev_attr_close_delay.attr,
2970 &dev_attr_closing_wait.attr,
2971 &dev_attr_custom_divisor.attr,
2972 &dev_attr_io_type.attr,
2973 &dev_attr_iomem_base.attr,
2974 &dev_attr_iomem_reg_shift.attr,
2975 &dev_attr_console.attr,
2979 static const struct attribute_group tty_dev_attr_group = {
2980 .attrs = tty_dev_attrs,
2984 * uart_add_one_port - attach a driver-defined port structure
2985 * @drv: pointer to the uart low level driver structure for this port
2986 * @uport: uart port structure to use for this port.
2988 * Context: task context, might sleep
2990 * This allows the driver to register its own uart_port structure
2991 * with the core driver. The main purpose is to allow the low
2992 * level uart drivers to expand uart_port, rather than having yet
2993 * more levels of structures.
2995 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2997 struct uart_state *state;
2998 struct tty_port *port;
3000 struct device *tty_dev;
3003 if (uport->line >= drv->nr)
3006 state = drv->state + uport->line;
3007 port = &state->port;
3009 mutex_lock(&port_mutex);
3010 mutex_lock(&port->mutex);
3011 if (state->uart_port) {
3016 /* Link the port to the driver state table and vice versa */
3017 atomic_set(&state->refcount, 1);
3018 init_waitqueue_head(&state->remove_wait);
3019 state->uart_port = uport;
3020 uport->state = state;
3022 state->pm_state = UART_PM_STATE_UNDEFINED;
3023 uport->cons = drv->cons;
3024 uport->minor = drv->tty_driver->minor_start + uport->line;
3025 uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
3026 drv->tty_driver->name_base + uport->line);
3033 * If this port is in use as a console then the spinlock is already
3036 if (!uart_console_enabled(uport))
3037 uart_port_spin_lock_init(uport);
3039 if (uport->cons && uport->dev)
3040 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
3042 tty_port_link_device(port, drv->tty_driver, uport->line);
3043 uart_configure_port(drv, state, uport);
3045 port->console = uart_console(uport);
3048 if (uport->attr_group)
3051 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
3053 if (!uport->tty_groups) {
3057 uport->tty_groups[0] = &tty_dev_attr_group;
3058 if (uport->attr_group)
3059 uport->tty_groups[1] = uport->attr_group;
3062 * Register the port whether it's detected or not. This allows
3063 * setserial to be used to alter this port's parameters.
3065 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
3066 uport->line, uport->dev, port, uport->tty_groups);
3067 if (!IS_ERR(tty_dev)) {
3068 device_set_wakeup_capable(tty_dev, 1);
3070 dev_err(uport->dev, "Cannot register tty device on line %d\n",
3075 * Ensure UPF_DEAD is not set.
3077 uport->flags &= ~UPF_DEAD;
3080 mutex_unlock(&port->mutex);
3081 mutex_unlock(&port_mutex);
3085 EXPORT_SYMBOL(uart_add_one_port);
3088 * uart_remove_one_port - detach a driver defined port structure
3089 * @drv: pointer to the uart low level driver structure for this port
3090 * @uport: uart port structure for this port
3092 * Context: task context, might sleep
3094 * This unhooks (and hangs up) the specified port structure from the
3095 * core driver. No further calls will be made to the low-level code
3098 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
3100 struct uart_state *state = drv->state + uport->line;
3101 struct tty_port *port = &state->port;
3102 struct uart_port *uart_port;
3103 struct tty_struct *tty;
3106 mutex_lock(&port_mutex);
3109 * Mark the port "dead" - this prevents any opens from
3110 * succeeding while we shut down the port.
3112 mutex_lock(&port->mutex);
3113 uart_port = uart_port_check(state);
3114 if (uart_port != uport)
3115 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
3119 mutex_unlock(&port->mutex);
3123 uport->flags |= UPF_DEAD;
3124 mutex_unlock(&port->mutex);
3127 * Remove the devices from the tty layer
3129 tty_port_unregister_device(port, drv->tty_driver, uport->line);
3131 tty = tty_port_tty_get(port);
3133 tty_vhangup(port->tty);
3138 * If the port is used as a console, unregister it
3140 if (uart_console(uport))
3141 unregister_console(uport->cons);
3144 * Free the port IO and memory resources, if any.
3146 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
3147 uport->ops->release_port(uport);
3148 kfree(uport->tty_groups);
3152 * Indicate that there isn't a port here anymore.
3154 uport->type = PORT_UNKNOWN;
3156 mutex_lock(&port->mutex);
3157 WARN_ON(atomic_dec_return(&state->refcount) < 0);
3158 wait_event(state->remove_wait, !atomic_read(&state->refcount));
3159 state->uart_port = NULL;
3160 mutex_unlock(&port->mutex);
3162 mutex_unlock(&port_mutex);
3166 EXPORT_SYMBOL(uart_remove_one_port);
3169 * Are the two ports equivalent?
3171 bool uart_match_port(const struct uart_port *port1,
3172 const struct uart_port *port2)
3174 if (port1->iotype != port2->iotype)
3177 switch (port1->iotype) {
3179 return port1->iobase == port2->iobase;
3181 return port1->iobase == port2->iobase &&
3182 port1->hub6 == port2->hub6;
3189 return port1->mapbase == port2->mapbase;
3194 EXPORT_SYMBOL(uart_match_port);
3197 * uart_handle_dcd_change - handle a change of carrier detect state
3198 * @uport: uart_port structure for the open port
3199 * @status: new carrier detect status, nonzero if active
3201 * Caller must hold uport->lock
3203 void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
3205 struct tty_port *port = &uport->state->port;
3206 struct tty_struct *tty = port->tty;
3207 struct tty_ldisc *ld;
3209 lockdep_assert_held_once(&uport->lock);
3212 ld = tty_ldisc_ref(tty);
3214 if (ld->ops->dcd_change)
3215 ld->ops->dcd_change(tty, status);
3216 tty_ldisc_deref(ld);
3220 uport->icount.dcd++;
3222 if (uart_dcd_enabled(uport)) {
3224 wake_up_interruptible(&port->open_wait);
3229 EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3232 * uart_handle_cts_change - handle a change of clear-to-send state
3233 * @uport: uart_port structure for the open port
3234 * @status: new clear to send status, nonzero if active
3236 * Caller must hold uport->lock
3238 void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
3240 lockdep_assert_held_once(&uport->lock);
3242 uport->icount.cts++;
3244 if (uart_softcts_mode(uport)) {
3245 if (uport->hw_stopped) {
3247 uport->hw_stopped = 0;
3248 uport->ops->start_tx(uport);
3249 uart_write_wakeup(uport);
3253 uport->hw_stopped = 1;
3254 uport->ops->stop_tx(uport);
3260 EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3263 * uart_insert_char - push a char to the uart layer
3265 * User is responsible to call tty_flip_buffer_push when they are done with
3268 * @port: corresponding port
3269 * @status: state of the serial port RX buffer (LSR for 8250)
3270 * @overrun: mask of overrun bits in @status
3271 * @ch: character to push
3272 * @flag: flag for the character (see TTY_NORMAL and friends)
3274 void uart_insert_char(struct uart_port *port, unsigned int status,
3275 unsigned int overrun, unsigned int ch, unsigned int flag)
3277 struct tty_port *tport = &port->state->port;
3279 if ((status & port->ignore_status_mask & ~overrun) == 0)
3280 if (tty_insert_flip_char(tport, ch, flag) == 0)
3281 ++port->icount.buf_overrun;
3284 * Overrun is special. Since it's reported immediately,
3285 * it doesn't affect the current character.
3287 if (status & ~port->ignore_status_mask & overrun)
3288 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3289 ++port->icount.buf_overrun;
3291 EXPORT_SYMBOL_GPL(uart_insert_char);
3293 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
3294 static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
3296 static void uart_sysrq_on(struct work_struct *w)
3298 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3300 sysrq_toggle_support(1);
3301 pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3302 sysrq_toggle_seq_len, sysrq_toggle_seq);
3304 static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
3307 * uart_try_toggle_sysrq - Enables SysRq from serial line
3308 * @port: uart_port structure where char(s) after BREAK met
3309 * @ch: new character in the sequence after received BREAK
3311 * Enables magic SysRq when the required sequence is met on port
3312 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3314 * Returns false if @ch is out of enabling sequence and should be
3315 * handled some other way, true if @ch was consumed.
3317 bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
3319 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3321 if (!sysrq_toggle_seq_len)
3324 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
3325 if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
3326 port->sysrq_seq = 0;
3330 if (++port->sysrq_seq < sysrq_toggle_seq_len) {
3331 port->sysrq = jiffies + SYSRQ_TIMEOUT;
3335 schedule_work(&sysrq_enable_work);
3340 EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
3344 * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3345 * @port: uart device's target port
3347 * This function implements the device tree binding described in
3348 * Documentation/devicetree/bindings/serial/rs485.txt.
3350 int uart_get_rs485_mode(struct uart_port *port)
3352 struct serial_rs485 *rs485conf = &port->rs485;
3353 struct device *dev = port->dev;
3357 ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3360 rs485conf->delay_rts_before_send = rs485_delay[0];
3361 rs485conf->delay_rts_after_send = rs485_delay[1];
3363 rs485conf->delay_rts_before_send = 0;
3364 rs485conf->delay_rts_after_send = 0;
3368 * Clear full-duplex and enabled flags, set RTS polarity to active high
3369 * to get to a defined state with the following properties:
3371 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3372 SER_RS485_TERMINATE_BUS |
3373 SER_RS485_RTS_AFTER_SEND);
3374 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
3376 if (device_property_read_bool(dev, "rs485-rx-during-tx"))
3377 rs485conf->flags |= SER_RS485_RX_DURING_TX;
3379 if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
3380 rs485conf->flags |= SER_RS485_ENABLED;
3382 if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3383 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3384 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3388 * Disabling termination by default is the safe choice: Else if many
3389 * bus participants enable it, no communication is possible at all.
3390 * Works fine for short cables and users may enable for longer cables.
3392 port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3394 if (IS_ERR(port->rs485_term_gpio)) {
3395 ret = PTR_ERR(port->rs485_term_gpio);
3396 port->rs485_term_gpio = NULL;
3397 return dev_err_probe(dev, ret, "Cannot get rs485-term-gpios\n");
3402 EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
3404 MODULE_DESCRIPTION("Serial driver core");
3405 MODULE_LICENSE("GPL");