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 * uart_write_wakeup - schedule write processing
102 * @port: port to be processed
104 * This routine is used by the interrupt handler to schedule processing in the
105 * software interrupt portion of the driver. A driver is expected to call this
106 * function when the number of characters in the transmit buffer have dropped
109 * Locking: @port->lock should be held
111 void uart_write_wakeup(struct uart_port *port)
113 struct uart_state *state = port->state;
115 * This means you called this function _after_ the port was
116 * closed. No cookie for you.
119 tty_port_tty_wakeup(&state->port);
121 EXPORT_SYMBOL(uart_write_wakeup);
123 static void uart_stop(struct tty_struct *tty)
125 struct uart_state *state = tty->driver_data;
126 struct uart_port *port;
129 port = uart_port_lock(state, flags);
131 port->ops->stop_tx(port);
132 uart_port_unlock(port, flags);
135 static void __uart_start(struct tty_struct *tty)
137 struct uart_state *state = tty->driver_data;
138 struct uart_port *port = state->uart_port;
140 if (port && !uart_tx_stopped(port))
141 port->ops->start_tx(port);
144 static void uart_start(struct tty_struct *tty)
146 struct uart_state *state = tty->driver_data;
147 struct uart_port *port;
150 port = uart_port_lock(state, flags);
152 uart_port_unlock(port, flags);
156 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
161 if (port->rs485.flags & SER_RS485_ENABLED) {
166 spin_lock_irqsave(&port->lock, flags);
168 port->mctrl = (old & ~clear) | set;
169 if (old != port->mctrl)
170 port->ops->set_mctrl(port, port->mctrl);
171 spin_unlock_irqrestore(&port->lock, flags);
174 #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
175 #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
177 static void uart_port_dtr_rts(struct uart_port *uport, int raise)
180 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
182 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
186 * Startup the port. This will be called once per open. All calls
187 * will be serialised by the per-port mutex.
189 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
192 struct uart_port *uport = uart_port_check(state);
197 if (uport->type == PORT_UNKNOWN)
201 * Make sure the device is in D0 state.
203 uart_change_pm(state, UART_PM_STATE_ON);
206 * Initialise and allocate the transmit and temporary
209 page = get_zeroed_page(GFP_KERNEL);
213 uart_port_lock(state, flags);
214 if (!state->xmit.buf) {
215 state->xmit.buf = (unsigned char *) page;
216 uart_circ_clear(&state->xmit);
217 uart_port_unlock(uport, flags);
219 uart_port_unlock(uport, flags);
221 * Do not free() the page under the port lock, see
227 retval = uport->ops->startup(uport);
229 if (uart_console(uport) && uport->cons->cflag) {
230 tty->termios.c_cflag = uport->cons->cflag;
231 tty->termios.c_ispeed = uport->cons->ispeed;
232 tty->termios.c_ospeed = uport->cons->ospeed;
233 uport->cons->cflag = 0;
234 uport->cons->ispeed = 0;
235 uport->cons->ospeed = 0;
238 * Initialise the hardware port settings.
240 uart_change_speed(tty, state, NULL);
243 * Setup the RTS and DTR signals once the
244 * port is open and ready to respond.
246 if (init_hw && C_BAUD(tty))
247 uart_port_dtr_rts(uport, 1);
251 * This is to allow setserial on this port. People may want to set
252 * port/irq/type and then reconfigure the port properly if it failed
255 if (retval && capable(CAP_SYS_ADMIN))
261 static int uart_startup(struct tty_struct *tty, struct uart_state *state,
264 struct tty_port *port = &state->port;
267 if (tty_port_initialized(port))
270 retval = uart_port_startup(tty, state, init_hw);
272 set_bit(TTY_IO_ERROR, &tty->flags);
278 * This routine will shutdown a serial port; interrupts are disabled, and
279 * DTR is dropped if the hangup on close termio flag is on. Calls to
280 * uart_shutdown are serialised by the per-port semaphore.
282 * uport == NULL if uart_port has already been removed
284 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
286 struct uart_port *uport = uart_port_check(state);
287 struct tty_port *port = &state->port;
289 char *xmit_buf = NULL;
292 * Set the TTY IO error marker
295 set_bit(TTY_IO_ERROR, &tty->flags);
297 if (tty_port_initialized(port)) {
298 tty_port_set_initialized(port, 0);
301 * Turn off DTR and RTS early.
303 if (uport && uart_console(uport) && tty) {
304 uport->cons->cflag = tty->termios.c_cflag;
305 uport->cons->ispeed = tty->termios.c_ispeed;
306 uport->cons->ospeed = tty->termios.c_ospeed;
309 if (!tty || C_HUPCL(tty))
310 uart_port_dtr_rts(uport, 0);
312 uart_port_shutdown(port);
316 * It's possible for shutdown to be called after suspend if we get
317 * a DCD drop (hangup) at just the right time. Clear suspended bit so
318 * we don't try to resume a port that has been shutdown.
320 tty_port_set_suspended(port, 0);
323 * Do not free() the transmit buffer page under the port lock since
324 * this can create various circular locking scenarios. For instance,
325 * console driver may need to allocate/free a debug object, which
326 * can endup in printk() recursion.
328 uart_port_lock(state, flags);
329 xmit_buf = state->xmit.buf;
330 state->xmit.buf = NULL;
331 uart_port_unlock(uport, flags);
333 free_page((unsigned long)xmit_buf);
337 * uart_update_timeout - update per-port frame timing information
338 * @port: uart_port structure describing the port
339 * @cflag: termios cflag value
340 * @baud: speed of the port
342 * Set the @port frame timing information from which the FIFO timeout value is
343 * derived. The @cflag value should reflect the actual hardware settings as
344 * number of bits, parity, stop bits and baud rate is taken into account here.
346 * Locking: caller is expected to take @port->lock
349 uart_update_timeout(struct uart_port *port, unsigned int cflag,
352 unsigned int size = tty_get_frame_size(cflag);
355 frame_time = (u64)size * NSEC_PER_SEC;
356 port->frame_time = DIV64_U64_ROUND_UP(frame_time, baud);
358 EXPORT_SYMBOL(uart_update_timeout);
361 * uart_get_baud_rate - return baud rate for a particular port
362 * @port: uart_port structure describing the port in question.
363 * @termios: desired termios settings
364 * @old: old termios (or %NULL)
365 * @min: minimum acceptable baud rate
366 * @max: maximum acceptable baud rate
368 * Decode the termios structure into a numeric baud rate, taking account of the
369 * magic 38400 baud rate (with spd_* flags), and mapping the %B0 rate to 9600
372 * If the new baud rate is invalid, try the @old termios setting. If it's still
373 * invalid, we try 9600 baud.
375 * The @termios structure is updated to reflect the baud rate we're actually
376 * going to be using. Don't do this for the case where B0 is requested ("hang
379 * Locking: caller dependent
382 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
383 struct ktermios *old, unsigned int min, unsigned int max)
387 unsigned int altbaud;
389 upf_t flags = port->flags & UPF_SPD_MASK;
409 for (try = 0; try < 2; try++) {
410 baud = tty_termios_baud_rate(termios);
413 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
416 if (try == 0 && baud == 38400)
420 * Special case: B0 rate.
427 if (baud >= min && baud <= max)
431 * Oops, the quotient was zero. Try again with
432 * the old baud rate if possible.
434 termios->c_cflag &= ~CBAUD;
436 baud = tty_termios_baud_rate(old);
438 tty_termios_encode_baud_rate(termios,
445 * As a last resort, if the range cannot be met then clip to
446 * the nearest chip supported rate.
450 tty_termios_encode_baud_rate(termios,
453 tty_termios_encode_baud_rate(termios,
457 /* Should never happen */
461 EXPORT_SYMBOL(uart_get_baud_rate);
464 * uart_get_divisor - return uart clock divisor
465 * @port: uart_port structure describing the port
466 * @baud: desired baud rate
468 * Calculate the divisor (baud_base / baud) for the specified @baud,
469 * appropriately rounded.
471 * If 38400 baud and custom divisor is selected, return the custom divisor
474 * Locking: caller dependent
477 uart_get_divisor(struct uart_port *port, unsigned int baud)
482 * Old custom speed handling.
484 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
485 quot = port->custom_divisor;
487 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
491 EXPORT_SYMBOL(uart_get_divisor);
493 /* Caller holds port mutex */
494 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
495 struct ktermios *old_termios)
497 struct uart_port *uport = uart_port_check(state);
498 struct ktermios *termios;
502 * If we have no tty, termios, or the port does not exist,
503 * then we can't set the parameters for this port.
505 if (!tty || uport->type == PORT_UNKNOWN)
508 termios = &tty->termios;
509 uport->ops->set_termios(uport, termios, old_termios);
512 * Set modem status enables based on termios cflag
514 spin_lock_irq(&uport->lock);
515 if (termios->c_cflag & CRTSCTS)
516 uport->status |= UPSTAT_CTS_ENABLE;
518 uport->status &= ~UPSTAT_CTS_ENABLE;
520 if (termios->c_cflag & CLOCAL)
521 uport->status &= ~UPSTAT_DCD_ENABLE;
523 uport->status |= UPSTAT_DCD_ENABLE;
525 /* reset sw-assisted CTS flow control based on (possibly) new mode */
526 hw_stopped = uport->hw_stopped;
527 uport->hw_stopped = uart_softcts_mode(uport) &&
528 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
529 if (uport->hw_stopped) {
531 uport->ops->stop_tx(uport);
536 spin_unlock_irq(&uport->lock);
539 static int uart_put_char(struct tty_struct *tty, unsigned char c)
541 struct uart_state *state = tty->driver_data;
542 struct uart_port *port;
543 struct circ_buf *circ;
548 port = uart_port_lock(state, flags);
550 uart_port_unlock(port, flags);
554 if (port && uart_circ_chars_free(circ) != 0) {
555 circ->buf[circ->head] = c;
556 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
559 uart_port_unlock(port, flags);
563 static void uart_flush_chars(struct tty_struct *tty)
568 static int uart_write(struct tty_struct *tty,
569 const unsigned char *buf, int count)
571 struct uart_state *state = tty->driver_data;
572 struct uart_port *port;
573 struct circ_buf *circ;
578 * This means you called this function _after_ the port was
579 * closed. No cookie for you.
586 port = uart_port_lock(state, flags);
589 uart_port_unlock(port, flags);
594 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
599 memcpy(circ->buf + circ->head, buf, c);
600 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
607 uart_port_unlock(port, flags);
611 static unsigned int uart_write_room(struct tty_struct *tty)
613 struct uart_state *state = tty->driver_data;
614 struct uart_port *port;
618 port = uart_port_lock(state, flags);
619 ret = uart_circ_chars_free(&state->xmit);
620 uart_port_unlock(port, flags);
624 static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
626 struct uart_state *state = tty->driver_data;
627 struct uart_port *port;
631 port = uart_port_lock(state, flags);
632 ret = uart_circ_chars_pending(&state->xmit);
633 uart_port_unlock(port, flags);
637 static void uart_flush_buffer(struct tty_struct *tty)
639 struct uart_state *state = tty->driver_data;
640 struct uart_port *port;
644 * This means you called this function _after_ the port was
645 * closed. No cookie for you.
652 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
654 port = uart_port_lock(state, flags);
657 uart_circ_clear(&state->xmit);
658 if (port->ops->flush_buffer)
659 port->ops->flush_buffer(port);
660 uart_port_unlock(port, flags);
661 tty_port_tty_wakeup(&state->port);
665 * This function performs low-level write of high-priority XON/XOFF
666 * character and accounting for it.
668 * Requires uart_port to implement .serial_out().
670 void uart_xchar_out(struct uart_port *uport, int offset)
672 serial_port_out(uport, offset, uport->x_char);
676 EXPORT_SYMBOL_GPL(uart_xchar_out);
679 * This function is used to send a high-priority XON/XOFF character to
682 static void uart_send_xchar(struct tty_struct *tty, char ch)
684 struct uart_state *state = tty->driver_data;
685 struct uart_port *port;
688 port = uart_port_ref(state);
692 if (port->ops->send_xchar)
693 port->ops->send_xchar(port, ch);
695 spin_lock_irqsave(&port->lock, flags);
698 port->ops->start_tx(port);
699 spin_unlock_irqrestore(&port->lock, flags);
701 uart_port_deref(port);
704 static void uart_throttle(struct tty_struct *tty)
706 struct uart_state *state = tty->driver_data;
707 upstat_t mask = UPSTAT_SYNC_FIFO;
708 struct uart_port *port;
710 port = uart_port_ref(state);
715 mask |= UPSTAT_AUTOXOFF;
717 mask |= UPSTAT_AUTORTS;
719 if (port->status & mask) {
720 port->ops->throttle(port);
721 mask &= ~port->status;
724 if (mask & UPSTAT_AUTORTS)
725 uart_clear_mctrl(port, TIOCM_RTS);
727 if (mask & UPSTAT_AUTOXOFF)
728 uart_send_xchar(tty, STOP_CHAR(tty));
730 uart_port_deref(port);
733 static void uart_unthrottle(struct tty_struct *tty)
735 struct uart_state *state = tty->driver_data;
736 upstat_t mask = UPSTAT_SYNC_FIFO;
737 struct uart_port *port;
739 port = uart_port_ref(state);
744 mask |= UPSTAT_AUTOXOFF;
746 mask |= UPSTAT_AUTORTS;
748 if (port->status & mask) {
749 port->ops->unthrottle(port);
750 mask &= ~port->status;
753 if (mask & UPSTAT_AUTORTS)
754 uart_set_mctrl(port, TIOCM_RTS);
756 if (mask & UPSTAT_AUTOXOFF)
757 uart_send_xchar(tty, START_CHAR(tty));
759 uart_port_deref(port);
762 static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
764 struct uart_state *state = container_of(port, struct uart_state, port);
765 struct uart_port *uport;
769 * Ensure the state we copy is consistent and no hardware changes
772 mutex_lock(&port->mutex);
773 uport = uart_port_check(state);
777 retinfo->type = uport->type;
778 retinfo->line = uport->line;
779 retinfo->port = uport->iobase;
780 if (HIGH_BITS_OFFSET)
781 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
782 retinfo->irq = uport->irq;
783 retinfo->flags = (__force int)uport->flags;
784 retinfo->xmit_fifo_size = uport->fifosize;
785 retinfo->baud_base = uport->uartclk / 16;
786 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
787 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
788 ASYNC_CLOSING_WAIT_NONE :
789 jiffies_to_msecs(port->closing_wait) / 10;
790 retinfo->custom_divisor = uport->custom_divisor;
791 retinfo->hub6 = uport->hub6;
792 retinfo->io_type = uport->iotype;
793 retinfo->iomem_reg_shift = uport->regshift;
794 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
798 mutex_unlock(&port->mutex);
802 static int uart_get_info_user(struct tty_struct *tty,
803 struct serial_struct *ss)
805 struct uart_state *state = tty->driver_data;
806 struct tty_port *port = &state->port;
808 return uart_get_info(port, ss) < 0 ? -EIO : 0;
811 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
812 struct uart_state *state,
813 struct serial_struct *new_info)
815 struct uart_port *uport = uart_port_check(state);
816 unsigned long new_port;
817 unsigned int change_irq, change_port, closing_wait;
818 unsigned int old_custom_divisor, close_delay;
819 upf_t old_flags, new_flags;
825 new_port = new_info->port;
826 if (HIGH_BITS_OFFSET)
827 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
829 new_info->irq = irq_canonicalize(new_info->irq);
830 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
831 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
832 ASYNC_CLOSING_WAIT_NONE :
833 msecs_to_jiffies(new_info->closing_wait * 10);
836 change_irq = !(uport->flags & UPF_FIXED_PORT)
837 && new_info->irq != uport->irq;
840 * Since changing the 'type' of the port changes its resource
841 * allocations, we should treat type changes the same as
844 change_port = !(uport->flags & UPF_FIXED_PORT)
845 && (new_port != uport->iobase ||
846 (unsigned long)new_info->iomem_base != uport->mapbase ||
847 new_info->hub6 != uport->hub6 ||
848 new_info->io_type != uport->iotype ||
849 new_info->iomem_reg_shift != uport->regshift ||
850 new_info->type != uport->type);
852 old_flags = uport->flags;
853 new_flags = (__force upf_t)new_info->flags;
854 old_custom_divisor = uport->custom_divisor;
856 if (!capable(CAP_SYS_ADMIN)) {
858 if (change_irq || change_port ||
859 (new_info->baud_base != uport->uartclk / 16) ||
860 (close_delay != port->close_delay) ||
861 (closing_wait != port->closing_wait) ||
862 (new_info->xmit_fifo_size &&
863 new_info->xmit_fifo_size != uport->fifosize) ||
864 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
866 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
867 (new_flags & UPF_USR_MASK));
868 uport->custom_divisor = new_info->custom_divisor;
872 if (change_irq || change_port) {
873 retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
879 * Ask the low level driver to verify the settings.
881 if (uport->ops->verify_port)
882 retval = uport->ops->verify_port(uport, new_info);
884 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
885 (new_info->baud_base < 9600))
891 if (change_port || change_irq) {
895 * Make sure that we are the sole user of this port.
897 if (tty_port_users(port) > 1)
901 * We need to shutdown the serial port at the old
902 * port/type/irq combination.
904 uart_shutdown(tty, state);
908 unsigned long old_iobase, old_mapbase;
909 unsigned int old_type, old_iotype, old_hub6, old_shift;
911 old_iobase = uport->iobase;
912 old_mapbase = uport->mapbase;
913 old_type = uport->type;
914 old_hub6 = uport->hub6;
915 old_iotype = uport->iotype;
916 old_shift = uport->regshift;
919 * Free and release old regions
921 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
922 uport->ops->release_port(uport);
924 uport->iobase = new_port;
925 uport->type = new_info->type;
926 uport->hub6 = new_info->hub6;
927 uport->iotype = new_info->io_type;
928 uport->regshift = new_info->iomem_reg_shift;
929 uport->mapbase = (unsigned long)new_info->iomem_base;
932 * Claim and map the new regions
934 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
935 retval = uport->ops->request_port(uport);
937 /* Always success - Jean II */
942 * If we fail to request resources for the
943 * new port, try to restore the old settings.
946 uport->iobase = old_iobase;
947 uport->type = old_type;
948 uport->hub6 = old_hub6;
949 uport->iotype = old_iotype;
950 uport->regshift = old_shift;
951 uport->mapbase = old_mapbase;
953 if (old_type != PORT_UNKNOWN) {
954 retval = uport->ops->request_port(uport);
956 * If we failed to restore the old settings,
960 uport->type = PORT_UNKNOWN;
968 /* Added to return the correct error -Ram Gupta */
974 uport->irq = new_info->irq;
975 if (!(uport->flags & UPF_FIXED_PORT))
976 uport->uartclk = new_info->baud_base * 16;
977 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
978 (new_flags & UPF_CHANGE_MASK);
979 uport->custom_divisor = new_info->custom_divisor;
980 port->close_delay = close_delay;
981 port->closing_wait = closing_wait;
982 if (new_info->xmit_fifo_size)
983 uport->fifosize = new_info->xmit_fifo_size;
987 if (uport->type == PORT_UNKNOWN)
989 if (tty_port_initialized(port)) {
990 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
991 old_custom_divisor != uport->custom_divisor) {
993 * If they're setting up a custom divisor or speed,
994 * instead of clearing it, then bitch about it.
996 if (uport->flags & UPF_SPD_MASK) {
997 dev_notice_ratelimited(uport->dev,
998 "%s sets custom speed on %s. This is deprecated.\n",
1000 tty_name(port->tty));
1002 uart_change_speed(tty, state, NULL);
1005 retval = uart_startup(tty, state, 1);
1007 tty_port_set_initialized(port, true);
1015 static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
1017 struct uart_state *state = tty->driver_data;
1018 struct tty_port *port = &state->port;
1021 down_write(&tty->termios_rwsem);
1023 * This semaphore protects port->count. It is also
1024 * very useful to prevent opens. Also, take the
1025 * port configuration semaphore to make sure that a
1026 * module insertion/removal doesn't change anything
1029 mutex_lock(&port->mutex);
1030 retval = uart_set_info(tty, port, state, ss);
1031 mutex_unlock(&port->mutex);
1032 up_write(&tty->termios_rwsem);
1037 * uart_get_lsr_info - get line status register info
1038 * @tty: tty associated with the UART
1039 * @state: UART being queried
1040 * @value: returned modem value
1042 static int uart_get_lsr_info(struct tty_struct *tty,
1043 struct uart_state *state, unsigned int __user *value)
1045 struct uart_port *uport = uart_port_check(state);
1046 unsigned int result;
1048 result = uport->ops->tx_empty(uport);
1051 * If we're about to load something into the transmit
1052 * register, we'll pretend the transmitter isn't empty to
1053 * avoid a race condition (depending on when the transmit
1054 * interrupt happens).
1056 if (uport->x_char ||
1057 ((uart_circ_chars_pending(&state->xmit) > 0) &&
1058 !uart_tx_stopped(uport)))
1059 result &= ~TIOCSER_TEMT;
1061 return put_user(result, value);
1064 static int uart_tiocmget(struct tty_struct *tty)
1066 struct uart_state *state = tty->driver_data;
1067 struct tty_port *port = &state->port;
1068 struct uart_port *uport;
1071 mutex_lock(&port->mutex);
1072 uport = uart_port_check(state);
1076 if (!tty_io_error(tty)) {
1077 result = uport->mctrl;
1078 spin_lock_irq(&uport->lock);
1079 result |= uport->ops->get_mctrl(uport);
1080 spin_unlock_irq(&uport->lock);
1083 mutex_unlock(&port->mutex);
1088 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1090 struct uart_state *state = tty->driver_data;
1091 struct tty_port *port = &state->port;
1092 struct uart_port *uport;
1095 mutex_lock(&port->mutex);
1096 uport = uart_port_check(state);
1100 if (!tty_io_error(tty)) {
1101 uart_update_mctrl(uport, set, clear);
1105 mutex_unlock(&port->mutex);
1109 static int uart_break_ctl(struct tty_struct *tty, int break_state)
1111 struct uart_state *state = tty->driver_data;
1112 struct tty_port *port = &state->port;
1113 struct uart_port *uport;
1116 mutex_lock(&port->mutex);
1117 uport = uart_port_check(state);
1121 if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1122 uport->ops->break_ctl(uport, break_state);
1125 mutex_unlock(&port->mutex);
1129 static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
1131 struct tty_port *port = &state->port;
1132 struct uart_port *uport;
1135 if (!capable(CAP_SYS_ADMIN))
1139 * Take the per-port semaphore. This prevents count from
1140 * changing, and hence any extra opens of the port while
1141 * we're auto-configuring.
1143 if (mutex_lock_interruptible(&port->mutex))
1144 return -ERESTARTSYS;
1146 uport = uart_port_check(state);
1153 if (tty_port_users(port) == 1) {
1154 uart_shutdown(tty, state);
1157 * If we already have a port type configured,
1158 * we must release its resources.
1160 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1161 uport->ops->release_port(uport);
1163 flags = UART_CONFIG_TYPE;
1164 if (uport->flags & UPF_AUTO_IRQ)
1165 flags |= UART_CONFIG_IRQ;
1168 * This will claim the ports resources if
1171 uport->ops->config_port(uport, flags);
1173 ret = uart_startup(tty, state, 1);
1175 tty_port_set_initialized(port, true);
1180 mutex_unlock(&port->mutex);
1184 static void uart_enable_ms(struct uart_port *uport)
1187 * Force modem status interrupts on
1189 if (uport->ops->enable_ms)
1190 uport->ops->enable_ms(uport);
1194 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1195 * - mask passed in arg for lines of interest
1196 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1197 * Caller should use TIOCGICOUNT to see which one it was
1199 * FIXME: This wants extracting into a common all driver implementation
1200 * of TIOCMWAIT using tty_port.
1202 static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1204 struct uart_port *uport;
1205 struct tty_port *port = &state->port;
1206 DECLARE_WAITQUEUE(wait, current);
1207 struct uart_icount cprev, cnow;
1211 * note the counters on entry
1213 uport = uart_port_ref(state);
1216 spin_lock_irq(&uport->lock);
1217 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1218 uart_enable_ms(uport);
1219 spin_unlock_irq(&uport->lock);
1221 add_wait_queue(&port->delta_msr_wait, &wait);
1223 spin_lock_irq(&uport->lock);
1224 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1225 spin_unlock_irq(&uport->lock);
1227 set_current_state(TASK_INTERRUPTIBLE);
1229 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1230 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1231 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1232 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1239 /* see if a signal did it */
1240 if (signal_pending(current)) {
1247 __set_current_state(TASK_RUNNING);
1248 remove_wait_queue(&port->delta_msr_wait, &wait);
1249 uart_port_deref(uport);
1255 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1256 * Return: write counters to the user passed counter struct
1257 * NB: both 1->0 and 0->1 transitions are counted except for
1258 * RI where only 0->1 is counted.
1260 static int uart_get_icount(struct tty_struct *tty,
1261 struct serial_icounter_struct *icount)
1263 struct uart_state *state = tty->driver_data;
1264 struct uart_icount cnow;
1265 struct uart_port *uport;
1267 uport = uart_port_ref(state);
1270 spin_lock_irq(&uport->lock);
1271 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1272 spin_unlock_irq(&uport->lock);
1273 uart_port_deref(uport);
1275 icount->cts = cnow.cts;
1276 icount->dsr = cnow.dsr;
1277 icount->rng = cnow.rng;
1278 icount->dcd = cnow.dcd;
1279 icount->rx = cnow.rx;
1280 icount->tx = cnow.tx;
1281 icount->frame = cnow.frame;
1282 icount->overrun = cnow.overrun;
1283 icount->parity = cnow.parity;
1284 icount->brk = cnow.brk;
1285 icount->buf_overrun = cnow.buf_overrun;
1290 #define SER_RS485_LEGACY_FLAGS (SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
1291 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
1292 SER_RS485_TERMINATE_BUS)
1294 static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
1296 u32 flags = rs485->flags;
1298 /* Don't return -EINVAL for unsupported legacy flags */
1299 flags &= ~SER_RS485_LEGACY_FLAGS;
1302 * For any bit outside of the legacy ones that is not supported by
1303 * the driver, return -EINVAL.
1305 if (flags & ~port->rs485_supported.flags)
1308 /* Asking for address w/o addressing mode? */
1309 if (!(rs485->flags & SER_RS485_ADDRB) &&
1310 (rs485->flags & (SER_RS485_ADDR_RECV|SER_RS485_ADDR_DEST)))
1313 /* Address given but not enabled? */
1314 if (!(rs485->flags & SER_RS485_ADDR_RECV) && rs485->addr_recv)
1316 if (!(rs485->flags & SER_RS485_ADDR_DEST) && rs485->addr_dest)
1322 static void uart_sanitize_serial_rs485_delays(struct uart_port *port,
1323 struct serial_rs485 *rs485)
1325 if (!port->rs485_supported.delay_rts_before_send) {
1326 if (rs485->delay_rts_before_send) {
1327 dev_warn_ratelimited(port->dev,
1328 "%s (%d): RTS delay before sending not supported\n",
1329 port->name, port->line);
1331 rs485->delay_rts_before_send = 0;
1332 } else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) {
1333 rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY;
1334 dev_warn_ratelimited(port->dev,
1335 "%s (%d): RTS delay before sending clamped to %u ms\n",
1336 port->name, port->line, rs485->delay_rts_before_send);
1339 if (!port->rs485_supported.delay_rts_after_send) {
1340 if (rs485->delay_rts_after_send) {
1341 dev_warn_ratelimited(port->dev,
1342 "%s (%d): RTS delay after sending not supported\n",
1343 port->name, port->line);
1345 rs485->delay_rts_after_send = 0;
1346 } else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
1347 rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;
1348 dev_warn_ratelimited(port->dev,
1349 "%s (%d): RTS delay after sending clamped to %u ms\n",
1350 port->name, port->line, rs485->delay_rts_after_send);
1354 static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485)
1356 u32 supported_flags = port->rs485_supported.flags;
1358 if (!(rs485->flags & SER_RS485_ENABLED)) {
1359 memset(rs485, 0, sizeof(*rs485));
1363 /* Pick sane settings if the user hasn't */
1364 if ((supported_flags & (SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND)) &&
1365 !(rs485->flags & SER_RS485_RTS_ON_SEND) ==
1366 !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
1367 dev_warn_ratelimited(port->dev,
1368 "%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
1369 port->name, port->line);
1370 rs485->flags |= SER_RS485_RTS_ON_SEND;
1371 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
1372 supported_flags |= SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND;
1375 rs485->flags &= supported_flags;
1377 uart_sanitize_serial_rs485_delays(port, rs485);
1379 /* Return clean padding area to userspace */
1380 memset(rs485->padding0, 0, sizeof(rs485->padding0));
1381 memset(rs485->padding1, 0, sizeof(rs485->padding1));
1384 static void uart_set_rs485_termination(struct uart_port *port,
1385 const struct serial_rs485 *rs485)
1387 if (!(rs485->flags & SER_RS485_ENABLED))
1390 gpiod_set_value_cansleep(port->rs485_term_gpio,
1391 !!(rs485->flags & SER_RS485_TERMINATE_BUS));
1394 int uart_rs485_config(struct uart_port *port)
1396 struct serial_rs485 *rs485 = &port->rs485;
1399 uart_sanitize_serial_rs485(port, rs485);
1400 uart_set_rs485_termination(port, rs485);
1402 ret = port->rs485_config(port, NULL, rs485);
1404 memset(rs485, 0, sizeof(*rs485));
1408 EXPORT_SYMBOL_GPL(uart_rs485_config);
1410 static int uart_get_rs485_config(struct uart_port *port,
1411 struct serial_rs485 __user *rs485)
1413 unsigned long flags;
1414 struct serial_rs485 aux;
1416 spin_lock_irqsave(&port->lock, flags);
1418 spin_unlock_irqrestore(&port->lock, flags);
1420 if (copy_to_user(rs485, &aux, sizeof(aux)))
1426 static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
1427 struct serial_rs485 __user *rs485_user)
1429 struct serial_rs485 rs485;
1431 unsigned long flags;
1433 if (!port->rs485_config)
1436 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1439 ret = uart_check_rs485_flags(port, &rs485);
1442 uart_sanitize_serial_rs485(port, &rs485);
1443 uart_set_rs485_termination(port, &rs485);
1445 spin_lock_irqsave(&port->lock, flags);
1446 ret = port->rs485_config(port, &tty->termios, &rs485);
1448 port->rs485 = rs485;
1449 spin_unlock_irqrestore(&port->lock, flags);
1453 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1459 static int uart_get_iso7816_config(struct uart_port *port,
1460 struct serial_iso7816 __user *iso7816)
1462 unsigned long flags;
1463 struct serial_iso7816 aux;
1465 if (!port->iso7816_config)
1468 spin_lock_irqsave(&port->lock, flags);
1469 aux = port->iso7816;
1470 spin_unlock_irqrestore(&port->lock, flags);
1472 if (copy_to_user(iso7816, &aux, sizeof(aux)))
1478 static int uart_set_iso7816_config(struct uart_port *port,
1479 struct serial_iso7816 __user *iso7816_user)
1481 struct serial_iso7816 iso7816;
1483 unsigned long flags;
1485 if (!port->iso7816_config)
1488 if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1492 * There are 5 words reserved for future use. Check that userspace
1493 * doesn't put stuff in there to prevent breakages in the future.
1495 for (i = 0; i < 5; i++)
1496 if (iso7816.reserved[i])
1499 spin_lock_irqsave(&port->lock, flags);
1500 ret = port->iso7816_config(port, &iso7816);
1501 spin_unlock_irqrestore(&port->lock, flags);
1505 if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1512 * Called via sys_ioctl. We can use spin_lock_irq() here.
1515 uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1517 struct uart_state *state = tty->driver_data;
1518 struct tty_port *port = &state->port;
1519 struct uart_port *uport;
1520 void __user *uarg = (void __user *)arg;
1521 int ret = -ENOIOCTLCMD;
1525 * These ioctls don't rely on the hardware to be present.
1529 down_write(&tty->termios_rwsem);
1530 ret = uart_do_autoconfig(tty, state);
1531 up_write(&tty->termios_rwsem);
1535 if (ret != -ENOIOCTLCMD)
1538 if (tty_io_error(tty)) {
1544 * The following should only be used when hardware is present.
1548 ret = uart_wait_modem_status(state, arg);
1552 if (ret != -ENOIOCTLCMD)
1555 /* rs485_config requires more locking than others */
1556 if (cmd == TIOCGRS485)
1557 down_write(&tty->termios_rwsem);
1559 mutex_lock(&port->mutex);
1560 uport = uart_port_check(state);
1562 if (!uport || tty_io_error(tty)) {
1568 * All these rely on hardware being present and need to be
1569 * protected against the tty being hung up.
1573 case TIOCSERGETLSR: /* Get line status register */
1574 ret = uart_get_lsr_info(tty, state, uarg);
1578 ret = uart_get_rs485_config(uport, uarg);
1582 ret = uart_set_rs485_config(tty, uport, uarg);
1586 ret = uart_set_iso7816_config(state->uart_port, uarg);
1590 ret = uart_get_iso7816_config(state->uart_port, uarg);
1593 if (uport->ops->ioctl)
1594 ret = uport->ops->ioctl(uport, cmd, arg);
1598 mutex_unlock(&port->mutex);
1599 if (cmd == TIOCGRS485)
1600 up_write(&tty->termios_rwsem);
1605 static void uart_set_ldisc(struct tty_struct *tty)
1607 struct uart_state *state = tty->driver_data;
1608 struct uart_port *uport;
1609 struct tty_port *port = &state->port;
1611 if (!tty_port_initialized(port))
1614 mutex_lock(&state->port.mutex);
1615 uport = uart_port_check(state);
1616 if (uport && uport->ops->set_ldisc)
1617 uport->ops->set_ldisc(uport, &tty->termios);
1618 mutex_unlock(&state->port.mutex);
1621 static void uart_set_termios(struct tty_struct *tty,
1622 struct ktermios *old_termios)
1624 struct uart_state *state = tty->driver_data;
1625 struct uart_port *uport;
1626 unsigned int cflag = tty->termios.c_cflag;
1627 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1628 bool sw_changed = false;
1630 mutex_lock(&state->port.mutex);
1631 uport = uart_port_check(state);
1636 * Drivers doing software flow control also need to know
1637 * about changes to these input settings.
1639 if (uport->flags & UPF_SOFT_FLOW) {
1640 iflag_mask |= IXANY|IXON|IXOFF;
1642 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1643 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1647 * These are the bits that are used to setup various
1648 * flags in the low level driver. We can ignore the Bfoo
1649 * bits in c_cflag; c_[io]speed will always be set
1650 * appropriately by set_termios() in tty_ioctl.c
1652 if ((cflag ^ old_termios->c_cflag) == 0 &&
1653 tty->termios.c_ospeed == old_termios->c_ospeed &&
1654 tty->termios.c_ispeed == old_termios->c_ispeed &&
1655 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1660 uart_change_speed(tty, state, old_termios);
1661 /* reload cflag from termios; port driver may have overridden flags */
1662 cflag = tty->termios.c_cflag;
1664 /* Handle transition to B0 status */
1665 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1666 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1667 /* Handle transition away from B0 status */
1668 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1669 unsigned int mask = TIOCM_DTR;
1671 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1673 uart_set_mctrl(uport, mask);
1676 mutex_unlock(&state->port.mutex);
1680 * Calls to uart_close() are serialised via the tty_lock in
1681 * drivers/tty/tty_io.c:tty_release()
1682 * drivers/tty/tty_io.c:do_tty_hangup()
1684 static void uart_close(struct tty_struct *tty, struct file *filp)
1686 struct uart_state *state = tty->driver_data;
1689 struct uart_driver *drv = tty->driver->driver_state;
1690 struct tty_port *port;
1692 state = drv->state + tty->index;
1693 port = &state->port;
1694 spin_lock_irq(&port->lock);
1696 spin_unlock_irq(&port->lock);
1700 pr_debug("uart_close(%d) called\n", tty->index);
1702 tty_port_close(tty->port, tty, filp);
1705 static void uart_tty_port_shutdown(struct tty_port *port)
1707 struct uart_state *state = container_of(port, struct uart_state, port);
1708 struct uart_port *uport = uart_port_check(state);
1712 * At this point, we stop accepting input. To do this, we
1713 * disable the receive line status interrupts.
1715 if (WARN(!uport, "detached port still initialized!\n"))
1718 spin_lock_irq(&uport->lock);
1719 uport->ops->stop_rx(uport);
1720 spin_unlock_irq(&uport->lock);
1722 uart_port_shutdown(port);
1725 * It's possible for shutdown to be called after suspend if we get
1726 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1727 * we don't try to resume a port that has been shutdown.
1729 tty_port_set_suspended(port, 0);
1732 * Free the transmit buffer.
1734 spin_lock_irq(&uport->lock);
1735 buf = state->xmit.buf;
1736 state->xmit.buf = NULL;
1737 spin_unlock_irq(&uport->lock);
1739 free_page((unsigned long)buf);
1741 uart_change_pm(state, UART_PM_STATE_OFF);
1744 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1746 struct uart_state *state = tty->driver_data;
1747 struct uart_port *port;
1748 unsigned long char_time, expire, fifo_timeout;
1750 port = uart_port_ref(state);
1754 if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
1755 uart_port_deref(port);
1760 * Set the check interval to be 1/5 of the estimated time to
1761 * send a single character, and make it at least 1. The check
1762 * interval should also be less than the timeout.
1764 * Note: we have to use pretty tight timings here to satisfy
1767 char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL);
1769 if (timeout && timeout < char_time)
1770 char_time = timeout;
1772 if (!uart_cts_enabled(port)) {
1774 * If the transmitter hasn't cleared in twice the approximate
1775 * amount of time to send the entire FIFO, it probably won't
1776 * ever clear. This assumes the UART isn't doing flow
1777 * control, which is currently the case. Hence, if it ever
1778 * takes longer than FIFO timeout, this is probably due to a
1779 * UART bug of some kind. So, we clamp the timeout parameter at
1782 fifo_timeout = uart_fifo_timeout(port);
1783 if (timeout == 0 || timeout > 2 * fifo_timeout)
1784 timeout = 2 * fifo_timeout;
1787 expire = jiffies + timeout;
1789 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1790 port->line, jiffies, expire);
1793 * Check whether the transmitter is empty every 'char_time'.
1794 * 'timeout' / 'expire' give us the maximum amount of time
1797 while (!port->ops->tx_empty(port)) {
1798 msleep_interruptible(jiffies_to_msecs(char_time));
1799 if (signal_pending(current))
1801 if (timeout && time_after(jiffies, expire))
1804 uart_port_deref(port);
1808 * Calls to uart_hangup() are serialised by the tty_lock in
1809 * drivers/tty/tty_io.c:do_tty_hangup()
1810 * This runs from a workqueue and can sleep for a _short_ time only.
1812 static void uart_hangup(struct tty_struct *tty)
1814 struct uart_state *state = tty->driver_data;
1815 struct tty_port *port = &state->port;
1816 struct uart_port *uport;
1817 unsigned long flags;
1819 pr_debug("uart_hangup(%d)\n", tty->index);
1821 mutex_lock(&port->mutex);
1822 uport = uart_port_check(state);
1823 WARN(!uport, "hangup of detached port!\n");
1825 if (tty_port_active(port)) {
1826 uart_flush_buffer(tty);
1827 uart_shutdown(tty, state);
1828 spin_lock_irqsave(&port->lock, flags);
1830 spin_unlock_irqrestore(&port->lock, flags);
1831 tty_port_set_active(port, 0);
1832 tty_port_tty_set(port, NULL);
1833 if (uport && !uart_console(uport))
1834 uart_change_pm(state, UART_PM_STATE_OFF);
1835 wake_up_interruptible(&port->open_wait);
1836 wake_up_interruptible(&port->delta_msr_wait);
1838 mutex_unlock(&port->mutex);
1841 /* uport == NULL if uart_port has already been removed */
1842 static void uart_port_shutdown(struct tty_port *port)
1844 struct uart_state *state = container_of(port, struct uart_state, port);
1845 struct uart_port *uport = uart_port_check(state);
1848 * clear delta_msr_wait queue to avoid mem leaks: we may free
1849 * the irq here so the queue might never be woken up. Note
1850 * that we won't end up waiting on delta_msr_wait again since
1851 * any outstanding file descriptors should be pointing at
1852 * hung_up_tty_fops now.
1854 wake_up_interruptible(&port->delta_msr_wait);
1857 /* Free the IRQ and disable the port. */
1858 uport->ops->shutdown(uport);
1860 /* Ensure that the IRQ handler isn't running on another CPU. */
1861 synchronize_irq(uport->irq);
1865 static int uart_carrier_raised(struct tty_port *port)
1867 struct uart_state *state = container_of(port, struct uart_state, port);
1868 struct uart_port *uport;
1871 uport = uart_port_ref(state);
1873 * Should never observe uport == NULL since checks for hangup should
1874 * abort the tty_port_block_til_ready() loop before checking for carrier
1875 * raised -- but report carrier raised if it does anyway so open will
1876 * continue and not sleep
1878 if (WARN_ON(!uport))
1880 spin_lock_irq(&uport->lock);
1881 uart_enable_ms(uport);
1882 mctrl = uport->ops->get_mctrl(uport);
1883 spin_unlock_irq(&uport->lock);
1884 uart_port_deref(uport);
1885 if (mctrl & TIOCM_CAR)
1890 static void uart_dtr_rts(struct tty_port *port, int raise)
1892 struct uart_state *state = container_of(port, struct uart_state, port);
1893 struct uart_port *uport;
1895 uport = uart_port_ref(state);
1898 uart_port_dtr_rts(uport, raise);
1899 uart_port_deref(uport);
1902 static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
1904 struct uart_driver *drv = driver->driver_state;
1905 struct uart_state *state = drv->state + tty->index;
1907 tty->driver_data = state;
1909 return tty_standard_install(driver, tty);
1913 * Calls to uart_open are serialised by the tty_lock in
1914 * drivers/tty/tty_io.c:tty_open()
1915 * Note that if this fails, then uart_close() _will_ be called.
1917 * In time, we want to scrap the "opening nonpresent ports"
1918 * behaviour and implement an alternative way for setserial
1919 * to set base addresses/ports/types. This will allow us to
1920 * get rid of a certain amount of extra tests.
1922 static int uart_open(struct tty_struct *tty, struct file *filp)
1924 struct uart_state *state = tty->driver_data;
1927 retval = tty_port_open(&state->port, tty, filp);
1934 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1936 struct uart_state *state = container_of(port, struct uart_state, port);
1937 struct uart_port *uport;
1940 uport = uart_port_check(state);
1941 if (!uport || uport->flags & UPF_DEAD)
1945 * Start up the serial port.
1947 ret = uart_startup(tty, state, 0);
1949 tty_port_set_active(port, 1);
1954 static const char *uart_type(struct uart_port *port)
1956 const char *str = NULL;
1958 if (port->ops->type)
1959 str = port->ops->type(port);
1967 #ifdef CONFIG_PROC_FS
1969 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1971 struct uart_state *state = drv->state + i;
1972 struct tty_port *port = &state->port;
1973 enum uart_pm_state pm_state;
1974 struct uart_port *uport;
1976 unsigned int status;
1979 mutex_lock(&port->mutex);
1980 uport = uart_port_check(state);
1984 mmio = uport->iotype >= UPIO_MEM;
1985 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1986 uport->line, uart_type(uport),
1987 mmio ? "mmio:0x" : "port:",
1988 mmio ? (unsigned long long)uport->mapbase
1989 : (unsigned long long)uport->iobase,
1992 if (uport->type == PORT_UNKNOWN) {
1997 if (capable(CAP_SYS_ADMIN)) {
1998 pm_state = state->pm_state;
1999 if (pm_state != UART_PM_STATE_ON)
2000 uart_change_pm(state, UART_PM_STATE_ON);
2001 spin_lock_irq(&uport->lock);
2002 status = uport->ops->get_mctrl(uport);
2003 spin_unlock_irq(&uport->lock);
2004 if (pm_state != UART_PM_STATE_ON)
2005 uart_change_pm(state, pm_state);
2007 seq_printf(m, " tx:%d rx:%d",
2008 uport->icount.tx, uport->icount.rx);
2009 if (uport->icount.frame)
2010 seq_printf(m, " fe:%d", uport->icount.frame);
2011 if (uport->icount.parity)
2012 seq_printf(m, " pe:%d", uport->icount.parity);
2013 if (uport->icount.brk)
2014 seq_printf(m, " brk:%d", uport->icount.brk);
2015 if (uport->icount.overrun)
2016 seq_printf(m, " oe:%d", uport->icount.overrun);
2017 if (uport->icount.buf_overrun)
2018 seq_printf(m, " bo:%d", uport->icount.buf_overrun);
2020 #define INFOBIT(bit, str) \
2021 if (uport->mctrl & (bit)) \
2022 strncat(stat_buf, (str), sizeof(stat_buf) - \
2023 strlen(stat_buf) - 2)
2024 #define STATBIT(bit, str) \
2025 if (status & (bit)) \
2026 strncat(stat_buf, (str), sizeof(stat_buf) - \
2027 strlen(stat_buf) - 2)
2031 INFOBIT(TIOCM_RTS, "|RTS");
2032 STATBIT(TIOCM_CTS, "|CTS");
2033 INFOBIT(TIOCM_DTR, "|DTR");
2034 STATBIT(TIOCM_DSR, "|DSR");
2035 STATBIT(TIOCM_CAR, "|CD");
2036 STATBIT(TIOCM_RNG, "|RI");
2040 seq_puts(m, stat_buf);
2046 mutex_unlock(&port->mutex);
2049 static int uart_proc_show(struct seq_file *m, void *v)
2051 struct tty_driver *ttydrv = m->private;
2052 struct uart_driver *drv = ttydrv->driver_state;
2055 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
2056 for (i = 0; i < drv->nr; i++)
2057 uart_line_info(m, drv, i);
2062 static inline bool uart_console_enabled(struct uart_port *port)
2064 return uart_console(port) && (port->cons->flags & CON_ENABLED);
2067 static void uart_port_spin_lock_init(struct uart_port *port)
2069 spin_lock_init(&port->lock);
2070 lockdep_set_class(&port->lock, &port_lock_key);
2073 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
2075 * uart_console_write - write a console message to a serial port
2076 * @port: the port to write the message
2077 * @s: array of characters
2078 * @count: number of characters in string to write
2079 * @putchar: function to write character to port
2081 void uart_console_write(struct uart_port *port, const char *s,
2083 void (*putchar)(struct uart_port *, unsigned char))
2087 for (i = 0; i < count; i++, s++) {
2089 putchar(port, '\r');
2093 EXPORT_SYMBOL_GPL(uart_console_write);
2096 * uart_get_console - get uart port for console
2097 * @ports: ports to search in
2098 * @nr: number of @ports
2099 * @co: console to search for
2100 * Returns: uart_port for the console @co
2102 * Check whether an invalid uart number has been specified (as @co->index), and
2103 * if so, search for the first available port that does have console support.
2105 struct uart_port * __init
2106 uart_get_console(struct uart_port *ports, int nr, struct console *co)
2108 int idx = co->index;
2110 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
2111 ports[idx].membase == NULL))
2112 for (idx = 0; idx < nr; idx++)
2113 if (ports[idx].iobase != 0 ||
2114 ports[idx].membase != NULL)
2123 * uart_parse_earlycon - Parse earlycon options
2124 * @p: ptr to 2nd field (ie., just beyond '<name>,')
2125 * @iotype: ptr for decoded iotype (out)
2126 * @addr: ptr for decoded mapbase/iobase (out)
2127 * @options: ptr for <options> field; %NULL if not present (out)
2129 * Decodes earlycon kernel command line parameters of the form:
2130 * * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2131 * * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2133 * The optional form:
2134 * * earlycon=<name>,0x<addr>,<options>
2135 * * console=<name>,0x<addr>,<options>
2137 * is also accepted; the returned @iotype will be %UPIO_MEM.
2139 * Returns: 0 on success or -%EINVAL on failure
2141 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
2144 if (strncmp(p, "mmio,", 5) == 0) {
2147 } else if (strncmp(p, "mmio16,", 7) == 0) {
2148 *iotype = UPIO_MEM16;
2150 } else if (strncmp(p, "mmio32,", 7) == 0) {
2151 *iotype = UPIO_MEM32;
2153 } else if (strncmp(p, "mmio32be,", 9) == 0) {
2154 *iotype = UPIO_MEM32BE;
2156 } else if (strncmp(p, "mmio32native,", 13) == 0) {
2157 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
2158 UPIO_MEM32BE : UPIO_MEM32;
2160 } else if (strncmp(p, "io,", 3) == 0) {
2161 *iotype = UPIO_PORT;
2163 } else if (strncmp(p, "0x", 2) == 0) {
2170 * Before you replace it with kstrtoull(), think about options separator
2171 * (',') it will not tolerate
2173 *addr = simple_strtoull(p, NULL, 0);
2181 EXPORT_SYMBOL_GPL(uart_parse_earlycon);
2184 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
2185 * @options: pointer to option string
2186 * @baud: pointer to an 'int' variable for the baud rate.
2187 * @parity: pointer to an 'int' variable for the parity.
2188 * @bits: pointer to an 'int' variable for the number of data bits.
2189 * @flow: pointer to an 'int' variable for the flow control character.
2191 * uart_parse_options() decodes a string containing the serial console
2192 * options. The format of the string is <baud><parity><bits><flow>,
2196 uart_parse_options(const char *options, int *baud, int *parity,
2197 int *bits, int *flow)
2199 const char *s = options;
2201 *baud = simple_strtoul(s, NULL, 10);
2202 while (*s >= '0' && *s <= '9')
2211 EXPORT_SYMBOL_GPL(uart_parse_options);
2214 * uart_set_options - setup the serial console parameters
2215 * @port: pointer to the serial ports uart_port structure
2216 * @co: console pointer
2218 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2219 * @bits: number of data bits
2220 * @flow: flow control character - 'r' (rts)
2223 uart_set_options(struct uart_port *port, struct console *co,
2224 int baud, int parity, int bits, int flow)
2226 struct ktermios termios;
2227 static struct ktermios dummy;
2230 * Ensure that the serial-console lock is initialised early.
2232 * Note that the console-enabled check is needed because of kgdboc,
2233 * which can end up calling uart_set_options() for an already enabled
2234 * console via tty_find_polling_driver() and uart_poll_init().
2236 if (!uart_console_enabled(port) && !port->console_reinit)
2237 uart_port_spin_lock_init(port);
2239 memset(&termios, 0, sizeof(struct ktermios));
2241 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2242 tty_termios_encode_baud_rate(&termios, baud, baud);
2245 termios.c_cflag |= CS7;
2247 termios.c_cflag |= CS8;
2251 termios.c_cflag |= PARODD;
2254 termios.c_cflag |= PARENB;
2259 termios.c_cflag |= CRTSCTS;
2262 * some uarts on other side don't support no flow control.
2263 * So we set * DTR in host uart to make them happy
2265 port->mctrl |= TIOCM_DTR;
2267 port->ops->set_termios(port, &termios, &dummy);
2269 * Allow the setting of the UART parameters with a NULL console
2273 co->cflag = termios.c_cflag;
2274 co->ispeed = termios.c_ispeed;
2275 co->ospeed = termios.c_ospeed;
2280 EXPORT_SYMBOL_GPL(uart_set_options);
2281 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2284 * uart_change_pm - set power state of the port
2286 * @state: port descriptor
2287 * @pm_state: new state
2289 * Locking: port->mutex has to be held
2291 static void uart_change_pm(struct uart_state *state,
2292 enum uart_pm_state pm_state)
2294 struct uart_port *port = uart_port_check(state);
2296 if (state->pm_state != pm_state) {
2297 if (port && port->ops->pm)
2298 port->ops->pm(port, pm_state, state->pm_state);
2299 state->pm_state = pm_state;
2304 struct uart_port *port;
2305 struct uart_driver *driver;
2308 static int serial_match_port(struct device *dev, void *data)
2310 struct uart_match *match = data;
2311 struct tty_driver *tty_drv = match->driver->tty_driver;
2312 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2315 return dev->devt == devt; /* Actually, only one tty per port */
2318 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2320 struct uart_state *state = drv->state + uport->line;
2321 struct tty_port *port = &state->port;
2322 struct device *tty_dev;
2323 struct uart_match match = {uport, drv};
2325 mutex_lock(&port->mutex);
2327 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2328 if (tty_dev && device_may_wakeup(tty_dev)) {
2329 enable_irq_wake(uport->irq);
2330 put_device(tty_dev);
2331 mutex_unlock(&port->mutex);
2334 put_device(tty_dev);
2337 * Nothing to do if the console is not suspending
2338 * except stop_rx to prevent any asynchronous data
2339 * over RX line. However ensure that we will be
2340 * able to Re-start_rx later.
2342 if (!console_suspend_enabled && uart_console(uport)) {
2343 if (uport->ops->start_rx)
2344 uport->ops->stop_rx(uport);
2348 uport->suspended = 1;
2350 if (tty_port_initialized(port)) {
2351 const struct uart_ops *ops = uport->ops;
2355 tty_port_set_suspended(port, 1);
2356 tty_port_set_initialized(port, 0);
2358 spin_lock_irq(&uport->lock);
2359 ops->stop_tx(uport);
2360 ops->set_mctrl(uport, 0);
2361 /* save mctrl so it can be restored on resume */
2362 mctrl = uport->mctrl;
2364 ops->stop_rx(uport);
2365 spin_unlock_irq(&uport->lock);
2368 * Wait for the transmitter to empty.
2370 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2373 dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2376 ops->shutdown(uport);
2377 uport->mctrl = mctrl;
2381 * Disable the console device before suspending.
2383 if (uart_console(uport))
2384 console_stop(uport->cons);
2386 uart_change_pm(state, UART_PM_STATE_OFF);
2388 mutex_unlock(&port->mutex);
2392 EXPORT_SYMBOL(uart_suspend_port);
2394 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2396 struct uart_state *state = drv->state + uport->line;
2397 struct tty_port *port = &state->port;
2398 struct device *tty_dev;
2399 struct uart_match match = {uport, drv};
2400 struct ktermios termios;
2402 mutex_lock(&port->mutex);
2404 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2405 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2406 if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2407 disable_irq_wake(uport->irq);
2408 put_device(tty_dev);
2409 mutex_unlock(&port->mutex);
2412 put_device(tty_dev);
2413 uport->suspended = 0;
2416 * Re-enable the console device after suspending.
2418 if (uart_console(uport)) {
2420 * First try to use the console cflag setting.
2422 memset(&termios, 0, sizeof(struct ktermios));
2423 termios.c_cflag = uport->cons->cflag;
2424 termios.c_ispeed = uport->cons->ispeed;
2425 termios.c_ospeed = uport->cons->ospeed;
2428 * If that's unset, use the tty termios setting.
2430 if (port->tty && termios.c_cflag == 0)
2431 termios = port->tty->termios;
2433 if (console_suspend_enabled)
2434 uart_change_pm(state, UART_PM_STATE_ON);
2435 uport->ops->set_termios(uport, &termios, NULL);
2436 if (!console_suspend_enabled && uport->ops->start_rx)
2437 uport->ops->start_rx(uport);
2438 if (console_suspend_enabled)
2439 console_start(uport->cons);
2442 if (tty_port_suspended(port)) {
2443 const struct uart_ops *ops = uport->ops;
2446 uart_change_pm(state, UART_PM_STATE_ON);
2447 spin_lock_irq(&uport->lock);
2448 ops->set_mctrl(uport, 0);
2449 spin_unlock_irq(&uport->lock);
2450 if (console_suspend_enabled || !uart_console(uport)) {
2451 /* Protected by port mutex for now */
2452 struct tty_struct *tty = port->tty;
2454 ret = ops->startup(uport);
2457 uart_change_speed(tty, state, NULL);
2458 spin_lock_irq(&uport->lock);
2459 ops->set_mctrl(uport, uport->mctrl);
2460 ops->start_tx(uport);
2461 spin_unlock_irq(&uport->lock);
2462 tty_port_set_initialized(port, 1);
2465 * Failed to resume - maybe hardware went away?
2466 * Clear the "initialized" flag so we won't try
2467 * to call the low level drivers shutdown method.
2469 uart_shutdown(tty, state);
2473 tty_port_set_suspended(port, 0);
2476 mutex_unlock(&port->mutex);
2480 EXPORT_SYMBOL(uart_resume_port);
2483 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2487 switch (port->iotype) {
2489 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2492 snprintf(address, sizeof(address),
2493 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2501 snprintf(address, sizeof(address),
2502 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2505 strlcpy(address, "*unknown*", sizeof(address));
2509 pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
2510 port->dev ? dev_name(port->dev) : "",
2511 port->dev ? ": " : "",
2513 address, port->irq, port->uartclk / 16, uart_type(port));
2515 /* The magic multiplier feature is a bit obscure, so report it too. */
2516 if (port->flags & UPF_MAGIC_MULTIPLIER)
2517 pr_info("%s%s%s extra baud rates supported: %d, %d",
2518 port->dev ? dev_name(port->dev) : "",
2519 port->dev ? ": " : "",
2521 port->uartclk / 8, port->uartclk / 4);
2525 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2526 struct uart_port *port)
2531 * If there isn't a port here, don't do anything further.
2533 if (!port->iobase && !port->mapbase && !port->membase)
2537 * Now do the auto configuration stuff. Note that config_port
2538 * is expected to claim the resources and map the port for us.
2541 if (port->flags & UPF_AUTO_IRQ)
2542 flags |= UART_CONFIG_IRQ;
2543 if (port->flags & UPF_BOOT_AUTOCONF) {
2544 if (!(port->flags & UPF_FIXED_TYPE)) {
2545 port->type = PORT_UNKNOWN;
2546 flags |= UART_CONFIG_TYPE;
2548 port->ops->config_port(port, flags);
2551 if (port->type != PORT_UNKNOWN) {
2552 unsigned long flags;
2554 uart_report_port(drv, port);
2556 /* Power up port for set_mctrl() */
2557 uart_change_pm(state, UART_PM_STATE_ON);
2560 * Ensure that the modem control lines are de-activated.
2561 * keep the DTR setting that is set in uart_set_options()
2562 * We probably don't need a spinlock around this, but
2564 spin_lock_irqsave(&port->lock, flags);
2565 port->mctrl &= TIOCM_DTR;
2566 if (port->rs485.flags & SER_RS485_ENABLED &&
2567 !(port->rs485.flags & SER_RS485_RTS_AFTER_SEND))
2568 port->mctrl |= TIOCM_RTS;
2569 port->ops->set_mctrl(port, port->mctrl);
2570 spin_unlock_irqrestore(&port->lock, flags);
2573 * If this driver supports console, and it hasn't been
2574 * successfully registered yet, try to re-register it.
2575 * It may be that the port was not available.
2577 if (port->cons && !(port->cons->flags & CON_ENABLED))
2578 register_console(port->cons);
2581 * Power down all ports by default, except the
2582 * console if we have one.
2584 if (!uart_console(port))
2585 uart_change_pm(state, UART_PM_STATE_OFF);
2589 #ifdef CONFIG_CONSOLE_POLL
2591 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2593 struct uart_driver *drv = driver->driver_state;
2594 struct uart_state *state = drv->state + line;
2595 struct tty_port *tport;
2596 struct uart_port *port;
2603 tport = &state->port;
2604 mutex_lock(&tport->mutex);
2606 port = uart_port_check(state);
2607 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
2612 if (port->ops->poll_init) {
2614 * We don't set initialized as we only initialized the hw,
2615 * e.g. state->xmit is still uninitialized.
2617 if (!tty_port_initialized(tport))
2618 ret = port->ops->poll_init(port);
2621 if (!ret && options) {
2622 uart_parse_options(options, &baud, &parity, &bits, &flow);
2623 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2626 mutex_unlock(&tport->mutex);
2630 static int uart_poll_get_char(struct tty_driver *driver, int line)
2632 struct uart_driver *drv = driver->driver_state;
2633 struct uart_state *state = drv->state + line;
2634 struct uart_port *port;
2637 port = uart_port_ref(state);
2639 ret = port->ops->poll_get_char(port);
2640 uart_port_deref(port);
2646 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2648 struct uart_driver *drv = driver->driver_state;
2649 struct uart_state *state = drv->state + line;
2650 struct uart_port *port;
2652 port = uart_port_ref(state);
2657 port->ops->poll_put_char(port, '\r');
2658 port->ops->poll_put_char(port, ch);
2659 uart_port_deref(port);
2663 static const struct tty_operations uart_ops = {
2664 .install = uart_install,
2666 .close = uart_close,
2667 .write = uart_write,
2668 .put_char = uart_put_char,
2669 .flush_chars = uart_flush_chars,
2670 .write_room = uart_write_room,
2671 .chars_in_buffer= uart_chars_in_buffer,
2672 .flush_buffer = uart_flush_buffer,
2673 .ioctl = uart_ioctl,
2674 .throttle = uart_throttle,
2675 .unthrottle = uart_unthrottle,
2676 .send_xchar = uart_send_xchar,
2677 .set_termios = uart_set_termios,
2678 .set_ldisc = uart_set_ldisc,
2680 .start = uart_start,
2681 .hangup = uart_hangup,
2682 .break_ctl = uart_break_ctl,
2683 .wait_until_sent= uart_wait_until_sent,
2684 #ifdef CONFIG_PROC_FS
2685 .proc_show = uart_proc_show,
2687 .tiocmget = uart_tiocmget,
2688 .tiocmset = uart_tiocmset,
2689 .set_serial = uart_set_info_user,
2690 .get_serial = uart_get_info_user,
2691 .get_icount = uart_get_icount,
2692 #ifdef CONFIG_CONSOLE_POLL
2693 .poll_init = uart_poll_init,
2694 .poll_get_char = uart_poll_get_char,
2695 .poll_put_char = uart_poll_put_char,
2699 static const struct tty_port_operations uart_port_ops = {
2700 .carrier_raised = uart_carrier_raised,
2701 .dtr_rts = uart_dtr_rts,
2702 .activate = uart_port_activate,
2703 .shutdown = uart_tty_port_shutdown,
2707 * uart_register_driver - register a driver with the uart core layer
2708 * @drv: low level driver structure
2710 * Register a uart driver with the core driver. We in turn register with the
2711 * tty layer, and initialise the core driver per-port state.
2713 * We have a proc file in /proc/tty/driver which is named after the normal
2716 * @drv->port should be %NULL, and the per-port structures should be registered
2717 * using uart_add_one_port() after this call has succeeded.
2719 * Locking: none, Interrupts: enabled
2721 int uart_register_driver(struct uart_driver *drv)
2723 struct tty_driver *normal;
2724 int i, retval = -ENOMEM;
2729 * Maybe we should be using a slab cache for this, especially if
2730 * we have a large number of ports to handle.
2732 drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL);
2736 normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
2737 TTY_DRIVER_DYNAMIC_DEV);
2738 if (IS_ERR(normal)) {
2739 retval = PTR_ERR(normal);
2743 drv->tty_driver = normal;
2745 normal->driver_name = drv->driver_name;
2746 normal->name = drv->dev_name;
2747 normal->major = drv->major;
2748 normal->minor_start = drv->minor;
2749 normal->type = TTY_DRIVER_TYPE_SERIAL;
2750 normal->subtype = SERIAL_TYPE_NORMAL;
2751 normal->init_termios = tty_std_termios;
2752 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2753 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2754 normal->driver_state = drv;
2755 tty_set_operations(normal, &uart_ops);
2758 * Initialise the UART state(s).
2760 for (i = 0; i < drv->nr; i++) {
2761 struct uart_state *state = drv->state + i;
2762 struct tty_port *port = &state->port;
2764 tty_port_init(port);
2765 port->ops = &uart_port_ops;
2768 retval = tty_register_driver(normal);
2772 for (i = 0; i < drv->nr; i++)
2773 tty_port_destroy(&drv->state[i].port);
2774 tty_driver_kref_put(normal);
2780 EXPORT_SYMBOL(uart_register_driver);
2783 * uart_unregister_driver - remove a driver from the uart core layer
2784 * @drv: low level driver structure
2786 * Remove all references to a driver from the core driver. The low level
2787 * driver must have removed all its ports via the uart_remove_one_port() if it
2788 * registered them with uart_add_one_port(). (I.e. @drv->port is %NULL.)
2790 * Locking: none, Interrupts: enabled
2792 void uart_unregister_driver(struct uart_driver *drv)
2794 struct tty_driver *p = drv->tty_driver;
2797 tty_unregister_driver(p);
2798 tty_driver_kref_put(p);
2799 for (i = 0; i < drv->nr; i++)
2800 tty_port_destroy(&drv->state[i].port);
2803 drv->tty_driver = NULL;
2805 EXPORT_SYMBOL(uart_unregister_driver);
2807 struct tty_driver *uart_console_device(struct console *co, int *index)
2809 struct uart_driver *p = co->data;
2811 return p->tty_driver;
2813 EXPORT_SYMBOL_GPL(uart_console_device);
2815 static ssize_t uartclk_show(struct device *dev,
2816 struct device_attribute *attr, char *buf)
2818 struct serial_struct tmp;
2819 struct tty_port *port = dev_get_drvdata(dev);
2821 uart_get_info(port, &tmp);
2822 return sprintf(buf, "%d\n", tmp.baud_base * 16);
2825 static ssize_t type_show(struct device *dev,
2826 struct device_attribute *attr, char *buf)
2828 struct serial_struct tmp;
2829 struct tty_port *port = dev_get_drvdata(dev);
2831 uart_get_info(port, &tmp);
2832 return sprintf(buf, "%d\n", tmp.type);
2835 static ssize_t line_show(struct device *dev,
2836 struct device_attribute *attr, char *buf)
2838 struct serial_struct tmp;
2839 struct tty_port *port = dev_get_drvdata(dev);
2841 uart_get_info(port, &tmp);
2842 return sprintf(buf, "%d\n", tmp.line);
2845 static ssize_t port_show(struct device *dev,
2846 struct device_attribute *attr, char *buf)
2848 struct serial_struct tmp;
2849 struct tty_port *port = dev_get_drvdata(dev);
2850 unsigned long ioaddr;
2852 uart_get_info(port, &tmp);
2854 if (HIGH_BITS_OFFSET)
2855 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2856 return sprintf(buf, "0x%lX\n", ioaddr);
2859 static ssize_t irq_show(struct device *dev,
2860 struct device_attribute *attr, char *buf)
2862 struct serial_struct tmp;
2863 struct tty_port *port = dev_get_drvdata(dev);
2865 uart_get_info(port, &tmp);
2866 return sprintf(buf, "%d\n", tmp.irq);
2869 static ssize_t flags_show(struct device *dev,
2870 struct device_attribute *attr, char *buf)
2872 struct serial_struct tmp;
2873 struct tty_port *port = dev_get_drvdata(dev);
2875 uart_get_info(port, &tmp);
2876 return sprintf(buf, "0x%X\n", tmp.flags);
2879 static ssize_t xmit_fifo_size_show(struct device *dev,
2880 struct device_attribute *attr, char *buf)
2882 struct serial_struct tmp;
2883 struct tty_port *port = dev_get_drvdata(dev);
2885 uart_get_info(port, &tmp);
2886 return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
2889 static ssize_t close_delay_show(struct device *dev,
2890 struct device_attribute *attr, char *buf)
2892 struct serial_struct tmp;
2893 struct tty_port *port = dev_get_drvdata(dev);
2895 uart_get_info(port, &tmp);
2896 return sprintf(buf, "%d\n", tmp.close_delay);
2899 static ssize_t closing_wait_show(struct device *dev,
2900 struct device_attribute *attr, char *buf)
2902 struct serial_struct tmp;
2903 struct tty_port *port = dev_get_drvdata(dev);
2905 uart_get_info(port, &tmp);
2906 return sprintf(buf, "%d\n", tmp.closing_wait);
2909 static ssize_t custom_divisor_show(struct device *dev,
2910 struct device_attribute *attr, char *buf)
2912 struct serial_struct tmp;
2913 struct tty_port *port = dev_get_drvdata(dev);
2915 uart_get_info(port, &tmp);
2916 return sprintf(buf, "%d\n", tmp.custom_divisor);
2919 static ssize_t io_type_show(struct device *dev,
2920 struct device_attribute *attr, char *buf)
2922 struct serial_struct tmp;
2923 struct tty_port *port = dev_get_drvdata(dev);
2925 uart_get_info(port, &tmp);
2926 return sprintf(buf, "%d\n", tmp.io_type);
2929 static ssize_t iomem_base_show(struct device *dev,
2930 struct device_attribute *attr, char *buf)
2932 struct serial_struct tmp;
2933 struct tty_port *port = dev_get_drvdata(dev);
2935 uart_get_info(port, &tmp);
2936 return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
2939 static ssize_t iomem_reg_shift_show(struct device *dev,
2940 struct device_attribute *attr, char *buf)
2942 struct serial_struct tmp;
2943 struct tty_port *port = dev_get_drvdata(dev);
2945 uart_get_info(port, &tmp);
2946 return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
2949 static ssize_t console_show(struct device *dev,
2950 struct device_attribute *attr, char *buf)
2952 struct tty_port *port = dev_get_drvdata(dev);
2953 struct uart_state *state = container_of(port, struct uart_state, port);
2954 struct uart_port *uport;
2955 bool console = false;
2957 mutex_lock(&port->mutex);
2958 uport = uart_port_check(state);
2960 console = uart_console_enabled(uport);
2961 mutex_unlock(&port->mutex);
2963 return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2966 static ssize_t console_store(struct device *dev,
2967 struct device_attribute *attr, const char *buf, size_t count)
2969 struct tty_port *port = dev_get_drvdata(dev);
2970 struct uart_state *state = container_of(port, struct uart_state, port);
2971 struct uart_port *uport;
2972 bool oldconsole, newconsole;
2975 ret = kstrtobool(buf, &newconsole);
2979 mutex_lock(&port->mutex);
2980 uport = uart_port_check(state);
2982 oldconsole = uart_console_enabled(uport);
2983 if (oldconsole && !newconsole) {
2984 ret = unregister_console(uport->cons);
2985 } else if (!oldconsole && newconsole) {
2986 if (uart_console(uport)) {
2987 uport->console_reinit = 1;
2988 register_console(uport->cons);
2996 mutex_unlock(&port->mutex);
2998 return ret < 0 ? ret : count;
3001 static DEVICE_ATTR_RO(uartclk);
3002 static DEVICE_ATTR_RO(type);
3003 static DEVICE_ATTR_RO(line);
3004 static DEVICE_ATTR_RO(port);
3005 static DEVICE_ATTR_RO(irq);
3006 static DEVICE_ATTR_RO(flags);
3007 static DEVICE_ATTR_RO(xmit_fifo_size);
3008 static DEVICE_ATTR_RO(close_delay);
3009 static DEVICE_ATTR_RO(closing_wait);
3010 static DEVICE_ATTR_RO(custom_divisor);
3011 static DEVICE_ATTR_RO(io_type);
3012 static DEVICE_ATTR_RO(iomem_base);
3013 static DEVICE_ATTR_RO(iomem_reg_shift);
3014 static DEVICE_ATTR_RW(console);
3016 static struct attribute *tty_dev_attrs[] = {
3017 &dev_attr_uartclk.attr,
3018 &dev_attr_type.attr,
3019 &dev_attr_line.attr,
3020 &dev_attr_port.attr,
3022 &dev_attr_flags.attr,
3023 &dev_attr_xmit_fifo_size.attr,
3024 &dev_attr_close_delay.attr,
3025 &dev_attr_closing_wait.attr,
3026 &dev_attr_custom_divisor.attr,
3027 &dev_attr_io_type.attr,
3028 &dev_attr_iomem_base.attr,
3029 &dev_attr_iomem_reg_shift.attr,
3030 &dev_attr_console.attr,
3034 static const struct attribute_group tty_dev_attr_group = {
3035 .attrs = tty_dev_attrs,
3039 * uart_add_one_port - attach a driver-defined port structure
3040 * @drv: pointer to the uart low level driver structure for this port
3041 * @uport: uart port structure to use for this port.
3043 * Context: task context, might sleep
3045 * This allows the driver @drv to register its own uart_port structure with the
3046 * core driver. The main purpose is to allow the low level uart drivers to
3047 * expand uart_port, rather than having yet more levels of structures.
3049 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
3051 struct uart_state *state;
3052 struct tty_port *port;
3054 struct device *tty_dev;
3057 if (uport->line >= drv->nr)
3060 state = drv->state + uport->line;
3061 port = &state->port;
3063 mutex_lock(&port_mutex);
3064 mutex_lock(&port->mutex);
3065 if (state->uart_port) {
3070 /* Link the port to the driver state table and vice versa */
3071 atomic_set(&state->refcount, 1);
3072 init_waitqueue_head(&state->remove_wait);
3073 state->uart_port = uport;
3074 uport->state = state;
3076 state->pm_state = UART_PM_STATE_UNDEFINED;
3077 uport->cons = drv->cons;
3078 uport->minor = drv->tty_driver->minor_start + uport->line;
3079 uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
3080 drv->tty_driver->name_base + uport->line);
3087 * If this port is in use as a console then the spinlock is already
3090 if (!uart_console_enabled(uport))
3091 uart_port_spin_lock_init(uport);
3093 if (uport->cons && uport->dev)
3094 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
3096 tty_port_link_device(port, drv->tty_driver, uport->line);
3097 uart_configure_port(drv, state, uport);
3099 port->console = uart_console(uport);
3102 if (uport->attr_group)
3105 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
3107 if (!uport->tty_groups) {
3111 uport->tty_groups[0] = &tty_dev_attr_group;
3112 if (uport->attr_group)
3113 uport->tty_groups[1] = uport->attr_group;
3116 * Register the port whether it's detected or not. This allows
3117 * setserial to be used to alter this port's parameters.
3119 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
3120 uport->line, uport->dev, port, uport->tty_groups);
3121 if (!IS_ERR(tty_dev)) {
3122 device_set_wakeup_capable(tty_dev, 1);
3124 dev_err(uport->dev, "Cannot register tty device on line %d\n",
3129 * Ensure UPF_DEAD is not set.
3131 uport->flags &= ~UPF_DEAD;
3134 mutex_unlock(&port->mutex);
3135 mutex_unlock(&port_mutex);
3139 EXPORT_SYMBOL(uart_add_one_port);
3142 * uart_remove_one_port - detach a driver defined port structure
3143 * @drv: pointer to the uart low level driver structure for this port
3144 * @uport: uart port structure for this port
3146 * Context: task context, might sleep
3148 * This unhooks (and hangs up) the specified port structure from the core
3149 * driver. No further calls will be made to the low-level code for this port.
3151 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
3153 struct uart_state *state = drv->state + uport->line;
3154 struct tty_port *port = &state->port;
3155 struct uart_port *uart_port;
3156 struct tty_struct *tty;
3159 mutex_lock(&port_mutex);
3162 * Mark the port "dead" - this prevents any opens from
3163 * succeeding while we shut down the port.
3165 mutex_lock(&port->mutex);
3166 uart_port = uart_port_check(state);
3167 if (uart_port != uport)
3168 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
3172 mutex_unlock(&port->mutex);
3176 uport->flags |= UPF_DEAD;
3177 mutex_unlock(&port->mutex);
3180 * Remove the devices from the tty layer
3182 tty_port_unregister_device(port, drv->tty_driver, uport->line);
3184 tty = tty_port_tty_get(port);
3186 tty_vhangup(port->tty);
3191 * If the port is used as a console, unregister it
3193 if (uart_console(uport))
3194 unregister_console(uport->cons);
3197 * Free the port IO and memory resources, if any.
3199 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
3200 uport->ops->release_port(uport);
3201 kfree(uport->tty_groups);
3205 * Indicate that there isn't a port here anymore.
3207 uport->type = PORT_UNKNOWN;
3209 mutex_lock(&port->mutex);
3210 WARN_ON(atomic_dec_return(&state->refcount) < 0);
3211 wait_event(state->remove_wait, !atomic_read(&state->refcount));
3212 state->uart_port = NULL;
3213 mutex_unlock(&port->mutex);
3215 mutex_unlock(&port_mutex);
3219 EXPORT_SYMBOL(uart_remove_one_port);
3222 * uart_match_port - are the two ports equivalent?
3223 * @port1: first port
3224 * @port2: second port
3226 * This utility function can be used to determine whether two uart_port
3227 * structures describe the same port.
3229 bool uart_match_port(const struct uart_port *port1,
3230 const struct uart_port *port2)
3232 if (port1->iotype != port2->iotype)
3235 switch (port1->iotype) {
3237 return port1->iobase == port2->iobase;
3239 return port1->iobase == port2->iobase &&
3240 port1->hub6 == port2->hub6;
3247 return port1->mapbase == port2->mapbase;
3252 EXPORT_SYMBOL(uart_match_port);
3255 * uart_handle_dcd_change - handle a change of carrier detect state
3256 * @uport: uart_port structure for the open port
3257 * @status: new carrier detect status, nonzero if active
3259 * Caller must hold uport->lock.
3261 void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
3263 struct tty_port *port = &uport->state->port;
3264 struct tty_struct *tty = port->tty;
3265 struct tty_ldisc *ld;
3267 lockdep_assert_held_once(&uport->lock);
3270 ld = tty_ldisc_ref(tty);
3272 if (ld->ops->dcd_change)
3273 ld->ops->dcd_change(tty, status);
3274 tty_ldisc_deref(ld);
3278 uport->icount.dcd++;
3280 if (uart_dcd_enabled(uport)) {
3282 wake_up_interruptible(&port->open_wait);
3287 EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3290 * uart_handle_cts_change - handle a change of clear-to-send state
3291 * @uport: uart_port structure for the open port
3292 * @status: new clear to send status, nonzero if active
3294 * Caller must hold uport->lock.
3296 void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
3298 lockdep_assert_held_once(&uport->lock);
3300 uport->icount.cts++;
3302 if (uart_softcts_mode(uport)) {
3303 if (uport->hw_stopped) {
3305 uport->hw_stopped = 0;
3306 uport->ops->start_tx(uport);
3307 uart_write_wakeup(uport);
3311 uport->hw_stopped = 1;
3312 uport->ops->stop_tx(uport);
3318 EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3321 * uart_insert_char - push a char to the uart layer
3323 * User is responsible to call tty_flip_buffer_push when they are done with
3326 * @port: corresponding port
3327 * @status: state of the serial port RX buffer (LSR for 8250)
3328 * @overrun: mask of overrun bits in @status
3329 * @ch: character to push
3330 * @flag: flag for the character (see TTY_NORMAL and friends)
3332 void uart_insert_char(struct uart_port *port, unsigned int status,
3333 unsigned int overrun, unsigned int ch, unsigned int flag)
3335 struct tty_port *tport = &port->state->port;
3337 if ((status & port->ignore_status_mask & ~overrun) == 0)
3338 if (tty_insert_flip_char(tport, ch, flag) == 0)
3339 ++port->icount.buf_overrun;
3342 * Overrun is special. Since it's reported immediately,
3343 * it doesn't affect the current character.
3345 if (status & ~port->ignore_status_mask & overrun)
3346 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3347 ++port->icount.buf_overrun;
3349 EXPORT_SYMBOL_GPL(uart_insert_char);
3351 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
3352 static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
3354 static void uart_sysrq_on(struct work_struct *w)
3356 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3358 sysrq_toggle_support(1);
3359 pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3360 sysrq_toggle_seq_len, sysrq_toggle_seq);
3362 static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
3365 * uart_try_toggle_sysrq - Enables SysRq from serial line
3366 * @port: uart_port structure where char(s) after BREAK met
3367 * @ch: new character in the sequence after received BREAK
3369 * Enables magic SysRq when the required sequence is met on port
3370 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3372 * Returns: %false if @ch is out of enabling sequence and should be
3373 * handled some other way, %true if @ch was consumed.
3375 bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
3377 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3379 if (!sysrq_toggle_seq_len)
3382 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
3383 if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
3384 port->sysrq_seq = 0;
3388 if (++port->sysrq_seq < sysrq_toggle_seq_len) {
3389 port->sysrq = jiffies + SYSRQ_TIMEOUT;
3393 schedule_work(&sysrq_enable_work);
3398 EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
3402 * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3403 * @port: uart device's target port
3405 * This function implements the device tree binding described in
3406 * Documentation/devicetree/bindings/serial/rs485.txt.
3408 int uart_get_rs485_mode(struct uart_port *port)
3410 struct serial_rs485 *rs485conf = &port->rs485;
3411 struct device *dev = port->dev;
3415 ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3418 rs485conf->delay_rts_before_send = rs485_delay[0];
3419 rs485conf->delay_rts_after_send = rs485_delay[1];
3421 rs485conf->delay_rts_before_send = 0;
3422 rs485conf->delay_rts_after_send = 0;
3425 uart_sanitize_serial_rs485_delays(port, rs485conf);
3428 * Clear full-duplex and enabled flags, set RTS polarity to active high
3429 * to get to a defined state with the following properties:
3431 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3432 SER_RS485_TERMINATE_BUS |
3433 SER_RS485_RTS_AFTER_SEND);
3434 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
3436 if (device_property_read_bool(dev, "rs485-rx-during-tx"))
3437 rs485conf->flags |= SER_RS485_RX_DURING_TX;
3439 if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
3440 rs485conf->flags |= SER_RS485_ENABLED;
3442 if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3443 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3444 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3448 * Disabling termination by default is the safe choice: Else if many
3449 * bus participants enable it, no communication is possible at all.
3450 * Works fine for short cables and users may enable for longer cables.
3452 port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3454 if (IS_ERR(port->rs485_term_gpio)) {
3455 ret = PTR_ERR(port->rs485_term_gpio);
3456 port->rs485_term_gpio = NULL;
3457 return dev_err_probe(dev, ret, "Cannot get rs485-term-gpios\n");
3459 if (port->rs485_term_gpio)
3460 port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS;
3464 EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
3466 /* Compile-time assertions for serial_rs485 layout */
3467 static_assert(offsetof(struct serial_rs485, padding) ==
3468 (offsetof(struct serial_rs485, delay_rts_after_send) + sizeof(__u32)));
3469 static_assert(offsetof(struct serial_rs485, padding1) ==
3470 offsetof(struct serial_rs485, padding[1]));
3471 static_assert((offsetof(struct serial_rs485, padding[4]) + sizeof(__u32)) ==
3472 sizeof(struct serial_rs485));
3474 MODULE_DESCRIPTION("Serial driver core");
3475 MODULE_LICENSE("GPL");