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>
18 #include <linux/kernel.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/device.h>
23 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
24 #include <linux/serial_core.h>
25 #include <linux/sysrq.h>
26 #include <linux/delay.h>
27 #include <linux/mutex.h>
28 #include <linux/math64.h>
29 #include <linux/security.h>
31 #include <linux/irq.h>
32 #include <linux/uaccess.h>
35 * This is used to lock changes in serial line configuration.
37 static DEFINE_MUTEX(port_mutex);
40 * lockdep: port->lock is initialized in two places, but we
41 * want only one lock-class:
43 static struct lock_class_key port_lock_key;
45 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
48 * Max time with active RTS before/after data is sent.
50 #define RS485_MAX_RTS_DELAY 100 /* msecs */
52 static void uart_change_pm(struct uart_state *state,
53 enum uart_pm_state pm_state);
55 static void uart_port_shutdown(struct tty_port *port);
57 static int uart_dcd_enabled(struct uart_port *uport)
59 return !!(uport->status & UPSTAT_DCD_ENABLE);
62 static inline struct uart_port *uart_port_ref(struct uart_state *state)
64 if (atomic_add_unless(&state->refcount, 1, 0))
65 return state->uart_port;
69 static inline void uart_port_deref(struct uart_port *uport)
71 if (atomic_dec_and_test(&uport->state->refcount))
72 wake_up(&uport->state->remove_wait);
75 #define uart_port_lock(state, flags) \
77 struct uart_port *__uport = uart_port_ref(state); \
79 spin_lock_irqsave(&__uport->lock, flags); \
83 #define uart_port_unlock(uport, flags) \
85 struct uart_port *__uport = uport; \
87 spin_unlock_irqrestore(&__uport->lock, flags); \
88 uart_port_deref(__uport); \
92 static inline struct uart_port *uart_port_check(struct uart_state *state)
94 lockdep_assert_held(&state->port.mutex);
95 return state->uart_port;
99 * uart_write_wakeup - schedule write processing
100 * @port: port to be processed
102 * This routine is used by the interrupt handler to schedule processing in the
103 * software interrupt portion of the driver. A driver is expected to call this
104 * function when the number of characters in the transmit buffer have dropped
107 * Locking: @port->lock should be held
109 void uart_write_wakeup(struct uart_port *port)
111 struct uart_state *state = port->state;
113 * This means you called this function _after_ the port was
114 * closed. No cookie for you.
117 tty_port_tty_wakeup(&state->port);
119 EXPORT_SYMBOL(uart_write_wakeup);
121 static void uart_stop(struct tty_struct *tty)
123 struct uart_state *state = tty->driver_data;
124 struct uart_port *port;
127 port = uart_port_lock(state, flags);
129 port->ops->stop_tx(port);
130 uart_port_unlock(port, flags);
133 static void __uart_start(struct tty_struct *tty)
135 struct uart_state *state = tty->driver_data;
136 struct uart_port *port = state->uart_port;
138 if (port && !(port->flags & UPF_DEAD) && !uart_tx_stopped(port))
139 port->ops->start_tx(port);
142 static void uart_start(struct tty_struct *tty)
144 struct uart_state *state = tty->driver_data;
145 struct uart_port *port;
148 port = uart_port_lock(state, flags);
150 uart_port_unlock(port, flags);
154 uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
159 spin_lock_irqsave(&port->lock, flags);
161 port->mctrl = (old & ~clear) | set;
162 if (old != port->mctrl && !(port->rs485.flags & SER_RS485_ENABLED))
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, bool active)
173 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
175 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
178 /* Caller holds port mutex */
179 static void uart_change_line_settings(struct tty_struct *tty, struct uart_state *state,
180 const struct ktermios *old_termios)
182 struct uart_port *uport = uart_port_check(state);
183 struct ktermios *termios;
187 * If we have no tty, termios, or the port does not exist,
188 * then we can't set the parameters for this port.
190 if (!tty || uport->type == PORT_UNKNOWN)
193 termios = &tty->termios;
194 uport->ops->set_termios(uport, termios, old_termios);
197 * Set modem status enables based on termios cflag
199 spin_lock_irq(&uport->lock);
200 if (termios->c_cflag & CRTSCTS)
201 uport->status |= UPSTAT_CTS_ENABLE;
203 uport->status &= ~UPSTAT_CTS_ENABLE;
205 if (termios->c_cflag & CLOCAL)
206 uport->status &= ~UPSTAT_DCD_ENABLE;
208 uport->status |= UPSTAT_DCD_ENABLE;
210 /* reset sw-assisted CTS flow control based on (possibly) new mode */
211 old_hw_stopped = uport->hw_stopped;
212 uport->hw_stopped = uart_softcts_mode(uport) &&
213 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
214 if (uport->hw_stopped != old_hw_stopped) {
216 uport->ops->stop_tx(uport);
220 spin_unlock_irq(&uport->lock);
224 * Startup the port. This will be called once per open. All calls
225 * will be serialised by the per-port mutex.
227 static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
230 struct uart_port *uport = uart_port_check(state);
235 if (uport->type == PORT_UNKNOWN)
239 * Make sure the device is in D0 state.
241 uart_change_pm(state, UART_PM_STATE_ON);
244 * Initialise and allocate the transmit and temporary
247 page = get_zeroed_page(GFP_KERNEL);
251 uart_port_lock(state, flags);
252 if (!state->xmit.buf) {
253 state->xmit.buf = (unsigned char *) page;
254 uart_circ_clear(&state->xmit);
255 uart_port_unlock(uport, flags);
257 uart_port_unlock(uport, flags);
259 * Do not free() the page under the port lock, see
265 retval = uport->ops->startup(uport);
267 if (uart_console(uport) && uport->cons->cflag) {
268 tty->termios.c_cflag = uport->cons->cflag;
269 tty->termios.c_ispeed = uport->cons->ispeed;
270 tty->termios.c_ospeed = uport->cons->ospeed;
271 uport->cons->cflag = 0;
272 uport->cons->ispeed = 0;
273 uport->cons->ospeed = 0;
276 * Initialise the hardware port settings.
278 uart_change_line_settings(tty, state, NULL);
281 * Setup the RTS and DTR signals once the
282 * port is open and ready to respond.
284 if (init_hw && C_BAUD(tty))
285 uart_port_dtr_rts(uport, true);
289 * This is to allow setserial on this port. People may want to set
290 * port/irq/type and then reconfigure the port properly if it failed
293 if (retval && capable(CAP_SYS_ADMIN))
299 static int uart_startup(struct tty_struct *tty, struct uart_state *state,
302 struct tty_port *port = &state->port;
305 if (tty_port_initialized(port))
308 retval = uart_port_startup(tty, state, init_hw);
310 set_bit(TTY_IO_ERROR, &tty->flags);
316 * This routine will shutdown a serial port; interrupts are disabled, and
317 * DTR is dropped if the hangup on close termio flag is on. Calls to
318 * uart_shutdown are serialised by the per-port semaphore.
320 * uport == NULL if uart_port has already been removed
322 static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
324 struct uart_port *uport = uart_port_check(state);
325 struct tty_port *port = &state->port;
327 char *xmit_buf = NULL;
330 * Set the TTY IO error marker
333 set_bit(TTY_IO_ERROR, &tty->flags);
335 if (tty_port_initialized(port)) {
336 tty_port_set_initialized(port, false);
339 * Turn off DTR and RTS early.
341 if (uport && uart_console(uport) && tty) {
342 uport->cons->cflag = tty->termios.c_cflag;
343 uport->cons->ispeed = tty->termios.c_ispeed;
344 uport->cons->ospeed = tty->termios.c_ospeed;
347 if (!tty || C_HUPCL(tty))
348 uart_port_dtr_rts(uport, false);
350 uart_port_shutdown(port);
354 * It's possible for shutdown to be called after suspend if we get
355 * a DCD drop (hangup) at just the right time. Clear suspended bit so
356 * we don't try to resume a port that has been shutdown.
358 tty_port_set_suspended(port, false);
361 * Do not free() the transmit buffer page under the port lock since
362 * this can create various circular locking scenarios. For instance,
363 * console driver may need to allocate/free a debug object, which
364 * can endup in printk() recursion.
366 uart_port_lock(state, flags);
367 xmit_buf = state->xmit.buf;
368 state->xmit.buf = NULL;
369 uart_port_unlock(uport, flags);
371 free_page((unsigned long)xmit_buf);
375 * uart_update_timeout - update per-port frame timing information
376 * @port: uart_port structure describing the port
377 * @cflag: termios cflag value
378 * @baud: speed of the port
380 * Set the @port frame timing information from which the FIFO timeout value is
381 * derived. The @cflag value should reflect the actual hardware settings as
382 * number of bits, parity, stop bits and baud rate is taken into account here.
384 * Locking: caller is expected to take @port->lock
387 uart_update_timeout(struct uart_port *port, unsigned int cflag,
390 unsigned int size = tty_get_frame_size(cflag);
393 frame_time = (u64)size * NSEC_PER_SEC;
394 port->frame_time = DIV64_U64_ROUND_UP(frame_time, baud);
396 EXPORT_SYMBOL(uart_update_timeout);
399 * uart_get_baud_rate - return baud rate for a particular port
400 * @port: uart_port structure describing the port in question.
401 * @termios: desired termios settings
402 * @old: old termios (or %NULL)
403 * @min: minimum acceptable baud rate
404 * @max: maximum acceptable baud rate
406 * Decode the termios structure into a numeric baud rate, taking account of the
407 * magic 38400 baud rate (with spd_* flags), and mapping the %B0 rate to 9600
410 * If the new baud rate is invalid, try the @old termios setting. If it's still
411 * invalid, we try 9600 baud.
413 * The @termios structure is updated to reflect the baud rate we're actually
414 * going to be using. Don't do this for the case where B0 is requested ("hang
417 * Locking: caller dependent
420 uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
421 const struct ktermios *old, unsigned int min, unsigned int max)
425 unsigned int altbaud;
427 upf_t flags = port->flags & UPF_SPD_MASK;
447 for (try = 0; try < 2; try++) {
448 baud = tty_termios_baud_rate(termios);
451 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
454 if (try == 0 && baud == 38400)
458 * Special case: B0 rate.
465 if (baud >= min && baud <= max)
469 * Oops, the quotient was zero. Try again with
470 * the old baud rate if possible.
472 termios->c_cflag &= ~CBAUD;
474 baud = tty_termios_baud_rate(old);
476 tty_termios_encode_baud_rate(termios,
483 * As a last resort, if the range cannot be met then clip to
484 * the nearest chip supported rate.
488 tty_termios_encode_baud_rate(termios,
491 tty_termios_encode_baud_rate(termios,
495 /* Should never happen */
499 EXPORT_SYMBOL(uart_get_baud_rate);
502 * uart_get_divisor - return uart clock divisor
503 * @port: uart_port structure describing the port
504 * @baud: desired baud rate
506 * Calculate the divisor (baud_base / baud) for the specified @baud,
507 * appropriately rounded.
509 * If 38400 baud and custom divisor is selected, return the custom divisor
512 * Locking: caller dependent
515 uart_get_divisor(struct uart_port *port, unsigned int baud)
520 * Old custom speed handling.
522 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
523 quot = port->custom_divisor;
525 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
529 EXPORT_SYMBOL(uart_get_divisor);
531 static int uart_put_char(struct tty_struct *tty, unsigned char c)
533 struct uart_state *state = tty->driver_data;
534 struct uart_port *port;
535 struct circ_buf *circ;
540 port = uart_port_lock(state, flags);
542 uart_port_unlock(port, flags);
546 if (port && uart_circ_chars_free(circ) != 0) {
547 circ->buf[circ->head] = c;
548 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
551 uart_port_unlock(port, flags);
555 static void uart_flush_chars(struct tty_struct *tty)
560 static int uart_write(struct tty_struct *tty,
561 const unsigned char *buf, int count)
563 struct uart_state *state = tty->driver_data;
564 struct uart_port *port;
565 struct circ_buf *circ;
570 * This means you called this function _after_ the port was
571 * closed. No cookie for you.
578 port = uart_port_lock(state, flags);
581 uart_port_unlock(port, flags);
586 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
591 memcpy(circ->buf + circ->head, buf, c);
592 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
599 uart_port_unlock(port, flags);
603 static unsigned int uart_write_room(struct tty_struct *tty)
605 struct uart_state *state = tty->driver_data;
606 struct uart_port *port;
610 port = uart_port_lock(state, flags);
611 ret = uart_circ_chars_free(&state->xmit);
612 uart_port_unlock(port, flags);
616 static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
618 struct uart_state *state = tty->driver_data;
619 struct uart_port *port;
623 port = uart_port_lock(state, flags);
624 ret = uart_circ_chars_pending(&state->xmit);
625 uart_port_unlock(port, flags);
629 static void uart_flush_buffer(struct tty_struct *tty)
631 struct uart_state *state = tty->driver_data;
632 struct uart_port *port;
636 * This means you called this function _after_ the port was
637 * closed. No cookie for you.
644 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
646 port = uart_port_lock(state, flags);
649 uart_circ_clear(&state->xmit);
650 if (port->ops->flush_buffer)
651 port->ops->flush_buffer(port);
652 uart_port_unlock(port, flags);
653 tty_port_tty_wakeup(&state->port);
657 * This function performs low-level write of high-priority XON/XOFF
658 * character and accounting for it.
660 * Requires uart_port to implement .serial_out().
662 void uart_xchar_out(struct uart_port *uport, int offset)
664 serial_port_out(uport, offset, uport->x_char);
668 EXPORT_SYMBOL_GPL(uart_xchar_out);
671 * This function is used to send a high-priority XON/XOFF character to
674 static void uart_send_xchar(struct tty_struct *tty, char ch)
676 struct uart_state *state = tty->driver_data;
677 struct uart_port *port;
680 port = uart_port_ref(state);
684 if (port->ops->send_xchar)
685 port->ops->send_xchar(port, ch);
687 spin_lock_irqsave(&port->lock, flags);
690 port->ops->start_tx(port);
691 spin_unlock_irqrestore(&port->lock, flags);
693 uart_port_deref(port);
696 static void uart_throttle(struct tty_struct *tty)
698 struct uart_state *state = tty->driver_data;
699 upstat_t mask = UPSTAT_SYNC_FIFO;
700 struct uart_port *port;
702 port = uart_port_ref(state);
707 mask |= UPSTAT_AUTOXOFF;
709 mask |= UPSTAT_AUTORTS;
711 if (port->status & mask) {
712 port->ops->throttle(port);
713 mask &= ~port->status;
716 if (mask & UPSTAT_AUTORTS)
717 uart_clear_mctrl(port, TIOCM_RTS);
719 if (mask & UPSTAT_AUTOXOFF)
720 uart_send_xchar(tty, STOP_CHAR(tty));
722 uart_port_deref(port);
725 static void uart_unthrottle(struct tty_struct *tty)
727 struct uart_state *state = tty->driver_data;
728 upstat_t mask = UPSTAT_SYNC_FIFO;
729 struct uart_port *port;
731 port = uart_port_ref(state);
736 mask |= UPSTAT_AUTOXOFF;
738 mask |= UPSTAT_AUTORTS;
740 if (port->status & mask) {
741 port->ops->unthrottle(port);
742 mask &= ~port->status;
745 if (mask & UPSTAT_AUTORTS)
746 uart_set_mctrl(port, TIOCM_RTS);
748 if (mask & UPSTAT_AUTOXOFF)
749 uart_send_xchar(tty, START_CHAR(tty));
751 uart_port_deref(port);
754 static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
756 struct uart_state *state = container_of(port, struct uart_state, port);
757 struct uart_port *uport;
761 * Ensure the state we copy is consistent and no hardware changes
764 mutex_lock(&port->mutex);
765 uport = uart_port_check(state);
769 retinfo->type = uport->type;
770 retinfo->line = uport->line;
771 retinfo->port = uport->iobase;
772 if (HIGH_BITS_OFFSET)
773 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
774 retinfo->irq = uport->irq;
775 retinfo->flags = (__force int)uport->flags;
776 retinfo->xmit_fifo_size = uport->fifosize;
777 retinfo->baud_base = uport->uartclk / 16;
778 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
779 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
780 ASYNC_CLOSING_WAIT_NONE :
781 jiffies_to_msecs(port->closing_wait) / 10;
782 retinfo->custom_divisor = uport->custom_divisor;
783 retinfo->hub6 = uport->hub6;
784 retinfo->io_type = uport->iotype;
785 retinfo->iomem_reg_shift = uport->regshift;
786 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
790 mutex_unlock(&port->mutex);
794 static int uart_get_info_user(struct tty_struct *tty,
795 struct serial_struct *ss)
797 struct uart_state *state = tty->driver_data;
798 struct tty_port *port = &state->port;
800 return uart_get_info(port, ss) < 0 ? -EIO : 0;
803 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
804 struct uart_state *state,
805 struct serial_struct *new_info)
807 struct uart_port *uport = uart_port_check(state);
808 unsigned long new_port;
809 unsigned int change_irq, change_port, closing_wait;
810 unsigned int old_custom_divisor, close_delay;
811 upf_t old_flags, new_flags;
817 new_port = new_info->port;
818 if (HIGH_BITS_OFFSET)
819 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
821 new_info->irq = irq_canonicalize(new_info->irq);
822 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
823 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
824 ASYNC_CLOSING_WAIT_NONE :
825 msecs_to_jiffies(new_info->closing_wait * 10);
828 change_irq = !(uport->flags & UPF_FIXED_PORT)
829 && new_info->irq != uport->irq;
832 * Since changing the 'type' of the port changes its resource
833 * allocations, we should treat type changes the same as
836 change_port = !(uport->flags & UPF_FIXED_PORT)
837 && (new_port != uport->iobase ||
838 (unsigned long)new_info->iomem_base != uport->mapbase ||
839 new_info->hub6 != uport->hub6 ||
840 new_info->io_type != uport->iotype ||
841 new_info->iomem_reg_shift != uport->regshift ||
842 new_info->type != uport->type);
844 old_flags = uport->flags;
845 new_flags = (__force upf_t)new_info->flags;
846 old_custom_divisor = uport->custom_divisor;
848 if (!capable(CAP_SYS_ADMIN)) {
850 if (change_irq || change_port ||
851 (new_info->baud_base != uport->uartclk / 16) ||
852 (close_delay != port->close_delay) ||
853 (closing_wait != port->closing_wait) ||
854 (new_info->xmit_fifo_size &&
855 new_info->xmit_fifo_size != uport->fifosize) ||
856 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
858 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
859 (new_flags & UPF_USR_MASK));
860 uport->custom_divisor = new_info->custom_divisor;
864 if (change_irq || change_port) {
865 retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
871 * Ask the low level driver to verify the settings.
873 if (uport->ops->verify_port)
874 retval = uport->ops->verify_port(uport, new_info);
876 if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
877 (new_info->baud_base < 9600))
883 if (change_port || change_irq) {
887 * Make sure that we are the sole user of this port.
889 if (tty_port_users(port) > 1)
893 * We need to shutdown the serial port at the old
894 * port/type/irq combination.
896 uart_shutdown(tty, state);
900 unsigned long old_iobase, old_mapbase;
901 unsigned int old_type, old_iotype, old_hub6, old_shift;
903 old_iobase = uport->iobase;
904 old_mapbase = uport->mapbase;
905 old_type = uport->type;
906 old_hub6 = uport->hub6;
907 old_iotype = uport->iotype;
908 old_shift = uport->regshift;
911 * Free and release old regions
913 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
914 uport->ops->release_port(uport);
916 uport->iobase = new_port;
917 uport->type = new_info->type;
918 uport->hub6 = new_info->hub6;
919 uport->iotype = new_info->io_type;
920 uport->regshift = new_info->iomem_reg_shift;
921 uport->mapbase = (unsigned long)new_info->iomem_base;
924 * Claim and map the new regions
926 if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
927 retval = uport->ops->request_port(uport);
929 /* Always success - Jean II */
934 * If we fail to request resources for the
935 * new port, try to restore the old settings.
938 uport->iobase = old_iobase;
939 uport->type = old_type;
940 uport->hub6 = old_hub6;
941 uport->iotype = old_iotype;
942 uport->regshift = old_shift;
943 uport->mapbase = old_mapbase;
945 if (old_type != PORT_UNKNOWN) {
946 retval = uport->ops->request_port(uport);
948 * If we failed to restore the old settings,
952 uport->type = PORT_UNKNOWN;
960 /* Added to return the correct error -Ram Gupta */
966 uport->irq = new_info->irq;
967 if (!(uport->flags & UPF_FIXED_PORT))
968 uport->uartclk = new_info->baud_base * 16;
969 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
970 (new_flags & UPF_CHANGE_MASK);
971 uport->custom_divisor = new_info->custom_divisor;
972 port->close_delay = close_delay;
973 port->closing_wait = closing_wait;
974 if (new_info->xmit_fifo_size)
975 uport->fifosize = new_info->xmit_fifo_size;
979 if (uport->type == PORT_UNKNOWN)
981 if (tty_port_initialized(port)) {
982 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
983 old_custom_divisor != uport->custom_divisor) {
985 * If they're setting up a custom divisor or speed,
986 * instead of clearing it, then bitch about it.
988 if (uport->flags & UPF_SPD_MASK) {
989 dev_notice_ratelimited(uport->dev,
990 "%s sets custom speed on %s. This is deprecated.\n",
992 tty_name(port->tty));
994 uart_change_line_settings(tty, state, NULL);
997 retval = uart_startup(tty, state, true);
999 tty_port_set_initialized(port, true);
1007 static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
1009 struct uart_state *state = tty->driver_data;
1010 struct tty_port *port = &state->port;
1013 down_write(&tty->termios_rwsem);
1015 * This semaphore protects port->count. It is also
1016 * very useful to prevent opens. Also, take the
1017 * port configuration semaphore to make sure that a
1018 * module insertion/removal doesn't change anything
1021 mutex_lock(&port->mutex);
1022 retval = uart_set_info(tty, port, state, ss);
1023 mutex_unlock(&port->mutex);
1024 up_write(&tty->termios_rwsem);
1029 * uart_get_lsr_info - get line status register info
1030 * @tty: tty associated with the UART
1031 * @state: UART being queried
1032 * @value: returned modem value
1034 static int uart_get_lsr_info(struct tty_struct *tty,
1035 struct uart_state *state, unsigned int __user *value)
1037 struct uart_port *uport = uart_port_check(state);
1038 unsigned int result;
1040 result = uport->ops->tx_empty(uport);
1043 * If we're about to load something into the transmit
1044 * register, we'll pretend the transmitter isn't empty to
1045 * avoid a race condition (depending on when the transmit
1046 * interrupt happens).
1048 if (uport->x_char ||
1049 ((uart_circ_chars_pending(&state->xmit) > 0) &&
1050 !uart_tx_stopped(uport)))
1051 result &= ~TIOCSER_TEMT;
1053 return put_user(result, value);
1056 static int uart_tiocmget(struct tty_struct *tty)
1058 struct uart_state *state = tty->driver_data;
1059 struct tty_port *port = &state->port;
1060 struct uart_port *uport;
1063 mutex_lock(&port->mutex);
1064 uport = uart_port_check(state);
1068 if (!tty_io_error(tty)) {
1069 result = uport->mctrl;
1070 spin_lock_irq(&uport->lock);
1071 result |= uport->ops->get_mctrl(uport);
1072 spin_unlock_irq(&uport->lock);
1075 mutex_unlock(&port->mutex);
1080 uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1082 struct uart_state *state = tty->driver_data;
1083 struct tty_port *port = &state->port;
1084 struct uart_port *uport;
1087 mutex_lock(&port->mutex);
1088 uport = uart_port_check(state);
1092 if (!tty_io_error(tty)) {
1093 uart_update_mctrl(uport, set, clear);
1097 mutex_unlock(&port->mutex);
1101 static int uart_break_ctl(struct tty_struct *tty, int break_state)
1103 struct uart_state *state = tty->driver_data;
1104 struct tty_port *port = &state->port;
1105 struct uart_port *uport;
1108 mutex_lock(&port->mutex);
1109 uport = uart_port_check(state);
1113 if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1114 uport->ops->break_ctl(uport, break_state);
1117 mutex_unlock(&port->mutex);
1121 static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
1123 struct tty_port *port = &state->port;
1124 struct uart_port *uport;
1127 if (!capable(CAP_SYS_ADMIN))
1131 * Take the per-port semaphore. This prevents count from
1132 * changing, and hence any extra opens of the port while
1133 * we're auto-configuring.
1135 if (mutex_lock_interruptible(&port->mutex))
1136 return -ERESTARTSYS;
1138 uport = uart_port_check(state);
1145 if (tty_port_users(port) == 1) {
1146 uart_shutdown(tty, state);
1149 * If we already have a port type configured,
1150 * we must release its resources.
1152 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1153 uport->ops->release_port(uport);
1155 flags = UART_CONFIG_TYPE;
1156 if (uport->flags & UPF_AUTO_IRQ)
1157 flags |= UART_CONFIG_IRQ;
1160 * This will claim the ports resources if
1163 uport->ops->config_port(uport, flags);
1165 ret = uart_startup(tty, state, true);
1167 tty_port_set_initialized(port, true);
1172 mutex_unlock(&port->mutex);
1176 static void uart_enable_ms(struct uart_port *uport)
1179 * Force modem status interrupts on
1181 if (uport->ops->enable_ms)
1182 uport->ops->enable_ms(uport);
1186 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1187 * - mask passed in arg for lines of interest
1188 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1189 * Caller should use TIOCGICOUNT to see which one it was
1191 * FIXME: This wants extracting into a common all driver implementation
1192 * of TIOCMWAIT using tty_port.
1194 static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1196 struct uart_port *uport;
1197 struct tty_port *port = &state->port;
1198 DECLARE_WAITQUEUE(wait, current);
1199 struct uart_icount cprev, cnow;
1203 * note the counters on entry
1205 uport = uart_port_ref(state);
1208 spin_lock_irq(&uport->lock);
1209 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1210 uart_enable_ms(uport);
1211 spin_unlock_irq(&uport->lock);
1213 add_wait_queue(&port->delta_msr_wait, &wait);
1215 spin_lock_irq(&uport->lock);
1216 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1217 spin_unlock_irq(&uport->lock);
1219 set_current_state(TASK_INTERRUPTIBLE);
1221 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1222 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1223 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1224 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1231 /* see if a signal did it */
1232 if (signal_pending(current)) {
1239 __set_current_state(TASK_RUNNING);
1240 remove_wait_queue(&port->delta_msr_wait, &wait);
1241 uart_port_deref(uport);
1247 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1248 * Return: write counters to the user passed counter struct
1249 * NB: both 1->0 and 0->1 transitions are counted except for
1250 * RI where only 0->1 is counted.
1252 static int uart_get_icount(struct tty_struct *tty,
1253 struct serial_icounter_struct *icount)
1255 struct uart_state *state = tty->driver_data;
1256 struct uart_icount cnow;
1257 struct uart_port *uport;
1259 uport = uart_port_ref(state);
1262 spin_lock_irq(&uport->lock);
1263 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1264 spin_unlock_irq(&uport->lock);
1265 uart_port_deref(uport);
1267 icount->cts = cnow.cts;
1268 icount->dsr = cnow.dsr;
1269 icount->rng = cnow.rng;
1270 icount->dcd = cnow.dcd;
1271 icount->rx = cnow.rx;
1272 icount->tx = cnow.tx;
1273 icount->frame = cnow.frame;
1274 icount->overrun = cnow.overrun;
1275 icount->parity = cnow.parity;
1276 icount->brk = cnow.brk;
1277 icount->buf_overrun = cnow.buf_overrun;
1282 #define SER_RS485_LEGACY_FLAGS (SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
1283 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
1284 SER_RS485_TERMINATE_BUS)
1286 static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
1288 u32 flags = rs485->flags;
1290 /* Don't return -EINVAL for unsupported legacy flags */
1291 flags &= ~SER_RS485_LEGACY_FLAGS;
1294 * For any bit outside of the legacy ones that is not supported by
1295 * the driver, return -EINVAL.
1297 if (flags & ~port->rs485_supported.flags)
1300 /* Asking for address w/o addressing mode? */
1301 if (!(rs485->flags & SER_RS485_ADDRB) &&
1302 (rs485->flags & (SER_RS485_ADDR_RECV|SER_RS485_ADDR_DEST)))
1305 /* Address given but not enabled? */
1306 if (!(rs485->flags & SER_RS485_ADDR_RECV) && rs485->addr_recv)
1308 if (!(rs485->flags & SER_RS485_ADDR_DEST) && rs485->addr_dest)
1314 static void uart_sanitize_serial_rs485_delays(struct uart_port *port,
1315 struct serial_rs485 *rs485)
1317 if (!port->rs485_supported.delay_rts_before_send) {
1318 if (rs485->delay_rts_before_send) {
1319 dev_warn_ratelimited(port->dev,
1320 "%s (%d): RTS delay before sending not supported\n",
1321 port->name, port->line);
1323 rs485->delay_rts_before_send = 0;
1324 } else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) {
1325 rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY;
1326 dev_warn_ratelimited(port->dev,
1327 "%s (%d): RTS delay before sending clamped to %u ms\n",
1328 port->name, port->line, rs485->delay_rts_before_send);
1331 if (!port->rs485_supported.delay_rts_after_send) {
1332 if (rs485->delay_rts_after_send) {
1333 dev_warn_ratelimited(port->dev,
1334 "%s (%d): RTS delay after sending not supported\n",
1335 port->name, port->line);
1337 rs485->delay_rts_after_send = 0;
1338 } else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
1339 rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;
1340 dev_warn_ratelimited(port->dev,
1341 "%s (%d): RTS delay after sending clamped to %u ms\n",
1342 port->name, port->line, rs485->delay_rts_after_send);
1346 static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485)
1348 u32 supported_flags = port->rs485_supported.flags;
1350 if (!(rs485->flags & SER_RS485_ENABLED)) {
1351 memset(rs485, 0, sizeof(*rs485));
1355 /* Pick sane settings if the user hasn't */
1356 if ((supported_flags & (SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND)) &&
1357 !(rs485->flags & SER_RS485_RTS_ON_SEND) ==
1358 !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
1359 dev_warn_ratelimited(port->dev,
1360 "%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
1361 port->name, port->line);
1362 rs485->flags |= SER_RS485_RTS_ON_SEND;
1363 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
1364 supported_flags |= SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND;
1367 rs485->flags &= supported_flags;
1369 uart_sanitize_serial_rs485_delays(port, rs485);
1371 /* Return clean padding area to userspace */
1372 memset(rs485->padding0, 0, sizeof(rs485->padding0));
1373 memset(rs485->padding1, 0, sizeof(rs485->padding1));
1376 static void uart_set_rs485_termination(struct uart_port *port,
1377 const struct serial_rs485 *rs485)
1379 if (!(rs485->flags & SER_RS485_ENABLED))
1382 gpiod_set_value_cansleep(port->rs485_term_gpio,
1383 !!(rs485->flags & SER_RS485_TERMINATE_BUS));
1386 static int uart_rs485_config(struct uart_port *port)
1388 struct serial_rs485 *rs485 = &port->rs485;
1391 uart_sanitize_serial_rs485(port, rs485);
1392 uart_set_rs485_termination(port, rs485);
1394 ret = port->rs485_config(port, NULL, rs485);
1396 memset(rs485, 0, sizeof(*rs485));
1401 static int uart_get_rs485_config(struct uart_port *port,
1402 struct serial_rs485 __user *rs485)
1404 unsigned long flags;
1405 struct serial_rs485 aux;
1407 spin_lock_irqsave(&port->lock, flags);
1409 spin_unlock_irqrestore(&port->lock, flags);
1411 if (copy_to_user(rs485, &aux, sizeof(aux)))
1417 static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
1418 struct serial_rs485 __user *rs485_user)
1420 struct serial_rs485 rs485;
1422 unsigned long flags;
1424 if (!port->rs485_config)
1427 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1430 ret = uart_check_rs485_flags(port, &rs485);
1433 uart_sanitize_serial_rs485(port, &rs485);
1434 uart_set_rs485_termination(port, &rs485);
1436 spin_lock_irqsave(&port->lock, flags);
1437 ret = port->rs485_config(port, &tty->termios, &rs485);
1439 port->rs485 = rs485;
1441 /* Reset RTS and other mctrl lines when disabling RS485 */
1442 if (!(rs485.flags & SER_RS485_ENABLED))
1443 port->ops->set_mctrl(port, port->mctrl);
1445 spin_unlock_irqrestore(&port->lock, flags);
1449 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1455 static int uart_get_iso7816_config(struct uart_port *port,
1456 struct serial_iso7816 __user *iso7816)
1458 unsigned long flags;
1459 struct serial_iso7816 aux;
1461 if (!port->iso7816_config)
1464 spin_lock_irqsave(&port->lock, flags);
1465 aux = port->iso7816;
1466 spin_unlock_irqrestore(&port->lock, flags);
1468 if (copy_to_user(iso7816, &aux, sizeof(aux)))
1474 static int uart_set_iso7816_config(struct uart_port *port,
1475 struct serial_iso7816 __user *iso7816_user)
1477 struct serial_iso7816 iso7816;
1479 unsigned long flags;
1481 if (!port->iso7816_config)
1484 if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1488 * There are 5 words reserved for future use. Check that userspace
1489 * doesn't put stuff in there to prevent breakages in the future.
1491 for (i = 0; i < ARRAY_SIZE(iso7816.reserved); i++)
1492 if (iso7816.reserved[i])
1495 spin_lock_irqsave(&port->lock, flags);
1496 ret = port->iso7816_config(port, &iso7816);
1497 spin_unlock_irqrestore(&port->lock, flags);
1501 if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1508 * Called via sys_ioctl. We can use spin_lock_irq() here.
1511 uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1513 struct uart_state *state = tty->driver_data;
1514 struct tty_port *port = &state->port;
1515 struct uart_port *uport;
1516 void __user *uarg = (void __user *)arg;
1517 int ret = -ENOIOCTLCMD;
1521 * These ioctls don't rely on the hardware to be present.
1525 down_write(&tty->termios_rwsem);
1526 ret = uart_do_autoconfig(tty, state);
1527 up_write(&tty->termios_rwsem);
1531 if (ret != -ENOIOCTLCMD)
1534 if (tty_io_error(tty)) {
1540 * The following should only be used when hardware is present.
1544 ret = uart_wait_modem_status(state, arg);
1548 if (ret != -ENOIOCTLCMD)
1551 /* rs485_config requires more locking than others */
1552 if (cmd == TIOCSRS485)
1553 down_write(&tty->termios_rwsem);
1555 mutex_lock(&port->mutex);
1556 uport = uart_port_check(state);
1558 if (!uport || tty_io_error(tty)) {
1564 * All these rely on hardware being present and need to be
1565 * protected against the tty being hung up.
1569 case TIOCSERGETLSR: /* Get line status register */
1570 ret = uart_get_lsr_info(tty, state, uarg);
1574 ret = uart_get_rs485_config(uport, uarg);
1578 ret = uart_set_rs485_config(tty, uport, uarg);
1582 ret = uart_set_iso7816_config(state->uart_port, uarg);
1586 ret = uart_get_iso7816_config(state->uart_port, uarg);
1589 if (uport->ops->ioctl)
1590 ret = uport->ops->ioctl(uport, cmd, arg);
1594 mutex_unlock(&port->mutex);
1595 if (cmd == TIOCSRS485)
1596 up_write(&tty->termios_rwsem);
1601 static void uart_set_ldisc(struct tty_struct *tty)
1603 struct uart_state *state = tty->driver_data;
1604 struct uart_port *uport;
1605 struct tty_port *port = &state->port;
1607 if (!tty_port_initialized(port))
1610 mutex_lock(&state->port.mutex);
1611 uport = uart_port_check(state);
1612 if (uport && uport->ops->set_ldisc)
1613 uport->ops->set_ldisc(uport, &tty->termios);
1614 mutex_unlock(&state->port.mutex);
1617 static void uart_set_termios(struct tty_struct *tty,
1618 const struct ktermios *old_termios)
1620 struct uart_state *state = tty->driver_data;
1621 struct uart_port *uport;
1622 unsigned int cflag = tty->termios.c_cflag;
1623 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1624 bool sw_changed = false;
1626 mutex_lock(&state->port.mutex);
1627 uport = uart_port_check(state);
1632 * Drivers doing software flow control also need to know
1633 * about changes to these input settings.
1635 if (uport->flags & UPF_SOFT_FLOW) {
1636 iflag_mask |= IXANY|IXON|IXOFF;
1638 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1639 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1643 * These are the bits that are used to setup various
1644 * flags in the low level driver. We can ignore the Bfoo
1645 * bits in c_cflag; c_[io]speed will always be set
1646 * appropriately by set_termios() in tty_ioctl.c
1648 if ((cflag ^ old_termios->c_cflag) == 0 &&
1649 tty->termios.c_ospeed == old_termios->c_ospeed &&
1650 tty->termios.c_ispeed == old_termios->c_ispeed &&
1651 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1656 uart_change_line_settings(tty, state, old_termios);
1657 /* reload cflag from termios; port driver may have overridden flags */
1658 cflag = tty->termios.c_cflag;
1660 /* Handle transition to B0 status */
1661 if (((old_termios->c_cflag & CBAUD) != B0) && ((cflag & CBAUD) == B0))
1662 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1663 /* Handle transition away from B0 status */
1664 else if (((old_termios->c_cflag & CBAUD) == B0) && ((cflag & CBAUD) != B0)) {
1665 unsigned int mask = TIOCM_DTR;
1667 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1669 uart_set_mctrl(uport, mask);
1672 mutex_unlock(&state->port.mutex);
1676 * Calls to uart_close() are serialised via the tty_lock in
1677 * drivers/tty/tty_io.c:tty_release()
1678 * drivers/tty/tty_io.c:do_tty_hangup()
1680 static void uart_close(struct tty_struct *tty, struct file *filp)
1682 struct uart_state *state = tty->driver_data;
1685 struct uart_driver *drv = tty->driver->driver_state;
1686 struct tty_port *port;
1688 state = drv->state + tty->index;
1689 port = &state->port;
1690 spin_lock_irq(&port->lock);
1692 spin_unlock_irq(&port->lock);
1696 pr_debug("uart_close(%d) called\n", tty->index);
1698 tty_port_close(tty->port, tty, filp);
1701 static void uart_tty_port_shutdown(struct tty_port *port)
1703 struct uart_state *state = container_of(port, struct uart_state, port);
1704 struct uart_port *uport = uart_port_check(state);
1708 * At this point, we stop accepting input. To do this, we
1709 * disable the receive line status interrupts.
1711 if (WARN(!uport, "detached port still initialized!\n"))
1714 spin_lock_irq(&uport->lock);
1715 uport->ops->stop_rx(uport);
1716 spin_unlock_irq(&uport->lock);
1718 uart_port_shutdown(port);
1721 * It's possible for shutdown to be called after suspend if we get
1722 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1723 * we don't try to resume a port that has been shutdown.
1725 tty_port_set_suspended(port, false);
1728 * Free the transmit buffer.
1730 spin_lock_irq(&uport->lock);
1731 buf = state->xmit.buf;
1732 state->xmit.buf = NULL;
1733 spin_unlock_irq(&uport->lock);
1735 free_page((unsigned long)buf);
1737 uart_change_pm(state, UART_PM_STATE_OFF);
1740 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1742 struct uart_state *state = tty->driver_data;
1743 struct uart_port *port;
1744 unsigned long char_time, expire, fifo_timeout;
1746 port = uart_port_ref(state);
1750 if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
1751 uart_port_deref(port);
1756 * Set the check interval to be 1/5 of the estimated time to
1757 * send a single character, and make it at least 1. The check
1758 * interval should also be less than the timeout.
1760 * Note: we have to use pretty tight timings here to satisfy
1763 char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL);
1765 if (timeout && timeout < char_time)
1766 char_time = timeout;
1768 if (!uart_cts_enabled(port)) {
1770 * If the transmitter hasn't cleared in twice the approximate
1771 * amount of time to send the entire FIFO, it probably won't
1772 * ever clear. This assumes the UART isn't doing flow
1773 * control, which is currently the case. Hence, if it ever
1774 * takes longer than FIFO timeout, this is probably due to a
1775 * UART bug of some kind. So, we clamp the timeout parameter at
1778 fifo_timeout = uart_fifo_timeout(port);
1779 if (timeout == 0 || timeout > 2 * fifo_timeout)
1780 timeout = 2 * fifo_timeout;
1783 expire = jiffies + timeout;
1785 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1786 port->line, jiffies, expire);
1789 * Check whether the transmitter is empty every 'char_time'.
1790 * 'timeout' / 'expire' give us the maximum amount of time
1793 while (!port->ops->tx_empty(port)) {
1794 msleep_interruptible(jiffies_to_msecs(char_time));
1795 if (signal_pending(current))
1797 if (timeout && time_after(jiffies, expire))
1800 uart_port_deref(port);
1804 * Calls to uart_hangup() are serialised by the tty_lock in
1805 * drivers/tty/tty_io.c:do_tty_hangup()
1806 * This runs from a workqueue and can sleep for a _short_ time only.
1808 static void uart_hangup(struct tty_struct *tty)
1810 struct uart_state *state = tty->driver_data;
1811 struct tty_port *port = &state->port;
1812 struct uart_port *uport;
1813 unsigned long flags;
1815 pr_debug("uart_hangup(%d)\n", tty->index);
1817 mutex_lock(&port->mutex);
1818 uport = uart_port_check(state);
1819 WARN(!uport, "hangup of detached port!\n");
1821 if (tty_port_active(port)) {
1822 uart_flush_buffer(tty);
1823 uart_shutdown(tty, state);
1824 spin_lock_irqsave(&port->lock, flags);
1826 spin_unlock_irqrestore(&port->lock, flags);
1827 tty_port_set_active(port, false);
1828 tty_port_tty_set(port, NULL);
1829 if (uport && !uart_console(uport))
1830 uart_change_pm(state, UART_PM_STATE_OFF);
1831 wake_up_interruptible(&port->open_wait);
1832 wake_up_interruptible(&port->delta_msr_wait);
1834 mutex_unlock(&port->mutex);
1837 /* uport == NULL if uart_port has already been removed */
1838 static void uart_port_shutdown(struct tty_port *port)
1840 struct uart_state *state = container_of(port, struct uart_state, port);
1841 struct uart_port *uport = uart_port_check(state);
1844 * clear delta_msr_wait queue to avoid mem leaks: we may free
1845 * the irq here so the queue might never be woken up. Note
1846 * that we won't end up waiting on delta_msr_wait again since
1847 * any outstanding file descriptors should be pointing at
1848 * hung_up_tty_fops now.
1850 wake_up_interruptible(&port->delta_msr_wait);
1853 /* Free the IRQ and disable the port. */
1854 uport->ops->shutdown(uport);
1856 /* Ensure that the IRQ handler isn't running on another CPU. */
1857 synchronize_irq(uport->irq);
1861 static bool uart_carrier_raised(struct tty_port *port)
1863 struct uart_state *state = container_of(port, struct uart_state, port);
1864 struct uart_port *uport;
1867 uport = uart_port_ref(state);
1869 * Should never observe uport == NULL since checks for hangup should
1870 * abort the tty_port_block_til_ready() loop before checking for carrier
1871 * raised -- but report carrier raised if it does anyway so open will
1872 * continue and not sleep
1874 if (WARN_ON(!uport))
1876 spin_lock_irq(&uport->lock);
1877 uart_enable_ms(uport);
1878 mctrl = uport->ops->get_mctrl(uport);
1879 spin_unlock_irq(&uport->lock);
1880 uart_port_deref(uport);
1882 return mctrl & TIOCM_CAR;
1885 static void uart_dtr_rts(struct tty_port *port, bool active)
1887 struct uart_state *state = container_of(port, struct uart_state, port);
1888 struct uart_port *uport;
1890 uport = uart_port_ref(state);
1893 uart_port_dtr_rts(uport, active);
1894 uart_port_deref(uport);
1897 static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
1899 struct uart_driver *drv = driver->driver_state;
1900 struct uart_state *state = drv->state + tty->index;
1902 tty->driver_data = state;
1904 return tty_standard_install(driver, tty);
1908 * Calls to uart_open are serialised by the tty_lock in
1909 * drivers/tty/tty_io.c:tty_open()
1910 * Note that if this fails, then uart_close() _will_ be called.
1912 * In time, we want to scrap the "opening nonpresent ports"
1913 * behaviour and implement an alternative way for setserial
1914 * to set base addresses/ports/types. This will allow us to
1915 * get rid of a certain amount of extra tests.
1917 static int uart_open(struct tty_struct *tty, struct file *filp)
1919 struct uart_state *state = tty->driver_data;
1922 retval = tty_port_open(&state->port, tty, filp);
1929 static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1931 struct uart_state *state = container_of(port, struct uart_state, port);
1932 struct uart_port *uport;
1935 uport = uart_port_check(state);
1936 if (!uport || uport->flags & UPF_DEAD)
1940 * Start up the serial port.
1942 ret = uart_startup(tty, state, false);
1944 tty_port_set_active(port, true);
1949 static const char *uart_type(struct uart_port *port)
1951 const char *str = NULL;
1953 if (port->ops->type)
1954 str = port->ops->type(port);
1962 #ifdef CONFIG_PROC_FS
1964 static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1966 struct uart_state *state = drv->state + i;
1967 struct tty_port *port = &state->port;
1968 enum uart_pm_state pm_state;
1969 struct uart_port *uport;
1971 unsigned int status;
1974 mutex_lock(&port->mutex);
1975 uport = uart_port_check(state);
1979 mmio = uport->iotype >= UPIO_MEM;
1980 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1981 uport->line, uart_type(uport),
1982 mmio ? "mmio:0x" : "port:",
1983 mmio ? (unsigned long long)uport->mapbase
1984 : (unsigned long long)uport->iobase,
1987 if (uport->type == PORT_UNKNOWN) {
1992 if (capable(CAP_SYS_ADMIN)) {
1993 pm_state = state->pm_state;
1994 if (pm_state != UART_PM_STATE_ON)
1995 uart_change_pm(state, UART_PM_STATE_ON);
1996 spin_lock_irq(&uport->lock);
1997 status = uport->ops->get_mctrl(uport);
1998 spin_unlock_irq(&uport->lock);
1999 if (pm_state != UART_PM_STATE_ON)
2000 uart_change_pm(state, pm_state);
2002 seq_printf(m, " tx:%d rx:%d",
2003 uport->icount.tx, uport->icount.rx);
2004 if (uport->icount.frame)
2005 seq_printf(m, " fe:%d", uport->icount.frame);
2006 if (uport->icount.parity)
2007 seq_printf(m, " pe:%d", uport->icount.parity);
2008 if (uport->icount.brk)
2009 seq_printf(m, " brk:%d", uport->icount.brk);
2010 if (uport->icount.overrun)
2011 seq_printf(m, " oe:%d", uport->icount.overrun);
2012 if (uport->icount.buf_overrun)
2013 seq_printf(m, " bo:%d", uport->icount.buf_overrun);
2015 #define INFOBIT(bit, str) \
2016 if (uport->mctrl & (bit)) \
2017 strncat(stat_buf, (str), sizeof(stat_buf) - \
2018 strlen(stat_buf) - 2)
2019 #define STATBIT(bit, str) \
2020 if (status & (bit)) \
2021 strncat(stat_buf, (str), sizeof(stat_buf) - \
2022 strlen(stat_buf) - 2)
2026 INFOBIT(TIOCM_RTS, "|RTS");
2027 STATBIT(TIOCM_CTS, "|CTS");
2028 INFOBIT(TIOCM_DTR, "|DTR");
2029 STATBIT(TIOCM_DSR, "|DSR");
2030 STATBIT(TIOCM_CAR, "|CD");
2031 STATBIT(TIOCM_RNG, "|RI");
2035 seq_puts(m, stat_buf);
2041 mutex_unlock(&port->mutex);
2044 static int uart_proc_show(struct seq_file *m, void *v)
2046 struct tty_driver *ttydrv = m->private;
2047 struct uart_driver *drv = ttydrv->driver_state;
2050 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
2051 for (i = 0; i < drv->nr; i++)
2052 uart_line_info(m, drv, i);
2057 static void uart_port_spin_lock_init(struct uart_port *port)
2059 spin_lock_init(&port->lock);
2060 lockdep_set_class(&port->lock, &port_lock_key);
2063 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
2065 * uart_console_write - write a console message to a serial port
2066 * @port: the port to write the message
2067 * @s: array of characters
2068 * @count: number of characters in string to write
2069 * @putchar: function to write character to port
2071 void uart_console_write(struct uart_port *port, const char *s,
2073 void (*putchar)(struct uart_port *, unsigned char))
2077 for (i = 0; i < count; i++, s++) {
2079 putchar(port, '\r');
2083 EXPORT_SYMBOL_GPL(uart_console_write);
2086 * uart_get_console - get uart port for console
2087 * @ports: ports to search in
2088 * @nr: number of @ports
2089 * @co: console to search for
2090 * Returns: uart_port for the console @co
2092 * Check whether an invalid uart number has been specified (as @co->index), and
2093 * if so, search for the first available port that does have console support.
2095 struct uart_port * __init
2096 uart_get_console(struct uart_port *ports, int nr, struct console *co)
2098 int idx = co->index;
2100 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
2101 ports[idx].membase == NULL))
2102 for (idx = 0; idx < nr; idx++)
2103 if (ports[idx].iobase != 0 ||
2104 ports[idx].membase != NULL)
2113 * uart_parse_earlycon - Parse earlycon options
2114 * @p: ptr to 2nd field (ie., just beyond '<name>,')
2115 * @iotype: ptr for decoded iotype (out)
2116 * @addr: ptr for decoded mapbase/iobase (out)
2117 * @options: ptr for <options> field; %NULL if not present (out)
2119 * Decodes earlycon kernel command line parameters of the form:
2120 * * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2121 * * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2123 * The optional form:
2124 * * earlycon=<name>,0x<addr>,<options>
2125 * * console=<name>,0x<addr>,<options>
2127 * is also accepted; the returned @iotype will be %UPIO_MEM.
2129 * Returns: 0 on success or -%EINVAL on failure
2131 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
2134 if (strncmp(p, "mmio,", 5) == 0) {
2137 } else if (strncmp(p, "mmio16,", 7) == 0) {
2138 *iotype = UPIO_MEM16;
2140 } else if (strncmp(p, "mmio32,", 7) == 0) {
2141 *iotype = UPIO_MEM32;
2143 } else if (strncmp(p, "mmio32be,", 9) == 0) {
2144 *iotype = UPIO_MEM32BE;
2146 } else if (strncmp(p, "mmio32native,", 13) == 0) {
2147 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
2148 UPIO_MEM32BE : UPIO_MEM32;
2150 } else if (strncmp(p, "io,", 3) == 0) {
2151 *iotype = UPIO_PORT;
2153 } else if (strncmp(p, "0x", 2) == 0) {
2160 * Before you replace it with kstrtoull(), think about options separator
2161 * (',') it will not tolerate
2163 *addr = simple_strtoull(p, NULL, 0);
2171 EXPORT_SYMBOL_GPL(uart_parse_earlycon);
2174 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
2175 * @options: pointer to option string
2176 * @baud: pointer to an 'int' variable for the baud rate.
2177 * @parity: pointer to an 'int' variable for the parity.
2178 * @bits: pointer to an 'int' variable for the number of data bits.
2179 * @flow: pointer to an 'int' variable for the flow control character.
2181 * uart_parse_options() decodes a string containing the serial console
2182 * options. The format of the string is <baud><parity><bits><flow>,
2186 uart_parse_options(const char *options, int *baud, int *parity,
2187 int *bits, int *flow)
2189 const char *s = options;
2191 *baud = simple_strtoul(s, NULL, 10);
2192 while (*s >= '0' && *s <= '9')
2201 EXPORT_SYMBOL_GPL(uart_parse_options);
2204 * uart_set_options - setup the serial console parameters
2205 * @port: pointer to the serial ports uart_port structure
2206 * @co: console pointer
2208 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2209 * @bits: number of data bits
2210 * @flow: flow control character - 'r' (rts)
2212 * Locking: Caller must hold console_list_lock in order to serialize
2213 * early initialization of the serial-console lock.
2216 uart_set_options(struct uart_port *port, struct console *co,
2217 int baud, int parity, int bits, int flow)
2219 struct ktermios termios;
2220 static struct ktermios dummy;
2223 * Ensure that the serial-console lock is initialised early.
2225 * Note that the console-registered check is needed because
2226 * kgdboc can call uart_set_options() for an already registered
2227 * console via tty_find_polling_driver() and uart_poll_init().
2229 if (!uart_console_registered_locked(port) && !port->console_reinit)
2230 uart_port_spin_lock_init(port);
2232 memset(&termios, 0, sizeof(struct ktermios));
2234 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2235 tty_termios_encode_baud_rate(&termios, baud, baud);
2238 termios.c_cflag |= CS7;
2240 termios.c_cflag |= CS8;
2244 termios.c_cflag |= PARODD;
2247 termios.c_cflag |= PARENB;
2252 termios.c_cflag |= CRTSCTS;
2255 * some uarts on other side don't support no flow control.
2256 * So we set * DTR in host uart to make them happy
2258 port->mctrl |= TIOCM_DTR;
2260 port->ops->set_termios(port, &termios, &dummy);
2262 * Allow the setting of the UART parameters with a NULL console
2266 co->cflag = termios.c_cflag;
2267 co->ispeed = termios.c_ispeed;
2268 co->ospeed = termios.c_ospeed;
2273 EXPORT_SYMBOL_GPL(uart_set_options);
2274 #endif /* CONFIG_SERIAL_CORE_CONSOLE */
2277 * uart_change_pm - set power state of the port
2279 * @state: port descriptor
2280 * @pm_state: new state
2282 * Locking: port->mutex has to be held
2284 static void uart_change_pm(struct uart_state *state,
2285 enum uart_pm_state pm_state)
2287 struct uart_port *port = uart_port_check(state);
2289 if (state->pm_state != pm_state) {
2290 if (port && port->ops->pm)
2291 port->ops->pm(port, pm_state, state->pm_state);
2292 state->pm_state = pm_state;
2297 struct uart_port *port;
2298 struct uart_driver *driver;
2301 static int serial_match_port(struct device *dev, void *data)
2303 struct uart_match *match = data;
2304 struct tty_driver *tty_drv = match->driver->tty_driver;
2305 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2308 return dev->devt == devt; /* Actually, only one tty per port */
2311 int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2313 struct uart_state *state = drv->state + uport->line;
2314 struct tty_port *port = &state->port;
2315 struct device *tty_dev;
2316 struct uart_match match = {uport, drv};
2318 mutex_lock(&port->mutex);
2320 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2321 if (tty_dev && device_may_wakeup(tty_dev)) {
2322 enable_irq_wake(uport->irq);
2323 put_device(tty_dev);
2324 mutex_unlock(&port->mutex);
2327 put_device(tty_dev);
2330 * Nothing to do if the console is not suspending
2331 * except stop_rx to prevent any asynchronous data
2332 * over RX line. However ensure that we will be
2333 * able to Re-start_rx later.
2335 if (!console_suspend_enabled && uart_console(uport)) {
2336 if (uport->ops->start_rx)
2337 uport->ops->stop_rx(uport);
2341 uport->suspended = 1;
2343 if (tty_port_initialized(port)) {
2344 const struct uart_ops *ops = uport->ops;
2348 tty_port_set_suspended(port, true);
2349 tty_port_set_initialized(port, false);
2351 spin_lock_irq(&uport->lock);
2352 ops->stop_tx(uport);
2353 if (!(uport->rs485.flags & SER_RS485_ENABLED))
2354 ops->set_mctrl(uport, 0);
2355 /* save mctrl so it can be restored on resume */
2356 mctrl = uport->mctrl;
2358 ops->stop_rx(uport);
2359 spin_unlock_irq(&uport->lock);
2362 * Wait for the transmitter to empty.
2364 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2367 dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2370 ops->shutdown(uport);
2371 uport->mctrl = mctrl;
2375 * Disable the console device before suspending.
2377 if (uart_console(uport))
2378 console_stop(uport->cons);
2380 uart_change_pm(state, UART_PM_STATE_OFF);
2382 mutex_unlock(&port->mutex);
2386 EXPORT_SYMBOL(uart_suspend_port);
2388 int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2390 struct uart_state *state = drv->state + uport->line;
2391 struct tty_port *port = &state->port;
2392 struct device *tty_dev;
2393 struct uart_match match = {uport, drv};
2394 struct ktermios termios;
2396 mutex_lock(&port->mutex);
2398 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
2399 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2400 if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2401 disable_irq_wake(uport->irq);
2402 put_device(tty_dev);
2403 mutex_unlock(&port->mutex);
2406 put_device(tty_dev);
2407 uport->suspended = 0;
2410 * Re-enable the console device after suspending.
2412 if (uart_console(uport)) {
2414 * First try to use the console cflag setting.
2416 memset(&termios, 0, sizeof(struct ktermios));
2417 termios.c_cflag = uport->cons->cflag;
2418 termios.c_ispeed = uport->cons->ispeed;
2419 termios.c_ospeed = uport->cons->ospeed;
2422 * If that's unset, use the tty termios setting.
2424 if (port->tty && termios.c_cflag == 0)
2425 termios = port->tty->termios;
2427 if (console_suspend_enabled)
2428 uart_change_pm(state, UART_PM_STATE_ON);
2429 uport->ops->set_termios(uport, &termios, NULL);
2430 if (!console_suspend_enabled && uport->ops->start_rx)
2431 uport->ops->start_rx(uport);
2432 if (console_suspend_enabled)
2433 console_start(uport->cons);
2436 if (tty_port_suspended(port)) {
2437 const struct uart_ops *ops = uport->ops;
2440 uart_change_pm(state, UART_PM_STATE_ON);
2441 spin_lock_irq(&uport->lock);
2442 if (!(uport->rs485.flags & SER_RS485_ENABLED))
2443 ops->set_mctrl(uport, 0);
2444 spin_unlock_irq(&uport->lock);
2445 if (console_suspend_enabled || !uart_console(uport)) {
2446 /* Protected by port mutex for now */
2447 struct tty_struct *tty = port->tty;
2449 ret = ops->startup(uport);
2452 uart_change_line_settings(tty, state, NULL);
2453 spin_lock_irq(&uport->lock);
2454 if (!(uport->rs485.flags & SER_RS485_ENABLED))
2455 ops->set_mctrl(uport, uport->mctrl);
2457 uart_rs485_config(uport);
2458 ops->start_tx(uport);
2459 spin_unlock_irq(&uport->lock);
2460 tty_port_set_initialized(port, true);
2463 * Failed to resume - maybe hardware went away?
2464 * Clear the "initialized" flag so we won't try
2465 * to call the low level drivers shutdown method.
2467 uart_shutdown(tty, state);
2471 tty_port_set_suspended(port, false);
2474 mutex_unlock(&port->mutex);
2478 EXPORT_SYMBOL(uart_resume_port);
2481 uart_report_port(struct uart_driver *drv, struct uart_port *port)
2485 switch (port->iotype) {
2487 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2490 snprintf(address, sizeof(address),
2491 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2499 snprintf(address, sizeof(address),
2500 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2503 strscpy(address, "*unknown*", sizeof(address));
2507 pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
2508 port->dev ? dev_name(port->dev) : "",
2509 port->dev ? ": " : "",
2511 address, port->irq, port->uartclk / 16, uart_type(port));
2513 /* The magic multiplier feature is a bit obscure, so report it too. */
2514 if (port->flags & UPF_MAGIC_MULTIPLIER)
2515 pr_info("%s%s%s extra baud rates supported: %d, %d",
2516 port->dev ? dev_name(port->dev) : "",
2517 port->dev ? ": " : "",
2519 port->uartclk / 8, port->uartclk / 4);
2523 uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2524 struct uart_port *port)
2529 * If there isn't a port here, don't do anything further.
2531 if (!port->iobase && !port->mapbase && !port->membase)
2535 * Now do the auto configuration stuff. Note that config_port
2536 * is expected to claim the resources and map the port for us.
2539 if (port->flags & UPF_AUTO_IRQ)
2540 flags |= UART_CONFIG_IRQ;
2541 if (port->flags & UPF_BOOT_AUTOCONF) {
2542 if (!(port->flags & UPF_FIXED_TYPE)) {
2543 port->type = PORT_UNKNOWN;
2544 flags |= UART_CONFIG_TYPE;
2546 port->ops->config_port(port, flags);
2549 if (port->type != PORT_UNKNOWN) {
2550 unsigned long flags;
2552 uart_report_port(drv, port);
2554 /* Power up port for set_mctrl() */
2555 uart_change_pm(state, UART_PM_STATE_ON);
2558 * Ensure that the modem control lines are de-activated.
2559 * keep the DTR setting that is set in uart_set_options()
2560 * We probably don't need a spinlock around this, but
2562 spin_lock_irqsave(&port->lock, flags);
2563 port->mctrl &= TIOCM_DTR;
2564 if (!(port->rs485.flags & SER_RS485_ENABLED))
2565 port->ops->set_mctrl(port, port->mctrl);
2567 uart_rs485_config(port);
2568 spin_unlock_irqrestore(&port->lock, flags);
2571 * If this driver supports console, and it hasn't been
2572 * successfully registered yet, try to re-register it.
2573 * It may be that the port was not available.
2575 if (port->cons && !console_is_registered(port->cons))
2576 register_console(port->cons);
2579 * Power down all ports by default, except the
2580 * console if we have one.
2582 if (!uart_console(port))
2583 uart_change_pm(state, UART_PM_STATE_OFF);
2587 #ifdef CONFIG_CONSOLE_POLL
2589 static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2591 struct uart_driver *drv = driver->driver_state;
2592 struct uart_state *state = drv->state + line;
2593 enum uart_pm_state pm_state;
2594 struct tty_port *tport;
2595 struct uart_port *port;
2602 tport = &state->port;
2603 mutex_lock(&tport->mutex);
2605 port = uart_port_check(state);
2606 if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
2611 pm_state = state->pm_state;
2612 uart_change_pm(state, UART_PM_STATE_ON);
2614 if (port->ops->poll_init) {
2616 * We don't set initialized as we only initialized the hw,
2617 * e.g. state->xmit is still uninitialized.
2619 if (!tty_port_initialized(tport))
2620 ret = port->ops->poll_init(port);
2623 if (!ret && options) {
2624 uart_parse_options(options, &baud, &parity, &bits, &flow);
2625 console_list_lock();
2626 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2627 console_list_unlock();
2631 uart_change_pm(state, pm_state);
2632 mutex_unlock(&tport->mutex);
2636 static int uart_poll_get_char(struct tty_driver *driver, int line)
2638 struct uart_driver *drv = driver->driver_state;
2639 struct uart_state *state = drv->state + line;
2640 struct uart_port *port;
2643 port = uart_port_ref(state);
2645 ret = port->ops->poll_get_char(port);
2646 uart_port_deref(port);
2652 static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2654 struct uart_driver *drv = driver->driver_state;
2655 struct uart_state *state = drv->state + line;
2656 struct uart_port *port;
2658 port = uart_port_ref(state);
2663 port->ops->poll_put_char(port, '\r');
2664 port->ops->poll_put_char(port, ch);
2665 uart_port_deref(port);
2669 static const struct tty_operations uart_ops = {
2670 .install = uart_install,
2672 .close = uart_close,
2673 .write = uart_write,
2674 .put_char = uart_put_char,
2675 .flush_chars = uart_flush_chars,
2676 .write_room = uart_write_room,
2677 .chars_in_buffer= uart_chars_in_buffer,
2678 .flush_buffer = uart_flush_buffer,
2679 .ioctl = uart_ioctl,
2680 .throttle = uart_throttle,
2681 .unthrottle = uart_unthrottle,
2682 .send_xchar = uart_send_xchar,
2683 .set_termios = uart_set_termios,
2684 .set_ldisc = uart_set_ldisc,
2686 .start = uart_start,
2687 .hangup = uart_hangup,
2688 .break_ctl = uart_break_ctl,
2689 .wait_until_sent= uart_wait_until_sent,
2690 #ifdef CONFIG_PROC_FS
2691 .proc_show = uart_proc_show,
2693 .tiocmget = uart_tiocmget,
2694 .tiocmset = uart_tiocmset,
2695 .set_serial = uart_set_info_user,
2696 .get_serial = uart_get_info_user,
2697 .get_icount = uart_get_icount,
2698 #ifdef CONFIG_CONSOLE_POLL
2699 .poll_init = uart_poll_init,
2700 .poll_get_char = uart_poll_get_char,
2701 .poll_put_char = uart_poll_put_char,
2705 static const struct tty_port_operations uart_port_ops = {
2706 .carrier_raised = uart_carrier_raised,
2707 .dtr_rts = uart_dtr_rts,
2708 .activate = uart_port_activate,
2709 .shutdown = uart_tty_port_shutdown,
2713 * uart_register_driver - register a driver with the uart core layer
2714 * @drv: low level driver structure
2716 * Register a uart driver with the core driver. We in turn register with the
2717 * tty layer, and initialise the core driver per-port state.
2719 * We have a proc file in /proc/tty/driver which is named after the normal
2722 * @drv->port should be %NULL, and the per-port structures should be registered
2723 * using uart_add_one_port() after this call has succeeded.
2725 * Locking: none, Interrupts: enabled
2727 int uart_register_driver(struct uart_driver *drv)
2729 struct tty_driver *normal;
2730 int i, retval = -ENOMEM;
2735 * Maybe we should be using a slab cache for this, especially if
2736 * we have a large number of ports to handle.
2738 drv->state = kcalloc(drv->nr, sizeof(struct uart_state), GFP_KERNEL);
2742 normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
2743 TTY_DRIVER_DYNAMIC_DEV);
2744 if (IS_ERR(normal)) {
2745 retval = PTR_ERR(normal);
2749 drv->tty_driver = normal;
2751 normal->driver_name = drv->driver_name;
2752 normal->name = drv->dev_name;
2753 normal->major = drv->major;
2754 normal->minor_start = drv->minor;
2755 normal->type = TTY_DRIVER_TYPE_SERIAL;
2756 normal->subtype = SERIAL_TYPE_NORMAL;
2757 normal->init_termios = tty_std_termios;
2758 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2759 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2760 normal->driver_state = drv;
2761 tty_set_operations(normal, &uart_ops);
2764 * Initialise the UART state(s).
2766 for (i = 0; i < drv->nr; i++) {
2767 struct uart_state *state = drv->state + i;
2768 struct tty_port *port = &state->port;
2770 tty_port_init(port);
2771 port->ops = &uart_port_ops;
2774 retval = tty_register_driver(normal);
2778 for (i = 0; i < drv->nr; i++)
2779 tty_port_destroy(&drv->state[i].port);
2780 tty_driver_kref_put(normal);
2786 EXPORT_SYMBOL(uart_register_driver);
2789 * uart_unregister_driver - remove a driver from the uart core layer
2790 * @drv: low level driver structure
2792 * Remove all references to a driver from the core driver. The low level
2793 * driver must have removed all its ports via the uart_remove_one_port() if it
2794 * registered them with uart_add_one_port(). (I.e. @drv->port is %NULL.)
2796 * Locking: none, Interrupts: enabled
2798 void uart_unregister_driver(struct uart_driver *drv)
2800 struct tty_driver *p = drv->tty_driver;
2803 tty_unregister_driver(p);
2804 tty_driver_kref_put(p);
2805 for (i = 0; i < drv->nr; i++)
2806 tty_port_destroy(&drv->state[i].port);
2809 drv->tty_driver = NULL;
2811 EXPORT_SYMBOL(uart_unregister_driver);
2813 struct tty_driver *uart_console_device(struct console *co, int *index)
2815 struct uart_driver *p = co->data;
2817 return p->tty_driver;
2819 EXPORT_SYMBOL_GPL(uart_console_device);
2821 static ssize_t uartclk_show(struct device *dev,
2822 struct device_attribute *attr, char *buf)
2824 struct serial_struct tmp;
2825 struct tty_port *port = dev_get_drvdata(dev);
2827 uart_get_info(port, &tmp);
2828 return sprintf(buf, "%d\n", tmp.baud_base * 16);
2831 static ssize_t type_show(struct device *dev,
2832 struct device_attribute *attr, char *buf)
2834 struct serial_struct tmp;
2835 struct tty_port *port = dev_get_drvdata(dev);
2837 uart_get_info(port, &tmp);
2838 return sprintf(buf, "%d\n", tmp.type);
2841 static ssize_t line_show(struct device *dev,
2842 struct device_attribute *attr, char *buf)
2844 struct serial_struct tmp;
2845 struct tty_port *port = dev_get_drvdata(dev);
2847 uart_get_info(port, &tmp);
2848 return sprintf(buf, "%d\n", tmp.line);
2851 static ssize_t port_show(struct device *dev,
2852 struct device_attribute *attr, char *buf)
2854 struct serial_struct tmp;
2855 struct tty_port *port = dev_get_drvdata(dev);
2856 unsigned long ioaddr;
2858 uart_get_info(port, &tmp);
2860 if (HIGH_BITS_OFFSET)
2861 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2862 return sprintf(buf, "0x%lX\n", ioaddr);
2865 static ssize_t irq_show(struct device *dev,
2866 struct device_attribute *attr, char *buf)
2868 struct serial_struct tmp;
2869 struct tty_port *port = dev_get_drvdata(dev);
2871 uart_get_info(port, &tmp);
2872 return sprintf(buf, "%d\n", tmp.irq);
2875 static ssize_t flags_show(struct device *dev,
2876 struct device_attribute *attr, char *buf)
2878 struct serial_struct tmp;
2879 struct tty_port *port = dev_get_drvdata(dev);
2881 uart_get_info(port, &tmp);
2882 return sprintf(buf, "0x%X\n", tmp.flags);
2885 static ssize_t xmit_fifo_size_show(struct device *dev,
2886 struct device_attribute *attr, char *buf)
2888 struct serial_struct tmp;
2889 struct tty_port *port = dev_get_drvdata(dev);
2891 uart_get_info(port, &tmp);
2892 return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
2895 static ssize_t close_delay_show(struct device *dev,
2896 struct device_attribute *attr, char *buf)
2898 struct serial_struct tmp;
2899 struct tty_port *port = dev_get_drvdata(dev);
2901 uart_get_info(port, &tmp);
2902 return sprintf(buf, "%d\n", tmp.close_delay);
2905 static ssize_t closing_wait_show(struct device *dev,
2906 struct device_attribute *attr, char *buf)
2908 struct serial_struct tmp;
2909 struct tty_port *port = dev_get_drvdata(dev);
2911 uart_get_info(port, &tmp);
2912 return sprintf(buf, "%d\n", tmp.closing_wait);
2915 static ssize_t custom_divisor_show(struct device *dev,
2916 struct device_attribute *attr, char *buf)
2918 struct serial_struct tmp;
2919 struct tty_port *port = dev_get_drvdata(dev);
2921 uart_get_info(port, &tmp);
2922 return sprintf(buf, "%d\n", tmp.custom_divisor);
2925 static ssize_t io_type_show(struct device *dev,
2926 struct device_attribute *attr, char *buf)
2928 struct serial_struct tmp;
2929 struct tty_port *port = dev_get_drvdata(dev);
2931 uart_get_info(port, &tmp);
2932 return sprintf(buf, "%d\n", tmp.io_type);
2935 static ssize_t iomem_base_show(struct device *dev,
2936 struct device_attribute *attr, char *buf)
2938 struct serial_struct tmp;
2939 struct tty_port *port = dev_get_drvdata(dev);
2941 uart_get_info(port, &tmp);
2942 return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
2945 static ssize_t iomem_reg_shift_show(struct device *dev,
2946 struct device_attribute *attr, char *buf)
2948 struct serial_struct tmp;
2949 struct tty_port *port = dev_get_drvdata(dev);
2951 uart_get_info(port, &tmp);
2952 return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
2955 static ssize_t console_show(struct device *dev,
2956 struct device_attribute *attr, char *buf)
2958 struct tty_port *port = dev_get_drvdata(dev);
2959 struct uart_state *state = container_of(port, struct uart_state, port);
2960 struct uart_port *uport;
2961 bool console = false;
2963 mutex_lock(&port->mutex);
2964 uport = uart_port_check(state);
2966 console = uart_console_registered(uport);
2967 mutex_unlock(&port->mutex);
2969 return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2972 static ssize_t console_store(struct device *dev,
2973 struct device_attribute *attr, const char *buf, size_t count)
2975 struct tty_port *port = dev_get_drvdata(dev);
2976 struct uart_state *state = container_of(port, struct uart_state, port);
2977 struct uart_port *uport;
2978 bool oldconsole, newconsole;
2981 ret = kstrtobool(buf, &newconsole);
2985 mutex_lock(&port->mutex);
2986 uport = uart_port_check(state);
2988 oldconsole = uart_console_registered(uport);
2989 if (oldconsole && !newconsole) {
2990 ret = unregister_console(uport->cons);
2991 } else if (!oldconsole && newconsole) {
2992 if (uart_console(uport)) {
2993 uport->console_reinit = 1;
2994 register_console(uport->cons);
3002 mutex_unlock(&port->mutex);
3004 return ret < 0 ? ret : count;
3007 static DEVICE_ATTR_RO(uartclk);
3008 static DEVICE_ATTR_RO(type);
3009 static DEVICE_ATTR_RO(line);
3010 static DEVICE_ATTR_RO(port);
3011 static DEVICE_ATTR_RO(irq);
3012 static DEVICE_ATTR_RO(flags);
3013 static DEVICE_ATTR_RO(xmit_fifo_size);
3014 static DEVICE_ATTR_RO(close_delay);
3015 static DEVICE_ATTR_RO(closing_wait);
3016 static DEVICE_ATTR_RO(custom_divisor);
3017 static DEVICE_ATTR_RO(io_type);
3018 static DEVICE_ATTR_RO(iomem_base);
3019 static DEVICE_ATTR_RO(iomem_reg_shift);
3020 static DEVICE_ATTR_RW(console);
3022 static struct attribute *tty_dev_attrs[] = {
3023 &dev_attr_uartclk.attr,
3024 &dev_attr_type.attr,
3025 &dev_attr_line.attr,
3026 &dev_attr_port.attr,
3028 &dev_attr_flags.attr,
3029 &dev_attr_xmit_fifo_size.attr,
3030 &dev_attr_close_delay.attr,
3031 &dev_attr_closing_wait.attr,
3032 &dev_attr_custom_divisor.attr,
3033 &dev_attr_io_type.attr,
3034 &dev_attr_iomem_base.attr,
3035 &dev_attr_iomem_reg_shift.attr,
3036 &dev_attr_console.attr,
3040 static const struct attribute_group tty_dev_attr_group = {
3041 .attrs = tty_dev_attrs,
3045 * uart_add_one_port - attach a driver-defined port structure
3046 * @drv: pointer to the uart low level driver structure for this port
3047 * @uport: uart port structure to use for this port.
3049 * Context: task context, might sleep
3051 * This allows the driver @drv to register its own uart_port structure with the
3052 * core driver. The main purpose is to allow the low level uart drivers to
3053 * expand uart_port, rather than having yet more levels of structures.
3055 int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
3057 struct uart_state *state;
3058 struct tty_port *port;
3060 struct device *tty_dev;
3063 if (uport->line >= drv->nr)
3066 state = drv->state + uport->line;
3067 port = &state->port;
3069 mutex_lock(&port_mutex);
3070 mutex_lock(&port->mutex);
3071 if (state->uart_port) {
3076 /* Link the port to the driver state table and vice versa */
3077 atomic_set(&state->refcount, 1);
3078 init_waitqueue_head(&state->remove_wait);
3079 state->uart_port = uport;
3080 uport->state = state;
3082 state->pm_state = UART_PM_STATE_UNDEFINED;
3083 uport->cons = drv->cons;
3084 uport->minor = drv->tty_driver->minor_start + uport->line;
3085 uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
3086 drv->tty_driver->name_base + uport->line);
3093 * If this port is in use as a console then the spinlock is already
3096 if (!uart_console_registered(uport))
3097 uart_port_spin_lock_init(uport);
3099 if (uport->cons && uport->dev)
3100 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
3102 tty_port_link_device(port, drv->tty_driver, uport->line);
3103 uart_configure_port(drv, state, uport);
3105 port->console = uart_console(uport);
3108 if (uport->attr_group)
3111 uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
3113 if (!uport->tty_groups) {
3117 uport->tty_groups[0] = &tty_dev_attr_group;
3118 if (uport->attr_group)
3119 uport->tty_groups[1] = uport->attr_group;
3122 * Register the port whether it's detected or not. This allows
3123 * setserial to be used to alter this port's parameters.
3125 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
3126 uport->line, uport->dev, port, uport->tty_groups);
3127 if (!IS_ERR(tty_dev)) {
3128 device_set_wakeup_capable(tty_dev, 1);
3130 dev_err(uport->dev, "Cannot register tty device on line %d\n",
3135 * Ensure UPF_DEAD is not set.
3137 uport->flags &= ~UPF_DEAD;
3140 mutex_unlock(&port->mutex);
3141 mutex_unlock(&port_mutex);
3145 EXPORT_SYMBOL(uart_add_one_port);
3148 * uart_remove_one_port - detach a driver defined port structure
3149 * @drv: pointer to the uart low level driver structure for this port
3150 * @uport: uart port structure for this port
3152 * Context: task context, might sleep
3154 * This unhooks (and hangs up) the specified port structure from the core
3155 * driver. No further calls will be made to the low-level code for this port.
3157 int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
3159 struct uart_state *state = drv->state + uport->line;
3160 struct tty_port *port = &state->port;
3161 struct uart_port *uart_port;
3162 struct tty_struct *tty;
3165 mutex_lock(&port_mutex);
3168 * Mark the port "dead" - this prevents any opens from
3169 * succeeding while we shut down the port.
3171 mutex_lock(&port->mutex);
3172 uart_port = uart_port_check(state);
3173 if (uart_port != uport)
3174 dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
3178 mutex_unlock(&port->mutex);
3182 uport->flags |= UPF_DEAD;
3183 mutex_unlock(&port->mutex);
3186 * Remove the devices from the tty layer
3188 tty_port_unregister_device(port, drv->tty_driver, uport->line);
3190 tty = tty_port_tty_get(port);
3192 tty_vhangup(port->tty);
3197 * If the port is used as a console, unregister it
3199 if (uart_console(uport))
3200 unregister_console(uport->cons);
3203 * Free the port IO and memory resources, if any.
3205 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
3206 uport->ops->release_port(uport);
3207 kfree(uport->tty_groups);
3211 * Indicate that there isn't a port here anymore.
3213 uport->type = PORT_UNKNOWN;
3215 mutex_lock(&port->mutex);
3216 WARN_ON(atomic_dec_return(&state->refcount) < 0);
3217 wait_event(state->remove_wait, !atomic_read(&state->refcount));
3218 state->uart_port = NULL;
3219 mutex_unlock(&port->mutex);
3221 mutex_unlock(&port_mutex);
3225 EXPORT_SYMBOL(uart_remove_one_port);
3228 * uart_match_port - are the two ports equivalent?
3229 * @port1: first port
3230 * @port2: second port
3232 * This utility function can be used to determine whether two uart_port
3233 * structures describe the same port.
3235 bool uart_match_port(const struct uart_port *port1,
3236 const struct uart_port *port2)
3238 if (port1->iotype != port2->iotype)
3241 switch (port1->iotype) {
3243 return port1->iobase == port2->iobase;
3245 return port1->iobase == port2->iobase &&
3246 port1->hub6 == port2->hub6;
3253 return port1->mapbase == port2->mapbase;
3258 EXPORT_SYMBOL(uart_match_port);
3261 * uart_handle_dcd_change - handle a change of carrier detect state
3262 * @uport: uart_port structure for the open port
3263 * @active: new carrier detect status
3265 * Caller must hold uport->lock.
3267 void uart_handle_dcd_change(struct uart_port *uport, bool active)
3269 struct tty_port *port = &uport->state->port;
3270 struct tty_struct *tty = port->tty;
3271 struct tty_ldisc *ld;
3273 lockdep_assert_held_once(&uport->lock);
3276 ld = tty_ldisc_ref(tty);
3278 if (ld->ops->dcd_change)
3279 ld->ops->dcd_change(tty, active);
3280 tty_ldisc_deref(ld);
3284 uport->icount.dcd++;
3286 if (uart_dcd_enabled(uport)) {
3288 wake_up_interruptible(&port->open_wait);
3293 EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3296 * uart_handle_cts_change - handle a change of clear-to-send state
3297 * @uport: uart_port structure for the open port
3298 * @active: new clear-to-send status
3300 * Caller must hold uport->lock.
3302 void uart_handle_cts_change(struct uart_port *uport, bool active)
3304 lockdep_assert_held_once(&uport->lock);
3306 uport->icount.cts++;
3308 if (uart_softcts_mode(uport)) {
3309 if (uport->hw_stopped) {
3311 uport->hw_stopped = false;
3312 uport->ops->start_tx(uport);
3313 uart_write_wakeup(uport);
3317 uport->hw_stopped = true;
3318 uport->ops->stop_tx(uport);
3324 EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3327 * uart_insert_char - push a char to the uart layer
3329 * User is responsible to call tty_flip_buffer_push when they are done with
3332 * @port: corresponding port
3333 * @status: state of the serial port RX buffer (LSR for 8250)
3334 * @overrun: mask of overrun bits in @status
3335 * @ch: character to push
3336 * @flag: flag for the character (see TTY_NORMAL and friends)
3338 void uart_insert_char(struct uart_port *port, unsigned int status,
3339 unsigned int overrun, unsigned int ch, unsigned int flag)
3341 struct tty_port *tport = &port->state->port;
3343 if ((status & port->ignore_status_mask & ~overrun) == 0)
3344 if (tty_insert_flip_char(tport, ch, flag) == 0)
3345 ++port->icount.buf_overrun;
3348 * Overrun is special. Since it's reported immediately,
3349 * it doesn't affect the current character.
3351 if (status & ~port->ignore_status_mask & overrun)
3352 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3353 ++port->icount.buf_overrun;
3355 EXPORT_SYMBOL_GPL(uart_insert_char);
3357 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
3358 static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
3360 static void uart_sysrq_on(struct work_struct *w)
3362 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3364 sysrq_toggle_support(1);
3365 pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3366 sysrq_toggle_seq_len, sysrq_toggle_seq);
3368 static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
3371 * uart_try_toggle_sysrq - Enables SysRq from serial line
3372 * @port: uart_port structure where char(s) after BREAK met
3373 * @ch: new character in the sequence after received BREAK
3375 * Enables magic SysRq when the required sequence is met on port
3376 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3378 * Returns: %false if @ch is out of enabling sequence and should be
3379 * handled some other way, %true if @ch was consumed.
3381 bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
3383 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3385 if (!sysrq_toggle_seq_len)
3388 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
3389 if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
3390 port->sysrq_seq = 0;
3394 if (++port->sysrq_seq < sysrq_toggle_seq_len) {
3395 port->sysrq = jiffies + SYSRQ_TIMEOUT;
3399 schedule_work(&sysrq_enable_work);
3404 EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
3408 * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3409 * @port: uart device's target port
3411 * This function implements the device tree binding described in
3412 * Documentation/devicetree/bindings/serial/rs485.txt.
3414 int uart_get_rs485_mode(struct uart_port *port)
3416 struct serial_rs485 *rs485conf = &port->rs485;
3417 struct device *dev = port->dev;
3420 int rx_during_tx_gpio_flag;
3422 ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3425 rs485conf->delay_rts_before_send = rs485_delay[0];
3426 rs485conf->delay_rts_after_send = rs485_delay[1];
3428 rs485conf->delay_rts_before_send = 0;
3429 rs485conf->delay_rts_after_send = 0;
3432 uart_sanitize_serial_rs485_delays(port, rs485conf);
3435 * Clear full-duplex and enabled flags, set RTS polarity to active high
3436 * to get to a defined state with the following properties:
3438 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3439 SER_RS485_TERMINATE_BUS |
3440 SER_RS485_RTS_AFTER_SEND);
3441 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
3443 if (device_property_read_bool(dev, "rs485-rx-during-tx"))
3444 rs485conf->flags |= SER_RS485_RX_DURING_TX;
3446 if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
3447 rs485conf->flags |= SER_RS485_ENABLED;
3449 if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3450 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3451 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3455 * Disabling termination by default is the safe choice: Else if many
3456 * bus participants enable it, no communication is possible at all.
3457 * Works fine for short cables and users may enable for longer cables.
3459 port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3461 if (IS_ERR(port->rs485_term_gpio)) {
3462 ret = PTR_ERR(port->rs485_term_gpio);
3463 port->rs485_term_gpio = NULL;
3464 return dev_err_probe(dev, ret, "Cannot get rs485-term-gpios\n");
3466 if (port->rs485_term_gpio)
3467 port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS;
3469 rx_during_tx_gpio_flag = (rs485conf->flags & SER_RS485_RX_DURING_TX) ?
3470 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
3471 port->rs485_rx_during_tx_gpio = devm_gpiod_get_optional(dev,
3472 "rs485-rx-during-tx",
3473 rx_during_tx_gpio_flag);
3474 if (IS_ERR(port->rs485_rx_during_tx_gpio)) {
3475 ret = PTR_ERR(port->rs485_rx_during_tx_gpio);
3476 port->rs485_rx_during_tx_gpio = NULL;
3477 return dev_err_probe(dev, ret, "Cannot get rs485-rx-during-tx-gpios\n");
3482 EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
3484 /* Compile-time assertions for serial_rs485 layout */
3485 static_assert(offsetof(struct serial_rs485, padding) ==
3486 (offsetof(struct serial_rs485, delay_rts_after_send) + sizeof(__u32)));
3487 static_assert(offsetof(struct serial_rs485, padding1) ==
3488 offsetof(struct serial_rs485, padding[1]));
3489 static_assert((offsetof(struct serial_rs485, padding[4]) + sizeof(__u32)) ==
3490 sizeof(struct serial_rs485));
3492 MODULE_DESCRIPTION("Serial driver core");
3493 MODULE_LICENSE("GPL");